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 {
|
|
|
|
|
2023-10-26 23:39:21 +00:00
|
|
|
GENERATE_CMP_EXT(
|
|
|
|
,
|
|
|
|
NarInfo,
|
|
|
|
me->url,
|
|
|
|
me->compression,
|
|
|
|
me->fileHash,
|
|
|
|
me->fileSize,
|
|
|
|
static_cast<const ValidPathInfo &>(*me));
|
|
|
|
|
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-11-07 12:40:21 +00:00
|
|
|
if (colon == s.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-11-07 12:40:21 +00:00
|
|
|
if (eol == s.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);
|
2023-10-13 01:48:15 +00:00
|
|
|
res += "FileHash: " + fileHash->to_string(HashFormat::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);
|
2023-10-13 01:48:15 +00:00
|
|
|
res += "NarHash: " + narHash.to_string(HashFormat::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;
|
|
|
|
}
|
|
|
|
|
2023-10-23 01:12:54 +00:00
|
|
|
nlohmann::json NarInfo::toJSON(
|
|
|
|
const Store & store,
|
|
|
|
bool includeImpureInfo,
|
|
|
|
HashFormat hashFormat) const
|
|
|
|
{
|
|
|
|
using nlohmann::json;
|
|
|
|
|
|
|
|
auto jsonObject = ValidPathInfo::toJSON(store, includeImpureInfo, hashFormat);
|
|
|
|
|
|
|
|
if (includeImpureInfo) {
|
|
|
|
if (!url.empty())
|
|
|
|
jsonObject["url"] = url;
|
2023-10-27 00:01:36 +00:00
|
|
|
if (!compression.empty())
|
|
|
|
jsonObject["compression"] = compression;
|
2023-10-23 01:12:54 +00:00
|
|
|
if (fileHash)
|
|
|
|
jsonObject["downloadHash"] = fileHash->to_string(hashFormat, true);
|
|
|
|
if (fileSize)
|
|
|
|
jsonObject["downloadSize"] = fileSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
return jsonObject;
|
|
|
|
}
|
|
|
|
|
|
|
|
NarInfo NarInfo::fromJSON(
|
|
|
|
const Store & store,
|
|
|
|
const StorePath & path,
|
|
|
|
const nlohmann::json & json)
|
|
|
|
{
|
|
|
|
using nlohmann::detail::value_t;
|
|
|
|
|
2023-10-23 01:12:54 +00:00
|
|
|
NarInfo res {
|
|
|
|
ValidPathInfo {
|
|
|
|
path,
|
|
|
|
UnkeyedValidPathInfo::fromJSON(store, json),
|
|
|
|
}
|
|
|
|
};
|
2023-10-23 01:12:54 +00:00
|
|
|
|
|
|
|
if (json.contains("url"))
|
|
|
|
res.url = ensureType(valueAt(json, "url"), value_t::string);
|
|
|
|
|
2023-10-27 00:01:36 +00:00
|
|
|
if (json.contains("compression"))
|
|
|
|
res.compression = ensureType(valueAt(json, "compression"), value_t::string);
|
|
|
|
|
2023-10-23 01:12:54 +00:00
|
|
|
if (json.contains("downloadHash"))
|
|
|
|
res.fileHash = Hash::parseAny(
|
|
|
|
static_cast<const std::string &>(
|
|
|
|
ensureType(valueAt(json, "downloadHash"), value_t::string)),
|
|
|
|
std::nullopt);
|
|
|
|
|
|
|
|
if (json.contains("downloadSize"))
|
|
|
|
res.fileSize = ensureType(valueAt(json, "downloadSize"), value_t::number_integer);
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2016-02-16 15:38:44 +00:00
|
|
|
}
|