Add newHashAllowEmpty helper function

This replaces the copy&paste with a helper function in hash.hh.
This commit is contained in:
Matthew Bauer 2020-06-12 10:09:42 -05:00
parent 19aa892f20
commit b260c9ee03
6 changed files with 24 additions and 38 deletions

View file

@ -720,12 +720,7 @@ static void prim_derivationStrict(EvalState & state, const Pos & pos, Value * *
HashType ht = outputHashAlgo.empty() ? htUnknown : parseHashType(outputHashAlgo); HashType ht = outputHashAlgo.empty() ? htUnknown : parseHashType(outputHashAlgo);
Hash h; Hash h = newHashAllowEmpty(*outputHash, ht);
if (outputHash->empty()) {
h = Hash(ht);
printError("warning: found empty hash, assuming you wanted '%s'", h.to_string());
} else
h = Hash(*outputHash, ht);
auto outPath = state.store->makeFixedOutputPath(ingestionMethod, h, drvName); auto outPath = state.store->makeFixedOutputPath(ingestionMethod, h, drvName);
if (!jsonObject) drv.env["out"] = state.store->printStorePath(outPath); if (!jsonObject) drv.env["out"] = state.store->printStorePath(outPath);
@ -1131,14 +1126,9 @@ static void prim_path(EvalState & state, const Pos & pos, Value * * args, Value
filterFun = attr.value; filterFun = attr.value;
} else if (n == "recursive") } else if (n == "recursive")
method = FileIngestionMethod { state.forceBool(*attr.value, *attr.pos) }; method = FileIngestionMethod { state.forceBool(*attr.value, *attr.pos) };
else if (n == "sha256") { else if (n == "sha256")
auto hashStr = state.forceStringNoCtx(*attr.value, *attr.pos); expectedHash = newHashAllowEmpty(state.forceStringNoCtx(*attr.value, *attr.pos), htSHA256);
if (hashStr == "") { else
expectedHash = Hash(htSHA256);
printError("warning: found empty hash, assuming you wanted '%s'", expectedHash.to_string());
} else
expectedHash = Hash(hashStr, htSHA256);
} else
throw EvalError(format("unsupported argument '%1%' to 'addPath', at %2%") % attr.name % *attr.pos); throw EvalError(format("unsupported argument '%1%' to 'addPath', at %2%") % attr.name % *attr.pos);
} }
if (path.empty()) if (path.empty())

View file

@ -102,14 +102,9 @@ static void fetch(EvalState & state, const Pos & pos, Value * * args, Value & v,
string n(attr.name); string n(attr.name);
if (n == "url") if (n == "url")
url = state.forceStringNoCtx(*attr.value, *attr.pos); url = state.forceStringNoCtx(*attr.value, *attr.pos);
else if (n == "sha256") { else if (n == "sha256")
auto hashStr = state.forceStringNoCtx(*attr.value, *attr.pos); expectedHash = newHashAllowEmpty(state.forceStringNoCtx(*attr.value, *attr.pos), htSHA256);
if (hashStr == "") { else if (n == "name")
expectedHash = Hash(htSHA256);
printError("warning: found empty hash, assuming you wanted '%s'", expectedHash->to_string());
} else
expectedHash = Hash(hashStr, htSHA256);
} else if (n == "name")
name = state.forceStringNoCtx(*attr.value, *attr.pos); name = state.forceStringNoCtx(*attr.value, *attr.pos);
else else
throw EvalError("unsupported argument '%s' to '%s', at %s", throw EvalError("unsupported argument '%s' to '%s', at %s",

View file

@ -34,14 +34,9 @@ std::unique_ptr<Input> inputFromAttrs(const Attrs & attrs)
for (auto & inputScheme : *inputSchemes) { for (auto & inputScheme : *inputSchemes) {
auto res = inputScheme->inputFromAttrs(attrs2); auto res = inputScheme->inputFromAttrs(attrs2);
if (res) { if (res) {
if (auto narHash = maybeGetStrAttr(attrs, "narHash")) { if (auto narHash = maybeGetStrAttr(attrs, "narHash"))
if (narHash->empty()) {
res->narHash = Hash(htUnknown);
printError("warning: found empty hash, assuming you wanted '%s'", res->narHash->to_string());
} else
// FIXME: require SRI hash. // FIXME: require SRI hash.
res->narHash = Hash(*narHash); res->narHash = newHashAllowEmpty(*narHash, htUnknown);
}
return res; return res;
} }
} }

View file

@ -263,14 +263,8 @@ struct TarballInputScheme : InputScheme
throw Error("unsupported tarball input attribute '%s'", name); throw Error("unsupported tarball input attribute '%s'", name);
auto input = std::make_unique<TarballInput>(parseURL(getStrAttr(attrs, "url"))); auto input = std::make_unique<TarballInput>(parseURL(getStrAttr(attrs, "url")));
if (auto hash = maybeGetStrAttr(attrs, "hash")) { if (auto hash = maybeGetStrAttr(attrs, "hash"))
if (hash->empty()) { input->hash = newHashAllowEmpty(*hash, htUnknown);
input->hash = Hash(htUnknown);
printError("warning: found empty hash, assuming you wanted '%s'", input->hash->to_string());
} else
// FIXME: require SRI hash.
input->hash = Hash(*hash);
}
return input; return input;
} }

View file

@ -205,6 +205,16 @@ Hash::Hash(const std::string & s, HashType type)
throw BadHash("hash '%s' has wrong length for hash type '%s'", s, printHashType(type)); throw BadHash("hash '%s' has wrong length for hash type '%s'", s, printHashType(type));
} }
Hash newHashAllowEmpty(std::string hashStr, HashType ht)
{
if (hashStr.empty())
{
Hash h(ht);
warn("found empty hash, assuming you wanted '%s'", h.to_string());
} else
return Hash(hashStr, ht);
}
union Ctx union Ctx
{ {

View file

@ -94,6 +94,8 @@ struct Hash
} }
}; };
/* Helper that defaults empty hashes to the 0 hash. */
Hash newHashAllowEmpty(std::string hashStr, HashType ht);
/* Print a hash in base-16 if it's MD5, or base-32 otherwise. */ /* Print a hash in base-16 if it's MD5, or base-32 otherwise. */
string printHash16or32(const Hash & hash); string printHash16or32(const Hash & hash);