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