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