-
Notifications
You must be signed in to change notification settings - Fork 5
/
cpigs.cc
256 lines (220 loc) · 5.76 KB
/
cpigs.cc
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
// Copyright © 2022 Alexandre Detiste <[email protected]>
// SPDX-License-Identifier: GPL-2.0-or-later
#include <iostream>
#include <map>
#include <algorithm>
#include <ctime>
#include <string.h>
#include <time.h>
#include "explain.h"
#include "filters.h"
#include "locate.h"
#include "dpkg.h"
#include "shellexp.h"
using namespace std;
#ifndef BUSTER
#include <filesystem>
namespace fs = std::filesystem;
#else
#include <experimental/filesystem>
using namespace std::experimental;
namespace fs = std::experimental::filesystem;
#endif
clock_t beg = clock();
void elapsed(string action)
{
if (getenv("ELAPSED") == NULL) return;
clock_t end = clock();
double elapsed_seconds = (end - beg) * 1000 / CLOCKS_PER_SEC;
cerr << "elapsed " << action << ": " << elapsed_seconds << endl;
beg = end;
}
int usage()
{
cerr << "usage: " << endl;
cerr << " cpigs [-n] [NUMBER] : default format" << endl;
cerr << " cpigs -e : export in ncdu format" << endl;
cerr << " cpigs -c : export in .csv format" << endl;
cerr << " cpigs -C : export in .csv format, also static files" << endl;
return 1;
}
static void output_pigs(long unsigned int limit, const map<string, size_t>& usage)
{
vector<pair<string,size_t>> pigs;
copy(usage.begin(), usage.end(), back_inserter(pigs));
sort(pigs.rbegin(), pigs.rend(), [](const auto &left,
const auto &right) {
return left.second < right.second;
});
for (size_t i = 0; i < pigs.size() && i < limit; ++i) {
if (pigs[i].second > 0) cout << pigs[i].second << " " << pigs[i].first << endl;
}
}
void output_ncdu(vector<string>& cruft_db)
{
// https://dev.yorhel.nl/ncdu/jsonfmt
// https://github.com/rofl0r/ncdu/blob/master/src/dir_export.c
cout << "[1,0,{\"progname\": \"cpigs\", \"progver\": \"0.9\",";
cout << "\"timestamp\": " << int(time(nullptr)) << "},\n";
cout << "[{\"name\":\"/\"}"; // not the ','
fs::path last_dir = "/";
vector<string>::iterator it;
for (it=cruft_db.begin(); it != cruft_db.end(); it++)
{
fs::path cruft = *it;
fs::path dirname;
error_code ec;
if (fs::is_directory(cruft, ec)) {
dirname = cruft;
} else {
dirname = cruft.parent_path();
}
if (last_dir != dirname)
{
auto l = last_dir.begin();
auto d = dirname.begin();
int common_len = 0;
while(l != last_dir.end() && d != dirname.end() && *l == *d)
{
common_len++;
l++;
d++;
}
int len_last_dir = 0;
for(l = last_dir.begin(); l != last_dir.end() ;l++) len_last_dir++;
int closed = len_last_dir - common_len;
for(int c = closed; c; c--) cout << ']';
int skipped = 0;
for(auto& part : dirname)
{
if (skipped >= common_len) {
cout << ",\n[{\"name\":" << part << "}";
}
skipped++;
}
last_dir = dirname;
}
if (!fs::is_directory(cruft, ec)) {
fs::path basename = cruft.filename();
cout << ",\n{\"name\":" << basename;
try {
if(fs::is_symlink(cruft)) {
// some arbitrary value
// still better than /var/cache/pbuilder/base.cow/dev/core -> /proc/kcore
// showing up as 128 TiB and dwarfing everything else
cout << ",\"dsize\":1024";
} else {
auto fsize = fs::file_size(cruft);
cout << ",\"dsize\":" << fsize;
}
}
catch (...) {}
cout << "}";
}
}
for(auto& part : last_dir) { cout << ']'; }
cout << "]" << endl;
}
int main(int argc, char *argv[])
{
long unsigned int limit = 10;
bool ncdu = false, csv = false, static_ = false;
if (argc == 2 && !strcmp(argv[1], "-e")) {
ncdu = true;
} else if (argc == 2 && !strcmp(argv[1], "-c")) {
csv = true;
} else if (argc == 2 && !strcmp(argv[1], "-C")) {
csv = true;
static_ = true;
} else if (argc == 3) {
try {
limit = stoi(argv[2]);
} catch(...) { return usage(); }
} else if (argc == 2) {
try {
limit = stoi(argv[1]);
} catch(...) { return usage(); }
}
vector<string> fs;
read_locate(fs, "/usr/share/cruft/ignore", "/");
elapsed("plocate");
if (csv) cout << "path;package;type;cruft;size" << endl;
vector<string> packages;
vector<string> dpkg;
dpkg_start("/");
read_dpkg(packages, dpkg, static_, "/");
dpkg_end();
elapsed("dpkg");
vector<string> cruft_db;
vector<string>::iterator left=fs.begin();
vector<string>::iterator right=dpkg.begin();
while (left != fs.end() )
{
if (*left==*right) {
left++;
right++;
} else if (*left < *right) {
cruft_db.push_back(*left);
left++;
} else {
right++;
}
if (right == dpkg.end())
while(left !=fs.end()) {cruft_db.push_back(*left); left++;};
}
elapsed("main set match");
if (ncdu) {
output_ncdu(cruft_db);
return 0;
};
vector<owner> globs;
read_filters("/etc/cruft/filters/", "/usr/share/cruft/ruleset", packages, globs);
setenv("CRUFT_ROOT", "", 0);
read_explain("/etc/cruft/explain/", packages, globs);
elapsed("read filters");
std::map<std::string, size_t> usage{{"UNKNOWN", 0}};
vector<string>::iterator cruft=cruft_db.begin();
vector<owner>::iterator owners;
while (cruft != cruft_db.end()) {
owners = globs.begin();
string package = "UNKNOWN";
while (owners != globs.end()) {
bool match;
match = myglob(*cruft,(*owners).path);
if (match) {
package = (*owners).package;
break;
}
owners++;
}
char type;
size_t fsize;
try
{
if (fs::is_symlink(*cruft)) {
type = 'l';
fsize = 1024;
} else if (fs::is_directory(*cruft)) {
type = 'd';
fsize = 1024;
} else {
type = 'f';
fsize = fs::file_size(*cruft);
}
}
catch (...) {
type = '?';
fsize = 1024;
}
if (csv) {
cout << *cruft << ';' << package << ';' << type << ";1;" << fsize << endl;
} else {
if (usage.count(package) == 0) usage[package] = 0;
usage[package] += fsize;
}
cruft++;
}
elapsed("extra vs globs");
output_pigs(limit, usage);
return 0;
}