Fixed a compile problem in the non-Linux code.
[rsync/rsync-patches.git] / xattrs.diff
index 25edadf..4a23f45 100644 (file)
@@ -1,13 +1,18 @@
-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).
 
-This patch adds preliminary support for extended attributes.
-
-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 more efficiently handle large xattrs, especially when
@@ -41,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;
@@ -49,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;
@@ -60,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;
@@ -71,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;
@@ -140,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)]),
@@ -160,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...)
 +            ;;
@@ -278,7 +288,7 @@ TODO:
  }
 --- old/lib/sysxattr.c
 +++ new/lib/sysxattr.c
-@@ -0,0 +1,87 @@
+@@ -0,0 +1,136 @@
 +/*
 + * Extended attribute support for rsync.
 + *
@@ -317,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)
@@ -344,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)
@@ -359,6 +369,55 @@ 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
 +
 +#error You need to create xattr compatibility functions.
@@ -368,13 +427,15 @@ TODO:
 +#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: */
@@ -384,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);
 +
@@ -501,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);
  
@@ -568,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.
@@ -597,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;
@@ -604,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;
@@ -617,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)
@@ -703,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)
@@ -813,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
 +                      {
@@ -862,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
 +              }
@@ -914,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);