1 /* -*- buffer-read-only: t -*- vi: set ro: */
2 /* DO NOT EDIT! GENERATED AUTOMATICALLY! */
3 /* provide consistent interface to chown for systems that don't interpret
4 an ID of -1 as meaning `don't change the corresponding ID'.
6 Copyright (C) 1997, 2004-2007, 2009-2011 Free Software Foundation, Inc.
8 This program is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
21 /* written by Jim Meyering */
36 /* Simple stub that always fails with ENOSYS, for mingw. */
38 chown (const char *file _GL_UNUSED, uid_t uid _GL_UNUSED,
45 #else /* HAVE_CHOWN */
47 /* Below we refer to the system's chown(). */
50 /* The results of open() in this file are not used with fchdir,
51 therefore save some unnecessary work in fchdir.c. */
55 /* Provide a more-closely POSIX-conforming version of chown on
56 systems with one or both of the following problems:
57 - chown doesn't treat an ID of -1 as meaning
58 `don't change the corresponding ID'.
59 - chown doesn't dereference symlinks. */
62 rpl_chown (const char *file, uid_t uid, gid_t gid)
65 bool stat_valid = false;
68 # if CHOWN_CHANGE_TIME_BUG
69 if (gid != (gid_t) -1 || uid != (uid_t) -1)
77 # if CHOWN_FAILS_TO_HONOR_ID_OF_NEGATIVE_ONE
78 if (gid == (gid_t) -1 || uid == (uid_t) -1)
80 /* Stat file to get id(s) that should remain unchanged. */
81 if (!stat_valid && stat (file, &st))
83 if (gid == (gid_t) -1)
85 if (uid == (uid_t) -1)
90 # if CHOWN_MODIFIES_SYMLINK
92 /* Handle the case in which the system-supplied chown function
93 does *not* follow symlinks. Instead, it changes permissions
94 on the symlink itself. To work around that, we open the
95 file (but this can fail due to lack of read or write permission) and
96 use fchown on the resulting descriptor. */
97 int open_flags = O_NONBLOCK | O_NOCTTY;
98 int fd = open (file, O_RDONLY | open_flags);
101 && 0 <= (fd = open (file, O_WRONLY | open_flags))))
104 bool fchown_socket_failure;
106 result = fchown (fd, uid, gid);
109 /* POSIX says fchown can fail with errno == EINVAL on sockets
110 and pipes, so fall back on chown in that case. */
111 fchown_socket_failure =
112 (result != 0 && saved_errno == EINVAL
113 && fstat (fd, &st) == 0
114 && (S_ISFIFO (st.st_mode) || S_ISSOCK (st.st_mode)));
118 if (! fchown_socket_failure)
124 else if (errno != EACCES)
129 # if CHOWN_TRAILING_SLASH_BUG
132 size_t len = strlen (file);
133 if (len && file[len - 1] == '/' && stat (file, &st))
138 result = chown (file, uid, gid);
140 # if CHOWN_CHANGE_TIME_BUG
141 if (result == 0 && stat_valid
142 && (uid == st.st_uid || uid == (uid_t) -1)
143 && (gid == st.st_gid || gid == (gid_t) -1))
145 /* No change in ownership, but at least one argument was not -1,
146 so we are required to update ctime. Since chown succeeded,
147 we assume that chmod will do likewise. Fortunately, on all
148 known systems where a 'no-op' chown skips the ctime update, a
149 'no-op' chmod still does the trick. */
150 result = chmod (file, st.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO
151 | S_ISUID | S_ISGID | S_ISVTX));
158 #endif /* HAVE_CHOWN */