mirror of
https://github.com/privatevoid-net/nix-super.git
synced 2024-11-23 14:36:16 +02:00
WIP: Make Hash always store a valid hash type
This commit is contained in:
parent
984e521392
commit
20799a5151
8 changed files with 53 additions and 52 deletions
|
@ -4997,7 +4997,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(htSHA256);
|
Hash nullHash(htSHA256);
|
||||||
res = info->narHash == nullHash || info->narHash == current.first;
|
res = info->narHash == nullHash || info->narHash == current.first;
|
||||||
}
|
}
|
||||||
|
|
|
@ -116,7 +116,8 @@ struct ValidPathInfo
|
||||||
{
|
{
|
||||||
StorePath path;
|
StorePath path;
|
||||||
std::optional<StorePath> deriver;
|
std::optional<StorePath> deriver;
|
||||||
Hash narHash;
|
// TODO document this
|
||||||
|
std::optional<Hash> narHash;
|
||||||
StorePathSet references;
|
StorePathSet references;
|
||||||
time_t registrationTime = 0;
|
time_t registrationTime = 0;
|
||||||
uint64_t narSize = 0; // 0 = unknown
|
uint64_t narSize = 0; // 0 = unknown
|
||||||
|
|
|
@ -16,16 +16,19 @@
|
||||||
|
|
||||||
namespace nix {
|
namespace nix {
|
||||||
|
|
||||||
|
static size_t regularHashSize(HashType type) {
|
||||||
|
switch (type) {
|
||||||
|
case htMD5: return md5HashSize;
|
||||||
|
case htSHA1: return sha1HashSize;
|
||||||
|
case htSHA256: return sha256HashSize;
|
||||||
|
case htSHA512: return sha512HashSize;
|
||||||
|
}
|
||||||
|
abort();
|
||||||
|
}
|
||||||
|
|
||||||
void Hash::init()
|
void Hash::init()
|
||||||
{
|
{
|
||||||
if (!type) abort();
|
hashSize = regularHashSize(type);
|
||||||
switch (*type) {
|
|
||||||
case htMD5: hashSize = md5HashSize; break;
|
|
||||||
case htSHA1: hashSize = sha1HashSize; break;
|
|
||||||
case htSHA256: hashSize = sha256HashSize; break;
|
|
||||||
case htSHA512: hashSize = sha512HashSize; break;
|
|
||||||
}
|
|
||||||
assert(hashSize <= maxHashSize);
|
assert(hashSize <= maxHashSize);
|
||||||
memset(hash, 0, maxHashSize);
|
memset(hash, 0, maxHashSize);
|
||||||
}
|
}
|
||||||
|
@ -105,18 +108,11 @@ 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 == SRI || includeType) {
|
if (base == SRI || includeType) {
|
||||||
s += printHashType(assertInitHashType(*this));
|
s += printHashType(type);
|
||||||
s += base == SRI ? '-' : ':';
|
s += base == SRI ? '-' : ':';
|
||||||
}
|
}
|
||||||
switch (base) {
|
switch (base) {
|
||||||
|
@ -137,29 +133,36 @@ std::string Hash::to_string(Base base, bool includeType) const
|
||||||
Hash::Hash(std::string_view s, HashType type) : Hash(s, std::optional { type }) { }
|
Hash::Hash(std::string_view s, HashType type) : Hash(s, std::optional { type }) { }
|
||||||
Hash::Hash(std::string_view s) : Hash(s, std::optional<HashType>{}) { }
|
Hash::Hash(std::string_view s) : Hash(s, std::optional<HashType>{}) { }
|
||||||
|
|
||||||
Hash::Hash(std::string_view s, std::optional<HashType> type)
|
Hash::Hash(std::string_view s, std::optional<HashType> optType)
|
||||||
: type(type)
|
|
||||||
{
|
{
|
||||||
size_t pos = 0;
|
size_t pos = 0;
|
||||||
bool isSRI = false;
|
bool isSRI = false;
|
||||||
|
|
||||||
|
// Find the : or - separater, and set `isSRI` to the correct value
|
||||||
auto sep = s.find(':');
|
auto sep = s.find(':');
|
||||||
if (sep == string::npos) {
|
if (sep == string::npos) {
|
||||||
sep = s.find('-');
|
sep = s.find('-');
|
||||||
if (sep != string::npos) {
|
if (sep != string::npos)
|
||||||
isSRI = true;
|
isSRI = true;
|
||||||
} else if (! type)
|
|
||||||
throw BadHash("hash '%s' does not include a type", s);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Parse the has type before the separater, if there was one.
|
||||||
|
std::optional<HashType> optParsedType;
|
||||||
if (sep != string::npos) {
|
if (sep != string::npos) {
|
||||||
string hts = string(s, 0, sep);
|
auto hts = s.substr(0, sep);
|
||||||
this->type = parseHashType(hts);
|
auto optParsedType = parseHashType(hts);
|
||||||
if (!this->type)
|
if (!optParsedType)
|
||||||
throw BadHash("unknown hash type '%s'", hts);
|
throw BadHash("unknown hash type '%s'", hts);
|
||||||
if (type && type != this->type)
|
}
|
||||||
throw BadHash("hash '%s' should have type '%s'", s, printHashType(*type));
|
|
||||||
pos = sep + 1;
|
// Either the string or user must provide the type, if they both do they
|
||||||
|
// must agree.
|
||||||
|
if (!optParsedType && !optType) {
|
||||||
|
throw BadHash("hash '%s' does not include a type, nor is the type otherwise known from context.", s);
|
||||||
|
} else {
|
||||||
|
this->type = optParsedType ? *optParsedType : *optType;
|
||||||
|
if (optParsedType && optType && *optParsedType != *optType)
|
||||||
|
throw BadHash("hash '%s' should have type '%s'", s, printHashType(*optType));
|
||||||
}
|
}
|
||||||
|
|
||||||
init();
|
init();
|
||||||
|
@ -214,7 +217,7 @@ Hash::Hash(std::string_view s, std::optional<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));
|
||||||
}
|
}
|
||||||
|
|
||||||
Hash newHashAllowEmpty(std::string hashStr, std::optional<HashType> ht)
|
Hash newHashAllowEmpty(std::string hashStr, std::optional<HashType> ht)
|
||||||
|
@ -267,7 +270,7 @@ static void finish(HashType ht, Ctx & ctx, unsigned char * hash)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
Hash hashString(HashType ht, const string & s)
|
Hash hashString(HashType ht, std::string_view s)
|
||||||
{
|
{
|
||||||
Ctx ctx;
|
Ctx ctx;
|
||||||
Hash hash(ht);
|
Hash hash(ht);
|
||||||
|
@ -334,7 +337,7 @@ HashResult hashPath(
|
||||||
|
|
||||||
Hash compressHash(const Hash & hash, unsigned int newSize)
|
Hash compressHash(const Hash & hash, unsigned int newSize)
|
||||||
{
|
{
|
||||||
Hash h;
|
Hash h(hash.type);
|
||||||
h.hashSize = newSize;
|
h.hashSize = newSize;
|
||||||
for (unsigned int i = 0; i < hash.hashSize; ++i)
|
for (unsigned int i = 0; i < hash.hashSize; ++i)
|
||||||
h.hash[i % newSize] ^= hash.hash[i];
|
h.hash[i % newSize] ^= hash.hash[i];
|
||||||
|
@ -342,7 +345,7 @@ Hash compressHash(const Hash & hash, unsigned int newSize)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
std::optional<HashType> parseHashTypeOpt(const string & s)
|
std::optional<HashType> parseHashTypeOpt(std::string_view s)
|
||||||
{
|
{
|
||||||
if (s == "md5") return htMD5;
|
if (s == "md5") return htMD5;
|
||||||
else if (s == "sha1") return htSHA1;
|
else if (s == "sha1") return htSHA1;
|
||||||
|
@ -351,7 +354,7 @@ std::optional<HashType> parseHashTypeOpt(const string & s)
|
||||||
else return std::optional<HashType> {};
|
else return std::optional<HashType> {};
|
||||||
}
|
}
|
||||||
|
|
||||||
HashType parseHashType(const string & s)
|
HashType parseHashType(std::string_view s)
|
||||||
{
|
{
|
||||||
auto opt_h = parseHashTypeOpt(s);
|
auto opt_h = parseHashTypeOpt(s);
|
||||||
if (opt_h)
|
if (opt_h)
|
||||||
|
|
|
@ -25,14 +25,11 @@ enum Base : int { Base64, Base32, Base16, SRI };
|
||||||
|
|
||||||
struct Hash
|
struct Hash
|
||||||
{
|
{
|
||||||
static const unsigned int maxHashSize = 64;
|
constexpr static size_t maxHashSize = 64;
|
||||||
unsigned int hashSize = 0;
|
size_t hashSize = 0;
|
||||||
unsigned char hash[maxHashSize] = {};
|
uint8_t hash[maxHashSize] = {};
|
||||||
|
|
||||||
std::optional<HashType> type = {};
|
HashType type;
|
||||||
|
|
||||||
/* Create an unset hash object. */
|
|
||||||
Hash() { };
|
|
||||||
|
|
||||||
/* Create a zero-filled hash object. */
|
/* Create a zero-filled hash object. */
|
||||||
Hash(HashType type) : type(type) { init(); };
|
Hash(HashType type) : type(type) { init(); };
|
||||||
|
@ -105,7 +102,7 @@ Hash newHashAllowEmpty(std::string hashStr, std::optional<HashType> ht);
|
||||||
string printHash16or32(const Hash & hash);
|
string printHash16or32(const Hash & hash);
|
||||||
|
|
||||||
/* Compute the hash of the given string. */
|
/* Compute the hash of the given string. */
|
||||||
Hash hashString(HashType ht, const string & s);
|
Hash hashString(HashType ht, std::string_view s);
|
||||||
|
|
||||||
/* Compute the hash of the given file. */
|
/* Compute the hash of the given file. */
|
||||||
Hash hashFile(HashType ht, const Path & path);
|
Hash hashFile(HashType ht, const Path & path);
|
||||||
|
@ -121,9 +118,9 @@ HashResult hashPath(HashType ht, const Path & path,
|
||||||
Hash compressHash(const Hash & hash, unsigned int newSize);
|
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(std::string_view s);
|
||||||
/* Will return nothing on parse error */
|
/* Will return nothing on parse error */
|
||||||
std::optional<HashType> parseHashTypeOpt(const string & s);
|
std::optional<HashType> parseHashTypeOpt(std::string_view s);
|
||||||
|
|
||||||
/* And the reverse. */
|
/* And the reverse. */
|
||||||
string printHashType(HashType ht);
|
string printHashType(HashType ht);
|
||||||
|
|
|
@ -153,7 +153,7 @@ static int _main(int argc, char * * argv)
|
||||||
|
|
||||||
/* If an expected hash is given, the file may already exist in
|
/* If an expected hash is given, the file may already exist in
|
||||||
the store. */
|
the store. */
|
||||||
Hash hash, expectedHash(ht);
|
Hash hash(ht), expectedHash(ht);
|
||||||
std::optional<StorePath> storePath;
|
std::optional<StorePath> storePath;
|
||||||
if (args.size() == 2) {
|
if (args.size() == 2) {
|
||||||
expectedHash = Hash(args[1], ht);
|
expectedHash = Hash(args[1], ht);
|
||||||
|
|
|
@ -372,8 +372,8 @@ static void opQuery(Strings opFlags, Strings opArgs)
|
||||||
for (auto & j : maybeUseOutputs(store->followLinksToStorePath(i), useOutput, forceRealise)) {
|
for (auto & j : maybeUseOutputs(store->followLinksToStorePath(i), useOutput, forceRealise)) {
|
||||||
auto info = store->queryPathInfo(j);
|
auto info = store->queryPathInfo(j);
|
||||||
if (query == qHash) {
|
if (query == qHash) {
|
||||||
assert(info->narHash.type == htSHA256);
|
assert(info->narHash && info->narHash->type == htSHA256);
|
||||||
cout << fmt("%s\n", info->narHash.to_string(Base32, true));
|
cout << fmt("%s\n", info->narHash->to_string(Base32, true));
|
||||||
} else if (query == qSize)
|
} else if (query == qSize)
|
||||||
cout << fmt("%d\n", info->narSize);
|
cout << fmt("%d\n", info->narSize);
|
||||||
}
|
}
|
||||||
|
@ -725,7 +725,7 @@ static void opVerifyPath(Strings opFlags, Strings opArgs)
|
||||||
auto path = store->followLinksToStorePath(i);
|
auto path = store->followLinksToStorePath(i);
|
||||||
printMsg(lvlTalkative, "checking path '%s'...", store->printStorePath(path));
|
printMsg(lvlTalkative, "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) {
|
||||||
|
@ -734,7 +734,7 @@ static void opVerifyPath(Strings opFlags, Strings opArgs)
|
||||||
.hint = hintfmt(
|
.hint = hintfmt(
|
||||||
"path '%s' was modified! expected hash '%s', got '%s'",
|
"path '%s' was modified! expected hash '%s', got '%s'",
|
||||||
store->printStorePath(path),
|
store->printStorePath(path),
|
||||||
info->narHash.to_string(Base32, true),
|
info->narHash->to_string(Base32, true),
|
||||||
current.first.to_string(Base32, true))
|
current.first.to_string(Base32, true))
|
||||||
});
|
});
|
||||||
status = 1;
|
status = 1;
|
||||||
|
@ -864,7 +864,7 @@ static void opServe(Strings opFlags, Strings opArgs)
|
||||||
out << info->narSize // downloadSize
|
out << info->narSize // downloadSize
|
||||||
<< info->narSize;
|
<< info->narSize;
|
||||||
if (GET_PROTOCOL_MINOR(clientVersion) >= 4)
|
if (GET_PROTOCOL_MINOR(clientVersion) >= 4)
|
||||||
out << (info->narHash ? info->narHash.to_string(Base32, true) : "") << info->ca << info->sigs;
|
out << (info->narHash ? info->narHash->to_string(Base32, true) : "") << info->ca << info->sigs;
|
||||||
} catch (InvalidPath &) {
|
} catch (InvalidPath &) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -46,9 +46,9 @@ struct CmdAddToStore : MixDryRun, StoreCommand
|
||||||
auto narHash = hashString(htSHA256, *sink.s);
|
auto narHash = hashString(htSHA256, *sink.s);
|
||||||
|
|
||||||
ValidPathInfo info(store->makeFixedOutputPath(FileIngestionMethod::Recursive, narHash, *namePart));
|
ValidPathInfo info(store->makeFixedOutputPath(FileIngestionMethod::Recursive, narHash, *namePart));
|
||||||
info.narHash = narHash;
|
*info.narHash = narHash;
|
||||||
info.narSize = sink.s->size();
|
info.narSize = sink.s->size();
|
||||||
info.ca = makeFixedOutputCA(FileIngestionMethod::Recursive, info.narHash);
|
info.ca = makeFixedOutputCA(FileIngestionMethod::Recursive, *info.narHash);
|
||||||
|
|
||||||
if (!dryRun) {
|
if (!dryRun) {
|
||||||
auto source = StringSource { *sink.s };
|
auto source = StringSource { *sink.s };
|
||||||
|
|
|
@ -139,7 +139,7 @@ StorePath getDerivationEnvironment(ref<Store> store, const StorePath & drvPath)
|
||||||
.path = shellOutPath,
|
.path = shellOutPath,
|
||||||
.hash = DerivationOutputHash {
|
.hash = DerivationOutputHash {
|
||||||
.method = FileIngestionMethod::Flat,
|
.method = FileIngestionMethod::Flat,
|
||||||
.hash = Hash { },
|
.hash = Hash { htSHA256 },
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
drv.env["out"] = store->printStorePath(shellOutPath);
|
drv.env["out"] = store->printStorePath(shellOutPath);
|
||||||
|
|
Loading…
Reference in a new issue