Avoid gcc warning on cygwin.
[gnulib.git] / lib / copy-acl.c
1 /* copy-acl.c - copy access control list from one file to another file
2
3    Copyright (C) 2002-2003, 2005-2008 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 3 of the License, or
8    (at your option) 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, see <http://www.gnu.org/licenses/>.
17
18    Written by Paul Eggert and Andreas Gruenbacher.  */
19
20 #include <config.h>
21
22 #include "acl.h"
23
24 #include "acl-internal.h"
25
26
27 /* Copy access control lists from one file to another. If SOURCE_DESC is
28    a valid file descriptor, use file descriptor operations, else use
29    filename based operations on SRC_NAME. Likewise for DEST_DESC and
30    DST_NAME.
31    If access control lists are not available, fchmod the target file to
32    MODE.  Also sets the non-permission bits of the destination file
33    (S_ISUID, S_ISGID, S_ISVTX) to those from MODE if any are set.
34    Return 0 if successful.
35    Return -2 and set errno for an error relating to the source file.
36    Return -1 and set errno for an error relating to the destination file.  */
37
38 static int
39 qcopy_acl (const char *src_name, int source_desc, const char *dst_name,
40            int dest_desc, mode_t mode)
41 {
42   int ret;
43
44 #if USE_ACL && HAVE_ACL_GET_FILE
45   /* POSIX 1003.1e (draft 17 -- abandoned) specific version.  */
46   /* Linux, FreeBSD, MacOS X, IRIX, Tru64 */
47
48   acl_t acl;
49   if (HAVE_ACL_GET_FD && source_desc != -1)
50     acl = acl_get_fd (source_desc);
51   else
52     acl = acl_get_file (src_name, ACL_TYPE_ACCESS);
53   if (acl == NULL)
54     {
55       if (ACL_NOT_WELL_SUPPORTED (errno))
56         return qset_acl (dst_name, dest_desc, mode);
57       else
58         return -2;
59     }
60
61   if (HAVE_ACL_SET_FD && dest_desc != -1)
62     ret = acl_set_fd (dest_desc, acl);
63   else
64     ret = acl_set_file (dst_name, ACL_TYPE_ACCESS, acl);
65   if (ret != 0)
66     {
67       int saved_errno = errno;
68
69       if (ACL_NOT_WELL_SUPPORTED (errno))
70         {
71           int nontrivial = acl_access_nontrivial (acl);
72
73           acl_free (acl);
74
75           if (!nontrivial)
76             {
77               if (chmod_or_fchmod (dst_name, dest_desc, mode) != 0)
78                 saved_errno = errno;
79               else
80                 return 0;
81             }
82           else
83             chmod_or_fchmod (dst_name, dest_desc, mode);
84         }
85       else
86         {
87           acl_free (acl);
88           chmod_or_fchmod (dst_name, dest_desc, mode);
89         }
90       errno = saved_errno;
91       return -1;
92     }
93   else
94     acl_free (acl);
95
96   if (!MODE_INSIDE_ACL || (mode & (S_ISUID | S_ISGID | S_ISVTX)))
97     {
98       /* We did not call chmod so far, and either the mode and the ACL are
99          separate or special bits are to be set which don't fit into ACLs.  */
100
101       if (chmod_or_fchmod (dst_name, dest_desc, mode) != 0)
102         return -1;
103     }
104
105   if (S_ISDIR (mode))
106     {
107       acl = acl_get_file (src_name, ACL_TYPE_DEFAULT);
108       if (acl == NULL)
109         return -2;
110
111       if (acl_set_file (dst_name, ACL_TYPE_DEFAULT, acl))
112         {
113           int saved_errno = errno;
114
115           acl_free (acl);
116           errno = saved_errno;
117           return -1;
118         }
119       else
120         acl_free (acl);
121     }
122   return 0;
123
124 #elif USE_ACL && defined ACL_NO_TRIVIAL
125   /* Solaris 10 NFSv4 ACLs.  */
126
127   acl_t *aclp = NULL;
128   ret = (source_desc < 0
129          ? acl_get (src_name, ACL_NO_TRIVIAL, &aclp)
130          : facl_get (source_desc, ACL_NO_TRIVIAL, &aclp));
131   if (ret != 0 && errno != ENOSYS)
132     return -2;
133
134   ret = qset_acl (dst_name, dest_desc, mode);
135   if (ret != 0)
136     return -1;
137
138   if (aclp)
139     {
140       ret = (dest_desc < 0
141              ? acl_set (dst_name, aclp)
142              : facl_set (dest_desc, aclp));
143       if (ret != 0)
144         {
145           int saved_errno = errno;
146
147           acl_free (aclp);
148           errno = saved_errno;
149           return -1;
150         }
151       acl_free (aclp);
152     }
153
154   return 0;
155
156 #else
157
158   ret = qset_acl (dst_name, dest_desc, mode);
159   return ret;
160
161 #endif
162 }
163
164
165 /* Copy access control lists from one file to another. If SOURCE_DESC is
166    a valid file descriptor, use file descriptor operations, else use
167    filename based operations on SRC_NAME. Likewise for DEST_DESC and
168    DST_NAME.
169    If access control lists are not available, fchmod the target file to
170    MODE.  Also sets the non-permission bits of the destination file
171    (S_ISUID, S_ISGID, S_ISVTX) to those from MODE if any are set.
172    Return 0 if successful, otherwise output a diagnostic and return -1.  */
173
174 int
175 copy_acl (const char *src_name, int source_desc, const char *dst_name,
176           int dest_desc, mode_t mode)
177 {
178   int ret = qcopy_acl (src_name, source_desc, dst_name, dest_desc, mode);
179   switch (ret)
180     {
181     case -2:
182       error (0, errno, "%s", quote (src_name));
183       return -1;
184
185     case -1:
186       error (0, errno, _("preserving permissions for %s"), quote (dst_name));
187       return -1;
188
189     default:
190       return 0;
191     }
192 }