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.
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 static size_t prior_xattr_count = (size_t)-1;
86 /* ------------------------------------------------------------------------- */
88 static void rsync_xal_free(item_list *xalp)
91 rsync_xa *rxas = xalp->items;
93 for (i = 0; i < xalp->count; i++) {
95 /*free(rxas[i].name);*/
100 void free_xattr(stat_x *sxp)
104 rsync_xal_free(sxp->xattr);
109 static int rsync_xal_compare_names(const void *x1, const void *x2)
111 const rsync_xa *xa1 = x1;
112 const rsync_xa *xa2 = x2;
113 return strcmp(xa1->name, xa2->name);
116 static ssize_t get_xattr_names(const char *fname)
123 namebuf = new_array(char, namebuf_len);
125 out_of_memory("get_xattr_names");
129 /* The length returned includes all the '\0' terminators. */
130 list_len = sys_llistxattr(fname, namebuf, namebuf_len);
132 if ((size_t)list_len <= namebuf_len)
134 } else if (errno == ENOTSUP)
136 else if (errno != ERANGE) {
137 arg = (double)namebuf_len;
139 rsyserr(FERROR_XFER, errno,
140 "get_xattr_names: llistxattr(\"%s\",%.0f) failed",
144 list_len = sys_llistxattr(fname, NULL, 0);
151 namebuf_len = list_len + 1024;
152 namebuf = new_array(char, namebuf_len);
154 out_of_memory("get_xattr_names");
160 /* On entry, the *len_ptr parameter contains the size of the extra space we
161 * should allocate when we create a buffer for the data. On exit, it contains
162 * the length of the datum. */
163 static char *get_xattr_data(const char *fname, const char *name, size_t *len_ptr,
164 int no_missing_error)
166 size_t datum_len = sys_lgetxattr(fname, name, NULL, 0);
167 size_t extra_len = *len_ptr;
170 *len_ptr = datum_len;
172 if (datum_len == (size_t)-1) {
173 if (errno == ENOTSUP || no_missing_error)
175 rsyserr(FERROR_XFER, errno,
176 "get_xattr_data: lgetxattr(\"%s\",\"%s\",0) failed",
181 if (!datum_len && !extra_len)
182 extra_len = 1; /* request non-zero amount of memory */
183 if (datum_len + extra_len < datum_len)
184 overflow_exit("get_xattr_data");
185 if (!(ptr = new_array(char, datum_len + extra_len)))
186 out_of_memory("get_xattr_data");
189 size_t len = sys_lgetxattr(fname, name, ptr, datum_len);
190 if (len != datum_len) {
191 if (len == (size_t)-1) {
192 rsyserr(FERROR_XFER, errno,
193 "get_xattr_data: lgetxattr(\"%s\",\"%s\",%ld)"
194 " failed", fname, name, (long)datum_len);
197 "get_xattr_data: lgetxattr(\"%s\",\"%s\",%ld)"
198 " returned %ld\n", fname, name,
199 (long)datum_len, (long)len);
209 static int rsync_xal_get(const char *fname, item_list *xalp)
211 ssize_t list_len, name_len;
212 size_t datum_len, name_offset;
214 #ifdef HAVE_LINUX_XATTRS
215 int user_only = am_sender ? 0 : am_root <= 0;
220 /* This puts the name list into the "namebuf" buffer. */
221 if ((list_len = get_xattr_names(fname)) < 0)
224 for (name = namebuf; list_len > 0; name += name_len) {
225 name_len = strlen(name) + 1;
226 list_len -= name_len;
228 #ifdef HAVE_LINUX_XATTRS
229 /* We always ignore the system namespace, and non-root
230 * ignores everything but the user namespace. */
231 if (user_only ? !HAS_PREFIX(name, USER_PREFIX)
232 : HAS_PREFIX(name, SYSTEM_PREFIX))
236 /* No rsync.%FOO attributes are copied w/o 2 -X options. */
237 if (name_len > RPRE_LEN && name[RPRE_LEN] == '%'
238 && HAS_PREFIX(name, RSYNC_PREFIX)) {
239 if ((am_sender && preserve_xattrs < 2)
241 && (strcmp(name+RPRE_LEN+1, XSTAT_SUFFIX) == 0
242 || strcmp(name+RPRE_LEN+1, XACC_ACL_SUFFIX) == 0
243 || strcmp(name+RPRE_LEN+1, XDEF_ACL_SUFFIX) == 0)))
247 datum_len = name_len; /* Pass extra size to get_xattr_data() */
248 if (!(ptr = get_xattr_data(fname, name, &datum_len, 0)))
251 if (datum_len > MAX_FULL_DATUM) {
252 /* For large datums, we store a flag and a checksum. */
253 name_offset = 1 + MAX_DIGEST_LEN;
254 sum_init(checksum_seed);
255 sum_update(ptr, datum_len);
258 if (!(ptr = new_array(char, name_offset + name_len)))
259 out_of_memory("rsync_xal_get");
260 *ptr = XSTATE_ABBREV;
263 name_offset = datum_len;
265 rxa = EXPAND_ITEM_LIST(xalp, rsync_xa, RSYNC_XAL_INITIAL);
266 rxa->name = ptr + name_offset;
267 memcpy(rxa->name, name, name_len);
269 rxa->name_len = name_len;
270 rxa->datum_len = datum_len;
275 qsort(rxa, count, sizeof (rsync_xa), rsync_xal_compare_names);
276 for (rxa += count-1; count; count--, rxa--)
281 /* Read the xattr(s) for this filename. */
282 int get_xattr(const char *fname, stat_x *sxp)
284 sxp->xattr = new(item_list);
285 *sxp->xattr = empty_xattr;
286 if (rsync_xal_get(fname, sxp->xattr) < 0) {
293 int copy_xattrs(const char *source, const char *dest)
295 ssize_t list_len, name_len;
298 #ifdef HAVE_LINUX_XATTRS
299 int user_only = am_root <= 0;
302 /* This puts the name list into the "namebuf" buffer. */
303 if ((list_len = get_xattr_names(source)) < 0)
306 for (name = namebuf; list_len > 0; name += name_len) {
307 name_len = strlen(name) + 1;
308 list_len -= name_len;
310 #ifdef HAVE_LINUX_XATTRS
311 /* We always ignore the system namespace, and non-root
312 * ignores everything but the user namespace. */
313 if (user_only ? !HAS_PREFIX(name, USER_PREFIX)
314 : HAS_PREFIX(name, SYSTEM_PREFIX))
319 if (!(ptr = get_xattr_data(source, name, &datum_len, 0)))
321 if (sys_lsetxattr(dest, name, ptr, datum_len) < 0) {
322 int save_errno = errno ? errno : EINVAL;
323 rsyserr(FERROR_XFER, errno,
324 "rsync_xal_set: lsetxattr(\"%s\",\"%s\") failed",
335 static int find_matching_xattr(item_list *xalp)
338 item_list *lst = rsync_xal_l.items;
340 for (i = 0; i < rsync_xal_l.count; i++) {
341 rsync_xa *rxas1 = lst[i].items;
342 rsync_xa *rxas2 = xalp->items;
344 /* Wrong number of elements? */
345 if (lst[i].count != xalp->count)
347 /* any elements different? */
348 for (j = 0; j < xalp->count; j++) {
349 if (rxas1[j].name_len != rxas2[j].name_len
350 || rxas1[j].datum_len != rxas2[j].datum_len
351 || strcmp(rxas1[j].name, rxas2[j].name))
353 if (rxas1[j].datum_len > MAX_FULL_DATUM) {
354 if (memcmp(rxas1[j].datum + 1,
356 MAX_DIGEST_LEN) != 0)
359 if (memcmp(rxas1[j].datum, rxas2[j].datum,
364 /* no differences found. This is The One! */
365 if (j == xalp->count)
372 /* Store *xalp on the end of rsync_xal_l */
373 static void rsync_xal_store(item_list *xalp)
375 item_list *new_lst = EXPAND_ITEM_LIST(&rsync_xal_l, item_list, RSYNC_XAL_LIST_INITIAL);
376 /* Since the following call starts a new list, we know it will hold the
377 * entire initial-count, not just enough space for one new item. */
378 *new_lst = empty_xattr;
379 (void)EXPAND_ITEM_LIST(new_lst, rsync_xa, xalp->count);
380 memcpy(new_lst->items, xalp->items, xalp->count * sizeof (rsync_xa));
381 new_lst->count = xalp->count;
385 /* Send the make_xattr()-generated xattr list for this flist entry. */
386 int send_xattr(stat_x *sxp, int f)
388 int ndx = find_matching_xattr(sxp->xattr);
390 /* Send 0 (-1 + 1) to indicate that literal xattr data follows. */
391 write_varint(f, ndx + 1);
395 int count = sxp->xattr->count;
396 write_varint(f, count);
397 for (rxa = sxp->xattr->items; count--; rxa++) {
398 size_t name_len = rxa->name_len;
399 const char *name = rxa->name;
400 /* Strip the rsync prefix from disguised namespaces. */
401 if (name_len > RPRE_LEN
402 #ifdef HAVE_LINUX_XATTRS
405 && name[RPRE_LEN] != '%' && HAS_PREFIX(name, RSYNC_PREFIX)) {
407 name_len -= RPRE_LEN;
409 #ifndef HAVE_LINUX_XATTRS
411 /* Put everything else in the user namespace. */
412 name_len += UPRE_LEN;
415 write_varint(f, name_len);
416 write_varint(f, rxa->datum_len);
417 #ifndef HAVE_LINUX_XATTRS
418 if (name_len > rxa->name_len) {
419 write_buf(f, USER_PREFIX, UPRE_LEN);
420 name_len -= UPRE_LEN;
423 write_buf(f, name, name_len);
424 if (rxa->datum_len > MAX_FULL_DATUM)
425 write_buf(f, rxa->datum + 1, MAX_DIGEST_LEN);
427 write_buf(f, rxa->datum, rxa->datum_len);
429 ndx = rsync_xal_l.count; /* pre-incremented count */
430 rsync_xal_store(sxp->xattr); /* adds item to rsync_xal_l */
436 /* Return a flag indicating if we need to change a file's xattrs. If
437 * "find_all" is specified, also mark any abbreviated xattrs that we
438 * need so that send_xattr_request() can tell the sender about them. */
439 int xattr_diff(struct file_struct *file, stat_x *sxp, int find_all)
441 item_list *lst = rsync_xal_l.items;
442 rsync_xa *snd_rxa, *rec_rxa;
443 int snd_cnt, rec_cnt;
444 int cmp, same, xattrs_equal = 1;
446 if (sxp && XATTR_READY(*sxp)) {
447 rec_rxa = sxp->xattr->items;
448 rec_cnt = sxp->xattr->count;
454 if (F_XATTR(file) >= 0)
455 lst += F_XATTR(file);
459 snd_rxa = lst->items;
460 snd_cnt = lst->count;
462 /* If the count of the sender's xattrs is different from our
463 * (receiver's) xattrs, the lists are not the same. */
464 if (snd_cnt != rec_cnt) {
471 cmp = rec_cnt ? strcmp(snd_rxa->name, rec_rxa->name) : -1;
474 else if (snd_rxa->datum_len > MAX_FULL_DATUM) {
475 same = cmp == 0 && snd_rxa->datum_len == rec_rxa->datum_len
476 && memcmp(snd_rxa->datum + 1, rec_rxa->datum + 1,
477 MAX_DIGEST_LEN) == 0;
478 /* Flag unrequested items that we need. */
479 if (!same && find_all && snd_rxa->datum[0] == XSTATE_ABBREV)
480 snd_rxa->datum[0] = XSTATE_TODO;
482 same = cmp == 0 && snd_rxa->datum_len == rec_rxa->datum_len
483 && memcmp(snd_rxa->datum, rec_rxa->datum,
484 snd_rxa->datum_len) == 0;
505 return !xattrs_equal;
508 /* When called by the generator (with a NULL fname), this tells the sender
509 * all the abbreviated xattr values we need. When called by the sender
510 * (with a non-NULL fname), we send all the extra xattr data it needs.
511 * The generator may also call with f_out < 0 to just change all the
512 * XSTATE_ABBREV states into XSTATE_DONE. */
513 void send_xattr_request(const char *fname, struct file_struct *file, int f_out)
515 item_list *lst = rsync_xal_l.items;
516 int cnt, prior_req = 0;
519 lst += F_XATTR(file);
520 for (rxa = lst->items, cnt = lst->count; cnt--; rxa++) {
521 if (rxa->datum_len <= MAX_FULL_DATUM)
523 switch (rxa->datum[0]) {
525 /* Items left abbreviated matched the sender's checksum, so
526 * the receiver will cache the local data for future use. */
528 rxa->datum[0] = XSTATE_DONE;
537 /* Flag that we handled this abbreviated item. */
538 rxa->datum[0] = XSTATE_DONE;
540 write_varint(f_out, rxa->num - prior_req);
541 prior_req = rxa->num;
547 /* Re-read the long datum. */
548 if (!(ptr = get_xattr_data(fname, rxa->name, &len, 0))) {
549 rprintf(FERROR_XFER, "failed to re-read xattr %s for %s\n", rxa->name, fname);
550 write_varint(f_out, 0);
554 write_varint(f_out, len); /* length might have changed! */
555 write_buf(f_out, ptr, len);
561 write_byte(f_out, 0); /* end the list */
564 /* When called by the sender, read the request from the generator and mark
565 * any needed xattrs with a flag that lets us know they need to be sent to
566 * the receiver. When called by the receiver, reads the sent data and
567 * stores it in place of its checksum. */
568 int recv_xattr_request(struct file_struct *file, int f_in)
570 item_list *lst = rsync_xal_l.items;
571 char *old_datum, *name;
573 int rel_pos, cnt, num, got_xattr_data = 0;
575 if (F_XATTR(file) < 0) {
576 rprintf(FERROR, "recv_xattr_request: internal data error!\n");
577 exit_cleanup(RERR_STREAMIO);
579 lst += F_XATTR(file);
584 while ((rel_pos = read_varint(f_in)) != 0) {
586 while (cnt && rxa->num < num) {
590 if (!cnt || rxa->num != num) {
591 rprintf(FERROR, "[%s] could not find xattr #%d for %s\n",
592 who_am_i(), num, f_name(file, NULL));
593 exit_cleanup(RERR_STREAMIO);
595 if (!XATTR_ABBREV(*rxa) || rxa->datum[0] != XSTATE_ABBREV) {
596 rprintf(FERROR, "[%s] internal abbrev error on %s (%s, len=%ld)!\n",
597 who_am_i(), f_name(file, NULL), rxa->name, (long)rxa->datum_len);
598 exit_cleanup(RERR_STREAMIO);
602 rxa->datum[0] = XSTATE_TODO;
606 old_datum = rxa->datum;
607 rxa->datum_len = read_varint(f_in);
609 if (rxa->name_len + rxa->datum_len < rxa->name_len)
610 overflow_exit("recv_xattr_request");
611 rxa->datum = new_array(char, rxa->datum_len + rxa->name_len);
613 out_of_memory("recv_xattr_request");
614 name = rxa->datum + rxa->datum_len;
615 memcpy(name, rxa->name, rxa->name_len);
618 read_buf(f_in, rxa->datum, rxa->datum_len);
622 return got_xattr_data;
625 /* ------------------------------------------------------------------------- */
627 /* receive and build the rsync_xattr_lists */
628 void receive_xattr(struct file_struct *file, int f)
630 static item_list temp_xattr = EMPTY_ITEM_LIST;
632 #ifdef HAVE_LINUX_XATTRS
637 int ndx = read_varint(f);
639 if (ndx < 0 || (size_t)ndx > rsync_xal_l.count) {
640 rprintf(FERROR, "receive_xattr: xa index %d out of"
641 " range for %s\n", ndx, f_name(file, NULL));
642 exit_cleanup(RERR_STREAMIO);
646 F_XATTR(file) = ndx - 1;
650 if ((count = read_varint(f)) != 0) {
651 (void)EXPAND_ITEM_LIST(&temp_xattr, rsync_xa, count);
652 temp_xattr.count = 0;
655 for (num = 1; num <= count; num++) {
658 size_t name_len = read_varint(f);
659 size_t datum_len = read_varint(f);
660 size_t dget_len = datum_len > MAX_FULL_DATUM ? 1 + MAX_DIGEST_LEN : datum_len;
661 size_t extra_len = MIGHT_NEED_RPRE ? RPRE_LEN : 0;
662 if ((dget_len + extra_len < dget_len)
663 || (dget_len + extra_len + name_len < dget_len))
664 overflow_exit("receive_xattr");
665 ptr = new_array(char, dget_len + extra_len + name_len);
667 out_of_memory("receive_xattr");
668 name = ptr + dget_len + extra_len;
669 read_buf(f, name, name_len);
670 if (dget_len == datum_len)
671 read_buf(f, ptr, dget_len);
673 *ptr = XSTATE_ABBREV;
674 read_buf(f, ptr + 1, MAX_DIGEST_LEN);
676 #ifdef HAVE_LINUX_XATTRS
677 /* Non-root can only save the user namespace. */
678 if (am_root <= 0 && !HAS_PREFIX(name, USER_PREFIX)) {
684 name_len += RPRE_LEN;
685 memcpy(name, RSYNC_PREFIX, RPRE_LEN);
689 /* This OS only has a user namespace, so we either
690 * strip the user prefix, or we put a non-user
691 * namespace inside our rsync hierarchy. */
692 if (HAS_PREFIX(name, USER_PREFIX)) {
694 name_len -= UPRE_LEN;
695 } else if (am_root) {
697 name_len += RPRE_LEN;
698 memcpy(name, RSYNC_PREFIX, RPRE_LEN);
704 /* No rsync.%FOO attributes are copied w/o 2 -X options. */
705 if (preserve_xattrs < 2 && name_len > RPRE_LEN
706 && name[RPRE_LEN] == '%' && HAS_PREFIX(name, RSYNC_PREFIX)) {
710 rxa = EXPAND_ITEM_LIST(&temp_xattr, rsync_xa, 1);
713 rxa->name_len = name_len;
714 rxa->datum_len = datum_len;
718 if (need_sort && count > 1)
719 qsort(temp_xattr.items, count, sizeof (rsync_xa), rsync_xal_compare_names);
721 ndx = rsync_xal_l.count; /* pre-incremented count */
722 rsync_xal_store(&temp_xattr); /* adds item to rsync_xal_l */
727 /* Turn the xattr data in stat_x into cached xattr data, setting the index
728 * values in the file struct. */
729 void cache_tmp_xattr(struct file_struct *file, stat_x *sxp)
736 if (prior_xattr_count == (size_t)-1)
737 prior_xattr_count = rsync_xal_l.count;
738 ndx = find_matching_xattr(sxp->xattr);
740 rsync_xal_store(sxp->xattr); /* adds item to rsync_xal_l */
745 void uncache_tmp_xattrs(void)
747 if (prior_xattr_count != (size_t)-1) {
748 item_list *xattr_item = rsync_xal_l.items;
749 item_list *xattr_start = xattr_item + prior_xattr_count;
750 xattr_item += rsync_xal_l.count;
751 rsync_xal_l.count = prior_xattr_count;
752 while (xattr_item-- > xattr_start) {
753 rsync_xal_free(xattr_item);
754 free(xattr_item->items);
756 prior_xattr_count = (size_t)-1;
760 static int rsync_xal_set(const char *fname, item_list *xalp,
761 const char *fnamecmp, stat_x *sxp)
763 rsync_xa *rxas = xalp->items;
766 char *name, *ptr, sum[MAX_DIGEST_LEN];
767 #ifdef HAVE_LINUX_XATTRS
768 int user_only = am_root <= 0;
773 /* This puts the current name list into the "namebuf" buffer. */
774 if ((list_len = get_xattr_names(fname)) < 0)
777 for (i = 0; i < xalp->count; i++) {
780 if (XATTR_ABBREV(rxas[i])) {
781 /* See if the fnamecmp version is identical. */
782 len = name_len = rxas[i].name_len;
783 if ((ptr = get_xattr_data(fnamecmp, name, &len, 1)) == NULL) {
787 rprintf(FERROR, "Missing abbreviated xattr value, %s, for %s\n",
788 rxas[i].name, full_fname(fname));
792 if (len != rxas[i].datum_len) {
797 sum_init(checksum_seed);
798 sum_update(ptr, len);
800 if (memcmp(sum, rxas[i].datum + 1, MAX_DIGEST_LEN) != 0) {
805 if (fname == fnamecmp)
806 ; /* Value is already set when identical */
807 else if (sys_lsetxattr(fname, name, ptr, len) < 0) {
808 rsyserr(FERROR_XFER, errno,
809 "rsync_xal_set: lsetxattr(\"%s\",\"%s\") failed",
812 } else /* make sure caller sets mtime */
813 sxp->st.st_mtime = (time_t)-1;
815 if (am_generator) { /* generator items stay abbreviated */
820 memcpy(ptr + len, name, name_len);
823 rxas[i].name = name = ptr + len;
828 if (sys_lsetxattr(fname, name, rxas[i].datum, rxas[i].datum_len) < 0) {
829 rsyserr(FERROR_XFER, errno,
830 "rsync_xal_set: lsetxattr(\"%s\",\"%s\") failed",
833 } else /* make sure caller sets mtime */
834 sxp->st.st_mtime = (time_t)-1;
837 /* Remove any extraneous names. */
838 for (name = namebuf; list_len > 0; name += name_len) {
839 name_len = strlen(name) + 1;
840 list_len -= name_len;
842 #ifdef HAVE_LINUX_XATTRS
843 /* We always ignore the system namespace, and non-root
844 * ignores everything but the user namespace. */
845 if (user_only ? !HAS_PREFIX(name, USER_PREFIX)
846 : HAS_PREFIX(name, SYSTEM_PREFIX))
849 if (am_root < 0 && name_len > RPRE_LEN
850 && name[RPRE_LEN] == '%' && strcmp(name, XSTAT_ATTR) == 0)
853 for (i = 0; i < xalp->count; i++) {
854 if (strcmp(name, rxas[i].name) == 0)
857 if (i == xalp->count) {
858 if (sys_lremovexattr(fname, name) < 0) {
859 rsyserr(FERROR_XFER, errno,
860 "rsync_xal_clear: lremovexattr(\"%s\",\"%s\") failed",
863 } else /* make sure caller sets mtime */
864 sxp->st.st_mtime = (time_t)-1;
871 /* Set extended attributes on indicated filename. */
872 int set_xattr(const char *fname, const struct file_struct *file,
873 const char *fnamecmp, stat_x *sxp)
876 item_list *lst = rsync_xal_l.items;
879 return 1; /* FIXME: --dry-run needs to compute this value */
881 if (read_only || list_only) {
887 return rsync_xal_set(fname, lst + ndx, fnamecmp, sxp);
891 char *get_xattr_acl(const char *fname, int is_access_acl, size_t *len_p)
893 const char *name = is_access_acl ? XACC_ACL_ATTR : XDEF_ACL_ATTR;
894 *len_p = 0; /* no extra data alloc needed from get_xattr_data() */
895 return get_xattr_data(fname, name, len_p, 1);
898 int set_xattr_acl(const char *fname, int is_access_acl, const char *buf, size_t buf_len)
900 const char *name = is_access_acl ? XACC_ACL_ATTR : XDEF_ACL_ATTR;
901 if (sys_lsetxattr(fname, name, buf, buf_len) < 0) {
902 rsyserr(FERROR_XFER, errno,
903 "set_xattr_acl: lsetxattr(\"%s\",\"%s\") failed",
910 int del_def_xattr_acl(const char *fname)
912 return sys_lremovexattr(fname, XDEF_ACL_ATTR);
916 int get_stat_xattr(const char *fname, int fd, STRUCT_STAT *fst, STRUCT_STAT *xst)
918 int mode, rdev_major, rdev_minor, uid, gid, len;
921 if (am_root >= 0 || IS_DEVICE(fst->st_mode) || IS_SPECIAL(fst->st_mode))
930 len = sys_lgetxattr(fname, XSTAT_ATTR, buf, sizeof buf - 1);
933 len = sys_fgetxattr(fd, XSTAT_ATTR, buf, sizeof buf - 1);
935 if (len >= (int)sizeof buf) {
940 if (errno == ENOTSUP || errno == ENOATTR)
942 if (errno == EPERM && S_ISLNK(fst->st_mode)) {
947 rsyserr(FERROR_XFER, errno, "failed to read xattr %s for %s",
948 XSTAT_ATTR, full_fname(fname));
953 if (sscanf(buf, "%o %d,%d %d:%d",
954 &mode, &rdev_major, &rdev_minor, &uid, &gid) != 5) {
955 rprintf(FERROR, "Corrupt %s xattr attached to %s: \"%s\"\n",
956 XSTAT_ATTR, full_fname(fname), buf);
957 exit_cleanup(RERR_FILEIO);
960 xst->st_mode = from_wire_mode(mode);
961 xst->st_rdev = MAKEDEV(rdev_major, rdev_minor);
968 int set_stat_xattr(const char *fname, struct file_struct *file, mode_t new_mode)
970 STRUCT_STAT fst, xst;
977 if (read_only || list_only) {
978 rsyserr(FERROR_XFER, EROFS, "failed to write xattr %s for %s",
979 XSTAT_ATTR, full_fname(fname));
983 if (x_lstat(fname, &fst, &xst) < 0) {
984 rsyserr(FERROR_XFER, errno, "failed to re-stat %s",
989 fst.st_mode &= (_S_IFMT | CHMOD_BITS);
990 fmode = new_mode & (_S_IFMT | CHMOD_BITS);
992 if (IS_DEVICE(fmode)) {
993 uint32 *devp = F_RDEV_P(file);
994 rdev = MAKEDEV(DEV_MAJOR(devp), DEV_MINOR(devp));
998 /* Dump the special permissions and enable full owner access. */
999 mode = (fst.st_mode & _S_IFMT) | (fmode & ACCESSPERMS)
1000 | (S_ISDIR(fst.st_mode) ? 0700 : 0600);
1001 if (fst.st_mode != mode)
1002 do_chmod(fname, mode);
1003 if (!IS_DEVICE(fst.st_mode))
1004 fst.st_rdev = 0; /* just in case */
1006 if (mode == fmode && fst.st_rdev == rdev
1007 && fst.st_uid == F_OWNER(file) && fst.st_gid == F_GROUP(file)) {
1008 /* xst.st_mode will be 0 if there's no current stat xattr */
1009 if (xst.st_mode && sys_lremovexattr(fname, XSTAT_ATTR) < 0) {
1010 rsyserr(FERROR_XFER, errno,
1011 "delete of stat xattr failed for %s",
1018 if (xst.st_mode != fmode || xst.st_rdev != rdev
1019 || xst.st_uid != F_OWNER(file) || xst.st_gid != F_GROUP(file)) {
1021 int len = snprintf(buf, sizeof buf, "%o %u,%u %u:%u",
1022 to_wire_mode(fmode),
1023 (int)major(rdev), (int)minor(rdev),
1024 F_OWNER(file), F_GROUP(file));
1025 if (sys_lsetxattr(fname, XSTAT_ATTR, buf, len) < 0) {
1026 if (errno == EPERM && S_ISLNK(fst.st_mode))
1028 rsyserr(FERROR_XFER, errno,
1029 "failed to write xattr %s for %s",
1030 XSTAT_ATTR, full_fname(fname));
1038 int x_stat(const char *fname, STRUCT_STAT *fst, STRUCT_STAT *xst)
1040 int ret = do_stat(fname, fst);
1041 if ((ret < 0 || get_stat_xattr(fname, -1, fst, xst) < 0) && xst)
1046 int x_lstat(const char *fname, STRUCT_STAT *fst, STRUCT_STAT *xst)
1048 int ret = do_lstat(fname, fst);
1049 if ((ret < 0 || get_stat_xattr(fname, -1, fst, xst) < 0) && xst)
1054 int x_fstat(int fd, STRUCT_STAT *fst, STRUCT_STAT *xst)
1056 int ret = do_fstat(fd, fst);
1057 if ((ret < 0 || get_stat_xattr(NULL, fd, fst, xst) < 0) && xst)
1062 #endif /* SUPPORT_XATTRS */