Add maxdepth to recurse()
This also makes more sense.
This commit is contained in:
parent
e14d9412f8
commit
7b2465c101
7
chgrp.c
7
chgrp.c
|
@ -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':
|
||||||
|
|
7
chmod.c
7
chmod.c
|
@ -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':
|
||||||
|
|
7
chown.c
7
chown.c
|
@ -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
3
du.c
|
@ -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;
|
||||||
|
|
18
fs.h
18
fs.h
|
@ -3,23 +3,23 @@
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
|
|
||||||
struct history {
|
struct history {
|
||||||
struct history *prev;
|
struct history *prev;
|
||||||
dev_t dev;
|
dev_t dev;
|
||||||
ino_t ino;
|
ino_t ino;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct recursor {
|
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 follow;
|
int maxdepth;
|
||||||
int flags;
|
int follow;
|
||||||
|
int flags;
|
||||||
};
|
};
|
||||||
|
|
||||||
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;
|
||||||
|
|
|
@ -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,26 +61,28 @@ 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);
|
||||||
|
|
||||||
while ((d = readdir(dp))) {
|
if (!r->maxdepth || r->depth + 1 < r->maxdepth) {
|
||||||
if (r->follow == 'H') {
|
while ((d = readdir(dp))) {
|
||||||
statf_name = "lstat";
|
if (r->follow == 'H') {
|
||||||
statf = lstat;
|
statf_name = "lstat";
|
||||||
}
|
statf = lstat;
|
||||||
if (!strcmp(d->d_name, ".") || !strcmp(d->d_name, ".."))
|
}
|
||||||
continue;
|
if (!strcmp(d->d_name, ".") || !strcmp(d->d_name, ".."))
|
||||||
estrlcpy(subpath, path, sizeof(subpath));
|
continue;
|
||||||
if (path[strlen(path) - 1] != '/')
|
estrlcpy(subpath, path, sizeof(subpath));
|
||||||
estrlcat(subpath, "/", sizeof(subpath));
|
if (path[strlen(path) - 1] != '/')
|
||||||
estrlcat(subpath, d->d_name, sizeof(subpath));
|
estrlcat(subpath, "/", sizeof(subpath));
|
||||||
if (statf(subpath, &dst) < 0) {
|
estrlcat(subpath, d->d_name, sizeof(subpath));
|
||||||
weprintf("%s %s:", statf_name, subpath);
|
if (statf(subpath, &dst) < 0) {
|
||||||
recurse_status = 1;
|
weprintf("%s %s:", statf_name, subpath);
|
||||||
} else if ((r->flags & SAMEDEV) && dst.st_dev != st.st_dev) {
|
recurse_status = 1;
|
||||||
continue;
|
} else if ((r->flags & SAMEDEV) && dst.st_dev != st.st_dev) {
|
||||||
} else {
|
continue;
|
||||||
r->depth++;
|
} else {
|
||||||
(r->fn)(subpath, &dst, data, r);
|
r->depth++;
|
||||||
r->depth--;
|
(r->fn)(subpath, &dst, data, r);
|
||||||
|
r->depth--;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -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
3
mv.c
|
@ -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
5
rm.c
|
@ -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
3
tar.c
|
@ -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';
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user