Fixed the sending of new files with long xattrs.
[rsync/rsync-patches.git] / xattrs.diff
1 This patch adds support for extended attributes.
2
3 To use this patch, run these commands for a successful build:
4
5     patch -p1 <patches/xattrs.diff
6     ./prepare-source
7     ./configure
8     make
9
10 CAUTION:  this patch has been recently reworked, and needs more testing!
11
12 --- old/Makefile.in
13 +++ new/Makefile.in
14 @@ -28,13 +28,13 @@ VERSION=@VERSION@
15  
16  HEADERS=byteorder.h config.h errcode.h proto.h rsync.h smb_acls.h lib/pool_alloc.h
17  LIBOBJ=lib/wildmatch.o lib/compat.o lib/snprintf.o lib/mdfour.o lib/md5.o \
18 -       lib/permstring.o lib/pool_alloc.o lib/sysacls.o @LIBOBJS@
19 +       lib/permstring.o lib/pool_alloc.o lib/sysacls.o lib/sysxattrs.o @LIBOBJS@
20  ZLIBOBJ=zlib/deflate.o zlib/inffast.o zlib/inflate.o zlib/inftrees.o \
21         zlib/trees.o zlib/zutil.o zlib/adler32.o zlib/compress.o zlib/crc32.o
22  OBJS1=flist.o rsync.o generator.o receiver.o cleanup.o sender.o exclude.o \
23         util.o main.o checksum.o match.o syscall.o log.o backup.o
24  OBJS2=options.o io.o compat.o hlink.o token.o uidlist.o socket.o \
25 -       fileio.o batch.o clientname.o chmod.o acls.o
26 +       fileio.o batch.o clientname.o chmod.o acls.o xattrs.o
27  OBJS3=progress.o pipe.o
28  DAEMON_OBJ = params.o loadparm.o clientserver.o access.o connection.o authenticate.o
29  popt_OBJS=popt/findme.o  popt/popt.o  popt/poptconfig.o \
30 --- old/backup.c
31 +++ new/backup.c
32 @@ -23,6 +23,7 @@
33  extern int verbose;
34  extern int am_root;
35  extern int preserve_acls;
36 +extern int preserve_xattrs;
37  extern int preserve_devices;
38  extern int preserve_specials;
39  extern int preserve_links;
40 @@ -134,6 +135,9 @@ static int make_bak_dir(char *fullpath)
41  #ifdef SUPPORT_ACLS
42                                 sx.acc_acl = sx.def_acl = NULL;
43  #endif
44 +#ifdef SUPPORT_XATTRS
45 +                               sx.xattr = NULL;
46 +#endif
47                                 if (!(file = make_file(rel, NULL, NULL, 0, NO_FILTERS)))
48                                         continue;
49  #ifdef SUPPORT_ACLS
50 @@ -143,7 +147,14 @@ static int make_bak_dir(char *fullpath)
51                                         free_acl(&sx);
52                                 }
53  #endif
54 -                               set_file_attrs(fullpath, file, NULL, 0);
55 +#ifdef SUPPORT_XATTRS
56 +                               if (preserve_xattrs) {
57 +                                       get_xattr(rel, &sx);
58 +                                       cache_xattr(file, &sx);
59 +                                       free_xattr(&sx);
60 +                               }
61 +#endif
62 +                               set_file_attrs(fullpath, file, NULL, NULL, 0);
63                                 free(file);
64                         }
65                 }
66 @@ -194,6 +205,9 @@ static int keep_backup(const char *fname
67  #ifdef SUPPORT_ACLS
68         sx.acc_acl = sx.def_acl = NULL;
69  #endif
70 +#ifdef SUPPORT_XATTRS
71 +       sx.xattr = NULL;
72 +#endif
73  
74         if (!(file = make_file(fname, NULL, NULL, 0, NO_FILTERS)))
75                 return 1; /* the file could have disappeared */
76 @@ -210,6 +224,13 @@ static int keep_backup(const char *fname
77                 free_acl(&sx);
78         }
79  #endif
80 +#ifdef SUPPORT_XATTRS
81 +       if (preserve_xattrs) {
82 +               get_xattr(fname, &sx);
83 +               cache_xattr(file, &sx);
84 +               free_xattr(&sx);
85 +       }
86 +#endif
87  
88         /* Check to see if this is a device file, or link */
89         if ((am_root && preserve_devices && IS_DEVICE(file->mode))
90 @@ -288,7 +309,7 @@ static int keep_backup(const char *fname
91                         robust_unlink(fname); /* Just in case... */
92                 }
93         }
94 -       set_file_attrs(buf, file, NULL, 0);
95 +       set_file_attrs(buf, file, NULL, fname, 0);
96         unmake_file(file);
97  
98         if (verbose > 1) {
99 --- old/cleanup.c
100 +++ new/cleanup.c
101 @@ -142,7 +142,7 @@ NORETURN void _exit_cleanup(int code, co
102                                 flush_write_file(cleanup_fd_w);
103                                 close(cleanup_fd_w);
104                         }
105 -                       finish_transfer(cleanup_new_fname, fname, NULL,
106 +                       finish_transfer(cleanup_new_fname, fname, NULL, NULL,
107                                         cleanup_file, 0, !partial_dir);
108                 }
109  
110 --- old/compat.c
111 +++ new/compat.c
112 @@ -43,6 +43,7 @@ extern int protocol_version;
113  extern int preserve_uid;
114  extern int preserve_gid;
115  extern int preserve_acls;
116 +extern int preserve_xattrs;
117  extern int preserve_hard_links;
118  extern int need_messages_from_generator;
119  extern int delete_mode, delete_before, delete_during, delete_after;
120 @@ -65,6 +66,8 @@ void setup_protocol(int f_out,int f_in)
121                 preserve_gid = ++file_extra_cnt;
122         if (preserve_acls && !am_sender)
123                 preserve_acls = ++file_extra_cnt;
124 +       if (preserve_xattrs)
125 +               preserve_xattrs = ++file_extra_cnt;
126  
127         if (remote_protocol == 0) {
128                 if (!read_batch)
129 @@ -119,6 +122,13 @@ void setup_protocol(int f_out,int f_in)
130                             protocol_version);
131                         exit_cleanup(RERR_PROTOCOL);
132                 }
133 +               if (preserve_xattrs) {
134 +                       rprintf(FERROR,
135 +                           "--xattrs requires protocol 30 or higher"
136 +                           " (negotiated %d).\n",
137 +                           protocol_version);
138 +                       exit_cleanup(RERR_PROTOCOL);
139 +               }
140         }
141  
142         if (delete_mode && !(delete_before+delete_during+delete_after)) {
143 --- old/configure.in
144 +++ new/configure.in
145 @@ -891,6 +891,46 @@ samba_cv_HAVE_ACL_GET_PERM_NP=yes,samba_
146      esac
147  fi
148  
149 +AC_CHECK_HEADERS(attr/xattr.h)
150 +AC_CHECK_HEADERS(sys/xattr.h)
151 +AC_CHECK_HEADERS(sys/extattr.h)
152 +
153 +#################################################
154 +# check for extended attribute support
155 +AC_MSG_CHECKING(whether to support extended attributes)
156 +AC_ARG_ENABLE(xattr-support,
157 +       AC_HELP_STRING([--disable-xattr-support],
158 +                      [Turn off extended attribute support]))
159 +
160 +if test x"$enable_xattr_support" = x"no"; then
161 +    AC_MSG_RESULT(no)
162 +else
163 +    case "$host_os" in
164 +    *linux*)
165 +       AC_MSG_RESULT(Using Linux xattrs)
166 +       AC_DEFINE(HAVE_LINUX_XATTRS, 1, [True if you have Linux xattrs])
167 +       AC_DEFINE(SUPPORT_XATTRS, 1, [Define to 1 to add support for extended attributes])
168 +       ;;
169 +    darwin*)
170 +       AC_MSG_RESULT(Using OS X xattrs)
171 +       AC_DEFINE(HAVE_OSX_XATTRS, 1, [True if you have Mac OS X xattrs])
172 +       AC_DEFINE(SUPPORT_XATTRS, 1)
173 +       ;;
174 +    freebsd*)
175 +       AC_MSG_RESULT(Using FreeBSD extattrs)
176 +       AC_DEFINE(HAVE_FREEBSD_XATTRS, 1, [True if you have FreeBSD xattrs])
177 +       AC_DEFINE(SUPPORT_XATTRS, 1)
178 +       ;;
179 +    *)
180 +       if test x"$enable_xattr_support" = x"yes"; then
181 +           AC_MSG_ERROR(Failed to find extended attribute support)
182 +       else
183 +           AC_MSG_RESULT(No extended attribute support found)
184 +       fi
185 +       ;;
186 +    esac
187 +fi
188 +
189  AC_CONFIG_FILES([Makefile lib/dummy zlib/dummy popt/dummy shconfig])
190  AC_OUTPUT
191  
192 --- old/flist.c
193 +++ new/flist.c
194 @@ -43,6 +43,7 @@ extern int one_file_system;
195  extern int copy_dirlinks;
196  extern int keep_dirlinks;
197  extern int preserve_acls;
198 +extern int preserve_xattrs;
199  extern int preserve_links;
200  extern int preserve_hard_links;
201  extern int preserve_devices;
202 @@ -888,6 +889,10 @@ static struct file_struct *recv_file_ent
203         if (preserve_acls && !S_ISLNK(mode))
204                 receive_acl(file, f);
205  #endif
206 +#ifdef SUPPORT_XATTRS
207 +       if (preserve_xattrs)
208 +               receive_xattr(file, f );
209 +#endif
210  
211         if (S_ISREG(mode) || S_ISLNK(mode))
212                 stats.total_size += file_length;
213 @@ -1160,7 +1165,7 @@ static struct file_struct *send_file_nam
214                                           int flags, int filter_flags)
215  {
216         struct file_struct *file;
217 -#ifdef SUPPORT_ACLS
218 +#if defined SUPPORT_ACLS || defined SUPPORT_XATTRS
219         statx sx;
220  #endif
221  
222 @@ -1179,6 +1184,13 @@ static struct file_struct *send_file_nam
223                         return NULL;
224         }
225  #endif
226 +#ifdef SUPPORT_XATTRS
227 +       if (preserve_xattrs && f >= 0) {
228 +               sx.xattr = NULL;
229 +               if (get_xattr(fname, &sx) < 0)
230 +                       return NULL;
231 +       }
232 +#endif
233  
234         maybe_emit_filelist_progress(flist->count + flist_count_offset);
235  
236 @@ -1192,6 +1204,12 @@ static struct file_struct *send_file_nam
237                         free_acl(&sx);
238                 }
239  #endif
240 +#ifdef SUPPORT_XATTRS
241 +               if (preserve_xattrs) {
242 +                       F_XATTR(file) = send_xattr(&sx, f);
243 +                       free_xattr(&sx);
244 +               }
245 +#endif
246         }
247         return file;
248  }
249 --- old/generator.c
250 +++ new/generator.c
251 @@ -36,6 +36,7 @@ extern int relative_paths;
252  extern int implied_dirs;
253  extern int keep_dirlinks;
254  extern int preserve_acls;
255 +extern int preserve_xattrs;
256  extern int preserve_links;
257  extern int preserve_devices;
258  extern int preserve_specials;
259 @@ -532,11 +533,19 @@ int unchanged_attrs(const char *fname, s
260                         return 0;
261         }
262  #endif
263 +#ifdef SUPPORT_XATTRS
264 +       if (preserve_xattrs) {
265 +               if (!XATTR_READY(*sxp))
266 +                       get_xattr(fname, sxp);
267 +               if (xattr_diff(file, sxp, 0))
268 +                       return 0;
269 +       }
270 +#endif
271  
272         return 1;
273  }
274  
275 -void itemize(const char *fname, struct file_struct *file, int ndx, int statret,
276 +void itemize(const char *fnamecmp, struct file_struct *file, int ndx, int statret,
277              statx *sxp, int32 iflags, uchar fnamecmp_type,
278              const char *xname)
279  {
280 @@ -562,16 +571,29 @@ void itemize(const char *fname, struct f
281  #ifdef SUPPORT_ACLS
282                 if (preserve_acls && !S_ISLNK(file->mode)) {
283                         if (!ACL_READY(*sxp))
284 -                               get_acl(fname, sxp);
285 +                               get_acl(fnamecmp, sxp);
286                         if (set_acl(NULL, file, sxp) == 0)
287                                 iflags |= ITEM_REPORT_ACL;
288                 }
289  #endif
290 -       } else
291 +#ifdef SUPPORT_XATTRS
292 +               if (preserve_xattrs) {
293 +                       if (!XATTR_READY(*sxp))
294 +                               get_xattr(fnamecmp, sxp);
295 +                       if (xattr_diff(file, sxp, 1))
296 +                               iflags |= ITEM_REPORT_XATTR;
297 +               }
298 +#endif
299 +       } else {
300 +#ifdef SUPPORT_XATTRS
301 +               if (preserve_xattrs && xattr_diff(file, NULL, 1))
302 +                       iflags |= ITEM_REPORT_XATTR;
303 +#endif
304                 iflags |= ITEM_IS_NEW;
305 +       }
306  
307         iflags &= 0xffff;
308 -       if ((iflags & SIGNIFICANT_ITEM_FLAGS || verbose > 1
309 +       if ((iflags & (SIGNIFICANT_ITEM_FLAGS|ITEM_REPORT_XATTR) || verbose > 1
310           || stdout_format_has_i > 1 || (xname && *xname)) && !read_batch) {
311                 if (protocol_version >= 29) {
312                         if (ndx >= 0)
313 @@ -581,6 +603,10 @@ void itemize(const char *fname, struct f
314                                 write_byte(sock_f_out, fnamecmp_type);
315                         if (iflags & ITEM_XNAME_FOLLOWS)
316                                 write_vstring(sock_f_out, xname, strlen(xname));
317 +#ifdef SUPPORT_XATTRS
318 +                       if (iflags & ITEM_REPORT_XATTR && !dry_run)
319 +                               send_xattr_request(NULL, file, sock_f_out);
320 +#endif
321                 } else if (ndx >= 0) {
322                         enum logcode code = logfile_format_has_i ? FINFO : FCLIENT;
323                         log_item(code, file, &stats, iflags, xname);
324 @@ -855,14 +881,14 @@ static int try_dests_reg(struct file_str
325                         if (preserve_hard_links && F_IS_HLINKED(file))
326                                 finish_hard_link(file, fname, &sxp->st, itemizing, code, j);
327                         if (itemizing && (verbose > 1 || stdout_format_has_i > 1)) {
328 -                               itemize(fname, file, ndx, 1, sxp,
329 +                               itemize(cmpbuf, file, ndx, 1, sxp,
330                                         ITEM_LOCAL_CHANGE | ITEM_XNAME_FOLLOWS,
331                                         0, "");
332                         }
333                 } else
334  #endif
335                 if (itemizing)
336 -                       itemize(fname, file, ndx, 0, sxp, 0, 0, NULL);
337 +                       itemize(cmpbuf, file, ndx, 0, sxp, 0, 0, NULL);
338                 if (verbose > 1 && maybe_ATTRS_REPORT)
339                         rprintf(FCLIENT, "%s is uptodate\n", fname);
340                 return -2;
341 @@ -879,9 +905,13 @@ static int try_dests_reg(struct file_str
342                         }
343                         return -1;
344                 }
345 +               set_file_attrs(fname, file, NULL, cmpbuf, 0);
346                 if (itemizing)
347 -                       itemize(fname, file, ndx, 0, sxp, ITEM_LOCAL_CHANGE, 0, NULL);
348 -               set_file_attrs(fname, file, NULL, 0);
349 +                       itemize(cmpbuf, file, ndx, 0, sxp, ITEM_LOCAL_CHANGE, 0, NULL);
350 +#ifdef SUPPORT_XATTRS
351 +               if (preserve_xattrs)
352 +                       xattr_clear_locals(file);
353 +#endif
354                 if (maybe_ATTRS_REPORT
355                  && ((!itemizing && verbose && match_level == 2)
356                   || (verbose > 1 && match_level == 3))) {
357 @@ -1029,7 +1059,7 @@ static int try_dests_non(struct file_str
358                             : ITEM_LOCAL_CHANGE
359                              + (match_level == 3 ? ITEM_XNAME_FOLLOWS : 0);
360                         char *lp = match_level == 3 ? "" : NULL;
361 -                       itemize(fname, file, ndx, 0, sxp, chg + ITEM_MATCHED, 0, lp);
362 +                       itemize(cmpbuf, file, ndx, 0, sxp, chg + ITEM_MATCHED, 0, lp);
363                 }
364                 if (verbose > 1 && maybe_ATTRS_REPORT) {
365                         rprintf(FCLIENT, "%s%s is uptodate\n",
366 @@ -1112,6 +1142,9 @@ static void recv_generator(char *fname, 
367  #ifdef SUPPORT_ACLS
368         sx.acc_acl = sx.def_acl = NULL;
369  #endif
370 +#ifdef SUPPORT_XATTRS
371 +       sx.xattr = NULL;
372 +#endif
373         if (dry_run > 1) {
374                 if (fuzzy_dirlist) {
375                         flist_free(fuzzy_dirlist);
376 @@ -1224,7 +1257,7 @@ static void recv_generator(char *fname, 
377                                 goto cleanup;
378                         }
379                 }
380 -               if (set_file_attrs(fname, file, real_ret ? NULL : &real_sx, 0)
381 +               if (set_file_attrs(fname, file, real_ret ? NULL : &real_sx, NULL, 0)
382                     && verbose && code != FNONE && f_out != -1)
383                         rprintf(code, "%s/\n", fname);
384                 if (real_ret != 0 && one_file_system)
385 @@ -1278,9 +1311,9 @@ static void recv_generator(char *fname, 
386                         else if ((len = readlink(fname, lnk, MAXPATHLEN-1)) > 0
387                               && strncmp(lnk, sl, len) == 0 && sl[len] == '\0') {
388                                 /* The link is pointing to the right place. */
389 +                               set_file_attrs(fname, file, &sx, NULL, maybe_ATTRS_REPORT);
390                                 if (itemizing)
391                                         itemize(fname, file, ndx, 0, &sx, 0, 0, NULL);
392 -                               set_file_attrs(fname, file, &sx, maybe_ATTRS_REPORT);
393  #ifdef SUPPORT_HARD_LINKS
394                                 if (preserve_hard_links && F_IS_HLINKED(file))
395                                         finish_hard_link(file, fname, &sx.st, itemizing, code, -1);
396 @@ -1317,7 +1350,7 @@ static void recv_generator(char *fname, 
397                         rsyserr(FERROR, errno, "symlink %s -> \"%s\" failed",
398                                 full_fname(fname), sl);
399                 } else {
400 -                       set_file_attrs(fname, file, NULL, 0);
401 +                       set_file_attrs(fname, file, NULL, NULL, 0);
402                         if (itemizing) {
403                                 itemize(fname, file, ndx, statret, &sx,
404                                         ITEM_LOCAL_CHANGE, 0, NULL);
405 @@ -1357,9 +1390,9 @@ static void recv_generator(char *fname, 
406                          && BITS_EQUAL(sx.st.st_mode, file->mode, _S_IFMT)
407                          && sx.st.st_rdev == rdev) {
408                                 /* The device or special file is identical. */
409 +                               set_file_attrs(fname, file, &sx, NULL, maybe_ATTRS_REPORT);
410                                 if (itemizing)
411                                         itemize(fname, file, ndx, 0, &sx, 0, 0, NULL);
412 -                               set_file_attrs(fname, file, &sx, maybe_ATTRS_REPORT);
413  #ifdef SUPPORT_HARD_LINKS
414                                 if (preserve_hard_links && F_IS_HLINKED(file))
415                                         finish_hard_link(file, fname, &sx.st, itemizing, code, -1);
416 @@ -1399,7 +1432,7 @@ static void recv_generator(char *fname, 
417                         rsyserr(FERROR, errno, "mknod %s failed",
418                                 full_fname(fname));
419                 } else {
420 -                       set_file_attrs(fname, file, NULL, 0);
421 +                       set_file_attrs(fname, file, NULL, NULL, 0);
422                         if (itemizing) {
423                                 itemize(fname, file, ndx, statret, &sx,
424                                         ITEM_LOCAL_CHANGE, 0, NULL);
425 @@ -1529,9 +1562,9 @@ static void recv_generator(char *fname, 
426                         do_unlink(partialptr);
427                         handle_partial_dir(partialptr, PDIR_DELETE);
428                 }
429 +               set_file_attrs(fname, file, &sx, NULL, maybe_ATTRS_REPORT);
430                 if (itemizing)
431                         itemize(fnamecmp, file, ndx, statret, &sx, 0, 0, NULL);
432 -               set_file_attrs(fname, file, &sx, maybe_ATTRS_REPORT);
433  #ifdef SUPPORT_HARD_LINKS
434                 if (preserve_hard_links && F_IS_HLINKED(file))
435                         finish_hard_link(file, fname, &sx.st, itemizing, code, -1);
436 @@ -1636,6 +1669,10 @@ static void recv_generator(char *fname, 
437                 if (preserve_acls)
438                         free_acl(&real_sx);
439  #endif
440 +#ifdef SUPPORT_XATTRS
441 +               if (preserve_xattrs)
442 +                       free_xattr(&real_sx);
443 +#endif
444         }
445  
446         if (!do_xfers) {
447 @@ -1657,7 +1694,7 @@ static void recv_generator(char *fname, 
448  
449         if (f_copy >= 0) {
450                 close(f_copy);
451 -               set_file_attrs(backupptr, back_file, NULL, 0);
452 +               set_file_attrs(backupptr, back_file, NULL, NULL, 0);
453                 if (verbose > 1) {
454                         rprintf(FINFO, "backed up %s to %s\n",
455                                 fname, backupptr);
456 @@ -1672,6 +1709,10 @@ static void recv_generator(char *fname, 
457         if (preserve_acls)
458                 free_acl(&sx);
459  #endif
460 +#ifdef SUPPORT_XATTRS
461 +       if (preserve_xattrs)
462 +               free_xattr(&sx);
463 +#endif
464         return;
465  }
466  
467 --- old/lib/sysxattrs.c
468 +++ new/lib/sysxattrs.c
469 @@ -0,0 +1,135 @@
470 +/*
471 + * Extended attribute support for rsync.
472 + *
473 + * Copyright (C) 2004 Red Hat, Inc.
474 + * Written by Jay Fenlason.
475 + *
476 + * This program is free software; you can redistribute it and/or modify
477 + * it under the terms of the GNU General Public License as published by
478 + * the Free Software Foundation; either version 2 of the License, or
479 + * (at your option) any later version.
480 + *
481 + * This program is distributed in the hope that it will be useful,
482 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
483 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
484 + * GNU General Public License for more details.
485 + *
486 + * You should have received a copy of the GNU General Public License along
487 + * with this program; if not, write to the Free Software Foundation, Inc.,
488 + * 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
489 + */
490 +
491 +#include "rsync.h"
492 +#include "sysxattrs.h"
493 +
494 +#ifdef SUPPORT_XATTRS
495 +
496 +#if defined HAVE_LINUX_XATTRS
497 +
498 +ssize_t sys_lgetxattr(const char *path, const char *name, void *value, size_t size)
499 +{
500 +       return lgetxattr(path, name, value, size);
501 +}
502 +
503 +ssize_t sys_fgetxattr(int filedes, const char *name, void *value, size_t size)
504 +{
505 +       return fgetxattr(filedes, name, value, size);
506 +}
507 +
508 +int sys_lsetxattr(const char *path, const char *name, const void *value, size_t size)
509 +{
510 +       return lsetxattr(path, name, value, size, 0);
511 +}
512 +
513 +int sys_lremovexattr(const char *path, const char *name)
514 +{
515 +       return lremovexattr(path, name);
516 +}
517 +
518 +ssize_t sys_llistxattr(const char *path, char *list, size_t size)
519 +{
520 +       return llistxattr(path, list, size);
521 +}
522 +
523 +#elif HAVE_OSX_XATTRS
524 +
525 +ssize_t sys_lgetxattr(const char *path, const char *name, void *value, size_t size)
526 +{
527 +       return getxattr(path, name, value, size, 0, XATTR_NOFOLLOW);
528 +}
529 +
530 +ssize_t sys_fgetxattr(int filedes, const char *name, void *value, size_t size)
531 +{
532 +       return fgetxattr(filedes, name, value, size, 0, 0);
533 +}
534 +
535 +int sys_lsetxattr(const char *path, const char *name, const void *value, size_t size)
536 +{
537 +       return setxattr(path, name, value, size, 0, XATTR_NOFOLLOW);
538 +}
539 +
540 +int sys_lremovexattr(const char *path, const char *name)
541 +{
542 +       return removexattr(path, name, XATTR_NOFOLLOW);
543 +}
544 +
545 +ssize_t sys_llistxattr(const char *path, char *list, size_t size)
546 +{
547 +       return listxattr(path, list, size, XATTR_NOFOLLOW);
548 +}
549 +
550 +#elif HAVE_FREEBSD_XATTRS
551 +
552 +ssize_t sys_lgetxattr(const char *path, const char *name, void *value, size_t size)
553 +{
554 +       return extattr_get_link(path, EXTATTR_NAMESPACE_USER, name, value, size);
555 +}
556 +
557 +ssize_t sys_fgetxattr(int filedes, const char *name, void *value, size_t size)
558 +{
559 +       return extattr_get_fd(filedes, EXTATTR_NAMESPACE_USER, name, value, size);
560 +}
561 +
562 +int sys_lsetxattr(const char *path, const char *name, const void *value, size_t size)
563 +{
564 +       return extattr_set_link(path, EXTATTR_NAMESPACE_USER, name, value, size);
565 +}
566 +
567 +int sys_lremovexattr(const char *path, const char *name)
568 +{
569 +       return extattr_delete_link(path, EXTATTR_NAMESPACE_USER, name);
570 +}
571 +
572 +ssize_t sys_llistxattr(const char *path, char *list, size_t size)
573 +{
574 +       unsigned char keylen;
575 +       ssize_t off, len = extattr_list_link(path, EXTATTR_NAMESPACE_USER, list, size);
576 +
577 +       if (len <= 0 || (size_t)len > size)
578 +               return len;
579 +
580 +       /* FreeBSD puts a single-byte length before each string, with no '\0'
581 +        * terminator.  We need to change this into a series of null-terminted
582 +        * strings.  Since the size is the same, we can simply transform the
583 +        * output in place. */
584 +       for (off = 0; off < len; off += keylen + 1) {
585 +               keylen = ((unsigned char*)list)[off];
586 +               if (off + keylen >= len) {
587 +                       /* Should be impossible, but kernel bugs happen! */
588 +                       errno = EINVAL;
589 +                       return -1;
590 +               }
591 +               memmove(list+off, list+off+1, keylen);
592 +               list[off+keylen] = '\0';
593 +       }
594 +
595 +       return len;
596 +}
597 +
598 +#else
599 +
600 +#error You need to create xattr compatibility functions.
601 +
602 +#endif
603 +
604 +#endif /* SUPPORT_XATTRS */
605 --- old/lib/sysxattrs.h
606 +++ new/lib/sysxattrs.h
607 @@ -0,0 +1,26 @@
608 +#ifdef SUPPORT_XATTRS
609 +
610 +#if defined HAVE_ATTR_XATTR_H
611 +#include <attr/xattr.h>
612 +#elif defined HAVE_SYS_XATTR_H
613 +#include <sys/xattr.h>
614 +#elif defined HAVE_SYS_EXTATTR_H
615 +#include <sys/extattr.h>
616 +#endif
617 +
618 +/* Linux 2.4 does not define this as a distinct errno value: */
619 +#ifndef ENOATTR
620 +#define ENOATTR ENODATA
621 +#endif
622 +
623 +ssize_t sys_lgetxattr(const char *path, const char *name, void *value, size_t size);
624 +ssize_t sys_fgetxattr(int filedes, const char *name, void *value, size_t size);
625 +int sys_lsetxattr(const char *path, const char *name, const void *value, size_t size);
626 +int sys_lremovexattr(const char *path, const char *name);
627 +ssize_t sys_llistxattr(const char *path, char *list, size_t size);
628 +
629 +#else
630 +
631 +/* No xattrs available */
632 +
633 +#endif
634 --- old/main.c
635 +++ new/main.c
636 @@ -608,7 +608,7 @@ static void fix_basis_dirs(void)
637  }
638  
639  /* This is only called by the sender. */
640 -static void read_final_goodbye(int f_in, int f_out)
641 +static void read_final_goodbye(int f_in)
642  {
643         int i, iflags, xlen;
644         uchar fnamecmp_type;
645 @@ -617,8 +617,8 @@ static void read_final_goodbye(int f_in,
646         if (protocol_version < 29)
647                 i = read_int(f_in);
648         else {
649 -               i = read_ndx_and_attrs(f_in, f_out, &iflags,
650 -                                      &fnamecmp_type, xname, &xlen);
651 +               i = read_ndx_and_attrs(f_in, &iflags, &fnamecmp_type,
652 +                                      xname, &xlen);
653         }
654  
655         if (i != NDX_DONE) {
656 @@ -677,7 +677,7 @@ static void do_server_sender(int f_in, i
657         io_flush(FULL_FLUSH);
658         handle_stats(f_out);
659         if (protocol_version >= 24)
660 -               read_final_goodbye(f_in, f_out);
661 +               read_final_goodbye(f_in);
662         io_flush(FULL_FLUSH);
663         exit_cleanup(0);
664  }
665 @@ -740,7 +740,7 @@ static int do_recv(int f_in, int f_out, 
666                         kluge_around_eof = -1;
667  
668                         /* This should only get stopped via a USR2 signal. */
669 -                       read_ndx_and_attrs(f_in, -1, &iflags, &fnamecmp_type,
670 +                       read_ndx_and_attrs(f_in, &iflags, &fnamecmp_type,
671                                            xname, &xlen);
672  
673                         rprintf(FERROR, "Invalid packet at end of run [%s]\n",
674 @@ -977,7 +977,7 @@ int client_run(int f_in, int f_out, pid_
675                 io_flush(FULL_FLUSH);
676                 handle_stats(-1);
677                 if (protocol_version >= 24)
678 -                       read_final_goodbye(f_in, f_out);
679 +                       read_final_goodbye(f_in);
680                 if (pid != -1) {
681                         if (verbose > 3)
682                                 rprintf(FINFO,"client_run waiting on %d\n", (int) pid);
683 --- old/options.c
684 +++ new/options.c
685 @@ -47,6 +47,7 @@ int copy_links = 0;
686  int preserve_links = 0;
687  int preserve_hard_links = 0;
688  int preserve_acls = 0;
689 +int preserve_xattrs = 0;
690  int preserve_perms = 0;
691  int preserve_executability = 0;
692  int preserve_devices = 0;
693 @@ -201,6 +202,7 @@ static void print_rsync_version(enum log
694         char const *have_inplace = "no ";
695         char const *hardlinks = "no ";
696         char const *acls = "no ";
697 +       char const *xattrs = "no ";
698         char const *links = "no ";
699         char const *ipv6 = "no ";
700         STRUCT_STAT *dumstat;
701 @@ -220,7 +222,9 @@ static void print_rsync_version(enum log
702  #ifdef SUPPORT_ACLS
703         acls = "";
704  #endif
705 -
706 +#ifdef SUPPORT_XATTRS
707 +       xattrs = "";
708 +#endif
709  #ifdef SUPPORT_LINKS
710         links = "";
711  #endif
712 @@ -239,8 +243,8 @@ static void print_rsync_version(enum log
713                 (int)(sizeof (int64) * 8));
714         rprintf(f, "    %ssocketpairs, %shardlinks, %ssymlinks, %sIPv6, batchfiles, %sinplace,\n",
715                 got_socketpair, hardlinks, links, ipv6, have_inplace);
716 -       rprintf(f, "    %sappend, %sACLs\n",
717 -               have_inplace, acls);
718 +       rprintf(f, "    %sappend, %sACLs, %sxattrs\n",
719 +               have_inplace, acls, xattrs);
720  
721  #ifdef MAINTAINER_MODE
722         rprintf(f, "Panic Action: \"%s\"\n", get_panic_action());
723 @@ -286,7 +290,7 @@ void usage(enum logcode F)
724    rprintf(F," -q, --quiet                 suppress non-error messages\n");
725    rprintf(F,"     --no-motd               suppress daemon-mode MOTD (see manpage caveat)\n");
726    rprintf(F," -c, --checksum              skip based on checksum, not mod-time & size\n");
727 -  rprintf(F," -a, --archive               archive mode; same as -rlptgoD (no -H, -A)\n");
728 +  rprintf(F," -a, --archive               archive mode; equals -rlptgoD (no -H,-A,-X)\n");
729    rprintf(F,"     --no-OPTION             turn off an implied OPTION (e.g. --no-D)\n");
730    rprintf(F," -r, --recursive             recurse into directories\n");
731    rprintf(F," -R, --relative              use relative path names\n");
732 @@ -311,6 +315,9 @@ void usage(enum logcode F)
733  #ifdef SUPPORT_ACLS
734    rprintf(F," -A, --acls                  preserve ACLs (implies --perms)\n");
735  #endif
736 +#ifdef SUPPORT_XATTRS
737 +  rprintf(F," -X, --xattrs                preserve extended attributes (implies --perms)\n");
738 +#endif
739    rprintf(F," -o, --owner                 preserve owner (super-user only)\n");
740    rprintf(F," -g, --group                 preserve group\n");
741    rprintf(F,"     --devices               preserve device files (super-user only)\n");
742 @@ -438,6 +445,9 @@ static struct poptOption long_options[] 
743    {"acls",            'A', POPT_ARG_NONE,   0, 'A', 0, 0 },
744    {"no-acls",          0,  POPT_ARG_VAL,    &preserve_acls, 0, 0, 0 },
745    {"no-A",             0,  POPT_ARG_VAL,    &preserve_acls, 0, 0, 0 },
746 +  {"xattrs",          'X', POPT_ARG_NONE,   0, 'X', 0, 0 },
747 +  {"no-xattrs",        0,  POPT_ARG_VAL,    &preserve_xattrs, 0, 0, 0 },
748 +  {"no-X",             0,  POPT_ARG_VAL,    &preserve_xattrs, 0, 0, 0 },
749    {"times",           't', POPT_ARG_VAL,    &preserve_times, 1, 0, 0 },
750    {"no-times",         0,  POPT_ARG_VAL,    &preserve_times, 0, 0, 0 },
751    {"no-t",             0,  POPT_ARG_VAL,    &preserve_times, 0, 0, 0 },
752 @@ -1126,6 +1136,17 @@ int parse_arguments(int *argc, const cha
753                         return 0;
754  #endif
755  
756 +               case 'X':
757 +#ifdef SUPPORT_XATTRS
758 +                       preserve_xattrs = 1;
759 +                       preserve_perms = 1;
760 +                       break;
761 +#else
762 +                       snprintf(err_buf,sizeof(err_buf),
763 +                                "extended attributes are not supported on this %s\n",
764 +                                am_server ? "server" : "client");
765 +                       return 0;
766 +#endif
767  
768                 default:
769                         /* A large opt value means that set_refuse_options()
770 @@ -1590,6 +1611,10 @@ void server_options(char **args,int *arg
771         if (preserve_acls)
772                 argstr[x++] = 'A';
773  #endif
774 +#ifdef SUPPORT_XATTRS
775 +       if (preserve_xattrs)
776 +               argstr[x++] = 'X';
777 +#endif
778         if (recurse)
779                 argstr[x++] = 'r';
780         if (always_checksum)
781 --- old/receiver.c
782 +++ new/receiver.c
783 @@ -22,6 +22,7 @@
784  #include "rsync.h"
785  
786  extern int verbose;
787 +extern int dry_run;
788  extern int do_xfers;
789  extern int am_server;
790  extern int do_progress;
791 @@ -37,6 +38,7 @@ extern int protocol_version;
792  extern int relative_paths;
793  extern int preserve_hard_links;
794  extern int preserve_perms;
795 +extern int preserve_xattrs;
796  extern int basis_dir_cnt;
797  extern int make_backups;
798  extern int cleanup_got_literal;
799 @@ -366,8 +368,8 @@ int recv_files(int f_in, char *local_nam
800                 cleanup_disable();
801  
802                 /* This call also sets cur_flist. */
803 -               ndx = read_ndx_and_attrs(f_in, -1, &iflags,
804 -                                        &fnamecmp_type, xname, &xlen);
805 +               ndx = read_ndx_and_attrs(f_in, &iflags, &fnamecmp_type,
806 +                                        xname, &xlen);
807                 if (ndx == NDX_DONE) {
808                         if (inc_recurse && first_flist) {
809                                 flist_free(first_flist);
810 @@ -397,8 +399,17 @@ int recv_files(int f_in, char *local_nam
811                 if (verbose > 2)
812                         rprintf(FINFO, "recv_files(%s)\n", fname);
813  
814 +#ifdef SUPPORT_XATTRS
815 +               if (iflags & ITEM_REPORT_XATTR && !dry_run)
816 +                       recv_xattr_request(file, f_in);
817 +#endif
818 +
819                 if (!(iflags & ITEM_TRANSFER)) {
820                         maybe_log_item(file, iflags, itemizing, xname);
821 +#ifdef SUPPORT_XATTRS
822 +                       if (preserve_xattrs && iflags & ITEM_REPORT_XATTR && !dry_run)
823 +                               set_file_attrs(fname, file, NULL, fname, 0);
824 +#endif
825                         continue;
826                 }
827                 if (phase == 2) {
828 @@ -655,15 +666,15 @@ int recv_files(int f_in, char *local_nam
829                                 temp_copy_name = NULL;
830                         else
831                                 temp_copy_name = partialptr;
832 -                       finish_transfer(fname, fnametmp, temp_copy_name,
833 -                                       file, recv_ok, 1);
834 +                       finish_transfer(fname, fnametmp, fnamecmp,
835 +                                       temp_copy_name, file, recv_ok, 1);
836                         if (fnamecmp == partialptr) {
837                                 do_unlink(partialptr);
838                                 handle_partial_dir(partialptr, PDIR_DELETE);
839                         }
840                 } else if (keep_partial && partialptr
841                     && handle_partial_dir(partialptr, PDIR_CREATE)) {
842 -                       finish_transfer(partialptr, fnametmp, NULL,
843 +                       finish_transfer(partialptr, fnametmp, fnamecmp, NULL,
844                                         file, recv_ok, !partial_dir);
845                         if (delay_updates && recv_ok) {
846                                 bitbag_set_bit(delayed_bits, ndx);
847 --- old/rsync.c
848 +++ new/rsync.c
849 @@ -32,6 +32,7 @@
850  extern int verbose;
851  extern int dry_run;
852  extern int preserve_acls;
853 +extern int preserve_xattrs;
854  extern int preserve_perms;
855  extern int preserve_executability;
856  extern int preserve_times;
857 @@ -91,10 +92,8 @@ void setup_iconv()
858  }
859  #endif
860  
861 -/* This is used by sender.c with a valid f_out, and by receive.c with
862 - * f_out = -1. */
863 -int read_ndx_and_attrs(int f_in, int f_out, int *iflag_ptr,
864 -                      uchar *type_ptr, char *buf, int *len_ptr)
865 +int read_ndx_and_attrs(int f_in, int *iflag_ptr, uchar *type_ptr,
866 +                      char *buf, int *len_ptr)
867  {
868         int len, iflags = 0;
869         struct file_list *flist;
870 @@ -181,11 +180,6 @@ int read_ndx_and_attrs(int f_in, int f_o
871                                 ndx, who_am_i());
872                         exit_cleanup(RERR_PROTOCOL);
873                 }
874 -       } else if (f_out >= 0) {
875 -               if (inc_recurse)
876 -                       send_extra_file_list(f_out, FILECNT_LOOKAHEAD);
877 -               write_ndx_and_attrs(f_out, ndx, iflags,
878 -                                   fnamecmp_type, buf, len);
879         }
880  
881         *iflag_ptr = iflags;
882 @@ -228,8 +222,8 @@ mode_t dest_mode(mode_t flist_mode, mode
883         return new_mode;
884  }
885  
886 -int set_file_attrs(char *fname, struct file_struct *file, statx *sxp,
887 -                  int flags)
888 +int set_file_attrs(const char *fname, struct file_struct *file, statx *sxp,
889 +                  const char *fnamecmp, int flags)
890  {
891         int updated = 0;
892         statx sx2;
893 @@ -247,6 +241,9 @@ int set_file_attrs(char *fname, struct f
894  #ifdef SUPPORT_ACLS
895                 sx2.acc_acl = sx2.def_acl = NULL;
896  #endif
897 +#ifdef SUPPORT_XATTRS
898 +               sx2.xattr = NULL;
899 +#endif
900                 if (!preserve_perms && S_ISDIR(new_mode)
901                  && sx2.st.st_mode & S_ISGID) {
902                         /* We just created this directory and its setgid
903 @@ -261,6 +258,11 @@ int set_file_attrs(char *fname, struct f
904                 get_acl(fname, sxp);
905  #endif
906  
907 +#ifdef SUPPORT_XATTRS
908 +       if (preserve_xattrs && fnamecmp)
909 +               set_xattr(fname, file, fnamecmp, sxp);
910 +#endif
911 +
912         if (!preserve_times || (S_ISDIR(sxp->st.st_mode) && omit_dir_times))
913                 flags |= ATTRS_SKIP_MTIME;
914         if (!(flags & ATTRS_SKIP_MTIME)
915 @@ -353,10 +355,16 @@ int set_file_attrs(char *fname, struct f
916                         rprintf(FCLIENT, "%s is uptodate\n", fname);
917         }
918    cleanup:
919 +       if (sxp == &sx2) {
920  #ifdef SUPPORT_ACLS
921 -       if (preserve_acls && sxp == &sx2)
922 -               free_acl(&sx2);
923 +               if (preserve_acls)
924 +                       free_acl(&sx2);
925 +#endif
926 +#ifdef SUPPORT_XATTRS
927 +               if (preserve_xattrs)
928 +                       free_xattr(&sx2);
929  #endif
930 +       }
931         return updated;
932  }
933  
934 @@ -378,7 +386,8 @@ RETSIGTYPE sig_int(UNUSED(int val))
935   * attributes (e.g. permissions, ownership, etc.).  If partialptr is not
936   * NULL and the robust_rename() call is forced to copy the temp file, we
937   * stage the file into the partial-dir and then rename it into place. */
938 -void finish_transfer(char *fname, char *fnametmp, char *partialptr,
939 +void finish_transfer(const char *fname, const char *fnametmp,
940 +                    const char *fnamecmp, const char *partialptr,
941                      struct file_struct *file, int ok_to_set_time,
942                      int overwriting_basis)
943  {
944 @@ -395,7 +404,7 @@ void finish_transfer(char *fname, char *
945                 return;
946  
947         /* Change permissions before putting the file into place. */
948 -       set_file_attrs(fnametmp, file, NULL,
949 +       set_file_attrs(fnametmp, file, NULL, fnamecmp,
950                        ok_to_set_time ? 0 : ATTRS_SKIP_MTIME);
951  
952         /* move tmp file over real file */
953 @@ -419,7 +428,7 @@ void finish_transfer(char *fname, char *
954         fnametmp = partialptr ? partialptr : fname;
955  
956    do_set_file_attrs:
957 -       set_file_attrs(fnametmp, file, NULL,
958 +       set_file_attrs(fnametmp, file, NULL, fnamecmp,
959                        ok_to_set_time ? 0 : ATTRS_SKIP_MTIME);
960  
961         if (partialptr) {
962 --- old/rsync.h
963 +++ new/rsync.h
964 @@ -569,6 +569,7 @@ extern int file_extra_cnt;
965  extern int preserve_uid;
966  extern int preserve_gid;
967  extern int preserve_acls;
968 +extern int preserve_xattrs;
969  
970  #define FILE_STRUCT_LEN (offsetof(struct file_struct, basename))
971  #define EXTRA_LEN (sizeof (union file_extras))
972 @@ -601,7 +602,8 @@ extern int preserve_acls;
973  /* When the associated option is on, all entries will have these present: */
974  #define F_OWNER(f) REQ_EXTRA(f, preserve_uid)->unum
975  #define F_GROUP(f) REQ_EXTRA(f, preserve_gid)->unum
976 -#define F_ACL(f) REQ_EXTRA(f, preserve_acls)->unum
977 +#define F_ACL(f) REQ_EXTRA(f, preserve_acls)->num
978 +#define F_XATTR(f) REQ_EXTRA(f, preserve_xattrs)->num
979  
980  /* These items are per-entry optional and mutally exclusive: */
981  #define F_HL_GNUM(f) OPT_EXTRA(f, LEN64_BUMP(f))->num
982 @@ -793,9 +795,13 @@ typedef struct {
983      struct rsync_acl *acc_acl; /* access ACL */
984      struct rsync_acl *def_acl; /* default ACL */
985  #endif
986 +#ifdef SUPPORT_XATTRS
987 +    item_list *xattr;
988 +#endif
989  } statx;
990  
991  #define ACL_READY(sx) ((sx).acc_acl != NULL)
992 +#define XATTR_READY(sx) ((sx).xattr != NULL)
993  
994  #include "proto.h"
995  
996 --- old/rsync.yo
997 +++ new/rsync.yo
998 @@ -301,7 +301,7 @@ to the detailed description below for a 
999   -q, --quiet                 suppress non-error messages
1000       --no-motd               suppress daemon-mode MOTD (see caveat)
1001   -c, --checksum              skip based on checksum, not mod-time & size
1002 - -a, --archive               archive mode; same as -rlptgoD (no -H, -A)
1003 + -a, --archive               archive mode; equals -rlptgoD (no -H,-A,-X)
1004       --no-OPTION             turn off an implied OPTION (e.g. --no-D)
1005   -r, --recursive             recurse into directories
1006   -R, --relative              use relative path names
1007 @@ -324,6 +324,7 @@ to the detailed description below for a 
1008   -E, --executability         preserve executability
1009       --chmod=CHMOD           affect file and/or directory permissions
1010   -A, --acls                  preserve ACLs (implies -p)
1011 + -X, --xattrs                preserve extended attrs (implies -p)
1012   -o, --owner                 preserve owner (super-user only)
1013   -g, --group                 preserve group
1014       --devices               preserve device files (super-user only)
1015 @@ -831,6 +832,11 @@ dit(bf(-A, --acls)) This option causes r
1016  ACLs to be the same as the source ACLs.  This nonstandard option only
1017  works if the remote rsync also supports it.  bf(--acls) implies bf(--perms).
1018  
1019 +dit(bf(-X, --xattrs)) This option causes rsync to update the remote
1020 +extended attributes to be the same as the local ones.  This will work
1021 +only if the remote machine's rsync supports this option also. This is
1022 +a non-standard option.
1023 +
1024  dit(bf(--chmod)) This option tells rsync to apply one or more
1025  comma-separated "chmod" strings to the permission of the files in the
1026  transfer.  The resulting value is treated as though it was the permissions
1027 --- old/sender.c
1028 +++ new/sender.c
1029 @@ -22,6 +22,7 @@
1030  #include "rsync.h"
1031  
1032  extern int verbose;
1033 +extern int dry_run;
1034  extern int do_xfers;
1035  extern int am_server;
1036  extern int am_daemon;
1037 @@ -33,6 +34,7 @@ extern int csum_length;
1038  extern int append_mode;
1039  extern int io_error;
1040  extern int allowed_lull;
1041 +extern int preserve_xattrs;
1042  extern int protocol_version;
1043  extern int remove_source_files;
1044  extern int updating_basis_file;
1045 @@ -144,8 +146,9 @@ void successful_send(int ndx)
1046                 rsyserr(FERROR, errno, "sender failed to remove %s", fname);
1047  }
1048  
1049 -void write_ndx_and_attrs(int f_out, int ndx, int iflags,
1050 -                        uchar fnamecmp_type, char *buf, int len)
1051 +static void write_ndx_and_attrs(int f_out, int ndx, int iflags,
1052 +                               const char *fname, struct file_struct *file,
1053 +                               uchar fnamecmp_type, char *buf, int len)
1054  {
1055         write_ndx(f_out, ndx);
1056         if (protocol_version < 29)
1057 @@ -155,6 +158,10 @@ void write_ndx_and_attrs(int f_out, int 
1058                 write_byte(f_out, fnamecmp_type);
1059         if (iflags & ITEM_XNAME_FOLLOWS)
1060                 write_vstring(f_out, buf, len);
1061 +#ifdef SUPPORT_XATTRS
1062 +       if (preserve_xattrs && iflags & ITEM_REPORT_XATTR && !dry_run)
1063 +               send_xattr_request(fname, file, f_out);
1064 +#endif
1065  }
1066  
1067  void send_files(int f_in, int f_out)
1068 @@ -183,8 +190,8 @@ void send_files(int f_in, int f_out)
1069                         send_extra_file_list(f_out, FILECNT_LOOKAHEAD);
1070  
1071                 /* This call also sets cur_flist. */
1072 -               ndx = read_ndx_and_attrs(f_in, f_out, &iflags,
1073 -                                        &fnamecmp_type, xname, &xlen);
1074 +               ndx = read_ndx_and_attrs(f_in, &iflags, &fnamecmp_type,
1075 +                                        xname, &xlen);
1076                 if (ndx == NDX_DONE) {
1077                         if (inc_recurse && first_flist) {
1078                                 flist_free(first_flist);
1079 @@ -201,6 +208,9 @@ void send_files(int f_in, int f_out)
1080                         continue;
1081                 }
1082  
1083 +               if (inc_recurse)
1084 +                       send_extra_file_list(f_out, FILECNT_LOOKAHEAD);
1085 +
1086                 file = cur_flist->files[ndx - cur_flist->ndx_start];
1087                 if (F_ROOTDIR(file)) {
1088                         path = F_ROOTDIR(file);
1089 @@ -215,8 +225,15 @@ void send_files(int f_in, int f_out)
1090                 if (verbose > 2)
1091                         rprintf(FINFO, "send_files(%d, %s%s%s)\n", ndx, path,slash,fname);
1092  
1093 +#ifdef SUPPORT_XATTRS
1094 +               if (preserve_xattrs && iflags & ITEM_REPORT_XATTR)
1095 +                       recv_xattr_request(file, f_in);
1096 +#endif
1097 +
1098                 if (!(iflags & ITEM_TRANSFER)) {
1099                         maybe_log_item(file, iflags, itemizing, xname);
1100 +                       write_ndx_and_attrs(f_out, ndx, iflags, fname, file,
1101 +                                           fnamecmp_type, xname, xlen);
1102                         continue;
1103                 }
1104                 if (phase == 2) {
1105 @@ -251,8 +268,8 @@ void send_files(int f_in, int f_out)
1106  
1107                 if (!do_xfers) { /* log the transfer */
1108                         log_item(FCLIENT, file, &stats, iflags, NULL);
1109 -                       write_ndx_and_attrs(f_out, ndx, iflags, fnamecmp_type,
1110 -                                           xname, xlen);
1111 +                       write_ndx_and_attrs(f_out, ndx, iflags, fname, file,
1112 +                                           fnamecmp_type, xname, xlen);
1113                         continue;
1114                 }
1115  
1116 @@ -305,8 +322,8 @@ void send_files(int f_in, int f_out)
1117                                 path,slash,fname, (double)st.st_size);
1118                 }
1119  
1120 -               write_ndx_and_attrs(f_out, ndx, iflags, fnamecmp_type,
1121 -                                   xname, xlen);
1122 +               write_ndx_and_attrs(f_out, ndx, iflags, fname, file,
1123 +                                   fnamecmp_type, xname, xlen);
1124                 write_sum_head(f_xfer, s);
1125  
1126                 if (verbose > 2)
1127 --- old/testsuite/xattrs.test
1128 +++ new/testsuite/xattrs.test
1129 @@ -0,0 +1,90 @@
1130 +#! /bin/sh
1131 +
1132 +# This program is distributable under the terms of the GNU GPL (see
1133 +# COPYING).
1134 +
1135 +# Test that rsync handles basic xattr preservation.
1136 +
1137 +. $srcdir/testsuite/rsync.fns
1138 +
1139 +$RSYNC --version | grep ", xattrs" >/dev/null || test_skipped "Rsync is configured without xattr support"
1140 +
1141 +case "$RSYNC" in
1142 +*protocol=29*) test_skipped "xattr support requires protocol 30" ;;
1143 +esac
1144 +
1145 +xset() {
1146 +    xnam="$1"
1147 +    xval="$2"
1148 +    shift 2
1149 +    setfattr -n "$xnam" -v "$xval" "${@}"
1150 +    #xattr -s "$xnam" "$xval" "${@}"
1151 +}
1152 +
1153 +xls() {
1154 +    getfattr -d "${@}"
1155 +    #xattr -l "${@}"
1156 +}
1157 +
1158 +makepath "$fromdir/foo"
1159 +echo something >"$fromdir/file1"
1160 +echo else >"$fromdir/file2"
1161 +echo last >"$fromdir/foo/file3"
1162 +
1163 +makepath "$chkdir/foo"
1164 +echo wow >"$chkdir/file1"
1165 +cp -p "$fromdir/foo/file3" "$chkdir/foo"
1166 +
1167 +files='foo file1 file2 foo/file3'
1168 +
1169 +cd "$fromdir"
1170 +
1171 +xset user.short 'this is short' file1 2>/dev/null || test_skipped "Unable to set an xattr"
1172 +xset user.long 'this is a long attribute that will be truncated in the initial data send' file1
1173 +xset user.good 'this is good' file1
1174 +xset user.nice 'this is nice' file1
1175 +
1176 +xset user.foo foo file2
1177 +xset user.bar bar file2
1178 +xset user.long 'a long attribute for our new file that tests to ensure that this works' file2
1179 +
1180 +xset user.foo 'new foo' foo/file3
1181 +xset user.bar 'new bar' foo/file3
1182 +xset user.long 'this is also a long attribute that will be truncated in the initial data send' foo/file3
1183 +xset user.equal 'this long attribute should remain the same and not need to be transferred' foo/file3
1184 +
1185 +xset user.short 'old short' "$chkdir/file1"
1186 +xset user.extra 'remove me' "$chkdir/file1"
1187 +
1188 +xset user.foo 'old foo' "$chkdir/foo/file3"
1189 +xset user.equal 'this long attribute should remain the same and not need to be transferred' "$chkdir/foo/file3"
1190 +
1191 +xls $files >"$scratchdir/xattrs.txt"
1192 +
1193 +# OK, let's try a simple xattr copy.
1194 +checkit "$RSYNC -avX . \"$chkdir/\"" "$fromdir" "$chkdir"
1195 +
1196 +cd "$chkdir"
1197 +xls $files | diff $diffopt "$scratchdir/xattrs.txt" -
1198 +
1199 +cd "$fromdir"
1200 +
1201 +checkit "$RSYNC -aiX --copy-dest=../chk . ../to" "$fromdir" "$todir"
1202 +
1203 +cd "$todir"
1204 +xls $files | diff $diffopt "$scratchdir/xattrs.txt" -
1205 +
1206 +cd "$fromdir"
1207 +xset user.nice 'this is nice, but different' file1
1208 +
1209 +xls $files >"$scratchdir/xattrs.txt"
1210 +
1211 +rm -rf "$todir"
1212 +
1213 +checkit "$RSYNC -aiX --link-dest=../chk . ../to" "$chkdir" "$todir"
1214 +
1215 +cd "$todir"
1216 +xls $files | diff $diffopt "$scratchdir/xattrs.txt" -
1217 +
1218 +# The script would have aborted on error, so getting here means we've won.
1219 +exit 0
1220 --- old/xattrs.c
1221 +++ new/xattrs.c
1222 @@ -0,0 +1,771 @@
1223 +/*
1224 + * Extended Attribute support for rsync.
1225 + * Written by Jay Fenlason, vaguely based on the ACLs patch.
1226 + *
1227 + * Copyright (C) 2004 Red Hat, Inc.
1228 + * Copyright (C) 2006, 2007 Wayne Davison
1229 + *
1230 + * This program is free software; you can redistribute it and/or modify
1231 + * it under the terms of the GNU General Public License version 2 as
1232 + * published by the Free Software Foundation.
1233 + *
1234 + * This program is distributed in the hope that it will be useful,
1235 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
1236 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1237 + * GNU General Public License for more details.
1238 + *
1239 + * You should have received a copy of the GNU General Public License along
1240 + * with this program; if not, write to the Free Software Foundation, Inc.,
1241 + * 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
1242 + */
1243 +
1244 +#include "rsync.h"
1245 +#include "lib/sysxattrs.h"
1246 +
1247 +#ifdef SUPPORT_XATTRS
1248 +
1249 +extern int dry_run;
1250 +extern int am_root;
1251 +extern int am_sender;
1252 +extern int am_generator;
1253 +extern int read_only;
1254 +extern int list_only;
1255 +extern int checksum_seed;
1256 +
1257 +#define RSYNC_XAL_INITIAL 5
1258 +#define RSYNC_XAL_LIST_INITIAL 100
1259 +
1260 +#define MAX_FULL_DATUM 32
1261 +
1262 +#define HAS_PREFIX(str, prfx) (*(str) == *(prfx) \
1263 +                           && strncmp(str, prfx, sizeof (prfx) - 1) == 0)
1264 +
1265 +#define XATTR_ABBREV(x) ((size_t)((x).name - (x).datum) < (x).datum_len)
1266 +
1267 +#define XSTATE_ABBREV  0
1268 +#define XSTATE_DONE    1
1269 +#define XSTATE_TODO    2
1270 +#define XSTATE_LOCAL   3
1271 +
1272 +#define USER_PREFIX "user."
1273 +#define UPRE_LEN ((int)sizeof USER_PREFIX - 1)
1274 +#define SYSTEM_PREFIX "system."
1275 +#define SPRE_LEN ((int)sizeof SYSTEM_PREFIX - 1)
1276 +
1277 +#ifdef HAVE_LINUX_XATTRS
1278 +#define RPRE_LEN 0
1279 +#else
1280 +#define RSYNC_PREFIX "rsync."
1281 +#define RPRE_LEN ((int)sizeof RSYNC_PREFIX - 1)
1282 +#endif
1283 +
1284 +typedef struct {
1285 +       char *datum, *name;
1286 +       size_t datum_len, name_len;
1287 +} rsync_xa;
1288 +
1289 +static size_t namebuf_len = 0;
1290 +static char *namebuf = NULL;
1291 +
1292 +static item_list empty_xattr = EMPTY_ITEM_LIST;
1293 +static item_list rsync_xal_l = EMPTY_ITEM_LIST;
1294 +
1295 +/* ------------------------------------------------------------------------- */
1296 +
1297 +static void rsync_xal_free(item_list *xalp)
1298 +{
1299 +       size_t i;
1300 +       rsync_xa *rxas = xalp->items;
1301 +
1302 +       for (i = 0; i < xalp->count; i++) {
1303 +               free(rxas[i].datum);
1304 +               /*free(rxas[i].name);*/
1305 +       }
1306 +       xalp->count = 0;
1307 +}
1308 +
1309 +void free_xattr(statx *sxp)
1310 +{
1311 +       if (!sxp->xattr)
1312 +               return;
1313 +       rsync_xal_free(sxp->xattr);
1314 +       free(sxp->xattr);
1315 +       sxp->xattr = NULL;
1316 +}
1317 +
1318 +static int rsync_xal_compare_names(const void *x1, const void *x2)
1319 +{
1320 +       const rsync_xa *xa1 = x1;
1321 +       const rsync_xa *xa2 = x2;
1322 +       return strcmp(xa1->name, xa2->name);
1323 +}
1324 +
1325 +static ssize_t get_xattr_names(const char *fname)
1326 +{
1327 +       ssize_t list_len;
1328 +
1329 +       if (!namebuf) {
1330 +               namebuf_len = 1024;
1331 +               namebuf = new_array(char, namebuf_len);
1332 +               if (!namebuf)
1333 +                       out_of_memory("get_xattr_names");
1334 +       }
1335 +
1336 +       /* The length returned includes all the '\0' terminators. */
1337 +       list_len = sys_llistxattr(fname, namebuf, namebuf_len);
1338 +       if (list_len > (ssize_t)namebuf_len) {
1339 +               list_len = -1;
1340 +               errno = ERANGE;
1341 +       }
1342 +       if (list_len >= 0)
1343 +               return list_len;
1344 +       if (errno == ENOTSUP)
1345 +               return 0;
1346 +       if (errno == ERANGE) {
1347 +               list_len = sys_llistxattr(fname, NULL, 0);
1348 +               if (list_len < 0) {
1349 +                       rsyserr(FERROR, errno,
1350 +                               "get_xattr_names: llistxattr(\"%s\",0) failed",
1351 +                               fname);
1352 +                       return -1;
1353 +               }
1354 +               if (namebuf_len)
1355 +                       free(namebuf);
1356 +               namebuf_len = list_len + 1024;
1357 +               namebuf = new_array(char, namebuf_len);
1358 +               if (!namebuf)
1359 +                       out_of_memory("get_xattr_names");
1360 +               list_len = sys_llistxattr(fname, namebuf, namebuf_len);
1361 +               if (list_len >= 0)
1362 +                       return list_len;
1363 +       }
1364 +
1365 +       rsyserr(FERROR, errno,
1366 +               "get_xattr_names: llistxattr(\"%s\",%ld) failed",
1367 +               fname, (long)namebuf_len);
1368 +       return -1;
1369 +}
1370 +
1371 +/* On entry, the *len_ptr parameter contains the size of the extra space we
1372 + * should allocate when we create a buffer for the data.  On exit, it contains
1373 + * the length of the datum. */
1374 +static char *get_xattr_data(const char *fname, const char *name, size_t *len_ptr,
1375 +                           int no_missing_error)
1376 +{
1377 +       size_t datum_len = sys_lgetxattr(fname, name, NULL, 0);
1378 +       char *ptr;
1379 +
1380 +       if (datum_len == (size_t)-1) {
1381 +               if (errno == ENOTSUP || no_missing_error)
1382 +                       return NULL;
1383 +               rsyserr(FERROR, errno,
1384 +                       "get_xattr_data: lgetxattr(\"%s\",\"%s\",0) failed",
1385 +                       fname, name);
1386 +               return NULL;
1387 +       }
1388 +
1389 +       if (datum_len + *len_ptr < datum_len /* checks for overflow */
1390 +        || !(ptr = new_array(char, datum_len + *len_ptr)))
1391 +               out_of_memory("get_xattr_data");
1392 +
1393 +       *len_ptr = datum_len;
1394 +
1395 +       if (datum_len) {
1396 +               size_t len = sys_lgetxattr(fname, name, ptr, datum_len);
1397 +               if (len != datum_len) {
1398 +                       if (len == (size_t)-1) {
1399 +                               rsyserr(FERROR, errno,
1400 +                                   "get_xattr_data: lgetxattr(\"%s\",\"%s\",%ld)"
1401 +                                   " failed", fname, name, (long)datum_len);
1402 +                       } else {
1403 +                               rprintf(FERROR,
1404 +                                   "get_xattr_data: lgetxattr(\"%s\",\"%s\",%ld)"
1405 +                                   " returned %ld\n", fname, name,
1406 +                                   (long)datum_len, (long)len);
1407 +                       }
1408 +                       free(ptr);
1409 +                       return NULL;
1410 +               }
1411 +       }
1412 +
1413 +       return ptr;
1414 +}
1415 +
1416 +static int rsync_xal_get(const char *fname, item_list *xalp)
1417 +{
1418 +       ssize_t list_len, name_len;
1419 +       size_t datum_len, name_offset;
1420 +       char *name, *ptr;
1421 +#ifdef HAVE_LINUX_XATTRS
1422 +       int user_only = am_sender ? 0 : !am_root;
1423 +#endif
1424 +
1425 +       /* This puts the name list into the "namebuf" buffer. */
1426 +       if ((list_len = get_xattr_names(fname)) < 0)
1427 +               return -1;
1428 +
1429 +       for (name = namebuf; list_len > 0; name += name_len) {
1430 +               rsync_xa *rxas;
1431 +
1432 +               name_len = strlen(name) + 1;
1433 +               list_len -= name_len;
1434 +
1435 +#ifdef HAVE_LINUX_XATTRS
1436 +               /* We always ignore the system namespace, and non-root
1437 +                * ignores everything but the user namespace. */
1438 +               if (user_only ? !HAS_PREFIX(name, USER_PREFIX)
1439 +                             : HAS_PREFIX(name, SYSTEM_PREFIX))
1440 +                       continue;
1441 +#endif
1442 +
1443 +               datum_len = name_len; /* Pass extra size to get_xattr_data() */
1444 +               if (!(ptr = get_xattr_data(fname, name, &datum_len, 0)))
1445 +                       return -1;
1446 +
1447 +               if (datum_len > MAX_FULL_DATUM) {
1448 +                       /* For large datums, we store a flag and a checksum. */
1449 +                       name_offset = 1 + MAX_DIGEST_LEN;
1450 +                       sum_init(checksum_seed);
1451 +                       sum_update(ptr, datum_len);
1452 +                       free(ptr);
1453 +
1454 +                       if (!(ptr = new_array(char, name_offset + name_len)))
1455 +                               out_of_memory("rsync_xal_get");
1456 +                       *ptr = XSTATE_ABBREV;
1457 +                       sum_end(ptr + 1);
1458 +               } else
1459 +                       name_offset = datum_len;
1460 +
1461 +               rxas = EXPAND_ITEM_LIST(xalp, rsync_xa, RSYNC_XAL_INITIAL);
1462 +               rxas->name = ptr + name_offset;
1463 +               memcpy(rxas->name, name, name_len);
1464 +               rxas->datum = ptr;
1465 +               rxas->name_len = name_len;
1466 +               rxas->datum_len = datum_len;
1467 +       }
1468 +       if (xalp->count > 1)
1469 +               qsort(xalp->items, xalp->count, sizeof (rsync_xa), rsync_xal_compare_names);
1470 +       return 0;
1471 +}
1472 +
1473 +/* Read the xattr(s) for this filename. */
1474 +int get_xattr(const char *fname, statx *sxp)
1475 +{
1476 +       sxp->xattr = new(item_list);
1477 +       *sxp->xattr = empty_xattr;
1478 +       if (rsync_xal_get(fname, sxp->xattr) < 0) {
1479 +               free_xattr(sxp);
1480 +               return -1;
1481 +       }
1482 +       return 0;
1483 +}
1484 +
1485 +static int find_matching_xattr(item_list *xalp)
1486 +{
1487 +       size_t i, j;
1488 +       item_list *lst = rsync_xal_l.items;
1489 +
1490 +       for (i = 0; i < rsync_xal_l.count; i++) {
1491 +               rsync_xa *rxas1 = lst[i].items;
1492 +               rsync_xa *rxas2 = xalp->items;
1493 +
1494 +               /* Wrong number of elements? */
1495 +               if (lst[i].count != xalp->count)
1496 +                       continue;
1497 +               /* any elements different? */
1498 +               for (j = 0; j < xalp->count; j++) {
1499 +                       if (rxas1[j].name_len != rxas2[j].name_len
1500 +                        || rxas1[j].datum_len != rxas2[j].datum_len
1501 +                        || strcmp(rxas1[j].name, rxas2[j].name))
1502 +                               break;
1503 +                       if (rxas1[j].datum_len > MAX_FULL_DATUM) {
1504 +                               if (memcmp(rxas1[j].datum + 1,
1505 +                                          rxas2[j].datum + 1,
1506 +                                          MAX_DIGEST_LEN) != 0)
1507 +                                       break;
1508 +                       } else {
1509 +                               if (memcmp(rxas1[j].datum, rxas2[j].datum,
1510 +                                          rxas2[j].datum_len))
1511 +                                       break;
1512 +                       }
1513 +               }
1514 +               /* no differences found.  This is The One! */
1515 +               if (j == xalp->count)
1516 +                       return i;
1517 +       }
1518 +
1519 +       return -1;
1520 +}
1521 +
1522 +/* Store *xalp on the end of rsync_xal_l */
1523 +static void rsync_xal_store(item_list *xalp)
1524 +{
1525 +       item_list *new_lst = EXPAND_ITEM_LIST(&rsync_xal_l, item_list, RSYNC_XAL_LIST_INITIAL);
1526 +       /* Since the following call starts a new list, we know it will hold the
1527 +        * entire initial-count, not just enough space for one new item. */
1528 +       *new_lst = empty_xattr;
1529 +       (void)EXPAND_ITEM_LIST(new_lst, rsync_xa, xalp->count);
1530 +       memcpy(new_lst->items, xalp->items, xalp->count * sizeof (rsync_xa));
1531 +       new_lst->count = xalp->count;
1532 +       xalp->count = 0;
1533 +}
1534 +
1535 +/* Send the make_xattr()-generated xattr list for this flist entry. */
1536 +int send_xattr(statx *sxp, int f)
1537 +{
1538 +       int ndx = find_matching_xattr(sxp->xattr);
1539 +
1540 +       /* Send 0 (-1 + 1) to indicate that literal xattr data follows. */
1541 +       write_abbrevint(f, ndx + 1);
1542 +
1543 +       if (ndx < 0) {
1544 +               rsync_xa *rxa;
1545 +               int count = sxp->xattr->count;
1546 +               write_abbrevint(f, count);
1547 +               for (rxa = sxp->xattr->items; count--; rxa++) {
1548 +#ifdef HAVE_LINUX_XATTRS
1549 +                       write_abbrevint(f, rxa->name_len);
1550 +                       write_abbrevint(f, rxa->datum_len);
1551 +                       write_buf(f, rxa->name, rxa->name_len);
1552 +#else
1553 +                       /* We strip the rsync prefix from disguised namespaces
1554 +                        * and put everything else in the user namespace. */
1555 +                       if (HAS_PREFIX(rxa->name, RSYNC_PREFIX)
1556 +                        && rxa->name[RPRE_LEN] != '%') {
1557 +                               write_abbrevint(f, rxa->name_len - RPRE_LEN);
1558 +                               write_abbrevint(f, rxa->datum_len);
1559 +                               write_buf(f, rxa->name + RPRE_LEN, rxa->name_len - RPRE_LEN);
1560 +                       } else {
1561 +                               write_abbrevint(f, rxa->name_len + UPRE_LEN);
1562 +                               write_abbrevint(f, rxa->datum_len);
1563 +                               write_buf(f, USER_PREFIX, UPRE_LEN);
1564 +                               write_buf(f, rxa->name, rxa->name_len);
1565 +                       }
1566 +#endif
1567 +                       if (rxa->datum_len > MAX_FULL_DATUM)
1568 +                               write_buf(f, rxa->datum + 1, MAX_DIGEST_LEN);
1569 +                       else
1570 +                               write_buf(f, rxa->datum, rxa->datum_len);
1571 +               }
1572 +               ndx = rsync_xal_l.count; /* pre-incremented count */
1573 +               rsync_xal_store(sxp->xattr); /* adds item to rsync_xal_l */
1574 +       }
1575 +
1576 +       return ndx;
1577 +}
1578 +
1579 +/* Return a flag indicating if we need to change a file's xattrs.  If
1580 + * "find_all" is specified, also mark any abbreviated xattrs that we
1581 + * need so that send_xattr_request() can tell the sender about them. */
1582 +int xattr_diff(struct file_struct *file, statx *sxp, int find_all)
1583 +{
1584 +       item_list *lst = rsync_xal_l.items;
1585 +       rsync_xa *snd_rxa, *rec_rxa;
1586 +       int snd_cnt, rec_cnt;
1587 +       int cmp, same, xattrs_equal = 1;
1588 +
1589 +       if (sxp && XATTR_READY(*sxp)) {
1590 +               rec_rxa = sxp->xattr->items;
1591 +               rec_cnt = sxp->xattr->count;
1592 +       } else {
1593 +               rec_rxa = NULL;
1594 +               rec_cnt = 0;
1595 +       }
1596 +
1597 +       if (F_XATTR(file) >= 0)
1598 +               lst += F_XATTR(file);
1599 +       else
1600 +               lst = &empty_xattr;
1601 +
1602 +       snd_rxa = lst->items;
1603 +       snd_cnt = lst->count;
1604 +
1605 +       /* If the count of the sender's xattrs is different from our
1606 +        * (receiver's) xattrs, the lists are not the same. */
1607 +       if (snd_cnt != rec_cnt) {
1608 +               if (!find_all)
1609 +                       return 1;
1610 +               xattrs_equal = 0;
1611 +       }
1612 +
1613 +       while (snd_cnt) {
1614 +               cmp = rec_cnt ? strcmp(snd_rxa->name, rec_rxa->name) : -1;
1615 +               if (cmp > 0)
1616 +                       same = 0;
1617 +               else if (snd_rxa->datum_len > MAX_FULL_DATUM) {
1618 +                       same = cmp == 0 && snd_rxa->datum_len == rec_rxa->datum_len
1619 +                           && memcmp(snd_rxa->datum + 1, rec_rxa->datum + 1,
1620 +                                     MAX_DIGEST_LEN) == 0;
1621 +                       /* Flag unrequested items that we need. */
1622 +                       if (!same && find_all && snd_rxa->datum[0] == XSTATE_ABBREV)
1623 +                               snd_rxa->datum[0] = XSTATE_TODO;
1624 +               } else {
1625 +                       same = cmp == 0 && snd_rxa->datum_len == rec_rxa->datum_len
1626 +                           && memcmp(snd_rxa->datum, rec_rxa->datum,
1627 +                                     snd_rxa->datum_len) == 0;
1628 +               }
1629 +               if (!same) {
1630 +                       if (!find_all)
1631 +                               return 1;
1632 +                       xattrs_equal = 0;
1633 +               }
1634 +
1635 +               if (cmp <= 0) {
1636 +                       snd_rxa++;
1637 +                       snd_cnt--;
1638 +               }
1639 +               if (cmp >= 0) {
1640 +                       rec_rxa++;
1641 +                       rec_cnt--;
1642 +               }
1643 +       }
1644 +
1645 +       if (rec_cnt)
1646 +               xattrs_equal = 0;
1647 +
1648 +       return !xattrs_equal;
1649 +}
1650 +
1651 +/* When called by the generator with a NULL fname, this tells the sender
1652 + * which abbreviated xattr values we need.  When called by the sender
1653 + * (with a non-NULL fname), we send all the extra xattr data it needs. */
1654 +void send_xattr_request(const char *fname, struct file_struct *file, int f_out)
1655 +{
1656 +       item_list *lst = rsync_xal_l.items;
1657 +       int j, cnt, prior_req = -1;
1658 +       rsync_xa *rxa;
1659 +
1660 +       lst += F_XATTR(file);
1661 +       cnt = lst->count;
1662 +       for (rxa = lst->items, j = 0; j < cnt; rxa++, j++) {
1663 +               if (rxa->datum_len <= MAX_FULL_DATUM)
1664 +                       continue;
1665 +               switch (rxa->datum[0]) {
1666 +               case XSTATE_LOCAL:
1667 +                       /* Items set locally will get cached by receiver. */
1668 +                       rxa->datum[0] = XSTATE_DONE;
1669 +                       continue;
1670 +               case XSTATE_TODO:
1671 +                       break;
1672 +               default:
1673 +                       continue;
1674 +               }
1675 +
1676 +               /* Flag that we handled this abbreviated item. */
1677 +               rxa->datum[0] = XSTATE_DONE;
1678 +
1679 +               write_abbrevint(f_out, j - prior_req);
1680 +               prior_req = j;
1681 +
1682 +               if (fname) {
1683 +                       size_t len = 0;
1684 +                       char *ptr;
1685 +
1686 +                       /* Re-read the long datum. */
1687 +                       if (!(ptr = get_xattr_data(fname, rxa->name, &len, 0)))
1688 +                               continue;
1689 +
1690 +                       write_abbrevint(f_out, len); /* length might have changed! */
1691 +                       write_buf(f_out, ptr, len);
1692 +                       free(ptr);
1693 +               }
1694 +       }
1695 +
1696 +       write_byte(f_out, 0); /* end the list */
1697 +}
1698 +
1699 +/* Any items set locally by the generator that the receiver doesn't
1700 + * get told about get changed back to XSTATE_ABBREV. */
1701 +void xattr_clear_locals(struct file_struct *file)
1702 +{
1703 +       item_list *lst = rsync_xal_l.items;
1704 +       rsync_xa *rxa;
1705 +       int cnt;
1706 +
1707 +       if (F_XATTR(file) < 0)
1708 +               return;
1709 +
1710 +       lst += F_XATTR(file);
1711 +       cnt = lst->count;
1712 +       for (rxa = lst->items; cnt--; rxa++) {
1713 +               if (rxa->datum_len <= MAX_FULL_DATUM)
1714 +                       continue;
1715 +               if (rxa->datum[0] == XSTATE_LOCAL)
1716 +                       rxa->datum[0] = XSTATE_ABBREV;
1717 +       }
1718 +}
1719 +
1720 +/* When called by the sender, read the request from the generator and mark
1721 + * any needed xattrs with a flag that lets us know they need to be sent to
1722 + * the receiver.  When called by the receiver, reads the sent data and
1723 + * stores it in place of its checksum. */
1724 +void recv_xattr_request(struct file_struct *file, int f_in)
1725 +{
1726 +       item_list *lst = rsync_xal_l.items;
1727 +       char *old_datum, *name;
1728 +       rsync_xa *rxa;
1729 +       int rel_pos, cnt;
1730 +
1731 +       if (F_XATTR(file) < 0)
1732 +               exit_cleanup(RERR_STREAMIO); /* XXX */
1733 +       lst += F_XATTR(file);
1734 +
1735 +       cnt = lst->count;
1736 +       rxa = lst->items;
1737 +       rxa -= 1;
1738 +       while ((rel_pos = read_abbrevint(f_in)) != 0) {
1739 +               rxa += rel_pos;
1740 +               cnt -= rel_pos;
1741 +               if (cnt < 0 || rxa->datum_len <= MAX_FULL_DATUM
1742 +                || rxa->datum[0] != XSTATE_ABBREV)
1743 +                       exit_cleanup(RERR_STREAMIO); /* XXX */
1744 +
1745 +               if (am_sender) {
1746 +                       rxa->datum[0] = XSTATE_TODO;
1747 +                       continue;
1748 +               }
1749 +
1750 +               old_datum = rxa->datum;
1751 +               rxa->datum_len = read_abbrevint(f_in);
1752 +
1753 +               if (rxa->name_len + rxa->datum_len < rxa->name_len)
1754 +                       out_of_memory("recv_xattr_request"); /* overflow */
1755 +               rxa->datum = new_array(char, rxa->datum_len + rxa->name_len);
1756 +               if (!rxa->datum)
1757 +                       out_of_memory("recv_xattr_request");
1758 +               name = rxa->datum + rxa->datum_len;
1759 +               memcpy(name, rxa->name, rxa->name_len);
1760 +               rxa->name = name;
1761 +               free(old_datum);
1762 +               read_buf(f_in, rxa->datum, rxa->datum_len);
1763 +       }
1764 +}
1765 +
1766 +/* ------------------------------------------------------------------------- */
1767 +
1768 +/* receive and build the rsync_xattr_lists */
1769 +void receive_xattr(struct file_struct *file, int f)
1770 +{
1771 +       static item_list temp_xattr = EMPTY_ITEM_LIST;
1772 +       int count;
1773 +       int ndx = read_abbrevint(f);
1774 +
1775 +       if (ndx < 0 || (size_t)ndx > rsync_xal_l.count) {
1776 +               rprintf(FERROR, "receive_xattr: xa index %d out of"
1777 +                       " range for %s\n", ndx, f_name(file, NULL));
1778 +               exit_cleanup(RERR_STREAMIO);
1779 +       }
1780 +
1781 +       if (ndx != 0) {
1782 +               F_XATTR(file) = ndx - 1;
1783 +               return;
1784 +       }
1785 +       
1786 +       if ((count = read_abbrevint(f)) != 0) {
1787 +               (void)EXPAND_ITEM_LIST(&temp_xattr, rsync_xa, count);
1788 +               temp_xattr.count = 0;
1789 +       }
1790 +
1791 +       while (count--) {
1792 +               char *ptr, *name;
1793 +               rsync_xa *rxa;
1794 +               size_t name_len = read_abbrevint(f);
1795 +               size_t datum_len = read_abbrevint(f);
1796 +               size_t dget_len = datum_len > MAX_FULL_DATUM ? 1 + MAX_DIGEST_LEN : datum_len;
1797 +#ifdef HAVE_LINUX_XATTRS
1798 +               size_t extra_len = 0;
1799 +#else
1800 +               size_t extra_len = am_root ? RPRE_LEN : 0;
1801 +               if (dget_len + extra_len < dget_len)
1802 +                       out_of_memory("receive_xattr"); /* overflow */
1803 +#endif
1804 +               if (dget_len + extra_len + name_len < dget_len)
1805 +                       out_of_memory("receive_xattr"); /* overflow */
1806 +               ptr = new_array(char, dget_len + extra_len + name_len);
1807 +               if (!ptr)
1808 +                       out_of_memory("receive_xattr");
1809 +               name = ptr + dget_len + extra_len;
1810 +               read_buf(f, name, name_len);
1811 +               if (dget_len == datum_len)
1812 +                       read_buf(f, ptr, dget_len);
1813 +               else {
1814 +                       *ptr = XSTATE_ABBREV;
1815 +                       read_buf(f, ptr + 1, MAX_DIGEST_LEN);
1816 +               }
1817 +#ifdef HAVE_LINUX_XATTRS
1818 +               /* Non-root can only save the user namespace. */
1819 +               if (!am_root && !HAS_PREFIX(name, USER_PREFIX)) {
1820 +                       free(ptr);
1821 +                       continue;
1822 +               }
1823 +#else
1824 +               /* This OS only has a user namespace, so we either
1825 +                * strip the user prefix, or we put a non-user
1826 +                * namespace inside our rsync hierarchy. */
1827 +               if (HAS_PREFIX(name, USER_PREFIX)) {
1828 +                       name += UPRE_LEN;
1829 +                       name_len -= UPRE_LEN;
1830 +               } else if (am_root) {
1831 +                       name -= RPRE_LEN;
1832 +                       name_len += RPRE_LEN;
1833 +                       memcpy(name, RSYNC_PREFIX, RPRE_LEN);
1834 +               } else {
1835 +                       free(ptr);
1836 +                       continue;
1837 +               }
1838 +#endif
1839 +               rxa = EXPAND_ITEM_LIST(&temp_xattr, rsync_xa, 1);
1840 +               rxa->name = name;
1841 +               rxa->datum = ptr;
1842 +               rxa->name_len = name_len;
1843 +               rxa->datum_len = datum_len;
1844 +       }
1845 +
1846 +       ndx = rsync_xal_l.count; /* pre-incremented count */
1847 +       rsync_xal_store(&temp_xattr); /* adds item to rsync_xal_l */
1848 +
1849 +       F_XATTR(file) = ndx;
1850 +}
1851 +
1852 +/* Turn the xattr data in statx into cached xattr data, setting the index
1853 + * values in the file struct. */
1854 +void cache_xattr(struct file_struct *file, statx *sxp)
1855 +{
1856 +       int ndx;
1857 +
1858 +       if (!sxp->xattr)
1859 +               return;
1860 +
1861 +       ndx = find_matching_xattr(sxp->xattr);
1862 +       if (ndx < 0)
1863 +               rsync_xal_store(sxp->xattr); /* adds item to rsync_xal_l */
1864 +
1865 +       F_XATTR(file) = ndx;
1866 +}
1867 +
1868 +static int rsync_xal_set(const char *fname, item_list *xalp,
1869 +                        const char *fnamecmp, statx *sxp)
1870 +{
1871 +       rsync_xa *rxas = xalp->items;
1872 +       ssize_t list_len;
1873 +       size_t i, len;
1874 +       char *name, *ptr, sum[MAX_DIGEST_LEN];
1875 +       int name_len, ret = 0;
1876 +
1877 +       /* This puts the current name list into the "namebuf" buffer. */
1878 +       if ((list_len = get_xattr_names(fname)) < 0)
1879 +               return -1;
1880 +
1881 +       for (i = 0; i < xalp->count; i++) {
1882 +               name = rxas[i].name;
1883 +
1884 +               if (XATTR_ABBREV(rxas[i])) {
1885 +                       /* See if the fnamecmp version is identical. */
1886 +                       len = name_len = rxas[i].name_len;
1887 +                       if ((ptr = get_xattr_data(fnamecmp, name, &len, 1)) == NULL) {
1888 +                         still_abbrev:
1889 +                               if (am_generator)
1890 +                                       continue;
1891 +                               rprintf(FERROR, "Missing abbreviated xattr value, %s, for %s\n",
1892 +                                       rxas[i].name, full_fname(fname));
1893 +                               ret = -1;
1894 +                               continue;
1895 +                       }
1896 +                       if (len != rxas[i].datum_len) {
1897 +                               free(ptr);
1898 +                               goto still_abbrev;
1899 +                       }
1900 +
1901 +                       sum_init(checksum_seed);
1902 +                       sum_update(ptr, len);
1903 +                       sum_end(sum);
1904 +                       if (memcmp(sum, rxas[i].datum + 1, MAX_DIGEST_LEN) != 0) {
1905 +                               free(ptr);
1906 +                               goto still_abbrev;
1907 +                       }
1908 +
1909 +                       if (fname == fnamecmp)
1910 +                               ; /* Value is already set when identical */
1911 +                       else if (sys_lsetxattr(fname, name, ptr, len) < 0) {
1912 +                               rsyserr(FERROR, errno,
1913 +                                       "rsync_xal_set: lsetxattr(\"%s\",\"%s\") failed",
1914 +                                       fname, name);
1915 +                               ret = -1;
1916 +                       } else /* make sure caller sets mtime */
1917 +                               sxp->st.st_mtime = (time_t)-1;
1918 +
1919 +                       if (am_generator) { /* generator items stay abbreviated */
1920 +                               if (rxas[i].datum[0] == XSTATE_ABBREV)
1921 +                                       rxas[i].datum[0] = XSTATE_LOCAL;
1922 +                               free(ptr);
1923 +                               continue;
1924 +                       }
1925 +
1926 +                       memcpy(ptr + len, name, name_len);
1927 +                       free(rxas[i].datum);
1928 +
1929 +                       rxas[i].name = name = ptr + len;
1930 +                       rxas[i].datum = ptr;
1931 +                       continue;
1932 +               }
1933 +
1934 +               if (sys_lsetxattr(fname, name, rxas[i].datum, rxas[i].datum_len) < 0) {
1935 +                       rsyserr(FERROR, errno,
1936 +                               "rsync_xal_set: lsetxattr(\"%s\",\"%s\") failed",
1937 +                               fname, name);
1938 +                       ret = -1;
1939 +               } else /* make sure caller sets mtime */
1940 +                       sxp->st.st_mtime = (time_t)-1;
1941 +       }
1942 +
1943 +       /* Remove any extraneous names. */
1944 +       for (name = namebuf; list_len > 0; name += name_len) {
1945 +               name_len = strlen(name) + 1;
1946 +               list_len -= name_len;
1947 +
1948 +#ifdef HAVE_LINUX_XATTRS
1949 +               /* We always ignore the system namespace, and non-root
1950 +                * ignores everything but the user namespace. */
1951 +               if (am_root ? HAS_PREFIX(name, SYSTEM_PREFIX)
1952 +                           : !HAS_PREFIX(name, USER_PREFIX))
1953 +                       continue;
1954 +#endif
1955 +
1956 +               for (i = 0; i < xalp->count; i++) {
1957 +                       if (strcmp(name, rxas[i].name) == 0)
1958 +                               break;
1959 +               }
1960 +               if (i == xalp->count) {
1961 +                       if (sys_lremovexattr(fname, name) < 0) {
1962 +                               rsyserr(FERROR, errno,
1963 +                                       "rsync_xal_clear: lremovexattr(\"%s\",\"%s\") failed",
1964 +                                       fname, name);
1965 +                               ret = -1;
1966 +                       } else /* make sure caller sets mtime */
1967 +                               sxp->st.st_mtime = (time_t)-1;
1968 +               }
1969 +       }
1970 +
1971 +       return ret;
1972 +}
1973 +
1974 +/* Set extended attributes on indicated filename. */
1975 +int set_xattr(const char *fname, const struct file_struct *file,
1976 +             const char *fnamecmp, statx *sxp)
1977 +{
1978 +       int ndx;
1979 +       item_list *lst = rsync_xal_l.items;
1980 +
1981 +       if (dry_run)
1982 +               return 1; /* FIXME: --dry-run needs to compute this value */
1983 +
1984 +       if (read_only || list_only) {
1985 +               errno = EROFS;
1986 +               return -1;
1987 +       }
1988 +
1989 +       ndx = F_XATTR(file);
1990 +       return rsync_xal_set(fname, lst + ndx, fnamecmp, sxp);
1991 +}
1992 +
1993 +#endif /* SUPPORT_XATTRS */