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)
{
line++;
}
} else {
- du(scandir, gotdata, ctx);
+ du(scandir, gotdata, scan_error, ctx);
}
if (mode != SCANDUMP) {
size_t maxpathlen;
}
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;
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;
}
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) {
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;
path = snewn(pathsize, char);
strcpy(path, inpath);
- du_recurse(&path, pathlen, &pathsize, gotdata, gotdata_ctx);
+ du_recurse(&path, pathlen, &pathsize, gotdata, err, gotdata_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);