2021-07-26 11:31:09 +00:00
|
|
|
#include "path-info.hh"
|
|
|
|
#include "worker-protocol.hh"
|
2022-03-08 21:53:26 +00:00
|
|
|
#include "worker-protocol-impl.hh"
|
2022-03-08 22:03:03 +00:00
|
|
|
#include "store-api.hh"
|
2021-07-26 11:31:09 +00:00
|
|
|
|
|
|
|
namespace nix {
|
|
|
|
|
2023-01-13 20:23:29 +00: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 21:38:43 +00:00
|
|
|
+ concatStringsSep(",", store.printStorePathSet(references));
|
2023-01-13 20:23:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void ValidPathInfo::sign(const Store & store, const SecretKey & secretKey)
|
|
|
|
{
|
|
|
|
sigs.insert(secretKey.signDetached(fingerprint(store)));
|
|
|
|
}
|
|
|
|
|
2023-02-28 16:34:18 +00:00
|
|
|
std::optional<ContentAddressWithReferences> ValidPathInfo::contentAddressWithReferences() const
|
2023-01-14 19:27:28 +00:00
|
|
|
{
|
|
|
|
if (! ca)
|
|
|
|
return std::nullopt;
|
|
|
|
|
2023-01-23 17:58:11 +00:00
|
|
|
return std::visit(overloaded {
|
|
|
|
[&](const TextHash & th) -> ContentAddressWithReferences {
|
|
|
|
assert(references.count(path) == 0);
|
|
|
|
return TextInfo {
|
2023-02-28 17:13:43 +00:00
|
|
|
.hash = th,
|
2023-02-28 16:57:20 +00:00
|
|
|
.references = references,
|
2023-01-23 17:58:11 +00: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 17:13:43 +00:00
|
|
|
.hash = foh,
|
2023-02-28 16:57:20 +00:00
|
|
|
.references = {
|
2023-01-23 17:58:11 +00:00
|
|
|
.others = std::move(refs),
|
|
|
|
.self = hasSelfReference,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
},
|
2023-03-30 21:12:49 +00:00
|
|
|
}, ca->raw);
|
2023-01-14 19:27:28 +00:00
|
|
|
}
|
2023-01-13 20:23:29 +00:00
|
|
|
|
|
|
|
bool ValidPathInfo::isContentAddressed(const Store & store) const
|
|
|
|
{
|
2023-02-28 16:34:18 +00:00
|
|
|
auto fullCaOpt = contentAddressWithReferences();
|
2023-01-13 20:23:29 +00:00
|
|
|
|
2023-01-14 19:27:28 +00:00
|
|
|
if (! fullCaOpt)
|
|
|
|
return false;
|
|
|
|
|
2023-01-23 17:58:11 +00:00
|
|
|
auto caPath = store.makeFixedOutputPathFromCA(path.name(), *fullCaOpt);
|
2023-01-13 20:23:29 +00: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 21:38:43 +00:00
|
|
|
for (auto & r : references)
|
2023-01-13 20:23:29 +00:00
|
|
|
refs.push_back(std::string(r.to_string()));
|
|
|
|
return refs;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-01-14 19:27:28 +00:00
|
|
|
ValidPathInfo::ValidPathInfo(
|
|
|
|
const Store & store,
|
2023-01-23 17:58:11 +00:00
|
|
|
std::string_view name,
|
|
|
|
ContentAddressWithReferences && ca,
|
2023-01-14 19:27:28 +00:00
|
|
|
Hash narHash)
|
2023-01-23 17:58:11 +00:00
|
|
|
: path(store.makeFixedOutputPathFromCA(name, ca))
|
2023-01-14 19:27:28 +00:00
|
|
|
, narHash(narHash)
|
|
|
|
{
|
|
|
|
std::visit(overloaded {
|
|
|
|
[this](TextInfo && ti) {
|
2023-01-14 21:38:43 +00:00
|
|
|
this->references = std::move(ti.references);
|
2023-01-14 19:27:28 +00:00
|
|
|
this->ca = std::move((TextHash &&) ti);
|
|
|
|
},
|
|
|
|
[this](FixedOutputInfo && foi) {
|
2023-01-14 21:38:43 +00:00
|
|
|
this->references = std::move(foi.references.others);
|
|
|
|
if (foi.references.self)
|
|
|
|
this->references.insert(path);
|
2023-01-14 19:27:28 +00:00
|
|
|
this->ca = std::move((FixedOutputHash &&) foi);
|
|
|
|
},
|
2023-03-30 21:12:49 +00:00
|
|
|
}, std::move(ca).raw);
|
2023-01-14 19:27:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-07-26 11:31:09 +00: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-04-17 17:40:46 +00:00
|
|
|
info.references = WorkerProto::Serialise<StorePathSet>::read(store,
|
|
|
|
WorkerProto::ReadConn { .from = source });
|
2021-07-26 11:31:09 +00:00
|
|
|
source >> info.registrationTime >> info.narSize;
|
|
|
|
if (format >= 16) {
|
|
|
|
source >> info.ultimate;
|
|
|
|
info.sigs = readStrings<StringSet>(source);
|
2023-03-30 21:12:49 +00:00
|
|
|
info.ca = ContentAddress::parseOpt(readString(source));
|
2021-07-26 11:31:09 +00:00
|
|
|
}
|
|
|
|
return info;
|
|
|
|
}
|
|
|
|
|
2023-01-13 20:23:29 +00:00
|
|
|
|
2021-07-26 11:31:09 +00: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-04-17 17:40:46 +00:00
|
|
|
WorkerProto::write(store,
|
|
|
|
WorkerProto::WriteConn { .to = sink },
|
|
|
|
references);
|
2021-07-26 11:31:09 +00:00
|
|
|
sink << registrationTime << narSize;
|
|
|
|
if (format >= 16) {
|
|
|
|
sink << ultimate
|
|
|
|
<< sigs
|
|
|
|
<< renderContentAddress(ca);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|