summaryrefslogtreecommitdiff
path: root/lib/fchmodat.c
diff options
context:
space:
mode:
authorPaul Eggert <eggert@cs.ucla.edu>2022-07-05 23:57:32 -0500
committerPaul Eggert <eggert@cs.ucla.edu>2022-07-06 00:00:18 -0500
commit27436451ecbf250db4d1704c586763cb40e6dfeb (patch)
treea0713c338993726cc8bf4a8d1cef7d0336fa5065 /lib/fchmodat.c
parent2be06b13dd9582d7216c479e9874f0df6d32746b (diff)
downloademacs-27436451ecbf250db4d1704c586763cb40e6dfeb.tar.gz
emacs-27436451ecbf250db4d1704c586763cb40e6dfeb.tar.bz2
emacs-27436451ecbf250db4d1704c586763cb40e6dfeb.zip
Update from Gnulib by running admin/merge-gnulib
* admin/merge-gnulib (AVOIDED_MODULES): Add chmod.
Diffstat (limited to 'lib/fchmodat.c')
-rw-r--r--lib/fchmodat.c59
1 files changed, 20 insertions, 39 deletions
diff --git a/lib/fchmodat.c b/lib/fchmodat.c
index dc535833660..164e2c4a95f 100644
--- a/lib/fchmodat.c
+++ b/lib/fchmodat.c
@@ -83,9 +83,10 @@ fchmodat (int dir, char const *file, mode_t mode, int flags)
# if NEED_FCHMODAT_NONSYMLINK_FIX
if (flags == AT_SYMLINK_NOFOLLOW)
{
- struct stat st;
+# if HAVE_READLINKAT
+ char readlink_buf[1];
-# if defined O_PATH && defined AT_EMPTY_PATH
+# ifdef O_PATH
/* Open a file descriptor with O_NOFOLLOW, to make sure we don't
follow symbolic links, if /proc is mounted. O_PATH is used to
avoid a failure if the file is not readable.
@@ -94,49 +95,29 @@ fchmodat (int dir, char const *file, mode_t mode, int flags)
if (fd < 0)
return fd;
- /* Up to Linux 5.3 at least, when FILE refers to a symbolic link, the
- chmod call below will change the permissions of the symbolic link
- - which is undesired - and on many file systems (ext4, btrfs, jfs,
- xfs, ..., but not reiserfs) fail with error EOPNOTSUPP - which is
- misleading. Therefore test for a symbolic link explicitly.
- Use fstatat because fstat does not work on O_PATH descriptors
- before Linux 3.6. */
- if (fstatat (fd, "", &st, AT_EMPTY_PATH) != 0)
+ int err;
+ if (0 <= readlinkat (fd, "", readlink_buf, sizeof readlink_buf))
+ err = EOPNOTSUPP;
+ else if (errno == EINVAL)
{
- int stat_errno = errno;
- close (fd);
- errno = stat_errno;
- return -1;
- }
- if (S_ISLNK (st.st_mode))
- {
- close (fd);
- errno = EOPNOTSUPP;
- return -1;
+ static char const fmt[] = "/proc/self/fd/%d";
+ char buf[sizeof fmt - sizeof "%d" + INT_BUFSIZE_BOUND (int)];
+ sprintf (buf, fmt, fd);
+ err = chmod (buf, mode) == 0 ? 0 : errno == ENOENT ? -1 : errno;
}
+ else
+ err = errno == ENOENT ? -1 : errno;
-# if defined __linux__ || defined __ANDROID__ || defined __CYGWIN__
- static char const fmt[] = "/proc/self/fd/%d";
- char buf[sizeof fmt - sizeof "%d" + INT_BUFSIZE_BOUND (int)];
- sprintf (buf, fmt, fd);
- int chmod_result = chmod (buf, mode);
- int chmod_errno = errno;
close (fd);
- if (chmod_result == 0)
- return chmod_result;
- if (chmod_errno != ENOENT)
- {
- errno = chmod_errno;
- return chmod_result;
- }
+
+ errno = err;
+ if (0 <= err)
+ return err == 0 ? 0 : -1;
# endif
- /* /proc is not mounted or would not work as in GNU/Linux. */
-# else
- int fstatat_result = fstatat (dir, file, &st, AT_SYMLINK_NOFOLLOW);
- if (fstatat_result != 0)
- return fstatat_result;
- if (S_ISLNK (st.st_mode))
+ /* O_PATH + /proc is not supported. */
+
+ if (0 <= readlinkat (dir, file, readlink_buf, sizeof readlink_buf))
{
errno = EOPNOTSUPP;
return -1;