From 2eb493ca51e97228a7dc8e28e414df627cb3a329 Mon Sep 17 00:00:00 2001 From: John Ericson Date: Mon, 17 Apr 2023 10:28:54 -0400 Subject: [PATCH] Fix `DerivationOutput::fromJSON` --- src/libstore/derivations.cc | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/src/libstore/derivations.cc b/src/libstore/derivations.cc index 56f30f2e4..fc76ae7ad 100644 --- a/src/libstore/derivations.cc +++ b/src/libstore/derivations.cc @@ -997,15 +997,13 @@ DerivationOutput DerivationOutput::fromJSON( for (const auto & [key, _] : json) keys.insert(key); - auto methodAlgo = [&]() -> std::pair { + auto methodAlgo = [&]() -> std::pair { std::string hashAlgo = json["hashAlgo"]; - auto method = FileIngestionMethod::Flat; - if (hashAlgo.substr(0, 2) == "r:") { - method = FileIngestionMethod::Recursive; - hashAlgo = hashAlgo.substr(2); - } - auto hashType = parseHashType(hashAlgo); - return { method, hashType }; + // remaining to parse, will be mutated by parsers + std::string_view s = hashAlgo; + ContentAddressMethod method = ContentAddressMethod::parsePrefix(s); + auto hashType = parseHashType(s); + return { std::move(method), std::move(hashType) }; }; if (keys == (std::set { "path" })) { @@ -1018,7 +1016,7 @@ DerivationOutput DerivationOutput::fromJSON( auto [method, hashType] = methodAlgo(); auto dof = DerivationOutput::CAFixed { .ca = ContentAddressWithReferences::fromParts( - method, + std::move(method), Hash::parseNonSRIUnprefixed((std::string) json["hash"], hashType), {}), }; @@ -1030,8 +1028,8 @@ DerivationOutput DerivationOutput::fromJSON( else if (keys == (std::set { "hashAlgo" })) { auto [method, hashType] = methodAlgo(); return DerivationOutput::CAFloating { - .method = method, - .hashType = hashType, + .method = std::move(method), + .hashType = std::move(hashType), }; } @@ -1042,7 +1040,7 @@ DerivationOutput DerivationOutput::fromJSON( else if (keys == (std::set { "hashAlgo", "impure" })) { auto [method, hashType] = methodAlgo(); return DerivationOutput::Impure { - .method = method, + .method = std::move(method), .hashType = hashType, }; }