chore(3p/nix/hash): prefer StatusOr over throwing constructor
The use of `unwrap_throw` can be used as a later grep target. Change-Id: I8c54ed90c4289f07aecb8a1393dd10204c8bce4e Reviewed-on: https://cl.tvl.fyi/c/depot/+/1493 Reviewed-by: glittershark <grfn@gws.fyi> Reviewed-by: tazjin <mail@tazj.in> Tested-by: BuildkiteCI
This commit is contained in:
parent
2a292c71f4
commit
1cbffe21f3
15 changed files with 97 additions and 49 deletions
|
|
@ -64,19 +64,25 @@ void builtinFetchurl(const BasicDerivation& drv, const std::string& netrcData) {
|
|||
|
||||
/* Try the hashed mirrors first. */
|
||||
if (getAttr("outputHashMode") == "flat") {
|
||||
for (auto hashedMirror : settings.hashedMirrors.get()) {
|
||||
try {
|
||||
if (!absl::EndsWith(hashedMirror, "/")) {
|
||||
hashedMirror += '/';
|
||||
auto hash_ = Hash::deserialize(getAttr("outputHash"),
|
||||
parseHashType(getAttr("outputHashAlgo")));
|
||||
if (hash_.ok()) {
|
||||
auto h = *hash_;
|
||||
for (auto hashedMirror : settings.hashedMirrors.get()) {
|
||||
try {
|
||||
if (!absl::EndsWith(hashedMirror, "/")) {
|
||||
hashedMirror += '/';
|
||||
}
|
||||
fetch(hashedMirror + printHashType(h.type) + "/" +
|
||||
h.to_string(Base16, false));
|
||||
return;
|
||||
} catch (Error& e) {
|
||||
LOG(ERROR) << e.what();
|
||||
}
|
||||
auto ht = parseHashType(getAttr("outputHashAlgo"));
|
||||
auto h = Hash(getAttr("outputHash"), ht);
|
||||
fetch(hashedMirror + printHashType(h.type) + "/" +
|
||||
h.to_string(Base16, false));
|
||||
return;
|
||||
} catch (Error& e) {
|
||||
LOG(ERROR) << e.what();
|
||||
}
|
||||
} else {
|
||||
LOG(ERROR) << "checking mirrors for '" << mainUrl
|
||||
<< "': " << hash_.status().ToString();
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
4
third_party/nix/src/libstore/derivations.cc
vendored
4
third_party/nix/src/libstore/derivations.cc
vendored
|
|
@ -13,6 +13,7 @@
|
|||
|
||||
namespace nix {
|
||||
|
||||
// TODO(#statusor): looks like easy absl::Status conversion
|
||||
void DerivationOutput::parseHashInfo(bool& recursive, Hash& hash) const {
|
||||
recursive = false;
|
||||
std::string algo = hashAlgo;
|
||||
|
|
@ -27,7 +28,8 @@ void DerivationOutput::parseHashInfo(bool& recursive, Hash& hash) const {
|
|||
throw Error(format("unknown hash algorithm '%1%'") % algo);
|
||||
}
|
||||
|
||||
hash = Hash(this->hash, hashType);
|
||||
auto hash_ = Hash::deserialize(this->hash, hashType);
|
||||
hash = Hash::unwrap_throw(hash_);
|
||||
}
|
||||
|
||||
BasicDerivation BasicDerivation::from_proto(
|
||||
|
|
|
|||
|
|
@ -115,7 +115,12 @@ struct LegacySSHStore : public Store {
|
|||
|
||||
if (GET_PROTOCOL_MINOR(conn->remoteVersion) >= 4) {
|
||||
auto s = readString(conn->from);
|
||||
info->narHash = s.empty() ? Hash() : Hash(s);
|
||||
if (s.empty()) {
|
||||
info->narHash = Hash();
|
||||
} else {
|
||||
auto hash_ = Hash::deserialize(s);
|
||||
info->narHash = Hash::unwrap_throw(hash_);
|
||||
}
|
||||
conn->from >> info->ca;
|
||||
info->sigs = readStrings<StringSet>(conn->from);
|
||||
}
|
||||
|
|
|
|||
10
third_party/nix/src/libstore/local-store.cc
vendored
10
third_party/nix/src/libstore/local-store.cc
vendored
|
|
@ -8,6 +8,7 @@
|
|||
#include <iostream>
|
||||
|
||||
#include <absl/strings/numbers.h>
|
||||
#include <absl/strings/str_cat.h>
|
||||
#include <absl/strings/str_split.h>
|
||||
#include <fcntl.h>
|
||||
#include <glog/logging.h>
|
||||
|
|
@ -681,11 +682,12 @@ void LocalStore::queryPathInfoUncached(
|
|||
|
||||
info->id = useQueryPathInfo.getInt(0);
|
||||
|
||||
try {
|
||||
info->narHash = Hash(useQueryPathInfo.getStr(1));
|
||||
} catch (BadHash& e) {
|
||||
throw Error("in valid-path entry for '%s': %s", path, e.what());
|
||||
auto hash_ = Hash::deserialize(useQueryPathInfo.getStr(1));
|
||||
if (!hash_.ok()) {
|
||||
throw Error(absl::StrCat("in valid-path entry for '", path,
|
||||
"': ", hash_.status().ToString()));
|
||||
}
|
||||
info->narHash = *hash_;
|
||||
|
||||
info->registrationTime = useQueryPathInfo.getInt(2);
|
||||
|
||||
|
|
|
|||
|
|
@ -226,10 +226,13 @@ class NarInfoDiskCacheImpl final : public NarInfoDiskCache {
|
|||
narInfo->url = queryNAR.getStr(2);
|
||||
narInfo->compression = queryNAR.getStr(3);
|
||||
if (!queryNAR.isNull(4)) {
|
||||
narInfo->fileHash = Hash(queryNAR.getStr(4));
|
||||
auto hash_ = Hash::deserialize(queryNAR.getStr(4));
|
||||
// TODO(#statusor): does this throw mess with retrySQLite?
|
||||
narInfo->fileHash = Hash::unwrap_throw(hash_);
|
||||
}
|
||||
narInfo->fileSize = queryNAR.getInt(5);
|
||||
narInfo->narHash = Hash(queryNAR.getStr(6));
|
||||
auto hash_ = Hash::deserialize(queryNAR.getStr(6));
|
||||
narInfo->narHash = Hash::unwrap_throw(hash_);
|
||||
narInfo->narSize = queryNAR.getInt(7);
|
||||
for (auto r : absl::StrSplit(queryNAR.getStr(8), absl::ByChar(' '))) {
|
||||
narInfo->references.insert(absl::StrCat(cache.storeDir, "/", r));
|
||||
|
|
|
|||
10
third_party/nix/src/libstore/nar-info.cc
vendored
10
third_party/nix/src/libstore/nar-info.cc
vendored
|
|
@ -14,11 +14,13 @@ NarInfo::NarInfo(const Store& store, const std::string& s,
|
|||
};
|
||||
|
||||
auto parseHashField = [&](const std::string& s) {
|
||||
try {
|
||||
return Hash(s);
|
||||
} catch (BadHash&) {
|
||||
auto hash_ = Hash::deserialize(s);
|
||||
if (hash_.ok()) {
|
||||
return *hash_;
|
||||
} else {
|
||||
// TODO(#statusor): return an actual error
|
||||
corrupt();
|
||||
return Hash(); // never reached
|
||||
return Hash();
|
||||
}
|
||||
};
|
||||
|
||||
|
|
|
|||
3
third_party/nix/src/libstore/remote-store.cc
vendored
3
third_party/nix/src/libstore/remote-store.cc
vendored
|
|
@ -359,7 +359,8 @@ void RemoteStore::queryPathInfoUncached(
|
|||
if (!info->deriver.empty()) {
|
||||
assertStorePath(info->deriver);
|
||||
}
|
||||
info->narHash = Hash(readString(conn->from), htSHA256);
|
||||
auto hash_ = Hash::deserialize(readString(conn->from), htSHA256);
|
||||
info->narHash = Hash::unwrap_throw(hash_);
|
||||
info->references = readStorePaths<PathSet>(*this, conn->from);
|
||||
conn->from >> info->registrationTime >> info->narSize;
|
||||
if (GET_PROTOCOL_MINOR(conn->daemonVersion) >= 16) {
|
||||
|
|
|
|||
3
third_party/nix/src/libstore/rpc-store.cc
vendored
3
third_party/nix/src/libstore/rpc-store.cc
vendored
|
|
@ -100,7 +100,8 @@ void RpcStore::queryPathInfoUncached(
|
|||
if (!info->deriver.empty()) {
|
||||
assertStorePath(info->deriver);
|
||||
}
|
||||
info->narHash = Hash(path_info.nar_hash(), htSHA256);
|
||||
auto hash_ = Hash::deserialize(path_info.nar_hash(), htSHA256);
|
||||
info->narHash = Hash::unwrap_throw(hash_);
|
||||
info->references.insert(path_info.references().begin(),
|
||||
path_info.references().end());
|
||||
info->registrationTime =
|
||||
|
|
|
|||
10
third_party/nix/src/libstore/store-api.cc
vendored
10
third_party/nix/src/libstore/store-api.cc
vendored
|
|
@ -774,7 +774,8 @@ ValidPathInfo decodeValidPathInfo(std::istream& str, bool hashGiven) {
|
|||
if (hashGiven) {
|
||||
std::string s;
|
||||
getline(str, s);
|
||||
info.narHash = Hash(s, htSHA256);
|
||||
auto hash_ = Hash::deserialize(s, htSHA256);
|
||||
info.narHash = Hash::unwrap_throw(hash_);
|
||||
getline(str, s);
|
||||
if (!absl::SimpleAtoi(s, &info.narSize)) {
|
||||
throw Error("number expected");
|
||||
|
|
@ -829,7 +830,8 @@ bool ValidPathInfo::isContentAddressed(const Store& store) const {
|
|||
};
|
||||
|
||||
if (absl::StartsWith(ca, "text:")) {
|
||||
Hash hash(std::string(ca, 5));
|
||||
auto hash_ = Hash::deserialize(std::string_view(ca).substr(5));
|
||||
Hash hash = Hash::unwrap_throw(hash_);
|
||||
if (store.makeTextPath(storePathToName(path), hash, references) == path) {
|
||||
return true;
|
||||
}
|
||||
|
|
@ -839,7 +841,9 @@ bool ValidPathInfo::isContentAddressed(const Store& store) const {
|
|||
|
||||
else if (absl::StartsWith(ca, "fixed:")) {
|
||||
bool recursive = ca.compare(6, 2, "r:") == 0;
|
||||
Hash hash(std::string(ca, recursive ? 8 : 6));
|
||||
auto hash_ =
|
||||
Hash::deserialize(std::string_view(ca).substr(recursive ? 8 : 6));
|
||||
Hash hash = Hash::unwrap_throw(hash_);
|
||||
if (references.empty() &&
|
||||
store.makeFixedOutputPath(recursive, hash, storePathToName(path)) ==
|
||||
path) {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue