diff options
author | John Ericson <John.Ericson@Obsidian.Systems> | 2020-06-02 17:04:21 +0000 |
---|---|---|
committer | John Ericson <John.Ericson@Obsidian.Systems> | 2020-06-02 17:04:21 +0000 |
commit | a33270ce1d815069d3fec43225a10e33c8e94287 (patch) | |
tree | 00e28ad9068873b657c0c1b8b36de8f772be145e /src/libstore/store-api.cc | |
parent | 25e61812f341a4467ab4fdffb12f63b48a3d0272 (diff) |
Clean up `ValidPathInfo::isContentAddressed` with `std::visit`
Diffstat (limited to 'src/libstore/store-api.cc')
-rw-r--r-- | src/libstore/store-api.cc | 45 |
1 files changed, 22 insertions, 23 deletions
diff --git a/src/libstore/store-api.cc b/src/libstore/store-api.cc index ea30ed105..4bd3121ed 100644 --- a/src/libstore/store-api.cc +++ b/src/libstore/store-api.cc @@ -753,36 +753,35 @@ void ValidPathInfo::sign(const Store & store, const SecretKey & secretKey) 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 { - auto warn = [&]() { - printError("warning: path '%s' claims to be content-addressed but isn't", store.printStorePath(path)); - }; + if (! ca) return false; - if (! ca) {} + auto caPath = std::visit(overloaded { + [&](TextHash th) { + return store.makeTextPath(path.name(), th.hash, references); + }, + [&](FileSystemHash fsh) { + auto refs = cloneStorePathSet(references); + bool hasSelfReference = false; + if (refs.count(path)) { + hasSelfReference = true; + refs.erase(path); + } + return store.makeFixedOutputPath(fsh.method, fsh.hash, path.name(), refs, hasSelfReference); + } + }, *ca); - else if (auto p = std::get_if<TextHash>(&*ca)) { - if (store.makeTextPath(path.name(), p->hash, references) == path) - return true; - else - warn(); - } + bool res = caPath == path; - else if (auto p = std::get_if<FileSystemHash>(&*ca)) { - auto refs = cloneStorePathSet(references); - bool hasSelfReference = false; - if (refs.count(path)) { - hasSelfReference = true; - refs.erase(path); - } - if (store.makeFixedOutputPath(p->method, p->hash, path.name(), refs, hasSelfReference) == path) - return true; - else - warn(); - } + if (!res) + printError("warning: path '%s' claims to be content-addressed but isn't", store.printStorePath(path)); - return false; + return res; } |