diff options
author | Eelco Dolstra <e.dolstra@tudelft.nl> | 2006-09-24T18·23+0000 |
---|---|---|
committer | Eelco Dolstra <e.dolstra@tudelft.nl> | 2006-09-24T18·23+0000 |
commit | 0e705391dbe5340154d59106e591671923d107d0 (patch) | |
tree | 47a7b7bce403a3b622f59476c143a202d9163ba6 | |
parent | e47e0c2dbe808f299eb912da94640b95954703f7 (diff) |
* Primop `toPath' to convert a string to a path.
* Primop `pathExists' to check for path existence.
-rw-r--r-- | src/libexpr/primops.cc | 33 | ||||
-rw-r--r-- | tests/lang/eval-okay-pathexists.exp | 1 | ||||
-rw-r--r-- | tests/lang/eval-okay-pathexists.nix | 3 |
3 files changed, 30 insertions, 7 deletions
diff --git a/src/libexpr/primops.cc b/src/libexpr/primops.cc index 5c26233e8870..96ce4f71e645 100644 --- a/src/libexpr/primops.cc +++ b/src/libexpr/primops.cc @@ -49,13 +49,6 @@ static Expr primImport(EvalState & state, const ATermVector & args) if (matchPath(arg, arg2)) path = aterm2String(arg2); - else if (matchStr(arg, arg2)) { - path = aterm2String(arg2); - if (path == "" || path[0] != '/') - throw EvalError("`import' requires an absolute path name"); - path = canonPath(path); - } - else if (matchAttrs(arg, es)) { Expr a = queryAttr(arg, "type"); @@ -80,6 +73,17 @@ static Expr primImport(EvalState & state, const ATermVector & args) } +static Expr primPathExists(EvalState & state, const ATermVector & args) +{ + Expr arg = evalExpr(state, args[0]), arg2; + + if (!matchPath(arg, arg2)) + throw TypeError("`pathExists' requires a path as its argument"); + + return makeBool(pathExists(aterm2String(arg2))); +} + + static void flattenList(EvalState & state, Expr e, ATermList & result) { ATermList es; @@ -476,6 +480,19 @@ static Expr primToString(EvalState & state, const ATermVector & args) } +/* Convert the argument to a path. */ +static Expr primToPath(EvalState & state, const ATermVector & args) +{ + Expr e = evalExpr(state, args[0]); + ATerm t = coerceToString(e); + if (!t) throw TypeError(format("cannot coerce %1% to a path in `toPath'") % showType(e)); + Path path = aterm2String(t); + if (path == "" || path[0] != '/') + throw EvalError("string doesn't represent an absolute path in `toPath'"); + return makePath(toATerm(canonPath(path))); +} + + /* Convert the argument (which can be any Nix expression) to an XML representation returned in a string. Not all Nix expressions can be sensibly or completely represented (e.g., functions). */ @@ -809,11 +826,13 @@ void EvalState::addPrimOps() addPrimOp("__currentTime", 0, primCurrentTime); addPrimOp("import", 1, primImport); + addPrimOp("__pathExists", 1, primPathExists); addPrimOp("derivation!", 1, primDerivationStrict); addPrimOp("derivation", 1, primDerivationLazy); addPrimOp("baseNameOf", 1, primBaseNameOf); addPrimOp("dirOf", 1, primDirOf); addPrimOp("toString", 1, primToString); + addPrimOp("__toPath", 1, primToPath); addPrimOp("__toXML", 1, primToXML); addPrimOp("__toFile", 1, primToFile); addPrimOp("isNull", 1, primIsNull); diff --git a/tests/lang/eval-okay-pathexists.exp b/tests/lang/eval-okay-pathexists.exp new file mode 100644 index 000000000000..2015847b65e7 --- /dev/null +++ b/tests/lang/eval-okay-pathexists.exp @@ -0,0 +1 @@ +Bool(True) diff --git a/tests/lang/eval-okay-pathexists.nix b/tests/lang/eval-okay-pathexists.nix new file mode 100644 index 000000000000..cabd6d9c75f3 --- /dev/null +++ b/tests/lang/eval-okay-pathexists.nix @@ -0,0 +1,3 @@ +builtins.pathExists (builtins.toPath ./lib.nix) +&& builtins.pathExists ./lib.nix +&& !builtins.pathExists ./bla.nix |