forked from NixOS/nix
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from obsidiansystems/git+validPathInfo-ca-prope…
…r-datatype valid path info ca proper datatype
- Loading branch information
Showing
24 changed files
with
243 additions
and
156 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
#include "content-address.hh" | ||
|
||
namespace nix { | ||
|
||
std::string FileSystemHash::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:"; | ||
case FileIngestionMethod::Git: | ||
return "git:"; | ||
} | ||
abort(); | ||
} | ||
|
||
std::string makeFixedOutputCA(FileIngestionMethod method, const Hash & hash) | ||
{ | ||
return "fixed:" | ||
+ makeFileIngestionPrefix(method) | ||
+ hash.to_string(); | ||
} | ||
|
||
// 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(); | ||
}, | ||
[](FileSystemHash 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 != HashType::SHA256) { | ||
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 FileSystemHash { FileIngestionMethod::Recursive, Hash(string(hashRaw)) }; | ||
} else if (methodAndHash.substr(0, 4) == "git:") { | ||
std::string_view hashRaw = methodAndHash.substr(4, string::npos); | ||
return FileSystemHash { FileIngestionMethod::Git, Hash(string(hashRaw)) }; | ||
} else { | ||
std::string_view hashRaw = methodAndHash; | ||
return FileSystemHash { FileIngestionMethod::Flat, 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,68 @@ | ||
#pragma once | ||
|
||
#include <variant> | ||
#include "hash.hh" | ||
|
||
namespace nix { | ||
|
||
enum struct FileIngestionMethod : uint8_t { | ||
Flat, | ||
Recursive, | ||
Git, | ||
}; | ||
|
||
|
||
struct TextHash { | ||
Hash hash; | ||
TextHash(const TextHash &) = default; | ||
TextHash(TextHash &&) = default; | ||
TextHash & operator = (const TextHash &) = default; | ||
}; | ||
|
||
/// Pair of a hash, and how the file system was ingested | ||
struct FileSystemHash { | ||
FileIngestionMethod method; | ||
Hash hash; | ||
FileSystemHash(FileIngestionMethod method, Hash hash) | ||
: method(std::move(method)) | ||
, hash(std::move(hash)) | ||
{ } | ||
FileSystemHash(const FileSystemHash &) = default; | ||
FileSystemHash(FileSystemHash &&) = default; | ||
FileSystemHash & operator = (const FileSystemHash &) = default; | ||
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 | ||
FileSystemHash // 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
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.