2016-02-16 15:38:44 +00:00
|
|
|
#include "globals.hh"
|
|
|
|
#include "nar-info.hh"
|
2020-08-06 18:31:48 +00:00
|
|
|
#include "store-api.hh"
|
2016-02-16 15:38:44 +00:00
|
|
|
|
|
|
|
namespace nix {
|
|
|
|
|
2016-06-01 12:49:12 +00:00
|
|
|
NarInfo::NarInfo(const Store & store, const std::string & s, const std::string & whence)
|
2020-08-06 18:31:48 +00:00
|
|
|
: ValidPathInfo(StorePath(StorePath::dummy), Hash(Hash::dummy)) // FIXME: hack
|
2016-02-16 15:38:44 +00:00
|
|
|
{
|
2016-05-31 11:23:54 +00:00
|
|
|
auto corrupt = [&]() {
|
2020-06-19 18:41:33 +00:00
|
|
|
return Error("NAR info file '%1%' is corrupt", whence);
|
2016-02-16 15:38:44 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
auto parseHashField = [&](const string & s) {
|
2016-04-20 12:12:38 +00:00
|
|
|
try {
|
2020-07-01 22:34:18 +00:00
|
|
|
return Hash::parseAnyPrefixed(s);
|
2016-04-20 12:12:38 +00:00
|
|
|
} catch (BadHash &) {
|
2020-06-19 18:41:33 +00:00
|
|
|
throw corrupt();
|
2016-04-20 12:12:38 +00:00
|
|
|
}
|
2016-02-16 15:38:44 +00:00
|
|
|
};
|
|
|
|
|
2019-12-05 18:11:09 +00:00
|
|
|
bool havePath = false;
|
2020-08-06 18:31:48 +00:00
|
|
|
bool haveNarHash = false;
|
2019-12-05 18:11:09 +00:00
|
|
|
|
2016-02-16 15:38:44 +00:00
|
|
|
size_t pos = 0;
|
|
|
|
while (pos < s.size()) {
|
|
|
|
|
|
|
|
size_t colon = s.find(':', pos);
|
2020-06-19 18:41:33 +00:00
|
|
|
if (colon == std::string::npos) throw corrupt();
|
2016-02-16 15:38:44 +00:00
|
|
|
|
|
|
|
std::string name(s, pos, colon - pos);
|
|
|
|
|
|
|
|
size_t eol = s.find('\n', colon + 2);
|
2020-06-19 18:41:33 +00:00
|
|
|
if (eol == std::string::npos) throw corrupt();
|
2016-02-16 15:38:44 +00:00
|
|
|
|
|
|
|
std::string value(s, colon + 2, eol - colon - 2);
|
|
|
|
|
|
|
|
if (name == "StorePath") {
|
2019-12-05 18:11:09 +00:00
|
|
|
path = store.parseStorePath(value);
|
|
|
|
havePath = true;
|
2016-02-16 15:38:44 +00:00
|
|
|
}
|
|
|
|
else if (name == "URL")
|
|
|
|
url = value;
|
|
|
|
else if (name == "Compression")
|
|
|
|
compression = value;
|
|
|
|
else if (name == "FileHash")
|
|
|
|
fileHash = parseHashField(value);
|
|
|
|
else if (name == "FileSize") {
|
2020-06-19 18:41:33 +00:00
|
|
|
if (!string2Int(value, fileSize)) throw corrupt();
|
2016-02-16 15:38:44 +00:00
|
|
|
}
|
2020-08-06 18:31:48 +00:00
|
|
|
else if (name == "NarHash") {
|
2016-02-16 15:38:44 +00:00
|
|
|
narHash = parseHashField(value);
|
2020-08-06 18:31:48 +00:00
|
|
|
haveNarHash = true;
|
|
|
|
}
|
2016-02-16 15:38:44 +00:00
|
|
|
else if (name == "NarSize") {
|
2020-06-19 18:41:33 +00:00
|
|
|
if (!string2Int(value, narSize)) throw corrupt();
|
2016-02-16 15:38:44 +00:00
|
|
|
}
|
|
|
|
else if (name == "References") {
|
|
|
|
auto refs = tokenizeString<Strings>(value, " ");
|
2020-06-19 18:41:33 +00:00
|
|
|
if (!references.empty()) throw corrupt();
|
2019-12-05 18:11:09 +00:00
|
|
|
for (auto & r : refs)
|
2020-06-29 19:58:31 +00:00
|
|
|
insertReferencePossiblyToSelf(StorePath(r));
|
2016-02-16 15:38:44 +00:00
|
|
|
}
|
|
|
|
else if (name == "Deriver") {
|
2019-12-05 18:11:09 +00:00
|
|
|
if (value != "unknown-deriver")
|
2020-06-16 12:16:39 +00:00
|
|
|
deriver = StorePath(value);
|
2016-02-16 15:38:44 +00:00
|
|
|
}
|
|
|
|
else if (name == "System")
|
|
|
|
system = value;
|
|
|
|
else if (name == "Sig")
|
2016-03-21 17:05:47 +00:00
|
|
|
sigs.insert(value);
|
2016-08-03 11:17:11 +00:00
|
|
|
else if (name == "CA") {
|
2020-07-02 21:44:18 +00:00
|
|
|
if (ca) throw corrupt();
|
2020-06-02 00:37:43 +00:00
|
|
|
// FIXME: allow blank ca or require skipping field?
|
2020-06-02 15:00:10 +00:00
|
|
|
ca = parseContentAddressOpt(value);
|
2016-08-03 11:17:11 +00:00
|
|
|
}
|
2016-02-16 15:38:44 +00:00
|
|
|
|
|
|
|
pos = eol + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (compression == "") compression = "bzip2";
|
|
|
|
|
2020-08-06 18:31:48 +00:00
|
|
|
if (!havePath || !haveNarHash || url.empty() || narSize == 0) throw corrupt();
|
2016-02-16 15:38:44 +00:00
|
|
|
}
|
|
|
|
|
2019-12-05 18:11:09 +00:00
|
|
|
std::string NarInfo::to_string(const Store & store) const
|
2016-02-16 15:38:44 +00:00
|
|
|
{
|
|
|
|
std::string res;
|
2019-12-05 18:11:09 +00:00
|
|
|
res += "StorePath: " + store.printStorePath(path) + "\n";
|
2016-02-16 15:38:44 +00:00
|
|
|
res += "URL: " + url + "\n";
|
|
|
|
assert(compression != "");
|
|
|
|
res += "Compression: " + compression + "\n";
|
2020-06-19 18:41:33 +00:00
|
|
|
assert(fileHash && fileHash->type == htSHA256);
|
|
|
|
res += "FileHash: " + fileHash->to_string(Base32, true) + "\n";
|
2016-02-16 15:38:44 +00:00
|
|
|
res += "FileSize: " + std::to_string(fileSize) + "\n";
|
2020-08-05 19:30:38 +00:00
|
|
|
assert(narHash.type == htSHA256);
|
2020-08-05 18:42:48 +00:00
|
|
|
res += "NarHash: " + narHash.to_string(Base32, true) + "\n";
|
2016-02-16 15:38:44 +00:00
|
|
|
res += "NarSize: " + std::to_string(narSize) + "\n";
|
|
|
|
|
|
|
|
res += "References: " + concatStringsSep(" ", shortRefs()) + "\n";
|
|
|
|
|
2019-12-05 18:11:09 +00:00
|
|
|
if (deriver)
|
|
|
|
res += "Deriver: " + std::string(deriver->to_string()) + "\n";
|
2016-02-16 15:38:44 +00:00
|
|
|
|
|
|
|
if (!system.empty())
|
|
|
|
res += "System: " + system + "\n";
|
|
|
|
|
2016-03-21 17:05:47 +00:00
|
|
|
for (auto sig : sigs)
|
2016-02-16 15:38:44 +00:00
|
|
|
res += "Sig: " + sig + "\n";
|
|
|
|
|
2020-06-02 00:37:43 +00:00
|
|
|
if (ca)
|
|
|
|
res += "CA: " + renderContentAddress(*ca) + "\n";
|
2016-08-03 11:17:11 +00:00
|
|
|
|
2016-02-16 15:38:44 +00:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|