2016-04-21 14:59:50 +02:00
|
|
|
#include "command.hh"
|
|
|
|
#include "shared.hh"
|
|
|
|
#include "store-api.hh"
|
|
|
|
|
2016-08-26 18:55:55 +02:00
|
|
|
|
|
|
|
#include "json.hh"
|
|
|
|
|
|
|
|
|
2016-04-21 14:59:50 +02:00
|
|
|
#include <iomanip>
|
2016-04-21 17:53:47 +02:00
|
|
|
#include <algorithm>
|
2016-04-21 14:59:50 +02:00
|
|
|
|
|
|
|
using namespace nix;
|
|
|
|
|
|
|
|
struct CmdPathInfo : StorePathsCommand
|
|
|
|
{
|
|
|
|
bool showSize = false;
|
|
|
|
bool showClosureSize = false;
|
2016-04-22 14:39:37 +02:00
|
|
|
bool showSigs = false;
|
2016-08-26 18:55:55 +02:00
|
|
|
bool json = false;
|
2016-04-21 14:59:50 +02:00
|
|
|
|
|
|
|
CmdPathInfo()
|
|
|
|
{
|
|
|
|
mkFlag('s', "size", "print size of the NAR dump of each path", &showSize);
|
|
|
|
mkFlag('S', "closure-size", "print sum size of the NAR dumps of the closure of each path", &showClosureSize);
|
2016-04-22 14:39:37 +02:00
|
|
|
mkFlag(0, "sigs", "show signatures", &showSigs);
|
2016-08-26 18:55:55 +02:00
|
|
|
mkFlag(0, "json", "produce JSON output", &json);
|
2016-04-21 14:59:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string name() override
|
|
|
|
{
|
|
|
|
return "path-info";
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string description() override
|
|
|
|
{
|
|
|
|
return "query information about store paths";
|
|
|
|
}
|
|
|
|
|
|
|
|
Examples examples() override
|
|
|
|
{
|
|
|
|
return {
|
|
|
|
Example{
|
|
|
|
"To show the closure sizes of every path in the current NixOS system closure, sorted by size:",
|
|
|
|
"nix path-info -rS /run/current-system | sort -nk2"
|
|
|
|
},
|
|
|
|
Example{
|
|
|
|
"To check the existence of a path in a binary cache:",
|
|
|
|
"nix path-info -r /nix/store/7qvk5c91...-geeqie-1.1 --store https://cache.nixos.org/"
|
|
|
|
},
|
2016-08-26 18:55:55 +02:00
|
|
|
Example{
|
|
|
|
"To print the 10 most recently added paths (using --json and the jq(1) command):",
|
|
|
|
"nix path-info --all --json | jq -r 'sort_by(.registrationTime)[-11:-1][].path'"
|
|
|
|
},
|
2016-04-21 14:59:50 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
void run(ref<Store> store, Paths storePaths) override
|
|
|
|
{
|
|
|
|
size_t pathLen = 0;
|
|
|
|
for (auto & storePath : storePaths)
|
|
|
|
pathLen = std::max(pathLen, storePath.size());
|
|
|
|
|
2016-08-26 18:55:55 +02:00
|
|
|
auto getClosureSize = [&](const Path & storePath) {
|
|
|
|
size_t totalSize = 0;
|
|
|
|
PathSet closure;
|
|
|
|
store->computeFSClosure(storePath, closure, false, false);
|
|
|
|
for (auto & p : closure)
|
|
|
|
totalSize += store->queryPathInfo(p)->narSize;
|
|
|
|
return totalSize;
|
|
|
|
};
|
2016-04-21 14:59:50 +02:00
|
|
|
|
2016-08-26 18:55:55 +02:00
|
|
|
if (json) {
|
|
|
|
JSONList jsonRoot(std::cout, true);
|
2016-04-21 14:59:50 +02:00
|
|
|
|
2016-08-26 18:55:55 +02:00
|
|
|
for (auto storePath : storePaths) {
|
|
|
|
auto info = store->queryPathInfo(storePath);
|
|
|
|
storePath = info->path;
|
|
|
|
|
|
|
|
auto jsonPath = jsonRoot.object();
|
|
|
|
jsonPath
|
|
|
|
.attr("path", storePath)
|
|
|
|
.attr("narHash", info->narHash.to_string())
|
|
|
|
.attr("narSize", info->narSize);
|
|
|
|
|
|
|
|
if (showClosureSize)
|
|
|
|
jsonPath.attr("closureSize", getClosureSize(storePath));
|
|
|
|
|
|
|
|
if (info->deriver != "")
|
|
|
|
jsonPath.attr("deriver", info->deriver);
|
|
|
|
|
|
|
|
{
|
|
|
|
auto jsonRefs = jsonPath.list("references");
|
|
|
|
for (auto & ref : info->references)
|
|
|
|
jsonRefs.elem(ref);
|
|
|
|
}
|
2016-04-21 14:59:50 +02:00
|
|
|
|
2016-08-26 18:55:55 +02:00
|
|
|
if (info->registrationTime)
|
|
|
|
jsonPath.attr("registrationTime", info->registrationTime);
|
|
|
|
|
|
|
|
if (info->ultimate)
|
|
|
|
jsonPath.attr("ultimate", info->ultimate);
|
|
|
|
|
|
|
|
if (info->ca != "")
|
|
|
|
jsonPath.attr("ca", info->ca);
|
|
|
|
|
|
|
|
if (!info->sigs.empty()) {
|
|
|
|
auto jsonSigs = jsonPath.list("signatures");
|
|
|
|
for (auto & sig : info->sigs)
|
|
|
|
jsonSigs.elem(sig);
|
|
|
|
}
|
2016-04-21 14:59:50 +02:00
|
|
|
}
|
2016-08-26 18:55:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
else {
|
|
|
|
|
|
|
|
for (auto storePath : storePaths) {
|
|
|
|
auto info = store->queryPathInfo(storePath);
|
|
|
|
storePath = info->path; // FIXME: screws up padding
|
2016-04-21 14:59:50 +02:00
|
|
|
|
2016-08-26 18:55:55 +02:00
|
|
|
std::cout << storePath << std::string(std::max(0, (int) pathLen - (int) storePath.size()), ' ');
|
|
|
|
|
|
|
|
if (showSize)
|
|
|
|
std::cout << '\t' << std::setw(11) << info->narSize;
|
|
|
|
|
|
|
|
if (showClosureSize)
|
|
|
|
std::cout << '\t' << std::setw(11) << getClosureSize(storePath);
|
|
|
|
|
|
|
|
if (showSigs) {
|
|
|
|
std::cout << '\t';
|
|
|
|
Strings ss;
|
|
|
|
if (info->ultimate) ss.push_back("ultimate");
|
|
|
|
if (info->ca != "") ss.push_back("ca:" + info->ca);
|
|
|
|
for (auto & sig : info->sigs) ss.push_back(sig);
|
|
|
|
std::cout << concatStringsSep(" ", ss);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::cout << std::endl;
|
2016-04-22 14:39:37 +02:00
|
|
|
}
|
|
|
|
|
2016-04-21 14:59:50 +02:00
|
|
|
}
|
2016-08-26 18:55:55 +02:00
|
|
|
|
2016-04-21 14:59:50 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
static RegisterCommand r1(make_ref<CmdPathInfo>());
|