EvalState::copyPathToStore(): Return a StorePath

This commit is contained in:
Eelco Dolstra 2022-12-20 14:58:39 +01:00
parent 845fc3f605
commit bda879170f
3 changed files with 17 additions and 16 deletions

View file

@ -2250,7 +2250,7 @@ BackedStringView EvalState::coerceToString(const PosIdx pos, Value & v, PathSet
if (canonicalizePath) if (canonicalizePath)
path = canonPath(*path); path = canonPath(*path);
if (copyToStore) if (copyToStore)
path = copyPathToStore(context, std::move(path).toOwned()); path = store->printStorePath(copyPathToStore(context, std::move(path).toOwned()));
return path; return path;
} }
@ -2293,26 +2293,26 @@ BackedStringView EvalState::coerceToString(const PosIdx pos, Value & v, PathSet
} }
std::string EvalState::copyPathToStore(PathSet & context, const Path & path) StorePath EvalState::copyPathToStore(PathSet & context, const Path & path)
{ {
if (nix::isDerivation(path)) if (nix::isDerivation(path))
throwEvalError("file names are not allowed to end in '%1%'", drvExtension); throwEvalError("file names are not allowed to end in '%1%'", drvExtension);
Path dstPath; auto dstPath = [&]() -> StorePath
{
auto i = srcToStore.find(path); auto i = srcToStore.find(path);
if (i != srcToStore.end()) if (i != srcToStore.end()) return i->second;
dstPath = store->printStorePath(i->second);
else { auto dstPath = settings.readOnlyMode
auto p = settings.readOnlyMode
? store->computeStorePathForPath(std::string(baseNameOf(path)), checkSourcePath(path)).first ? store->computeStorePathForPath(std::string(baseNameOf(path)), checkSourcePath(path)).first
: store->addToStore(std::string(baseNameOf(path)), checkSourcePath(path), FileIngestionMethod::Recursive, htSHA256, defaultPathFilter, repair); : store->addToStore(std::string(baseNameOf(path)), checkSourcePath(path), FileIngestionMethod::Recursive, htSHA256, defaultPathFilter, repair);
dstPath = store->printStorePath(p); allowPath(dstPath);
allowPath(p); srcToStore.insert_or_assign(path, dstPath);
srcToStore.insert_or_assign(path, std::move(p)); printMsg(lvlChatty, "copied source '%1%' -> '%2%'", path, store->printStorePath(dstPath));
printMsg(lvlChatty, "copied source '%1%' -> '%2%'", path, dstPath); return dstPath;
} }();
context.insert(dstPath); context.insert(store->printStorePath(dstPath));
return dstPath; return dstPath;
} }

View file

@ -400,7 +400,7 @@ public:
bool coerceMore = false, bool copyToStore = true, bool coerceMore = false, bool copyToStore = true,
bool canonicalizePath = true); bool canonicalizePath = true);
std::string copyPathToStore(PathSet & context, const Path & path); StorePath copyPathToStore(PathSet & context, const Path & path);
/* Path coercion. Converts strings, paths and derivations to a /* Path coercion. Converts strings, paths and derivations to a
path. The result is guaranteed to be a canonicalised, absolute path. The result is guaranteed to be a canonicalised, absolute

View file

@ -1,6 +1,7 @@
#include "value-to-json.hh" #include "value-to-json.hh"
#include "eval-inline.hh" #include "eval-inline.hh"
#include "util.hh" #include "util.hh"
#include "store-api.hh"
#include <cstdlib> #include <cstdlib>
#include <iomanip> #include <iomanip>
@ -35,7 +36,7 @@ json printValueAsJSON(EvalState & state, bool strict,
case nPath: case nPath:
if (copyToStore) if (copyToStore)
out = state.copyPathToStore(context, v.path); out = state.store->printStorePath(state.copyPathToStore(context, v.path));
else else
out = v.path; out = v.path;
break; break;