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) {
weprintf("%s %s:", chownf_name, path);
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);
}
}
@ -44,14 +44,15 @@ int
main(int argc, char *argv[])
{
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 {
case 'h':
hflag = 1;
break;
case 'R':
r.flags &= ~NODIRS;
r.maxdepth = 0;
break;
case 'H':
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) {
weprintf("chmod %s:", path);
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);
}
}
@ -31,7 +31,8 @@ usage(void)
int
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;
argv0 = argv[0], argc--, argv++;
@ -42,7 +43,7 @@ main(int argc, char *argv[])
for (i = 1; (*argv)[i]; i++) {
switch ((*argv)[i]) {
case 'R':
r.flags &= ~NODIRS;
r.maxdepth = 0;
break;
case 'H':
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) {
weprintf("%s %s:", chownf_name, path);
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);
}
}
@ -48,7 +48,8 @@ main(int argc, char *argv[])
{
struct group *gr;
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;
ARGBEGIN {
@ -57,7 +58,7 @@ main(int argc, char *argv[])
break;
case 'r':
case 'R':
r.flags &= ~NODIRS;
r.maxdepth = 0;
break;
case 'H':
case 'L':

3
du.c
View File

@ -55,7 +55,8 @@ usage(void)
int
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;
int kflag = 0, dflag = 0;
char *bsize;

18
fs.h
View File

@ -3,23 +3,23 @@
#include <sys/types.h>
struct history {
struct history *prev;
dev_t dev;
ino_t ino;
struct history *prev;
dev_t dev;
ino_t ino;
};
struct recursor {
void (*fn)(const char *, struct stat *st, void *, struct recursor *);
struct history *hist;
int depth;
int follow;
int flags;
void (*fn)(const char *, struct stat *st, void *, struct recursor *);
struct history *hist;
int depth;
int maxdepth;
int follow;
int flags;
};
enum {
SAMEDEV = 1 << 0,
DIRFIRST = 1 << 1,
NODIRS = 1 << 2,
};
extern int cp_aflag;

View File

@ -37,7 +37,7 @@ recurse(const char *path, void *data, struct recursor *r)
recurse_status = 1;
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);
return;
}
@ -61,26 +61,28 @@ recurse(const char *path, void *data, struct recursor *r)
if (!r->depth && (r->flags & DIRFIRST))
(r->fn)(path, &st, data, r);
while ((d = readdir(dp))) {
if (r->follow == 'H') {
statf_name = "lstat";
statf = lstat;
}
if (!strcmp(d->d_name, ".") || !strcmp(d->d_name, ".."))
continue;
estrlcpy(subpath, path, sizeof(subpath));
if (path[strlen(path) - 1] != '/')
estrlcat(subpath, "/", sizeof(subpath));
estrlcat(subpath, d->d_name, sizeof(subpath));
if (statf(subpath, &dst) < 0) {
weprintf("%s %s:", statf_name, subpath);
recurse_status = 1;
} else if ((r->flags & SAMEDEV) && dst.st_dev != st.st_dev) {
continue;
} else {
r->depth++;
(r->fn)(subpath, &dst, data, r);
r->depth--;
if (!r->maxdepth || r->depth + 1 < r->maxdepth) {
while ((d = readdir(dp))) {
if (r->follow == 'H') {
statf_name = "lstat";
statf = lstat;
}
if (!strcmp(d->d_name, ".") || !strcmp(d->d_name, ".."))
continue;
estrlcpy(subpath, path, sizeof(subpath));
if (path[strlen(path) - 1] != '/')
estrlcat(subpath, "/", sizeof(subpath));
estrlcat(subpath, d->d_name, sizeof(subpath));
if (statf(subpath, &dst) < 0) {
weprintf("%s %s:", statf_name, subpath);
recurse_status = 1;
} else if ((r->flags & SAMEDEV) && dst.st_dev != st.st_dev) {
continue;
} else {
r->depth++;
(r->fn)(subpath, &dst, data, r);
r->depth--;
}
}
}

View File

@ -14,7 +14,7 @@ int rm_status = 0;
void
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);
if (rmdir(path) < 0) {

3
mv.c
View File

@ -12,7 +12,8 @@ static int mv_status = 0;
static int
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))
return (mv_status = 0);

5
rm.c
View File

@ -11,7 +11,8 @@ usage(void)
int
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 {
case 'f':
@ -19,7 +20,7 @@ main(int argc, char *argv[])
break;
case 'R':
case 'r':
r.flags &= ~NODIRS;
r.maxdepth = 0;
break;
default:
usage();

3
tar.c
View File

@ -315,7 +315,8 @@ int
main(int argc, char *argv[])
{
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;
char *file = NULL, *dir = ".", mode = '\0';