recurse: add symlink derefencing flags -H and -L

This commit is contained in:
Tai Chi Minh Ralph Eastwood 2015-02-09 19:53:24 +00:00 committed by sin
parent 5a13865385
commit 82bc92da51
7 changed files with 37 additions and 19 deletions

12
chgrp.c
View File

@ -10,19 +10,20 @@
static int gid; static int gid;
static int status; static int status;
static int rflag; static int rflag;
static char fflag = 'P';
static struct stat st; static struct stat st;
static char *chown_f_name = "chown"; static char *chown_f_name = "chown";
static int (*chown_f)(const char *, uid_t, gid_t) = chown; static int (*chown_f)(const char *, uid_t, gid_t) = chown;
static void static void
chgrp(const char *path) chgrp(const char *path, char fflag)
{ {
if (chown_f(path, st.st_uid, gid) < 0) { if (chown_f(path, st.st_uid, gid) < 0) {
weprintf("%s %s:", chown_f_name, path); weprintf("%s %s:", chown_f_name, path);
status = 1; status = 1;
} }
if (rflag) if (rflag)
recurse(path, chgrp); recurse(path, chgrp, fflag);
} }
static void static void
@ -44,6 +45,11 @@ main(int argc, char *argv[])
case 'R': case 'R':
rflag = 1; rflag = 1;
break; break;
case 'H':
case 'L':
case 'P':
fflag = ARGC();
break;
default: default:
usage(); usage();
} ARGEND; } ARGEND;
@ -67,7 +73,7 @@ main(int argc, char *argv[])
status = 1; status = 1;
continue; continue;
} }
chgrp(*argv); chgrp(*argv, fflag);
} }
return status; return status;
} }

View File

@ -9,7 +9,7 @@ static mode_t mask = 0;
static int ret = 0; static int ret = 0;
void void
chmodr(const char *path) chmodr(const char *path, char fflag)
{ {
struct stat st; struct stat st;
mode_t m; mode_t m;
@ -26,7 +26,7 @@ chmodr(const char *path)
ret = 1; ret = 1;
} }
if (rflag) if (rflag)
recurse(path, chmodr); recurse(path, chmodr, fflag);
} }
static void static void
@ -65,7 +65,7 @@ done:
usage(); usage();
for (++i; i < argc; i++) for (++i; i < argc; i++)
chmodr(argv[i]); chmodr(argv[i], 'P');
return ret; return ret;
} }

12
chown.c
View File

@ -9,6 +9,7 @@
#include "util.h" #include "util.h"
static int rflag = 0; static int rflag = 0;
static int fflag = 'P';
static uid_t uid = -1; static uid_t uid = -1;
static gid_t gid = -1; static gid_t gid = -1;
static int ret = 0; static int ret = 0;
@ -16,14 +17,14 @@ static char *chown_f_name = "chown";
static int (*chown_f)(const char *, uid_t, gid_t) = chown; static int (*chown_f)(const char *, uid_t, gid_t) = chown;
static void static void
chownpwgr(const char *path) chownpwgr(const char *path, int fflag)
{ {
if (chown_f(path, uid, gid) < 0) { if (chown_f(path, uid, gid) < 0) {
weprintf("%s %s:", chown_f_name, path); weprintf("%s %s:", chown_f_name, path);
ret = 1; ret = 1;
} }
if (rflag) if (rflag)
recurse(path, chownpwgr); recurse(path, chownpwgr, fflag);
} }
static void static void
@ -48,6 +49,11 @@ main(int argc, char *argv[])
case 'r': case 'r':
rflag = 1; rflag = 1;
break; break;
case 'H':
case 'L':
case 'P':
fflag = ARGC();
break;
default: default:
usage(); usage();
} ARGEND; } ARGEND;
@ -88,7 +94,7 @@ main(int argc, char *argv[])
} }
} }
for (; argc > 0; argc--, argv++) for (; argc > 0; argc--, argv++)
chownpwgr(argv[0]); chownpwgr(argv[0], fflag);
return ret; return ret;
} }

View File

@ -11,14 +11,16 @@
#include "../util.h" #include "../util.h"
void void
recurse(const char *path, void (*fn)(const char *)) recurse(const char *path, void (*fn)(const char *, char), char follow)
{ {
char buf[PATH_MAX]; char buf[PATH_MAX];
struct dirent *d; struct dirent *d;
struct stat st; struct stat lst, st;
DIR *dp; DIR *dp;
if (lstat(path, &st) < 0 || !S_ISDIR(st.st_mode)) if (lstat(path, &lst) < 0 || stat(path, &st) < 0 ||
!(S_ISDIR(lst.st_mode) ||
(follow != 'P' && S_ISLNK(lst.st_mode) && S_ISDIR(st.st_mode))))
return; return;
if (!(dp = opendir(path))) if (!(dp = opendir(path)))
@ -35,7 +37,7 @@ recurse(const char *path, void (*fn)(const char *))
eprintf("path too long\n"); eprintf("path too long\n");
if (strlcat(buf, d->d_name, sizeof(buf)) >= sizeof(buf)) if (strlcat(buf, d->d_name, sizeof(buf)) >= sizeof(buf))
eprintf("path too long\n"); eprintf("path too long\n");
fn(buf); fn(buf, follow == 'H' ? 'P' : follow);
} }
closedir(dp); closedir(dp);

View File

@ -12,7 +12,7 @@ void
rm(const char *path) rm(const char *path)
{ {
if (rm_rflag) if (rm_rflag)
recurse(path, rm); recurse(path, rm, 'P');
if (remove(path) < 0) { if (remove(path) < 0) {
if (!rm_fflag) if (!rm_fflag)
weprintf("remove %s:", path); weprintf("remove %s:", path);

12
tar.c
View File

@ -44,7 +44,7 @@ static void putoctal(char *, unsigned, int);
static int archive(const char *); static int archive(const char *);
static int unarchive(char *, int, char[Blksiz]); static int unarchive(char *, int, char[Blksiz]);
static int print(char *, int , char[Blksiz]); static int print(char *, int , char[Blksiz]);
static void c(const char *); static void c(const char *, char);
static void xt(int (*)(char*, int, char[Blksiz])); static void xt(int (*)(char*, int, char[Blksiz]));
static FILE *tarfile; static FILE *tarfile;
@ -52,6 +52,7 @@ static ino_t tarinode;
static dev_t tardev; static dev_t tardev;
static int mflag; static int mflag;
static char fflag = 'P';
static char filtermode; static char filtermode;
static FILE * static FILE *
@ -245,10 +246,10 @@ print(char * fname, int l, char b[Blksiz])
} }
static void static void
c(const char * path) c(const char * path, char fflag)
{ {
archive(path); archive(path);
recurse(path, c); recurse(path, c, fflag);
} }
static void static void
@ -304,6 +305,9 @@ main(int argc, char *argv[])
usage(); usage();
filtermode = ARGC(); filtermode = ARGC();
break; break;
case 'h':
fflag = 'L';
break;
default: default:
usage(); usage();
} ARGEND; } ARGEND;
@ -325,7 +329,7 @@ main(int argc, char *argv[])
tarfile = stdout; tarfile = stdout;
} }
chdir(dir); chdir(dir);
c(argv[0]); c(argv[0], fflag);
break; break;
case 't': case 't':
case 'x': case 'x':

2
util.h
View File

@ -60,7 +60,7 @@ mode_t getumask(void);
char *humansize(double); char *humansize(double);
mode_t parsemode(const char *, mode_t, mode_t); mode_t parsemode(const char *, mode_t, mode_t);
void putword(const char *); void putword(const char *);
void recurse(const char *, void (*)(const char *)); void recurse(const char *, void (*)(const char *, char), char);
#undef strtonum #undef strtonum
long long strtonum(const char *, long long, long long, const char **); long long strtonum(const char *, long long, long long, const char **);
long long enstrtonum(int, const char *, long long, long long); long long enstrtonum(int, const char *, long long, long long);