sbase/chmod.c
FRIGN 3111908b03 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.
2015-03-19 01:08:19 +01:00

83 lines
1.5 KiB
C

/* See LICENSE file for copyright and license details. */
#include <sys/stat.h>
#include "fs.h"
#include "util.h"
static int Rflag = 0;
static char *modestr = "";
static mode_t mask = 0;
static int ret = 0;
static void
chmodr(const char *path, struct stat *st, void *data, struct recursor *r)
{
mode_t m;
m = parsemode(modestr, st ? st->st_mode : 0, mask);
if (chmod(path, m) < 0) {
weprintf("chmod %s:", path);
ret = 1;
} else if (Rflag && st && S_ISDIR(st->st_mode)) {
recurse(path, NULL, r);
}
}
static void
usage(void)
{
eprintf("usage: %s [-R [-H | -L | -P]] mode file ...\n", argv0);
}
int
main(int argc, char *argv[])
{
struct recursor r = { .fn = chmodr, .hist = NULL, .depth = 0, .follow = 'P', .flags = 0};
size_t i;
argv0 = argv[0], argc--, argv++;
for (; *argv && (*argv)[0] == '-'; argc--, argv++) {
if (!(*argv)[1])
usage();
for (i = 1; (*argv)[i]; i++) {
switch ((*argv)[i]) {
case 'R':
Rflag = 1;
break;
case 'H':
case 'L':
case 'P':
r.follow = (*argv)[i];
break;
case 'r': case 'w': case 'x': case 's': case 't':
/* -[rwxst] are valid modes, so we're done */
if (i == 1)
goto done;
/* fallthrough */
case '-':
/* -- terminator */
if (i == 1 && !(*argv)[i + 1]) {
argv++;
argc--;
goto done;
}
/* fallthrough */
default:
usage();
}
}
}
done:
mask = getumask();
modestr = *argv;
if (argc < 2)
usage();
for (--argc, ++argv; *argv; argc--, argv++)
recurse(*argv, NULL, &r);
return ret || recurse_status;
}