}
--- old/lib/sysxattr.c
+++ new/lib/sysxattr.c
-@@ -0,0 +1,136 @@
+@@ -0,0 +1,135 @@
+/*
+ * Extended attribute support for rsync.
+ *
+ * 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; ) {
++ for (off = 0; off < len; off += keylen + 1) {
+ keylen = ((unsigned char*)list)[off];
+ if (off + keylen >= len) {
+ /* Should be impossible, but kernel bugs happen! */
+ return -1;
+ }
+ memmove(list+off, list+off+1, keylen);
-+ off += keylen;
-+ list[off++] = '\0';
++ list[off+keylen] = '\0';
+ }
+
+ return len;
transfer. The resulting value is treated as though it was the permissions
--- old/xattr.c
+++ new/xattr.c
-@@ -0,0 +1,406 @@
+@@ -0,0 +1,417 @@
+/*
+ * Extended Attribute support for rsync.
+ * Written by Jay Fenlason, vaguely based on the ACLs patch.
+#define RSYNC_XAL_INITIAL 5
+#define RSYNC_XAL_LIST_INITIAL 100
+
++#define HAS_PREFIX(str, prfx) (*(str) == *(prfx) \
++ && strncmp(str, prfx, sizeof (prfx) - 1) == 0)
++
+#define USER_PREFIX "user."
+#define UPRE_LEN ((int)sizeof USER_PREFIX - 1)
+#define SYSTEM_PREFIX "system."
+ if (errno == ERANGE) {
+ list_len = sys_llistxattr(fname, NULL, 0);
+ if (list_len < 0) {
-+ rsyserr(FERROR, errno, "%s: rsync_xal_get: llistxattr",
++ rsyserr(FERROR, errno,
++ "rsync_xal_get: llistxattr(\"%s\",0) failed",
+ fname);
+ return -1;
+ }
+ list_len = sys_llistxattr(fname, namebuf, namebuf_len);
+ if (list_len < 0) {
+ rsyserr(FERROR, errno,
-+ "%s: rsync_xal_get: re-llistxattr failed",
-+ fname);
++ "rsync_xal_get: llistxattr(\"%s\",%ld) failed",
++ fname, (long)namebuf_len);
+ return -1;
+ }
+ } else {
+ rsyserr(FERROR, errno,
-+ "%s: rsync_xal_get: llistxattr failed:",
-+ fname);
++ "rsync_xal_get: llistxattr(\"%s\",%ld) failed",
++ fname, (long)namebuf_len);
+ return -1;
+ }
+ }
+
-+ for (name = namebuf; list_len > 0 ; list_len -= name_len, name += name_len) {
++ for (name = namebuf; list_len > 0; name += name_len) {
+ rsync_xa *rxas;
+
+ name_len = strlen(name) + 1;
++ list_len -= name_len;
+
+#ifdef HAVE_LINUX_XATTRS
+ /* We don't send the system namespace. */
-+ if (strncmp(name, SYSTEM_PREFIX, SPRE_LEN) == 0)
++ if (HAS_PREFIX(name, SYSTEM_PREFIX))
+ continue;
+#endif
+
+ if (errno == ENOTSUP)
+ return -1;
+ rsyserr(FERROR, errno,
-+ "%s: rsync_xal_get: lgetxattr %s failed",
++ "rsync_xal_get: lgetxattr(\"%s\",\"%s\",0) failed",
+ fname, name);
+ return -1;
+ }
+ if (len != datum_len) {
+ if (len < 0) {
+ rsyserr(FERROR, errno,
-+ "rsync_xal_get: lgetxattr(%s,%s)"
-+ " failed", fname, name);
++ "rsync_xal_get: lgetxattr(\"%s\",\"%s\",%ld)"
++ " failed", fname, name, (long)datum_len);
+ } else {
+ rprintf(FERROR,
-+ "rsync_xal_get: lgetxattr(%s,%s)"
-+ " returned %ld instead of %ld\n",
-+ fname, name,
-+ (long)len, (long)datum_len);
++ "rsync_xal_get: lgetxattr(\"%s\",\"%s\",%ld)"
++ " returned %ld\n", fname, name,
++ (long)datum_len, (long)len);
+ }
+ free(ptr);
+ return -1;
+ }
+ rxas = EXPAND_ITEM_LIST(xalp, rsync_xa, RSYNC_XAL_INITIAL);
+ rxas->name = ptr + datum_len;
-+ memcpy(rxas->name, name, name_len);
++ rxas->datum = ptr;
+ rxas->name_len = name_len;
+ rxas->datum_len = datum_len;
-+ rxas->datum = ptr;
++ memcpy(rxas->name, name, name_len);
+ }
+ if (xalp->count > 1)
+ qsort(xalp->items, xalp->count, sizeof (rsync_xa), rsync_xal_compare_names);
+#else
+ /* We strip the rsync prefix from disguised namespaces
+ * and put everything else in the user namespace. */
-+ if (strncmp(rxa->name, RSYNC_PREFIX, RPRE_LEN) == 0) {
++ if (HAS_PREFIX(rxa->name, RSYNC_PREFIX)
++ && rxa->name[RPRE_LEN] != '%') {
+ write_int(f, rxa->name_len - RPRE_LEN);
+ write_int(f, rxa->datum_len);
+ write_buf(f, rxa->name + RPRE_LEN, rxa->name_len - RPRE_LEN);
+ rsync_xa *rxa;
+ size_t name_len = read_int(f);
+ size_t datum_len = read_int(f);
-+ if (name_len + datum_len < name_len)
-+ out_of_memory("receive_xattr"); /* overflow */
-+#ifndef HAVE_LINUX_XATTRS
-+ if (name_len + datum_len + RPRE_LEN < RPRE_LEN)
++#ifdef HAVE_LINUX_XATTRS
++ size_t extra_len = 0;
++#else
++ size_t extra_len = am_root ? RPRE_LEN : 0;
++ if (datum_len + extra_len < datum_len)
+ out_of_memory("receive_xattr"); /* overflow */
+#endif
-+ ptr = new_array(char, name_len + datum_len + RPRE_LEN);
++ if (name_len + datum_len + extra_len < name_len)
++ out_of_memory("receive_xattr"); /* overflow */
++ ptr = new_array(char, name_len + datum_len + extra_len);
+ if (!ptr)
+ out_of_memory("receive_xattr");
-+ name = ptr + datum_len + RPRE_LEN;
++ name = ptr + datum_len + extra_len;
+ read_buf(f, name, name_len);
+ read_buf(f, ptr, datum_len);
+#ifdef HAVE_LINUX_XATTRS
+ /* Non-root can only save the user namespace. */
-+ if (!am_root && strncmp(name, USER_PREFIX, UPRE_LEN) != 0) {
++ if (!am_root && !HAS_PREFIX(name, USER_PREFIX)) {
+ free(ptr);
+ continue;
+ }
+ /* This OS only has a user namespace, so we either
+ * strip the user prefix, or we put a non-user
+ * namespace inside our rsync hierarchy. */
-+ if (strncmp(name, USER_PREFIX, UPRE_LEN) == 0) {
++ if (HAS_PREFIX(name, USER_PREFIX)) {
+ name += UPRE_LEN;
+ name_len -= UPRE_LEN;
-+ } else {
++ } else if (am_root) {
+ name -= RPRE_LEN;
++ name_len += RPRE_LEN;
+ memcpy(name, RSYNC_PREFIX, RPRE_LEN);
++ } else {
++ free(ptr);
++ continue;
+ }
+#endif
+ rxa = EXPAND_ITEM_LIST(&temp_xattr, rsync_xa, count);
+ rxa->name = name;
-+ rxa->name_len = name_len;
+ rxa->datum = ptr;
++ rxa->name_len = name_len;
+ rxa->datum_len = datum_len;
+ }
+ ndx = rsync_xal_l.count; /* pre-incremented count */
+ } else if (tag == 'x') {
+ ndx = read_int(f);
+ if (ndx < 0 || (size_t)ndx >= rsync_xal_l.count) {
-+ rprintf(FERROR, "%s: receive_xattr: xa index %d out of range\n",
-+ f_name(file, NULL), ndx);
++ rprintf(FERROR, "receive_xattr: xa index %d out of"
++ " range for %s\n", ndx, f_name(file, NULL));
+ exit_cleanup(RERR_STREAMIO);
+ }
+ } else {
-+ rprintf(FERROR,
-+ "%s: receive_xattr: unknown extended attribute type tag: %c\n",
-+ f_name(file, NULL), tag);
++ rprintf(FERROR, "receive_xattr: unknown extended attribute"
++ " type tag (%c) for %s\n", tag, f_name(file, NULL));
+ exit_cleanup(RERR_STREAMIO);
-+ ndx = 0; /* silence a compiler warning... */
+ }
+
+ SIVAL(ndx_ptr, 0, ndx);
+ for (i = 0; i < xalp->count; i++) {
+ 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",
++ rsyserr(FERROR, errno,
++ "rsync_xal_set: lsetxattr(\"%s\",\"%s\") failed",
+ fname, rxas[i].name);
+ ret = -1;
+ }