Add SILENT flag to recurse()

recurse() is getting smarter every day. I expect it to pass the Turing
test in a few months.
Along the way, it was reported that "rm -f" on nonexistant files reports
their missing as an internal recurse()-error.
So recurse() knows when to shut up, I added the SILENT flag to fix all
these things.
This commit is contained in:
FRIGN 2015-04-19 14:00:47 +02:00 committed by sin
parent 7b2465c101
commit f83d7bc647
4 changed files with 19 additions and 13 deletions

1
fs.h
View File

@ -20,6 +20,7 @@ struct recursor {
enum { enum {
SAMEDEV = 1 << 0, SAMEDEV = 1 << 0,
DIRFIRST = 1 << 1, DIRFIRST = 1 << 1,
SILENT = 1 << 2,
}; };
extern int cp_aflag; extern int cp_aflag;

View File

@ -33,8 +33,10 @@ recurse(const char *path, void *data, struct recursor *r)
} }
if (statf(path, &st) < 0) { if (statf(path, &st) < 0) {
weprintf("%s %s:", statf_name, path); if (!(r->flags & SILENT)) {
recurse_status = 1; weprintf("%s %s:", statf_name, path);
recurse_status = 1;
}
return; return;
} }
if (!S_ISDIR(st.st_mode)) { if (!S_ISDIR(st.st_mode)) {
@ -53,8 +55,10 @@ recurse(const char *path, void *data, struct recursor *r)
return; return;
if (!(dp = opendir(path))) { if (!(dp = opendir(path))) {
weprintf("opendir %s:", path); if (!(r->flags & SILENT)) {
recurse_status = 1; weprintf("opendir %s:", path);
recurse_status = 1;
}
return; return;
} }
@ -74,8 +78,10 @@ recurse(const char *path, void *data, struct recursor *r)
estrlcat(subpath, "/", sizeof(subpath)); estrlcat(subpath, "/", sizeof(subpath));
estrlcat(subpath, d->d_name, sizeof(subpath)); estrlcat(subpath, d->d_name, sizeof(subpath));
if (statf(subpath, &dst) < 0) { if (statf(subpath, &dst) < 0) {
weprintf("%s %s:", statf_name, subpath); if (!(r->flags & SILENT)) {
recurse_status = 1; weprintf("%s %s:", statf_name, subpath);
recurse_status = 1;
}
} else if ((r->flags & SAMEDEV) && dst.st_dev != st.st_dev) { } else if ((r->flags & SAMEDEV) && dst.st_dev != st.st_dev) {
continue; continue;
} else { } else {

View File

@ -8,7 +8,6 @@
#include "../fs.h" #include "../fs.h"
#include "../util.h" #include "../util.h"
int rm_fflag = 0;
int rm_status = 0; int rm_status = 0;
void void
@ -18,15 +17,15 @@ rm(const char *path, struct stat *st, void *data, struct recursor *r)
recurse(path, NULL, r); recurse(path, NULL, r);
if (rmdir(path) < 0) { if (rmdir(path) < 0) {
if (!rm_fflag) if (!(r->flags & SILENT))
weprintf("rmdir %s:", path); weprintf("rmdir %s:", path);
if (!(rm_fflag && errno == ENOENT)) if (!((r->flags & SILENT) && errno == ENOENT))
rm_status = 1; rm_status = 1;
} }
} else if (unlink(path) < 0) { } else if (unlink(path) < 0) {
if (!rm_fflag) if (!(r->flags & SILENT))
weprintf("unlink %s:", path); weprintf("unlink %s:", path);
if (!(rm_fflag && errno == ENOENT)) if (!((r->flags & SILENT) && errno == ENOENT))
rm_status = 1; rm_status = 1;
} }
} }

4
rm.c
View File

@ -16,7 +16,7 @@ main(int argc, char *argv[])
ARGBEGIN { ARGBEGIN {
case 'f': case 'f':
rm_fflag = 1; r.flags |= SILENT;
break; break;
case 'R': case 'R':
case 'r': case 'r':
@ -27,7 +27,7 @@ main(int argc, char *argv[])
} ARGEND; } ARGEND;
if (!argc) { if (!argc) {
if (!rm_fflag) if (!(r.flags & SILENT))
usage(); usage();
else else
return 0; return 0;