2016-02-16 16:38:44 +01:00
|
|
|
#include "nar-info.hh"
|
2020-05-19 16:54:39 +02:00
|
|
|
|
2016-02-16 16:38:44 +01:00
|
|
|
#include "globals.hh"
|
|
|
|
|
|
|
|
namespace nix {
|
|
|
|
|
2016-06-01 14:49:12 +02:00
|
|
|
NarInfo::NarInfo(const Store& store, const std::string& s,
|
|
|
|
const std::string& whence) {
|
2016-05-31 13:23:54 +02:00
|
|
|
auto corrupt = [&]() {
|
2017-07-30 13:27:57 +02:00
|
|
|
throw Error(format("NAR info file '%1%' is corrupt") % whence);
|
2016-02-16 16:38:44 +01:00
|
|
|
};
|
2020-05-17 17:31:57 +02:00
|
|
|
|
2020-05-24 23:29:21 +02:00
|
|
|
auto parseHashField = [&](const std::string& s) {
|
2016-04-20 14:12:38 +02:00
|
|
|
try {
|
2017-07-04 14:47:59 +02:00
|
|
|
return Hash(s);
|
2016-04-20 14:12:38 +02:00
|
|
|
} catch (BadHash&) {
|
|
|
|
corrupt();
|
2016-05-31 13:23:54 +02:00
|
|
|
return Hash(); // never reached
|
2016-04-20 14:12:38 +02:00
|
|
|
}
|
2016-02-16 16:38:44 +01:00
|
|
|
};
|
2020-05-17 17:31:57 +02:00
|
|
|
|
2016-02-16 16:38:44 +01:00
|
|
|
size_t pos = 0;
|
|
|
|
while (pos < s.size()) {
|
|
|
|
size_t colon = s.find(':', pos);
|
|
|
|
if (colon == std::string::npos) {
|
|
|
|
corrupt();
|
2020-05-19 19:55:58 +02:00
|
|
|
}
|
2020-05-17 17:31:57 +02:00
|
|
|
|
2016-02-16 16:38:44 +01:00
|
|
|
std::string name(s, pos, colon - pos);
|
2020-05-17 17:31:57 +02:00
|
|
|
|
2016-02-16 16:38:44 +01:00
|
|
|
size_t eol = s.find('\n', colon + 2);
|
|
|
|
if (eol == std::string::npos) {
|
|
|
|
corrupt();
|
2020-05-19 19:55:58 +02:00
|
|
|
}
|
2020-05-17 17:31:57 +02:00
|
|
|
|
2016-02-16 16:38:44 +01:00
|
|
|
std::string value(s, colon + 2, eol - colon - 2);
|
2020-05-17 17:31:57 +02:00
|
|
|
|
2016-02-16 16:38:44 +01:00
|
|
|
if (name == "StorePath") {
|
2016-06-01 14:49:12 +02:00
|
|
|
if (!store.isStorePath(value)) {
|
|
|
|
corrupt();
|
2020-05-19 19:55:58 +02:00
|
|
|
}
|
2016-02-16 16:38:44 +01:00
|
|
|
path = value;
|
|
|
|
} 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();
|
2020-05-19 19:55:58 +02:00
|
|
|
}
|
2016-02-16 16:38:44 +01:00
|
|
|
} else if (name == "NarHash") {
|
|
|
|
narHash = parseHashField(value);
|
|
|
|
} else if (name == "NarSize") {
|
|
|
|
if (!string2Int(value, narSize)) {
|
|
|
|
corrupt();
|
2020-05-19 19:55:58 +02:00
|
|
|
}
|
2016-02-16 16:38:44 +01:00
|
|
|
} else if (name == "References") {
|
|
|
|
auto refs = tokenizeString<Strings>(value, " ");
|
|
|
|
if (!references.empty()) {
|
|
|
|
corrupt();
|
2020-05-19 19:55:58 +02:00
|
|
|
}
|
2016-02-16 16:38:44 +01:00
|
|
|
for (auto& r : refs) {
|
2016-06-01 14:49:12 +02:00
|
|
|
auto r2 = store.storeDir + "/" + r;
|
|
|
|
if (!store.isStorePath(r2)) {
|
|
|
|
corrupt();
|
2020-05-19 19:55:58 +02:00
|
|
|
}
|
2016-02-16 16:38:44 +01:00
|
|
|
references.insert(r2);
|
|
|
|
}
|
|
|
|
} else if (name == "Deriver") {
|
2017-03-28 13:08:13 +02:00
|
|
|
if (value != "unknown-deriver") {
|
|
|
|
auto p = store.storeDir + "/" + value;
|
|
|
|
if (!store.isStorePath(p)) {
|
|
|
|
corrupt();
|
2020-05-19 19:55:58 +02:00
|
|
|
}
|
2017-03-28 13:08:13 +02:00
|
|
|
deriver = p;
|
2016-02-16 16:38:44 +01:00
|
|
|
}
|
|
|
|
} else if (name == "System") {
|
|
|
|
system = value;
|
|
|
|
} else if (name == "Sig") {
|
2016-03-21 18:05:47 +01:00
|
|
|
sigs.insert(value);
|
2016-08-03 13:17:11 +02:00
|
|
|
} else if (name == "CA") {
|
|
|
|
if (!ca.empty()) {
|
|
|
|
corrupt();
|
2020-05-19 19:55:58 +02:00
|
|
|
}
|
2016-08-03 13:17:11 +02:00
|
|
|
ca = value;
|
2016-02-16 16:38:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
pos = eol + 1;
|
2020-05-17 17:31:57 +02:00
|
|
|
}
|
2016-02-16 16:38:44 +01:00
|
|
|
|
2020-05-20 23:27:37 +02:00
|
|
|
if (compression.empty()) {
|
2016-02-16 16:38:44 +01:00
|
|
|
compression = "bzip2";
|
2020-05-19 19:55:58 +02:00
|
|
|
}
|
2020-05-17 17:31:57 +02:00
|
|
|
|
2016-07-21 18:35:12 +02:00
|
|
|
if (path.empty() || url.empty() || narSize == 0 || !narHash) {
|
|
|
|
corrupt();
|
2020-05-19 19:55:58 +02:00
|
|
|
}
|
2016-02-16 16:38:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string NarInfo::to_string() const {
|
|
|
|
std::string res;
|
|
|
|
res += "StorePath: " + path + "\n";
|
|
|
|
res += "URL: " + url + "\n";
|
2020-05-20 23:27:37 +02:00
|
|
|
assert(!compression.empty());
|
2016-02-16 16:38:44 +01:00
|
|
|
res += "Compression: " + compression + "\n";
|
|
|
|
assert(fileHash.type == htSHA256);
|
2017-07-04 14:47:59 +02:00
|
|
|
res += "FileHash: " + fileHash.to_string(Base32) + "\n";
|
2016-02-16 16:38:44 +01:00
|
|
|
res += "FileSize: " + std::to_string(fileSize) + "\n";
|
|
|
|
assert(narHash.type == htSHA256);
|
2017-07-04 14:47:59 +02:00
|
|
|
res += "NarHash: " + narHash.to_string(Base32) + "\n";
|
2016-02-16 16:38:44 +01:00
|
|
|
res += "NarSize: " + std::to_string(narSize) + "\n";
|
|
|
|
|
|
|
|
res += "References: " + concatStringsSep(" ", shortRefs()) + "\n";
|
|
|
|
|
|
|
|
if (!deriver.empty()) {
|
|
|
|
res += "Deriver: " + baseNameOf(deriver) + "\n";
|
2020-05-19 19:55:58 +02:00
|
|
|
}
|
2016-02-16 16:38:44 +01:00
|
|
|
|
|
|
|
if (!system.empty()) {
|
|
|
|
res += "System: " + system + "\n";
|
2020-05-19 19:55:58 +02:00
|
|
|
}
|
2016-02-16 16:38:44 +01:00
|
|
|
|
2020-05-20 23:58:43 +02:00
|
|
|
for (const auto& sig : sigs) {
|
2016-02-16 16:38:44 +01:00
|
|
|
res += "Sig: " + sig + "\n";
|
2020-05-19 20:04:08 +02:00
|
|
|
}
|
2016-02-16 16:38:44 +01:00
|
|
|
|
2016-08-03 13:17:11 +02:00
|
|
|
if (!ca.empty()) {
|
|
|
|
res += "CA: " + ca + "\n";
|
2020-05-19 19:55:58 +02:00
|
|
|
}
|
2016-08-03 13:17:11 +02:00
|
|
|
|
2016-02-16 16:38:44 +01:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace nix
|