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 /* ------------------------------------------------------------------------- */
87 static void rsync_xal_free(item_list *xalp)
90 rsync_xa *rxas = xalp->items;
92 for (i = 0; i < xalp->count; i++) {
94 /*free(rxas[i].name);*/
99 void free_xattr(stat_x *sxp)
103 rsync_xal_free(sxp->xattr);
108 static int rsync_xal_compare_names(const void *x1, const void *x2)
110 const rsync_xa *xa1 = x1;
111 const rsync_xa *xa2 = x2;
112 return strcmp(xa1->name, xa2->name);
115 static ssize_t get_xattr_names(const char *fname)
122 namebuf = new_array(char, namebuf_len);
124 out_of_memory("get_xattr_names");
128 /* The length returned includes all the '\0' terminators. */
129 list_len = sys_llistxattr(fname, namebuf, namebuf_len);
131 if ((size_t)list_len <= namebuf_len)
133 } else if (errno == ENOTSUP)
135 else if (errno != ERANGE) {
138 rsyserr(FERROR_XFER, errno,
139 "get_xattr_names: llistxattr(\"%s\",%s) failed",
140 fname, big_num(arg));
143 list_len = sys_llistxattr(fname, NULL, 0);
150 namebuf_len = list_len + 1024;
151 namebuf = new_array(char, namebuf_len);
153 out_of_memory("get_xattr_names");
159 /* On entry, the *len_ptr parameter contains the size of the extra space we
160 * should allocate when we create a buffer for the data. On exit, it contains
161 * the length of the datum. */
162 static char *get_xattr_data(const char *fname, const char *name, size_t *len_ptr,
163 int no_missing_error)
165 size_t datum_len = sys_lgetxattr(fname, name, NULL, 0);
166 size_t extra_len = *len_ptr;
169 *len_ptr = datum_len;
171 if (datum_len == (size_t)-1) {
172 if (errno == ENOTSUP || no_missing_error)
174 rsyserr(FERROR_XFER, errno,
175 "get_xattr_data: lgetxattr(\"%s\",\"%s\",0) failed",
180 if (!datum_len && !extra_len)
181 extra_len = 1; /* request non-zero amount of memory */
182 if (datum_len + extra_len < datum_len)
183 overflow_exit("get_xattr_data");
184 if (!(ptr = new_array(char, datum_len + extra_len)))
185 out_of_memory("get_xattr_data");
188 size_t len = sys_lgetxattr(fname, name, ptr, datum_len);
189 if (len != datum_len) {
190 if (len == (size_t)-1) {
191 rsyserr(FERROR_XFER, errno,
192 "get_xattr_data: lgetxattr(\"%s\",\"%s\",%ld)"
193 " failed", fname, name, (long)datum_len);
196 "get_xattr_data: lgetxattr(\"%s\",\"%s\",%ld)"
197 " returned %ld\n", fname, name,
198 (long)datum_len, (long)len);
208 static int rsync_xal_get(const char *fname, item_list *xalp)
210 ssize_t list_len, name_len;
211 size_t datum_len, name_offset;
213 #ifdef HAVE_LINUX_XATTRS
214 int user_only = am_sender ? 0 : !am_root;
219 /* This puts the name list into the "namebuf" buffer. */
220 if ((list_len = get_xattr_names(fname)) < 0)
223 for (name = namebuf; list_len > 0; name += name_len) {
224 name_len = strlen(name) + 1;
225 list_len -= name_len;
227 #ifdef HAVE_LINUX_XATTRS
228 /* We always ignore the system namespace, and non-root
229 * ignores everything but the user namespace. */
230 if (user_only ? !HAS_PREFIX(name, USER_PREFIX)
231 : HAS_PREFIX(name, SYSTEM_PREFIX))
235 /* No rsync.%FOO attributes are copied w/o 2 -X options. */
236 if (name_len > RPRE_LEN && name[RPRE_LEN] == '%'
237 && HAS_PREFIX(name, RSYNC_PREFIX)) {
238 if ((am_sender && preserve_xattrs < 2)
240 && (strcmp(name+RPRE_LEN+1, XSTAT_SUFFIX) == 0
241 || strcmp(name+RPRE_LEN+1, XACC_ACL_SUFFIX) == 0
242 || strcmp(name+RPRE_LEN+1, XDEF_ACL_SUFFIX) == 0)))
246 datum_len = name_len; /* Pass extra size to get_xattr_data() */
247 if (!(ptr = get_xattr_data(fname, name, &datum_len, 0)))
250 if (datum_len > MAX_FULL_DATUM) {
251 /* For large datums, we store a flag and a checksum. */
252 name_offset = 1 + MAX_DIGEST_LEN;
253 sum_init(checksum_seed);
254 sum_update(ptr, datum_len);
257 if (!(ptr = new_array(char, name_offset + name_len)))
258 out_of_memory("rsync_xal_get");
259 *ptr = XSTATE_ABBREV;
262 name_offset = datum_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 int copy_xattrs(const char *source, const char *dest)
294 ssize_t list_len, name_len;
297 #ifdef HAVE_LINUX_XATTRS
298 int user_only = am_sender ? 0 : am_root <= 0;
301 /* This puts the name list into the "namebuf" buffer. */
302 if ((list_len = get_xattr_names(source)) < 0)
305 for (name = namebuf; list_len > 0; name += name_len) {
306 name_len = strlen(name) + 1;
307 list_len -= name_len;
309 #ifdef HAVE_LINUX_XATTRS
310 /* We always ignore the system namespace, and non-root
311 * ignores everything but the user namespace. */
312 if (user_only ? !HAS_PREFIX(name, USER_PREFIX)
313 : HAS_PREFIX(name, SYSTEM_PREFIX))
318 if (!(ptr = get_xattr_data(source, name, &datum_len, 0)))
320 if (sys_lsetxattr(dest, name, ptr, datum_len) < 0) {
321 int save_errno = errno ? errno : EINVAL;
322 rsyserr(FERROR_XFER, errno,
323 "rsync_xal_set: lsetxattr(\"%s\",\"%s\") failed",
334 static int find_matching_xattr(item_list *xalp)
337 item_list *lst = rsync_xal_l.items;
339 for (i = 0; i < rsync_xal_l.count; i++) {
340 rsync_xa *rxas1 = lst[i].items;
341 rsync_xa *rxas2 = xalp->items;
343 /* Wrong number of elements? */
344 if (lst[i].count != xalp->count)
346 /* any elements different? */
347 for (j = 0; j < xalp->count; j++) {
348 if (rxas1[j].name_len != rxas2[j].name_len
349 || rxas1[j].datum_len != rxas2[j].datum_len
350 || strcmp(rxas1[j].name, rxas2[j].name))
352 if (rxas1[j].datum_len > MAX_FULL_DATUM) {
353 if (memcmp(rxas1[j].datum + 1,
355 MAX_DIGEST_LEN) != 0)
358 if (memcmp(rxas1[j].datum, rxas2[j].datum,
363 /* no differences found. This is The One! */
364 if (j == xalp->count)
371 /* Store *xalp on the end of rsync_xal_l */
372 static void rsync_xal_store(item_list *xalp)
374 item_list *new_lst = EXPAND_ITEM_LIST(&rsync_xal_l, item_list, RSYNC_XAL_LIST_INITIAL);
375 /* Since the following call starts a new list, we know it will hold the
376 * entire initial-count, not just enough space for one new item. */
377 *new_lst = empty_xattr;
378 (void)EXPAND_ITEM_LIST(new_lst, rsync_xa, xalp->count);
379 memcpy(new_lst->items, xalp->items, xalp->count * sizeof (rsync_xa));
380 new_lst->count = xalp->count;
384 /* Send the make_xattr()-generated xattr list for this flist entry. */
385 int send_xattr(stat_x *sxp, int f)
387 int ndx = find_matching_xattr(sxp->xattr);
389 /* Send 0 (-1 + 1) to indicate that literal xattr data follows. */
390 write_varint(f, ndx + 1);
394 int count = sxp->xattr->count;
395 write_varint(f, count);
396 for (rxa = sxp->xattr->items; count--; rxa++) {
397 size_t name_len = rxa->name_len;
398 const char *name = rxa->name;
399 /* Strip the rsync prefix from disguised namespaces. */
400 if (name_len > RPRE_LEN
401 #ifdef HAVE_LINUX_XATTRS
404 && name[RPRE_LEN] != '%' && HAS_PREFIX(name, RSYNC_PREFIX)) {
406 name_len -= RPRE_LEN;
408 #ifndef HAVE_LINUX_XATTRS
410 /* Put everything else in the user namespace. */
411 name_len += UPRE_LEN;
414 write_varint(f, name_len);
415 write_varint(f, rxa->datum_len);
416 #ifndef HAVE_LINUX_XATTRS
417 if (name_len > rxa->name_len) {
418 write_buf(f, USER_PREFIX, UPRE_LEN);
419 name_len -= UPRE_LEN;
422 write_buf(f, name, name_len);
423 if (rxa->datum_len > MAX_FULL_DATUM)
424 write_buf(f, rxa->datum + 1, MAX_DIGEST_LEN);
426 write_buf(f, rxa->datum, rxa->datum_len);
428 ndx = rsync_xal_l.count; /* pre-incremented count */
429 rsync_xal_store(sxp->xattr); /* adds item to rsync_xal_l */
435 /* Return a flag indicating if we need to change a file's xattrs. If
436 * "find_all" is specified, also mark any abbreviated xattrs that we
437 * need so that send_xattr_request() can tell the sender about them. */
438 int xattr_diff(struct file_struct *file, stat_x *sxp, int find_all)
440 item_list *lst = rsync_xal_l.items;
441 rsync_xa *snd_rxa, *rec_rxa;
442 int snd_cnt, rec_cnt;
443 int cmp, same, xattrs_equal = 1;
445 if (sxp && XATTR_READY(*sxp)) {
446 rec_rxa = sxp->xattr->items;
447 rec_cnt = sxp->xattr->count;
453 if (F_XATTR(file) >= 0)
454 lst += F_XATTR(file);
458 snd_rxa = lst->items;
459 snd_cnt = lst->count;
461 /* If the count of the sender's xattrs is different from our
462 * (receiver's) xattrs, the lists are not the same. */
463 if (snd_cnt != rec_cnt) {
470 cmp = rec_cnt ? strcmp(snd_rxa->name, rec_rxa->name) : -1;
473 else if (snd_rxa->datum_len > MAX_FULL_DATUM) {
474 same = cmp == 0 && snd_rxa->datum_len == rec_rxa->datum_len
475 && memcmp(snd_rxa->datum + 1, rec_rxa->datum + 1,
476 MAX_DIGEST_LEN) == 0;
477 /* Flag unrequested items that we need. */
478 if (!same && find_all && snd_rxa->datum[0] == XSTATE_ABBREV)
479 snd_rxa->datum[0] = XSTATE_TODO;
481 same = cmp == 0 && snd_rxa->datum_len == rec_rxa->datum_len
482 && memcmp(snd_rxa->datum, rec_rxa->datum,
483 snd_rxa->datum_len) == 0;
504 return !xattrs_equal;
507 /* When called by the generator (with a NULL fname), this tells the sender
508 * all the abbreviated xattr values we need. When called by the sender
509 * (with a non-NULL fname), we send all the extra xattr data it needs.
510 * The generator may also call with f_out < 0 to just change all the
511 * XSTATE_ABBREV states into XSTATE_DONE. */
512 void send_xattr_request(const char *fname, struct file_struct *file, int f_out)
514 item_list *lst = rsync_xal_l.items;
515 int cnt, prior_req = 0;
518 lst += F_XATTR(file);
519 for (rxa = lst->items, cnt = lst->count; cnt--; rxa++) {
520 if (rxa->datum_len <= MAX_FULL_DATUM)
522 switch (rxa->datum[0]) {
524 /* Items left abbreviated matched the sender's checksum, so
525 * the receiver will cache the local data for future use. */
527 rxa->datum[0] = XSTATE_DONE;
536 /* Flag that we handled this abbreviated item. */
537 rxa->datum[0] = XSTATE_DONE;
539 write_varint(f_out, rxa->num - prior_req);
540 prior_req = rxa->num;
546 /* Re-read the long datum. */
547 if (!(ptr = get_xattr_data(fname, rxa->name, &len, 0))) {
548 rprintf(FERROR_XFER, "failed to re-read xattr %s for %s\n", rxa->name, fname);
549 write_varint(f_out, 0);
553 write_varint(f_out, len); /* length might have changed! */
554 write_buf(f_out, ptr, len);
560 write_byte(f_out, 0); /* end the list */
563 /* When called by the sender, read the request from the generator and mark
564 * any needed xattrs with a flag that lets us know they need to be sent to
565 * the receiver. When called by the receiver, reads the sent data and
566 * stores it in place of its checksum. */
567 int recv_xattr_request(struct file_struct *file, int f_in)
569 item_list *lst = rsync_xal_l.items;
570 char *old_datum, *name;
572 int rel_pos, cnt, num, got_xattr_data = 0;
574 if (F_XATTR(file) < 0) {
575 rprintf(FERROR, "recv_xattr_request: internal data error!\n");
576 exit_cleanup(RERR_STREAMIO);
578 lst += F_XATTR(file);
583 while ((rel_pos = read_varint(f_in)) != 0) {
585 while (cnt && rxa->num < num) {
589 if (!cnt || rxa->num != num) {
590 rprintf(FERROR, "[%s] could not find xattr #%d for %s\n",
591 who_am_i(), num, f_name(file, NULL));
592 exit_cleanup(RERR_STREAMIO);
594 if (!XATTR_ABBREV(*rxa) || rxa->datum[0] != XSTATE_ABBREV) {
595 rprintf(FERROR, "[%s] internal abbrev error on %s (%s, len=%ld)!\n",
596 who_am_i(), f_name(file, NULL), rxa->name, (long)rxa->datum_len);
597 exit_cleanup(RERR_STREAMIO);
601 rxa->datum[0] = XSTATE_TODO;
605 old_datum = rxa->datum;
606 rxa->datum_len = read_varint(f_in);
608 if (rxa->name_len + rxa->datum_len < rxa->name_len)
609 overflow_exit("recv_xattr_request");
610 rxa->datum = new_array(char, rxa->datum_len + rxa->name_len);
612 out_of_memory("recv_xattr_request");
613 name = rxa->datum + rxa->datum_len;
614 memcpy(name, rxa->name, rxa->name_len);
617 read_buf(f_in, rxa->datum, rxa->datum_len);
621 return got_xattr_data;
624 /* ------------------------------------------------------------------------- */
626 /* receive and build the rsync_xattr_lists */
627 void receive_xattr(struct file_struct *file, int f)
629 static item_list temp_xattr = EMPTY_ITEM_LIST;
631 #ifdef HAVE_LINUX_XATTRS
636 int ndx = read_varint(f);
638 if (ndx < 0 || (size_t)ndx > rsync_xal_l.count) {
639 rprintf(FERROR, "receive_xattr: xa index %d out of"
640 " range for %s\n", ndx, f_name(file, NULL));
641 exit_cleanup(RERR_STREAMIO);
645 F_XATTR(file) = ndx - 1;
649 if ((count = read_varint(f)) != 0) {
650 (void)EXPAND_ITEM_LIST(&temp_xattr, rsync_xa, count);
651 temp_xattr.count = 0;
654 for (num = 1; num <= count; num++) {
657 size_t name_len = read_varint(f);
658 size_t datum_len = read_varint(f);
659 size_t dget_len = datum_len > MAX_FULL_DATUM ? 1 + MAX_DIGEST_LEN : datum_len;
660 size_t extra_len = MIGHT_NEED_RPRE ? RPRE_LEN : 0;
661 if ((dget_len + extra_len < dget_len)
662 || (dget_len + extra_len + name_len < dget_len))
663 overflow_exit("receive_xattr");
664 ptr = new_array(char, dget_len + extra_len + name_len);
666 out_of_memory("receive_xattr");
667 name = ptr + dget_len + extra_len;
668 read_buf(f, name, name_len);
669 if (dget_len == datum_len)
670 read_buf(f, ptr, dget_len);
672 *ptr = XSTATE_ABBREV;
673 read_buf(f, ptr + 1, MAX_DIGEST_LEN);
675 #ifdef HAVE_LINUX_XATTRS
676 /* Non-root can only save the user namespace. */
677 if (am_root <= 0 && !HAS_PREFIX(name, USER_PREFIX)) {
683 name_len += RPRE_LEN;
684 memcpy(name, RSYNC_PREFIX, RPRE_LEN);
688 /* This OS only has a user namespace, so we either
689 * strip the user prefix, or we put a non-user
690 * namespace inside our rsync hierarchy. */
691 if (HAS_PREFIX(name, USER_PREFIX)) {
693 name_len -= UPRE_LEN;
694 } else if (am_root) {
696 name_len += RPRE_LEN;
697 memcpy(name, RSYNC_PREFIX, RPRE_LEN);
703 /* No rsync.%FOO attributes are copied w/o 2 -X options. */
704 if (preserve_xattrs < 2 && name_len > RPRE_LEN
705 && name[RPRE_LEN] == '%' && HAS_PREFIX(name, RSYNC_PREFIX)) {
709 rxa = EXPAND_ITEM_LIST(&temp_xattr, rsync_xa, 1);
712 rxa->name_len = name_len;
713 rxa->datum_len = datum_len;
717 if (need_sort && count > 1)
718 qsort(temp_xattr.items, count, sizeof (rsync_xa), rsync_xal_compare_names);
720 ndx = rsync_xal_l.count; /* pre-incremented count */
721 rsync_xal_store(&temp_xattr); /* adds item to rsync_xal_l */
726 /* Turn the xattr data in stat_x into cached xattr data, setting the index
727 * values in the file struct. */
728 void cache_xattr(struct file_struct *file, stat_x *sxp)
735 ndx = find_matching_xattr(sxp->xattr);
737 rsync_xal_store(sxp->xattr); /* adds item to rsync_xal_l */
742 static int rsync_xal_set(const char *fname, item_list *xalp,
743 const char *fnamecmp, stat_x *sxp)
745 rsync_xa *rxas = xalp->items;
748 char *name, *ptr, sum[MAX_DIGEST_LEN];
749 #ifdef HAVE_LINUX_XATTRS
750 int user_only = am_root <= 0;
755 /* This puts the current name list into the "namebuf" buffer. */
756 if ((list_len = get_xattr_names(fname)) < 0)
759 for (i = 0; i < xalp->count; i++) {
762 if (XATTR_ABBREV(rxas[i])) {
763 /* See if the fnamecmp version is identical. */
764 len = name_len = rxas[i].name_len;
765 if ((ptr = get_xattr_data(fnamecmp, name, &len, 1)) == NULL) {
769 rprintf(FERROR, "Missing abbreviated xattr value, %s, for %s\n",
770 rxas[i].name, full_fname(fname));
774 if (len != rxas[i].datum_len) {
779 sum_init(checksum_seed);
780 sum_update(ptr, len);
782 if (memcmp(sum, rxas[i].datum + 1, MAX_DIGEST_LEN) != 0) {
787 if (fname == fnamecmp)
788 ; /* Value is already set when identical */
789 else if (sys_lsetxattr(fname, name, ptr, len) < 0) {
790 rsyserr(FERROR_XFER, errno,
791 "rsync_xal_set: lsetxattr(\"%s\",\"%s\") failed",
794 } else /* make sure caller sets mtime */
795 sxp->st.st_mtime = (time_t)-1;
797 if (am_generator) { /* generator items stay abbreviated */
802 memcpy(ptr + len, name, name_len);
805 rxas[i].name = name = ptr + len;
810 if (sys_lsetxattr(fname, name, rxas[i].datum, rxas[i].datum_len) < 0) {
811 rsyserr(FERROR_XFER, errno,
812 "rsync_xal_set: lsetxattr(\"%s\",\"%s\") failed",
815 } else /* make sure caller sets mtime */
816 sxp->st.st_mtime = (time_t)-1;
819 /* Remove any extraneous names. */
820 for (name = namebuf; list_len > 0; name += name_len) {
821 name_len = strlen(name) + 1;
822 list_len -= name_len;
824 #ifdef HAVE_LINUX_XATTRS
825 /* We always ignore the system namespace, and non-root
826 * ignores everything but the user namespace. */
827 if (user_only ? !HAS_PREFIX(name, USER_PREFIX)
828 : HAS_PREFIX(name, SYSTEM_PREFIX))
831 if (am_root < 0 && name_len > RPRE_LEN
832 && name[RPRE_LEN] == '%' && strcmp(name, XSTAT_ATTR) == 0)
835 for (i = 0; i < xalp->count; i++) {
836 if (strcmp(name, rxas[i].name) == 0)
839 if (i == xalp->count) {
840 if (sys_lremovexattr(fname, name) < 0) {
841 rsyserr(FERROR_XFER, errno,
842 "rsync_xal_clear: lremovexattr(\"%s\",\"%s\") failed",
845 } else /* make sure caller sets mtime */
846 sxp->st.st_mtime = (time_t)-1;
853 /* Set extended attributes on indicated filename. */
854 int set_xattr(const char *fname, const struct file_struct *file,
855 const char *fnamecmp, stat_x *sxp)
858 item_list *lst = rsync_xal_l.items;
861 return 1; /* FIXME: --dry-run needs to compute this value */
863 if (read_only || list_only) {
869 return rsync_xal_set(fname, lst + ndx, fnamecmp, sxp);
873 char *get_xattr_acl(const char *fname, int is_access_acl, size_t *len_p)
875 const char *name = is_access_acl ? XACC_ACL_ATTR : XDEF_ACL_ATTR;
876 *len_p = 0; /* no extra data alloc needed from get_xattr_data() */
877 return get_xattr_data(fname, name, len_p, 1);
880 int set_xattr_acl(const char *fname, int is_access_acl, const char *buf, size_t buf_len)
882 const char *name = is_access_acl ? XACC_ACL_ATTR : XDEF_ACL_ATTR;
883 if (sys_lsetxattr(fname, name, buf, buf_len) < 0) {
884 rsyserr(FERROR_XFER, errno,
885 "set_xattr_acl: lsetxattr(\"%s\",\"%s\") failed",
892 int del_def_xattr_acl(const char *fname)
894 return sys_lremovexattr(fname, XDEF_ACL_ATTR);
898 int get_stat_xattr(const char *fname, int fd, STRUCT_STAT *fst, STRUCT_STAT *xst)
900 int mode, rdev_major, rdev_minor, uid, gid, len;
903 if (am_root >= 0 || IS_DEVICE(fst->st_mode) || IS_SPECIAL(fst->st_mode))
912 len = sys_lgetxattr(fname, XSTAT_ATTR, buf, sizeof buf - 1);
915 len = sys_fgetxattr(fd, XSTAT_ATTR, buf, sizeof buf - 1);
917 if (len >= (int)sizeof buf) {
922 if (errno == ENOTSUP || errno == ENOATTR)
924 if (errno == EPERM && S_ISLNK(fst->st_mode)) {
929 rsyserr(FERROR_XFER, errno, "failed to read xattr %s for %s",
930 XSTAT_ATTR, full_fname(fname));
935 if (sscanf(buf, "%o %d,%d %d:%d",
936 &mode, &rdev_major, &rdev_minor, &uid, &gid) != 5) {
937 rprintf(FERROR, "Corrupt %s xattr attached to %s: \"%s\"\n",
938 XSTAT_ATTR, full_fname(fname), buf);
939 exit_cleanup(RERR_FILEIO);
942 xst->st_mode = from_wire_mode(mode);
943 xst->st_rdev = MAKEDEV(rdev_major, rdev_minor);
950 int set_stat_xattr(const char *fname, struct file_struct *file, mode_t new_mode)
952 STRUCT_STAT fst, xst;
959 if (read_only || list_only) {
960 rsyserr(FERROR_XFER, EROFS, "failed to write xattr %s for %s",
961 XSTAT_ATTR, full_fname(fname));
965 if (x_lstat(fname, &fst, &xst) < 0) {
966 rsyserr(FERROR_XFER, errno, "failed to re-stat %s",
971 fst.st_mode &= (_S_IFMT | CHMOD_BITS);
972 fmode = new_mode & (_S_IFMT | CHMOD_BITS);
974 if (IS_DEVICE(fmode) || IS_SPECIAL(fmode)) {
975 uint32 *devp = F_RDEV_P(file);
976 rdev = MAKEDEV(DEV_MAJOR(devp), DEV_MINOR(devp));
980 /* Dump the special permissions and enable full owner access. */
981 mode = (fst.st_mode & _S_IFMT) | (fmode & ACCESSPERMS)
982 | (S_ISDIR(fst.st_mode) ? 0700 : 0600);
983 if (fst.st_mode != mode)
984 do_chmod(fname, mode);
985 if (!IS_DEVICE(fst.st_mode) && !IS_SPECIAL(fst.st_mode))
986 fst.st_rdev = 0; /* just in case */
988 if (mode == fmode && fst.st_rdev == rdev
989 && fst.st_uid == F_OWNER(file) && fst.st_gid == F_GROUP(file)) {
990 /* xst.st_mode will be 0 if there's no current stat xattr */
991 if (xst.st_mode && sys_lremovexattr(fname, XSTAT_ATTR) < 0) {
992 rsyserr(FERROR_XFER, errno,
993 "delete of stat xattr failed for %s",
1000 if (xst.st_mode != fmode || xst.st_rdev != rdev
1001 || xst.st_uid != F_OWNER(file) || xst.st_gid != F_GROUP(file)) {
1003 int len = snprintf(buf, sizeof buf, "%o %u,%u %u:%u",
1004 to_wire_mode(fmode),
1005 (int)major(rdev), (int)minor(rdev),
1006 F_OWNER(file), F_GROUP(file));
1007 if (sys_lsetxattr(fname, XSTAT_ATTR, buf, len) < 0) {
1008 if (errno == EPERM && S_ISLNK(fst.st_mode))
1010 rsyserr(FERROR_XFER, errno,
1011 "failed to write xattr %s for %s",
1012 XSTAT_ATTR, full_fname(fname));
1020 int x_stat(const char *fname, STRUCT_STAT *fst, STRUCT_STAT *xst)
1022 int ret = do_stat(fname, fst);
1023 if ((ret < 0 || get_stat_xattr(fname, -1, fst, xst) < 0) && xst)
1028 int x_lstat(const char *fname, STRUCT_STAT *fst, STRUCT_STAT *xst)
1030 int ret = do_lstat(fname, fst);
1031 if ((ret < 0 || get_stat_xattr(fname, -1, fst, xst) < 0) && xst)
1036 int x_fstat(int fd, STRUCT_STAT *fst, STRUCT_STAT *xst)
1038 int ret = do_fstat(fd, fst);
1039 if ((ret < 0 || get_stat_xattr(NULL, fd, fst, xst) < 0) && xst)
1044 #endif /* SUPPORT_XATTRS */