Skip to content

Commit c2b0d87

Browse files
committed
exportReferencesGraph: Export more complete info in JSON format
This writes info about every path in the closure in the same format as ‘nix path-info --json’. Thus it also includes NAR hashes and sizes. Example: [ { "path": "/nix/store/10h6li26i7g6z3mdpvra09yyf10mmzdr-hello-2.10", "narHash": "sha256:0ckdc4z20kkmpqdilx0wl6cricxv90lh85xpv2qljppcmz6vzcxl", "narSize": 197648, "references": [ "/nix/store/10h6li26i7g6z3mdpvra09yyf10mmzdr-hello-2.10", "/nix/store/27binbdy296qvjycdgr1535v8872vz3z-glibc-2.24" ], "closureSize": 20939776 }, { "path": "/nix/store/27binbdy296qvjycdgr1535v8872vz3z-glibc-2.24", "narHash": "sha256:1nfn3m3p98y1c0kd0brp80dn9n5mycwgrk183j17rajya0h7gax3", "narSize": 20742128, "references": [ "/nix/store/27binbdy296qvjycdgr1535v8872vz3z-glibc-2.24" ], "closureSize": 20742128 } ] Fixes #1134.
1 parent 6de33a9 commit c2b0d87

File tree

4 files changed

+90
-50
lines changed

4 files changed

+90
-50
lines changed

src/libstore/build.cc

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include "builtins.hh"
1111
#include "finally.hh"
1212
#include "compression.hh"
13+
#include "json.hh"
1314

1415
#include <algorithm>
1516
#include <iostream>
@@ -2273,9 +2274,18 @@ void DerivationGoal::doExportReferencesGraph()
22732274
}
22742275
}
22752276

2276-
/* Write closure info to `fileName'. */
2277+
/* Write closure info to <fileName>. */
22772278
writeFile(tmpDir + "/" + fileName,
22782279
worker.store.makeValidityRegistration(paths, false, false));
2280+
2281+
/* Write a more comprehensive JSON serialisation to
2282+
<fileName>.json. */
2283+
std::ostringstream str;
2284+
{
2285+
JSONPlaceholder jsonRoot(str, true);
2286+
worker.store.pathInfoToJSON(jsonRoot, paths, false, true);
2287+
}
2288+
writeFile(tmpDir + "/" + fileName + ".json", str.str());
22792289
}
22802290
}
22812291

src/libstore/store-api.cc

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include "util.hh"
55
#include "nar-info-disk-cache.hh"
66
#include "thread-pool.hh"
7+
#include "json.hh"
78

89
#include <future>
910

@@ -439,6 +440,64 @@ string Store::makeValidityRegistration(const PathSet & paths,
439440
}
440441

441442

443+
void Store::pathInfoToJSON(JSONPlaceholder & jsonOut, const PathSet & storePaths,
444+
bool includeImpureInfo, bool showClosureSize)
445+
{
446+
auto jsonList = jsonOut.list();
447+
448+
for (auto storePath : storePaths) {
449+
auto info = queryPathInfo(storePath);
450+
storePath = info->path;
451+
452+
auto jsonPath = jsonList.object();
453+
jsonPath
454+
.attr("path", storePath)
455+
.attr("narHash", info->narHash.to_string())
456+
.attr("narSize", info->narSize);
457+
458+
{
459+
auto jsonRefs = jsonPath.list("references");
460+
for (auto & ref : info->references)
461+
jsonRefs.elem(ref);
462+
}
463+
464+
if (info->ca != "")
465+
jsonPath.attr("ca", info->ca);
466+
467+
if (showClosureSize)
468+
jsonPath.attr("closureSize", getClosureSize(storePath));
469+
470+
if (!includeImpureInfo) continue;
471+
472+
if (info->deriver != "")
473+
jsonPath.attr("deriver", info->deriver);
474+
475+
if (info->registrationTime)
476+
jsonPath.attr("registrationTime", info->registrationTime);
477+
478+
if (info->ultimate)
479+
jsonPath.attr("ultimate", info->ultimate);
480+
481+
if (!info->sigs.empty()) {
482+
auto jsonSigs = jsonPath.list("signatures");
483+
for (auto & sig : info->sigs)
484+
jsonSigs.elem(sig);
485+
}
486+
}
487+
}
488+
489+
490+
unsigned long long Store::getClosureSize(const Path & storePath)
491+
{
492+
unsigned long long totalSize = 0;
493+
PathSet closure;
494+
computeFSClosure(storePath, closure, false, false);
495+
for (auto & p : closure)
496+
totalSize += queryPathInfo(p)->narSize;
497+
return totalSize;
498+
}
499+
500+
442501
const Store::Stats & Store::getStats()
443502
{
444503
{

src/libstore/store-api.hh

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ struct Derivation;
2222
class FSAccessor;
2323
class NarInfoDiskCache;
2424
class Store;
25+
class JSONPlaceholder;
2526

2627

2728
/* Size of the hash part of store paths, in base-32 characters. */
@@ -469,6 +470,19 @@ public:
469470
string makeValidityRegistration(const PathSet & paths,
470471
bool showDerivers, bool showHash);
471472

473+
/* Write a JSON representation of store path metadata, such as the
474+
hash and the references. If ‘includeImpureInfo’ is true,
475+
variable elements such as the registration time are
476+
included. If ‘showClosureSize’ is true, the closure size of
477+
each path is included. */
478+
void pathInfoToJSON(JSONPlaceholder & jsonOut, const PathSet & storePaths,
479+
bool includeImpureInfo, bool showClosureSize);
480+
481+
/* Return the size of the closure of the specified path, that is,
482+
the sum of the size of the NAR serialisation of each path in
483+
the closure. */
484+
unsigned long long getClosureSize(const Path & storePath);
485+
472486
/* Optimise the disk space usage of the Nix store by hard-linking files
473487
with the same contents. */
474488
virtual void optimiseStore() = 0;

src/nix/path-info.cc

Lines changed: 6 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -65,55 +65,12 @@ struct CmdPathInfo : StorePathsCommand
6565
for (auto & storePath : storePaths)
6666
pathLen = std::max(pathLen, storePath.size());
6767

68-
auto getClosureSize = [&](const Path & storePath) -> unsigned long long {
69-
unsigned long long totalSize = 0;
70-
PathSet closure;
71-
store->computeFSClosure(storePath, closure, false, false);
72-
for (auto & p : closure)
73-
totalSize += store->queryPathInfo(p)->narSize;
74-
return totalSize;
75-
};
76-
7768
if (json) {
78-
JSONList jsonRoot(std::cout, true);
79-
80-
for (auto storePath : storePaths) {
81-
auto info = store->queryPathInfo(storePath);
82-
storePath = info->path;
83-
84-
auto jsonPath = jsonRoot.object();
85-
jsonPath
86-
.attr("path", storePath)
87-
.attr("narHash", info->narHash.to_string())
88-
.attr("narSize", info->narSize);
89-
90-
if (showClosureSize)
91-
jsonPath.attr("closureSize", getClosureSize(storePath));
92-
93-
if (info->deriver != "")
94-
jsonPath.attr("deriver", info->deriver);
95-
96-
{
97-
auto jsonRefs = jsonPath.list("references");
98-
for (auto & ref : info->references)
99-
jsonRefs.elem(ref);
100-
}
101-
102-
if (info->registrationTime)
103-
jsonPath.attr("registrationTime", info->registrationTime);
104-
105-
if (info->ultimate)
106-
jsonPath.attr("ultimate", info->ultimate);
107-
108-
if (info->ca != "")
109-
jsonPath.attr("ca", info->ca);
110-
111-
if (!info->sigs.empty()) {
112-
auto jsonSigs = jsonPath.list("signatures");
113-
for (auto & sig : info->sigs)
114-
jsonSigs.elem(sig);
115-
}
116-
}
69+
JSONPlaceholder jsonRoot(std::cout, true);
70+
store->pathInfoToJSON(jsonRoot,
71+
// FIXME: preserve order?
72+
PathSet(storePaths.begin(), storePaths.end()),
73+
true, showClosureSize);
11774
}
11875

11976
else {
@@ -128,7 +85,7 @@ struct CmdPathInfo : StorePathsCommand
12885
std::cout << '\t' << std::setw(11) << info->narSize;
12986

13087
if (showClosureSize)
131-
std::cout << '\t' << std::setw(11) << getClosureSize(storePath);
88+
std::cout << '\t' << std::setw(11) << store->getClosureSize(storePath);
13289

13390
if (showSigs) {
13491
std::cout << '\t';

0 commit comments

Comments
 (0)