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 0
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
/* checks for overflow */
182 || !(ptr
= new_array(char, datum_len
+ extra_len
)))
183 out_of_memory("get_xattr_data");
186 size_t len
= sys_lgetxattr(fname
, name
, ptr
, datum_len
);
187 if (len
!= datum_len
) {
188 if (len
== (size_t)-1) {
189 rsyserr(FERROR_XFER
, errno
,
190 "get_xattr_data: lgetxattr(\"%s\",\"%s\",%ld)"
191 " failed", fname
, name
, (long)datum_len
);
194 "get_xattr_data: lgetxattr(\"%s\",\"%s\",%ld)"
195 " returned %ld\n", fname
, name
,
196 (long)datum_len
, (long)len
);
206 static int rsync_xal_get(const char *fname
, item_list
*xalp
)
208 ssize_t list_len
, name_len
;
209 size_t datum_len
, name_offset
;
211 #ifdef HAVE_LINUX_XATTRS
212 int user_only
= am_sender
? 0 : !am_root
;
217 /* This puts the name list into the "namebuf" buffer. */
218 if ((list_len
= get_xattr_names(fname
)) < 0)
221 for (name
= namebuf
; list_len
> 0; name
+= name_len
) {
222 name_len
= strlen(name
) + 1;
223 list_len
-= name_len
;
225 #ifdef HAVE_LINUX_XATTRS
226 /* We always ignore the system namespace, and non-root
227 * ignores everything but the user namespace. */
228 if (user_only
? !HAS_PREFIX(name
, USER_PREFIX
)
229 : HAS_PREFIX(name
, SYSTEM_PREFIX
))
233 /* No rsync.%FOO attributes are copied w/o 2 -X options. */
234 if (name_len
> RPRE_LEN
&& name
[RPRE_LEN
] == '%'
235 && HAS_PREFIX(name
, RSYNC_PREFIX
)) {
236 if ((am_sender
&& preserve_xattrs
< 2)
238 && (strcmp(name
+RPRE_LEN
+1, XSTAT_SUFFIX
) == 0
239 || strcmp(name
+RPRE_LEN
+1, XACC_ACL_SUFFIX
) == 0
240 || strcmp(name
+RPRE_LEN
+1, XDEF_ACL_SUFFIX
) == 0)))
244 datum_len
= name_len
; /* Pass extra size to get_xattr_data() */
245 if (!(ptr
= get_xattr_data(fname
, name
, &datum_len
, 0)))
248 if (datum_len
> MAX_FULL_DATUM
) {
249 /* For large datums, we store a flag and a checksum. */
250 name_offset
= 1 + MAX_DIGEST_LEN
;
251 sum_init(checksum_seed
);
252 sum_update(ptr
, datum_len
);
255 if (!(ptr
= new_array(char, name_offset
+ name_len
)))
256 out_of_memory("rsync_xal_get");
257 *ptr
= XSTATE_ABBREV
;
260 name_offset
= datum_len
;
262 rxa
= EXPAND_ITEM_LIST(xalp
, rsync_xa
, RSYNC_XAL_INITIAL
);
263 rxa
->name
= ptr
+ name_offset
;
264 memcpy(rxa
->name
, name
, name_len
);
266 rxa
->name_len
= name_len
;
267 rxa
->datum_len
= datum_len
;
272 qsort(rxa
, count
, sizeof (rsync_xa
), rsync_xal_compare_names
);
273 for (rxa
+= count
-1; count
; count
--, rxa
--)
278 /* Read the xattr(s) for this filename. */
279 int get_xattr(const char *fname
, stat_x
*sxp
)
281 sxp
->xattr
= new(item_list
);
282 *sxp
->xattr
= empty_xattr
;
283 if (rsync_xal_get(fname
, sxp
->xattr
) < 0) {
290 static int find_matching_xattr(item_list
*xalp
)
293 item_list
*lst
= rsync_xal_l
.items
;
295 for (i
= 0; i
< rsync_xal_l
.count
; i
++) {
296 rsync_xa
*rxas1
= lst
[i
].items
;
297 rsync_xa
*rxas2
= xalp
->items
;
299 /* Wrong number of elements? */
300 if (lst
[i
].count
!= xalp
->count
)
302 /* any elements different? */
303 for (j
= 0; j
< xalp
->count
; j
++) {
304 if (rxas1
[j
].name_len
!= rxas2
[j
].name_len
305 || rxas1
[j
].datum_len
!= rxas2
[j
].datum_len
306 || strcmp(rxas1
[j
].name
, rxas2
[j
].name
))
308 if (rxas1
[j
].datum_len
> MAX_FULL_DATUM
) {
309 if (memcmp(rxas1
[j
].datum
+ 1,
311 MAX_DIGEST_LEN
) != 0)
314 if (memcmp(rxas1
[j
].datum
, rxas2
[j
].datum
,
319 /* no differences found. This is The One! */
320 if (j
== xalp
->count
)
327 /* Store *xalp on the end of rsync_xal_l */
328 static void rsync_xal_store(item_list
*xalp
)
330 item_list
*new_lst
= EXPAND_ITEM_LIST(&rsync_xal_l
, item_list
, RSYNC_XAL_LIST_INITIAL
);
331 /* Since the following call starts a new list, we know it will hold the
332 * entire initial-count, not just enough space for one new item. */
333 *new_lst
= empty_xattr
;
334 (void)EXPAND_ITEM_LIST(new_lst
, rsync_xa
, xalp
->count
);
335 memcpy(new_lst
->items
, xalp
->items
, xalp
->count
* sizeof (rsync_xa
));
336 new_lst
->count
= xalp
->count
;
340 /* Send the make_xattr()-generated xattr list for this flist entry. */
341 int send_xattr(stat_x
*sxp
, int f
)
343 int ndx
= find_matching_xattr(sxp
->xattr
);
345 /* Send 0 (-1 + 1) to indicate that literal xattr data follows. */
346 write_varint(f
, ndx
+ 1);
350 int count
= sxp
->xattr
->count
;
351 write_varint(f
, count
);
352 for (rxa
= sxp
->xattr
->items
; count
--; rxa
++) {
353 size_t name_len
= rxa
->name_len
;
354 const char *name
= rxa
->name
;
355 /* Strip the rsync prefix from disguised namespaces. */
356 if (name_len
> RPRE_LEN
357 #ifdef HAVE_LINUX_XATTRS
360 && name
[RPRE_LEN
] != '%' && HAS_PREFIX(name
, RSYNC_PREFIX
)) {
362 name_len
-= RPRE_LEN
;
364 #ifndef HAVE_LINUX_XATTRS
366 /* Put everything else in the user namespace. */
367 name_len
+= UPRE_LEN
;
370 write_varint(f
, name_len
);
371 write_varint(f
, rxa
->datum_len
);
372 #ifndef HAVE_LINUX_XATTRS
373 if (name_len
> rxa
->name_len
) {
374 write_buf(f
, USER_PREFIX
, UPRE_LEN
);
375 name_len
-= UPRE_LEN
;
378 write_buf(f
, name
, name_len
);
379 if (rxa
->datum_len
> MAX_FULL_DATUM
)
380 write_buf(f
, rxa
->datum
+ 1, MAX_DIGEST_LEN
);
382 write_buf(f
, rxa
->datum
, rxa
->datum_len
);
384 ndx
= rsync_xal_l
.count
; /* pre-incremented count */
385 rsync_xal_store(sxp
->xattr
); /* adds item to rsync_xal_l */
391 /* Return a flag indicating if we need to change a file's xattrs. If
392 * "find_all" is specified, also mark any abbreviated xattrs that we
393 * need so that send_xattr_request() can tell the sender about them. */
394 int xattr_diff(struct file_struct
*file
, stat_x
*sxp
, int find_all
)
396 item_list
*lst
= rsync_xal_l
.items
;
397 rsync_xa
*snd_rxa
, *rec_rxa
;
398 int snd_cnt
, rec_cnt
;
399 int cmp
, same
, xattrs_equal
= 1;
401 if (sxp
&& XATTR_READY(*sxp
)) {
402 rec_rxa
= sxp
->xattr
->items
;
403 rec_cnt
= sxp
->xattr
->count
;
409 if (F_XATTR(file
) >= 0)
410 lst
+= F_XATTR(file
);
414 snd_rxa
= lst
->items
;
415 snd_cnt
= lst
->count
;
417 /* If the count of the sender's xattrs is different from our
418 * (receiver's) xattrs, the lists are not the same. */
419 if (snd_cnt
!= rec_cnt
) {
426 cmp
= rec_cnt
? strcmp(snd_rxa
->name
, rec_rxa
->name
) : -1;
429 else if (snd_rxa
->datum_len
> MAX_FULL_DATUM
) {
430 same
= cmp
== 0 && snd_rxa
->datum_len
== rec_rxa
->datum_len
431 && memcmp(snd_rxa
->datum
+ 1, rec_rxa
->datum
+ 1,
432 MAX_DIGEST_LEN
) == 0;
433 /* Flag unrequested items that we need. */
434 if (!same
&& find_all
&& snd_rxa
->datum
[0] == XSTATE_ABBREV
)
435 snd_rxa
->datum
[0] = XSTATE_TODO
;
437 same
= cmp
== 0 && snd_rxa
->datum_len
== rec_rxa
->datum_len
438 && memcmp(snd_rxa
->datum
, rec_rxa
->datum
,
439 snd_rxa
->datum_len
) == 0;
460 return !xattrs_equal
;
463 /* When called by the generator with a NULL fname, this tells the sender
464 * which abbreviated xattr values we need. When called by the sender
465 * (with a non-NULL fname), we send all the extra xattr data it needs. */
466 void send_xattr_request(const char *fname
, struct file_struct
*file
, int f_out
)
468 item_list
*lst
= rsync_xal_l
.items
;
469 int cnt
, prior_req
= 0;
472 lst
+= F_XATTR(file
);
473 for (rxa
= lst
->items
, cnt
= lst
->count
; cnt
--; rxa
++) {
474 if (rxa
->datum_len
<= MAX_FULL_DATUM
)
476 switch (rxa
->datum
[0]) {
478 /* Items left abbreviated matched the sender's checksum, so
479 * the receiver will cache the local data for future use. */
481 rxa
->datum
[0] = XSTATE_DONE
;
489 /* Flag that we handled this abbreviated item. */
490 rxa
->datum
[0] = XSTATE_DONE
;
492 write_varint(f_out
, rxa
->num
- prior_req
);
493 prior_req
= rxa
->num
;
499 /* Re-read the long datum. */
500 if (!(ptr
= get_xattr_data(fname
, rxa
->name
, &len
, 0))) {
501 rprintf(FERROR_XFER
, "failed to re-read xattr %s for %s\n", rxa
->name
, fname
);
502 write_varint(f_out
, 0);
506 write_varint(f_out
, len
); /* length might have changed! */
507 write_buf(f_out
, ptr
, len
);
512 write_byte(f_out
, 0); /* end the list */
515 /* When called by the sender, read the request from the generator and mark
516 * any needed xattrs with a flag that lets us know they need to be sent to
517 * the receiver. When called by the receiver, reads the sent data and
518 * stores it in place of its checksum. */
519 int recv_xattr_request(struct file_struct
*file
, int f_in
)
521 item_list
*lst
= rsync_xal_l
.items
;
522 char *old_datum
, *name
;
524 int rel_pos
, cnt
, num
, got_xattr_data
= 0;
526 if (F_XATTR(file
) < 0) {
527 rprintf(FERROR
, "recv_xattr_request: internal data error!\n");
528 exit_cleanup(RERR_STREAMIO
);
530 lst
+= F_XATTR(file
);
535 while ((rel_pos
= read_varint(f_in
)) != 0) {
537 while (cnt
&& rxa
->num
< num
) {
541 if (!cnt
|| rxa
->num
!= num
) {
542 rprintf(FERROR
, "[%s] could not find xattr #%d for %s\n",
543 who_am_i(), num
, f_name(file
, NULL
));
544 exit_cleanup(RERR_STREAMIO
);
546 if (rxa
->datum_len
<= MAX_FULL_DATUM
|| rxa
->datum
[0] != XSTATE_ABBREV
) {
547 rprintf(FERROR
, "[%s] internal abbrev error!\n", who_am_i());
548 exit_cleanup(RERR_STREAMIO
);
552 rxa
->datum
[0] = XSTATE_TODO
;
556 old_datum
= rxa
->datum
;
557 rxa
->datum_len
= read_varint(f_in
);
559 if (rxa
->name_len
+ rxa
->datum_len
< rxa
->name_len
)
560 out_of_memory("recv_xattr_request"); /* overflow */
561 rxa
->datum
= new_array(char, rxa
->datum_len
+ rxa
->name_len
);
563 out_of_memory("recv_xattr_request");
564 name
= rxa
->datum
+ rxa
->datum_len
;
565 memcpy(name
, rxa
->name
, rxa
->name_len
);
568 read_buf(f_in
, rxa
->datum
, rxa
->datum_len
);
572 return got_xattr_data
;
575 /* ------------------------------------------------------------------------- */
577 /* receive and build the rsync_xattr_lists */
578 void receive_xattr(struct file_struct
*file
, int f
)
580 static item_list temp_xattr
= EMPTY_ITEM_LIST
;
582 #ifdef HAVE_LINUX_XATTRS
587 int ndx
= read_varint(f
);
589 if (ndx
< 0 || (size_t)ndx
> rsync_xal_l
.count
) {
590 rprintf(FERROR
, "receive_xattr: xa index %d out of"
591 " range for %s\n", ndx
, f_name(file
, NULL
));
592 exit_cleanup(RERR_STREAMIO
);
596 F_XATTR(file
) = ndx
- 1;
600 if ((count
= read_varint(f
)) != 0) {
601 (void)EXPAND_ITEM_LIST(&temp_xattr
, rsync_xa
, count
);
602 temp_xattr
.count
= 0;
605 for (num
= 1; num
<= count
; num
++) {
608 size_t name_len
= read_varint(f
);
609 size_t datum_len
= read_varint(f
);
610 size_t dget_len
= datum_len
> MAX_FULL_DATUM
? 1 + MAX_DIGEST_LEN
: datum_len
;
611 size_t extra_len
= MIGHT_NEED_RPRE
? RPRE_LEN
: 0;
612 if (dget_len
+ extra_len
< dget_len
)
613 out_of_memory("receive_xattr"); /* overflow */
614 if (dget_len
+ extra_len
+ name_len
< dget_len
)
615 out_of_memory("receive_xattr"); /* overflow */
616 ptr
= new_array(char, dget_len
+ extra_len
+ name_len
);
618 out_of_memory("receive_xattr");
619 name
= ptr
+ dget_len
+ extra_len
;
620 read_buf(f
, name
, name_len
);
621 if (dget_len
== datum_len
)
622 read_buf(f
, ptr
, dget_len
);
624 *ptr
= XSTATE_ABBREV
;
625 read_buf(f
, ptr
+ 1, MAX_DIGEST_LEN
);
627 #ifdef HAVE_LINUX_XATTRS
628 /* Non-root can only save the user namespace. */
629 if (am_root
<= 0 && !HAS_PREFIX(name
, USER_PREFIX
)) {
635 name_len
+= RPRE_LEN
;
636 memcpy(name
, RSYNC_PREFIX
, RPRE_LEN
);
640 /* This OS only has a user namespace, so we either
641 * strip the user prefix, or we put a non-user
642 * namespace inside our rsync hierarchy. */
643 if (HAS_PREFIX(name
, USER_PREFIX
)) {
645 name_len
-= UPRE_LEN
;
646 } else if (am_root
) {
648 name_len
+= RPRE_LEN
;
649 memcpy(name
, RSYNC_PREFIX
, RPRE_LEN
);
655 /* No rsync.%FOO attributes are copied w/o 2 -X options. */
656 if (preserve_xattrs
< 2 && name_len
> RPRE_LEN
657 && name
[RPRE_LEN
] == '%' && HAS_PREFIX(name
, RSYNC_PREFIX
)) {
661 rxa
= EXPAND_ITEM_LIST(&temp_xattr
, rsync_xa
, 1);
664 rxa
->name_len
= name_len
;
665 rxa
->datum_len
= datum_len
;
669 if (need_sort
&& count
> 1)
670 qsort(temp_xattr
.items
, count
, sizeof (rsync_xa
), rsync_xal_compare_names
);
672 ndx
= rsync_xal_l
.count
; /* pre-incremented count */
673 rsync_xal_store(&temp_xattr
); /* adds item to rsync_xal_l */
678 /* Turn the xattr data in stat_x into cached xattr data, setting the index
679 * values in the file struct. */
680 void cache_xattr(struct file_struct
*file
, stat_x
*sxp
)
687 ndx
= find_matching_xattr(sxp
->xattr
);
689 rsync_xal_store(sxp
->xattr
); /* adds item to rsync_xal_l */
694 static int rsync_xal_set(const char *fname
, item_list
*xalp
,
695 const char *fnamecmp
, stat_x
*sxp
)
697 rsync_xa
*rxas
= xalp
->items
;
700 char *name
, *ptr
, sum
[MAX_DIGEST_LEN
];
704 /* This puts the current name list into the "namebuf" buffer. */
705 if ((list_len
= get_xattr_names(fname
)) < 0)
708 for (i
= 0; i
< xalp
->count
; i
++) {
711 if (XATTR_ABBREV(rxas
[i
])) {
712 /* See if the fnamecmp version is identical. */
713 len
= name_len
= rxas
[i
].name_len
;
714 if ((ptr
= get_xattr_data(fnamecmp
, name
, &len
, 1)) == NULL
) {
718 rprintf(FERROR
, "Missing abbreviated xattr value, %s, for %s\n",
719 rxas
[i
].name
, full_fname(fname
));
723 if (len
!= rxas
[i
].datum_len
) {
728 sum_init(checksum_seed
);
729 sum_update(ptr
, len
);
731 if (memcmp(sum
, rxas
[i
].datum
+ 1, MAX_DIGEST_LEN
) != 0) {
736 if (fname
== fnamecmp
)
737 ; /* Value is already set when identical */
738 else if (sys_lsetxattr(fname
, name
, ptr
, len
) < 0) {
739 rsyserr(FERROR_XFER
, errno
,
740 "rsync_xal_set: lsetxattr(\"%s\",\"%s\") failed",
743 } else /* make sure caller sets mtime */
744 sxp
->st
.st_mtime
= (time_t)-1;
746 if (am_generator
) { /* generator items stay abbreviated */
751 memcpy(ptr
+ len
, name
, name_len
);
754 rxas
[i
].name
= name
= ptr
+ len
;
759 if (sys_lsetxattr(fname
, name
, rxas
[i
].datum
, rxas
[i
].datum_len
) < 0) {
760 rsyserr(FERROR_XFER
, errno
,
761 "rsync_xal_set: lsetxattr(\"%s\",\"%s\") failed",
764 } else /* make sure caller sets mtime */
765 sxp
->st
.st_mtime
= (time_t)-1;
768 /* Remove any extraneous names. */
769 for (name
= namebuf
; list_len
> 0; name
+= name_len
) {
770 name_len
= strlen(name
) + 1;
771 list_len
-= name_len
;
773 #ifdef HAVE_LINUX_XATTRS
774 /* We always ignore the system namespace, and non-root
775 * ignores everything but the user namespace. */
776 if (am_root
? HAS_PREFIX(name
, SYSTEM_PREFIX
)
777 : !HAS_PREFIX(name
, USER_PREFIX
))
780 if (am_root
< 0 && name_len
> RPRE_LEN
781 && name
[RPRE_LEN
] == '%' && strcmp(name
, XSTAT_ATTR
) == 0)
784 for (i
= 0; i
< xalp
->count
; i
++) {
785 if (strcmp(name
, rxas
[i
].name
) == 0)
788 if (i
== xalp
->count
) {
789 if (sys_lremovexattr(fname
, name
) < 0) {
790 rsyserr(FERROR_XFER
, errno
,
791 "rsync_xal_clear: lremovexattr(\"%s\",\"%s\") failed",
794 } else /* make sure caller sets mtime */
795 sxp
->st
.st_mtime
= (time_t)-1;
802 /* Set extended attributes on indicated filename. */
803 int set_xattr(const char *fname
, const struct file_struct
*file
,
804 const char *fnamecmp
, stat_x
*sxp
)
807 item_list
*lst
= rsync_xal_l
.items
;
810 return 1; /* FIXME: --dry-run needs to compute this value */
812 if (read_only
|| list_only
) {
818 return rsync_xal_set(fname
, lst
+ ndx
, fnamecmp
, sxp
);
822 char *get_xattr_acl(const char *fname
, int is_access_acl
, size_t *len_p
)
824 const char *name
= is_access_acl
? XACC_ACL_ATTR
: XDEF_ACL_ATTR
;
825 *len_p
= 0; /* no extra data alloc needed from get_xattr_data() */
826 return get_xattr_data(fname
, name
, len_p
, 1);
829 int set_xattr_acl(const char *fname
, int is_access_acl
, const char *buf
, size_t buf_len
)
831 const char *name
= is_access_acl
? XACC_ACL_ATTR
: XDEF_ACL_ATTR
;
832 if (sys_lsetxattr(fname
, name
, buf
, buf_len
) < 0) {
833 rsyserr(FERROR_XFER
, errno
,
834 "set_xattr_acl: lsetxattr(\"%s\",\"%s\") failed",
841 int del_def_xattr_acl(const char *fname
)
843 return sys_lremovexattr(fname
, XDEF_ACL_ATTR
);
847 int get_stat_xattr(const char *fname
, int fd
, STRUCT_STAT
*fst
, STRUCT_STAT
*xst
)
849 int mode
, rdev_major
, rdev_minor
, uid
, gid
, len
;
852 if (am_root
>= 0 || IS_DEVICE(fst
->st_mode
) || IS_SPECIAL(fst
->st_mode
))
861 len
= sys_lgetxattr(fname
, XSTAT_ATTR
, buf
, sizeof buf
- 1);
864 len
= sys_fgetxattr(fd
, XSTAT_ATTR
, buf
, sizeof buf
- 1);
866 if (len
>= (int)sizeof buf
) {
871 if (errno
== ENOTSUP
|| errno
== ENOATTR
)
873 if (errno
== EPERM
&& S_ISLNK(fst
->st_mode
)) {
878 rsyserr(FERROR_XFER
, errno
, "failed to read xattr %s for %s",
879 XSTAT_ATTR
, full_fname(fname
));
884 if (sscanf(buf
, "%o %d,%d %d:%d",
885 &mode
, &rdev_major
, &rdev_minor
, &uid
, &gid
) != 5) {
886 rprintf(FERROR
, "Corrupt %s xattr attached to %s: \"%s\"\n",
887 XSTAT_ATTR
, full_fname(fname
), buf
);
888 exit_cleanup(RERR_FILEIO
);
891 xst
->st_mode
= from_wire_mode(mode
);
892 xst
->st_rdev
= MAKEDEV(rdev_major
, rdev_minor
);
899 int set_stat_xattr(const char *fname
, struct file_struct
*file
, mode_t new_mode
)
901 STRUCT_STAT fst
, xst
;
908 if (read_only
|| list_only
) {
909 rsyserr(FERROR_XFER
, EROFS
, "failed to write xattr %s for %s",
910 XSTAT_ATTR
, full_fname(fname
));
914 if (x_lstat(fname
, &fst
, &xst
) < 0) {
915 rsyserr(FERROR_XFER
, errno
, "failed to re-stat %s",
920 fst
.st_mode
&= (_S_IFMT
| CHMOD_BITS
);
921 fmode
= new_mode
& (_S_IFMT
| CHMOD_BITS
);
923 if (IS_DEVICE(fmode
) || IS_SPECIAL(fmode
)) {
924 uint32
*devp
= F_RDEV_P(file
);
925 rdev
= MAKEDEV(DEV_MAJOR(devp
), DEV_MINOR(devp
));
929 /* Dump the special permissions and enable full owner access. */
930 mode
= (fst
.st_mode
& _S_IFMT
) | (fmode
& ACCESSPERMS
)
931 | (S_ISDIR(fst
.st_mode
) ? 0700 : 0600);
932 if (fst
.st_mode
!= mode
)
933 do_chmod(fname
, mode
);
934 if (!IS_DEVICE(fst
.st_mode
) && !IS_SPECIAL(fst
.st_mode
))
935 fst
.st_rdev
= 0; /* just in case */
937 if (mode
== fmode
&& fst
.st_rdev
== rdev
938 && fst
.st_uid
== F_OWNER(file
) && fst
.st_gid
== F_GROUP(file
)) {
939 /* xst.st_mode will be 0 if there's no current stat xattr */
940 if (xst
.st_mode
&& sys_lremovexattr(fname
, XSTAT_ATTR
) < 0) {
941 rsyserr(FERROR_XFER
, errno
,
942 "delete of stat xattr failed for %s",
949 if (xst
.st_mode
!= fmode
|| xst
.st_rdev
!= rdev
950 || xst
.st_uid
!= F_OWNER(file
) || xst
.st_gid
!= F_GROUP(file
)) {
952 int len
= snprintf(buf
, sizeof buf
, "%o %u,%u %u:%u",
954 (int)major(rdev
), (int)minor(rdev
),
955 F_OWNER(file
), F_GROUP(file
));
956 if (sys_lsetxattr(fname
, XSTAT_ATTR
, buf
, len
) < 0) {
957 if (errno
== EPERM
&& S_ISLNK(fst
.st_mode
))
959 rsyserr(FERROR_XFER
, errno
,
960 "failed to write xattr %s for %s",
961 XSTAT_ATTR
, full_fname(fname
));
969 int x_stat(const char *fname
, STRUCT_STAT
*fst
, STRUCT_STAT
*xst
)
971 int ret
= do_stat(fname
, fst
);
972 if ((ret
< 0 || get_stat_xattr(fname
, -1, fst
, xst
) < 0) && xst
)
977 int x_lstat(const char *fname
, STRUCT_STAT
*fst
, STRUCT_STAT
*xst
)
979 int ret
= do_lstat(fname
, fst
);
980 if ((ret
< 0 || get_stat_xattr(fname
, -1, fst
, xst
) < 0) && xst
)
985 int x_fstat(int fd
, STRUCT_STAT
*fst
, STRUCT_STAT
*xst
)
987 int ret
= do_fstat(fd
, fst
);
988 if ((ret
< 0 || get_stat_xattr(NULL
, fd
, fst
, xst
) < 0) && xst
)
993 #endif /* SUPPORT_XATTRS */