70322ae3 |
1 | /* |
2 | * Main program for agedu. |
3 | */ |
4 | |
353bc75d |
5 | #include "agedu.h" |
50e82fdc |
6 | |
70322ae3 |
7 | #include "du.h" |
8 | #include "trie.h" |
9 | #include "index.h" |
995db599 |
10 | #include "alloc.h" |
70322ae3 |
11 | #include "html.h" |
12 | #include "httpd.h" |
84849cbd |
13 | #include "fgetline.h" |
70322ae3 |
14 | |
373a02e5 |
15 | /* |
16 | * Path separator. This global variable affects the behaviour of |
17 | * various parts of the code when they need to deal with path |
18 | * separators. The path separator appropriate to a particular data |
19 | * set is encoded in the index file storing that data set; data |
20 | * sets generated on Unix will of course have the default '/', but |
21 | * foreign data sets are conceivable and must be handled correctly. |
22 | */ |
23 | char pathsep = '/'; |
24 | |
70322ae3 |
25 | void fatal(const char *fmt, ...) |
26 | { |
27 | va_list ap; |
28 | fprintf(stderr, "%s: ", PNAME); |
29 | va_start(ap, fmt); |
30 | vfprintf(stderr, fmt, ap); |
31 | va_end(ap); |
32 | fprintf(stderr, "\n"); |
33 | exit(1); |
34 | } |
35 | |
9d0b9596 |
36 | struct inclusion_exclusion { |
0ba55302 |
37 | int type; |
9d0b9596 |
38 | const char *wildcard; |
39 | int path; |
40 | }; |
41 | |
70322ae3 |
42 | struct ctx { |
43 | triebuild *tb; |
44 | dev_t datafile_dev, filesystem_dev; |
45 | ino_t datafile_ino; |
46 | time_t last_output_update; |
8b1f55d6 |
47 | int progress, progwidth; |
84849cbd |
48 | int straight_to_dump; |
9d0b9596 |
49 | struct inclusion_exclusion *inex; |
50 | int ninex; |
51 | int crossfs; |
f59a5d34 |
52 | int usemtime; |
05b0f827 |
53 | int fakeatimes; |
70322ae3 |
54 | }; |
55 | |
84849cbd |
56 | static void dump_line(const char *pathname, const struct trie_file *tf) |
57 | { |
58 | const char *p; |
59 | printf("%llu %llu ", tf->size, tf->atime); |
60 | for (p = pathname; *p; p++) { |
61 | if (*p >= ' ' && *p < 127 && *p != '%') |
62 | putchar(*p); |
63 | else |
64 | printf("%%%02x", (unsigned char)*p); |
65 | } |
66 | putchar('\n'); |
67 | } |
68 | |
9c6e61f2 |
69 | static int gotdata(void *vctx, const char *pathname, const STRUCT_STAT *st) |
70322ae3 |
70 | { |
71 | struct ctx *ctx = (struct ctx *)vctx; |
72 | struct trie_file file; |
73 | time_t t; |
9d0b9596 |
74 | int i, include; |
75 | const char *filename; |
70322ae3 |
76 | |
77 | /* |
78 | * Filter out our own data file. |
79 | */ |
80 | if (st->st_dev == ctx->datafile_dev && st->st_ino == ctx->datafile_ino) |
81 | return 0; |
82 | |
83 | /* |
84 | * Don't cross the streams^W^Wany file system boundary. |
70322ae3 |
85 | */ |
9d0b9596 |
86 | if (!ctx->crossfs && st->st_dev != ctx->filesystem_dev) |
70322ae3 |
87 | return 0; |
88 | |
84849cbd |
89 | file.size = (unsigned long long)512 * st->st_blocks; |
f59a5d34 |
90 | if (ctx->usemtime || (ctx->fakeatimes && S_ISDIR(st->st_mode))) |
05b0f827 |
91 | file.atime = st->st_mtime; |
92 | else |
7e25423c |
93 | file.atime = max(st->st_mtime, st->st_atime); |
0ba55302 |
94 | |
70322ae3 |
95 | /* |
9d0b9596 |
96 | * Filter based on wildcards. |
70322ae3 |
97 | */ |
9d0b9596 |
98 | include = 1; |
373a02e5 |
99 | filename = strrchr(pathname, pathsep); |
9d0b9596 |
100 | if (!filename) |
101 | filename = pathname; |
102 | else |
103 | filename++; |
104 | for (i = 0; i < ctx->ninex; i++) { |
105 | if (fnmatch(ctx->inex[i].wildcard, |
0ba55302 |
106 | ctx->inex[i].path ? pathname : filename, 0) == 0) |
107 | include = ctx->inex[i].type; |
108 | } |
109 | if (include == -1) |
110 | return 0; /* ignore this entry and any subdirs */ |
111 | if (include == 0) { |
112 | /* |
113 | * Here we are supposed to be filtering an entry out, but |
114 | * still recursing into it if it's a directory. However, |
115 | * we can't actually leave out any directory whose |
116 | * subdirectories we then look at. So we cheat, in that |
117 | * case, by setting the size to zero. |
118 | */ |
119 | if (!S_ISDIR(st->st_mode)) |
120 | return 0; /* just ignore */ |
121 | else |
84849cbd |
122 | file.size = 0; |
9d0b9596 |
123 | } |
70322ae3 |
124 | |
84849cbd |
125 | if (ctx->straight_to_dump) |
126 | dump_line(pathname, &file); |
127 | else |
128 | triebuild_add(ctx->tb, pathname, &file); |
70322ae3 |
129 | |
84849cbd |
130 | if (ctx->progress) { |
131 | t = time(NULL); |
132 | if (t != ctx->last_output_update) { |
8b1f55d6 |
133 | fprintf(stderr, "%-*.*s\r", ctx->progwidth, ctx->progwidth, |
134 | pathname); |
135 | fflush(stderr); |
84849cbd |
136 | ctx->last_output_update = t; |
8b1f55d6 |
137 | } |
70322ae3 |
138 | } |
139 | |
140 | return 1; |
141 | } |
142 | |
09fd7619 |
143 | static void scan_error(void *vctx, const char *fmt, ...) |
144 | { |
145 | struct ctx *ctx = (struct ctx *)vctx; |
146 | va_list ap; |
147 | |
148 | if (ctx->progress) { |
149 | fprintf(stderr, "%-*s\r", ctx->progwidth, ""); |
150 | fflush(stderr); |
151 | } |
152 | |
153 | fprintf(stderr, "%s: ", PNAME); |
154 | va_start(ap, fmt); |
155 | vfprintf(stderr, fmt, ap); |
156 | va_end(ap); |
157 | |
158 | ctx->last_output_update--; /* force a progress report next time */ |
159 | } |
160 | |
e9e7a1bf |
161 | static void text_query(const void *mappedfile, const char *querydir, |
16139d21 |
162 | time_t t, int showfiles, int depth) |
70322ae3 |
163 | { |
164 | size_t maxpathlen; |
165 | char *pathbuf; |
166 | unsigned long xi1, xi2; |
16139d21 |
167 | unsigned long long size; |
70322ae3 |
168 | |
169 | maxpathlen = trie_maxpathlen(mappedfile); |
170 | pathbuf = snewn(maxpathlen + 1, char); |
171 | |
172 | /* |
173 | * We want to query everything between the supplied filename |
174 | * (inclusive) and that filename with a ^A on the end |
175 | * (exclusive). So find the x indices for each. |
176 | */ |
256c29a2 |
177 | strcpy(pathbuf, querydir); |
178 | make_successor(pathbuf); |
e9e7a1bf |
179 | xi1 = trie_before(mappedfile, querydir); |
70322ae3 |
180 | xi2 = trie_before(mappedfile, pathbuf); |
181 | |
16139d21 |
182 | if (!showfiles && xi2 - xi1 == 1) |
0313b788 |
183 | return; /* file, or empty dir => no display */ |
184 | |
70322ae3 |
185 | /* |
186 | * Now do the lookups in the age index. |
187 | */ |
16139d21 |
188 | if (xi2 - xi1 == 1) { |
189 | /* |
190 | * We are querying an individual file, so we should not |
191 | * depend on the index entries either side of the node, |
192 | * since they almost certainly don't both exist. Instead, |
193 | * just look up the file's size and atime in the main trie. |
194 | */ |
195 | const struct trie_file *f = trie_getfile(mappedfile, xi1); |
196 | if (f->atime < t) |
197 | size = f->size; |
198 | else |
199 | size = 0; |
200 | } else { |
201 | unsigned long long s1, s2; |
202 | s1 = index_query(mappedfile, xi1, t); |
203 | s2 = index_query(mappedfile, xi2, t); |
204 | size = s2 - s1; |
205 | } |
70322ae3 |
206 | |
16139d21 |
207 | if (size == 0) |
010dd2a2 |
208 | return; /* no space taken up => no display */ |
209 | |
70322ae3 |
210 | if (depth > 0) { |
211 | /* |
212 | * Now scan for first-level subdirectories and report |
213 | * those too. |
214 | */ |
215 | xi1++; |
216 | while (xi1 < xi2) { |
217 | trie_getpath(mappedfile, xi1, pathbuf); |
16139d21 |
218 | text_query(mappedfile, pathbuf, t, showfiles, depth-1); |
256c29a2 |
219 | make_successor(pathbuf); |
70322ae3 |
220 | xi1 = trie_before(mappedfile, pathbuf); |
221 | } |
222 | } |
16e591d6 |
223 | |
224 | /* Display in units of 1Kb */ |
16139d21 |
225 | printf("%-11llu %s\n", (size) / 1024, querydir); |
70322ae3 |
226 | } |
227 | |
56fa1896 |
228 | /* |
229 | * Largely frivolous way to define all my command-line options. I |
230 | * present here a parametric macro which declares a series of |
231 | * _logical_ option identifiers, and for each one declares zero or |
232 | * more short option characters and zero or more long option |
233 | * words. Then I repeatedly invoke that macro with its arguments |
234 | * defined to be various other macros, which allows me to |
235 | * variously: |
236 | * |
237 | * - define an enum allocating a distinct integer value to each |
238 | * logical option id |
239 | * - define a string consisting of precisely all the short option |
240 | * characters |
241 | * - define a string array consisting of all the long option |
242 | * strings |
243 | * - define (with help from auxiliary enums) integer arrays |
244 | * parallel to both of the above giving the logical option id |
245 | * for each physical short and long option |
246 | * - define an array indexed by logical option id indicating |
e9e7a1bf |
247 | * whether the option in question takes a value |
248 | * - define a function which prints out brief online help for all |
249 | * the options. |
56fa1896 |
250 | * |
251 | * It's not at all clear to me that this trickery is actually |
252 | * particularly _efficient_ - it still, after all, requires going |
253 | * linearly through the option list at run time and doing a |
254 | * strcmp, whereas in an ideal world I'd have liked the lists of |
255 | * long and short options to be pre-sorted so that a binary search |
256 | * or some other more efficient lookup was possible. (Not that |
257 | * asymptotic algorithmic complexity is remotely vital in option |
258 | * parsing, but if I were doing this in, say, Lisp or something |
259 | * with an equivalently powerful preprocessor then once I'd had |
260 | * the idea of preparing the option-parsing data structures at |
261 | * compile time I would probably have made the effort to prepare |
262 | * them _properly_. I could have Perl generate me a source file |
263 | * from some sort of description, I suppose, but that would seem |
264 | * like overkill. And in any case, it's more of a challenge to |
265 | * achieve as much as possible by cunning use of cpp and enum than |
266 | * to just write some sensible and logical code in a Turing- |
267 | * complete language. I said it was largely frivolous :-) |
268 | * |
269 | * This approach does have the virtue that it brings together the |
e9e7a1bf |
270 | * option ids, option spellings and help text into a single |
271 | * combined list and defines them all in exactly one place. If I |
272 | * want to add a new option, or a new spelling for an option, I |
273 | * only have to modify the main OPTHELP macro below and then add |
274 | * code to process the new logical id. |
56fa1896 |
275 | * |
276 | * (Though, really, even that isn't ideal, since it still involves |
277 | * modifying the source file in more than one place. In a |
278 | * _properly_ ideal world, I'd be able to interleave the option |
279 | * definitions with the code fragments that process them. And then |
280 | * not bother defining logical identifiers for them at all - those |
281 | * would be automatically generated, since I wouldn't have any |
282 | * need to specify them manually in another part of the code.) |
c5c3510f |
283 | * |
284 | * One other helpful consequence of the enum-based structure here |
285 | * is that it causes a compiler error if I accidentally try to |
286 | * define the same option (short or long) twice. |
56fa1896 |
287 | */ |
288 | |
e9e7a1bf |
289 | #define OPTHELP(NOVAL, VAL, SHORT, LONG, HELPPFX, HELPARG, HELPLINE, HELPOPT) \ |
bf53e756 |
290 | HELPPFX("usage") HELPLINE(PNAME " [options] action [action...]") \ |
e9e7a1bf |
291 | HELPPFX("actions") \ |
292 | VAL(SCAN) SHORT(s) LONG(scan) \ |
293 | HELPARG("directory") HELPOPT("scan and index a directory") \ |
67159944 |
294 | NOVAL(HTTPD) SHORT(w) LONG(web) LONG(server) LONG(httpd) \ |
295 | HELPOPT("serve HTML reports from a temporary web server") \ |
296 | VAL(TEXT) SHORT(t) LONG(text) \ |
297 | HELPARG("subdir") HELPOPT("print a plain text report on a subdirectory") \ |
298 | NOVAL(REMOVE) SHORT(R) LONG(remove) LONG(delete) LONG(unlink) \ |
299 | HELPOPT("remove the index file") \ |
c5c3510f |
300 | NOVAL(DUMP) SHORT(D) LONG(dump) HELPOPT("dump the index file on stdout") \ |
c5c3510f |
301 | NOVAL(LOAD) SHORT(L) LONG(load) \ |
84849cbd |
302 | HELPOPT("load and index a dump file") \ |
67159944 |
303 | VAL(SCANDUMP) SHORT(S) LONG(scan_dump) \ |
304 | HELPARG("directory") HELPOPT("scan only, generating a dump") \ |
e9e7a1bf |
305 | VAL(HTML) SHORT(H) LONG(html) \ |
306 | HELPARG("subdir") HELPOPT("print an HTML report on a subdirectory") \ |
e9e7a1bf |
307 | HELPPFX("options") \ |
308 | VAL(DATAFILE) SHORT(f) LONG(file) \ |
c5c3510f |
309 | HELPARG("filename") HELPOPT("[most modes] specify index file") \ |
56fa1896 |
310 | NOVAL(CROSSFS) LONG(cross_fs) \ |
e9e7a1bf |
311 | HELPOPT("[--scan] cross filesystem boundaries") \ |
56fa1896 |
312 | NOVAL(NOCROSSFS) LONG(no_cross_fs) \ |
e9e7a1bf |
313 | HELPOPT("[--scan] stick to one filesystem") \ |
0ba55302 |
314 | VAL(PRUNE) LONG(prune) \ |
315 | HELPARG("wildcard") HELPOPT("[--scan] prune files matching pattern") \ |
316 | VAL(PRUNEPATH) LONG(prune_path) \ |
317 | HELPARG("wildcard") HELPOPT("[--scan] prune pathnames matching pattern") \ |
67159944 |
318 | VAL(EXCLUDE) LONG(exclude) \ |
319 | HELPARG("wildcard") HELPOPT("[--scan] exclude files matching pattern") \ |
320 | VAL(EXCLUDEPATH) LONG(exclude_path) \ |
321 | HELPARG("wildcard") HELPOPT("[--scan] exclude pathnames matching pattern") \ |
322 | VAL(INCLUDE) LONG(include) \ |
323 | HELPARG("wildcard") HELPOPT("[--scan] include files matching pattern") \ |
324 | VAL(INCLUDEPATH) LONG(include_path) \ |
325 | HELPARG("wildcard") HELPOPT("[--scan] include pathnames matching pattern") \ |
326 | NOVAL(PROGRESS) LONG(progress) LONG(scan_progress) \ |
327 | HELPOPT("[--scan] report progress on stderr") \ |
328 | NOVAL(NOPROGRESS) LONG(no_progress) LONG(no_scan_progress) \ |
329 | HELPOPT("[--scan] do not report progress") \ |
330 | NOVAL(TTYPROGRESS) LONG(tty_progress) LONG(tty_scan_progress) \ |
331 | LONG(progress_tty) LONG(scan_progress_tty) \ |
332 | HELPOPT("[--scan] report progress if stderr is a tty") \ |
05b0f827 |
333 | NOVAL(DIRATIME) LONG(dir_atime) LONG(dir_atimes) \ |
67159944 |
334 | HELPOPT("[--scan,--load] keep real atimes on directories") \ |
05b0f827 |
335 | NOVAL(NODIRATIME) LONG(no_dir_atime) LONG(no_dir_atimes) \ |
67159944 |
336 | HELPOPT("[--scan,--load] fake atimes on directories") \ |
f59a5d34 |
337 | NOVAL(MTIME) LONG(mtime) \ |
338 | HELPOPT("[--scan] use mtime instead of atime") \ |
16139d21 |
339 | NOVAL(SHOWFILES) LONG(files) \ |
340 | HELPOPT("[--web,--html,--text] list individual files") \ |
f2e52893 |
341 | VAL(AGERANGE) SHORT(r) LONG(age_range) LONG(range) LONG(ages) \ |
67159944 |
342 | HELPARG("age[-age]") HELPOPT("[--web,--html] set limits of colour coding") \ |
1e8d78b9 |
343 | VAL(SERVERADDR) LONG(address) LONG(addr) LONG(server_address) \ |
344 | LONG(server_addr) \ |
345 | HELPARG("addr[:port]") HELPOPT("[--web] specify HTTP server address") \ |
e9e7a1bf |
346 | VAL(AUTH) LONG(auth) LONG(http_auth) LONG(httpd_auth) \ |
347 | LONG(server_auth) LONG(web_auth) \ |
348 | HELPARG("type") HELPOPT("[--web] specify HTTP authentication method") \ |
1e8d78b9 |
349 | VAL(AUTHFILE) LONG(auth_file) \ |
350 | HELPARG("filename") HELPOPT("[--web] read HTTP Basic user/pass from file") \ |
351 | VAL(AUTHFD) LONG(auth_fd) \ |
352 | HELPARG("fd") HELPOPT("[--web] read HTTP Basic user/pass from fd") \ |
67159944 |
353 | VAL(TQDEPTH) SHORT(d) LONG(depth) LONG(max_depth) LONG(maximum_depth) \ |
354 | HELPARG("levels") HELPOPT("[--text] recurse to this many levels") \ |
355 | VAL(MINAGE) SHORT(a) LONG(age) LONG(min_age) LONG(minimum_age) \ |
356 | HELPARG("age") HELPOPT("[--text] include only files older than this") \ |
e9e7a1bf |
357 | HELPPFX("also") \ |
358 | NOVAL(HELP) SHORT(h) LONG(help) HELPOPT("display this help text") \ |
359 | NOVAL(VERSION) SHORT(V) LONG(version) HELPOPT("report version number") \ |
360 | NOVAL(LICENCE) LONG(licence) LONG(license) \ |
361 | HELPOPT("display (MIT) licence text") \ |
56fa1896 |
362 | |
363 | #define IGNORE(x) |
364 | #define DEFENUM(x) OPT_ ## x, |
365 | #define ZERO(x) 0, |
366 | #define ONE(x) 1, |
367 | #define STRING(x) #x , |
368 | #define STRINGNOCOMMA(x) #x |
369 | #define SHORTNEWOPT(x) SHORTtmp_ ## x = OPT_ ## x, |
370 | #define SHORTTHISOPT(x) SHORTtmp2_ ## x, SHORTVAL_ ## x = SHORTtmp2_ ## x - 1, |
371 | #define SHORTOPTVAL(x) SHORTVAL_ ## x, |
372 | #define SHORTTMP(x) SHORTtmp3_ ## x, |
373 | #define LONGNEWOPT(x) LONGtmp_ ## x = OPT_ ## x, |
374 | #define LONGTHISOPT(x) LONGtmp2_ ## x, LONGVAL_ ## x = LONGtmp2_ ## x - 1, |
375 | #define LONGOPTVAL(x) LONGVAL_ ## x, |
376 | #define LONGTMP(x) SHORTtmp3_ ## x, |
377 | |
e9e7a1bf |
378 | #define OPTIONS(NOVAL, VAL, SHORT, LONG) \ |
379 | OPTHELP(NOVAL, VAL, SHORT, LONG, IGNORE, IGNORE, IGNORE, IGNORE) |
380 | |
56fa1896 |
381 | enum { OPTIONS(DEFENUM,DEFENUM,IGNORE,IGNORE) NOPTIONS }; |
382 | enum { OPTIONS(IGNORE,IGNORE,SHORTTMP,IGNORE) NSHORTOPTS }; |
383 | enum { OPTIONS(IGNORE,IGNORE,IGNORE,LONGTMP) NLONGOPTS }; |
384 | static const int opthasval[NOPTIONS] = {OPTIONS(ZERO,ONE,IGNORE,IGNORE)}; |
385 | static const char shortopts[] = {OPTIONS(IGNORE,IGNORE,STRINGNOCOMMA,IGNORE)}; |
386 | static const char *const longopts[] = {OPTIONS(IGNORE,IGNORE,IGNORE,STRING)}; |
387 | enum { OPTIONS(SHORTNEWOPT,SHORTNEWOPT,SHORTTHISOPT,IGNORE) }; |
388 | enum { OPTIONS(LONGNEWOPT,LONGNEWOPT,IGNORE,LONGTHISOPT) }; |
389 | static const int shortvals[] = {OPTIONS(IGNORE,IGNORE,SHORTOPTVAL,IGNORE)}; |
390 | static const int longvals[] = {OPTIONS(IGNORE,IGNORE,IGNORE,LONGOPTVAL)}; |
391 | |
e9e7a1bf |
392 | static void usage(FILE *fp) |
393 | { |
394 | char longbuf[80]; |
395 | const char *prefix, *shortopt, *longopt, *optarg; |
396 | int i, optex; |
397 | |
398 | #define HELPRESET prefix = shortopt = longopt = optarg = NULL, optex = -1 |
399 | #define HELPNOVAL(s) optex = 0; |
400 | #define HELPVAL(s) optex = 1; |
401 | #define HELPSHORT(s) if (!shortopt) shortopt = "-" #s; |
402 | #define HELPLONG(s) if (!longopt) { \ |
403 | strcpy(longbuf, "--" #s); longopt = longbuf; \ |
404 | for (i = 0; longbuf[i]; i++) if (longbuf[i] == '_') longbuf[i] = '-'; } |
405 | #define HELPPFX(s) prefix = s; |
406 | #define HELPARG(s) optarg = s; |
407 | #define HELPLINE(s) assert(optex == -1); \ |
408 | fprintf(fp, "%7s%c %s\n", prefix?prefix:"", prefix?':':' ', s); \ |
409 | HELPRESET; |
410 | #define HELPOPT(s) assert((optex == 1 && optarg) || (optex == 0 && !optarg)); \ |
411 | assert(shortopt || longopt); \ |
412 | i = fprintf(fp, "%7s%c %s%s%s%s%s", prefix?prefix:"", prefix?':':' ', \ |
413 | shortopt?shortopt:"", shortopt&&longopt?", ":"", longopt?longopt:"", \ |
414 | optarg?" ":"", optarg?optarg:""); \ |
415 | fprintf(fp, "%*s %s\n", i<32?32-i:0,"",s); HELPRESET; |
416 | |
417 | HELPRESET; |
418 | OPTHELP(HELPNOVAL, HELPVAL, HELPSHORT, HELPLONG, |
419 | HELPPFX, HELPARG, HELPLINE, HELPOPT); |
420 | |
421 | #undef HELPRESET |
422 | #undef HELPNOVAL |
423 | #undef HELPVAL |
424 | #undef HELPSHORT |
425 | #undef HELPLONG |
426 | #undef HELPPFX |
427 | #undef HELPARG |
428 | #undef HELPLINE |
429 | #undef HELPOPT |
430 | } |
431 | |
f2e52893 |
432 | static time_t parse_age(time_t now, const char *agestr) |
433 | { |
434 | time_t t; |
435 | struct tm tm; |
436 | int nunits; |
437 | char unit[2]; |
438 | |
439 | t = now; |
440 | |
441 | if (2 != sscanf(agestr, "%d%1[DdWwMmYy]", &nunits, unit)) { |
442 | fprintf(stderr, "%s: age specification should be a number followed by" |
443 | " one of d,w,m,y\n", PNAME); |
444 | exit(1); |
445 | } |
446 | |
447 | if (unit[0] == 'd') { |
448 | t -= 86400 * nunits; |
449 | } else if (unit[0] == 'w') { |
450 | t -= 86400 * 7 * nunits; |
451 | } else { |
452 | int ym; |
453 | |
454 | tm = *localtime(&t); |
455 | ym = tm.tm_year * 12 + tm.tm_mon; |
456 | |
457 | if (unit[0] == 'm') |
458 | ym -= nunits; |
459 | else |
460 | ym -= 12 * nunits; |
461 | |
462 | tm.tm_year = ym / 12; |
463 | tm.tm_mon = ym % 12; |
464 | |
465 | t = mktime(&tm); |
466 | } |
467 | |
468 | return t; |
469 | } |
470 | |
70322ae3 |
471 | int main(int argc, char **argv) |
472 | { |
473 | int fd, count; |
474 | struct ctx actx, *ctx = &actx; |
475 | struct stat st; |
476 | off_t totalsize, realsize; |
477 | void *mappedfile; |
478 | triewalk *tw; |
479 | indexbuild *ib; |
14601b5d |
480 | const struct trie_file *tf, *prevtf; |
bf53e756 |
481 | char *filename = PNAME ".dat"; |
70322ae3 |
482 | int doing_opts = 1; |
355c3af7 |
483 | enum { TEXT, HTML, SCAN, DUMP, SCANDUMP, LOAD, HTTPD, REMOVE }; |
444c684c |
484 | struct action { |
485 | int mode; |
486 | char *arg; |
487 | } *actions = NULL; |
488 | int nactions = 0, actionsize = 0, action; |
f2e52893 |
489 | time_t now = time(NULL); |
490 | time_t textcutoff = now, htmlnewest = now, htmloldest = now; |
491 | int htmlautoagerange = 1; |
1e8d78b9 |
492 | const char *httpserveraddr = NULL; |
493 | int httpserverport = 0; |
494 | const char *httpauthdata = NULL; |
812e4bf2 |
495 | int auth = HTTPD_AUTH_MAGIC | HTTPD_AUTH_BASIC; |
8b1f55d6 |
496 | int progress = 1; |
9d0b9596 |
497 | struct inclusion_exclusion *inex = NULL; |
498 | int ninex = 0, inexsize = 0; |
499 | int crossfs = 0; |
16e591d6 |
500 | int tqdepth = 1; |
05b0f827 |
501 | int fakediratimes = 1; |
f59a5d34 |
502 | int mtime = 0; |
16139d21 |
503 | int showfiles = 0; |
70322ae3 |
504 | |
56fa1896 |
505 | #ifdef DEBUG_MAD_OPTION_PARSING_MACROS |
506 | { |
507 | static const char *const optnames[NOPTIONS] = { |
508 | OPTIONS(STRING,STRING,IGNORE,IGNORE) |
509 | }; |
510 | int i; |
511 | for (i = 0; i < NSHORTOPTS; i++) |
512 | printf("-%c == %s [%s]\n", shortopts[i], optnames[shortvals[i]], |
513 | opthasval[shortvals[i]] ? "value" : "no value"); |
514 | for (i = 0; i < NLONGOPTS; i++) |
515 | printf("--%s == %s [%s]\n", longopts[i], optnames[longvals[i]], |
516 | opthasval[longvals[i]] ? "value" : "no value"); |
517 | } |
518 | #endif |
519 | |
70322ae3 |
520 | while (--argc > 0) { |
521 | char *p = *++argv; |
70322ae3 |
522 | |
523 | if (doing_opts && *p == '-') { |
56fa1896 |
524 | int wordstart = 1; |
525 | |
70322ae3 |
526 | if (!strcmp(p, "--")) { |
527 | doing_opts = 0; |
56fa1896 |
528 | continue; |
529 | } |
530 | |
531 | p++; |
532 | while (*p) { |
533 | int optid = -1; |
534 | int i; |
535 | char *optval; |
536 | |
537 | if (wordstart && *p == '-') { |
70322ae3 |
538 | /* |
56fa1896 |
539 | * GNU-style long option. |
70322ae3 |
540 | */ |
56fa1896 |
541 | p++; |
542 | optval = strchr(p, '='); |
543 | if (optval) |
544 | *optval++ = '\0'; |
545 | |
546 | for (i = 0; i < NLONGOPTS; i++) { |
547 | const char *opt = longopts[i], *s = p; |
548 | int match = 1; |
549 | /* |
550 | * The underscores in the option names |
551 | * defined above may be given by the user |
552 | * as underscores or dashes, or omitted |
553 | * entirely. |
554 | */ |
555 | while (*opt) { |
556 | if (*opt == '_') { |
557 | if (*s == '-' || *s == '_') |
558 | s++; |
559 | } else { |
560 | if (*opt != *s) { |
561 | match = 0; |
562 | break; |
563 | } |
564 | s++; |
565 | } |
566 | opt++; |
567 | } |
568 | if (match && !*s) { |
569 | optid = longvals[i]; |
570 | break; |
70322ae3 |
571 | } |
572 | } |
56fa1896 |
573 | |
574 | if (optid < 0) { |
575 | fprintf(stderr, "%s: unrecognised option '--%s'\n", |
576 | PNAME, p); |
577 | return 1; |
578 | } |
579 | |
580 | if (!opthasval[optid]) { |
581 | if (optval) { |
582 | fprintf(stderr, "%s: unexpected argument to option" |
583 | " '--%s'\n", PNAME, p); |
812e4bf2 |
584 | return 1; |
585 | } |
56fa1896 |
586 | } else { |
587 | if (!optval) { |
588 | if (--argc > 0) { |
589 | optval = *++argv; |
590 | } else { |
591 | fprintf(stderr, "%s: option '--%s' expects" |
592 | " an argument\n", PNAME, p); |
593 | return 1; |
594 | } |
9d0b9596 |
595 | } |
70322ae3 |
596 | } |
56fa1896 |
597 | |
598 | p += strlen(p); /* finished with this argument word */ |
70322ae3 |
599 | } else { |
56fa1896 |
600 | /* |
601 | * Short option. |
602 | */ |
70322ae3 |
603 | char c = *p++; |
604 | |
56fa1896 |
605 | for (i = 0; i < NSHORTOPTS; i++) |
606 | if (c == shortopts[i]) { |
607 | optid = shortvals[i]; |
608 | break; |
609 | } |
610 | |
611 | if (optid < 0) { |
612 | fprintf(stderr, "%s: unrecognised option '-%c'\n", |
613 | PNAME, c); |
614 | return 1; |
615 | } |
616 | |
617 | if (opthasval[optid]) { |
70322ae3 |
618 | if (*p) { |
619 | optval = p; |
620 | p += strlen(p); |
621 | } else if (--argc > 0) { |
622 | optval = *++argv; |
623 | } else { |
56fa1896 |
624 | fprintf(stderr, "%s: option '-%c' expects" |
70322ae3 |
625 | " an argument\n", PNAME, c); |
626 | return 1; |
627 | } |
56fa1896 |
628 | } else { |
629 | optval = NULL; |
630 | } |
631 | } |
632 | |
633 | wordstart = 0; |
634 | |
635 | /* |
636 | * Now actually process the option. |
637 | */ |
638 | switch (optid) { |
639 | case OPT_HELP: |
e9e7a1bf |
640 | usage(stdout); |
56fa1896 |
641 | return 0; |
642 | case OPT_VERSION: |
e6fde1f7 |
643 | #ifdef PACKAGE_VERSION |
644 | printf("%s, revision %s\n", PNAME, PACKAGE_VERSION); |
645 | #else |
646 | printf("%s: version number not available when not built" |
647 | " via automake\n", PNAME); |
648 | #endif |
56fa1896 |
649 | return 0; |
650 | case OPT_LICENCE: |
5a29503d |
651 | { |
652 | extern const char *const licence[]; |
653 | int i; |
654 | |
655 | for (i = 0; licence[i]; i++) |
656 | fputs(licence[i], stdout); |
657 | |
658 | return 0; |
659 | } |
56fa1896 |
660 | return 0; |
661 | case OPT_SCAN: |
444c684c |
662 | if (nactions >= actionsize) { |
663 | actionsize = nactions * 3 / 2 + 16; |
664 | actions = sresize(actions, actionsize, struct action); |
665 | } |
666 | actions[nactions].mode = SCAN; |
667 | actions[nactions].arg = optval; |
668 | nactions++; |
56fa1896 |
669 | break; |
84849cbd |
670 | case OPT_SCANDUMP: |
444c684c |
671 | if (nactions >= actionsize) { |
672 | actionsize = nactions * 3 / 2 + 16; |
673 | actions = sresize(actions, actionsize, struct action); |
674 | } |
675 | actions[nactions].mode = SCANDUMP; |
676 | actions[nactions].arg = optval; |
677 | nactions++; |
84849cbd |
678 | break; |
56fa1896 |
679 | case OPT_DUMP: |
444c684c |
680 | if (nactions >= actionsize) { |
681 | actionsize = nactions * 3 / 2 + 16; |
682 | actions = sresize(actions, actionsize, struct action); |
683 | } |
684 | actions[nactions].mode = DUMP; |
685 | actions[nactions].arg = NULL; |
686 | nactions++; |
56fa1896 |
687 | break; |
84849cbd |
688 | case OPT_LOAD: |
444c684c |
689 | if (nactions >= actionsize) { |
690 | actionsize = nactions * 3 / 2 + 16; |
691 | actions = sresize(actions, actionsize, struct action); |
692 | } |
693 | actions[nactions].mode = LOAD; |
694 | actions[nactions].arg = NULL; |
695 | nactions++; |
84849cbd |
696 | break; |
56fa1896 |
697 | case OPT_TEXT: |
444c684c |
698 | if (nactions >= actionsize) { |
699 | actionsize = nactions * 3 / 2 + 16; |
700 | actions = sresize(actions, actionsize, struct action); |
701 | } |
702 | actions[nactions].mode = TEXT; |
703 | actions[nactions].arg = optval; |
704 | nactions++; |
56fa1896 |
705 | break; |
706 | case OPT_HTML: |
444c684c |
707 | if (nactions >= actionsize) { |
708 | actionsize = nactions * 3 / 2 + 16; |
709 | actions = sresize(actions, actionsize, struct action); |
710 | } |
711 | actions[nactions].mode = HTML; |
712 | actions[nactions].arg = optval; |
713 | nactions++; |
56fa1896 |
714 | break; |
715 | case OPT_HTTPD: |
444c684c |
716 | if (nactions >= actionsize) { |
717 | actionsize = nactions * 3 / 2 + 16; |
718 | actions = sresize(actions, actionsize, struct action); |
719 | } |
720 | actions[nactions].mode = HTTPD; |
721 | actions[nactions].arg = NULL; |
722 | nactions++; |
56fa1896 |
723 | break; |
355c3af7 |
724 | case OPT_REMOVE: |
725 | if (nactions >= actionsize) { |
726 | actionsize = nactions * 3 / 2 + 16; |
727 | actions = sresize(actions, actionsize, struct action); |
728 | } |
729 | actions[nactions].mode = REMOVE; |
730 | actions[nactions].arg = NULL; |
731 | nactions++; |
732 | break; |
56fa1896 |
733 | case OPT_PROGRESS: |
734 | progress = 2; |
735 | break; |
736 | case OPT_NOPROGRESS: |
737 | progress = 0; |
738 | break; |
739 | case OPT_TTYPROGRESS: |
740 | progress = 1; |
741 | break; |
742 | case OPT_CROSSFS: |
743 | crossfs = 1; |
744 | break; |
745 | case OPT_NOCROSSFS: |
746 | crossfs = 0; |
747 | break; |
05b0f827 |
748 | case OPT_DIRATIME: |
749 | fakediratimes = 0; |
750 | break; |
751 | case OPT_NODIRATIME: |
752 | fakediratimes = 1; |
753 | break; |
16139d21 |
754 | case OPT_SHOWFILES: |
755 | showfiles = 1; |
756 | break; |
f59a5d34 |
757 | case OPT_MTIME: |
758 | mtime = 1; |
759 | break; |
56fa1896 |
760 | case OPT_DATAFILE: |
761 | filename = optval; |
762 | break; |
16e591d6 |
763 | case OPT_TQDEPTH: |
764 | tqdepth = atoi(optval); |
765 | break; |
56fa1896 |
766 | case OPT_MINAGE: |
f2e52893 |
767 | textcutoff = parse_age(now, optval); |
768 | break; |
769 | case OPT_AGERANGE: |
770 | if (!strcmp(optval, "auto")) { |
771 | htmlautoagerange = 1; |
772 | } else { |
773 | char *q = optval + strcspn(optval, "-:"); |
774 | if (*q) |
775 | *q++ = '\0'; |
776 | htmloldest = parse_age(now, optval); |
777 | htmlnewest = *q ? parse_age(now, q) : now; |
778 | htmlautoagerange = 0; |
779 | } |
56fa1896 |
780 | break; |
1e8d78b9 |
781 | case OPT_SERVERADDR: |
782 | { |
783 | char *port; |
784 | if (optval[0] == '[' && |
785 | (port = strchr(optval, ']')) != NULL) |
786 | port++; |
787 | else |
788 | port = optval; |
789 | port += strcspn(port, ":"); |
790 | if (port) |
791 | *port++ = '\0'; |
792 | httpserveraddr = optval; |
793 | httpserverport = atoi(port); |
794 | } |
795 | break; |
56fa1896 |
796 | case OPT_AUTH: |
797 | if (!strcmp(optval, "magic")) |
798 | auth = HTTPD_AUTH_MAGIC; |
799 | else if (!strcmp(optval, "basic")) |
800 | auth = HTTPD_AUTH_BASIC; |
801 | else if (!strcmp(optval, "none")) |
802 | auth = HTTPD_AUTH_NONE; |
803 | else if (!strcmp(optval, "default")) |
804 | auth = HTTPD_AUTH_MAGIC | HTTPD_AUTH_BASIC; |
f2e52893 |
805 | else if (!strcmp(optval, "help") || |
806 | !strcmp(optval, "list")) { |
bf53e756 |
807 | printf(PNAME ": supported HTTP authentication types" |
f2e52893 |
808 | " are:\n" |
809 | " magic use Linux /proc/net/tcp to" |
810 | " determine owner of peer socket\n" |
811 | " basic HTTP Basic username and" |
812 | " password authentication\n" |
813 | " default use 'magic' if possible, " |
814 | " otherwise fall back to 'basic'\n" |
815 | " none unauthenticated HTTP (if" |
816 | " the data file is non-confidential)\n"); |
817 | return 0; |
818 | } else { |
56fa1896 |
819 | fprintf(stderr, "%s: unrecognised authentication" |
820 | " type '%s'\n%*s options are 'magic'," |
821 | " 'basic', 'none', 'default'\n", |
822 | PNAME, optval, (int)strlen(PNAME), ""); |
823 | return 1; |
824 | } |
825 | break; |
1e8d78b9 |
826 | case OPT_AUTHFILE: |
827 | case OPT_AUTHFD: |
828 | { |
829 | int fd; |
830 | char namebuf[40]; |
831 | const char *name; |
832 | char *authbuf; |
833 | int authlen, authsize; |
834 | int ret; |
835 | |
836 | if (optid == OPT_AUTHFILE) { |
837 | fd = open(optval, O_RDONLY); |
838 | if (fd < 0) { |
839 | fprintf(stderr, "%s: %s: open: %s\n", PNAME, |
840 | optval, strerror(errno)); |
841 | return 1; |
842 | } |
843 | name = optval; |
844 | } else { |
845 | fd = atoi(optval); |
846 | name = namebuf; |
847 | sprintf(namebuf, "fd %d", fd); |
848 | } |
849 | |
850 | authlen = 0; |
851 | authsize = 256; |
852 | authbuf = snewn(authsize, char); |
853 | while ((ret = read(fd, authbuf+authlen, |
854 | authsize-authlen)) > 0) { |
855 | authlen += ret; |
856 | if ((authsize - authlen) < (authsize / 16)) { |
857 | authsize = authlen * 3 / 2 + 4096; |
858 | authbuf = sresize(authbuf, authsize, char); |
859 | } |
860 | } |
861 | if (ret < 0) { |
862 | fprintf(stderr, "%s: %s: read: %s\n", PNAME, |
863 | name, strerror(errno)); |
864 | return 1; |
865 | } |
866 | if (optid == OPT_AUTHFILE) |
867 | close(fd); |
868 | httpauthdata = authbuf; |
869 | } |
870 | break; |
56fa1896 |
871 | case OPT_INCLUDE: |
872 | case OPT_INCLUDEPATH: |
873 | case OPT_EXCLUDE: |
874 | case OPT_EXCLUDEPATH: |
0ba55302 |
875 | case OPT_PRUNE: |
876 | case OPT_PRUNEPATH: |
56fa1896 |
877 | if (ninex >= inexsize) { |
878 | inexsize = ninex * 3 / 2 + 16; |
879 | inex = sresize(inex, inexsize, |
880 | struct inclusion_exclusion); |
881 | } |
882 | inex[ninex].path = (optid == OPT_INCLUDEPATH || |
0ba55302 |
883 | optid == OPT_EXCLUDEPATH || |
884 | optid == OPT_PRUNEPATH); |
885 | inex[ninex].type = (optid == OPT_INCLUDE ? 1 : |
886 | optid == OPT_INCLUDEPATH ? 1 : |
887 | optid == OPT_EXCLUDE ? 0 : |
888 | optid == OPT_EXCLUDEPATH ? 0 : |
889 | optid == OPT_PRUNE ? -1 : |
890 | /* optid == OPT_PRUNEPATH ? */ -1); |
56fa1896 |
891 | inex[ninex].wildcard = optval; |
892 | ninex++; |
893 | break; |
894 | } |
895 | } |
70322ae3 |
896 | } else { |
e9e7a1bf |
897 | fprintf(stderr, "%s: unexpected argument '%s'\n", PNAME, p); |
898 | return 1; |
70322ae3 |
899 | } |
900 | } |
901 | |
444c684c |
902 | if (nactions == 0) { |
e9e7a1bf |
903 | usage(stderr); |
904 | return 1; |
444c684c |
905 | } |
906 | |
907 | for (action = 0; action < nactions; action++) { |
908 | int mode = actions[action].mode; |
909 | |
910 | if (mode == SCAN || mode == SCANDUMP || mode == LOAD) { |
911 | const char *scandir = actions[action].arg; |
14601b5d |
912 | |
444c684c |
913 | if (mode == LOAD) { |
914 | char *buf = fgetline(stdin); |
915 | unsigned newpathsep; |
916 | buf[strcspn(buf, "\r\n")] = '\0'; |
bf53e756 |
917 | if (1 != sscanf(buf, DUMPHDR "%x", |
444c684c |
918 | &newpathsep)) { |
919 | fprintf(stderr, "%s: header in dump file not recognised\n", |
920 | PNAME); |
921 | return 1; |
922 | } |
923 | pathsep = (char)newpathsep; |
924 | sfree(buf); |
84849cbd |
925 | } |
70322ae3 |
926 | |
444c684c |
927 | if (mode == SCAN || mode == LOAD) { |
928 | /* |
929 | * Prepare to write out the index file. |
930 | */ |
cc7db507 |
931 | fd = open(filename, O_RDWR | O_TRUNC | O_CREAT, |
932 | S_IRUSR | S_IWUSR); |
444c684c |
933 | if (fd < 0) { |
934 | fprintf(stderr, "%s: %s: open: %s\n", PNAME, filename, |
935 | strerror(errno)); |
936 | return 1; |
937 | } |
938 | if (fstat(fd, &st) < 0) { |
bf53e756 |
939 | perror(PNAME ": fstat"); |
444c684c |
940 | return 1; |
941 | } |
942 | ctx->datafile_dev = st.st_dev; |
943 | ctx->datafile_ino = st.st_ino; |
944 | ctx->straight_to_dump = 0; |
945 | } else { |
946 | ctx->datafile_dev = -1; |
947 | ctx->datafile_ino = -1; |
948 | ctx->straight_to_dump = 1; |
84849cbd |
949 | } |
444c684c |
950 | |
951 | if (mode == SCAN || mode == SCANDUMP) { |
952 | if (stat(scandir, &st) < 0) { |
953 | fprintf(stderr, "%s: %s: stat: %s\n", PNAME, scandir, |
954 | strerror(errno)); |
955 | return 1; |
956 | } |
957 | ctx->filesystem_dev = crossfs ? 0 : st.st_dev; |
84849cbd |
958 | } |
70322ae3 |
959 | |
444c684c |
960 | ctx->inex = inex; |
961 | ctx->ninex = ninex; |
962 | ctx->crossfs = crossfs; |
05b0f827 |
963 | ctx->fakeatimes = fakediratimes; |
f59a5d34 |
964 | ctx->usemtime = mtime; |
444c684c |
965 | |
966 | ctx->last_output_update = time(NULL); |
967 | |
968 | /* progress==1 means report progress only if stderr is a tty */ |
969 | if (progress == 1) |
970 | progress = isatty(2) ? 2 : 0; |
971 | ctx->progress = progress; |
972 | { |
973 | struct winsize ws; |
9cb5a01c |
974 | if (progress && |
975 | ioctl(2, TIOCGWINSZ, &ws) == 0 && |
976 | ws.ws_col > 0) |
444c684c |
977 | ctx->progwidth = ws.ws_col - 1; |
978 | else |
979 | ctx->progwidth = 79; |
84849cbd |
980 | } |
84849cbd |
981 | |
444c684c |
982 | if (mode == SCANDUMP) |
bf53e756 |
983 | printf(DUMPHDR "%02x\n", (unsigned char)pathsep); |
8b1f55d6 |
984 | |
444c684c |
985 | /* |
986 | * Scan the directory tree, and write out the trie component |
987 | * of the data file. |
988 | */ |
989 | if (mode != SCANDUMP) { |
990 | ctx->tb = triebuild_new(fd); |
991 | } |
992 | if (mode == LOAD) { |
993 | char *buf; |
994 | int line = 2; |
995 | while ((buf = fgetline(stdin)) != NULL) { |
996 | struct trie_file tf; |
997 | char *p, *q; |
998 | |
999 | buf[strcspn(buf, "\r\n")] = '\0'; |
1000 | |
1001 | p = buf; |
1002 | q = p; |
1003 | while (*p && *p != ' ') p++; |
1004 | if (!*p) { |
1005 | fprintf(stderr, "%s: dump file line %d: expected at least" |
1006 | " three fields\n", PNAME, line); |
1007 | return 1; |
1008 | } |
1009 | *p++ = '\0'; |
1010 | tf.size = strtoull(q, NULL, 10); |
1011 | q = p; |
1012 | while (*p && *p != ' ') p++; |
1013 | if (!*p) { |
1014 | fprintf(stderr, "%s: dump file line %d: expected at least" |
1015 | " three fields\n", PNAME, line); |
1016 | return 1; |
1017 | } |
1018 | *p++ = '\0'; |
1019 | tf.atime = strtoull(q, NULL, 10); |
1020 | q = buf; |
1021 | while (*p) { |
1022 | int c = *p; |
1023 | if (*p == '%') { |
1024 | int i; |
1025 | p++; |
1026 | c = 0; |
1027 | for (i = 0; i < 2; i++) { |
de693987 |
1028 | c *= 16; |
444c684c |
1029 | if (*p >= '0' && *p <= '9') |
1030 | c += *p - '0'; |
1031 | else if (*p >= 'A' && *p <= 'F') |
1032 | c += *p - ('A' - 10); |
1033 | else if (*p >= 'a' && *p <= 'f') |
1034 | c += *p - ('a' - 10); |
1035 | else { |
1036 | fprintf(stderr, "%s: dump file line %d: unable" |
1037 | " to parse hex escape\n", PNAME, line); |
1038 | } |
1039 | p++; |
1040 | } |
1041 | } |
1042 | *q++ = c; |
1043 | p++; |
1044 | } |
1045 | *q = '\0'; |
1046 | triebuild_add(ctx->tb, buf, &tf); |
1047 | sfree(buf); |
de693987 |
1048 | line++; |
444c684c |
1049 | } |
1050 | } else { |
09fd7619 |
1051 | du(scandir, gotdata, scan_error, ctx); |
444c684c |
1052 | } |
1053 | if (mode != SCANDUMP) { |
14601b5d |
1054 | size_t maxpathlen; |
522edd92 |
1055 | size_t delta; |
14601b5d |
1056 | char *buf, *prevbuf; |
1057 | |
444c684c |
1058 | count = triebuild_finish(ctx->tb); |
1059 | triebuild_free(ctx->tb); |
84849cbd |
1060 | |
444c684c |
1061 | if (ctx->progress) { |
1062 | fprintf(stderr, "%-*s\r", ctx->progwidth, ""); |
1063 | fflush(stderr); |
1064 | } |
84849cbd |
1065 | |
444c684c |
1066 | /* |
1067 | * Work out how much space the cumulative index trees |
1068 | * will take; enlarge the file, and memory-map it. |
1069 | */ |
1070 | if (fstat(fd, &st) < 0) { |
bf53e756 |
1071 | perror(PNAME ": fstat"); |
444c684c |
1072 | return 1; |
1073 | } |
84849cbd |
1074 | |
522edd92 |
1075 | printf("Built pathname index, %d entries," |
1076 | " %llu bytes of index\n", count, |
50e82fdc |
1077 | (unsigned long long)st.st_size); |
444c684c |
1078 | |
522edd92 |
1079 | totalsize = index_initial_size(st.st_size, count); |
1080 | totalsize += totalsize / 10; |
444c684c |
1081 | |
1082 | if (lseek(fd, totalsize-1, SEEK_SET) < 0) { |
bf53e756 |
1083 | perror(PNAME ": lseek"); |
84849cbd |
1084 | return 1; |
1085 | } |
444c684c |
1086 | if (write(fd, "\0", 1) < 1) { |
bf53e756 |
1087 | perror(PNAME ": write"); |
84849cbd |
1088 | return 1; |
1089 | } |
444c684c |
1090 | |
444c684c |
1091 | mappedfile = mmap(NULL, totalsize, PROT_READ|PROT_WRITE,MAP_SHARED, fd, 0); |
1092 | if (!mappedfile) { |
bf53e756 |
1093 | perror(PNAME ": mmap"); |
444c684c |
1094 | return 1; |
84849cbd |
1095 | } |
444c684c |
1096 | |
05b0f827 |
1097 | if (fakediratimes) { |
1098 | printf("Faking directory atimes\n"); |
1099 | trie_fake_dir_atimes(mappedfile); |
1100 | } |
1101 | |
1102 | printf("Building index\n"); |
522edd92 |
1103 | ib = indexbuild_new(mappedfile, st.st_size, count, &delta); |
14601b5d |
1104 | maxpathlen = trie_maxpathlen(mappedfile); |
1105 | buf = snewn(maxpathlen, char); |
1106 | prevbuf = snewn(maxpathlen, char); |
444c684c |
1107 | tw = triewalk_new(mappedfile); |
14601b5d |
1108 | prevbuf[0] = '\0'; |
1109 | tf = triewalk_next(tw, buf); |
1110 | assert(tf); |
1111 | while (1) { |
1112 | int i; |
1113 | |
522edd92 |
1114 | if (totalsize - indexbuild_realsize(ib) < delta) { |
645dbd49 |
1115 | const void *oldfile = mappedfile; |
1116 | ptrdiff_t diff; |
1117 | |
522edd92 |
1118 | /* |
1119 | * Unmap the file, grow it, and remap it. |
1120 | */ |
1121 | munmap(mappedfile, totalsize); |
1122 | |
1123 | totalsize += delta; |
1124 | totalsize += totalsize / 10; |
1125 | |
1126 | if (lseek(fd, totalsize-1, SEEK_SET) < 0) { |
1127 | perror(PNAME ": lseek"); |
1128 | return 1; |
1129 | } |
1130 | if (write(fd, "\0", 1) < 1) { |
1131 | perror(PNAME ": write"); |
1132 | return 1; |
1133 | } |
1134 | |
1135 | mappedfile = mmap(NULL, totalsize, PROT_READ|PROT_WRITE,MAP_SHARED, fd, 0); |
1136 | if (!mappedfile) { |
1137 | perror(PNAME ": mmap"); |
1138 | return 1; |
1139 | } |
1140 | |
1141 | indexbuild_rebase(ib, mappedfile); |
1142 | triewalk_rebase(tw, mappedfile); |
645dbd49 |
1143 | diff = (const unsigned char *)mappedfile - |
1144 | (const unsigned char *)oldfile; |
1145 | if (prevtf) |
1146 | prevtf = (const struct trie_file *) |
1147 | (((const unsigned char *)prevtf) + diff); |
1148 | if (tf) |
1149 | tf = (const struct trie_file *) |
1150 | (((const unsigned char *)tf) + diff); |
522edd92 |
1151 | } |
1152 | |
14601b5d |
1153 | /* |
1154 | * Get the next file from the index. So we are |
1155 | * currently holding, and have not yet |
1156 | * indexed, prevtf (with pathname prevbuf) and |
1157 | * tf (with pathname buf). |
1158 | */ |
1159 | prevtf = tf; |
1160 | memcpy(prevbuf, buf, maxpathlen); |
1161 | tf = triewalk_next(tw, buf); |
1162 | |
1163 | if (!tf) |
1164 | buf[0] = '\0'; |
1165 | |
1166 | /* |
1167 | * Find the first differing character position |
1168 | * between our two pathnames. |
1169 | */ |
1170 | for (i = 0; prevbuf[i] && prevbuf[i] == buf[i]; i++); |
1171 | |
1172 | /* |
1173 | * If prevbuf was a directory name and buf is |
1174 | * something inside that directory, then |
1175 | * trie_before() will be called on prevbuf |
1176 | * itself. Hence we must drop a tag before it, |
1177 | * so that the resulting index is usable. |
1178 | */ |
1179 | if ((!prevbuf[i] && (buf[i] == pathsep || |
1180 | (i > 0 && buf[i-1] == pathsep)))) |
1181 | indexbuild_tag(ib); |
1182 | |
1183 | /* |
1184 | * Add prevtf to the index. |
1185 | */ |
1186 | indexbuild_add(ib, prevtf); |
1187 | |
1188 | if (!tf) { |
1189 | /* |
1190 | * Drop an unconditional final tag, and |
1191 | * get out of this loop. |
1192 | */ |
1193 | indexbuild_tag(ib); |
1194 | break; |
1195 | } |
14601b5d |
1196 | |
1197 | /* |
1198 | * If prevbuf was a filename inside some |
1199 | * directory which buf is outside, then |
1200 | * trie_before() will be called on some |
1201 | * pathname either equal to buf or epsilon |
1202 | * less than it. Either way, we're going to |
1203 | * need to drop a tag after prevtf. |
1204 | */ |
1205 | if (strchr(prevbuf+i, pathsep) || !tf) |
1206 | indexbuild_tag(ib); |
1207 | } |
1208 | |
444c684c |
1209 | triewalk_free(tw); |
1210 | realsize = indexbuild_realsize(ib); |
1211 | indexbuild_free(ib); |
1212 | |
1213 | munmap(mappedfile, totalsize); |
1214 | ftruncate(fd, realsize); |
1215 | close(fd); |
522edd92 |
1216 | printf("Final index file size = %llu bytes\n", |
50e82fdc |
1217 | (unsigned long long)realsize); |
84849cbd |
1218 | } |
444c684c |
1219 | } else if (mode == TEXT) { |
1220 | char *querydir = actions[action].arg; |
1221 | size_t pathlen; |
70322ae3 |
1222 | |
444c684c |
1223 | fd = open(filename, O_RDONLY); |
1224 | if (fd < 0) { |
1225 | fprintf(stderr, "%s: %s: open: %s\n", PNAME, filename, |
1226 | strerror(errno)); |
1227 | return 1; |
1228 | } |
1229 | if (fstat(fd, &st) < 0) { |
bf53e756 |
1230 | perror(PNAME ": fstat"); |
444c684c |
1231 | return 1; |
1232 | } |
1233 | totalsize = st.st_size; |
1234 | mappedfile = mmap(NULL, totalsize, PROT_READ, MAP_SHARED, fd, 0); |
1235 | if (!mappedfile) { |
bf53e756 |
1236 | perror(PNAME ": mmap"); |
444c684c |
1237 | return 1; |
84849cbd |
1238 | } |
444c684c |
1239 | pathsep = trie_pathsep(mappedfile); |
70322ae3 |
1240 | |
84849cbd |
1241 | /* |
444c684c |
1242 | * Trim trailing slash, just in case. |
84849cbd |
1243 | */ |
444c684c |
1244 | pathlen = strlen(querydir); |
1245 | if (pathlen > 0 && querydir[pathlen-1] == pathsep) |
1246 | querydir[--pathlen] = '\0'; |
1247 | |
16139d21 |
1248 | text_query(mappedfile, querydir, textcutoff, showfiles, tqdepth); |
56cae6e1 |
1249 | |
1250 | munmap(mappedfile, totalsize); |
444c684c |
1251 | } else if (mode == HTML) { |
1252 | char *querydir = actions[action].arg; |
92d3b326 |
1253 | size_t pathlen, maxpathlen; |
1254 | char *pathbuf; |
444c684c |
1255 | struct html_config cfg; |
1256 | unsigned long xi; |
1257 | char *html; |
1258 | |
1259 | fd = open(filename, O_RDONLY); |
1260 | if (fd < 0) { |
1261 | fprintf(stderr, "%s: %s: open: %s\n", PNAME, filename, |
1262 | strerror(errno)); |
1263 | return 1; |
1264 | } |
84849cbd |
1265 | if (fstat(fd, &st) < 0) { |
bf53e756 |
1266 | perror(PNAME ": fstat"); |
84849cbd |
1267 | return 1; |
1268 | } |
444c684c |
1269 | totalsize = st.st_size; |
1270 | mappedfile = mmap(NULL, totalsize, PROT_READ, MAP_SHARED, fd, 0); |
1271 | if (!mappedfile) { |
bf53e756 |
1272 | perror(PNAME ": mmap"); |
444c684c |
1273 | return 1; |
1274 | } |
1275 | pathsep = trie_pathsep(mappedfile); |
70322ae3 |
1276 | |
92d3b326 |
1277 | maxpathlen = trie_maxpathlen(mappedfile); |
1278 | pathbuf = snewn(maxpathlen, char); |
1279 | |
444c684c |
1280 | /* |
1281 | * Trim trailing slash, just in case. |
1282 | */ |
1283 | pathlen = strlen(querydir); |
1284 | if (pathlen > 0 && querydir[pathlen-1] == pathsep) |
1285 | querydir[--pathlen] = '\0'; |
1286 | |
1287 | xi = trie_before(mappedfile, querydir); |
92d3b326 |
1288 | if (xi >= trie_count(mappedfile) || |
1289 | (trie_getpath(mappedfile, xi, pathbuf), |
1290 | strcmp(pathbuf, querydir))) { |
1291 | fprintf(stderr, "%s: pathname '%s' does not exist in index\n" |
1292 | "%*s(check it is spelled exactly as it is in the " |
1293 | "index, including\n%*sany leading './')\n", |
1294 | PNAME, querydir, |
1295 | (int)(1+sizeof(PNAME)), "", |
1296 | (int)(1+sizeof(PNAME)), ""); |
1297 | } else if (!index_has_root(mappedfile, xi)) { |
1298 | fprintf(stderr, "%s: pathname '%s' is" |
1299 | " a file, not a directory\n", PNAME, querydir); |
1300 | } else { |
1301 | cfg.format = NULL; |
1302 | cfg.autoage = htmlautoagerange; |
1303 | cfg.oldest = htmloldest; |
1304 | cfg.newest = htmlnewest; |
16139d21 |
1305 | cfg.showfiles = showfiles; |
92d3b326 |
1306 | html = html_query(mappedfile, xi, &cfg); |
1307 | fputs(html, stdout); |
1308 | } |
56cae6e1 |
1309 | |
1310 | munmap(mappedfile, totalsize); |
92d3b326 |
1311 | sfree(pathbuf); |
444c684c |
1312 | } else if (mode == DUMP) { |
1313 | size_t maxpathlen; |
1314 | char *buf; |
70322ae3 |
1315 | |
444c684c |
1316 | fd = open(filename, O_RDONLY); |
1317 | if (fd < 0) { |
1318 | fprintf(stderr, "%s: %s: open: %s\n", PNAME, filename, |
1319 | strerror(errno)); |
84849cbd |
1320 | return 1; |
1321 | } |
444c684c |
1322 | if (fstat(fd, &st) < 0) { |
bf53e756 |
1323 | perror(PNAME ": fstat"); |
84849cbd |
1324 | return 1; |
1325 | } |
444c684c |
1326 | totalsize = st.st_size; |
1327 | mappedfile = mmap(NULL, totalsize, PROT_READ, MAP_SHARED, fd, 0); |
84849cbd |
1328 | if (!mappedfile) { |
bf53e756 |
1329 | perror(PNAME ": mmap"); |
84849cbd |
1330 | return 1; |
1331 | } |
444c684c |
1332 | pathsep = trie_pathsep(mappedfile); |
1333 | |
1334 | maxpathlen = trie_maxpathlen(mappedfile); |
1335 | buf = snewn(maxpathlen, char); |
84849cbd |
1336 | |
bf53e756 |
1337 | printf(DUMPHDR "%02x\n", (unsigned char)pathsep); |
84849cbd |
1338 | tw = triewalk_new(mappedfile); |
444c684c |
1339 | while ((tf = triewalk_next(tw, buf)) != NULL) |
1340 | dump_line(buf, tf); |
84849cbd |
1341 | triewalk_free(tw); |
56cae6e1 |
1342 | |
1343 | munmap(mappedfile, totalsize); |
444c684c |
1344 | } else if (mode == HTTPD) { |
1345 | struct html_config pcfg; |
1346 | struct httpd_config dcfg; |
70322ae3 |
1347 | |
444c684c |
1348 | fd = open(filename, O_RDONLY); |
1349 | if (fd < 0) { |
1350 | fprintf(stderr, "%s: %s: open: %s\n", PNAME, filename, |
1351 | strerror(errno)); |
1352 | return 1; |
1353 | } |
1354 | if (fstat(fd, &st) < 0) { |
bf53e756 |
1355 | perror(PNAME ": fstat"); |
444c684c |
1356 | return 1; |
1357 | } |
1358 | totalsize = st.st_size; |
1359 | mappedfile = mmap(NULL, totalsize, PROT_READ, MAP_SHARED, fd, 0); |
1360 | if (!mappedfile) { |
bf53e756 |
1361 | perror(PNAME ": mmap"); |
444c684c |
1362 | return 1; |
1363 | } |
1364 | pathsep = trie_pathsep(mappedfile); |
1365 | |
1366 | dcfg.address = httpserveraddr; |
1367 | dcfg.port = httpserverport; |
1368 | dcfg.basicauthdata = httpauthdata; |
1369 | pcfg.format = NULL; |
1370 | pcfg.autoage = htmlautoagerange; |
1371 | pcfg.oldest = htmloldest; |
1372 | pcfg.newest = htmlnewest; |
16139d21 |
1373 | pcfg.showfiles = showfiles; |
444c684c |
1374 | run_httpd(mappedfile, auth, &dcfg, &pcfg); |
56cae6e1 |
1375 | munmap(mappedfile, totalsize); |
355c3af7 |
1376 | } else if (mode == REMOVE) { |
1377 | if (remove(filename) < 0) { |
1378 | fprintf(stderr, "%s: %s: remove: %s\n", PNAME, filename, |
1379 | strerror(errno)); |
1380 | return 1; |
1381 | } |
70322ae3 |
1382 | } |
70322ae3 |
1383 | } |
1384 | |
1385 | return 0; |
1386 | } |