-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathwrapper.cpp
198 lines (180 loc) · 5.97 KB
/
wrapper.cpp
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
// SPDX-License-Identifier: LGPL-3.0
/* this includes fixes
/nix/store/cqhdk51xqxj1990v20y3wfnvhr0r8yds-nix-1.11.15-dev/include/nix/util.hh:362:24: error: implicit instantiation of undefined template 'std::__cxx11::basic_istringstream<char, std::char_traits<char>, std::allocator<char> >'
/nix/store/c30dlkmiyrjxxjv6nv63igjkzcj1fzxi-gcc-6.4.0/include/c++/6.4.0/iosfwd:100:11: note: template is declared here
*/
#include <sstream>
#include <iostream>
#include <unordered_map>
#if NIXVER >= 219
#include <config.h> // #define SYSTEM
#include <util.hh> // restoreSignals
#include <current-process.hh> // restoreProcessContext
#include <shared.hh> // initNix
#include <local-store.hh>
#include <remote-store.hh>
#else
#include <nix/config.h> // #define SYSTEM
#include <nix/util.hh> // restoreSignals
#include <nix/shared.hh> // initNix
#include <nix/local-store.hh>
#include <nix/remote-store.hh>
#endif
#include "wrapper.hpp"
#if NIXVER >= 219
#include <gc-store.hh>
#include <store-cast.hh>
#define findroots(store) require<GcStore>(*store).findRoots(false)
#else
#if NIXVER >= 208
#include <nix/gc-store.hh>
#include <nix/store-cast.hh>
#define findroots(store) require<GcStore>(*store).findRoots(false)
#else
#if NIXVER >= 207
#include <nix/gc-store.hh>
#define findroots(store) requireGcStore(*store).findRoots(false)
#else
#if NIXVER >= 203
#define findroots(store) store->findRoots(false)
#else
#define findroots(store) store->findRoots()
#endif
#endif
#endif
#endif
#if NIXVER >= 204
#define PATH StorePath
#else
#define PATH Path
#endif
#if NIXVER >= 204
// ->deriver is optional<storepath>
#define DERIVER_IS_EMPTY(d) (!d.has_value())
#define DERIVER_GET(d) d.value()
#else
// ->deriver is path, aka string
#define DERIVER_IS_EMPTY(d) d.empty()
#define DERIVER_GET(d) d
#endif
extern "C" {
typedef struct {
std::shared_ptr<const nix::ValidPathInfo> data;
unsigned index;
} Info;
extern void register_node(void *graph, path_t *node);
extern void register_edge(void *graph, unsigned from, unsigned to);
int populateGraph(void *graph, const char* rootPath) {
using namespace nix;
int retcode = handleExceptions("nix-du", [graph, rootPath]() {
initNix();
auto store = openStore();
std::unordered_map<PATH, Info> node_to_id;
// Registers the node if it was not already registered, and return its path info
// Returns: pair of a boolean indicating if it was already visited, and path info
auto get_infos = [&] (const PATH& p) {
auto it = node_to_id.find(p);
if (it==node_to_id.end()) {
Info info = {
store->queryPathInfo(p).get_ptr(), //data
(unsigned)(node_to_id.size()), // index
};
path_t entry;
entry.is_root = 0;
entry.size = info.data->narSize;
#if NIXVER >= 204
std::string path = store->storeDir + "/";
path.append(p.to_string());
#else
std::string path = info.data->path;
#endif
entry.path = path.c_str();
node_to_id[p] = info;
register_node(graph, &entry);
return std::make_pair(false, info);
} else {
return std::make_pair(true, it->second);
}
};
// queue for graph traversal
std::vector<PATH> queue;
// initialise with either all nodes or just the root we want
if (!rootPath) {
// dump all the store
std::set<PATH> paths = store->queryAllValidPaths();
std::copy(paths.begin(), paths.end(), std::back_inserter(queue));
} else {
// dump only the recursive closure of rootPath
#if NIXVER >= 204
const PATH rootDrv = store->followLinksToStorePath(rootPath);
#else
const Path naiveRootPath(rootPath);
const PATH rootDrv = store->followLinksToStorePath(naiveRootPath);
#endif
if (!store->isValidPath(rootDrv)) {
throw Error("'%s' is not a valid path", rootPath);
}
queue.push_back(rootDrv);
}
// follow references in graph traversal, register corresponding edges
while (!queue.empty()) {
PATH path = queue.back();
queue.pop_back();
Info from = get_infos(path).second;
// register edges to references
for (const PATH& dep: from.data->references) {
Info to; bool cached;
std::tie(cached, to) = get_infos(dep);
register_edge(graph, from.index, to.index);
if (!cached) {
queue.push_back(dep);
}
}
// register edges from/to drv if this path has a derivation
if ((settings.gcKeepOutputs || settings.gcKeepDerivations) && (!DERIVER_IS_EMPTY(from.data->deriver)) && store->isValidPath(DERIVER_GET(from.data->deriver))) {
Info drv; bool drv_was_cached;
std::tie(drv_was_cached, drv) = get_infos(DERIVER_GET(from.data->deriver));
if (settings.gcKeepDerivations) {
register_edge(graph, from.index, drv.index);
}
if (settings.gcKeepOutputs) {
register_edge(graph, drv.index, from.index);
}
if (!drv_was_cached) {
queue.push_back(DERIVER_GET(from.data->deriver));
}
}
}
if (!rootPath) {
// register roots and add edge to corresponding store path
unsigned index = node_to_id.size();
#if NIXVER >= 203
for (auto &[storepath, links] : findroots(store)) {
for (auto link: links) {
#else
for (auto root : findroots(store)) {{
PATH link, storepath;
std::tie(link, storepath) = root;
#endif
if (store->isValidPath(storepath)) {
path_t entry;
entry.is_root = 1;
entry.size = link.size();
entry.path = link.c_str();
register_node(graph, &entry);
Info to = get_infos(storepath).second;
register_edge(graph, index, to.index);
++index;
}
}
}
}
});
#if NIXVER >= 204
restoreProcessContext();
#else
restoreSignals();
#endif
return retcode;
}
}