1 /* copy-acl.c - copy access control list from one file to another file
3 Copyright (C) 2002-2003, 2005-2008 Free Software Foundation, Inc.
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.
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.
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/>.
18 Written by Paul Eggert and Andreas Gruenbacher. */
24 #include "acl-internal.h"
26 /* Copy access control lists from one file to another. If SOURCE_DESC is
27 a valid file descriptor, use file descriptor operations, else use
28 filename based operations on SRC_NAME. Likewise for DEST_DESC and
30 If access control lists are not available, fchmod the target file to
31 MODE. Also sets the non-permission bits of the destination file
32 (S_ISUID, S_ISGID, S_ISVTX) to those from MODE if any are set.
33 Return 0 if successful, otherwise output a diagnostic and return -1. */
36 copy_acl (const char *src_name, int source_desc, const char *dst_name,
37 int dest_desc, mode_t mode)
41 #if USE_ACL && HAVE_ACL_GET_FILE && HAVE_ACL_SET_FILE && HAVE_ACL_FREE
42 /* POSIX 1003.1e (draft 17 -- abandoned) specific version. */
43 /* Linux, FreeBSD, MacOS X, IRIX, Tru64 */
46 if (HAVE_ACL_GET_FD && source_desc != -1)
47 acl = acl_get_fd (source_desc);
49 acl = acl_get_file (src_name, ACL_TYPE_ACCESS);
52 if (ACL_NOT_WELL_SUPPORTED (errno))
53 return set_acl (dst_name, dest_desc, mode);
56 error (0, errno, "%s", quote (src_name));
61 if (HAVE_ACL_SET_FD && dest_desc != -1)
62 ret = acl_set_fd (dest_desc, acl);
64 ret = acl_set_file (dst_name, ACL_TYPE_ACCESS, acl);
67 int saved_errno = errno;
69 if (ACL_NOT_WELL_SUPPORTED (errno))
71 int n = acl_entries (acl);
74 /* On most hosts with MODE_INSIDE_ACL an ACL is trivial if n == 3,
75 and it cannot be less than 3. On IRIX 6.5 it is also trivial if
77 For simplicity and safety, assume the ACL is trivial if n <= 3.
78 Also see file-has-acl.c for some of the other possibilities;
79 it's not clear whether that complexity is needed here. */
80 if (n <= 3 * MODE_INSIDE_ACL)
82 if (chmod_or_fchmod (dst_name, dest_desc, mode) != 0)
88 chmod_or_fchmod (dst_name, dest_desc, mode);
93 chmod_or_fchmod (dst_name, dest_desc, mode);
95 error (0, saved_errno, _("preserving permissions for %s"),
102 if (!MODE_INSIDE_ACL || (mode & (S_ISUID | S_ISGID | S_ISVTX)))
104 /* We did not call chmod so far, and either the mode and the ACL are
105 separate or special bits are to be set which don't fit into ACLs. */
107 if (chmod_or_fchmod (dst_name, dest_desc, mode) != 0)
109 error (0, errno, _("preserving permissions for %s"),
117 acl = acl_get_file (src_name, ACL_TYPE_DEFAULT);
120 error (0, errno, "%s", quote (src_name));
124 if (acl_set_file (dst_name, ACL_TYPE_DEFAULT, acl))
126 error (0, errno, _("preserving permissions for %s"),
138 # if USE_ACL && defined ACL_NO_TRIVIAL
139 /* Solaris 10 NFSv4 ACLs. */
141 ret = (source_desc < 0
142 ? acl_get (src_name, ACL_NO_TRIVIAL, &aclp)
143 : facl_get (source_desc, ACL_NO_TRIVIAL, &aclp));
144 if (ret != 0 && errno != ENOSYS)
146 error (0, errno, "%s", quote (src_name));
151 ret = qset_acl (dst_name, dest_desc, mode);
153 error (0, errno, _("preserving permissions for %s"), quote (dst_name));
155 # if USE_ACL && defined ACL_NO_TRIVIAL
156 if (ret == 0 && aclp)
159 ? acl_set (dst_name, aclp)
160 : facl_set (dest_desc, aclp));
162 error (0, errno, _("preserving permissions for %s"), quote (dst_name));