Fix formatting in gdb.ada/lazy-string.exp
[binutils-gdb.git] / gnulib / import / chown.c
blob705ca85874d39f29fda0a33b4ac42348ca1c6ad5
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".
4 Copyright (C) 1997, 2004-2007, 2009-2022 Free Software Foundation, Inc.
6 This file is free software: you can redistribute it and/or modify
7 it under the terms of the GNU Lesser General Public License as
8 published by the Free Software Foundation; either version 2.1 of the
9 License, or (at your option) any later version.
11 This file 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 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public License
17 along with this program. If not, see <https://www.gnu.org/licenses/>. */
19 /* written by Jim Meyering */
21 #include <config.h>
23 /* Specification. */
24 #include <unistd.h>
26 #include <errno.h>
27 #include <fcntl.h>
28 #include <stdbool.h>
29 #include <string.h>
30 #include <sys/stat.h>
32 #if !HAVE_CHOWN
34 /* Simple stub that always fails with ENOSYS, for mingw. */
35 int
36 chown (_GL_UNUSED const char *file, _GL_UNUSED uid_t uid,
37 _GL_UNUSED gid_t gid)
39 errno = ENOSYS;
40 return -1;
43 #else /* HAVE_CHOWN */
45 /* Below we refer to the system's chown(). */
46 # undef chown
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. */
54 int
55 rpl_chown (const char *file, uid_t uid, gid_t gid)
57 struct stat st;
58 bool stat_valid = false;
59 int result;
61 # if CHOWN_CHANGE_TIME_BUG
62 if (gid != (gid_t) -1 || uid != (uid_t) -1)
64 if (stat (file, &st))
65 return -1;
66 stat_valid = true;
68 # endif
70 # if CHOWN_FAILS_TO_HONOR_ID_OF_NEGATIVE_ONE
71 if (gid == (gid_t) -1 || uid == (uid_t) -1)
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;
81 # endif
83 # if CHOWN_MODIFIES_SYMLINK
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))))
96 int saved_errno;
97 bool fchown_socket_failure;
99 result = fchown (fd, uid, gid);
100 saved_errno = errno;
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)));
109 close (fd);
111 if (! fchown_socket_failure)
113 errno = saved_errno;
114 return result;
117 else if (errno != EACCES)
118 return -1;
120 # endif
122 # if CHOWN_TRAILING_SLASH_BUG
123 if (!stat_valid)
125 size_t len = strlen (file);
126 if (len && file[len - 1] == '/' && stat (file, &st))
127 return -1;
129 # endif
131 result = chown (file, uid, gid);
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))
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));
146 # endif
148 return result;
151 #endif /* HAVE_CHOWN */