Factor out the path realisation bit of IFD

This commit is contained in:
regnat 2021-12-21 08:42:19 +01:00
parent 6e6e998930
commit cbbd21ec07

View file

@ -76,6 +76,19 @@ void EvalState::realiseContext(const PathSet & context)
} }
} }
static Path realisePath(EvalState & state, const Pos & pos, Value & v, bool requireAbsolutePath = true)
{
PathSet context;
Path path = requireAbsolutePath
? state.coerceToPath(pos, v, context)
: state.coerceToString(pos, v, context, false, false);
state.realiseContext(context);
return state.checkSourcePath(state.toRealPath(path, context));
}
/* Add and attribute to the given attribute map from the output name to /* Add and attribute to the given attribute map from the output name to
the output path, or a placeholder. the output path, or a placeholder.
@ -109,11 +122,9 @@ static void mkOutputString(EvalState & state, Value & v,
argument. */ argument. */
static void import(EvalState & state, const Pos & pos, Value & vPath, Value * vScope, Value & v) static void import(EvalState & state, const Pos & pos, Value & vPath, Value * vScope, Value & v)
{ {
PathSet context; Path path;
Path path = state.coerceToPath(pos, vPath, context);
try { try {
state.realiseContext(context); path = realisePath(state, pos, vPath);
} catch (InvalidPathError & e) { } catch (InvalidPathError & e) {
throw EvalError({ throw EvalError({
.msg = hintfmt("cannot import '%1%', since path '%2%' is not valid", path, e.path), .msg = hintfmt("cannot import '%1%', since path '%2%' is not valid", path, e.path),
@ -124,8 +135,6 @@ static void import(EvalState & state, const Pos & pos, Value & vPath, Value * vS
throw; throw;
} }
Path realPath = state.checkSourcePath(state.toRealPath(path, context));
// FIXME // FIXME
auto isValidDerivationInStore = [&]() -> std::optional<StorePath> { auto isValidDerivationInStore = [&]() -> std::optional<StorePath> {
if (!state.store->isStorePath(path)) if (!state.store->isStorePath(path))
@ -177,7 +186,7 @@ static void import(EvalState & state, const Pos & pos, Value & vPath, Value * vS
else { else {
if (!vScope) if (!vScope)
state.evalFile(realPath, v); state.evalFile(path, v);
else { else {
state.forceAttrs(*vScope); state.forceAttrs(*vScope);
@ -195,8 +204,8 @@ static void import(EvalState & state, const Pos & pos, Value & vPath, Value * vS
// No need to call staticEnv.sort(), because // No need to call staticEnv.sort(), because
// args[0]->attrs is already sorted. // args[0]->attrs is already sorted.
printTalkative("evaluating file '%1%'", realPath); printTalkative("evaluating file '%1%'", path);
Expr * e = state.parseExprFromFile(resolveExprPath(realPath), staticEnv); Expr * e = state.parseExprFromFile(resolveExprPath(path), staticEnv);
e->eval(state, *env, v); e->eval(state, *env, v);
} }
@ -281,22 +290,19 @@ extern "C" typedef void (*ValueInitializer)(EvalState & state, Value & v);
/* Load a ValueInitializer from a DSO and return whatever it initializes */ /* Load a ValueInitializer from a DSO and return whatever it initializes */
void prim_importNative(EvalState & state, const Pos & pos, Value * * args, Value & v) void prim_importNative(EvalState & state, const Pos & pos, Value * * args, Value & v)
{ {
PathSet context; Path path;
Path path = state.coerceToPath(pos, *args[0], context);
try { try {
state.realiseContext(context); path = realisePath(state, pos, *args[0]);
} catch (InvalidPathError & e) { } catch (InvalidPathError & e) {
throw EvalError({ throw EvalError({
.msg = hintfmt( .msg = hintfmt("cannot import '%1%', since path '%2%' is not valid", path, e.path),
"cannot import '%1%', since path '%2%' is not valid",
path, e.path),
.errPos = pos .errPos = pos
}); });
} catch (Error & e) {
e.addTrace(pos, "while importing '%s'", path);
throw;
} }
path = state.checkSourcePath(path);
string sym = state.forceStringNoCtx(*args[1], pos); string sym = state.forceStringNoCtx(*args[1], pos);
void *handle = dlopen(path.c_str(), RTLD_LAZY | RTLD_LOCAL); void *handle = dlopen(path.c_str(), RTLD_LAZY | RTLD_LOCAL);
@ -1349,10 +1355,9 @@ static RegisterPrimOp primop_storePath({
static void prim_pathExists(EvalState & state, const Pos & pos, Value * * args, Value & v) static void prim_pathExists(EvalState & state, const Pos & pos, Value * * args, Value & v)
{ {
PathSet context; Path path;
Path path = state.coerceToPath(pos, *args[0], context);
try { try {
state.realiseContext(context); path = realisePath(state, pos, *args[0]);
} catch (InvalidPathError & e) { } catch (InvalidPathError & e) {
throw EvalError({ throw EvalError({
.msg = hintfmt( .msg = hintfmt(
@ -1363,7 +1368,7 @@ static void prim_pathExists(EvalState & state, const Pos & pos, Value * * args,
} }
try { try {
mkBool(v, pathExists(state.checkSourcePath(path))); mkBool(v, pathExists(path));
} catch (SysError & e) { } catch (SysError & e) {
/* Don't give away info from errors while canonicalising /* Don't give away info from errors while canonicalising
path in restricted mode. */ path in restricted mode. */
@ -1426,17 +1431,16 @@ static RegisterPrimOp primop_dirOf({
/* Return the contents of a file as a string. */ /* Return the contents of a file as a string. */
static void prim_readFile(EvalState & state, const Pos & pos, Value * * args, Value & v) static void prim_readFile(EvalState & state, const Pos & pos, Value * * args, Value & v)
{ {
PathSet context; Path path;
Path path = state.coerceToPath(pos, *args[0], context);
try { try {
state.realiseContext(context); path = realisePath(state, pos, *args[0]);
} catch (InvalidPathError & e) { } catch (InvalidPathError & e) {
throw EvalError({ throw EvalError({
.msg = hintfmt("cannot read '%1%', since path '%2%' is not valid", path, e.path), .msg = hintfmt("cannot read '%1%', since path '%2%' is not valid", path, e.path),
.errPos = pos .errPos = pos
}); });
} }
string s = readFile(state.checkSourcePath(state.toRealPath(path, context))); string s = readFile(path);
if (s.find((char) 0) != string::npos) if (s.find((char) 0) != string::npos)
throw Error("the contents of the file '%1%' cannot be represented as a Nix string", path); throw Error("the contents of the file '%1%' cannot be represented as a Nix string", path);
mkString(v, s.c_str()); mkString(v, s.c_str());
@ -1475,11 +1479,10 @@ static void prim_findFile(EvalState & state, const Pos & pos, Value * * args, Va
pos pos
); );
PathSet context; Path path;
string path = state.coerceToString(pos, *i->value, context, false, false);
try { try {
state.realiseContext(context); path = realisePath(state, pos, *i->value, false);
} catch (InvalidPathError & e) { } catch (InvalidPathError & e) {
throw EvalError({ throw EvalError({
.msg = hintfmt("cannot find '%1%', since path '%2%' is not valid", path, e.path), .msg = hintfmt("cannot find '%1%', since path '%2%' is not valid", path, e.path),
@ -1512,15 +1515,14 @@ static void prim_hashFile(EvalState & state, const Pos & pos, Value * * args, Va
.errPos = pos .errPos = pos
}); });
PathSet context; Path path;
Path path = state.coerceToPath(pos, *args[1], context);
try { try {
state.realiseContext(context); path = realisePath(state, pos, *args[1]);
} catch (InvalidPathError & e) { } catch (InvalidPathError & e) {
throw EvalError("cannot read '%s' since path '%s' is not valid, at %s", path, e.path, pos); throw EvalError("cannot read '%s' since path '%s' is not valid, at %s", path, e.path, pos);
} }
mkString(v, hashFile(*ht, state.checkSourcePath(state.toRealPath(path, context))).to_string(Base16, false)); mkString(v, hashFile(*ht, path).to_string(Base16, false));
} }
static RegisterPrimOp primop_hashFile({ static RegisterPrimOp primop_hashFile({
@ -1537,10 +1539,9 @@ static RegisterPrimOp primop_hashFile({
/* Read a directory (without . or ..) */ /* Read a directory (without . or ..) */
static void prim_readDir(EvalState & state, const Pos & pos, Value * * args, Value & v) static void prim_readDir(EvalState & state, const Pos & pos, Value * * args, Value & v)
{ {
PathSet ctx; Path path;
Path path = state.coerceToPath(pos, *args[0], ctx);
try { try {
state.realiseContext(ctx); path = realisePath(state, pos, *args[0]);
} catch (InvalidPathError & e) { } catch (InvalidPathError & e) {
throw EvalError({ throw EvalError({
.msg = hintfmt("cannot read '%1%', since path '%2%' is not valid", path, e.path), .msg = hintfmt("cannot read '%1%', since path '%2%' is not valid", path, e.path),
@ -1548,7 +1549,7 @@ static void prim_readDir(EvalState & state, const Pos & pos, Value * * args, Val
}); });
} }
DirEntries entries = readDirectory(state.checkSourcePath(path)); DirEntries entries = readDirectory(path);
state.mkAttrs(v, entries.size()); state.mkAttrs(v, entries.size());
for (auto & ent : entries) { for (auto & ent : entries) {