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
58 #define RSYNC_PREFIX "rsync."
59 #define RPRE_LEN ((int)sizeof RSYNC_PREFIX - 1)
64 size_t datum_len, name_len;
67 static size_t namebuf_len = 0;
68 static char *namebuf = NULL;
70 static item_list empty_xattr = EMPTY_ITEM_LIST;
71 static item_list rsync_xal_l = EMPTY_ITEM_LIST;
73 /* ------------------------------------------------------------------------- */
75 static void rsync_xal_free(item_list *xalp)
78 rsync_xa *rxas = xalp->items;
80 for (i = 0; i < xalp->count; i++) {
82 /*free(rxas[i].name);*/
87 void free_xattr(statx *sxp)
91 rsync_xal_free(sxp->xattr);
96 static int rsync_xal_compare_names(const void *x1, const void *x2)
98 const rsync_xa *xa1 = x1;
99 const rsync_xa *xa2 = x2;
100 return strcmp(xa1->name, xa2->name);
103 static ssize_t get_xattr_names(const char *fname)
109 namebuf = new_array(char, namebuf_len);
111 out_of_memory("get_xattr_names");
114 /* The length returned includes all the '\0' terminators. */
115 list_len = sys_llistxattr(fname, namebuf, namebuf_len);
116 if (list_len > (ssize_t)namebuf_len) {
122 if (errno == ENOTSUP)
124 if (errno == ERANGE) {
125 list_len = sys_llistxattr(fname, NULL, 0);
127 rsyserr(FERROR, errno,
128 "get_xattr_names: llistxattr(\"%s\",0) failed",
134 namebuf_len = list_len + 1024;
135 namebuf = new_array(char, namebuf_len);
137 out_of_memory("get_xattr_names");
138 list_len = sys_llistxattr(fname, namebuf, namebuf_len);
143 rsyserr(FERROR, errno,
144 "get_xattr_names: llistxattr(\"%s\",%ld) failed",
145 fname, (long)namebuf_len);
149 /* On entry, the *len_ptr parameter contains the size of the extra space we
150 * should allocate when we create a buffer for the data. On exit, it contains
151 * the length of the datum. */
152 static char *get_xattr_data(const char *fname, const char *name, size_t *len_ptr,
153 int no_missing_error)
155 size_t datum_len = sys_lgetxattr(fname, name, NULL, 0);
158 if (datum_len == (size_t)-1) {
159 if (errno == ENOTSUP || no_missing_error)
161 rsyserr(FERROR, errno,
162 "get_xattr_data: lgetxattr(\"%s\",\"%s\",0) failed",
167 if (datum_len + *len_ptr < datum_len /* checks for overflow */
168 || !(ptr = new_array(char, datum_len + *len_ptr)))
169 out_of_memory("get_xattr_data");
171 *len_ptr = datum_len;
174 size_t len = sys_lgetxattr(fname, name, ptr, datum_len);
175 if (len != datum_len) {
176 if (len == (size_t)-1) {
177 rsyserr(FERROR, errno,
178 "get_xattr_data: lgetxattr(\"%s\",\"%s\",%ld)"
179 " failed", fname, name, (long)datum_len);
182 "get_xattr_data: lgetxattr(\"%s\",\"%s\",%ld)"
183 " returned %ld\n", fname, name,
184 (long)datum_len, (long)len);
194 static int rsync_xal_get(const char *fname, item_list *xalp)
196 ssize_t list_len, name_len;
197 size_t datum_len, name_offset;
199 #ifdef HAVE_LINUX_XATTRS
200 int user_only = am_sender ? 0 : !am_root;
203 /* This puts the name list into the "namebuf" buffer. */
204 if ((list_len = get_xattr_names(fname)) < 0)
207 for (name = namebuf; list_len > 0; name += name_len) {
210 name_len = strlen(name) + 1;
211 list_len -= name_len;
213 #ifdef HAVE_LINUX_XATTRS
214 /* We always ignore the system namespace, and non-root
215 * ignores everything but the user namespace. */
216 if (user_only ? !HAS_PREFIX(name, USER_PREFIX)
217 : HAS_PREFIX(name, SYSTEM_PREFIX))
221 datum_len = name_len; /* Pass extra size to get_xattr_data() */
222 if (!(ptr = get_xattr_data(fname, name, &datum_len, 0)))
225 if (datum_len > MAX_FULL_DATUM) {
226 /* For large datums, we store a flag and a checksum. */
227 name_offset = 1 + MAX_DIGEST_LEN;
228 sum_init(checksum_seed);
229 sum_update(ptr, datum_len);
232 if (!(ptr = new_array(char, name_offset + name_len)))
233 out_of_memory("rsync_xal_get");
234 *ptr = XSTATE_ABBREV;
237 name_offset = datum_len;
239 rxas = EXPAND_ITEM_LIST(xalp, rsync_xa, RSYNC_XAL_INITIAL);
240 rxas->name = ptr + name_offset;
241 memcpy(rxas->name, name, name_len);
243 rxas->name_len = name_len;
244 rxas->datum_len = datum_len;
247 qsort(xalp->items, xalp->count, sizeof (rsync_xa), rsync_xal_compare_names);
251 /* Read the xattr(s) for this filename. */
252 int get_xattr(const char *fname, statx *sxp)
254 sxp->xattr = new(item_list);
255 *sxp->xattr = empty_xattr;
256 if (rsync_xal_get(fname, sxp->xattr) < 0) {
263 static int find_matching_xattr(item_list *xalp)
266 item_list *lst = rsync_xal_l.items;
268 for (i = 0; i < rsync_xal_l.count; i++) {
269 rsync_xa *rxas1 = lst[i].items;
270 rsync_xa *rxas2 = xalp->items;
272 /* Wrong number of elements? */
273 if (lst[i].count != xalp->count)
275 /* any elements different? */
276 for (j = 0; j < xalp->count; j++) {
277 if (rxas1[j].name_len != rxas2[j].name_len
278 || rxas1[j].datum_len != rxas2[j].datum_len
279 || strcmp(rxas1[j].name, rxas2[j].name))
281 if (rxas1[j].datum_len > MAX_FULL_DATUM) {
282 if (memcmp(rxas1[j].datum + 1,
284 MAX_DIGEST_LEN) != 0)
287 if (memcmp(rxas1[j].datum, rxas2[j].datum,
292 /* no differences found. This is The One! */
293 if (j == xalp->count)
300 /* Store *xalp on the end of rsync_xal_l */
301 static void rsync_xal_store(item_list *xalp)
303 item_list *new_lst = EXPAND_ITEM_LIST(&rsync_xal_l, item_list, RSYNC_XAL_LIST_INITIAL);
304 /* Since the following call starts a new list, we know it will hold the
305 * entire initial-count, not just enough space for one new item. */
306 *new_lst = empty_xattr;
307 (void)EXPAND_ITEM_LIST(new_lst, rsync_xa, xalp->count);
308 memcpy(new_lst->items, xalp->items, xalp->count * sizeof (rsync_xa));
309 new_lst->count = xalp->count;
313 /* Send the make_xattr()-generated xattr list for this flist entry. */
314 int send_xattr(statx *sxp, int f)
316 int ndx = find_matching_xattr(sxp->xattr);
318 /* Send 0 (-1 + 1) to indicate that literal xattr data follows. */
319 write_varint(f, ndx + 1);
323 int count = sxp->xattr->count;
324 write_varint(f, count);
325 for (rxa = sxp->xattr->items; count--; rxa++) {
326 #ifdef HAVE_LINUX_XATTRS
327 write_varint(f, rxa->name_len);
328 write_varint(f, rxa->datum_len);
329 write_buf(f, rxa->name, rxa->name_len);
331 /* We strip the rsync prefix from disguised namespaces
332 * and put everything else in the user namespace. */
333 if (HAS_PREFIX(rxa->name, RSYNC_PREFIX)
334 && rxa->name[RPRE_LEN] != '%') {
335 write_varint(f, rxa->name_len - RPRE_LEN);
336 write_varint(f, rxa->datum_len);
337 write_buf(f, rxa->name + RPRE_LEN, rxa->name_len - RPRE_LEN);
339 write_varint(f, rxa->name_len + UPRE_LEN);
340 write_varint(f, rxa->datum_len);
341 write_buf(f, USER_PREFIX, UPRE_LEN);
342 write_buf(f, rxa->name, rxa->name_len);
345 if (rxa->datum_len > MAX_FULL_DATUM)
346 write_buf(f, rxa->datum + 1, MAX_DIGEST_LEN);
348 write_buf(f, rxa->datum, rxa->datum_len);
350 ndx = rsync_xal_l.count; /* pre-incremented count */
351 rsync_xal_store(sxp->xattr); /* adds item to rsync_xal_l */
357 /* Return a flag indicating if we need to change a file's xattrs. If
358 * "find_all" is specified, also mark any abbreviated xattrs that we
359 * need so that send_xattr_request() can tell the sender about them. */
360 int xattr_diff(struct file_struct *file, statx *sxp, int find_all)
362 item_list *lst = rsync_xal_l.items;
363 rsync_xa *snd_rxa, *rec_rxa;
364 int snd_cnt, rec_cnt;
365 int cmp, same, xattrs_equal = 1;
367 if (sxp && XATTR_READY(*sxp)) {
368 rec_rxa = sxp->xattr->items;
369 rec_cnt = sxp->xattr->count;
375 if (F_XATTR(file) >= 0)
376 lst += F_XATTR(file);
380 snd_rxa = lst->items;
381 snd_cnt = lst->count;
383 /* If the count of the sender's xattrs is different from our
384 * (receiver's) xattrs, the lists are not the same. */
385 if (snd_cnt != rec_cnt) {
392 cmp = rec_cnt ? strcmp(snd_rxa->name, rec_rxa->name) : -1;
395 else if (snd_rxa->datum_len > MAX_FULL_DATUM) {
396 same = cmp == 0 && snd_rxa->datum_len == rec_rxa->datum_len
397 && memcmp(snd_rxa->datum + 1, rec_rxa->datum + 1,
398 MAX_DIGEST_LEN) == 0;
399 /* Flag unrequested items that we need. */
400 if (!same && find_all && snd_rxa->datum[0] == XSTATE_ABBREV)
401 snd_rxa->datum[0] = XSTATE_TODO;
403 same = cmp == 0 && snd_rxa->datum_len == rec_rxa->datum_len
404 && memcmp(snd_rxa->datum, rec_rxa->datum,
405 snd_rxa->datum_len) == 0;
426 return !xattrs_equal;
429 /* When called by the generator with a NULL fname, this tells the sender
430 * which abbreviated xattr values we need. When called by the sender
431 * (with a non-NULL fname), we send all the extra xattr data it needs. */
432 void send_xattr_request(const char *fname, struct file_struct *file, int f_out)
434 item_list *lst = rsync_xal_l.items;
435 int j, cnt, prior_req = -1;
438 lst += F_XATTR(file);
440 for (rxa = lst->items, j = 0; j < cnt; rxa++, j++) {
441 if (rxa->datum_len <= MAX_FULL_DATUM)
443 switch (rxa->datum[0]) {
445 /* Items set locally will get cached by receiver. */
446 rxa->datum[0] = XSTATE_DONE;
454 /* Flag that we handled this abbreviated item. */
455 rxa->datum[0] = XSTATE_DONE;
457 write_varint(f_out, j - prior_req);
464 /* Re-read the long datum. */
465 if (!(ptr = get_xattr_data(fname, rxa->name, &len, 0)))
468 write_varint(f_out, len); /* length might have changed! */
469 write_buf(f_out, ptr, len);
474 write_byte(f_out, 0); /* end the list */
477 /* Any items set locally by the generator that the receiver doesn't
478 * get told about get changed back to XSTATE_ABBREV. */
479 void xattr_clear_locals(struct file_struct *file)
481 item_list *lst = rsync_xal_l.items;
485 if (F_XATTR(file) < 0)
488 lst += F_XATTR(file);
490 for (rxa = lst->items; cnt--; rxa++) {
491 if (rxa->datum_len <= MAX_FULL_DATUM)
493 if (rxa->datum[0] == XSTATE_LOCAL)
494 rxa->datum[0] = XSTATE_ABBREV;
498 /* When called by the sender, read the request from the generator and mark
499 * any needed xattrs with a flag that lets us know they need to be sent to
500 * the receiver. When called by the receiver, reads the sent data and
501 * stores it in place of its checksum. */
502 void recv_xattr_request(struct file_struct *file, int f_in)
504 item_list *lst = rsync_xal_l.items;
505 char *old_datum, *name;
509 if (F_XATTR(file) < 0) {
510 rprintf(FERROR, "recv_xattr_request: internal data error!\n");
511 exit_cleanup(RERR_STREAMIO);
513 lst += F_XATTR(file);
518 while ((rel_pos = read_varint(f_in)) != 0) {
521 if (cnt < 0 || rxa->datum_len <= MAX_FULL_DATUM
522 || rxa->datum[0] != XSTATE_ABBREV) {
523 rprintf(FERROR, "recv_xattr_request: internal abbrev error!\n");
524 exit_cleanup(RERR_STREAMIO);
528 rxa->datum[0] = XSTATE_TODO;
532 old_datum = rxa->datum;
533 rxa->datum_len = read_varint(f_in);
535 if (rxa->name_len + rxa->datum_len < rxa->name_len)
536 out_of_memory("recv_xattr_request"); /* overflow */
537 rxa->datum = new_array(char, rxa->datum_len + rxa->name_len);
539 out_of_memory("recv_xattr_request");
540 name = rxa->datum + rxa->datum_len;
541 memcpy(name, rxa->name, rxa->name_len);
544 read_buf(f_in, rxa->datum, rxa->datum_len);
548 /* ------------------------------------------------------------------------- */
550 /* receive and build the rsync_xattr_lists */
551 void receive_xattr(struct file_struct *file, int f)
553 static item_list temp_xattr = EMPTY_ITEM_LIST;
555 int ndx = read_varint(f);
557 if (ndx < 0 || (size_t)ndx > rsync_xal_l.count) {
558 rprintf(FERROR, "receive_xattr: xa index %d out of"
559 " range for %s\n", ndx, f_name(file, NULL));
560 exit_cleanup(RERR_STREAMIO);
564 F_XATTR(file) = ndx - 1;
568 if ((count = read_varint(f)) != 0) {
569 (void)EXPAND_ITEM_LIST(&temp_xattr, rsync_xa, count);
570 temp_xattr.count = 0;
576 size_t name_len = read_varint(f);
577 size_t datum_len = read_varint(f);
578 size_t dget_len = datum_len > MAX_FULL_DATUM ? 1 + MAX_DIGEST_LEN : datum_len;
579 #ifdef HAVE_LINUX_XATTRS
580 size_t extra_len = 0;
582 size_t extra_len = am_root ? RPRE_LEN : 0;
583 if (dget_len + extra_len < dget_len)
584 out_of_memory("receive_xattr"); /* overflow */
586 if (dget_len + extra_len + name_len < dget_len)
587 out_of_memory("receive_xattr"); /* overflow */
588 ptr = new_array(char, dget_len + extra_len + name_len);
590 out_of_memory("receive_xattr");
591 name = ptr + dget_len + extra_len;
592 read_buf(f, name, name_len);
593 if (dget_len == datum_len)
594 read_buf(f, ptr, dget_len);
596 *ptr = XSTATE_ABBREV;
597 read_buf(f, ptr + 1, MAX_DIGEST_LEN);
599 #ifdef HAVE_LINUX_XATTRS
600 /* Non-root can only save the user namespace. */
601 if (!am_root && !HAS_PREFIX(name, USER_PREFIX)) {
606 /* This OS only has a user namespace, so we either
607 * strip the user prefix, or we put a non-user
608 * namespace inside our rsync hierarchy. */
609 if (HAS_PREFIX(name, USER_PREFIX)) {
611 name_len -= UPRE_LEN;
612 } else if (am_root) {
614 name_len += RPRE_LEN;
615 memcpy(name, RSYNC_PREFIX, RPRE_LEN);
621 rxa = EXPAND_ITEM_LIST(&temp_xattr, rsync_xa, 1);
624 rxa->name_len = name_len;
625 rxa->datum_len = datum_len;
628 ndx = rsync_xal_l.count; /* pre-incremented count */
629 rsync_xal_store(&temp_xattr); /* adds item to rsync_xal_l */
634 /* Turn the xattr data in statx into cached xattr data, setting the index
635 * values in the file struct. */
636 void cache_xattr(struct file_struct *file, statx *sxp)
643 ndx = find_matching_xattr(sxp->xattr);
645 rsync_xal_store(sxp->xattr); /* adds item to rsync_xal_l */
650 static int rsync_xal_set(const char *fname, item_list *xalp,
651 const char *fnamecmp, statx *sxp)
653 rsync_xa *rxas = xalp->items;
656 char *name, *ptr, sum[MAX_DIGEST_LEN];
657 int name_len, ret = 0;
659 /* This puts the current name list into the "namebuf" buffer. */
660 if ((list_len = get_xattr_names(fname)) < 0)
663 for (i = 0; i < xalp->count; i++) {
666 if (XATTR_ABBREV(rxas[i])) {
667 /* See if the fnamecmp version is identical. */
668 len = name_len = rxas[i].name_len;
669 if ((ptr = get_xattr_data(fnamecmp, name, &len, 1)) == NULL) {
673 rprintf(FERROR, "Missing abbreviated xattr value, %s, for %s\n",
674 rxas[i].name, full_fname(fname));
678 if (len != rxas[i].datum_len) {
683 sum_init(checksum_seed);
684 sum_update(ptr, len);
686 if (memcmp(sum, rxas[i].datum + 1, MAX_DIGEST_LEN) != 0) {
691 if (fname == fnamecmp)
692 ; /* Value is already set when identical */
693 else if (sys_lsetxattr(fname, name, ptr, len) < 0) {
694 rsyserr(FERROR, errno,
695 "rsync_xal_set: lsetxattr(\"%s\",\"%s\") failed",
698 } else /* make sure caller sets mtime */
699 sxp->st.st_mtime = (time_t)-1;
701 if (am_generator) { /* generator items stay abbreviated */
702 if (rxas[i].datum[0] == XSTATE_ABBREV)
703 rxas[i].datum[0] = XSTATE_LOCAL;
708 memcpy(ptr + len, name, name_len);
711 rxas[i].name = name = ptr + len;
716 if (sys_lsetxattr(fname, name, rxas[i].datum, rxas[i].datum_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;
725 /* Remove any extraneous names. */
726 for (name = namebuf; list_len > 0; name += name_len) {
727 name_len = strlen(name) + 1;
728 list_len -= name_len;
730 #ifdef HAVE_LINUX_XATTRS
731 /* We always ignore the system namespace, and non-root
732 * ignores everything but the user namespace. */
733 if (am_root ? HAS_PREFIX(name, SYSTEM_PREFIX)
734 : !HAS_PREFIX(name, USER_PREFIX))
738 for (i = 0; i < xalp->count; i++) {
739 if (strcmp(name, rxas[i].name) == 0)
742 if (i == xalp->count) {
743 if (sys_lremovexattr(fname, name) < 0) {
744 rsyserr(FERROR, errno,
745 "rsync_xal_clear: lremovexattr(\"%s\",\"%s\") failed",
748 } else /* make sure caller sets mtime */
749 sxp->st.st_mtime = (time_t)-1;
756 /* Set extended attributes on indicated filename. */
757 int set_xattr(const char *fname, const struct file_struct *file,
758 const char *fnamecmp, statx *sxp)
761 item_list *lst = rsync_xal_l.items;
764 return 1; /* FIXME: --dry-run needs to compute this value */
766 if (read_only || list_only) {
772 return rsync_xal_set(fname, lst + ndx, fnamecmp, sxp);
775 #endif /* SUPPORT_XATTRS */