- Made the "types" array a single static instead of a separate
[rsync/rsync-patches.git] / acls.diff
index 9f04d64..31ab54f 100644 (file)
--- a/acls.diff
+++ b/acls.diff
@@ -32,7 +32,7 @@ ACLs to a non-ACL-supporting disk should complain.
  popt_OBJS=popt/findme.o  popt/popt.o  popt/poptconfig.o \
 --- old/acls.c
 +++ new/acls.c
-@@ -0,0 +1,1202 @@
+@@ -0,0 +1,1230 @@
 +/* -*- c-file-style: "linux" -*-
 +   Copyright (C) Andrew Tridgell 1996
 +   Copyright (C) Paul Mackerras 1996
@@ -77,6 +77,7 @@ ACLs to a non-ACL-supporting disk should complain.
 +} rsync_acl;
 +
 +static const rsync_acl rsync_acl_initializer = { 0, 0, NULL };
++static SMB_ACL_TYPE_T types[] = {SMB_ACL_TYPE_ACCESS, SMB_ACL_TYPE_DEFAULT};
 +
 +static void expand_rsync_acl(rsync_acl *racl)
 +{
@@ -372,14 +373,31 @@ ACLs to a non-ACL-supporting disk should complain.
 +              "unknown SMB_ACL_TYPE_T";
 +}
 +
++/*
++ * Overwrite racl with a new three-entry ACL from the given permissions.
++ */
++static void perms_to_acl(int perms, rsync_acl *racl)
++{
++      racl->count = 0;
++      expand_rsync_acl(racl);
++      racl->races[racl->count].tag_type = SMB_ACL_USER_OBJ;
++      racl->races[racl->count++].access = (perms >> 6) & 7;
++      expand_rsync_acl(racl);
++      racl->races[racl->count].tag_type = SMB_ACL_GROUP_OBJ;
++      racl->races[racl->count++].access = (perms >> 3) & 7;
++      expand_rsync_acl(racl);
++      racl->races[racl->count].tag_type = SMB_ACL_OTHER;
++      racl->races[racl->count++].access = (perms >> 0) & 7;
++}
++
 +/* Generate the ACL(s) for this flist entry;
 + * ACL(s) are either sent or cleaned-up by send_acl() below. */
 +
 +int make_acl(const struct file_struct *file, const char *fname)
 +{
-+      SMB_ACL_TYPE_T *type,
-+              types[] = {SMB_ACL_TYPE_ACCESS, SMB_ACL_TYPE_DEFAULT};
++      SMB_ACL_TYPE_T *type;
 +      rsync_acl *curr_racl;
++
 +      if (!preserve_acls || S_ISLNK(file->mode))
 +              return 1;
 +      for (type = &types[0], curr_racl = &_curr_rsync_acls[0];
@@ -389,15 +407,21 @@ ACLs to a non-ACL-supporting disk should complain.
 +              SMB_ACL_T sacl;
 +              BOOL ok;
 +              *curr_racl = rsync_acl_initializer;
-+              if (!(sacl = sys_acl_get_file(fname, *type))) {
++              if ((sacl = sys_acl_get_file(fname, *type)) != 0) {
++                      ok = unpack_smb_acl(curr_racl, sacl);
++                      sys_acl_free_acl(sacl);
++                      if (!ok)
++                              return -1;
++              } else if (errno == ENOTSUP) {
++                      /* ACLs are not supported.  Invent an access ACL from
++                       * permissions; let the default ACL default to empty. */
++                      if (*type == SMB_ACL_TYPE_ACCESS)
++                              perms_to_acl(file->mode & ACCESSPERMS, curr_racl);
++              } else {
 +                      rprintf(FERROR, "send_acl: sys_acl_get_file(%s, %s): %s\n",
 +                              fname, str_acl_type(*type), strerror(errno));
 +                      return -1;
 +              }
-+              ok = unpack_smb_acl(curr_racl, sacl);
-+              sys_acl_free_acl(sacl);
-+              if (!ok)
-+                      return -1;
 +      }
 +      return 0;
 +}
@@ -407,9 +431,9 @@ ACLs to a non-ACL-supporting disk should complain.
 +
 +void send_acl(const struct file_struct *file, int f)
 +{
-+      SMB_ACL_TYPE_T *type,
-+              types[] = {SMB_ACL_TYPE_ACCESS, SMB_ACL_TYPE_DEFAULT};
++      SMB_ACL_TYPE_T *type;
 +      rsync_acl *curr_racl;
++
 +      if (!preserve_acls || S_ISLNK(file->mode))
 +              return;
 +      for (type = &types[0], curr_racl = &_curr_rsync_acls[0];
@@ -754,11 +778,12 @@ ACLs to a non-ACL-supporting disk should complain.
 +
 +void receive_acl(struct file_struct *file, int f)
 +{
-+      SMB_ACL_TYPE_T *type,
-+              types[] = {SMB_ACL_TYPE_ACCESS, SMB_ACL_TYPE_DEFAULT};
++      SMB_ACL_TYPE_T *type;
 +      char *fname;
++
 +      if (!preserve_acls || S_ISLNK(file->mode))
 +              return;
++
 +      fname = f_name(file, NULL);
 +      for (type = &types[0];
 +           type < &types[0] + sizeof types / sizeof types[0]
@@ -828,14 +853,14 @@ ACLs to a non-ACL-supporting disk should complain.
 +
 +void sort_file_acl_index_lists()
 +{
-+      SMB_ACL_TYPE_T *type,
-+              types[] = {SMB_ACL_TYPE_ACCESS, SMB_ACL_TYPE_DEFAULT};
++      SMB_ACL_TYPE_T *type;
++
 +      if (!preserve_acls)
 +              return;
++
 +      for (type = &types[0];
 +           type < &types[0] + sizeof types / sizeof types[0];
-+           type++)
-+      {
++           type++) {
 +              file_acl_index_list *fileaclidx_list =
 +                      file_acl_index_lists(*type);
 +              if (!fileaclidx_list->count)
@@ -877,11 +902,12 @@ ACLs to a non-ACL-supporting disk should complain.
 +
 +int dup_acl(const char *orig, const char *bak, mode_t mode)
 +{
-+      SMB_ACL_TYPE_T *type,
-+              types[] = {SMB_ACL_TYPE_ACCESS, SMB_ACL_TYPE_DEFAULT};
++      SMB_ACL_TYPE_T *type;
 +      int ret = 0;
++
 +      if (!preserve_acls)
 +              return 1;
++
 +      for (type = &types[0];
 +           type < &types[0] + sizeof types / sizeof types[0]
 +               && (*type == SMB_ACL_TYPE_ACCESS || S_ISDIR(mode));
@@ -915,12 +941,12 @@ ACLs to a non-ACL-supporting disk should complain.
 +                      ; /* presume they're unequal */
 +              }
 +              if (*type == SMB_ACL_TYPE_DEFAULT && !racl_orig.count) {
-+                      if (-1 == sys_acl_delete_def_file(bak)) {
++                      if (sys_acl_delete_def_file(bak) < 0) {
 +                              rprintf(FERROR, "dup_acl: sys_acl_delete_def_file(%s): %s\n",
 +                                      bak, strerror(errno));
 +                              ret = -1;
 +                      }
-+              } else if (-1 == sys_acl_set_file(bak, *type, sacl_bak)) {
++              } else if (sys_acl_set_file(bak, *type, sacl_bak) < 0) {
 +                      rprintf(FERROR, "dup_acl: sys_acl_set_file(%s, %s): %s\n",
 +                              bak, str_acl_type(*type), strerror(errno));
 +                      ret = -1;
@@ -952,12 +978,13 @@ ACLs to a non-ACL-supporting disk should complain.
 +                        const char *orig, const char *dest)
 +{
 +      if (preserve_acls) {
-+              SMB_ACL_TYPE_T *type,
-+                      types[] = {SMB_ACL_TYPE_ACCESS, SMB_ACL_TYPE_DEFAULT};
++              SMB_ACL_TYPE_T *type;
 +              SMB_ACL_T *sacl;
++
 +              backup_orig_file = file;
 +              backup_orig_fname = orig;
 +              backup_dest_fname = dest;
++
 +              for (type = &types[0], sacl = &_backup_sacl[0];
 +                   type < &types[0] + sizeof types / sizeof types[0];
 +                   type++) {
@@ -977,17 +1004,16 @@ ACLs to a non-ACL-supporting disk should complain.
 +static int set_keep_backup_acl()
 +{
 +      if (preserve_acls) {
-+              SMB_ACL_TYPE_T *type,
-+                      types[] = {SMB_ACL_TYPE_ACCESS, SMB_ACL_TYPE_DEFAULT};
++              SMB_ACL_TYPE_T *type;
 +              SMB_ACL_T *sacl;
 +              int ret = 0;
++
 +              for (type = &types[0], sacl = &_backup_sacl[0];
 +                   type < &types[0] + sizeof types / sizeof types[0];
 +                   type++) {
 +                      if (*sacl) {
-+                              if (-1 == sys_acl_set_file(backup_dest_fname,
-+                                                         *type, *sacl))
-+                              {
++                              if (sys_acl_set_file(backup_dest_fname,
++                                                   *type, *sacl) < 0) {
 +                                      rprintf(FERROR, "push_keep_backup_acl: sys_acl_get_file(%s, %s): %s\n",
 +                                              backup_dest_fname,
 +                                              str_acl_type(*type),
@@ -1004,12 +1030,13 @@ ACLs to a non-ACL-supporting disk should complain.
 +void cleanup_keep_backup_acl()
 +{
 +      if (preserve_acls) {
-+              SMB_ACL_TYPE_T *type,
-+                      types[] = {SMB_ACL_TYPE_ACCESS, SMB_ACL_TYPE_DEFAULT};
++              SMB_ACL_TYPE_T *type;
 +              SMB_ACL_T *sacl;
++
 +              backup_orig_file = NULL;
 +              backup_orig_fname = null_string;
 +              backup_dest_fname = null_string;
++
 +              for (type = &types[0], sacl = &_backup_sacl[0];
 +                   type < &types[0] + sizeof types / sizeof types[0];
 +                   type++) {
@@ -1025,10 +1052,11 @@ ACLs to a non-ACL-supporting disk should complain.
 +int set_acl(const char *fname, const struct file_struct *file)
 +{
 +      int unchanged = 1;
-+      SMB_ACL_TYPE_T *type,
-+              types[] = {SMB_ACL_TYPE_ACCESS, SMB_ACL_TYPE_DEFAULT};
++      SMB_ACL_TYPE_T *type;
++
 +      if (dry_run || !preserve_acls || S_ISLNK(file->mode))
 +              return 1;
++
 +      if (file == backup_orig_file) {
 +              if (!strcmp(fname, backup_dest_fname))
 +                      return set_keep_backup_acl();
@@ -1062,7 +1090,7 @@ ACLs to a non-ACL-supporting disk should complain.
 +              if (ok)
 +                      continue;
 +              if (*type == SMB_ACL_TYPE_DEFAULT && !racl_new->count) {
-+                      if (-1 == sys_acl_delete_def_file(fname)) {
++                      if (sys_acl_delete_def_file(fname) < 0) {
 +                              rprintf(FERROR, "set_acl: sys_acl_delete_def_file(%s): %s\n",
 +                                      fname, strerror(errno));
 +                              unchanged = -1;
@@ -1074,7 +1102,7 @@ ACLs to a non-ACL-supporting disk should complain.
 +                                      unchanged = -1;
 +                                      continue;
 +                              }
-+                      if (-1 == sys_acl_set_file(fname, *type, *sacl_new)) {
++                      if (sys_acl_set_file(fname, *type, *sacl_new) < 0) {
 +                              rprintf(FERROR, "set_acl: sys_acl_set_file(%s, %s): %s\n",
 +                                      fname, str_acl_type(*type),
 +                                      strerror(errno));
@@ -1237,28 +1265,34 @@ ACLs to a non-ACL-supporting disk should complain.
 +#endif /* SUPPORT_ACLS */
 --- old/backup.c
 +++ new/backup.c
-@@ -132,6 +132,7 @@ static int make_bak_dir(char *fullpath)
+@@ -132,6 +132,9 @@ static int make_bak_dir(char *fullpath)
                        } else {
                                do_lchown(fullpath, st.st_uid, st.st_gid);
                                do_chmod(fullpath, st.st_mode);
-+                              (void)DUP_ACL(end, fullpath, st.st_mode);
++#ifdef SUPPORT_ACLS
++                              dup_acl(end, fullpath, st.st_mode);
++#endif
                        }
                }
                *p = '/';
-@@ -185,6 +186,8 @@ static int keep_backup(char *fname)
+@@ -185,6 +188,10 @@ static int keep_backup(char *fname)
        if (!(buf = get_backup_name(fname)))
                return 0;
  
-+      PUSH_KEEP_BACKUP_ACL(file, fname, buf);
++#ifdef SUPPORT_ACLS
++      push_keep_backup_acl(file, fname, buf);
++#endif
 +
        /* Check to see if this is a device file, or link */
        if ((am_root && preserve_devices && IS_DEVICE(file->mode))
         || (preserve_specials && IS_SPECIAL(file->mode))) {
-@@ -260,6 +263,7 @@ static int keep_backup(char *fname)
+@@ -260,6 +267,9 @@ static int keep_backup(char *fname)
                }
        }
        set_file_attrs(buf, file, NULL, 0);
-+      CLEANUP_KEEP_BACKUP_ACL();
++#ifdef SUPPORT_ACLS
++      cleanup_keep_backup_acl();
++#endif
        free(file);
  
        if (verbose > 1) {
@@ -1356,40 +1390,48 @@ ACLs to a non-ACL-supporting disk should complain.
  
 --- old/flist.c
 +++ new/flist.c
-@@ -966,6 +966,8 @@ static struct file_struct *send_file_nam
+@@ -966,6 +966,10 @@ static struct file_struct *send_file_nam
                         f == -2 ? SERVER_FILTERS : ALL_FILTERS);
        if (!file)
                return NULL;
-+      if (MAKE_ACL(file, fname) < 0)
++#ifdef SUPPORT_ACLS
++      if (make_acl(file, fname) < 0)
 +              return NULL;
++#endif
  
        if (chmod_modes && !S_ISLNK(file->mode))
                file->mode = tweak_mode(file->mode, chmod_modes);
-@@ -977,6 +979,10 @@ static struct file_struct *send_file_nam
+@@ -977,6 +981,12 @@ static struct file_struct *send_file_nam
        if (file->basename[0]) {
                flist->files[flist->count++] = file;
                send_file_entry(file, f);
-+              SEND_ACL(file, f);
++#ifdef SUPPORT_ACLS
++              send_acl(file, f);
 +      } else {
 +              /* Cleanup unsent ACL(s). */
-+              SEND_ACL(file, -1);
++              send_acl(file, -1);
++#endif
        }
        return file;
  }
-@@ -1365,6 +1371,8 @@ struct file_list *recv_file_list(int f)
+@@ -1365,6 +1375,10 @@ struct file_list *recv_file_list(int f)
                        flags |= read_byte(f) << 8;
                file = receive_file_entry(flist, flags, f);
  
-+              RECEIVE_ACL(file, f);
++#ifdef SUPPORT_ACLS
++              receive_acl(file, f);
++#endif
 +
                if (S_ISREG(file->mode) || S_ISLNK(file->mode))
                        stats.total_size += file->length;
  
-@@ -1387,6 +1395,8 @@ struct file_list *recv_file_list(int f)
+@@ -1387,6 +1401,10 @@ struct file_list *recv_file_list(int f)
  
        clean_flist(flist, relative_paths, 1);
  
-+      SORT_FILE_ACL_INDEX_LISTS();
++#ifdef SUPPORT_ACLS
++      sort_file_acl_index_lists();
++#endif
 +
        if (f >= 0) {
                recv_uid_list(f, flist);
@@ -4885,21 +4927,23 @@ ACLs to a non-ACL-supporting disk should complain.
        if (daemon_chmod_modes && !S_ISLNK(flist_mode))
                cur_mode = tweak_mode(cur_mode, daemon_chmod_modes);
        return (flist_mode & ~CHMOD_BITS) | (cur_mode & CHMOD_BITS);
-@@ -217,6 +218,11 @@ int set_file_attrs(char *fname, struct f
+@@ -203,6 +204,13 @@ int set_file_attrs(char *fname, struct f
+               updated = 1;
        }
- #endif
  
-+      /* It's fine to call SET_ACL now; the generator will enable
-+       * writability on the directory using chmod if necessary. */
-+      if (SET_ACL(fname, file) == 0)
++#ifdef SUPPORT_ACLS
++      /* It's OK to call set_acl() now, even for a dir, as the generator
++       * will enable owner-writability using chmod, if necessary. */
++      if (set_acl(fname, file) == 0)
 +              updated = 1;
++#endif
 +
-       if (verbose > 1 && flags & ATTRS_REPORT) {
-               enum logcode code = daemon_log_format_has_i || dry_run
-                                 ? FCLIENT : FINFO;
+ #ifdef HAVE_CHMOD
+       if ((st->st_mode & CHMOD_BITS) != (file->mode & CHMOD_BITS)) {
+               int ret = do_chmod(fname, file->mode);
 --- old/rsync.h
 +++ new/rsync.h
-@@ -658,6 +658,44 @@ struct chmod_mode_struct;
+@@ -658,6 +658,20 @@ struct chmod_mode_struct;
  
  #define UNUSED(x) x __attribute__((__unused__))
  
@@ -4912,33 +4956,9 @@ ACLs to a non-ACL-supporting disk should complain.
 +#define ACLS_NEED_MASK 1
 +#endif
 +
-+#ifdef SUPPORT_ACLS
-+#ifdef HAVE_SYS_ACL_H
++#if defined SUPPORT_ACLS && defined HAVE_SYS_ACL_H
 +#include <sys/acl.h>
 +#endif
-+#define MAKE_ACL(file, fname)                 make_acl(file, fname)
-+#define SEND_ACL(file, f)                     send_acl(file, f)
-+#define RECEIVE_ACL(file, f)                  receive_acl(file, f)
-+#define SORT_FILE_ACL_INDEX_LISTS()           sort_file_acl_index_lists()
-+#define SET_ACL(fname, file)                  set_acl(fname, file)
-+#define NEXT_ACL_UID()                                next_acl_uid()
-+#define ACL_UID_MAP(uid)                      acl_uid_map(uid)
-+#define PUSH_KEEP_BACKUP_ACL(file, orig, dest) \
-+                                      push_keep_backup_acl(file, orig, dest)
-+#define CLEANUP_KEEP_BACKUP_ACL()             cleanup_keep_backup_acl()
-+#define DUP_ACL(orig, dest, mode)             dup_acl(orig, dest, mode)
-+#else /* SUPPORT_ACLS */
-+#define MAKE_ACL(file, fname)                 1 /* checked return value */
-+#define SEND_ACL(file, f)
-+#define RECEIVE_ACL(file, f)
-+#define SORT_FILE_ACL_INDEX_LISTS()
-+#define SET_ACL(fname, file)                  1 /* checked return value */
-+#define NEXT_ACL_UID()        
-+#define ACL_UID_MAP(uid)
-+#define PUSH_KEEP_BACKUP_ACL(file, orig, dest)
-+#define CLEANUP_KEEP_BACKUP_ACL()
-+#define DUP_ACL(src, orig, mode)              1 /* checked return value */
-+#endif /* SUPPORT_ACLS */
 +#include "smb_acls.h"
 +
  #include "proto.h"
@@ -5018,7 +5038,7 @@ ACLs to a non-ACL-supporting disk should complain.
 +#ifndef _SMB_ACLS_H
 +#define _SMB_ACLS_H
 +
-+#if defined(HAVE_POSIX_ACLS)
++#if defined HAVE_POSIX_ACLS
 +
 +/* This is an identity mapping (just remove the SMB_). */
 +
@@ -5048,7 +5068,7 @@ ACLs to a non-ACL-supporting disk should complain.
 +#define SMB_ACL_TYPE_ACCESS   ACL_TYPE_ACCESS
 +#define SMB_ACL_TYPE_DEFAULT  ACL_TYPE_DEFAULT
 +
-+#elif defined(HAVE_TRU64_ACLS)
++#elif defined HAVE_TRU64_ACLS
 +
 +/* This is for DEC/Compaq Tru64 UNIX */
 +
@@ -5078,7 +5098,7 @@ ACLs to a non-ACL-supporting disk should complain.
 +#define SMB_ACL_TYPE_ACCESS   ACL_TYPE_ACCESS
 +#define SMB_ACL_TYPE_DEFAULT  ACL_TYPE_DEFAULT
 +
-+#elif defined(HAVE_UNIXWARE_ACLS) || defined(HAVE_SOLARIS_ACLS)
++#elif defined HAVE_UNIXWARE_ACLS || defined HAVE_SOLARIS_ACLS
 +/*
 + * Donated by Michael Davidson <md@sco.COM> for UnixWare / OpenUNIX.
 + * Modified by Toomas Soome <tsoome@ut.ee> for Solaris.
@@ -5116,7 +5136,7 @@ ACLs to a non-ACL-supporting disk should complain.
 +#define SMB_ACL_TYPE_ACCESS   0
 +#define SMB_ACL_TYPE_DEFAULT  1
 +
-+#elif defined(HAVE_HPUX_ACLS)
++#elif defined HAVE_HPUX_ACLS
 +
 +/*
 + * Based on the Solaris & UnixWare code.
@@ -5157,7 +5177,7 @@ ACLs to a non-ACL-supporting disk should complain.
 +#define SMB_ACL_TYPE_ACCESS   0
 +#define SMB_ACL_TYPE_DEFAULT  1
 +
-+#elif defined(HAVE_IRIX_ACLS)
++#elif defined HAVE_IRIX_ACLS
 +
 +#define SMB_ACL_TAG_T         acl_tag_t
 +#define SMB_ACL_TYPE_T                acl_type_t
@@ -5189,7 +5209,7 @@ ACLs to a non-ACL-supporting disk should complain.
 +#define SMB_ACL_TYPE_ACCESS   ACL_TYPE_ACCESS
 +#define SMB_ACL_TYPE_DEFAULT  ACL_TYPE_DEFAULT
 +
-+#elif defined(HAVE_AIX_ACLS)
++#elif defined HAVE_AIX_ACLS
 +
 +/* Donated by Medha Date, mdate@austin.ibm.com, for IBM */
 +