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