Commit eb6a8a4dfb ("qcopy-acl: Optimize copying of ACLs by directly
copying the attributes.") caused qcopy_acl() not to override the ACL of
the destination file if the source file doesn't have an ACL, and
likewise for the default ACL.

* lib/qcopy-acl.c (is_attr_permissions): Remember the POSIX ACL
attributes that were copied in struct copy_context.
(qcopy_acl): Clarify that the source and destination files are supposed
to have the same file type and that the non-permission bits of the
destination file are always set.  When POSIX ACLs are not copied as
xattrs, initialize the POSIX ACLs of the destination file based on the
file mode.
---
 lib/qcopy-acl.c | 65 +++++++++++++++++++++++++++++++++++++++++--------
 1 file changed, 55 insertions(+), 10 deletions(-)

diff --git a/lib/qcopy-acl.c b/lib/qcopy-acl.c
index e59b088b77..07cb052534 100644
--- a/lib/qcopy-acl.c
+++ b/lib/qcopy-acl.c
@@ -18,6 +18,7 @@
    Written by Paul Eggert, Andreas Grünbacher, and Bruno Haible.  */
 
 #include <config.h>
+#include <fcntl.h>
 
 #include "acl.h"
 
@@ -26,6 +27,7 @@
 #if USE_XATTR
 
 # include <attr/libattr.h>
+# include <attr/error_context.h>
 # include <dirent.h>
 # include <string.h>
 
@@ -42,30 +44,55 @@
 #  define XATTR_NAME_POSIX_ACL_DEFAULT "system.posix_acl_default"
 # endif
 
+#ifndef HAVE_ACL_DELETE_DEF_FILE_AT
+# define HAVE_ACL_DELETE_DEF_FILE_AT false
+# define acl_delete_def_file_at(dirfd, path_p, at_flags) (-1)
+#endif
+
+struct copy_context
+  {
+    struct error_context ctx;
+    bool posix_acl_access;
+    bool posix_acl_default;
+  };
+
 /* Returns 1 if NAME is the name of an extended attribute that is related
    to permissions, i.e. ACLs.  Returns 0 otherwise.  */
 
 static int
 is_attr_permissions (const char *name, struct error_context *ctx)
 {
+  struct copy_context *copy_ctx =
+    (struct copy_context *) (void *)
+    ((char *)ctx - offsetof (struct copy_context, ctx));
+
   /* We need to explicitly test for the known extended attribute names,
      because at least on CentOS 7, attr_copy_action does not do it.  */
-  return streq (name, XATTR_NAME_POSIX_ACL_ACCESS)
-         || streq (name, XATTR_NAME_POSIX_ACL_DEFAULT)
-         || streq (name, XATTR_NAME_NFSV4_ACL)
+  if (streq (name, XATTR_NAME_POSIX_ACL_ACCESS))
+    {
+      copy_ctx->posix_acl_access = true;
+      return true;
+    }
+  if (streq (name, XATTR_NAME_POSIX_ACL_DEFAULT))
+    {
+      copy_ctx->posix_acl_default = true;
+      return true;
+    }
+  return streq (name, XATTR_NAME_NFSV4_ACL)
          || attr_copy_action (name, ctx) == ATTR_ACTION_PERMISSIONS;
 }
 
 #endif  /* USE_XATTR */
 
-/* Copy access control lists from one file to another. If SOURCE_DESC is
-   a valid file descriptor, use file descriptor operations, else use
-   filename based operations on SRC_NAME. Likewise for DEST_DESC and
-   DST_NAME.
+/* Copy access control lists from one file to another.
+   Assumes that the destination file has the same file type as the source file.
+   If SOURCE_DESC is a valid file descriptor, use file descriptor
+   operations, else use filename based operations on SRC_NAME.
+   Likewise for DEST_DESC and DST_NAME.
    MODE should be the source file's st_mode.
    If access control lists are not available, fchmod the target file to
    MODE.  Also sets the non-permission bits of the destination file
-   (S_ISUID, S_ISGID, S_ISVTX) to those from MODE if any are set.
+   (S_ISUID, S_ISGID, S_ISVTX) to those from MODE.
    Return 0 if successful.
    Return -2 and set errno for an error relating to the source file.
    Return -1 and set errno for an error relating to the destination file.  */
@@ -89,10 +116,28 @@ qcopy_acl (const char *src_name, int source_desc, const 
char *dst_name,
      to copy */
   if (ret == 0)
     {
+      struct copy_context copied = {};
+
       ret = source_desc < 0 || dest_desc < 0
-        ? attr_copy_file (src_name, dst_name, is_attr_permissions, NULL)
+        ? attr_copy_file (src_name, dst_name, is_attr_permissions, &copied.ctx)
         : attr_copy_fd (src_name, source_desc, dst_name, dest_desc,
-                        is_attr_permissions, NULL);
+                        is_attr_permissions, &copied.ctx);
+
+      if (ret == 0 && !copied.posix_acl_access)
+        {
+         acl_t acl = acl_from_mode (mode);
+         ret = dest_desc < 0
+           ? acl_set_file (dst_name, ACL_TYPE_ACCESS, acl)
+           : acl_set_fd (dest_desc, acl);
+         acl_free (acl);
+       }
+
+      if (ret == 0 && S_ISDIR (mode) && !copied.posix_acl_default)
+       {
+         ret = dest_desc < 0 || ! HAVE_ACL_DELETE_DEF_FILE_AT
+          ? acl_delete_def_file (dst_name)
+          : acl_delete_def_file_at (dest_desc, "", AT_EMPTY_PATH);
+       }
 
       /* Copying can fail with EOPNOTSUPP even when the source
          permissions are trivial (Bug#78328).  Don't report an error
-- 
2.56.1


Reply via email to