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 strcpy(pathbuf
, querydir
);
175 make_successor(pathbuf
);
176 xi1
= trie_before(mappedfile
, querydir
);
177 xi2
= trie_before(mappedfile
, pathbuf
);
180 * Now do the lookups in the age index.
182 s1
= index_query(mappedfile
, xi1
, t
);
183 s2
= index_query(mappedfile
, xi2
, t
);
186 return; /* no space taken up => no display */
190 * Now scan for first-level subdirectories and report
195 trie_getpath(mappedfile
, xi1
, pathbuf
);
196 text_query(mappedfile
, pathbuf
, t
, depth
-1);
197 make_successor(pathbuf
);
198 xi1
= trie_before(mappedfile
, pathbuf
);
202 /* Display in units of 1Kb */
203 printf("%-11llu %s\n", (s2
- s1
) / 1024, querydir
);
207 * Largely frivolous way to define all my command-line options. I
208 * present here a parametric macro which declares a series of
209 * _logical_ option identifiers, and for each one declares zero or
210 * more short option characters and zero or more long option
211 * words. Then I repeatedly invoke that macro with its arguments
212 * defined to be various other macros, which allows me to
215 * - define an enum allocating a distinct integer value to each
217 * - define a string consisting of precisely all the short option
219 * - define a string array consisting of all the long option
221 * - define (with help from auxiliary enums) integer arrays
222 * parallel to both of the above giving the logical option id
223 * for each physical short and long option
224 * - define an array indexed by logical option id indicating
225 * whether the option in question takes a value
226 * - define a function which prints out brief online help for all
229 * It's not at all clear to me that this trickery is actually
230 * particularly _efficient_ - it still, after all, requires going
231 * linearly through the option list at run time and doing a
232 * strcmp, whereas in an ideal world I'd have liked the lists of
233 * long and short options to be pre-sorted so that a binary search
234 * or some other more efficient lookup was possible. (Not that
235 * asymptotic algorithmic complexity is remotely vital in option
236 * parsing, but if I were doing this in, say, Lisp or something
237 * with an equivalently powerful preprocessor then once I'd had
238 * the idea of preparing the option-parsing data structures at
239 * compile time I would probably have made the effort to prepare
240 * them _properly_. I could have Perl generate me a source file
241 * from some sort of description, I suppose, but that would seem
242 * like overkill. And in any case, it's more of a challenge to
243 * achieve as much as possible by cunning use of cpp and enum than
244 * to just write some sensible and logical code in a Turing-
245 * complete language. I said it was largely frivolous :-)
247 * This approach does have the virtue that it brings together the
248 * option ids, option spellings and help text into a single
249 * combined list and defines them all in exactly one place. If I
250 * want to add a new option, or a new spelling for an option, I
251 * only have to modify the main OPTHELP macro below and then add
252 * code to process the new logical id.
254 * (Though, really, even that isn't ideal, since it still involves
255 * modifying the source file in more than one place. In a
256 * _properly_ ideal world, I'd be able to interleave the option
257 * definitions with the code fragments that process them. And then
258 * not bother defining logical identifiers for them at all - those
259 * would be automatically generated, since I wouldn't have any
260 * need to specify them manually in another part of the code.)
263 #define OPTHELP(NOVAL, VAL, SHORT, LONG, HELPPFX, HELPARG, HELPLINE, HELPOPT) \
264 HELPPFX("usage") HELPLINE("agedu [options] action [action...]") \
266 VAL(SCAN) SHORT(s) LONG(scan) \
267 HELPARG("directory") HELPOPT("scan and index a directory") \
268 NOVAL(DUMP) SHORT(d) LONG(dump) HELPOPT("dump the index file on stdout") \
269 VAL(SCANDUMP) SHORT(S) LONG(scan_dump) \
270 HELPARG("directory") HELPOPT("scan only, generating a dump") \
271 NOVAL(LOAD) SHORT(l) LONG(load) \
272 HELPOPT("load and index a dump file") \
273 VAL(TEXT) SHORT(t) LONG(text) \
274 HELPARG("subdir") HELPOPT("print a plain text report on a subdirectory") \
275 VAL(HTML) SHORT(H) LONG(html) \
276 HELPARG("subdir") HELPOPT("print an HTML report on a subdirectory") \
277 NOVAL(HTTPD) SHORT(w) LONG(web) LONG(server) LONG(httpd) \
278 HELPOPT("serve HTML reports from a temporary web server") \
280 VAL(DATAFILE) SHORT(f) LONG(file) \
281 HELPARG("filename") HELPOPT("[all modes] specify index file") \
282 NOVAL(PROGRESS) LONG(progress) LONG(scan_progress) \
283 HELPOPT("[--scan] report progress on stderr") \
284 NOVAL(NOPROGRESS) LONG(no_progress) LONG(no_scan_progress) \
285 HELPOPT("[--scan] do not report progress") \
286 NOVAL(TTYPROGRESS) LONG(tty_progress) LONG(tty_scan_progress) \
287 LONG(progress_tty) LONG(scan_progress_tty) \
288 HELPOPT("[--scan] report progress if stderr is a tty") \
289 NOVAL(CROSSFS) LONG(cross_fs) \
290 HELPOPT("[--scan] cross filesystem boundaries") \
291 NOVAL(NOCROSSFS) LONG(no_cross_fs) \
292 HELPOPT("[--scan] stick to one filesystem") \
293 VAL(INCLUDE) LONG(include) \
294 HELPARG("wildcard") HELPOPT("[--scan] include files matching pattern") \
295 VAL(INCLUDEPATH) LONG(include_path) \
296 HELPARG("wildcard") HELPOPT("[--scan] include pathnames matching pattern") \
297 VAL(EXCLUDE) LONG(exclude) \
298 HELPARG("wildcard") HELPOPT("[--scan] exclude files matching pattern") \
299 VAL(EXCLUDEPATH) LONG(exclude_path) \
300 HELPARG("wildcard") HELPOPT("[--scan] exclude pathnames matching pattern") \
301 VAL(PRUNE) LONG(prune) \
302 HELPARG("wildcard") HELPOPT("[--scan] prune files matching pattern") \
303 VAL(PRUNEPATH) LONG(prune_path) \
304 HELPARG("wildcard") HELPOPT("[--scan] prune pathnames matching pattern") \
305 VAL(TQDEPTH) LONG(depth) LONG(max_depth) LONG(maximum_depth) \
306 HELPARG("levels") HELPOPT("[--text] recurse to this many levels") \
307 VAL(MINAGE) SHORT(a) LONG(age) LONG(min_age) LONG(minimum_age) \
308 HELPARG("age") HELPOPT("[--text] include only files older than this") \
309 VAL(AGERANGE) SHORT(r) LONG(age_range) LONG(range) LONG(ages) \
310 HELPARG("age[-age]") HELPOPT("[--html,--web] set limits of colour coding") \
311 VAL(SERVERADDR) LONG(address) LONG(addr) LONG(server_address) \
313 HELPARG("addr[:port]") HELPOPT("[--web] specify HTTP server address") \
314 VAL(AUTH) LONG(auth) LONG(http_auth) LONG(httpd_auth) \
315 LONG(server_auth) LONG(web_auth) \
316 HELPARG("type") HELPOPT("[--web] specify HTTP authentication method") \
317 VAL(AUTHFILE) LONG(auth_file) \
318 HELPARG("filename") HELPOPT("[--web] read HTTP Basic user/pass from file") \
319 VAL(AUTHFD) LONG(auth_fd) \
320 HELPARG("fd") HELPOPT("[--web] read HTTP Basic user/pass from fd") \
322 NOVAL(HELP) SHORT(h) LONG(help) HELPOPT("display this help text") \
323 NOVAL(VERSION) SHORT(V) LONG(version) HELPOPT("report version number") \
324 NOVAL(LICENCE) LONG(licence) LONG(license) \
325 HELPOPT("display (MIT) licence text") \
328 #define DEFENUM(x) OPT_ ## x,
331 #define STRING(x) #x ,
332 #define STRINGNOCOMMA(x) #x
333 #define SHORTNEWOPT(x) SHORTtmp_ ## x = OPT_ ## x,
334 #define SHORTTHISOPT(x) SHORTtmp2_ ## x, SHORTVAL_ ## x = SHORTtmp2_ ## x - 1,
335 #define SHORTOPTVAL(x) SHORTVAL_ ## x,
336 #define SHORTTMP(x) SHORTtmp3_ ## x,
337 #define LONGNEWOPT(x) LONGtmp_ ## x = OPT_ ## x,
338 #define LONGTHISOPT(x) LONGtmp2_ ## x, LONGVAL_ ## x = LONGtmp2_ ## x - 1,
339 #define LONGOPTVAL(x) LONGVAL_ ## x,
340 #define LONGTMP(x) SHORTtmp3_ ## x,
342 #define OPTIONS(NOVAL, VAL, SHORT, LONG) \
343 OPTHELP(NOVAL, VAL, SHORT, LONG, IGNORE, IGNORE, IGNORE, IGNORE)
345 enum { OPTIONS(DEFENUM
,DEFENUM
,IGNORE
,IGNORE
) NOPTIONS
};
346 enum { OPTIONS(IGNORE
,IGNORE
,SHORTTMP
,IGNORE
) NSHORTOPTS
};
347 enum { OPTIONS(IGNORE
,IGNORE
,IGNORE
,LONGTMP
) NLONGOPTS
};
348 static const int opthasval
[NOPTIONS
] = {OPTIONS(ZERO
,ONE
,IGNORE
,IGNORE
)};
349 static const char shortopts
[] = {OPTIONS(IGNORE
,IGNORE
,STRINGNOCOMMA
,IGNORE
)};
350 static const char *const longopts
[] = {OPTIONS(IGNORE
,IGNORE
,IGNORE
,STRING
)};
351 enum { OPTIONS(SHORTNEWOPT
,SHORTNEWOPT
,SHORTTHISOPT
,IGNORE
) };
352 enum { OPTIONS(LONGNEWOPT
,LONGNEWOPT
,IGNORE
,LONGTHISOPT
) };
353 static const int shortvals
[] = {OPTIONS(IGNORE
,IGNORE
,SHORTOPTVAL
,IGNORE
)};
354 static const int longvals
[] = {OPTIONS(IGNORE
,IGNORE
,IGNORE
,LONGOPTVAL
)};
356 static void usage(FILE *fp
)
359 const char *prefix
, *shortopt
, *longopt
, *optarg
;
362 #define HELPRESET prefix = shortopt = longopt = optarg = NULL, optex = -1
363 #define HELPNOVAL(s) optex = 0;
364 #define HELPVAL(s) optex = 1;
365 #define HELPSHORT(s) if (!shortopt) shortopt = "-" #s;
366 #define HELPLONG(s) if (!longopt) { \
367 strcpy(longbuf, "--" #s); longopt = longbuf; \
368 for (i = 0; longbuf[i]; i++) if (longbuf[i] == '_') longbuf[i] = '-'; }
369 #define HELPPFX(s) prefix = s;
370 #define HELPARG(s) optarg = s;
371 #define HELPLINE(s) assert(optex == -1); \
372 fprintf(fp, "%7s%c %s\n", prefix?prefix:"", prefix?':':' ', s); \
374 #define HELPOPT(s) assert((optex == 1 && optarg) || (optex == 0 && !optarg)); \
375 assert(shortopt || longopt); \
376 i = fprintf(fp, "%7s%c %s%s%s%s%s", prefix?prefix:"", prefix?':':' ', \
377 shortopt?shortopt:"", shortopt&&longopt?", ":"", longopt?longopt:"", \
378 optarg?" ":"", optarg?optarg:""); \
379 fprintf(fp, "%*s %s\n", i<32?32-i:0,"",s); HELPRESET;
382 OPTHELP(HELPNOVAL
, HELPVAL
, HELPSHORT
, HELPLONG
,
383 HELPPFX
, HELPARG
, HELPLINE
, HELPOPT
);
396 static time_t parse_age(time_t now
, const char *agestr
)
405 if (2 != sscanf(agestr
, "%d%1[DdWwMmYy]", &nunits
, unit
)) {
406 fprintf(stderr
, "%s: age specification should be a number followed by"
407 " one of d,w,m,y\n", PNAME
);
411 if (unit
[0] == 'd') {
413 } else if (unit
[0] == 'w') {
414 t
-= 86400 * 7 * nunits
;
419 ym
= tm
.tm_year
* 12 + tm
.tm_mon
;
426 tm
.tm_year
= ym
/ 12;
435 int main(int argc
, char **argv
)
438 struct ctx actx
, *ctx
= &actx
;
440 off_t totalsize
, realsize
;
444 const struct trie_file
*tf
;
445 char *filename
= "agedu.dat";
447 enum { TEXT
, HTML
, SCAN
, DUMP
, SCANDUMP
, LOAD
, HTTPD
};
452 int nactions
= 0, actionsize
= 0, action
;
453 time_t now
= time(NULL
);
454 time_t textcutoff
= now
, htmlnewest
= now
, htmloldest
= now
;
455 int htmlautoagerange
= 1;
456 const char *httpserveraddr
= NULL
;
457 int httpserverport
= 0;
458 const char *httpauthdata
= NULL
;
459 int auth
= HTTPD_AUTH_MAGIC
| HTTPD_AUTH_BASIC
;
461 struct inclusion_exclusion
*inex
= NULL
;
462 int ninex
= 0, inexsize
= 0;
466 #ifdef DEBUG_MAD_OPTION_PARSING_MACROS
468 static const char *const optnames
[NOPTIONS
] = {
469 OPTIONS(STRING
,STRING
,IGNORE
,IGNORE
)
472 for (i
= 0; i
< NSHORTOPTS
; i
++)
473 printf("-%c == %s [%s]\n", shortopts
[i
], optnames
[shortvals
[i
]],
474 opthasval
[shortvals
[i
]] ?
"value" : "no value");
475 for (i
= 0; i
< NLONGOPTS
; i
++)
476 printf("--%s == %s [%s]\n", longopts
[i
], optnames
[longvals
[i
]],
477 opthasval
[longvals
[i
]] ?
"value" : "no value");
484 if (doing_opts
&& *p
== '-') {
487 if (!strcmp(p
, "--")) {
498 if (wordstart
&& *p
== '-') {
500 * GNU-style long option.
503 optval
= strchr(p
, '=');
507 for (i
= 0; i
< NLONGOPTS
; i
++) {
508 const char *opt
= longopts
[i
], *s
= p
;
511 * The underscores in the option names
512 * defined above may be given by the user
513 * as underscores or dashes, or omitted
518 if (*s
== '-' || *s
== '_')
536 fprintf(stderr
, "%s: unrecognised option '--%s'\n",
541 if (!opthasval
[optid
]) {
543 fprintf(stderr
, "%s: unexpected argument to option"
544 " '--%s'\n", PNAME
, p
);
552 fprintf(stderr
, "%s: option '--%s' expects"
553 " an argument\n", PNAME
, p
);
559 p
+= strlen(p
); /* finished with this argument word */
566 for (i
= 0; i
< NSHORTOPTS
; i
++)
567 if (c
== shortopts
[i
]) {
568 optid
= shortvals
[i
];
573 fprintf(stderr
, "%s: unrecognised option '-%c'\n",
578 if (opthasval
[optid
]) {
582 } else if (--argc
> 0) {
585 fprintf(stderr
, "%s: option '-%c' expects"
586 " an argument\n", PNAME
, c
);
597 * Now actually process the option.
604 printf("FIXME: version();\n");
607 printf("FIXME: licence();\n");
610 if (nactions
>= actionsize
) {
611 actionsize
= nactions
* 3 / 2 + 16;
612 actions
= sresize(actions
, actionsize
, struct action
);
614 actions
[nactions
].mode
= SCAN
;
615 actions
[nactions
].arg
= optval
;
619 if (nactions
>= actionsize
) {
620 actionsize
= nactions
* 3 / 2 + 16;
621 actions
= sresize(actions
, actionsize
, struct action
);
623 actions
[nactions
].mode
= SCANDUMP
;
624 actions
[nactions
].arg
= optval
;
628 if (nactions
>= actionsize
) {
629 actionsize
= nactions
* 3 / 2 + 16;
630 actions
= sresize(actions
, actionsize
, struct action
);
632 actions
[nactions
].mode
= DUMP
;
633 actions
[nactions
].arg
= NULL
;
637 if (nactions
>= actionsize
) {
638 actionsize
= nactions
* 3 / 2 + 16;
639 actions
= sresize(actions
, actionsize
, struct action
);
641 actions
[nactions
].mode
= LOAD
;
642 actions
[nactions
].arg
= NULL
;
646 if (nactions
>= actionsize
) {
647 actionsize
= nactions
* 3 / 2 + 16;
648 actions
= sresize(actions
, actionsize
, struct action
);
650 actions
[nactions
].mode
= TEXT
;
651 actions
[nactions
].arg
= optval
;
655 if (nactions
>= actionsize
) {
656 actionsize
= nactions
* 3 / 2 + 16;
657 actions
= sresize(actions
, actionsize
, struct action
);
659 actions
[nactions
].mode
= HTML
;
660 actions
[nactions
].arg
= optval
;
664 if (nactions
>= actionsize
) {
665 actionsize
= nactions
* 3 / 2 + 16;
666 actions
= sresize(actions
, actionsize
, struct action
);
668 actions
[nactions
].mode
= HTTPD
;
669 actions
[nactions
].arg
= NULL
;
678 case OPT_TTYPROGRESS
:
691 tqdepth
= atoi(optval
);
694 textcutoff
= parse_age(now
, optval
);
697 if (!strcmp(optval
, "auto")) {
698 htmlautoagerange
= 1;
700 char *q
= optval
+ strcspn(optval
, "-:");
703 htmloldest
= parse_age(now
, optval
);
704 htmlnewest
= *q ?
parse_age(now
, q
) : now
;
705 htmlautoagerange
= 0;
711 if (optval
[0] == '[' &&
712 (port
= strchr(optval
, ']')) != NULL
)
716 port
+= strcspn(port
, ":");
719 httpserveraddr
= optval
;
720 httpserverport
= atoi(port
);
724 if (!strcmp(optval
, "magic"))
725 auth
= HTTPD_AUTH_MAGIC
;
726 else if (!strcmp(optval
, "basic"))
727 auth
= HTTPD_AUTH_BASIC
;
728 else if (!strcmp(optval
, "none"))
729 auth
= HTTPD_AUTH_NONE
;
730 else if (!strcmp(optval
, "default"))
731 auth
= HTTPD_AUTH_MAGIC
| HTTPD_AUTH_BASIC
;
732 else if (!strcmp(optval
, "help") ||
733 !strcmp(optval
, "list")) {
734 printf("agedu: supported HTTP authentication types"
736 " magic use Linux /proc/net/tcp to"
737 " determine owner of peer socket\n"
738 " basic HTTP Basic username and"
739 " password authentication\n"
740 " default use 'magic' if possible, "
741 " otherwise fall back to 'basic'\n"
742 " none unauthenticated HTTP (if"
743 " the data file is non-confidential)\n");
746 fprintf(stderr
, "%s: unrecognised authentication"
747 " type '%s'\n%*s options are 'magic',"
748 " 'basic', 'none', 'default'\n",
749 PNAME
, optval
, (int)strlen(PNAME
), "");
760 int authlen
, authsize
;
763 if (optid
== OPT_AUTHFILE
) {
764 fd
= open(optval
, O_RDONLY
);
766 fprintf(stderr
, "%s: %s: open: %s\n", PNAME
,
767 optval
, strerror(errno
));
774 sprintf(namebuf
, "fd %d", fd
);
779 authbuf
= snewn(authsize
, char);
780 while ((ret
= read(fd
, authbuf
+authlen
,
781 authsize
-authlen
)) > 0) {
783 if ((authsize
- authlen
) < (authsize
/ 16)) {
784 authsize
= authlen
* 3 / 2 + 4096;
785 authbuf
= sresize(authbuf
, authsize
, char);
789 fprintf(stderr
, "%s: %s: read: %s\n", PNAME
,
790 name
, strerror(errno
));
793 if (optid
== OPT_AUTHFILE
)
795 httpauthdata
= authbuf
;
799 case OPT_INCLUDEPATH
:
801 case OPT_EXCLUDEPATH
:
804 if (ninex
>= inexsize
) {
805 inexsize
= ninex
* 3 / 2 + 16;
806 inex
= sresize(inex
, inexsize
,
807 struct inclusion_exclusion
);
809 inex
[ninex
].path
= (optid
== OPT_INCLUDEPATH
||
810 optid
== OPT_EXCLUDEPATH
||
811 optid
== OPT_PRUNEPATH
);
812 inex
[ninex
].type
= (optid
== OPT_INCLUDE ?
1 :
813 optid
== OPT_INCLUDEPATH ?
1 :
814 optid
== OPT_EXCLUDE ?
0 :
815 optid
== OPT_EXCLUDEPATH ?
0 :
816 optid
== OPT_PRUNE ?
-1 :
817 /* optid == OPT_PRUNEPATH ? */ -1);
818 inex
[ninex
].wildcard
= optval
;
824 fprintf(stderr
, "%s: unexpected argument '%s'\n", PNAME
, p
);
834 for (action
= 0; action
< nactions
; action
++) {
835 int mode
= actions
[action
].mode
;
837 if (mode
== SCAN
|| mode
== SCANDUMP
|| mode
== LOAD
) {
838 const char *scandir
= actions
[action
].arg
;
840 char *buf
= fgetline(stdin
);
842 buf
[strcspn(buf
, "\r\n")] = '\0';
843 if (1 != sscanf(buf
, "agedu dump file. pathsep=%x",
845 fprintf(stderr
, "%s: header in dump file not recognised\n",
849 pathsep
= (char)newpathsep
;
853 if (mode
== SCAN
|| mode
== LOAD
) {
855 * Prepare to write out the index file.
857 fd
= open(filename
, O_RDWR
| O_TRUNC
| O_CREAT
, S_IRWXU
);
859 fprintf(stderr
, "%s: %s: open: %s\n", PNAME
, filename
,
863 if (fstat(fd
, &st
) < 0) {
864 perror("agedu: fstat");
867 ctx
->datafile_dev
= st
.st_dev
;
868 ctx
->datafile_ino
= st
.st_ino
;
869 ctx
->straight_to_dump
= 0;
871 ctx
->datafile_dev
= -1;
872 ctx
->datafile_ino
= -1;
873 ctx
->straight_to_dump
= 1;
876 if (mode
== SCAN
|| mode
== SCANDUMP
) {
877 if (stat(scandir
, &st
) < 0) {
878 fprintf(stderr
, "%s: %s: stat: %s\n", PNAME
, scandir
,
882 ctx
->filesystem_dev
= crossfs ?
0 : st
.st_dev
;
887 ctx
->crossfs
= crossfs
;
889 ctx
->last_output_update
= time(NULL
);
891 /* progress==1 means report progress only if stderr is a tty */
893 progress
= isatty(2) ?
2 : 0;
894 ctx
->progress
= progress
;
897 if (progress
&& ioctl(2, TIOCGWINSZ
, &ws
) == 0)
898 ctx
->progwidth
= ws
.ws_col
- 1;
903 if (mode
== SCANDUMP
)
904 printf("agedu dump file. pathsep=%02x\n", (unsigned char)pathsep
);
907 * Scan the directory tree, and write out the trie component
910 if (mode
!= SCANDUMP
) {
911 ctx
->tb
= triebuild_new(fd
);
916 while ((buf
= fgetline(stdin
)) != NULL
) {
920 buf
[strcspn(buf
, "\r\n")] = '\0';
924 while (*p
&& *p
!= ' ') p
++;
926 fprintf(stderr
, "%s: dump file line %d: expected at least"
927 " three fields\n", PNAME
, line
);
931 tf
.size
= strtoull(q
, NULL
, 10);
933 while (*p
&& *p
!= ' ') p
++;
935 fprintf(stderr
, "%s: dump file line %d: expected at least"
936 " three fields\n", PNAME
, line
);
940 tf
.atime
= strtoull(q
, NULL
, 10);
948 for (i
= 0; i
< 2; i
++) {
949 if (*p
>= '0' && *p
<= '9')
951 else if (*p
>= 'A' && *p
<= 'F')
952 c
+= *p
- ('A' - 10);
953 else if (*p
>= 'a' && *p
<= 'f')
954 c
+= *p
- ('a' - 10);
956 fprintf(stderr
, "%s: dump file line %d: unable"
957 " to parse hex escape\n", PNAME
, line
);
966 triebuild_add(ctx
->tb
, buf
, &tf
);
970 du(scandir
, gotdata
, ctx
);
972 if (mode
!= SCANDUMP
) {
973 count
= triebuild_finish(ctx
->tb
);
974 triebuild_free(ctx
->tb
);
977 fprintf(stderr
, "%-*s\r", ctx
->progwidth
, "");
982 * Work out how much space the cumulative index trees
983 * will take; enlarge the file, and memory-map it.
985 if (fstat(fd
, &st
) < 0) {
986 perror("agedu: fstat");
990 printf("Built pathname index, %d entries, %ju bytes\n", count
,
991 (intmax_t)st
.st_size
);
993 totalsize
= index_compute_size(st
.st_size
, count
);
995 if (lseek(fd
, totalsize
-1, SEEK_SET
) < 0) {
996 perror("agedu: lseek");
999 if (write(fd
, "\0", 1) < 1) {
1000 perror("agedu: write");
1004 printf("Upper bound on index file size = %ju bytes\n",
1005 (intmax_t)totalsize
);
1007 mappedfile
= mmap(NULL
, totalsize
, PROT_READ
|PROT_WRITE
,MAP_SHARED
, fd
, 0);
1009 perror("agedu: mmap");
1013 ib
= indexbuild_new(mappedfile
, st
.st_size
, count
);
1014 tw
= triewalk_new(mappedfile
);
1015 while ((tf
= triewalk_next(tw
, NULL
)) != NULL
)
1016 indexbuild_add(ib
, tf
);
1018 realsize
= indexbuild_realsize(ib
);
1019 indexbuild_free(ib
);
1021 munmap(mappedfile
, totalsize
);
1022 ftruncate(fd
, realsize
);
1024 printf("Actual index file size = %ju bytes\n", (intmax_t)realsize
);
1026 } else if (mode
== TEXT
) {
1027 char *querydir
= actions
[action
].arg
;
1030 fd
= open(filename
, O_RDONLY
);
1032 fprintf(stderr
, "%s: %s: open: %s\n", PNAME
, filename
,
1036 if (fstat(fd
, &st
) < 0) {
1037 perror("agedu: fstat");
1040 totalsize
= st
.st_size
;
1041 mappedfile
= mmap(NULL
, totalsize
, PROT_READ
, MAP_SHARED
, fd
, 0);
1043 perror("agedu: mmap");
1046 pathsep
= trie_pathsep(mappedfile
);
1049 * Trim trailing slash, just in case.
1051 pathlen
= strlen(querydir
);
1052 if (pathlen
> 0 && querydir
[pathlen
-1] == pathsep
)
1053 querydir
[--pathlen
] = '\0';
1055 text_query(mappedfile
, querydir
, textcutoff
, tqdepth
);
1056 } else if (mode
== HTML
) {
1057 char *querydir
= actions
[action
].arg
;
1059 struct html_config cfg
;
1063 fd
= open(filename
, O_RDONLY
);
1065 fprintf(stderr
, "%s: %s: open: %s\n", PNAME
, filename
,
1069 if (fstat(fd
, &st
) < 0) {
1070 perror("agedu: fstat");
1073 totalsize
= st
.st_size
;
1074 mappedfile
= mmap(NULL
, totalsize
, PROT_READ
, MAP_SHARED
, fd
, 0);
1076 perror("agedu: mmap");
1079 pathsep
= trie_pathsep(mappedfile
);
1082 * Trim trailing slash, just in case.
1084 pathlen
= strlen(querydir
);
1085 if (pathlen
> 0 && querydir
[pathlen
-1] == pathsep
)
1086 querydir
[--pathlen
] = '\0';
1088 xi
= trie_before(mappedfile
, querydir
);
1090 cfg
.autoage
= htmlautoagerange
;
1091 cfg
.oldest
= htmloldest
;
1092 cfg
.newest
= htmlnewest
;
1093 html
= html_query(mappedfile
, xi
, &cfg
);
1094 fputs(html
, stdout
);
1095 } else if (mode
== DUMP
) {
1099 fd
= open(filename
, O_RDONLY
);
1101 fprintf(stderr
, "%s: %s: open: %s\n", PNAME
, filename
,
1105 if (fstat(fd
, &st
) < 0) {
1106 perror("agedu: fstat");
1109 totalsize
= st
.st_size
;
1110 mappedfile
= mmap(NULL
, totalsize
, PROT_READ
, MAP_SHARED
, fd
, 0);
1112 perror("agedu: mmap");
1115 pathsep
= trie_pathsep(mappedfile
);
1117 maxpathlen
= trie_maxpathlen(mappedfile
);
1118 buf
= snewn(maxpathlen
, char);
1120 printf("agedu dump file. pathsep=%02x\n", (unsigned char)pathsep
);
1121 tw
= triewalk_new(mappedfile
);
1122 while ((tf
= triewalk_next(tw
, buf
)) != NULL
)
1125 } else if (mode
== HTTPD
) {
1126 struct html_config pcfg
;
1127 struct httpd_config dcfg
;
1129 fd
= open(filename
, O_RDONLY
);
1131 fprintf(stderr
, "%s: %s: open: %s\n", PNAME
, filename
,
1135 if (fstat(fd
, &st
) < 0) {
1136 perror("agedu: fstat");
1139 totalsize
= st
.st_size
;
1140 mappedfile
= mmap(NULL
, totalsize
, PROT_READ
, MAP_SHARED
, fd
, 0);
1142 perror("agedu: mmap");
1145 pathsep
= trie_pathsep(mappedfile
);
1147 dcfg
.address
= httpserveraddr
;
1148 dcfg
.port
= httpserverport
;
1149 dcfg
.basicauthdata
= httpauthdata
;
1151 pcfg
.autoage
= htmlautoagerange
;
1152 pcfg
.oldest
= htmloldest
;
1153 pcfg
.newest
= htmlnewest
;
1154 run_httpd(mappedfile
, auth
, &dcfg
, &pcfg
);