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