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-2009 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 checksum_seed
;
38 #define RSYNC_XAL_INITIAL 5
39 #define RSYNC_XAL_LIST_INITIAL 100
41 #define MAX_FULL_DATUM 32
43 #define HAS_PREFIX(str, prfx) (*(str) == *(prfx) \
44 && strncmp(str, prfx, sizeof (prfx) - 1) == 0)
46 #define XATTR_ABBREV(x) ((size_t)((x).name - (x).datum) < (x).datum_len)
48 #define XSTATE_ABBREV 1
52 #define USER_PREFIX "user."
53 #define UPRE_LEN ((int)sizeof USER_PREFIX - 1)
54 #define SYSTEM_PREFIX "system."
55 #define SPRE_LEN ((int)sizeof SYSTEM_PREFIX - 1)
57 #ifdef HAVE_LINUX_XATTRS
58 #define MIGHT_NEED_RPRE (am_root < 0)
59 #define RSYNC_PREFIX USER_PREFIX "rsync."
61 #define MIGHT_NEED_RPRE am_root
62 #define RSYNC_PREFIX "rsync."
64 #define RPRE_LEN ((int)sizeof RSYNC_PREFIX - 1)
66 #define XSTAT_SUFFIX "stat"
67 #define XSTAT_ATTR RSYNC_PREFIX "%" XSTAT_SUFFIX
68 #define XACC_ACL_SUFFIX "aacl"
69 #define XACC_ACL_ATTR RSYNC_PREFIX "%" XACC_ACL_SUFFIX
70 #define XDEF_ACL_SUFFIX "dacl"
71 #define XDEF_ACL_ATTR RSYNC_PREFIX "%" XDEF_ACL_SUFFIX
75 size_t datum_len
, name_len
;
79 static size_t namebuf_len
= 0;
80 static char *namebuf
= NULL
;
82 static item_list empty_xattr
= EMPTY_ITEM_LIST
;
83 static item_list rsync_xal_l
= EMPTY_ITEM_LIST
;
85 static size_t prior_xattr_count
= (size_t)-1;
87 /* ------------------------------------------------------------------------- */
89 static void rsync_xal_free(item_list
*xalp
)
92 rsync_xa
*rxas
= xalp
->items
;
94 for (i
= 0; i
< xalp
->count
; i
++) {
96 /*free(rxas[i].name);*/
101 void free_xattr(stat_x
*sxp
)
105 rsync_xal_free(sxp
->xattr
);
110 static int rsync_xal_compare_names(const void *x1
, const void *x2
)
112 const rsync_xa
*xa1
= x1
;
113 const rsync_xa
*xa2
= x2
;
114 return strcmp(xa1
->name
, xa2
->name
);
117 static ssize_t
get_xattr_names(const char *fname
)
124 namebuf
= new_array(char, namebuf_len
);
126 out_of_memory("get_xattr_names");
130 /* The length returned includes all the '\0' terminators. */
131 list_len
= sys_llistxattr(fname
, namebuf
, namebuf_len
);
133 if ((size_t)list_len
<= namebuf_len
)
135 } else if (errno
== ENOTSUP
)
137 else if (errno
!= ERANGE
) {
140 rsyserr(FERROR_XFER
, errno
,
141 "get_xattr_names: llistxattr(\"%s\",%s) failed",
142 fname
, big_num(arg
));
145 list_len
= sys_llistxattr(fname
, NULL
, 0);
152 namebuf_len
= list_len
+ 1024;
153 namebuf
= new_array(char, namebuf_len
);
155 out_of_memory("get_xattr_names");
161 /* On entry, the *len_ptr parameter contains the size of the extra space we
162 * should allocate when we create a buffer for the data. On exit, it contains
163 * the length of the datum. */
164 static char *get_xattr_data(const char *fname
, const char *name
, size_t *len_ptr
,
165 int no_missing_error
)
167 size_t datum_len
= sys_lgetxattr(fname
, name
, NULL
, 0);
168 size_t extra_len
= *len_ptr
;
171 *len_ptr
= datum_len
;
173 if (datum_len
== (size_t)-1) {
174 if (errno
== ENOTSUP
|| no_missing_error
)
176 rsyserr(FERROR_XFER
, errno
,
177 "get_xattr_data: lgetxattr(\"%s\",\"%s\",0) failed",
182 if (!datum_len
&& !extra_len
)
183 extra_len
= 1; /* request non-zero amount of memory */
184 if (datum_len
+ extra_len
< datum_len
)
185 overflow_exit("get_xattr_data");
186 if (!(ptr
= new_array(char, datum_len
+ extra_len
)))
187 out_of_memory("get_xattr_data");
190 size_t len
= sys_lgetxattr(fname
, name
, ptr
, datum_len
);
191 if (len
!= datum_len
) {
192 if (len
== (size_t)-1) {
193 rsyserr(FERROR_XFER
, errno
,
194 "get_xattr_data: lgetxattr(\"%s\",\"%s\",%ld)"
195 " failed", fname
, name
, (long)datum_len
);
198 "get_xattr_data: lgetxattr(\"%s\",\"%s\",%ld)"
199 " returned %ld\n", fname
, name
,
200 (long)datum_len
, (long)len
);
210 static int rsync_xal_get(const char *fname
, item_list
*xalp
)
212 ssize_t list_len
, name_len
;
213 size_t datum_len
, name_offset
;
215 #ifdef HAVE_LINUX_XATTRS
216 int user_only
= am_sender
? 0 : !am_root
;
221 /* This puts the name list into the "namebuf" buffer. */
222 if ((list_len
= get_xattr_names(fname
)) < 0)
225 for (name
= namebuf
; list_len
> 0; name
+= name_len
) {
226 name_len
= strlen(name
) + 1;
227 list_len
-= name_len
;
229 #ifdef HAVE_LINUX_XATTRS
230 /* We always ignore the system namespace, and non-root
231 * ignores everything but the user namespace. */
232 if (user_only
? !HAS_PREFIX(name
, USER_PREFIX
)
233 : HAS_PREFIX(name
, SYSTEM_PREFIX
))
237 /* No rsync.%FOO attributes are copied w/o 2 -X options. */
238 if (name_len
> RPRE_LEN
&& name
[RPRE_LEN
] == '%'
239 && HAS_PREFIX(name
, RSYNC_PREFIX
)) {
240 if ((am_sender
&& preserve_xattrs
< 2)
242 && (strcmp(name
+RPRE_LEN
+1, XSTAT_SUFFIX
) == 0
243 || strcmp(name
+RPRE_LEN
+1, XACC_ACL_SUFFIX
) == 0
244 || strcmp(name
+RPRE_LEN
+1, XDEF_ACL_SUFFIX
) == 0)))
248 datum_len
= name_len
; /* Pass extra size to get_xattr_data() */
249 if (!(ptr
= get_xattr_data(fname
, name
, &datum_len
, 0)))
252 if (datum_len
> MAX_FULL_DATUM
) {
253 /* For large datums, we store a flag and a checksum. */
254 name_offset
= 1 + MAX_DIGEST_LEN
;
255 sum_init(checksum_seed
);
256 sum_update(ptr
, datum_len
);
259 if (!(ptr
= new_array(char, name_offset
+ name_len
)))
260 out_of_memory("rsync_xal_get");
261 *ptr
= XSTATE_ABBREV
;
264 name_offset
= datum_len
;
266 rxa
= EXPAND_ITEM_LIST(xalp
, rsync_xa
, RSYNC_XAL_INITIAL
);
267 rxa
->name
= ptr
+ name_offset
;
268 memcpy(rxa
->name
, name
, name_len
);
270 rxa
->name_len
= name_len
;
271 rxa
->datum_len
= datum_len
;
276 qsort(rxa
, count
, sizeof (rsync_xa
), rsync_xal_compare_names
);
277 for (rxa
+= count
-1; count
; count
--, rxa
--)
282 /* Read the xattr(s) for this filename. */
283 int get_xattr(const char *fname
, stat_x
*sxp
)
285 sxp
->xattr
= new(item_list
);
286 *sxp
->xattr
= empty_xattr
;
288 #ifdef NO_SPECIAL_XATTRS
289 if (IS_SPECIAL(sxp
->st
.st_mode
))
292 #ifdef NO_DEVICE_XATTRS
293 if (IS_DEVICE(sxp
->st
.st_mode
))
296 #ifdef NO_SYMLINK_XATTRS
297 if (S_ISLNK(sxp
->st
.st_mode
))
301 if (rsync_xal_get(fname
, sxp
->xattr
) < 0) {
308 int copy_xattrs(const char *source
, const char *dest
)
310 ssize_t list_len
, name_len
;
313 #ifdef HAVE_LINUX_XATTRS
314 int user_only
= am_sender
? 0 : am_root
<= 0;
317 /* This puts the name list into the "namebuf" buffer. */
318 if ((list_len
= get_xattr_names(source
)) < 0)
321 for (name
= namebuf
; list_len
> 0; name
+= name_len
) {
322 name_len
= strlen(name
) + 1;
323 list_len
-= name_len
;
325 #ifdef HAVE_LINUX_XATTRS
326 /* We always ignore the system namespace, and non-root
327 * ignores everything but the user namespace. */
328 if (user_only
? !HAS_PREFIX(name
, USER_PREFIX
)
329 : HAS_PREFIX(name
, SYSTEM_PREFIX
))
334 if (!(ptr
= get_xattr_data(source
, name
, &datum_len
, 0)))
336 if (sys_lsetxattr(dest
, name
, ptr
, datum_len
) < 0) {
337 int save_errno
= errno
? errno
: EINVAL
;
338 rsyserr(FERROR_XFER
, errno
,
339 "copy_xattrs: lsetxattr(\"%s\",\"%s\") failed",
350 static int find_matching_xattr(item_list
*xalp
)
353 item_list
*lst
= rsync_xal_l
.items
;
355 for (i
= 0; i
< rsync_xal_l
.count
; i
++) {
356 rsync_xa
*rxas1
= lst
[i
].items
;
357 rsync_xa
*rxas2
= xalp
->items
;
359 /* Wrong number of elements? */
360 if (lst
[i
].count
!= xalp
->count
)
362 /* any elements different? */
363 for (j
= 0; j
< xalp
->count
; j
++) {
364 if (rxas1
[j
].name_len
!= rxas2
[j
].name_len
365 || rxas1
[j
].datum_len
!= rxas2
[j
].datum_len
366 || strcmp(rxas1
[j
].name
, rxas2
[j
].name
))
368 if (rxas1
[j
].datum_len
> MAX_FULL_DATUM
) {
369 if (memcmp(rxas1
[j
].datum
+ 1,
371 MAX_DIGEST_LEN
) != 0)
374 if (memcmp(rxas1
[j
].datum
, rxas2
[j
].datum
,
379 /* no differences found. This is The One! */
380 if (j
== xalp
->count
)
387 /* Store *xalp on the end of rsync_xal_l */
388 static void rsync_xal_store(item_list
*xalp
)
390 item_list
*new_lst
= EXPAND_ITEM_LIST(&rsync_xal_l
, item_list
, RSYNC_XAL_LIST_INITIAL
);
391 /* Since the following call starts a new list, we know it will hold the
392 * entire initial-count, not just enough space for one new item. */
393 *new_lst
= empty_xattr
;
394 (void)EXPAND_ITEM_LIST(new_lst
, rsync_xa
, xalp
->count
);
395 memcpy(new_lst
->items
, xalp
->items
, xalp
->count
* sizeof (rsync_xa
));
396 new_lst
->count
= xalp
->count
;
400 /* Send the make_xattr()-generated xattr list for this flist entry. */
401 int send_xattr(int f
, stat_x
*sxp
)
403 int ndx
= find_matching_xattr(sxp
->xattr
);
405 /* Send 0 (-1 + 1) to indicate that literal xattr data follows. */
406 write_varint(f
, ndx
+ 1);
410 int count
= sxp
->xattr
->count
;
411 write_varint(f
, count
);
412 for (rxa
= sxp
->xattr
->items
; count
--; rxa
++) {
413 size_t name_len
= rxa
->name_len
;
414 const char *name
= rxa
->name
;
415 /* Strip the rsync prefix from disguised namespaces. */
416 if (name_len
> RPRE_LEN
417 #ifdef HAVE_LINUX_XATTRS
420 && name
[RPRE_LEN
] != '%' && HAS_PREFIX(name
, RSYNC_PREFIX
)) {
422 name_len
-= RPRE_LEN
;
424 #ifndef HAVE_LINUX_XATTRS
426 /* Put everything else in the user namespace. */
427 name_len
+= UPRE_LEN
;
430 write_varint(f
, name_len
);
431 write_varint(f
, rxa
->datum_len
);
432 #ifndef HAVE_LINUX_XATTRS
433 if (name_len
> rxa
->name_len
) {
434 write_buf(f
, USER_PREFIX
, UPRE_LEN
);
435 name_len
-= UPRE_LEN
;
438 write_buf(f
, name
, name_len
);
439 if (rxa
->datum_len
> MAX_FULL_DATUM
)
440 write_buf(f
, rxa
->datum
+ 1, MAX_DIGEST_LEN
);
442 write_buf(f
, rxa
->datum
, rxa
->datum_len
);
444 ndx
= rsync_xal_l
.count
; /* pre-incremented count */
445 rsync_xal_store(sxp
->xattr
); /* adds item to rsync_xal_l */
451 /* Return a flag indicating if we need to change a file's xattrs. If
452 * "find_all" is specified, also mark any abbreviated xattrs that we
453 * need so that send_xattr_request() can tell the sender about them. */
454 int xattr_diff(struct file_struct
*file
, stat_x
*sxp
, int find_all
)
456 item_list
*lst
= rsync_xal_l
.items
;
457 rsync_xa
*snd_rxa
, *rec_rxa
;
458 int snd_cnt
, rec_cnt
;
459 int cmp
, same
, xattrs_equal
= 1;
461 if (sxp
&& XATTR_READY(*sxp
)) {
462 rec_rxa
= sxp
->xattr
->items
;
463 rec_cnt
= sxp
->xattr
->count
;
469 if (F_XATTR(file
) >= 0)
470 lst
+= F_XATTR(file
);
474 snd_rxa
= lst
->items
;
475 snd_cnt
= lst
->count
;
477 /* If the count of the sender's xattrs is different from our
478 * (receiver's) xattrs, the lists are not the same. */
479 if (snd_cnt
!= rec_cnt
) {
486 cmp
= rec_cnt
? strcmp(snd_rxa
->name
, rec_rxa
->name
) : -1;
489 else if (snd_rxa
->datum_len
> MAX_FULL_DATUM
) {
490 same
= cmp
== 0 && snd_rxa
->datum_len
== rec_rxa
->datum_len
491 && memcmp(snd_rxa
->datum
+ 1, rec_rxa
->datum
+ 1,
492 MAX_DIGEST_LEN
) == 0;
493 /* Flag unrequested items that we need. */
494 if (!same
&& find_all
&& snd_rxa
->datum
[0] == XSTATE_ABBREV
)
495 snd_rxa
->datum
[0] = XSTATE_TODO
;
497 same
= cmp
== 0 && snd_rxa
->datum_len
== rec_rxa
->datum_len
498 && memcmp(snd_rxa
->datum
, rec_rxa
->datum
,
499 snd_rxa
->datum_len
) == 0;
520 return !xattrs_equal
;
523 /* When called by the generator (with a NULL fname), this tells the sender
524 * all the abbreviated xattr values we need. When called by the sender
525 * (with a non-NULL fname), we send all the extra xattr data it needs.
526 * The generator may also call with f_out < 0 to just change all the
527 * XSTATE_ABBREV states into XSTATE_DONE. */
528 void send_xattr_request(const char *fname
, struct file_struct
*file
, int f_out
)
530 item_list
*lst
= rsync_xal_l
.items
;
531 int cnt
, prior_req
= 0;
534 lst
+= F_XATTR(file
);
535 for (rxa
= lst
->items
, cnt
= lst
->count
; cnt
--; rxa
++) {
536 if (rxa
->datum_len
<= MAX_FULL_DATUM
)
538 switch (rxa
->datum
[0]) {
540 /* Items left abbreviated matched the sender's checksum, so
541 * the receiver will cache the local data for future use. */
543 rxa
->datum
[0] = XSTATE_DONE
;
552 /* Flag that we handled this abbreviated item. */
553 rxa
->datum
[0] = XSTATE_DONE
;
555 write_varint(f_out
, rxa
->num
- prior_req
);
556 prior_req
= rxa
->num
;
562 /* Re-read the long datum. */
563 if (!(ptr
= get_xattr_data(fname
, rxa
->name
, &len
, 0))) {
564 rprintf(FERROR_XFER
, "failed to re-read xattr %s for %s\n", rxa
->name
, fname
);
565 write_varint(f_out
, 0);
569 write_varint(f_out
, len
); /* length might have changed! */
570 write_buf(f_out
, ptr
, len
);
576 write_byte(f_out
, 0); /* end the list */
579 /* When called by the sender, read the request from the generator and mark
580 * any needed xattrs with a flag that lets us know they need to be sent to
581 * the receiver. When called by the receiver, reads the sent data and
582 * stores it in place of its checksum. */
583 int recv_xattr_request(struct file_struct
*file
, int f_in
)
585 item_list
*lst
= rsync_xal_l
.items
;
586 char *old_datum
, *name
;
588 int rel_pos
, cnt
, num
, got_xattr_data
= 0;
590 if (F_XATTR(file
) < 0) {
591 rprintf(FERROR
, "recv_xattr_request: internal data error!\n");
592 exit_cleanup(RERR_PROTOCOL
);
594 lst
+= F_XATTR(file
);
599 while ((rel_pos
= read_varint(f_in
)) != 0) {
601 while (cnt
&& rxa
->num
< num
) {
605 if (!cnt
|| rxa
->num
!= num
) {
606 rprintf(FERROR
, "[%s] could not find xattr #%d for %s\n",
607 who_am_i(), num
, f_name(file
, NULL
));
608 exit_cleanup(RERR_PROTOCOL
);
610 if (!XATTR_ABBREV(*rxa
) || rxa
->datum
[0] != XSTATE_ABBREV
) {
611 rprintf(FERROR
, "[%s] internal abbrev error on %s (%s, len=%ld)!\n",
612 who_am_i(), f_name(file
, NULL
), rxa
->name
, (long)rxa
->datum_len
);
613 exit_cleanup(RERR_PROTOCOL
);
617 rxa
->datum
[0] = XSTATE_TODO
;
621 old_datum
= rxa
->datum
;
622 rxa
->datum_len
= read_varint(f_in
);
624 if (rxa
->name_len
+ rxa
->datum_len
< rxa
->name_len
)
625 overflow_exit("recv_xattr_request");
626 rxa
->datum
= new_array(char, rxa
->datum_len
+ rxa
->name_len
);
628 out_of_memory("recv_xattr_request");
629 name
= rxa
->datum
+ rxa
->datum_len
;
630 memcpy(name
, rxa
->name
, rxa
->name_len
);
633 read_buf(f_in
, rxa
->datum
, rxa
->datum_len
);
637 return got_xattr_data
;
640 /* ------------------------------------------------------------------------- */
642 /* receive and build the rsync_xattr_lists */
643 void receive_xattr(int f
, struct file_struct
*file
)
645 static item_list temp_xattr
= EMPTY_ITEM_LIST
;
647 #ifdef HAVE_LINUX_XATTRS
652 int ndx
= read_varint(f
);
654 if (ndx
< 0 || (size_t)ndx
> rsync_xal_l
.count
) {
655 rprintf(FERROR
, "receive_xattr: xa index %d out of"
656 " range for %s\n", ndx
, f_name(file
, NULL
));
657 exit_cleanup(RERR_STREAMIO
);
661 F_XATTR(file
) = ndx
- 1;
665 if ((count
= read_varint(f
)) != 0) {
666 (void)EXPAND_ITEM_LIST(&temp_xattr
, rsync_xa
, count
);
667 temp_xattr
.count
= 0;
670 for (num
= 1; num
<= count
; num
++) {
673 size_t name_len
= read_varint(f
);
674 size_t datum_len
= read_varint(f
);
675 size_t dget_len
= datum_len
> MAX_FULL_DATUM
? 1 + MAX_DIGEST_LEN
: datum_len
;
676 size_t extra_len
= MIGHT_NEED_RPRE
? RPRE_LEN
: 0;
677 if ((dget_len
+ extra_len
< dget_len
)
678 || (dget_len
+ extra_len
+ name_len
< dget_len
))
679 overflow_exit("receive_xattr");
680 ptr
= new_array(char, dget_len
+ extra_len
+ name_len
);
682 out_of_memory("receive_xattr");
683 name
= ptr
+ dget_len
+ extra_len
;
684 read_buf(f
, name
, name_len
);
685 if (dget_len
== datum_len
)
686 read_buf(f
, ptr
, dget_len
);
688 *ptr
= XSTATE_ABBREV
;
689 read_buf(f
, ptr
+ 1, MAX_DIGEST_LEN
);
691 #ifdef HAVE_LINUX_XATTRS
692 /* Non-root can only save the user namespace. */
693 if (am_root
<= 0 && !HAS_PREFIX(name
, USER_PREFIX
)) {
699 name_len
+= RPRE_LEN
;
700 memcpy(name
, RSYNC_PREFIX
, RPRE_LEN
);
704 /* This OS only has a user namespace, so we either
705 * strip the user prefix, or we put a non-user
706 * namespace inside our rsync hierarchy. */
707 if (HAS_PREFIX(name
, USER_PREFIX
)) {
709 name_len
-= UPRE_LEN
;
710 } else if (am_root
) {
712 name_len
+= RPRE_LEN
;
713 memcpy(name
, RSYNC_PREFIX
, RPRE_LEN
);
719 /* No rsync.%FOO attributes are copied w/o 2 -X options. */
720 if (preserve_xattrs
< 2 && name_len
> RPRE_LEN
721 && name
[RPRE_LEN
] == '%' && HAS_PREFIX(name
, RSYNC_PREFIX
)) {
725 rxa
= EXPAND_ITEM_LIST(&temp_xattr
, rsync_xa
, 1);
728 rxa
->name_len
= name_len
;
729 rxa
->datum_len
= datum_len
;
733 if (need_sort
&& count
> 1)
734 qsort(temp_xattr
.items
, count
, sizeof (rsync_xa
), rsync_xal_compare_names
);
736 ndx
= rsync_xal_l
.count
; /* pre-incremented count */
737 rsync_xal_store(&temp_xattr
); /* adds item to rsync_xal_l */
742 /* Turn the xattr data in stat_x into cached xattr data, setting the index
743 * values in the file struct. */
744 void cache_tmp_xattr(struct file_struct
*file
, stat_x
*sxp
)
751 if (prior_xattr_count
== (size_t)-1)
752 prior_xattr_count
= rsync_xal_l
.count
;
753 ndx
= find_matching_xattr(sxp
->xattr
);
755 rsync_xal_store(sxp
->xattr
); /* adds item to rsync_xal_l */
760 void uncache_tmp_xattrs(void)
762 if (prior_xattr_count
!= (size_t)-1) {
763 item_list
*xattr_item
= rsync_xal_l
.items
;
764 item_list
*xattr_start
= xattr_item
+ prior_xattr_count
;
765 xattr_item
+= rsync_xal_l
.count
;
766 rsync_xal_l
.count
= prior_xattr_count
;
767 while (xattr_item
-- > xattr_start
) {
768 rsync_xal_free(xattr_item
);
769 free(xattr_item
->items
);
771 prior_xattr_count
= (size_t)-1;
775 static int rsync_xal_set(const char *fname
, item_list
*xalp
,
776 const char *fnamecmp
, stat_x
*sxp
)
778 rsync_xa
*rxas
= xalp
->items
;
781 char *name
, *ptr
, sum
[MAX_DIGEST_LEN
];
782 #ifdef HAVE_LINUX_XATTRS
783 int user_only
= am_root
<= 0;
788 /* This puts the current name list into the "namebuf" buffer. */
789 if ((list_len
= get_xattr_names(fname
)) < 0)
792 for (i
= 0; i
< xalp
->count
; i
++) {
795 if (XATTR_ABBREV(rxas
[i
])) {
796 /* See if the fnamecmp version is identical. */
797 len
= name_len
= rxas
[i
].name_len
;
798 if ((ptr
= get_xattr_data(fnamecmp
, name
, &len
, 1)) == NULL
) {
802 rprintf(FERROR
, "Missing abbreviated xattr value, %s, for %s\n",
803 rxas
[i
].name
, full_fname(fname
));
807 if (len
!= rxas
[i
].datum_len
) {
812 sum_init(checksum_seed
);
813 sum_update(ptr
, len
);
815 if (memcmp(sum
, rxas
[i
].datum
+ 1, MAX_DIGEST_LEN
) != 0) {
820 if (fname
== fnamecmp
)
821 ; /* Value is already set when identical */
822 else if (sys_lsetxattr(fname
, name
, ptr
, len
) < 0) {
823 rsyserr(FERROR_XFER
, errno
,
824 "rsync_xal_set: lsetxattr(\"%s\",\"%s\") failed",
827 } else /* make sure caller sets mtime */
828 sxp
->st
.st_mtime
= (time_t)-1;
830 if (am_generator
) { /* generator items stay abbreviated */
835 memcpy(ptr
+ len
, name
, name_len
);
838 rxas
[i
].name
= name
= ptr
+ len
;
843 if (sys_lsetxattr(fname
, name
, rxas
[i
].datum
, rxas
[i
].datum_len
) < 0) {
844 rsyserr(FERROR_XFER
, errno
,
845 "rsync_xal_set: lsetxattr(\"%s\",\"%s\") failed",
848 } else /* make sure caller sets mtime */
849 sxp
->st
.st_mtime
= (time_t)-1;
852 /* Remove any extraneous names. */
853 for (name
= namebuf
; list_len
> 0; name
+= name_len
) {
854 name_len
= strlen(name
) + 1;
855 list_len
-= name_len
;
857 #ifdef HAVE_LINUX_XATTRS
858 /* We always ignore the system namespace, and non-root
859 * ignores everything but the user namespace. */
860 if (user_only
? !HAS_PREFIX(name
, USER_PREFIX
)
861 : HAS_PREFIX(name
, SYSTEM_PREFIX
))
864 if (am_root
< 0 && name_len
> RPRE_LEN
865 && name
[RPRE_LEN
] == '%' && strcmp(name
, XSTAT_ATTR
) == 0)
868 for (i
= 0; i
< xalp
->count
; i
++) {
869 if (strcmp(name
, rxas
[i
].name
) == 0)
872 if (i
== xalp
->count
) {
873 if (sys_lremovexattr(fname
, name
) < 0) {
874 rsyserr(FERROR_XFER
, errno
,
875 "rsync_xal_clear: lremovexattr(\"%s\",\"%s\") failed",
878 } else /* make sure caller sets mtime */
879 sxp
->st
.st_mtime
= (time_t)-1;
886 /* Set extended attributes on indicated filename. */
887 int set_xattr(const char *fname
, const struct file_struct
*file
,
888 const char *fnamecmp
, stat_x
*sxp
)
891 item_list
*lst
= rsync_xal_l
.items
;
894 return 1; /* FIXME: --dry-run needs to compute this value */
896 if (read_only
|| list_only
) {
901 #ifdef NO_SPECIAL_XATTRS
902 if (IS_SPECIAL(sxp
->st
.st_mode
)) {
907 #ifdef NO_DEVICE_XATTRS
908 if (IS_DEVICE(sxp
->st
.st_mode
)) {
913 #ifdef NO_SYMLINK_XATTRS
914 if (S_ISLNK(sxp
->st
.st_mode
)) {
921 return rsync_xal_set(fname
, lst
+ ndx
, fnamecmp
, sxp
);
925 char *get_xattr_acl(const char *fname
, int is_access_acl
, size_t *len_p
)
927 const char *name
= is_access_acl
? XACC_ACL_ATTR
: XDEF_ACL_ATTR
;
928 *len_p
= 0; /* no extra data alloc needed from get_xattr_data() */
929 return get_xattr_data(fname
, name
, len_p
, 1);
932 int set_xattr_acl(const char *fname
, int is_access_acl
, const char *buf
, size_t buf_len
)
934 const char *name
= is_access_acl
? XACC_ACL_ATTR
: XDEF_ACL_ATTR
;
935 if (sys_lsetxattr(fname
, name
, buf
, buf_len
) < 0) {
936 rsyserr(FERROR_XFER
, errno
,
937 "set_xattr_acl: lsetxattr(\"%s\",\"%s\") failed",
944 int del_def_xattr_acl(const char *fname
)
946 return sys_lremovexattr(fname
, XDEF_ACL_ATTR
);
950 int get_stat_xattr(const char *fname
, int fd
, STRUCT_STAT
*fst
, STRUCT_STAT
*xst
)
952 int mode
, rdev_major
, rdev_minor
, uid
, gid
, len
;
955 if (am_root
>= 0 || IS_DEVICE(fst
->st_mode
) || IS_SPECIAL(fst
->st_mode
))
964 len
= sys_lgetxattr(fname
, XSTAT_ATTR
, buf
, sizeof buf
- 1);
967 len
= sys_fgetxattr(fd
, XSTAT_ATTR
, buf
, sizeof buf
- 1);
969 if (len
>= (int)sizeof buf
) {
974 if (errno
== ENOTSUP
|| errno
== ENOATTR
)
976 if (errno
== EPERM
&& S_ISLNK(fst
->st_mode
)) {
981 rsyserr(FERROR_XFER
, errno
, "failed to read xattr %s for %s",
982 XSTAT_ATTR
, full_fname(fname
));
987 if (sscanf(buf
, "%o %d,%d %d:%d",
988 &mode
, &rdev_major
, &rdev_minor
, &uid
, &gid
) != 5) {
989 rprintf(FERROR
, "Corrupt %s xattr attached to %s: \"%s\"\n",
990 XSTAT_ATTR
, full_fname(fname
), buf
);
991 exit_cleanup(RERR_FILEIO
);
994 xst
->st_mode
= from_wire_mode(mode
);
995 xst
->st_rdev
= MAKEDEV(rdev_major
, rdev_minor
);
1002 int set_stat_xattr(const char *fname
, struct file_struct
*file
, mode_t new_mode
)
1004 STRUCT_STAT fst
, xst
;
1011 if (read_only
|| list_only
) {
1012 rsyserr(FERROR_XFER
, EROFS
, "failed to write xattr %s for %s",
1013 XSTAT_ATTR
, full_fname(fname
));
1017 if (x_lstat(fname
, &fst
, &xst
) < 0) {
1018 rsyserr(FERROR_XFER
, errno
, "failed to re-stat %s",
1023 fst
.st_mode
&= (_S_IFMT
| CHMOD_BITS
);
1024 fmode
= new_mode
& (_S_IFMT
| CHMOD_BITS
);
1026 if (IS_DEVICE(fmode
)) {
1027 uint32
*devp
= F_RDEV_P(file
);
1028 rdev
= MAKEDEV(DEV_MAJOR(devp
), DEV_MINOR(devp
));
1032 /* Dump the special permissions and enable full owner access. */
1033 mode
= (fst
.st_mode
& _S_IFMT
) | (fmode
& ACCESSPERMS
)
1034 | (S_ISDIR(fst
.st_mode
) ? 0700 : 0600);
1035 if (fst
.st_mode
!= mode
)
1036 do_chmod(fname
, mode
);
1037 if (!IS_DEVICE(fst
.st_mode
))
1038 fst
.st_rdev
= 0; /* just in case */
1040 if (mode
== fmode
&& fst
.st_rdev
== rdev
1041 && fst
.st_uid
== F_OWNER(file
) && fst
.st_gid
== F_GROUP(file
)) {
1042 /* xst.st_mode will be 0 if there's no current stat xattr */
1043 if (xst
.st_mode
&& sys_lremovexattr(fname
, XSTAT_ATTR
) < 0) {
1044 rsyserr(FERROR_XFER
, errno
,
1045 "delete of stat xattr failed for %s",
1052 if (xst
.st_mode
!= fmode
|| xst
.st_rdev
!= rdev
1053 || xst
.st_uid
!= F_OWNER(file
) || xst
.st_gid
!= F_GROUP(file
)) {
1055 int len
= snprintf(buf
, sizeof buf
, "%o %u,%u %u:%u",
1056 to_wire_mode(fmode
),
1057 (int)major(rdev
), (int)minor(rdev
),
1058 F_OWNER(file
), F_GROUP(file
));
1059 if (sys_lsetxattr(fname
, XSTAT_ATTR
, buf
, len
) < 0) {
1060 if (errno
== EPERM
&& S_ISLNK(fst
.st_mode
))
1062 rsyserr(FERROR_XFER
, errno
,
1063 "failed to write xattr %s for %s",
1064 XSTAT_ATTR
, full_fname(fname
));
1072 int x_stat(const char *fname
, STRUCT_STAT
*fst
, STRUCT_STAT
*xst
)
1074 int ret
= do_stat(fname
, fst
);
1075 if ((ret
< 0 || get_stat_xattr(fname
, -1, fst
, xst
) < 0) && xst
)
1080 int x_lstat(const char *fname
, STRUCT_STAT
*fst
, STRUCT_STAT
*xst
)
1082 int ret
= do_lstat(fname
, fst
);
1083 if ((ret
< 0 || get_stat_xattr(fname
, -1, fst
, xst
) < 0) && xst
)
1088 int x_fstat(int fd
, STRUCT_STAT
*fst
, STRUCT_STAT
*xst
)
1090 int ret
= do_fstat(fd
, fst
);
1091 if ((ret
< 0 || get_stat_xattr(NULL
, fd
, fst
, xst
) < 0) && xst
)
1096 #endif /* SUPPORT_XATTRS */