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