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.
25 #include "lib/sysxattrs.h"
32 extern int am_generator;
35 extern int preserve_xattrs;
36 extern int checksum_seed;
38 #define RSYNC_XAL_INITIAL 5
39 #define RSYNC_XAL_LIST_INITIAL 100
41 #define MAX_FULL_DATUM 32
43 #define HAS_PREFIX(str, prfx) (*(str) == *(prfx) \
44 && strncmp(str, prfx, sizeof (prfx) - 1) == 0)
46 #define XATTR_ABBREV(x) ((size_t)((x).name - (x).datum) < (x).datum_len)
48 #define XSTATE_ABBREV 1
52 #define USER_PREFIX "user."
53 #define UPRE_LEN ((int)sizeof USER_PREFIX - 1)
54 #define SYSTEM_PREFIX "system."
55 #define SPRE_LEN ((int)sizeof SYSTEM_PREFIX - 1)
57 #ifdef HAVE_LINUX_XATTRS
58 #define MIGHT_NEED_RPRE (am_root < 0)
59 #define RSYNC_PREFIX USER_PREFIX "rsync."
61 #define MIGHT_NEED_RPRE am_root
62 #define RSYNC_PREFIX "rsync."
64 #define RPRE_LEN ((int)sizeof RSYNC_PREFIX - 1)
66 #define XSTAT_SUFFIX "stat"
67 #define XSTAT_ATTR RSYNC_PREFIX "%" XSTAT_SUFFIX
68 #define XACC_ACL_SUFFIX "aacl"
69 #define XACC_ACL_ATTR RSYNC_PREFIX "%" XACC_ACL_SUFFIX
70 #define XDEF_ACL_SUFFIX "dacl"
71 #define XDEF_ACL_ATTR RSYNC_PREFIX "%" XDEF_ACL_SUFFIX
75 size_t datum_len, name_len;
79 static size_t namebuf_len = 0;
80 static char *namebuf = NULL;
82 static item_list empty_xattr = EMPTY_ITEM_LIST;
83 static item_list rsync_xal_l = EMPTY_ITEM_LIST;
85 static size_t prior_xattr_count = (size_t)-1;
87 /* ------------------------------------------------------------------------- */
89 static void rsync_xal_free(item_list *xalp)
92 rsync_xa *rxas = xalp->items;
94 for (i = 0; i < xalp->count; i++) {
96 /*free(rxas[i].name);*/
101 void free_xattr(stat_x *sxp)
105 rsync_xal_free(sxp->xattr);
110 static int rsync_xal_compare_names(const void *x1, const void *x2)
112 const rsync_xa *xa1 = x1;
113 const rsync_xa *xa2 = x2;
114 return strcmp(xa1->name, xa2->name);
117 static ssize_t get_xattr_names(const char *fname)
124 namebuf = new_array(char, namebuf_len);
126 out_of_memory("get_xattr_names");
130 /* The length returned includes all the '\0' terminators. */
131 list_len = sys_llistxattr(fname, namebuf, namebuf_len);
133 if ((size_t)list_len <= namebuf_len)
135 } else if (errno == ENOTSUP)
137 else if (errno != ERANGE) {
140 rsyserr(FERROR_XFER, errno,
141 "get_xattr_names: llistxattr(\"%s\",%s) failed",
142 fname, big_num(arg));
145 list_len = sys_llistxattr(fname, NULL, 0);
152 namebuf_len = list_len + 1024;
153 namebuf = new_array(char, namebuf_len);
155 out_of_memory("get_xattr_names");
161 /* On entry, the *len_ptr parameter contains the size of the extra space we
162 * should allocate when we create a buffer for the data. On exit, it contains
163 * the length of the datum. */
164 static char *get_xattr_data(const char *fname, const char *name, size_t *len_ptr,
165 int no_missing_error)
167 size_t datum_len = sys_lgetxattr(fname, name, NULL, 0);
168 size_t extra_len = *len_ptr;
171 *len_ptr = datum_len;
173 if (datum_len == (size_t)-1) {
174 if (errno == ENOTSUP || no_missing_error)
176 rsyserr(FERROR_XFER, errno,
177 "get_xattr_data: lgetxattr(\"%s\",\"%s\",0) failed",
182 if (!datum_len && !extra_len)
183 extra_len = 1; /* request non-zero amount of memory */
184 if (datum_len + extra_len < datum_len)
185 overflow_exit("get_xattr_data");
186 if (!(ptr = new_array(char, datum_len + extra_len)))
187 out_of_memory("get_xattr_data");
190 size_t len = sys_lgetxattr(fname, name, ptr, datum_len);
191 if (len != datum_len) {
192 if (len == (size_t)-1) {
193 rsyserr(FERROR_XFER, errno,
194 "get_xattr_data: lgetxattr(\"%s\",\"%s\",%ld)"
195 " failed", fname, name, (long)datum_len);
198 "get_xattr_data: lgetxattr(\"%s\",\"%s\",%ld)"
199 " returned %ld\n", fname, name,
200 (long)datum_len, (long)len);
210 static int rsync_xal_get(const char *fname, item_list *xalp)
212 ssize_t list_len, name_len;
213 size_t datum_len, name_offset;
215 #ifdef HAVE_LINUX_XATTRS
216 int user_only = am_sender ? 0 : !am_root;
221 /* This puts the name list into the "namebuf" buffer. */
222 if ((list_len = get_xattr_names(fname)) < 0)
225 for (name = namebuf; list_len > 0; name += name_len) {
226 name_len = strlen(name) + 1;
227 list_len -= name_len;
229 #ifdef HAVE_LINUX_XATTRS
230 /* We always ignore the system namespace, and non-root
231 * ignores everything but the user namespace. */
232 if (user_only ? !HAS_PREFIX(name, USER_PREFIX)
233 : HAS_PREFIX(name, SYSTEM_PREFIX))
237 /* No rsync.%FOO attributes are copied w/o 2 -X options. */
238 if (name_len > RPRE_LEN && name[RPRE_LEN] == '%'
239 && HAS_PREFIX(name, RSYNC_PREFIX)) {
240 if ((am_sender && preserve_xattrs < 2)
242 && (strcmp(name+RPRE_LEN+1, XSTAT_SUFFIX) == 0
243 || strcmp(name+RPRE_LEN+1, XACC_ACL_SUFFIX) == 0
244 || strcmp(name+RPRE_LEN+1, XDEF_ACL_SUFFIX) == 0)))
248 datum_len = name_len; /* Pass extra size to get_xattr_data() */
249 if (!(ptr = get_xattr_data(fname, name, &datum_len, 0)))
252 if (datum_len > MAX_FULL_DATUM) {
253 /* For large datums, we store a flag and a checksum. */
254 name_offset = 1 + MAX_DIGEST_LEN;
255 sum_init(checksum_seed);
256 sum_update(ptr, datum_len);
259 if (!(ptr = new_array(char, name_offset + name_len)))
260 out_of_memory("rsync_xal_get");
261 *ptr = XSTATE_ABBREV;
264 name_offset = datum_len;
266 rxa = EXPAND_ITEM_LIST(xalp, rsync_xa, RSYNC_XAL_INITIAL);
267 rxa->name = ptr + name_offset;
268 memcpy(rxa->name, name, name_len);
270 rxa->name_len = name_len;
271 rxa->datum_len = datum_len;
276 qsort(rxa, count, sizeof (rsync_xa), rsync_xal_compare_names);
277 for (rxa += count-1; count; count--, rxa--)
282 /* Read the xattr(s) for this filename. */
283 int get_xattr(const char *fname, stat_x *sxp)
285 sxp->xattr = new(item_list);
286 *sxp->xattr = empty_xattr;
287 if (rsync_xal_get(fname, sxp->xattr) < 0) {
294 int copy_xattrs(const char *source, const char *dest)
296 ssize_t list_len, name_len;
299 #ifdef HAVE_LINUX_XATTRS
300 int user_only = am_sender ? 0 : am_root <= 0;
303 /* This puts the name list into the "namebuf" buffer. */
304 if ((list_len = get_xattr_names(source)) < 0)
307 for (name = namebuf; list_len > 0; name += name_len) {
308 name_len = strlen(name) + 1;
309 list_len -= name_len;
311 #ifdef HAVE_LINUX_XATTRS
312 /* We always ignore the system namespace, and non-root
313 * ignores everything but the user namespace. */
314 if (user_only ? !HAS_PREFIX(name, USER_PREFIX)
315 : HAS_PREFIX(name, SYSTEM_PREFIX))
320 if (!(ptr = get_xattr_data(source, name, &datum_len, 0)))
322 if (sys_lsetxattr(dest, name, ptr, datum_len) < 0) {
323 int save_errno = errno ? errno : EINVAL;
324 rsyserr(FERROR_XFER, errno,
325 "rsync_xal_set: lsetxattr(\"%s\",\"%s\") failed",
336 static int find_matching_xattr(item_list *xalp)
339 item_list *lst = rsync_xal_l.items;
341 for (i = 0; i < rsync_xal_l.count; i++) {
342 rsync_xa *rxas1 = lst[i].items;
343 rsync_xa *rxas2 = xalp->items;
345 /* Wrong number of elements? */
346 if (lst[i].count != xalp->count)
348 /* any elements different? */
349 for (j = 0; j < xalp->count; j++) {
350 if (rxas1[j].name_len != rxas2[j].name_len
351 || rxas1[j].datum_len != rxas2[j].datum_len
352 || strcmp(rxas1[j].name, rxas2[j].name))
354 if (rxas1[j].datum_len > MAX_FULL_DATUM) {
355 if (memcmp(rxas1[j].datum + 1,
357 MAX_DIGEST_LEN) != 0)
360 if (memcmp(rxas1[j].datum, rxas2[j].datum,
365 /* no differences found. This is The One! */
366 if (j == xalp->count)
373 /* Store *xalp on the end of rsync_xal_l */
374 static void rsync_xal_store(item_list *xalp)
376 item_list *new_lst = EXPAND_ITEM_LIST(&rsync_xal_l, item_list, RSYNC_XAL_LIST_INITIAL);
377 /* Since the following call starts a new list, we know it will hold the
378 * entire initial-count, not just enough space for one new item. */
379 *new_lst = empty_xattr;
380 (void)EXPAND_ITEM_LIST(new_lst, rsync_xa, xalp->count);
381 memcpy(new_lst->items, xalp->items, xalp->count * sizeof (rsync_xa));
382 new_lst->count = xalp->count;
386 /* Send the make_xattr()-generated xattr list for this flist entry. */
387 int send_xattr(stat_x *sxp, int f)
389 int ndx = find_matching_xattr(sxp->xattr);
391 /* Send 0 (-1 + 1) to indicate that literal xattr data follows. */
392 write_varint(f, ndx + 1);
396 int count = sxp->xattr->count;
397 write_varint(f, count);
398 for (rxa = sxp->xattr->items; count--; rxa++) {
399 size_t name_len = rxa->name_len;
400 const char *name = rxa->name;
401 /* Strip the rsync prefix from disguised namespaces. */
402 if (name_len > RPRE_LEN
403 #ifdef HAVE_LINUX_XATTRS
406 && name[RPRE_LEN] != '%' && HAS_PREFIX(name, RSYNC_PREFIX)) {
408 name_len -= RPRE_LEN;
410 #ifndef HAVE_LINUX_XATTRS
412 /* Put everything else in the user namespace. */
413 name_len += UPRE_LEN;
416 write_varint(f, name_len);
417 write_varint(f, rxa->datum_len);
418 #ifndef HAVE_LINUX_XATTRS
419 if (name_len > rxa->name_len) {
420 write_buf(f, USER_PREFIX, UPRE_LEN);
421 name_len -= UPRE_LEN;
424 write_buf(f, name, name_len);
425 if (rxa->datum_len > MAX_FULL_DATUM)
426 write_buf(f, rxa->datum + 1, MAX_DIGEST_LEN);
428 write_buf(f, rxa->datum, rxa->datum_len);
430 ndx = rsync_xal_l.count; /* pre-incremented count */
431 rsync_xal_store(sxp->xattr); /* adds item to rsync_xal_l */
437 /* Return a flag indicating if we need to change a file's xattrs. If
438 * "find_all" is specified, also mark any abbreviated xattrs that we
439 * need so that send_xattr_request() can tell the sender about them. */
440 int xattr_diff(struct file_struct *file, stat_x *sxp, int find_all)
442 item_list *lst = rsync_xal_l.items;
443 rsync_xa *snd_rxa, *rec_rxa;
444 int snd_cnt, rec_cnt;
445 int cmp, same, xattrs_equal = 1;
447 if (sxp && XATTR_READY(*sxp)) {
448 rec_rxa = sxp->xattr->items;
449 rec_cnt = sxp->xattr->count;
455 if (F_XATTR(file) >= 0)
456 lst += F_XATTR(file);
460 snd_rxa = lst->items;
461 snd_cnt = lst->count;
463 /* If the count of the sender's xattrs is different from our
464 * (receiver's) xattrs, the lists are not the same. */
465 if (snd_cnt != rec_cnt) {
472 cmp = rec_cnt ? strcmp(snd_rxa->name, rec_rxa->name) : -1;
475 else if (snd_rxa->datum_len > MAX_FULL_DATUM) {
476 same = cmp == 0 && snd_rxa->datum_len == rec_rxa->datum_len
477 && memcmp(snd_rxa->datum + 1, rec_rxa->datum + 1,
478 MAX_DIGEST_LEN) == 0;
479 /* Flag unrequested items that we need. */
480 if (!same && find_all && snd_rxa->datum[0] == XSTATE_ABBREV)
481 snd_rxa->datum[0] = XSTATE_TODO;
483 same = cmp == 0 && snd_rxa->datum_len == rec_rxa->datum_len
484 && memcmp(snd_rxa->datum, rec_rxa->datum,
485 snd_rxa->datum_len) == 0;
506 return !xattrs_equal;
509 /* When called by the generator (with a NULL fname), this tells the sender
510 * all the abbreviated xattr values we need. When called by the sender
511 * (with a non-NULL fname), we send all the extra xattr data it needs.
512 * The generator may also call with f_out < 0 to just change all the
513 * XSTATE_ABBREV states into XSTATE_DONE. */
514 void send_xattr_request(const char *fname, struct file_struct *file, int f_out)
516 item_list *lst = rsync_xal_l.items;
517 int cnt, prior_req = 0;
520 lst += F_XATTR(file);
521 for (rxa = lst->items, cnt = lst->count; cnt--; rxa++) {
522 if (rxa->datum_len <= MAX_FULL_DATUM)
524 switch (rxa->datum[0]) {
526 /* Items left abbreviated matched the sender's checksum, so
527 * the receiver will cache the local data for future use. */
529 rxa->datum[0] = XSTATE_DONE;
538 /* Flag that we handled this abbreviated item. */
539 rxa->datum[0] = XSTATE_DONE;
541 write_varint(f_out, rxa->num - prior_req);
542 prior_req = rxa->num;
548 /* Re-read the long datum. */
549 if (!(ptr = get_xattr_data(fname, rxa->name, &len, 0))) {
550 rprintf(FERROR_XFER, "failed to re-read xattr %s for %s\n", rxa->name, fname);
551 write_varint(f_out, 0);
555 write_varint(f_out, len); /* length might have changed! */
556 write_buf(f_out, ptr, len);
562 write_byte(f_out, 0); /* end the list */
565 /* When called by the sender, read the request from the generator and mark
566 * any needed xattrs with a flag that lets us know they need to be sent to
567 * the receiver. When called by the receiver, reads the sent data and
568 * stores it in place of its checksum. */
569 int recv_xattr_request(struct file_struct *file, int f_in)
571 item_list *lst = rsync_xal_l.items;
572 char *old_datum, *name;
574 int rel_pos, cnt, num, got_xattr_data = 0;
576 if (F_XATTR(file) < 0) {
577 rprintf(FERROR, "recv_xattr_request: internal data error!\n");
578 exit_cleanup(RERR_STREAMIO);
580 lst += F_XATTR(file);
585 while ((rel_pos = read_varint(f_in)) != 0) {
587 while (cnt && rxa->num < num) {
591 if (!cnt || rxa->num != num) {
592 rprintf(FERROR, "[%s] could not find xattr #%d for %s\n",
593 who_am_i(), num, f_name(file, NULL));
594 exit_cleanup(RERR_STREAMIO);
596 if (!XATTR_ABBREV(*rxa) || rxa->datum[0] != XSTATE_ABBREV) {
597 rprintf(FERROR, "[%s] internal abbrev error on %s (%s, len=%ld)!\n",
598 who_am_i(), f_name(file, NULL), rxa->name, (long)rxa->datum_len);
599 exit_cleanup(RERR_STREAMIO);
603 rxa->datum[0] = XSTATE_TODO;
607 old_datum = rxa->datum;
608 rxa->datum_len = read_varint(f_in);
610 if (rxa->name_len + rxa->datum_len < rxa->name_len)
611 overflow_exit("recv_xattr_request");
612 rxa->datum = new_array(char, rxa->datum_len + rxa->name_len);
614 out_of_memory("recv_xattr_request");
615 name = rxa->datum + rxa->datum_len;
616 memcpy(name, rxa->name, rxa->name_len);
619 read_buf(f_in, rxa->datum, rxa->datum_len);
623 return got_xattr_data;
626 /* ------------------------------------------------------------------------- */
628 /* receive and build the rsync_xattr_lists */
629 void receive_xattr(struct file_struct *file, int f)
631 static item_list temp_xattr = EMPTY_ITEM_LIST;
633 #ifdef HAVE_LINUX_XATTRS
638 int ndx = read_varint(f);
640 if (ndx < 0 || (size_t)ndx > rsync_xal_l.count) {
641 rprintf(FERROR, "receive_xattr: xa index %d out of"
642 " range for %s\n", ndx, f_name(file, NULL));
643 exit_cleanup(RERR_STREAMIO);
647 F_XATTR(file) = ndx - 1;
651 if ((count = read_varint(f)) != 0) {
652 (void)EXPAND_ITEM_LIST(&temp_xattr, rsync_xa, count);
653 temp_xattr.count = 0;
656 for (num = 1; num <= count; num++) {
659 size_t name_len = read_varint(f);
660 size_t datum_len = read_varint(f);
661 size_t dget_len = datum_len > MAX_FULL_DATUM ? 1 + MAX_DIGEST_LEN : datum_len;
662 size_t extra_len = MIGHT_NEED_RPRE ? RPRE_LEN : 0;
663 if ((dget_len + extra_len < dget_len)
664 || (dget_len + extra_len + name_len < dget_len))
665 overflow_exit("receive_xattr");
666 ptr = new_array(char, dget_len + extra_len + name_len);
668 out_of_memory("receive_xattr");
669 name = ptr + dget_len + extra_len;
670 read_buf(f, name, name_len);
671 if (dget_len == datum_len)
672 read_buf(f, ptr, dget_len);
674 *ptr = XSTATE_ABBREV;
675 read_buf(f, ptr + 1, MAX_DIGEST_LEN);
677 #ifdef HAVE_LINUX_XATTRS
678 /* Non-root can only save the user namespace. */
679 if (am_root <= 0 && !HAS_PREFIX(name, USER_PREFIX)) {
685 name_len += RPRE_LEN;
686 memcpy(name, RSYNC_PREFIX, RPRE_LEN);
690 /* This OS only has a user namespace, so we either
691 * strip the user prefix, or we put a non-user
692 * namespace inside our rsync hierarchy. */
693 if (HAS_PREFIX(name, USER_PREFIX)) {
695 name_len -= UPRE_LEN;
696 } else if (am_root) {
698 name_len += RPRE_LEN;
699 memcpy(name, RSYNC_PREFIX, RPRE_LEN);
705 /* No rsync.%FOO attributes are copied w/o 2 -X options. */
706 if (preserve_xattrs < 2 && name_len > RPRE_LEN
707 && name[RPRE_LEN] == '%' && HAS_PREFIX(name, RSYNC_PREFIX)) {
711 rxa = EXPAND_ITEM_LIST(&temp_xattr, rsync_xa, 1);
714 rxa->name_len = name_len;
715 rxa->datum_len = datum_len;
719 if (need_sort && count > 1)
720 qsort(temp_xattr.items, count, sizeof (rsync_xa), rsync_xal_compare_names);
722 ndx = rsync_xal_l.count; /* pre-incremented count */
723 rsync_xal_store(&temp_xattr); /* adds item to rsync_xal_l */
728 /* Turn the xattr data in stat_x into cached xattr data, setting the index
729 * values in the file struct. */
730 void cache_tmp_xattr(struct file_struct *file, stat_x *sxp)
737 if (prior_xattr_count == (size_t)-1)
738 prior_xattr_count = rsync_xal_l.count;
739 ndx = find_matching_xattr(sxp->xattr);
741 rsync_xal_store(sxp->xattr); /* adds item to rsync_xal_l */
746 void uncache_tmp_xattrs(void)
748 if (prior_xattr_count != (size_t)-1) {
749 item_list *xattr_item = rsync_xal_l.items;
750 item_list *xattr_start = xattr_item + prior_xattr_count;
751 xattr_item += rsync_xal_l.count;
752 rsync_xal_l.count = prior_xattr_count;
753 while (xattr_item-- > xattr_start) {
754 rsync_xal_free(xattr_item);
757 prior_xattr_count = (size_t)-1;
761 static int rsync_xal_set(const char *fname, item_list *xalp,
762 const char *fnamecmp, stat_x *sxp)
764 rsync_xa *rxas = xalp->items;
767 char *name, *ptr, sum[MAX_DIGEST_LEN];
768 #ifdef HAVE_LINUX_XATTRS
769 int user_only = am_root <= 0;
774 /* This puts the current name list into the "namebuf" buffer. */
775 if ((list_len = get_xattr_names(fname)) < 0)
778 for (i = 0; i < xalp->count; i++) {
781 if (XATTR_ABBREV(rxas[i])) {
782 /* See if the fnamecmp version is identical. */
783 len = name_len = rxas[i].name_len;
784 if ((ptr = get_xattr_data(fnamecmp, name, &len, 1)) == NULL) {
788 rprintf(FERROR, "Missing abbreviated xattr value, %s, for %s\n",
789 rxas[i].name, full_fname(fname));
793 if (len != rxas[i].datum_len) {
798 sum_init(checksum_seed);
799 sum_update(ptr, len);
801 if (memcmp(sum, rxas[i].datum + 1, MAX_DIGEST_LEN) != 0) {
806 if (fname == fnamecmp)
807 ; /* Value is already set when identical */
808 else if (sys_lsetxattr(fname, name, ptr, len) < 0) {
809 rsyserr(FERROR_XFER, errno,
810 "rsync_xal_set: lsetxattr(\"%s\",\"%s\") failed",
813 } else /* make sure caller sets mtime */
814 sxp->st.st_mtime = (time_t)-1;
816 if (am_generator) { /* generator items stay abbreviated */
821 memcpy(ptr + len, name, name_len);
824 rxas[i].name = name = ptr + len;
829 if (sys_lsetxattr(fname, name, rxas[i].datum, rxas[i].datum_len) < 0) {
830 rsyserr(FERROR_XFER, errno,
831 "rsync_xal_set: lsetxattr(\"%s\",\"%s\") failed",
834 } else /* make sure caller sets mtime */
835 sxp->st.st_mtime = (time_t)-1;
838 /* Remove any extraneous names. */
839 for (name = namebuf; list_len > 0; name += name_len) {
840 name_len = strlen(name) + 1;
841 list_len -= name_len;
843 #ifdef HAVE_LINUX_XATTRS
844 /* We always ignore the system namespace, and non-root
845 * ignores everything but the user namespace. */
846 if (user_only ? !HAS_PREFIX(name, USER_PREFIX)
847 : HAS_PREFIX(name, SYSTEM_PREFIX))
850 if (am_root < 0 && name_len > RPRE_LEN
851 && name[RPRE_LEN] == '%' && strcmp(name, XSTAT_ATTR) == 0)
854 for (i = 0; i < xalp->count; i++) {
855 if (strcmp(name, rxas[i].name) == 0)
858 if (i == xalp->count) {
859 if (sys_lremovexattr(fname, name) < 0) {
860 rsyserr(FERROR_XFER, errno,
861 "rsync_xal_clear: lremovexattr(\"%s\",\"%s\") failed",
864 } else /* make sure caller sets mtime */
865 sxp->st.st_mtime = (time_t)-1;
872 /* Set extended attributes on indicated filename. */
873 int set_xattr(const char *fname, const struct file_struct *file,
874 const char *fnamecmp, stat_x *sxp)
877 item_list *lst = rsync_xal_l.items;
880 return 1; /* FIXME: --dry-run needs to compute this value */
882 if (read_only || list_only) {
888 return rsync_xal_set(fname, lst + ndx, fnamecmp, sxp);
892 char *get_xattr_acl(const char *fname, int is_access_acl, size_t *len_p)
894 const char *name = is_access_acl ? XACC_ACL_ATTR : XDEF_ACL_ATTR;
895 *len_p = 0; /* no extra data alloc needed from get_xattr_data() */
896 return get_xattr_data(fname, name, len_p, 1);
899 int set_xattr_acl(const char *fname, int is_access_acl, const char *buf, size_t buf_len)
901 const char *name = is_access_acl ? XACC_ACL_ATTR : XDEF_ACL_ATTR;
902 if (sys_lsetxattr(fname, name, buf, buf_len) < 0) {
903 rsyserr(FERROR_XFER, errno,
904 "set_xattr_acl: lsetxattr(\"%s\",\"%s\") failed",
911 int del_def_xattr_acl(const char *fname)
913 return sys_lremovexattr(fname, XDEF_ACL_ATTR);
917 int get_stat_xattr(const char *fname, int fd, STRUCT_STAT *fst, STRUCT_STAT *xst)
919 int mode, rdev_major, rdev_minor, uid, gid, len;
922 if (am_root >= 0 || IS_DEVICE(fst->st_mode) || IS_SPECIAL(fst->st_mode))
931 len = sys_lgetxattr(fname, XSTAT_ATTR, buf, sizeof buf - 1);
934 len = sys_fgetxattr(fd, XSTAT_ATTR, buf, sizeof buf - 1);
936 if (len >= (int)sizeof buf) {
941 if (errno == ENOTSUP || errno == ENOATTR)
943 if (errno == EPERM && S_ISLNK(fst->st_mode)) {
948 rsyserr(FERROR_XFER, errno, "failed to read xattr %s for %s",
949 XSTAT_ATTR, full_fname(fname));
954 if (sscanf(buf, "%o %d,%d %d:%d",
955 &mode, &rdev_major, &rdev_minor, &uid, &gid) != 5) {
956 rprintf(FERROR, "Corrupt %s xattr attached to %s: \"%s\"\n",
957 XSTAT_ATTR, full_fname(fname), buf);
958 exit_cleanup(RERR_FILEIO);
961 xst->st_mode = from_wire_mode(mode);
962 xst->st_rdev = MAKEDEV(rdev_major, rdev_minor);
969 int set_stat_xattr(const char *fname, struct file_struct *file, mode_t new_mode)
971 STRUCT_STAT fst, xst;
978 if (read_only || list_only) {
979 rsyserr(FERROR_XFER, EROFS, "failed to write xattr %s for %s",
980 XSTAT_ATTR, full_fname(fname));
984 if (x_lstat(fname, &fst, &xst) < 0) {
985 rsyserr(FERROR_XFER, errno, "failed to re-stat %s",
990 fst.st_mode &= (_S_IFMT | CHMOD_BITS);
991 fmode = new_mode & (_S_IFMT | CHMOD_BITS);
993 if (IS_DEVICE(fmode)) {
994 uint32 *devp = F_RDEV_P(file);
995 rdev = MAKEDEV(DEV_MAJOR(devp), DEV_MINOR(devp));
999 /* Dump the special permissions and enable full owner access. */
1000 mode = (fst.st_mode & _S_IFMT) | (fmode & ACCESSPERMS)
1001 | (S_ISDIR(fst.st_mode) ? 0700 : 0600);
1002 if (fst.st_mode != mode)
1003 do_chmod(fname, mode);
1004 if (!IS_DEVICE(fst.st_mode))
1005 fst.st_rdev = 0; /* just in case */
1007 if (mode == fmode && fst.st_rdev == rdev
1008 && fst.st_uid == F_OWNER(file) && fst.st_gid == F_GROUP(file)) {
1009 /* xst.st_mode will be 0 if there's no current stat xattr */
1010 if (xst.st_mode && sys_lremovexattr(fname, XSTAT_ATTR) < 0) {
1011 rsyserr(FERROR_XFER, errno,
1012 "delete of stat xattr failed for %s",
1019 if (xst.st_mode != fmode || xst.st_rdev != rdev
1020 || xst.st_uid != F_OWNER(file) || xst.st_gid != F_GROUP(file)) {
1022 int len = snprintf(buf, sizeof buf, "%o %u,%u %u:%u",
1023 to_wire_mode(fmode),
1024 (int)major(rdev), (int)minor(rdev),
1025 F_OWNER(file), F_GROUP(file));
1026 if (sys_lsetxattr(fname, XSTAT_ATTR, buf, len) < 0) {
1027 if (errno == EPERM && S_ISLNK(fst.st_mode))
1029 rsyserr(FERROR_XFER, errno,
1030 "failed to write xattr %s for %s",
1031 XSTAT_ATTR, full_fname(fname));
1039 int x_stat(const char *fname, STRUCT_STAT *fst, STRUCT_STAT *xst)
1041 int ret = do_stat(fname, fst);
1042 if ((ret < 0 || get_stat_xattr(fname, -1, fst, xst) < 0) && xst)
1047 int x_lstat(const char *fname, STRUCT_STAT *fst, STRUCT_STAT *xst)
1049 int ret = do_lstat(fname, fst);
1050 if ((ret < 0 || get_stat_xattr(fname, -1, fst, xst) < 0) && xst)
1055 int x_fstat(int fd, STRUCT_STAT *fst, STRUCT_STAT *xst)
1057 int ret = do_fstat(fd, fst);
1058 if ((ret < 0 || get_stat_xattr(NULL, fd, fst, xst) < 0) && xst)
1063 #endif /* SUPPORT_XATTRS */