1
0
Fork 0
mirror of https://github.com/NixOS/nix synced 2024-09-19 10:50:24 -04:00

Clean up ValidPathInfo::isContentAddressed with std::visit

This commit is contained in:
John Ericson 2020-06-02 17:04:21 +00:00
parent 25e61812f3
commit a33270ce1d
2 changed files with 24 additions and 24 deletions

View file

@ -24,6 +24,7 @@ std::string makeFixedOutputCA(FileIngestionMethod method, const Hash & hash)
+ hash.to_string(); + hash.to_string();
} }
// FIXME Put this somewhere?
template<class... Ts> struct overloaded : Ts... { using Ts::operator()...; }; template<class... Ts> struct overloaded : Ts... { using Ts::operator()...; };
template<class... Ts> overloaded(Ts...) -> overloaded<Ts...>; template<class... Ts> overloaded(Ts...) -> overloaded<Ts...>;

View file

@ -753,36 +753,35 @@ void ValidPathInfo::sign(const Store & store, const SecretKey & secretKey)
sigs.insert(secretKey.signDetached(fingerprint(store))); sigs.insert(secretKey.signDetached(fingerprint(store)));
} }
// FIXME Put this somewhere?
template<class... Ts> struct overloaded : Ts... { using Ts::operator()...; };
template<class... Ts> overloaded(Ts...) -> overloaded<Ts...>;
bool ValidPathInfo::isContentAddressed(const Store & store) const bool ValidPathInfo::isContentAddressed(const Store & store) const
{ {
auto warn = [&]() { if (! ca) return false;
printError("warning: path '%s' claims to be content-addressed but isn't", store.printStorePath(path));
};
if (! ca) {} auto caPath = std::visit(overloaded {
[&](TextHash th) {
else if (auto p = std::get_if<TextHash>(&*ca)) { return store.makeTextPath(path.name(), th.hash, references);
if (store.makeTextPath(path.name(), p->hash, references) == path) },
return true; [&](FileSystemHash fsh) {
else auto refs = cloneStorePathSet(references);
warn(); bool hasSelfReference = false;
} if (refs.count(path)) {
hasSelfReference = true;
else if (auto p = std::get_if<FileSystemHash>(&*ca)) { refs.erase(path);
auto refs = cloneStorePathSet(references); }
bool hasSelfReference = false; return store.makeFixedOutputPath(fsh.method, fsh.hash, path.name(), refs, hasSelfReference);
if (refs.count(path)) {
hasSelfReference = true;
refs.erase(path);
} }
if (store.makeFixedOutputPath(p->method, p->hash, path.name(), refs, hasSelfReference) == path) }, *ca);
return true;
else
warn();
}
return false; bool res = caPath == path;
if (!res)
printError("warning: path '%s' claims to be content-addressed but isn't", store.printStorePath(path));
return res;
} }