Fixed a compile problem in the non-Linux code.
[rsync/rsync-patches.git] / xattrs.diff
index c8030e3..4a23f45 100644 (file)
@@ -1,14 +1,22 @@
-Depends-On-Patch: acls.diff
+This patch adds basic support for extended attributes.  It works, but there
+are some things that still needs to be improved (see TODO list below).
 
-After applying this patch, run these commands for a successful build:
+To use this patch, run these commands for a successful build:
 
+    patch -p1 <patches/acls.diff
+    patch -p1 <patches/xattrs.diff
     ./prepare-source
     ./configure --enable-acl-support --enable-xattr-support
     make
 
+Alternately, if you don't want ACL support, configure it this way:
+
+    ./configure --enable-xattr-support
+
 TODO:
 
- - This patch needs to be rewritten to more efficiently handle large xattrs.
+ - This patch needs to more efficiently handle large xattrs, especially when
+   they're unchanged.
 
  - Extraneous xattr values need to be removed from files that are not being
    recreated.
@@ -38,7 +46,7 @@ TODO:
  popt_OBJS=popt/findme.o  popt/popt.o  popt/poptconfig.o \
 --- old/acls.c
 +++ new/acls.c
-@@ -31,6 +31,7 @@ extern int read_only;
+@@ -30,6 +30,7 @@ extern int read_only;
  extern int list_only;
  extern int orig_umask;
  extern int preserve_acls;
@@ -46,7 +54,7 @@ TODO:
  extern unsigned int file_struct_len;
  
  /* === ACL structures === */
-@@ -742,6 +743,10 @@ void receive_acl(struct file_struct *fil
+@@ -741,6 +742,10 @@ void receive_acl(struct file_struct *fil
        type = SMB_ACL_TYPE_ACCESS;
        racl_list = &access_acl_list;
        ndx_ptr = (char*)file + file_struct_len;
@@ -57,7 +65,7 @@ TODO:
        do {
                char tag = read_byte(f);
                int ndx;
-@@ -801,6 +806,10 @@ void cache_acl(struct file_struct *file,
+@@ -800,6 +805,10 @@ void cache_acl(struct file_struct *file,
        racl = sxp->acc_acl;
        racl_list = &access_acl_list;
        ndx_ptr = (char*)file + file_struct_len;
@@ -68,7 +76,7 @@ TODO:
        do {
                if (!racl)
                        ndx = -1;
-@@ -921,6 +930,10 @@ int set_acl(const char *fname, const str
+@@ -920,6 +929,10 @@ int set_acl(const char *fname, const str
  
        type = SMB_ACL_TYPE_ACCESS;
        ndx_ptr = (char*)file + file_struct_len;
@@ -137,12 +145,13 @@ TODO:
        if ((am_root && preserve_devices && IS_DEVICE(file->mode))
 --- old/configure.in
 +++ new/configure.in
-@@ -856,6 +856,35 @@ samba_cv_HAVE_ACL_GET_PERM_NP=yes,samba_
+@@ -856,6 +856,40 @@ samba_cv_HAVE_ACL_GET_PERM_NP=yes,samba_
    AC_MSG_RESULT(no)
  )
  
 +AC_CHECK_HEADERS(attr/xattr.h)
 +AC_CHECK_HEADERS(sys/xattr.h)
++AC_CHECK_HEADERS(sys/extattr.h)
 +AC_MSG_CHECKING(whether to support extended attributes)
 +AC_ARG_ENABLE(xattr-support,
 +AC_HELP_STRING([--enable-xattr-support], [Include extended attribute support (default=no)]),
@@ -157,6 +166,10 @@ TODO:
 +            AC_MSG_RESULT(Using OS X xattrs)
 +            AC_DEFINE(HAVE_OSX_XATTRS, 1, [True if you have Mac OS X xattrs])
 +            ;;
++      freebsd*)
++            AC_MSG_RESULT(Using FreeBSD extattrs)
++            AC_DEFINE(HAVE_FREEBSD_XATTRS, 1, [True if you have FreeBSD xattrs])
++            ;;
 +      *)
 +            AC_MSG_RESULT(Xattrs requested but not Linux or OS X.  Good luck...)
 +            ;;
@@ -164,10 +177,10 @@ TODO:
 +      ;;
 +  *)
 +      AC_MSG_RESULT(no)
-+      AC_DEFINE(HAVE_NA_XATTRS, 1, [True if you don't have extended attributes])
++      AC_DEFINE(HAVE_NO_XATTRS, 1, [True if you don't have extended attributes])
 +  esac ],
 +  AC_MSG_RESULT(no)
-+  AC_DEFINE(HAVE_NO_XATTRL, 1, [True if you don't have extended attributes])
++  AC_DEFINE(HAVE_NO_XATTRS, 1, [True if you don't have extended attributes])
 +)
 +
  AC_CONFIG_FILES([Makefile lib/dummy zlib/dummy popt/dummy shconfig])
@@ -275,7 +288,7 @@ TODO:
  }
 --- old/lib/sysxattr.c
 +++ new/lib/sysxattr.c
-@@ -0,0 +1,81 @@
+@@ -0,0 +1,136 @@
 +/*
 + * Extended attribute support for rsync.
 + *
@@ -300,6 +313,8 @@ TODO:
 +#include "rsync.h"
 +#include "sysxattr.h"
 +
++#ifdef SUPPORT_XATTRS
++
 +#if defined HAVE_LINUX_XATTRS
 +
 +ssize_t sys_lgetxattr(const char *path, const char *name, void *value, size_t size)
@@ -312,9 +327,9 @@ TODO:
 +      return fgetxattr(filedes, name, value, size);
 +}
 +
-+int sys_lsetxattr(const char *path, const char *name, const void *value, size_t size, int flags)
++int sys_lsetxattr(const char *path, const char *name, const void *value, size_t size)
 +{
-+      return lsetxattr(path, name, value, size, flags);
++      return lsetxattr(path, name, value, size, 0);
 +}
 +
 +int sys_lremovexattr(const char *path, const char *name)
@@ -339,9 +354,9 @@ TODO:
 +      return fgetxattr(filedes, name, value, size, 0, 0);
 +}
 +
-+int sys_lsetxattr(const char *path, const char *name, const void *value, size_t size, int flags)
++int sys_lsetxattr(const char *path, const char *name, const void *value, size_t size)
 +{
-+      return setxattr(path, name, value, size, 0, XATTR_NOFOLLOW | flags);
++      return setxattr(path, name, value, size, 0, XATTR_NOFOLLOW);
 +}
 +
 +int sys_lremovexattr(const char *path, const char *name)
@@ -354,18 +369,73 @@ TODO:
 +      return listxattr(path, list, size, XATTR_NOFOLLOW);
 +}
 +
++#elif HAVE_FREEBSD_XATTRS
++
++ssize_t sys_lgetxattr(const char *path, const char *name, void *value, size_t size)
++{
++      return extattr_get_link(path, EXTATTR_NAMESPACE_USER, name, value, size);
++}
++
++ssize_t sys_fgetxattr(int filedes, const char *name, void *value, size_t size)
++{
++      return extattr_get_fd(filedes, EXTATTR_NAMESPACE_USER, name, value, size);
++}
++
++int sys_lsetxattr(const char *path, const char *name, const void *value, size_t size)
++{
++      return extattr_set_link(path, EXTATTR_NAMESPACE_USER, name, value, size);
++}
++
++int sys_lremovexattr(const char *path, const char *name)
++{
++      return extattr_delete_link(path, EXTATTR_NAMESPACE_USER, name);
++}
++
++ssize_t sys_llistxattr(const char *path, char *list, size_t size)
++{
++      unsigned char keylen;
++      ssize_t off, len = extattr_list_link(path, EXTATTR_NAMESPACE_USER, list, size);
++
++      if (len <= 0 || (size_t)len > size)
++              return len;
++
++      /* FreeBSD puts a single-byte length before each string, with no '\0'
++       * terminator.  We need to change this into a series of null-terminted
++       * strings.  Since the size is the same, we can simply transform the
++       * output in place. */
++      for (off = 0; off < len; ) {
++              keylen = ((unsigned char*)list)[off];
++              if (off + keylen >= len) {
++                      /* Should be impossible, but kernel bugs happen! */
++                      errno = EINVAL;
++                      return -1;
++              }
++              memmove(list+off, list+off+1, keylen);
++              off += keylen;
++              list[off++] = '\0';
++      }
++
++      return len;
++}
++
 +#else
 +
-+#endif /* No xattrs */
++#error You need to create xattr compatibility functions.
++
++#endif
++
++#endif /* SUPPORT_XATTRS */
 --- old/lib/sysxattr.h
 +++ new/lib/sysxattr.h
-@@ -0,0 +1,24 @@
+@@ -0,0 +1,26 @@
 +#ifdef SUPPORT_XATTRS
 +
 +#if defined HAVE_ATTR_XATTR_H
 +#include <attr/xattr.h>
 +#elif defined HAVE_SYS_XATTR_H
 +#include <sys/xattr.h>
++#elif defined HAVE_SYS_EXTATTR_H
++#include <sys/extattr.h>
 +#endif
 +
 +/* Linux 2.4 does not define this as a distinct errno value: */
@@ -375,7 +445,7 @@ TODO:
 +
 +ssize_t sys_lgetxattr(const char *path, const char *name, void *value, size_t size);
 +ssize_t sys_fgetxattr(int filedes, const char *name, void *value, size_t size);
-+int sys_lsetxattr(const char *path, const char *name, const void *value, size_t size, int flags);
++int sys_lsetxattr(const char *path, const char *name, const void *value, size_t size);
 +int sys_lremovexattr(const char *path, const char *name);
 +ssize_t sys_llistxattr(const char *path, char *list, size_t size);
 +
@@ -413,19 +483,18 @@ TODO:
  #ifdef SUPPORT_LINKS
        links = "";
  #endif
-@@ -234,9 +238,9 @@ static void print_rsync_version(enum log
-       rprintf(f, "Copyright (C) 1996-2006 by Andrew Tridgell, Wayne Davison, and others.\n");
-       rprintf(f, "<http://rsync.samba.org/>\n");
-       rprintf(f, "Capabilities: %d-bit files, %ssocketpairs, "
--              "%shard links, %sACLs, %ssymlinks, batchfiles,\n",
-+              "%shard links, %sACLs, %sxattrs, %ssymlinks, batchfiles,\n",
-               (int) (sizeof (OFF_T) * 8),
--              got_socketpair, hardlinks, acls, links);
-+              got_socketpair, hardlinks, acls, xattrs, links);
+@@ -236,8 +240,8 @@ static void print_rsync_version(enum log
+       rprintf(f, "Capabilities: %d-bit files, %ssocketpairs, %shard links, %ssymlinks,\n",
+               (int) (sizeof (OFF_T) * 8), got_socketpair, hardlinks, links);
+-      rprintf(f, "              batchfiles, %sinplace, %sIPv6, %sACLs,\n",
+-              have_inplace, ipv6, acls);
++      rprintf(f, "              batchfiles, %sinplace, %sIPv6, %sACLs, %sxattrs,\n",
++              have_inplace, ipv6, acls, xattrs);
  
        /* Note that this field may not have type ino_t.  It depends
         * on the complicated interaction between largefile feature
-@@ -290,7 +294,7 @@ void usage(enum logcode F)
+@@ -289,7 +293,7 @@ void usage(enum logcode F)
    rprintf(F," -q, --quiet                 suppress non-error messages\n");
    rprintf(F,"     --no-motd               suppress daemon-mode MOTD (see manpage caveat)\n");
    rprintf(F," -c, --checksum              skip based on checksum, not mod-time & size\n");
@@ -434,7 +503,7 @@ TODO:
    rprintf(F,"     --no-OPTION             turn off an implied OPTION (e.g. --no-D)\n");
    rprintf(F," -r, --recursive             recurse into directories\n");
    rprintf(F," -R, --relative              use relative path names\n");
-@@ -315,6 +319,9 @@ void usage(enum logcode F)
+@@ -314,6 +318,9 @@ void usage(enum logcode F)
  #ifdef SUPPORT_ACLS
    rprintf(F," -A, --acls                  preserve ACLs (implies --perms)\n");
  #endif
@@ -444,7 +513,7 @@ TODO:
    rprintf(F," -o, --owner                 preserve owner (super-user only)\n");
    rprintf(F," -g, --group                 preserve group\n");
    rprintf(F,"     --devices               preserve device files (super-user only)\n");
-@@ -437,6 +444,9 @@ static struct poptOption long_options[] 
+@@ -436,6 +443,9 @@ static struct poptOption long_options[] 
    {"acls",            'A', POPT_ARG_NONE,   0, 'A', 0, 0 },
    {"no-acls",          0,  POPT_ARG_VAL,    &preserve_acls, 0, 0, 0 },
    {"no-A",             0,  POPT_ARG_VAL,    &preserve_acls, 0, 0, 0 },
@@ -454,7 +523,7 @@ TODO:
    {"times",           't', POPT_ARG_VAL,    &preserve_times, 1, 0, 0 },
    {"no-times",         0,  POPT_ARG_VAL,    &preserve_times, 0, 0, 0 },
    {"no-t",             0,  POPT_ARG_VAL,    &preserve_times, 0, 0, 0 },
-@@ -1118,6 +1128,17 @@ int parse_arguments(int *argc, const cha
+@@ -1117,6 +1127,17 @@ int parse_arguments(int *argc, const cha
                        return 0;
  #endif
  
@@ -468,11 +537,11 @@ TODO:
 +                               "extended attributes are not supported on this %s\n",
 +                               am_server ? "server" : "client");
 +                      return 0;
-+#endif /* SUPPORT_XATTRS */
++#endif
  
                default:
                        /* A large opt value means that set_refuse_options()
-@@ -1564,6 +1585,10 @@ void server_options(char **args,int *arg
+@@ -1563,6 +1584,10 @@ void server_options(char **args,int *arg
        if (preserve_acls)
                argstr[x++] = 'A';
  #endif
@@ -493,7 +562,7 @@ TODO:
  extern int preserve_perms;
  extern int preserve_executability;
  extern int preserve_times;
-@@ -219,6 +220,10 @@ int set_file_attrs(char *fname, struct f
+@@ -218,6 +219,10 @@ int set_file_attrs(char *fname, struct f
        if (daemon_chmod_modes && !S_ISLNK(new_mode))
                new_mode = tweak_mode(new_mode, daemon_chmod_modes);
  
@@ -506,18 +575,18 @@ TODO:
         * will enable owner-writability using chmod, if necessary.
 --- old/rsync.h
 +++ new/rsync.h
-@@ -501,6 +501,10 @@ struct idev {
+@@ -500,6 +500,10 @@ struct idev {
  #define ACLS_NEED_MASK 1
  #endif
  
-+#if defined HAVE_LINUX_XATTRS || defined HAVE_OSX_XATTRS
++#ifndef HAVE_NO_XATTRS
 +#define SUPPORT_XATTRS 1
 +#endif
 +
  #define GID_NONE ((gid_t)-1)
  
  #define HL_CHECK_MASTER       0
-@@ -695,6 +699,9 @@ typedef struct {
+@@ -694,6 +698,9 @@ typedef struct {
      struct rsync_acl *acc_acl; /* access ACL */
      struct rsync_acl *def_acl; /* default ACL */
  #endif
@@ -560,7 +629,7 @@ TODO:
  transfer.  The resulting value is treated as though it was the permissions
 --- old/xattr.c
 +++ new/xattr.c
-@@ -0,0 +1,375 @@
+@@ -0,0 +1,393 @@
 +/*
 + * Extended Attribute support for rsync.
 + * Written by Jay Fenlason, vaguely based on the ACLs patch.
@@ -589,6 +658,7 @@ TODO:
 +#ifdef SUPPORT_XATTRS
 +
 +extern int dry_run;
++extern int am_root;
 +extern int read_only;
 +extern int list_only;
 +extern unsigned int file_struct_len;
@@ -596,6 +666,11 @@ TODO:
 +#define RSYNC_XAL_INITIAL 5
 +#define RSYNC_XAL_LIST_INITIAL 100
 +
++#define USER_PREFIX "user."
++#define UPRE_LEN (sizeof USER_PREFIX - 1)
++#define ROOT_PREFIX "root."
++#define RPRE_LEN (sizeof ROOT_PREFIX - 1)
++
 +typedef struct {
 +      char *name;
 +      char *datum;
@@ -609,11 +684,6 @@ TODO:
 +static item_list empty_xattr = EMPTY_ITEM_LIST;
 +static item_list rsync_xal_l = EMPTY_ITEM_LIST;
 +
-+#ifdef HAVE_OSX_XATTRS
-+#define UNIQUE_PREFIX "user.0S%." /* OSX */
-+#define UPRE_LEN (sizeof UNIQUE_PREFIX - 1)
-+#endif
-+
 +/* ------------------------------------------------------------------------- */
 +
 +static void rsync_xal_free(item_list *xalp)
@@ -695,9 +765,19 @@ TODO:
 +      if (name_size == 0)
 +              return 0;
 +      for (left = name_size, name = namebuf; left > 0 ; left -= len, name += len) {
-+              rsync_xa *rxas = EXPAND_ITEM_LIST(xalp, rsync_xa, RSYNC_XAL_INITIAL);
++              rsync_xa *rxas;
 +
 +              len = strlen(name) + 1;
++
++#ifdef HAVE_LINUX_XATTRS
++              /* We only send user and root namespaces. */
++              if (strncmp(name, USER_PREFIX, UPRE_LEN) != 0
++               && strncmp(name, ROOT_PREFIX, RPRE_LEN) != 0)
++                      continue;
++#endif
++
++              rxas = EXPAND_ITEM_LIST(xalp, rsync_xa, RSYNC_XAL_INITIAL);
++
 +              datum_size = sys_lgetxattr(fname, name, NULL, 0);
 +              if (datum_size < 0) {
 +                      if (errno == ENOTSUP)
@@ -805,12 +885,13 @@ TODO:
 +              write_byte(f, 'X');
 +              write_int(f, count);
 +              for (rxa = sxp->xattr->items; count--; rxa++) {
-+#ifdef HAVE_OSX_XATTRS
-+                      if (strncmp(rxa->name, "user.", 5) != 0
-+                       && strncmp(rxa->name, "system.", 7) != 0) {
++#ifndef HAVE_LINUX_XATTRS
++                      /* This OS only sends the user namespace, so we may have
++                       * stored a root namespace from Linux in user space. */
++                      if (strncmp(rxa->name, ROOT_PREFIX, RPRE_LEN) != 0) {
 +                              write_int(f, rxa->name_len + UPRE_LEN);
 +                              write_int(f, rxa->datum_len);
-+                              write_buf(f, UNIQUE_PREFIX, UPRE_LEN);
++                              write_buf(f, USER_PREFIX, UPRE_LEN);
 +                      } else
 +#endif
 +                      {
@@ -854,10 +935,16 @@ TODO:
 +                      rxa->datum_len = datum_len;
 +                      rxa->name = ptr;
 +                      rxa->datum = ptr + name_len;
-+#ifdef HAVE_OSX_XATTRS
-+                      if (strncmp(rxa->name, UNIQUE_PREFIX, UPRE_LEN) == 0) {
++#ifdef HAVE_LINUX_XATTRS
++                      if (!am_root && strncmp(ptr, ROOT_PREFIX, RPRE_LEN) == 0) {
++                              temp_xattr.count--;
++                              free(ptr);
++                              continue;
++                      }
++#else
++                      if (strncmp(ptr, USER_PREFIX, UPRE_LEN) == 0) {
 +                              rxa->name_len -= UPRE_LEN;
-+                              memmove(rxa->name, rxa->name + UPRE_LEN, rxa->name_len);
++                              memmove(ptr, ptr + UPRE_LEN, rxa->name_len);
 +                      }
 +#endif
 +              }
@@ -906,7 +993,7 @@ TODO:
 +      int ret = 0;
 +
 +      for (i = 0; i < xalp->count; i++) {
-+              int status = sys_lsetxattr(fname, rxas[i].name, rxas[i].datum, rxas[i].datum_len, 0);
++              int status = sys_lsetxattr(fname, rxas[i].name, rxas[i].datum, rxas[i].datum_len);
 +              if (status < 0) {
 +                      rsyserr(FERROR, errno, "%s: rsync_xal_set: lsetxattr %s failed",
 +                              fname, rxas[i].name);