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 preserve_links;
36 extern int preserve_devices;
37 extern int preserve_specials;
38 extern int checksum_seed;
40 #define RSYNC_XAL_INITIAL 5
41 #define RSYNC_XAL_LIST_INITIAL 100
43 #define MAX_FULL_DATUM 32
45 #define HAS_PREFIX(str, prfx) (*(str) == *(prfx) \
46 && strncmp(str, prfx, sizeof (prfx) - 1) == 0)
48 #define XATTR_ABBREV(x) ((size_t)((x).name - (x).datum) < (x).datum_len)
50 #define XSTATE_ABBREV 1
54 #define USER_PREFIX "user."
55 #define UPRE_LEN ((int)sizeof USER_PREFIX - 1)
56 #define SYSTEM_PREFIX "system."
57 #define SPRE_LEN ((int)sizeof SYSTEM_PREFIX - 1)
59 #ifdef HAVE_LINUX_XATTRS
60 #define MIGHT_NEED_RPRE (am_root < 0)
61 #define RSYNC_PREFIX USER_PREFIX "rsync."
63 #define MIGHT_NEED_RPRE am_root
64 #define RSYNC_PREFIX "rsync."
66 #define RPRE_LEN ((int)sizeof RSYNC_PREFIX - 1)
68 #define XSTAT_SUFFIX "stat"
69 #define XSTAT_ATTR RSYNC_PREFIX "%" XSTAT_SUFFIX
70 #define XACC_ACL_SUFFIX "aacl"
71 #define XACC_ACL_ATTR RSYNC_PREFIX "%" XACC_ACL_SUFFIX
72 #define XDEF_ACL_SUFFIX "dacl"
73 #define XDEF_ACL_ATTR RSYNC_PREFIX "%" XDEF_ACL_SUFFIX
77 size_t datum_len, name_len;
81 static size_t namebuf_len = 0;
82 static char *namebuf = NULL;
84 static item_list empty_xattr = EMPTY_ITEM_LIST;
85 static item_list rsync_xal_l = EMPTY_ITEM_LIST;
87 static size_t prior_xattr_count = (size_t)-1;
89 /* ------------------------------------------------------------------------- */
91 static void rsync_xal_free(item_list *xalp)
94 rsync_xa *rxas = xalp->items;
96 for (i = 0; i < xalp->count; i++) {
98 /*free(rxas[i].name);*/
103 void free_xattr(stat_x *sxp)
107 rsync_xal_free(sxp->xattr);
112 static int rsync_xal_compare_names(const void *x1, const void *x2)
114 const rsync_xa *xa1 = x1;
115 const rsync_xa *xa2 = x2;
116 return strcmp(xa1->name, xa2->name);
119 static ssize_t get_xattr_names(const char *fname)
126 namebuf = new_array(char, namebuf_len);
128 out_of_memory("get_xattr_names");
132 /* The length returned includes all the '\0' terminators. */
133 list_len = sys_llistxattr(fname, namebuf, namebuf_len);
135 if ((size_t)list_len <= namebuf_len)
137 } else if (errno == ENOTSUP)
139 else if (errno != ERANGE) {
140 arg = (double)namebuf_len;
142 rsyserr(FERROR_XFER, errno,
143 "get_xattr_names: llistxattr(\"%s\",%.0f) failed",
144 full_fname(fname), arg);
147 list_len = sys_llistxattr(fname, NULL, 0);
154 namebuf_len = list_len + 1024;
155 namebuf = new_array(char, namebuf_len);
157 out_of_memory("get_xattr_names");
163 /* On entry, the *len_ptr parameter contains the size of the extra space we
164 * should allocate when we create a buffer for the data. On exit, it contains
165 * the length of the datum. */
166 static char *get_xattr_data(const char *fname, const char *name, size_t *len_ptr,
167 int no_missing_error)
169 size_t datum_len = sys_lgetxattr(fname, name, NULL, 0);
170 size_t extra_len = *len_ptr;
173 *len_ptr = datum_len;
175 if (datum_len == (size_t)-1) {
176 if (errno == ENOTSUP || no_missing_error)
178 rsyserr(FERROR_XFER, errno,
179 "get_xattr_data: lgetxattr(\"%s\",\"%s\",0) failed",
180 full_fname(fname), name);
184 if (!datum_len && !extra_len)
185 extra_len = 1; /* request non-zero amount of memory */
186 if (datum_len + extra_len < datum_len)
187 overflow_exit("get_xattr_data");
188 if (!(ptr = new_array(char, datum_len + extra_len)))
189 out_of_memory("get_xattr_data");
192 size_t len = sys_lgetxattr(fname, name, ptr, datum_len);
193 if (len != datum_len) {
194 if (len == (size_t)-1) {
195 rsyserr(FERROR_XFER, errno,
196 "get_xattr_data: lgetxattr(\"%s\",\"%s\",%ld)"
197 " failed", full_fname(fname), name, (long)datum_len);
200 "get_xattr_data: lgetxattr(\"%s\",\"%s\",%ld)"
201 " returned %ld\n", full_fname(fname), name,
202 (long)datum_len, (long)len);
212 static int rsync_xal_get(const char *fname, item_list *xalp)
214 ssize_t list_len, name_len;
215 size_t datum_len, name_offset;
217 #ifdef HAVE_LINUX_XATTRS
218 int user_only = am_sender ? 0 : am_root <= 0;
223 /* This puts the name list into the "namebuf" buffer. */
224 if ((list_len = get_xattr_names(fname)) < 0)
227 for (name = namebuf; list_len > 0; name += name_len) {
228 name_len = strlen(name) + 1;
229 list_len -= name_len;
231 #ifdef HAVE_LINUX_XATTRS
232 /* We always ignore the system namespace, and non-root
233 * ignores everything but the user namespace. */
234 if (user_only ? !HAS_PREFIX(name, USER_PREFIX)
235 : HAS_PREFIX(name, SYSTEM_PREFIX))
239 /* No rsync.%FOO attributes are copied w/o 2 -X options. */
240 if (name_len > RPRE_LEN && name[RPRE_LEN] == '%'
241 && HAS_PREFIX(name, RSYNC_PREFIX)) {
242 if ((am_sender && preserve_xattrs < 2)
244 && (strcmp(name+RPRE_LEN+1, XSTAT_SUFFIX) == 0
245 || strcmp(name+RPRE_LEN+1, XACC_ACL_SUFFIX) == 0
246 || strcmp(name+RPRE_LEN+1, XDEF_ACL_SUFFIX) == 0)))
250 datum_len = name_len; /* Pass extra size to get_xattr_data() */
251 if (!(ptr = get_xattr_data(fname, name, &datum_len, 0)))
254 if (datum_len > MAX_FULL_DATUM) {
255 /* For large datums, we store a flag and a checksum. */
256 name_offset = 1 + MAX_DIGEST_LEN;
257 sum_init(checksum_seed);
258 sum_update(ptr, datum_len);
261 if (!(ptr = new_array(char, name_offset + name_len)))
262 out_of_memory("rsync_xal_get");
263 *ptr = XSTATE_ABBREV;
266 name_offset = datum_len;
268 rxa = EXPAND_ITEM_LIST(xalp, rsync_xa, RSYNC_XAL_INITIAL);
269 rxa->name = ptr + name_offset;
270 memcpy(rxa->name, name, name_len);
272 rxa->name_len = name_len;
273 rxa->datum_len = datum_len;
278 qsort(rxa, count, sizeof (rsync_xa), rsync_xal_compare_names);
279 for (rxa += count-1; count; count--, rxa--)
284 /* Read the xattr(s) for this filename. */
285 int get_xattr(const char *fname, stat_x *sxp)
287 sxp->xattr = new(item_list);
288 *sxp->xattr = empty_xattr;
290 if (S_ISREG(sxp->st.st_mode) || S_ISDIR(sxp->st.st_mode)) {
291 /* Everyone supports this. */
292 } else if (S_ISLNK(sxp->st.st_mode)) {
293 #ifndef NO_SYMLINK_XATTRS
297 } else if (IS_SPECIAL(sxp->st.st_mode)) {
298 #ifndef NO_SPECIAL_XATTRS
299 if (!preserve_specials)
302 } else if (IS_DEVICE(sxp->st.st_mode)) {
303 #ifndef NO_DEVICE_XATTRS
304 if (!preserve_devices)
309 if (rsync_xal_get(fname, sxp->xattr) < 0) {
316 int copy_xattrs(const char *source, const char *dest)
318 ssize_t list_len, name_len;
321 #ifdef HAVE_LINUX_XATTRS
322 int user_only = am_root <= 0;
325 /* This puts the name list into the "namebuf" buffer. */
326 if ((list_len = get_xattr_names(source)) < 0)
329 for (name = namebuf; list_len > 0; name += name_len) {
330 name_len = strlen(name) + 1;
331 list_len -= name_len;
333 #ifdef HAVE_LINUX_XATTRS
334 /* We always ignore the system namespace, and non-root
335 * ignores everything but the user namespace. */
336 if (user_only ? !HAS_PREFIX(name, USER_PREFIX)
337 : HAS_PREFIX(name, SYSTEM_PREFIX))
342 if (!(ptr = get_xattr_data(source, name, &datum_len, 0)))
344 if (sys_lsetxattr(dest, name, ptr, datum_len) < 0) {
345 int save_errno = errno ? errno : EINVAL;
346 rsyserr(FERROR_XFER, errno,
347 "copy_xattrs: lsetxattr(\"%s\",\"%s\") failed",
348 full_fname(dest), name);
358 static int find_matching_xattr(item_list *xalp)
361 item_list *lst = rsync_xal_l.items;
363 for (i = 0; i < rsync_xal_l.count; i++) {
364 rsync_xa *rxas1 = lst[i].items;
365 rsync_xa *rxas2 = xalp->items;
367 /* Wrong number of elements? */
368 if (lst[i].count != xalp->count)
370 /* any elements different? */
371 for (j = 0; j < xalp->count; j++) {
372 if (rxas1[j].name_len != rxas2[j].name_len
373 || rxas1[j].datum_len != rxas2[j].datum_len
374 || strcmp(rxas1[j].name, rxas2[j].name))
376 if (rxas1[j].datum_len > MAX_FULL_DATUM) {
377 if (memcmp(rxas1[j].datum + 1,
379 MAX_DIGEST_LEN) != 0)
382 if (memcmp(rxas1[j].datum, rxas2[j].datum,
387 /* no differences found. This is The One! */
388 if (j == xalp->count)
395 /* Store *xalp on the end of rsync_xal_l */
396 static void rsync_xal_store(item_list *xalp)
398 item_list *new_lst = EXPAND_ITEM_LIST(&rsync_xal_l, item_list, RSYNC_XAL_LIST_INITIAL);
399 /* Since the following call starts a new list, we know it will hold the
400 * entire initial-count, not just enough space for one new item. */
401 *new_lst = empty_xattr;
402 (void)EXPAND_ITEM_LIST(new_lst, rsync_xa, xalp->count);
403 memcpy(new_lst->items, xalp->items, xalp->count * sizeof (rsync_xa));
404 new_lst->count = xalp->count;
408 /* Send the make_xattr()-generated xattr list for this flist entry. */
409 int send_xattr(int f, stat_x *sxp)
411 int ndx = find_matching_xattr(sxp->xattr);
413 /* Send 0 (-1 + 1) to indicate that literal xattr data follows. */
414 write_varint(f, ndx + 1);
418 int count = sxp->xattr->count;
419 write_varint(f, count);
420 for (rxa = sxp->xattr->items; count--; rxa++) {
421 size_t name_len = rxa->name_len;
422 const char *name = rxa->name;
423 /* Strip the rsync prefix from disguised namespaces. */
424 if (name_len > RPRE_LEN
425 #ifdef HAVE_LINUX_XATTRS
428 && name[RPRE_LEN] != '%' && HAS_PREFIX(name, RSYNC_PREFIX)) {
430 name_len -= RPRE_LEN;
432 #ifndef HAVE_LINUX_XATTRS
434 /* Put everything else in the user namespace. */
435 name_len += UPRE_LEN;
438 write_varint(f, name_len);
439 write_varint(f, rxa->datum_len);
440 #ifndef HAVE_LINUX_XATTRS
441 if (name_len > rxa->name_len) {
442 write_buf(f, USER_PREFIX, UPRE_LEN);
443 name_len -= UPRE_LEN;
446 write_buf(f, name, name_len);
447 if (rxa->datum_len > MAX_FULL_DATUM)
448 write_buf(f, rxa->datum + 1, MAX_DIGEST_LEN);
450 write_buf(f, rxa->datum, rxa->datum_len);
452 ndx = rsync_xal_l.count; /* pre-incremented count */
453 rsync_xal_store(sxp->xattr); /* adds item to rsync_xal_l */
459 /* Return a flag indicating if we need to change a file's xattrs. If
460 * "find_all" is specified, also mark any abbreviated xattrs that we
461 * need so that send_xattr_request() can tell the sender about them. */
462 int xattr_diff(struct file_struct *file, stat_x *sxp, int find_all)
464 item_list *lst = rsync_xal_l.items;
465 rsync_xa *snd_rxa, *rec_rxa;
466 int snd_cnt, rec_cnt;
467 int cmp, same, xattrs_equal = 1;
469 if (sxp && XATTR_READY(*sxp)) {
470 rec_rxa = sxp->xattr->items;
471 rec_cnt = sxp->xattr->count;
477 if (F_XATTR(file) >= 0)
478 lst += F_XATTR(file);
482 snd_rxa = lst->items;
483 snd_cnt = lst->count;
485 /* If the count of the sender's xattrs is different from our
486 * (receiver's) xattrs, the lists are not the same. */
487 if (snd_cnt != rec_cnt) {
494 cmp = rec_cnt ? strcmp(snd_rxa->name, rec_rxa->name) : -1;
497 else if (snd_rxa->datum_len > MAX_FULL_DATUM) {
498 same = cmp == 0 && snd_rxa->datum_len == rec_rxa->datum_len
499 && memcmp(snd_rxa->datum + 1, rec_rxa->datum + 1,
500 MAX_DIGEST_LEN) == 0;
501 /* Flag unrequested items that we need. */
502 if (!same && find_all && snd_rxa->datum[0] == XSTATE_ABBREV)
503 snd_rxa->datum[0] = XSTATE_TODO;
505 same = cmp == 0 && snd_rxa->datum_len == rec_rxa->datum_len
506 && memcmp(snd_rxa->datum, rec_rxa->datum,
507 snd_rxa->datum_len) == 0;
528 return !xattrs_equal;
531 /* When called by the generator (with a NULL fname), this tells the sender
532 * all the abbreviated xattr values we need. When called by the sender
533 * (with a non-NULL fname), we send all the extra xattr data it needs.
534 * The generator may also call with f_out < 0 to just change all the
535 * XSTATE_ABBREV states into XSTATE_DONE. */
536 void send_xattr_request(const char *fname, struct file_struct *file, int f_out)
538 item_list *lst = rsync_xal_l.items;
539 int cnt, prior_req = 0;
542 lst += F_XATTR(file);
543 for (rxa = lst->items, cnt = lst->count; cnt--; rxa++) {
544 if (rxa->datum_len <= MAX_FULL_DATUM)
546 switch (rxa->datum[0]) {
548 /* Items left abbreviated matched the sender's checksum, so
549 * the receiver will cache the local data for future use. */
551 rxa->datum[0] = XSTATE_DONE;
560 /* Flag that we handled this abbreviated item. */
561 rxa->datum[0] = XSTATE_DONE;
563 write_varint(f_out, rxa->num - prior_req);
564 prior_req = rxa->num;
570 /* Re-read the long datum. */
571 if (!(ptr = get_xattr_data(fname, rxa->name, &len, 0))) {
572 rprintf(FERROR_XFER, "failed to re-read xattr %s for %s\n", rxa->name, fname);
573 write_varint(f_out, 0);
577 write_varint(f_out, len); /* length might have changed! */
578 write_buf(f_out, ptr, len);
584 write_byte(f_out, 0); /* end the list */
587 /* When called by the sender, read the request from the generator and mark
588 * any needed xattrs with a flag that lets us know they need to be sent to
589 * the receiver. When called by the receiver, reads the sent data and
590 * stores it in place of its checksum. */
591 int recv_xattr_request(struct file_struct *file, int f_in)
593 item_list *lst = rsync_xal_l.items;
594 char *old_datum, *name;
596 int rel_pos, cnt, num, got_xattr_data = 0;
598 if (F_XATTR(file) < 0) {
599 rprintf(FERROR, "recv_xattr_request: internal data error!\n");
600 exit_cleanup(RERR_PROTOCOL);
602 lst += F_XATTR(file);
607 while ((rel_pos = read_varint(f_in)) != 0) {
609 while (cnt && rxa->num < num) {
613 if (!cnt || rxa->num != num) {
614 rprintf(FERROR, "[%s] could not find xattr #%d for %s\n",
615 who_am_i(), num, f_name(file, NULL));
616 exit_cleanup(RERR_PROTOCOL);
618 if (!XATTR_ABBREV(*rxa) || rxa->datum[0] != XSTATE_ABBREV) {
619 rprintf(FERROR, "[%s] internal abbrev error on %s (%s, len=%ld)!\n",
620 who_am_i(), f_name(file, NULL), rxa->name, (long)rxa->datum_len);
621 exit_cleanup(RERR_PROTOCOL);
625 rxa->datum[0] = XSTATE_TODO;
629 old_datum = rxa->datum;
630 rxa->datum_len = read_varint(f_in);
632 if (rxa->name_len + rxa->datum_len < rxa->name_len)
633 overflow_exit("recv_xattr_request");
634 rxa->datum = new_array(char, rxa->datum_len + rxa->name_len);
636 out_of_memory("recv_xattr_request");
637 name = rxa->datum + rxa->datum_len;
638 memcpy(name, rxa->name, rxa->name_len);
641 read_buf(f_in, rxa->datum, rxa->datum_len);
645 return got_xattr_data;
648 /* ------------------------------------------------------------------------- */
650 /* receive and build the rsync_xattr_lists */
651 void receive_xattr(int f, struct file_struct *file)
653 static item_list temp_xattr = EMPTY_ITEM_LIST;
655 #ifdef HAVE_LINUX_XATTRS
660 int ndx = read_varint(f);
662 if (ndx < 0 || (size_t)ndx > rsync_xal_l.count) {
663 rprintf(FERROR, "receive_xattr: xa index %d out of"
664 " range for %s\n", ndx, f_name(file, NULL));
665 exit_cleanup(RERR_PROTOCOL);
669 F_XATTR(file) = ndx - 1;
673 if ((count = read_varint(f)) != 0) {
674 (void)EXPAND_ITEM_LIST(&temp_xattr, rsync_xa, count);
675 temp_xattr.count = 0;
678 for (num = 1; num <= count; num++) {
681 size_t name_len = read_varint(f);
682 size_t datum_len = read_varint(f);
683 size_t dget_len = datum_len > MAX_FULL_DATUM ? 1 + MAX_DIGEST_LEN : datum_len;
684 size_t extra_len = MIGHT_NEED_RPRE ? RPRE_LEN : 0;
685 if ((dget_len + extra_len < dget_len)
686 || (dget_len + extra_len + name_len < dget_len))
687 overflow_exit("receive_xattr");
688 ptr = new_array(char, dget_len + extra_len + name_len);
690 out_of_memory("receive_xattr");
691 name = ptr + dget_len + extra_len;
692 read_buf(f, name, name_len);
693 if (dget_len == datum_len)
694 read_buf(f, ptr, dget_len);
696 *ptr = XSTATE_ABBREV;
697 read_buf(f, ptr + 1, MAX_DIGEST_LEN);
699 #ifdef HAVE_LINUX_XATTRS
700 /* Non-root can only save the user namespace. */
701 if (am_root <= 0 && !HAS_PREFIX(name, USER_PREFIX)) {
707 name_len += RPRE_LEN;
708 memcpy(name, RSYNC_PREFIX, RPRE_LEN);
712 /* This OS only has a user namespace, so we either
713 * strip the user prefix, or we put a non-user
714 * namespace inside our rsync hierarchy. */
715 if (HAS_PREFIX(name, USER_PREFIX)) {
717 name_len -= UPRE_LEN;
718 } else if (am_root) {
720 name_len += RPRE_LEN;
721 memcpy(name, RSYNC_PREFIX, RPRE_LEN);
727 /* No rsync.%FOO attributes are copied w/o 2 -X options. */
728 if (preserve_xattrs < 2 && name_len > RPRE_LEN
729 && name[RPRE_LEN] == '%' && HAS_PREFIX(name, RSYNC_PREFIX)) {
733 rxa = EXPAND_ITEM_LIST(&temp_xattr, rsync_xa, 1);
736 rxa->name_len = name_len;
737 rxa->datum_len = datum_len;
741 if (need_sort && count > 1)
742 qsort(temp_xattr.items, count, sizeof (rsync_xa), rsync_xal_compare_names);
744 ndx = rsync_xal_l.count; /* pre-incremented count */
745 rsync_xal_store(&temp_xattr); /* adds item to rsync_xal_l */
750 /* Turn the xattr data in stat_x into cached xattr data, setting the index
751 * values in the file struct. */
752 void cache_tmp_xattr(struct file_struct *file, stat_x *sxp)
759 if (prior_xattr_count == (size_t)-1)
760 prior_xattr_count = rsync_xal_l.count;
761 ndx = find_matching_xattr(sxp->xattr);
763 rsync_xal_store(sxp->xattr); /* adds item to rsync_xal_l */
768 void uncache_tmp_xattrs(void)
770 if (prior_xattr_count != (size_t)-1) {
771 item_list *xattr_item = rsync_xal_l.items;
772 item_list *xattr_start = xattr_item + prior_xattr_count;
773 xattr_item += rsync_xal_l.count;
774 rsync_xal_l.count = prior_xattr_count;
775 while (xattr_item-- > xattr_start) {
776 rsync_xal_free(xattr_item);
777 free(xattr_item->items);
779 prior_xattr_count = (size_t)-1;
783 static int rsync_xal_set(const char *fname, item_list *xalp,
784 const char *fnamecmp, stat_x *sxp)
786 rsync_xa *rxas = xalp->items;
789 char *name, *ptr, sum[MAX_DIGEST_LEN];
790 #ifdef HAVE_LINUX_XATTRS
791 int user_only = am_root <= 0;
796 /* This puts the current name list into the "namebuf" buffer. */
797 if ((list_len = get_xattr_names(fname)) < 0)
800 for (i = 0; i < xalp->count; i++) {
803 if (XATTR_ABBREV(rxas[i])) {
804 /* See if the fnamecmp version is identical. */
805 len = name_len = rxas[i].name_len;
806 if ((ptr = get_xattr_data(fnamecmp, name, &len, 1)) == NULL) {
810 rprintf(FERROR, "Missing abbreviated xattr value, %s, for %s\n",
811 rxas[i].name, full_fname(fname));
815 if (len != rxas[i].datum_len) {
820 sum_init(checksum_seed);
821 sum_update(ptr, len);
823 if (memcmp(sum, rxas[i].datum + 1, MAX_DIGEST_LEN) != 0) {
828 if (fname == fnamecmp)
829 ; /* Value is already set when identical */
830 else if (sys_lsetxattr(fname, name, ptr, len) < 0) {
831 rsyserr(FERROR_XFER, errno,
832 "rsync_xal_set: lsetxattr(\"%s\",\"%s\") failed",
833 full_fname(fname), name);
835 } else /* make sure caller sets mtime */
836 sxp->st.st_mtime = (time_t)-1;
838 if (am_generator) { /* generator items stay abbreviated */
843 memcpy(ptr + len, name, name_len);
846 rxas[i].name = name = ptr + len;
851 if (sys_lsetxattr(fname, name, rxas[i].datum, rxas[i].datum_len) < 0) {
852 rsyserr(FERROR_XFER, errno,
853 "rsync_xal_set: lsetxattr(\"%s\",\"%s\") failed",
854 full_fname(fname), name);
856 } else /* make sure caller sets mtime */
857 sxp->st.st_mtime = (time_t)-1;
860 /* Remove any extraneous names. */
861 for (name = namebuf; list_len > 0; name += name_len) {
862 name_len = strlen(name) + 1;
863 list_len -= name_len;
865 #ifdef HAVE_LINUX_XATTRS
866 /* We always ignore the system namespace, and non-root
867 * ignores everything but the user namespace. */
868 if (user_only ? !HAS_PREFIX(name, USER_PREFIX)
869 : HAS_PREFIX(name, SYSTEM_PREFIX))
872 if (am_root < 0 && name_len > RPRE_LEN
873 && name[RPRE_LEN] == '%' && strcmp(name, XSTAT_ATTR) == 0)
876 for (i = 0; i < xalp->count; i++) {
877 if (strcmp(name, rxas[i].name) == 0)
880 if (i == xalp->count) {
881 if (sys_lremovexattr(fname, name) < 0) {
882 rsyserr(FERROR_XFER, errno,
883 "rsync_xal_set: lremovexattr(\"%s\",\"%s\") failed",
884 full_fname(fname), name);
886 } else /* make sure caller sets mtime */
887 sxp->st.st_mtime = (time_t)-1;
894 /* Set extended attributes on indicated filename. */
895 int set_xattr(const char *fname, const struct file_struct *file,
896 const char *fnamecmp, stat_x *sxp)
899 item_list *lst = rsync_xal_l.items;
902 return 1; /* FIXME: --dry-run needs to compute this value */
904 if (read_only || list_only) {
909 #ifdef NO_SPECIAL_XATTRS
910 if (IS_SPECIAL(sxp->st.st_mode)) {
915 #ifdef NO_DEVICE_XATTRS
916 if (IS_DEVICE(sxp->st.st_mode)) {
921 #ifdef NO_SYMLINK_XATTRS
922 if (S_ISLNK(sxp->st.st_mode)) {
929 return rsync_xal_set(fname, lst + ndx, fnamecmp, sxp);
933 char *get_xattr_acl(const char *fname, int is_access_acl, size_t *len_p)
935 const char *name = is_access_acl ? XACC_ACL_ATTR : XDEF_ACL_ATTR;
936 *len_p = 0; /* no extra data alloc needed from get_xattr_data() */
937 return get_xattr_data(fname, name, len_p, 1);
940 int set_xattr_acl(const char *fname, int is_access_acl, const char *buf, size_t buf_len)
942 const char *name = is_access_acl ? XACC_ACL_ATTR : XDEF_ACL_ATTR;
943 if (sys_lsetxattr(fname, name, buf, buf_len) < 0) {
944 rsyserr(FERROR_XFER, errno,
945 "set_xattr_acl: lsetxattr(\"%s\",\"%s\") failed",
946 full_fname(fname), name);
952 int del_def_xattr_acl(const char *fname)
954 return sys_lremovexattr(fname, XDEF_ACL_ATTR);
958 int get_stat_xattr(const char *fname, int fd, STRUCT_STAT *fst, STRUCT_STAT *xst)
960 int mode, rdev_major, rdev_minor, uid, gid, len;
963 if (am_root >= 0 || IS_DEVICE(fst->st_mode) || IS_SPECIAL(fst->st_mode))
972 len = sys_lgetxattr(fname, XSTAT_ATTR, buf, sizeof buf - 1);
975 len = sys_fgetxattr(fd, XSTAT_ATTR, buf, sizeof buf - 1);
977 if (len >= (int)sizeof buf) {
982 if (errno == ENOTSUP || errno == ENOATTR)
984 if (errno == EPERM && S_ISLNK(fst->st_mode)) {
989 rsyserr(FERROR_XFER, errno, "failed to read xattr %s for %s",
990 XSTAT_ATTR, full_fname(fname));
995 if (sscanf(buf, "%o %d,%d %d:%d",
996 &mode, &rdev_major, &rdev_minor, &uid, &gid) != 5) {
997 rprintf(FERROR, "Corrupt %s xattr attached to %s: \"%s\"\n",
998 XSTAT_ATTR, full_fname(fname), buf);
999 exit_cleanup(RERR_FILEIO);
1002 xst->st_mode = from_wire_mode(mode);
1003 xst->st_rdev = MAKEDEV(rdev_major, rdev_minor);
1010 int set_stat_xattr(const char *fname, struct file_struct *file, mode_t new_mode)
1012 STRUCT_STAT fst, xst;
1019 if (read_only || list_only) {
1020 rsyserr(FERROR_XFER, EROFS, "failed to write xattr %s for %s",
1021 XSTAT_ATTR, full_fname(fname));
1025 if (x_lstat(fname, &fst, &xst) < 0) {
1026 rsyserr(FERROR_XFER, errno, "failed to re-stat %s",
1031 fst.st_mode &= (_S_IFMT | CHMOD_BITS);
1032 fmode = new_mode & (_S_IFMT | CHMOD_BITS);
1034 if (IS_DEVICE(fmode)) {
1035 uint32 *devp = F_RDEV_P(file);
1036 rdev = MAKEDEV(DEV_MAJOR(devp), DEV_MINOR(devp));
1040 /* Dump the special permissions and enable full owner access. */
1041 mode = (fst.st_mode & _S_IFMT) | (fmode & ACCESSPERMS)
1042 | (S_ISDIR(fst.st_mode) ? 0700 : 0600);
1043 if (fst.st_mode != mode)
1044 do_chmod(fname, mode);
1045 if (!IS_DEVICE(fst.st_mode))
1046 fst.st_rdev = 0; /* just in case */
1048 if (mode == fmode && fst.st_rdev == rdev
1049 && fst.st_uid == F_OWNER(file) && fst.st_gid == F_GROUP(file)) {
1050 /* xst.st_mode will be 0 if there's no current stat xattr */
1051 if (xst.st_mode && sys_lremovexattr(fname, XSTAT_ATTR) < 0) {
1052 rsyserr(FERROR_XFER, errno,
1053 "delete of stat xattr failed for %s",
1060 if (xst.st_mode != fmode || xst.st_rdev != rdev
1061 || xst.st_uid != F_OWNER(file) || xst.st_gid != F_GROUP(file)) {
1063 int len = snprintf(buf, sizeof buf, "%o %u,%u %u:%u",
1064 to_wire_mode(fmode),
1065 (int)major(rdev), (int)minor(rdev),
1066 F_OWNER(file), F_GROUP(file));
1067 if (sys_lsetxattr(fname, XSTAT_ATTR, buf, len) < 0) {
1068 if (errno == EPERM && S_ISLNK(fst.st_mode))
1070 rsyserr(FERROR_XFER, errno,
1071 "failed to write xattr %s for %s",
1072 XSTAT_ATTR, full_fname(fname));
1080 int x_stat(const char *fname, STRUCT_STAT *fst, STRUCT_STAT *xst)
1082 int ret = do_stat(fname, fst);
1083 if ((ret < 0 || get_stat_xattr(fname, -1, fst, xst) < 0) && xst)
1088 int x_lstat(const char *fname, STRUCT_STAT *fst, STRUCT_STAT *xst)
1090 int ret = do_lstat(fname, fst);
1091 if ((ret < 0 || get_stat_xattr(fname, -1, fst, xst) < 0) && xst)
1096 int x_fstat(int fd, STRUCT_STAT *fst, STRUCT_STAT *xst)
1098 int ret = do_fstat(fd, fst);
1099 if ((ret < 0 || get_stat_xattr(NULL, fd, fst, xst) < 0) && xst)
1104 #endif /* SUPPORT_XATTRS */