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
;
55 static void dump_line(const char *pathname
, const struct trie_file
*tf
)
58 printf("%llu %llu ", tf
->size
, tf
->atime
);
59 for (p
= pathname
; *p
; p
++) {
60 if (*p
>= ' ' && *p
< 127 && *p
!= '%')
63 printf("%%%02x", (unsigned char)*p
);
68 static int gotdata(void *vctx
, const char *pathname
, const STRUCT_STAT
*st
)
70 struct ctx
*ctx
= (struct ctx
*)vctx
;
71 struct trie_file file
;
77 * Filter out our own data file.
79 if (st
->st_dev
== ctx
->datafile_dev
&& st
->st_ino
== ctx
->datafile_ino
)
83 * Don't cross the streams^W^Wany file system boundary.
85 if (!ctx
->crossfs
&& st
->st_dev
!= ctx
->filesystem_dev
)
88 file
.size
= (unsigned long long)512 * st
->st_blocks
;
89 if (ctx
->fakeatimes
&& S_ISDIR(st
->st_mode
))
90 file
.atime
= st
->st_mtime
;
92 file
.atime
= st
->st_atime
;
95 * Filter based on wildcards.
98 filename
= strrchr(pathname
, pathsep
);
103 for (i
= 0; i
< ctx
->ninex
; i
++) {
104 if (fnmatch(ctx
->inex
[i
].wildcard
,
105 ctx
->inex
[i
].path ? pathname
: filename
, 0) == 0)
106 include
= ctx
->inex
[i
].type
;
109 return 0; /* ignore this entry and any subdirs */
112 * Here we are supposed to be filtering an entry out, but
113 * still recursing into it if it's a directory. However,
114 * we can't actually leave out any directory whose
115 * subdirectories we then look at. So we cheat, in that
116 * case, by setting the size to zero.
118 if (!S_ISDIR(st
->st_mode
))
119 return 0; /* just ignore */
124 if (ctx
->straight_to_dump
)
125 dump_line(pathname
, &file
);
127 triebuild_add(ctx
->tb
, pathname
, &file
);
131 if (t
!= ctx
->last_output_update
) {
132 fprintf(stderr
, "%-*.*s\r", ctx
->progwidth
, ctx
->progwidth
,
135 ctx
->last_output_update
= t
;
142 static void text_query(const void *mappedfile
, const char *querydir
,
147 unsigned long xi1
, xi2
;
148 unsigned long long s1
, s2
;
150 maxpathlen
= trie_maxpathlen(mappedfile
);
151 pathbuf
= snewn(maxpathlen
+ 1, char);
154 * We want to query everything between the supplied filename
155 * (inclusive) and that filename with a ^A on the end
156 * (exclusive). So find the x indices for each.
158 strcpy(pathbuf
, querydir
);
159 make_successor(pathbuf
);
160 xi1
= trie_before(mappedfile
, querydir
);
161 xi2
= trie_before(mappedfile
, pathbuf
);
164 return; /* file, or empty dir => no display */
167 * Now do the lookups in the age index.
169 s1
= index_query(mappedfile
, xi1
, t
);
170 s2
= index_query(mappedfile
, xi2
, t
);
173 return; /* no space taken up => no display */
177 * Now scan for first-level subdirectories and report
182 trie_getpath(mappedfile
, xi1
, pathbuf
);
183 text_query(mappedfile
, pathbuf
, t
, depth
-1);
184 make_successor(pathbuf
);
185 xi1
= trie_before(mappedfile
, pathbuf
);
189 /* Display in units of 1Kb */
190 printf("%-11llu %s\n", (s2
- s1
) / 1024, querydir
);
194 * Largely frivolous way to define all my command-line options. I
195 * present here a parametric macro which declares a series of
196 * _logical_ option identifiers, and for each one declares zero or
197 * more short option characters and zero or more long option
198 * words. Then I repeatedly invoke that macro with its arguments
199 * defined to be various other macros, which allows me to
202 * - define an enum allocating a distinct integer value to each
204 * - define a string consisting of precisely all the short option
206 * - define a string array consisting of all the long option
208 * - define (with help from auxiliary enums) integer arrays
209 * parallel to both of the above giving the logical option id
210 * for each physical short and long option
211 * - define an array indexed by logical option id indicating
212 * whether the option in question takes a value
213 * - define a function which prints out brief online help for all
216 * It's not at all clear to me that this trickery is actually
217 * particularly _efficient_ - it still, after all, requires going
218 * linearly through the option list at run time and doing a
219 * strcmp, whereas in an ideal world I'd have liked the lists of
220 * long and short options to be pre-sorted so that a binary search
221 * or some other more efficient lookup was possible. (Not that
222 * asymptotic algorithmic complexity is remotely vital in option
223 * parsing, but if I were doing this in, say, Lisp or something
224 * with an equivalently powerful preprocessor then once I'd had
225 * the idea of preparing the option-parsing data structures at
226 * compile time I would probably have made the effort to prepare
227 * them _properly_. I could have Perl generate me a source file
228 * from some sort of description, I suppose, but that would seem
229 * like overkill. And in any case, it's more of a challenge to
230 * achieve as much as possible by cunning use of cpp and enum than
231 * to just write some sensible and logical code in a Turing-
232 * complete language. I said it was largely frivolous :-)
234 * This approach does have the virtue that it brings together the
235 * option ids, option spellings and help text into a single
236 * combined list and defines them all in exactly one place. If I
237 * want to add a new option, or a new spelling for an option, I
238 * only have to modify the main OPTHELP macro below and then add
239 * code to process the new logical id.
241 * (Though, really, even that isn't ideal, since it still involves
242 * modifying the source file in more than one place. In a
243 * _properly_ ideal world, I'd be able to interleave the option
244 * definitions with the code fragments that process them. And then
245 * not bother defining logical identifiers for them at all - those
246 * would be automatically generated, since I wouldn't have any
247 * need to specify them manually in another part of the code.)
249 * One other helpful consequence of the enum-based structure here
250 * is that it causes a compiler error if I accidentally try to
251 * define the same option (short or long) twice.
254 #define OPTHELP(NOVAL, VAL, SHORT, LONG, HELPPFX, HELPARG, HELPLINE, HELPOPT) \
255 HELPPFX("usage") HELPLINE(PNAME " [options] action [action...]") \
257 VAL(SCAN) SHORT(s) LONG(scan) \
258 HELPARG("directory") HELPOPT("scan and index a directory") \
259 NOVAL(HTTPD) SHORT(w) LONG(web) LONG(server) LONG(httpd) \
260 HELPOPT("serve HTML reports from a temporary web server") \
261 VAL(TEXT) SHORT(t) LONG(text) \
262 HELPARG("subdir") HELPOPT("print a plain text report on a subdirectory") \
263 NOVAL(REMOVE) SHORT(R) LONG(remove) LONG(delete) LONG(unlink) \
264 HELPOPT("remove the index file") \
265 NOVAL(DUMP) SHORT(D) LONG(dump) HELPOPT("dump the index file on stdout") \
266 NOVAL(LOAD) SHORT(L) LONG(load) \
267 HELPOPT("load and index a dump file") \
268 VAL(SCANDUMP) SHORT(S) LONG(scan_dump) \
269 HELPARG("directory") HELPOPT("scan only, generating a dump") \
270 VAL(HTML) SHORT(H) LONG(html) \
271 HELPARG("subdir") HELPOPT("print an HTML report on a subdirectory") \
273 VAL(DATAFILE) SHORT(f) LONG(file) \
274 HELPARG("filename") HELPOPT("[most modes] specify index file") \
275 NOVAL(CROSSFS) LONG(cross_fs) \
276 HELPOPT("[--scan] cross filesystem boundaries") \
277 NOVAL(NOCROSSFS) LONG(no_cross_fs) \
278 HELPOPT("[--scan] stick to one filesystem") \
279 VAL(PRUNE) LONG(prune) \
280 HELPARG("wildcard") HELPOPT("[--scan] prune files matching pattern") \
281 VAL(PRUNEPATH) LONG(prune_path) \
282 HELPARG("wildcard") HELPOPT("[--scan] prune pathnames matching pattern") \
283 VAL(EXCLUDE) LONG(exclude) \
284 HELPARG("wildcard") HELPOPT("[--scan] exclude files matching pattern") \
285 VAL(EXCLUDEPATH) LONG(exclude_path) \
286 HELPARG("wildcard") HELPOPT("[--scan] exclude pathnames matching pattern") \
287 VAL(INCLUDE) LONG(include) \
288 HELPARG("wildcard") HELPOPT("[--scan] include files matching pattern") \
289 VAL(INCLUDEPATH) LONG(include_path) \
290 HELPARG("wildcard") HELPOPT("[--scan] include pathnames matching pattern") \
291 NOVAL(PROGRESS) LONG(progress) LONG(scan_progress) \
292 HELPOPT("[--scan] report progress on stderr") \
293 NOVAL(NOPROGRESS) LONG(no_progress) LONG(no_scan_progress) \
294 HELPOPT("[--scan] do not report progress") \
295 NOVAL(TTYPROGRESS) LONG(tty_progress) LONG(tty_scan_progress) \
296 LONG(progress_tty) LONG(scan_progress_tty) \
297 HELPOPT("[--scan] report progress if stderr is a tty") \
298 NOVAL(DIRATIME) LONG(dir_atime) LONG(dir_atimes) \
299 HELPOPT("[--scan,--load] keep real atimes on directories") \
300 NOVAL(NODIRATIME) LONG(no_dir_atime) LONG(no_dir_atimes) \
301 HELPOPT("[--scan,--load] fake atimes on directories") \
302 VAL(AGERANGE) SHORT(r) LONG(age_range) LONG(range) LONG(ages) \
303 HELPARG("age[-age]") HELPOPT("[--web,--html] set limits of colour coding") \
304 VAL(SERVERADDR) LONG(address) LONG(addr) LONG(server_address) \
306 HELPARG("addr[:port]") HELPOPT("[--web] specify HTTP server address") \
307 VAL(AUTH) LONG(auth) LONG(http_auth) LONG(httpd_auth) \
308 LONG(server_auth) LONG(web_auth) \
309 HELPARG("type") HELPOPT("[--web] specify HTTP authentication method") \
310 VAL(AUTHFILE) LONG(auth_file) \
311 HELPARG("filename") HELPOPT("[--web] read HTTP Basic user/pass from file") \
312 VAL(AUTHFD) LONG(auth_fd) \
313 HELPARG("fd") HELPOPT("[--web] read HTTP Basic user/pass from fd") \
314 VAL(TQDEPTH) SHORT(d) LONG(depth) LONG(max_depth) LONG(maximum_depth) \
315 HELPARG("levels") HELPOPT("[--text] recurse to this many levels") \
316 VAL(MINAGE) SHORT(a) LONG(age) LONG(min_age) LONG(minimum_age) \
317 HELPARG("age") HELPOPT("[--text] include only files older than this") \
319 NOVAL(HELP) SHORT(h) LONG(help) HELPOPT("display this help text") \
320 NOVAL(VERSION) SHORT(V) LONG(version) HELPOPT("report version number") \
321 NOVAL(LICENCE) LONG(licence) LONG(license) \
322 HELPOPT("display (MIT) licence text") \
325 #define DEFENUM(x) OPT_ ## x,
328 #define STRING(x) #x ,
329 #define STRINGNOCOMMA(x) #x
330 #define SHORTNEWOPT(x) SHORTtmp_ ## x = OPT_ ## x,
331 #define SHORTTHISOPT(x) SHORTtmp2_ ## x, SHORTVAL_ ## x = SHORTtmp2_ ## x - 1,
332 #define SHORTOPTVAL(x) SHORTVAL_ ## x,
333 #define SHORTTMP(x) SHORTtmp3_ ## x,
334 #define LONGNEWOPT(x) LONGtmp_ ## x = OPT_ ## x,
335 #define LONGTHISOPT(x) LONGtmp2_ ## x, LONGVAL_ ## x = LONGtmp2_ ## x - 1,
336 #define LONGOPTVAL(x) LONGVAL_ ## x,
337 #define LONGTMP(x) SHORTtmp3_ ## x,
339 #define OPTIONS(NOVAL, VAL, SHORT, LONG) \
340 OPTHELP(NOVAL, VAL, SHORT, LONG, IGNORE, IGNORE, IGNORE, IGNORE)
342 enum { OPTIONS(DEFENUM
,DEFENUM
,IGNORE
,IGNORE
) NOPTIONS
};
343 enum { OPTIONS(IGNORE
,IGNORE
,SHORTTMP
,IGNORE
) NSHORTOPTS
};
344 enum { OPTIONS(IGNORE
,IGNORE
,IGNORE
,LONGTMP
) NLONGOPTS
};
345 static const int opthasval
[NOPTIONS
] = {OPTIONS(ZERO
,ONE
,IGNORE
,IGNORE
)};
346 static const char shortopts
[] = {OPTIONS(IGNORE
,IGNORE
,STRINGNOCOMMA
,IGNORE
)};
347 static const char *const longopts
[] = {OPTIONS(IGNORE
,IGNORE
,IGNORE
,STRING
)};
348 enum { OPTIONS(SHORTNEWOPT
,SHORTNEWOPT
,SHORTTHISOPT
,IGNORE
) };
349 enum { OPTIONS(LONGNEWOPT
,LONGNEWOPT
,IGNORE
,LONGTHISOPT
) };
350 static const int shortvals
[] = {OPTIONS(IGNORE
,IGNORE
,SHORTOPTVAL
,IGNORE
)};
351 static const int longvals
[] = {OPTIONS(IGNORE
,IGNORE
,IGNORE
,LONGOPTVAL
)};
353 static void usage(FILE *fp
)
356 const char *prefix
, *shortopt
, *longopt
, *optarg
;
359 #define HELPRESET prefix = shortopt = longopt = optarg = NULL, optex = -1
360 #define HELPNOVAL(s) optex = 0;
361 #define HELPVAL(s) optex = 1;
362 #define HELPSHORT(s) if (!shortopt) shortopt = "-" #s;
363 #define HELPLONG(s) if (!longopt) { \
364 strcpy(longbuf, "--" #s); longopt = longbuf; \
365 for (i = 0; longbuf[i]; i++) if (longbuf[i] == '_') longbuf[i] = '-'; }
366 #define HELPPFX(s) prefix = s;
367 #define HELPARG(s) optarg = s;
368 #define HELPLINE(s) assert(optex == -1); \
369 fprintf(fp, "%7s%c %s\n", prefix?prefix:"", prefix?':':' ', s); \
371 #define HELPOPT(s) assert((optex == 1 && optarg) || (optex == 0 && !optarg)); \
372 assert(shortopt || longopt); \
373 i = fprintf(fp, "%7s%c %s%s%s%s%s", prefix?prefix:"", prefix?':':' ', \
374 shortopt?shortopt:"", shortopt&&longopt?", ":"", longopt?longopt:"", \
375 optarg?" ":"", optarg?optarg:""); \
376 fprintf(fp, "%*s %s\n", i<32?32-i:0,"",s); HELPRESET;
379 OPTHELP(HELPNOVAL
, HELPVAL
, HELPSHORT
, HELPLONG
,
380 HELPPFX
, HELPARG
, HELPLINE
, HELPOPT
);
393 static time_t parse_age(time_t now
, const char *agestr
)
402 if (2 != sscanf(agestr
, "%d%1[DdWwMmYy]", &nunits
, unit
)) {
403 fprintf(stderr
, "%s: age specification should be a number followed by"
404 " one of d,w,m,y\n", PNAME
);
408 if (unit
[0] == 'd') {
410 } else if (unit
[0] == 'w') {
411 t
-= 86400 * 7 * nunits
;
416 ym
= tm
.tm_year
* 12 + tm
.tm_mon
;
423 tm
.tm_year
= ym
/ 12;
432 int main(int argc
, char **argv
)
435 struct ctx actx
, *ctx
= &actx
;
437 off_t totalsize
, realsize
;
441 const struct trie_file
*tf
;
442 char *filename
= PNAME
".dat";
444 enum { TEXT
, HTML
, SCAN
, DUMP
, SCANDUMP
, LOAD
, HTTPD
, REMOVE
};
449 int nactions
= 0, actionsize
= 0, action
;
450 time_t now
= time(NULL
);
451 time_t textcutoff
= now
, htmlnewest
= now
, htmloldest
= now
;
452 int htmlautoagerange
= 1;
453 const char *httpserveraddr
= NULL
;
454 int httpserverport
= 0;
455 const char *httpauthdata
= NULL
;
456 int auth
= HTTPD_AUTH_MAGIC
| HTTPD_AUTH_BASIC
;
458 struct inclusion_exclusion
*inex
= NULL
;
459 int ninex
= 0, inexsize
= 0;
462 int fakediratimes
= 1;
464 #ifdef DEBUG_MAD_OPTION_PARSING_MACROS
466 static const char *const optnames
[NOPTIONS
] = {
467 OPTIONS(STRING
,STRING
,IGNORE
,IGNORE
)
470 for (i
= 0; i
< NSHORTOPTS
; i
++)
471 printf("-%c == %s [%s]\n", shortopts
[i
], optnames
[shortvals
[i
]],
472 opthasval
[shortvals
[i
]] ?
"value" : "no value");
473 for (i
= 0; i
< NLONGOPTS
; i
++)
474 printf("--%s == %s [%s]\n", longopts
[i
], optnames
[longvals
[i
]],
475 opthasval
[longvals
[i
]] ?
"value" : "no value");
482 if (doing_opts
&& *p
== '-') {
485 if (!strcmp(p
, "--")) {
496 if (wordstart
&& *p
== '-') {
498 * GNU-style long option.
501 optval
= strchr(p
, '=');
505 for (i
= 0; i
< NLONGOPTS
; i
++) {
506 const char *opt
= longopts
[i
], *s
= p
;
509 * The underscores in the option names
510 * defined above may be given by the user
511 * as underscores or dashes, or omitted
516 if (*s
== '-' || *s
== '_')
534 fprintf(stderr
, "%s: unrecognised option '--%s'\n",
539 if (!opthasval
[optid
]) {
541 fprintf(stderr
, "%s: unexpected argument to option"
542 " '--%s'\n", PNAME
, p
);
550 fprintf(stderr
, "%s: option '--%s' expects"
551 " an argument\n", PNAME
, p
);
557 p
+= strlen(p
); /* finished with this argument word */
564 for (i
= 0; i
< NSHORTOPTS
; i
++)
565 if (c
== shortopts
[i
]) {
566 optid
= shortvals
[i
];
571 fprintf(stderr
, "%s: unrecognised option '-%c'\n",
576 if (opthasval
[optid
]) {
580 } else if (--argc
> 0) {
583 fprintf(stderr
, "%s: option '-%c' expects"
584 " an argument\n", PNAME
, c
);
595 * Now actually process the option.
602 #ifdef PACKAGE_VERSION
603 printf("%s, revision %s\n", PNAME
, PACKAGE_VERSION
);
605 printf("%s: version number not available when not built"
606 " via automake\n", PNAME
);
611 extern const char *const licence
[];
614 for (i
= 0; licence
[i
]; i
++)
615 fputs(licence
[i
], stdout
);
621 if (nactions
>= actionsize
) {
622 actionsize
= nactions
* 3 / 2 + 16;
623 actions
= sresize(actions
, actionsize
, struct action
);
625 actions
[nactions
].mode
= SCAN
;
626 actions
[nactions
].arg
= optval
;
630 if (nactions
>= actionsize
) {
631 actionsize
= nactions
* 3 / 2 + 16;
632 actions
= sresize(actions
, actionsize
, struct action
);
634 actions
[nactions
].mode
= SCANDUMP
;
635 actions
[nactions
].arg
= optval
;
639 if (nactions
>= actionsize
) {
640 actionsize
= nactions
* 3 / 2 + 16;
641 actions
= sresize(actions
, actionsize
, struct action
);
643 actions
[nactions
].mode
= DUMP
;
644 actions
[nactions
].arg
= NULL
;
648 if (nactions
>= actionsize
) {
649 actionsize
= nactions
* 3 / 2 + 16;
650 actions
= sresize(actions
, actionsize
, struct action
);
652 actions
[nactions
].mode
= LOAD
;
653 actions
[nactions
].arg
= NULL
;
657 if (nactions
>= actionsize
) {
658 actionsize
= nactions
* 3 / 2 + 16;
659 actions
= sresize(actions
, actionsize
, struct action
);
661 actions
[nactions
].mode
= TEXT
;
662 actions
[nactions
].arg
= optval
;
666 if (nactions
>= actionsize
) {
667 actionsize
= nactions
* 3 / 2 + 16;
668 actions
= sresize(actions
, actionsize
, struct action
);
670 actions
[nactions
].mode
= HTML
;
671 actions
[nactions
].arg
= optval
;
675 if (nactions
>= actionsize
) {
676 actionsize
= nactions
* 3 / 2 + 16;
677 actions
= sresize(actions
, actionsize
, struct action
);
679 actions
[nactions
].mode
= HTTPD
;
680 actions
[nactions
].arg
= NULL
;
684 if (nactions
>= actionsize
) {
685 actionsize
= nactions
* 3 / 2 + 16;
686 actions
= sresize(actions
, actionsize
, struct action
);
688 actions
[nactions
].mode
= REMOVE
;
689 actions
[nactions
].arg
= NULL
;
698 case OPT_TTYPROGRESS
:
717 tqdepth
= atoi(optval
);
720 textcutoff
= parse_age(now
, optval
);
723 if (!strcmp(optval
, "auto")) {
724 htmlautoagerange
= 1;
726 char *q
= optval
+ strcspn(optval
, "-:");
729 htmloldest
= parse_age(now
, optval
);
730 htmlnewest
= *q ?
parse_age(now
, q
) : now
;
731 htmlautoagerange
= 0;
737 if (optval
[0] == '[' &&
738 (port
= strchr(optval
, ']')) != NULL
)
742 port
+= strcspn(port
, ":");
745 httpserveraddr
= optval
;
746 httpserverport
= atoi(port
);
750 if (!strcmp(optval
, "magic"))
751 auth
= HTTPD_AUTH_MAGIC
;
752 else if (!strcmp(optval
, "basic"))
753 auth
= HTTPD_AUTH_BASIC
;
754 else if (!strcmp(optval
, "none"))
755 auth
= HTTPD_AUTH_NONE
;
756 else if (!strcmp(optval
, "default"))
757 auth
= HTTPD_AUTH_MAGIC
| HTTPD_AUTH_BASIC
;
758 else if (!strcmp(optval
, "help") ||
759 !strcmp(optval
, "list")) {
760 printf(PNAME
": supported HTTP authentication types"
762 " magic use Linux /proc/net/tcp to"
763 " determine owner of peer socket\n"
764 " basic HTTP Basic username and"
765 " password authentication\n"
766 " default use 'magic' if possible, "
767 " otherwise fall back to 'basic'\n"
768 " none unauthenticated HTTP (if"
769 " the data file is non-confidential)\n");
772 fprintf(stderr
, "%s: unrecognised authentication"
773 " type '%s'\n%*s options are 'magic',"
774 " 'basic', 'none', 'default'\n",
775 PNAME
, optval
, (int)strlen(PNAME
), "");
786 int authlen
, authsize
;
789 if (optid
== OPT_AUTHFILE
) {
790 fd
= open(optval
, O_RDONLY
);
792 fprintf(stderr
, "%s: %s: open: %s\n", PNAME
,
793 optval
, strerror(errno
));
800 sprintf(namebuf
, "fd %d", fd
);
805 authbuf
= snewn(authsize
, char);
806 while ((ret
= read(fd
, authbuf
+authlen
,
807 authsize
-authlen
)) > 0) {
809 if ((authsize
- authlen
) < (authsize
/ 16)) {
810 authsize
= authlen
* 3 / 2 + 4096;
811 authbuf
= sresize(authbuf
, authsize
, char);
815 fprintf(stderr
, "%s: %s: read: %s\n", PNAME
,
816 name
, strerror(errno
));
819 if (optid
== OPT_AUTHFILE
)
821 httpauthdata
= authbuf
;
825 case OPT_INCLUDEPATH
:
827 case OPT_EXCLUDEPATH
:
830 if (ninex
>= inexsize
) {
831 inexsize
= ninex
* 3 / 2 + 16;
832 inex
= sresize(inex
, inexsize
,
833 struct inclusion_exclusion
);
835 inex
[ninex
].path
= (optid
== OPT_INCLUDEPATH
||
836 optid
== OPT_EXCLUDEPATH
||
837 optid
== OPT_PRUNEPATH
);
838 inex
[ninex
].type
= (optid
== OPT_INCLUDE ?
1 :
839 optid
== OPT_INCLUDEPATH ?
1 :
840 optid
== OPT_EXCLUDE ?
0 :
841 optid
== OPT_EXCLUDEPATH ?
0 :
842 optid
== OPT_PRUNE ?
-1 :
843 /* optid == OPT_PRUNEPATH ? */ -1);
844 inex
[ninex
].wildcard
= optval
;
850 fprintf(stderr
, "%s: unexpected argument '%s'\n", PNAME
, p
);
860 for (action
= 0; action
< nactions
; action
++) {
861 int mode
= actions
[action
].mode
;
863 if (mode
== SCAN
|| mode
== SCANDUMP
|| mode
== LOAD
) {
864 const char *scandir
= actions
[action
].arg
;
866 char *buf
= fgetline(stdin
);
868 buf
[strcspn(buf
, "\r\n")] = '\0';
869 if (1 != sscanf(buf
, DUMPHDR
"%x",
871 fprintf(stderr
, "%s: header in dump file not recognised\n",
875 pathsep
= (char)newpathsep
;
879 if (mode
== SCAN
|| mode
== LOAD
) {
881 * Prepare to write out the index file.
883 fd
= open(filename
, O_RDWR
| O_TRUNC
| O_CREAT
, S_IRWXU
);
885 fprintf(stderr
, "%s: %s: open: %s\n", PNAME
, filename
,
889 if (fstat(fd
, &st
) < 0) {
890 perror(PNAME
": fstat");
893 ctx
->datafile_dev
= st
.st_dev
;
894 ctx
->datafile_ino
= st
.st_ino
;
895 ctx
->straight_to_dump
= 0;
897 ctx
->datafile_dev
= -1;
898 ctx
->datafile_ino
= -1;
899 ctx
->straight_to_dump
= 1;
902 if (mode
== SCAN
|| mode
== SCANDUMP
) {
903 if (stat(scandir
, &st
) < 0) {
904 fprintf(stderr
, "%s: %s: stat: %s\n", PNAME
, scandir
,
908 ctx
->filesystem_dev
= crossfs ?
0 : st
.st_dev
;
913 ctx
->crossfs
= crossfs
;
914 ctx
->fakeatimes
= fakediratimes
;
916 ctx
->last_output_update
= time(NULL
);
918 /* progress==1 means report progress only if stderr is a tty */
920 progress
= isatty(2) ?
2 : 0;
921 ctx
->progress
= progress
;
924 if (progress
&& ioctl(2, TIOCGWINSZ
, &ws
) == 0)
925 ctx
->progwidth
= ws
.ws_col
- 1;
930 if (mode
== SCANDUMP
)
931 printf(DUMPHDR
"%02x\n", (unsigned char)pathsep
);
934 * Scan the directory tree, and write out the trie component
937 if (mode
!= SCANDUMP
) {
938 ctx
->tb
= triebuild_new(fd
);
943 while ((buf
= fgetline(stdin
)) != NULL
) {
947 buf
[strcspn(buf
, "\r\n")] = '\0';
951 while (*p
&& *p
!= ' ') p
++;
953 fprintf(stderr
, "%s: dump file line %d: expected at least"
954 " three fields\n", PNAME
, line
);
958 tf
.size
= strtoull(q
, NULL
, 10);
960 while (*p
&& *p
!= ' ') p
++;
962 fprintf(stderr
, "%s: dump file line %d: expected at least"
963 " three fields\n", PNAME
, line
);
967 tf
.atime
= strtoull(q
, NULL
, 10);
975 for (i
= 0; i
< 2; i
++) {
977 if (*p
>= '0' && *p
<= '9')
979 else if (*p
>= 'A' && *p
<= 'F')
980 c
+= *p
- ('A' - 10);
981 else if (*p
>= 'a' && *p
<= 'f')
982 c
+= *p
- ('a' - 10);
984 fprintf(stderr
, "%s: dump file line %d: unable"
985 " to parse hex escape\n", PNAME
, line
);
994 triebuild_add(ctx
->tb
, buf
, &tf
);
999 du(scandir
, gotdata
, ctx
);
1001 if (mode
!= SCANDUMP
) {
1002 count
= triebuild_finish(ctx
->tb
);
1003 triebuild_free(ctx
->tb
);
1005 if (ctx
->progress
) {
1006 fprintf(stderr
, "%-*s\r", ctx
->progwidth
, "");
1011 * Work out how much space the cumulative index trees
1012 * will take; enlarge the file, and memory-map it.
1014 if (fstat(fd
, &st
) < 0) {
1015 perror(PNAME
": fstat");
1019 printf("Built pathname index, %d entries, %llu bytes\n", count
,
1020 (unsigned long long)st
.st_size
);
1022 totalsize
= index_compute_size(st
.st_size
, count
);
1024 if (lseek(fd
, totalsize
-1, SEEK_SET
) < 0) {
1025 perror(PNAME
": lseek");
1028 if (write(fd
, "\0", 1) < 1) {
1029 perror(PNAME
": write");
1033 printf("Upper bound on index file size = %llu bytes\n",
1034 (unsigned long long)totalsize
);
1036 mappedfile
= mmap(NULL
, totalsize
, PROT_READ
|PROT_WRITE
,MAP_SHARED
, fd
, 0);
1038 perror(PNAME
": mmap");
1042 if (fakediratimes
) {
1043 printf("Faking directory atimes\n");
1044 trie_fake_dir_atimes(mappedfile
);
1047 printf("Building index\n");
1048 ib
= indexbuild_new(mappedfile
, st
.st_size
, count
);
1049 tw
= triewalk_new(mappedfile
);
1050 while ((tf
= triewalk_next(tw
, NULL
)) != NULL
)
1051 indexbuild_add(ib
, tf
);
1053 realsize
= indexbuild_realsize(ib
);
1054 indexbuild_free(ib
);
1056 munmap(mappedfile
, totalsize
);
1057 ftruncate(fd
, realsize
);
1059 printf("Actual index file size = %llu bytes\n",
1060 (unsigned long long)realsize
);
1062 } else if (mode
== TEXT
) {
1063 char *querydir
= actions
[action
].arg
;
1066 fd
= open(filename
, O_RDONLY
);
1068 fprintf(stderr
, "%s: %s: open: %s\n", PNAME
, filename
,
1072 if (fstat(fd
, &st
) < 0) {
1073 perror(PNAME
": fstat");
1076 totalsize
= st
.st_size
;
1077 mappedfile
= mmap(NULL
, totalsize
, PROT_READ
, MAP_SHARED
, fd
, 0);
1079 perror(PNAME
": mmap");
1082 pathsep
= trie_pathsep(mappedfile
);
1085 * Trim trailing slash, just in case.
1087 pathlen
= strlen(querydir
);
1088 if (pathlen
> 0 && querydir
[pathlen
-1] == pathsep
)
1089 querydir
[--pathlen
] = '\0';
1091 text_query(mappedfile
, querydir
, textcutoff
, tqdepth
);
1092 } else if (mode
== HTML
) {
1093 char *querydir
= actions
[action
].arg
;
1095 struct html_config cfg
;
1099 fd
= open(filename
, O_RDONLY
);
1101 fprintf(stderr
, "%s: %s: open: %s\n", PNAME
, filename
,
1105 if (fstat(fd
, &st
) < 0) {
1106 perror(PNAME
": fstat");
1109 totalsize
= st
.st_size
;
1110 mappedfile
= mmap(NULL
, totalsize
, PROT_READ
, MAP_SHARED
, fd
, 0);
1112 perror(PNAME
": mmap");
1115 pathsep
= trie_pathsep(mappedfile
);
1118 * Trim trailing slash, just in case.
1120 pathlen
= strlen(querydir
);
1121 if (pathlen
> 0 && querydir
[pathlen
-1] == pathsep
)
1122 querydir
[--pathlen
] = '\0';
1124 xi
= trie_before(mappedfile
, querydir
);
1126 cfg
.autoage
= htmlautoagerange
;
1127 cfg
.oldest
= htmloldest
;
1128 cfg
.newest
= htmlnewest
;
1129 html
= html_query(mappedfile
, xi
, &cfg
);
1130 fputs(html
, stdout
);
1131 } else if (mode
== DUMP
) {
1135 fd
= open(filename
, O_RDONLY
);
1137 fprintf(stderr
, "%s: %s: open: %s\n", PNAME
, filename
,
1141 if (fstat(fd
, &st
) < 0) {
1142 perror(PNAME
": fstat");
1145 totalsize
= st
.st_size
;
1146 mappedfile
= mmap(NULL
, totalsize
, PROT_READ
, MAP_SHARED
, fd
, 0);
1148 perror(PNAME
": mmap");
1151 pathsep
= trie_pathsep(mappedfile
);
1153 maxpathlen
= trie_maxpathlen(mappedfile
);
1154 buf
= snewn(maxpathlen
, char);
1156 printf(DUMPHDR
"%02x\n", (unsigned char)pathsep
);
1157 tw
= triewalk_new(mappedfile
);
1158 while ((tf
= triewalk_next(tw
, buf
)) != NULL
)
1161 } else if (mode
== HTTPD
) {
1162 struct html_config pcfg
;
1163 struct httpd_config dcfg
;
1165 fd
= open(filename
, O_RDONLY
);
1167 fprintf(stderr
, "%s: %s: open: %s\n", PNAME
, filename
,
1171 if (fstat(fd
, &st
) < 0) {
1172 perror(PNAME
": fstat");
1175 totalsize
= st
.st_size
;
1176 mappedfile
= mmap(NULL
, totalsize
, PROT_READ
, MAP_SHARED
, fd
, 0);
1178 perror(PNAME
": mmap");
1181 pathsep
= trie_pathsep(mappedfile
);
1183 dcfg
.address
= httpserveraddr
;
1184 dcfg
.port
= httpserverport
;
1185 dcfg
.basicauthdata
= httpauthdata
;
1187 pcfg
.autoage
= htmlautoagerange
;
1188 pcfg
.oldest
= htmloldest
;
1189 pcfg
.newest
= htmlnewest
;
1190 run_httpd(mappedfile
, auth
, &dcfg
, &pcfg
);
1191 } else if (mode
== REMOVE
) {
1192 if (remove(filename
) < 0) {
1193 fprintf(stderr
, "%s: %s: remove: %s\n", PNAME
, filename
,