2021-07-26 11:31:09 +00:00
|
|
|
#include "path-info.hh"
|
2022-03-08 22:03:03 +00:00
|
|
|
#include "store-api.hh"
|
2021-07-26 11:31:09 +00:00
|
|
|
|
|
|
|
namespace nix {
|
|
|
|
|
2024-03-04 03:59:31 +00:00
|
|
|
GENERATE_CMP_EXT(
|
|
|
|
,
|
|
|
|
UnkeyedValidPathInfo,
|
|
|
|
me->deriver,
|
|
|
|
me->narHash,
|
|
|
|
me->references,
|
|
|
|
me->registrationTime,
|
|
|
|
me->narSize,
|
|
|
|
//me->id,
|
|
|
|
me->ultimate,
|
|
|
|
me->sigs,
|
|
|
|
me->ca);
|
|
|
|
|
|
|
|
GENERATE_CMP_EXT(
|
|
|
|
,
|
|
|
|
ValidPathInfo,
|
|
|
|
me->path,
|
|
|
|
static_cast<const UnkeyedValidPathInfo &>(*me));
|
|
|
|
|
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 {
|
2023-07-05 22:53:44 +00:00
|
|
|
[&](const TextIngestionMethod &) -> ContentAddressWithReferences {
|
2023-01-23 17:58:11 +00:00
|
|
|
assert(references.count(path) == 0);
|
|
|
|
return TextInfo {
|
2023-07-05 22:53:44 +00:00
|
|
|
.hash = ca->hash,
|
2023-02-28 16:57:20 +00:00
|
|
|
.references = references,
|
2023-01-23 17:58:11 +00:00
|
|
|
};
|
|
|
|
},
|
2023-07-05 22:53:44 +00:00
|
|
|
[&](const FileIngestionMethod & m2) -> ContentAddressWithReferences {
|
2023-01-23 17:58:11 +00:00
|
|
|
auto refs = references;
|
|
|
|
bool hasSelfReference = false;
|
|
|
|
if (refs.count(path)) {
|
|
|
|
hasSelfReference = true;
|
|
|
|
refs.erase(path);
|
|
|
|
}
|
|
|
|
return FixedOutputInfo {
|
2023-07-05 22:53:44 +00:00
|
|
|
.method = m2,
|
|
|
|
.hash = ca->hash,
|
2023-02-28 16:57:20 +00:00
|
|
|
.references = {
|
2023-01-23 17:58:11 +00:00
|
|
|
.others = std::move(refs),
|
|
|
|
.self = hasSelfReference,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
},
|
2023-07-05 22:53:44 +00:00
|
|
|
}, ca->method.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)
|
2024-03-04 03:59:31 +00:00
|
|
|
: UnkeyedValidPathInfo(narHash)
|
|
|
|
, path(store.makeFixedOutputPathFromCA(name, ca))
|
2023-01-14 19:27:28 +00:00
|
|
|
{
|
|
|
|
std::visit(overloaded {
|
|
|
|
[this](TextInfo && ti) {
|
2023-01-14 21:38:43 +00:00
|
|
|
this->references = std::move(ti.references);
|
2023-07-05 22:53:44 +00:00
|
|
|
this->ca = ContentAddress {
|
|
|
|
.method = TextIngestionMethod {},
|
|
|
|
.hash = std::move(ti.hash),
|
|
|
|
};
|
2023-01-14 19:27:28 +00:00
|
|
|
},
|
|
|
|
[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-07-05 22:53:44 +00:00
|
|
|
this->ca = ContentAddress {
|
|
|
|
.method = std::move(foi.method),
|
|
|
|
.hash = std::move(foi.hash),
|
|
|
|
};
|
2023-01-14 19:27:28 +00:00
|
|
|
},
|
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
|
|
|
}
|