Skip to content

Commit

Permalink
Merge pull request #3674 from matthewbauer/allow-empty-hash2
Browse files Browse the repository at this point in the history
Allow empty hash in derivations
  • Loading branch information
edolstra authored Jun 12, 2020
2 parents 9f736dd + ea0d29d commit 00fa7e2
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 6 deletions.
5 changes: 3 additions & 2 deletions src/libexpr/primops.cc
Original file line number Diff line number Diff line change
Expand Up @@ -719,7 +719,8 @@ static void prim_derivationStrict(EvalState & state, const Pos & pos, Value * *
throw Error(format("multiple outputs are not supported in fixed-output derivations, at %1%") % posDrvName);

HashType ht = outputHashAlgo.empty() ? htUnknown : parseHashType(outputHashAlgo);
Hash h(*outputHash, ht);

Hash h = newHashAllowEmpty(*outputHash, ht);

auto outPath = state.store->makeFixedOutputPath(ingestionMethod, h, drvName);
if (!jsonObject) drv.env["out"] = state.store->printStorePath(outPath);
Expand Down Expand Up @@ -1126,7 +1127,7 @@ static void prim_path(EvalState & state, const Pos & pos, Value * * args, Value
} else if (n == "recursive")
method = FileIngestionMethod { state.forceBool(*attr.value, *attr.pos) };
else if (n == "sha256")
expectedHash = Hash(state.forceStringNoCtx(*attr.value, *attr.pos), htSHA256);
expectedHash = newHashAllowEmpty(state.forceStringNoCtx(*attr.value, *attr.pos), htSHA256);
else
throw EvalError(format("unsupported argument '%1%' to 'addPath', at %2%") % attr.name % *attr.pos);
}
Expand Down
2 changes: 1 addition & 1 deletion src/libexpr/primops/fetchTree.cc
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ static void fetch(EvalState & state, const Pos & pos, Value * * args, Value & v,
if (n == "url")
url = state.forceStringNoCtx(*attr.value, *attr.pos);
else if (n == "sha256")
expectedHash = Hash(state.forceStringNoCtx(*attr.value, *attr.pos), htSHA256);
expectedHash = newHashAllowEmpty(state.forceStringNoCtx(*attr.value, *attr.pos), htSHA256);
else if (n == "name")
name = state.forceStringNoCtx(*attr.value, *attr.pos);
else
Expand Down
2 changes: 1 addition & 1 deletion src/libfetchers/fetchers.cc
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ std::unique_ptr<Input> inputFromAttrs(const Attrs & attrs)
if (res) {
if (auto narHash = maybeGetStrAttr(attrs, "narHash"))
// FIXME: require SRI hash.
res->narHash = Hash(*narHash);
res->narHash = newHashAllowEmpty(*narHash, htUnknown);
return res;
}
}
Expand Down
3 changes: 1 addition & 2 deletions src/libfetchers/tarball.cc
Original file line number Diff line number Diff line change
Expand Up @@ -264,8 +264,7 @@ struct TarballInputScheme : InputScheme

auto input = std::make_unique<TarballInput>(parseURL(getStrAttr(attrs, "url")));
if (auto hash = maybeGetStrAttr(attrs, "hash"))
// FIXME: require SRI hash.
input->hash = Hash(*hash);
input->hash = newHashAllowEmpty(*hash, htUnknown);

return input;
}
Expand Down
10 changes: 10 additions & 0 deletions src/libutil/hash.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}

Hash newHashAllowEmpty(std::string hashStr, HashType ht)
{
if (hashStr.empty())
{
Hash h(ht);
warn("found empty hash, assuming you wanted '%s'", h.to_string(SRI));
} else
return Hash(hashStr, ht);
}


union Ctx
{
Expand Down
2 changes: 2 additions & 0 deletions src/libutil/hash.hh
Original file line number Diff line number Diff line change
Expand Up @@ -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. */
string printHash16or32(const Hash & hash);
Expand Down

0 comments on commit 00fa7e2

Please sign in to comment.