2016-02-16 15:38:44 +00:00
|
|
|
#include "globals.hh"
|
|
|
|
#include "nar-info.hh"
|
|
|
|
|
|
|
|
namespace nix {
|
|
|
|
|
2016-06-01 12:49:12 +00:00
|
|
|
NarInfo::NarInfo(const Store & store, const std::string & s, const std::string & whence)
|
2020-01-21 20:14:13 +00:00
|
|
|
: ValidPathInfo(StorePath::dummy.clone()) // FIXME: hack
|
2016-02-16 15:38:44 +00:00
|
|
|
{
|
2016-05-31 11:23:54 +00:00
|
|
|
auto corrupt = [&]() {
|
2017-07-30 11:27:57 +00:00
|
|
|
throw Error(format("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 {
|
2017-07-04 12:47:59 +00:00
|
|
|
return Hash(s);
|
2016-04-20 12:12:38 +00:00
|
|
|
} catch (BadHash &) {
|
|
|
|
corrupt();
|
2016-05-31 11:23:54 +00:00
|
|
|
return Hash(); // never reached
|
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;
|
|
|
|
|
2016-02-16 15:38:44 +00:00
|
|
|
size_t pos = 0;
|
|
|
|
while (pos < s.size()) {
|
|
|
|
|
|
|
|
size_t colon = s.find(':', pos);
|
|
|
|
if (colon == std::string::npos) corrupt();
|
|
|
|
|
|
|
|
std::string name(s, pos, colon - pos);
|
|
|
|
|
|
|
|
size_t eol = s.find('\n', colon + 2);
|
|
|
|
if (eol == std::string::npos) corrupt();
|
|
|
|
|
|
|
|
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") {
|
|
|
|
if (!string2Int(value, fileSize)) corrupt();
|
|
|
|
}
|
|
|
|
else if (name == "NarHash")
|
|
|
|
narHash = parseHashField(value);
|
|
|
|
else if (name == "NarSize") {
|
|
|
|
if (!string2Int(value, narSize)) corrupt();
|
|
|
|
}
|
|
|
|
else if (name == "References") {
|
|
|
|
auto refs = tokenizeString<Strings>(value, " ");
|
|
|
|
if (!references.empty()) corrupt();
|
2019-12-05 18:11:09 +00:00
|
|
|
for (auto & r : refs)
|
|
|
|
references.insert(StorePath::fromBaseName(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")
|
|
|
|
deriver = StorePath::fromBaseName(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") {
|
|
|
|
if (!ca.empty()) corrupt();
|
|
|
|
ca = value;
|
|
|
|
}
|
2016-02-16 15:38:44 +00:00
|
|
|
|
|
|
|
pos = eol + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (compression == "") compression = "bzip2";
|
|
|
|
|
2019-12-05 18:11:09 +00:00
|
|
|
if (!havePath || url.empty() || narSize == 0 || !narHash) 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-03-28 23:22:10 +00:00
|
|
|
assert(fileHash.type == HashType::SHA256);
|
|
|
|
res += "FileHash: " + fileHash.to_string(Base::Base32) + "\n";
|
2016-02-16 15:38:44 +00:00
|
|
|
res += "FileSize: " + std::to_string(fileSize) + "\n";
|
2020-03-28 23:22:10 +00:00
|
|
|
assert(narHash.type == HashType::SHA256);
|
|
|
|
res += "NarHash: " + narHash.to_string(Base::Base32) + "\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";
|
|
|
|
|
2016-08-03 11:17:11 +00:00
|
|
|
if (!ca.empty())
|
|
|
|
res += "CA: " + ca + "\n";
|
|
|
|
|
2016-02-16 15:38:44 +00:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|