Add InputAccessor::fetchToStore()

This commit is contained in:
Eelco Dolstra 2022-08-11 20:03:22 +02:00
parent c0d33087c8
commit 2e0d63caf6
6 changed files with 59 additions and 27 deletions

View file

@ -2257,13 +2257,7 @@ StorePath EvalState::copyPathToStore(PathSet & context, const SourcePath & path)
auto dstPath = i != srcToStore.end()
? i->second
: [&]() {
auto source = sinkToSource([&](Sink & sink) {
path.dumpPath(sink);
});
auto dstPath =
settings.readOnlyMode
? store->computeStorePathFromDump(*source, path.baseName()).first
: store->addToStoreFromDump(*source, path.baseName(), FileIngestionMethod::Recursive, htSHA256, repair);
auto dstPath = path.fetchToStore(store, path.baseName(), nullptr, repair);
allowPath(dstPath);
srcToStore.insert_or_assign(path, dstPath);
printMsg(lvlChatty, "copied source '%1%' -> '%2%'", path, store->printStorePath(dstPath));

View file

@ -2006,7 +2006,8 @@ static void addPath(
}
#endif
PathFilter filter = filterFun ? ([&](const Path & p) {
std::unique_ptr<PathFilter> filter;
if (filterFun) filter = std::make_unique<PathFilter>([&](const Path & p) {
SourcePath path2{path.accessor, CanonPath(p)};
auto st = path2.lstat();
@ -2025,7 +2026,7 @@ static void addPath(
state.callFunction(*filterFun, 2, args, res, pos);
return state.forceBool(res, pos);
}) : defaultPathFilter;
});
std::optional<StorePath> expectedStorePath;
if (expectedHash)
@ -2036,13 +2037,10 @@ static void addPath(
// store on-demand.
if (!expectedHash || !state.store->isValidPath(*expectedStorePath)) {
auto source = sinkToSource([&](Sink & sink) {
path.dumpPath(sink, filter);
});
auto dstPath =
settings.readOnlyMode
? state.store->computeStorePathFromDump(*source, name, method, htSHA256, refs).first
: state.store->addToStoreFromDump(*source, name, method, htSHA256, state.repair);
// FIXME
if (method != FileIngestionMethod::Recursive)
throw Error("'recursive = false' is not implemented");
auto dstPath = path.fetchToStore(state.store, name, filter.get(), state.repair);
if (expectedHash && expectedStorePath != dstPath)
state.debugThrowLastTrace(Error("store path mismatch in (possibly filtered) path added from '%s'", path));
state.allowAndSetStorePathString(dstPath, v);

View file

@ -121,16 +121,7 @@ std::pair<StorePath, Input> Input::fetchToStore(ref<Store> store) const
auto [storePath, input] = [&]() -> std::pair<StorePath, Input> {
try {
auto [accessor, input2] = getAccessor(store);
// FIXME: add an optimisation for the case where the
// accessor is an FSInputAccessor pointing to a store
// path.
auto source = sinkToSource([&, accessor{accessor}](Sink & sink) {
accessor->dumpPath(CanonPath::root, sink);
});
auto storePath = store->addToStoreFromDump(*source, input2.getName());
auto storePath = accessor->root().fetchToStore(store, input2.getName());
return {storePath, input2};
} catch (Error & e) {
e.addTrace({}, "while fetching the input '%s'", to_string());

View file

@ -1,5 +1,6 @@
#include "input-accessor.hh"
#include "util.hh"
#include "store-api.hh"
#include <atomic>
@ -85,6 +86,28 @@ void InputAccessor::dumpPath(
dump(path);
}
StorePath InputAccessor::fetchToStore(
ref<Store> store,
const CanonPath & path,
std::string_view name,
PathFilter * filter,
RepairFlag repair)
{
// FIXME: add an optimisation for the case where the accessor is
// an FSInputAccessor pointing to a store path.
auto source = sinkToSource([&](Sink & sink) {
dumpPath(path, sink, filter ? *filter : defaultPathFilter);
});
auto storePath =
settings.readOnlyMode
? store->computeStorePathFromDump(*source, name).first
: store->addToStoreFromDump(*source, name, FileIngestionMethod::Recursive, htSHA256, repair);
return storePath;
}
std::optional<InputAccessor::Stat> InputAccessor::maybeLstat(const CanonPath & path)
{
// FIXME: merge these into one operation.
@ -164,6 +187,15 @@ ref<MemoryInputAccessor> makeMemoryInputAccessor()
return make_ref<MemoryInputAccessorImpl>();
}
StorePath SourcePath::fetchToStore(
ref<Store> store,
std::string_view name,
PathFilter * filter,
RepairFlag repair) const
{
return accessor->fetchToStore(store, path, name, filter, repair);
}
std::string_view SourcePath::baseName() const
{
return path.baseName().value_or("source");

View file

@ -4,12 +4,16 @@
#include "types.hh"
#include "archive.hh"
#include "canon-path.hh"
#include "repair-flag.hh"
namespace nix {
MakeError(RestrictedPathError, Error);
struct SourcePath;
struct StorePath;
class Store;
enum RepairFlag;
struct InputAccessor : public std::enable_shared_from_this<InputAccessor>
{
@ -52,6 +56,13 @@ struct InputAccessor : public std::enable_shared_from_this<InputAccessor>
Sink & sink,
PathFilter & filter = defaultPathFilter);
StorePath fetchToStore(
ref<Store> store,
const CanonPath & path,
std::string_view name,
PathFilter * filter = nullptr,
RepairFlag repair = NoRepair);
/* Return a corresponding path in the root filesystem, if
possible. This is only possible for inputs that are
materialized in the root filesystem. */
@ -124,6 +135,12 @@ struct SourcePath
PathFilter & filter = defaultPathFilter) const
{ return accessor->dumpPath(path, sink, filter); }
StorePath fetchToStore(
ref<Store> store,
std::string_view name,
PathFilter * filter = nullptr,
RepairFlag repair = NoRepair) const;
std::optional<CanonPath> getPhysicalPath() const
{ return accessor->getPhysicalPath(path); }

View file

@ -8,4 +8,4 @@ libplugintest_ALLOW_UNDEFINED := 1
libplugintest_EXCLUDE_FROM_LIBRARY_LIST := 1
libplugintest_CXXFLAGS := -I src/libutil -I src/libexpr -I src/libfetchers
libplugintest_CXXFLAGS := -I src/libutil -I src/libexpr -I src/libfetchers -I src/libstore