2021-07-26 14:31:09 +03:00
|
|
|
#include "path-info.hh"
|
|
|
|
#include "worker-protocol.hh"
|
2022-03-09 00:03:03 +02:00
|
|
|
#include "store-api.hh"
|
2021-07-26 14:31:09 +03:00
|
|
|
|
|
|
|
namespace nix {
|
|
|
|
|
2023-01-13 22:23:29 +02:00
|
|
|
std::string ValidPathInfo::fingerprint(const Store & store) const
|
|
|
|
{
|
|
|
|
if (narSize == 0)
|
|
|
|
throw Error("cannot calculate fingerprint of path '%s' because its size is not known",
|
|
|
|
store.printStorePath(path));
|
|
|
|
return
|
|
|
|
"1;" + store.printStorePath(path) + ";"
|
|
|
|
+ narHash.to_string(Base32, true) + ";"
|
|
|
|
+ std::to_string(narSize) + ";"
|
2023-01-14 23:38:43 +02:00
|
|
|
+ concatStringsSep(",", store.printStorePathSet(references));
|
2023-01-13 22:23:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void ValidPathInfo::sign(const Store & store, const SecretKey & secretKey)
|
|
|
|
{
|
|
|
|
sigs.insert(secretKey.signDetached(fingerprint(store)));
|
|
|
|
}
|
|
|
|
|
2023-02-28 18:34:18 +02:00
|
|
|
std::optional<ContentAddressWithReferences> ValidPathInfo::contentAddressWithReferences() const
|
2023-01-14 21:27:28 +02:00
|
|
|
{
|
|
|
|
if (! ca)
|
|
|
|
return std::nullopt;
|
|
|
|
|
2023-01-23 19:58:11 +02:00
|
|
|
return std::visit(overloaded {
|
|
|
|
[&](const TextHash & th) -> ContentAddressWithReferences {
|
|
|
|
assert(references.count(path) == 0);
|
|
|
|
return TextInfo {
|
2023-02-28 19:13:43 +02:00
|
|
|
.hash = th,
|
2023-02-28 18:57:20 +02:00
|
|
|
.references = references,
|
2023-01-23 19:58:11 +02:00
|
|
|
};
|
|
|
|
},
|
|
|
|
[&](const FixedOutputHash & foh) -> ContentAddressWithReferences {
|
|
|
|
auto refs = references;
|
|
|
|
bool hasSelfReference = false;
|
|
|
|
if (refs.count(path)) {
|
|
|
|
hasSelfReference = true;
|
|
|
|
refs.erase(path);
|
|
|
|
}
|
|
|
|
return FixedOutputInfo {
|
2023-02-28 19:13:43 +02:00
|
|
|
.hash = foh,
|
2023-02-28 18:57:20 +02:00
|
|
|
.references = {
|
2023-01-23 19:58:11 +02:00
|
|
|
.others = std::move(refs),
|
|
|
|
.self = hasSelfReference,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
},
|
2023-03-31 00:12:49 +03:00
|
|
|
}, ca->raw);
|
2023-01-14 21:27:28 +02:00
|
|
|
}
|
2023-01-13 22:23:29 +02:00
|
|
|
|
|
|
|
bool ValidPathInfo::isContentAddressed(const Store & store) const
|
|
|
|
{
|
2023-02-28 18:34:18 +02:00
|
|
|
auto fullCaOpt = contentAddressWithReferences();
|
2023-01-13 22:23:29 +02:00
|
|
|
|
2023-01-14 21:27:28 +02:00
|
|
|
if (! fullCaOpt)
|
|
|
|
return false;
|
|
|
|
|
2023-01-23 19:58:11 +02:00
|
|
|
auto caPath = store.makeFixedOutputPathFromCA(path.name(), *fullCaOpt);
|
2023-01-13 22:23:29 +02:00
|
|
|
|
|
|
|
bool res = caPath == path;
|
|
|
|
|
|
|
|
if (!res)
|
|
|
|
printError("warning: path '%s' claims to be content-addressed but isn't", store.printStorePath(path));
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
size_t ValidPathInfo::checkSignatures(const Store & store, const PublicKeys & publicKeys) const
|
|
|
|
{
|
|
|
|
if (isContentAddressed(store)) return maxSigs;
|
|
|
|
|
|
|
|
size_t good = 0;
|
|
|
|
for (auto & sig : sigs)
|
|
|
|
if (checkSignature(store, publicKeys, sig))
|
|
|
|
good++;
|
|
|
|
return good;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool ValidPathInfo::checkSignature(const Store & store, const PublicKeys & publicKeys, const std::string & sig) const
|
|
|
|
{
|
|
|
|
return verifyDetached(fingerprint(store), sig, publicKeys);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Strings ValidPathInfo::shortRefs() const
|
|
|
|
{
|
|
|
|
Strings refs;
|
2023-01-14 23:38:43 +02:00
|
|
|
for (auto & r : references)
|
2023-01-13 22:23:29 +02:00
|
|
|
refs.push_back(std::string(r.to_string()));
|
|
|
|
return refs;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-01-14 21:27:28 +02:00
|
|
|
ValidPathInfo::ValidPathInfo(
|
|
|
|
const Store & store,
|
2023-01-23 19:58:11 +02:00
|
|
|
std::string_view name,
|
|
|
|
ContentAddressWithReferences && ca,
|
2023-01-14 21:27:28 +02:00
|
|
|
Hash narHash)
|
2023-01-23 19:58:11 +02:00
|
|
|
: path(store.makeFixedOutputPathFromCA(name, ca))
|
2023-01-14 21:27:28 +02:00
|
|
|
, narHash(narHash)
|
|
|
|
{
|
|
|
|
std::visit(overloaded {
|
|
|
|
[this](TextInfo && ti) {
|
2023-01-14 23:38:43 +02:00
|
|
|
this->references = std::move(ti.references);
|
2023-01-14 21:27:28 +02:00
|
|
|
this->ca = std::move((TextHash &&) ti);
|
|
|
|
},
|
|
|
|
[this](FixedOutputInfo && foi) {
|
2023-01-14 23:38:43 +02:00
|
|
|
this->references = std::move(foi.references.others);
|
|
|
|
if (foi.references.self)
|
|
|
|
this->references.insert(path);
|
2023-01-14 21:27:28 +02:00
|
|
|
this->ca = std::move((FixedOutputHash &&) foi);
|
|
|
|
},
|
2023-03-31 00:12:49 +03:00
|
|
|
}, std::move(ca).raw);
|
2023-01-14 21:27:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-07-26 14:31:09 +03:00
|
|
|
ValidPathInfo ValidPathInfo::read(Source & source, const Store & store, unsigned int format)
|
|
|
|
{
|
|
|
|
return read(source, store, format, store.parseStorePath(readString(source)));
|
|
|
|
}
|
|
|
|
|
|
|
|
ValidPathInfo ValidPathInfo::read(Source & source, const Store & store, unsigned int format, StorePath && path)
|
|
|
|
{
|
|
|
|
auto deriver = readString(source);
|
|
|
|
auto narHash = Hash::parseAny(readString(source), htSHA256);
|
|
|
|
ValidPathInfo info(path, narHash);
|
|
|
|
if (deriver != "") info.deriver = store.parseStorePath(deriver);
|
2023-05-18 05:04:59 +03:00
|
|
|
info.references = WorkerProto<StorePathSet>::read(store, source);
|
2021-07-26 14:31:09 +03:00
|
|
|
source >> info.registrationTime >> info.narSize;
|
|
|
|
if (format >= 16) {
|
|
|
|
source >> info.ultimate;
|
|
|
|
info.sigs = readStrings<StringSet>(source);
|
2023-03-31 00:12:49 +03:00
|
|
|
info.ca = ContentAddress::parseOpt(readString(source));
|
2021-07-26 14:31:09 +03:00
|
|
|
}
|
|
|
|
return info;
|
|
|
|
}
|
|
|
|
|
2023-01-13 22:23:29 +02:00
|
|
|
|
2021-07-26 14:31:09 +03:00
|
|
|
void ValidPathInfo::write(
|
|
|
|
Sink & sink,
|
|
|
|
const Store & store,
|
|
|
|
unsigned int format,
|
|
|
|
bool includePath) const
|
|
|
|
{
|
|
|
|
if (includePath)
|
|
|
|
sink << store.printStorePath(path);
|
|
|
|
sink << (deriver ? store.printStorePath(*deriver) : "")
|
|
|
|
<< narHash.to_string(Base16, false);
|
2023-05-18 05:04:59 +03:00
|
|
|
workerProtoWrite(store, sink, references);
|
2021-07-26 14:31:09 +03:00
|
|
|
sink << registrationTime << narSize;
|
|
|
|
if (format >= 16) {
|
|
|
|
sink << ultimate
|
|
|
|
<< sigs
|
|
|
|
<< renderContentAddress(ca);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|