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
;
71 static void dump_line(const char *pathname
, const struct trie_file
*tf
)
74 printf("%llu %llu ", tf
->size
, tf
->atime
);
75 for (p
= pathname
; *p
; p
++) {
76 if (*p
>= ' ' && *p
< 127 && *p
!= '%')
79 printf("%%%02x", (unsigned char)*p
);
84 static int gotdata(void *vctx
, const char *pathname
, const struct stat64
*st
)
86 struct ctx
*ctx
= (struct ctx
*)vctx
;
87 struct trie_file file
;
93 * Filter out our own data file.
95 if (st
->st_dev
== ctx
->datafile_dev
&& st
->st_ino
== ctx
->datafile_ino
)
99 * Don't cross the streams^W^Wany file system boundary.
101 if (!ctx
->crossfs
&& st
->st_dev
!= ctx
->filesystem_dev
)
104 file
.size
= (unsigned long long)512 * st
->st_blocks
;
105 file
.atime
= st
->st_atime
;
108 * Filter based on wildcards.
111 filename
= strrchr(pathname
, pathsep
);
116 for (i
= 0; i
< ctx
->ninex
; i
++) {
117 if (fnmatch(ctx
->inex
[i
].wildcard
,
118 ctx
->inex
[i
].path ? pathname
: filename
, 0) == 0)
119 include
= ctx
->inex
[i
].type
;
122 return 0; /* ignore this entry and any subdirs */
125 * Here we are supposed to be filtering an entry out, but
126 * still recursing into it if it's a directory. However,
127 * we can't actually leave out any directory whose
128 * subdirectories we then look at. So we cheat, in that
129 * case, by setting the size to zero.
131 if (!S_ISDIR(st
->st_mode
))
132 return 0; /* just ignore */
137 if (ctx
->straight_to_dump
)
138 dump_line(pathname
, &file
);
140 triebuild_add(ctx
->tb
, pathname
, &file
);
144 if (t
!= ctx
->last_output_update
) {
145 fprintf(stderr
, "%-*.*s\r", ctx
->progwidth
, ctx
->progwidth
,
148 ctx
->last_output_update
= t
;
155 static void text_query(const void *mappedfile
, const char *querydir
,
160 unsigned long xi1
, xi2
;
161 unsigned long long s1
, s2
;
163 maxpathlen
= trie_maxpathlen(mappedfile
);
164 pathbuf
= snewn(maxpathlen
+ 1, char);
167 * We want to query everything between the supplied filename
168 * (inclusive) and that filename with a ^A on the end
169 * (exclusive). So find the x indices for each.
171 strcpy(pathbuf
, querydir
);
172 make_successor(pathbuf
);
173 xi1
= trie_before(mappedfile
, querydir
);
174 xi2
= trie_before(mappedfile
, pathbuf
);
177 * Now do the lookups in the age index.
179 s1
= index_query(mappedfile
, xi1
, t
);
180 s2
= index_query(mappedfile
, xi2
, t
);
183 return; /* no space taken up => no display */
187 * Now scan for first-level subdirectories and report
192 trie_getpath(mappedfile
, xi1
, pathbuf
);
193 text_query(mappedfile
, pathbuf
, t
, depth
-1);
194 make_successor(pathbuf
);
195 xi1
= trie_before(mappedfile
, pathbuf
);
199 /* Display in units of 1Kb */
200 printf("%-11llu %s\n", (s2
- s1
) / 1024, querydir
);
204 * Largely frivolous way to define all my command-line options. I
205 * present here a parametric macro which declares a series of
206 * _logical_ option identifiers, and for each one declares zero or
207 * more short option characters and zero or more long option
208 * words. Then I repeatedly invoke that macro with its arguments
209 * defined to be various other macros, which allows me to
212 * - define an enum allocating a distinct integer value to each
214 * - define a string consisting of precisely all the short option
216 * - define a string array consisting of all the long option
218 * - define (with help from auxiliary enums) integer arrays
219 * parallel to both of the above giving the logical option id
220 * for each physical short and long option
221 * - define an array indexed by logical option id indicating
222 * whether the option in question takes a value
223 * - define a function which prints out brief online help for all
226 * It's not at all clear to me that this trickery is actually
227 * particularly _efficient_ - it still, after all, requires going
228 * linearly through the option list at run time and doing a
229 * strcmp, whereas in an ideal world I'd have liked the lists of
230 * long and short options to be pre-sorted so that a binary search
231 * or some other more efficient lookup was possible. (Not that
232 * asymptotic algorithmic complexity is remotely vital in option
233 * parsing, but if I were doing this in, say, Lisp or something
234 * with an equivalently powerful preprocessor then once I'd had
235 * the idea of preparing the option-parsing data structures at
236 * compile time I would probably have made the effort to prepare
237 * them _properly_. I could have Perl generate me a source file
238 * from some sort of description, I suppose, but that would seem
239 * like overkill. And in any case, it's more of a challenge to
240 * achieve as much as possible by cunning use of cpp and enum than
241 * to just write some sensible and logical code in a Turing-
242 * complete language. I said it was largely frivolous :-)
244 * This approach does have the virtue that it brings together the
245 * option ids, option spellings and help text into a single
246 * combined list and defines them all in exactly one place. If I
247 * want to add a new option, or a new spelling for an option, I
248 * only have to modify the main OPTHELP macro below and then add
249 * code to process the new logical id.
251 * (Though, really, even that isn't ideal, since it still involves
252 * modifying the source file in more than one place. In a
253 * _properly_ ideal world, I'd be able to interleave the option
254 * definitions with the code fragments that process them. And then
255 * not bother defining logical identifiers for them at all - those
256 * would be automatically generated, since I wouldn't have any
257 * need to specify them manually in another part of the code.)
260 #define OPTHELP(NOVAL, VAL, SHORT, LONG, HELPPFX, HELPARG, HELPLINE, HELPOPT) \
261 HELPPFX("usage") HELPLINE("agedu [options] action [action...]") \
263 VAL(SCAN) SHORT(s) LONG(scan) \
264 HELPARG("directory") HELPOPT("scan and index a directory") \
265 NOVAL(DUMP) SHORT(d) LONG(dump) HELPOPT("dump the index file on stdout") \
266 VAL(SCANDUMP) SHORT(S) LONG(scan_dump) \
267 HELPARG("directory") HELPOPT("scan only, generating a dump") \
268 NOVAL(LOAD) SHORT(l) LONG(load) \
269 HELPOPT("load and index a dump file") \
270 VAL(TEXT) SHORT(t) LONG(text) \
271 HELPARG("subdir") HELPOPT("print a plain text report on a subdirectory") \
272 VAL(HTML) SHORT(H) LONG(html) \
273 HELPARG("subdir") HELPOPT("print an HTML report on a subdirectory") \
274 NOVAL(HTTPD) SHORT(w) LONG(web) LONG(server) LONG(httpd) \
275 HELPOPT("serve HTML reports from a temporary web server") \
277 VAL(DATAFILE) SHORT(f) LONG(file) \
278 HELPARG("filename") HELPOPT("[all modes] specify index file") \
279 NOVAL(PROGRESS) LONG(progress) LONG(scan_progress) \
280 HELPOPT("[--scan] report progress on stderr") \
281 NOVAL(NOPROGRESS) LONG(no_progress) LONG(no_scan_progress) \
282 HELPOPT("[--scan] do not report progress") \
283 NOVAL(TTYPROGRESS) LONG(tty_progress) LONG(tty_scan_progress) \
284 LONG(progress_tty) LONG(scan_progress_tty) \
285 HELPOPT("[--scan] report progress if stderr is a tty") \
286 NOVAL(CROSSFS) LONG(cross_fs) \
287 HELPOPT("[--scan] cross filesystem boundaries") \
288 NOVAL(NOCROSSFS) LONG(no_cross_fs) \
289 HELPOPT("[--scan] stick to one filesystem") \
290 VAL(INCLUDE) LONG(include) \
291 HELPARG("wildcard") HELPOPT("[--scan] include files matching pattern") \
292 VAL(INCLUDEPATH) LONG(include_path) \
293 HELPARG("wildcard") HELPOPT("[--scan] include pathnames matching pattern") \
294 VAL(EXCLUDE) LONG(exclude) \
295 HELPARG("wildcard") HELPOPT("[--scan] exclude files matching pattern") \
296 VAL(EXCLUDEPATH) LONG(exclude_path) \
297 HELPARG("wildcard") HELPOPT("[--scan] exclude pathnames matching pattern") \
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(TQDEPTH) LONG(depth) LONG(max_depth) LONG(maximum_depth) \
303 HELPARG("levels") HELPOPT("[--text] recurse to this many levels") \
304 VAL(MINAGE) SHORT(a) LONG(age) LONG(min_age) LONG(minimum_age) \
305 HELPARG("age") HELPOPT("[--text] include only files older than this") \
306 VAL(AGERANGE) SHORT(r) LONG(age_range) LONG(range) LONG(ages) \
307 HELPARG("age[-age]") HELPOPT("[--html,--web] set limits of colour coding") \
308 VAL(SERVERADDR) LONG(address) LONG(addr) LONG(server_address) \
310 HELPARG("addr[:port]") HELPOPT("[--web] specify HTTP server address") \
311 VAL(AUTH) LONG(auth) LONG(http_auth) LONG(httpd_auth) \
312 LONG(server_auth) LONG(web_auth) \
313 HELPARG("type") HELPOPT("[--web] specify HTTP authentication method") \
314 VAL(AUTHFILE) LONG(auth_file) \
315 HELPARG("filename") HELPOPT("[--web] read HTTP Basic user/pass from file") \
316 VAL(AUTHFD) LONG(auth_fd) \
317 HELPARG("fd") HELPOPT("[--web] read HTTP Basic user/pass from fd") \
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
= "agedu.dat";
444 enum { TEXT
, HTML
, SCAN
, DUMP
, SCANDUMP
, LOAD
, HTTPD
};
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;
463 #ifdef DEBUG_MAD_OPTION_PARSING_MACROS
465 static const char *const optnames
[NOPTIONS
] = {
466 OPTIONS(STRING
,STRING
,IGNORE
,IGNORE
)
469 for (i
= 0; i
< NSHORTOPTS
; i
++)
470 printf("-%c == %s [%s]\n", shortopts
[i
], optnames
[shortvals
[i
]],
471 opthasval
[shortvals
[i
]] ?
"value" : "no value");
472 for (i
= 0; i
< NLONGOPTS
; i
++)
473 printf("--%s == %s [%s]\n", longopts
[i
], optnames
[longvals
[i
]],
474 opthasval
[longvals
[i
]] ?
"value" : "no value");
481 if (doing_opts
&& *p
== '-') {
484 if (!strcmp(p
, "--")) {
495 if (wordstart
&& *p
== '-') {
497 * GNU-style long option.
500 optval
= strchr(p
, '=');
504 for (i
= 0; i
< NLONGOPTS
; i
++) {
505 const char *opt
= longopts
[i
], *s
= p
;
508 * The underscores in the option names
509 * defined above may be given by the user
510 * as underscores or dashes, or omitted
515 if (*s
== '-' || *s
== '_')
533 fprintf(stderr
, "%s: unrecognised option '--%s'\n",
538 if (!opthasval
[optid
]) {
540 fprintf(stderr
, "%s: unexpected argument to option"
541 " '--%s'\n", PNAME
, p
);
549 fprintf(stderr
, "%s: option '--%s' expects"
550 " an argument\n", PNAME
, p
);
556 p
+= strlen(p
); /* finished with this argument word */
563 for (i
= 0; i
< NSHORTOPTS
; i
++)
564 if (c
== shortopts
[i
]) {
565 optid
= shortvals
[i
];
570 fprintf(stderr
, "%s: unrecognised option '-%c'\n",
575 if (opthasval
[optid
]) {
579 } else if (--argc
> 0) {
582 fprintf(stderr
, "%s: option '-%c' expects"
583 " an argument\n", PNAME
, c
);
594 * Now actually process the option.
601 printf("FIXME: version();\n");
605 extern const char *const licence
[];
608 for (i
= 0; licence
[i
]; i
++)
609 fputs(licence
[i
], stdout
);
615 if (nactions
>= actionsize
) {
616 actionsize
= nactions
* 3 / 2 + 16;
617 actions
= sresize(actions
, actionsize
, struct action
);
619 actions
[nactions
].mode
= SCAN
;
620 actions
[nactions
].arg
= optval
;
624 if (nactions
>= actionsize
) {
625 actionsize
= nactions
* 3 / 2 + 16;
626 actions
= sresize(actions
, actionsize
, struct action
);
628 actions
[nactions
].mode
= SCANDUMP
;
629 actions
[nactions
].arg
= optval
;
633 if (nactions
>= actionsize
) {
634 actionsize
= nactions
* 3 / 2 + 16;
635 actions
= sresize(actions
, actionsize
, struct action
);
637 actions
[nactions
].mode
= DUMP
;
638 actions
[nactions
].arg
= NULL
;
642 if (nactions
>= actionsize
) {
643 actionsize
= nactions
* 3 / 2 + 16;
644 actions
= sresize(actions
, actionsize
, struct action
);
646 actions
[nactions
].mode
= LOAD
;
647 actions
[nactions
].arg
= NULL
;
651 if (nactions
>= actionsize
) {
652 actionsize
= nactions
* 3 / 2 + 16;
653 actions
= sresize(actions
, actionsize
, struct action
);
655 actions
[nactions
].mode
= TEXT
;
656 actions
[nactions
].arg
= optval
;
660 if (nactions
>= actionsize
) {
661 actionsize
= nactions
* 3 / 2 + 16;
662 actions
= sresize(actions
, actionsize
, struct action
);
664 actions
[nactions
].mode
= HTML
;
665 actions
[nactions
].arg
= optval
;
669 if (nactions
>= actionsize
) {
670 actionsize
= nactions
* 3 / 2 + 16;
671 actions
= sresize(actions
, actionsize
, struct action
);
673 actions
[nactions
].mode
= HTTPD
;
674 actions
[nactions
].arg
= NULL
;
683 case OPT_TTYPROGRESS
:
696 tqdepth
= atoi(optval
);
699 textcutoff
= parse_age(now
, optval
);
702 if (!strcmp(optval
, "auto")) {
703 htmlautoagerange
= 1;
705 char *q
= optval
+ strcspn(optval
, "-:");
708 htmloldest
= parse_age(now
, optval
);
709 htmlnewest
= *q ?
parse_age(now
, q
) : now
;
710 htmlautoagerange
= 0;
716 if (optval
[0] == '[' &&
717 (port
= strchr(optval
, ']')) != NULL
)
721 port
+= strcspn(port
, ":");
724 httpserveraddr
= optval
;
725 httpserverport
= atoi(port
);
729 if (!strcmp(optval
, "magic"))
730 auth
= HTTPD_AUTH_MAGIC
;
731 else if (!strcmp(optval
, "basic"))
732 auth
= HTTPD_AUTH_BASIC
;
733 else if (!strcmp(optval
, "none"))
734 auth
= HTTPD_AUTH_NONE
;
735 else if (!strcmp(optval
, "default"))
736 auth
= HTTPD_AUTH_MAGIC
| HTTPD_AUTH_BASIC
;
737 else if (!strcmp(optval
, "help") ||
738 !strcmp(optval
, "list")) {
739 printf("agedu: supported HTTP authentication types"
741 " magic use Linux /proc/net/tcp to"
742 " determine owner of peer socket\n"
743 " basic HTTP Basic username and"
744 " password authentication\n"
745 " default use 'magic' if possible, "
746 " otherwise fall back to 'basic'\n"
747 " none unauthenticated HTTP (if"
748 " the data file is non-confidential)\n");
751 fprintf(stderr
, "%s: unrecognised authentication"
752 " type '%s'\n%*s options are 'magic',"
753 " 'basic', 'none', 'default'\n",
754 PNAME
, optval
, (int)strlen(PNAME
), "");
765 int authlen
, authsize
;
768 if (optid
== OPT_AUTHFILE
) {
769 fd
= open(optval
, O_RDONLY
);
771 fprintf(stderr
, "%s: %s: open: %s\n", PNAME
,
772 optval
, strerror(errno
));
779 sprintf(namebuf
, "fd %d", fd
);
784 authbuf
= snewn(authsize
, char);
785 while ((ret
= read(fd
, authbuf
+authlen
,
786 authsize
-authlen
)) > 0) {
788 if ((authsize
- authlen
) < (authsize
/ 16)) {
789 authsize
= authlen
* 3 / 2 + 4096;
790 authbuf
= sresize(authbuf
, authsize
, char);
794 fprintf(stderr
, "%s: %s: read: %s\n", PNAME
,
795 name
, strerror(errno
));
798 if (optid
== OPT_AUTHFILE
)
800 httpauthdata
= authbuf
;
804 case OPT_INCLUDEPATH
:
806 case OPT_EXCLUDEPATH
:
809 if (ninex
>= inexsize
) {
810 inexsize
= ninex
* 3 / 2 + 16;
811 inex
= sresize(inex
, inexsize
,
812 struct inclusion_exclusion
);
814 inex
[ninex
].path
= (optid
== OPT_INCLUDEPATH
||
815 optid
== OPT_EXCLUDEPATH
||
816 optid
== OPT_PRUNEPATH
);
817 inex
[ninex
].type
= (optid
== OPT_INCLUDE ?
1 :
818 optid
== OPT_INCLUDEPATH ?
1 :
819 optid
== OPT_EXCLUDE ?
0 :
820 optid
== OPT_EXCLUDEPATH ?
0 :
821 optid
== OPT_PRUNE ?
-1 :
822 /* optid == OPT_PRUNEPATH ? */ -1);
823 inex
[ninex
].wildcard
= optval
;
829 fprintf(stderr
, "%s: unexpected argument '%s'\n", PNAME
, p
);
839 for (action
= 0; action
< nactions
; action
++) {
840 int mode
= actions
[action
].mode
;
842 if (mode
== SCAN
|| mode
== SCANDUMP
|| mode
== LOAD
) {
843 const char *scandir
= actions
[action
].arg
;
845 char *buf
= fgetline(stdin
);
847 buf
[strcspn(buf
, "\r\n")] = '\0';
848 if (1 != sscanf(buf
, "agedu dump file. pathsep=%x",
850 fprintf(stderr
, "%s: header in dump file not recognised\n",
854 pathsep
= (char)newpathsep
;
858 if (mode
== SCAN
|| mode
== LOAD
) {
860 * Prepare to write out the index file.
862 fd
= open(filename
, O_RDWR
| O_TRUNC
| O_CREAT
, S_IRWXU
);
864 fprintf(stderr
, "%s: %s: open: %s\n", PNAME
, filename
,
868 if (fstat(fd
, &st
) < 0) {
869 perror("agedu: fstat");
872 ctx
->datafile_dev
= st
.st_dev
;
873 ctx
->datafile_ino
= st
.st_ino
;
874 ctx
->straight_to_dump
= 0;
876 ctx
->datafile_dev
= -1;
877 ctx
->datafile_ino
= -1;
878 ctx
->straight_to_dump
= 1;
881 if (mode
== SCAN
|| mode
== SCANDUMP
) {
882 if (stat(scandir
, &st
) < 0) {
883 fprintf(stderr
, "%s: %s: stat: %s\n", PNAME
, scandir
,
887 ctx
->filesystem_dev
= crossfs ?
0 : st
.st_dev
;
892 ctx
->crossfs
= crossfs
;
894 ctx
->last_output_update
= time(NULL
);
896 /* progress==1 means report progress only if stderr is a tty */
898 progress
= isatty(2) ?
2 : 0;
899 ctx
->progress
= progress
;
902 if (progress
&& ioctl(2, TIOCGWINSZ
, &ws
) == 0)
903 ctx
->progwidth
= ws
.ws_col
- 1;
908 if (mode
== SCANDUMP
)
909 printf("agedu dump file. pathsep=%02x\n", (unsigned char)pathsep
);
912 * Scan the directory tree, and write out the trie component
915 if (mode
!= SCANDUMP
) {
916 ctx
->tb
= triebuild_new(fd
);
921 while ((buf
= fgetline(stdin
)) != NULL
) {
925 buf
[strcspn(buf
, "\r\n")] = '\0';
929 while (*p
&& *p
!= ' ') p
++;
931 fprintf(stderr
, "%s: dump file line %d: expected at least"
932 " three fields\n", PNAME
, line
);
936 tf
.size
= strtoull(q
, NULL
, 10);
938 while (*p
&& *p
!= ' ') p
++;
940 fprintf(stderr
, "%s: dump file line %d: expected at least"
941 " three fields\n", PNAME
, line
);
945 tf
.atime
= strtoull(q
, NULL
, 10);
953 for (i
= 0; i
< 2; i
++) {
954 if (*p
>= '0' && *p
<= '9')
956 else if (*p
>= 'A' && *p
<= 'F')
957 c
+= *p
- ('A' - 10);
958 else if (*p
>= 'a' && *p
<= 'f')
959 c
+= *p
- ('a' - 10);
961 fprintf(stderr
, "%s: dump file line %d: unable"
962 " to parse hex escape\n", PNAME
, line
);
971 triebuild_add(ctx
->tb
, buf
, &tf
);
975 du(scandir
, gotdata
, ctx
);
977 if (mode
!= SCANDUMP
) {
978 count
= triebuild_finish(ctx
->tb
);
979 triebuild_free(ctx
->tb
);
982 fprintf(stderr
, "%-*s\r", ctx
->progwidth
, "");
987 * Work out how much space the cumulative index trees
988 * will take; enlarge the file, and memory-map it.
990 if (fstat(fd
, &st
) < 0) {
991 perror("agedu: fstat");
995 printf("Built pathname index, %d entries, %ju bytes\n", count
,
996 (intmax_t)st
.st_size
);
998 totalsize
= index_compute_size(st
.st_size
, count
);
1000 if (lseek(fd
, totalsize
-1, SEEK_SET
) < 0) {
1001 perror("agedu: lseek");
1004 if (write(fd
, "\0", 1) < 1) {
1005 perror("agedu: write");
1009 printf("Upper bound on index file size = %ju bytes\n",
1010 (intmax_t)totalsize
);
1012 mappedfile
= mmap(NULL
, totalsize
, PROT_READ
|PROT_WRITE
,MAP_SHARED
, fd
, 0);
1014 perror("agedu: mmap");
1018 ib
= indexbuild_new(mappedfile
, st
.st_size
, count
);
1019 tw
= triewalk_new(mappedfile
);
1020 while ((tf
= triewalk_next(tw
, NULL
)) != NULL
)
1021 indexbuild_add(ib
, tf
);
1023 realsize
= indexbuild_realsize(ib
);
1024 indexbuild_free(ib
);
1026 munmap(mappedfile
, totalsize
);
1027 ftruncate(fd
, realsize
);
1029 printf("Actual index file size = %ju bytes\n", (intmax_t)realsize
);
1031 } else if (mode
== TEXT
) {
1032 char *querydir
= actions
[action
].arg
;
1035 fd
= open(filename
, O_RDONLY
);
1037 fprintf(stderr
, "%s: %s: open: %s\n", PNAME
, filename
,
1041 if (fstat(fd
, &st
) < 0) {
1042 perror("agedu: fstat");
1045 totalsize
= st
.st_size
;
1046 mappedfile
= mmap(NULL
, totalsize
, PROT_READ
, MAP_SHARED
, fd
, 0);
1048 perror("agedu: mmap");
1051 pathsep
= trie_pathsep(mappedfile
);
1054 * Trim trailing slash, just in case.
1056 pathlen
= strlen(querydir
);
1057 if (pathlen
> 0 && querydir
[pathlen
-1] == pathsep
)
1058 querydir
[--pathlen
] = '\0';
1060 text_query(mappedfile
, querydir
, textcutoff
, tqdepth
);
1061 } else if (mode
== HTML
) {
1062 char *querydir
= actions
[action
].arg
;
1064 struct html_config cfg
;
1068 fd
= open(filename
, O_RDONLY
);
1070 fprintf(stderr
, "%s: %s: open: %s\n", PNAME
, filename
,
1074 if (fstat(fd
, &st
) < 0) {
1075 perror("agedu: fstat");
1078 totalsize
= st
.st_size
;
1079 mappedfile
= mmap(NULL
, totalsize
, PROT_READ
, MAP_SHARED
, fd
, 0);
1081 perror("agedu: mmap");
1084 pathsep
= trie_pathsep(mappedfile
);
1087 * Trim trailing slash, just in case.
1089 pathlen
= strlen(querydir
);
1090 if (pathlen
> 0 && querydir
[pathlen
-1] == pathsep
)
1091 querydir
[--pathlen
] = '\0';
1093 xi
= trie_before(mappedfile
, querydir
);
1095 cfg
.autoage
= htmlautoagerange
;
1096 cfg
.oldest
= htmloldest
;
1097 cfg
.newest
= htmlnewest
;
1098 html
= html_query(mappedfile
, xi
, &cfg
);
1099 fputs(html
, stdout
);
1100 } else if (mode
== DUMP
) {
1104 fd
= open(filename
, O_RDONLY
);
1106 fprintf(stderr
, "%s: %s: open: %s\n", PNAME
, filename
,
1110 if (fstat(fd
, &st
) < 0) {
1111 perror("agedu: fstat");
1114 totalsize
= st
.st_size
;
1115 mappedfile
= mmap(NULL
, totalsize
, PROT_READ
, MAP_SHARED
, fd
, 0);
1117 perror("agedu: mmap");
1120 pathsep
= trie_pathsep(mappedfile
);
1122 maxpathlen
= trie_maxpathlen(mappedfile
);
1123 buf
= snewn(maxpathlen
, char);
1125 printf("agedu dump file. pathsep=%02x\n", (unsigned char)pathsep
);
1126 tw
= triewalk_new(mappedfile
);
1127 while ((tf
= triewalk_next(tw
, buf
)) != NULL
)
1130 } else if (mode
== HTTPD
) {
1131 struct html_config pcfg
;
1132 struct httpd_config dcfg
;
1134 fd
= open(filename
, O_RDONLY
);
1136 fprintf(stderr
, "%s: %s: open: %s\n", PNAME
, filename
,
1140 if (fstat(fd
, &st
) < 0) {
1141 perror("agedu: fstat");
1144 totalsize
= st
.st_size
;
1145 mappedfile
= mmap(NULL
, totalsize
, PROT_READ
, MAP_SHARED
, fd
, 0);
1147 perror("agedu: mmap");
1150 pathsep
= trie_pathsep(mappedfile
);
1152 dcfg
.address
= httpserveraddr
;
1153 dcfg
.port
= httpserverport
;
1154 dcfg
.basicauthdata
= httpauthdata
;
1156 pcfg
.autoage
= htmlautoagerange
;
1157 pcfg
.oldest
= htmloldest
;
1158 pcfg
.newest
= htmlnewest
;
1159 run_httpd(mappedfile
, auth
, &dcfg
, &pcfg
);