annotate lib/set-mode-acl.c @ 10126:66d52359961e

Rename acl.c to set-mode-acl.c.
author Bruno Haible <bruno@clisp.org>
date Sun, 25 May 2008 13:44:20 +0200
parents
children 0c9f7838132c
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
10126
66d52359961e Rename acl.c to set-mode-acl.c.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
1 /* set-mode-acl.c - set access control list equivalent to a mode
66d52359961e Rename acl.c to set-mode-acl.c.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
2
66d52359961e Rename acl.c to set-mode-acl.c.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
3 Copyright (C) 2002-2003, 2005-2008 Free Software Foundation, Inc.
66d52359961e Rename acl.c to set-mode-acl.c.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4
66d52359961e Rename acl.c to set-mode-acl.c.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
5 This program is free software: you can redistribute it and/or modify
66d52359961e Rename acl.c to set-mode-acl.c.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
6 it under the terms of the GNU General Public License as published by
66d52359961e Rename acl.c to set-mode-acl.c.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
7 the Free Software Foundation; either version 3 of the License, or
66d52359961e Rename acl.c to set-mode-acl.c.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
8 (at your option) any later version.
66d52359961e Rename acl.c to set-mode-acl.c.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
9
66d52359961e Rename acl.c to set-mode-acl.c.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
10 This program is distributed in the hope that it will be useful,
66d52359961e Rename acl.c to set-mode-acl.c.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
66d52359961e Rename acl.c to set-mode-acl.c.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
66d52359961e Rename acl.c to set-mode-acl.c.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
13 GNU General Public License for more details.
66d52359961e Rename acl.c to set-mode-acl.c.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
14
66d52359961e Rename acl.c to set-mode-acl.c.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
15 You should have received a copy of the GNU General Public License
66d52359961e Rename acl.c to set-mode-acl.c.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
66d52359961e Rename acl.c to set-mode-acl.c.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
17
66d52359961e Rename acl.c to set-mode-acl.c.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
18 Written by Paul Eggert and Andreas Gruenbacher. */
66d52359961e Rename acl.c to set-mode-acl.c.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
19
66d52359961e Rename acl.c to set-mode-acl.c.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
20 #include <config.h>
66d52359961e Rename acl.c to set-mode-acl.c.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
21
66d52359961e Rename acl.c to set-mode-acl.c.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
22 #include "acl.h"
66d52359961e Rename acl.c to set-mode-acl.c.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
23
66d52359961e Rename acl.c to set-mode-acl.c.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
24 #include "acl-internal.h"
66d52359961e Rename acl.c to set-mode-acl.c.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
25
66d52359961e Rename acl.c to set-mode-acl.c.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
26 /* If DESC is a valid file descriptor use fchmod to change the
66d52359961e Rename acl.c to set-mode-acl.c.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
27 file's mode to MODE on systems that have fchown. On systems
66d52359961e Rename acl.c to set-mode-acl.c.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
28 that don't have fchown and if DESC is invalid, use chown on
66d52359961e Rename acl.c to set-mode-acl.c.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
29 NAME instead. */
66d52359961e Rename acl.c to set-mode-acl.c.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
30
66d52359961e Rename acl.c to set-mode-acl.c.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
31 int
66d52359961e Rename acl.c to set-mode-acl.c.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
32 chmod_or_fchmod (const char *name, int desc, mode_t mode)
66d52359961e Rename acl.c to set-mode-acl.c.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
33 {
66d52359961e Rename acl.c to set-mode-acl.c.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
34 if (HAVE_FCHMOD && desc != -1)
66d52359961e Rename acl.c to set-mode-acl.c.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
35 return fchmod (desc, mode);
66d52359961e Rename acl.c to set-mode-acl.c.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
36 else
66d52359961e Rename acl.c to set-mode-acl.c.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
37 return chmod (name, mode);
66d52359961e Rename acl.c to set-mode-acl.c.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
38 }
66d52359961e Rename acl.c to set-mode-acl.c.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
39
66d52359961e Rename acl.c to set-mode-acl.c.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
40 /* Set the access control lists of a file. If DESC is a valid file
66d52359961e Rename acl.c to set-mode-acl.c.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
41 descriptor, use file descriptor operations where available, else use
66d52359961e Rename acl.c to set-mode-acl.c.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
42 filename based operations on NAME. If access control lists are not
66d52359961e Rename acl.c to set-mode-acl.c.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
43 available, fchmod the target file to MODE. Also sets the
66d52359961e Rename acl.c to set-mode-acl.c.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
44 non-permission bits of the destination file (S_ISUID, S_ISGID, S_ISVTX)
66d52359961e Rename acl.c to set-mode-acl.c.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
45 to those from MODE if any are set. System call return value
66d52359961e Rename acl.c to set-mode-acl.c.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
46 semantics. */
66d52359961e Rename acl.c to set-mode-acl.c.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
47
66d52359961e Rename acl.c to set-mode-acl.c.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
48 int
66d52359961e Rename acl.c to set-mode-acl.c.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
49 qset_acl (char const *name, int desc, mode_t mode)
66d52359961e Rename acl.c to set-mode-acl.c.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
50 {
66d52359961e Rename acl.c to set-mode-acl.c.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
51 #if USE_ACL
66d52359961e Rename acl.c to set-mode-acl.c.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
52 # if MODE_INSIDE_ACL
66d52359961e Rename acl.c to set-mode-acl.c.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
53 # if HAVE_ACL_SET_FILE && HAVE_ACL_FREE
66d52359961e Rename acl.c to set-mode-acl.c.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
54 /* POSIX 1003.1e draft 17 (abandoned) specific version. */
66d52359961e Rename acl.c to set-mode-acl.c.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
55 /* Linux, FreeBSD, IRIX, Tru64 */
66d52359961e Rename acl.c to set-mode-acl.c.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
56
66d52359961e Rename acl.c to set-mode-acl.c.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
57 /* We must also have acl_from_text and acl_delete_def_file.
66d52359961e Rename acl.c to set-mode-acl.c.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
58 (acl_delete_def_file could be emulated with acl_init followed
66d52359961e Rename acl.c to set-mode-acl.c.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
59 by acl_set_file, but acl_set_file with an empty acl is
66d52359961e Rename acl.c to set-mode-acl.c.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
60 unspecified.) */
66d52359961e Rename acl.c to set-mode-acl.c.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
61
66d52359961e Rename acl.c to set-mode-acl.c.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
62 # ifndef HAVE_ACL_FROM_TEXT
66d52359961e Rename acl.c to set-mode-acl.c.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
63 # error Must have acl_from_text (see POSIX 1003.1e draft 17).
66d52359961e Rename acl.c to set-mode-acl.c.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
64 # endif
66d52359961e Rename acl.c to set-mode-acl.c.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
65 # ifndef HAVE_ACL_DELETE_DEF_FILE
66d52359961e Rename acl.c to set-mode-acl.c.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
66 # error Must have acl_delete_def_file (see POSIX 1003.1e draft 17).
66d52359961e Rename acl.c to set-mode-acl.c.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
67 # endif
66d52359961e Rename acl.c to set-mode-acl.c.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
68
66d52359961e Rename acl.c to set-mode-acl.c.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
69 acl_t acl;
66d52359961e Rename acl.c to set-mode-acl.c.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
70 int ret;
66d52359961e Rename acl.c to set-mode-acl.c.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
71
66d52359961e Rename acl.c to set-mode-acl.c.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
72 if (HAVE_ACL_FROM_MODE) /* Linux */
66d52359961e Rename acl.c to set-mode-acl.c.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
73 {
66d52359961e Rename acl.c to set-mode-acl.c.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
74 acl = acl_from_mode (mode);
66d52359961e Rename acl.c to set-mode-acl.c.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
75 if (!acl)
66d52359961e Rename acl.c to set-mode-acl.c.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
76 return -1;
66d52359961e Rename acl.c to set-mode-acl.c.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
77 }
66d52359961e Rename acl.c to set-mode-acl.c.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
78 else /* FreeBSD, IRIX, Tru64 */
66d52359961e Rename acl.c to set-mode-acl.c.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
79 {
66d52359961e Rename acl.c to set-mode-acl.c.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
80 /* If we were to create the ACL using the functions acl_init(),
66d52359961e Rename acl.c to set-mode-acl.c.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
81 acl_create_entry(), acl_set_tag_type(), acl_set_qualifier(),
66d52359961e Rename acl.c to set-mode-acl.c.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
82 acl_get_permset(), acl_clear_perm[s](), acl_add_perm(), we
66d52359961e Rename acl.c to set-mode-acl.c.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
83 would need to create a qualifier. I don't know how to do this.
66d52359961e Rename acl.c to set-mode-acl.c.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
84 So create it using acl_from_text(). */
66d52359961e Rename acl.c to set-mode-acl.c.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
85
66d52359961e Rename acl.c to set-mode-acl.c.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
86 # if HAVE_ACL_FREE_TEXT /* Tru64 */
66d52359961e Rename acl.c to set-mode-acl.c.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
87 char acl_text[] = "u::---,g::---,o::---,";
66d52359961e Rename acl.c to set-mode-acl.c.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
88 # else /* FreeBSD, IRIX */
66d52359961e Rename acl.c to set-mode-acl.c.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
89 char acl_text[] = "u::---,g::---,o::---";
66d52359961e Rename acl.c to set-mode-acl.c.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
90 # endif
66d52359961e Rename acl.c to set-mode-acl.c.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
91
66d52359961e Rename acl.c to set-mode-acl.c.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
92 if (mode & S_IRUSR) acl_text[ 3] = 'r';
66d52359961e Rename acl.c to set-mode-acl.c.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
93 if (mode & S_IWUSR) acl_text[ 4] = 'w';
66d52359961e Rename acl.c to set-mode-acl.c.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
94 if (mode & S_IXUSR) acl_text[ 5] = 'x';
66d52359961e Rename acl.c to set-mode-acl.c.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
95 if (mode & S_IRGRP) acl_text[10] = 'r';
66d52359961e Rename acl.c to set-mode-acl.c.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
96 if (mode & S_IWGRP) acl_text[11] = 'w';
66d52359961e Rename acl.c to set-mode-acl.c.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
97 if (mode & S_IXGRP) acl_text[12] = 'x';
66d52359961e Rename acl.c to set-mode-acl.c.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
98 if (mode & S_IROTH) acl_text[17] = 'r';
66d52359961e Rename acl.c to set-mode-acl.c.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
99 if (mode & S_IWOTH) acl_text[18] = 'w';
66d52359961e Rename acl.c to set-mode-acl.c.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
100 if (mode & S_IXOTH) acl_text[19] = 'x';
66d52359961e Rename acl.c to set-mode-acl.c.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
101
66d52359961e Rename acl.c to set-mode-acl.c.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
102 acl = acl_from_text (acl_text);
66d52359961e Rename acl.c to set-mode-acl.c.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
103 if (!acl)
66d52359961e Rename acl.c to set-mode-acl.c.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
104 return -1;
66d52359961e Rename acl.c to set-mode-acl.c.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
105 }
66d52359961e Rename acl.c to set-mode-acl.c.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
106 if (HAVE_ACL_SET_FD && desc != -1)
66d52359961e Rename acl.c to set-mode-acl.c.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
107 ret = acl_set_fd (desc, acl);
66d52359961e Rename acl.c to set-mode-acl.c.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
108 else
66d52359961e Rename acl.c to set-mode-acl.c.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
109 ret = acl_set_file (name, ACL_TYPE_ACCESS, acl);
66d52359961e Rename acl.c to set-mode-acl.c.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
110 if (ret != 0)
66d52359961e Rename acl.c to set-mode-acl.c.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
111 {
66d52359961e Rename acl.c to set-mode-acl.c.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
112 int saved_errno = errno;
66d52359961e Rename acl.c to set-mode-acl.c.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
113 acl_free (acl);
66d52359961e Rename acl.c to set-mode-acl.c.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
114
66d52359961e Rename acl.c to set-mode-acl.c.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
115 if (ACL_NOT_WELL_SUPPORTED (errno))
66d52359961e Rename acl.c to set-mode-acl.c.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
116 {
66d52359961e Rename acl.c to set-mode-acl.c.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
117 if (chmod_or_fchmod (name, desc, mode) != 0)
66d52359961e Rename acl.c to set-mode-acl.c.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
118 saved_errno = errno;
66d52359961e Rename acl.c to set-mode-acl.c.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
119 else
66d52359961e Rename acl.c to set-mode-acl.c.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
120 return 0;
66d52359961e Rename acl.c to set-mode-acl.c.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
121 }
66d52359961e Rename acl.c to set-mode-acl.c.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
122 errno = saved_errno;
66d52359961e Rename acl.c to set-mode-acl.c.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
123 return -1;
66d52359961e Rename acl.c to set-mode-acl.c.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
124 }
66d52359961e Rename acl.c to set-mode-acl.c.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
125 else
66d52359961e Rename acl.c to set-mode-acl.c.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
126 acl_free (acl);
66d52359961e Rename acl.c to set-mode-acl.c.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
127
66d52359961e Rename acl.c to set-mode-acl.c.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
128 if (S_ISDIR (mode) && acl_delete_def_file (name))
66d52359961e Rename acl.c to set-mode-acl.c.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
129 return -1;
66d52359961e Rename acl.c to set-mode-acl.c.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
130
66d52359961e Rename acl.c to set-mode-acl.c.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
131 if (mode & (S_ISUID | S_ISGID | S_ISVTX))
66d52359961e Rename acl.c to set-mode-acl.c.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
132 {
66d52359961e Rename acl.c to set-mode-acl.c.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
133 /* We did not call chmod so far, so the special bits have not yet
66d52359961e Rename acl.c to set-mode-acl.c.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
134 been set. */
66d52359961e Rename acl.c to set-mode-acl.c.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
135
66d52359961e Rename acl.c to set-mode-acl.c.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
136 if (chmod_or_fchmod (name, desc, mode))
66d52359961e Rename acl.c to set-mode-acl.c.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
137 return -1;
66d52359961e Rename acl.c to set-mode-acl.c.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
138 }
66d52359961e Rename acl.c to set-mode-acl.c.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
139 return 0;
66d52359961e Rename acl.c to set-mode-acl.c.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
140
66d52359961e Rename acl.c to set-mode-acl.c.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
141 # elif defined ACL_NO_TRIVIAL
66d52359961e Rename acl.c to set-mode-acl.c.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
142 /* Solaris 10, with NFSv4 ACLs. */
66d52359961e Rename acl.c to set-mode-acl.c.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
143 acl_t *aclp;
66d52359961e Rename acl.c to set-mode-acl.c.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
144 char acl_text[] = "user::---,group::---,mask:---,other:---";
66d52359961e Rename acl.c to set-mode-acl.c.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
145
66d52359961e Rename acl.c to set-mode-acl.c.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
146 if (mode & S_IRUSR) acl_text[ 6] = 'r';
66d52359961e Rename acl.c to set-mode-acl.c.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
147 if (mode & S_IWUSR) acl_text[ 7] = 'w';
66d52359961e Rename acl.c to set-mode-acl.c.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
148 if (mode & S_IXUSR) acl_text[ 8] = 'x';
66d52359961e Rename acl.c to set-mode-acl.c.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
149 if (mode & S_IRGRP) acl_text[17] = acl_text[26] = 'r';
66d52359961e Rename acl.c to set-mode-acl.c.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
150 if (mode & S_IWGRP) acl_text[18] = acl_text[27] = 'w';
66d52359961e Rename acl.c to set-mode-acl.c.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
151 if (mode & S_IXGRP) acl_text[19] = acl_text[28] = 'x';
66d52359961e Rename acl.c to set-mode-acl.c.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
152 if (mode & S_IROTH) acl_text[36] = 'r';
66d52359961e Rename acl.c to set-mode-acl.c.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
153 if (mode & S_IWOTH) acl_text[37] = 'w';
66d52359961e Rename acl.c to set-mode-acl.c.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
154 if (mode & S_IXOTH) acl_text[38] = 'x';
66d52359961e Rename acl.c to set-mode-acl.c.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
155
66d52359961e Rename acl.c to set-mode-acl.c.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
156 if (acl_fromtext (acl_text, &aclp) != 0)
66d52359961e Rename acl.c to set-mode-acl.c.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
157 {
66d52359961e Rename acl.c to set-mode-acl.c.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
158 errno = ENOMEM;
66d52359961e Rename acl.c to set-mode-acl.c.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
159 return -1;
66d52359961e Rename acl.c to set-mode-acl.c.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
160 }
66d52359961e Rename acl.c to set-mode-acl.c.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
161 else
66d52359961e Rename acl.c to set-mode-acl.c.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
162 {
66d52359961e Rename acl.c to set-mode-acl.c.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
163 int acl_result = (desc < 0 ? acl_set (name, aclp) : facl_set (desc, aclp));
66d52359961e Rename acl.c to set-mode-acl.c.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
164 int acl_errno = errno;
66d52359961e Rename acl.c to set-mode-acl.c.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
165 acl_free (aclp);
66d52359961e Rename acl.c to set-mode-acl.c.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
166 if (acl_result == 0 || acl_errno != ENOSYS)
66d52359961e Rename acl.c to set-mode-acl.c.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
167 {
66d52359961e Rename acl.c to set-mode-acl.c.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
168 errno = acl_errno;
66d52359961e Rename acl.c to set-mode-acl.c.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
169 return acl_result;
66d52359961e Rename acl.c to set-mode-acl.c.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
170 }
66d52359961e Rename acl.c to set-mode-acl.c.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
171 }
66d52359961e Rename acl.c to set-mode-acl.c.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
172
66d52359961e Rename acl.c to set-mode-acl.c.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
173 return chmod_or_fchmod (name, desc, mode);
66d52359961e Rename acl.c to set-mode-acl.c.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
174
66d52359961e Rename acl.c to set-mode-acl.c.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
175 # else /* Unknown flavor of ACLs */
66d52359961e Rename acl.c to set-mode-acl.c.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
176 return chmod_or_fchmod (name, desc, mode);
66d52359961e Rename acl.c to set-mode-acl.c.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
177 # endif
66d52359961e Rename acl.c to set-mode-acl.c.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
178 # else /* !MODE_INSIDE_ACL */
66d52359961e Rename acl.c to set-mode-acl.c.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
179 # if HAVE_ACL_SET_FILE && HAVE_ACL_FREE
66d52359961e Rename acl.c to set-mode-acl.c.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
180 /* POSIX 1003.1e draft 17 (abandoned) specific version. */
66d52359961e Rename acl.c to set-mode-acl.c.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
181 /* MacOS X */
66d52359961e Rename acl.c to set-mode-acl.c.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
182
66d52359961e Rename acl.c to set-mode-acl.c.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
183 acl_t acl;
66d52359961e Rename acl.c to set-mode-acl.c.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
184 int ret;
66d52359961e Rename acl.c to set-mode-acl.c.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
185
66d52359961e Rename acl.c to set-mode-acl.c.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
186 /* Remove the ACL if the file has ACLs. */
66d52359961e Rename acl.c to set-mode-acl.c.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
187 if (HAVE_ACL_GET_FD && desc != -1)
66d52359961e Rename acl.c to set-mode-acl.c.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
188 acl = acl_get_fd (desc);
66d52359961e Rename acl.c to set-mode-acl.c.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
189 else
66d52359961e Rename acl.c to set-mode-acl.c.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
190 acl = acl_get_file (name, ACL_TYPE_ACCESS);
66d52359961e Rename acl.c to set-mode-acl.c.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
191 if (acl)
66d52359961e Rename acl.c to set-mode-acl.c.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
192 {
66d52359961e Rename acl.c to set-mode-acl.c.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
193 # if HAVE_ACL_COPY_EXT_NATIVE && HAVE_ACL_CREATE_ENTRY_NP /* MacOS X */
66d52359961e Rename acl.c to set-mode-acl.c.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
194 static const char empty_acl_text[] = "!#acl 1\n";
66d52359961e Rename acl.c to set-mode-acl.c.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
195 # else /* Unknown flavor of POSIX-like ACLs */
66d52359961e Rename acl.c to set-mode-acl.c.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
196 # error Unknown flavor of POSIX-like ACLs - add support for your platform.
66d52359961e Rename acl.c to set-mode-acl.c.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
197 # endif
66d52359961e Rename acl.c to set-mode-acl.c.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
198
66d52359961e Rename acl.c to set-mode-acl.c.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
199 acl = acl_from_text (empty_acl_text);
66d52359961e Rename acl.c to set-mode-acl.c.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
200 if (acl)
66d52359961e Rename acl.c to set-mode-acl.c.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
201 {
66d52359961e Rename acl.c to set-mode-acl.c.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
202 if (HAVE_ACL_SET_FD && desc != -1)
66d52359961e Rename acl.c to set-mode-acl.c.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
203 ret = acl_set_fd (desc, acl);
66d52359961e Rename acl.c to set-mode-acl.c.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
204 else
66d52359961e Rename acl.c to set-mode-acl.c.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
205 ret = acl_set_file (name, ACL_TYPE_ACCESS, acl);
66d52359961e Rename acl.c to set-mode-acl.c.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
206 if (ret != 0)
66d52359961e Rename acl.c to set-mode-acl.c.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
207 {
66d52359961e Rename acl.c to set-mode-acl.c.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
208 int saved_errno = errno;
66d52359961e Rename acl.c to set-mode-acl.c.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
209
66d52359961e Rename acl.c to set-mode-acl.c.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
210 acl_free (acl);
66d52359961e Rename acl.c to set-mode-acl.c.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
211
66d52359961e Rename acl.c to set-mode-acl.c.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
212 if (ACL_NOT_WELL_SUPPORTED (saved_errno))
66d52359961e Rename acl.c to set-mode-acl.c.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
213 {
66d52359961e Rename acl.c to set-mode-acl.c.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
214 if (chmod_or_fchmod (name, desc, mode) != 0)
66d52359961e Rename acl.c to set-mode-acl.c.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
215 saved_errno = errno;
66d52359961e Rename acl.c to set-mode-acl.c.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
216 else
66d52359961e Rename acl.c to set-mode-acl.c.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
217 return 0;
66d52359961e Rename acl.c to set-mode-acl.c.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
218 }
66d52359961e Rename acl.c to set-mode-acl.c.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
219 errno = saved_errno;
66d52359961e Rename acl.c to set-mode-acl.c.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
220 return -1;
66d52359961e Rename acl.c to set-mode-acl.c.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
221 }
66d52359961e Rename acl.c to set-mode-acl.c.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
222 }
66d52359961e Rename acl.c to set-mode-acl.c.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
223 }
66d52359961e Rename acl.c to set-mode-acl.c.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
224
66d52359961e Rename acl.c to set-mode-acl.c.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
225 return chmod_or_fchmod (name, desc, mode);
66d52359961e Rename acl.c to set-mode-acl.c.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
226 # else /* Unknown flavor of ACLs */
66d52359961e Rename acl.c to set-mode-acl.c.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
227 return chmod_or_fchmod (name, desc, mode);
66d52359961e Rename acl.c to set-mode-acl.c.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
228 # endif
66d52359961e Rename acl.c to set-mode-acl.c.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
229 # endif
66d52359961e Rename acl.c to set-mode-acl.c.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
230 #else /* !USE_ACL */
66d52359961e Rename acl.c to set-mode-acl.c.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
231 return chmod_or_fchmod (name, desc, mode);
66d52359961e Rename acl.c to set-mode-acl.c.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
232 #endif
66d52359961e Rename acl.c to set-mode-acl.c.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
233 }
66d52359961e Rename acl.c to set-mode-acl.c.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
234
66d52359961e Rename acl.c to set-mode-acl.c.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
235 /* As with qset_acl, but also output a diagnostic on failure. */
66d52359961e Rename acl.c to set-mode-acl.c.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
236
66d52359961e Rename acl.c to set-mode-acl.c.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
237 int
66d52359961e Rename acl.c to set-mode-acl.c.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
238 set_acl (char const *name, int desc, mode_t mode)
66d52359961e Rename acl.c to set-mode-acl.c.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
239 {
66d52359961e Rename acl.c to set-mode-acl.c.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
240 int r = qset_acl (name, desc, mode);
66d52359961e Rename acl.c to set-mode-acl.c.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
241 if (r != 0)
66d52359961e Rename acl.c to set-mode-acl.c.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
242 error (0, errno, _("setting permissions for %s"), quote (name));
66d52359961e Rename acl.c to set-mode-acl.c.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
243 return r;
66d52359961e Rename acl.c to set-mode-acl.c.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
244 }