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-2008 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.
24 #include "lib/sysxattrs.h"
31 extern int am_generator
;
34 extern int preserve_xattrs
;
35 extern int checksum_seed
;
37 #define RSYNC_XAL_INITIAL 5
38 #define RSYNC_XAL_LIST_INITIAL 100
40 #define MAX_FULL_DATUM 32
42 #define HAS_PREFIX(str, prfx) (*(str) == *(prfx) \
43 && strncmp(str, prfx, sizeof (prfx) - 1) == 0)
45 #define XATTR_ABBREV(x) ((size_t)((x).name - (x).datum) < (x).datum_len)
47 #define XSTATE_ABBREV 1
51 #define USER_PREFIX "user."
52 #define UPRE_LEN ((int)sizeof USER_PREFIX - 1)
53 #define SYSTEM_PREFIX "system."
54 #define SPRE_LEN ((int)sizeof SYSTEM_PREFIX - 1)
56 #ifdef HAVE_LINUX_XATTRS
57 #define MIGHT_NEED_RPRE (am_root < 0)
58 #define RSYNC_PREFIX USER_PREFIX "rsync."
60 #define MIGHT_NEED_RPRE am_root
61 #define RSYNC_PREFIX "rsync."
63 #define RPRE_LEN ((int)sizeof RSYNC_PREFIX - 1)
65 #define XSTAT_SUFFIX "stat"
66 #define XSTAT_ATTR RSYNC_PREFIX "%" XSTAT_SUFFIX
67 #define XACC_ACL_SUFFIX "aacl"
68 #define XACC_ACL_ATTR RSYNC_PREFIX "%" XACC_ACL_SUFFIX
69 #define XDEF_ACL_SUFFIX "dacl"
70 #define XDEF_ACL_ATTR RSYNC_PREFIX "%" XDEF_ACL_SUFFIX
74 size_t datum_len
, name_len
;
78 static size_t namebuf_len
= 0;
79 static char *namebuf
= NULL
;
81 static item_list empty_xattr
= EMPTY_ITEM_LIST
;
82 static item_list rsync_xal_l
= EMPTY_ITEM_LIST
;
84 /* ------------------------------------------------------------------------- */
86 static void rsync_xal_free(item_list
*xalp
)
89 rsync_xa
*rxas
= xalp
->items
;
91 for (i
= 0; i
< xalp
->count
; i
++) {
93 /*free(rxas[i].name);*/
98 void free_xattr(stat_x
*sxp
)
102 rsync_xal_free(sxp
->xattr
);
107 static int rsync_xal_compare_names(const void *x1
, const void *x2
)
109 const rsync_xa
*xa1
= x1
;
110 const rsync_xa
*xa2
= x2
;
111 return strcmp(xa1
->name
, xa2
->name
);
114 static ssize_t
get_xattr_names(const char *fname
)
121 namebuf
= new_array(char, namebuf_len
);
123 out_of_memory("get_xattr_names");
127 /* The length returned includes all the '\0' terminators. */
128 list_len
= sys_llistxattr(fname
, namebuf
, namebuf_len
);
130 if ((size_t)list_len
<= namebuf_len
)
132 } else if (errno
== ENOTSUP
)
134 else if (errno
!= ERANGE
) {
135 arg
= (double)namebuf_len
;
137 rsyserr(FERROR_XFER
, errno
,
138 "get_xattr_names: llistxattr(\"%s\",%.0f) failed",
142 list_len
= sys_llistxattr(fname
, NULL
, 0);
149 namebuf_len
= list_len
+ 1024;
150 namebuf
= new_array(char, namebuf_len
);
152 out_of_memory("get_xattr_names");
158 /* On entry, the *len_ptr parameter contains the size of the extra space we
159 * should allocate when we create a buffer for the data. On exit, it contains
160 * the length of the datum. */
161 static char *get_xattr_data(const char *fname
, const char *name
, size_t *len_ptr
,
162 int no_missing_error
)
164 size_t datum_len
= sys_lgetxattr(fname
, name
, NULL
, 0);
165 size_t extra_len
= *len_ptr
;
168 *len_ptr
= datum_len
;
170 if (datum_len
== (size_t)-1) {
171 if (errno
== ENOTSUP
|| no_missing_error
)
173 rsyserr(FERROR_XFER
, errno
,
174 "get_xattr_data: lgetxattr(\"%s\",\"%s\",0) failed",
179 if (!datum_len
&& !extra_len
)
180 extra_len
= 1; /* request non-zero amount of memory */
181 if (datum_len
+ extra_len
< datum_len
)
182 overflow_exit("get_xattr_data");
183 if (!(ptr
= new_array(char, datum_len
+ extra_len
)))
184 out_of_memory("get_xattr_data");
187 size_t len
= sys_lgetxattr(fname
, name
, ptr
, datum_len
);
188 if (len
!= datum_len
) {
189 if (len
== (size_t)-1) {
190 rsyserr(FERROR_XFER
, errno
,
191 "get_xattr_data: lgetxattr(\"%s\",\"%s\",%ld)"
192 " failed", fname
, name
, (long)datum_len
);
195 "get_xattr_data: lgetxattr(\"%s\",\"%s\",%ld)"
196 " returned %ld\n", fname
, name
,
197 (long)datum_len
, (long)len
);
207 static int rsync_xal_get(const char *fname
, item_list
*xalp
)
209 ssize_t list_len
, name_len
;
210 size_t datum_len
, name_offset
;
212 #ifdef HAVE_LINUX_XATTRS
213 int user_only
= am_sender
? 0 : !am_root
;
218 /* This puts the name list into the "namebuf" buffer. */
219 if ((list_len
= get_xattr_names(fname
)) < 0)
222 for (name
= namebuf
; list_len
> 0; name
+= name_len
) {
223 name_len
= strlen(name
) + 1;
224 list_len
-= name_len
;
226 #ifdef HAVE_LINUX_XATTRS
227 /* We always ignore the system namespace, and non-root
228 * ignores everything but the user namespace. */
229 if (user_only
? !HAS_PREFIX(name
, USER_PREFIX
)
230 : HAS_PREFIX(name
, SYSTEM_PREFIX
))
234 /* No rsync.%FOO attributes are copied w/o 2 -X options. */
235 if (name_len
> RPRE_LEN
&& name
[RPRE_LEN
] == '%'
236 && HAS_PREFIX(name
, RSYNC_PREFIX
)) {
237 if ((am_sender
&& preserve_xattrs
< 2)
239 && (strcmp(name
+RPRE_LEN
+1, XSTAT_SUFFIX
) == 0
240 || strcmp(name
+RPRE_LEN
+1, XACC_ACL_SUFFIX
) == 0
241 || strcmp(name
+RPRE_LEN
+1, XDEF_ACL_SUFFIX
) == 0)))
245 datum_len
= name_len
; /* Pass extra size to get_xattr_data() */
246 if (!(ptr
= get_xattr_data(fname
, name
, &datum_len
, 0)))
249 if (datum_len
> MAX_FULL_DATUM
) {
250 /* For large datums, we store a flag and a checksum. */
251 name_offset
= 1 + MAX_DIGEST_LEN
;
252 sum_init(checksum_seed
);
253 sum_update(ptr
, datum_len
);
256 if (!(ptr
= new_array(char, name_offset
+ name_len
)))
257 out_of_memory("rsync_xal_get");
258 *ptr
= XSTATE_ABBREV
;
261 name_offset
= datum_len
;
263 rxa
= EXPAND_ITEM_LIST(xalp
, rsync_xa
, RSYNC_XAL_INITIAL
);
264 rxa
->name
= ptr
+ name_offset
;
265 memcpy(rxa
->name
, name
, name_len
);
267 rxa
->name_len
= name_len
;
268 rxa
->datum_len
= datum_len
;
273 qsort(rxa
, count
, sizeof (rsync_xa
), rsync_xal_compare_names
);
274 for (rxa
+= count
-1; count
; count
--, rxa
--)
279 /* Read the xattr(s) for this filename. */
280 int get_xattr(const char *fname
, stat_x
*sxp
)
282 sxp
->xattr
= new(item_list
);
283 *sxp
->xattr
= empty_xattr
;
284 if (rsync_xal_get(fname
, sxp
->xattr
) < 0) {
291 int copy_xattrs(const char *source
, const char *dest
)
293 ssize_t list_len
, name_len
;
296 #ifdef HAVE_LINUX_XATTRS
297 int user_only
= am_sender
? 0 : am_root
<= 0;
300 /* This puts the name list into the "namebuf" buffer. */
301 if ((list_len
= get_xattr_names(source
)) < 0)
304 for (name
= namebuf
; list_len
> 0; name
+= name_len
) {
305 name_len
= strlen(name
) + 1;
306 list_len
-= name_len
;
308 #ifdef HAVE_LINUX_XATTRS
309 /* We always ignore the system namespace, and non-root
310 * ignores everything but the user namespace. */
311 if (user_only
? !HAS_PREFIX(name
, USER_PREFIX
)
312 : HAS_PREFIX(name
, SYSTEM_PREFIX
))
317 if (!(ptr
= get_xattr_data(source
, name
, &datum_len
, 0)))
319 if (sys_lsetxattr(dest
, name
, ptr
, datum_len
) < 0) {
320 int save_errno
= errno
? errno
: EINVAL
;
321 rsyserr(FERROR_XFER
, errno
,
322 "rsync_xal_set: lsetxattr(\"%s\",\"%s\") failed",
333 static int find_matching_xattr(item_list
*xalp
)
336 item_list
*lst
= rsync_xal_l
.items
;
338 for (i
= 0; i
< rsync_xal_l
.count
; i
++) {
339 rsync_xa
*rxas1
= lst
[i
].items
;
340 rsync_xa
*rxas2
= xalp
->items
;
342 /* Wrong number of elements? */
343 if (lst
[i
].count
!= xalp
->count
)
345 /* any elements different? */
346 for (j
= 0; j
< xalp
->count
; j
++) {
347 if (rxas1
[j
].name_len
!= rxas2
[j
].name_len
348 || rxas1
[j
].datum_len
!= rxas2
[j
].datum_len
349 || strcmp(rxas1
[j
].name
, rxas2
[j
].name
))
351 if (rxas1
[j
].datum_len
> MAX_FULL_DATUM
) {
352 if (memcmp(rxas1
[j
].datum
+ 1,
354 MAX_DIGEST_LEN
) != 0)
357 if (memcmp(rxas1
[j
].datum
, rxas2
[j
].datum
,
362 /* no differences found. This is The One! */
363 if (j
== xalp
->count
)
370 /* Store *xalp on the end of rsync_xal_l */
371 static void rsync_xal_store(item_list
*xalp
)
373 item_list
*new_lst
= EXPAND_ITEM_LIST(&rsync_xal_l
, item_list
, RSYNC_XAL_LIST_INITIAL
);
374 /* Since the following call starts a new list, we know it will hold the
375 * entire initial-count, not just enough space for one new item. */
376 *new_lst
= empty_xattr
;
377 (void)EXPAND_ITEM_LIST(new_lst
, rsync_xa
, xalp
->count
);
378 memcpy(new_lst
->items
, xalp
->items
, xalp
->count
* sizeof (rsync_xa
));
379 new_lst
->count
= xalp
->count
;
383 /* Send the make_xattr()-generated xattr list for this flist entry. */
384 int send_xattr(stat_x
*sxp
, int f
)
386 int ndx
= find_matching_xattr(sxp
->xattr
);
388 /* Send 0 (-1 + 1) to indicate that literal xattr data follows. */
389 write_varint(f
, ndx
+ 1);
393 int count
= sxp
->xattr
->count
;
394 write_varint(f
, count
);
395 for (rxa
= sxp
->xattr
->items
; count
--; rxa
++) {
396 size_t name_len
= rxa
->name_len
;
397 const char *name
= rxa
->name
;
398 /* Strip the rsync prefix from disguised namespaces. */
399 if (name_len
> RPRE_LEN
400 #ifdef HAVE_LINUX_XATTRS
403 && name
[RPRE_LEN
] != '%' && HAS_PREFIX(name
, RSYNC_PREFIX
)) {
405 name_len
-= RPRE_LEN
;
407 #ifndef HAVE_LINUX_XATTRS
409 /* Put everything else in the user namespace. */
410 name_len
+= UPRE_LEN
;
413 write_varint(f
, name_len
);
414 write_varint(f
, rxa
->datum_len
);
415 #ifndef HAVE_LINUX_XATTRS
416 if (name_len
> rxa
->name_len
) {
417 write_buf(f
, USER_PREFIX
, UPRE_LEN
);
418 name_len
-= UPRE_LEN
;
421 write_buf(f
, name
, name_len
);
422 if (rxa
->datum_len
> MAX_FULL_DATUM
)
423 write_buf(f
, rxa
->datum
+ 1, MAX_DIGEST_LEN
);
425 write_buf(f
, rxa
->datum
, rxa
->datum_len
);
427 ndx
= rsync_xal_l
.count
; /* pre-incremented count */
428 rsync_xal_store(sxp
->xattr
); /* adds item to rsync_xal_l */
434 /* Return a flag indicating if we need to change a file's xattrs. If
435 * "find_all" is specified, also mark any abbreviated xattrs that we
436 * need so that send_xattr_request() can tell the sender about them. */
437 int xattr_diff(struct file_struct
*file
, stat_x
*sxp
, int find_all
)
439 item_list
*lst
= rsync_xal_l
.items
;
440 rsync_xa
*snd_rxa
, *rec_rxa
;
441 int snd_cnt
, rec_cnt
;
442 int cmp
, same
, xattrs_equal
= 1;
444 if (sxp
&& XATTR_READY(*sxp
)) {
445 rec_rxa
= sxp
->xattr
->items
;
446 rec_cnt
= sxp
->xattr
->count
;
452 if (F_XATTR(file
) >= 0)
453 lst
+= F_XATTR(file
);
457 snd_rxa
= lst
->items
;
458 snd_cnt
= lst
->count
;
460 /* If the count of the sender's xattrs is different from our
461 * (receiver's) xattrs, the lists are not the same. */
462 if (snd_cnt
!= rec_cnt
) {
469 cmp
= rec_cnt
? strcmp(snd_rxa
->name
, rec_rxa
->name
) : -1;
472 else if (snd_rxa
->datum_len
> MAX_FULL_DATUM
) {
473 same
= cmp
== 0 && snd_rxa
->datum_len
== rec_rxa
->datum_len
474 && memcmp(snd_rxa
->datum
+ 1, rec_rxa
->datum
+ 1,
475 MAX_DIGEST_LEN
) == 0;
476 /* Flag unrequested items that we need. */
477 if (!same
&& find_all
&& snd_rxa
->datum
[0] == XSTATE_ABBREV
)
478 snd_rxa
->datum
[0] = XSTATE_TODO
;
480 same
= cmp
== 0 && snd_rxa
->datum_len
== rec_rxa
->datum_len
481 && memcmp(snd_rxa
->datum
, rec_rxa
->datum
,
482 snd_rxa
->datum_len
) == 0;
503 return !xattrs_equal
;
506 /* When called by the generator (with a NULL fname), this tells the sender
507 * all the abbreviated xattr values we need. When called by the sender
508 * (with a non-NULL fname), we send all the extra xattr data it needs.
509 * The generator may also call with f_out < 0 to just change all the
510 * XSTATE_ABBREV states into XSTATE_DONE. */
511 void send_xattr_request(const char *fname
, struct file_struct
*file
, int f_out
)
513 item_list
*lst
= rsync_xal_l
.items
;
514 int cnt
, prior_req
= 0;
517 lst
+= F_XATTR(file
);
518 for (rxa
= lst
->items
, cnt
= lst
->count
; cnt
--; rxa
++) {
519 if (rxa
->datum_len
<= MAX_FULL_DATUM
)
521 switch (rxa
->datum
[0]) {
523 /* Items left abbreviated matched the sender's checksum, so
524 * the receiver will cache the local data for future use. */
526 rxa
->datum
[0] = XSTATE_DONE
;
535 /* Flag that we handled this abbreviated item. */
536 rxa
->datum
[0] = XSTATE_DONE
;
538 write_varint(f_out
, rxa
->num
- prior_req
);
539 prior_req
= rxa
->num
;
545 /* Re-read the long datum. */
546 if (!(ptr
= get_xattr_data(fname
, rxa
->name
, &len
, 0))) {
547 rprintf(FERROR_XFER
, "failed to re-read xattr %s for %s\n", rxa
->name
, fname
);
548 write_varint(f_out
, 0);
552 write_varint(f_out
, len
); /* length might have changed! */
553 write_buf(f_out
, ptr
, len
);
559 write_byte(f_out
, 0); /* end the list */
562 /* When called by the sender, read the request from the generator and mark
563 * any needed xattrs with a flag that lets us know they need to be sent to
564 * the receiver. When called by the receiver, reads the sent data and
565 * stores it in place of its checksum. */
566 int recv_xattr_request(struct file_struct
*file
, int f_in
)
568 item_list
*lst
= rsync_xal_l
.items
;
569 char *old_datum
, *name
;
571 int rel_pos
, cnt
, num
, got_xattr_data
= 0;
573 if (F_XATTR(file
) < 0) {
574 rprintf(FERROR
, "recv_xattr_request: internal data error!\n");
575 exit_cleanup(RERR_STREAMIO
);
577 lst
+= F_XATTR(file
);
582 while ((rel_pos
= read_varint(f_in
)) != 0) {
584 while (cnt
&& rxa
->num
< num
) {
588 if (!cnt
|| rxa
->num
!= num
) {
589 rprintf(FERROR
, "[%s] could not find xattr #%d for %s\n",
590 who_am_i(), num
, f_name(file
, NULL
));
591 exit_cleanup(RERR_STREAMIO
);
593 if (!XATTR_ABBREV(*rxa
) || rxa
->datum
[0] != XSTATE_ABBREV
) {
594 rprintf(FERROR
, "[%s] internal abbrev error on %s (%s, len=%ld)!\n",
595 who_am_i(), f_name(file
, NULL
), rxa
->name
, (long)rxa
->datum_len
);
596 exit_cleanup(RERR_STREAMIO
);
600 rxa
->datum
[0] = XSTATE_TODO
;
604 old_datum
= rxa
->datum
;
605 rxa
->datum_len
= read_varint(f_in
);
607 if (rxa
->name_len
+ rxa
->datum_len
< rxa
->name_len
)
608 overflow_exit("recv_xattr_request");
609 rxa
->datum
= new_array(char, rxa
->datum_len
+ rxa
->name_len
);
611 out_of_memory("recv_xattr_request");
612 name
= rxa
->datum
+ rxa
->datum_len
;
613 memcpy(name
, rxa
->name
, rxa
->name_len
);
616 read_buf(f_in
, rxa
->datum
, rxa
->datum_len
);
620 return got_xattr_data
;
623 /* ------------------------------------------------------------------------- */
625 /* receive and build the rsync_xattr_lists */
626 void receive_xattr(struct file_struct
*file
, int f
)
628 static item_list temp_xattr
= EMPTY_ITEM_LIST
;
630 #ifdef HAVE_LINUX_XATTRS
635 int ndx
= read_varint(f
);
637 if (ndx
< 0 || (size_t)ndx
> rsync_xal_l
.count
) {
638 rprintf(FERROR
, "receive_xattr: xa index %d out of"
639 " range for %s\n", ndx
, f_name(file
, NULL
));
640 exit_cleanup(RERR_STREAMIO
);
644 F_XATTR(file
) = ndx
- 1;
648 if ((count
= read_varint(f
)) != 0) {
649 (void)EXPAND_ITEM_LIST(&temp_xattr
, rsync_xa
, count
);
650 temp_xattr
.count
= 0;
653 for (num
= 1; num
<= count
; num
++) {
656 size_t name_len
= read_varint(f
);
657 size_t datum_len
= read_varint(f
);
658 size_t dget_len
= datum_len
> MAX_FULL_DATUM
? 1 + MAX_DIGEST_LEN
: datum_len
;
659 size_t extra_len
= MIGHT_NEED_RPRE
? RPRE_LEN
: 0;
660 if ((dget_len
+ extra_len
< dget_len
)
661 || (dget_len
+ extra_len
+ name_len
< dget_len
))
662 overflow_exit("receive_xattr");
663 ptr
= new_array(char, dget_len
+ extra_len
+ name_len
);
665 out_of_memory("receive_xattr");
666 name
= ptr
+ dget_len
+ extra_len
;
667 read_buf(f
, name
, name_len
);
668 if (dget_len
== datum_len
)
669 read_buf(f
, ptr
, dget_len
);
671 *ptr
= XSTATE_ABBREV
;
672 read_buf(f
, ptr
+ 1, MAX_DIGEST_LEN
);
674 #ifdef HAVE_LINUX_XATTRS
675 /* Non-root can only save the user namespace. */
676 if (am_root
<= 0 && !HAS_PREFIX(name
, USER_PREFIX
)) {
682 name_len
+= RPRE_LEN
;
683 memcpy(name
, RSYNC_PREFIX
, RPRE_LEN
);
687 /* This OS only has a user namespace, so we either
688 * strip the user prefix, or we put a non-user
689 * namespace inside our rsync hierarchy. */
690 if (HAS_PREFIX(name
, USER_PREFIX
)) {
692 name_len
-= UPRE_LEN
;
693 } else if (am_root
) {
695 name_len
+= RPRE_LEN
;
696 memcpy(name
, RSYNC_PREFIX
, RPRE_LEN
);
702 /* No rsync.%FOO attributes are copied w/o 2 -X options. */
703 if (preserve_xattrs
< 2 && name_len
> RPRE_LEN
704 && name
[RPRE_LEN
] == '%' && HAS_PREFIX(name
, RSYNC_PREFIX
)) {
708 rxa
= EXPAND_ITEM_LIST(&temp_xattr
, rsync_xa
, 1);
711 rxa
->name_len
= name_len
;
712 rxa
->datum_len
= datum_len
;
716 if (need_sort
&& count
> 1)
717 qsort(temp_xattr
.items
, count
, sizeof (rsync_xa
), rsync_xal_compare_names
);
719 ndx
= rsync_xal_l
.count
; /* pre-incremented count */
720 rsync_xal_store(&temp_xattr
); /* adds item to rsync_xal_l */
725 /* Turn the xattr data in stat_x into cached xattr data, setting the index
726 * values in the file struct. */
727 void cache_xattr(struct file_struct
*file
, stat_x
*sxp
)
734 ndx
= find_matching_xattr(sxp
->xattr
);
736 rsync_xal_store(sxp
->xattr
); /* adds item to rsync_xal_l */
741 static int rsync_xal_set(const char *fname
, item_list
*xalp
,
742 const char *fnamecmp
, stat_x
*sxp
)
744 rsync_xa
*rxas
= xalp
->items
;
747 char *name
, *ptr
, sum
[MAX_DIGEST_LEN
];
748 #ifdef HAVE_LINUX_XATTRS
749 int user_only
= am_root
<= 0;
754 /* This puts the current name list into the "namebuf" buffer. */
755 if ((list_len
= get_xattr_names(fname
)) < 0)
758 for (i
= 0; i
< xalp
->count
; i
++) {
761 if (XATTR_ABBREV(rxas
[i
])) {
762 /* See if the fnamecmp version is identical. */
763 len
= name_len
= rxas
[i
].name_len
;
764 if ((ptr
= get_xattr_data(fnamecmp
, name
, &len
, 1)) == NULL
) {
768 rprintf(FERROR
, "Missing abbreviated xattr value, %s, for %s\n",
769 rxas
[i
].name
, full_fname(fname
));
773 if (len
!= rxas
[i
].datum_len
) {
778 sum_init(checksum_seed
);
779 sum_update(ptr
, len
);
781 if (memcmp(sum
, rxas
[i
].datum
+ 1, MAX_DIGEST_LEN
) != 0) {
786 if (fname
== fnamecmp
)
787 ; /* Value is already set when identical */
788 else if (sys_lsetxattr(fname
, name
, ptr
, len
) < 0) {
789 rsyserr(FERROR_XFER
, errno
,
790 "rsync_xal_set: lsetxattr(\"%s\",\"%s\") failed",
793 } else /* make sure caller sets mtime */
794 sxp
->st
.st_mtime
= (time_t)-1;
796 if (am_generator
) { /* generator items stay abbreviated */
801 memcpy(ptr
+ len
, name
, name_len
);
804 rxas
[i
].name
= name
= ptr
+ len
;
809 if (sys_lsetxattr(fname
, name
, rxas
[i
].datum
, rxas
[i
].datum_len
) < 0) {
810 rsyserr(FERROR_XFER
, errno
,
811 "rsync_xal_set: lsetxattr(\"%s\",\"%s\") failed",
814 } else /* make sure caller sets mtime */
815 sxp
->st
.st_mtime
= (time_t)-1;
818 /* Remove any extraneous names. */
819 for (name
= namebuf
; list_len
> 0; name
+= name_len
) {
820 name_len
= strlen(name
) + 1;
821 list_len
-= name_len
;
823 #ifdef HAVE_LINUX_XATTRS
824 /* We always ignore the system namespace, and non-root
825 * ignores everything but the user namespace. */
826 if (user_only
? !HAS_PREFIX(name
, USER_PREFIX
)
827 : HAS_PREFIX(name
, SYSTEM_PREFIX
))
830 if (am_root
< 0 && name_len
> RPRE_LEN
831 && name
[RPRE_LEN
] == '%' && strcmp(name
, XSTAT_ATTR
) == 0)
834 for (i
= 0; i
< xalp
->count
; i
++) {
835 if (strcmp(name
, rxas
[i
].name
) == 0)
838 if (i
== xalp
->count
) {
839 if (sys_lremovexattr(fname
, name
) < 0) {
840 rsyserr(FERROR_XFER
, errno
,
841 "rsync_xal_clear: lremovexattr(\"%s\",\"%s\") failed",
844 } else /* make sure caller sets mtime */
845 sxp
->st
.st_mtime
= (time_t)-1;
852 /* Set extended attributes on indicated filename. */
853 int set_xattr(const char *fname
, const struct file_struct
*file
,
854 const char *fnamecmp
, stat_x
*sxp
)
857 item_list
*lst
= rsync_xal_l
.items
;
860 return 1; /* FIXME: --dry-run needs to compute this value */
862 if (read_only
|| list_only
) {
868 return rsync_xal_set(fname
, lst
+ ndx
, fnamecmp
, sxp
);
872 char *get_xattr_acl(const char *fname
, int is_access_acl
, size_t *len_p
)
874 const char *name
= is_access_acl
? XACC_ACL_ATTR
: XDEF_ACL_ATTR
;
875 *len_p
= 0; /* no extra data alloc needed from get_xattr_data() */
876 return get_xattr_data(fname
, name
, len_p
, 1);
879 int set_xattr_acl(const char *fname
, int is_access_acl
, const char *buf
, size_t buf_len
)
881 const char *name
= is_access_acl
? XACC_ACL_ATTR
: XDEF_ACL_ATTR
;
882 if (sys_lsetxattr(fname
, name
, buf
, buf_len
) < 0) {
883 rsyserr(FERROR_XFER
, errno
,
884 "set_xattr_acl: lsetxattr(\"%s\",\"%s\") failed",
891 int del_def_xattr_acl(const char *fname
)
893 return sys_lremovexattr(fname
, XDEF_ACL_ATTR
);
897 int get_stat_xattr(const char *fname
, int fd
, STRUCT_STAT
*fst
, STRUCT_STAT
*xst
)
899 int mode
, rdev_major
, rdev_minor
, uid
, gid
, len
;
902 if (am_root
>= 0 || IS_DEVICE(fst
->st_mode
) || IS_SPECIAL(fst
->st_mode
))
911 len
= sys_lgetxattr(fname
, XSTAT_ATTR
, buf
, sizeof buf
- 1);
914 len
= sys_fgetxattr(fd
, XSTAT_ATTR
, buf
, sizeof buf
- 1);
916 if (len
>= (int)sizeof buf
) {
921 if (errno
== ENOTSUP
|| errno
== ENOATTR
)
923 if (errno
== EPERM
&& S_ISLNK(fst
->st_mode
)) {
928 rsyserr(FERROR_XFER
, errno
, "failed to read xattr %s for %s",
929 XSTAT_ATTR
, full_fname(fname
));
934 if (sscanf(buf
, "%o %d,%d %d:%d",
935 &mode
, &rdev_major
, &rdev_minor
, &uid
, &gid
) != 5) {
936 rprintf(FERROR
, "Corrupt %s xattr attached to %s: \"%s\"\n",
937 XSTAT_ATTR
, full_fname(fname
), buf
);
938 exit_cleanup(RERR_FILEIO
);
941 xst
->st_mode
= from_wire_mode(mode
);
942 xst
->st_rdev
= MAKEDEV(rdev_major
, rdev_minor
);
949 int set_stat_xattr(const char *fname
, struct file_struct
*file
, mode_t new_mode
)
951 STRUCT_STAT fst
, xst
;
958 if (read_only
|| list_only
) {
959 rsyserr(FERROR_XFER
, EROFS
, "failed to write xattr %s for %s",
960 XSTAT_ATTR
, full_fname(fname
));
964 if (x_lstat(fname
, &fst
, &xst
) < 0) {
965 rsyserr(FERROR_XFER
, errno
, "failed to re-stat %s",
970 fst
.st_mode
&= (_S_IFMT
| CHMOD_BITS
);
971 fmode
= new_mode
& (_S_IFMT
| CHMOD_BITS
);
973 if (IS_DEVICE(fmode
) || IS_SPECIAL(fmode
)) {
974 uint32
*devp
= F_RDEV_P(file
);
975 rdev
= MAKEDEV(DEV_MAJOR(devp
), DEV_MINOR(devp
));
979 /* Dump the special permissions and enable full owner access. */
980 mode
= (fst
.st_mode
& _S_IFMT
) | (fmode
& ACCESSPERMS
)
981 | (S_ISDIR(fst
.st_mode
) ? 0700 : 0600);
982 if (fst
.st_mode
!= mode
)
983 do_chmod(fname
, mode
);
984 if (!IS_DEVICE(fst
.st_mode
) && !IS_SPECIAL(fst
.st_mode
))
985 fst
.st_rdev
= 0; /* just in case */
987 if (mode
== fmode
&& fst
.st_rdev
== rdev
988 && fst
.st_uid
== F_OWNER(file
) && fst
.st_gid
== F_GROUP(file
)) {
989 /* xst.st_mode will be 0 if there's no current stat xattr */
990 if (xst
.st_mode
&& sys_lremovexattr(fname
, XSTAT_ATTR
) < 0) {
991 rsyserr(FERROR_XFER
, errno
,
992 "delete of stat xattr failed for %s",
999 if (xst
.st_mode
!= fmode
|| xst
.st_rdev
!= rdev
1000 || xst
.st_uid
!= F_OWNER(file
) || xst
.st_gid
!= F_GROUP(file
)) {
1002 int len
= snprintf(buf
, sizeof buf
, "%o %u,%u %u:%u",
1003 to_wire_mode(fmode
),
1004 (int)major(rdev
), (int)minor(rdev
),
1005 F_OWNER(file
), F_GROUP(file
));
1006 if (sys_lsetxattr(fname
, XSTAT_ATTR
, buf
, len
) < 0) {
1007 if (errno
== EPERM
&& S_ISLNK(fst
.st_mode
))
1009 rsyserr(FERROR_XFER
, errno
,
1010 "failed to write xattr %s for %s",
1011 XSTAT_ATTR
, full_fname(fname
));
1019 int x_stat(const char *fname
, STRUCT_STAT
*fst
, STRUCT_STAT
*xst
)
1021 int ret
= do_stat(fname
, fst
);
1022 if ((ret
< 0 || get_stat_xattr(fname
, -1, fst
, xst
) < 0) && xst
)
1027 int x_lstat(const char *fname
, STRUCT_STAT
*fst
, STRUCT_STAT
*xst
)
1029 int ret
= do_lstat(fname
, fst
);
1030 if ((ret
< 0 || get_stat_xattr(fname
, -1, fst
, xst
) < 0) && xst
)
1035 int x_fstat(int fd
, STRUCT_STAT
*fst
, STRUCT_STAT
*xst
)
1037 int ret
= do_fstat(fd
, fst
);
1038 if ((ret
< 0 || get_stat_xattr(NULL
, fd
, fst
, xst
) < 0) && xst
)
1043 #endif /* SUPPORT_XATTRS */