Invent a cunning means of faking plausible atimes for directories,
[sgt/agedu] / agedu.c
diff --git a/agedu.c b/agedu.c
index d015824..74d1971 100644 (file)
--- a/agedu.c
+++ b/agedu.c
@@ -20,6 +20,7 @@
 #include <sys/ioctl.h>
 #include <fnmatch.h>
 
+#include "agedu.h"
 #include "du.h"
 #include "trie.h"
 #include "index.h"
 #include "httpd.h"
 #include "fgetline.h"
 
-#define PNAME "agedu"
-
-#define lenof(x) (sizeof((x))/sizeof(*(x)))
-
 /*
  * Path separator. This global variable affects the behaviour of
  * various parts of the code when they need to deal with path
@@ -69,6 +66,7 @@ struct ctx {
     struct inclusion_exclusion *inex;
     int ninex;
     int crossfs;
+    int fakeatimes;
 };
 
 static void dump_line(const char *pathname, const struct trie_file *tf)
@@ -105,7 +103,10 @@ static int gotdata(void *vctx, const char *pathname, const struct stat64 *st)
        return 0;
 
     file.size = (unsigned long long)512 * st->st_blocks;
-    file.atime = st->st_atime;
+    if (ctx->fakeatimes && S_ISDIR(st->st_mode))
+       file.atime = st->st_mtime;
+    else
+       file.atime = st->st_atime;
 
     /*
      * Filter based on wildcards.
@@ -176,6 +177,9 @@ static void text_query(const void *mappedfile, const char *querydir,
     xi1 = trie_before(mappedfile, querydir);
     xi2 = trie_before(mappedfile, pathbuf);
 
+    if (xi2 - xi1 == 1)
+       return;                        /* file, or empty dir => no display */
+
     /*
      * Now do the lookups in the age index.
      */
@@ -261,7 +265,7 @@ static void text_query(const void *mappedfile, const char *querydir,
  */
 
 #define OPTHELP(NOVAL, VAL, SHORT, LONG, HELPPFX, HELPARG, HELPLINE, HELPOPT) \
-    HELPPFX("usage") HELPLINE("agedu [options] action [action...]") \
+    HELPPFX("usage") HELPLINE(PNAME " [options] action [action...]") \
     HELPPFX("actions") \
     VAL(SCAN) SHORT(s) LONG(scan) \
        HELPARG("directory") HELPOPT("scan and index a directory") \
@@ -302,6 +306,10 @@ static void text_query(const void *mappedfile, const char *querydir,
         HELPARG("wildcard") HELPOPT("[--scan] prune files matching pattern") \
     VAL(PRUNEPATH) LONG(prune_path) \
         HELPARG("wildcard") HELPOPT("[--scan] prune pathnames matching pattern") \
+    NOVAL(DIRATIME) LONG(dir_atime) LONG(dir_atimes) \
+        HELPOPT("[--scan] keep real atimes on directories") \
+    NOVAL(NODIRATIME) LONG(no_dir_atime) LONG(no_dir_atimes) \
+        HELPOPT("[--scan] fake atimes on directories") \
     VAL(TQDEPTH) LONG(depth) LONG(max_depth) LONG(maximum_depth) \
         HELPARG("levels") HELPOPT("[--text] recurse to this many levels") \
     VAL(MINAGE) SHORT(a) LONG(age) LONG(min_age) LONG(minimum_age) \
@@ -442,7 +450,7 @@ int main(int argc, char **argv)
     triewalk *tw;
     indexbuild *ib;
     const struct trie_file *tf;
-    char *filename = "agedu.dat";
+    char *filename = PNAME ".dat";
     int doing_opts = 1;
     enum { TEXT, HTML, SCAN, DUMP, SCANDUMP, LOAD, HTTPD };
     struct action {
@@ -462,6 +470,7 @@ int main(int argc, char **argv)
     int ninex = 0, inexsize = 0;
     int crossfs = 0;
     int tqdepth = 1;
+    int fakediratimes = 1;
 
 #ifdef DEBUG_MAD_OPTION_PARSING_MACROS
     {
@@ -692,6 +701,12 @@ int main(int argc, char **argv)
                  case OPT_NOCROSSFS:
                    crossfs = 0;
                    break;
+                 case OPT_DIRATIME:
+                   fakediratimes = 0;
+                   break;
+                 case OPT_NODIRATIME:
+                   fakediratimes = 1;
+                   break;
                  case OPT_DATAFILE:
                    filename = optval;
                    break;
@@ -739,7 +754,7 @@ int main(int argc, char **argv)
                        auth = HTTPD_AUTH_MAGIC | HTTPD_AUTH_BASIC;
                    else if (!strcmp(optval, "help") ||
                             !strcmp(optval, "list")) {
-                       printf("agedu: supported HTTP authentication types"
+                       printf(PNAME ": supported HTTP authentication types"
                               " are:\n"
                               "       magic      use Linux /proc/net/tcp to"
                               " determine owner of peer socket\n"
@@ -848,7 +863,7 @@ int main(int argc, char **argv)
                char *buf = fgetline(stdin);
                unsigned newpathsep;
                buf[strcspn(buf, "\r\n")] = '\0';
-               if (1 != sscanf(buf, "agedu dump file. pathsep=%x",
+               if (1 != sscanf(buf, DUMPHDR "%x",
                                &newpathsep)) {
                    fprintf(stderr, "%s: header in dump file not recognised\n",
                            PNAME);
@@ -869,7 +884,7 @@ int main(int argc, char **argv)
                    return 1;
                }
                if (fstat(fd, &st) < 0) {
-                   perror("agedu: fstat");
+                   perror(PNAME ": fstat");
                    return 1;
                }
                ctx->datafile_dev = st.st_dev;
@@ -893,6 +908,7 @@ int main(int argc, char **argv)
            ctx->inex = inex;
            ctx->ninex = ninex;
            ctx->crossfs = crossfs;
+           ctx->fakeatimes = fakediratimes;
 
            ctx->last_output_update = time(NULL);
 
@@ -909,7 +925,7 @@ int main(int argc, char **argv)
            }
 
            if (mode == SCANDUMP)
-               printf("agedu dump file. pathsep=%02x\n", (unsigned char)pathsep);
+               printf(DUMPHDR "%02x\n", (unsigned char)pathsep);
 
            /*
             * Scan the directory tree, and write out the trie component
@@ -991,7 +1007,7 @@ int main(int argc, char **argv)
                 * will take; enlarge the file, and memory-map it.
                 */
                if (fstat(fd, &st) < 0) {
-                   perror("agedu: fstat");
+                   perror(PNAME ": fstat");
                    return 1;
                }
 
@@ -1001,11 +1017,11 @@ int main(int argc, char **argv)
                totalsize = index_compute_size(st.st_size, count);
 
                if (lseek(fd, totalsize-1, SEEK_SET) < 0) {
-                   perror("agedu: lseek");
+                   perror(PNAME ": lseek");
                    return 1;
                }
                if (write(fd, "\0", 1) < 1) {
-                   perror("agedu: write");
+                   perror(PNAME ": write");
                    return 1;
                }
 
@@ -1014,10 +1030,16 @@ int main(int argc, char **argv)
 
                mappedfile = mmap(NULL, totalsize, PROT_READ|PROT_WRITE,MAP_SHARED, fd, 0);
                if (!mappedfile) {
-                   perror("agedu: mmap");
+                   perror(PNAME ": mmap");
                    return 1;
                }
 
+               if (fakediratimes) {
+                   printf("Faking directory atimes\n");
+                   trie_fake_dir_atimes(mappedfile);
+               }
+
+               printf("Building index\n");
                ib = indexbuild_new(mappedfile, st.st_size, count);
                tw = triewalk_new(mappedfile);
                while ((tf = triewalk_next(tw, NULL)) != NULL)
@@ -1042,13 +1064,13 @@ int main(int argc, char **argv)
                return 1;
            }
            if (fstat(fd, &st) < 0) {
-               perror("agedu: fstat");
+               perror(PNAME ": fstat");
                return 1;
            }
            totalsize = st.st_size;
            mappedfile = mmap(NULL, totalsize, PROT_READ, MAP_SHARED, fd, 0);
            if (!mappedfile) {
-               perror("agedu: mmap");
+               perror(PNAME ": mmap");
                return 1;
            }
            pathsep = trie_pathsep(mappedfile);
@@ -1075,13 +1097,13 @@ int main(int argc, char **argv)
                return 1;
            }
            if (fstat(fd, &st) < 0) {
-               perror("agedu: fstat");
+               perror(PNAME ": fstat");
                return 1;
            }
            totalsize = st.st_size;
            mappedfile = mmap(NULL, totalsize, PROT_READ, MAP_SHARED, fd, 0);
            if (!mappedfile) {
-               perror("agedu: mmap");
+               perror(PNAME ": mmap");
                return 1;
            }
            pathsep = trie_pathsep(mappedfile);
@@ -1111,13 +1133,13 @@ int main(int argc, char **argv)
                return 1;
            }
            if (fstat(fd, &st) < 0) {
-               perror("agedu: fstat");
+               perror(PNAME ": fstat");
                return 1;
            }
            totalsize = st.st_size;
            mappedfile = mmap(NULL, totalsize, PROT_READ, MAP_SHARED, fd, 0);
            if (!mappedfile) {
-               perror("agedu: mmap");
+               perror(PNAME ": mmap");
                return 1;
            }
            pathsep = trie_pathsep(mappedfile);
@@ -1125,7 +1147,7 @@ int main(int argc, char **argv)
            maxpathlen = trie_maxpathlen(mappedfile);
            buf = snewn(maxpathlen, char);
 
-           printf("agedu dump file. pathsep=%02x\n", (unsigned char)pathsep);
+           printf(DUMPHDR "%02x\n", (unsigned char)pathsep);
            tw = triewalk_new(mappedfile);
            while ((tf = triewalk_next(tw, buf)) != NULL)
                dump_line(buf, tf);
@@ -1141,13 +1163,13 @@ int main(int argc, char **argv)
                return 1;
            }
            if (fstat(fd, &st) < 0) {
-               perror("agedu: fstat");
+               perror(PNAME ": fstat");
                return 1;
            }
            totalsize = st.st_size;
            mappedfile = mmap(NULL, totalsize, PROT_READ, MAP_SHARED, fd, 0);
            if (!mappedfile) {
-               perror("agedu: mmap");
+               perror(PNAME ": mmap");
                return 1;
            }
            pathsep = trie_pathsep(mappedfile);