annotate lib/dirchownmod.c @ 17907:0a1c2535cad9

euidaccess: Fix Android build * modules/euidaccess (Depends-on): Add fcntl-h to ensure that AT_EACCESS gets declared.
author Kevin Cernekee <cernekee@google.com>
date Wed, 11 Feb 2015 15:22:54 -0800
parents ab58d4870664
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
6969
bbdf9204a185 Import from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
1 /* Change the ownership and mode bits of a directory.
bbdf9204a185 Import from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
2
17848
ab58d4870664 version-etc: new year
Paul Eggert <eggert@cs.ucla.edu>
parents: 17587
diff changeset
3 Copyright (C) 2006-2015 Free Software Foundation, Inc.
6969
bbdf9204a185 Import from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
4
9309
bbbbbf4cd1c5 Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents: 8982
diff changeset
5 This program is free software: you can redistribute it and/or modify
6969
bbdf9204a185 Import from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
6 it under the terms of the GNU General Public License as published by
9309
bbbbbf4cd1c5 Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents: 8982
diff changeset
7 the Free Software Foundation; either version 3 of the License, or
bbbbbf4cd1c5 Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents: 8982
diff changeset
8 (at your option) any later version.
6969
bbdf9204a185 Import from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
9
bbdf9204a185 Import from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
10 This program is distributed in the hope that it will be useful,
bbdf9204a185 Import from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
bbdf9204a185 Import from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
bbdf9204a185 Import from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
13 GNU General Public License for more details.
bbdf9204a185 Import from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
14
bbdf9204a185 Import from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
15 You should have received a copy of the GNU General Public License
9309
bbbbbf4cd1c5 Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents: 8982
diff changeset
16 along with this program. If not, see <http://www.gnu.org/licenses/>. */
6969
bbdf9204a185 Import from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
17
bbdf9204a185 Import from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
18 /* Written by Paul Eggert. */
bbdf9204a185 Import from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
19
7302
8a1a9361108c * _fpending.c: Include <config.h> unconditionally, since we no
Paul Eggert <eggert@cs.ucla.edu>
parents: 7225
diff changeset
20 #include <config.h>
6969
bbdf9204a185 Import from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
21
bbdf9204a185 Import from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
22 #include "dirchownmod.h"
bbdf9204a185 Import from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
23
bbdf9204a185 Import from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
24 #include <errno.h>
bbdf9204a185 Import from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
25 #include <sys/types.h>
bbdf9204a185 Import from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
26 #include <sys/stat.h>
bbdf9204a185 Import from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
27 #include <unistd.h>
bbdf9204a185 Import from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
28
bbdf9204a185 Import from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
29 #include "stat-macros.h"
bbdf9204a185 Import from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
30
7173
0dfc3e6e50d2 BeOS portability.
Paul Eggert <eggert@cs.ucla.edu>
parents: 6969
diff changeset
31 #ifndef HAVE_FCHMOD
0dfc3e6e50d2 BeOS portability.
Paul Eggert <eggert@cs.ucla.edu>
parents: 6969
diff changeset
32 # define HAVE_FCHMOD 0
0dfc3e6e50d2 BeOS portability.
Paul Eggert <eggert@cs.ucla.edu>
parents: 6969
diff changeset
33 # undef fchmod
0dfc3e6e50d2 BeOS portability.
Paul Eggert <eggert@cs.ucla.edu>
parents: 6969
diff changeset
34 # define fchmod(fd, mode) (-1)
0dfc3e6e50d2 BeOS portability.
Paul Eggert <eggert@cs.ucla.edu>
parents: 6969
diff changeset
35 #endif
0dfc3e6e50d2 BeOS portability.
Paul Eggert <eggert@cs.ucla.edu>
parents: 6969
diff changeset
36
7312
796e1837ac66 * lib/dirchownmod.c: Don't include fcntl.h; no longer needed.
Paul Eggert <eggert@cs.ucla.edu>
parents: 7302
diff changeset
37 /* Change the ownership and mode bits of a directory. If FD is
796e1837ac66 * lib/dirchownmod.c: Don't include fcntl.h; no longer needed.
Paul Eggert <eggert@cs.ucla.edu>
parents: 7302
diff changeset
38 nonnegative, it should be a file descriptor associated with the
796e1837ac66 * lib/dirchownmod.c: Don't include fcntl.h; no longer needed.
Paul Eggert <eggert@cs.ucla.edu>
parents: 7302
diff changeset
39 directory; close it before returning. DIR is the name of the
796e1837ac66 * lib/dirchownmod.c: Don't include fcntl.h; no longer needed.
Paul Eggert <eggert@cs.ucla.edu>
parents: 7302
diff changeset
40 directory.
6969
bbdf9204a185 Import from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
41
bbdf9204a185 Import from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
42 If MKDIR_MODE is not (mode_t) -1, mkdir (DIR, MKDIR_MODE) has just
bbdf9204a185 Import from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
43 been executed successfully with umask zero, so DIR should be a
bbdf9204a185 Import from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
44 directory (not a symbolic link).
bbdf9204a185 Import from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
45
bbdf9204a185 Import from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
46 First, set the file's owner to OWNER and group to GROUP, but leave
bbdf9204a185 Import from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
47 the owner alone if OWNER is (uid_t) -1, and similarly for GROUP.
bbdf9204a185 Import from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
48
bbdf9204a185 Import from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
49 Then, set the file's mode bits to MODE, except preserve any of the
bbdf9204a185 Import from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
50 bits that correspond to zero bits in MODE_BITS. In other words,
bbdf9204a185 Import from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
51 MODE_BITS is a mask that specifies which of the file's mode bits
bbdf9204a185 Import from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
52 should be set or cleared. MODE should be a subset of MODE_BITS,
bbdf9204a185 Import from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
53 which in turn should be a subset of CHMOD_MODE_BITS.
bbdf9204a185 Import from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
54
bbdf9204a185 Import from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
55 This implementation assumes the current umask is zero.
bbdf9204a185 Import from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
56
bbdf9204a185 Import from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
57 Return 0 if successful, -1 (setting errno) otherwise. Unsuccessful
bbdf9204a185 Import from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
58 calls may do the chown but not the chmod. */
bbdf9204a185 Import from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
59
bbdf9204a185 Import from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
60 int
7312
796e1837ac66 * lib/dirchownmod.c: Don't include fcntl.h; no longer needed.
Paul Eggert <eggert@cs.ucla.edu>
parents: 7302
diff changeset
61 dirchownmod (int fd, char const *dir, mode_t mkdir_mode,
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 10664
diff changeset
62 uid_t owner, gid_t group,
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 10664
diff changeset
63 mode_t mode, mode_t mode_bits)
6969
bbdf9204a185 Import from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
64 {
bbdf9204a185 Import from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
65 struct stat st;
7312
796e1837ac66 * lib/dirchownmod.c: Don't include fcntl.h; no longer needed.
Paul Eggert <eggert@cs.ucla.edu>
parents: 7302
diff changeset
66 int result = (fd < 0 ? stat (dir, &st) : fstat (fd, &st));
6969
bbdf9204a185 Import from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
67
bbdf9204a185 Import from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
68 if (result == 0)
bbdf9204a185 Import from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
69 {
bbdf9204a185 Import from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
70 mode_t dir_mode = st.st_mode;
bbdf9204a185 Import from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
71
bbdf9204a185 Import from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
72 /* Check whether DIR is a directory. If FD is nonnegative, this
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 10664
diff changeset
73 check avoids changing the ownership and mode bits of the
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 10664
diff changeset
74 wrong file in many cases. This doesn't fix all the race
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 10664
diff changeset
75 conditions, but it is better than nothing. */
6969
bbdf9204a185 Import from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
76 if (! S_ISDIR (dir_mode))
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 10664
diff changeset
77 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 10664
diff changeset
78 errno = ENOTDIR;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 10664
diff changeset
79 result = -1;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 10664
diff changeset
80 }
6969
bbdf9204a185 Import from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
81 else
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 10664
diff changeset
82 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 10664
diff changeset
83 /* If at least one of the S_IXUGO bits are set, chown might
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 10664
diff changeset
84 clear the S_ISUID and S_SGID bits. Keep track of any
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 10664
diff changeset
85 file mode bits whose values are indeterminate due to this
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 10664
diff changeset
86 issue. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 10664
diff changeset
87 mode_t indeterminate = 0;
6969
bbdf9204a185 Import from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
88
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 10664
diff changeset
89 /* On some systems, chown clears S_ISUID and S_ISGID, so do
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 10664
diff changeset
90 chown before chmod. On older System V hosts, ordinary
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 10664
diff changeset
91 users can give their files away via chown; don't worry
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 10664
diff changeset
92 about that here, since users shouldn't do that. */
6969
bbdf9204a185 Import from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
93
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 10664
diff changeset
94 if ((owner != (uid_t) -1 && owner != st.st_uid)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 10664
diff changeset
95 || (group != (gid_t) -1 && group != st.st_gid))
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 10664
diff changeset
96 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 10664
diff changeset
97 result = (0 <= fd
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 10664
diff changeset
98 ? fchown (fd, owner, group)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 10664
diff changeset
99 : mkdir_mode != (mode_t) -1
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 10664
diff changeset
100 ? lchown (dir, owner, group)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 10664
diff changeset
101 : chown (dir, owner, group));
6969
bbdf9204a185 Import from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
102
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 10664
diff changeset
103 /* Either the user cares about an indeterminate bit and
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 10664
diff changeset
104 it'll be set properly by chmod below, or the user
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 10664
diff changeset
105 doesn't care and it's OK to use the bit's pre-chown
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 10664
diff changeset
106 value. So there's no need to re-stat DIR here. */
6969
bbdf9204a185 Import from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
107
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 10664
diff changeset
108 if (result == 0 && (dir_mode & S_IXUGO))
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 10664
diff changeset
109 indeterminate = dir_mode & (S_ISUID | S_ISGID);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 10664
diff changeset
110 }
6969
bbdf9204a185 Import from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
111
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 10664
diff changeset
112 /* If the file mode bits might not be right, use chmod to
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 10664
diff changeset
113 change them. Don't change bits the user doesn't care
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 10664
diff changeset
114 about. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 10664
diff changeset
115 if (result == 0 && (((dir_mode ^ mode) | indeterminate) & mode_bits))
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 10664
diff changeset
116 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 10664
diff changeset
117 mode_t chmod_mode =
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 10664
diff changeset
118 mode | (dir_mode & CHMOD_MODE_BITS & ~mode_bits);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 10664
diff changeset
119 result = (HAVE_FCHMOD && 0 <= fd
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 10664
diff changeset
120 ? fchmod (fd, chmod_mode)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 10664
diff changeset
121 : mkdir_mode != (mode_t) -1
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 10664
diff changeset
122 ? lchmod (dir, chmod_mode)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 10664
diff changeset
123 : chmod (dir, chmod_mode));
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 10664
diff changeset
124 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 10664
diff changeset
125 }
6969
bbdf9204a185 Import from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
126 }
bbdf9204a185 Import from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
127
bbdf9204a185 Import from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
128 if (0 <= fd)
bbdf9204a185 Import from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
129 {
bbdf9204a185 Import from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
130 if (result == 0)
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 10664
diff changeset
131 result = close (fd);
6969
bbdf9204a185 Import from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
132 else
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 10664
diff changeset
133 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 10664
diff changeset
134 int e = errno;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 10664
diff changeset
135 close (fd);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 10664
diff changeset
136 errno = e;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 10664
diff changeset
137 }
6969
bbdf9204a185 Import from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
138 }
bbdf9204a185 Import from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
139
bbdf9204a185 Import from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
140 return result;
bbdf9204a185 Import from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
141 }