Refactor recurse() again
Okay, why yet another recurse()-refactor? The last one added the recursor-struct, which simplified things on the user-end, but there was still one thing that bugged me a lot: Previously, all fn()'s were forced to (l)stat the paths themselves. This does not work well when you try to keep up with H-, L- and P- flags at the same time, as each utility-function would have to set the right function-pointer for (l)stat every single time. This is not desirable. Furthermore, recurse should be easy to use and not involve trouble finding the right (l)stat-function to do it right. So, what we needed was a stat-argument for each fn(), so it is directly accessible. This was impossible to do though when the fn()'s are still directly called by the programs to "start" the recurse. Thus, the fundamental change is to make recurse() the function to go, while designing the fn()'s in a way they can "live" with st being NULL (we don't want a null-pointer-deref). What you can see in this commit is the result of this work. Why all this trouble instead of using nftw? The special thing about recurse() is that you tell the function when to recurse() in your fn(). You don't need special flags to tell nftw() to skip the subtree, just to give an example. The only single downside to this is that now, you are not allowed to unconditionally call recurse() from your fn(). It has to be a directory. However, that is a cost I think is easily weighed up by the advantages. Another thing is the history: I added a procedure at the end of the outmost recurse to free the history. This way we don't leak memory. A simple optimization on the side: - if (h->dev == st.st_dev && h->ino == st.st_ino) + if (h->ino == st.st_ino && h->dev == st.st_dev) First compare the likely difference in inode-numbers instead of checking the unlikely condition that the device-numbers are different.
This commit is contained in:
parent
b3e8b17235
commit
3111908b03
17
chgrp.c
17
chgrp.c
|
@ -8,14 +8,13 @@
|
||||||
#include "fs.h"
|
#include "fs.h"
|
||||||
#include "util.h"
|
#include "util.h"
|
||||||
|
|
||||||
static struct stat st;
|
|
||||||
static int hflag = 0;
|
static int hflag = 0;
|
||||||
static int Rflag = 0;
|
static int Rflag = 0;
|
||||||
static gid_t gid = -1;
|
static gid_t gid = -1;
|
||||||
static int ret = 0;
|
static int ret = 0;
|
||||||
|
|
||||||
static void
|
static void
|
||||||
chgrp(const char *path, void *data, struct recursor *r)
|
chgrp(const char *path, struct stat *st, void *data, struct recursor *r)
|
||||||
{
|
{
|
||||||
char *chownf_name;
|
char *chownf_name;
|
||||||
int (*chownf)(const char *, uid_t, gid_t);
|
int (*chownf)(const char *, uid_t, gid_t);
|
||||||
|
@ -28,10 +27,10 @@ chgrp(const char *path, void *data, struct recursor *r)
|
||||||
chownf = chown;
|
chownf = chown;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (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 (Rflag) {
|
} else if (Rflag && st && S_ISDIR(st->st_mode)) {
|
||||||
recurse(path, NULL, r);
|
recurse(path, NULL, r);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -76,14 +75,8 @@ main(int argc, char *argv[])
|
||||||
}
|
}
|
||||||
gid = gr->gr_gid;
|
gid = gr->gr_gid;
|
||||||
|
|
||||||
for (argc--, argv++; *argv; argc--, argv++) {
|
for (argc--, argv++; *argv; argc--, argv++)
|
||||||
if (stat(*argv, &st) < 0) {
|
recurse(*argv, NULL, &r);
|
||||||
weprintf("stat %s:", *argv);
|
|
||||||
ret = 1;
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
chgrp(*argv, NULL, &r);
|
|
||||||
}
|
|
||||||
|
|
||||||
return ret || recurse_status;
|
return ret || recurse_status;
|
||||||
}
|
}
|
||||||
|
|
16
chmod.c
16
chmod.c
|
@ -10,23 +10,17 @@ static mode_t mask = 0;
|
||||||
static int ret = 0;
|
static int ret = 0;
|
||||||
|
|
||||||
static void
|
static void
|
||||||
chmodr(const char *path, void *data, struct recursor *r)
|
chmodr(const char *path, struct stat *st, void *data, struct recursor *r)
|
||||||
{
|
{
|
||||||
struct stat st;
|
|
||||||
mode_t m;
|
mode_t m;
|
||||||
|
|
||||||
if (stat(path, &st) < 0) {
|
m = parsemode(modestr, st ? st->st_mode : 0, mask);
|
||||||
weprintf("stat %s:", path);
|
|
||||||
ret = 1;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
m = parsemode(modestr, st.st_mode, mask);
|
|
||||||
if (chmod(path, m) < 0) {
|
if (chmod(path, m) < 0) {
|
||||||
weprintf("chmod %s:", path);
|
weprintf("chmod %s:", path);
|
||||||
ret = 1;
|
ret = 1;
|
||||||
} else if (Rflag)
|
} else if (Rflag && st && S_ISDIR(st->st_mode)) {
|
||||||
recurse(path, NULL, r);
|
recurse(path, NULL, r);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
|
@ -82,7 +76,7 @@ done:
|
||||||
usage();
|
usage();
|
||||||
|
|
||||||
for (--argc, ++argv; *argv; argc--, argv++)
|
for (--argc, ++argv; *argv; argc--, argv++)
|
||||||
chmodr(*argv, NULL, &r);
|
recurse(*argv, NULL, &r);
|
||||||
|
|
||||||
return ret || recurse_status;
|
return ret || recurse_status;
|
||||||
}
|
}
|
||||||
|
|
6
chown.c
6
chown.c
|
@ -17,7 +17,7 @@ static gid_t gid = -1;
|
||||||
static int ret = 0;
|
static int ret = 0;
|
||||||
|
|
||||||
static void
|
static void
|
||||||
chownpwgr(const char *path, void *data, struct recursor *r)
|
chownpwgr(const char *path, struct stat *st, void *data, struct recursor *r)
|
||||||
{
|
{
|
||||||
char *chownf_name;
|
char *chownf_name;
|
||||||
int (*chownf)(const char *, uid_t, gid_t);
|
int (*chownf)(const char *, uid_t, gid_t);
|
||||||
|
@ -33,7 +33,7 @@ chownpwgr(const char *path, 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 (Rflag) {
|
} else if (Rflag && st && S_ISDIR(st->st_mode)) {
|
||||||
recurse(path, NULL, r);
|
recurse(path, NULL, r);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -99,7 +99,7 @@ main(int argc, char *argv[])
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
for (argc--, argv++; *argv; argc--, argv++)
|
for (argc--, argv++; *argv; argc--, argv++)
|
||||||
chownpwgr(*argv, NULL, &r);
|
recurse(*argv, NULL, &r);
|
||||||
|
|
||||||
return ret || recurse_status;
|
return ret || recurse_status;
|
||||||
}
|
}
|
||||||
|
|
30
du.c
30
du.c
|
@ -17,7 +17,6 @@ static size_t blksize = 512;
|
||||||
static int aflag = 0;
|
static int aflag = 0;
|
||||||
static int sflag = 0;
|
static int sflag = 0;
|
||||||
static int hflag = 0;
|
static int hflag = 0;
|
||||||
static int ret = 0;
|
|
||||||
|
|
||||||
static void
|
static void
|
||||||
printpath(size_t n, const char *path)
|
printpath(size_t n, const char *path)
|
||||||
|
@ -35,25 +34,16 @@ nblks(blkcnt_t blocks)
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
du(const char *path, void *total, struct recursor *r)
|
du(const char *path, struct stat *st, void *total, struct recursor *r)
|
||||||
{
|
{
|
||||||
struct stat st;
|
|
||||||
size_t subtotal = 0;
|
size_t subtotal = 0;
|
||||||
|
|
||||||
if (lstat(path, &st) < 0) {
|
if (st && S_ISDIR(st->st_mode))
|
||||||
if (!(r->depth) || errno != ENOENT)
|
|
||||||
weprintf("stat %s:", path);
|
|
||||||
if (!(r->depth))
|
|
||||||
ret = 1;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (S_ISDIR(st.st_mode))
|
|
||||||
recurse(path, &subtotal, r);
|
recurse(path, &subtotal, r);
|
||||||
*((size_t *)total) += subtotal + nblks(st.st_blocks);
|
*((size_t *)total) += subtotal + nblks(st ? st->st_blocks : 0);
|
||||||
|
|
||||||
if (!sflag && r->depth <= maxdepth && (S_ISDIR(st.st_mode) || aflag))
|
if (!sflag && r->depth <= maxdepth && r->depth && st && (S_ISDIR(st->st_mode) || aflag))
|
||||||
printpath(subtotal + nblks(st.st_blocks), path);
|
printpath(subtotal + nblks(st->st_blocks), path);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
|
@ -109,16 +99,14 @@ main(int argc, char *argv[])
|
||||||
blksize = 1024;
|
blksize = 1024;
|
||||||
|
|
||||||
if (!argc) {
|
if (!argc) {
|
||||||
du(".", &n, &r);
|
recurse(".", &n, &r);
|
||||||
if (sflag && !ret)
|
|
||||||
printpath(nblks(n), ".");
|
printpath(nblks(n), ".");
|
||||||
} else {
|
} else {
|
||||||
for (; *argv; argc--, argv++) {
|
for (; *argv; argc--, argv++) {
|
||||||
du(argv[0], &n, &r);
|
recurse(*argv, &n, &r);
|
||||||
if (sflag && !ret)
|
printpath(n, *argv);
|
||||||
printpath(n, argv[0]);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return ret || recurse_status;
|
return recurse_status;
|
||||||
}
|
}
|
||||||
|
|
5
fs.h
5
fs.h
|
@ -1,4 +1,5 @@
|
||||||
/* See LICENSE file for copyright and license details. */
|
/* See LICENSE file for copyright and license details. */
|
||||||
|
#include <sys/stat.h>
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
|
|
||||||
struct history {
|
struct history {
|
||||||
|
@ -8,7 +9,7 @@ struct history {
|
||||||
};
|
};
|
||||||
|
|
||||||
struct recursor {
|
struct recursor {
|
||||||
void (*fn)(const char *, 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 follow;
|
||||||
|
@ -36,4 +37,4 @@ extern int recurse_status;
|
||||||
void recurse(const char *, void *, struct recursor *);
|
void recurse(const char *, void *, struct recursor *);
|
||||||
|
|
||||||
int cp(const char *, const char *, int);
|
int cp(const char *, const char *, int);
|
||||||
void rm(const char *, void *, struct recursor *);
|
void rm(const char *, struct stat *st, void *, struct recursor *);
|
||||||
|
|
|
@ -33,24 +33,23 @@ recurse(const char *path, void *data, struct recursor *r)
|
||||||
}
|
}
|
||||||
|
|
||||||
if (statf(path, &st) < 0) {
|
if (statf(path, &st) < 0) {
|
||||||
if (errno != ENOENT) {
|
|
||||||
weprintf("%s %s:", statf_name, path);
|
weprintf("%s %s:", statf_name, path);
|
||||||
recurse_status = 1;
|
recurse_status = 1;
|
||||||
}
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (!S_ISDIR(st.st_mode))
|
if (!S_ISDIR(st.st_mode)) {
|
||||||
|
(r->fn)(path, &st, data, r);
|
||||||
return;
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
new = emalloc(sizeof(struct history));
|
new = emalloc(sizeof(struct history));
|
||||||
|
|
||||||
new->prev = r->hist;
|
new->prev = r->hist;
|
||||||
r->hist = new;
|
r->hist = new;
|
||||||
new->dev = st.st_dev;
|
new->dev = st.st_dev;
|
||||||
new->ino = st.st_ino;
|
new->ino = st.st_ino;
|
||||||
|
|
||||||
for (h = new->prev; h; h = h->prev)
|
for (h = new->prev; h; h = h->prev)
|
||||||
if (h->dev == st.st_dev && h->ino == st.st_ino)
|
if (h->ino == st.st_ino && h->dev == st.st_dev)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
if (!(dp = opendir(path))) {
|
if (!(dp = opendir(path))) {
|
||||||
|
@ -70,17 +69,27 @@ recurse(const char *path, void *data, struct recursor *r)
|
||||||
if (path[strlen(path) - 1] != '/')
|
if (path[strlen(path) - 1] != '/')
|
||||||
estrlcat(subpath, "/", sizeof(subpath));
|
estrlcat(subpath, "/", sizeof(subpath));
|
||||||
estrlcat(subpath, d->d_name, sizeof(subpath));
|
estrlcat(subpath, d->d_name, sizeof(subpath));
|
||||||
if ((r->flags & SAMEDEV) && statf(subpath, &dst) < 0) {
|
if (statf(subpath, &dst) < 0) {
|
||||||
if (errno != ENOENT) {
|
|
||||||
weprintf("%s %s:", statf_name, subpath);
|
weprintf("%s %s:", statf_name, subpath);
|
||||||
recurse_status = 1;
|
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;
|
||||||
|
} else {
|
||||||
r->depth++;
|
r->depth++;
|
||||||
(r->fn)(subpath, data, r);
|
(r->fn)(subpath, &dst, data, r);
|
||||||
r->depth--;
|
r->depth--;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!r->depth) {
|
||||||
|
(r->fn)(path, &st, data, r);
|
||||||
|
|
||||||
|
for (; r->hist; ) {
|
||||||
|
h = r->hist;
|
||||||
|
r->hist = r->hist->prev;
|
||||||
|
free(h);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
closedir(dp);
|
closedir(dp);
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,4 +1,6 @@
|
||||||
/* See LICENSE file for copyright and license details. */
|
/* See LICENSE file for copyright and license details. */
|
||||||
|
#include <sys/stat.h>
|
||||||
|
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
|
||||||
|
@ -10,9 +12,9 @@ int rm_rflag = 0;
|
||||||
int rm_status = 0;
|
int rm_status = 0;
|
||||||
|
|
||||||
void
|
void
|
||||||
rm(const char *path, void *data, struct recursor *r)
|
rm(const char *path, struct stat *st, void *data, struct recursor *r)
|
||||||
{
|
{
|
||||||
if (rm_rflag)
|
if (rm_rflag && st && S_ISDIR(st->st_mode))
|
||||||
recurse(path, NULL, r);
|
recurse(path, NULL, r);
|
||||||
if (remove(path) < 0) {
|
if (remove(path) < 0) {
|
||||||
if (!rm_fflag)
|
if (!rm_fflag)
|
||||||
|
|
2
mv.c
2
mv.c
|
@ -21,7 +21,7 @@ mv(const char *s1, const char *s2, int depth)
|
||||||
cp_HLPflag = 'P';
|
cp_HLPflag = 'P';
|
||||||
rm_rflag = 1;
|
rm_rflag = 1;
|
||||||
cp(s1, s2, depth);
|
cp(s1, s2, depth);
|
||||||
rm(s1, NULL, &r);
|
rm(s1, NULL, NULL, &r);
|
||||||
return (mv_status = cp_status || rm_status);
|
return (mv_status = cp_status || rm_status);
|
||||||
}
|
}
|
||||||
mv_status = 1;
|
mv_status = 1;
|
||||||
|
|
2
rm.c
2
rm.c
|
@ -33,7 +33,7 @@ main(int argc, char *argv[])
|
||||||
}
|
}
|
||||||
|
|
||||||
for (; *argv; argc--, argv++)
|
for (; *argv; argc--, argv++)
|
||||||
rm(*argv, NULL, &r);
|
recurse(*argv, NULL, &r);
|
||||||
|
|
||||||
return rm_status || recurse_status;
|
return rm_status || recurse_status;
|
||||||
}
|
}
|
||||||
|
|
6
tar.c
6
tar.c
|
@ -235,9 +235,11 @@ print(char * fname, int l, char b[BLKSIZ])
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
c(const char *path, void *data, struct recursor *r)
|
c(const char *path, struct stat *st, void *data, struct recursor *r)
|
||||||
{
|
{
|
||||||
archive(path);
|
archive(path);
|
||||||
|
|
||||||
|
if (st && S_ISDIR(st->st_mode))
|
||||||
recurse(path, NULL, r);
|
recurse(path, NULL, r);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -318,7 +320,7 @@ main(int argc, char *argv[])
|
||||||
tarfile = stdout;
|
tarfile = stdout;
|
||||||
}
|
}
|
||||||
chdir(dir);
|
chdir(dir);
|
||||||
c(argv[0], NULL, &r);
|
recurse(argv[0], NULL, &r);
|
||||||
break;
|
break;
|
||||||
case 't':
|
case 't':
|
||||||
case 'x':
|
case 'x':
|
||||||
|
|
Loading…
Reference in New Issue
Block a user