mirror of
https://github.com/NixOS/nix.git
synced 2025-11-08 19:46:02 +01:00
Merge pull request #14249 from NixOS/more-to-real-path-cleanups
More toRealPath cleanups
This commit is contained in:
commit
6dcc468253
13 changed files with 47 additions and 38 deletions
|
|
@ -588,7 +588,11 @@ static void fetch(
|
||||||
|
|
||||||
if (expectedHash) {
|
if (expectedHash) {
|
||||||
auto hash = unpack ? state.store->queryPathInfo(storePath)->narHash
|
auto hash = unpack ? state.store->queryPathInfo(storePath)->narHash
|
||||||
: hashFile(HashAlgorithm::SHA256, state.store->toRealPath(storePath));
|
: hashPath(
|
||||||
|
{state.store->requireStoreObjectAccessor(storePath)},
|
||||||
|
FileSerialisationMethod::Flat,
|
||||||
|
HashAlgorithm::SHA256)
|
||||||
|
.hash;
|
||||||
if (hash != *expectedHash) {
|
if (hash != *expectedHash) {
|
||||||
state
|
state
|
||||||
.error<EvalError>(
|
.error<EvalError>(
|
||||||
|
|
|
||||||
|
|
@ -332,8 +332,7 @@ std::pair<ref<SourceAccessor>, Input> Input::getAccessorUnchecked(ref<Store> sto
|
||||||
|
|
||||||
debug("using substituted/cached input '%s' in '%s'", to_string(), store->printStorePath(storePath));
|
debug("using substituted/cached input '%s' in '%s'", to_string(), store->printStorePath(storePath));
|
||||||
|
|
||||||
// We just ensured the store object was there
|
auto accessor = store->requireStoreObjectAccessor(storePath);
|
||||||
auto accessor = ref{store->getFSAccessor(storePath)};
|
|
||||||
|
|
||||||
accessor->fingerprint = getFingerprint(store);
|
accessor->fingerprint = getFingerprint(store);
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -399,7 +399,8 @@ struct GitHubInputScheme : GitArchiveInputScheme
|
||||||
Headers headers = makeHeadersWithAuthTokens(*input.settings, host, input);
|
Headers headers = makeHeadersWithAuthTokens(*input.settings, host, input);
|
||||||
|
|
||||||
auto downloadResult = downloadFile(store, *input.settings, url, "source", headers);
|
auto downloadResult = downloadFile(store, *input.settings, url, "source", headers);
|
||||||
auto json = nlohmann::json::parse(store->getFSAccessor(downloadResult.storePath)->readFile(CanonPath::root));
|
auto json = nlohmann::json::parse(
|
||||||
|
store->requireStoreObjectAccessor(downloadResult.storePath)->readFile(CanonPath::root));
|
||||||
|
|
||||||
return RefInfo{
|
return RefInfo{
|
||||||
.rev = Hash::parseAny(std::string{json["sha"]}, HashAlgorithm::SHA1),
|
.rev = Hash::parseAny(std::string{json["sha"]}, HashAlgorithm::SHA1),
|
||||||
|
|
@ -473,7 +474,8 @@ struct GitLabInputScheme : GitArchiveInputScheme
|
||||||
Headers headers = makeHeadersWithAuthTokens(*input.settings, host, input);
|
Headers headers = makeHeadersWithAuthTokens(*input.settings, host, input);
|
||||||
|
|
||||||
auto downloadResult = downloadFile(store, *input.settings, url, "source", headers);
|
auto downloadResult = downloadFile(store, *input.settings, url, "source", headers);
|
||||||
auto json = nlohmann::json::parse(store->getFSAccessor(downloadResult.storePath)->readFile(CanonPath::root));
|
auto json = nlohmann::json::parse(
|
||||||
|
store->requireStoreObjectAccessor(downloadResult.storePath)->readFile(CanonPath::root));
|
||||||
|
|
||||||
if (json.is_array() && json.size() >= 1 && json[0]["id"] != nullptr) {
|
if (json.is_array() && json.size() >= 1 && json[0]["id"] != nullptr) {
|
||||||
return RefInfo{.rev = Hash::parseAny(std::string(json[0]["id"]), HashAlgorithm::SHA1)};
|
return RefInfo{.rev = Hash::parseAny(std::string(json[0]["id"]), HashAlgorithm::SHA1)};
|
||||||
|
|
@ -549,7 +551,7 @@ struct SourceHutInputScheme : GitArchiveInputScheme
|
||||||
std::string refUri;
|
std::string refUri;
|
||||||
if (ref == "HEAD") {
|
if (ref == "HEAD") {
|
||||||
auto downloadFileResult = downloadFile(store, *input.settings, fmt("%s/HEAD", base_url), "source", headers);
|
auto downloadFileResult = downloadFile(store, *input.settings, fmt("%s/HEAD", base_url), "source", headers);
|
||||||
auto contents = nix::ref(store->getFSAccessor(downloadFileResult.storePath))->readFile(CanonPath::root);
|
auto contents = store->requireStoreObjectAccessor(downloadFileResult.storePath)->readFile(CanonPath::root);
|
||||||
|
|
||||||
auto remoteLine = git::parseLsRemoteLine(getLine(contents).first);
|
auto remoteLine = git::parseLsRemoteLine(getLine(contents).first);
|
||||||
if (!remoteLine) {
|
if (!remoteLine) {
|
||||||
|
|
@ -563,7 +565,7 @@ struct SourceHutInputScheme : GitArchiveInputScheme
|
||||||
|
|
||||||
auto downloadFileResult =
|
auto downloadFileResult =
|
||||||
downloadFile(store, *input.settings, fmt("%s/info/refs", base_url), "source", headers);
|
downloadFile(store, *input.settings, fmt("%s/info/refs", base_url), "source", headers);
|
||||||
auto contents = nix::ref(store->getFSAccessor(downloadFileResult.storePath))->readFile(CanonPath::root);
|
auto contents = store->requireStoreObjectAccessor(downloadFileResult.storePath)->readFile(CanonPath::root);
|
||||||
std::istringstream is(contents);
|
std::istringstream is(contents);
|
||||||
|
|
||||||
std::string line;
|
std::string line;
|
||||||
|
|
|
||||||
|
|
@ -329,9 +329,7 @@ struct MercurialInputScheme : InputScheme
|
||||||
Input input(_input);
|
Input input(_input);
|
||||||
|
|
||||||
auto storePath = fetchToStore(store, input);
|
auto storePath = fetchToStore(store, input);
|
||||||
|
auto accessor = store->requireStoreObjectAccessor(storePath);
|
||||||
// We just added it, it should be there.
|
|
||||||
auto accessor = ref{store->getFSAccessor(storePath)};
|
|
||||||
|
|
||||||
accessor->setPathDisplay("«" + input.to_string() + "»");
|
accessor->setPathDisplay("«" + input.to_string() + "»");
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -138,7 +138,7 @@ struct PathInputScheme : InputScheme
|
||||||
storePath = store->addToStoreFromDump(*src, "source");
|
storePath = store->addToStoreFromDump(*src, "source");
|
||||||
}
|
}
|
||||||
|
|
||||||
auto accessor = ref{store->getFSAccessor(*storePath)};
|
auto accessor = store->requireStoreObjectAccessor(*storePath);
|
||||||
|
|
||||||
// To prevent `fetchToStore()` copying the path again to Nix
|
// To prevent `fetchToStore()` copying the path again to Nix
|
||||||
// store, pre-create an entry in the fetcher cache.
|
// store, pre-create an entry in the fetcher cache.
|
||||||
|
|
|
||||||
|
|
@ -529,15 +529,9 @@ bool Worker::pathContentsGood(const StorePath & path)
|
||||||
return i->second;
|
return i->second;
|
||||||
printInfo("checking path '%s'...", store.printStorePath(path));
|
printInfo("checking path '%s'...", store.printStorePath(path));
|
||||||
auto info = store.queryPathInfo(path);
|
auto info = store.queryPathInfo(path);
|
||||||
bool res;
|
bool res = false;
|
||||||
if (!pathExists(store.printStorePath(path)))
|
if (auto accessor = store.getFSAccessor(path, /*requireValidPath=*/false)) {
|
||||||
res = false;
|
auto current = hashPath({ref{accessor}}, FileIngestionMethod::NixArchive, info->narHash.algo).first;
|
||||||
else {
|
|
||||||
auto current = hashPath(
|
|
||||||
{store.getFSAccessor(), CanonPath(path.to_string())},
|
|
||||||
FileIngestionMethod::NixArchive,
|
|
||||||
info->narHash.algo)
|
|
||||||
.first;
|
|
||||||
Hash nullHash(HashAlgorithm::SHA256);
|
Hash nullHash(HashAlgorithm::SHA256);
|
||||||
res = info->narHash == nullHash || info->narHash == current;
|
res = info->narHash == nullHash || info->narHash == current;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -724,10 +724,28 @@ public:
|
||||||
* the Nix store.
|
* the Nix store.
|
||||||
*
|
*
|
||||||
* @return nullptr if the store doesn't contain an object at the
|
* @return nullptr if the store doesn't contain an object at the
|
||||||
* givine path.
|
* given path.
|
||||||
*/
|
*/
|
||||||
virtual std::shared_ptr<SourceAccessor> getFSAccessor(const StorePath & path, bool requireValidPath = true) = 0;
|
virtual std::shared_ptr<SourceAccessor> getFSAccessor(const StorePath & path, bool requireValidPath = true) = 0;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get an accessor for the store object or throw an Error if it's invalid or
|
||||||
|
* doesn't exist.
|
||||||
|
*
|
||||||
|
* @throws InvalidPath if the store object doesn't exist or (if requireValidPath = true) is
|
||||||
|
* invalid.
|
||||||
|
*/
|
||||||
|
[[nodiscard]] ref<SourceAccessor> requireStoreObjectAccessor(const StorePath & path, bool requireValidPath = true)
|
||||||
|
{
|
||||||
|
auto accessor = getFSAccessor(path, requireValidPath);
|
||||||
|
if (!accessor) {
|
||||||
|
throw InvalidPath(
|
||||||
|
requireValidPath ? "path '%1%' is not a valid store path" : "store path '%1%' does not exist",
|
||||||
|
printStorePath(path));
|
||||||
|
}
|
||||||
|
return ref<SourceAccessor>{accessor};
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Repair the contents of the given path by redownloading it using
|
* Repair the contents of the given path by redownloading it using
|
||||||
* a substituter (if available).
|
* a substituter (if available).
|
||||||
|
|
|
||||||
|
|
@ -1130,7 +1130,7 @@ Derivation Store::derivationFromPath(const StorePath & drvPath)
|
||||||
|
|
||||||
static Derivation readDerivationCommon(Store & store, const StorePath & drvPath, bool requireValidPath)
|
static Derivation readDerivationCommon(Store & store, const StorePath & drvPath, bool requireValidPath)
|
||||||
{
|
{
|
||||||
auto accessor = store.getFSAccessor(drvPath, requireValidPath);
|
auto accessor = store.requireStoreObjectAccessor(drvPath, requireValidPath);
|
||||||
try {
|
try {
|
||||||
return parseDerivation(store, accessor->readFile(CanonPath::root), Derivation::nameFromPath(drvPath));
|
return parseDerivation(store, accessor->readFile(CanonPath::root), Derivation::nameFromPath(drvPath));
|
||||||
} catch (FormatError & e) {
|
} catch (FormatError & e) {
|
||||||
|
|
|
||||||
|
|
@ -41,10 +41,7 @@ struct CmdCatStore : StoreCommand, MixCat
|
||||||
void run(ref<Store> store) override
|
void run(ref<Store> store) override
|
||||||
{
|
{
|
||||||
auto [storePath, rest] = store->toStorePath(path);
|
auto [storePath, rest] = store->toStorePath(path);
|
||||||
auto accessor = store->getFSAccessor(storePath);
|
cat(store->requireStoreObjectAccessor(storePath), CanonPath{rest});
|
||||||
if (!accessor)
|
|
||||||
throw InvalidPath("path '%1%' is not a valid store path", store->printStorePath(storePath));
|
|
||||||
cat(ref{std::move(accessor)}, CanonPath{rest});
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -299,11 +299,9 @@ static StorePath getDerivationEnvironment(ref<Store> store, ref<Store> evalStore
|
||||||
|
|
||||||
for (auto & [_0, optPath] : evalStore->queryPartialDerivationOutputMap(shellDrvPath)) {
|
for (auto & [_0, optPath] : evalStore->queryPartialDerivationOutputMap(shellDrvPath)) {
|
||||||
assert(optPath);
|
assert(optPath);
|
||||||
auto & outPath = *optPath;
|
auto accessor = evalStore->requireStoreObjectAccessor(*optPath);
|
||||||
assert(store->isValidPath(outPath));
|
if (auto st = accessor->maybeLstat(CanonPath::root); st && st->fileSize.value_or(0))
|
||||||
auto outPathS = store->toRealPath(outPath);
|
return *optPath;
|
||||||
if (lstat(outPathS).st_size)
|
|
||||||
return outPath;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
throw Error("get-env.sh failed to produce an environment");
|
throw Error("get-env.sh failed to produce an environment");
|
||||||
|
|
@ -502,7 +500,9 @@ struct Common : InstallableCommand, MixProfile
|
||||||
|
|
||||||
debug("reading environment file '%s'", strPath);
|
debug("reading environment file '%s'", strPath);
|
||||||
|
|
||||||
return {BuildEnvironment::parseJSON(readFile(store->toRealPath(shellOutPath))), strPath};
|
return {
|
||||||
|
BuildEnvironment::parseJSON(store->requireStoreObjectAccessor(shellOutPath)->readFile(CanonPath::root)),
|
||||||
|
strPath};
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -115,10 +115,7 @@ struct CmdLsStore : StoreCommand, MixLs
|
||||||
void run(ref<Store> store) override
|
void run(ref<Store> store) override
|
||||||
{
|
{
|
||||||
auto [storePath, rest] = store->toStorePath(path);
|
auto [storePath, rest] = store->toStorePath(path);
|
||||||
auto accessor = store->getFSAccessor(storePath);
|
list(store->requireStoreObjectAccessor(storePath), CanonPath{rest});
|
||||||
if (!accessor)
|
|
||||||
throw InvalidPath("path '%1%' is not a valid store path", store->printStorePath(storePath));
|
|
||||||
list(ref{std::move(accessor)}, CanonPath{rest});
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -603,7 +603,7 @@ static void registerValidity(bool reregister, bool hashGiven, bool canonicalise)
|
||||||
#endif
|
#endif
|
||||||
if (!hashGiven) {
|
if (!hashGiven) {
|
||||||
HashResult hash = hashPath(
|
HashResult hash = hashPath(
|
||||||
{ref{store->getFSAccessor(info->path, false)}},
|
{store->requireStoreObjectAccessor(info->path, /*requireValidPath=*/false)},
|
||||||
FileSerialisationMethod::NixArchive,
|
FileSerialisationMethod::NixArchive,
|
||||||
HashAlgorithm::SHA256);
|
HashAlgorithm::SHA256);
|
||||||
info->narHash = hash.hash;
|
info->narHash = hash.hash;
|
||||||
|
|
|
||||||
|
|
@ -207,7 +207,7 @@ struct CmdWhyDepends : SourceExprCommand, MixOperateOnOptions
|
||||||
contain the reference. */
|
contain the reference. */
|
||||||
std::map<std::string, Strings> hits;
|
std::map<std::string, Strings> hits;
|
||||||
|
|
||||||
auto accessor = store->getFSAccessor(node.path);
|
auto accessor = store->requireStoreObjectAccessor(node.path);
|
||||||
|
|
||||||
auto visitPath = [&](this auto && recur, const CanonPath & p) -> void {
|
auto visitPath = [&](this auto && recur, const CanonPath & p) -> void {
|
||||||
auto st = accessor->maybeLstat(p);
|
auto st = accessor->maybeLstat(p);
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue