From 0e705391dbe5340154d59106e591671923d107d0 Mon Sep 17 00:00:00 2001 From: Eelco Dolstra Date: Sun, 24 Sep 2006 18:23:32 +0000 Subject: [PATCH] * Primop `toPath' to convert a string to a path. * Primop `pathExists' to check for path existence. --- src/libexpr/primops.cc | 33 +++++++++++++++++++++++------ tests/lang/eval-okay-pathexists.exp | 1 + tests/lang/eval-okay-pathexists.nix | 3 +++ 3 files changed, 30 insertions(+), 7 deletions(-) create mode 100644 tests/lang/eval-okay-pathexists.exp create mode 100644 tests/lang/eval-okay-pathexists.nix diff --git a/src/libexpr/primops.cc b/src/libexpr/primops.cc index 5c26233e8..96ce4f71e 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 000000000..2015847b6 --- /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 000000000..cabd6d9c7 --- /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