Use *at functions with appropriate flags instead of lstat/lchown

This commit is contained in:
Michael Forney
2019-11-01 20:43:32 -07:00
parent 3e160b616a
commit 039b54aa51
4 changed files with 29 additions and 51 deletions

17
chown.c
View File

@@ -1,5 +1,6 @@
/* See LICENSE file for copyright and license details. */
#include <errno.h>
#include <fcntl.h>
#include <grp.h>
#include <limits.h>
#include <pwd.h>
@@ -18,19 +19,13 @@ static int ret = 0;
static void
chownpwgr(const char *path, struct stat *st, void *data, struct recursor *r)
{
char *chownf_name;
int (*chownf)(const char *, uid_t, gid_t);
int flags = 0;
if ((r->maxdepth == 0 && r->follow == 'P') || (r->follow == 'H' && r->depth) || (hflag && !(r->depth))) {
chownf_name = "lchown";
chownf = lchown;
} else {
chownf_name = "chown";
chownf = chown;
}
if ((r->maxdepth == 0 && r->follow == 'P') || (r->follow == 'H' && r->depth) || (hflag && !(r->depth)))
flags |= AT_SYMLINK_NOFOLLOW;
if (chownf(path, uid, gid) < 0) {
weprintf("%s %s:", chownf_name, path);
if (fchownat(AT_FDCWD, path, uid, gid, flags) < 0) {
weprintf("chown %s:", path);
ret = 1;
} else if (S_ISDIR(st->st_mode)) {
recurse(path, NULL, r);