Fix copyPathToStore()

This commit is contained in:
Eelco Dolstra 2022-05-09 15:29:42 +02:00
parent e89d3e0edf
commit e7f8aa8bdd
5 changed files with 58 additions and 44 deletions

View file

@ -2027,13 +2027,10 @@ BackedStringView EvalState::coerceToString(const PosIdx pos, Value & v, PathSet
}
if (v.type() == nPath) {
auto path = v.path().to_string();
if (canonicalizePath)
// FIXME: unnecessary?
path = canonPath(path);
if (copyToStore)
path = copyPathToStore(context, path);
return path;
auto path = v.path();
return copyToStore
? store->printStorePath(copyPathToStore(context, path))
: path.path;
}
if (v.type() == nAttrs) {
@ -2075,39 +2072,34 @@ BackedStringView EvalState::coerceToString(const PosIdx pos, Value & v, PathSet
}
std::string EvalState::copyPathToStore(PathSet & context, const Path & path)
StorePath EvalState::copyPathToStore(PathSet & context, const SourcePath & path)
{
#if 0
if (nix::isDerivation(path))
throwEvalError("file names are not allowed to end in '%1%'", drvExtension);
if (nix::isDerivation(path.path))
throw EvalError("file names are not allowed to end in '%s'", drvExtension);
Path dstPath;
auto i = srcToStore.find(path);
if (i != srcToStore.end())
dstPath = store->printStorePath(i->second);
else {
// FIXME: use SourcePath
auto path2 = unpackPath(path);
auto dstPath = i != srcToStore.end()
? i->second
: [&]() {
#if 0
auto p = settings.readOnlyMode
? store->computeStorePathForPath(path2.baseName(), canonPath(path)).first
: store->addToStore(path2.baseName(), canonPath(path), FileIngestionMethod::Recursive, htSHA256, defaultPathFilter, repair);
#endif
auto source = sinkToSource([&](Sink & sink) {
path2.dumpPath(sink);
path.dumpPath(sink);
});
// FIXME: readOnlyMode
auto p = store->addToStoreFromDump(*source, path2.baseName(), FileIngestionMethod::Recursive, htSHA256, repair);
dstPath = store->printStorePath(p);
allowPath(p);
srcToStore.insert_or_assign(path, std::move(p));
printMsg(lvlChatty, "copied source '%1%' -> '%2%'", path, dstPath);
}
context.insert(dstPath);
auto dstPath = store->addToStoreFromDump(*source, path.baseName(), FileIngestionMethod::Recursive, htSHA256, repair);
allowPath(dstPath);
srcToStore.insert_or_assign(path, dstPath);
printMsg(lvlChatty, "copied source '%1%' -> '%2%'", path, store->printStorePath(dstPath));
return dstPath;
}();
context.insert(store->printStorePath(dstPath));
return dstPath;
#endif
abort();
}

View file

@ -50,11 +50,6 @@ struct Env
void copyContext(const Value & v, PathSet & context);
/* Cache for calls to addToStore(); maps source paths to the store
paths. */
typedef std::map<Path, StorePath> SrcToStore;
std::ostream & printValue(const EvalState & state, std::ostream & str, const Value & v);
std::string printValue(const EvalState & state, const Value & v);
@ -112,7 +107,10 @@ public:
RootValue vImportedDrvToDerivation = nullptr;
private:
SrcToStore srcToStore;
/* Cache for calls to addToStore(); maps source paths to the store
paths. */
std::map<SourcePath, StorePath> srcToStore;
/* A cache from path names to parse trees. */
#if HAVE_BOEHMGC
@ -308,7 +306,7 @@ public:
bool coerceMore = false, bool copyToStore = true,
bool canonicalizePath = true);
std::string copyPathToStore(PathSet & context, const Path & path);
StorePath copyPathToStore(PathSet & context, const SourcePath & path);
/* Path coercion. Converts strings, paths and derivations to a
path. The result is guaranteed to be a canonicalised, absolute

View file

@ -1549,7 +1549,8 @@ static void prim_findFile(EvalState & state, const PosIdx pos, Value * * args, V
// FIXME: checkSourcePath?
v.mkPath(state.findFile(searchPath, path, pos));
#endif
abort();
throw ThrownError("findFile('%s'): not implemented", path);
}
static RegisterPrimOp primop_findFile(RegisterPrimOp::Info {

View file

@ -2,6 +2,7 @@
#include "json.hh"
#include "eval-inline.hh"
#include "util.hh"
#include "store-api.hh"
#include <cstdlib>
#include <iomanip>
@ -33,7 +34,9 @@ void printValueAsJSON(EvalState & state, bool strict,
case nPath:
// FIXME: handle accessors
out.write(state.copyPathToStore(context, v.path().path));
out.write(
state.store->printStorePath(
state.copyPathToStore(context, v.path())));
break;
case nNull:

View file

@ -42,6 +42,16 @@ struct InputAccessor
const Path & path,
Sink & sink,
PathFilter & filter = defaultPathFilter);
bool operator == (const InputAccessor & x) const
{
return number == x.number;
}
bool operator < (const InputAccessor & x) const
{
return number < x.number;
}
};
struct FSInputAccessor : InputAccessor
@ -94,6 +104,16 @@ struct SourcePath
std::string to_string() const;
SourcePath append(std::string_view s) const;
bool operator == (const SourcePath & x) const
{
return std::tie(accessor, path) == std::tie(x.accessor, x.path);
}
bool operator < (const SourcePath & x) const
{
return std::tie(accessor, path) < std::tie(x.accessor, x.path);
}
};
std::ostream & operator << (std::ostream & str, const SourcePath & path);