gnulib: import chown
[binutils-gdb.git] / gnulib / import / chown.c
1 /* provide consistent interface to chown for systems that don't interpret
2 an ID of -1 as meaning "don't change the corresponding ID".
3
4 Copyright (C) 1997, 2004-2007, 2009-2021 Free Software Foundation, Inc.
5
6 This program is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <https://www.gnu.org/licenses/>. */
18
19 /* written by Jim Meyering */
20
21 #include <config.h>
22
23 /* Specification. */
24 #include <unistd.h>
25
26 #include <errno.h>
27 #include <fcntl.h>
28 #include <stdbool.h>
29 #include <string.h>
30 #include <sys/stat.h>
31
32 #if !HAVE_CHOWN
33
34 /* Simple stub that always fails with ENOSYS, for mingw. */
35 int
36 chown (const char *file _GL_UNUSED, uid_t uid _GL_UNUSED,
37 gid_t gid _GL_UNUSED)
38 {
39 errno = ENOSYS;
40 return -1;
41 }
42
43 #else /* HAVE_CHOWN */
44
45 /* Below we refer to the system's chown(). */
46 # undef chown
47
48 /* Provide a more-closely POSIX-conforming version of chown on
49 systems with one or both of the following problems:
50 - chown doesn't treat an ID of -1 as meaning
51 "don't change the corresponding ID".
52 - chown doesn't dereference symlinks. */
53
54 int
55 rpl_chown (const char *file, uid_t uid, gid_t gid)
56 {
57 struct stat st;
58 bool stat_valid = false;
59 int result;
60
61 # if CHOWN_CHANGE_TIME_BUG
62 if (gid != (gid_t) -1 || uid != (uid_t) -1)
63 {
64 if (stat (file, &st))
65 return -1;
66 stat_valid = true;
67 }
68 # endif
69
70 # if CHOWN_FAILS_TO_HONOR_ID_OF_NEGATIVE_ONE
71 if (gid == (gid_t) -1 || uid == (uid_t) -1)
72 {
73 /* Stat file to get id(s) that should remain unchanged. */
74 if (!stat_valid && stat (file, &st))
75 return -1;
76 if (gid == (gid_t) -1)
77 gid = st.st_gid;
78 if (uid == (uid_t) -1)
79 uid = st.st_uid;
80 }
81 # endif
82
83 # if CHOWN_MODIFIES_SYMLINK
84 {
85 /* Handle the case in which the system-supplied chown function
86 does *not* follow symlinks. Instead, it changes permissions
87 on the symlink itself. To work around that, we open the
88 file (but this can fail due to lack of read or write permission) and
89 use fchown on the resulting descriptor. */
90 int open_flags = O_NONBLOCK | O_NOCTTY | O_CLOEXEC;
91 int fd = open (file, O_RDONLY | open_flags);
92 if (0 <= fd
93 || (errno == EACCES
94 && 0 <= (fd = open (file, O_WRONLY | open_flags))))
95 {
96 int saved_errno;
97 bool fchown_socket_failure;
98
99 result = fchown (fd, uid, gid);
100 saved_errno = errno;
101
102 /* POSIX says fchown can fail with errno == EINVAL on sockets
103 and pipes, so fall back on chown in that case. */
104 fchown_socket_failure =
105 (result != 0 && saved_errno == EINVAL
106 && fstat (fd, &st) == 0
107 && (S_ISFIFO (st.st_mode) || S_ISSOCK (st.st_mode)));
108
109 close (fd);
110
111 if (! fchown_socket_failure)
112 {
113 errno = saved_errno;
114 return result;
115 }
116 }
117 else if (errno != EACCES)
118 return -1;
119 }
120 # endif
121
122 # if CHOWN_TRAILING_SLASH_BUG
123 if (!stat_valid)
124 {
125 size_t len = strlen (file);
126 if (len && file[len - 1] == '/' && stat (file, &st))
127 return -1;
128 }
129 # endif
130
131 result = chown (file, uid, gid);
132
133 # if CHOWN_CHANGE_TIME_BUG
134 if (result == 0 && stat_valid
135 && (uid == st.st_uid || uid == (uid_t) -1)
136 && (gid == st.st_gid || gid == (gid_t) -1))
137 {
138 /* No change in ownership, but at least one argument was not -1,
139 so we are required to update ctime. Since chown succeeded,
140 we assume that chmod will do likewise. Fortunately, on all
141 known systems where a 'no-op' chown skips the ctime update, a
142 'no-op' chmod still does the trick. */
143 result = chmod (file, st.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO
144 | S_ISUID | S_ISGID | S_ISVTX));
145 }
146 # endif
147
148 return result;
149 }
150
151 #endif /* HAVE_CHOWN */