Fixed an ACL/xattr corruption issue where the --backup option could cause
[rsync/rsync.git] / acls.c
diff --git a/acls.c b/acls.c
index d9023e2..105b985 100644 (file)
--- a/acls.c
+++ b/acls.c
@@ -3,7 +3,7 @@
  *
  * Copyright (C) 1996 Andrew Tridgell
  * Copyright (C) 1996 Paul Mackerras
- * Copyright (C) 2006-2008 Wayne Davison
+ * Copyright (C) 2006-2009 Wayne Davison
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -88,6 +88,9 @@ static const rsync_acl empty_rsync_acl = {
 static item_list access_acl_list = EMPTY_ITEM_LIST;
 static item_list default_acl_list = EMPTY_ITEM_LIST;
 
+static size_t prior_access_count = (size_t)-1;
+static size_t prior_default_count = (size_t)-1;
+
 /* === Calculations on ACL types === */
 
 static const char *str_acl_type(SMB_ACL_TYPE_T type)
@@ -137,7 +140,8 @@ static void rsync_acl_strip_perms(rsync_acl *racl)
        else {
                if (racl->group_obj == racl->mask_obj)
                        racl->group_obj = NO_ENTRY;
-               racl->mask_obj = NO_ENTRY;
+               if (racl->names.count != 0)
+                       racl->mask_obj = NO_ENTRY;
        }
        racl->other_obj = NO_ENTRY;
 }
@@ -749,14 +753,7 @@ static int recv_rsync_acl(item_list *racl_list, SMB_ACL_TYPE_T type, int f)
        /* If we received a superfluous mask, throw it away. */
        duo_item->racl.mask_obj = NO_ENTRY;
 #else
-       if (!duo_item->racl.names.count) {
-               /* If we received a superfluous mask, throw it away. */
-               if (duo_item->racl.mask_obj != NO_ENTRY) {
-                       /* Mask off the group perms with it first. */
-                       duo_item->racl.group_obj &= duo_item->racl.mask_obj | NO_ENTRY;
-                       duo_item->racl.mask_obj = NO_ENTRY;
-               }
-       } else if (duo_item->racl.mask_obj == NO_ENTRY) /* Must be non-empty with lists. */
+       if (duo_item->racl.names.count && duo_item->racl.mask_obj == NO_ENTRY) /* Must be non-empty with lists. */
                duo_item->racl.mask_obj = (computed_mask_bits | duo_item->racl.group_obj) & ~NO_ENTRY;
 #endif
 
@@ -794,17 +791,50 @@ static int cache_rsync_acl(rsync_acl *racl, SMB_ACL_TYPE_T type, item_list *racl
 
 /* Turn the ACL data in stat_x into cached ACL data, setting the index
  * values in the file struct. */
-void cache_acl(struct file_struct *file, stat_x *sxp)
+void cache_tmp_acl(struct file_struct *file, stat_x *sxp)
 {
+       if (prior_access_count == (size_t)-1)
+               prior_access_count = access_acl_list.count;
+
        F_ACL(file) = cache_rsync_acl(sxp->acc_acl,
                                      SMB_ACL_TYPE_ACCESS, &access_acl_list);
 
        if (S_ISDIR(sxp->st.st_mode)) {
+               if (prior_default_count == (size_t)-1)
+                       prior_default_count = default_acl_list.count;
                F_DIR_DEFACL(file) = cache_rsync_acl(sxp->def_acl,
                                      SMB_ACL_TYPE_DEFAULT, &default_acl_list);
        }
 }
 
+static void uncache_duo_acls(item_list *duo_list, size_t start)
+{
+       acl_duo *duo_item = duo_list->items;
+       acl_duo *duo_start = duo_item + start;
+
+       duo_item += duo_list->count;
+       duo_list->count = start;
+
+       while (duo_item-- > duo_start) {
+               rsync_acl_free(&duo_item->racl);
+               if (duo_item->sacl)
+                       sys_acl_free_acl(duo_item->sacl);
+       }
+}
+
+void uncache_tmp_acls(void)
+{
+       if (prior_access_count != (size_t)-1) {
+               uncache_duo_acls(&access_acl_list, prior_access_count);
+               prior_access_count = (size_t)-1;
+       }
+
+       if (prior_default_count != (size_t)-1) {
+               uncache_duo_acls(&default_acl_list, prior_default_count);
+               prior_default_count = (size_t)-1;
+       }
+}
+
 #ifndef HAVE_OSX_ACLS
 static mode_t change_sacl_perms(SMB_ACL_T sacl, rsync_acl *racl, mode_t old_mode, mode_t mode)
 {
@@ -1081,7 +1111,7 @@ int default_perms_for_dir(const char *dir)
        /* Apply the permission-bit entries of the default ACL, if any. */
        if (racl.user_obj != NO_ENTRY) {
                perms = rsync_acl_get_perms(&racl);
-               if (verbose > 2)
+               if (DEBUG_GTE(ACL, 1))
                        rprintf(FINFO, "got ACL-based default perms %o for directory %s\n", perms, dir);
        }