exp2l: Work around a NetBSD 10.0/i386 bug.
[gnulib.git] / lib / lchmod.c
blob8056f0b197df92d65c4918a02cf88f40aff9e9ab
1 /* Implement lchmod on platforms where it does not work correctly.
3 Copyright 2020-2024 Free Software Foundation, Inc.
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <https://www.gnu.org/licenses/>. */
18 /* written by Paul Eggert */
20 #include <config.h>
22 /* Specification. */
23 #include <sys/stat.h>
25 #include <errno.h>
26 #include <fcntl.h>
27 #include <stdio.h>
28 #include <string.h>
29 #include <unistd.h>
31 #include <intprops.h>
33 /* Work like chmod, except when FILE is a symbolic link.
34 In that case, on systems where permissions on symbolic links are unsupported
35 (such as Linux), set errno to EOPNOTSUPP and return -1. */
37 int
38 lchmod (char const *file, mode_t mode)
40 char readlink_buf[1];
42 #ifdef O_PATH
43 /* Open a file descriptor with O_NOFOLLOW, to make sure we don't
44 follow symbolic links, if /proc is mounted. O_PATH is used to
45 avoid a failure if the file is not readable.
46 Cf. <https://sourceware.org/bugzilla/show_bug.cgi?id=14578> */
47 int fd = open (file, O_PATH | O_NOFOLLOW | O_CLOEXEC);
48 if (fd < 0)
49 return fd;
51 int err;
52 if (0 <= readlinkat (fd, "", readlink_buf, sizeof readlink_buf))
53 err = EOPNOTSUPP;
54 else if (errno == EINVAL)
56 static char const fmt[] = "/proc/self/fd/%d";
57 char buf[sizeof fmt - sizeof "%d" + INT_BUFSIZE_BOUND (int)];
58 sprintf (buf, fmt, fd);
59 err = chmod (buf, mode) == 0 ? 0 : errno == ENOENT ? -1 : errno;
61 else
62 err = errno == ENOENT ? -1 : errno;
64 close (fd);
66 errno = err;
67 if (0 <= err)
68 return err == 0 ? 0 : -1;
69 #endif
71 size_t len = strlen (file);
72 if (len && file[len - 1] == '/')
74 struct stat st;
75 if (lstat (file, &st) < 0)
76 return -1;
77 if (!S_ISDIR (st.st_mode))
79 errno = ENOTDIR;
80 return -1;
84 /* O_PATH + /proc is not supported. */
86 if (0 <= readlink (file, readlink_buf, sizeof readlink_buf))
88 errno = EOPNOTSUPP;
89 return -1;
92 /* Fall back on chmod, despite a possible race. */
93 return chmod (file, mode);