Refactor enmasse() and recurse() to reflect depth

The HLP-changes to sbase have been a great addition of functionality,
but they kind of "polluted" the enmasse() and recurse() prototypes.
As this will come in handy in the future, knowing at which "depth"
you are inside a recursing function is an important functionality.

Instead of having a special HLP-flag passed to enmasse, each sub-
function needs to provide it on its own and can calculate results
based on the current depth (for instance, 'H' implies 'P' at
depth > 0).
A special case is recurse(), because it actually depends on the
follow-type. A new flag "recurse_follow" brings consistency into
what used to be spread across different naming conventions (fflag,
HLP_flag, ...).

This also fixes numerous bugs with the behaviour of HLP in the
tools using it.
This commit is contained in:
FRIGN
2015-03-02 21:43:56 +01:00
parent 274e86e1aa
commit 8dc92fbd6c
13 changed files with 71 additions and 59 deletions

25
chown.c
View File

@@ -9,22 +9,21 @@
#include "util.h"
static int rflag = 0;
static int HLPflag = 'P';
static uid_t uid = -1;
static gid_t gid = -1;
static int ret = 0;
static char *chown_f_name = "chown";
static int (*chown_f)(const char *, uid_t, gid_t) = chown;
static char *chownf_name = "chown";
static int (*chownf)(const char *, uid_t, gid_t) = chown;
static void
chownpwgr(const char *path, int HLPflag)
chownpwgr(const char *path, int depth)
{
if (chown_f(path, uid, gid) < 0) {
weprintf("%s %s:", chown_f_name, path);
if (chownf(path, uid, gid) < 0) {
weprintf("%s %s:", chownf_name, path);
ret = 1;
}
if (rflag)
recurse(path, chownpwgr, HLPflag);
recurse(path, chownpwgr, depth);
}
static void
@@ -42,8 +41,8 @@ main(int argc, char *argv[])
ARGBEGIN {
case 'h':
chown_f_name = "lchown";
chown_f = lchown;
chownf_name = "lchown";
chownf = lchown;
break;
case 'r':
case 'R':
@@ -52,7 +51,7 @@ main(int argc, char *argv[])
case 'H':
case 'L':
case 'P':
HLPflag = ARGC();
recurse_follow = ARGC();
break;
default:
usage();
@@ -60,6 +59,10 @@ main(int argc, char *argv[])
if (argc == 0)
usage();
if (recurse_follow == 'P') {
chownf_name = "lchown";
chownf = lchown;
}
owner = argv[0];
argv++;
@@ -94,7 +97,7 @@ main(int argc, char *argv[])
}
}
for (; argc > 0; argc--, argv++)
chownpwgr(argv[0], HLPflag);
chownpwgr(argv[0], 0);
return ret;
}