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