1d774081c2166e3c5fd6dd92b8e1e5e34ced6ac8
2 * Main program for agedu.
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.
25 void fatal(const char *fmt
, ...)
28 fprintf(stderr
, "%s: ", PNAME
);
30 vfprintf(stderr
, fmt
, ap
);
32 fprintf(stderr
, "\n");
36 struct inclusion_exclusion
{
44 dev_t datafile_dev
, filesystem_dev
;
46 time_t last_output_update
;
47 int progress
, progwidth
;
49 struct inclusion_exclusion
*inex
;
56 static void dump_line(const char *pathname
, const struct trie_file
*tf
)
59 if (printf("%llu %llu ", tf
->size
, tf
->atime
) < 0) goto error
;
60 for (p
= pathname
; *p
; p
++) {
61 if (*p
>= ' ' && *p
< 127 && *p
!= '%') {
62 if (putchar(*p
) == EOF
) goto error
;
64 if (printf("%%%02x", (unsigned char)*p
) < 0) goto error
;
67 if (putchar('\n') == EOF
) goto error
;
70 fatal("standard output: %s", strerror(errno
));
73 static int gotdata(void *vctx
, const char *pathname
, const STRUCT_STAT
*st
)
75 struct ctx
*ctx
= (struct ctx
*)vctx
;
76 struct trie_file file
;
82 * Filter out our own data file.
84 if (st
->st_dev
== ctx
->datafile_dev
&& st
->st_ino
== ctx
->datafile_ino
)
88 * Don't cross the streams^W^Wany file system boundary.
90 if (!ctx
->crossfs
&& st
->st_dev
!= ctx
->filesystem_dev
)
93 file
.size
= (unsigned long long)512 * st
->st_blocks
;
94 if (ctx
->usemtime
|| (ctx
->fakeatimes
&& S_ISDIR(st
->st_mode
)))
95 file
.atime
= st
->st_mtime
;
97 file
.atime
= max(st
->st_mtime
, st
->st_atime
);
100 * Filter based on wildcards.
103 filename
= strrchr(pathname
, pathsep
);
108 for (i
= 0; i
< ctx
->ninex
; i
++) {
109 if (fnmatch(ctx
->inex
[i
].wildcard
,
110 ctx
->inex
[i
].path ? pathname
: filename
, 0) == 0)
111 include
= ctx
->inex
[i
].type
;
114 return 0; /* ignore this entry and any subdirs */
117 * Here we are supposed to be filtering an entry out, but
118 * still recursing into it if it's a directory. However,
119 * we can't actually leave out any directory whose
120 * subdirectories we then look at. So we cheat, in that
121 * case, by setting the size to zero.
123 if (!S_ISDIR(st
->st_mode
))
124 return 0; /* just ignore */
129 if (ctx
->straight_to_dump
)
130 dump_line(pathname
, &file
);
132 triebuild_add(ctx
->tb
, pathname
, &file
);
136 if (t
!= ctx
->last_output_update
) {
137 fprintf(stderr
, "%-*.*s\r", ctx
->progwidth
, ctx
->progwidth
,
140 ctx
->last_output_update
= t
;
147 static void scan_error(void *vctx
, const char *fmt
, ...)
149 struct ctx
*ctx
= (struct ctx
*)vctx
;
153 fprintf(stderr
, "%-*s\r", ctx
->progwidth
, "");
157 fprintf(stderr
, "%s: ", PNAME
);
159 vfprintf(stderr
, fmt
, ap
);
162 ctx
->last_output_update
--; /* force a progress report next time */
165 static void text_query(const void *mappedfile
, const char *querydir
,
166 time_t t
, int showfiles
, int depth
, FILE *fp
)
170 unsigned long xi1
, xi2
;
171 unsigned long long size
;
173 maxpathlen
= trie_maxpathlen(mappedfile
);
174 pathbuf
= snewn(maxpathlen
+ 1, char);
177 * We want to query everything between the supplied filename
178 * (inclusive) and that filename with a ^A on the end
179 * (exclusive). So find the x indices for each.
181 strcpy(pathbuf
, querydir
);
182 make_successor(pathbuf
);
183 xi1
= trie_before(mappedfile
, querydir
);
184 xi2
= trie_before(mappedfile
, pathbuf
);
186 if (!showfiles
&& xi2
- xi1
== 1)
187 return; /* file, or empty dir => no display */
190 * Now do the lookups in the age index.
192 if (xi2
- xi1
== 1) {
194 * We are querying an individual file, so we should not
195 * depend on the index entries either side of the node,
196 * since they almost certainly don't both exist. Instead,
197 * just look up the file's size and atime in the main trie.
199 const struct trie_file
*f
= trie_getfile(mappedfile
, xi1
);
205 unsigned long long s1
, s2
;
206 s1
= index_query(mappedfile
, xi1
, t
);
207 s2
= index_query(mappedfile
, xi2
, t
);
212 return; /* no space taken up => no display */
216 * Now scan for first-level subdirectories and report
219 int newdepth
= (depth
> 0 ? depth
- 1 : depth
);
222 trie_getpath(mappedfile
, xi1
, pathbuf
);
223 text_query(mappedfile
, pathbuf
, t
, showfiles
, newdepth
, fp
);
224 make_successor(pathbuf
);
225 xi1
= trie_before(mappedfile
, pathbuf
);
229 /* Display in units of 1Kb */
230 fprintf(fp
, "%-11llu %s\n", (size
) / 1024, querydir
);
234 * Largely frivolous way to define all my command-line options. I
235 * present here a parametric macro which declares a series of
236 * _logical_ option identifiers, and for each one declares zero or
237 * more short option characters and zero or more long option
238 * words. Then I repeatedly invoke that macro with its arguments
239 * defined to be various other macros, which allows me to
242 * - define an enum allocating a distinct integer value to each
244 * - define a string consisting of precisely all the short option
246 * - define a string array consisting of all the long option
248 * - define (with help from auxiliary enums) integer arrays
249 * parallel to both of the above giving the logical option id
250 * for each physical short and long option
251 * - define an array indexed by logical option id indicating
252 * whether the option in question takes a value
253 * - define a function which prints out brief online help for all
256 * It's not at all clear to me that this trickery is actually
257 * particularly _efficient_ - it still, after all, requires going
258 * linearly through the option list at run time and doing a
259 * strcmp, whereas in an ideal world I'd have liked the lists of
260 * long and short options to be pre-sorted so that a binary search
261 * or some other more efficient lookup was possible. (Not that
262 * asymptotic algorithmic complexity is remotely vital in option
263 * parsing, but if I were doing this in, say, Lisp or something
264 * with an equivalently powerful preprocessor then once I'd had
265 * the idea of preparing the option-parsing data structures at
266 * compile time I would probably have made the effort to prepare
267 * them _properly_. I could have Perl generate me a source file
268 * from some sort of description, I suppose, but that would seem
269 * like overkill. And in any case, it's more of a challenge to
270 * achieve as much as possible by cunning use of cpp and enum than
271 * to just write some sensible and logical code in a Turing-
272 * complete language. I said it was largely frivolous :-)
274 * This approach does have the virtue that it brings together the
275 * option ids, option spellings and help text into a single
276 * combined list and defines them all in exactly one place. If I
277 * want to add a new option, or a new spelling for an option, I
278 * only have to modify the main OPTHELP macro below and then add
279 * code to process the new logical id.
281 * (Though, really, even that isn't ideal, since it still involves
282 * modifying the source file in more than one place. In a
283 * _properly_ ideal world, I'd be able to interleave the option
284 * definitions with the code fragments that process them. And then
285 * not bother defining logical identifiers for them at all - those
286 * would be automatically generated, since I wouldn't have any
287 * need to specify them manually in another part of the code.)
289 * One other helpful consequence of the enum-based structure here
290 * is that it causes a compiler error if I accidentally try to
291 * define the same option (short or long) twice.
294 #define OPTHELP(NOVAL, VAL, SHORT, LONG, HELPPFX, HELPARG, HELPLINE, HELPOPT) \
295 HELPPFX("usage") HELPLINE(PNAME " [options] action [action...]") \
297 VAL(SCAN) SHORT(s) LONG(scan) \
298 HELPARG("directory") HELPOPT("scan and index a directory") \
299 NOVAL(HTTPD) SHORT(w) LONG(web) LONG(server) LONG(httpd) \
300 HELPOPT("serve HTML reports from a temporary web server") \
301 VAL(TEXT) SHORT(t) LONG(text) \
302 HELPARG("subdir") HELPOPT("print a plain text report on a subdirectory") \
303 NOVAL(REMOVE) SHORT(R) LONG(remove) LONG(delete) LONG(unlink) \
304 HELPOPT("remove the index file") \
305 NOVAL(DUMP) SHORT(D) LONG(dump) HELPOPT("dump the index file on stdout") \
306 NOVAL(LOAD) SHORT(L) LONG(load) \
307 HELPOPT("load and index a dump file") \
308 VAL(SCANDUMP) SHORT(S) LONG(scan_dump) \
309 HELPARG("directory") HELPOPT("scan only, generating a dump") \
310 VAL(HTML) SHORT(H) LONG(html) \
311 HELPARG("subdir") HELPOPT("print an HTML report on a subdirectory") \
312 NOVAL(CGI) LONG(cgi) \
313 HELPOPT("do the right thing when run from a CGI script") \
315 VAL(DATAFILE) SHORT(f) LONG(file) \
316 HELPARG("filename") HELPOPT("[most modes] specify index file") \
317 NOVAL(CROSSFS) LONG(cross_fs) \
318 HELPOPT("[--scan] cross filesystem boundaries") \
319 NOVAL(NOCROSSFS) LONG(no_cross_fs) \
320 HELPOPT("[--scan] stick to one filesystem") \
321 VAL(PRUNE) LONG(prune) \
322 HELPARG("wildcard") HELPOPT("[--scan] prune files matching pattern") \
323 VAL(PRUNEPATH) LONG(prune_path) \
324 HELPARG("wildcard") HELPOPT("[--scan] prune pathnames matching pattern") \
325 VAL(EXCLUDE) LONG(exclude) \
326 HELPARG("wildcard") HELPOPT("[--scan] exclude files matching pattern") \
327 VAL(EXCLUDEPATH) LONG(exclude_path) \
328 HELPARG("wildcard") HELPOPT("[--scan] exclude pathnames matching pattern") \
329 VAL(INCLUDE) LONG(include) \
330 HELPARG("wildcard") HELPOPT("[--scan] include files matching pattern") \
331 VAL(INCLUDEPATH) LONG(include_path) \
332 HELPARG("wildcard") HELPOPT("[--scan] include pathnames matching pattern") \
333 NOVAL(PROGRESS) LONG(progress) LONG(scan_progress) \
334 HELPOPT("[--scan] report progress on stderr") \
335 NOVAL(NOPROGRESS) LONG(no_progress) LONG(no_scan_progress) \
336 HELPOPT("[--scan] do not report progress") \
337 NOVAL(TTYPROGRESS) LONG(tty_progress) LONG(tty_scan_progress) \
338 LONG(progress_tty) LONG(scan_progress_tty) \
339 HELPOPT("[--scan] report progress if stderr is a tty") \
340 NOVAL(DIRATIME) LONG(dir_atime) LONG(dir_atimes) \
341 HELPOPT("[--scan,--load] keep real atimes on directories") \
342 NOVAL(NODIRATIME) LONG(no_dir_atime) LONG(no_dir_atimes) \
343 HELPOPT("[--scan,--load] fake atimes on directories") \
344 NOVAL(MTIME) LONG(mtime) \
345 HELPOPT("[--scan] use mtime instead of atime") \
346 NOVAL(SHOWFILES) LONG(files) \
347 HELPOPT("[--web,--html,--text] list individual files") \
348 VAL(AGERANGE) SHORT(r) LONG(age_range) LONG(range) LONG(ages) \
349 HELPARG("age[-age]") HELPOPT("[--web,--html] set limits of colour coding") \
350 VAL(OUTFILE) SHORT(o) LONG(output) \
351 HELPARG("filename") HELPOPT("[--html] specify output file or directory name") \
352 VAL(SERVERADDR) LONG(address) LONG(addr) LONG(server_address) \
354 HELPARG("addr[:port]") HELPOPT("[--web] specify HTTP server address") \
355 VAL(AUTH) LONG(auth) LONG(http_auth) LONG(httpd_auth) \
356 LONG(server_auth) LONG(web_auth) \
357 HELPARG("type") HELPOPT("[--web] specify HTTP authentication method") \
358 VAL(AUTHFILE) LONG(auth_file) \
359 HELPARG("filename") HELPOPT("[--web] read HTTP Basic user/pass from file") \
360 VAL(AUTHFD) LONG(auth_fd) \
361 HELPARG("fd") HELPOPT("[--web] read HTTP Basic user/pass from fd") \
362 VAL(DEPTH) SHORT(d) LONG(depth) LONG(max_depth) LONG(maximum_depth) \
363 HELPARG("levels") HELPOPT("[--text,--html] recurse to this many levels") \
364 VAL(MINAGE) SHORT(a) LONG(age) LONG(min_age) LONG(minimum_age) \
365 HELPARG("age") HELPOPT("[--text] include only files older than this") \
367 NOVAL(HELP) SHORT(h) LONG(help) HELPOPT("display this help text") \
368 NOVAL(VERSION) SHORT(V) LONG(version) HELPOPT("report version number") \
369 NOVAL(LICENCE) LONG(licence) LONG(license) \
370 HELPOPT("display (MIT) licence text") \
373 #define DEFENUM(x) OPT_ ## x,
376 #define STRING(x) #x ,
377 #define STRINGNOCOMMA(x) #x
378 #define SHORTNEWOPT(x) SHORTtmp_ ## x = OPT_ ## x,
379 #define SHORTTHISOPT(x) SHORTtmp2_ ## x, SHORTVAL_ ## x = SHORTtmp2_ ## x - 1,
380 #define SHORTOPTVAL(x) SHORTVAL_ ## x,
381 #define SHORTTMP(x) SHORTtmp3_ ## x,
382 #define LONGNEWOPT(x) LONGtmp_ ## x = OPT_ ## x,
383 #define LONGTHISOPT(x) LONGtmp2_ ## x, LONGVAL_ ## x = LONGtmp2_ ## x - 1,
384 #define LONGOPTVAL(x) LONGVAL_ ## x,
385 #define LONGTMP(x) SHORTtmp3_ ## x,
387 #define OPTIONS(NOVAL, VAL, SHORT, LONG) \
388 OPTHELP(NOVAL, VAL, SHORT, LONG, IGNORE, IGNORE, IGNORE, IGNORE)
390 enum { OPTIONS(DEFENUM
,DEFENUM
,IGNORE
,IGNORE
) NOPTIONS
};
391 enum { OPTIONS(IGNORE
,IGNORE
,SHORTTMP
,IGNORE
) NSHORTOPTS
};
392 enum { OPTIONS(IGNORE
,IGNORE
,IGNORE
,LONGTMP
) NLONGOPTS
};
393 static const int opthasval
[NOPTIONS
] = {OPTIONS(ZERO
,ONE
,IGNORE
,IGNORE
)};
394 static const char shortopts
[] = {OPTIONS(IGNORE
,IGNORE
,STRINGNOCOMMA
,IGNORE
)};
395 static const char *const longopts
[] = {OPTIONS(IGNORE
,IGNORE
,IGNORE
,STRING
)};
396 enum { OPTIONS(SHORTNEWOPT
,SHORTNEWOPT
,SHORTTHISOPT
,IGNORE
) };
397 enum { OPTIONS(LONGNEWOPT
,LONGNEWOPT
,IGNORE
,LONGTHISOPT
) };
398 static const int shortvals
[] = {OPTIONS(IGNORE
,IGNORE
,SHORTOPTVAL
,IGNORE
)};
399 static const int longvals
[] = {OPTIONS(IGNORE
,IGNORE
,IGNORE
,LONGOPTVAL
)};
401 static void usage(FILE *fp
)
404 const char *prefix
, *shortopt
, *longopt
, *optarg
;
407 #define HELPRESET prefix = shortopt = longopt = optarg = NULL, optex = -1
408 #define HELPNOVAL(s) optex = 0;
409 #define HELPVAL(s) optex = 1;
410 #define HELPSHORT(s) if (!shortopt) shortopt = "-" #s;
411 #define HELPLONG(s) if (!longopt) { \
412 strcpy(longbuf, "--" #s); longopt = longbuf; \
413 for (i = 0; longbuf[i]; i++) if (longbuf[i] == '_') longbuf[i] = '-'; }
414 #define HELPPFX(s) prefix = s;
415 #define HELPARG(s) optarg = s;
416 #define HELPLINE(s) assert(optex == -1); \
417 fprintf(fp, "%7s%c %s\n", prefix?prefix:"", prefix?':':' ', s); \
419 #define HELPOPT(s) assert((optex == 1 && optarg) || (optex == 0 && !optarg)); \
420 assert(shortopt || longopt); \
421 i = fprintf(fp, "%7s%c %s%s%s%s%s", prefix?prefix:"", prefix?':':' ', \
422 shortopt?shortopt:"", shortopt&&longopt?", ":"", longopt?longopt:"", \
423 optarg?" ":"", optarg?optarg:""); \
424 fprintf(fp, "%*s %s\n", i<32?32-i:0,"",s); HELPRESET;
427 OPTHELP(HELPNOVAL
, HELPVAL
, HELPSHORT
, HELPLONG
,
428 HELPPFX
, HELPARG
, HELPLINE
, HELPOPT
);
441 static time_t parse_age(time_t now
, const char *agestr
)
450 if (2 != sscanf(agestr
, "%d%1[DdWwMmYy]", &nunits
, unit
)) {
451 fprintf(stderr
, "%s: age specification should be a number followed by"
452 " one of d,w,m,y\n", PNAME
);
456 if (unit
[0] == 'd') {
458 } else if (unit
[0] == 'w') {
459 t
-= 86400 * 7 * nunits
;
464 ym
= tm
.tm_year
* 12 + tm
.tm_mon
;
471 tm
.tm_year
= ym
/ 12;
480 int main(int argc
, char **argv
)
483 struct ctx actx
, *ctx
= &actx
;
485 off_t totalsize
, realsize
;
489 const struct trie_file
*tf
, *prevtf
;
490 char *filename
= PNAME
".dat";
492 enum { TEXT
, HTML
, SCAN
, DUMP
, SCANDUMP
, LOAD
, HTTPD
, REMOVE
};
497 int nactions
= 0, actionsize
= 0, action
;
498 time_t now
= time(NULL
);
499 time_t textcutoff
= now
, htmlnewest
= now
, htmloldest
= now
;
500 int htmlautoagerange
= 1;
501 const char *httpserveraddr
= NULL
;
502 int httpserverport
= 0;
503 const char *httpauthdata
= NULL
;
504 const char *outfile
= NULL
;
505 int auth
= HTTPD_AUTH_MAGIC
| HTTPD_AUTH_BASIC
;
507 struct inclusion_exclusion
*inex
= NULL
;
508 int ninex
= 0, inexsize
= 0;
510 int depth
= -1, gotdepth
= 0;
511 int fakediratimes
= 1;
515 #ifdef DEBUG_MAD_OPTION_PARSING_MACROS
517 static const char *const optnames
[NOPTIONS
] = {
518 OPTIONS(STRING
,STRING
,IGNORE
,IGNORE
)
521 for (i
= 0; i
< NSHORTOPTS
; i
++)
522 printf("-%c == %s [%s]\n", shortopts
[i
], optnames
[shortvals
[i
]],
523 opthasval
[shortvals
[i
]] ?
"value" : "no value");
524 for (i
= 0; i
< NLONGOPTS
; i
++)
525 printf("--%s == %s [%s]\n", longopts
[i
], optnames
[longvals
[i
]],
526 opthasval
[longvals
[i
]] ?
"value" : "no value");
533 if (doing_opts
&& *p
== '-') {
536 if (!strcmp(p
, "--")) {
547 if (wordstart
&& *p
== '-') {
549 * GNU-style long option.
552 optval
= strchr(p
, '=');
556 for (i
= 0; i
< NLONGOPTS
; i
++) {
557 const char *opt
= longopts
[i
], *s
= p
;
560 * The underscores in the option names
561 * defined above may be given by the user
562 * as underscores or dashes, or omitted
567 if (*s
== '-' || *s
== '_')
585 fprintf(stderr
, "%s: unrecognised option '--%s'\n",
590 if (!opthasval
[optid
]) {
592 fprintf(stderr
, "%s: unexpected argument to option"
593 " '--%s'\n", PNAME
, p
);
601 fprintf(stderr
, "%s: option '--%s' expects"
602 " an argument\n", PNAME
, p
);
608 p
+= strlen(p
); /* finished with this argument word */
615 for (i
= 0; i
< NSHORTOPTS
; i
++)
616 if (c
== shortopts
[i
]) {
617 optid
= shortvals
[i
];
622 fprintf(stderr
, "%s: unrecognised option '-%c'\n",
627 if (opthasval
[optid
]) {
631 } else if (--argc
> 0) {
634 fprintf(stderr
, "%s: option '-%c' expects"
635 " an argument\n", PNAME
, c
);
646 * Now actually process the option.
653 #ifdef PACKAGE_VERSION
654 printf("%s, revision %s\n", PNAME
, PACKAGE_VERSION
);
656 printf("%s: version number not available when not built"
657 " via automake\n", PNAME
);
662 extern const char *const licence
[];
665 for (i
= 0; licence
[i
]; i
++)
666 fputs(licence
[i
], stdout
);
672 if (nactions
>= actionsize
) {
673 actionsize
= nactions
* 3 / 2 + 16;
674 actions
= sresize(actions
, actionsize
, struct action
);
676 actions
[nactions
].mode
= SCAN
;
677 actions
[nactions
].arg
= optval
;
681 if (nactions
>= actionsize
) {
682 actionsize
= nactions
* 3 / 2 + 16;
683 actions
= sresize(actions
, actionsize
, struct action
);
685 actions
[nactions
].mode
= SCANDUMP
;
686 actions
[nactions
].arg
= optval
;
690 if (nactions
>= actionsize
) {
691 actionsize
= nactions
* 3 / 2 + 16;
692 actions
= sresize(actions
, actionsize
, struct action
);
694 actions
[nactions
].mode
= DUMP
;
695 actions
[nactions
].arg
= NULL
;
699 if (nactions
>= actionsize
) {
700 actionsize
= nactions
* 3 / 2 + 16;
701 actions
= sresize(actions
, actionsize
, struct action
);
703 actions
[nactions
].mode
= LOAD
;
704 actions
[nactions
].arg
= NULL
;
708 if (nactions
>= actionsize
) {
709 actionsize
= nactions
* 3 / 2 + 16;
710 actions
= sresize(actions
, actionsize
, struct action
);
712 actions
[nactions
].mode
= TEXT
;
713 actions
[nactions
].arg
= optval
;
718 if (nactions
>= actionsize
) {
719 actionsize
= nactions
* 3 / 2 + 16;
720 actions
= sresize(actions
, actionsize
, struct action
);
722 actions
[nactions
].mode
= HTML
;
723 actions
[nactions
].arg
= (optid
== OPT_HTML ? optval
:
728 if (nactions
>= actionsize
) {
729 actionsize
= nactions
* 3 / 2 + 16;
730 actions
= sresize(actions
, actionsize
, struct action
);
732 actions
[nactions
].mode
= HTTPD
;
733 actions
[nactions
].arg
= NULL
;
737 if (nactions
>= actionsize
) {
738 actionsize
= nactions
* 3 / 2 + 16;
739 actions
= sresize(actions
, actionsize
, struct action
);
741 actions
[nactions
].mode
= REMOVE
;
742 actions
[nactions
].arg
= NULL
;
751 case OPT_TTYPROGRESS
:
776 if (!strcasecmp(optval
, "unlimited") ||
777 !strcasecmp(optval
, "infinity") ||
778 !strcasecmp(optval
, "infinite") ||
779 !strcasecmp(optval
, "inf") ||
780 !strcasecmp(optval
, "maximum") ||
781 !strcasecmp(optval
, "max"))
784 depth
= atoi(optval
);
791 textcutoff
= parse_age(now
, optval
);
794 if (!strcmp(optval
, "auto")) {
795 htmlautoagerange
= 1;
797 char *q
= optval
+ strcspn(optval
, "-:");
800 htmloldest
= parse_age(now
, optval
);
801 htmlnewest
= *q ?
parse_age(now
, q
) : now
;
802 htmlautoagerange
= 0;
808 if (optval
[0] == '[' &&
809 (port
= strchr(optval
, ']')) != NULL
)
813 port
+= strcspn(port
, ":");
816 httpserveraddr
= optval
;
817 httpserverport
= atoi(port
);
821 if (!strcmp(optval
, "magic"))
822 auth
= HTTPD_AUTH_MAGIC
;
823 else if (!strcmp(optval
, "basic"))
824 auth
= HTTPD_AUTH_BASIC
;
825 else if (!strcmp(optval
, "none"))
826 auth
= HTTPD_AUTH_NONE
;
827 else if (!strcmp(optval
, "default"))
828 auth
= HTTPD_AUTH_MAGIC
| HTTPD_AUTH_BASIC
;
829 else if (!strcmp(optval
, "help") ||
830 !strcmp(optval
, "list")) {
831 printf(PNAME
": supported HTTP authentication types"
833 " magic use Linux /proc/net/tcp to"
834 " determine owner of peer socket\n"
835 " basic HTTP Basic username and"
836 " password authentication\n"
837 " default use 'magic' if possible, "
838 " otherwise fall back to 'basic'\n"
839 " none unauthenticated HTTP (if"
840 " the data file is non-confidential)\n");
843 fprintf(stderr
, "%s: unrecognised authentication"
844 " type '%s'\n%*s options are 'magic',"
845 " 'basic', 'none', 'default'\n",
846 PNAME
, optval
, (int)strlen(PNAME
), "");
857 int authlen
, authsize
;
860 if (optid
== OPT_AUTHFILE
) {
861 fd
= open(optval
, O_RDONLY
);
863 fprintf(stderr
, "%s: %s: open: %s\n", PNAME
,
864 optval
, strerror(errno
));
871 sprintf(namebuf
, "fd %d", fd
);
876 authbuf
= snewn(authsize
, char);
877 while ((ret
= read(fd
, authbuf
+authlen
,
878 authsize
-authlen
)) > 0) {
880 if ((authsize
- authlen
) < (authsize
/ 16)) {
881 authsize
= authlen
* 3 / 2 + 4096;
882 authbuf
= sresize(authbuf
, authsize
, char);
886 fprintf(stderr
, "%s: %s: read: %s\n", PNAME
,
887 name
, strerror(errno
));
890 if (optid
== OPT_AUTHFILE
)
892 httpauthdata
= authbuf
;
896 case OPT_INCLUDEPATH
:
898 case OPT_EXCLUDEPATH
:
901 if (ninex
>= inexsize
) {
902 inexsize
= ninex
* 3 / 2 + 16;
903 inex
= sresize(inex
, inexsize
,
904 struct inclusion_exclusion
);
906 inex
[ninex
].path
= (optid
== OPT_INCLUDEPATH
||
907 optid
== OPT_EXCLUDEPATH
||
908 optid
== OPT_PRUNEPATH
);
909 inex
[ninex
].type
= (optid
== OPT_INCLUDE ?
1 :
910 optid
== OPT_INCLUDEPATH ?
1 :
911 optid
== OPT_EXCLUDE ?
0 :
912 optid
== OPT_EXCLUDEPATH ?
0 :
913 optid
== OPT_PRUNE ?
-1 :
914 /* optid == OPT_PRUNEPATH ? */ -1);
915 inex
[ninex
].wildcard
= optval
;
921 fprintf(stderr
, "%s: unexpected argument '%s'\n", PNAME
, p
);
931 for (action
= 0; action
< nactions
; action
++) {
932 int mode
= actions
[action
].mode
;
934 if (mode
== SCAN
|| mode
== SCANDUMP
|| mode
== LOAD
) {
935 const char *scandir
= actions
[action
].arg
;
938 char *buf
= fgetline(stdin
);
940 buf
[strcspn(buf
, "\r\n")] = '\0';
941 if (1 != sscanf(buf
, DUMPHDR
"%x",
943 fprintf(stderr
, "%s: header in dump file not recognised\n",
947 pathsep
= (char)newpathsep
;
951 if (mode
== SCAN
|| mode
== LOAD
) {
953 * Prepare to write out the index file.
955 fd
= open(filename
, O_RDWR
| O_TRUNC
| O_CREAT
,
958 fprintf(stderr
, "%s: %s: open: %s\n", PNAME
, filename
,
962 if (fstat(fd
, &st
) < 0) {
963 perror(PNAME
": fstat");
966 ctx
->datafile_dev
= st
.st_dev
;
967 ctx
->datafile_ino
= st
.st_ino
;
968 ctx
->straight_to_dump
= 0;
970 ctx
->datafile_dev
= -1;
971 ctx
->datafile_ino
= -1;
972 ctx
->straight_to_dump
= 1;
975 if (mode
== SCAN
|| mode
== SCANDUMP
) {
976 if (stat(scandir
, &st
) < 0) {
977 fprintf(stderr
, "%s: %s: stat: %s\n", PNAME
, scandir
,
981 ctx
->filesystem_dev
= crossfs ?
0 : st
.st_dev
;
986 ctx
->crossfs
= crossfs
;
987 ctx
->fakeatimes
= fakediratimes
;
988 ctx
->usemtime
= mtime
;
990 ctx
->last_output_update
= time(NULL
);
992 /* progress==1 means report progress only if stderr is a tty */
994 progress
= isatty(2) ?
2 : 0;
995 ctx
->progress
= progress
;
999 ioctl(2, TIOCGWINSZ
, &ws
) == 0 &&
1001 ctx
->progwidth
= ws
.ws_col
- 1;
1003 ctx
->progwidth
= 79;
1006 if (mode
== SCANDUMP
)
1007 printf(DUMPHDR
"%02x\n", (unsigned char)pathsep
);
1010 * Scan the directory tree, and write out the trie component
1013 if (mode
!= SCANDUMP
) {
1014 ctx
->tb
= triebuild_new(fd
);
1019 while ((buf
= fgetline(stdin
)) != NULL
) {
1020 struct trie_file tf
;
1023 buf
[strcspn(buf
, "\r\n")] = '\0';
1027 while (*p
&& *p
!= ' ') p
++;
1029 fprintf(stderr
, "%s: dump file line %d: expected at least"
1030 " three fields\n", PNAME
, line
);
1034 tf
.size
= strtoull(q
, NULL
, 10);
1036 while (*p
&& *p
!= ' ') p
++;
1038 fprintf(stderr
, "%s: dump file line %d: expected at least"
1039 " three fields\n", PNAME
, line
);
1043 tf
.atime
= strtoull(q
, NULL
, 10);
1051 for (i
= 0; i
< 2; i
++) {
1053 if (*p
>= '0' && *p
<= '9')
1055 else if (*p
>= 'A' && *p
<= 'F')
1056 c
+= *p
- ('A' - 10);
1057 else if (*p
>= 'a' && *p
<= 'f')
1058 c
+= *p
- ('a' - 10);
1060 fprintf(stderr
, "%s: dump file line %d: unable"
1061 " to parse hex escape\n", PNAME
, line
);
1071 triebuild_add(ctx
->tb
, buf
, &tf
);
1076 du(scandir
, gotdata
, scan_error
, ctx
);
1078 if (mode
!= SCANDUMP
) {
1081 char *buf
, *prevbuf
;
1083 count
= triebuild_finish(ctx
->tb
);
1084 triebuild_free(ctx
->tb
);
1086 if (ctx
->progress
) {
1087 fprintf(stderr
, "%-*s\r", ctx
->progwidth
, "");
1092 * Work out how much space the cumulative index trees
1093 * will take; enlarge the file, and memory-map it.
1095 if (fstat(fd
, &st
) < 0) {
1096 perror(PNAME
": fstat");
1100 printf("Built pathname index, %d entries,"
1101 " %llu bytes of index\n", count
,
1102 (unsigned long long)st
.st_size
);
1104 totalsize
= index_initial_size(st
.st_size
, count
);
1105 totalsize
+= totalsize
/ 10;
1107 if (lseek(fd
, totalsize
-1, SEEK_SET
) < 0) {
1108 perror(PNAME
": lseek");
1111 if (write(fd
, "\0", 1) < 1) {
1112 perror(PNAME
": write");
1116 mappedfile
= mmap(NULL
, totalsize
, PROT_READ
|PROT_WRITE
,MAP_SHARED
, fd
, 0);
1118 perror(PNAME
": mmap");
1122 if (fakediratimes
) {
1123 printf("Faking directory atimes\n");
1124 trie_fake_dir_atimes(mappedfile
);
1127 printf("Building index\n");
1128 ib
= indexbuild_new(mappedfile
, st
.st_size
, count
, &delta
);
1129 maxpathlen
= trie_maxpathlen(mappedfile
);
1130 buf
= snewn(maxpathlen
, char);
1131 prevbuf
= snewn(maxpathlen
, char);
1132 tw
= triewalk_new(mappedfile
);
1134 tf
= triewalk_next(tw
, buf
);
1139 if (totalsize
- indexbuild_realsize(ib
) < delta
) {
1140 const void *oldfile
= mappedfile
;
1144 * Unmap the file, grow it, and remap it.
1146 munmap(mappedfile
, totalsize
);
1149 totalsize
+= totalsize
/ 10;
1151 if (lseek(fd
, totalsize
-1, SEEK_SET
) < 0) {
1152 perror(PNAME
": lseek");
1155 if (write(fd
, "\0", 1) < 1) {
1156 perror(PNAME
": write");
1160 mappedfile
= mmap(NULL
, totalsize
, PROT_READ
|PROT_WRITE
,MAP_SHARED
, fd
, 0);
1162 perror(PNAME
": mmap");
1166 indexbuild_rebase(ib
, mappedfile
);
1167 triewalk_rebase(tw
, mappedfile
);
1168 diff
= (const unsigned char *)mappedfile
-
1169 (const unsigned char *)oldfile
;
1171 prevtf
= (const struct trie_file
*)
1172 (((const unsigned char *)prevtf
) + diff
);
1174 tf
= (const struct trie_file
*)
1175 (((const unsigned char *)tf
) + diff
);
1179 * Get the next file from the index. So we are
1180 * currently holding, and have not yet
1181 * indexed, prevtf (with pathname prevbuf) and
1182 * tf (with pathname buf).
1185 memcpy(prevbuf
, buf
, maxpathlen
);
1186 tf
= triewalk_next(tw
, buf
);
1192 * Find the first differing character position
1193 * between our two pathnames.
1195 for (i
= 0; prevbuf
[i
] && prevbuf
[i
] == buf
[i
]; i
++);
1198 * If prevbuf was a directory name and buf is
1199 * something inside that directory, then
1200 * trie_before() will be called on prevbuf
1201 * itself. Hence we must drop a tag before it,
1202 * so that the resulting index is usable.
1204 if ((!prevbuf
[i
] && (buf
[i
] == pathsep
||
1205 (i
> 0 && buf
[i
-1] == pathsep
))))
1209 * Add prevtf to the index.
1211 indexbuild_add(ib
, prevtf
);
1215 * Drop an unconditional final tag, and
1216 * get out of this loop.
1223 * If prevbuf was a filename inside some
1224 * directory which buf is outside, then
1225 * trie_before() will be called on some
1226 * pathname either equal to buf or epsilon
1227 * less than it. Either way, we're going to
1228 * need to drop a tag after prevtf.
1230 if (strchr(prevbuf
+i
, pathsep
) || !tf
)
1235 realsize
= indexbuild_realsize(ib
);
1236 indexbuild_free(ib
);
1238 munmap(mappedfile
, totalsize
);
1239 ftruncate(fd
, realsize
);
1241 printf("Final index file size = %llu bytes\n",
1242 (unsigned long long)realsize
);
1244 } else if (mode
== TEXT
) {
1245 char *querydir
= actions
[action
].arg
;
1248 fd
= open(filename
, O_RDONLY
);
1250 fprintf(stderr
, "%s: %s: open: %s\n", PNAME
, filename
,
1254 if (fstat(fd
, &st
) < 0) {
1255 perror(PNAME
": fstat");
1258 totalsize
= st
.st_size
;
1259 mappedfile
= mmap(NULL
, totalsize
, PROT_READ
, MAP_SHARED
, fd
, 0);
1261 perror(PNAME
": mmap");
1264 pathsep
= trie_pathsep(mappedfile
);
1267 * Trim trailing slash, just in case.
1269 pathlen
= strlen(querydir
);
1270 if (pathlen
> 0 && querydir
[pathlen
-1] == pathsep
)
1271 querydir
[--pathlen
] = '\0';
1274 depth
= 1; /* default for text mode */
1275 if (outfile
!= NULL
) {
1276 FILE *fp
= fopen(outfile
, "w");
1278 fprintf(stderr
, "%s: %s: open: %s\n", PNAME
,
1279 outfile
, strerror(errno
));
1282 text_query(mappedfile
, querydir
, textcutoff
, showfiles
,
1286 text_query(mappedfile
, querydir
, textcutoff
, showfiles
,
1290 munmap(mappedfile
, totalsize
);
1291 } else if (mode
== HTML
) {
1292 char *querydir
= actions
[action
].arg
;
1293 size_t pathlen
, maxpathlen
;
1295 struct html_config cfg
;
1299 fd
= open(filename
, O_RDONLY
);
1301 fprintf(stderr
, "%s: %s: open: %s\n", PNAME
, filename
,
1304 printf("Status: 500\nContent-type: text/html\n\n"
1306 "<title>500 Internal Server Error</title>"
1308 "<h1>500 Internal Server Error</h1>"
1309 "<p><code>agedu</code> suffered an internal error."
1310 "</body></html>\n");
1315 if (fstat(fd
, &st
) < 0) {
1316 fprintf(stderr
, "%s: %s: fstat: %s\n", PNAME
, filename
,
1319 printf("Status: 500\nContent-type: text/html\n\n"
1321 "<title>500 Internal Server Error</title>"
1323 "<h1>500 Internal Server Error</h1>"
1324 "<p><code>agedu</code> suffered an internal error."
1325 "</body></html>\n");
1330 totalsize
= st
.st_size
;
1331 mappedfile
= mmap(NULL
, totalsize
, PROT_READ
, MAP_SHARED
, fd
, 0);
1333 fprintf(stderr
, "%s: %s: mmap: %s\n", PNAME
, filename
,
1336 printf("Status: 500\nContent-type: text/html\n\n"
1338 "<title>500 Internal Server Error</title>"
1340 "<h1>500 Internal Server Error</h1>"
1341 "<p><code>agedu</code> suffered an internal error."
1342 "</body></html>\n");
1347 pathsep
= trie_pathsep(mappedfile
);
1349 maxpathlen
= trie_maxpathlen(mappedfile
);
1350 pathbuf
= snewn(maxpathlen
, char);
1354 * If we're run in --cgi mode, read PATH_INFO to get
1355 * a numeric pathname index.
1357 char *path_info
= getenv("PATH_INFO");
1363 * Because we need relative links to go to the
1364 * right place, it's important that our
1365 * PATH_INFO should contain a slash right at the
1366 * start, and no slashes anywhere else.
1368 if (path_info
[0] != '/') {
1369 char *servername
= getenv("SERVER_NAME");
1370 char *scriptname
= getenv("SCRIPT_NAME");
1371 if (!servername
|| !scriptname
) {
1373 fprintf(stderr
, "%s: SCRIPT_NAME unset\n", PNAME
);
1374 else if (scriptname
)
1375 fprintf(stderr
, "%s: SCRIPT_NAME unset\n", PNAME
);
1377 fprintf(stderr
, "%s: SERVER_NAME and "
1378 "SCRIPT_NAME both unset\n", PNAME
);
1379 printf("Status: 500\nContent-type: text/html\n\n"
1381 "<title>500 Internal Server Error</title>"
1383 "<h1>500 Internal Server Error</h1>"
1384 "<p><code>agedu</code> suffered an internal "
1386 "</body></html>\n");
1389 printf("Status: 301\n"
1390 "Location: http://%s/%s/\n"
1391 "Content-type: text/html\n\n"
1393 "<title>301 Moved</title>"
1395 "<h1>301 Moved</h1>"
1398 servername
, scriptname
);
1400 } else if (strchr(path_info
+1, '/')) {
1401 printf("Status: 404\nContent-type: text/html\n\n"
1403 "<title>404 Not Found</title>"
1405 "<h1>400 Not Found</h1>"
1406 "<p>Invalid <code>agedu</code> pathname."
1407 "</body></html>\n");
1410 xi
= atoi(path_info
+ 1);
1412 if (xi
>= trie_count(mappedfile
)) {
1413 printf("Status: 404\nContent-type: text/html\n\n"
1415 "<title>404 Not Found</title>"
1417 "<h1>400 Not Found</h1>"
1418 "<p>This is not a valid pathname index."
1419 "</body></html>\n");
1421 } else if (!index_has_root(mappedfile
, xi
)) {
1422 printf("Status: 404\nContent-type: text/html\n\n"
1424 "<title>404 Not Found</title>"
1426 "<h1>404 Not Found</h1>"
1427 "<p>Pathname index out of range."
1428 "</body></html>\n");
1433 * In ordinary --html mode, process a query
1434 * directory passed in on the command line.
1438 * Trim trailing slash, just in case.
1440 pathlen
= strlen(querydir
);
1441 if (pathlen
> 0 && querydir
[pathlen
-1] == pathsep
)
1442 querydir
[--pathlen
] = '\0';
1444 xi
= trie_before(mappedfile
, querydir
);
1445 if (xi
>= trie_count(mappedfile
) ||
1446 (trie_getpath(mappedfile
, xi
, pathbuf
),
1447 strcmp(pathbuf
, querydir
))) {
1448 fprintf(stderr
, "%s: pathname '%s' does not exist in index\n"
1449 "%*s(check it is spelled exactly as it is in the "
1450 "index, including\n%*sany leading './')\n",
1452 (int)(1+sizeof(PNAME
)), "",
1453 (int)(1+sizeof(PNAME
)), "");
1455 } else if (!index_has_root(mappedfile
, xi
)) {
1456 fprintf(stderr
, "%s: pathname '%s' is"
1457 " a file, not a directory\n", PNAME
, querydir
);
1462 if (!querydir
|| !gotdepth
) {
1464 * Single output file.
1467 cfg
.format
= "%.0lu"; /* use crosslinks in --cgi mode */
1471 cfg
.rootpage
= NULL
;
1472 cfg
.autoage
= htmlautoagerange
;
1473 cfg
.oldest
= htmloldest
;
1474 cfg
.newest
= htmlnewest
;
1475 cfg
.showfiles
= showfiles
;
1476 html
= html_query(mappedfile
, xi
, &cfg
, 1);
1477 if (querydir
&& outfile
!= NULL
) {
1478 FILE *fp
= fopen(outfile
, "w");
1480 fprintf(stderr
, "%s: %s: open: %s\n", PNAME
,
1481 outfile
, strerror(errno
));
1483 } else if (fputs(html
, fp
) < 0) {
1484 fprintf(stderr
, "%s: %s: write: %s\n", PNAME
,
1485 outfile
, strerror(errno
));
1488 } else if (fclose(fp
) < 0) {
1489 fprintf(stderr
, "%s: %s: fclose: %s\n", PNAME
,
1490 outfile
, strerror(errno
));
1495 printf("Content-type: text/html\n\n");
1497 fputs(html
, stdout
);
1501 * Multiple output files.
1503 int dirlen
= outfile ?
2+strlen(outfile
) : 3;
1504 char prefix
[dirlen
];
1506 if (mkdir(outfile
, 0777) < 0 && errno
!= EEXIST
) {
1507 fprintf(stderr
, "%s: %s: mkdir: %s\n", PNAME
,
1508 outfile
, strerror(errno
));
1511 snprintf(prefix
, dirlen
, "%s/", outfile
);
1513 snprintf(prefix
, dirlen
, "./");
1517 * pathbuf is only set up in the plain-HTML case and
1518 * not in the CGI case; but that's OK, because the
1519 * CGI case can't come to this branch of the if
1522 make_successor(pathbuf
);
1523 xi2
= trie_before(mappedfile
, pathbuf
);
1525 cfg
.format
= "%lu.html";
1526 cfg
.rootpage
= "index.html";
1527 cfg
.autoage
= htmlautoagerange
;
1528 cfg
.oldest
= htmloldest
;
1529 cfg
.newest
= htmlnewest
;
1530 cfg
.showfiles
= showfiles
;
1531 if (html_dump(mappedfile
, xi
, xi2
, depth
, &cfg
, prefix
))
1535 munmap(mappedfile
, totalsize
);
1537 } else if (mode
== DUMP
) {
1541 fd
= open(filename
, O_RDONLY
);
1543 fprintf(stderr
, "%s: %s: open: %s\n", PNAME
, filename
,
1547 if (fstat(fd
, &st
) < 0) {
1548 perror(PNAME
": fstat");
1551 totalsize
= st
.st_size
;
1552 mappedfile
= mmap(NULL
, totalsize
, PROT_READ
, MAP_SHARED
, fd
, 0);
1554 perror(PNAME
": mmap");
1557 pathsep
= trie_pathsep(mappedfile
);
1559 maxpathlen
= trie_maxpathlen(mappedfile
);
1560 buf
= snewn(maxpathlen
, char);
1562 printf(DUMPHDR
"%02x\n", (unsigned char)pathsep
);
1563 tw
= triewalk_new(mappedfile
);
1564 while ((tf
= triewalk_next(tw
, buf
)) != NULL
)
1568 munmap(mappedfile
, totalsize
);
1569 } else if (mode
== HTTPD
) {
1570 struct html_config pcfg
;
1571 struct httpd_config dcfg
;
1573 fd
= open(filename
, O_RDONLY
);
1575 fprintf(stderr
, "%s: %s: open: %s\n", PNAME
, filename
,
1579 if (fstat(fd
, &st
) < 0) {
1580 perror(PNAME
": fstat");
1583 totalsize
= st
.st_size
;
1584 mappedfile
= mmap(NULL
, totalsize
, PROT_READ
, MAP_SHARED
, fd
, 0);
1586 perror(PNAME
": mmap");
1589 pathsep
= trie_pathsep(mappedfile
);
1591 dcfg
.address
= httpserveraddr
;
1592 dcfg
.port
= httpserverport
;
1593 dcfg
.basicauthdata
= httpauthdata
;
1595 pcfg
.rootpage
= NULL
;
1596 pcfg
.autoage
= htmlautoagerange
;
1597 pcfg
.oldest
= htmloldest
;
1598 pcfg
.newest
= htmlnewest
;
1599 pcfg
.showfiles
= showfiles
;
1600 run_httpd(mappedfile
, auth
, &dcfg
, &pcfg
);
1601 munmap(mappedfile
, totalsize
);
1602 } else if (mode
== REMOVE
) {
1603 if (remove(filename
) < 0) {
1604 fprintf(stderr
, "%s: %s: remove: %s\n", PNAME
, filename
,