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) { if (v.type() == nPath) {
auto path = v.path().to_string(); auto path = v.path();
if (canonicalizePath) return copyToStore
// FIXME: unnecessary? ? store->printStorePath(copyPathToStore(context, path))
path = canonPath(path); : path.path;
if (copyToStore)
path = copyPathToStore(context, path);
return path;
} }
if (v.type() == nAttrs) { 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.path))
if (nix::isDerivation(path)) throw EvalError("file names are not allowed to end in '%s'", drvExtension);
throwEvalError("file names are not allowed to end in '%1%'", drvExtension);
Path dstPath;
auto i = srcToStore.find(path); auto i = srcToStore.find(path);
if (i != srcToStore.end())
dstPath = store->printStorePath(i->second); auto dstPath = i != srcToStore.end()
else { ? i->second
// FIXME: use SourcePath : [&]() {
auto path2 = unpackPath(path);
#if 0 #if 0
auto p = settings.readOnlyMode auto p = settings.readOnlyMode
? store->computeStorePathForPath(path2.baseName(), canonPath(path)).first ? store->computeStorePathForPath(path2.baseName(), canonPath(path)).first
: store->addToStore(path2.baseName(), canonPath(path), FileIngestionMethod::Recursive, htSHA256, defaultPathFilter, repair); : store->addToStore(path2.baseName(), canonPath(path), FileIngestionMethod::Recursive, htSHA256, defaultPathFilter, repair);
#endif #endif
auto source = sinkToSource([&](Sink & sink) { auto source = sinkToSource([&](Sink & sink) {
path2.dumpPath(sink); path.dumpPath(sink);
}); });
// FIXME: readOnlyMode // FIXME: readOnlyMode
auto p = store->addToStoreFromDump(*source, path2.baseName(), FileIngestionMethod::Recursive, htSHA256, repair); auto dstPath = store->addToStoreFromDump(*source, path.baseName(), FileIngestionMethod::Recursive, htSHA256, 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;
#endif
abort();
} }

View file

@ -50,11 +50,6 @@ struct Env
void copyContext(const Value & v, PathSet & context); 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::ostream & printValue(const EvalState & state, std::ostream & str, const Value & v);
std::string printValue(const EvalState & state, const Value & v); std::string printValue(const EvalState & state, const Value & v);
@ -112,7 +107,10 @@ public:
RootValue vImportedDrvToDerivation = nullptr; RootValue vImportedDrvToDerivation = nullptr;
private: 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. */ /* A cache from path names to parse trees. */
#if HAVE_BOEHMGC #if HAVE_BOEHMGC
@ -308,7 +306,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 SourcePath & 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

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

View file

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

View file

@ -42,6 +42,16 @@ struct InputAccessor
const Path & path, const Path & path,
Sink & sink, Sink & sink,
PathFilter & filter = defaultPathFilter); 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 struct FSInputAccessor : InputAccessor
@ -94,6 +104,16 @@ struct SourcePath
std::string to_string() const; std::string to_string() const;
SourcePath append(std::string_view s) 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); std::ostream & operator << (std::ostream & str, const SourcePath & path);