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 printf("%llu %llu ", tf
->size
, tf
->atime
);
60 for (p
= pathname
; *p
; p
++) {
61 if (*p
>= ' ' && *p
< 127 && *p
!= '%')
64 printf("%%%02x", (unsigned char)*p
);
69 static int gotdata(void *vctx
, const char *pathname
, const STRUCT_STAT
*st
)
71 struct ctx
*ctx
= (struct ctx
*)vctx
;
72 struct trie_file file
;
78 * Filter out our own data file.
80 if (st
->st_dev
== ctx
->datafile_dev
&& st
->st_ino
== ctx
->datafile_ino
)
84 * Don't cross the streams^W^Wany file system boundary.
86 if (!ctx
->crossfs
&& st
->st_dev
!= ctx
->filesystem_dev
)
89 file
.size
= (unsigned long long)512 * st
->st_blocks
;
90 if (ctx
->usemtime
|| (ctx
->fakeatimes
&& S_ISDIR(st
->st_mode
)))
91 file
.atime
= st
->st_mtime
;
93 file
.atime
= st
->st_atime
;
96 * Filter based on wildcards.
99 filename
= strrchr(pathname
, pathsep
);
104 for (i
= 0; i
< ctx
->ninex
; i
++) {
105 if (fnmatch(ctx
->inex
[i
].wildcard
,
106 ctx
->inex
[i
].path ? pathname
: filename
, 0) == 0)
107 include
= ctx
->inex
[i
].type
;
110 return 0; /* ignore this entry and any subdirs */
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.
119 if (!S_ISDIR(st
->st_mode
))
120 return 0; /* just ignore */
125 if (ctx
->straight_to_dump
)
126 dump_line(pathname
, &file
);
128 triebuild_add(ctx
->tb
, pathname
, &file
);
132 if (t
!= ctx
->last_output_update
) {
133 fprintf(stderr
, "%-*.*s\r", ctx
->progwidth
, ctx
->progwidth
,
136 ctx
->last_output_update
= t
;
143 static void scan_error(void *vctx
, const char *fmt
, ...)
145 struct ctx
*ctx
= (struct ctx
*)vctx
;
149 fprintf(stderr
, "%-*s\r", ctx
->progwidth
, "");
153 fprintf(stderr
, "%s: ", PNAME
);
155 vfprintf(stderr
, fmt
, ap
);
158 ctx
->last_output_update
--; /* force a progress report next time */
161 static void text_query(const void *mappedfile
, const char *querydir
,
166 unsigned long xi1
, xi2
;
167 unsigned long long s1
, s2
;
169 maxpathlen
= trie_maxpathlen(mappedfile
);
170 pathbuf
= snewn(maxpathlen
+ 1, char);
173 * We want to query everything between the supplied filename
174 * (inclusive) and that filename with a ^A on the end
175 * (exclusive). So find the x indices for each.
177 strcpy(pathbuf
, querydir
);
178 make_successor(pathbuf
);
179 xi1
= trie_before(mappedfile
, querydir
);
180 xi2
= trie_before(mappedfile
, pathbuf
);
183 return; /* file, or empty dir => no display */
186 * Now do the lookups in the age index.
188 s1
= index_query(mappedfile
, xi1
, t
);
189 s2
= index_query(mappedfile
, xi2
, t
);
192 return; /* no space taken up => no display */
196 * Now scan for first-level subdirectories and report
201 trie_getpath(mappedfile
, xi1
, pathbuf
);
202 text_query(mappedfile
, pathbuf
, t
, depth
-1);
203 make_successor(pathbuf
);
204 xi1
= trie_before(mappedfile
, pathbuf
);
208 /* Display in units of 1Kb */
209 printf("%-11llu %s\n", (s2
- s1
) / 1024, querydir
);
213 * Largely frivolous way to define all my command-line options. I
214 * present here a parametric macro which declares a series of
215 * _logical_ option identifiers, and for each one declares zero or
216 * more short option characters and zero or more long option
217 * words. Then I repeatedly invoke that macro with its arguments
218 * defined to be various other macros, which allows me to
221 * - define an enum allocating a distinct integer value to each
223 * - define a string consisting of precisely all the short option
225 * - define a string array consisting of all the long option
227 * - define (with help from auxiliary enums) integer arrays
228 * parallel to both of the above giving the logical option id
229 * for each physical short and long option
230 * - define an array indexed by logical option id indicating
231 * whether the option in question takes a value
232 * - define a function which prints out brief online help for all
235 * It's not at all clear to me that this trickery is actually
236 * particularly _efficient_ - it still, after all, requires going
237 * linearly through the option list at run time and doing a
238 * strcmp, whereas in an ideal world I'd have liked the lists of
239 * long and short options to be pre-sorted so that a binary search
240 * or some other more efficient lookup was possible. (Not that
241 * asymptotic algorithmic complexity is remotely vital in option
242 * parsing, but if I were doing this in, say, Lisp or something
243 * with an equivalently powerful preprocessor then once I'd had
244 * the idea of preparing the option-parsing data structures at
245 * compile time I would probably have made the effort to prepare
246 * them _properly_. I could have Perl generate me a source file
247 * from some sort of description, I suppose, but that would seem
248 * like overkill. And in any case, it's more of a challenge to
249 * achieve as much as possible by cunning use of cpp and enum than
250 * to just write some sensible and logical code in a Turing-
251 * complete language. I said it was largely frivolous :-)
253 * This approach does have the virtue that it brings together the
254 * option ids, option spellings and help text into a single
255 * combined list and defines them all in exactly one place. If I
256 * want to add a new option, or a new spelling for an option, I
257 * only have to modify the main OPTHELP macro below and then add
258 * code to process the new logical id.
260 * (Though, really, even that isn't ideal, since it still involves
261 * modifying the source file in more than one place. In a
262 * _properly_ ideal world, I'd be able to interleave the option
263 * definitions with the code fragments that process them. And then
264 * not bother defining logical identifiers for them at all - those
265 * would be automatically generated, since I wouldn't have any
266 * need to specify them manually in another part of the code.)
268 * One other helpful consequence of the enum-based structure here
269 * is that it causes a compiler error if I accidentally try to
270 * define the same option (short or long) twice.
273 #define OPTHELP(NOVAL, VAL, SHORT, LONG, HELPPFX, HELPARG, HELPLINE, HELPOPT) \
274 HELPPFX("usage") HELPLINE(PNAME " [options] action [action...]") \
276 VAL(SCAN) SHORT(s) LONG(scan) \
277 HELPARG("directory") HELPOPT("scan and index a directory") \
278 NOVAL(HTTPD) SHORT(w) LONG(web) LONG(server) LONG(httpd) \
279 HELPOPT("serve HTML reports from a temporary web server") \
280 VAL(TEXT) SHORT(t) LONG(text) \
281 HELPARG("subdir") HELPOPT("print a plain text report on a subdirectory") \
282 NOVAL(REMOVE) SHORT(R) LONG(remove) LONG(delete) LONG(unlink) \
283 HELPOPT("remove the index file") \
284 NOVAL(DUMP) SHORT(D) LONG(dump) HELPOPT("dump the index file on stdout") \
285 NOVAL(LOAD) SHORT(L) LONG(load) \
286 HELPOPT("load and index a dump file") \
287 VAL(SCANDUMP) SHORT(S) LONG(scan_dump) \
288 HELPARG("directory") HELPOPT("scan only, generating a dump") \
289 VAL(HTML) SHORT(H) LONG(html) \
290 HELPARG("subdir") HELPOPT("print an HTML report on a subdirectory") \
292 VAL(DATAFILE) SHORT(f) LONG(file) \
293 HELPARG("filename") HELPOPT("[most modes] specify index file") \
294 NOVAL(CROSSFS) LONG(cross_fs) \
295 HELPOPT("[--scan] cross filesystem boundaries") \
296 NOVAL(NOCROSSFS) LONG(no_cross_fs) \
297 HELPOPT("[--scan] stick to one filesystem") \
298 VAL(PRUNE) LONG(prune) \
299 HELPARG("wildcard") HELPOPT("[--scan] prune files matching pattern") \
300 VAL(PRUNEPATH) LONG(prune_path) \
301 HELPARG("wildcard") HELPOPT("[--scan] prune pathnames matching pattern") \
302 VAL(EXCLUDE) LONG(exclude) \
303 HELPARG("wildcard") HELPOPT("[--scan] exclude files matching pattern") \
304 VAL(EXCLUDEPATH) LONG(exclude_path) \
305 HELPARG("wildcard") HELPOPT("[--scan] exclude pathnames matching pattern") \
306 VAL(INCLUDE) LONG(include) \
307 HELPARG("wildcard") HELPOPT("[--scan] include files matching pattern") \
308 VAL(INCLUDEPATH) LONG(include_path) \
309 HELPARG("wildcard") HELPOPT("[--scan] include pathnames matching pattern") \
310 NOVAL(PROGRESS) LONG(progress) LONG(scan_progress) \
311 HELPOPT("[--scan] report progress on stderr") \
312 NOVAL(NOPROGRESS) LONG(no_progress) LONG(no_scan_progress) \
313 HELPOPT("[--scan] do not report progress") \
314 NOVAL(TTYPROGRESS) LONG(tty_progress) LONG(tty_scan_progress) \
315 LONG(progress_tty) LONG(scan_progress_tty) \
316 HELPOPT("[--scan] report progress if stderr is a tty") \
317 NOVAL(DIRATIME) LONG(dir_atime) LONG(dir_atimes) \
318 HELPOPT("[--scan,--load] keep real atimes on directories") \
319 NOVAL(NODIRATIME) LONG(no_dir_atime) LONG(no_dir_atimes) \
320 HELPOPT("[--scan,--load] fake atimes on directories") \
321 NOVAL(MTIME) LONG(mtime) \
322 HELPOPT("[--scan] use mtime instead of atime") \
323 NOVAL(FULL) LONG(full_index) \
324 HELPOPT("[--scan] index every file individually") \
325 VAL(AGERANGE) SHORT(r) LONG(age_range) LONG(range) LONG(ages) \
326 HELPARG("age[-age]") HELPOPT("[--web,--html] set limits of colour coding") \
327 VAL(SERVERADDR) LONG(address) LONG(addr) LONG(server_address) \
329 HELPARG("addr[:port]") HELPOPT("[--web] specify HTTP server address") \
330 VAL(AUTH) LONG(auth) LONG(http_auth) LONG(httpd_auth) \
331 LONG(server_auth) LONG(web_auth) \
332 HELPARG("type") HELPOPT("[--web] specify HTTP authentication method") \
333 VAL(AUTHFILE) LONG(auth_file) \
334 HELPARG("filename") HELPOPT("[--web] read HTTP Basic user/pass from file") \
335 VAL(AUTHFD) LONG(auth_fd) \
336 HELPARG("fd") HELPOPT("[--web] read HTTP Basic user/pass from fd") \
337 VAL(TQDEPTH) SHORT(d) LONG(depth) LONG(max_depth) LONG(maximum_depth) \
338 HELPARG("levels") HELPOPT("[--text] recurse to this many levels") \
339 VAL(MINAGE) SHORT(a) LONG(age) LONG(min_age) LONG(minimum_age) \
340 HELPARG("age") HELPOPT("[--text] include only files older than this") \
342 NOVAL(HELP) SHORT(h) LONG(help) HELPOPT("display this help text") \
343 NOVAL(VERSION) SHORT(V) LONG(version) HELPOPT("report version number") \
344 NOVAL(LICENCE) LONG(licence) LONG(license) \
345 HELPOPT("display (MIT) licence text") \
348 #define DEFENUM(x) OPT_ ## x,
351 #define STRING(x) #x ,
352 #define STRINGNOCOMMA(x) #x
353 #define SHORTNEWOPT(x) SHORTtmp_ ## x = OPT_ ## x,
354 #define SHORTTHISOPT(x) SHORTtmp2_ ## x, SHORTVAL_ ## x = SHORTtmp2_ ## x - 1,
355 #define SHORTOPTVAL(x) SHORTVAL_ ## x,
356 #define SHORTTMP(x) SHORTtmp3_ ## x,
357 #define LONGNEWOPT(x) LONGtmp_ ## x = OPT_ ## x,
358 #define LONGTHISOPT(x) LONGtmp2_ ## x, LONGVAL_ ## x = LONGtmp2_ ## x - 1,
359 #define LONGOPTVAL(x) LONGVAL_ ## x,
360 #define LONGTMP(x) SHORTtmp3_ ## x,
362 #define OPTIONS(NOVAL, VAL, SHORT, LONG) \
363 OPTHELP(NOVAL, VAL, SHORT, LONG, IGNORE, IGNORE, IGNORE, IGNORE)
365 enum { OPTIONS(DEFENUM
,DEFENUM
,IGNORE
,IGNORE
) NOPTIONS
};
366 enum { OPTIONS(IGNORE
,IGNORE
,SHORTTMP
,IGNORE
) NSHORTOPTS
};
367 enum { OPTIONS(IGNORE
,IGNORE
,IGNORE
,LONGTMP
) NLONGOPTS
};
368 static const int opthasval
[NOPTIONS
] = {OPTIONS(ZERO
,ONE
,IGNORE
,IGNORE
)};
369 static const char shortopts
[] = {OPTIONS(IGNORE
,IGNORE
,STRINGNOCOMMA
,IGNORE
)};
370 static const char *const longopts
[] = {OPTIONS(IGNORE
,IGNORE
,IGNORE
,STRING
)};
371 enum { OPTIONS(SHORTNEWOPT
,SHORTNEWOPT
,SHORTTHISOPT
,IGNORE
) };
372 enum { OPTIONS(LONGNEWOPT
,LONGNEWOPT
,IGNORE
,LONGTHISOPT
) };
373 static const int shortvals
[] = {OPTIONS(IGNORE
,IGNORE
,SHORTOPTVAL
,IGNORE
)};
374 static const int longvals
[] = {OPTIONS(IGNORE
,IGNORE
,IGNORE
,LONGOPTVAL
)};
376 static void usage(FILE *fp
)
379 const char *prefix
, *shortopt
, *longopt
, *optarg
;
382 #define HELPRESET prefix = shortopt = longopt = optarg = NULL, optex = -1
383 #define HELPNOVAL(s) optex = 0;
384 #define HELPVAL(s) optex = 1;
385 #define HELPSHORT(s) if (!shortopt) shortopt = "-" #s;
386 #define HELPLONG(s) if (!longopt) { \
387 strcpy(longbuf, "--" #s); longopt = longbuf; \
388 for (i = 0; longbuf[i]; i++) if (longbuf[i] == '_') longbuf[i] = '-'; }
389 #define HELPPFX(s) prefix = s;
390 #define HELPARG(s) optarg = s;
391 #define HELPLINE(s) assert(optex == -1); \
392 fprintf(fp, "%7s%c %s\n", prefix?prefix:"", prefix?':':' ', s); \
394 #define HELPOPT(s) assert((optex == 1 && optarg) || (optex == 0 && !optarg)); \
395 assert(shortopt || longopt); \
396 i = fprintf(fp, "%7s%c %s%s%s%s%s", prefix?prefix:"", prefix?':':' ', \
397 shortopt?shortopt:"", shortopt&&longopt?", ":"", longopt?longopt:"", \
398 optarg?" ":"", optarg?optarg:""); \
399 fprintf(fp, "%*s %s\n", i<32?32-i:0,"",s); HELPRESET;
402 OPTHELP(HELPNOVAL
, HELPVAL
, HELPSHORT
, HELPLONG
,
403 HELPPFX
, HELPARG
, HELPLINE
, HELPOPT
);
416 static time_t parse_age(time_t now
, const char *agestr
)
425 if (2 != sscanf(agestr
, "%d%1[DdWwMmYy]", &nunits
, unit
)) {
426 fprintf(stderr
, "%s: age specification should be a number followed by"
427 " one of d,w,m,y\n", PNAME
);
431 if (unit
[0] == 'd') {
433 } else if (unit
[0] == 'w') {
434 t
-= 86400 * 7 * nunits
;
439 ym
= tm
.tm_year
* 12 + tm
.tm_mon
;
446 tm
.tm_year
= ym
/ 12;
455 int main(int argc
, char **argv
)
458 struct ctx actx
, *ctx
= &actx
;
460 off_t totalsize
, realsize
;
464 const struct trie_file
*tf
, *prevtf
;
465 char *filename
= PNAME
".dat";
467 enum { TEXT
, HTML
, SCAN
, DUMP
, SCANDUMP
, LOAD
, HTTPD
, REMOVE
};
472 int nactions
= 0, actionsize
= 0, action
;
473 time_t now
= time(NULL
);
474 time_t textcutoff
= now
, htmlnewest
= now
, htmloldest
= now
;
475 int htmlautoagerange
= 1;
476 const char *httpserveraddr
= NULL
;
477 int httpserverport
= 0;
478 const char *httpauthdata
= NULL
;
479 int auth
= HTTPD_AUTH_MAGIC
| HTTPD_AUTH_BASIC
;
481 struct inclusion_exclusion
*inex
= NULL
;
482 int ninex
= 0, inexsize
= 0;
485 int fakediratimes
= 1;
489 #ifdef DEBUG_MAD_OPTION_PARSING_MACROS
491 static const char *const optnames
[NOPTIONS
] = {
492 OPTIONS(STRING
,STRING
,IGNORE
,IGNORE
)
495 for (i
= 0; i
< NSHORTOPTS
; i
++)
496 printf("-%c == %s [%s]\n", shortopts
[i
], optnames
[shortvals
[i
]],
497 opthasval
[shortvals
[i
]] ?
"value" : "no value");
498 for (i
= 0; i
< NLONGOPTS
; i
++)
499 printf("--%s == %s [%s]\n", longopts
[i
], optnames
[longvals
[i
]],
500 opthasval
[longvals
[i
]] ?
"value" : "no value");
507 if (doing_opts
&& *p
== '-') {
510 if (!strcmp(p
, "--")) {
521 if (wordstart
&& *p
== '-') {
523 * GNU-style long option.
526 optval
= strchr(p
, '=');
530 for (i
= 0; i
< NLONGOPTS
; i
++) {
531 const char *opt
= longopts
[i
], *s
= p
;
534 * The underscores in the option names
535 * defined above may be given by the user
536 * as underscores or dashes, or omitted
541 if (*s
== '-' || *s
== '_')
559 fprintf(stderr
, "%s: unrecognised option '--%s'\n",
564 if (!opthasval
[optid
]) {
566 fprintf(stderr
, "%s: unexpected argument to option"
567 " '--%s'\n", PNAME
, p
);
575 fprintf(stderr
, "%s: option '--%s' expects"
576 " an argument\n", PNAME
, p
);
582 p
+= strlen(p
); /* finished with this argument word */
589 for (i
= 0; i
< NSHORTOPTS
; i
++)
590 if (c
== shortopts
[i
]) {
591 optid
= shortvals
[i
];
596 fprintf(stderr
, "%s: unrecognised option '-%c'\n",
601 if (opthasval
[optid
]) {
605 } else if (--argc
> 0) {
608 fprintf(stderr
, "%s: option '-%c' expects"
609 " an argument\n", PNAME
, c
);
620 * Now actually process the option.
627 #ifdef PACKAGE_VERSION
628 printf("%s, revision %s\n", PNAME
, PACKAGE_VERSION
);
630 printf("%s: version number not available when not built"
631 " via automake\n", PNAME
);
636 extern const char *const licence
[];
639 for (i
= 0; licence
[i
]; i
++)
640 fputs(licence
[i
], stdout
);
646 if (nactions
>= actionsize
) {
647 actionsize
= nactions
* 3 / 2 + 16;
648 actions
= sresize(actions
, actionsize
, struct action
);
650 actions
[nactions
].mode
= SCAN
;
651 actions
[nactions
].arg
= optval
;
655 if (nactions
>= actionsize
) {
656 actionsize
= nactions
* 3 / 2 + 16;
657 actions
= sresize(actions
, actionsize
, struct action
);
659 actions
[nactions
].mode
= SCANDUMP
;
660 actions
[nactions
].arg
= optval
;
664 if (nactions
>= actionsize
) {
665 actionsize
= nactions
* 3 / 2 + 16;
666 actions
= sresize(actions
, actionsize
, struct action
);
668 actions
[nactions
].mode
= DUMP
;
669 actions
[nactions
].arg
= NULL
;
673 if (nactions
>= actionsize
) {
674 actionsize
= nactions
* 3 / 2 + 16;
675 actions
= sresize(actions
, actionsize
, struct action
);
677 actions
[nactions
].mode
= LOAD
;
678 actions
[nactions
].arg
= NULL
;
682 if (nactions
>= actionsize
) {
683 actionsize
= nactions
* 3 / 2 + 16;
684 actions
= sresize(actions
, actionsize
, struct action
);
686 actions
[nactions
].mode
= TEXT
;
687 actions
[nactions
].arg
= optval
;
691 if (nactions
>= actionsize
) {
692 actionsize
= nactions
* 3 / 2 + 16;
693 actions
= sresize(actions
, actionsize
, struct action
);
695 actions
[nactions
].mode
= HTML
;
696 actions
[nactions
].arg
= optval
;
700 if (nactions
>= actionsize
) {
701 actionsize
= nactions
* 3 / 2 + 16;
702 actions
= sresize(actions
, actionsize
, struct action
);
704 actions
[nactions
].mode
= HTTPD
;
705 actions
[nactions
].arg
= NULL
;
709 if (nactions
>= actionsize
) {
710 actionsize
= nactions
* 3 / 2 + 16;
711 actions
= sresize(actions
, actionsize
, struct action
);
713 actions
[nactions
].mode
= REMOVE
;
714 actions
[nactions
].arg
= NULL
;
723 case OPT_TTYPROGRESS
:
748 tqdepth
= atoi(optval
);
751 textcutoff
= parse_age(now
, optval
);
754 if (!strcmp(optval
, "auto")) {
755 htmlautoagerange
= 1;
757 char *q
= optval
+ strcspn(optval
, "-:");
760 htmloldest
= parse_age(now
, optval
);
761 htmlnewest
= *q ?
parse_age(now
, q
) : now
;
762 htmlautoagerange
= 0;
768 if (optval
[0] == '[' &&
769 (port
= strchr(optval
, ']')) != NULL
)
773 port
+= strcspn(port
, ":");
776 httpserveraddr
= optval
;
777 httpserverport
= atoi(port
);
781 if (!strcmp(optval
, "magic"))
782 auth
= HTTPD_AUTH_MAGIC
;
783 else if (!strcmp(optval
, "basic"))
784 auth
= HTTPD_AUTH_BASIC
;
785 else if (!strcmp(optval
, "none"))
786 auth
= HTTPD_AUTH_NONE
;
787 else if (!strcmp(optval
, "default"))
788 auth
= HTTPD_AUTH_MAGIC
| HTTPD_AUTH_BASIC
;
789 else if (!strcmp(optval
, "help") ||
790 !strcmp(optval
, "list")) {
791 printf(PNAME
": supported HTTP authentication types"
793 " magic use Linux /proc/net/tcp to"
794 " determine owner of peer socket\n"
795 " basic HTTP Basic username and"
796 " password authentication\n"
797 " default use 'magic' if possible, "
798 " otherwise fall back to 'basic'\n"
799 " none unauthenticated HTTP (if"
800 " the data file is non-confidential)\n");
803 fprintf(stderr
, "%s: unrecognised authentication"
804 " type '%s'\n%*s options are 'magic',"
805 " 'basic', 'none', 'default'\n",
806 PNAME
, optval
, (int)strlen(PNAME
), "");
817 int authlen
, authsize
;
820 if (optid
== OPT_AUTHFILE
) {
821 fd
= open(optval
, O_RDONLY
);
823 fprintf(stderr
, "%s: %s: open: %s\n", PNAME
,
824 optval
, strerror(errno
));
831 sprintf(namebuf
, "fd %d", fd
);
836 authbuf
= snewn(authsize
, char);
837 while ((ret
= read(fd
, authbuf
+authlen
,
838 authsize
-authlen
)) > 0) {
840 if ((authsize
- authlen
) < (authsize
/ 16)) {
841 authsize
= authlen
* 3 / 2 + 4096;
842 authbuf
= sresize(authbuf
, authsize
, char);
846 fprintf(stderr
, "%s: %s: read: %s\n", PNAME
,
847 name
, strerror(errno
));
850 if (optid
== OPT_AUTHFILE
)
852 httpauthdata
= authbuf
;
856 case OPT_INCLUDEPATH
:
858 case OPT_EXCLUDEPATH
:
861 if (ninex
>= inexsize
) {
862 inexsize
= ninex
* 3 / 2 + 16;
863 inex
= sresize(inex
, inexsize
,
864 struct inclusion_exclusion
);
866 inex
[ninex
].path
= (optid
== OPT_INCLUDEPATH
||
867 optid
== OPT_EXCLUDEPATH
||
868 optid
== OPT_PRUNEPATH
);
869 inex
[ninex
].type
= (optid
== OPT_INCLUDE ?
1 :
870 optid
== OPT_INCLUDEPATH ?
1 :
871 optid
== OPT_EXCLUDE ?
0 :
872 optid
== OPT_EXCLUDEPATH ?
0 :
873 optid
== OPT_PRUNE ?
-1 :
874 /* optid == OPT_PRUNEPATH ? */ -1);
875 inex
[ninex
].wildcard
= optval
;
881 fprintf(stderr
, "%s: unexpected argument '%s'\n", PNAME
, p
);
891 for (action
= 0; action
< nactions
; action
++) {
892 int mode
= actions
[action
].mode
;
894 if (mode
== SCAN
|| mode
== SCANDUMP
|| mode
== LOAD
) {
895 const char *scandir
= actions
[action
].arg
;
898 char *buf
= fgetline(stdin
);
900 buf
[strcspn(buf
, "\r\n")] = '\0';
901 if (1 != sscanf(buf
, DUMPHDR
"%x",
903 fprintf(stderr
, "%s: header in dump file not recognised\n",
907 pathsep
= (char)newpathsep
;
911 if (mode
== SCAN
|| mode
== LOAD
) {
913 * Prepare to write out the index file.
915 fd
= open(filename
, O_RDWR
| O_TRUNC
| O_CREAT
,
918 fprintf(stderr
, "%s: %s: open: %s\n", PNAME
, filename
,
922 if (fstat(fd
, &st
) < 0) {
923 perror(PNAME
": fstat");
926 ctx
->datafile_dev
= st
.st_dev
;
927 ctx
->datafile_ino
= st
.st_ino
;
928 ctx
->straight_to_dump
= 0;
930 ctx
->datafile_dev
= -1;
931 ctx
->datafile_ino
= -1;
932 ctx
->straight_to_dump
= 1;
935 if (mode
== SCAN
|| mode
== SCANDUMP
) {
936 if (stat(scandir
, &st
) < 0) {
937 fprintf(stderr
, "%s: %s: stat: %s\n", PNAME
, scandir
,
941 ctx
->filesystem_dev
= crossfs ?
0 : st
.st_dev
;
946 ctx
->crossfs
= crossfs
;
947 ctx
->fakeatimes
= fakediratimes
;
948 ctx
->usemtime
= mtime
;
950 ctx
->last_output_update
= time(NULL
);
952 /* progress==1 means report progress only if stderr is a tty */
954 progress
= isatty(2) ?
2 : 0;
955 ctx
->progress
= progress
;
958 if (progress
&& ioctl(2, TIOCGWINSZ
, &ws
) == 0)
959 ctx
->progwidth
= ws
.ws_col
- 1;
964 if (mode
== SCANDUMP
)
965 printf(DUMPHDR
"%02x\n", (unsigned char)pathsep
);
968 * Scan the directory tree, and write out the trie component
971 if (mode
!= SCANDUMP
) {
972 ctx
->tb
= triebuild_new(fd
);
977 while ((buf
= fgetline(stdin
)) != NULL
) {
981 buf
[strcspn(buf
, "\r\n")] = '\0';
985 while (*p
&& *p
!= ' ') p
++;
987 fprintf(stderr
, "%s: dump file line %d: expected at least"
988 " three fields\n", PNAME
, line
);
992 tf
.size
= strtoull(q
, NULL
, 10);
994 while (*p
&& *p
!= ' ') p
++;
996 fprintf(stderr
, "%s: dump file line %d: expected at least"
997 " three fields\n", PNAME
, line
);
1001 tf
.atime
= strtoull(q
, NULL
, 10);
1009 for (i
= 0; i
< 2; i
++) {
1011 if (*p
>= '0' && *p
<= '9')
1013 else if (*p
>= 'A' && *p
<= 'F')
1014 c
+= *p
- ('A' - 10);
1015 else if (*p
>= 'a' && *p
<= 'f')
1016 c
+= *p
- ('a' - 10);
1018 fprintf(stderr
, "%s: dump file line %d: unable"
1019 " to parse hex escape\n", PNAME
, line
);
1028 triebuild_add(ctx
->tb
, buf
, &tf
);
1033 du(scandir
, gotdata
, scan_error
, ctx
);
1035 if (mode
!= SCANDUMP
) {
1037 char *buf
, *prevbuf
;
1039 count
= triebuild_finish(ctx
->tb
);
1040 triebuild_free(ctx
->tb
);
1042 if (ctx
->progress
) {
1043 fprintf(stderr
, "%-*s\r", ctx
->progwidth
, "");
1048 * Work out how much space the cumulative index trees
1049 * will take; enlarge the file, and memory-map it.
1051 if (fstat(fd
, &st
) < 0) {
1052 perror(PNAME
": fstat");
1056 printf("Built pathname index, %d entries, %llu bytes\n", count
,
1057 (unsigned long long)st
.st_size
);
1059 totalsize
= index_compute_size(st
.st_size
, count
);
1061 if (lseek(fd
, totalsize
-1, SEEK_SET
) < 0) {
1062 perror(PNAME
": lseek");
1065 if (write(fd
, "\0", 1) < 1) {
1066 perror(PNAME
": write");
1070 printf("Upper bound on index file size = %llu bytes\n",
1071 (unsigned long long)totalsize
);
1073 mappedfile
= mmap(NULL
, totalsize
, PROT_READ
|PROT_WRITE
,MAP_SHARED
, fd
, 0);
1075 perror(PNAME
": mmap");
1079 if (fakediratimes
) {
1080 printf("Faking directory atimes\n");
1081 trie_fake_dir_atimes(mappedfile
);
1084 printf("Building index\n");
1085 ib
= indexbuild_new(mappedfile
, st
.st_size
, count
);
1086 maxpathlen
= trie_maxpathlen(mappedfile
);
1087 buf
= snewn(maxpathlen
, char);
1088 prevbuf
= snewn(maxpathlen
, char);
1089 tw
= triewalk_new(mappedfile
);
1091 tf
= triewalk_next(tw
, buf
);
1097 * Get the next file from the index. So we are
1098 * currently holding, and have not yet
1099 * indexed, prevtf (with pathname prevbuf) and
1100 * tf (with pathname buf).
1103 memcpy(prevbuf
, buf
, maxpathlen
);
1104 tf
= triewalk_next(tw
, buf
);
1110 * Find the first differing character position
1111 * between our two pathnames.
1113 for (i
= 0; prevbuf
[i
] && prevbuf
[i
] == buf
[i
]; i
++);
1116 * If prevbuf was a directory name and buf is
1117 * something inside that directory, then
1118 * trie_before() will be called on prevbuf
1119 * itself. Hence we must drop a tag before it,
1120 * so that the resulting index is usable.
1122 if ((!prevbuf
[i
] && (buf
[i
] == pathsep
||
1123 (i
> 0 && buf
[i
-1] == pathsep
))))
1127 * Add prevtf to the index.
1129 indexbuild_add(ib
, prevtf
);
1133 * Drop an unconditional final tag, and
1134 * get out of this loop.
1141 * In full-index mode, index everything.
1147 * If prevbuf was a filename inside some
1148 * directory which buf is outside, then
1149 * trie_before() will be called on some
1150 * pathname either equal to buf or epsilon
1151 * less than it. Either way, we're going to
1152 * need to drop a tag after prevtf.
1154 if (strchr(prevbuf
+i
, pathsep
) || !tf
)
1159 realsize
= indexbuild_realsize(ib
);
1160 indexbuild_free(ib
);
1162 munmap(mappedfile
, totalsize
);
1163 ftruncate(fd
, realsize
);
1165 printf("Actual index file size = %llu bytes\n",
1166 (unsigned long long)realsize
);
1168 } else if (mode
== TEXT
) {
1169 char *querydir
= actions
[action
].arg
;
1172 fd
= open(filename
, O_RDONLY
);
1174 fprintf(stderr
, "%s: %s: open: %s\n", PNAME
, filename
,
1178 if (fstat(fd
, &st
) < 0) {
1179 perror(PNAME
": fstat");
1182 totalsize
= st
.st_size
;
1183 mappedfile
= mmap(NULL
, totalsize
, PROT_READ
, MAP_SHARED
, fd
, 0);
1185 perror(PNAME
": mmap");
1188 pathsep
= trie_pathsep(mappedfile
);
1191 * Trim trailing slash, just in case.
1193 pathlen
= strlen(querydir
);
1194 if (pathlen
> 0 && querydir
[pathlen
-1] == pathsep
)
1195 querydir
[--pathlen
] = '\0';
1197 text_query(mappedfile
, querydir
, textcutoff
, tqdepth
);
1199 munmap(mappedfile
, totalsize
);
1200 } else if (mode
== HTML
) {
1201 char *querydir
= actions
[action
].arg
;
1203 struct html_config cfg
;
1207 fd
= open(filename
, O_RDONLY
);
1209 fprintf(stderr
, "%s: %s: open: %s\n", PNAME
, filename
,
1213 if (fstat(fd
, &st
) < 0) {
1214 perror(PNAME
": fstat");
1217 totalsize
= st
.st_size
;
1218 mappedfile
= mmap(NULL
, totalsize
, PROT_READ
, MAP_SHARED
, fd
, 0);
1220 perror(PNAME
": mmap");
1223 pathsep
= trie_pathsep(mappedfile
);
1226 * Trim trailing slash, just in case.
1228 pathlen
= strlen(querydir
);
1229 if (pathlen
> 0 && querydir
[pathlen
-1] == pathsep
)
1230 querydir
[--pathlen
] = '\0';
1232 xi
= trie_before(mappedfile
, querydir
);
1234 cfg
.autoage
= htmlautoagerange
;
1235 cfg
.oldest
= htmloldest
;
1236 cfg
.newest
= htmlnewest
;
1237 html
= html_query(mappedfile
, xi
, &cfg
);
1238 fputs(html
, stdout
);
1240 munmap(mappedfile
, totalsize
);
1241 } else if (mode
== DUMP
) {
1245 fd
= open(filename
, O_RDONLY
);
1247 fprintf(stderr
, "%s: %s: open: %s\n", PNAME
, filename
,
1251 if (fstat(fd
, &st
) < 0) {
1252 perror(PNAME
": fstat");
1255 totalsize
= st
.st_size
;
1256 mappedfile
= mmap(NULL
, totalsize
, PROT_READ
, MAP_SHARED
, fd
, 0);
1258 perror(PNAME
": mmap");
1261 pathsep
= trie_pathsep(mappedfile
);
1263 maxpathlen
= trie_maxpathlen(mappedfile
);
1264 buf
= snewn(maxpathlen
, char);
1266 printf(DUMPHDR
"%02x\n", (unsigned char)pathsep
);
1267 tw
= triewalk_new(mappedfile
);
1268 while ((tf
= triewalk_next(tw
, buf
)) != NULL
)
1272 munmap(mappedfile
, totalsize
);
1273 } else if (mode
== HTTPD
) {
1274 struct html_config pcfg
;
1275 struct httpd_config dcfg
;
1277 fd
= open(filename
, O_RDONLY
);
1279 fprintf(stderr
, "%s: %s: open: %s\n", PNAME
, filename
,
1283 if (fstat(fd
, &st
) < 0) {
1284 perror(PNAME
": fstat");
1287 totalsize
= st
.st_size
;
1288 mappedfile
= mmap(NULL
, totalsize
, PROT_READ
, MAP_SHARED
, fd
, 0);
1290 perror(PNAME
": mmap");
1293 pathsep
= trie_pathsep(mappedfile
);
1295 dcfg
.address
= httpserveraddr
;
1296 dcfg
.port
= httpserverport
;
1297 dcfg
.basicauthdata
= httpauthdata
;
1299 pcfg
.autoage
= htmlautoagerange
;
1300 pcfg
.oldest
= htmloldest
;
1301 pcfg
.newest
= htmlnewest
;
1302 run_httpd(mappedfile
, auth
, &dcfg
, &pcfg
);
1303 munmap(mappedfile
, totalsize
);
1304 } else if (mode
== REMOVE
) {
1305 if (remove(filename
) < 0) {
1306 fprintf(stderr
, "%s: %s: remove: %s\n", PNAME
, filename
,