Mercurial > hg > octave-nkf > gnulib-hg
annotate lib/dirchownmod.c @ 7302:8a1a9361108c
* _fpending.c: Include <config.h> unconditionally, since we no
longer worry about uses that don't define HAVE_CONFIG_H.
* acl.c, alloca.c, argmatch.c, atexit.c, backupfile.c:
* basename.c, c-stack.c, c-strtod.c, calloc.c, canon-host.c:
* canonicalize.c, chdir-long.c, chdir-safer.c, chown.c:
* cloexec.c, close-stream.c, closeout.c, creat-safer.c:
* cycle-check.c, diacrit.c, dirchownmod.c, dirfd.c, dirname.c:
* dup-safer.c, dup2.c, error.c, euidaccess.c, exclude.c:
* exitfail.c, fchmodat.c, fchown-stub.c, fd-safer.c:
* file-type.c, fileblocks.c, filemode.c, filenamecat.c:
* fnmatch.c, fopen-safer.c, fprintftime.c, free.c, fsusage.c:
* ftruncate.c, fts-cycle.c, fts.c, full-write.c, gai_strerror.c:
* getcwd.c, getdate.y, getdomainname.c, getgroups.c:
* gethostname.c, gethrxtime.c, getloadavg.c, getlogin_r.c:
* getndelim2.c, getnline.c, getopt.c, getopt1.c, getpass.c:
* gettime.c, gettimeofday.c, getugroups.c, getusershell.c:
* glob.c, group-member.c, hard-locale.c, hash-pjw.c, hash.c:
* human.c, idcache.c, inet_ntop.c, inet_pton.c, inttostr.c:
* isdir.c, lchown.c, linebuffer.c, long-options.c, lstat.c:
* malloc.c, md5.c, memcasecmp.c, memchr.c, memcmp.c, memcoll.c:
* memcpy.c, memmove.c, memrchr.c, mkancesdirs.c, mkdir-p.c:
* mkdir.c, mkdirat.c, mkstemp-safer.c, mkstemp.c, modechange.c:
* mountlist.c, nanosleep.c, obstack.c, open-safer.c:
* openat-die.c, openat.c, pagealign_alloc.c, physmem.c:
* pipe-safer.c, posixtm.c, posixver.c, putenv.c, quote.c:
* quotearg.c, raise.c, readtokens.c, readtokens0.c, readutmp.c:
* realloc.c, regex.c, rename.c, rmdir.c, rpmatch.c, safe-read.c:
* same.c, save-cwd.c, savedir.c, setenv.c, settime.c, sha1.c:
* sig2str.c, snprintf.c, strdup.c, strerror.c, strftime.c:
* stripslash.c, strndup.c, strnlen.c, strpbrk.c, strtod.c:
* strtoimax.c, strtol.c, strverscmp.c, tempname.c, time_r.c:
* timegm.c, tmpfile-safer.c, unlinkdir.c, userspec.c, utime.c:
* utimecmp.c, utimens.c, version-etc-fsf.c, version-etc.c:
* xalloc-die.c, xgetcwd.c, xgethostname.c, xmalloc.c:
* xmemcoll.c, xnanosleep.c, xreadlink.c, xstrtod.c:
* xstrtoimax.c, xstrtol.c, xstrtoumax.c, yesno.c:
Likewise.
author | Paul Eggert <eggert@cs.ucla.edu> |
---|---|
date | Wed, 13 Sep 2006 22:38:14 +0000 |
parents | 3307ae6ea2e5 |
children | 796e1837ac66 |
rev | line source |
---|---|
6969 | 1 /* Change the ownership and mode bits of a directory. |
2 | |
3 Copyright (C) 2006 Free Software Foundation, Inc. | |
4 | |
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 2, or (at your option) | |
8 any later version. | |
9 | |
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. | |
14 | |
15 You should have received a copy of the GNU General Public License | |
16 along with this program; if not, write to the Free Software Foundation, | |
17 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ | |
18 | |
19 /* Written by Paul Eggert. */ | |
20 | |
7302
8a1a9361108c
* _fpending.c: Include <config.h> unconditionally, since we no
Paul Eggert <eggert@cs.ucla.edu>
parents:
7225
diff
changeset
|
21 #include <config.h> |
6969 | 22 |
23 #include "dirchownmod.h" | |
24 | |
25 #include <errno.h> | |
26 #include <sys/types.h> | |
27 #include <sys/stat.h> | |
28 #include <fcntl.h> | |
29 #include <unistd.h> | |
30 | |
31 #include "lchmod.h" | |
32 #include "stat-macros.h" | |
33 | |
7173 | 34 #ifndef HAVE_FCHMOD |
35 # define HAVE_FCHMOD 0 | |
36 # undef fchmod | |
37 # define fchmod(fd, mode) (-1) | |
38 #endif | |
39 | |
6969 | 40 /* Change the ownership and mode bits of the directory DIR. |
41 | |
42 If MKDIR_MODE is not (mode_t) -1, mkdir (DIR, MKDIR_MODE) has just | |
43 been executed successfully with umask zero, so DIR should be a | |
44 directory (not a symbolic link). | |
45 | |
46 First, set the file's owner to OWNER and group to GROUP, but leave | |
47 the owner alone if OWNER is (uid_t) -1, and similarly for GROUP. | |
48 | |
49 Then, set the file's mode bits to MODE, except preserve any of the | |
50 bits that correspond to zero bits in MODE_BITS. In other words, | |
51 MODE_BITS is a mask that specifies which of the file's mode bits | |
52 should be set or cleared. MODE should be a subset of MODE_BITS, | |
53 which in turn should be a subset of CHMOD_MODE_BITS. | |
54 | |
55 This implementation assumes the current umask is zero. | |
56 | |
57 Return 0 if successful, -1 (setting errno) otherwise. Unsuccessful | |
58 calls may do the chown but not the chmod. */ | |
59 | |
60 int | |
61 dirchownmod (char const *dir, mode_t mkdir_mode, | |
62 uid_t owner, gid_t group, | |
63 mode_t mode, mode_t mode_bits) | |
64 { | |
65 struct stat st; | |
66 int result; | |
67 | |
68 /* Manipulate DIR via a file descriptor if possible, to avoid some races. */ | |
69 int open_flags = O_RDONLY | O_DIRECTORY | O_NOCTTY | O_NOFOLLOW | O_NONBLOCK; | |
70 int fd = open (dir, open_flags); | |
71 | |
72 /* Fail if the directory is unreadable, the directory previously | |
73 existed or was created without read permission. Otherwise, get | |
74 the file's status. */ | |
75 if (0 <= fd) | |
76 result = fstat (fd, &st); | |
77 else if (errno != EACCES | |
78 || (mkdir_mode != (mode_t) -1 && mkdir_mode & S_IRUSR)) | |
79 return fd; | |
80 else | |
81 result = stat (dir, &st); | |
82 | |
83 if (result == 0) | |
84 { | |
85 mode_t dir_mode = st.st_mode; | |
86 | |
87 /* Check whether DIR is a directory. If FD is nonnegative, this | |
88 check avoids changing the ownership and mode bits of the | |
89 wrong file in many cases. This doesn't fix all the race | |
90 conditions, but it is better than nothing. */ | |
91 if (! S_ISDIR (dir_mode)) | |
92 { | |
93 errno = ENOTDIR; | |
94 result = -1; | |
95 } | |
96 else | |
97 { | |
98 /* If at least one of the S_IXUGO bits are set, chown might | |
99 clear the S_ISUID and S_SGID bits. Keep track of any | |
100 file mode bits whose values are indeterminate due to this | |
101 issue. */ | |
102 mode_t indeterminate = 0; | |
103 | |
104 /* On some systems, chown clears S_ISUID and S_ISGID, so do | |
105 chown before chmod. On older System V hosts, ordinary | |
106 users can give their files away via chown; don't worry | |
107 about that here, since users shouldn't do that. */ | |
108 | |
109 if ((owner != (uid_t) -1 && owner != st.st_uid) | |
110 || (group != (gid_t) -1 && group != st.st_gid)) | |
111 { | |
112 result = (0 <= fd | |
113 ? fchown (fd, owner, group) | |
114 : mkdir_mode != (mode_t) -1 | |
115 ? lchown (dir, owner, group) | |
116 : chown (dir, owner, group)); | |
117 | |
118 /* Either the user cares about an indeterminate bit and | |
119 it'll be set properly by chmod below, or the user | |
120 doesn't care and it's OK to use the bit's pre-chown | |
121 value. So there's no need to re-stat DIR here. */ | |
122 | |
123 if (result == 0 && (dir_mode & S_IXUGO)) | |
124 indeterminate = dir_mode & (S_ISUID | S_ISGID); | |
125 } | |
126 | |
127 /* If the file mode bits might not be right, use chmod to | |
128 change them. Don't change bits the user doesn't care | |
129 about. */ | |
130 if (result == 0 && (((dir_mode ^ mode) | indeterminate) & mode_bits)) | |
131 { | |
132 mode_t chmod_mode = | |
133 mode | (dir_mode & CHMOD_MODE_BITS & ~mode_bits); | |
7173 | 134 result = (HAVE_FCHMOD && 0 <= fd |
6969 | 135 ? fchmod (fd, chmod_mode) |
136 : mkdir_mode != (mode_t) -1 | |
137 ? lchmod (dir, chmod_mode) | |
138 : chmod (dir, chmod_mode)); | |
139 } | |
140 } | |
141 } | |
142 | |
143 if (0 <= fd) | |
144 { | |
145 if (result == 0) | |
146 result = close (fd); | |
147 else | |
148 { | |
149 int e = errno; | |
150 close (fd); | |
151 errno = e; | |
152 } | |
153 } | |
154 | |
155 return result; | |
156 } |