Arrange that the --html output mode delivers a sensible error
authorsimon <simon@cda61777-01e9-0310-a592-d414129be87e>
Fri, 29 May 2009 17:45:35 +0000 (17:45 +0000)
committersimon <simon@cda61777-01e9-0310-a592-d414129be87e>
Fri, 29 May 2009 17:45:35 +0000 (17:45 +0000)
message in place of a segfault, if you hand it a pathname it can't
find.

git-svn-id: svn://svn.tartarus.org/sgt/agedu@8587 cda61777-01e9-0310-a592-d414129be87e

agedu.c
index.c
index.h

diff --git a/agedu.c b/agedu.c
index 99fa1f8..7f36956 100644 (file)
--- a/agedu.c
+++ b/agedu.c
@@ -1228,7 +1228,8 @@ int main(int argc, char **argv)
            munmap(mappedfile, totalsize);
        } else if (mode == HTML) {
            char *querydir = actions[action].arg;
-           size_t pathlen;
+           size_t pathlen, maxpathlen;
+           char *pathbuf;
            struct html_config cfg;
            unsigned long xi;
            char *html;
@@ -1251,6 +1252,9 @@ int main(int argc, char **argv)
            }
            pathsep = trie_pathsep(mappedfile);
 
+           maxpathlen = trie_maxpathlen(mappedfile);
+           pathbuf = snewn(maxpathlen, char);
+
            /*
             * Trim trailing slash, just in case.
             */
@@ -1259,14 +1263,29 @@ int main(int argc, char **argv)
                querydir[--pathlen] = '\0';
 
            xi = trie_before(mappedfile, querydir);
-           cfg.format = NULL;
-           cfg.autoage = htmlautoagerange;
-           cfg.oldest = htmloldest;
-           cfg.newest = htmlnewest;
-           html = html_query(mappedfile, xi, &cfg);
-           fputs(html, stdout);
+           if (xi >= trie_count(mappedfile) ||
+               (trie_getpath(mappedfile, xi, pathbuf),
+                strcmp(pathbuf, querydir))) {
+               fprintf(stderr, "%s: pathname '%s' does not exist in index\n"
+                       "%*s(check it is spelled exactly as it is in the "
+                       "index, including\n%*sany leading './')\n",
+                       PNAME, querydir,
+                       (int)(1+sizeof(PNAME)), "",
+                       (int)(1+sizeof(PNAME)), "");
+           } else if (!index_has_root(mappedfile, xi)) {
+               fprintf(stderr, "%s: pathname '%s' is"
+                       " a file, not a directory\n", PNAME, querydir);
+           } else {
+               cfg.format = NULL;
+               cfg.autoage = htmlautoagerange;
+               cfg.oldest = htmloldest;
+               cfg.newest = htmlnewest;
+               html = html_query(mappedfile, xi, &cfg);
+               fputs(html, stdout);
+           }
 
            munmap(mappedfile, totalsize);
+           sfree(pathbuf);
        } else if (mode == DUMP) {
            size_t maxpathlen;
            char *buf;
diff --git a/index.c b/index.c
index 1cdb2ea..a74c165 100644 (file)
--- a/index.c
+++ b/index.c
@@ -283,6 +283,19 @@ void indexbuild_free(indexbuild *ib)
     sfree(ib);
 }
 
+int index_has_root(const void *t, int n)
+{
+    const off_t *roots;
+
+    roots = (const off_t *)((const char *)t + trie_get_index_offset(t));
+
+    if (n == 0)
+       return 1;
+    if (n < 0 || n >= trie_count(t) || !roots[n-1])
+       return 0;
+    return 1;
+}
+
 unsigned long long index_query(const void *t, int n, unsigned long long at)
 {
     const off_t *roots;
diff --git a/index.h b/index.h
index bc76900..97b9b65 100644 (file)
--- a/index.h
+++ b/index.h
@@ -51,6 +51,12 @@ off_t indexbuild_realsize(indexbuild *ib);
 void indexbuild_free(indexbuild *ib);
 
 /*
+ * Check to see if a name index has an index tree root available
+ * (i.e. represents a directory rather than a file).
+ */
+int index_has_root(const void *t, int n);
+
+/*
  * Query an index to find the total size of records with name
  * index strictly less than n, with atime less than at.
  */