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, 2007 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 version 2 as
10 * published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
23 #include "lib/sysxattrs.h"
30 extern int am_generator;
33 extern int checksum_seed;
35 #define RSYNC_XAL_INITIAL 5
36 #define RSYNC_XAL_LIST_INITIAL 100
38 #define MAX_FULL_DATUM 32
40 #define HAS_PREFIX(str, prfx) (*(str) == *(prfx) \
41 && strncmp(str, prfx, sizeof (prfx) - 1) == 0)
43 #define XATTR_ABBREV(x) ((size_t)((x).name - (x).datum) < (x).datum_len)
45 #define XSTATE_ABBREV 0
48 #define XSTATE_LOCAL 3
50 #define USER_PREFIX "user."
51 #define UPRE_LEN ((int)sizeof USER_PREFIX - 1)
52 #define SYSTEM_PREFIX "system."
53 #define SPRE_LEN ((int)sizeof SYSTEM_PREFIX - 1)
55 #ifdef HAVE_LINUX_XATTRS
56 #define MIGHT_NEED_RPRE (am_root < 0)
57 #define RSYNC_PREFIX USER_PREFIX "rsync."
59 #define MIGHT_NEED_RPRE am_root
60 #define RSYNC_PREFIX "rsync."
62 #define RPRE_LEN ((int)sizeof RSYNC_PREFIX - 1)
64 #define XSTAT_ATTR RSYNC_PREFIX "%stat"
65 #define XSTAT_LEN ((int)sizeof XSTAT_ATTR - 1)
69 size_t datum_len, name_len;
72 static size_t namebuf_len = 0;
73 static char *namebuf = NULL;
75 static item_list empty_xattr = EMPTY_ITEM_LIST;
76 static item_list rsync_xal_l = EMPTY_ITEM_LIST;
78 /* ------------------------------------------------------------------------- */
80 static void rsync_xal_free(item_list *xalp)
83 rsync_xa *rxas = xalp->items;
85 for (i = 0; i < xalp->count; i++) {
87 /*free(rxas[i].name);*/
92 void free_xattr(statx *sxp)
96 rsync_xal_free(sxp->xattr);
101 static int rsync_xal_compare_names(const void *x1, const void *x2)
103 const rsync_xa *xa1 = x1;
104 const rsync_xa *xa2 = x2;
105 return strcmp(xa1->name, xa2->name);
108 static ssize_t get_xattr_names(const char *fname)
114 namebuf = new_array(char, namebuf_len);
116 out_of_memory("get_xattr_names");
119 /* The length returned includes all the '\0' terminators. */
120 list_len = sys_llistxattr(fname, namebuf, namebuf_len);
121 if (list_len > (ssize_t)namebuf_len) {
127 if (errno == ENOTSUP)
129 if (errno == ERANGE) {
130 list_len = sys_llistxattr(fname, NULL, 0);
132 rsyserr(FERROR, errno,
133 "get_xattr_names: llistxattr(\"%s\",0) failed",
139 namebuf_len = list_len + 1024;
140 namebuf = new_array(char, namebuf_len);
142 out_of_memory("get_xattr_names");
143 list_len = sys_llistxattr(fname, namebuf, namebuf_len);
148 rsyserr(FERROR, errno,
149 "get_xattr_names: llistxattr(\"%s\",%ld) failed",
150 fname, (long)namebuf_len);
154 /* On entry, the *len_ptr parameter contains the size of the extra space we
155 * should allocate when we create a buffer for the data. On exit, it contains
156 * the length of the datum. */
157 static char *get_xattr_data(const char *fname, const char *name, size_t *len_ptr,
158 int no_missing_error)
160 size_t datum_len = sys_lgetxattr(fname, name, NULL, 0);
163 if (datum_len == (size_t)-1) {
164 if (errno == ENOTSUP || no_missing_error)
166 rsyserr(FERROR, errno,
167 "get_xattr_data: lgetxattr(\"%s\",\"%s\",0) failed",
172 if (datum_len + *len_ptr < datum_len /* checks for overflow */
173 || !(ptr = new_array(char, datum_len + *len_ptr)))
174 out_of_memory("get_xattr_data");
176 *len_ptr = datum_len;
179 size_t len = sys_lgetxattr(fname, name, ptr, datum_len);
180 if (len != datum_len) {
181 if (len == (size_t)-1) {
182 rsyserr(FERROR, errno,
183 "get_xattr_data: lgetxattr(\"%s\",\"%s\",%ld)"
184 " failed", fname, name, (long)datum_len);
187 "get_xattr_data: lgetxattr(\"%s\",\"%s\",%ld)"
188 " returned %ld\n", fname, name,
189 (long)datum_len, (long)len);
199 static int rsync_xal_get(const char *fname, item_list *xalp)
201 ssize_t list_len, name_len;
202 size_t datum_len, name_offset;
204 #ifdef HAVE_LINUX_XATTRS
205 int user_only = am_sender ? 0 : !am_root;
208 /* This puts the name list into the "namebuf" buffer. */
209 if ((list_len = get_xattr_names(fname)) < 0)
212 for (name = namebuf; list_len > 0; name += name_len) {
215 name_len = strlen(name) + 1;
216 list_len -= name_len;
218 #ifdef HAVE_LINUX_XATTRS
219 /* We always ignore the system namespace, and non-root
220 * ignores everything but the user namespace. */
221 if (user_only ? !HAS_PREFIX(name, USER_PREFIX)
222 : HAS_PREFIX(name, SYSTEM_PREFIX))
226 if (am_root < 0 && name_len == XSTAT_LEN + 1
227 && name[RPRE_LEN] == '%' && strcmp(name, XSTAT_ATTR) == 0)
230 datum_len = name_len; /* Pass extra size to get_xattr_data() */
231 if (!(ptr = get_xattr_data(fname, name, &datum_len, 0)))
234 if (datum_len > MAX_FULL_DATUM) {
235 /* For large datums, we store a flag and a checksum. */
236 name_offset = 1 + MAX_DIGEST_LEN;
237 sum_init(checksum_seed);
238 sum_update(ptr, datum_len);
241 if (!(ptr = new_array(char, name_offset + name_len)))
242 out_of_memory("rsync_xal_get");
243 *ptr = XSTATE_ABBREV;
246 name_offset = datum_len;
248 #ifdef HAVE_LINUX_XATTRS
249 if (am_root < 0 && name_len > RPRE_LEN
250 && HAS_PREFIX(name, RSYNC_PREFIX)) {
252 name_len -= RPRE_LEN;
256 rxas = EXPAND_ITEM_LIST(xalp, rsync_xa, RSYNC_XAL_INITIAL);
257 rxas->name = ptr + name_offset;
258 memcpy(rxas->name, name, name_len);
260 rxas->name_len = name_len;
261 rxas->datum_len = datum_len;
264 qsort(xalp->items, xalp->count, sizeof (rsync_xa), rsync_xal_compare_names);
268 /* Read the xattr(s) for this filename. */
269 int get_xattr(const char *fname, statx *sxp)
271 sxp->xattr = new(item_list);
272 *sxp->xattr = empty_xattr;
273 if (rsync_xal_get(fname, sxp->xattr) < 0) {
280 static int find_matching_xattr(item_list *xalp)
283 item_list *lst = rsync_xal_l.items;
285 for (i = 0; i < rsync_xal_l.count; i++) {
286 rsync_xa *rxas1 = lst[i].items;
287 rsync_xa *rxas2 = xalp->items;
289 /* Wrong number of elements? */
290 if (lst[i].count != xalp->count)
292 /* any elements different? */
293 for (j = 0; j < xalp->count; j++) {
294 if (rxas1[j].name_len != rxas2[j].name_len
295 || rxas1[j].datum_len != rxas2[j].datum_len
296 || strcmp(rxas1[j].name, rxas2[j].name))
298 if (rxas1[j].datum_len > MAX_FULL_DATUM) {
299 if (memcmp(rxas1[j].datum + 1,
301 MAX_DIGEST_LEN) != 0)
304 if (memcmp(rxas1[j].datum, rxas2[j].datum,
309 /* no differences found. This is The One! */
310 if (j == xalp->count)
317 /* Store *xalp on the end of rsync_xal_l */
318 static void rsync_xal_store(item_list *xalp)
320 item_list *new_lst = EXPAND_ITEM_LIST(&rsync_xal_l, item_list, RSYNC_XAL_LIST_INITIAL);
321 /* Since the following call starts a new list, we know it will hold the
322 * entire initial-count, not just enough space for one new item. */
323 *new_lst = empty_xattr;
324 (void)EXPAND_ITEM_LIST(new_lst, rsync_xa, xalp->count);
325 memcpy(new_lst->items, xalp->items, xalp->count * sizeof (rsync_xa));
326 new_lst->count = xalp->count;
330 /* Send the make_xattr()-generated xattr list for this flist entry. */
331 int send_xattr(statx *sxp, int f)
333 int ndx = find_matching_xattr(sxp->xattr);
335 /* Send 0 (-1 + 1) to indicate that literal xattr data follows. */
336 write_varint(f, ndx + 1);
340 int count = sxp->xattr->count;
341 write_varint(f, count);
342 for (rxa = sxp->xattr->items; count--; rxa++) {
343 #ifdef HAVE_LINUX_XATTRS
344 write_varint(f, rxa->name_len);
345 write_varint(f, rxa->datum_len);
346 write_buf(f, rxa->name, rxa->name_len);
348 /* We strip the rsync prefix from disguised namespaces
349 * and put everything else in the user namespace. */
350 if (HAS_PREFIX(rxa->name, RSYNC_PREFIX)
351 && rxa->name[RPRE_LEN] != '%') {
352 write_varint(f, rxa->name_len - RPRE_LEN);
353 write_varint(f, rxa->datum_len);
354 write_buf(f, rxa->name + RPRE_LEN, rxa->name_len - RPRE_LEN);
356 write_varint(f, rxa->name_len + UPRE_LEN);
357 write_varint(f, rxa->datum_len);
358 write_buf(f, USER_PREFIX, UPRE_LEN);
359 write_buf(f, rxa->name, rxa->name_len);
362 if (rxa->datum_len > MAX_FULL_DATUM)
363 write_buf(f, rxa->datum + 1, MAX_DIGEST_LEN);
365 write_buf(f, rxa->datum, rxa->datum_len);
367 ndx = rsync_xal_l.count; /* pre-incremented count */
368 rsync_xal_store(sxp->xattr); /* adds item to rsync_xal_l */
374 /* Return a flag indicating if we need to change a file's xattrs. If
375 * "find_all" is specified, also mark any abbreviated xattrs that we
376 * need so that send_xattr_request() can tell the sender about them. */
377 int xattr_diff(struct file_struct *file, statx *sxp, int find_all)
379 item_list *lst = rsync_xal_l.items;
380 rsync_xa *snd_rxa, *rec_rxa;
381 int snd_cnt, rec_cnt;
382 int cmp, same, xattrs_equal = 1;
384 if (sxp && XATTR_READY(*sxp)) {
385 rec_rxa = sxp->xattr->items;
386 rec_cnt = sxp->xattr->count;
392 if (F_XATTR(file) >= 0)
393 lst += F_XATTR(file);
397 snd_rxa = lst->items;
398 snd_cnt = lst->count;
400 /* If the count of the sender's xattrs is different from our
401 * (receiver's) xattrs, the lists are not the same. */
402 if (snd_cnt != rec_cnt) {
409 cmp = rec_cnt ? strcmp(snd_rxa->name, rec_rxa->name) : -1;
412 else if (snd_rxa->datum_len > MAX_FULL_DATUM) {
413 same = cmp == 0 && snd_rxa->datum_len == rec_rxa->datum_len
414 && memcmp(snd_rxa->datum + 1, rec_rxa->datum + 1,
415 MAX_DIGEST_LEN) == 0;
416 /* Flag unrequested items that we need. */
417 if (!same && find_all && snd_rxa->datum[0] == XSTATE_ABBREV)
418 snd_rxa->datum[0] = XSTATE_TODO;
420 same = cmp == 0 && snd_rxa->datum_len == rec_rxa->datum_len
421 && memcmp(snd_rxa->datum, rec_rxa->datum,
422 snd_rxa->datum_len) == 0;
443 return !xattrs_equal;
446 /* When called by the generator with a NULL fname, this tells the sender
447 * which abbreviated xattr values we need. When called by the sender
448 * (with a non-NULL fname), we send all the extra xattr data it needs. */
449 void send_xattr_request(const char *fname, struct file_struct *file, int f_out)
451 item_list *lst = rsync_xal_l.items;
452 int j, cnt, prior_req = -1;
455 lst += F_XATTR(file);
457 for (rxa = lst->items, j = 0; j < cnt; rxa++, j++) {
458 if (rxa->datum_len <= MAX_FULL_DATUM)
460 switch (rxa->datum[0]) {
462 /* Items set locally will get cached by receiver. */
463 rxa->datum[0] = XSTATE_DONE;
471 /* Flag that we handled this abbreviated item. */
472 rxa->datum[0] = XSTATE_DONE;
474 write_varint(f_out, j - prior_req);
481 /* Re-read the long datum. */
482 if (!(ptr = get_xattr_data(fname, rxa->name, &len, 0)))
485 write_varint(f_out, len); /* length might have changed! */
486 write_buf(f_out, ptr, len);
491 write_byte(f_out, 0); /* end the list */
494 /* Any items set locally by the generator that the receiver doesn't
495 * get told about get changed back to XSTATE_ABBREV. */
496 void xattr_clear_locals(struct file_struct *file)
498 item_list *lst = rsync_xal_l.items;
502 if (F_XATTR(file) < 0)
505 lst += F_XATTR(file);
507 for (rxa = lst->items; cnt--; rxa++) {
508 if (rxa->datum_len <= MAX_FULL_DATUM)
510 if (rxa->datum[0] == XSTATE_LOCAL)
511 rxa->datum[0] = XSTATE_ABBREV;
515 /* When called by the sender, read the request from the generator and mark
516 * any needed xattrs with a flag that lets us know they need to be sent to
517 * the receiver. When called by the receiver, reads the sent data and
518 * stores it in place of its checksum. */
519 void recv_xattr_request(struct file_struct *file, int f_in)
521 item_list *lst = rsync_xal_l.items;
522 char *old_datum, *name;
526 if (F_XATTR(file) < 0) {
527 rprintf(FERROR, "recv_xattr_request: internal data error!\n");
528 exit_cleanup(RERR_STREAMIO);
530 lst += F_XATTR(file);
535 while ((rel_pos = read_varint(f_in)) != 0) {
538 if (cnt < 0 || rxa->datum_len <= MAX_FULL_DATUM
539 || rxa->datum[0] != XSTATE_ABBREV) {
540 rprintf(FERROR, "recv_xattr_request: internal abbrev error!\n");
541 exit_cleanup(RERR_STREAMIO);
545 rxa->datum[0] = XSTATE_TODO;
549 old_datum = rxa->datum;
550 rxa->datum_len = read_varint(f_in);
552 if (rxa->name_len + rxa->datum_len < rxa->name_len)
553 out_of_memory("recv_xattr_request"); /* overflow */
554 rxa->datum = new_array(char, rxa->datum_len + rxa->name_len);
556 out_of_memory("recv_xattr_request");
557 name = rxa->datum + rxa->datum_len;
558 memcpy(name, rxa->name, rxa->name_len);
561 read_buf(f_in, rxa->datum, rxa->datum_len);
565 /* ------------------------------------------------------------------------- */
567 /* receive and build the rsync_xattr_lists */
568 void receive_xattr(struct file_struct *file, int f)
570 static item_list temp_xattr = EMPTY_ITEM_LIST;
572 int ndx = read_varint(f);
574 if (ndx < 0 || (size_t)ndx > rsync_xal_l.count) {
575 rprintf(FERROR, "receive_xattr: xa index %d out of"
576 " range for %s\n", ndx, f_name(file, NULL));
577 exit_cleanup(RERR_STREAMIO);
581 F_XATTR(file) = ndx - 1;
585 if ((count = read_varint(f)) != 0) {
586 (void)EXPAND_ITEM_LIST(&temp_xattr, rsync_xa, count);
587 temp_xattr.count = 0;
593 size_t name_len = read_varint(f);
594 size_t datum_len = read_varint(f);
595 size_t dget_len = datum_len > MAX_FULL_DATUM ? 1 + MAX_DIGEST_LEN : datum_len;
596 size_t extra_len = MIGHT_NEED_RPRE ? RPRE_LEN : 0;
597 if (dget_len + extra_len < dget_len)
598 out_of_memory("receive_xattr"); /* overflow */
599 if (dget_len + extra_len + name_len < dget_len)
600 out_of_memory("receive_xattr"); /* overflow */
601 ptr = new_array(char, dget_len + extra_len + name_len);
603 out_of_memory("receive_xattr");
604 name = ptr + dget_len + extra_len;
605 read_buf(f, name, name_len);
606 if (dget_len == datum_len)
607 read_buf(f, ptr, dget_len);
609 *ptr = XSTATE_ABBREV;
610 read_buf(f, ptr + 1, MAX_DIGEST_LEN);
612 #ifdef HAVE_LINUX_XATTRS
613 /* Non-root can only save the user namespace. */
614 if (am_root <= 0 && !HAS_PREFIX(name, USER_PREFIX)) {
620 name_len += RPRE_LEN;
621 memcpy(name, RSYNC_PREFIX, RPRE_LEN);
624 /* This OS only has a user namespace, so we either
625 * strip the user prefix, or we put a non-user
626 * namespace inside our rsync hierarchy. */
627 if (HAS_PREFIX(name, USER_PREFIX)) {
629 name_len -= UPRE_LEN;
630 } else if (am_root) {
632 name_len += RPRE_LEN;
633 memcpy(name, RSYNC_PREFIX, RPRE_LEN);
639 if (am_root < 0 && name_len == XSTAT_LEN + 1
640 && name[RPRE_LEN] == '%' && strcmp(name, XSTAT_ATTR) == 0) {
644 rxa = EXPAND_ITEM_LIST(&temp_xattr, rsync_xa, 1);
647 rxa->name_len = name_len;
648 rxa->datum_len = datum_len;
651 ndx = rsync_xal_l.count; /* pre-incremented count */
652 rsync_xal_store(&temp_xattr); /* adds item to rsync_xal_l */
657 /* Turn the xattr data in statx into cached xattr data, setting the index
658 * values in the file struct. */
659 void cache_xattr(struct file_struct *file, statx *sxp)
666 ndx = find_matching_xattr(sxp->xattr);
668 rsync_xal_store(sxp->xattr); /* adds item to rsync_xal_l */
673 static int rsync_xal_set(const char *fname, item_list *xalp,
674 const char *fnamecmp, statx *sxp)
676 rsync_xa *rxas = xalp->items;
679 char *name, *ptr, sum[MAX_DIGEST_LEN];
680 int name_len, ret = 0;
682 /* This puts the current name list into the "namebuf" buffer. */
683 if ((list_len = get_xattr_names(fname)) < 0)
686 for (i = 0; i < xalp->count; i++) {
689 if (XATTR_ABBREV(rxas[i])) {
690 /* See if the fnamecmp version is identical. */
691 len = name_len = rxas[i].name_len;
692 if ((ptr = get_xattr_data(fnamecmp, name, &len, 1)) == NULL) {
696 rprintf(FERROR, "Missing abbreviated xattr value, %s, for %s\n",
697 rxas[i].name, full_fname(fname));
701 if (len != rxas[i].datum_len) {
706 sum_init(checksum_seed);
707 sum_update(ptr, len);
709 if (memcmp(sum, rxas[i].datum + 1, MAX_DIGEST_LEN) != 0) {
714 if (fname == fnamecmp)
715 ; /* Value is already set when identical */
716 else if (sys_lsetxattr(fname, name, ptr, len) < 0) {
717 rsyserr(FERROR, errno,
718 "rsync_xal_set: lsetxattr(\"%s\",\"%s\") failed",
721 } else /* make sure caller sets mtime */
722 sxp->st.st_mtime = (time_t)-1;
724 if (am_generator) { /* generator items stay abbreviated */
725 if (rxas[i].datum[0] == XSTATE_ABBREV)
726 rxas[i].datum[0] = XSTATE_LOCAL;
731 memcpy(ptr + len, name, name_len);
734 rxas[i].name = name = ptr + len;
739 if (sys_lsetxattr(fname, name, rxas[i].datum, rxas[i].datum_len) < 0) {
740 rsyserr(FERROR, errno,
741 "rsync_xal_set: lsetxattr(\"%s\",\"%s\") failed",
744 } else /* make sure caller sets mtime */
745 sxp->st.st_mtime = (time_t)-1;
748 /* Remove any extraneous names. */
749 for (name = namebuf; list_len > 0; name += name_len) {
750 name_len = strlen(name) + 1;
751 list_len -= name_len;
753 #ifdef HAVE_LINUX_XATTRS
754 /* We always ignore the system namespace, and non-root
755 * ignores everything but the user namespace. */
756 if (am_root ? HAS_PREFIX(name, SYSTEM_PREFIX)
757 : !HAS_PREFIX(name, USER_PREFIX))
761 for (i = 0; i < xalp->count; i++) {
762 if (strcmp(name, rxas[i].name) == 0)
765 if (i == xalp->count) {
766 if (sys_lremovexattr(fname, name) < 0) {
767 rsyserr(FERROR, errno,
768 "rsync_xal_clear: lremovexattr(\"%s\",\"%s\") failed",
771 } else /* make sure caller sets mtime */
772 sxp->st.st_mtime = (time_t)-1;
779 /* Set extended attributes on indicated filename. */
780 int set_xattr(const char *fname, const struct file_struct *file,
781 const char *fnamecmp, statx *sxp)
784 item_list *lst = rsync_xal_l.items;
787 return 1; /* FIXME: --dry-run needs to compute this value */
789 if (read_only || list_only) {
795 return rsync_xal_set(fname, lst + ndx, fnamecmp, sxp);
798 int get_stat_xattr(const char *fname, int fd, STRUCT_STAT *fst, STRUCT_STAT *xst)
800 int mode, rdev_major, rdev_minor, uid, gid, len;
803 if (am_root >= 0 || IS_DEVICE(fst->st_mode) || IS_SPECIAL(fst->st_mode))
812 len = sys_lgetxattr(fname, XSTAT_ATTR, buf, sizeof buf - 1);
815 len = sys_fgetxattr(fd, XSTAT_ATTR, buf, sizeof buf - 1);
817 if (len >= (int)sizeof buf) {
822 if (errno == ENOTSUP || errno == ENOATTR)
824 if (errno == EPERM && S_ISLNK(fst->st_mode)) {
829 rsyserr(FERROR, errno, "failed to read xattr %s for %s",
830 XSTAT_ATTR, full_fname(fname));
835 if (sscanf(buf, "%o %d,%d %d:%d",
836 &mode, &rdev_major, &rdev_minor, &uid, &gid) != 5) {
837 rprintf(FERROR, "Corrupt %s xattr attached to %s: \"%s\"\n",
838 XSTAT_ATTR, full_fname(fname), buf);
839 exit_cleanup(RERR_FILEIO);
842 xst->st_mode = from_wire_mode(mode);
843 xst->st_rdev = MAKEDEV(rdev_major, rdev_minor);
850 int set_stat_xattr(const char *fname, struct file_struct *file)
852 STRUCT_STAT fst, xst;
859 if (read_only || list_only) {
860 rsyserr(FERROR, EROFS, "failed to write xattr %s for %s",
861 XSTAT_ATTR, full_fname(fname));
865 if (x_lstat(fname, &fst, &xst) < 0) {
866 rsyserr(FERROR, errno, "failed to re-stat %s",
871 fst.st_mode &= (_S_IFMT | CHMOD_BITS);
872 fmode = file->mode & (_S_IFMT | CHMOD_BITS);
874 if (IS_DEVICE(fmode) || IS_SPECIAL(fmode)) {
875 uint32 *devp = F_RDEV_P(file);
876 rdev = MAKEDEV(DEV_MAJOR(devp), DEV_MINOR(devp));
880 /* Dump the special permissions and enable full owner access. */
881 mode = (fst.st_mode & _S_IFMT) | (fmode & ACCESSPERMS)
882 | (S_ISDIR(fst.st_mode) ? 0700 : 0600);
883 if (fst.st_mode != mode)
884 do_chmod(fname, mode);
885 if (!IS_DEVICE(fst.st_mode) && !IS_SPECIAL(fst.st_mode))
886 fst.st_rdev = 0; /* just in case */
888 if (mode == fmode && fst.st_rdev == rdev
889 && fst.st_uid == F_UID(file) && fst.st_gid == F_GID(file)) {
890 /* xst.st_mode will be 0 if there's no current stat xattr */
891 if (xst.st_mode && sys_lremovexattr(fname, XSTAT_ATTR) < 0) {
892 rsyserr(FERROR, errno,
893 "delete of stat xattr failed for %s",
900 if (xst.st_mode != fmode || xst.st_rdev != rdev
901 || xst.st_uid != F_UID(file) || xst.st_gid != F_GID(file)) {
903 int len = snprintf(buf, sizeof buf, "%o %u,%u %u:%u",
905 (int)major(rdev), (int)minor(rdev),
906 (int)F_UID(file), (int)F_GID(file));
907 if (sys_lsetxattr(fname, XSTAT_ATTR, buf, len) < 0) {
908 if (errno == EPERM && S_ISLNK(fst.st_mode))
910 rsyserr(FERROR, errno,
911 "failed to write xattr %s for %s",
912 XSTAT_ATTR, full_fname(fname));
920 int x_stat(const char *fname, STRUCT_STAT *fst, STRUCT_STAT *xst)
922 int ret = do_stat(fname, fst);
923 if ((ret < 0 || get_stat_xattr(fname, -1, fst, xst) < 0) && xst)
928 int x_lstat(const char *fname, STRUCT_STAT *fst, STRUCT_STAT *xst)
930 int ret = do_lstat(fname, fst);
931 if ((ret < 0 || get_stat_xattr(fname, -1, fst, xst) < 0) && xst)
936 int x_fstat(int fd, STRUCT_STAT *fst, STRUCT_STAT *xst)
938 int ret = do_fstat(fd, fst);
939 if ((ret < 0 || get_stat_xattr(NULL, fd, fst, xst) < 0) && xst)
944 #endif /* SUPPORT_XATTRS */