246f0020c82e8bdf92b27aa900904c1a97c1acf9
2 * Main program for agedu.
16 #include <sys/types.h>
20 #include <sys/ioctl.h>
33 #define lenof(x) (sizeof((x))/sizeof(*(x)))
36 * Path separator. This global variable affects the behaviour of
37 * various parts of the code when they need to deal with path
38 * separators. The path separator appropriate to a particular data
39 * set is encoded in the index file storing that data set; data
40 * sets generated on Unix will of course have the default '/', but
41 * foreign data sets are conceivable and must be handled correctly.
45 void fatal(const char *fmt
, ...)
48 fprintf(stderr
, "%s: ", PNAME
);
50 vfprintf(stderr
, fmt
, ap
);
52 fprintf(stderr
, "\n");
56 struct inclusion_exclusion
{
64 dev_t datafile_dev
, filesystem_dev
;
66 time_t last_output_update
;
67 int progress
, progwidth
;
69 struct inclusion_exclusion
*inex
;
74 static void dump_line(const char *pathname
, const struct trie_file
*tf
)
77 printf("%llu %llu ", tf
->size
, tf
->atime
);
78 for (p
= pathname
; *p
; p
++) {
79 if (*p
>= ' ' && *p
< 127 && *p
!= '%')
82 printf("%%%02x", (unsigned char)*p
);
87 static int gotdata(void *vctx
, const char *pathname
, const struct stat64
*st
)
89 struct ctx
*ctx
= (struct ctx
*)vctx
;
90 struct trie_file file
;
96 * Filter out our own data file.
98 if (st
->st_dev
== ctx
->datafile_dev
&& st
->st_ino
== ctx
->datafile_ino
)
102 * Don't cross the streams^W^Wany file system boundary.
104 if (!ctx
->crossfs
&& st
->st_dev
!= ctx
->filesystem_dev
)
107 file
.size
= (unsigned long long)512 * st
->st_blocks
;
108 file
.atime
= st
->st_atime
;
111 * Filter based on wildcards.
114 filename
= strrchr(pathname
, pathsep
);
119 for (i
= 0; i
< ctx
->ninex
; i
++) {
120 if (fnmatch(ctx
->inex
[i
].wildcard
,
121 ctx
->inex
[i
].path ? pathname
: filename
, 0) == 0)
122 include
= ctx
->inex
[i
].type
;
125 return 0; /* ignore this entry and any subdirs */
128 * Here we are supposed to be filtering an entry out, but
129 * still recursing into it if it's a directory. However,
130 * we can't actually leave out any directory whose
131 * subdirectories we then look at. So we cheat, in that
132 * case, by setting the size to zero.
134 if (!S_ISDIR(st
->st_mode
))
135 return 0; /* just ignore */
140 if (ctx
->straight_to_dump
)
141 dump_line(pathname
, &file
);
143 triebuild_add(ctx
->tb
, pathname
, &file
);
147 if (t
!= ctx
->last_output_update
) {
148 fprintf(stderr
, "%-*.*s\r", ctx
->progwidth
, ctx
->progwidth
,
151 ctx
->last_output_update
= t
;
158 static void text_query(const void *mappedfile
, const char *querydir
,
163 unsigned long xi1
, xi2
;
164 unsigned long long s1
, s2
;
166 maxpathlen
= trie_maxpathlen(mappedfile
);
167 pathbuf
= snewn(maxpathlen
+ 1, char);
170 * We want to query everything between the supplied filename
171 * (inclusive) and that filename with a ^A on the end
172 * (exclusive). So find the x indices for each.
174 sprintf(pathbuf
, "%s\001", querydir
);
175 xi1
= trie_before(mappedfile
, querydir
);
176 xi2
= trie_before(mappedfile
, pathbuf
);
179 * Now do the lookups in the age index.
181 s1
= index_query(mappedfile
, xi1
, t
);
182 s2
= index_query(mappedfile
, xi2
, t
);
185 return; /* no space taken up => no display */
187 /* Display in units of 1Kb */
188 printf("%-11llu %s\n", (s2
- s1
) / 1024, querydir
);
192 * Now scan for first-level subdirectories and report
197 trie_getpath(mappedfile
, xi1
, pathbuf
);
198 text_query(mappedfile
, pathbuf
, t
, depth
-1);
199 strcat(pathbuf
, "\001");
200 xi1
= trie_before(mappedfile
, pathbuf
);
206 * Largely frivolous way to define all my command-line options. I
207 * present here a parametric macro which declares a series of
208 * _logical_ option identifiers, and for each one declares zero or
209 * more short option characters and zero or more long option
210 * words. Then I repeatedly invoke that macro with its arguments
211 * defined to be various other macros, which allows me to
214 * - define an enum allocating a distinct integer value to each
216 * - define a string consisting of precisely all the short option
218 * - define a string array consisting of all the long option
220 * - define (with help from auxiliary enums) integer arrays
221 * parallel to both of the above giving the logical option id
222 * for each physical short and long option
223 * - define an array indexed by logical option id indicating
224 * whether the option in question takes a value
225 * - define a function which prints out brief online help for all
228 * It's not at all clear to me that this trickery is actually
229 * particularly _efficient_ - it still, after all, requires going
230 * linearly through the option list at run time and doing a
231 * strcmp, whereas in an ideal world I'd have liked the lists of
232 * long and short options to be pre-sorted so that a binary search
233 * or some other more efficient lookup was possible. (Not that
234 * asymptotic algorithmic complexity is remotely vital in option
235 * parsing, but if I were doing this in, say, Lisp or something
236 * with an equivalently powerful preprocessor then once I'd had
237 * the idea of preparing the option-parsing data structures at
238 * compile time I would probably have made the effort to prepare
239 * them _properly_. I could have Perl generate me a source file
240 * from some sort of description, I suppose, but that would seem
241 * like overkill. And in any case, it's more of a challenge to
242 * achieve as much as possible by cunning use of cpp and enum than
243 * to just write some sensible and logical code in a Turing-
244 * complete language. I said it was largely frivolous :-)
246 * This approach does have the virtue that it brings together the
247 * option ids, option spellings and help text into a single
248 * combined list and defines them all in exactly one place. If I
249 * want to add a new option, or a new spelling for an option, I
250 * only have to modify the main OPTHELP macro below and then add
251 * code to process the new logical id.
253 * (Though, really, even that isn't ideal, since it still involves
254 * modifying the source file in more than one place. In a
255 * _properly_ ideal world, I'd be able to interleave the option
256 * definitions with the code fragments that process them. And then
257 * not bother defining logical identifiers for them at all - those
258 * would be automatically generated, since I wouldn't have any
259 * need to specify them manually in another part of the code.)
262 #define OPTHELP(NOVAL, VAL, SHORT, LONG, HELPPFX, HELPARG, HELPLINE, HELPOPT) \
263 HELPPFX("usage") HELPLINE("agedu [options] action") \
265 VAL(SCAN) SHORT(s) LONG(scan) \
266 HELPARG("directory") HELPOPT("scan and index a directory") \
267 NOVAL(DUMP) SHORT(d) LONG(dump) HELPOPT("dump the index file on stdout") \
268 VAL(SCANDUMP) SHORT(S) LONG(scan_dump) \
269 HELPARG("directory") HELPOPT("scan only, generating a dump") \
270 NOVAL(LOAD) SHORT(l) LONG(load) \
271 HELPOPT("load and index a dump file") \
272 VAL(TEXT) SHORT(t) LONG(text) \
273 HELPARG("subdir") HELPOPT("print a plain text report on a subdirectory") \
274 VAL(HTML) SHORT(H) LONG(html) \
275 HELPARG("subdir") HELPOPT("print an HTML report on a subdirectory") \
276 NOVAL(HTTPD) SHORT(w) LONG(web) LONG(server) LONG(httpd) \
277 HELPOPT("serve HTML reports from a temporary web server") \
279 VAL(DATAFILE) SHORT(f) LONG(file) \
280 HELPARG("filename") HELPOPT("[all modes] specify index file") \
281 NOVAL(PROGRESS) LONG(progress) LONG(scan_progress) \
282 HELPOPT("[--scan] report progress on stderr") \
283 NOVAL(NOPROGRESS) LONG(no_progress) LONG(no_scan_progress) \
284 HELPOPT("[--scan] do not report progress") \
285 NOVAL(TTYPROGRESS) LONG(tty_progress) LONG(tty_scan_progress) \
286 LONG(progress_tty) LONG(scan_progress_tty) \
287 HELPOPT("[--scan] report progress if stderr is a tty") \
288 NOVAL(CROSSFS) LONG(cross_fs) \
289 HELPOPT("[--scan] cross filesystem boundaries") \
290 NOVAL(NOCROSSFS) LONG(no_cross_fs) \
291 HELPOPT("[--scan] stick to one filesystem") \
292 VAL(INCLUDE) LONG(include) \
293 HELPARG("wildcard") HELPOPT("[--scan] include files matching pattern") \
294 VAL(INCLUDEPATH) LONG(include_path) \
295 HELPARG("wildcard") HELPOPT("[--scan] include pathnames matching pattern") \
296 VAL(EXCLUDE) LONG(exclude) \
297 HELPARG("wildcard") HELPOPT("[--scan] exclude files matching pattern") \
298 VAL(EXCLUDEPATH) LONG(exclude_path) \
299 HELPARG("wildcard") HELPOPT("[--scan] exclude pathnames matching pattern") \
300 VAL(PRUNE) LONG(prune) \
301 HELPARG("wildcard") HELPOPT("[--scan] prune files matching pattern") \
302 VAL(PRUNEPATH) LONG(prune_path) \
303 HELPARG("wildcard") HELPOPT("[--scan] prune pathnames matching pattern") \
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";
443 char *scandir
= NULL
;
444 char *querydir
= NULL
;
446 enum { USAGE
, TEXT
, HTML
, SCAN
, DUMP
, SCANDUMP
, LOAD
, HTTPD
} mode
= USAGE
;
447 time_t now
= time(NULL
);
448 time_t textcutoff
= now
, htmlnewest
= now
, htmloldest
= now
;
449 int htmlautoagerange
= 1;
450 const char *httpserveraddr
= NULL
;
451 int httpserverport
= 0;
452 const char *httpauthdata
= NULL
;
453 int auth
= HTTPD_AUTH_MAGIC
| HTTPD_AUTH_BASIC
;
455 struct inclusion_exclusion
*inex
= NULL
;
456 int ninex
= 0, inexsize
= 0;
459 #ifdef DEBUG_MAD_OPTION_PARSING_MACROS
461 static const char *const optnames
[NOPTIONS
] = {
462 OPTIONS(STRING
,STRING
,IGNORE
,IGNORE
)
465 for (i
= 0; i
< NSHORTOPTS
; i
++)
466 printf("-%c == %s [%s]\n", shortopts
[i
], optnames
[shortvals
[i
]],
467 opthasval
[shortvals
[i
]] ?
"value" : "no value");
468 for (i
= 0; i
< NLONGOPTS
; i
++)
469 printf("--%s == %s [%s]\n", longopts
[i
], optnames
[longvals
[i
]],
470 opthasval
[longvals
[i
]] ?
"value" : "no value");
477 if (doing_opts
&& *p
== '-') {
480 if (!strcmp(p
, "--")) {
491 if (wordstart
&& *p
== '-') {
493 * GNU-style long option.
496 optval
= strchr(p
, '=');
500 for (i
= 0; i
< NLONGOPTS
; i
++) {
501 const char *opt
= longopts
[i
], *s
= p
;
504 * The underscores in the option names
505 * defined above may be given by the user
506 * as underscores or dashes, or omitted
511 if (*s
== '-' || *s
== '_')
529 fprintf(stderr
, "%s: unrecognised option '--%s'\n",
534 if (!opthasval
[optid
]) {
536 fprintf(stderr
, "%s: unexpected argument to option"
537 " '--%s'\n", PNAME
, p
);
545 fprintf(stderr
, "%s: option '--%s' expects"
546 " an argument\n", PNAME
, p
);
552 p
+= strlen(p
); /* finished with this argument word */
559 for (i
= 0; i
< NSHORTOPTS
; i
++)
560 if (c
== shortopts
[i
]) {
561 optid
= shortvals
[i
];
566 fprintf(stderr
, "%s: unrecognised option '-%c'\n",
571 if (opthasval
[optid
]) {
575 } else if (--argc
> 0) {
578 fprintf(stderr
, "%s: option '-%c' expects"
579 " an argument\n", PNAME
, c
);
590 * Now actually process the option.
597 printf("FIXME: version();\n");
600 printf("FIXME: licence();\n");
633 case OPT_TTYPROGRESS
:
646 textcutoff
= parse_age(now
, optval
);
649 if (!strcmp(optval
, "auto")) {
650 htmlautoagerange
= 1;
652 char *q
= optval
+ strcspn(optval
, "-:");
655 htmloldest
= parse_age(now
, optval
);
656 htmlnewest
= *q ?
parse_age(now
, q
) : now
;
657 htmlautoagerange
= 0;
663 if (optval
[0] == '[' &&
664 (port
= strchr(optval
, ']')) != NULL
)
668 port
+= strcspn(port
, ":");
671 httpserveraddr
= optval
;
672 httpserverport
= atoi(port
);
676 if (!strcmp(optval
, "magic"))
677 auth
= HTTPD_AUTH_MAGIC
;
678 else if (!strcmp(optval
, "basic"))
679 auth
= HTTPD_AUTH_BASIC
;
680 else if (!strcmp(optval
, "none"))
681 auth
= HTTPD_AUTH_NONE
;
682 else if (!strcmp(optval
, "default"))
683 auth
= HTTPD_AUTH_MAGIC
| HTTPD_AUTH_BASIC
;
684 else if (!strcmp(optval
, "help") ||
685 !strcmp(optval
, "list")) {
686 printf("agedu: supported HTTP authentication types"
688 " magic use Linux /proc/net/tcp to"
689 " determine owner of peer socket\n"
690 " basic HTTP Basic username and"
691 " password authentication\n"
692 " default use 'magic' if possible, "
693 " otherwise fall back to 'basic'\n"
694 " none unauthenticated HTTP (if"
695 " the data file is non-confidential)\n");
698 fprintf(stderr
, "%s: unrecognised authentication"
699 " type '%s'\n%*s options are 'magic',"
700 " 'basic', 'none', 'default'\n",
701 PNAME
, optval
, (int)strlen(PNAME
), "");
712 int authlen
, authsize
;
715 if (optid
== OPT_AUTHFILE
) {
716 fd
= open(optval
, O_RDONLY
);
718 fprintf(stderr
, "%s: %s: open: %s\n", PNAME
,
719 optval
, strerror(errno
));
726 sprintf(namebuf
, "fd %d", fd
);
731 authbuf
= snewn(authsize
, char);
732 while ((ret
= read(fd
, authbuf
+authlen
,
733 authsize
-authlen
)) > 0) {
735 if ((authsize
- authlen
) < (authsize
/ 16)) {
736 authsize
= authlen
* 3 / 2 + 4096;
737 authbuf
= sresize(authbuf
, authsize
, char);
741 fprintf(stderr
, "%s: %s: read: %s\n", PNAME
,
742 name
, strerror(errno
));
745 if (optid
== OPT_AUTHFILE
)
747 httpauthdata
= authbuf
;
751 case OPT_INCLUDEPATH
:
753 case OPT_EXCLUDEPATH
:
756 if (ninex
>= inexsize
) {
757 inexsize
= ninex
* 3 / 2 + 16;
758 inex
= sresize(inex
, inexsize
,
759 struct inclusion_exclusion
);
761 inex
[ninex
].path
= (optid
== OPT_INCLUDEPATH
||
762 optid
== OPT_EXCLUDEPATH
||
763 optid
== OPT_PRUNEPATH
);
764 inex
[ninex
].type
= (optid
== OPT_INCLUDE ?
1 :
765 optid
== OPT_INCLUDEPATH ?
1 :
766 optid
== OPT_EXCLUDE ?
0 :
767 optid
== OPT_EXCLUDEPATH ?
0 :
768 optid
== OPT_PRUNE ?
-1 :
769 /* optid == OPT_PRUNEPATH ? */ -1);
770 inex
[ninex
].wildcard
= optval
;
776 fprintf(stderr
, "%s: unexpected argument '%s'\n", PNAME
, p
);
784 } else if (mode
== SCAN
|| mode
== SCANDUMP
|| mode
== LOAD
) {
787 char *buf
= fgetline(stdin
);
789 buf
[strcspn(buf
, "\r\n")] = '\0';
790 if (1 != sscanf(buf
, "agedu dump file. pathsep=%x",
792 fprintf(stderr
, "%s: header in dump file not recognised\n",
796 pathsep
= (char)newpathsep
;
800 if (mode
== SCAN
|| mode
== LOAD
) {
802 * Prepare to write out the index file.
804 fd
= open(filename
, O_RDWR
| O_TRUNC
| O_CREAT
, S_IRWXU
);
806 fprintf(stderr
, "%s: %s: open: %s\n", PNAME
, filename
,
810 if (fstat(fd
, &st
) < 0) {
811 perror("agedu: fstat");
814 ctx
->datafile_dev
= st
.st_dev
;
815 ctx
->datafile_ino
= st
.st_ino
;
816 ctx
->straight_to_dump
= 0;
818 ctx
->datafile_dev
= -1;
819 ctx
->datafile_ino
= -1;
820 ctx
->straight_to_dump
= 1;
823 if (mode
== SCAN
|| mode
== SCANDUMP
) {
824 if (stat(scandir
, &st
) < 0) {
825 fprintf(stderr
, "%s: %s: stat: %s\n", PNAME
, scandir
,
829 ctx
->filesystem_dev
= crossfs ?
0 : st
.st_dev
;
834 ctx
->crossfs
= crossfs
;
836 ctx
->last_output_update
= time(NULL
);
838 /* progress==1 means report progress only if stderr is a tty */
840 progress
= isatty(2) ?
2 : 0;
841 ctx
->progress
= progress
;
844 if (progress
&& ioctl(2, TIOCGWINSZ
, &ws
) == 0)
845 ctx
->progwidth
= ws
.ws_col
- 1;
850 if (mode
== SCANDUMP
)
851 printf("agedu dump file. pathsep=%02x\n", (unsigned char)pathsep
);
854 * Scan the directory tree, and write out the trie component
857 if (mode
!= SCANDUMP
) {
858 ctx
->tb
= triebuild_new(fd
);
863 while ((buf
= fgetline(stdin
)) != NULL
) {
867 buf
[strcspn(buf
, "\r\n")] = '\0';
871 while (*p
&& *p
!= ' ') p
++;
873 fprintf(stderr
, "%s: dump file line %d: expected at least"
874 " three fields\n", PNAME
, line
);
878 tf
.size
= strtoull(q
, NULL
, 10);
880 while (*p
&& *p
!= ' ') p
++;
882 fprintf(stderr
, "%s: dump file line %d: expected at least"
883 " three fields\n", PNAME
, line
);
887 tf
.atime
= strtoull(q
, NULL
, 10);
895 for (i
= 0; i
< 2; i
++) {
896 if (*p
>= '0' && *p
<= '9')
898 else if (*p
>= 'A' && *p
<= 'F')
899 c
+= *p
- ('A' - 10);
900 else if (*p
>= 'a' && *p
<= 'f')
901 c
+= *p
- ('a' - 10);
903 fprintf(stderr
, "%s: dump file line %d: unable"
904 " to parse hex escape\n", PNAME
, line
);
913 triebuild_add(ctx
->tb
, buf
, &tf
);
917 du(scandir
, gotdata
, ctx
);
919 if (mode
!= SCANDUMP
) {
920 count
= triebuild_finish(ctx
->tb
);
921 triebuild_free(ctx
->tb
);
924 fprintf(stderr
, "%-*s\r", ctx
->progwidth
, "");
929 * Work out how much space the cumulative index trees
930 * will take; enlarge the file, and memory-map it.
932 if (fstat(fd
, &st
) < 0) {
933 perror("agedu: fstat");
937 printf("Built pathname index, %d entries, %ju bytes\n", count
,
938 (intmax_t)st
.st_size
);
940 totalsize
= index_compute_size(st
.st_size
, count
);
942 if (lseek(fd
, totalsize
-1, SEEK_SET
) < 0) {
943 perror("agedu: lseek");
946 if (write(fd
, "\0", 1) < 1) {
947 perror("agedu: write");
951 printf("Upper bound on index file size = %ju bytes\n",
952 (intmax_t)totalsize
);
954 mappedfile
= mmap(NULL
, totalsize
, PROT_READ
|PROT_WRITE
,MAP_SHARED
, fd
, 0);
956 perror("agedu: mmap");
960 ib
= indexbuild_new(mappedfile
, st
.st_size
, count
);
961 tw
= triewalk_new(mappedfile
);
962 while ((tf
= triewalk_next(tw
, NULL
)) != NULL
)
963 indexbuild_add(ib
, tf
);
965 realsize
= indexbuild_realsize(ib
);
968 munmap(mappedfile
, totalsize
);
969 ftruncate(fd
, realsize
);
971 printf("Actual index file size = %ju bytes\n", (intmax_t)realsize
);
973 } else if (mode
== TEXT
) {
976 fd
= open(filename
, O_RDONLY
);
978 fprintf(stderr
, "%s: %s: open: %s\n", PNAME
, filename
,
982 if (fstat(fd
, &st
) < 0) {
983 perror("agedu: fstat");
986 totalsize
= st
.st_size
;
987 mappedfile
= mmap(NULL
, totalsize
, PROT_READ
, MAP_SHARED
, fd
, 0);
989 perror("agedu: mmap");
992 pathsep
= trie_pathsep(mappedfile
);
995 * Trim trailing slash, just in case.
997 pathlen
= strlen(querydir
);
998 if (pathlen
> 0 && querydir
[pathlen
-1] == pathsep
)
999 querydir
[--pathlen
] = '\0';
1001 text_query(mappedfile
, querydir
, textcutoff
, 1);
1002 } else if (mode
== HTML
) {
1004 struct html_config cfg
;
1008 fd
= open(filename
, O_RDONLY
);
1010 fprintf(stderr
, "%s: %s: open: %s\n", PNAME
, filename
,
1014 if (fstat(fd
, &st
) < 0) {
1015 perror("agedu: fstat");
1018 totalsize
= st
.st_size
;
1019 mappedfile
= mmap(NULL
, totalsize
, PROT_READ
, MAP_SHARED
, fd
, 0);
1021 perror("agedu: mmap");
1024 pathsep
= trie_pathsep(mappedfile
);
1027 * Trim trailing slash, just in case.
1029 pathlen
= strlen(querydir
);
1030 if (pathlen
> 0 && querydir
[pathlen
-1] == pathsep
)
1031 querydir
[--pathlen
] = '\0';
1033 xi
= trie_before(mappedfile
, querydir
);
1035 cfg
.autoage
= htmlautoagerange
;
1036 cfg
.oldest
= htmloldest
;
1037 cfg
.newest
= htmlnewest
;
1038 html
= html_query(mappedfile
, xi
, &cfg
);
1039 fputs(html
, stdout
);
1040 } else if (mode
== DUMP
) {
1044 fd
= open(filename
, O_RDONLY
);
1046 fprintf(stderr
, "%s: %s: open: %s\n", PNAME
, filename
,
1050 if (fstat(fd
, &st
) < 0) {
1051 perror("agedu: fstat");
1054 totalsize
= st
.st_size
;
1055 mappedfile
= mmap(NULL
, totalsize
, PROT_READ
, MAP_SHARED
, fd
, 0);
1057 perror("agedu: mmap");
1060 pathsep
= trie_pathsep(mappedfile
);
1062 maxpathlen
= trie_maxpathlen(mappedfile
);
1063 buf
= snewn(maxpathlen
, char);
1065 printf("agedu dump file. pathsep=%02x\n", (unsigned char)pathsep
);
1066 tw
= triewalk_new(mappedfile
);
1067 while ((tf
= triewalk_next(tw
, buf
)) != NULL
)
1070 } else if (mode
== HTTPD
) {
1071 struct html_config pcfg
;
1072 struct httpd_config dcfg
;
1074 fd
= open(filename
, O_RDONLY
);
1076 fprintf(stderr
, "%s: %s: open: %s\n", PNAME
, filename
,
1080 if (fstat(fd
, &st
) < 0) {
1081 perror("agedu: fstat");
1084 totalsize
= st
.st_size
;
1085 mappedfile
= mmap(NULL
, totalsize
, PROT_READ
, MAP_SHARED
, fd
, 0);
1087 perror("agedu: mmap");
1090 pathsep
= trie_pathsep(mappedfile
);
1092 dcfg
.address
= httpserveraddr
;
1093 dcfg
.port
= httpserverport
;
1094 dcfg
.basicauthdata
= httpauthdata
;
1096 pcfg
.autoage
= htmlautoagerange
;
1097 pcfg
.oldest
= htmloldest
;
1098 pcfg
.newest
= htmlnewest
;
1099 run_httpd(mappedfile
, auth
, &dcfg
, &pcfg
);