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