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_ATTR RSYNC_PREFIX "%stat"
66 #define XACC_ACL_ATTR RSYNC_PREFIX "%aacl"
67 #define XDEF_ACL_ATTR RSYNC_PREFIX "%dacl"
71 size_t datum_len
, name_len
;
75 static size_t namebuf_len
= 0;
76 static char *namebuf
= NULL
;
78 static item_list empty_xattr
= EMPTY_ITEM_LIST
;
79 static item_list rsync_xal_l
= EMPTY_ITEM_LIST
;
81 /* ------------------------------------------------------------------------- */
83 static void rsync_xal_free(item_list
*xalp
)
86 rsync_xa
*rxas
= xalp
->items
;
88 for (i
= 0; i
< xalp
->count
; i
++) {
90 /*free(rxas[i].name);*/
95 void free_xattr(stat_x
*sxp
)
99 rsync_xal_free(sxp
->xattr
);
104 static int rsync_xal_compare_names(const void *x1
, const void *x2
)
106 const rsync_xa
*xa1
= x1
;
107 const rsync_xa
*xa2
= x2
;
108 return strcmp(xa1
->name
, xa2
->name
);
111 static ssize_t
get_xattr_names(const char *fname
)
118 namebuf
= new_array(char, namebuf_len
);
120 out_of_memory("get_xattr_names");
124 /* The length returned includes all the '\0' terminators. */
125 list_len
= sys_llistxattr(fname
, namebuf
, namebuf_len
);
127 if ((size_t)list_len
<= namebuf_len
)
129 } else if (errno
== ENOTSUP
)
131 else if (errno
!= ERANGE
) {
132 arg
= (double)namebuf_len
;
134 rsyserr(FERROR_XFER
, errno
,
135 "get_xattr_names: llistxattr(\"%s\",%.0f) failed",
139 list_len
= sys_llistxattr(fname
, NULL
, 0);
146 namebuf_len
= list_len
+ 1024;
147 namebuf
= new_array(char, namebuf_len
);
149 out_of_memory("get_xattr_names");
155 /* On entry, the *len_ptr parameter contains the size of the extra space we
156 * should allocate when we create a buffer for the data. On exit, it contains
157 * the length of the datum. */
158 static char *get_xattr_data(const char *fname
, const char *name
, size_t *len_ptr
,
159 int no_missing_error
)
161 size_t datum_len
= sys_lgetxattr(fname
, name
, NULL
, 0);
162 size_t extra_len
= *len_ptr
;
165 *len_ptr
= datum_len
;
167 if (datum_len
== (size_t)-1) {
168 if (errno
== ENOTSUP
|| no_missing_error
)
170 rsyserr(FERROR_XFER
, errno
,
171 "get_xattr_data: lgetxattr(\"%s\",\"%s\",0) failed",
176 if (!datum_len
&& !extra_len
)
177 extra_len
= 1; /* request non-zero amount of memory */
178 if (datum_len
+ extra_len
< datum_len
/* checks for overflow */
179 || !(ptr
= new_array(char, datum_len
+ extra_len
)))
180 out_of_memory("get_xattr_data");
183 size_t len
= sys_lgetxattr(fname
, name
, ptr
, datum_len
);
184 if (len
!= datum_len
) {
185 if (len
== (size_t)-1) {
186 rsyserr(FERROR_XFER
, errno
,
187 "get_xattr_data: lgetxattr(\"%s\",\"%s\",%ld)"
188 " failed", fname
, name
, (long)datum_len
);
191 "get_xattr_data: lgetxattr(\"%s\",\"%s\",%ld)"
192 " returned %ld\n", fname
, name
,
193 (long)datum_len
, (long)len
);
203 static int rsync_xal_get(const char *fname
, item_list
*xalp
)
205 ssize_t list_len
, name_len
;
206 size_t datum_len
, name_offset
;
208 #ifdef HAVE_LINUX_XATTRS
209 int user_only
= am_sender
? 0 : !am_root
;
214 /* This puts the name list into the "namebuf" buffer. */
215 if ((list_len
= get_xattr_names(fname
)) < 0)
218 for (name
= namebuf
; list_len
> 0; name
+= name_len
) {
219 name_len
= strlen(name
) + 1;
220 list_len
-= name_len
;
222 #ifdef HAVE_LINUX_XATTRS
223 /* We always ignore the system namespace, and non-root
224 * ignores everything but the user namespace. */
225 if (user_only
? !HAS_PREFIX(name
, USER_PREFIX
)
226 : HAS_PREFIX(name
, SYSTEM_PREFIX
))
230 /* No rsync.%FOO attributes are copied w/o 2 -X options. */
231 if (name_len
> RPRE_LEN
&& name
[RPRE_LEN
] == '%'
232 && HAS_PREFIX(name
, RSYNC_PREFIX
)) {
233 if ((am_sender
&& preserve_xattrs
< 2)
234 || (am_root
< 0 && strcmp(name
, XSTAT_ATTR
) == 0))
238 datum_len
= name_len
; /* Pass extra size to get_xattr_data() */
239 if (!(ptr
= get_xattr_data(fname
, name
, &datum_len
, 0)))
242 if (datum_len
> MAX_FULL_DATUM
) {
243 /* For large datums, we store a flag and a checksum. */
244 name_offset
= 1 + MAX_DIGEST_LEN
;
245 sum_init(checksum_seed
);
246 sum_update(ptr
, datum_len
);
249 if (!(ptr
= new_array(char, name_offset
+ name_len
)))
250 out_of_memory("rsync_xal_get");
251 *ptr
= XSTATE_ABBREV
;
254 name_offset
= datum_len
;
256 #ifdef HAVE_LINUX_XATTRS
257 if (am_root
< 0 && name_len
> RPRE_LEN
&& name
[RPRE_LEN
] != '%'
258 && HAS_PREFIX(name
, RSYNC_PREFIX
)) {
260 name_len
-= RPRE_LEN
;
264 rxa
= EXPAND_ITEM_LIST(xalp
, rsync_xa
, RSYNC_XAL_INITIAL
);
265 rxa
->name
= ptr
+ name_offset
;
266 memcpy(rxa
->name
, name
, name_len
);
268 rxa
->name_len
= name_len
;
269 rxa
->datum_len
= datum_len
;
274 qsort(rxa
, count
, sizeof (rsync_xa
), rsync_xal_compare_names
);
275 for (rxa
+= count
-1; count
; count
--, rxa
--)
280 /* Read the xattr(s) for this filename. */
281 int get_xattr(const char *fname
, stat_x
*sxp
)
283 sxp
->xattr
= new(item_list
);
284 *sxp
->xattr
= empty_xattr
;
285 if (rsync_xal_get(fname
, sxp
->xattr
) < 0) {
292 static int find_matching_xattr(item_list
*xalp
)
295 item_list
*lst
= rsync_xal_l
.items
;
297 for (i
= 0; i
< rsync_xal_l
.count
; i
++) {
298 rsync_xa
*rxas1
= lst
[i
].items
;
299 rsync_xa
*rxas2
= xalp
->items
;
301 /* Wrong number of elements? */
302 if (lst
[i
].count
!= xalp
->count
)
304 /* any elements different? */
305 for (j
= 0; j
< xalp
->count
; j
++) {
306 if (rxas1
[j
].name_len
!= rxas2
[j
].name_len
307 || rxas1
[j
].datum_len
!= rxas2
[j
].datum_len
308 || strcmp(rxas1
[j
].name
, rxas2
[j
].name
))
310 if (rxas1
[j
].datum_len
> MAX_FULL_DATUM
) {
311 if (memcmp(rxas1
[j
].datum
+ 1,
313 MAX_DIGEST_LEN
) != 0)
316 if (memcmp(rxas1
[j
].datum
, rxas2
[j
].datum
,
321 /* no differences found. This is The One! */
322 if (j
== xalp
->count
)
329 /* Store *xalp on the end of rsync_xal_l */
330 static void rsync_xal_store(item_list
*xalp
)
332 item_list
*new_lst
= EXPAND_ITEM_LIST(&rsync_xal_l
, item_list
, RSYNC_XAL_LIST_INITIAL
);
333 /* Since the following call starts a new list, we know it will hold the
334 * entire initial-count, not just enough space for one new item. */
335 *new_lst
= empty_xattr
;
336 (void)EXPAND_ITEM_LIST(new_lst
, rsync_xa
, xalp
->count
);
337 memcpy(new_lst
->items
, xalp
->items
, xalp
->count
* sizeof (rsync_xa
));
338 new_lst
->count
= xalp
->count
;
342 /* Send the make_xattr()-generated xattr list for this flist entry. */
343 int send_xattr(stat_x
*sxp
, int f
)
345 int ndx
= find_matching_xattr(sxp
->xattr
);
347 /* Send 0 (-1 + 1) to indicate that literal xattr data follows. */
348 write_varint(f
, ndx
+ 1);
352 int count
= sxp
->xattr
->count
;
353 write_varint(f
, count
);
354 for (rxa
= sxp
->xattr
->items
; count
--; rxa
++) {
355 #ifdef HAVE_LINUX_XATTRS
356 write_varint(f
, rxa
->name_len
);
357 write_varint(f
, rxa
->datum_len
);
358 write_buf(f
, rxa
->name
, rxa
->name_len
);
360 /* We strip the rsync prefix from disguised namespaces
361 * and put everything else in the user namespace. */
362 if (HAS_PREFIX(rxa
->name
, RSYNC_PREFIX
)
363 && rxa
->name
[RPRE_LEN
] != '%') {
364 write_varint(f
, rxa
->name_len
- RPRE_LEN
);
365 write_varint(f
, rxa
->datum_len
);
366 write_buf(f
, rxa
->name
+ RPRE_LEN
, rxa
->name_len
- RPRE_LEN
);
368 write_varint(f
, rxa
->name_len
+ UPRE_LEN
);
369 write_varint(f
, rxa
->datum_len
);
370 write_buf(f
, USER_PREFIX
, UPRE_LEN
);
371 write_buf(f
, rxa
->name
, rxa
->name_len
);
374 if (rxa
->datum_len
> MAX_FULL_DATUM
)
375 write_buf(f
, rxa
->datum
+ 1, MAX_DIGEST_LEN
);
377 write_buf(f
, rxa
->datum
, rxa
->datum_len
);
379 ndx
= rsync_xal_l
.count
; /* pre-incremented count */
380 rsync_xal_store(sxp
->xattr
); /* adds item to rsync_xal_l */
386 /* Return a flag indicating if we need to change a file's xattrs. If
387 * "find_all" is specified, also mark any abbreviated xattrs that we
388 * need so that send_xattr_request() can tell the sender about them. */
389 int xattr_diff(struct file_struct
*file
, stat_x
*sxp
, int find_all
)
391 item_list
*lst
= rsync_xal_l
.items
;
392 rsync_xa
*snd_rxa
, *rec_rxa
;
393 int snd_cnt
, rec_cnt
;
394 int cmp
, same
, xattrs_equal
= 1;
396 if (sxp
&& XATTR_READY(*sxp
)) {
397 rec_rxa
= sxp
->xattr
->items
;
398 rec_cnt
= sxp
->xattr
->count
;
404 if (F_XATTR(file
) >= 0)
405 lst
+= F_XATTR(file
);
409 snd_rxa
= lst
->items
;
410 snd_cnt
= lst
->count
;
412 /* If the count of the sender's xattrs is different from our
413 * (receiver's) xattrs, the lists are not the same. */
414 if (snd_cnt
!= rec_cnt
) {
421 cmp
= rec_cnt
? strcmp(snd_rxa
->name
, rec_rxa
->name
) : -1;
424 else if (snd_rxa
->datum_len
> MAX_FULL_DATUM
) {
425 same
= cmp
== 0 && snd_rxa
->datum_len
== rec_rxa
->datum_len
426 && memcmp(snd_rxa
->datum
+ 1, rec_rxa
->datum
+ 1,
427 MAX_DIGEST_LEN
) == 0;
428 /* Flag unrequested items that we need. */
429 if (!same
&& find_all
&& snd_rxa
->datum
[0] == XSTATE_ABBREV
)
430 snd_rxa
->datum
[0] = XSTATE_TODO
;
432 same
= cmp
== 0 && snd_rxa
->datum_len
== rec_rxa
->datum_len
433 && memcmp(snd_rxa
->datum
, rec_rxa
->datum
,
434 snd_rxa
->datum_len
) == 0;
455 return !xattrs_equal
;
458 /* When called by the generator with a NULL fname, this tells the sender
459 * which abbreviated xattr values we need. When called by the sender
460 * (with a non-NULL fname), we send all the extra xattr data it needs. */
461 void send_xattr_request(const char *fname
, struct file_struct
*file
, int f_out
)
463 item_list
*lst
= rsync_xal_l
.items
;
464 int cnt
, prior_req
= 0;
467 lst
+= F_XATTR(file
);
468 for (rxa
= lst
->items
, cnt
= lst
->count
; cnt
--; rxa
++) {
469 if (rxa
->datum_len
<= MAX_FULL_DATUM
)
471 switch (rxa
->datum
[0]) {
473 /* Items left abbreviated matched the sender's checksum, so
474 * the receiver will cache the local data for future use. */
476 rxa
->datum
[0] = XSTATE_DONE
;
484 /* Flag that we handled this abbreviated item. */
485 rxa
->datum
[0] = XSTATE_DONE
;
487 write_varint(f_out
, rxa
->num
- prior_req
);
488 prior_req
= rxa
->num
;
494 /* Re-read the long datum. */
495 if (!(ptr
= get_xattr_data(fname
, rxa
->name
, &len
, 0))) {
496 rprintf(FERROR_XFER
, "failed to re-read xattr %s for %s\n", rxa
->name
, fname
);
497 write_varint(f_out
, 0);
501 write_varint(f_out
, len
); /* length might have changed! */
502 write_buf(f_out
, ptr
, len
);
507 write_byte(f_out
, 0); /* end the list */
510 /* When called by the sender, read the request from the generator and mark
511 * any needed xattrs with a flag that lets us know they need to be sent to
512 * the receiver. When called by the receiver, reads the sent data and
513 * stores it in place of its checksum. */
514 int recv_xattr_request(struct file_struct
*file
, int f_in
)
516 item_list
*lst
= rsync_xal_l
.items
;
517 char *old_datum
, *name
;
519 int rel_pos
, cnt
, num
, got_xattr_data
= 0;
521 if (F_XATTR(file
) < 0) {
522 rprintf(FERROR
, "recv_xattr_request: internal data error!\n");
523 exit_cleanup(RERR_STREAMIO
);
525 lst
+= F_XATTR(file
);
530 while ((rel_pos
= read_varint(f_in
)) != 0) {
532 while (cnt
&& rxa
->num
< num
) {
536 if (!cnt
|| rxa
->num
!= num
) {
537 rprintf(FERROR
, "[%s] could not find xattr #%d for %s\n",
538 who_am_i(), num
, f_name(file
, NULL
));
539 exit_cleanup(RERR_STREAMIO
);
541 if (rxa
->datum_len
<= MAX_FULL_DATUM
|| rxa
->datum
[0] != XSTATE_ABBREV
) {
542 rprintf(FERROR
, "[%s] internal abbrev error!\n", who_am_i());
543 exit_cleanup(RERR_STREAMIO
);
547 rxa
->datum
[0] = XSTATE_TODO
;
551 old_datum
= rxa
->datum
;
552 rxa
->datum_len
= read_varint(f_in
);
554 if (rxa
->name_len
+ rxa
->datum_len
< rxa
->name_len
)
555 out_of_memory("recv_xattr_request"); /* overflow */
556 rxa
->datum
= new_array(char, rxa
->datum_len
+ rxa
->name_len
);
558 out_of_memory("recv_xattr_request");
559 name
= rxa
->datum
+ rxa
->datum_len
;
560 memcpy(name
, rxa
->name
, rxa
->name_len
);
563 read_buf(f_in
, rxa
->datum
, rxa
->datum_len
);
567 return got_xattr_data
;
570 /* ------------------------------------------------------------------------- */
572 /* receive and build the rsync_xattr_lists */
573 void receive_xattr(struct file_struct
*file
, int f
)
575 static item_list temp_xattr
= EMPTY_ITEM_LIST
;
577 int ndx
= read_varint(f
);
579 if (ndx
< 0 || (size_t)ndx
> rsync_xal_l
.count
) {
580 rprintf(FERROR
, "receive_xattr: xa index %d out of"
581 " range for %s\n", ndx
, f_name(file
, NULL
));
582 exit_cleanup(RERR_STREAMIO
);
586 F_XATTR(file
) = ndx
- 1;
590 if ((count
= read_varint(f
)) != 0) {
591 (void)EXPAND_ITEM_LIST(&temp_xattr
, rsync_xa
, count
);
592 temp_xattr
.count
= 0;
595 for (num
= 1; num
<= count
; num
++) {
598 size_t name_len
= read_varint(f
);
599 size_t datum_len
= read_varint(f
);
600 size_t dget_len
= datum_len
> MAX_FULL_DATUM
? 1 + MAX_DIGEST_LEN
: datum_len
;
601 size_t extra_len
= MIGHT_NEED_RPRE
? RPRE_LEN
: 0;
602 if (dget_len
+ extra_len
< dget_len
)
603 out_of_memory("receive_xattr"); /* overflow */
604 if (dget_len
+ extra_len
+ name_len
< dget_len
)
605 out_of_memory("receive_xattr"); /* overflow */
606 ptr
= new_array(char, dget_len
+ extra_len
+ name_len
);
608 out_of_memory("receive_xattr");
609 name
= ptr
+ dget_len
+ extra_len
;
610 read_buf(f
, name
, name_len
);
611 if (dget_len
== datum_len
)
612 read_buf(f
, ptr
, dget_len
);
614 *ptr
= XSTATE_ABBREV
;
615 read_buf(f
, ptr
+ 1, MAX_DIGEST_LEN
);
617 #ifdef HAVE_LINUX_XATTRS
618 /* Non-root can only save the user namespace. */
619 if (am_root
<= 0 && !HAS_PREFIX(name
, USER_PREFIX
)) {
625 name_len
+= RPRE_LEN
;
626 memcpy(name
, RSYNC_PREFIX
, RPRE_LEN
);
629 /* This OS only has a user namespace, so we either
630 * strip the user prefix, or we put a non-user
631 * namespace inside our rsync hierarchy. */
632 if (HAS_PREFIX(name
, USER_PREFIX
)) {
634 name_len
-= UPRE_LEN
;
635 } else if (am_root
) {
637 name_len
+= RPRE_LEN
;
638 memcpy(name
, RSYNC_PREFIX
, RPRE_LEN
);
644 /* No rsync.%FOO attributes are copied w/o 2 -X options. */
645 if (preserve_xattrs
< 2 && name_len
> RPRE_LEN
646 && name
[RPRE_LEN
] == '%' && HAS_PREFIX(name
, RSYNC_PREFIX
)) {
650 rxa
= EXPAND_ITEM_LIST(&temp_xattr
, rsync_xa
, 1);
653 rxa
->name_len
= name_len
;
654 rxa
->datum_len
= datum_len
;
658 ndx
= rsync_xal_l
.count
; /* pre-incremented count */
659 rsync_xal_store(&temp_xattr
); /* adds item to rsync_xal_l */
664 /* Turn the xattr data in stat_x into cached xattr data, setting the index
665 * values in the file struct. */
666 void cache_xattr(struct file_struct
*file
, stat_x
*sxp
)
673 ndx
= find_matching_xattr(sxp
->xattr
);
675 rsync_xal_store(sxp
->xattr
); /* adds item to rsync_xal_l */
680 static int rsync_xal_set(const char *fname
, item_list
*xalp
,
681 const char *fnamecmp
, stat_x
*sxp
)
683 rsync_xa
*rxas
= xalp
->items
;
686 char *name
, *ptr
, sum
[MAX_DIGEST_LEN
];
687 int name_len
, ret
= 0;
689 /* This puts the current name list into the "namebuf" buffer. */
690 if ((list_len
= get_xattr_names(fname
)) < 0)
693 for (i
= 0; i
< xalp
->count
; i
++) {
696 if (XATTR_ABBREV(rxas
[i
])) {
697 /* See if the fnamecmp version is identical. */
698 len
= name_len
= rxas
[i
].name_len
;
699 if ((ptr
= get_xattr_data(fnamecmp
, name
, &len
, 1)) == NULL
) {
703 rprintf(FERROR
, "Missing abbreviated xattr value, %s, for %s\n",
704 rxas
[i
].name
, full_fname(fname
));
708 if (len
!= rxas
[i
].datum_len
) {
713 sum_init(checksum_seed
);
714 sum_update(ptr
, len
);
716 if (memcmp(sum
, rxas
[i
].datum
+ 1, MAX_DIGEST_LEN
) != 0) {
721 if (fname
== fnamecmp
)
722 ; /* Value is already set when identical */
723 else if (sys_lsetxattr(fname
, name
, ptr
, len
) < 0) {
724 rsyserr(FERROR_XFER
, errno
,
725 "rsync_xal_set: lsetxattr(\"%s\",\"%s\") failed",
728 } else /* make sure caller sets mtime */
729 sxp
->st
.st_mtime
= (time_t)-1;
731 if (am_generator
) { /* generator items stay abbreviated */
736 memcpy(ptr
+ len
, name
, name_len
);
739 rxas
[i
].name
= name
= ptr
+ len
;
744 if (sys_lsetxattr(fname
, name
, rxas
[i
].datum
, rxas
[i
].datum_len
) < 0) {
745 rsyserr(FERROR_XFER
, errno
,
746 "rsync_xal_set: lsetxattr(\"%s\",\"%s\") failed",
749 } else /* make sure caller sets mtime */
750 sxp
->st
.st_mtime
= (time_t)-1;
753 /* Remove any extraneous names. */
754 for (name
= namebuf
; list_len
> 0; name
+= name_len
) {
755 name_len
= strlen(name
) + 1;
756 list_len
-= name_len
;
758 #ifdef HAVE_LINUX_XATTRS
759 /* We always ignore the system namespace, and non-root
760 * ignores everything but the user namespace. */
761 if (am_root
? HAS_PREFIX(name
, SYSTEM_PREFIX
)
762 : !HAS_PREFIX(name
, USER_PREFIX
))
765 if (am_root
< 0 && name_len
> RPRE_LEN
766 && name
[RPRE_LEN
] == '%' && strcmp(name
, XSTAT_ATTR
) == 0)
769 for (i
= 0; i
< xalp
->count
; i
++) {
770 if (strcmp(name
, rxas
[i
].name
) == 0)
773 if (i
== xalp
->count
) {
774 if (sys_lremovexattr(fname
, name
) < 0) {
775 rsyserr(FERROR_XFER
, errno
,
776 "rsync_xal_clear: lremovexattr(\"%s\",\"%s\") failed",
779 } else /* make sure caller sets mtime */
780 sxp
->st
.st_mtime
= (time_t)-1;
787 /* Set extended attributes on indicated filename. */
788 int set_xattr(const char *fname
, const struct file_struct
*file
,
789 const char *fnamecmp
, stat_x
*sxp
)
792 item_list
*lst
= rsync_xal_l
.items
;
795 return 1; /* FIXME: --dry-run needs to compute this value */
797 if (read_only
|| list_only
) {
803 return rsync_xal_set(fname
, lst
+ ndx
, fnamecmp
, sxp
);
807 char *get_xattr_acl(const char *fname
, int is_access_acl
, size_t *len_p
)
809 const char *name
= is_access_acl
? XACC_ACL_ATTR
: XDEF_ACL_ATTR
;
810 *len_p
= 0; /* no extra data alloc needed from get_xattr_data() */
811 return get_xattr_data(fname
, name
, len_p
, 1);
814 int set_xattr_acl(const char *fname
, int is_access_acl
, const char *buf
, size_t buf_len
)
816 const char *name
= is_access_acl
? XACC_ACL_ATTR
: XDEF_ACL_ATTR
;
817 if (sys_lsetxattr(fname
, name
, buf
, buf_len
) < 0) {
818 rsyserr(FERROR_XFER
, errno
,
819 "set_xattr_acl: lsetxattr(\"%s\",\"%s\") failed",
826 int del_def_xattr_acl(const char *fname
)
828 return sys_lremovexattr(fname
, XDEF_ACL_ATTR
);
832 int get_stat_xattr(const char *fname
, int fd
, STRUCT_STAT
*fst
, STRUCT_STAT
*xst
)
834 int mode
, rdev_major
, rdev_minor
, uid
, gid
, len
;
837 if (am_root
>= 0 || IS_DEVICE(fst
->st_mode
) || IS_SPECIAL(fst
->st_mode
))
846 len
= sys_lgetxattr(fname
, XSTAT_ATTR
, buf
, sizeof buf
- 1);
849 len
= sys_fgetxattr(fd
, XSTAT_ATTR
, buf
, sizeof buf
- 1);
851 if (len
>= (int)sizeof buf
) {
856 if (errno
== ENOTSUP
|| errno
== ENOATTR
)
858 if (errno
== EPERM
&& S_ISLNK(fst
->st_mode
)) {
863 rsyserr(FERROR_XFER
, errno
, "failed to read xattr %s for %s",
864 XSTAT_ATTR
, full_fname(fname
));
869 if (sscanf(buf
, "%o %d,%d %d:%d",
870 &mode
, &rdev_major
, &rdev_minor
, &uid
, &gid
) != 5) {
871 rprintf(FERROR
, "Corrupt %s xattr attached to %s: \"%s\"\n",
872 XSTAT_ATTR
, full_fname(fname
), buf
);
873 exit_cleanup(RERR_FILEIO
);
876 xst
->st_mode
= from_wire_mode(mode
);
877 xst
->st_rdev
= MAKEDEV(rdev_major
, rdev_minor
);
884 int set_stat_xattr(const char *fname
, struct file_struct
*file
, mode_t new_mode
)
886 STRUCT_STAT fst
, xst
;
893 if (read_only
|| list_only
) {
894 rsyserr(FERROR_XFER
, EROFS
, "failed to write xattr %s for %s",
895 XSTAT_ATTR
, full_fname(fname
));
899 if (x_lstat(fname
, &fst
, &xst
) < 0) {
900 rsyserr(FERROR_XFER
, errno
, "failed to re-stat %s",
905 fst
.st_mode
&= (_S_IFMT
| CHMOD_BITS
);
906 fmode
= new_mode
& (_S_IFMT
| CHMOD_BITS
);
908 if (IS_DEVICE(fmode
) || IS_SPECIAL(fmode
)) {
909 uint32
*devp
= F_RDEV_P(file
);
910 rdev
= MAKEDEV(DEV_MAJOR(devp
), DEV_MINOR(devp
));
914 /* Dump the special permissions and enable full owner access. */
915 mode
= (fst
.st_mode
& _S_IFMT
) | (fmode
& ACCESSPERMS
)
916 | (S_ISDIR(fst
.st_mode
) ? 0700 : 0600);
917 if (fst
.st_mode
!= mode
)
918 do_chmod(fname
, mode
);
919 if (!IS_DEVICE(fst
.st_mode
) && !IS_SPECIAL(fst
.st_mode
))
920 fst
.st_rdev
= 0; /* just in case */
922 if (mode
== fmode
&& fst
.st_rdev
== rdev
923 && fst
.st_uid
== F_OWNER(file
) && fst
.st_gid
== F_GROUP(file
)) {
924 /* xst.st_mode will be 0 if there's no current stat xattr */
925 if (xst
.st_mode
&& sys_lremovexattr(fname
, XSTAT_ATTR
) < 0) {
926 rsyserr(FERROR_XFER
, errno
,
927 "delete of stat xattr failed for %s",
934 if (xst
.st_mode
!= fmode
|| xst
.st_rdev
!= rdev
935 || xst
.st_uid
!= F_OWNER(file
) || xst
.st_gid
!= F_GROUP(file
)) {
937 int len
= snprintf(buf
, sizeof buf
, "%o %u,%u %u:%u",
939 (int)major(rdev
), (int)minor(rdev
),
940 F_OWNER(file
), F_GROUP(file
));
941 if (sys_lsetxattr(fname
, XSTAT_ATTR
, buf
, len
) < 0) {
942 if (errno
== EPERM
&& S_ISLNK(fst
.st_mode
))
944 rsyserr(FERROR_XFER
, errno
,
945 "failed to write xattr %s for %s",
946 XSTAT_ATTR
, full_fname(fname
));
954 int x_stat(const char *fname
, STRUCT_STAT
*fst
, STRUCT_STAT
*xst
)
956 int ret
= do_stat(fname
, fst
);
957 if ((ret
< 0 || get_stat_xattr(fname
, -1, fst
, xst
) < 0) && xst
)
962 int x_lstat(const char *fname
, STRUCT_STAT
*fst
, STRUCT_STAT
*xst
)
964 int ret
= do_lstat(fname
, fst
);
965 if ((ret
< 0 || get_stat_xattr(fname
, -1, fst
, xst
) < 0) && xst
)
970 int x_fstat(int fd
, STRUCT_STAT
*fst
, STRUCT_STAT
*xst
)
972 int ret
= do_fstat(fd
, fst
);
973 if ((ret
< 0 || get_stat_xattr(NULL
, fd
, fst
, xst
) < 0) && xst
)
978 #endif /* SUPPORT_XATTRS */