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-2022 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 extern struct name_num_item
*xattr_sum_nni
;
43 extern int xattr_sum_len
;
45 #define RSYNC_XAL_INITIAL 5
46 #define RSYNC_XAL_LIST_INITIAL 100
48 #define MAX_XATTR_DIGEST_LEN MD5_DIGEST_LEN
49 #define MAX_FULL_DATUM 32
51 #define HAS_PREFIX(str, prfx) (*(str) == *(prfx) && strncmp(str, prfx, sizeof (prfx) - 1) == 0)
53 #define XATTR_ABBREV(x) ((size_t)((x).name - (x).datum) < (x).datum_len)
55 #define XSTATE_ABBREV 1
59 #define USER_PREFIX "user."
60 #define UPRE_LEN ((int)sizeof USER_PREFIX - 1)
61 #define SYSTEM_PREFIX "system."
62 #define SPRE_LEN ((int)sizeof SYSTEM_PREFIX - 1)
64 #ifdef HAVE_LINUX_XATTRS
65 #define MIGHT_NEED_RPRE (am_root <= 0)
66 #define RSYNC_PREFIX USER_PREFIX "rsync."
68 #define MIGHT_NEED_RPRE am_root
69 #define RSYNC_PREFIX "rsync."
71 #define RPRE_LEN ((int)sizeof RSYNC_PREFIX - 1)
73 #define XSTAT_SUFFIX "stat"
74 #define XSTAT_ATTR RSYNC_PREFIX "%" XSTAT_SUFFIX
75 #define XACC_ACL_SUFFIX "aacl"
76 #define XACC_ACL_ATTR RSYNC_PREFIX "%" XACC_ACL_SUFFIX
77 #define XDEF_ACL_SUFFIX "dacl"
78 #define XDEF_ACL_ATTR RSYNC_PREFIX "%" XDEF_ACL_SUFFIX
82 size_t datum_len
, name_len
;
86 struct _rsync_xa_list
;
88 typedef struct _rsync_xa_list_ref
{
89 struct _rsync_xa_list_ref
*next
;
93 typedef struct _rsync_xa_list
{
99 static size_t namebuf_len
= 0;
100 static char *namebuf
= NULL
;
102 static const rsync_xa_list empty_xa_list
= {
103 .xa_items
= EMPTY_ITEM_LIST
,
105 static const item_list empty_xattr
= EMPTY_ITEM_LIST
;
106 static item_list rsync_xal_l
= EMPTY_ITEM_LIST
;
107 static struct hashtable
*rsync_xal_h
= NULL
;
109 static size_t prior_xattr_count
= (size_t)-1;
111 /* ------------------------------------------------------------------------- */
113 static void rsync_xal_free(item_list
*xalp
)
116 rsync_xa
*rxas
= xalp
->items
;
121 for (i
= 0; i
< xalp
->count
; i
++) {
123 /*free(rxas[i].name);*/
128 void free_xattr(stat_x
*sxp
)
132 rsync_xal_free(sxp
->xattr
);
137 static int rsync_xal_compare_names(const void *x1
, const void *x2
)
139 const rsync_xa
*xa1
= x1
;
140 const rsync_xa
*xa2
= x2
;
141 return strcmp(xa1
->name
, xa2
->name
);
144 static ssize_t
get_xattr_names(const char *fname
)
151 namebuf
= new_array(char, namebuf_len
);
155 /* The length returned includes all the '\0' terminators. */
156 list_len
= sys_llistxattr(fname
, namebuf
, namebuf_len
);
158 if ((size_t)list_len
<= namebuf_len
)
160 } else if (errno
== ENOTSUP
)
162 else if (errno
!= ERANGE
) {
165 rsyserr(FERROR_XFER
, errno
,
166 "get_xattr_names: llistxattr(%s,%s) failed",
167 full_fname(fname
), big_num(arg
));
170 list_len
= sys_llistxattr(fname
, NULL
, 0);
177 namebuf_len
= list_len
+ 1024;
178 namebuf
= new_array(char, namebuf_len
);
184 /* On entry, the *len_ptr parameter contains the size of the extra space we
185 * should allocate when we create a buffer for the data. On exit, it contains
186 * the length of the datum. */
187 static char *get_xattr_data(const char *fname
, const char *name
, size_t *len_ptr
, int no_missing_error
)
189 size_t datum_len
= sys_lgetxattr(fname
, name
, NULL
, 0);
190 size_t extra_len
= *len_ptr
;
193 *len_ptr
= datum_len
;
195 if (datum_len
== (size_t)-1) {
196 if (errno
== ENOTSUP
|| no_missing_error
)
198 rsyserr(FERROR_XFER
, errno
,
199 "get_xattr_data: lgetxattr(%s,\"%s\",0) failed",
200 full_fname(fname
), name
);
204 if (!datum_len
&& !extra_len
)
205 extra_len
= 1; /* request non-zero amount of memory */
206 if (SIZE_MAX
- datum_len
< extra_len
)
207 overflow_exit("get_xattr_data");
208 ptr
= new_array(char, datum_len
+ extra_len
);
211 size_t len
= sys_lgetxattr(fname
, name
, ptr
, datum_len
);
212 if (len
!= datum_len
) {
213 if (len
== (size_t)-1) {
214 rsyserr(FERROR_XFER
, errno
,
215 "get_xattr_data: lgetxattr(%s,\"%s\",%ld) failed",
216 full_fname(fname
), name
, (long)datum_len
);
219 "get_xattr_data: lgetxattr(%s,\"%s\",%ld) returned %ld\n",
220 full_fname(fname
), name
,
221 (long)datum_len
, (long)len
);
231 static int rsync_xal_get(const char *fname
, item_list
*xalp
)
233 ssize_t list_len
, name_len
;
234 size_t datum_len
, name_offset
;
236 #ifdef HAVE_LINUX_XATTRS
237 int user_only
= am_sender
? 0 : !am_root
;
242 /* This puts the name list into the "namebuf" buffer. */
243 if ((list_len
= get_xattr_names(fname
)) < 0)
246 for (name
= namebuf
; list_len
> 0; name
+= name_len
) {
247 name_len
= strlen(name
) + 1;
248 list_len
-= name_len
;
250 if (saw_xattr_filter
) {
251 if (name_is_excluded(name
, NAME_IS_XATTR
, ALL_FILTERS
))
254 #ifdef HAVE_LINUX_XATTRS
255 /* Choose between ignoring the system namespace or (non-root) ignoring any non-user namespace. */
256 else if (user_only
? !HAS_PREFIX(name
, USER_PREFIX
) : HAS_PREFIX(name
, SYSTEM_PREFIX
))
260 /* No rsync.%FOO attributes are copied w/o 2 -X options. */
261 if (name_len
> RPRE_LEN
&& name
[RPRE_LEN
] == '%' && HAS_PREFIX(name
, RSYNC_PREFIX
)) {
262 if ((am_sender
&& preserve_xattrs
< 2)
264 && (strcmp(name
+RPRE_LEN
+1, XSTAT_SUFFIX
) == 0
265 || strcmp(name
+RPRE_LEN
+1, XACC_ACL_SUFFIX
) == 0
266 || strcmp(name
+RPRE_LEN
+1, XDEF_ACL_SUFFIX
) == 0)))
270 datum_len
= name_len
; /* Pass extra size to get_xattr_data() */
271 if (!(ptr
= get_xattr_data(fname
, name
, &datum_len
, 0)))
274 if (datum_len
> MAX_FULL_DATUM
) {
275 /* For large datums, we store a flag and a checksum. */
276 name_offset
= 1 + MAX_XATTR_DIGEST_LEN
;
277 sum_init(xattr_sum_nni
, checksum_seed
);
278 sum_update(ptr
, datum_len
);
281 ptr
= new_array(char, name_offset
+ name_len
);
282 *ptr
= XSTATE_ABBREV
;
285 name_offset
= datum_len
;
287 rxa
= EXPAND_ITEM_LIST(xalp
, rsync_xa
, RSYNC_XAL_INITIAL
);
288 rxa
->name
= ptr
+ name_offset
;
289 memcpy(rxa
->name
, name
, name_len
);
291 rxa
->name_len
= name_len
;
292 rxa
->datum_len
= datum_len
;
297 qsort(rxa
, count
, sizeof (rsync_xa
), rsync_xal_compare_names
);
298 for (rxa
+= count
-1; count
; count
--, rxa
--)
303 /* Read the xattr(s) for this filename. */
304 int get_xattr(const char *fname
, stat_x
*sxp
)
306 sxp
->xattr
= new(item_list
);
307 *sxp
->xattr
= empty_xattr
;
309 if (S_ISREG(sxp
->st
.st_mode
) || S_ISDIR(sxp
->st
.st_mode
)) {
310 /* Everyone supports this. */
311 } else if (S_ISLNK(sxp
->st
.st_mode
)) {
312 #ifndef NO_SYMLINK_XATTRS
316 } else if (IS_SPECIAL(sxp
->st
.st_mode
)) {
317 #ifndef NO_SPECIAL_XATTRS
318 if (!preserve_specials
)
321 } else if (IS_DEVICE(sxp
->st
.st_mode
)) {
322 #ifndef NO_DEVICE_XATTRS
323 if (!preserve_devices
)
326 } else if (IS_MISSING_FILE(sxp
->st
))
329 if (rsync_xal_get(fname
, sxp
->xattr
) < 0) {
336 int copy_xattrs(const char *source
, const char *dest
)
338 ssize_t list_len
, name_len
;
341 #ifdef HAVE_LINUX_XATTRS
342 int user_only
= am_sender
? 0 : am_root
<= 0;
345 /* This puts the name list into the "namebuf" buffer. */
346 if ((list_len
= get_xattr_names(source
)) < 0)
349 for (name
= namebuf
; list_len
> 0; name
+= name_len
) {
350 name_len
= strlen(name
) + 1;
351 list_len
-= name_len
;
353 if (saw_xattr_filter
) {
354 if (name_is_excluded(name
, NAME_IS_XATTR
, ALL_FILTERS
))
357 #ifdef HAVE_LINUX_XATTRS
358 /* Choose between ignoring the system namespace or (non-root) ignoring any non-user namespace. */
359 else if (user_only
? !HAS_PREFIX(name
, USER_PREFIX
) : HAS_PREFIX(name
, SYSTEM_PREFIX
))
364 if (!(ptr
= get_xattr_data(source
, name
, &datum_len
, 0)))
366 if (sys_lsetxattr(dest
, name
, ptr
, datum_len
) < 0) {
367 int save_errno
= errno
? errno
: EINVAL
;
368 rsyserr(FERROR_XFER
, errno
,
369 "copy_xattrs: lsetxattr(%s,\"%s\") failed",
370 full_fname(dest
), name
);
380 static int64
xattr_lookup_hash(const item_list
*xalp
)
382 const rsync_xa
*rxas
= xalp
->items
;
384 int64 key
= hashlittle2(&xalp
->count
, sizeof xalp
->count
);
386 for (i
= 0; i
< xalp
->count
; i
++) {
387 key
+= hashlittle2(rxas
[i
].name
, rxas
[i
].name_len
);
388 if (rxas
[i
].datum_len
> MAX_FULL_DATUM
)
389 key
+= hashlittle2(rxas
[i
].datum
, xattr_sum_len
);
391 key
+= hashlittle2(rxas
[i
].datum
, rxas
[i
].datum_len
);
397 static int find_matching_xattr(const item_list
*xalp
)
399 const struct ht_int64_node
*node
;
400 const rsync_xa_list_ref
*ref
;
403 if (rsync_xal_h
== NULL
)
406 key
= xattr_lookup_hash(xalp
);
408 node
= hashtable_find(rsync_xal_h
, key
, NULL
);
412 if (node
->data
== NULL
)
415 for (ref
= node
->data
; ref
!= NULL
; ref
= ref
->next
) {
416 const rsync_xa_list
*ptr
= rsync_xal_l
.items
;
417 const rsync_xa
*rxas1
;
418 const rsync_xa
*rxas2
= xalp
->items
;
422 rxas1
= ptr
->xa_items
.items
;
424 /* Wrong number of elements? */
425 if (ptr
->xa_items
.count
!= xalp
->count
)
427 /* any elements different? */
428 for (j
= 0; j
< xalp
->count
; j
++) {
429 if (rxas1
[j
].name_len
!= rxas2
[j
].name_len
430 || rxas1
[j
].datum_len
!= rxas2
[j
].datum_len
431 || strcmp(rxas1
[j
].name
, rxas2
[j
].name
))
433 if (rxas1
[j
].datum_len
> MAX_FULL_DATUM
) {
434 if (memcmp(rxas1
[j
].datum
+ 1,
439 if (memcmp(rxas1
[j
].datum
, rxas2
[j
].datum
,
444 /* no differences found. This is The One! */
445 if (j
== xalp
->count
)
452 /* Store *xalp on the end of rsync_xal_l */
453 static int rsync_xal_store(item_list
*xalp
)
455 struct ht_int64_node
*node
;
456 int ndx
= rsync_xal_l
.count
; /* pre-incremented count */
457 rsync_xa_list
*new_list
= EXPAND_ITEM_LIST(&rsync_xal_l
, rsync_xa_list
, RSYNC_XAL_LIST_INITIAL
);
458 rsync_xa_list_ref
*new_ref
;
459 /* Since the following call starts a new list, we know it will hold the
460 * entire initial-count, not just enough space for one new item. */
461 *new_list
= empty_xa_list
;
462 (void)EXPAND_ITEM_LIST(&new_list
->xa_items
, rsync_xa
, xalp
->count
);
463 memcpy(new_list
->xa_items
.items
, xalp
->items
, xalp
->count
* sizeof (rsync_xa
));
464 new_list
->xa_items
.count
= xalp
->count
;
468 new_list
->key
= xattr_lookup_hash(&new_list
->xa_items
);
470 if (rsync_xal_h
== NULL
)
471 rsync_xal_h
= hashtable_create(512, HT_KEY64
);
473 new_ref
= new0(rsync_xa_list_ref
);
476 node
= hashtable_find(rsync_xal_h
, new_list
->key
, new_ref
);
477 if (node
->data
!= (void*)new_ref
) {
478 rsync_xa_list_ref
*ref
= node
->data
;
480 while (ref
!= NULL
) {
481 if (ref
->next
!= NULL
) {
494 /* Send the make_xattr()-generated xattr list for this flist entry. */
495 int send_xattr(int f
, stat_x
*sxp
)
497 int ndx
= find_matching_xattr(sxp
->xattr
);
499 /* Send 0 (-1 + 1) to indicate that literal xattr data follows. */
500 write_varint(f
, ndx
+ 1);
504 int count
= sxp
->xattr
->count
;
505 write_varint(f
, count
);
506 for (rxa
= sxp
->xattr
->items
; count
--; rxa
++) {
507 size_t name_len
= rxa
->name_len
;
508 const char *name
= rxa
->name
;
509 /* Strip the rsync prefix from disguised namespaces. */
510 if (name_len
> RPRE_LEN
511 #ifdef HAVE_LINUX_XATTRS
514 && name
[RPRE_LEN
] != '%' && HAS_PREFIX(name
, RSYNC_PREFIX
)) {
516 name_len
-= RPRE_LEN
;
518 #ifndef HAVE_LINUX_XATTRS
520 /* Put everything else in the user namespace. */
521 name_len
+= UPRE_LEN
;
524 write_varint(f
, name_len
);
525 write_varint(f
, rxa
->datum_len
);
526 #ifndef HAVE_LINUX_XATTRS
527 if (name_len
> rxa
->name_len
) {
528 write_buf(f
, USER_PREFIX
, UPRE_LEN
);
529 name_len
-= UPRE_LEN
;
532 write_buf(f
, name
, name_len
);
533 if (rxa
->datum_len
> MAX_FULL_DATUM
)
534 write_buf(f
, rxa
->datum
+ 1, xattr_sum_len
);
536 write_bigbuf(f
, rxa
->datum
, rxa
->datum_len
);
538 ndx
= rsync_xal_store(sxp
->xattr
); /* adds item to rsync_xal_l */
544 /* Return a flag indicating if we need to change a file's xattrs. If
545 * "find_all" is specified, also mark any abbreviated xattrs that we
546 * need so that send_xattr_request() can tell the sender about them. */
547 int xattr_diff(struct file_struct
*file
, stat_x
*sxp
, int find_all
)
549 const rsync_xa_list
*glst
= rsync_xal_l
.items
;
550 const item_list
*lst
;
551 rsync_xa
*snd_rxa
, *rec_rxa
;
552 int snd_cnt
, rec_cnt
;
553 int cmp
, same
, xattrs_equal
= 1;
555 if (sxp
&& XATTR_READY(*sxp
)) {
556 rec_rxa
= sxp
->xattr
->items
;
557 rec_cnt
= sxp
->xattr
->count
;
563 if (F_XATTR(file
) >= 0) {
564 glst
+= F_XATTR(file
);
565 lst
= &glst
->xa_items
;
569 snd_rxa
= lst
->items
;
570 snd_cnt
= lst
->count
;
572 /* If the count of the sender's xattrs is different from our
573 * (receiver's) xattrs, the lists are not the same. */
574 if (snd_cnt
!= rec_cnt
) {
581 cmp
= rec_cnt
? strcmp(snd_rxa
->name
, rec_rxa
->name
) : -1;
584 else if (snd_rxa
->datum_len
> MAX_FULL_DATUM
) {
585 same
= cmp
== 0 && snd_rxa
->datum_len
== rec_rxa
->datum_len
586 && memcmp(snd_rxa
->datum
+ 1, rec_rxa
->datum
+ 1,
588 /* Flag unrequested items that we need. */
589 if (!same
&& find_all
&& snd_rxa
->datum
[0] == XSTATE_ABBREV
)
590 snd_rxa
->datum
[0] = XSTATE_TODO
;
592 same
= cmp
== 0 && snd_rxa
->datum_len
== rec_rxa
->datum_len
593 && memcmp(snd_rxa
->datum
, rec_rxa
->datum
,
594 snd_rxa
->datum_len
) == 0;
615 return !xattrs_equal
;
618 /* When called by the generator (with a NULL fname), this tells the sender
619 * all the abbreviated xattr values we need. When called by the sender
620 * (with a non-NULL fname), we send all the extra xattr data it needs.
621 * The generator may also call with f_out < 0 to just change all the
622 * XSTATE_ABBREV states into XSTATE_DONE. */
623 void send_xattr_request(const char *fname
, struct file_struct
*file
, int f_out
)
625 const rsync_xa_list
*glst
= rsync_xal_l
.items
;
626 const item_list
*lst
;
627 int cnt
, prior_req
= 0;
630 glst
+= F_XATTR(file
);
631 lst
= &glst
->xa_items
;
633 for (rxa
= lst
->items
, cnt
= lst
->count
; cnt
--; rxa
++) {
634 if (rxa
->datum_len
<= MAX_FULL_DATUM
)
636 switch (rxa
->datum
[0]) {
638 /* Items left abbreviated matched the sender's checksum, so
639 * the receiver will cache the local data for future use. */
641 rxa
->datum
[0] = XSTATE_DONE
;
650 /* Flag that we handled this abbreviated item. */
651 rxa
->datum
[0] = XSTATE_DONE
;
653 write_varint(f_out
, rxa
->num
- prior_req
);
654 prior_req
= rxa
->num
;
660 /* Re-read the long datum. */
661 if (!(ptr
= get_xattr_data(fname
, rxa
->name
, &len
, 0))) {
662 rprintf(FERROR_XFER
, "failed to re-read xattr %s for %s\n", rxa
->name
, fname
);
663 write_varint(f_out
, 0);
667 write_varint(f_out
, len
); /* length might have changed! */
668 write_bigbuf(f_out
, ptr
, len
);
674 write_byte(f_out
, 0); /* end the list */
677 /* When called by the sender, read the request from the generator and mark
678 * any needed xattrs with a flag that lets us know they need to be sent to
679 * the receiver. When called by the receiver, reads the sent data and
680 * stores it in place of its checksum. */
681 int recv_xattr_request(struct file_struct
*file
, int f_in
)
683 const rsync_xa_list
*glst
= rsync_xal_l
.items
;
684 const item_list
*lst
;
685 char *old_datum
, *name
;
687 int rel_pos
, cnt
, num
, got_xattr_data
= 0;
689 if (F_XATTR(file
) < 0) {
690 rprintf(FERROR
, "recv_xattr_request: internal data error!\n");
691 exit_cleanup(RERR_PROTOCOL
);
693 glst
+= F_XATTR(file
);
694 lst
= &glst
->xa_items
;
699 while ((rel_pos
= read_varint(f_in
)) != 0) {
702 /* The sender-related num values are only in order on the sender.
703 * We use that order here to scan forward or backward as needed. */
705 while (cnt
< (int)lst
->count
&& rxa
->num
> num
) {
710 while (cnt
> 1 && rxa
->num
< num
) {
717 /* The receiving side has no known num order, so we just scan
718 * forward (w/wrap) and hope that the next value is near by. */
719 for (j
= lst
->count
; j
> 1 && rxa
->num
!= num
; j
--) {
728 if (!cnt
|| rxa
->num
!= num
) {
729 rprintf(FERROR
, "[%s] could not find xattr #%d for %s\n",
730 who_am_i(), num
, f_name(file
, NULL
));
731 exit_cleanup(RERR_PROTOCOL
);
733 if (!XATTR_ABBREV(*rxa
) || rxa
->datum
[0] != XSTATE_ABBREV
) {
734 rprintf(FERROR
, "[%s] internal abbrev error on %s (%s, len=%ld)!\n",
735 who_am_i(), f_name(file
, NULL
), rxa
->name
, (long)rxa
->datum_len
);
736 exit_cleanup(RERR_PROTOCOL
);
740 rxa
->datum
[0] = XSTATE_TODO
;
744 old_datum
= rxa
->datum
;
745 rxa
->datum_len
= read_varint(f_in
);
747 if (SIZE_MAX
- rxa
->name_len
< rxa
->datum_len
)
748 overflow_exit("recv_xattr_request");
749 rxa
->datum
= new_array(char, rxa
->datum_len
+ rxa
->name_len
);
750 name
= rxa
->datum
+ rxa
->datum_len
;
751 memcpy(name
, rxa
->name
, rxa
->name_len
);
754 read_buf(f_in
, rxa
->datum
, rxa
->datum_len
);
758 return got_xattr_data
;
761 /* ------------------------------------------------------------------------- */
763 /* receive and build the rsync_xattr_lists */
764 void receive_xattr(int f
, struct file_struct
*file
)
766 static item_list temp_xattr
= EMPTY_ITEM_LIST
;
768 #ifdef HAVE_LINUX_XATTRS
773 int ndx
= read_varint(f
);
775 if (ndx
< 0 || (size_t)ndx
> rsync_xal_l
.count
) {
776 rprintf(FERROR
, "receive_xattr: xa index %d out of"
777 " range for %s\n", ndx
, f_name(file
, NULL
));
778 exit_cleanup(RERR_STREAMIO
);
782 F_XATTR(file
) = ndx
- 1;
786 if ((count
= read_varint(f
)) != 0) {
787 (void)EXPAND_ITEM_LIST(&temp_xattr
, rsync_xa
, count
);
788 temp_xattr
.count
= 0;
791 for (num
= 1; num
<= count
; num
++) {
794 size_t name_len
= read_varint(f
);
795 size_t datum_len
= read_varint(f
);
796 size_t dget_len
= datum_len
> MAX_FULL_DATUM
? 1 + (size_t)xattr_sum_len
: datum_len
;
797 size_t extra_len
= MIGHT_NEED_RPRE
? RPRE_LEN
: 0;
798 if (SIZE_MAX
- dget_len
< extra_len
|| SIZE_MAX
- dget_len
- extra_len
< name_len
)
799 overflow_exit("receive_xattr");
800 ptr
= new_array(char, dget_len
+ extra_len
+ name_len
);
801 name
= ptr
+ dget_len
+ extra_len
;
802 read_buf(f
, name
, name_len
);
803 if (name_len
< 1 || name
[name_len
-1] != '\0') {
804 rprintf(FERROR
, "Invalid xattr name received (missing trailing \\0).\n");
805 exit_cleanup(RERR_FILEIO
);
807 if (dget_len
== datum_len
)
808 read_buf(f
, ptr
, dget_len
);
810 *ptr
= XSTATE_ABBREV
;
811 read_buf(f
, ptr
+ 1, xattr_sum_len
);
814 if (saw_xattr_filter
) {
815 if (name_is_excluded(name
, NAME_IS_XATTR
, ALL_FILTERS
)) {
820 #ifdef HAVE_LINUX_XATTRS
821 /* Non-root can only save the user namespace. */
822 if (am_root
<= 0 && !HAS_PREFIX(name
, USER_PREFIX
)) {
823 if (!am_root
&& !saw_xattr_filter
) {
828 name_len
+= RPRE_LEN
;
829 memcpy(name
, RSYNC_PREFIX
, RPRE_LEN
);
833 /* This OS only has a user namespace, so we either
834 * strip the user prefix, or we put a non-user
835 * namespace inside our rsync hierarchy. */
836 if (HAS_PREFIX(name
, USER_PREFIX
)) {
838 name_len
-= UPRE_LEN
;
839 } else if (am_root
) {
841 name_len
+= RPRE_LEN
;
842 memcpy(name
, RSYNC_PREFIX
, RPRE_LEN
);
848 /* No rsync.%FOO attributes are copied w/o 2 -X options. */
849 if (preserve_xattrs
< 2 && name_len
> RPRE_LEN
850 && name
[RPRE_LEN
] == '%' && HAS_PREFIX(name
, RSYNC_PREFIX
)) {
855 rxa
= EXPAND_ITEM_LIST(&temp_xattr
, rsync_xa
, 1);
858 rxa
->name_len
= name_len
;
859 rxa
->datum_len
= datum_len
;
863 if (need_sort
&& count
> 1)
864 qsort(temp_xattr
.items
, count
, sizeof (rsync_xa
), rsync_xal_compare_names
);
866 ndx
= rsync_xal_store(&temp_xattr
); /* adds item to rsync_xal_l */
871 /* Turn the xattr data in stat_x into cached xattr data, setting the index
872 * values in the file struct. */
873 void cache_tmp_xattr(struct file_struct
*file
, stat_x
*sxp
)
880 if (prior_xattr_count
== (size_t)-1)
881 prior_xattr_count
= rsync_xal_l
.count
;
882 ndx
= find_matching_xattr(sxp
->xattr
);
884 rsync_xal_store(sxp
->xattr
); /* adds item to rsync_xal_l */
889 void uncache_tmp_xattrs(void)
891 if (prior_xattr_count
!= (size_t)-1) {
892 rsync_xa_list
*xa_list_item
= rsync_xal_l
.items
;
893 rsync_xa_list
*xa_list_start
= xa_list_item
+ prior_xattr_count
;
894 xa_list_item
+= rsync_xal_l
.count
;
895 rsync_xal_l
.count
= prior_xattr_count
;
896 while (xa_list_item
-- > xa_list_start
) {
897 struct ht_int64_node
*node
;
898 rsync_xa_list_ref
*ref
;
900 rsync_xal_free(&xa_list_item
->xa_items
);
902 if (rsync_xal_h
== NULL
)
905 node
= hashtable_find(rsync_xal_h
, xa_list_item
->key
, NULL
);
909 if (node
->data
== NULL
)
913 if (xa_list_item
->ndx
== ref
->ndx
) {
914 /* xa_list_item is the first in the list. */
915 node
->data
= ref
->next
;
921 rsync_xa_list_ref
*next
= ref
->next
;
924 if (xa_list_item
->ndx
== next
->ndx
) {
925 ref
->next
= next
->next
;
932 prior_xattr_count
= (size_t)-1;
936 static int rsync_xal_set(const char *fname
, item_list
*xalp
,
937 const char *fnamecmp
, stat_x
*sxp
)
939 rsync_xa
*rxas
= xalp
->items
;
942 char *name
, *ptr
, sum
[MAX_XATTR_DIGEST_LEN
];
943 #ifdef HAVE_LINUX_XATTRS
944 int user_only
= am_root
<= 0;
949 /* This puts the current name list into the "namebuf" buffer. */
950 if ((list_len
= get_xattr_names(fname
)) < 0)
953 for (i
= 0; i
< xalp
->count
; i
++) {
956 if (XATTR_ABBREV(rxas
[i
])) {
957 /* See if the fnamecmp version is identical. */
958 len
= name_len
= rxas
[i
].name_len
;
959 if ((ptr
= get_xattr_data(fnamecmp
, name
, &len
, 1)) == NULL
) {
963 rprintf(FERROR
, "Missing abbreviated xattr value, %s, for %s\n",
964 rxas
[i
].name
, full_fname(fname
));
968 if (len
!= rxas
[i
].datum_len
) {
973 sum_init(xattr_sum_nni
, checksum_seed
);
974 sum_update(ptr
, len
);
976 if (memcmp(sum
, rxas
[i
].datum
+ 1, xattr_sum_len
) != 0) {
981 if (fname
== fnamecmp
)
982 ; /* Value is already set when identical */
983 else if (sys_lsetxattr(fname
, name
, ptr
, len
) < 0) {
984 rsyserr(FERROR_XFER
, errno
,
985 "rsync_xal_set: lsetxattr(%s,\"%s\") failed",
986 full_fname(fname
), name
);
988 } else /* make sure caller sets mtime */
989 sxp
->st
.st_mtime
= (time_t)-1;
991 if (am_generator
) { /* generator items stay abbreviated */
996 memcpy(ptr
+ len
, name
, name_len
);
999 rxas
[i
].name
= name
= ptr
+ len
;
1000 rxas
[i
].datum
= ptr
;
1004 if (sys_lsetxattr(fname
, name
, rxas
[i
].datum
, rxas
[i
].datum_len
) < 0) {
1005 rsyserr(FERROR_XFER
, errno
,
1006 "rsync_xal_set: lsetxattr(%s,\"%s\") failed",
1007 full_fname(fname
), name
);
1009 } else /* make sure caller sets mtime */
1010 sxp
->st
.st_mtime
= (time_t)-1;
1013 /* Remove any extraneous names. */
1014 for (name
= namebuf
; list_len
> 0; name
+= name_len
) {
1015 name_len
= strlen(name
) + 1;
1016 list_len
-= name_len
;
1018 if (saw_xattr_filter
) {
1019 if (name_is_excluded(name
, NAME_IS_XATTR
, ALL_FILTERS
))
1022 #ifdef HAVE_LINUX_XATTRS
1023 /* Choose between ignoring the system namespace or (non-root) ignoring any non-user namespace. */
1024 else if (user_only
? !HAS_PREFIX(name
, USER_PREFIX
) : HAS_PREFIX(name
, SYSTEM_PREFIX
))
1027 if (am_root
< 0 && name_len
> RPRE_LEN
&& name
[RPRE_LEN
] == '%' && strcmp(name
, XSTAT_ATTR
) == 0)
1030 for (i
= 0; i
< xalp
->count
; i
++) {
1031 if (strcmp(name
, rxas
[i
].name
) == 0)
1034 if (i
== xalp
->count
) {
1035 if (sys_lremovexattr(fname
, name
) < 0) {
1036 rsyserr(FERROR_XFER
, errno
,
1037 "rsync_xal_set: lremovexattr(%s,\"%s\") failed",
1038 full_fname(fname
), name
);
1040 } else /* make sure caller sets mtime */
1041 sxp
->st
.st_mtime
= (time_t)-1;
1048 /* Set extended attributes on indicated filename. */
1049 int set_xattr(const char *fname
, const struct file_struct
*file
, const char *fnamecmp
, stat_x
*sxp
)
1051 rsync_xa_list
*glst
= rsync_xal_l
.items
;
1053 int ndx
, added_write_perm
= 0;
1056 return 1; /* FIXME: --dry-run needs to compute this value */
1058 if (read_only
|| list_only
) {
1063 #ifdef NO_SPECIAL_XATTRS
1064 if (IS_SPECIAL(sxp
->st
.st_mode
)) {
1069 #ifdef NO_DEVICE_XATTRS
1070 if (IS_DEVICE(sxp
->st
.st_mode
)) {
1075 #ifdef NO_SYMLINK_XATTRS
1076 if (S_ISLNK(sxp
->st
.st_mode
)) {
1082 /* If the target file lacks write permission, we try to add it
1083 * temporarily so we can change the extended attributes. */
1085 #ifdef SUPPORT_LINKS
1086 && !S_ISLNK(sxp
->st
.st_mode
)
1088 && access(fname
, W_OK
) < 0
1089 && do_chmod(fname
, (sxp
->st
.st_mode
& CHMOD_BITS
) | S_IWUSR
) == 0)
1090 added_write_perm
= 1;
1092 ndx
= F_XATTR(file
);
1094 lst
= &glst
->xa_items
;
1095 int return_value
= rsync_xal_set(fname
, lst
, fnamecmp
, sxp
);
1096 if (added_write_perm
) /* remove the temporary write permission */
1097 do_chmod(fname
, sxp
->st
.st_mode
);
1098 return return_value
;
1102 char *get_xattr_acl(const char *fname
, int is_access_acl
, size_t *len_p
)
1104 const char *name
= is_access_acl
? XACC_ACL_ATTR
: XDEF_ACL_ATTR
;
1105 *len_p
= 0; /* no extra data alloc needed from get_xattr_data() */
1106 return get_xattr_data(fname
, name
, len_p
, 1);
1109 int set_xattr_acl(const char *fname
, int is_access_acl
, const char *buf
, size_t buf_len
)
1111 const char *name
= is_access_acl
? XACC_ACL_ATTR
: XDEF_ACL_ATTR
;
1112 if (sys_lsetxattr(fname
, name
, buf
, buf_len
) < 0) {
1113 rsyserr(FERROR_XFER
, errno
,
1114 "set_xattr_acl: lsetxattr(%s,\"%s\") failed",
1115 full_fname(fname
), name
);
1121 int del_def_xattr_acl(const char *fname
)
1123 return sys_lremovexattr(fname
, XDEF_ACL_ATTR
);
1127 int get_stat_xattr(const char *fname
, int fd
, STRUCT_STAT
*fst
, STRUCT_STAT
*xst
)
1130 int rdev_major
, rdev_minor
, uid
, gid
, len
;
1133 if (am_root
>= 0 || IS_DEVICE(fst
->st_mode
) || IS_SPECIAL(fst
->st_mode
))
1142 len
= sys_lgetxattr(fname
, XSTAT_ATTR
, buf
, sizeof buf
- 1);
1145 len
= sys_fgetxattr(fd
, XSTAT_ATTR
, buf
, sizeof buf
- 1);
1147 if (len
>= (int)sizeof buf
) {
1152 if (errno
== ENOTSUP
|| errno
== ENOATTR
)
1154 if (errno
== EPERM
&& S_ISLNK(fst
->st_mode
)) {
1159 rsyserr(FERROR_XFER
, errno
, "failed to read xattr %s for %s",
1160 XSTAT_ATTR
, full_fname(fname
));
1165 if (sscanf(buf
, "%o %d,%d %d:%d",
1166 &mode
, &rdev_major
, &rdev_minor
, &uid
, &gid
) != 5) {
1167 rprintf(FERROR
, "Corrupt %s xattr attached to %s: \"%s\"\n",
1168 XSTAT_ATTR
, full_fname(fname
), buf
);
1169 exit_cleanup(RERR_FILEIO
);
1172 xst
->st_mode
= from_wire_mode(mode
);
1173 xst
->st_rdev
= MAKEDEV(rdev_major
, rdev_minor
);
1180 int set_stat_xattr(const char *fname
, struct file_struct
*file
, mode_t new_mode
)
1182 STRUCT_STAT fst
, xst
;
1189 if (read_only
|| list_only
) {
1190 rsyserr(FERROR_XFER
, EROFS
, "failed to write xattr %s for %s",
1191 XSTAT_ATTR
, full_fname(fname
));
1195 if (x_lstat(fname
, &fst
, &xst
) < 0) {
1196 rsyserr(FERROR_XFER
, errno
, "failed to re-stat %s",
1201 fst
.st_mode
&= (_S_IFMT
| CHMOD_BITS
);
1202 fmode
= new_mode
& (_S_IFMT
| CHMOD_BITS
);
1204 if (IS_DEVICE(fmode
)) {
1205 uint32
*devp
= F_RDEV_P(file
);
1206 rdev
= MAKEDEV(DEV_MAJOR(devp
), DEV_MINOR(devp
));
1210 /* Dump the special permissions and enable full owner access. */
1211 mode
= (fst
.st_mode
& _S_IFMT
) | (fmode
& ACCESSPERMS
)
1212 | (S_ISDIR(fst
.st_mode
) ? 0700 : 0600);
1213 if (fst
.st_mode
!= mode
)
1214 do_chmod(fname
, mode
);
1215 if (!IS_DEVICE(fst
.st_mode
))
1216 fst
.st_rdev
= 0; /* just in case */
1218 if (mode
== fmode
&& fst
.st_rdev
== rdev
1219 && fst
.st_uid
== F_OWNER(file
) && fst
.st_gid
== F_GROUP(file
)) {
1220 /* xst.st_mode will be 0 if there's no current stat xattr */
1221 if (xst
.st_mode
&& sys_lremovexattr(fname
, XSTAT_ATTR
) < 0) {
1222 rsyserr(FERROR_XFER
, errno
,
1223 "delete of stat xattr failed for %s",
1230 if (xst
.st_mode
!= fmode
|| xst
.st_rdev
!= rdev
1231 || xst
.st_uid
!= F_OWNER(file
) || xst
.st_gid
!= F_GROUP(file
)) {
1233 int len
= snprintf(buf
, sizeof buf
, "%o %u,%u %u:%u",
1234 to_wire_mode(fmode
),
1235 (int)major(rdev
), (int)minor(rdev
),
1236 F_OWNER(file
), F_GROUP(file
));
1237 if (sys_lsetxattr(fname
, XSTAT_ATTR
, buf
, len
) < 0) {
1238 if (errno
== EPERM
&& S_ISLNK(fst
.st_mode
))
1240 rsyserr(FERROR_XFER
, errno
,
1241 "failed to write xattr %s for %s",
1242 XSTAT_ATTR
, full_fname(fname
));
1250 int x_stat(const char *fname
, STRUCT_STAT
*fst
, STRUCT_STAT
*xst
)
1252 int ret
= do_stat(fname
, fst
);
1253 if ((ret
< 0 || get_stat_xattr(fname
, -1, fst
, xst
) < 0) && xst
)
1258 int x_lstat(const char *fname
, STRUCT_STAT
*fst
, STRUCT_STAT
*xst
)
1260 int ret
= do_lstat(fname
, fst
);
1261 if ((ret
< 0 || get_stat_xattr(fname
, -1, fst
, xst
) < 0) && xst
)
1266 int x_fstat(int fd
, STRUCT_STAT
*fst
, STRUCT_STAT
*xst
)
1268 int ret
= do_fstat(fd
, fst
);
1269 if ((ret
< 0 || get_stat_xattr(NULL
, fd
, fst
, xst
) < 0) && xst
)
1274 #endif /* SUPPORT_XATTRS */