This commit is contained in:
Carlo Nucera 2020-06-01 18:53:31 -04:00
parent 0e9438b6d3
commit da39092a39
5 changed files with 47 additions and 16 deletions

View file

@ -1,5 +1,6 @@
#pragma once
#include <variant>
#include "hash.hh"
namespace nix {
@ -23,6 +24,22 @@ struct FileSystemHash {
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<
Hash, // 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);

View file

@ -1,6 +1,7 @@
#pragma once
#include "rust-ffi.hh"
#include "file-hash.hh"
namespace nix {
@ -87,6 +88,15 @@ const size_t storePathHashLen = 32; // i.e. 160 bits
/* Extension of derivations in the Nix store. */
const std::string drvExtension = ".drv";
std::string to_string(FileIngestionMethod m) {
switch(m) {
case FileIngestionMethod::Flat:
return "false";
case FileIngestionMethod::Recursive:
return "true";
}
}
struct StorePathWithOutputs
{
StorePath path;

View file

@ -467,7 +467,7 @@ void Store::pathInfoToJSON(JSONPlaceholder & jsonOut, const StorePathSet & store
jsonRefs.elem(printStorePath(ref));
}
if (info->ca != "")
if (info->ca)
jsonPath.attr("ca", info->ca);
std::pair<uint64_t, uint64_t> closureSizes;

View file

@ -19,6 +19,7 @@
#include <memory>
#include <string>
#include <chrono>
#include <variant>
namespace nix {
@ -110,6 +111,19 @@ struct SubstitutablePathInfo
typedef std::map<StorePath, SubstitutablePathInfo> SubstitutablePathInfos;
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 {
[](Hash hash) {
return "text:" + hash.to_string();
},
[](FileSystemHash fsh) {
return makeFixedOutputCA(fsh.method, fsh.hash);
}
}, ca);
}
struct ValidPathInfo
{
@ -139,21 +153,11 @@ struct ValidPathInfo
that a particular output path was produced by a derivation; the
path then implies the contents.)
Ideally, the content-addressability assertion would just be a
Boolean, and the store path would be computed from
the name component, narHash and references. However,
1) we've accumulated several types of content-addressed paths
over the years; and 2) 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().
Ideally, the content-addressability assertion would just be a Boolean,
and the store path would be computed from the name component, narHash
and references. However, we support many types of content addresses.
*/
std::string ca;
std::optional<ContentAddress> ca;
bool operator == (const ValidPathInfo & i) const
{

View file

@ -856,7 +856,7 @@ static void opServe(Strings opFlags, Strings opArgs)
out << info->narSize // downloadSize
<< info->narSize;
if (GET_PROTOCOL_MINOR(clientVersion) >= 4)
out << (info->narHash ? info->narHash.to_string() : "") << info->ca << info->sigs;
out << (info->narHash ? info->narHash.to_string() : "") << renderContentAddress(info->ca) << info->sigs;
} catch (InvalidPath &) {
}
}