2 * Main program for agedu.
16 #include <sys/types.h>
20 #include <sys/ioctl.h>
33 * Path separator. This global variable affects the behaviour of
34 * various parts of the code when they need to deal with path
35 * separators. The path separator appropriate to a particular data
36 * set is encoded in the index file storing that data set; data
37 * sets generated on Unix will of course have the default '/', but
38 * foreign data sets are conceivable and must be handled correctly.
42 void fatal(const char *fmt
, ...)
45 fprintf(stderr
, "%s: ", PNAME
);
47 vfprintf(stderr
, fmt
, ap
);
49 fprintf(stderr
, "\n");
53 struct inclusion_exclusion
{
61 dev_t datafile_dev
, filesystem_dev
;
63 time_t last_output_update
;
64 int progress
, progwidth
;
66 struct inclusion_exclusion
*inex
;
72 static void dump_line(const char *pathname
, const struct trie_file
*tf
)
75 printf("%llu %llu ", tf
->size
, tf
->atime
);
76 for (p
= pathname
; *p
; p
++) {
77 if (*p
>= ' ' && *p
< 127 && *p
!= '%')
80 printf("%%%02x", (unsigned char)*p
);
85 static int gotdata(void *vctx
, const char *pathname
, const struct stat64
*st
)
87 struct ctx
*ctx
= (struct ctx
*)vctx
;
88 struct trie_file file
;
94 * Filter out our own data file.
96 if (st
->st_dev
== ctx
->datafile_dev
&& st
->st_ino
== ctx
->datafile_ino
)
100 * Don't cross the streams^W^Wany file system boundary.
102 if (!ctx
->crossfs
&& st
->st_dev
!= ctx
->filesystem_dev
)
105 file
.size
= (unsigned long long)512 * st
->st_blocks
;
106 if (ctx
->fakeatimes
&& S_ISDIR(st
->st_mode
))
107 file
.atime
= st
->st_mtime
;
109 file
.atime
= st
->st_atime
;
112 * Filter based on wildcards.
115 filename
= strrchr(pathname
, pathsep
);
120 for (i
= 0; i
< ctx
->ninex
; i
++) {
121 if (fnmatch(ctx
->inex
[i
].wildcard
,
122 ctx
->inex
[i
].path ? pathname
: filename
, 0) == 0)
123 include
= ctx
->inex
[i
].type
;
126 return 0; /* ignore this entry and any subdirs */
129 * Here we are supposed to be filtering an entry out, but
130 * still recursing into it if it's a directory. However,
131 * we can't actually leave out any directory whose
132 * subdirectories we then look at. So we cheat, in that
133 * case, by setting the size to zero.
135 if (!S_ISDIR(st
->st_mode
))
136 return 0; /* just ignore */
141 if (ctx
->straight_to_dump
)
142 dump_line(pathname
, &file
);
144 triebuild_add(ctx
->tb
, pathname
, &file
);
148 if (t
!= ctx
->last_output_update
) {
149 fprintf(stderr
, "%-*.*s\r", ctx
->progwidth
, ctx
->progwidth
,
152 ctx
->last_output_update
= t
;
159 static void text_query(const void *mappedfile
, const char *querydir
,
164 unsigned long xi1
, xi2
;
165 unsigned long long s1
, s2
;
167 maxpathlen
= trie_maxpathlen(mappedfile
);
168 pathbuf
= snewn(maxpathlen
+ 1, char);
171 * We want to query everything between the supplied filename
172 * (inclusive) and that filename with a ^A on the end
173 * (exclusive). So find the x indices for each.
175 strcpy(pathbuf
, querydir
);
176 make_successor(pathbuf
);
177 xi1
= trie_before(mappedfile
, querydir
);
178 xi2
= trie_before(mappedfile
, pathbuf
);
181 return; /* file, or empty dir => no display */
184 * Now do the lookups in the age index.
186 s1
= index_query(mappedfile
, xi1
, t
);
187 s2
= index_query(mappedfile
, xi2
, t
);
190 return; /* no space taken up => no display */
194 * Now scan for first-level subdirectories and report
199 trie_getpath(mappedfile
, xi1
, pathbuf
);
200 text_query(mappedfile
, pathbuf
, t
, depth
-1);
201 make_successor(pathbuf
);
202 xi1
= trie_before(mappedfile
, pathbuf
);
206 /* Display in units of 1Kb */
207 printf("%-11llu %s\n", (s2
- s1
) / 1024, querydir
);
211 * Largely frivolous way to define all my command-line options. I
212 * present here a parametric macro which declares a series of
213 * _logical_ option identifiers, and for each one declares zero or
214 * more short option characters and zero or more long option
215 * words. Then I repeatedly invoke that macro with its arguments
216 * defined to be various other macros, which allows me to
219 * - define an enum allocating a distinct integer value to each
221 * - define a string consisting of precisely all the short option
223 * - define a string array consisting of all the long option
225 * - define (with help from auxiliary enums) integer arrays
226 * parallel to both of the above giving the logical option id
227 * for each physical short and long option
228 * - define an array indexed by logical option id indicating
229 * whether the option in question takes a value
230 * - define a function which prints out brief online help for all
233 * It's not at all clear to me that this trickery is actually
234 * particularly _efficient_ - it still, after all, requires going
235 * linearly through the option list at run time and doing a
236 * strcmp, whereas in an ideal world I'd have liked the lists of
237 * long and short options to be pre-sorted so that a binary search
238 * or some other more efficient lookup was possible. (Not that
239 * asymptotic algorithmic complexity is remotely vital in option
240 * parsing, but if I were doing this in, say, Lisp or something
241 * with an equivalently powerful preprocessor then once I'd had
242 * the idea of preparing the option-parsing data structures at
243 * compile time I would probably have made the effort to prepare
244 * them _properly_. I could have Perl generate me a source file
245 * from some sort of description, I suppose, but that would seem
246 * like overkill. And in any case, it's more of a challenge to
247 * achieve as much as possible by cunning use of cpp and enum than
248 * to just write some sensible and logical code in a Turing-
249 * complete language. I said it was largely frivolous :-)
251 * This approach does have the virtue that it brings together the
252 * option ids, option spellings and help text into a single
253 * combined list and defines them all in exactly one place. If I
254 * want to add a new option, or a new spelling for an option, I
255 * only have to modify the main OPTHELP macro below and then add
256 * code to process the new logical id.
258 * (Though, really, even that isn't ideal, since it still involves
259 * modifying the source file in more than one place. In a
260 * _properly_ ideal world, I'd be able to interleave the option
261 * definitions with the code fragments that process them. And then
262 * not bother defining logical identifiers for them at all - those
263 * would be automatically generated, since I wouldn't have any
264 * need to specify them manually in another part of the code.)
266 * One other helpful consequence of the enum-based structure here
267 * is that it causes a compiler error if I accidentally try to
268 * define the same option (short or long) twice.
271 #define OPTHELP(NOVAL, VAL, SHORT, LONG, HELPPFX, HELPARG, HELPLINE, HELPOPT) \
272 HELPPFX("usage") HELPLINE(PNAME " [options] action [action...]") \
274 VAL(SCAN) SHORT(s) LONG(scan) \
275 HELPARG("directory") HELPOPT("scan and index a directory") \
276 NOVAL(DUMP) SHORT(D) LONG(dump) HELPOPT("dump the index file on stdout") \
277 VAL(SCANDUMP) SHORT(S) LONG(scan_dump) \
278 HELPARG("directory") HELPOPT("scan only, generating a dump") \
279 NOVAL(LOAD) SHORT(L) LONG(load) \
280 HELPOPT("load and index a dump file") \
281 VAL(TEXT) SHORT(t) LONG(text) \
282 HELPARG("subdir") HELPOPT("print a plain text report on a subdirectory") \
283 VAL(HTML) SHORT(H) LONG(html) \
284 HELPARG("subdir") HELPOPT("print an HTML report on a subdirectory") \
285 NOVAL(HTTPD) SHORT(w) LONG(web) LONG(server) LONG(httpd) \
286 HELPOPT("serve HTML reports from a temporary web server") \
287 NOVAL(REMOVE) SHORT(R) LONG(remove) LONG(delete) LONG(unlink) \
288 HELPOPT("remove the index file") \
290 VAL(DATAFILE) SHORT(f) LONG(file) \
291 HELPARG("filename") HELPOPT("[most modes] specify index file") \
292 NOVAL(PROGRESS) LONG(progress) LONG(scan_progress) \
293 HELPOPT("[--scan] report progress on stderr") \
294 NOVAL(NOPROGRESS) LONG(no_progress) LONG(no_scan_progress) \
295 HELPOPT("[--scan] do not report progress") \
296 NOVAL(TTYPROGRESS) LONG(tty_progress) LONG(tty_scan_progress) \
297 LONG(progress_tty) LONG(scan_progress_tty) \
298 HELPOPT("[--scan] report progress if stderr is a tty") \
299 NOVAL(CROSSFS) LONG(cross_fs) \
300 HELPOPT("[--scan] cross filesystem boundaries") \
301 NOVAL(NOCROSSFS) LONG(no_cross_fs) \
302 HELPOPT("[--scan] stick to one filesystem") \
303 VAL(INCLUDE) LONG(include) \
304 HELPARG("wildcard") HELPOPT("[--scan] include files matching pattern") \
305 VAL(INCLUDEPATH) LONG(include_path) \
306 HELPARG("wildcard") HELPOPT("[--scan] include pathnames matching pattern") \
307 VAL(EXCLUDE) LONG(exclude) \
308 HELPARG("wildcard") HELPOPT("[--scan] exclude files matching pattern") \
309 VAL(EXCLUDEPATH) LONG(exclude_path) \
310 HELPARG("wildcard") HELPOPT("[--scan] exclude pathnames matching pattern") \
311 VAL(PRUNE) LONG(prune) \
312 HELPARG("wildcard") HELPOPT("[--scan] prune files matching pattern") \
313 VAL(PRUNEPATH) LONG(prune_path) \
314 HELPARG("wildcard") HELPOPT("[--scan] prune pathnames matching pattern") \
315 NOVAL(DIRATIME) LONG(dir_atime) LONG(dir_atimes) \
316 HELPOPT("[--scan] keep real atimes on directories") \
317 NOVAL(NODIRATIME) LONG(no_dir_atime) LONG(no_dir_atimes) \
318 HELPOPT("[--scan] fake atimes on directories") \
319 VAL(TQDEPTH) SHORT(d) LONG(depth) LONG(max_depth) LONG(maximum_depth) \
320 HELPARG("levels") HELPOPT("[--text] recurse to this many levels") \
321 VAL(MINAGE) SHORT(a) LONG(age) LONG(min_age) LONG(minimum_age) \
322 HELPARG("age") HELPOPT("[--text] include only files older than this") \
323 VAL(AGERANGE) SHORT(r) LONG(age_range) LONG(range) LONG(ages) \
324 HELPARG("age[-age]") HELPOPT("[--html,--web] set limits of colour coding") \
325 VAL(SERVERADDR) LONG(address) LONG(addr) LONG(server_address) \
327 HELPARG("addr[:port]") HELPOPT("[--web] specify HTTP server address") \
328 VAL(AUTH) LONG(auth) LONG(http_auth) LONG(httpd_auth) \
329 LONG(server_auth) LONG(web_auth) \
330 HELPARG("type") HELPOPT("[--web] specify HTTP authentication method") \
331 VAL(AUTHFILE) LONG(auth_file) \
332 HELPARG("filename") HELPOPT("[--web] read HTTP Basic user/pass from file") \
333 VAL(AUTHFD) LONG(auth_fd) \
334 HELPARG("fd") HELPOPT("[--web] read HTTP Basic user/pass from fd") \
336 NOVAL(HELP) SHORT(h) LONG(help) HELPOPT("display this help text") \
337 NOVAL(VERSION) SHORT(V) LONG(version) HELPOPT("report version number") \
338 NOVAL(LICENCE) LONG(licence) LONG(license) \
339 HELPOPT("display (MIT) licence text") \
342 #define DEFENUM(x) OPT_ ## x,
345 #define STRING(x) #x ,
346 #define STRINGNOCOMMA(x) #x
347 #define SHORTNEWOPT(x) SHORTtmp_ ## x = OPT_ ## x,
348 #define SHORTTHISOPT(x) SHORTtmp2_ ## x, SHORTVAL_ ## x = SHORTtmp2_ ## x - 1,
349 #define SHORTOPTVAL(x) SHORTVAL_ ## x,
350 #define SHORTTMP(x) SHORTtmp3_ ## x,
351 #define LONGNEWOPT(x) LONGtmp_ ## x = OPT_ ## x,
352 #define LONGTHISOPT(x) LONGtmp2_ ## x, LONGVAL_ ## x = LONGtmp2_ ## x - 1,
353 #define LONGOPTVAL(x) LONGVAL_ ## x,
354 #define LONGTMP(x) SHORTtmp3_ ## x,
356 #define OPTIONS(NOVAL, VAL, SHORT, LONG) \
357 OPTHELP(NOVAL, VAL, SHORT, LONG, IGNORE, IGNORE, IGNORE, IGNORE)
359 enum { OPTIONS(DEFENUM
,DEFENUM
,IGNORE
,IGNORE
) NOPTIONS
};
360 enum { OPTIONS(IGNORE
,IGNORE
,SHORTTMP
,IGNORE
) NSHORTOPTS
};
361 enum { OPTIONS(IGNORE
,IGNORE
,IGNORE
,LONGTMP
) NLONGOPTS
};
362 static const int opthasval
[NOPTIONS
] = {OPTIONS(ZERO
,ONE
,IGNORE
,IGNORE
)};
363 static const char shortopts
[] = {OPTIONS(IGNORE
,IGNORE
,STRINGNOCOMMA
,IGNORE
)};
364 static const char *const longopts
[] = {OPTIONS(IGNORE
,IGNORE
,IGNORE
,STRING
)};
365 enum { OPTIONS(SHORTNEWOPT
,SHORTNEWOPT
,SHORTTHISOPT
,IGNORE
) };
366 enum { OPTIONS(LONGNEWOPT
,LONGNEWOPT
,IGNORE
,LONGTHISOPT
) };
367 static const int shortvals
[] = {OPTIONS(IGNORE
,IGNORE
,SHORTOPTVAL
,IGNORE
)};
368 static const int longvals
[] = {OPTIONS(IGNORE
,IGNORE
,IGNORE
,LONGOPTVAL
)};
370 static void usage(FILE *fp
)
373 const char *prefix
, *shortopt
, *longopt
, *optarg
;
376 #define HELPRESET prefix = shortopt = longopt = optarg = NULL, optex = -1
377 #define HELPNOVAL(s) optex = 0;
378 #define HELPVAL(s) optex = 1;
379 #define HELPSHORT(s) if (!shortopt) shortopt = "-" #s;
380 #define HELPLONG(s) if (!longopt) { \
381 strcpy(longbuf, "--" #s); longopt = longbuf; \
382 for (i = 0; longbuf[i]; i++) if (longbuf[i] == '_') longbuf[i] = '-'; }
383 #define HELPPFX(s) prefix = s;
384 #define HELPARG(s) optarg = s;
385 #define HELPLINE(s) assert(optex == -1); \
386 fprintf(fp, "%7s%c %s\n", prefix?prefix:"", prefix?':':' ', s); \
388 #define HELPOPT(s) assert((optex == 1 && optarg) || (optex == 0 && !optarg)); \
389 assert(shortopt || longopt); \
390 i = fprintf(fp, "%7s%c %s%s%s%s%s", prefix?prefix:"", prefix?':':' ', \
391 shortopt?shortopt:"", shortopt&&longopt?", ":"", longopt?longopt:"", \
392 optarg?" ":"", optarg?optarg:""); \
393 fprintf(fp, "%*s %s\n", i<32?32-i:0,"",s); HELPRESET;
396 OPTHELP(HELPNOVAL
, HELPVAL
, HELPSHORT
, HELPLONG
,
397 HELPPFX
, HELPARG
, HELPLINE
, HELPOPT
);
410 static time_t parse_age(time_t now
, const char *agestr
)
419 if (2 != sscanf(agestr
, "%d%1[DdWwMmYy]", &nunits
, unit
)) {
420 fprintf(stderr
, "%s: age specification should be a number followed by"
421 " one of d,w,m,y\n", PNAME
);
425 if (unit
[0] == 'd') {
427 } else if (unit
[0] == 'w') {
428 t
-= 86400 * 7 * nunits
;
433 ym
= tm
.tm_year
* 12 + tm
.tm_mon
;
440 tm
.tm_year
= ym
/ 12;
449 int main(int argc
, char **argv
)
452 struct ctx actx
, *ctx
= &actx
;
454 off_t totalsize
, realsize
;
458 const struct trie_file
*tf
;
459 char *filename
= PNAME
".dat";
461 enum { TEXT
, HTML
, SCAN
, DUMP
, SCANDUMP
, LOAD
, HTTPD
, REMOVE
};
466 int nactions
= 0, actionsize
= 0, action
;
467 time_t now
= time(NULL
);
468 time_t textcutoff
= now
, htmlnewest
= now
, htmloldest
= now
;
469 int htmlautoagerange
= 1;
470 const char *httpserveraddr
= NULL
;
471 int httpserverport
= 0;
472 const char *httpauthdata
= NULL
;
473 int auth
= HTTPD_AUTH_MAGIC
| HTTPD_AUTH_BASIC
;
475 struct inclusion_exclusion
*inex
= NULL
;
476 int ninex
= 0, inexsize
= 0;
479 int fakediratimes
= 1;
481 #ifdef DEBUG_MAD_OPTION_PARSING_MACROS
483 static const char *const optnames
[NOPTIONS
] = {
484 OPTIONS(STRING
,STRING
,IGNORE
,IGNORE
)
487 for (i
= 0; i
< NSHORTOPTS
; i
++)
488 printf("-%c == %s [%s]\n", shortopts
[i
], optnames
[shortvals
[i
]],
489 opthasval
[shortvals
[i
]] ?
"value" : "no value");
490 for (i
= 0; i
< NLONGOPTS
; i
++)
491 printf("--%s == %s [%s]\n", longopts
[i
], optnames
[longvals
[i
]],
492 opthasval
[longvals
[i
]] ?
"value" : "no value");
499 if (doing_opts
&& *p
== '-') {
502 if (!strcmp(p
, "--")) {
513 if (wordstart
&& *p
== '-') {
515 * GNU-style long option.
518 optval
= strchr(p
, '=');
522 for (i
= 0; i
< NLONGOPTS
; i
++) {
523 const char *opt
= longopts
[i
], *s
= p
;
526 * The underscores in the option names
527 * defined above may be given by the user
528 * as underscores or dashes, or omitted
533 if (*s
== '-' || *s
== '_')
551 fprintf(stderr
, "%s: unrecognised option '--%s'\n",
556 if (!opthasval
[optid
]) {
558 fprintf(stderr
, "%s: unexpected argument to option"
559 " '--%s'\n", PNAME
, p
);
567 fprintf(stderr
, "%s: option '--%s' expects"
568 " an argument\n", PNAME
, p
);
574 p
+= strlen(p
); /* finished with this argument word */
581 for (i
= 0; i
< NSHORTOPTS
; i
++)
582 if (c
== shortopts
[i
]) {
583 optid
= shortvals
[i
];
588 fprintf(stderr
, "%s: unrecognised option '-%c'\n",
593 if (opthasval
[optid
]) {
597 } else if (--argc
> 0) {
600 fprintf(stderr
, "%s: option '-%c' expects"
601 " an argument\n", PNAME
, c
);
612 * Now actually process the option.
619 printf("FIXME: version();\n");
623 extern const char *const licence
[];
626 for (i
= 0; licence
[i
]; i
++)
627 fputs(licence
[i
], stdout
);
633 if (nactions
>= actionsize
) {
634 actionsize
= nactions
* 3 / 2 + 16;
635 actions
= sresize(actions
, actionsize
, struct action
);
637 actions
[nactions
].mode
= SCAN
;
638 actions
[nactions
].arg
= optval
;
642 if (nactions
>= actionsize
) {
643 actionsize
= nactions
* 3 / 2 + 16;
644 actions
= sresize(actions
, actionsize
, struct action
);
646 actions
[nactions
].mode
= SCANDUMP
;
647 actions
[nactions
].arg
= optval
;
651 if (nactions
>= actionsize
) {
652 actionsize
= nactions
* 3 / 2 + 16;
653 actions
= sresize(actions
, actionsize
, struct action
);
655 actions
[nactions
].mode
= DUMP
;
656 actions
[nactions
].arg
= NULL
;
660 if (nactions
>= actionsize
) {
661 actionsize
= nactions
* 3 / 2 + 16;
662 actions
= sresize(actions
, actionsize
, struct action
);
664 actions
[nactions
].mode
= LOAD
;
665 actions
[nactions
].arg
= NULL
;
669 if (nactions
>= actionsize
) {
670 actionsize
= nactions
* 3 / 2 + 16;
671 actions
= sresize(actions
, actionsize
, struct action
);
673 actions
[nactions
].mode
= TEXT
;
674 actions
[nactions
].arg
= optval
;
678 if (nactions
>= actionsize
) {
679 actionsize
= nactions
* 3 / 2 + 16;
680 actions
= sresize(actions
, actionsize
, struct action
);
682 actions
[nactions
].mode
= HTML
;
683 actions
[nactions
].arg
= optval
;
687 if (nactions
>= actionsize
) {
688 actionsize
= nactions
* 3 / 2 + 16;
689 actions
= sresize(actions
, actionsize
, struct action
);
691 actions
[nactions
].mode
= HTTPD
;
692 actions
[nactions
].arg
= NULL
;
696 if (nactions
>= actionsize
) {
697 actionsize
= nactions
* 3 / 2 + 16;
698 actions
= sresize(actions
, actionsize
, struct action
);
700 actions
[nactions
].mode
= REMOVE
;
701 actions
[nactions
].arg
= NULL
;
710 case OPT_TTYPROGRESS
:
729 tqdepth
= atoi(optval
);
732 textcutoff
= parse_age(now
, optval
);
735 if (!strcmp(optval
, "auto")) {
736 htmlautoagerange
= 1;
738 char *q
= optval
+ strcspn(optval
, "-:");
741 htmloldest
= parse_age(now
, optval
);
742 htmlnewest
= *q ?
parse_age(now
, q
) : now
;
743 htmlautoagerange
= 0;
749 if (optval
[0] == '[' &&
750 (port
= strchr(optval
, ']')) != NULL
)
754 port
+= strcspn(port
, ":");
757 httpserveraddr
= optval
;
758 httpserverport
= atoi(port
);
762 if (!strcmp(optval
, "magic"))
763 auth
= HTTPD_AUTH_MAGIC
;
764 else if (!strcmp(optval
, "basic"))
765 auth
= HTTPD_AUTH_BASIC
;
766 else if (!strcmp(optval
, "none"))
767 auth
= HTTPD_AUTH_NONE
;
768 else if (!strcmp(optval
, "default"))
769 auth
= HTTPD_AUTH_MAGIC
| HTTPD_AUTH_BASIC
;
770 else if (!strcmp(optval
, "help") ||
771 !strcmp(optval
, "list")) {
772 printf(PNAME
": supported HTTP authentication types"
774 " magic use Linux /proc/net/tcp to"
775 " determine owner of peer socket\n"
776 " basic HTTP Basic username and"
777 " password authentication\n"
778 " default use 'magic' if possible, "
779 " otherwise fall back to 'basic'\n"
780 " none unauthenticated HTTP (if"
781 " the data file is non-confidential)\n");
784 fprintf(stderr
, "%s: unrecognised authentication"
785 " type '%s'\n%*s options are 'magic',"
786 " 'basic', 'none', 'default'\n",
787 PNAME
, optval
, (int)strlen(PNAME
), "");
798 int authlen
, authsize
;
801 if (optid
== OPT_AUTHFILE
) {
802 fd
= open(optval
, O_RDONLY
);
804 fprintf(stderr
, "%s: %s: open: %s\n", PNAME
,
805 optval
, strerror(errno
));
812 sprintf(namebuf
, "fd %d", fd
);
817 authbuf
= snewn(authsize
, char);
818 while ((ret
= read(fd
, authbuf
+authlen
,
819 authsize
-authlen
)) > 0) {
821 if ((authsize
- authlen
) < (authsize
/ 16)) {
822 authsize
= authlen
* 3 / 2 + 4096;
823 authbuf
= sresize(authbuf
, authsize
, char);
827 fprintf(stderr
, "%s: %s: read: %s\n", PNAME
,
828 name
, strerror(errno
));
831 if (optid
== OPT_AUTHFILE
)
833 httpauthdata
= authbuf
;
837 case OPT_INCLUDEPATH
:
839 case OPT_EXCLUDEPATH
:
842 if (ninex
>= inexsize
) {
843 inexsize
= ninex
* 3 / 2 + 16;
844 inex
= sresize(inex
, inexsize
,
845 struct inclusion_exclusion
);
847 inex
[ninex
].path
= (optid
== OPT_INCLUDEPATH
||
848 optid
== OPT_EXCLUDEPATH
||
849 optid
== OPT_PRUNEPATH
);
850 inex
[ninex
].type
= (optid
== OPT_INCLUDE ?
1 :
851 optid
== OPT_INCLUDEPATH ?
1 :
852 optid
== OPT_EXCLUDE ?
0 :
853 optid
== OPT_EXCLUDEPATH ?
0 :
854 optid
== OPT_PRUNE ?
-1 :
855 /* optid == OPT_PRUNEPATH ? */ -1);
856 inex
[ninex
].wildcard
= optval
;
862 fprintf(stderr
, "%s: unexpected argument '%s'\n", PNAME
, p
);
872 for (action
= 0; action
< nactions
; action
++) {
873 int mode
= actions
[action
].mode
;
875 if (mode
== SCAN
|| mode
== SCANDUMP
|| mode
== LOAD
) {
876 const char *scandir
= actions
[action
].arg
;
878 char *buf
= fgetline(stdin
);
880 buf
[strcspn(buf
, "\r\n")] = '\0';
881 if (1 != sscanf(buf
, DUMPHDR
"%x",
883 fprintf(stderr
, "%s: header in dump file not recognised\n",
887 pathsep
= (char)newpathsep
;
891 if (mode
== SCAN
|| mode
== LOAD
) {
893 * Prepare to write out the index file.
895 fd
= open(filename
, O_RDWR
| O_TRUNC
| O_CREAT
, S_IRWXU
);
897 fprintf(stderr
, "%s: %s: open: %s\n", PNAME
, filename
,
901 if (fstat(fd
, &st
) < 0) {
902 perror(PNAME
": fstat");
905 ctx
->datafile_dev
= st
.st_dev
;
906 ctx
->datafile_ino
= st
.st_ino
;
907 ctx
->straight_to_dump
= 0;
909 ctx
->datafile_dev
= -1;
910 ctx
->datafile_ino
= -1;
911 ctx
->straight_to_dump
= 1;
914 if (mode
== SCAN
|| mode
== SCANDUMP
) {
915 if (stat(scandir
, &st
) < 0) {
916 fprintf(stderr
, "%s: %s: stat: %s\n", PNAME
, scandir
,
920 ctx
->filesystem_dev
= crossfs ?
0 : st
.st_dev
;
925 ctx
->crossfs
= crossfs
;
926 ctx
->fakeatimes
= fakediratimes
;
928 ctx
->last_output_update
= time(NULL
);
930 /* progress==1 means report progress only if stderr is a tty */
932 progress
= isatty(2) ?
2 : 0;
933 ctx
->progress
= progress
;
936 if (progress
&& ioctl(2, TIOCGWINSZ
, &ws
) == 0)
937 ctx
->progwidth
= ws
.ws_col
- 1;
942 if (mode
== SCANDUMP
)
943 printf(DUMPHDR
"%02x\n", (unsigned char)pathsep
);
946 * Scan the directory tree, and write out the trie component
949 if (mode
!= SCANDUMP
) {
950 ctx
->tb
= triebuild_new(fd
);
955 while ((buf
= fgetline(stdin
)) != NULL
) {
959 buf
[strcspn(buf
, "\r\n")] = '\0';
963 while (*p
&& *p
!= ' ') p
++;
965 fprintf(stderr
, "%s: dump file line %d: expected at least"
966 " three fields\n", PNAME
, line
);
970 tf
.size
= strtoull(q
, NULL
, 10);
972 while (*p
&& *p
!= ' ') p
++;
974 fprintf(stderr
, "%s: dump file line %d: expected at least"
975 " three fields\n", PNAME
, line
);
979 tf
.atime
= strtoull(q
, NULL
, 10);
987 for (i
= 0; i
< 2; i
++) {
989 if (*p
>= '0' && *p
<= '9')
991 else if (*p
>= 'A' && *p
<= 'F')
992 c
+= *p
- ('A' - 10);
993 else if (*p
>= 'a' && *p
<= 'f')
994 c
+= *p
- ('a' - 10);
996 fprintf(stderr
, "%s: dump file line %d: unable"
997 " to parse hex escape\n", PNAME
, line
);
1006 triebuild_add(ctx
->tb
, buf
, &tf
);
1011 du(scandir
, gotdata
, ctx
);
1013 if (mode
!= SCANDUMP
) {
1014 count
= triebuild_finish(ctx
->tb
);
1015 triebuild_free(ctx
->tb
);
1017 if (ctx
->progress
) {
1018 fprintf(stderr
, "%-*s\r", ctx
->progwidth
, "");
1023 * Work out how much space the cumulative index trees
1024 * will take; enlarge the file, and memory-map it.
1026 if (fstat(fd
, &st
) < 0) {
1027 perror(PNAME
": fstat");
1031 printf("Built pathname index, %d entries, %ju bytes\n", count
,
1032 (intmax_t)st
.st_size
);
1034 totalsize
= index_compute_size(st
.st_size
, count
);
1036 if (lseek(fd
, totalsize
-1, SEEK_SET
) < 0) {
1037 perror(PNAME
": lseek");
1040 if (write(fd
, "\0", 1) < 1) {
1041 perror(PNAME
": write");
1045 printf("Upper bound on index file size = %ju bytes\n",
1046 (intmax_t)totalsize
);
1048 mappedfile
= mmap(NULL
, totalsize
, PROT_READ
|PROT_WRITE
,MAP_SHARED
, fd
, 0);
1050 perror(PNAME
": mmap");
1054 if (fakediratimes
) {
1055 printf("Faking directory atimes\n");
1056 trie_fake_dir_atimes(mappedfile
);
1059 printf("Building index\n");
1060 ib
= indexbuild_new(mappedfile
, st
.st_size
, count
);
1061 tw
= triewalk_new(mappedfile
);
1062 while ((tf
= triewalk_next(tw
, NULL
)) != NULL
)
1063 indexbuild_add(ib
, tf
);
1065 realsize
= indexbuild_realsize(ib
);
1066 indexbuild_free(ib
);
1068 munmap(mappedfile
, totalsize
);
1069 ftruncate(fd
, realsize
);
1071 printf("Actual index file size = %ju bytes\n", (intmax_t)realsize
);
1073 } else if (mode
== TEXT
) {
1074 char *querydir
= actions
[action
].arg
;
1077 fd
= open(filename
, O_RDONLY
);
1079 fprintf(stderr
, "%s: %s: open: %s\n", PNAME
, filename
,
1083 if (fstat(fd
, &st
) < 0) {
1084 perror(PNAME
": fstat");
1087 totalsize
= st
.st_size
;
1088 mappedfile
= mmap(NULL
, totalsize
, PROT_READ
, MAP_SHARED
, fd
, 0);
1090 perror(PNAME
": mmap");
1093 pathsep
= trie_pathsep(mappedfile
);
1096 * Trim trailing slash, just in case.
1098 pathlen
= strlen(querydir
);
1099 if (pathlen
> 0 && querydir
[pathlen
-1] == pathsep
)
1100 querydir
[--pathlen
] = '\0';
1102 text_query(mappedfile
, querydir
, textcutoff
, tqdepth
);
1103 } else if (mode
== HTML
) {
1104 char *querydir
= actions
[action
].arg
;
1106 struct html_config cfg
;
1110 fd
= open(filename
, O_RDONLY
);
1112 fprintf(stderr
, "%s: %s: open: %s\n", PNAME
, filename
,
1116 if (fstat(fd
, &st
) < 0) {
1117 perror(PNAME
": fstat");
1120 totalsize
= st
.st_size
;
1121 mappedfile
= mmap(NULL
, totalsize
, PROT_READ
, MAP_SHARED
, fd
, 0);
1123 perror(PNAME
": mmap");
1126 pathsep
= trie_pathsep(mappedfile
);
1129 * Trim trailing slash, just in case.
1131 pathlen
= strlen(querydir
);
1132 if (pathlen
> 0 && querydir
[pathlen
-1] == pathsep
)
1133 querydir
[--pathlen
] = '\0';
1135 xi
= trie_before(mappedfile
, querydir
);
1137 cfg
.autoage
= htmlautoagerange
;
1138 cfg
.oldest
= htmloldest
;
1139 cfg
.newest
= htmlnewest
;
1140 html
= html_query(mappedfile
, xi
, &cfg
);
1141 fputs(html
, stdout
);
1142 } else if (mode
== DUMP
) {
1146 fd
= open(filename
, O_RDONLY
);
1148 fprintf(stderr
, "%s: %s: open: %s\n", PNAME
, filename
,
1152 if (fstat(fd
, &st
) < 0) {
1153 perror(PNAME
": fstat");
1156 totalsize
= st
.st_size
;
1157 mappedfile
= mmap(NULL
, totalsize
, PROT_READ
, MAP_SHARED
, fd
, 0);
1159 perror(PNAME
": mmap");
1162 pathsep
= trie_pathsep(mappedfile
);
1164 maxpathlen
= trie_maxpathlen(mappedfile
);
1165 buf
= snewn(maxpathlen
, char);
1167 printf(DUMPHDR
"%02x\n", (unsigned char)pathsep
);
1168 tw
= triewalk_new(mappedfile
);
1169 while ((tf
= triewalk_next(tw
, buf
)) != NULL
)
1172 } else if (mode
== HTTPD
) {
1173 struct html_config pcfg
;
1174 struct httpd_config dcfg
;
1176 fd
= open(filename
, O_RDONLY
);
1178 fprintf(stderr
, "%s: %s: open: %s\n", PNAME
, filename
,
1182 if (fstat(fd
, &st
) < 0) {
1183 perror(PNAME
": fstat");
1186 totalsize
= st
.st_size
;
1187 mappedfile
= mmap(NULL
, totalsize
, PROT_READ
, MAP_SHARED
, fd
, 0);
1189 perror(PNAME
": mmap");
1192 pathsep
= trie_pathsep(mappedfile
);
1194 dcfg
.address
= httpserveraddr
;
1195 dcfg
.port
= httpserverport
;
1196 dcfg
.basicauthdata
= httpauthdata
;
1198 pcfg
.autoage
= htmlautoagerange
;
1199 pcfg
.oldest
= htmloldest
;
1200 pcfg
.newest
= htmlnewest
;
1201 run_httpd(mappedfile
, auth
, &dcfg
, &pcfg
);
1202 } else if (mode
== REMOVE
) {
1203 if (remove(filename
) < 0) {
1204 fprintf(stderr
, "%s: %s: remove: %s\n", PNAME
, filename
,