Remove HashType::Unknown

Instead, `Hash` uses `std::optional<HashType>`. In the future, we may
also make `Hash` itself require a known hash type, encoraging people to
use `std::optional<Hash>` instead.
This commit is contained in:
John Ericson 2020-06-02 15:52:13 +00:00
parent 6dd471ebf6
commit 450dcf2c1b
14 changed files with 72 additions and 53 deletions

View file

@ -718,7 +718,7 @@ static void prim_derivationStrict(EvalState & state, const Pos & pos, Value * *
if (outputs.size() != 1 || *(outputs.begin()) != "out") if (outputs.size() != 1 || *(outputs.begin()) != "out")
throw Error(format("multiple outputs are not supported in fixed-output derivations, at %1%") % posDrvName); throw Error(format("multiple outputs are not supported in fixed-output derivations, at %1%") % posDrvName);
HashType ht = outputHashAlgo.empty() ? HashType::Unknown : parseHashType(outputHashAlgo); std::optional<HashType> ht = parseHashTypeOpt(outputHashAlgo);
Hash h(*outputHash, ht); Hash h(*outputHash, ht);
auto outPath = state.store->makeFixedOutputPath(ingestionMethod, h, drvName); auto outPath = state.store->makeFixedOutputPath(ingestionMethod, h, drvName);
@ -726,7 +726,7 @@ static void prim_derivationStrict(EvalState & state, const Pos & pos, Value * *
drv.outputs.insert_or_assign("out", DerivationOutput { drv.outputs.insert_or_assign("out", DerivationOutput {
std::move(outPath), std::move(outPath),
(ingestionMethod == FileIngestionMethod::Recursive ? "r:" : "") (ingestionMethod == FileIngestionMethod::Recursive ? "r:" : "")
+ printHashType(h.type), + printHashType(*h.type),
h.to_string(Base::Base16, false), h.to_string(Base::Base16, false),
}); });
} }
@ -934,14 +934,14 @@ static void prim_findFile(EvalState & state, const Pos & pos, Value * * args, Va
static void prim_hashFile(EvalState & state, const Pos & pos, Value * * args, Value & v) static void prim_hashFile(EvalState & state, const Pos & pos, Value * * args, Value & v)
{ {
string type = state.forceStringNoCtx(*args[0], pos); string type = state.forceStringNoCtx(*args[0], pos);
HashType ht = parseHashType(type); std::optional<HashType> ht = parseHashType(type);
if (ht == HashType::Unknown) if (!ht)
throw Error(format("unknown hash type '%1%', at %2%") % type % pos); throw Error(format("unknown hash type '%1%', at %2%") % type % pos);
PathSet context; // discarded PathSet context; // discarded
Path p = state.coerceToPath(pos, *args[1], context); Path p = state.coerceToPath(pos, *args[1], context);
mkString(v, hashFile(ht, state.checkSourcePath(p)).to_string(Base::Base16, false), context); mkString(v, hashFile(*ht, state.checkSourcePath(p)).to_string(Base::Base16, false), context);
} }
/* Read a directory (without . or ..) */ /* Read a directory (without . or ..) */
@ -1812,14 +1812,14 @@ static void prim_stringLength(EvalState & state, const Pos & pos, Value * * args
static void prim_hashString(EvalState & state, const Pos & pos, Value * * args, Value & v) static void prim_hashString(EvalState & state, const Pos & pos, Value * * args, Value & v)
{ {
string type = state.forceStringNoCtx(*args[0], pos); string type = state.forceStringNoCtx(*args[0], pos);
HashType ht = parseHashType(type); std::optional<HashType> ht = parseHashType(type);
if (ht == HashType::Unknown) if (!ht)
throw Error(format("unknown hash type '%1%', at %2%") % type % pos); throw Error(format("unknown hash type '%1%', at %2%") % type % pos);
PathSet context; // discarded PathSet context; // discarded
string s = state.forceString(*args[1], context, pos); string s = state.forceString(*args[1], context, pos);
mkString(v, hashString(ht, s).to_string(Base::Base16, false), context); mkString(v, hashString(*ht, s).to_string(Base::Base16, false), context);
} }

View file

@ -3726,8 +3726,8 @@ void DerivationGoal::registerOutputs()
/* Check the hash. In hash mode, move the path produced by /* Check the hash. In hash mode, move the path produced by
the derivation to its content-addressed location. */ the derivation to its content-addressed location. */
Hash h2 = outputHashMode == FileIngestionMethod::Recursive Hash h2 = outputHashMode == FileIngestionMethod::Recursive
? hashPath(h.type, actualPath).first ? hashPath(*h.type, actualPath).first
: hashFile(h.type, actualPath); : hashFile(*h.type, actualPath);
auto dest = worker.store.makeFixedOutputPath(outputHashMode, h2, i.second.path.name()); auto dest = worker.store.makeFixedOutputPath(outputHashMode, h2, i.second.path.name());
@ -4999,7 +4999,7 @@ bool Worker::pathContentsGood(const StorePath & path)
if (!pathExists(store.printStorePath(path))) if (!pathExists(store.printStorePath(path)))
res = false; res = false;
else { else {
HashResult current = hashPath(info->narHash.type, store.printStorePath(path)); HashResult current = hashPath(*info->narHash.type, store.printStorePath(path));
Hash nullHash(HashType::SHA256); Hash nullHash(HashType::SHA256);
res = info->narHash == nullHash || info->narHash == current.first; res = info->narHash == nullHash || info->narHash == current.first;
} }

View file

@ -65,7 +65,7 @@ void builtinFetchurl(const BasicDerivation & drv, const std::string & netrcData)
if (!hasSuffix(hashedMirror, "/")) hashedMirror += '/'; if (!hasSuffix(hashedMirror, "/")) hashedMirror += '/';
auto ht = parseHashType(getAttr("outputHashAlgo")); auto ht = parseHashType(getAttr("outputHashAlgo"));
auto h = Hash(getAttr("outputHash"), ht); auto h = Hash(getAttr("outputHash"), ht);
fetch(hashedMirror + printHashType(h.type) + "/" + h.to_string(Base::Base16, false)); fetch(hashedMirror + printHashType(*h.type) + "/" + h.to_string(Base::Base16, false));
return; return;
} catch (Error & e) { } catch (Error & e) {
debug(e.what()); debug(e.what());

View file

@ -20,8 +20,6 @@ void DerivationOutput::parseHashInfo(FileIngestionMethod & recursive, Hash & has
} }
HashType hashType = parseHashType(algo); HashType hashType = parseHashType(algo);
if (hashType == HashType::Unknown)
throw Error("unknown hash algorithm '%s'", algo);
hash = Hash(this->hash, hashType); hash = Hash(this->hash, hashType);
} }

View file

@ -54,7 +54,7 @@ void Store::exportPath(const StorePath & path, Sink & sink)
filesystem corruption from spreading to other machines. filesystem corruption from spreading to other machines.
Don't complain if the stored hash is zero (unknown). */ Don't complain if the stored hash is zero (unknown). */
Hash hash = hashAndWriteSink.currentHash(); Hash hash = hashAndWriteSink.currentHash();
if (hash != info->narHash && info->narHash != Hash(info->narHash.type)) if (hash != info->narHash && info->narHash != Hash(*info->narHash.type))
throw Error("hash of path '%s' has changed from '%s' to '%s'!", throw Error("hash of path '%s' has changed from '%s' to '%s'!",
printStorePath(path), info->narHash.to_string(), hash.to_string()); printStorePath(path), info->narHash.to_string(), hash.to_string());

View file

@ -1264,9 +1264,9 @@ bool LocalStore::verifyStore(bool checkContents, RepairFlag repair)
std::unique_ptr<AbstractHashSink> hashSink; std::unique_ptr<AbstractHashSink> hashSink;
if (info->ca == "" || !info->references.count(info->path)) if (info->ca == "" || !info->references.count(info->path))
hashSink = std::make_unique<HashSink>(info->narHash.type); hashSink = std::make_unique<HashSink>(*info->narHash.type);
else else
hashSink = std::make_unique<HashModuloSink>(info->narHash.type, storePathToHash(printStorePath(info->path))); hashSink = std::make_unique<HashModuloSink>(*info->narHash.type, storePathToHash(printStorePath(info->path)));
dumpPath(Store::toRealPath(i), *hashSink); dumpPath(Store::toRealPath(i), *hashSink);
auto current = hashSink->finish(); auto current = hashSink->finish();

View file

@ -162,8 +162,6 @@ Args::Flag Args::Flag::mkHashTypeFlag(std::string && longName, HashType * ht)
.labels = {"hash-algo"}, .labels = {"hash-algo"},
.handler = {[ht](std::string s) { .handler = {[ht](std::string s) {
*ht = parseHashType(s); *ht = parseHashType(s);
if (*ht == HashType::Unknown)
throw UsageError("unknown hash type '%1%'", s);
}} }}
}; };
} }

View file

@ -4,6 +4,7 @@
#include <openssl/md5.h> #include <openssl/md5.h>
#include <openssl/sha.h> #include <openssl/sha.h>
#include "args.hh"
#include "hash.hh" #include "hash.hh"
#include "archive.hh" #include "archive.hh"
#include "util.hh" #include "util.hh"
@ -18,11 +19,13 @@ namespace nix {
void Hash::init() void Hash::init()
{ {
if (type == HashType::MD5) hashSize = md5HashSize; if (!type) abort();
else if (type == HashType::SHA1) hashSize = sha1HashSize; switch (*type) {
else if (type == HashType::SHA256) hashSize = sha256HashSize; case HashType::MD5: hashSize = md5HashSize; break;
else if (type == HashType::SHA512) hashSize = sha512HashSize; case HashType::SHA1: hashSize = sha1HashSize; break;
else abort(); case HashType::SHA256: hashSize = sha256HashSize; break;
case HashType::SHA512: hashSize = sha512HashSize; break;
}
assert(hashSize <= maxHashSize); assert(hashSize <= maxHashSize);
memset(hash, 0, maxHashSize); memset(hash, 0, maxHashSize);
} }
@ -102,11 +105,18 @@ string printHash16or32(const Hash & hash)
} }
HashType assertInitHashType(const Hash & h) {
if (h.type)
return *h.type;
else
abort();
}
std::string Hash::to_string(Base base, bool includeType) const std::string Hash::to_string(Base base, bool includeType) const
{ {
std::string s; std::string s;
if (base == Base::SRI || includeType) { if (base == Base::SRI || includeType) {
s += printHashType(type); s += printHashType(assertInitHashType(*this));
s += base == Base::SRI ? '-' : ':'; s += base == Base::SRI ? '-' : ':';
} }
switch (base) { switch (base) {
@ -124,8 +134,10 @@ std::string Hash::to_string(Base base, bool includeType) const
return s; return s;
} }
Hash::Hash(const std::string & s, HashType type) : Hash(s, std::optional { type }) { }
Hash::Hash(const std::string & s) : Hash(s, std::optional<HashType>{}) { }
Hash::Hash(const std::string & s, HashType type) Hash::Hash(const std::string & s, std::optional<HashType> type)
: type(type) : type(type)
{ {
size_t pos = 0; size_t pos = 0;
@ -136,17 +148,17 @@ Hash::Hash(const std::string & s, HashType type)
sep = s.find('-'); sep = s.find('-');
if (sep != string::npos) { if (sep != string::npos) {
isSRI = true; isSRI = true;
} else if (type == HashType::Unknown) } else if (! type)
throw BadHash("hash '%s' does not include a type", s); throw BadHash("hash '%s' does not include a type", s);
} }
if (sep != string::npos) { if (sep != string::npos) {
string hts = string(s, 0, sep); string hts = string(s, 0, sep);
this->type = parseHashType(hts); this->type = parseHashType(hts);
if (this->type == HashType::Unknown) if (!this->type)
throw BadHash("unknown hash type '%s'", hts); throw BadHash("unknown hash type '%s'", hts);
if (type != HashType::Unknown && type != this->type) if (type && type != this->type)
throw BadHash("hash '%s' should have type '%s'", s, printHashType(type)); throw BadHash("hash '%s' should have type '%s'", s, printHashType(*type));
pos = sep + 1; pos = sep + 1;
} }
@ -202,7 +214,7 @@ Hash::Hash(const std::string & s, HashType type)
} }
else else
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));
} }
@ -318,24 +330,34 @@ Hash compressHash(const Hash & hash, unsigned int newSize)
} }
HashType parseHashType(const string & s) std::optional<HashType> parseHashTypeOpt(const string & s)
{ {
if (s == "md5") return HashType::MD5; if (s == "md5") return HashType::MD5;
else if (s == "sha1") return HashType::SHA1; else if (s == "sha1") return HashType::SHA1;
else if (s == "sha256") return HashType::SHA256; else if (s == "sha256") return HashType::SHA256;
else if (s == "sha512") return HashType::SHA512; else if (s == "sha512") return HashType::SHA512;
else return HashType::Unknown; else return std::optional<HashType> {};
} }
HashType parseHashType(const string & s)
{
auto opt_h = parseHashTypeOpt(s);
if (opt_h)
return *opt_h;
else
throw UsageError("unknown hash algorithm '%1%'", s);
}
string printHashType(HashType ht) string printHashType(HashType ht)
{ {
if (ht == HashType::MD5) return "md5"; string ret;
else if (ht == HashType::SHA1) return "sha1"; switch (ht) {
else if (ht == HashType::SHA256) return "sha256"; case HashType::MD5: ret = "md5"; break;
else if (ht == HashType::SHA512) return "sha512"; case HashType::SHA1: ret = "sha1"; break;
else abort(); case HashType::SHA256: ret = "sha256"; break;
case HashType::SHA512: ret = "sha512"; break;
}
return ret;
} }
} }

View file

@ -11,7 +11,6 @@ MakeError(BadHash, Error);
enum struct HashType : char { enum struct HashType : char {
Unknown,
MD5, MD5,
SHA1, SHA1,
SHA256, SHA256,
@ -40,7 +39,7 @@ struct Hash
unsigned int hashSize = 0; unsigned int hashSize = 0;
unsigned char hash[maxHashSize] = {}; unsigned char hash[maxHashSize] = {};
HashType type = HashType::Unknown; std::optional<HashType> type = {};
/* Create an unset hash object. */ /* Create an unset hash object. */
Hash() { }; Hash() { };
@ -51,14 +50,18 @@ struct Hash
/* Initialize the hash from a string representation, in the format /* Initialize the hash from a string representation, in the format
"[<type>:]<base16|base32|base64>" or "<type>-<base64>" (a "[<type>:]<base16|base32|base64>" or "<type>-<base64>" (a
Subresource Integrity hash expression). If the 'type' argument Subresource Integrity hash expression). If the 'type' argument
is HashType::Unknown, then the hash type must be specified in the is not present, then the hash type must be specified in the
string. */ string. */
Hash(const std::string & s, HashType type = HashType::Unknown); Hash(const std::string & s, std::optional<HashType> type);
// type must be provided
Hash(const std::string & s, HashType type);
// hash type must be part of string
Hash(const std::string & s);
void init(); void init();
/* Check whether a hash is set. */ /* Check whether a hash is set. */
operator bool () const { return type != HashType::Unknown; } operator bool () const { return (bool) type; }
/* Check whether two hash are equal. */ /* Check whether two hash are equal. */
bool operator == (const Hash & h2) const; bool operator == (const Hash & h2) const;
@ -127,6 +130,8 @@ Hash compressHash(const Hash & hash, unsigned int newSize);
/* Parse a string representing a hash type. */ /* Parse a string representing a hash type. */
HashType parseHashType(const string & s); HashType parseHashType(const string & s);
/* Will return nothing on parse error */
std::optional<HashType> parseHashTypeOpt(const string & s);
/* And the reverse. */ /* And the reverse. */
string printHashType(HashType ht); string printHashType(HashType ht);

View file

@ -75,6 +75,6 @@ namespace nix {
TEST(hashString, hashingWithUnknownAlgoExits) { TEST(hashString, hashingWithUnknownAlgoExits) {
auto s = "unknown"; auto s = "unknown";
ASSERT_DEATH(hashString(HashType::Unknown, s), ""); ASSERT_DEATH(hashString(HashType::SHA512, s), "");
} }
} }

View file

@ -72,8 +72,6 @@ static int _main(int argc, char * * argv)
else if (*arg == "--type") { else if (*arg == "--type") {
string s = getArg(*arg, arg, end); string s = getArg(*arg, arg, end);
ht = parseHashType(s); ht = parseHashType(s);
if (ht == HashType::Unknown)
throw UsageError(format("unknown hash type '%1%'") % s);
} }
else if (*arg == "--print-path") else if (*arg == "--print-path")
printPath = true; printPath = true;

View file

@ -722,7 +722,7 @@ static void opVerifyPath(Strings opFlags, Strings opArgs)
auto path = store->followLinksToStorePath(i); auto path = store->followLinksToStorePath(i);
printMsg(Verbosity::Talkative, "checking path '%s'...", store->printStorePath(path)); printMsg(Verbosity::Talkative, "checking path '%s'...", store->printStorePath(path));
auto info = store->queryPathInfo(path); auto info = store->queryPathInfo(path);
HashSink sink(info->narHash.type); HashSink sink(*info->narHash.type);
store->narFromPath(path, sink); store->narFromPath(path, sink);
auto current = sink.finish(); auto current = sink.finish();
if (current.first != info->narHash) { if (current.first != info->narHash) {

View file

@ -79,7 +79,7 @@ static RegisterCommand r2("hash-path", [](){ return make_ref<CmdHash>(FileIngest
struct CmdToBase : Command struct CmdToBase : Command
{ {
Base base; Base base;
HashType ht = HashType::Unknown; HashType ht;
std::vector<std::string> args; std::vector<std::string> args;
CmdToBase(Base base) : base(base) CmdToBase(Base base) : base(base)
@ -132,8 +132,6 @@ static int compatNixHash(int argc, char * * argv)
else if (*arg == "--type") { else if (*arg == "--type") {
string s = getArg(*arg, arg, end); string s = getArg(*arg, arg, end);
ht = parseHashType(s); ht = parseHashType(s);
if (ht == HashType::Unknown)
throw UsageError(format("unknown hash type '%1%'") % s);
} }
else if (*arg == "--to-base16") op = opTo16; else if (*arg == "--to-base16") op = opTo16;
else if (*arg == "--to-base32") op = opTo32; else if (*arg == "--to-base32") op = opTo32;

View file

@ -88,9 +88,9 @@ struct CmdVerify : StorePathsCommand
std::unique_ptr<AbstractHashSink> hashSink; std::unique_ptr<AbstractHashSink> hashSink;
if (info->ca == "") if (info->ca == "")
hashSink = std::make_unique<HashSink>(info->narHash.type); hashSink = std::make_unique<HashSink>(*info->narHash.type);
else else
hashSink = std::make_unique<HashModuloSink>(info->narHash.type, storePathToHash(store->printStorePath(info->path))); hashSink = std::make_unique<HashModuloSink>(*info->narHash.type, storePathToHash(store->printStorePath(info->path)));
store->narFromPath(info->path, *hashSink); store->narFromPath(info->path, *hashSink);