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
|
|
|
{
|
2023-04-17 17:34:09 +00:00
|
|
|
unsigned line = 1;
|
|
|
|
|
|
|
|
auto corrupt = [&](const char * reason) {
|
|
|
|
return Error("NAR info file '%1%' is corrupt: %2%", whence,
|
|
|
|
std::string(reason) + (line > 0 ? " at line " + std::to_string(line) : ""));
|
2016-02-16 15:38:44 +00:00
|
|
|
};
|
|
|
|
|
2022-02-25 15:00:00 +00:00
|
|
|
auto parseHashField = [&](const std::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 &) {
|
2023-04-17 17:34:09 +00:00
|
|
|
throw corrupt("bad hash");
|
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);
|
2023-04-17 17:34:09 +00:00
|
|
|
if (colon == std::string::npos) throw corrupt("expecting ':'");
|
2016-02-16 15:38:44 +00:00
|
|
|
|
|
|
|
std::string name(s, pos, colon - pos);
|
|
|
|
|
|
|
|
size_t eol = s.find('\n', colon + 2);
|
2023-04-17 17:34:09 +00:00
|
|
|
if (eol == std::string::npos) throw corrupt("expecting '\\n'");
|
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") {
|
2021-01-08 11:22:21 +00:00
|
|
|
auto n = string2Int<decltype(fileSize)>(value);
|
2023-04-17 17:34:09 +00:00
|
|
|
if (!n) throw corrupt("invalid FileSize");
|
2021-01-08 11:22:21 +00:00
|
|
|
fileSize = *n;
|
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") {
|
2021-01-08 11:22:21 +00:00
|
|
|
auto n = string2Int<decltype(narSize)>(value);
|
2023-04-17 17:34:09 +00:00
|
|
|
if (!n) throw corrupt("invalid NarSize");
|
2021-01-08 11:22:21 +00:00
|
|
|
narSize = *n;
|
2016-02-16 15:38:44 +00:00
|
|
|
}
|
|
|
|
else if (name == "References") {
|
|
|
|
auto refs = tokenizeString<Strings>(value, " ");
|
2023-04-17 17:34:09 +00:00
|
|
|
if (!references.empty()) throw corrupt("extra References");
|
2019-12-05 18:11:09 +00:00
|
|
|
for (auto & r : refs)
|
2023-01-14 21:38:43 +00:00
|
|
|
references.insert(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 == "Sig")
|
2016-03-21 17:05:47 +00:00
|
|
|
sigs.insert(value);
|
2016-08-03 11:17:11 +00:00
|
|
|
else if (name == "CA") {
|
2023-04-17 17:34:09 +00:00
|
|
|
if (ca) throw corrupt("extra CA");
|
2020-06-02 00:37:43 +00:00
|
|
|
// FIXME: allow blank ca or require skipping field?
|
2023-03-30 21:12:49 +00:00
|
|
|
ca = ContentAddress::parseOpt(value);
|
2016-08-03 11:17:11 +00:00
|
|
|
}
|
2016-02-16 15:38:44 +00:00
|
|
|
|
|
|
|
pos = eol + 1;
|
2023-04-17 17:34:09 +00:00
|
|
|
line += 1;
|
2016-02-16 15:38:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (compression == "") compression = "bzip2";
|
|
|
|
|
2023-04-17 17:34:09 +00:00
|
|
|
if (!havePath || !haveNarHash || url.empty() || narSize == 0) {
|
|
|
|
line = 0; // don't include line information in the error
|
|
|
|
throw corrupt(
|
|
|
|
!havePath ? "StorePath missing" :
|
|
|
|
!haveNarHash ? "NarHash missing" :
|
|
|
|
url.empty() ? "URL missing" :
|
|
|
|
narSize == 0 ? "NarSize missing or zero"
|
|
|
|
: "?");
|
|
|
|
}
|
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
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|