Add maxdepth to recurse()

This also makes more sense.
This commit is contained in:
FRIGN 2015-04-18 22:04:49 +02:00 committed by sin
parent e14d9412f8
commit 7b2465c101
10 changed files with 54 additions and 45 deletions

View File

@ -29,7 +29,7 @@ chgrp(const char *path, struct stat *st, void *data, struct recursor *r)
if (st && chownf(path, st->st_uid, gid) < 0) { if (st && chownf(path, st->st_uid, gid) < 0) {
weprintf("%s %s:", chownf_name, path); weprintf("%s %s:", chownf_name, path);
ret = 1; ret = 1;
} else if (!(r->flags & NODIRS) && st && S_ISDIR(st->st_mode)) { } else if (st && S_ISDIR(st->st_mode)) {
recurse(path, NULL, r); recurse(path, NULL, r);
} }
} }
@ -44,14 +44,15 @@ int
main(int argc, char *argv[]) main(int argc, char *argv[])
{ {
struct group *gr; struct group *gr;
struct recursor r = { .fn = chgrp, .hist = NULL, .depth = 0, .follow = 'P', .flags = NODIRS }; struct recursor r = { .fn = chgrp, .hist = NULL, .depth = 0, .maxdepth = 1,
.follow = 'P', .flags = 0 };
ARGBEGIN { ARGBEGIN {
case 'h': case 'h':
hflag = 1; hflag = 1;
break; break;
case 'R': case 'R':
r.flags &= ~NODIRS; r.maxdepth = 0;
break; break;
case 'H': case 'H':
case 'L': case 'L':

View File

@ -17,7 +17,7 @@ chmodr(const char *path, struct stat *st, void *data, struct recursor *r)
if (chmod(path, m) < 0) { if (chmod(path, m) < 0) {
weprintf("chmod %s:", path); weprintf("chmod %s:", path);
ret = 1; ret = 1;
} else if (!(r->flags & NODIRS) && st && S_ISDIR(st->st_mode)) { } else if (st && S_ISDIR(st->st_mode)) {
recurse(path, NULL, r); recurse(path, NULL, r);
} }
} }
@ -31,7 +31,8 @@ usage(void)
int int
main(int argc, char *argv[]) main(int argc, char *argv[])
{ {
struct recursor r = { .fn = chmodr, .hist = NULL, .depth = 0, .follow = 'P', .flags = NODIRS}; struct recursor r = { .fn = chmodr, .hist = NULL, .depth = 0, .maxdepth = 1,
.follow = 'P', .flags = 0 };
size_t i; size_t i;
argv0 = argv[0], argc--, argv++; argv0 = argv[0], argc--, argv++;
@ -42,7 +43,7 @@ main(int argc, char *argv[])
for (i = 1; (*argv)[i]; i++) { for (i = 1; (*argv)[i]; i++) {
switch ((*argv)[i]) { switch ((*argv)[i]) {
case 'R': case 'R':
r.flags &= ~NODIRS; r.maxdepth = 0;
break; break;
case 'H': case 'H':
case 'L': case 'L':

View File

@ -32,7 +32,7 @@ chownpwgr(const char *path, struct stat *st, void *data, struct recursor *r)
if (chownf(path, uid, gid) < 0) { if (chownf(path, uid, gid) < 0) {
weprintf("%s %s:", chownf_name, path); weprintf("%s %s:", chownf_name, path);
ret = 1; ret = 1;
} else if (!(r->flags & NODIRS) && st && S_ISDIR(st->st_mode)) { } else if (st && S_ISDIR(st->st_mode)) {
recurse(path, NULL, r); recurse(path, NULL, r);
} }
} }
@ -48,7 +48,8 @@ main(int argc, char *argv[])
{ {
struct group *gr; struct group *gr;
struct passwd *pw; struct passwd *pw;
struct recursor r = { .fn = chownpwgr, .hist = NULL, .depth = 0, .follow = 'P', .flags = NODIRS}; struct recursor r = { .fn = chownpwgr, .hist = NULL, .depth = 0, .maxdepth = 1,
.follow = 'P', .flags = 0 };
char *owner, *group; char *owner, *group;
ARGBEGIN { ARGBEGIN {
@ -57,7 +58,7 @@ main(int argc, char *argv[])
break; break;
case 'r': case 'r':
case 'R': case 'R':
r.flags &= ~NODIRS; r.maxdepth = 0;
break; break;
case 'H': case 'H':
case 'L': case 'L':

3
du.c
View File

@ -55,7 +55,8 @@ usage(void)
int int
main(int argc, char *argv[]) main(int argc, char *argv[])
{ {
struct recursor r = { .fn = du, .hist = NULL, .depth = 0, .follow = 'P', .flags = 0}; struct recursor r = { .fn = du, .hist = NULL, .depth = 0, .maxdepth = 0,
.follow = 'P', .flags = 0 };
size_t n = 0; size_t n = 0;
int kflag = 0, dflag = 0; int kflag = 0, dflag = 0;
char *bsize; char *bsize;

2
fs.h
View File

@ -12,6 +12,7 @@ struct recursor {
void (*fn)(const char *, struct stat *st, void *, struct recursor *); void (*fn)(const char *, struct stat *st, void *, struct recursor *);
struct history *hist; struct history *hist;
int depth; int depth;
int maxdepth;
int follow; int follow;
int flags; int flags;
}; };
@ -19,7 +20,6 @@ struct recursor {
enum { enum {
SAMEDEV = 1 << 0, SAMEDEV = 1 << 0,
DIRFIRST = 1 << 1, DIRFIRST = 1 << 1,
NODIRS = 1 << 2,
}; };
extern int cp_aflag; extern int cp_aflag;

View File

@ -37,7 +37,7 @@ recurse(const char *path, void *data, struct recursor *r)
recurse_status = 1; recurse_status = 1;
return; return;
} }
if (!S_ISDIR(st.st_mode) || (S_ISDIR(st.st_mode) && (r->flags & NODIRS))) { if (!S_ISDIR(st.st_mode)) {
(r->fn)(path, &st, data, r); (r->fn)(path, &st, data, r);
return; return;
} }
@ -61,6 +61,7 @@ recurse(const char *path, void *data, struct recursor *r)
if (!r->depth && (r->flags & DIRFIRST)) if (!r->depth && (r->flags & DIRFIRST))
(r->fn)(path, &st, data, r); (r->fn)(path, &st, data, r);
if (!r->maxdepth || r->depth + 1 < r->maxdepth) {
while ((d = readdir(dp))) { while ((d = readdir(dp))) {
if (r->follow == 'H') { if (r->follow == 'H') {
statf_name = "lstat"; statf_name = "lstat";
@ -83,6 +84,7 @@ recurse(const char *path, void *data, struct recursor *r)
r->depth--; r->depth--;
} }
} }
}
if (!r->depth) { if (!r->depth) {
if (!(r->flags & DIRFIRST)) if (!(r->flags & DIRFIRST))

View File

@ -14,7 +14,7 @@ int rm_status = 0;
void void
rm(const char *path, struct stat *st, void *data, struct recursor *r) rm(const char *path, struct stat *st, void *data, struct recursor *r)
{ {
if (!(r->flags & NODIRS) && st && S_ISDIR(st->st_mode)) { if (!r->maxdepth && st && S_ISDIR(st->st_mode)) {
recurse(path, NULL, r); recurse(path, NULL, r);
if (rmdir(path) < 0) { if (rmdir(path) < 0) {

3
mv.c
View File

@ -12,7 +12,8 @@ static int mv_status = 0;
static int static int
mv(const char *s1, const char *s2, int depth) mv(const char *s1, const char *s2, int depth)
{ {
struct recursor r = { .fn = rm, .hist = NULL, .depth = 0, .follow = 'P', .flags = 0}; struct recursor r = { .fn = rm, .hist = NULL, .depth = 0, .maxdepth = 0,
.follow = 'P', .flags = 0 };
if (!rename(s1, s2)) if (!rename(s1, s2))
return (mv_status = 0); return (mv_status = 0);

5
rm.c
View File

@ -11,7 +11,8 @@ usage(void)
int int
main(int argc, char *argv[]) main(int argc, char *argv[])
{ {
struct recursor r = { .fn = rm, .hist = NULL, .depth = 0, .follow = 'P', .flags = NODIRS }; struct recursor r = { .fn = rm, .hist = NULL, .depth = 0, .maxdepth = 1,
.follow = 'P', .flags = 0 };
ARGBEGIN { ARGBEGIN {
case 'f': case 'f':
@ -19,7 +20,7 @@ main(int argc, char *argv[])
break; break;
case 'R': case 'R':
case 'r': case 'r':
r.flags &= ~NODIRS; r.maxdepth = 0;
break; break;
default: default:
usage(); usage();

3
tar.c
View File

@ -315,7 +315,8 @@ int
main(int argc, char *argv[]) main(int argc, char *argv[])
{ {
FILE *fp; FILE *fp;
struct recursor r = { .fn = c, .hist = NULL, .depth = 0, .follow = 'P', .flags = DIRFIRST}; struct recursor r = { .fn = c, .hist = NULL, .depth = 0, .maxdepth = 0,
.follow = 'P', .flags = DIRFIRST };
struct stat st; struct stat st;
char *file = NULL, *dir = ".", mode = '\0'; char *file = NULL, *dir = ".", mode = '\0';