Tinker slightly with the interaction of progress reports and error
authorsimon <simon@cda61777-01e9-0310-a592-d414129be87e>
Fri, 7 Nov 2008 18:56:56 +0000 (18:56 +0000)
committersimon <simon@cda61777-01e9-0310-a592-d414129be87e>
Fri, 7 Nov 2008 18:56:56 +0000 (18:56 +0000)
messages during a disk scan, so that the latter don't end up with
the remnants of one of the former at the end.

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

agedu.c
du.c
du.h

diff --git a/agedu.c b/agedu.c
index 50affaa..c1ed291 100644 (file)
--- a/agedu.c
+++ b/agedu.c
@@ -140,6 +140,24 @@ static int gotdata(void *vctx, const char *pathname, const STRUCT_STAT *st)
     return 1;
 }
 
+static void scan_error(void *vctx, const char *fmt, ...)
+{
+    struct ctx *ctx = (struct ctx *)vctx;
+    va_list ap;
+
+    if (ctx->progress) {
+       fprintf(stderr, "%-*s\r", ctx->progwidth, "");
+       fflush(stderr);
+    }
+
+    fprintf(stderr, "%s: ", PNAME);
+    va_start(ap, fmt);
+    vfprintf(stderr, fmt, ap);
+    va_end(ap);
+
+    ctx->last_output_update--;        /* force a progress report next time */
+}
+
 static void text_query(const void *mappedfile, const char *querydir,
                       time_t t, int depth)
 {
@@ -1012,7 +1030,7 @@ int main(int argc, char **argv)
                    line++;
                }
            } else {
-               du(scandir, gotdata, ctx);
+               du(scandir, gotdata, scan_error, ctx);
            }
            if (mode != SCANDUMP) {
                size_t maxpathlen;
diff --git a/du.c b/du.c
index 3c8d5ee..e49b0d3 100644 (file)
--- a/du.c
+++ b/du.c
@@ -170,7 +170,7 @@ static int str_cmp(const void *av, const void *bv)
 }
 
 static void du_recurse(char **path, size_t pathlen, size_t *pathsize,
-                      gotdata_fn_t gotdata, void *gotdata_ctx)
+                      gotdata_fn_t gotdata, err_fn_t err, void *gotdata_ctx)
 {
     const char *name;
     dirhandle d;
@@ -179,7 +179,7 @@ static void du_recurse(char **path, size_t pathlen, size_t *pathsize,
     size_t i, nnames, namesize;
 
     if (LSTAT(*path, &st) < 0) {
-       fprintf(stderr, "%s: lstat: %s\n", *path, strerror(errno));
+       err(gotdata_ctx, "%s: lstat: %s\n", *path, strerror(errno));
        return;
     }
 
@@ -193,7 +193,7 @@ static void du_recurse(char **path, size_t pathlen, size_t *pathsize,
     nnames = namesize = 0;
 
     if (open_dir(*path, &d) < 0) {
-       fprintf(stderr, "%s: opendir: %s\n", *path, strerror(errno));
+       err(gotdata_ctx, "%s: opendir: %s\n", *path, strerror(errno));
        return;
     }
     while ((name = read_dir(&d)) != NULL) {
@@ -231,14 +231,15 @@ static void du_recurse(char **path, size_t pathlen, size_t *pathsize,
            sprintf(*path + pathlen, "/%s", names[i]);
        }
 
-       du_recurse(path, newpathlen, pathsize, gotdata, gotdata_ctx);
+       du_recurse(path, newpathlen, pathsize, gotdata, err, gotdata_ctx);
 
        sfree(names[i]);
     }
     sfree(names);
 }
 
-void du(const char *inpath, gotdata_fn_t gotdata, void *gotdata_ctx)
+void du(const char *inpath, gotdata_fn_t gotdata, err_fn_t err,
+       void *gotdata_ctx)
 {
     char *path;
     size_t pathlen, pathsize;
@@ -248,5 +249,5 @@ void du(const char *inpath, gotdata_fn_t gotdata, void *gotdata_ctx)
     path = snewn(pathsize, char);
     strcpy(path, inpath);
 
-    du_recurse(&path, pathlen, &pathsize, gotdata, gotdata_ctx);
+    du_recurse(&path, pathlen, &pathsize, gotdata, err, gotdata_ctx);
 }
diff --git a/du.h b/du.h
index c0b73e8..670c572 100644 (file)
--- a/du.h
+++ b/du.h
@@ -18,7 +18,14 @@ typedef int (*gotdata_fn_t)(void *ctx,
                            const struct stat64 *st);
 
 /*
+ * Function called to report an error during scanning. The ctx is
+ * the same one passed to gotdata_fn_t.
+ */
+typedef void (*err_fn_t)(void *vctx, const char *fmt, ...);
+
+/*
  * Recursively scan a directory tree and report every
  * space-consuming item in it to gotdata().
  */
-void du(const char *path, gotdata_fn_t gotdata, void *gotdata_ctx);
+void du(const char *path, gotdata_fn_t gotdata, err_fn_t err,
+       void *gotdata_ctx);