From a33270ce1d815069d3fec43225a10e33c8e94287 Mon Sep 17 00:00:00 2001 From: John Ericson Date: Tue, 2 Jun 2020 17:04:21 +0000 Subject: [PATCH] Clean up `ValidPathInfo::isContentAddressed` with `std::visit` --- src/libstore/file-hash.cc | 1 + src/libstore/store-api.cc | 47 +++++++++++++++++++-------------------- 2 files changed, 24 insertions(+), 24 deletions(-) diff --git a/src/libstore/file-hash.cc b/src/libstore/file-hash.cc index 26153a424..4a3280653 100644 --- a/src/libstore/file-hash.cc +++ b/src/libstore/file-hash.cc @@ -24,6 +24,7 @@ std::string makeFixedOutputCA(FileIngestionMethod method, const Hash & hash) + hash.to_string(); } +// FIXME Put this somewhere? template struct overloaded : Ts... { using Ts::operator()...; }; template overloaded(Ts...) -> overloaded; 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 struct overloaded : Ts... { using Ts::operator()...; }; +template overloaded(Ts...) -> overloaded; 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) {} - - else if (auto p = std::get_if(&*ca)) { - if (store.makeTextPath(path.name(), p->hash, references) == path) - return true; - else - warn(); - } - - else if (auto p = std::get_if(&*ca)) { - auto refs = cloneStorePathSet(references); - bool hasSelfReference = false; - if (refs.count(path)) { - hasSelfReference = true; - refs.erase(path); + 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); } - if (store.makeFixedOutputPath(p->method, p->hash, path.name(), refs, hasSelfReference) == path) - return true; - else - warn(); - } + }, *ca); - 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; }