Commit | Line | Data |
---|---|---|
03019e41 WD |
1 | This patch adds basic support for extended attributes. It works, but there |
2 | are some things that still needs to be improved (see TODO list below). | |
bbdff76a | 3 | |
03019e41 | 4 | To use this patch, run these commands for a successful build: |
bbdff76a | 5 | |
03019e41 WD |
6 | patch -p1 <patches/acls.diff |
7 | patch -p1 <patches/xattrs.diff | |
27e96866 | 8 | ./prepare-source |
f787c90c | 9 | ./configure --enable-acl-support --enable-xattr-support |
bbdff76a WD |
10 | make |
11 | ||
03019e41 WD |
12 | Alternately, if you don't want ACL support, configure it this way: |
13 | ||
14 | ./configure --enable-xattr-support | |
15 | ||
0f6fb3c8 WD |
16 | TODO: |
17 | ||
d2dc8a18 WD |
18 | - This patch needs to more efficiently handle large xattrs, especially when |
19 | they're unchanged. | |
0f6fb3c8 | 20 | |
a74926e4 WD |
21 | - Extraneous xattr values need to be removed from files that are not being |
22 | recreated. | |
23 | ||
0f6fb3c8 WD |
24 | - We need to affect the itemized output to know when xattrs are being updated. |
25 | ||
26 | - We need to affect the --link-dest option to avoid hard-linking two files | |
27 | that differ in their xattrs (when --xattrs was specified). | |
28 | ||
9a7eef96 WD |
29 | --- old/Makefile.in |
30 | +++ new/Makefile.in | |
1ed0b5c9 | 31 | @@ -28,13 +28,13 @@ VERSION=@VERSION@ |
bbdff76a WD |
32 | |
33 | HEADERS=byteorder.h config.h errcode.h proto.h rsync.h smb_acls.h lib/pool_alloc.h | |
34 | LIBOBJ=lib/wildmatch.o lib/compat.o lib/snprintf.o lib/mdfour.o \ | |
35 | - lib/permstring.o lib/pool_alloc.o lib/sysacls.o @LIBOBJS@ | |
36 | + lib/permstring.o lib/pool_alloc.o lib/sysacls.o lib/sysxattr.o @LIBOBJS@ | |
4bf6f8c7 WD |
37 | ZLIBOBJ=zlib/deflate.o zlib/inffast.o zlib/inflate.o zlib/inftrees.o \ |
38 | zlib/trees.o zlib/zutil.o zlib/adler32.o zlib/compress.o zlib/crc32.o | |
bbdff76a WD |
39 | OBJS1=rsync.o generator.o receiver.o cleanup.o sender.o exclude.o util.o \ |
40 | main.o checksum.o match.o syscall.o log.o backup.o | |
41 | OBJS2=options.o flist.o io.o compat.o hlink.o token.o uidlist.o socket.o \ | |
610969d1 WD |
42 | - fileio.o batch.o clientname.o chmod.o acls.o |
43 | + fileio.o batch.o clientname.o chmod.o acls.o xattr.o | |
bbdff76a WD |
44 | OBJS3=progress.o pipe.o |
45 | DAEMON_OBJ = params.o loadparm.o clientserver.o access.o connection.o authenticate.o | |
46 | popt_OBJS=popt/findme.o popt/popt.o popt/poptconfig.o \ | |
0f6fb3c8 WD |
47 | --- old/acls.c |
48 | +++ new/acls.c | |
03019e41 | 49 | @@ -30,6 +30,7 @@ extern int read_only; |
8889f85e | 50 | extern int list_only; |
0f6fb3c8 WD |
51 | extern int orig_umask; |
52 | extern int preserve_acls; | |
53 | +extern int preserve_xattrs; | |
54 | extern unsigned int file_struct_len; | |
55 | ||
56 | /* === ACL structures === */ | |
03019e41 | 57 | @@ -741,6 +742,10 @@ void receive_acl(struct file_struct *fil |
0f6fb3c8 WD |
58 | type = SMB_ACL_TYPE_ACCESS; |
59 | racl_list = &access_acl_list; | |
60 | ndx_ptr = (char*)file + file_struct_len; | |
61 | +#ifdef SUPPORT_XATTRS | |
62 | + if (preserve_xattrs) | |
63 | + ndx_ptr += 4; | |
64 | +#endif | |
65 | do { | |
66 | char tag = read_byte(f); | |
67 | int ndx; | |
03019e41 | 68 | @@ -800,6 +805,10 @@ void cache_acl(struct file_struct *file, |
0f6fb3c8 WD |
69 | racl = sxp->acc_acl; |
70 | racl_list = &access_acl_list; | |
71 | ndx_ptr = (char*)file + file_struct_len; | |
72 | +#ifdef SUPPORT_XATTRS | |
73 | + if (preserve_xattrs) | |
74 | + ndx_ptr += 4; | |
75 | +#endif | |
76 | do { | |
77 | if (!racl) | |
78 | ndx = -1; | |
03019e41 | 79 | @@ -920,6 +929,10 @@ int set_acl(const char *fname, const str |
0f6fb3c8 WD |
80 | |
81 | type = SMB_ACL_TYPE_ACCESS; | |
82 | ndx_ptr = (char*)file + file_struct_len; | |
83 | +#ifdef SUPPORT_XATTRS | |
84 | + if (preserve_xattrs) | |
85 | + ndx_ptr += 4; | |
86 | +#endif | |
87 | do { | |
88 | acl_duo *duo_item; | |
89 | BOOL eq; | |
9a7eef96 WD |
90 | --- old/backup.c |
91 | +++ new/backup.c | |
dc52bc1a | 92 | @@ -30,6 +30,7 @@ extern char *backup_dir; |
93d6ca6d WD |
93 | |
94 | extern int am_root; | |
95 | extern int preserve_acls; | |
96 | +extern int preserve_xattrs; | |
97 | extern int preserve_devices; | |
98 | extern int preserve_specials; | |
99 | extern int preserve_links; | |
0f6fb3c8 WD |
100 | @@ -136,6 +137,9 @@ static int make_bak_dir(char *fullpath) |
101 | #ifdef SUPPORT_ACLS | |
102 | sx.acc_acl = sx.def_acl = NULL; | |
103 | #endif | |
104 | +#ifdef SUPPORT_XATTRS | |
105 | + sx.xattr = NULL; | |
106 | +#endif | |
107 | if (!(file = make_file(rel, NULL, NULL, 0, NO_FILTERS))) | |
108 | continue; | |
109 | #ifdef SUPPORT_ACLS | |
110 | @@ -144,6 +148,12 @@ static int make_bak_dir(char *fullpath) | |
111 | cache_acl(file, &sx); | |
112 | } | |
09cc6650 WD |
113 | #endif |
114 | +#ifdef SUPPORT_XATTRS | |
0f6fb3c8 WD |
115 | + if (preserve_xattrs) { |
116 | + get_xattr(rel, &sx); | |
117 | + cache_xattr(file, &sx); | |
118 | + } | |
09cc6650 | 119 | +#endif |
0f6fb3c8 WD |
120 | set_file_attrs(fullpath, file, NULL, 0); |
121 | free(file); | |
bbdff76a | 122 | } |
0f6fb3c8 WD |
123 | @@ -195,6 +205,9 @@ static int keep_backup(char *fname) |
124 | #ifdef SUPPORT_ACLS | |
125 | sx.acc_acl = sx.def_acl = NULL; | |
09cc6650 WD |
126 | #endif |
127 | +#ifdef SUPPORT_XATTRS | |
0f6fb3c8 | 128 | + sx.xattr = NULL; |
09cc6650 | 129 | +#endif |
bbdff76a | 130 | |
0f6fb3c8 WD |
131 | if (!(file = make_file(fname, NULL, NULL, 0, NO_FILTERS))) |
132 | return 1; /* the file could have disappeared */ | |
133 | @@ -208,6 +221,12 @@ static int keep_backup(char *fname) | |
134 | cache_acl(file, &sx); | |
135 | } | |
09cc6650 WD |
136 | #endif |
137 | +#ifdef SUPPORT_XATTRS | |
0f6fb3c8 WD |
138 | + if (preserve_xattrs) { |
139 | + get_xattr(fname, &sx); | |
140 | + cache_xattr(file, &sx); | |
141 | + } | |
09cc6650 | 142 | +#endif |
bbdff76a | 143 | |
0f6fb3c8 WD |
144 | /* Check to see if this is a device file, or link */ |
145 | if ((am_root && preserve_devices && IS_DEVICE(file->mode)) | |
9a7eef96 WD |
146 | --- old/configure.in |
147 | +++ new/configure.in | |
742a4103 | 148 | @@ -856,6 +856,40 @@ samba_cv_HAVE_ACL_GET_PERM_NP=yes,samba_ |
bbdff76a WD |
149 | AC_MSG_RESULT(no) |
150 | ) | |
151 | ||
152 | +AC_CHECK_HEADERS(attr/xattr.h) | |
49de5440 | 153 | +AC_CHECK_HEADERS(sys/xattr.h) |
742a4103 | 154 | +AC_CHECK_HEADERS(sys/extattr.h) |
bbdff76a | 155 | +AC_MSG_CHECKING(whether to support extended attributes) |
f787c90c WD |
156 | +AC_ARG_ENABLE(xattr-support, |
157 | +AC_HELP_STRING([--enable-xattr-support], [Include extended attribute support (default=no)]), | |
3b05e91f | 158 | +[ case "$enableval" in |
bbdff76a WD |
159 | + yes) |
160 | + case "$host_os" in | |
161 | + *linux*) | |
162 | + AC_MSG_RESULT(Using Linux xattrs) | |
163 | + AC_DEFINE(HAVE_LINUX_XATTRS, 1, [True if you have Linux xattrs]) | |
164 | + ;; | |
49de5440 WD |
165 | + darwin*) |
166 | + AC_MSG_RESULT(Using OS X xattrs) | |
167 | + AC_DEFINE(HAVE_OSX_XATTRS, 1, [True if you have Mac OS X xattrs]) | |
168 | + ;; | |
742a4103 WD |
169 | + freebsd*) |
170 | + AC_MSG_RESULT(Using FreeBSD extattrs) | |
171 | + AC_DEFINE(HAVE_FREEBSD_XATTRS, 1, [True if you have FreeBSD xattrs]) | |
172 | + ;; | |
bbdff76a | 173 | + *) |
49de5440 | 174 | + AC_MSG_RESULT(Xattrs requested but not Linux or OS X. Good luck...) |
bbdff76a WD |
175 | + ;; |
176 | + esac | |
177 | + ;; | |
178 | + *) | |
179 | + AC_MSG_RESULT(no) | |
742378cf | 180 | + AC_DEFINE(HAVE_NO_XATTRS, 1, [True if you don't have extended attributes]) |
bbdff76a WD |
181 | + esac ], |
182 | + AC_MSG_RESULT(no) | |
56473cb9 | 183 | + AC_DEFINE(HAVE_NO_XATTRS, 1, [True if you don't have extended attributes]) |
bbdff76a WD |
184 | +) |
185 | + | |
186 | AC_CONFIG_FILES([Makefile lib/dummy zlib/dummy popt/dummy shconfig]) | |
187 | AC_OUTPUT | |
188 | ||
9a7eef96 WD |
189 | --- old/flist.c |
190 | +++ new/flist.c | |
dc52bc1a | 191 | @@ -41,6 +41,7 @@ extern int one_file_system; |
93d6ca6d WD |
192 | extern int copy_dirlinks; |
193 | extern int keep_dirlinks; | |
194 | extern int preserve_acls; | |
195 | +extern int preserve_xattrs; | |
196 | extern int preserve_links; | |
197 | extern int preserve_hard_links; | |
198 | extern int preserve_devices; | |
de565f59 | 199 | @@ -498,7 +499,7 @@ static struct file_struct *receive_file_ |
0f6fb3c8 WD |
200 | char thisname[MAXPATHLEN]; |
201 | unsigned int l1 = 0, l2 = 0; | |
202 | int alloc_len, basename_len, dirname_len, linkname_len, sum_len; | |
203 | -#ifdef SUPPORT_ACLS | |
204 | +#if defined SUPPORT_ACLS || defined SUPPORT_XATTRS | |
205 | int xtra_len; | |
206 | #endif | |
207 | OFF_T file_length; | |
de565f59 | 208 | @@ -610,10 +611,16 @@ static struct file_struct *receive_file_ |
0f6fb3c8 WD |
209 | xtra_len = (S_ISDIR(mode) ? 2 : 1) * 4; |
210 | else | |
211 | xtra_len = 0; | |
212 | +#elif defined SUPPORT_XATTRS | |
213 | + xtra_len = 0; | |
214 | +#endif | |
215 | +#ifdef SUPPORT_XATTRS | |
216 | + if (preserve_xattrs) | |
217 | + xtra_len += 4; | |
218 | #endif | |
219 | ||
220 | alloc_len = file_struct_len + dirname_len + basename_len | |
221 | -#ifdef SUPPORT_ACLS | |
222 | +#if defined SUPPORT_ACLS || defined SUPPORT_XATTRS | |
223 | + xtra_len | |
224 | #endif | |
225 | + linkname_len + sum_len; | |
de565f59 | 226 | @@ -622,7 +629,7 @@ static struct file_struct *receive_file_ |
0f6fb3c8 WD |
227 | file = (struct file_struct *)bp; |
228 | memset(bp, 0, file_struct_len); | |
229 | bp += file_struct_len; | |
230 | -#ifdef SUPPORT_ACLS | |
231 | +#if defined SUPPORT_ACLS || defined SUPPORT_XATTRS | |
232 | bp += xtra_len; | |
233 | #endif | |
234 | ||
de565f59 | 235 | @@ -723,6 +730,10 @@ static struct file_struct *receive_file_ |
0f6fb3c8 WD |
236 | if (preserve_acls) |
237 | receive_acl(file, f); | |
238 | #endif | |
239 | +#ifdef SUPPORT_XATTRS | |
240 | + if (preserve_xattrs) | |
241 | + receive_xattr(file, f ); | |
242 | +#endif | |
243 | ||
244 | return file; | |
245 | } | |
a071aea2 WD |
246 | @@ -974,7 +985,7 @@ static struct file_struct *send_file_nam |
247 | unsigned short flags) | |
248 | { | |
249 | struct file_struct *file; | |
250 | -#ifdef SUPPORT_ACLS | |
251 | +#if defined SUPPORT_ACLS || defined SUPPORT_XATTRS | |
252 | statx sx; | |
253 | #endif | |
254 | ||
de565f59 | 255 | @@ -994,6 +1005,13 @@ static struct file_struct *send_file_nam |
0f6fb3c8 WD |
256 | return NULL; |
257 | } | |
09cc6650 WD |
258 | #endif |
259 | +#ifdef SUPPORT_XATTRS | |
0f6fb3c8 WD |
260 | + if (preserve_xattrs) { |
261 | + sx.xattr = NULL; | |
262 | + if (get_xattr(fname, &sx) < 0) | |
263 | + return NULL; | |
264 | + } | |
09cc6650 | 265 | +#endif |
bbdff76a | 266 | |
a2f9a486 WD |
267 | maybe_emit_filelist_progress(flist->count + flist_count_offset); |
268 | ||
de565f59 | 269 | @@ -1006,11 +1024,19 @@ static struct file_struct *send_file_nam |
93d6ca6d | 270 | if (preserve_acls) |
0f6fb3c8 | 271 | send_acl(&sx, f); |
93d6ca6d | 272 | #endif |
09cc6650 | 273 | +#ifdef SUPPORT_XATTRS |
93d6ca6d | 274 | + if (preserve_xattrs) |
0f6fb3c8 | 275 | + send_xattr(&sx, f); |
09cc6650 | 276 | +#endif |
bbdff76a | 277 | } else { |
93d6ca6d | 278 | #ifdef SUPPORT_ACLS |
93d6ca6d | 279 | if (preserve_acls) |
0f6fb3c8 | 280 | free_acl(&sx); |
09cc6650 WD |
281 | #endif |
282 | +#ifdef SUPPORT_XATTRS | |
93d6ca6d | 283 | + if (preserve_xattrs) |
0f6fb3c8 | 284 | + free_xattr(&sx); |
09cc6650 | 285 | +#endif |
bbdff76a WD |
286 | } |
287 | return file; | |
288 | } | |
9a7eef96 WD |
289 | --- old/lib/sysxattr.c |
290 | +++ new/lib/sysxattr.c | |
742a4103 | 291 | @@ -0,0 +1,136 @@ |
dc52bc1a WD |
292 | +/* |
293 | + * Extended attribute support for rsync. | |
294 | + * | |
295 | + * Copyright (C) 2004 Red Hat, Inc. | |
296 | + * Written by Jay Fenlason. | |
297 | + * | |
298 | + * This program is free software; you can redistribute it and/or modify | |
299 | + * it under the terms of the GNU General Public License as published by | |
300 | + * the Free Software Foundation; either version 2 of the License, or | |
301 | + * (at your option) any later version. | |
302 | + * | |
303 | + * This program is distributed in the hope that it will be useful, | |
304 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
305 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
306 | + * GNU General Public License for more details. | |
307 | + * | |
308 | + * You should have received a copy of the GNU General Public License along | |
309 | + * with this program; if not, write to the Free Software Foundation, Inc., | |
310 | + * 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. | |
311 | + */ | |
bbdff76a WD |
312 | + |
313 | +#include "rsync.h" | |
0f6fb3c8 | 314 | +#include "sysxattr.h" |
bbdff76a | 315 | + |
56473cb9 WD |
316 | +#ifdef SUPPORT_XATTRS |
317 | + | |
09cc6650 | 318 | +#if defined HAVE_LINUX_XATTRS |
bbdff76a WD |
319 | + |
320 | +ssize_t sys_lgetxattr(const char *path, const char *name, void *value, size_t size) | |
321 | +{ | |
322 | + return lgetxattr(path, name, value, size); | |
323 | +} | |
324 | + | |
8627d8a2 WD |
325 | +ssize_t sys_fgetxattr(int filedes, const char *name, void *value, size_t size) |
326 | +{ | |
0bac098f | 327 | + return fgetxattr(filedes, name, value, size); |
8627d8a2 WD |
328 | +} |
329 | + | |
742a4103 | 330 | +int sys_lsetxattr(const char *path, const char *name, const void *value, size_t size) |
bbdff76a | 331 | +{ |
742a4103 | 332 | + return lsetxattr(path, name, value, size, 0); |
bbdff76a WD |
333 | +} |
334 | + | |
a74926e4 WD |
335 | +int sys_lremovexattr(const char *path, const char *name) |
336 | +{ | |
337 | + return lremovexattr(path, name); | |
338 | +} | |
339 | + | |
bbdff76a WD |
340 | +ssize_t sys_llistxattr(const char *path, char *list, size_t size) |
341 | +{ | |
342 | + return llistxattr(path, list, size); | |
343 | +} | |
344 | + | |
49de5440 WD |
345 | +#elif HAVE_OSX_XATTRS |
346 | + | |
347 | +ssize_t sys_lgetxattr(const char *path, const char *name, void *value, size_t size) | |
348 | +{ | |
349 | + return getxattr(path, name, value, size, 0, XATTR_NOFOLLOW); | |
350 | +} | |
351 | + | |
8627d8a2 WD |
352 | +ssize_t sys_fgetxattr(int filedes, const char *name, void *value, size_t size) |
353 | +{ | |
0bac098f | 354 | + return fgetxattr(filedes, name, value, size, 0, 0); |
8627d8a2 WD |
355 | +} |
356 | + | |
742a4103 | 357 | +int sys_lsetxattr(const char *path, const char *name, const void *value, size_t size) |
49de5440 | 358 | +{ |
742a4103 | 359 | + return setxattr(path, name, value, size, 0, XATTR_NOFOLLOW); |
49de5440 WD |
360 | +} |
361 | + | |
a74926e4 WD |
362 | +int sys_lremovexattr(const char *path, const char *name) |
363 | +{ | |
364 | + return removexattr(path, name, XATTR_NOFOLLOW); | |
365 | +} | |
366 | + | |
49de5440 WD |
367 | +ssize_t sys_llistxattr(const char *path, char *list, size_t size) |
368 | +{ | |
369 | + return listxattr(path, list, size, XATTR_NOFOLLOW); | |
370 | +} | |
371 | + | |
742a4103 WD |
372 | +#elif HAVE_FREEBSD_XATTRS |
373 | + | |
374 | +ssize_t sys_lgetxattr(const char *path, const char *name, void *value, size_t size) | |
375 | +{ | |
376 | + return extattr_get_link(path, EXTATTR_NAMESPACE_USER, name, value, size); | |
377 | +} | |
378 | + | |
379 | +ssize_t sys_fgetxattr(int filedes, const char *name, void *value, size_t size) | |
380 | +{ | |
381 | + return extattr_get_fd(filedes, EXTATTR_NAMESPACE_USER, name, value, size); | |
382 | +} | |
383 | + | |
384 | +int sys_lsetxattr(const char *path, const char *name, const void *value, size_t size) | |
385 | +{ | |
386 | + return extattr_set_link(path, EXTATTR_NAMESPACE_USER, name, value, size); | |
387 | +} | |
388 | + | |
389 | +int sys_lremovexattr(const char *path, const char *name) | |
390 | +{ | |
391 | + return extattr_delete_link(path, EXTATTR_NAMESPACE_USER, name); | |
392 | +} | |
393 | + | |
394 | +ssize_t sys_llistxattr(const char *path, char *list, size_t size) | |
395 | +{ | |
396 | + unsigned char keylen; | |
397 | + ssize_t off, len = extattr_list_link(path, EXTATTR_NAMESPACE_USER, list, size); | |
398 | + | |
399 | + if (len <= 0 || (size_t)len > size) | |
400 | + return len; | |
401 | + | |
402 | + /* FreeBSD puts a single-byte length before each string, with no '\0' | |
403 | + * terminator. We need to change this into a series of null-terminted | |
404 | + * strings. Since the size is the same, we can simply transform the | |
405 | + * output in place. */ | |
406 | + for (off = 0; off < len; ) { | |
407 | + keylen = ((unsigned char*)list)[off]; | |
408 | + if (off + keylen >= len) { | |
409 | + /* Should be impossible, but kernel bugs happen! */ | |
410 | + errno = EINVAL; | |
411 | + return -1; | |
412 | + } | |
413 | + memmove(list+off, list+off+1, keylen); | |
414 | + off += keylen; | |
415 | + list[off++] = '\0'; | |
416 | + } | |
417 | + | |
418 | + return len; | |
419 | +} | |
420 | + | |
bbdff76a WD |
421 | +#else |
422 | + | |
56473cb9 WD |
423 | +#error You need to create xattr compatibility functions. |
424 | + | |
425 | +#endif | |
426 | + | |
427 | +#endif /* SUPPORT_XATTRS */ | |
9a7eef96 WD |
428 | --- old/lib/sysxattr.h |
429 | +++ new/lib/sysxattr.h | |
742a4103 | 430 | @@ -0,0 +1,26 @@ |
49de5440 | 431 | +#ifdef SUPPORT_XATTRS |
8889f85e | 432 | + |
49de5440 | 433 | +#if defined HAVE_ATTR_XATTR_H |
0f6fb3c8 | 434 | +#include <attr/xattr.h> |
49de5440 WD |
435 | +#elif defined HAVE_SYS_XATTR_H |
436 | +#include <sys/xattr.h> | |
742a4103 WD |
437 | +#elif defined HAVE_SYS_EXTATTR_H |
438 | +#include <sys/extattr.h> | |
49de5440 | 439 | +#endif |
bbdff76a | 440 | + |
a74926e4 WD |
441 | +/* Linux 2.4 does not define this as a distinct errno value: */ |
442 | +#ifndef ENOATTR | |
443 | +#define ENOATTR ENODATA | |
444 | +#endif | |
445 | + | |
bbdff76a | 446 | +ssize_t sys_lgetxattr(const char *path, const char *name, void *value, size_t size); |
8627d8a2 | 447 | +ssize_t sys_fgetxattr(int filedes, const char *name, void *value, size_t size); |
742a4103 | 448 | +int sys_lsetxattr(const char *path, const char *name, const void *value, size_t size); |
a74926e4 | 449 | +int sys_lremovexattr(const char *path, const char *name); |
bbdff76a WD |
450 | +ssize_t sys_llistxattr(const char *path, char *list, size_t size); |
451 | + | |
452 | +#else | |
453 | + | |
8889f85e | 454 | +/* No xattrs available */ |
49de5440 WD |
455 | + |
456 | +#endif | |
9a7eef96 WD |
457 | --- old/options.c |
458 | +++ new/options.c | |
dc52bc1a | 459 | @@ -48,6 +48,7 @@ int copy_links = 0; |
bbdff76a WD |
460 | int preserve_links = 0; |
461 | int preserve_hard_links = 0; | |
462 | int preserve_acls = 0; | |
463 | +int preserve_xattrs = 0; | |
464 | int preserve_perms = 0; | |
4a65fe72 | 465 | int preserve_executability = 0; |
bbdff76a | 466 | int preserve_devices = 0; |
4959107f | 467 | @@ -201,6 +202,7 @@ static void print_rsync_version(enum log |
bbdff76a WD |
468 | char const *have_inplace = "no "; |
469 | char const *hardlinks = "no "; | |
470 | char const *acls = "no "; | |
471 | + char const *xattrs = "no "; | |
472 | char const *links = "no "; | |
473 | char const *ipv6 = "no "; | |
474 | STRUCT_STAT *dumstat; | |
4959107f | 475 | @@ -220,7 +222,9 @@ static void print_rsync_version(enum log |
bbdff76a WD |
476 | #ifdef SUPPORT_ACLS |
477 | acls = ""; | |
478 | #endif | |
479 | - | |
480 | +#ifdef SUPPORT_XATTRS | |
481 | + xattrs = ""; | |
482 | +#endif | |
483 | #ifdef SUPPORT_LINKS | |
484 | links = ""; | |
485 | #endif | |
56473cb9 WD |
486 | @@ -236,8 +240,8 @@ static void print_rsync_version(enum log |
487 | rprintf(f, "Capabilities: %d-bit files, %ssocketpairs, %shard links, %ssymlinks,\n", | |
488 | (int) (sizeof (OFF_T) * 8), got_socketpair, hardlinks, links); | |
489 | ||
490 | - rprintf(f, " batchfiles, %sinplace, %sIPv6, %sACLs,\n", | |
491 | - have_inplace, ipv6, acls); | |
492 | + rprintf(f, " batchfiles, %sinplace, %sIPv6, %sACLs, %sxattrs,\n", | |
493 | + have_inplace, ipv6, acls, xattrs); | |
bbdff76a WD |
494 | |
495 | /* Note that this field may not have type ino_t. It depends | |
496 | * on the complicated interaction between largefile feature | |
56473cb9 | 497 | @@ -289,7 +293,7 @@ void usage(enum logcode F) |
1b57ecb0 | 498 | rprintf(F," -q, --quiet suppress non-error messages\n"); |
4959107f | 499 | rprintf(F," --no-motd suppress daemon-mode MOTD (see manpage caveat)\n"); |
1b57ecb0 WD |
500 | rprintf(F," -c, --checksum skip based on checksum, not mod-time & size\n"); |
501 | - rprintf(F," -a, --archive archive mode; same as -rlptgoD (no -H, -A)\n"); | |
502 | + rprintf(F," -a, --archive archive mode; equals -rlptgoD (no -H,-A,-X)\n"); | |
503 | rprintf(F," --no-OPTION turn off an implied OPTION (e.g. --no-D)\n"); | |
504 | rprintf(F," -r, --recursive recurse into directories\n"); | |
505 | rprintf(F," -R, --relative use relative path names\n"); | |
56473cb9 | 506 | @@ -314,6 +318,9 @@ void usage(enum logcode F) |
3610c43c | 507 | #ifdef SUPPORT_ACLS |
bbdff76a | 508 | rprintf(F," -A, --acls preserve ACLs (implies --perms)\n"); |
3610c43c WD |
509 | #endif |
510 | +#ifdef SUPPORT_XATTRS | |
bbdff76a | 511 | + rprintf(F," -X, --xattrs preserve extended attributes (implies --perms)\n"); |
3610c43c | 512 | +#endif |
4a65fe72 | 513 | rprintf(F," -o, --owner preserve owner (super-user only)\n"); |
bbdff76a | 514 | rprintf(F," -g, --group preserve group\n"); |
1ed0b5c9 | 515 | rprintf(F," --devices preserve device files (super-user only)\n"); |
56473cb9 | 516 | @@ -436,6 +443,9 @@ static struct poptOption long_options[] |
489b0a72 WD |
517 | {"acls", 'A', POPT_ARG_NONE, 0, 'A', 0, 0 }, |
518 | {"no-acls", 0, POPT_ARG_VAL, &preserve_acls, 0, 0, 0 }, | |
519 | {"no-A", 0, POPT_ARG_VAL, &preserve_acls, 0, 0, 0 }, | |
520 | + {"xattrs", 'X', POPT_ARG_NONE, 0, 'X', 0, 0 }, | |
521 | + {"no-xattrs", 0, POPT_ARG_VAL, &preserve_xattrs, 0, 0, 0 }, | |
522 | + {"no-X", 0, POPT_ARG_VAL, &preserve_xattrs, 0, 0, 0 }, | |
523 | {"times", 't', POPT_ARG_VAL, &preserve_times, 1, 0, 0 }, | |
524 | {"no-times", 0, POPT_ARG_VAL, &preserve_times, 0, 0, 0 }, | |
525 | {"no-t", 0, POPT_ARG_VAL, &preserve_times, 0, 0, 0 }, | |
56473cb9 | 526 | @@ -1117,6 +1127,17 @@ int parse_arguments(int *argc, const cha |
489b0a72 | 527 | return 0; |
3610c43c | 528 | #endif |
bbdff76a WD |
529 | |
530 | + case 'X': | |
531 | +#ifdef SUPPORT_XATTRS | |
532 | + preserve_xattrs = 1; | |
533 | + preserve_perms = 1; | |
489b0a72 | 534 | + break; |
bbdff76a WD |
535 | +#else |
536 | + snprintf(err_buf,sizeof(err_buf), | |
537 | + "extended attributes are not supported on this %s\n", | |
538 | + am_server ? "server" : "client"); | |
539 | + return 0; | |
d2dc8a18 | 540 | +#endif |
bbdff76a WD |
541 | |
542 | default: | |
543 | /* A large opt value means that set_refuse_options() | |
56473cb9 | 544 | @@ -1563,6 +1584,10 @@ void server_options(char **args,int *arg |
bbdff76a WD |
545 | if (preserve_acls) |
546 | argstr[x++] = 'A'; | |
3610c43c WD |
547 | #endif |
548 | +#ifdef SUPPORT_XATTRS | |
bbdff76a WD |
549 | + if (preserve_xattrs) |
550 | + argstr[x++] = 'X'; | |
3610c43c | 551 | +#endif |
bbdff76a WD |
552 | if (preserve_uid) |
553 | argstr[x++] = 'o'; | |
554 | if (preserve_gid) | |
9a7eef96 WD |
555 | --- old/rsync.c |
556 | +++ new/rsync.c | |
ff318e90 WD |
557 | @@ -33,6 +33,7 @@ |
558 | extern int verbose; | |
93d6ca6d | 559 | extern int dry_run; |
93d6ca6d WD |
560 | extern int preserve_acls; |
561 | +extern int preserve_xattrs; | |
562 | extern int preserve_perms; | |
563 | extern int preserve_executability; | |
564 | extern int preserve_times; | |
03019e41 | 565 | @@ -218,6 +219,10 @@ int set_file_attrs(char *fname, struct f |
071c5b19 WD |
566 | if (daemon_chmod_modes && !S_ISLNK(new_mode)) |
567 | new_mode = tweak_mode(new_mode, daemon_chmod_modes); | |
568 | ||
09cc6650 | 569 | +#ifdef SUPPORT_XATTRS |
0f6fb3c8 | 570 | + if (preserve_xattrs && set_xattr(fname, file, sxp) == 0) |
09cc6650 WD |
571 | + updated = 1; |
572 | +#endif | |
071c5b19 WD |
573 | #ifdef SUPPORT_ACLS |
574 | /* It's OK to call set_acl() now, even for a dir, as the generator | |
575 | * will enable owner-writability using chmod, if necessary. | |
9a7eef96 WD |
576 | --- old/rsync.h |
577 | +++ new/rsync.h | |
56473cb9 | 578 | @@ -500,6 +500,10 @@ struct idev { |
0f6fb3c8 | 579 | #define ACLS_NEED_MASK 1 |
09cc6650 | 580 | #endif |
bbdff76a | 581 | |
56473cb9 | 582 | +#ifndef HAVE_NO_XATTRS |
bbdff76a WD |
583 | +#define SUPPORT_XATTRS 1 |
584 | +#endif | |
585 | + | |
0f6fb3c8 WD |
586 | #define GID_NONE ((gid_t)-1) |
587 | ||
588 | #define HL_CHECK_MASTER 0 | |
56473cb9 | 589 | @@ -694,6 +698,9 @@ typedef struct { |
0f6fb3c8 WD |
590 | struct rsync_acl *acc_acl; /* access ACL */ |
591 | struct rsync_acl *def_acl; /* default ACL */ | |
592 | #endif | |
593 | +#ifdef SUPPORT_XATTRS | |
594 | + item_list *xattr; | |
bbdff76a | 595 | +#endif |
0f6fb3c8 | 596 | } statx; |
bbdff76a | 597 | |
0f6fb3c8 | 598 | #define ACL_READY(sx) ((sx).acc_acl != NULL) |
9a7eef96 WD |
599 | --- old/rsync.yo |
600 | +++ new/rsync.yo | |
4959107f | 601 | @@ -301,7 +301,7 @@ to the detailed description below for a |
1b57ecb0 | 602 | -q, --quiet suppress non-error messages |
4959107f | 603 | --no-motd suppress daemon-mode MOTD (see caveat) |
1b57ecb0 WD |
604 | -c, --checksum skip based on checksum, not mod-time & size |
605 | - -a, --archive archive mode; same as -rlptgoD (no -H, -A) | |
606 | + -a, --archive archive mode; equals -rlptgoD (no -H,-A,-X) | |
607 | --no-OPTION turn off an implied OPTION (e.g. --no-D) | |
608 | -r, --recursive recurse into directories | |
609 | -R, --relative use relative path names | |
4959107f | 610 | @@ -324,6 +324,7 @@ to the detailed description below for a |
4a65fe72 | 611 | -E, --executability preserve executability |
063cf77b | 612 | --chmod=CHMOD affect file and/or directory permissions |
4a65fe72 WD |
613 | -A, --acls preserve ACLs (implies -p) [non-standard] |
614 | + -X, --xattrs preserve extended attrs (implies -p) [n.s.] | |
4a65fe72 | 615 | -o, --owner preserve owner (super-user only) |
bbdff76a | 616 | -g, --group preserve group |
1ed0b5c9 | 617 | --devices preserve device files (super-user only) |
4959107f | 618 | @@ -818,6 +819,11 @@ version makes it incompatible with sendi |
3fcc541f WD |
619 | rsync unless you double the bf(--acls) option (e.g. bf(-AA)). This |
620 | doubling is not needed when pulling files from an older rsync. | |
bbdff76a WD |
621 | |
622 | +dit(bf(-X, --xattrs)) This option causes rsync to update the remote | |
623 | +extended attributes to be the same as the local ones. This will work | |
624 | +only if the remote machine's rsync supports this option also. This is | |
625 | +a non-standard option. | |
626 | + | |
4a65fe72 WD |
627 | dit(bf(--chmod)) This option tells rsync to apply one or more |
628 | comma-separated "chmod" strings to the permission of the files in the | |
629 | transfer. The resulting value is treated as though it was the permissions | |
9a7eef96 WD |
630 | --- old/xattr.c |
631 | +++ new/xattr.c | |
742a4103 | 632 | @@ -0,0 +1,376 @@ |
dc52bc1a WD |
633 | +/* |
634 | + * Extended Attribute support for rsync. | |
0f6fb3c8 | 635 | + * Written by Jay Fenlason, vaguely based on the ACLs patch. |
dc52bc1a WD |
636 | + * |
637 | + * Copyright (C) 2004 Red Hat, Inc. | |
0f6fb3c8 | 638 | + * Copyright (C) 2006 Wayne Davison |
dc52bc1a WD |
639 | + * |
640 | + * This program is free software; you can redistribute it and/or modify | |
641 | + * it under the terms of the GNU General Public License as published by | |
642 | + * the Free Software Foundation; either version 2 of the License, or | |
643 | + * (at your option) any later version. | |
644 | + * | |
645 | + * This program is distributed in the hope that it will be useful, | |
646 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
647 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
648 | + * GNU General Public License for more details. | |
649 | + * | |
650 | + * You should have received a copy of the GNU General Public License along | |
651 | + * with this program; if not, write to the Free Software Foundation, Inc., | |
652 | + * 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. | |
653 | + */ | |
bbdff76a WD |
654 | + |
655 | +#include "rsync.h" | |
656 | +#include "lib/sysxattr.h" | |
657 | + | |
658 | +#ifdef SUPPORT_XATTRS | |
659 | + | |
bbdff76a | 660 | +extern int dry_run; |
8889f85e WD |
661 | +extern int read_only; |
662 | +extern int list_only; | |
0f6fb3c8 | 663 | +extern unsigned int file_struct_len; |
bbdff76a WD |
664 | + |
665 | +#define RSYNC_XAL_INITIAL 5 | |
666 | +#define RSYNC_XAL_LIST_INITIAL 100 | |
667 | + | |
668 | +typedef struct { | |
bbdff76a | 669 | + char *name; |
bbdff76a | 670 | + char *datum; |
0f6fb3c8 WD |
671 | + size_t name_len; |
672 | + size_t datum_len; | |
bbdff76a WD |
673 | +} rsync_xa; |
674 | + | |
bbdff76a WD |
675 | +static size_t namebuf_len = 0; |
676 | +static char *namebuf = NULL; | |
677 | + | |
0f6fb3c8 WD |
678 | +static item_list empty_xattr = EMPTY_ITEM_LIST; |
679 | +static item_list rsync_xal_l = EMPTY_ITEM_LIST; | |
bbdff76a | 680 | + |
742a4103 WD |
681 | +#ifndef HAVE_LINUX_XATTRS |
682 | +/* Linux extended attributes keep the namespace in the key. | |
683 | + * Let's do the same for all other platforms. */ | |
684 | +#define USER_PREFIX "user." | |
685 | +#define UPRE_LEN (sizeof USER_PREFIX - 1) | |
517cc92f WD |
686 | +#endif |
687 | + | |
bbdff76a WD |
688 | +/* ------------------------------------------------------------------------- */ |
689 | + | |
0f6fb3c8 | 690 | +static void rsync_xal_free(item_list *xalp) |
bbdff76a WD |
691 | +{ |
692 | + size_t i; | |
0f6fb3c8 | 693 | + rsync_xa *rxas = xalp->items; |
bbdff76a | 694 | + |
0f6fb3c8 WD |
695 | + for (i = 0; i < xalp->count; i++) { |
696 | + free(rxas[i].name); | |
697 | + /* free(rxas[i].value); */ | |
bbdff76a | 698 | + } |
0f6fb3c8 | 699 | + xalp->count = 0; |
bbdff76a WD |
700 | +} |
701 | + | |
0f6fb3c8 | 702 | +void free_xattr(statx *sxp) |
bbdff76a | 703 | +{ |
0f6fb3c8 WD |
704 | + rsync_xal_free(sxp->xattr); |
705 | + free(sxp->xattr); | |
706 | + sxp->xattr = NULL; | |
707 | +} | |
bbdff76a | 708 | + |
0f6fb3c8 WD |
709 | +static int rsync_xal_compare_names(const void *x1, const void *x2) |
710 | +{ | |
711 | + const rsync_xa *xa1 = x1; | |
712 | + const rsync_xa *xa2 = x2; | |
bbdff76a WD |
713 | + return strcmp(xa1->name, xa2->name); |
714 | +} | |
715 | + | |
0f6fb3c8 | 716 | +static int rsync_xal_get(const char *fname, item_list *xalp) |
bbdff76a WD |
717 | +{ |
718 | + ssize_t name_size; | |
719 | + ssize_t datum_size; | |
720 | + ssize_t left; | |
721 | + char *name; | |
722 | + size_t len; | |
723 | + char *ptr; | |
724 | + | |
725 | + if (!namebuf) { | |
49de5440 | 726 | + namebuf_len = 1024; |
bbdff76a | 727 | + namebuf = new_array(char, namebuf_len); |
49de5440 | 728 | + if (!namebuf) |
bbdff76a WD |
729 | + out_of_memory("rsync_xal_get"); |
730 | + } | |
731 | + | |
49de5440 | 732 | + /* The length returned includes all the '\0' terminators. */ |
bbdff76a WD |
733 | + name_size = sys_llistxattr(fname, namebuf, namebuf_len); |
734 | + if (name_size > (ssize_t)namebuf_len) { | |
735 | + name_size = -1; | |
736 | + errno = ERANGE; | |
737 | + } | |
738 | + if (name_size < 0) { | |
739 | + if (errno == ENOTSUP) | |
0f6fb3c8 | 740 | + return 0; |
bbdff76a WD |
741 | + if (errno == ERANGE) { |
742 | + name_size = sys_llistxattr(fname, NULL, 0); | |
743 | + if (name_size < 0) { | |
0f6fb3c8 WD |
744 | + rsyserr(FERROR, errno, "%s: rsync_xal_get: llistxattr", |
745 | + fname); | |
e5754c5f | 746 | + return -1; |
bbdff76a | 747 | + } |
49de5440 | 748 | + namebuf = realloc_array(namebuf, char, name_size + 1024); |
bbdff76a WD |
749 | + if (!namebuf) |
750 | + out_of_memory("rsync_xal_get"); | |
49de5440 | 751 | + namebuf_len = name_size + 1024; |
bbdff76a WD |
752 | + name_size = sys_llistxattr(fname, namebuf, namebuf_len); |
753 | + if (name_size < 0) { | |
0f6fb3c8 WD |
754 | + rsyserr(FERROR, errno, |
755 | + "%s: rsync_xal_get: re-llistxattr failed", | |
756 | + fname); | |
e5754c5f | 757 | + return -1; |
bbdff76a WD |
758 | + } |
759 | + } else { | |
0f6fb3c8 WD |
760 | + rsyserr(FERROR, errno, |
761 | + "%s: rsync_xal_get: llistxattr failed:", | |
762 | + fname); | |
e5754c5f | 763 | + return -1; |
bbdff76a WD |
764 | + } |
765 | + } | |
bbdff76a | 766 | + if (name_size == 0) |
e5754c5f | 767 | + return 0; |
bbdff76a | 768 | + for (left = name_size, name = namebuf; left > 0 ; left -= len, name += len) { |
0f6fb3c8 | 769 | + rsync_xa *rxas = EXPAND_ITEM_LIST(xalp, rsync_xa, RSYNC_XAL_INITIAL); |
bbdff76a | 770 | + |
0f6fb3c8 | 771 | + len = strlen(name) + 1; |
49de5440 | 772 | + datum_size = sys_lgetxattr(fname, name, NULL, 0); |
bbdff76a WD |
773 | + if (datum_size < 0) { |
774 | + if (errno == ENOTSUP) | |
e5754c5f | 775 | + return -1; |
49de5440 WD |
776 | + rsyserr(FERROR, errno, |
777 | + "%s: rsync_xal_get: lgetxattr %s failed", | |
778 | + fname, name); | |
779 | + return -1; | |
bbdff76a WD |
780 | + } |
781 | + ptr = new_array(char, len + datum_size); | |
782 | + if (!ptr) | |
783 | + out_of_memory("rsync_xal_get"); | |
0f6fb3c8 | 784 | + memcpy(ptr, name, len); |
0f6fb3c8 WD |
785 | + rxas->name_len = len; |
786 | + rxas->name = ptr; | |
787 | + rxas->datum_len = datum_size; | |
788 | + rxas->datum = ptr + len; | |
49de5440 WD |
789 | + if (datum_size) { |
790 | + ssize_t size = sys_lgetxattr(fname, name, rxas->datum, datum_size); | |
791 | + if (size != datum_size) { | |
792 | + if (size < 0) { | |
793 | + rsyserr(FERROR, errno, | |
794 | + "rsync_xal_get: lgetxattr(%s,%s)" | |
795 | + " failed", fname, name); | |
796 | + } else { | |
797 | + rprintf(FERROR, | |
798 | + "rsync_xal_get: lgetxattr(%s,%s)" | |
bb6b64ea WD |
799 | + " returned %ld instead of %ld\n", |
800 | + fname, name, | |
801 | + (long)size, (long)datum_size); | |
49de5440 WD |
802 | + } |
803 | + return -1; | |
804 | + } | |
805 | + } | |
bbdff76a | 806 | + } |
0f6fb3c8 WD |
807 | + if (xalp->count > 1) |
808 | + qsort(xalp->items, xalp->count, sizeof (rsync_xa), rsync_xal_compare_names); | |
e5754c5f | 809 | + return 0; |
bbdff76a WD |
810 | +} |
811 | + | |
0f6fb3c8 WD |
812 | +/* Read the xattr(s) for this filename. */ |
813 | +int get_xattr(const char *fname, statx *sxp) | |
bbdff76a | 814 | +{ |
0f6fb3c8 WD |
815 | + sxp->xattr = new(item_list); |
816 | + *sxp->xattr = empty_xattr; | |
817 | + if (rsync_xal_get(fname, sxp->xattr) < 0) { | |
818 | + free_xattr(sxp); | |
819 | + return -1; | |
820 | + } | |
821 | + return 0; | |
bbdff76a WD |
822 | +} |
823 | + | |
0f6fb3c8 | 824 | +static int find_matching_xattr(item_list *xalp) |
bbdff76a | 825 | +{ |
0f6fb3c8 WD |
826 | + size_t i, j; |
827 | + item_list *lst = rsync_xal_l.items; | |
bbdff76a WD |
828 | + |
829 | + for (i = 0; i < rsync_xal_l.count; i++) { | |
0f6fb3c8 WD |
830 | + rsync_xa *rxas1 = lst[i].items; |
831 | + rsync_xa *rxas2 = xalp->items; | |
832 | + | |
bbdff76a | 833 | + /* Wrong number of elements? */ |
0f6fb3c8 | 834 | + if (lst[i].count != xalp->count) |
bbdff76a WD |
835 | + continue; |
836 | + /* any elements different? */ | |
0f6fb3c8 WD |
837 | + for (j = 0; j < xalp->count; j++) { |
838 | + if (rxas1[j].name_len != rxas2[j].name_len | |
839 | + || rxas1[j].datum_len != rxas2[j].datum_len | |
840 | + || strcmp(rxas1[j].name, rxas2[j].name) | |
841 | + || memcmp(rxas1[j].datum, rxas2[j].datum, rxas2[j].datum_len)) | |
bbdff76a WD |
842 | + break; |
843 | + } | |
844 | + /* no differences found. This is The One! */ | |
0f6fb3c8 WD |
845 | + if (j == xalp->count) |
846 | + return i; | |
bbdff76a | 847 | + } |
0f6fb3c8 WD |
848 | + |
849 | + return -1; | |
bbdff76a WD |
850 | +} |
851 | + | |
0f6fb3c8 WD |
852 | +/* Store *xalp on the end of rsync_xal_l */ |
853 | +static void rsync_xal_store(item_list *xalp) | |
bbdff76a | 854 | +{ |
0f6fb3c8 | 855 | + item_list *new_lst = EXPAND_ITEM_LIST(&rsync_xal_l, item_list, RSYNC_XAL_LIST_INITIAL); |
de565f59 WD |
856 | + /* Since the following call starts a new list, we know it will hold the |
857 | + * entire initial-count, not just enough space for one new item. */ | |
49de5440 WD |
858 | + *new_lst = empty_xattr; |
859 | + (void)EXPAND_ITEM_LIST(new_lst, rsync_xa, xalp->count); | |
860 | + memcpy(new_lst->items, xalp->items, xalp->count * sizeof (rsync_xa)); | |
0f6fb3c8 WD |
861 | + new_lst->count = xalp->count; |
862 | + xalp->count = 0; | |
bbdff76a WD |
863 | +} |
864 | + | |
0f6fb3c8 WD |
865 | +/* Send the make_xattr()-generated xattr list for this flist entry. */ |
866 | +void send_xattr(statx *sxp, int f) | |
bbdff76a | 867 | +{ |
0f6fb3c8 WD |
868 | + int ndx = find_matching_xattr(sxp->xattr); |
869 | + if (ndx != -1) { | |
bbdff76a | 870 | + write_byte(f, 'x'); |
0f6fb3c8 WD |
871 | + write_int(f, ndx); |
872 | + rsync_xal_free(sxp->xattr); | |
bbdff76a WD |
873 | + } else { |
874 | + rsync_xa *rxa; | |
0f6fb3c8 | 875 | + int count = sxp->xattr->count; |
bbdff76a WD |
876 | + write_byte(f, 'X'); |
877 | + write_int(f, count); | |
0f6fb3c8 | 878 | + for (rxa = sxp->xattr->items; count--; rxa++) { |
742a4103 WD |
879 | +#ifdef HAVE_LINUX_XATTRS |
880 | + /* Linux already puts the namespace in the key. */ | |
881 | + write_int(f, rxa->name_len); | |
882 | + write_int(f, rxa->datum_len); | |
883 | +#else | |
884 | + /* Other platforms need the namespace added in, | |
885 | + * so use the "user." namespace for all keys. */ | |
886 | + write_int(f, rxa->name_len + UPRE_LEN); | |
887 | + write_int(f, rxa->datum_len); | |
888 | + write_buf(f, USER_PREFIX, UPRE_LEN); | |
517cc92f | 889 | +#endif |
bbdff76a WD |
890 | + write_buf(f, rxa->name, rxa->name_len); |
891 | + write_buf(f, rxa->datum, rxa->datum_len); | |
892 | + } | |
49de5440 | 893 | + rsync_xal_store(sxp->xattr); /* adds item to rsync_xal_l */ |
bbdff76a | 894 | + } |
0f6fb3c8 | 895 | + free_xattr(sxp); |
bbdff76a WD |
896 | +} |
897 | + | |
bbdff76a | 898 | +/* ------------------------------------------------------------------------- */ |
bbdff76a | 899 | + |
0f6fb3c8 | 900 | +/* receive and build the rsync_xattr_lists */ |
bbdff76a WD |
901 | +void receive_xattr(struct file_struct *file, int f) |
902 | +{ | |
0f6fb3c8 WD |
903 | + static item_list temp_xattr = EMPTY_ITEM_LIST; |
904 | + int tag = read_byte(f); | |
905 | + char *ndx_ptr = (char*)file + file_struct_len; | |
906 | + int ndx; | |
bbdff76a | 907 | + |
bbdff76a | 908 | + if (tag == 'X') { |
0f6fb3c8 | 909 | + int i, count = read_int(f); |
bbdff76a | 910 | + for (i = 0; i < count; i++) { |
bbdff76a | 911 | + char *ptr; |
0f6fb3c8 WD |
912 | + rsync_xa *rxa; |
913 | + size_t name_len = read_int(f); | |
914 | + size_t datum_len = read_int(f); | |
3fcc541f WD |
915 | + if (name_len + datum_len < name_len) |
916 | + out_of_memory("receive_xattr"); /* overflow */ | |
0f6fb3c8 | 917 | + rxa = EXPAND_ITEM_LIST(&temp_xattr, rsync_xa, count); |
bbdff76a WD |
918 | + ptr = new_array(char, name_len + datum_len); |
919 | + if (!ptr) | |
920 | + out_of_memory("receive_xattr"); | |
921 | + read_buf(f, ptr, name_len); | |
922 | + read_buf(f, ptr + name_len, datum_len); | |
0f6fb3c8 WD |
923 | + rxa->name_len = name_len; |
924 | + rxa->datum_len = datum_len; | |
925 | + rxa->name = ptr; | |
926 | + rxa->datum = ptr + name_len; | |
742a4103 WD |
927 | +#ifndef HAVE_LINUX_XATTRS |
928 | + if (strncmp(rxa->name, USER_PREFIX, UPRE_LEN) == 0) { | |
1e883fdf WD |
929 | + rxa->name_len -= UPRE_LEN; |
930 | + memmove(rxa->name, rxa->name + UPRE_LEN, rxa->name_len); | |
517cc92f WD |
931 | + } |
932 | +#endif | |
bbdff76a | 933 | + } |
49de5440 WD |
934 | + ndx = rsync_xal_l.count; /* pre-incremented count */ |
935 | + rsync_xal_store(&temp_xattr); /* adds item to rsync_xal_l */ | |
0f6fb3c8 WD |
936 | + } else if (tag == 'x') { |
937 | + ndx = read_int(f); | |
938 | + if (ndx < 0 || (size_t)ndx >= rsync_xal_l.count) { | |
939 | + rprintf(FERROR, "%s: receive_xattr: xa index %d out of range\n", | |
940 | + f_name(file, NULL), ndx); | |
bbdff76a WD |
941 | + exit_cleanup(RERR_STREAMIO); |
942 | + } | |
0f6fb3c8 WD |
943 | + } else { |
944 | + rprintf(FERROR, | |
945 | + "%s: receive_xattr: unknown extended attribute type tag: %c\n", | |
946 | + f_name(file, NULL), tag); | |
947 | + exit_cleanup(RERR_STREAMIO); | |
948 | + ndx = 0; /* silence a compiler warning... */ | |
bbdff76a | 949 | + } |
bbdff76a | 950 | + |
0f6fb3c8 | 951 | + SIVAL(ndx_ptr, 0, ndx); |
bbdff76a WD |
952 | +} |
953 | + | |
0f6fb3c8 WD |
954 | +/* Turn the xattr data in statx into cached xattr data, setting the index |
955 | + * values in the file struct. */ | |
956 | +void cache_xattr(struct file_struct *file, statx *sxp) | |
bbdff76a | 957 | +{ |
0f6fb3c8 WD |
958 | + char *ndx_ptr = (char*)file + file_struct_len; |
959 | + int ndx; | |
bbdff76a | 960 | + |
0f6fb3c8 WD |
961 | + if (!sxp->xattr) |
962 | + return; | |
bbdff76a | 963 | + |
0f6fb3c8 WD |
964 | + ndx = find_matching_xattr(sxp->xattr); |
965 | + if (ndx == -1) | |
49de5440 | 966 | + rsync_xal_store(sxp->xattr); /* adds item to rsync_xal_l */ |
0f6fb3c8 | 967 | + free_xattr(sxp); |
bbdff76a | 968 | + |
0f6fb3c8 | 969 | + SIVAL(ndx_ptr, 0, ndx); |
bbdff76a WD |
970 | +} |
971 | + | |
0f6fb3c8 | 972 | +static int rsync_xal_set(const char *fname, item_list *xalp) |
bbdff76a | 973 | +{ |
0f6fb3c8 WD |
974 | + rsync_xa *rxas = xalp->items; |
975 | + size_t i; | |
976 | + int ret = 0; | |
bbdff76a | 977 | + |
0f6fb3c8 | 978 | + for (i = 0; i < xalp->count; i++) { |
742a4103 | 979 | + int status = sys_lsetxattr(fname, rxas[i].name, rxas[i].datum, rxas[i].datum_len); |
0f6fb3c8 WD |
980 | + if (status < 0) { |
981 | + rsyserr(FERROR, errno, "%s: rsync_xal_set: lsetxattr %s failed", | |
982 | + fname, rxas[i].name); | |
983 | + ret = -1; | |
984 | + } | |
bbdff76a | 985 | + } |
0f6fb3c8 | 986 | + return ret; |
bbdff76a WD |
987 | +} |
988 | + | |
0f6fb3c8 WD |
989 | +/* Set extended attributes on indicated filename. */ |
990 | +int set_xattr(const char *fname, const struct file_struct *file, UNUSED(statx *sxp)) | |
bbdff76a | 991 | +{ |
0f6fb3c8 WD |
992 | + int ndx; |
993 | + char *ndx_ptr = (char*)file + file_struct_len; | |
994 | + item_list *lst = rsync_xal_l.items; | |
bbdff76a | 995 | + |
93d6ca6d WD |
996 | + if (dry_run) |
997 | + return 1; /* FIXME: --dry-run needs to compute this value */ | |
998 | + | |
8889f85e WD |
999 | + if (read_only || list_only) { |
1000 | + errno = EROFS; | |
1001 | + return -1; | |
1002 | + } | |
1003 | + | |
0f6fb3c8 WD |
1004 | + ndx = IVAL(ndx_ptr, 0); |
1005 | + return rsync_xal_set(fname, lst + ndx); /* TODO: This needs to return 1 if no xattrs changed! */ | |
bbdff76a WD |
1006 | +} |
1007 | + | |
1008 | +#endif /* SUPPORT_XATTRS */ |