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