aboutsummaryrefslogtreecommitdiff
path: root/src/libstore/content-address.hh
diff options
context:
space:
mode:
authorCarlo Nucera <carlo.nucera@protonmail.com>2020-06-02 15:44:58 -0400
committerCarlo Nucera <carlo.nucera@protonmail.com>2020-06-02 15:44:58 -0400
commitfd2eb41e6433e72516ae149949b8b0050305293d (patch)
tree51c993342b3a1a66194e5c5a58359677cb6d9c2b /src/libstore/content-address.hh
parent343c20a404055d266227b7562f775f7fad37103f (diff)
Move file-hash to content-address
Diffstat (limited to 'src/libstore/content-address.hh')
-rw-r--r--src/libstore/content-address.hh66
1 files changed, 66 insertions, 0 deletions
diff --git a/src/libstore/content-address.hh b/src/libstore/content-address.hh
new file mode 100644
index 000000000..64d514751
--- /dev/null
+++ b/src/libstore/content-address.hh
@@ -0,0 +1,66 @@
+#pragma once
+
+#include <variant>
+#include "hash.hh"
+
+namespace nix {
+
+enum struct FileIngestionMethod : uint8_t {
+ Flat = false,
+ Recursive = true
+};
+
+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);
+
+}