1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
|
#include "lix/libcmd/command.hh"
#include "lix/libmain/shared.hh"
#include "lix/libstore/store-api.hh"
#include "lix/libmain/common-args.hh"
#include "lix/libutil/async.hh"
#include "path-info.hh"
#include <algorithm>
#include <array>
namespace nix {
struct CmdPathInfo : StorePathsCommand, MixJSON
{
bool showSize = false;
bool showClosureSize = false;
bool humanReadable = false;
bool showSigs = false;
CmdPathInfo()
{
addFlag({
.longName = "size",
.shortName = 's',
.description = "Print the size of the NAR serialisation of each path.",
.handler = {&showSize, true},
});
addFlag({
.longName = "closure-size",
.shortName = 'S',
.description = "Print the sum of the sizes of the NAR serialisations of the closure of each path.",
.handler = {&showClosureSize, true},
});
addFlag({
.longName = "human-readable",
.shortName = 'h',
.description = "With `-s` and `-S`, print sizes in a human-friendly format such as `5.67G`.",
.handler = {&humanReadable, true},
});
addFlag({
.longName = "sigs",
.description = "Show signatures.",
.handler = {&showSigs, true},
});
}
std::string description() override
{
return "query information about store paths";
}
std::string doc() override
{
return
#include "path-info.md"
;
}
Category category() override { return catSecondary; }
void printSize(uint64_t value)
{
if (!humanReadable) {
std::cout << fmt("\t%11d", value);
return;
}
static const std::array<char, 9> idents{{
' ', 'K', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y'
}};
size_t power = 0;
double res = value;
while (res > 1024 && power < idents.size()) {
++power;
res /= 1024;
}
std::cout << fmt("\t%6.1f%c", res, idents.at(power));
}
void run(ref<Store> store, Installables && installables) override
{
StorePathSet paths;
if (all) {
if (installables.size()) {
throw UsageError("'--all' does not expect arguments");
}
paths = aio().blockOn(store->queryAllValidPaths());
} else {
auto state = getEvaluator()->begin(aio());
if (operateOn == OperateOn::Output) {
for (auto i : installables) {
for (auto b : i->toDerivedPaths(*state)) {
std::visit(
overloaded{
[&](const DerivedPath::Built & bfd) {
for (auto & [_, output] :
aio().blockOn(resolveDerivedPath(*store, bfd, &*getEvalStore())))
{
paths.insert(output);
}
},
[&](const DerivedPath::Opaque & bo) { paths.insert(bo.path); },
},
b.path.raw()
);
}
}
} else {
auto drvPaths = Installable::toDerivations(*state, store, installables, true);
paths.insert(drvPaths.begin(), drvPaths.end());
}
if (recursive) {
// XXX: This only computes the store path closure, ignoring
// intermediate realisations
StorePathSet closure;
aio().blockOn(store->computeFSClosure(paths, closure));
paths.insert(closure.begin(), closure.end());
}
}
auto sorted = aio().blockOn(store->topoSortPaths(paths));
std::reverse(sorted.begin(), sorted.end());
run(store, std::move(sorted));
}
void run(ref<Store> store, StorePaths && storePaths) override
{
// Wipe the progress bar to prevent interference with the output.
// It's not needed any more because expensive evaluation or builds are already done here.
logger->pause();
size_t pathLen = 0;
for (auto & storePath : storePaths)
pathLen = std::max(pathLen, store->printStorePath(storePath).size());
if (json) {
std::cout << aio()
.blockOn(store->pathInfoToJSON(
// FIXME: preserve order?
StorePathSet(storePaths.begin(), storePaths.end()),
true,
showClosureSize,
HashFormat::SRI,
AllowInvalid
))
.dump();
}
else {
for (auto & storePath : storePaths) {
auto info = aio().blockOn(store->queryPathInfo(storePath));
auto storePathS = store->printStorePath(info->path);
std::cout << storePathS;
if (showSize || showClosureSize || showSigs)
std::cout << std::string(std::max(0, (int) pathLen - (int) storePathS.size()), ' ');
if (showSize)
printSize(info->narSize);
if (showClosureSize)
printSize(aio().blockOn(store->getClosureSize(info->path)).first);
if (showSigs) {
std::cout << '\t';
Strings ss;
if (info->ultimate) ss.push_back("ultimate");
if (info->ca) ss.push_back("ca:" + renderContentAddress(info->ca));
for (auto & sig : info->sigs) ss.push_back(sig);
std::cout << concatStringsSep(" ", ss);
}
std::cout << std::endl;
}
}
}
};
void registerNixPathInfo()
{
registerCommand<CmdPathInfo>("path-info");
}
}
|