X-Git-Url: http://erislabs.net/gitweb/?a=blobdiff_plain;f=lib%2Fdirchownmod.c;h=b3e9518086eeb7f89f98e966c3ff84814beeb22e;hb=ccd54782ae73477aa3f3987ab04db9e6fde025d6;hp=50e5fe117055b9e55866dc98ec2969b45d80e3fd;hpb=5f531596a92e1205a700218dedfe7a525eef21c8;p=gnulib.git diff --git a/lib/dirchownmod.c b/lib/dirchownmod.c index 50e5fe117..b3e951808 100644 --- a/lib/dirchownmod.c +++ b/lib/dirchownmod.c @@ -18,29 +18,29 @@ /* Written by Paul Eggert. */ -#ifdef HAVE_CONFIG_H -# include -#endif +#include #include "dirchownmod.h" #include #include #include -#include #include #include "lchmod.h" +#include "lchown.h" #include "stat-macros.h" -#ifndef O_DIRECTORY -# define O_DIRECTORY 0 -#endif -#ifndef O_NOFOLLOW -# define O_NOFOLLOW 0 +#ifndef HAVE_FCHMOD +# define HAVE_FCHMOD 0 +# undef fchmod +# define fchmod(fd, mode) (-1) #endif -/* Change the ownership and mode bits of the directory DIR. +/* Change the ownership and mode bits of a directory. If FD is + nonnegative, it should be a file descriptor associated with the + directory; close it before returning. DIR is the name of the + directory. If MKDIR_MODE is not (mode_t) -1, mkdir (DIR, MKDIR_MODE) has just been executed successfully with umask zero, so DIR should be a @@ -61,27 +61,12 @@ calls may do the chown but not the chmod. */ int -dirchownmod (char const *dir, mode_t mkdir_mode, +dirchownmod (int fd, char const *dir, mode_t mkdir_mode, uid_t owner, gid_t group, mode_t mode, mode_t mode_bits) { struct stat st; - int result; - - /* Manipulate DIR via a file descriptor if possible, to avoid some races. */ - int open_flags = O_RDONLY | O_DIRECTORY | O_NOCTTY | O_NOFOLLOW | O_NONBLOCK; - int fd = open (dir, open_flags); - - /* Fail if the directory is unreadable, the directory previously - existed or was created without read permission. Otherwise, get - the file's status. */ - if (0 <= fd) - result = fstat (fd, &st); - else if (errno != EACCES - || (mkdir_mode != (mode_t) -1 && mkdir_mode & S_IRUSR)) - return fd; - else - result = stat (dir, &st); + int result = (fd < 0 ? stat (dir, &st) : fstat (fd, &st)); if (result == 0) { @@ -134,7 +119,7 @@ dirchownmod (char const *dir, mode_t mkdir_mode, { mode_t chmod_mode = mode | (dir_mode & CHMOD_MODE_BITS & ~mode_bits); - result = (0 <= fd + result = (HAVE_FCHMOD && 0 <= fd ? fchmod (fd, chmod_mode) : mkdir_mode != (mode_t) -1 ? lchmod (dir, chmod_mode)