2016-02-16 17:38:44 +02:00
|
|
|
#include "globals.hh"
|
|
|
|
#include "nar-info.hh"
|
2020-08-06 21:31:48 +03:00
|
|
|
#include "store-api.hh"
|
2016-02-16 17:38:44 +02:00
|
|
|
|
|
|
|
namespace nix {
|
|
|
|
|
2023-10-27 02:39:21 +03:00
|
|
|
GENERATE_CMP_EXT(
|
|
|
|
,
|
|
|
|
NarInfo,
|
|
|
|
me->url,
|
|
|
|
me->compression,
|
|
|
|
me->fileHash,
|
|
|
|
me->fileSize,
|
|
|
|
static_cast<const ValidPathInfo &>(*me));
|
|
|
|
|
2016-06-01 15:49:12 +03:00
|
|
|
NarInfo::NarInfo(const Store & store, const std::string & s, const std::string & whence)
|
2020-08-06 21:31:48 +03:00
|
|
|
: ValidPathInfo(StorePath(StorePath::dummy), Hash(Hash::dummy)) // FIXME: hack
|
2016-02-16 17:38:44 +02:00
|
|
|
{
|
2023-04-17 20:34:09 +03: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 17:38:44 +02:00
|
|
|
};
|
|
|
|
|
2022-02-25 17:00:00 +02:00
|
|
|
auto parseHashField = [&](const std::string & s) {
|
2016-04-20 15:12:38 +03:00
|
|
|
try {
|
2020-07-02 01:34:18 +03:00
|
|
|
return Hash::parseAnyPrefixed(s);
|
2016-04-20 15:12:38 +03:00
|
|
|
} catch (BadHash &) {
|
2023-04-17 20:34:09 +03:00
|
|
|
throw corrupt("bad hash");
|
2016-04-20 15:12:38 +03:00
|
|
|
}
|
2016-02-16 17:38:44 +02:00
|
|
|
};
|
|
|
|
|
2019-12-05 20:11:09 +02:00
|
|
|
bool havePath = false;
|
2020-08-06 21:31:48 +03:00
|
|
|
bool haveNarHash = false;
|
2019-12-05 20:11:09 +02:00
|
|
|
|
2016-02-16 17:38:44 +02:00
|
|
|
size_t pos = 0;
|
|
|
|
while (pos < s.size()) {
|
|
|
|
|
|
|
|
size_t colon = s.find(':', pos);
|
2023-11-07 14:40:21 +02:00
|
|
|
if (colon == s.npos) throw corrupt("expecting ':'");
|
2016-02-16 17:38:44 +02:00
|
|
|
|
|
|
|
std::string name(s, pos, colon - pos);
|
|
|
|
|
|
|
|
size_t eol = s.find('\n', colon + 2);
|
2023-11-07 14:40:21 +02:00
|
|
|
if (eol == s.npos) throw corrupt("expecting '\\n'");
|
2016-02-16 17:38:44 +02:00
|
|
|
|
|
|
|
std::string value(s, colon + 2, eol - colon - 2);
|
|
|
|
|
|
|
|
if (name == "StorePath") {
|
2019-12-05 20:11:09 +02:00
|
|
|
path = store.parseStorePath(value);
|
|
|
|
havePath = true;
|
2016-02-16 17:38:44 +02: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 13:22:21 +02:00
|
|
|
auto n = string2Int<decltype(fileSize)>(value);
|
2023-04-17 20:34:09 +03:00
|
|
|
if (!n) throw corrupt("invalid FileSize");
|
2021-01-08 13:22:21 +02:00
|
|
|
fileSize = *n;
|
2016-02-16 17:38:44 +02:00
|
|
|
}
|
2020-08-06 21:31:48 +03:00
|
|
|
else if (name == "NarHash") {
|
2016-02-16 17:38:44 +02:00
|
|
|
narHash = parseHashField(value);
|
2020-08-06 21:31:48 +03:00
|
|
|
haveNarHash = true;
|
|
|
|
}
|
2016-02-16 17:38:44 +02:00
|
|
|
else if (name == "NarSize") {
|
2021-01-08 13:22:21 +02:00
|
|
|
auto n = string2Int<decltype(narSize)>(value);
|
2023-04-17 20:34:09 +03:00
|
|
|
if (!n) throw corrupt("invalid NarSize");
|
2021-01-08 13:22:21 +02:00
|
|
|
narSize = *n;
|
2016-02-16 17:38:44 +02:00
|
|
|
}
|
|
|
|
else if (name == "References") {
|
|
|
|
auto refs = tokenizeString<Strings>(value, " ");
|
2023-04-17 20:34:09 +03:00
|
|
|
if (!references.empty()) throw corrupt("extra References");
|
2019-12-05 20:11:09 +02:00
|
|
|
for (auto & r : refs)
|
2023-01-14 23:38:43 +02:00
|
|
|
references.insert(StorePath(r));
|
2016-02-16 17:38:44 +02:00
|
|
|
}
|
|
|
|
else if (name == "Deriver") {
|
2019-12-05 20:11:09 +02:00
|
|
|
if (value != "unknown-deriver")
|
2020-06-16 15:16:39 +03:00
|
|
|
deriver = StorePath(value);
|
2016-02-16 17:38:44 +02:00
|
|
|
}
|
|
|
|
else if (name == "Sig")
|
2016-03-21 19:05:47 +02:00
|
|
|
sigs.insert(value);
|
2016-08-03 14:17:11 +03:00
|
|
|
else if (name == "CA") {
|
2023-04-17 20:34:09 +03:00
|
|
|
if (ca) throw corrupt("extra CA");
|
2020-06-02 03:37:43 +03:00
|
|
|
// FIXME: allow blank ca or require skipping field?
|
2023-03-31 00:12:49 +03:00
|
|
|
ca = ContentAddress::parseOpt(value);
|
2016-08-03 14:17:11 +03:00
|
|
|
}
|
2016-02-16 17:38:44 +02:00
|
|
|
|
|
|
|
pos = eol + 1;
|
2023-04-17 20:34:09 +03:00
|
|
|
line += 1;
|
2016-02-16 17:38:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (compression == "") compression = "bzip2";
|
|
|
|
|
2023-04-17 20:34:09 +03: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 17:38:44 +02:00
|
|
|
}
|
|
|
|
|
2019-12-05 20:11:09 +02:00
|
|
|
std::string NarInfo::to_string(const Store & store) const
|
2016-02-16 17:38:44 +02:00
|
|
|
{
|
|
|
|
std::string res;
|
2019-12-05 20:11:09 +02:00
|
|
|
res += "StorePath: " + store.printStorePath(path) + "\n";
|
2016-02-16 17:38:44 +02:00
|
|
|
res += "URL: " + url + "\n";
|
|
|
|
assert(compression != "");
|
|
|
|
res += "Compression: " + compression + "\n";
|
2020-06-19 21:41:33 +03:00
|
|
|
assert(fileHash && fileHash->type == htSHA256);
|
2023-10-13 04:48:15 +03:00
|
|
|
res += "FileHash: " + fileHash->to_string(HashFormat::Base32, true) + "\n";
|
2016-02-16 17:38:44 +02:00
|
|
|
res += "FileSize: " + std::to_string(fileSize) + "\n";
|
2020-08-05 22:30:38 +03:00
|
|
|
assert(narHash.type == htSHA256);
|
2023-10-13 04:48:15 +03:00
|
|
|
res += "NarHash: " + narHash.to_string(HashFormat::Base32, true) + "\n";
|
2016-02-16 17:38:44 +02:00
|
|
|
res += "NarSize: " + std::to_string(narSize) + "\n";
|
|
|
|
|
|
|
|
res += "References: " + concatStringsSep(" ", shortRefs()) + "\n";
|
|
|
|
|
2019-12-05 20:11:09 +02:00
|
|
|
if (deriver)
|
|
|
|
res += "Deriver: " + std::string(deriver->to_string()) + "\n";
|
2016-02-16 17:38:44 +02:00
|
|
|
|
2016-03-21 19:05:47 +02:00
|
|
|
for (auto sig : sigs)
|
2016-02-16 17:38:44 +02:00
|
|
|
res += "Sig: " + sig + "\n";
|
|
|
|
|
2020-06-02 03:37:43 +03:00
|
|
|
if (ca)
|
|
|
|
res += "CA: " + renderContentAddress(*ca) + "\n";
|
2016-08-03 14:17:11 +03:00
|
|
|
|
2016-02-16 17:38:44 +02:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2023-10-23 04:12:54 +03: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 03:01:36 +03:00
|
|
|
if (!compression.empty())
|
|
|
|
jsonObject["compression"] = compression;
|
2023-10-23 04:12:54 +03: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 04:12:54 +03:00
|
|
|
NarInfo res {
|
|
|
|
ValidPathInfo {
|
|
|
|
path,
|
|
|
|
UnkeyedValidPathInfo::fromJSON(store, json),
|
|
|
|
}
|
|
|
|
};
|
2023-10-23 04:12:54 +03:00
|
|
|
|
|
|
|
if (json.contains("url"))
|
|
|
|
res.url = ensureType(valueAt(json, "url"), value_t::string);
|
|
|
|
|
2023-10-27 03:01:36 +03:00
|
|
|
if (json.contains("compression"))
|
|
|
|
res.compression = ensureType(valueAt(json, "compression"), value_t::string);
|
|
|
|
|
2023-10-23 04:12:54 +03: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 17:38:44 +02:00
|
|
|
}
|