2 * Extended Attribute support for rsync.
3 * Written by Jay Fenlason, vaguely based on the ACLs patch.
5 * Copyright (C) 2004 Red Hat, Inc.
6 * Copyright (C) 2006-2020 Wayne Davison
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 3 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, visit the http://fsf.org website.
25 #include "lib/sysxattrs.h"
32 extern int am_generator
;
35 extern int preserve_xattrs
;
36 extern int preserve_links
;
37 extern int preserve_devices
;
38 extern int preserve_specials
;
39 extern int checksum_seed
;
40 extern int saw_xattr_filter
;
42 #define RSYNC_XAL_INITIAL 5
43 #define RSYNC_XAL_LIST_INITIAL 100
45 #define MAX_FULL_DATUM 32
47 #define HAS_PREFIX(str, prfx) (*(str) == *(prfx) \
48 && strncmp(str, prfx, sizeof (prfx) - 1) == 0)
50 #define XATTR_ABBREV(x) ((size_t)((x).name - (x).datum) < (x).datum_len)
52 #define XSTATE_ABBREV 1
56 #define USER_PREFIX "user."
57 #define UPRE_LEN ((int)sizeof USER_PREFIX - 1)
58 #define SYSTEM_PREFIX "system."
59 #define SPRE_LEN ((int)sizeof SYSTEM_PREFIX - 1)
61 #ifdef HAVE_LINUX_XATTRS
62 #define MIGHT_NEED_RPRE (am_root < 0)
63 #define RSYNC_PREFIX USER_PREFIX "rsync."
65 #define MIGHT_NEED_RPRE am_root
66 #define RSYNC_PREFIX "rsync."
68 #define RPRE_LEN ((int)sizeof RSYNC_PREFIX - 1)
70 #define XSTAT_SUFFIX "stat"
71 #define XSTAT_ATTR RSYNC_PREFIX "%" XSTAT_SUFFIX
72 #define XACC_ACL_SUFFIX "aacl"
73 #define XACC_ACL_ATTR RSYNC_PREFIX "%" XACC_ACL_SUFFIX
74 #define XDEF_ACL_SUFFIX "dacl"
75 #define XDEF_ACL_ATTR RSYNC_PREFIX "%" XDEF_ACL_SUFFIX
79 size_t datum_len
, name_len
;
83 struct _rsync_xa_list
;
85 typedef struct _rsync_xa_list_ref
{
86 struct _rsync_xa_list_ref
*next
;
90 typedef struct _rsync_xa_list
{
96 static size_t namebuf_len
= 0;
97 static char *namebuf
= NULL
;
99 static const rsync_xa_list empty_xa_list
= {
100 .xa_items
= EMPTY_ITEM_LIST
,
102 static const item_list empty_xattr
= EMPTY_ITEM_LIST
;
103 static item_list rsync_xal_l
= EMPTY_ITEM_LIST
;
104 static struct hashtable
*rsync_xal_h
= NULL
;
106 static size_t prior_xattr_count
= (size_t)-1;
108 /* ------------------------------------------------------------------------- */
110 static void rsync_xal_free(item_list
*xalp
)
113 rsync_xa
*rxas
= xalp
->items
;
118 for (i
= 0; i
< xalp
->count
; i
++) {
120 /*free(rxas[i].name);*/
125 void free_xattr(stat_x
*sxp
)
129 rsync_xal_free(sxp
->xattr
);
134 static int rsync_xal_compare_names(const void *x1
, const void *x2
)
136 const rsync_xa
*xa1
= x1
;
137 const rsync_xa
*xa2
= x2
;
138 return strcmp(xa1
->name
, xa2
->name
);
141 static ssize_t
get_xattr_names(const char *fname
)
148 namebuf
= new_array(char, namebuf_len
);
150 out_of_memory("get_xattr_names");
154 /* The length returned includes all the '\0' terminators. */
155 list_len
= sys_llistxattr(fname
, namebuf
, namebuf_len
);
157 if ((size_t)list_len
<= namebuf_len
)
159 } else if (errno
== ENOTSUP
)
161 else if (errno
!= ERANGE
) {
164 rsyserr(FERROR_XFER
, errno
,
165 "get_xattr_names: llistxattr(%s,%s) failed",
166 full_fname(fname
), big_num(arg
));
169 list_len
= sys_llistxattr(fname
, NULL
, 0);
176 namebuf_len
= list_len
+ 1024;
177 namebuf
= new_array(char, namebuf_len
);
179 out_of_memory("get_xattr_names");
185 /* On entry, the *len_ptr parameter contains the size of the extra space we
186 * should allocate when we create a buffer for the data. On exit, it contains
187 * the length of the datum. */
188 static char *get_xattr_data(const char *fname
, const char *name
, size_t *len_ptr
,
189 int no_missing_error
)
191 size_t datum_len
= sys_lgetxattr(fname
, name
, NULL
, 0);
192 size_t extra_len
= *len_ptr
;
195 *len_ptr
= datum_len
;
197 if (datum_len
== (size_t)-1) {
198 if (errno
== ENOTSUP
|| no_missing_error
)
200 rsyserr(FERROR_XFER
, errno
,
201 "get_xattr_data: lgetxattr(%s,\"%s\",0) failed",
202 full_fname(fname
), name
);
206 if (!datum_len
&& !extra_len
)
207 extra_len
= 1; /* request non-zero amount of memory */
208 if (datum_len
+ extra_len
< datum_len
)
209 overflow_exit("get_xattr_data");
210 if (!(ptr
= new_array(char, datum_len
+ extra_len
)))
211 out_of_memory("get_xattr_data");
214 size_t len
= sys_lgetxattr(fname
, name
, ptr
, datum_len
);
215 if (len
!= datum_len
) {
216 if (len
== (size_t)-1) {
217 rsyserr(FERROR_XFER
, errno
,
218 "get_xattr_data: lgetxattr(%s,\"%s\",%ld) failed",
219 full_fname(fname
), name
, (long)datum_len
);
222 "get_xattr_data: lgetxattr(%s,\"%s\",%ld) returned %ld\n",
223 full_fname(fname
), name
,
224 (long)datum_len
, (long)len
);
234 static int rsync_xal_get(const char *fname
, item_list
*xalp
)
236 ssize_t list_len
, name_len
;
237 size_t datum_len
, name_offset
;
239 #ifdef HAVE_LINUX_XATTRS
240 int user_only
= am_sender
? 0 : !am_root
;
245 /* This puts the name list into the "namebuf" buffer. */
246 if ((list_len
= get_xattr_names(fname
)) < 0)
249 for (name
= namebuf
; list_len
> 0; name
+= name_len
) {
250 name_len
= strlen(name
) + 1;
251 list_len
-= name_len
;
253 if (saw_xattr_filter
) {
254 if (name_is_excluded(name
, NAME_IS_XATTR
, ALL_FILTERS
))
257 #ifdef HAVE_LINUX_XATTRS
258 /* Choose between ignoring the system namespace or (non-root) ignoring any non-user namespace. */
259 else if (user_only
? !HAS_PREFIX(name
, USER_PREFIX
) : HAS_PREFIX(name
, SYSTEM_PREFIX
))
263 /* No rsync.%FOO attributes are copied w/o 2 -X options. */
264 if (name_len
> RPRE_LEN
&& name
[RPRE_LEN
] == '%' && HAS_PREFIX(name
, RSYNC_PREFIX
)) {
265 if ((am_sender
&& preserve_xattrs
< 2)
267 && (strcmp(name
+RPRE_LEN
+1, XSTAT_SUFFIX
) == 0
268 || strcmp(name
+RPRE_LEN
+1, XACC_ACL_SUFFIX
) == 0
269 || strcmp(name
+RPRE_LEN
+1, XDEF_ACL_SUFFIX
) == 0)))
273 datum_len
= name_len
; /* Pass extra size to get_xattr_data() */
274 if (!(ptr
= get_xattr_data(fname
, name
, &datum_len
, 0)))
277 if (datum_len
> MAX_FULL_DATUM
) {
278 /* For large datums, we store a flag and a checksum. */
279 name_offset
= 1 + MAX_DIGEST_LEN
;
280 sum_init(-1, checksum_seed
);
281 sum_update(ptr
, datum_len
);
284 if (!(ptr
= new_array(char, name_offset
+ name_len
)))
285 out_of_memory("rsync_xal_get");
286 *ptr
= XSTATE_ABBREV
;
289 name_offset
= datum_len
;
291 rxa
= EXPAND_ITEM_LIST(xalp
, rsync_xa
, RSYNC_XAL_INITIAL
);
292 rxa
->name
= ptr
+ name_offset
;
293 memcpy(rxa
->name
, name
, name_len
);
295 rxa
->name_len
= name_len
;
296 rxa
->datum_len
= datum_len
;
301 qsort(rxa
, count
, sizeof (rsync_xa
), rsync_xal_compare_names
);
302 for (rxa
+= count
-1; count
; count
--, rxa
--)
307 /* Read the xattr(s) for this filename. */
308 int get_xattr(const char *fname
, stat_x
*sxp
)
310 sxp
->xattr
= new(item_list
);
311 *sxp
->xattr
= empty_xattr
;
313 if (S_ISREG(sxp
->st
.st_mode
) || S_ISDIR(sxp
->st
.st_mode
)) {
314 /* Everyone supports this. */
315 } else if (S_ISLNK(sxp
->st
.st_mode
)) {
316 #ifndef NO_SYMLINK_XATTRS
320 } else if (IS_SPECIAL(sxp
->st
.st_mode
)) {
321 #ifndef NO_SPECIAL_XATTRS
322 if (!preserve_specials
)
325 } else if (IS_DEVICE(sxp
->st
.st_mode
)) {
326 #ifndef NO_DEVICE_XATTRS
327 if (!preserve_devices
)
330 } else if (IS_MISSING_FILE(sxp
->st
))
333 if (rsync_xal_get(fname
, sxp
->xattr
) < 0) {
340 int copy_xattrs(const char *source
, const char *dest
)
342 ssize_t list_len
, name_len
;
345 #ifdef HAVE_LINUX_XATTRS
346 int user_only
= am_sender
? 0 : am_root
<= 0;
349 /* This puts the name list into the "namebuf" buffer. */
350 if ((list_len
= get_xattr_names(source
)) < 0)
353 for (name
= namebuf
; list_len
> 0; name
+= name_len
) {
354 name_len
= strlen(name
) + 1;
355 list_len
-= name_len
;
357 if (saw_xattr_filter
) {
358 if (name_is_excluded(name
, NAME_IS_XATTR
, ALL_FILTERS
))
361 #ifdef HAVE_LINUX_XATTRS
362 /* Choose between ignoring the system namespace or (non-root) ignoring any non-user namespace. */
363 else if (user_only
? !HAS_PREFIX(name
, USER_PREFIX
) : HAS_PREFIX(name
, SYSTEM_PREFIX
))
368 if (!(ptr
= get_xattr_data(source
, name
, &datum_len
, 0)))
370 if (sys_lsetxattr(dest
, name
, ptr
, datum_len
) < 0) {
371 int save_errno
= errno
? errno
: EINVAL
;
372 rsyserr(FERROR_XFER
, errno
,
373 "copy_xattrs: lsetxattr(%s,\"%s\") failed",
374 full_fname(dest
), name
);
384 static int64
xattr_lookup_hash(const item_list
*xalp
)
386 const rsync_xa
*rxas
= xalp
->items
;
388 int64 key
= hashlittle(&xalp
->count
, sizeof xalp
->count
);
390 for (i
= 0; i
< xalp
->count
; i
++) {
391 key
+= hashlittle(rxas
[i
].name
, rxas
[i
].name_len
);
392 if (rxas
[i
].datum_len
> MAX_FULL_DATUM
)
393 key
+= hashlittle(rxas
[i
].datum
, MAX_DIGEST_LEN
);
395 key
+= hashlittle(rxas
[i
].datum
, rxas
[i
].datum_len
);
399 /* This is very unlikely, but we should never
400 * return 0 as hashtable_find() doesn't like it. */
407 static int find_matching_xattr(const item_list
*xalp
)
409 const struct ht_int64_node
*node
;
410 const rsync_xa_list_ref
*ref
;
413 if (rsync_xal_h
== NULL
)
416 key
= xattr_lookup_hash(xalp
);
418 node
= hashtable_find(rsync_xal_h
, key
, 0);
422 if (node
->data
== NULL
)
425 for (ref
= node
->data
; ref
!= NULL
; ref
= ref
->next
) {
426 const rsync_xa_list
*ptr
= rsync_xal_l
.items
;
427 const rsync_xa
*rxas1
;
428 const rsync_xa
*rxas2
= xalp
->items
;
432 rxas1
= ptr
->xa_items
.items
;
434 /* Wrong number of elements? */
435 if (ptr
->xa_items
.count
!= xalp
->count
)
437 /* any elements different? */
438 for (j
= 0; j
< xalp
->count
; j
++) {
439 if (rxas1
[j
].name_len
!= rxas2
[j
].name_len
440 || rxas1
[j
].datum_len
!= rxas2
[j
].datum_len
441 || strcmp(rxas1
[j
].name
, rxas2
[j
].name
))
443 if (rxas1
[j
].datum_len
> MAX_FULL_DATUM
) {
444 if (memcmp(rxas1
[j
].datum
+ 1,
446 MAX_DIGEST_LEN
) != 0)
449 if (memcmp(rxas1
[j
].datum
, rxas2
[j
].datum
,
454 /* no differences found. This is The One! */
455 if (j
== xalp
->count
)
462 /* Store *xalp on the end of rsync_xal_l */
463 static int rsync_xal_store(item_list
*xalp
)
465 struct ht_int64_node
*node
;
466 int ndx
= rsync_xal_l
.count
; /* pre-incremented count */
467 rsync_xa_list
*new_list
= EXPAND_ITEM_LIST(&rsync_xal_l
, rsync_xa_list
, RSYNC_XAL_LIST_INITIAL
);
468 rsync_xa_list_ref
*new_ref
;
469 /* Since the following call starts a new list, we know it will hold the
470 * entire initial-count, not just enough space for one new item. */
471 *new_list
= empty_xa_list
;
472 (void)EXPAND_ITEM_LIST(&new_list
->xa_items
, rsync_xa
, xalp
->count
);
473 memcpy(new_list
->xa_items
.items
, xalp
->items
, xalp
->count
* sizeof (rsync_xa
));
474 new_list
->xa_items
.count
= xalp
->count
;
478 new_list
->key
= xattr_lookup_hash(&new_list
->xa_items
);
480 if (rsync_xal_h
== NULL
)
481 rsync_xal_h
= hashtable_create(512, 1);
482 if (rsync_xal_h
== NULL
)
483 out_of_memory("rsync_xal_h hashtable_create()");
485 node
= hashtable_find(rsync_xal_h
, new_list
->key
, 1);
487 out_of_memory("rsync_xal_h hashtable_find()");
489 new_ref
= new0(rsync_xa_list_ref
);
491 out_of_memory("new0(rsync_xa_list_ref)");
495 if (node
->data
!= NULL
) {
496 rsync_xa_list_ref
*ref
= node
->data
;
498 while (ref
!= NULL
) {
499 if (ref
->next
!= NULL
) {
508 node
->data
= new_ref
;
513 /* Send the make_xattr()-generated xattr list for this flist entry. */
514 int send_xattr(int f
, stat_x
*sxp
)
516 int ndx
= find_matching_xattr(sxp
->xattr
);
518 /* Send 0 (-1 + 1) to indicate that literal xattr data follows. */
519 write_varint(f
, ndx
+ 1);
523 int count
= sxp
->xattr
->count
;
524 write_varint(f
, count
);
525 for (rxa
= sxp
->xattr
->items
; count
--; rxa
++) {
526 size_t name_len
= rxa
->name_len
;
527 const char *name
= rxa
->name
;
528 /* Strip the rsync prefix from disguised namespaces. */
529 if (name_len
> RPRE_LEN
530 #ifdef HAVE_LINUX_XATTRS
533 && name
[RPRE_LEN
] != '%' && HAS_PREFIX(name
, RSYNC_PREFIX
)) {
535 name_len
-= RPRE_LEN
;
537 #ifndef HAVE_LINUX_XATTRS
539 /* Put everything else in the user namespace. */
540 name_len
+= UPRE_LEN
;
543 write_varint(f
, name_len
);
544 write_varint(f
, rxa
->datum_len
);
545 #ifndef HAVE_LINUX_XATTRS
546 if (name_len
> rxa
->name_len
) {
547 write_buf(f
, USER_PREFIX
, UPRE_LEN
);
548 name_len
-= UPRE_LEN
;
551 write_buf(f
, name
, name_len
);
552 if (rxa
->datum_len
> MAX_FULL_DATUM
)
553 write_buf(f
, rxa
->datum
+ 1, MAX_DIGEST_LEN
);
555 write_bigbuf(f
, rxa
->datum
, rxa
->datum_len
);
557 ndx
= rsync_xal_store(sxp
->xattr
); /* adds item to rsync_xal_l */
563 /* Return a flag indicating if we need to change a file's xattrs. If
564 * "find_all" is specified, also mark any abbreviated xattrs that we
565 * need so that send_xattr_request() can tell the sender about them. */
566 int xattr_diff(struct file_struct
*file
, stat_x
*sxp
, int find_all
)
568 const rsync_xa_list
*glst
= rsync_xal_l
.items
;
569 const item_list
*lst
;
570 rsync_xa
*snd_rxa
, *rec_rxa
;
571 int snd_cnt
, rec_cnt
;
572 int cmp
, same
, xattrs_equal
= 1;
574 if (sxp
&& XATTR_READY(*sxp
)) {
575 rec_rxa
= sxp
->xattr
->items
;
576 rec_cnt
= sxp
->xattr
->count
;
582 if (F_XATTR(file
) >= 0) {
583 glst
+= F_XATTR(file
);
584 lst
= &glst
->xa_items
;
588 snd_rxa
= lst
->items
;
589 snd_cnt
= lst
->count
;
591 /* If the count of the sender's xattrs is different from our
592 * (receiver's) xattrs, the lists are not the same. */
593 if (snd_cnt
!= rec_cnt
) {
600 cmp
= rec_cnt
? strcmp(snd_rxa
->name
, rec_rxa
->name
) : -1;
603 else if (snd_rxa
->datum_len
> MAX_FULL_DATUM
) {
604 same
= cmp
== 0 && snd_rxa
->datum_len
== rec_rxa
->datum_len
605 && memcmp(snd_rxa
->datum
+ 1, rec_rxa
->datum
+ 1,
606 MAX_DIGEST_LEN
) == 0;
607 /* Flag unrequested items that we need. */
608 if (!same
&& find_all
&& snd_rxa
->datum
[0] == XSTATE_ABBREV
)
609 snd_rxa
->datum
[0] = XSTATE_TODO
;
611 same
= cmp
== 0 && snd_rxa
->datum_len
== rec_rxa
->datum_len
612 && memcmp(snd_rxa
->datum
, rec_rxa
->datum
,
613 snd_rxa
->datum_len
) == 0;
634 return !xattrs_equal
;
637 /* When called by the generator (with a NULL fname), this tells the sender
638 * all the abbreviated xattr values we need. When called by the sender
639 * (with a non-NULL fname), we send all the extra xattr data it needs.
640 * The generator may also call with f_out < 0 to just change all the
641 * XSTATE_ABBREV states into XSTATE_DONE. */
642 void send_xattr_request(const char *fname
, struct file_struct
*file
, int f_out
)
644 const rsync_xa_list
*glst
= rsync_xal_l
.items
;
645 const item_list
*lst
;
646 int cnt
, prior_req
= 0;
649 glst
+= F_XATTR(file
);
650 lst
= &glst
->xa_items
;
652 for (rxa
= lst
->items
, cnt
= lst
->count
; cnt
--; rxa
++) {
653 if (rxa
->datum_len
<= MAX_FULL_DATUM
)
655 switch (rxa
->datum
[0]) {
657 /* Items left abbreviated matched the sender's checksum, so
658 * the receiver will cache the local data for future use. */
660 rxa
->datum
[0] = XSTATE_DONE
;
669 /* Flag that we handled this abbreviated item. */
670 rxa
->datum
[0] = XSTATE_DONE
;
672 write_varint(f_out
, rxa
->num
- prior_req
);
673 prior_req
= rxa
->num
;
679 /* Re-read the long datum. */
680 if (!(ptr
= get_xattr_data(fname
, rxa
->name
, &len
, 0))) {
681 rprintf(FERROR_XFER
, "failed to re-read xattr %s for %s\n", rxa
->name
, fname
);
682 write_varint(f_out
, 0);
686 write_varint(f_out
, len
); /* length might have changed! */
687 write_bigbuf(f_out
, ptr
, len
);
693 write_byte(f_out
, 0); /* end the list */
696 /* When called by the sender, read the request from the generator and mark
697 * any needed xattrs with a flag that lets us know they need to be sent to
698 * the receiver. When called by the receiver, reads the sent data and
699 * stores it in place of its checksum. */
700 int recv_xattr_request(struct file_struct
*file
, int f_in
)
702 const rsync_xa_list
*glst
= rsync_xal_l
.items
;
703 const item_list
*lst
;
704 char *old_datum
, *name
;
706 int rel_pos
, cnt
, num
, got_xattr_data
= 0;
708 if (F_XATTR(file
) < 0) {
709 rprintf(FERROR
, "recv_xattr_request: internal data error!\n");
710 exit_cleanup(RERR_PROTOCOL
);
712 glst
+= F_XATTR(file
);
713 lst
= &glst
->xa_items
;
718 while ((rel_pos
= read_varint(f_in
)) != 0) {
721 /* The sender-related num values are only in order on the sender.
722 * We use that order here to scan forward or backward as needed. */
724 while (cnt
< (int)lst
->count
&& rxa
->num
> num
) {
729 while (cnt
> 1 && rxa
->num
< num
) {
736 /* The receiving side has no known num order, so we just scan
737 * forward (w/wrap) and hope that the next value is near by. */
738 for (j
= lst
->count
; j
> 1 && rxa
->num
!= num
; j
--) {
747 if (!cnt
|| rxa
->num
!= num
) {
748 rprintf(FERROR
, "[%s] could not find xattr #%d for %s\n",
749 who_am_i(), num
, f_name(file
, NULL
));
750 exit_cleanup(RERR_PROTOCOL
);
752 if (!XATTR_ABBREV(*rxa
) || rxa
->datum
[0] != XSTATE_ABBREV
) {
753 rprintf(FERROR
, "[%s] internal abbrev error on %s (%s, len=%ld)!\n",
754 who_am_i(), f_name(file
, NULL
), rxa
->name
, (long)rxa
->datum_len
);
755 exit_cleanup(RERR_PROTOCOL
);
759 rxa
->datum
[0] = XSTATE_TODO
;
763 old_datum
= rxa
->datum
;
764 rxa
->datum_len
= read_varint(f_in
);
766 if (rxa
->name_len
+ rxa
->datum_len
< rxa
->name_len
)
767 overflow_exit("recv_xattr_request");
768 rxa
->datum
= new_array(char, rxa
->datum_len
+ rxa
->name_len
);
770 out_of_memory("recv_xattr_request");
771 name
= rxa
->datum
+ rxa
->datum_len
;
772 memcpy(name
, rxa
->name
, rxa
->name_len
);
775 read_buf(f_in
, rxa
->datum
, rxa
->datum_len
);
779 return got_xattr_data
;
782 /* ------------------------------------------------------------------------- */
784 /* receive and build the rsync_xattr_lists */
785 void receive_xattr(int f
, struct file_struct
*file
)
787 static item_list temp_xattr
= EMPTY_ITEM_LIST
;
789 #ifdef HAVE_LINUX_XATTRS
794 int ndx
= read_varint(f
);
796 if (ndx
< 0 || (size_t)ndx
> rsync_xal_l
.count
) {
797 rprintf(FERROR
, "receive_xattr: xa index %d out of"
798 " range for %s\n", ndx
, f_name(file
, NULL
));
799 exit_cleanup(RERR_STREAMIO
);
803 F_XATTR(file
) = ndx
- 1;
807 if ((count
= read_varint(f
)) != 0) {
808 (void)EXPAND_ITEM_LIST(&temp_xattr
, rsync_xa
, count
);
809 temp_xattr
.count
= 0;
812 for (num
= 1; num
<= count
; num
++) {
815 size_t name_len
= read_varint(f
);
816 size_t datum_len
= read_varint(f
);
817 size_t dget_len
= datum_len
> MAX_FULL_DATUM
? 1 + MAX_DIGEST_LEN
: datum_len
;
818 size_t extra_len
= MIGHT_NEED_RPRE
? RPRE_LEN
: 0;
819 if ((dget_len
+ extra_len
< dget_len
)
820 || (dget_len
+ extra_len
+ name_len
< dget_len
+ extra_len
))
821 overflow_exit("receive_xattr");
822 ptr
= new_array(char, dget_len
+ extra_len
+ name_len
);
824 out_of_memory("receive_xattr");
825 name
= ptr
+ dget_len
+ extra_len
;
826 read_buf(f
, name
, name_len
);
827 if (name_len
< 1 || name
[name_len
-1] != '\0') {
828 rprintf(FERROR
, "Invalid xattr name received (missing trailing \\0).\n");
829 exit_cleanup(RERR_FILEIO
);
831 if (dget_len
== datum_len
)
832 read_buf(f
, ptr
, dget_len
);
834 *ptr
= XSTATE_ABBREV
;
835 read_buf(f
, ptr
+ 1, MAX_DIGEST_LEN
);
838 if (saw_xattr_filter
) {
839 if (name_is_excluded(name
, NAME_IS_XATTR
, ALL_FILTERS
)) {
844 #ifdef HAVE_LINUX_XATTRS
845 /* Non-root can only save the user namespace. */
846 if (am_root
<= 0 && !HAS_PREFIX(name
, USER_PREFIX
)) {
847 if (!am_root
&& !saw_xattr_filter
) {
852 name_len
+= RPRE_LEN
;
853 memcpy(name
, RSYNC_PREFIX
, RPRE_LEN
);
857 /* This OS only has a user namespace, so we either
858 * strip the user prefix, or we put a non-user
859 * namespace inside our rsync hierarchy. */
860 if (HAS_PREFIX(name
, USER_PREFIX
)) {
862 name_len
-= UPRE_LEN
;
863 } else if (am_root
) {
865 name_len
+= RPRE_LEN
;
866 memcpy(name
, RSYNC_PREFIX
, RPRE_LEN
);
872 /* No rsync.%FOO attributes are copied w/o 2 -X options. */
873 if (preserve_xattrs
< 2 && name_len
> RPRE_LEN
874 && name
[RPRE_LEN
] == '%' && HAS_PREFIX(name
, RSYNC_PREFIX
)) {
879 rxa
= EXPAND_ITEM_LIST(&temp_xattr
, rsync_xa
, 1);
882 rxa
->name_len
= name_len
;
883 rxa
->datum_len
= datum_len
;
887 if (need_sort
&& count
> 1)
888 qsort(temp_xattr
.items
, count
, sizeof (rsync_xa
), rsync_xal_compare_names
);
890 ndx
= rsync_xal_store(&temp_xattr
); /* adds item to rsync_xal_l */
895 /* Turn the xattr data in stat_x into cached xattr data, setting the index
896 * values in the file struct. */
897 void cache_tmp_xattr(struct file_struct
*file
, stat_x
*sxp
)
904 if (prior_xattr_count
== (size_t)-1)
905 prior_xattr_count
= rsync_xal_l
.count
;
906 ndx
= find_matching_xattr(sxp
->xattr
);
908 rsync_xal_store(sxp
->xattr
); /* adds item to rsync_xal_l */
913 void uncache_tmp_xattrs(void)
915 if (prior_xattr_count
!= (size_t)-1) {
916 rsync_xa_list
*xa_list_item
= rsync_xal_l
.items
;
917 rsync_xa_list
*xa_list_start
= xa_list_item
+ prior_xattr_count
;
918 xa_list_item
+= rsync_xal_l
.count
;
919 rsync_xal_l
.count
= prior_xattr_count
;
920 while (xa_list_item
-- > xa_list_start
) {
921 struct ht_int64_node
*node
;
922 rsync_xa_list_ref
*ref
;
924 rsync_xal_free(&xa_list_item
->xa_items
);
926 if (rsync_xal_h
== NULL
)
929 node
= hashtable_find(rsync_xal_h
, xa_list_item
->key
, 0);
933 if (node
->data
== NULL
)
937 if (xa_list_item
->ndx
== ref
->ndx
) {
938 /* xa_list_item is the first in the list. */
939 node
->data
= ref
->next
;
944 while (ref
!= NULL
) {
945 if (ref
->next
== NULL
) {
949 if (xa_list_item
->ndx
== ref
->next
->ndx
) {
950 ref
->next
= ref
->next
->next
;
957 prior_xattr_count
= (size_t)-1;
961 static int rsync_xal_set(const char *fname
, item_list
*xalp
,
962 const char *fnamecmp
, stat_x
*sxp
)
964 rsync_xa
*rxas
= xalp
->items
;
967 char *name
, *ptr
, sum
[MAX_DIGEST_LEN
];
968 #ifdef HAVE_LINUX_XATTRS
969 int user_only
= am_root
<= 0;
974 /* This puts the current name list into the "namebuf" buffer. */
975 if ((list_len
= get_xattr_names(fname
)) < 0)
978 for (i
= 0; i
< xalp
->count
; i
++) {
981 if (XATTR_ABBREV(rxas
[i
])) {
983 /* See if the fnamecmp version is identical. */
984 len
= name_len
= rxas
[i
].name_len
;
985 if ((ptr
= get_xattr_data(fnamecmp
, name
, &len
, 1)) == NULL
) {
989 rprintf(FERROR
, "Missing abbreviated xattr value, %s, for %s\n",
990 rxas
[i
].name
, full_fname(fname
));
994 if (len
!= rxas
[i
].datum_len
) {
999 sum_init(-1, checksum_seed
);
1000 sum_update(ptr
, len
);
1001 sum_len
= sum_end(sum
);
1002 if (memcmp(sum
, rxas
[i
].datum
+ 1, sum_len
) != 0) {
1007 if (fname
== fnamecmp
)
1008 ; /* Value is already set when identical */
1009 else if (sys_lsetxattr(fname
, name
, ptr
, len
) < 0) {
1010 rsyserr(FERROR_XFER
, errno
,
1011 "rsync_xal_set: lsetxattr(%s,\"%s\") failed",
1012 full_fname(fname
), name
);
1014 } else /* make sure caller sets mtime */
1015 sxp
->st
.st_mtime
= (time_t)-1;
1017 if (am_generator
) { /* generator items stay abbreviated */
1022 memcpy(ptr
+ len
, name
, name_len
);
1023 free(rxas
[i
].datum
);
1025 rxas
[i
].name
= name
= ptr
+ len
;
1026 rxas
[i
].datum
= ptr
;
1030 if (sys_lsetxattr(fname
, name
, rxas
[i
].datum
, rxas
[i
].datum_len
) < 0) {
1031 rsyserr(FERROR_XFER
, errno
,
1032 "rsync_xal_set: lsetxattr(%s,\"%s\") failed",
1033 full_fname(fname
), name
);
1035 } else /* make sure caller sets mtime */
1036 sxp
->st
.st_mtime
= (time_t)-1;
1039 /* Remove any extraneous names. */
1040 for (name
= namebuf
; list_len
> 0; name
+= name_len
) {
1041 name_len
= strlen(name
) + 1;
1042 list_len
-= name_len
;
1044 if (saw_xattr_filter
) {
1045 if (name_is_excluded(name
, NAME_IS_XATTR
, ALL_FILTERS
))
1048 #ifdef HAVE_LINUX_XATTRS
1049 /* Choose between ignoring the system namespace or (non-root) ignoring any non-user namespace. */
1050 else if (user_only
? !HAS_PREFIX(name
, USER_PREFIX
) : HAS_PREFIX(name
, SYSTEM_PREFIX
))
1053 if (am_root
< 0 && name_len
> RPRE_LEN
&& name
[RPRE_LEN
] == '%' && strcmp(name
, XSTAT_ATTR
) == 0)
1056 for (i
= 0; i
< xalp
->count
; i
++) {
1057 if (strcmp(name
, rxas
[i
].name
) == 0)
1060 if (i
== xalp
->count
) {
1061 if (sys_lremovexattr(fname
, name
) < 0) {
1062 rsyserr(FERROR_XFER
, errno
,
1063 "rsync_xal_set: lremovexattr(%s,\"%s\") failed",
1064 full_fname(fname
), name
);
1066 } else /* make sure caller sets mtime */
1067 sxp
->st
.st_mtime
= (time_t)-1;
1074 /* Set extended attributes on indicated filename. */
1075 int set_xattr(const char *fname
, const struct file_struct
*file
,
1076 const char *fnamecmp
, stat_x
*sxp
)
1078 rsync_xa_list
*glst
= rsync_xal_l
.items
;
1083 return 1; /* FIXME: --dry-run needs to compute this value */
1085 if (read_only
|| list_only
) {
1090 #ifdef NO_SPECIAL_XATTRS
1091 if (IS_SPECIAL(sxp
->st
.st_mode
)) {
1096 #ifdef NO_DEVICE_XATTRS
1097 if (IS_DEVICE(sxp
->st
.st_mode
)) {
1102 #ifdef NO_SYMLINK_XATTRS
1103 if (S_ISLNK(sxp
->st
.st_mode
)) {
1109 ndx
= F_XATTR(file
);
1111 lst
= &glst
->xa_items
;
1112 return rsync_xal_set(fname
, lst
, fnamecmp
, sxp
);
1116 char *get_xattr_acl(const char *fname
, int is_access_acl
, size_t *len_p
)
1118 const char *name
= is_access_acl
? XACC_ACL_ATTR
: XDEF_ACL_ATTR
;
1119 *len_p
= 0; /* no extra data alloc needed from get_xattr_data() */
1120 return get_xattr_data(fname
, name
, len_p
, 1);
1123 int set_xattr_acl(const char *fname
, int is_access_acl
, const char *buf
, size_t buf_len
)
1125 const char *name
= is_access_acl
? XACC_ACL_ATTR
: XDEF_ACL_ATTR
;
1126 if (sys_lsetxattr(fname
, name
, buf
, buf_len
) < 0) {
1127 rsyserr(FERROR_XFER
, errno
,
1128 "set_xattr_acl: lsetxattr(%s,\"%s\") failed",
1129 full_fname(fname
), name
);
1135 int del_def_xattr_acl(const char *fname
)
1137 return sys_lremovexattr(fname
, XDEF_ACL_ATTR
);
1141 int get_stat_xattr(const char *fname
, int fd
, STRUCT_STAT
*fst
, STRUCT_STAT
*xst
)
1143 int mode
, rdev_major
, rdev_minor
, uid
, gid
, len
;
1146 if (am_root
>= 0 || IS_DEVICE(fst
->st_mode
) || IS_SPECIAL(fst
->st_mode
))
1155 len
= sys_lgetxattr(fname
, XSTAT_ATTR
, buf
, sizeof buf
- 1);
1158 len
= sys_fgetxattr(fd
, XSTAT_ATTR
, buf
, sizeof buf
- 1);
1160 if (len
>= (int)sizeof buf
) {
1165 if (errno
== ENOTSUP
|| errno
== ENOATTR
)
1167 if (errno
== EPERM
&& S_ISLNK(fst
->st_mode
)) {
1172 rsyserr(FERROR_XFER
, errno
, "failed to read xattr %s for %s",
1173 XSTAT_ATTR
, full_fname(fname
));
1178 if (sscanf(buf
, "%o %d,%d %d:%d",
1179 &mode
, &rdev_major
, &rdev_minor
, &uid
, &gid
) != 5) {
1180 rprintf(FERROR
, "Corrupt %s xattr attached to %s: \"%s\"\n",
1181 XSTAT_ATTR
, full_fname(fname
), buf
);
1182 exit_cleanup(RERR_FILEIO
);
1185 xst
->st_mode
= from_wire_mode(mode
);
1186 xst
->st_rdev
= MAKEDEV(rdev_major
, rdev_minor
);
1193 int set_stat_xattr(const char *fname
, struct file_struct
*file
, mode_t new_mode
)
1195 STRUCT_STAT fst
, xst
;
1202 if (read_only
|| list_only
) {
1203 rsyserr(FERROR_XFER
, EROFS
, "failed to write xattr %s for %s",
1204 XSTAT_ATTR
, full_fname(fname
));
1208 if (x_lstat(fname
, &fst
, &xst
) < 0) {
1209 rsyserr(FERROR_XFER
, errno
, "failed to re-stat %s",
1214 fst
.st_mode
&= (_S_IFMT
| CHMOD_BITS
);
1215 fmode
= new_mode
& (_S_IFMT
| CHMOD_BITS
);
1217 if (IS_DEVICE(fmode
)) {
1218 uint32
*devp
= F_RDEV_P(file
);
1219 rdev
= MAKEDEV(DEV_MAJOR(devp
), DEV_MINOR(devp
));
1223 /* Dump the special permissions and enable full owner access. */
1224 mode
= (fst
.st_mode
& _S_IFMT
) | (fmode
& ACCESSPERMS
)
1225 | (S_ISDIR(fst
.st_mode
) ? 0700 : 0600);
1226 if (fst
.st_mode
!= mode
)
1227 do_chmod(fname
, mode
);
1228 if (!IS_DEVICE(fst
.st_mode
))
1229 fst
.st_rdev
= 0; /* just in case */
1231 if (mode
== fmode
&& fst
.st_rdev
== rdev
1232 && fst
.st_uid
== F_OWNER(file
) && fst
.st_gid
== F_GROUP(file
)) {
1233 /* xst.st_mode will be 0 if there's no current stat xattr */
1234 if (xst
.st_mode
&& sys_lremovexattr(fname
, XSTAT_ATTR
) < 0) {
1235 rsyserr(FERROR_XFER
, errno
,
1236 "delete of stat xattr failed for %s",
1243 if (xst
.st_mode
!= fmode
|| xst
.st_rdev
!= rdev
1244 || xst
.st_uid
!= F_OWNER(file
) || xst
.st_gid
!= F_GROUP(file
)) {
1246 int len
= snprintf(buf
, sizeof buf
, "%o %u,%u %u:%u",
1247 to_wire_mode(fmode
),
1248 (int)major(rdev
), (int)minor(rdev
),
1249 F_OWNER(file
), F_GROUP(file
));
1250 if (sys_lsetxattr(fname
, XSTAT_ATTR
, buf
, len
) < 0) {
1251 if (errno
== EPERM
&& S_ISLNK(fst
.st_mode
))
1253 rsyserr(FERROR_XFER
, errno
,
1254 "failed to write xattr %s for %s",
1255 XSTAT_ATTR
, full_fname(fname
));
1263 int x_stat(const char *fname
, STRUCT_STAT
*fst
, STRUCT_STAT
*xst
)
1265 int ret
= do_stat(fname
, fst
);
1266 if ((ret
< 0 || get_stat_xattr(fname
, -1, fst
, xst
) < 0) && xst
)
1271 int x_lstat(const char *fname
, STRUCT_STAT
*fst
, STRUCT_STAT
*xst
)
1273 int ret
= do_lstat(fname
, fst
);
1274 if ((ret
< 0 || get_stat_xattr(fname
, -1, fst
, xst
) < 0) && xst
)
1279 int x_fstat(int fd
, STRUCT_STAT
*fst
, STRUCT_STAT
*xst
)
1281 int ret
= do_fstat(fd
, fst
);
1282 if ((ret
< 0 || get_stat_xattr(NULL
, fd
, fst
, xst
) < 0) && xst
)
1287 #endif /* SUPPORT_XATTRS */