Skip to content

Commit

Permalink
Merge branch 'substitute-fetchers' into ipfs-git-ipld
Browse files Browse the repository at this point in the history
  • Loading branch information
matthewbauer committed Jun 22, 2020
2 parents f85fc65 + 74b83c4 commit 62d6ee0
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/libexpr/primops/fetchTree.cc
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,20 @@ static void fetch(EvalState & state, const Pos & pos, Value * * args, Value & v,
if (evalSettings.pureEval && !expectedHash)
throw Error("in pure evaluation mode, '%s' requires a 'sha256' argument", who);

// try to substitute if we can
if (settings.useSubstitutes && expectedHash) {
auto substitutableStorePath = fetchers::trySubstitute(state.store,
unpack ? FileIngestionMethod::Recursive : FileIngestionMethod::Flat, *expectedHash, name);
if (substitutableStorePath) {
auto substitutablePath = state.store->toRealPath(*substitutableStorePath);
if (state.allowedPaths)
state.allowedPaths->insert(substitutablePath);

mkString(v, substitutablePath, PathSet({substitutablePath}));
return;
}
}

auto storePath =
unpack
? fetchers::downloadTarball(state.store, *url, name, (bool) expectedHash).storePath
Expand Down
18 changes: 18 additions & 0 deletions src/libfetchers/fetchers.cc
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,22 @@ std::pair<Tree, std::shared_ptr<const Input>> Input::fetchTree(ref<Store> store)
return {std::move(tree), input};
}

std::optional<StorePath> trySubstitute(ref<Store> store, FileIngestionMethod ingestionMethod,
Hash hash, std::string_view name)
{
auto substitutablePath = store->makeFixedOutputPath(ingestionMethod, hash, name);

try {
store->ensurePath(substitutablePath);

debug("using substituted path '%s'", store->printStorePath(substitutablePath));

return substitutablePath;
} catch (Error & e) {
debug("substitution of path '%s' failed: %s", store->printStorePath(substitutablePath), e.what());
}

return std::nullopt;
}

}
3 changes: 3 additions & 0 deletions src/libfetchers/fetchers.hh
Original file line number Diff line number Diff line change
Expand Up @@ -100,4 +100,7 @@ Tree downloadTarball(
const std::string & name,
bool immutable);

std::optional<StorePath> trySubstitute(ref<Store> store, FileIngestionMethod ingestionMethod,
Hash hash, std::string_view name);

}

0 comments on commit 62d6ee0

Please sign in to comment.