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