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 preserve_links
;
37 extern int preserve_devices
;
38 extern int preserve_specials
;
39 extern int checksum_seed
;
41 #define RSYNC_XAL_INITIAL 5
42 #define RSYNC_XAL_LIST_INITIAL 100
44 #define MAX_FULL_DATUM 32
46 #define HAS_PREFIX(str, prfx) (*(str) == *(prfx) \
47 && strncmp(str, prfx, sizeof (prfx) - 1) == 0)
49 #define XATTR_ABBREV(x) ((size_t)((x).name - (x).datum) < (x).datum_len)
51 #define XSTATE_ABBREV 1
55 #define USER_PREFIX "user."
56 #define UPRE_LEN ((int)sizeof USER_PREFIX - 1)
57 #define SYSTEM_PREFIX "system."
58 #define SPRE_LEN ((int)sizeof SYSTEM_PREFIX - 1)
60 #ifdef HAVE_LINUX_XATTRS
61 #define MIGHT_NEED_RPRE (am_root < 0)
62 #define RSYNC_PREFIX USER_PREFIX "rsync."
64 #define MIGHT_NEED_RPRE am_root
65 #define RSYNC_PREFIX "rsync."
67 #define RPRE_LEN ((int)sizeof RSYNC_PREFIX - 1)
69 #define XSTAT_SUFFIX "stat"
70 #define XSTAT_ATTR RSYNC_PREFIX "%" XSTAT_SUFFIX
71 #define XACC_ACL_SUFFIX "aacl"
72 #define XACC_ACL_ATTR RSYNC_PREFIX "%" XACC_ACL_SUFFIX
73 #define XDEF_ACL_SUFFIX "dacl"
74 #define XDEF_ACL_ATTR RSYNC_PREFIX "%" XDEF_ACL_SUFFIX
78 size_t datum_len
, name_len
;
82 static size_t namebuf_len
= 0;
83 static char *namebuf
= NULL
;
85 static item_list empty_xattr
= EMPTY_ITEM_LIST
;
86 static item_list rsync_xal_l
= EMPTY_ITEM_LIST
;
88 static size_t prior_xattr_count
= (size_t)-1;
90 /* ------------------------------------------------------------------------- */
92 static void rsync_xal_free(item_list
*xalp
)
95 rsync_xa
*rxas
= xalp
->items
;
97 for (i
= 0; i
< xalp
->count
; i
++) {
99 /*free(rxas[i].name);*/
104 void free_xattr(stat_x
*sxp
)
108 rsync_xal_free(sxp
->xattr
);
113 static int rsync_xal_compare_names(const void *x1
, const void *x2
)
115 const rsync_xa
*xa1
= x1
;
116 const rsync_xa
*xa2
= x2
;
117 return strcmp(xa1
->name
, xa2
->name
);
120 static ssize_t
get_xattr_names(const char *fname
)
127 namebuf
= new_array(char, namebuf_len
);
129 out_of_memory("get_xattr_names");
133 /* The length returned includes all the '\0' terminators. */
134 list_len
= sys_llistxattr(fname
, namebuf
, namebuf_len
);
136 if ((size_t)list_len
<= namebuf_len
)
138 } else if (errno
== ENOTSUP
)
140 else if (errno
!= ERANGE
) {
143 rsyserr(FERROR_XFER
, errno
,
144 "get_xattr_names: llistxattr(\"%s\",%s) failed",
145 full_fname(fname
), big_num(arg
));
148 list_len
= sys_llistxattr(fname
, NULL
, 0);
155 namebuf_len
= list_len
+ 1024;
156 namebuf
= new_array(char, namebuf_len
);
158 out_of_memory("get_xattr_names");
164 /* On entry, the *len_ptr parameter contains the size of the extra space we
165 * should allocate when we create a buffer for the data. On exit, it contains
166 * the length of the datum. */
167 static char *get_xattr_data(const char *fname
, const char *name
, size_t *len_ptr
,
168 int no_missing_error
)
170 size_t datum_len
= sys_lgetxattr(fname
, name
, NULL
, 0);
171 size_t extra_len
= *len_ptr
;
174 *len_ptr
= datum_len
;
176 if (datum_len
== (size_t)-1) {
177 if (errno
== ENOTSUP
|| no_missing_error
)
179 rsyserr(FERROR_XFER
, errno
,
180 "get_xattr_data: lgetxattr(\"%s\",\"%s\",0) failed",
181 full_fname(fname
), name
);
185 if (!datum_len
&& !extra_len
)
186 extra_len
= 1; /* request non-zero amount of memory */
187 if (datum_len
+ extra_len
< datum_len
)
188 overflow_exit("get_xattr_data");
189 if (!(ptr
= new_array(char, datum_len
+ extra_len
)))
190 out_of_memory("get_xattr_data");
193 size_t len
= sys_lgetxattr(fname
, name
, ptr
, datum_len
);
194 if (len
!= datum_len
) {
195 if (len
== (size_t)-1) {
196 rsyserr(FERROR_XFER
, errno
,
197 "get_xattr_data: lgetxattr(\"%s\",\"%s\",%ld)"
198 " failed", full_fname(fname
), name
, (long)datum_len
);
201 "get_xattr_data: lgetxattr(\"%s\",\"%s\",%ld)"
202 " returned %ld\n", full_fname(fname
), name
,
203 (long)datum_len
, (long)len
);
213 static int rsync_xal_get(const char *fname
, item_list
*xalp
)
215 ssize_t list_len
, name_len
;
216 size_t datum_len
, name_offset
;
218 #ifdef HAVE_LINUX_XATTRS
219 int user_only
= am_sender
? 0 : !am_root
;
224 /* This puts the name list into the "namebuf" buffer. */
225 if ((list_len
= get_xattr_names(fname
)) < 0)
228 for (name
= namebuf
; list_len
> 0; name
+= name_len
) {
229 name_len
= strlen(name
) + 1;
230 list_len
-= name_len
;
232 #ifdef HAVE_LINUX_XATTRS
233 /* We always ignore the system namespace, and non-root
234 * ignores everything but the user namespace. */
235 if (user_only
? !HAS_PREFIX(name
, USER_PREFIX
)
236 : HAS_PREFIX(name
, SYSTEM_PREFIX
))
240 /* No rsync.%FOO attributes are copied w/o 2 -X options. */
241 if (name_len
> RPRE_LEN
&& name
[RPRE_LEN
] == '%'
242 && HAS_PREFIX(name
, RSYNC_PREFIX
)) {
243 if ((am_sender
&& preserve_xattrs
< 2)
245 && (strcmp(name
+RPRE_LEN
+1, XSTAT_SUFFIX
) == 0
246 || strcmp(name
+RPRE_LEN
+1, XACC_ACL_SUFFIX
) == 0
247 || strcmp(name
+RPRE_LEN
+1, XDEF_ACL_SUFFIX
) == 0)))
251 datum_len
= name_len
; /* Pass extra size to get_xattr_data() */
252 if (!(ptr
= get_xattr_data(fname
, name
, &datum_len
, 0)))
255 if (datum_len
> MAX_FULL_DATUM
) {
256 /* For large datums, we store a flag and a checksum. */
257 name_offset
= 1 + MAX_DIGEST_LEN
;
258 sum_init(checksum_seed
);
259 sum_update(ptr
, datum_len
);
262 if (!(ptr
= new_array(char, name_offset
+ name_len
)))
263 out_of_memory("rsync_xal_get");
264 *ptr
= XSTATE_ABBREV
;
267 name_offset
= datum_len
;
269 rxa
= EXPAND_ITEM_LIST(xalp
, rsync_xa
, RSYNC_XAL_INITIAL
);
270 rxa
->name
= ptr
+ name_offset
;
271 memcpy(rxa
->name
, name
, name_len
);
273 rxa
->name_len
= name_len
;
274 rxa
->datum_len
= datum_len
;
279 qsort(rxa
, count
, sizeof (rsync_xa
), rsync_xal_compare_names
);
280 for (rxa
+= count
-1; count
; count
--, rxa
--)
285 /* Read the xattr(s) for this filename. */
286 int get_xattr(const char *fname
, stat_x
*sxp
)
288 sxp
->xattr
= new(item_list
);
289 *sxp
->xattr
= empty_xattr
;
291 if (S_ISREG(sxp
->st
.st_mode
) || S_ISDIR(sxp
->st
.st_mode
)) {
292 /* Everyone supports this. */
293 } else if (S_ISLNK(sxp
->st
.st_mode
)) {
294 #ifndef NO_SYMLINK_XATTRS
298 } else if (IS_SPECIAL(sxp
->st
.st_mode
)) {
299 #ifndef NO_SPECIAL_XATTRS
300 if (!preserve_specials
)
303 } else if (IS_DEVICE(sxp
->st
.st_mode
)) {
304 #ifndef NO_DEVICE_XATTRS
305 if (!preserve_devices
)
310 if (rsync_xal_get(fname
, sxp
->xattr
) < 0) {
317 int copy_xattrs(const char *source
, const char *dest
)
319 ssize_t list_len
, name_len
;
322 #ifdef HAVE_LINUX_XATTRS
323 int user_only
= am_sender
? 0 : am_root
<= 0;
326 /* This puts the name list into the "namebuf" buffer. */
327 if ((list_len
= get_xattr_names(source
)) < 0)
330 for (name
= namebuf
; list_len
> 0; name
+= name_len
) {
331 name_len
= strlen(name
) + 1;
332 list_len
-= name_len
;
334 #ifdef HAVE_LINUX_XATTRS
335 /* We always ignore the system namespace, and non-root
336 * ignores everything but the user namespace. */
337 if (user_only
? !HAS_PREFIX(name
, USER_PREFIX
)
338 : HAS_PREFIX(name
, SYSTEM_PREFIX
))
343 if (!(ptr
= get_xattr_data(source
, name
, &datum_len
, 0)))
345 if (sys_lsetxattr(dest
, name
, ptr
, datum_len
) < 0) {
346 int save_errno
= errno
? errno
: EINVAL
;
347 rsyserr(FERROR_XFER
, errno
,
348 "copy_xattrs: lsetxattr(\"%s\",\"%s\") failed",
349 full_fname(dest
), name
);
359 static int find_matching_xattr(item_list
*xalp
)
362 item_list
*lst
= rsync_xal_l
.items
;
364 for (i
= 0; i
< rsync_xal_l
.count
; i
++) {
365 rsync_xa
*rxas1
= lst
[i
].items
;
366 rsync_xa
*rxas2
= xalp
->items
;
368 /* Wrong number of elements? */
369 if (lst
[i
].count
!= xalp
->count
)
371 /* any elements different? */
372 for (j
= 0; j
< xalp
->count
; j
++) {
373 if (rxas1
[j
].name_len
!= rxas2
[j
].name_len
374 || rxas1
[j
].datum_len
!= rxas2
[j
].datum_len
375 || strcmp(rxas1
[j
].name
, rxas2
[j
].name
))
377 if (rxas1
[j
].datum_len
> MAX_FULL_DATUM
) {
378 if (memcmp(rxas1
[j
].datum
+ 1,
380 MAX_DIGEST_LEN
) != 0)
383 if (memcmp(rxas1
[j
].datum
, rxas2
[j
].datum
,
388 /* no differences found. This is The One! */
389 if (j
== xalp
->count
)
396 /* Store *xalp on the end of rsync_xal_l */
397 static void rsync_xal_store(item_list
*xalp
)
399 item_list
*new_lst
= EXPAND_ITEM_LIST(&rsync_xal_l
, item_list
, RSYNC_XAL_LIST_INITIAL
);
400 /* Since the following call starts a new list, we know it will hold the
401 * entire initial-count, not just enough space for one new item. */
402 *new_lst
= empty_xattr
;
403 (void)EXPAND_ITEM_LIST(new_lst
, rsync_xa
, xalp
->count
);
404 memcpy(new_lst
->items
, xalp
->items
, xalp
->count
* sizeof (rsync_xa
));
405 new_lst
->count
= xalp
->count
;
409 /* Send the make_xattr()-generated xattr list for this flist entry. */
410 int send_xattr(int f
, stat_x
*sxp
)
412 int ndx
= find_matching_xattr(sxp
->xattr
);
414 /* Send 0 (-1 + 1) to indicate that literal xattr data follows. */
415 write_varint(f
, ndx
+ 1);
419 int count
= sxp
->xattr
->count
;
420 write_varint(f
, count
);
421 for (rxa
= sxp
->xattr
->items
; count
--; rxa
++) {
422 size_t name_len
= rxa
->name_len
;
423 const char *name
= rxa
->name
;
424 /* Strip the rsync prefix from disguised namespaces. */
425 if (name_len
> RPRE_LEN
426 #ifdef HAVE_LINUX_XATTRS
429 && name
[RPRE_LEN
] != '%' && HAS_PREFIX(name
, RSYNC_PREFIX
)) {
431 name_len
-= RPRE_LEN
;
433 #ifndef HAVE_LINUX_XATTRS
435 /* Put everything else in the user namespace. */
436 name_len
+= UPRE_LEN
;
439 write_varint(f
, name_len
);
440 write_varint(f
, rxa
->datum_len
);
441 #ifndef HAVE_LINUX_XATTRS
442 if (name_len
> rxa
->name_len
) {
443 write_buf(f
, USER_PREFIX
, UPRE_LEN
);
444 name_len
-= UPRE_LEN
;
447 write_buf(f
, name
, name_len
);
448 if (rxa
->datum_len
> MAX_FULL_DATUM
)
449 write_buf(f
, rxa
->datum
+ 1, MAX_DIGEST_LEN
);
451 write_buf(f
, rxa
->datum
, rxa
->datum_len
);
453 ndx
= rsync_xal_l
.count
; /* pre-incremented count */
454 rsync_xal_store(sxp
->xattr
); /* adds item to rsync_xal_l */
460 /* Return a flag indicating if we need to change a file's xattrs. If
461 * "find_all" is specified, also mark any abbreviated xattrs that we
462 * need so that send_xattr_request() can tell the sender about them. */
463 int xattr_diff(struct file_struct
*file
, stat_x
*sxp
, int find_all
)
465 item_list
*lst
= rsync_xal_l
.items
;
466 rsync_xa
*snd_rxa
, *rec_rxa
;
467 int snd_cnt
, rec_cnt
;
468 int cmp
, same
, xattrs_equal
= 1;
470 if (sxp
&& XATTR_READY(*sxp
)) {
471 rec_rxa
= sxp
->xattr
->items
;
472 rec_cnt
= sxp
->xattr
->count
;
478 if (F_XATTR(file
) >= 0)
479 lst
+= F_XATTR(file
);
483 snd_rxa
= lst
->items
;
484 snd_cnt
= lst
->count
;
486 /* If the count of the sender's xattrs is different from our
487 * (receiver's) xattrs, the lists are not the same. */
488 if (snd_cnt
!= rec_cnt
) {
495 cmp
= rec_cnt
? strcmp(snd_rxa
->name
, rec_rxa
->name
) : -1;
498 else if (snd_rxa
->datum_len
> MAX_FULL_DATUM
) {
499 same
= cmp
== 0 && snd_rxa
->datum_len
== rec_rxa
->datum_len
500 && memcmp(snd_rxa
->datum
+ 1, rec_rxa
->datum
+ 1,
501 MAX_DIGEST_LEN
) == 0;
502 /* Flag unrequested items that we need. */
503 if (!same
&& find_all
&& snd_rxa
->datum
[0] == XSTATE_ABBREV
)
504 snd_rxa
->datum
[0] = XSTATE_TODO
;
506 same
= cmp
== 0 && snd_rxa
->datum_len
== rec_rxa
->datum_len
507 && memcmp(snd_rxa
->datum
, rec_rxa
->datum
,
508 snd_rxa
->datum_len
) == 0;
529 return !xattrs_equal
;
532 /* When called by the generator (with a NULL fname), this tells the sender
533 * all the abbreviated xattr values we need. When called by the sender
534 * (with a non-NULL fname), we send all the extra xattr data it needs.
535 * The generator may also call with f_out < 0 to just change all the
536 * XSTATE_ABBREV states into XSTATE_DONE. */
537 void send_xattr_request(const char *fname
, struct file_struct
*file
, int f_out
)
539 item_list
*lst
= rsync_xal_l
.items
;
540 int cnt
, prior_req
= 0;
543 lst
+= F_XATTR(file
);
544 for (rxa
= lst
->items
, cnt
= lst
->count
; cnt
--; rxa
++) {
545 if (rxa
->datum_len
<= MAX_FULL_DATUM
)
547 switch (rxa
->datum
[0]) {
549 /* Items left abbreviated matched the sender's checksum, so
550 * the receiver will cache the local data for future use. */
552 rxa
->datum
[0] = XSTATE_DONE
;
561 /* Flag that we handled this abbreviated item. */
562 rxa
->datum
[0] = XSTATE_DONE
;
564 write_varint(f_out
, rxa
->num
- prior_req
);
565 prior_req
= rxa
->num
;
571 /* Re-read the long datum. */
572 if (!(ptr
= get_xattr_data(fname
, rxa
->name
, &len
, 0))) {
573 rprintf(FERROR_XFER
, "failed to re-read xattr %s for %s\n", rxa
->name
, fname
);
574 write_varint(f_out
, 0);
578 write_varint(f_out
, len
); /* length might have changed! */
579 write_buf(f_out
, ptr
, len
);
585 write_byte(f_out
, 0); /* end the list */
588 /* When called by the sender, read the request from the generator and mark
589 * any needed xattrs with a flag that lets us know they need to be sent to
590 * the receiver. When called by the receiver, reads the sent data and
591 * stores it in place of its checksum. */
592 int recv_xattr_request(struct file_struct
*file
, int f_in
)
594 item_list
*lst
= rsync_xal_l
.items
;
595 char *old_datum
, *name
;
597 int rel_pos
, cnt
, num
, got_xattr_data
= 0;
599 if (F_XATTR(file
) < 0) {
600 rprintf(FERROR
, "recv_xattr_request: internal data error!\n");
601 exit_cleanup(RERR_PROTOCOL
);
603 lst
+= F_XATTR(file
);
608 while ((rel_pos
= read_varint(f_in
)) != 0) {
610 while (cnt
&& rxa
->num
< num
) {
614 if (!cnt
|| rxa
->num
!= num
) {
615 rprintf(FERROR
, "[%s] could not find xattr #%d for %s\n",
616 who_am_i(), num
, f_name(file
, NULL
));
617 exit_cleanup(RERR_PROTOCOL
);
619 if (!XATTR_ABBREV(*rxa
) || rxa
->datum
[0] != XSTATE_ABBREV
) {
620 rprintf(FERROR
, "[%s] internal abbrev error on %s (%s, len=%ld)!\n",
621 who_am_i(), f_name(file
, NULL
), rxa
->name
, (long)rxa
->datum_len
);
622 exit_cleanup(RERR_PROTOCOL
);
626 rxa
->datum
[0] = XSTATE_TODO
;
630 old_datum
= rxa
->datum
;
631 rxa
->datum_len
= read_varint(f_in
);
633 if (rxa
->name_len
+ rxa
->datum_len
< rxa
->name_len
)
634 overflow_exit("recv_xattr_request");
635 rxa
->datum
= new_array(char, rxa
->datum_len
+ rxa
->name_len
);
637 out_of_memory("recv_xattr_request");
638 name
= rxa
->datum
+ rxa
->datum_len
;
639 memcpy(name
, rxa
->name
, rxa
->name_len
);
642 read_buf(f_in
, rxa
->datum
, rxa
->datum_len
);
646 return got_xattr_data
;
649 /* ------------------------------------------------------------------------- */
651 /* receive and build the rsync_xattr_lists */
652 void receive_xattr(int f
, struct file_struct
*file
)
654 static item_list temp_xattr
= EMPTY_ITEM_LIST
;
656 #ifdef HAVE_LINUX_XATTRS
661 int ndx
= read_varint(f
);
663 if (ndx
< 0 || (size_t)ndx
> rsync_xal_l
.count
) {
664 rprintf(FERROR
, "receive_xattr: xa index %d out of"
665 " range for %s\n", ndx
, f_name(file
, NULL
));
666 exit_cleanup(RERR_STREAMIO
);
670 F_XATTR(file
) = ndx
- 1;
674 if ((count
= read_varint(f
)) != 0) {
675 (void)EXPAND_ITEM_LIST(&temp_xattr
, rsync_xa
, count
);
676 temp_xattr
.count
= 0;
679 for (num
= 1; num
<= count
; num
++) {
682 size_t name_len
= read_varint(f
);
683 size_t datum_len
= read_varint(f
);
684 size_t dget_len
= datum_len
> MAX_FULL_DATUM
? 1 + MAX_DIGEST_LEN
: datum_len
;
685 size_t extra_len
= MIGHT_NEED_RPRE
? RPRE_LEN
: 0;
686 if ((dget_len
+ extra_len
< dget_len
)
687 || (dget_len
+ extra_len
+ name_len
< dget_len
))
688 overflow_exit("receive_xattr");
689 ptr
= new_array(char, dget_len
+ extra_len
+ name_len
);
691 out_of_memory("receive_xattr");
692 name
= ptr
+ dget_len
+ extra_len
;
693 read_buf(f
, name
, name_len
);
694 if (dget_len
== datum_len
)
695 read_buf(f
, ptr
, dget_len
);
697 *ptr
= XSTATE_ABBREV
;
698 read_buf(f
, ptr
+ 1, MAX_DIGEST_LEN
);
700 #ifdef HAVE_LINUX_XATTRS
701 /* Non-root can only save the user namespace. */
702 if (am_root
<= 0 && !HAS_PREFIX(name
, USER_PREFIX
)) {
708 name_len
+= RPRE_LEN
;
709 memcpy(name
, RSYNC_PREFIX
, RPRE_LEN
);
713 /* This OS only has a user namespace, so we either
714 * strip the user prefix, or we put a non-user
715 * namespace inside our rsync hierarchy. */
716 if (HAS_PREFIX(name
, USER_PREFIX
)) {
718 name_len
-= UPRE_LEN
;
719 } else if (am_root
) {
721 name_len
+= RPRE_LEN
;
722 memcpy(name
, RSYNC_PREFIX
, RPRE_LEN
);
728 /* No rsync.%FOO attributes are copied w/o 2 -X options. */
729 if (preserve_xattrs
< 2 && name_len
> RPRE_LEN
730 && name
[RPRE_LEN
] == '%' && HAS_PREFIX(name
, RSYNC_PREFIX
)) {
734 rxa
= EXPAND_ITEM_LIST(&temp_xattr
, rsync_xa
, 1);
737 rxa
->name_len
= name_len
;
738 rxa
->datum_len
= datum_len
;
742 if (need_sort
&& count
> 1)
743 qsort(temp_xattr
.items
, count
, sizeof (rsync_xa
), rsync_xal_compare_names
);
745 ndx
= rsync_xal_l
.count
; /* pre-incremented count */
746 rsync_xal_store(&temp_xattr
); /* adds item to rsync_xal_l */
751 /* Turn the xattr data in stat_x into cached xattr data, setting the index
752 * values in the file struct. */
753 void cache_tmp_xattr(struct file_struct
*file
, stat_x
*sxp
)
760 if (prior_xattr_count
== (size_t)-1)
761 prior_xattr_count
= rsync_xal_l
.count
;
762 ndx
= find_matching_xattr(sxp
->xattr
);
764 rsync_xal_store(sxp
->xattr
); /* adds item to rsync_xal_l */
769 void uncache_tmp_xattrs(void)
771 if (prior_xattr_count
!= (size_t)-1) {
772 item_list
*xattr_item
= rsync_xal_l
.items
;
773 item_list
*xattr_start
= xattr_item
+ prior_xattr_count
;
774 xattr_item
+= rsync_xal_l
.count
;
775 rsync_xal_l
.count
= prior_xattr_count
;
776 while (xattr_item
-- > xattr_start
) {
777 rsync_xal_free(xattr_item
);
778 free(xattr_item
->items
);
780 prior_xattr_count
= (size_t)-1;
784 static int rsync_xal_set(const char *fname
, item_list
*xalp
,
785 const char *fnamecmp
, stat_x
*sxp
)
787 rsync_xa
*rxas
= xalp
->items
;
790 char *name
, *ptr
, sum
[MAX_DIGEST_LEN
];
791 #ifdef HAVE_LINUX_XATTRS
792 int user_only
= am_root
<= 0;
797 /* This puts the current name list into the "namebuf" buffer. */
798 if ((list_len
= get_xattr_names(fname
)) < 0)
801 for (i
= 0; i
< xalp
->count
; i
++) {
804 if (XATTR_ABBREV(rxas
[i
])) {
805 /* See if the fnamecmp version is identical. */
806 len
= name_len
= rxas
[i
].name_len
;
807 if ((ptr
= get_xattr_data(fnamecmp
, name
, &len
, 1)) == NULL
) {
811 rprintf(FERROR
, "Missing abbreviated xattr value, %s, for %s\n",
812 rxas
[i
].name
, full_fname(fname
));
816 if (len
!= rxas
[i
].datum_len
) {
821 sum_init(checksum_seed
);
822 sum_update(ptr
, len
);
824 if (memcmp(sum
, rxas
[i
].datum
+ 1, MAX_DIGEST_LEN
) != 0) {
829 if (fname
== fnamecmp
)
830 ; /* Value is already set when identical */
831 else if (sys_lsetxattr(fname
, name
, ptr
, len
) < 0) {
832 rsyserr(FERROR_XFER
, errno
,
833 "rsync_xal_set: lsetxattr(\"%s\",\"%s\") failed",
834 full_fname(fname
), name
);
836 } else /* make sure caller sets mtime */
837 sxp
->st
.st_mtime
= (time_t)-1;
839 if (am_generator
) { /* generator items stay abbreviated */
844 memcpy(ptr
+ len
, name
, name_len
);
847 rxas
[i
].name
= name
= ptr
+ len
;
852 if (sys_lsetxattr(fname
, name
, rxas
[i
].datum
, rxas
[i
].datum_len
) < 0) {
853 rsyserr(FERROR_XFER
, errno
,
854 "rsync_xal_set: lsetxattr(\"%s\",\"%s\") failed",
855 full_fname(fname
), name
);
857 } else /* make sure caller sets mtime */
858 sxp
->st
.st_mtime
= (time_t)-1;
861 /* Remove any extraneous names. */
862 for (name
= namebuf
; list_len
> 0; name
+= name_len
) {
863 name_len
= strlen(name
) + 1;
864 list_len
-= name_len
;
866 #ifdef HAVE_LINUX_XATTRS
867 /* We always ignore the system namespace, and non-root
868 * ignores everything but the user namespace. */
869 if (user_only
? !HAS_PREFIX(name
, USER_PREFIX
)
870 : HAS_PREFIX(name
, SYSTEM_PREFIX
))
873 if (am_root
< 0 && name_len
> RPRE_LEN
874 && name
[RPRE_LEN
] == '%' && strcmp(name
, XSTAT_ATTR
) == 0)
877 for (i
= 0; i
< xalp
->count
; i
++) {
878 if (strcmp(name
, rxas
[i
].name
) == 0)
881 if (i
== xalp
->count
) {
882 if (sys_lremovexattr(fname
, name
) < 0) {
883 rsyserr(FERROR_XFER
, errno
,
884 "rsync_xal_set: lremovexattr(\"%s\",\"%s\") failed",
885 full_fname(fname
), name
);
887 } else /* make sure caller sets mtime */
888 sxp
->st
.st_mtime
= (time_t)-1;
895 /* Set extended attributes on indicated filename. */
896 int set_xattr(const char *fname
, const struct file_struct
*file
,
897 const char *fnamecmp
, stat_x
*sxp
)
900 item_list
*lst
= rsync_xal_l
.items
;
903 return 1; /* FIXME: --dry-run needs to compute this value */
905 if (read_only
|| list_only
) {
910 #ifdef NO_SPECIAL_XATTRS
911 if (IS_SPECIAL(sxp
->st
.st_mode
)) {
916 #ifdef NO_DEVICE_XATTRS
917 if (IS_DEVICE(sxp
->st
.st_mode
)) {
922 #ifdef NO_SYMLINK_XATTRS
923 if (S_ISLNK(sxp
->st
.st_mode
)) {
930 return rsync_xal_set(fname
, lst
+ ndx
, fnamecmp
, sxp
);
934 char *get_xattr_acl(const char *fname
, int is_access_acl
, size_t *len_p
)
936 const char *name
= is_access_acl
? XACC_ACL_ATTR
: XDEF_ACL_ATTR
;
937 *len_p
= 0; /* no extra data alloc needed from get_xattr_data() */
938 return get_xattr_data(fname
, name
, len_p
, 1);
941 int set_xattr_acl(const char *fname
, int is_access_acl
, const char *buf
, size_t buf_len
)
943 const char *name
= is_access_acl
? XACC_ACL_ATTR
: XDEF_ACL_ATTR
;
944 if (sys_lsetxattr(fname
, name
, buf
, buf_len
) < 0) {
945 rsyserr(FERROR_XFER
, errno
,
946 "set_xattr_acl: lsetxattr(\"%s\",\"%s\") failed",
947 full_fname(fname
), name
);
953 int del_def_xattr_acl(const char *fname
)
955 return sys_lremovexattr(fname
, XDEF_ACL_ATTR
);
959 int get_stat_xattr(const char *fname
, int fd
, STRUCT_STAT
*fst
, STRUCT_STAT
*xst
)
961 int mode
, rdev_major
, rdev_minor
, uid
, gid
, len
;
964 if (am_root
>= 0 || IS_DEVICE(fst
->st_mode
) || IS_SPECIAL(fst
->st_mode
))
973 len
= sys_lgetxattr(fname
, XSTAT_ATTR
, buf
, sizeof buf
- 1);
976 len
= sys_fgetxattr(fd
, XSTAT_ATTR
, buf
, sizeof buf
- 1);
978 if (len
>= (int)sizeof buf
) {
983 if (errno
== ENOTSUP
|| errno
== ENOATTR
)
985 if (errno
== EPERM
&& S_ISLNK(fst
->st_mode
)) {
990 rsyserr(FERROR_XFER
, errno
, "failed to read xattr %s for %s",
991 XSTAT_ATTR
, full_fname(fname
));
996 if (sscanf(buf
, "%o %d,%d %d:%d",
997 &mode
, &rdev_major
, &rdev_minor
, &uid
, &gid
) != 5) {
998 rprintf(FERROR
, "Corrupt %s xattr attached to %s: \"%s\"\n",
999 XSTAT_ATTR
, full_fname(fname
), buf
);
1000 exit_cleanup(RERR_FILEIO
);
1003 xst
->st_mode
= from_wire_mode(mode
);
1004 xst
->st_rdev
= MAKEDEV(rdev_major
, rdev_minor
);
1011 int set_stat_xattr(const char *fname
, struct file_struct
*file
, mode_t new_mode
)
1013 STRUCT_STAT fst
, xst
;
1020 if (read_only
|| list_only
) {
1021 rsyserr(FERROR_XFER
, EROFS
, "failed to write xattr %s for %s",
1022 XSTAT_ATTR
, full_fname(fname
));
1026 if (x_lstat(fname
, &fst
, &xst
) < 0) {
1027 rsyserr(FERROR_XFER
, errno
, "failed to re-stat %s",
1032 fst
.st_mode
&= (_S_IFMT
| CHMOD_BITS
);
1033 fmode
= new_mode
& (_S_IFMT
| CHMOD_BITS
);
1035 if (IS_DEVICE(fmode
)) {
1036 uint32
*devp
= F_RDEV_P(file
);
1037 rdev
= MAKEDEV(DEV_MAJOR(devp
), DEV_MINOR(devp
));
1041 /* Dump the special permissions and enable full owner access. */
1042 mode
= (fst
.st_mode
& _S_IFMT
) | (fmode
& ACCESSPERMS
)
1043 | (S_ISDIR(fst
.st_mode
) ? 0700 : 0600);
1044 if (fst
.st_mode
!= mode
)
1045 do_chmod(fname
, mode
);
1046 if (!IS_DEVICE(fst
.st_mode
))
1047 fst
.st_rdev
= 0; /* just in case */
1049 if (mode
== fmode
&& fst
.st_rdev
== rdev
1050 && fst
.st_uid
== F_OWNER(file
) && fst
.st_gid
== F_GROUP(file
)) {
1051 /* xst.st_mode will be 0 if there's no current stat xattr */
1052 if (xst
.st_mode
&& sys_lremovexattr(fname
, XSTAT_ATTR
) < 0) {
1053 rsyserr(FERROR_XFER
, errno
,
1054 "delete of stat xattr failed for %s",
1061 if (xst
.st_mode
!= fmode
|| xst
.st_rdev
!= rdev
1062 || xst
.st_uid
!= F_OWNER(file
) || xst
.st_gid
!= F_GROUP(file
)) {
1064 int len
= snprintf(buf
, sizeof buf
, "%o %u,%u %u:%u",
1065 to_wire_mode(fmode
),
1066 (int)major(rdev
), (int)minor(rdev
),
1067 F_OWNER(file
), F_GROUP(file
));
1068 if (sys_lsetxattr(fname
, XSTAT_ATTR
, buf
, len
) < 0) {
1069 if (errno
== EPERM
&& S_ISLNK(fst
.st_mode
))
1071 rsyserr(FERROR_XFER
, errno
,
1072 "failed to write xattr %s for %s",
1073 XSTAT_ATTR
, full_fname(fname
));
1081 int x_stat(const char *fname
, STRUCT_STAT
*fst
, STRUCT_STAT
*xst
)
1083 int ret
= do_stat(fname
, fst
);
1084 if ((ret
< 0 || get_stat_xattr(fname
, -1, fst
, xst
) < 0) && xst
)
1089 int x_lstat(const char *fname
, STRUCT_STAT
*fst
, STRUCT_STAT
*xst
)
1091 int ret
= do_lstat(fname
, fst
);
1092 if ((ret
< 0 || get_stat_xattr(fname
, -1, fst
, xst
) < 0) && xst
)
1097 int x_fstat(int fd
, STRUCT_STAT
*fst
, STRUCT_STAT
*xst
)
1099 int ret
= do_fstat(fd
, fst
);
1100 if ((ret
< 0 || get_stat_xattr(NULL
, fd
, fst
, xst
) < 0) && xst
)
1105 #endif /* SUPPORT_XATTRS */