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