Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'origin/master' into substitute-other-st…
…oredir
- Loading branch information
Showing
29 changed files
with
278 additions
and
199 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
#include "content-address.hh" | ||
|
||
namespace nix { | ||
|
||
std::string FixedOutputHash::printMethodAlgo() const { | ||
return makeFileIngestionPrefix(method) + printHashType(*hash.type); | ||
} | ||
|
||
std::string makeFileIngestionPrefix(const FileIngestionMethod m) { | ||
switch (m) { | ||
case FileIngestionMethod::Flat: | ||
return ""; | ||
case FileIngestionMethod::Recursive: | ||
return "r:"; | ||
default: | ||
throw Error("impossible, caught both cases"); | ||
} | ||
} | ||
|
||
std::string makeFixedOutputCA(FileIngestionMethod method, const Hash & hash) | ||
{ | ||
return "fixed:" | ||
+ makeFileIngestionPrefix(method) | ||
+ hash.to_string(Base32, true); | ||
} | ||
|
||
// FIXME Put this somewhere? | ||
template<class... Ts> struct overloaded : Ts... { using Ts::operator()...; }; | ||
template<class... Ts> overloaded(Ts...) -> overloaded<Ts...>; | ||
|
||
std::string renderContentAddress(ContentAddress ca) { | ||
return std::visit(overloaded { | ||
[](TextHash th) { | ||
return "text:" + th.hash.to_string(Base32, true); | ||
}, | ||
[](FixedOutputHash fsh) { | ||
return makeFixedOutputCA(fsh.method, fsh.hash); | ||
} | ||
}, ca); | ||
} | ||
|
||
ContentAddress parseContentAddress(std::string_view rawCa) { | ||
auto prefixSeparator = rawCa.find(':'); | ||
if (prefixSeparator != string::npos) { | ||
auto prefix = string(rawCa, 0, prefixSeparator); | ||
if (prefix == "text") { | ||
auto hashTypeAndHash = rawCa.substr(prefixSeparator+1, string::npos); | ||
Hash hash = Hash(string(hashTypeAndHash)); | ||
if (*hash.type != htSHA256) { | ||
throw Error("parseContentAddress: the text hash should have type SHA256"); | ||
} | ||
return TextHash { hash }; | ||
} else if (prefix == "fixed") { | ||
// This has to be an inverse of makeFixedOutputCA | ||
auto methodAndHash = rawCa.substr(prefixSeparator+1, string::npos); | ||
if (methodAndHash.substr(0,2) == "r:") { | ||
std::string_view hashRaw = methodAndHash.substr(2,string::npos); | ||
return FixedOutputHash { | ||
.method = FileIngestionMethod::Recursive, | ||
.hash = Hash(string(hashRaw)), | ||
}; | ||
} else { | ||
std::string_view hashRaw = methodAndHash; | ||
return FixedOutputHash { | ||
.method = FileIngestionMethod::Flat, | ||
.hash = Hash(string(hashRaw)), | ||
}; | ||
} | ||
} else { | ||
throw Error("parseContentAddress: format not recognized; has to be text or fixed"); | ||
} | ||
} else { | ||
throw Error("Not a content address because it lacks an appropriate prefix"); | ||
} | ||
}; | ||
|
||
std::optional<ContentAddress> parseContentAddressOpt(std::string_view rawCaOpt) { | ||
return rawCaOpt == "" ? std::optional<ContentAddress> {} : parseContentAddress(rawCaOpt); | ||
}; | ||
|
||
std::string renderContentAddress(std::optional<ContentAddress> ca) { | ||
return ca ? renderContentAddress(*ca) : ""; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
#pragma once | ||
|
||
#include <variant> | ||
#include "hash.hh" | ||
|
||
namespace nix { | ||
|
||
enum struct FileIngestionMethod : uint8_t { | ||
Flat = false, | ||
Recursive = true | ||
}; | ||
|
||
struct TextHash { | ||
Hash hash; | ||
}; | ||
|
||
/// Pair of a hash, and how the file system was ingested | ||
struct FixedOutputHash { | ||
FileIngestionMethod method; | ||
Hash hash; | ||
std::string printMethodAlgo() const; | ||
}; | ||
|
||
/* | ||
We've accumulated several types of content-addressed paths over the years; | ||
fixed-output derivations support multiple hash algorithms and serialisation | ||
methods (flat file vs NAR). Thus, ‘ca’ has one of the following forms: | ||
* ‘text:sha256:<sha256 hash of file contents>’: For paths | ||
computed by makeTextPath() / addTextToStore(). | ||
* ‘fixed:<r?>:<ht>:<h>’: For paths computed by | ||
makeFixedOutputPath() / addToStore(). | ||
*/ | ||
typedef std::variant< | ||
TextHash, // for paths computed by makeTextPath() / addTextToStore | ||
FixedOutputHash // for path computed by makeFixedOutputPath | ||
> ContentAddress; | ||
|
||
/* Compute the prefix to the hash algorithm which indicates how the files were | ||
ingested. */ | ||
std::string makeFileIngestionPrefix(const FileIngestionMethod m); | ||
|
||
/* Compute the content-addressability assertion (ValidPathInfo::ca) | ||
for paths created by makeFixedOutputPath() / addToStore(). */ | ||
std::string makeFixedOutputCA(FileIngestionMethod method, const Hash & hash); | ||
|
||
std::string renderContentAddress(ContentAddress ca); | ||
|
||
std::string renderContentAddress(std::optional<ContentAddress> ca); | ||
|
||
ContentAddress parseContentAddress(std::string_view rawCa); | ||
|
||
std::optional<ContentAddress> parseContentAddressOpt(std::string_view rawCaOpt); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.