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