]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/basic/copy.c
Merge pull request #9165 from ssahani/networkd-netdevsim
[thirdparty/systemd.git] / src / basic / copy.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2 /***
3 This file is part of systemd.
4
5 Copyright 2014 Lennart Poettering
6 ***/
7
8 #include <dirent.h>
9 #include <errno.h>
10 #include <fcntl.h>
11 #include <stddef.h>
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <string.h>
15 #include <sys/sendfile.h>
16 #include <sys/stat.h>
17 #include <sys/xattr.h>
18 #include <time.h>
19 #include <unistd.h>
20
21 #include "alloc-util.h"
22 #include "btrfs-util.h"
23 #include "chattr-util.h"
24 #include "copy.h"
25 #include "dirent-util.h"
26 #include "fd-util.h"
27 #include "fileio.h"
28 #include "fs-util.h"
29 #include "io-util.h"
30 #include "macro.h"
31 #include "missing.h"
32 #include "string-util.h"
33 #include "strv.h"
34 #include "time-util.h"
35 #include "umask-util.h"
36 #include "user-util.h"
37 #include "xattr-util.h"
38
39 #define COPY_BUFFER_SIZE (16*1024u)
40
41 static ssize_t try_copy_file_range(
42 int fd_in, loff_t *off_in,
43 int fd_out, loff_t *off_out,
44 size_t len,
45 unsigned int flags) {
46
47 static int have = -1;
48 ssize_t r;
49
50 if (have == 0)
51 return -ENOSYS;
52
53 r = copy_file_range(fd_in, off_in, fd_out, off_out, len, flags);
54 if (have < 0)
55 have = r >= 0 || errno != ENOSYS;
56 if (r < 0)
57 return -errno;
58
59 return r;
60 }
61
62 enum {
63 FD_IS_NO_PIPE,
64 FD_IS_BLOCKING_PIPE,
65 FD_IS_NONBLOCKING_PIPE,
66 };
67
68 static int fd_is_nonblock_pipe(int fd) {
69 struct stat st;
70 int flags;
71
72 /* Checks whether the specified file descriptor refers to a pipe, and if so if O_NONBLOCK is set. */
73
74 if (fstat(fd, &st) < 0)
75 return -errno;
76
77 if (!S_ISFIFO(st.st_mode))
78 return FD_IS_NO_PIPE;
79
80 flags = fcntl(fd, F_GETFL);
81 if (flags < 0)
82 return -errno;
83
84 return FLAGS_SET(flags, O_NONBLOCK) ? FD_IS_NONBLOCKING_PIPE : FD_IS_BLOCKING_PIPE;
85 }
86
87 int copy_bytes_full(
88 int fdf, int fdt,
89 uint64_t max_bytes,
90 CopyFlags copy_flags,
91 void **ret_remains,
92 size_t *ret_remains_size) {
93
94 bool try_cfr = true, try_sendfile = true, try_splice = true;
95 int r, nonblock_pipe = -1;
96 size_t m = SSIZE_MAX; /* that is the maximum that sendfile and c_f_r accept */
97
98 assert(fdf >= 0);
99 assert(fdt >= 0);
100
101 /* Tries to copy bytes from the file descriptor 'fdf' to 'fdt' in the smartest possible way. Copies a maximum
102 * of 'max_bytes', which may be specified as UINT64_MAX, in which no maximum is applied. Returns negative on
103 * error, zero if EOF is hit before the bytes limit is hit and positive otherwise. If the copy fails for some
104 * reason but we read but didn't yet write some data an ret_remains/ret_remains_size is not NULL, then it will
105 * be initialized with an allocated buffer containing this "remaining" data. Note that these two parameters are
106 * initialized with a valid buffer only on failure and only if there's actually data already read. Otherwise
107 * these parameters if non-NULL are set to NULL. */
108
109 if (ret_remains)
110 *ret_remains = NULL;
111 if (ret_remains_size)
112 *ret_remains_size = 0;
113
114 /* Try btrfs reflinks first. This only works on regular, seekable files, hence let's check the file offsets of
115 * source and destination first. */
116 if ((copy_flags & COPY_REFLINK)) {
117 off_t foffset;
118
119 foffset = lseek(fdf, 0, SEEK_CUR);
120 if (foffset >= 0) {
121 off_t toffset;
122
123 toffset = lseek(fdt, 0, SEEK_CUR);
124 if (toffset >= 0) {
125
126 if (foffset == 0 && toffset == 0 && max_bytes == UINT64_MAX)
127 r = btrfs_reflink(fdf, fdt); /* full file reflink */
128 else
129 r = btrfs_clone_range(fdf, foffset, fdt, toffset, max_bytes == UINT64_MAX ? 0 : max_bytes); /* partial reflink */
130 if (r >= 0) {
131 off_t t;
132
133 /* This worked, yay! Now — to be fully correct — let's adjust the file pointers */
134 if (max_bytes == UINT64_MAX) {
135
136 /* We cloned to the end of the source file, let's position the read
137 * pointer there, and query it at the same time. */
138 t = lseek(fdf, 0, SEEK_END);
139 if (t < 0)
140 return -errno;
141 if (t < foffset)
142 return -ESPIPE;
143
144 /* Let's adjust the destination file write pointer by the same number
145 * of bytes. */
146 t = lseek(fdt, toffset + (t - foffset), SEEK_SET);
147 if (t < 0)
148 return -errno;
149
150 return 0; /* we copied the whole thing, hence hit EOF, return 0 */
151 } else {
152 t = lseek(fdf, foffset + max_bytes, SEEK_SET);
153 if (t < 0)
154 return -errno;
155
156 t = lseek(fdt, toffset + max_bytes, SEEK_SET);
157 if (t < 0)
158 return -errno;
159
160 return 1; /* we copied only some number of bytes, which worked, but this means we didn't hit EOF, return 1 */
161 }
162 }
163
164 log_debug_errno(r, "Reflinking didn't work, falling back to non-reflink copying: %m");
165 }
166 }
167 }
168
169 for (;;) {
170 ssize_t n;
171
172 if (max_bytes <= 0)
173 return 1; /* return > 0 if we hit the max_bytes limit */
174
175 if (max_bytes != UINT64_MAX && m > max_bytes)
176 m = max_bytes;
177
178 /* First try copy_file_range(), unless we already tried */
179 if (try_cfr) {
180 n = try_copy_file_range(fdf, NULL, fdt, NULL, m, 0u);
181 if (n < 0) {
182 if (!IN_SET(n, -EINVAL, -ENOSYS, -EXDEV, -EBADF))
183 return n;
184
185 try_cfr = false;
186 /* use fallback below */
187 } else if (n == 0) /* EOF */
188 break;
189 else
190 /* Success! */
191 goto next;
192 }
193
194 /* First try sendfile(), unless we already tried */
195 if (try_sendfile) {
196 n = sendfile(fdt, fdf, NULL, m);
197 if (n < 0) {
198 if (!IN_SET(errno, EINVAL, ENOSYS))
199 return -errno;
200
201 try_sendfile = false;
202 /* use fallback below */
203 } else if (n == 0) /* EOF */
204 break;
205 else
206 /* Success! */
207 goto next;
208 }
209
210 /* Then try splice, unless we already tried. */
211 if (try_splice) {
212
213 /* splice()'s asynchronous I/O support is a bit weird. When it encounters a pipe file
214 * descriptor, then it will ignore its O_NONBLOCK flag and instead only honour the
215 * SPLICE_F_NONBLOCK flag specified in its flag parameter. Let's hide this behaviour here, and
216 * check if either of the specified fds are a pipe, and if so, let's pass the flag
217 * automatically, depending on O_NONBLOCK being set.
218 *
219 * Here's a twist though: when we use it to move data between two pipes of which one has
220 * O_NONBLOCK set and the other has not, then we have no individual control over O_NONBLOCK
221 * behaviour. Hence in that case we can't use splice() and still guarantee systematic
222 * O_NONBLOCK behaviour, hence don't. */
223
224 if (nonblock_pipe < 0) {
225 int a, b;
226
227 /* Check if either of these fds is a pipe, and if so non-blocking or not */
228 a = fd_is_nonblock_pipe(fdf);
229 if (a < 0)
230 return a;
231
232 b = fd_is_nonblock_pipe(fdt);
233 if (b < 0)
234 return b;
235
236 if ((a == FD_IS_NO_PIPE && b == FD_IS_NO_PIPE) ||
237 (a == FD_IS_BLOCKING_PIPE && b == FD_IS_NONBLOCKING_PIPE) ||
238 (a == FD_IS_NONBLOCKING_PIPE && b == FD_IS_BLOCKING_PIPE))
239
240 /* splice() only works if one of the fds is a pipe. If neither is, let's skip
241 * this step right-away. As mentioned above, if one of the two fds refers to a
242 * blocking pipe and the other to a non-blocking pipe, we can't use splice()
243 * either, hence don't try either. This hence means we can only use splice() if
244 * either only one of the two fds is a pipe, or if both are pipes with the same
245 * nonblocking flag setting. */
246
247 try_splice = false;
248 else
249 nonblock_pipe = a == FD_IS_NONBLOCKING_PIPE || b == FD_IS_NONBLOCKING_PIPE;
250 }
251 }
252
253 if (try_splice) {
254 n = splice(fdf, NULL, fdt, NULL, m, nonblock_pipe ? SPLICE_F_NONBLOCK : 0);
255 if (n < 0) {
256 if (!IN_SET(errno, EINVAL, ENOSYS))
257 return -errno;
258
259 try_splice = false;
260 /* use fallback below */
261 } else if (n == 0) /* EOF */
262 break;
263 else
264 /* Success! */
265 goto next;
266 }
267
268 /* As a fallback just copy bits by hand */
269 {
270 uint8_t buf[MIN(m, COPY_BUFFER_SIZE)], *p = buf;
271 ssize_t z;
272
273 n = read(fdf, buf, sizeof buf);
274 if (n < 0)
275 return -errno;
276 if (n == 0) /* EOF */
277 break;
278
279 z = (size_t) n;
280 do {
281 ssize_t k;
282
283 k = write(fdt, p, z);
284 if (k < 0) {
285 r = -errno;
286
287 if (ret_remains) {
288 void *copy;
289
290 copy = memdup(p, z);
291 if (!copy)
292 return -ENOMEM;
293
294 *ret_remains = copy;
295 }
296
297 if (ret_remains_size)
298 *ret_remains_size = z;
299
300 return r;
301 }
302
303 assert(k <= z);
304 z -= k;
305 p += k;
306 } while (z > 0);
307 }
308
309 next:
310 if (max_bytes != (uint64_t) -1) {
311 assert(max_bytes >= (uint64_t) n);
312 max_bytes -= n;
313 }
314 /* sendfile accepts at most SSIZE_MAX-offset bytes to copy,
315 * so reduce our maximum by the amount we already copied,
316 * but don't go below our copy buffer size, unless we are
317 * close the limit of bytes we are allowed to copy. */
318 m = MAX(MIN(COPY_BUFFER_SIZE, max_bytes), m - n);
319 }
320
321 return 0; /* return 0 if we hit EOF earlier than the size limit */
322 }
323
324 static int fd_copy_symlink(
325 int df,
326 const char *from,
327 const struct stat *st,
328 int dt,
329 const char *to,
330 uid_t override_uid,
331 gid_t override_gid,
332 CopyFlags copy_flags) {
333
334 _cleanup_free_ char *target = NULL;
335 int r;
336
337 assert(from);
338 assert(st);
339 assert(to);
340
341 r = readlinkat_malloc(df, from, &target);
342 if (r < 0)
343 return r;
344
345 if (symlinkat(target, dt, to) < 0)
346 return -errno;
347
348 if (fchownat(dt, to,
349 uid_is_valid(override_uid) ? override_uid : st->st_uid,
350 gid_is_valid(override_gid) ? override_gid : st->st_gid,
351 AT_SYMLINK_NOFOLLOW) < 0)
352 return -errno;
353
354 return 0;
355 }
356
357 static int fd_copy_regular(
358 int df,
359 const char *from,
360 const struct stat *st,
361 int dt,
362 const char *to,
363 uid_t override_uid,
364 gid_t override_gid,
365 CopyFlags copy_flags) {
366
367 _cleanup_close_ int fdf = -1, fdt = -1;
368 struct timespec ts[2];
369 int r, q;
370
371 assert(from);
372 assert(st);
373 assert(to);
374
375 fdf = openat(df, from, O_RDONLY|O_CLOEXEC|O_NOCTTY|O_NOFOLLOW);
376 if (fdf < 0)
377 return -errno;
378
379 fdt = openat(dt, to, O_WRONLY|O_CREAT|O_EXCL|O_CLOEXEC|O_NOCTTY|O_NOFOLLOW, st->st_mode & 07777);
380 if (fdt < 0)
381 return -errno;
382
383 r = copy_bytes(fdf, fdt, (uint64_t) -1, copy_flags);
384 if (r < 0) {
385 (void) unlinkat(dt, to, 0);
386 return r;
387 }
388
389 if (fchown(fdt,
390 uid_is_valid(override_uid) ? override_uid : st->st_uid,
391 gid_is_valid(override_gid) ? override_gid : st->st_gid) < 0)
392 r = -errno;
393
394 if (fchmod(fdt, st->st_mode & 07777) < 0)
395 r = -errno;
396
397 ts[0] = st->st_atim;
398 ts[1] = st->st_mtim;
399 (void) futimens(fdt, ts);
400 (void) copy_xattr(fdf, fdt);
401
402 q = close(fdt);
403 fdt = -1;
404
405 if (q < 0) {
406 r = -errno;
407 (void) unlinkat(dt, to, 0);
408 }
409
410 return r;
411 }
412
413 static int fd_copy_fifo(
414 int df,
415 const char *from,
416 const struct stat *st,
417 int dt,
418 const char *to,
419 uid_t override_uid,
420 gid_t override_gid,
421 CopyFlags copy_flags) {
422 int r;
423
424 assert(from);
425 assert(st);
426 assert(to);
427
428 r = mkfifoat(dt, to, st->st_mode & 07777);
429 if (r < 0)
430 return -errno;
431
432 if (fchownat(dt, to,
433 uid_is_valid(override_uid) ? override_uid : st->st_uid,
434 gid_is_valid(override_gid) ? override_gid : st->st_gid,
435 AT_SYMLINK_NOFOLLOW) < 0)
436 r = -errno;
437
438 if (fchmodat(dt, to, st->st_mode & 07777, 0) < 0)
439 r = -errno;
440
441 return r;
442 }
443
444 static int fd_copy_node(
445 int df,
446 const char *from,
447 const struct stat *st,
448 int dt,
449 const char *to,
450 uid_t override_uid,
451 gid_t override_gid,
452 CopyFlags copy_flags) {
453 int r;
454
455 assert(from);
456 assert(st);
457 assert(to);
458
459 r = mknodat(dt, to, st->st_mode, st->st_rdev);
460 if (r < 0)
461 return -errno;
462
463 if (fchownat(dt, to,
464 uid_is_valid(override_uid) ? override_uid : st->st_uid,
465 gid_is_valid(override_gid) ? override_gid : st->st_gid,
466 AT_SYMLINK_NOFOLLOW) < 0)
467 r = -errno;
468
469 if (fchmodat(dt, to, st->st_mode & 07777, 0) < 0)
470 r = -errno;
471
472 return r;
473 }
474
475 static int fd_copy_directory(
476 int df,
477 const char *from,
478 const struct stat *st,
479 int dt,
480 const char *to,
481 dev_t original_device,
482 uid_t override_uid,
483 gid_t override_gid,
484 CopyFlags copy_flags) {
485
486 _cleanup_close_ int fdf = -1, fdt = -1;
487 _cleanup_closedir_ DIR *d = NULL;
488 struct dirent *de;
489 bool created;
490 int r;
491
492 assert(st);
493 assert(to);
494
495 if (from)
496 fdf = openat(df, from, O_RDONLY|O_DIRECTORY|O_CLOEXEC|O_NOCTTY|O_NOFOLLOW);
497 else
498 fdf = fcntl(df, F_DUPFD_CLOEXEC, 3);
499 if (fdf < 0)
500 return -errno;
501
502 d = fdopendir(fdf);
503 if (!d)
504 return -errno;
505 fdf = -1;
506
507 r = mkdirat(dt, to, st->st_mode & 07777);
508 if (r >= 0)
509 created = true;
510 else if (errno == EEXIST && (copy_flags & COPY_MERGE))
511 created = false;
512 else
513 return -errno;
514
515 fdt = openat(dt, to, O_RDONLY|O_DIRECTORY|O_CLOEXEC|O_NOCTTY|O_NOFOLLOW);
516 if (fdt < 0)
517 return -errno;
518
519 r = 0;
520
521 FOREACH_DIRENT_ALL(de, d, return -errno) {
522 struct stat buf;
523 int q;
524
525 if (dot_or_dot_dot(de->d_name))
526 continue;
527
528 if (fstatat(dirfd(d), de->d_name, &buf, AT_SYMLINK_NOFOLLOW) < 0) {
529 r = -errno;
530 continue;
531 }
532
533 if (buf.st_dev != original_device)
534 continue;
535
536 if (S_ISREG(buf.st_mode))
537 q = fd_copy_regular(dirfd(d), de->d_name, &buf, fdt, de->d_name, override_uid, override_gid, copy_flags);
538 else if (S_ISDIR(buf.st_mode))
539 q = fd_copy_directory(dirfd(d), de->d_name, &buf, fdt, de->d_name, original_device, override_uid, override_gid, copy_flags);
540 else if (S_ISLNK(buf.st_mode))
541 q = fd_copy_symlink(dirfd(d), de->d_name, &buf, fdt, de->d_name, override_uid, override_gid, copy_flags);
542 else if (S_ISFIFO(buf.st_mode))
543 q = fd_copy_fifo(dirfd(d), de->d_name, &buf, fdt, de->d_name, override_uid, override_gid, copy_flags);
544 else if (S_ISBLK(buf.st_mode) || S_ISCHR(buf.st_mode) || S_ISSOCK(buf.st_mode))
545 q = fd_copy_node(dirfd(d), de->d_name, &buf, fdt, de->d_name, override_uid, override_gid, copy_flags);
546 else
547 q = -EOPNOTSUPP;
548
549 if (q == -EEXIST && (copy_flags & COPY_MERGE))
550 q = 0;
551
552 if (q < 0)
553 r = q;
554 }
555
556 if (created) {
557 struct timespec ut[2] = {
558 st->st_atim,
559 st->st_mtim
560 };
561
562 if (fchown(fdt,
563 uid_is_valid(override_uid) ? override_uid : st->st_uid,
564 gid_is_valid(override_gid) ? override_gid : st->st_gid) < 0)
565 r = -errno;
566
567 if (fchmod(fdt, st->st_mode & 07777) < 0)
568 r = -errno;
569
570 (void) copy_xattr(dirfd(d), fdt);
571 (void) futimens(fdt, ut);
572 }
573
574 return r;
575 }
576
577 int copy_tree_at(int fdf, const char *from, int fdt, const char *to, uid_t override_uid, gid_t override_gid, CopyFlags copy_flags) {
578 struct stat st;
579
580 assert(from);
581 assert(to);
582
583 if (fstatat(fdf, from, &st, AT_SYMLINK_NOFOLLOW) < 0)
584 return -errno;
585
586 if (S_ISREG(st.st_mode))
587 return fd_copy_regular(fdf, from, &st, fdt, to, override_uid, override_gid, copy_flags);
588 else if (S_ISDIR(st.st_mode))
589 return fd_copy_directory(fdf, from, &st, fdt, to, st.st_dev, override_uid, override_gid, copy_flags);
590 else if (S_ISLNK(st.st_mode))
591 return fd_copy_symlink(fdf, from, &st, fdt, to, override_uid, override_gid, copy_flags);
592 else if (S_ISFIFO(st.st_mode))
593 return fd_copy_fifo(fdf, from, &st, fdt, to, override_uid, override_gid, copy_flags);
594 else if (S_ISBLK(st.st_mode) || S_ISCHR(st.st_mode) || S_ISSOCK(st.st_mode))
595 return fd_copy_node(fdf, from, &st, fdt, to, override_uid, override_gid, copy_flags);
596 else
597 return -EOPNOTSUPP;
598 }
599
600 int copy_tree(const char *from, const char *to, uid_t override_uid, gid_t override_gid, CopyFlags copy_flags) {
601 return copy_tree_at(AT_FDCWD, from, AT_FDCWD, to, override_uid, override_gid, copy_flags);
602 }
603
604 int copy_directory_fd(int dirfd, const char *to, CopyFlags copy_flags) {
605 struct stat st;
606
607 assert(dirfd >= 0);
608 assert(to);
609
610 if (fstat(dirfd, &st) < 0)
611 return -errno;
612
613 if (!S_ISDIR(st.st_mode))
614 return -ENOTDIR;
615
616 return fd_copy_directory(dirfd, NULL, &st, AT_FDCWD, to, st.st_dev, UID_INVALID, GID_INVALID, copy_flags);
617 }
618
619 int copy_directory(const char *from, const char *to, CopyFlags copy_flags) {
620 struct stat st;
621
622 assert(from);
623 assert(to);
624
625 if (lstat(from, &st) < 0)
626 return -errno;
627
628 if (!S_ISDIR(st.st_mode))
629 return -ENOTDIR;
630
631 return fd_copy_directory(AT_FDCWD, from, &st, AT_FDCWD, to, st.st_dev, UID_INVALID, GID_INVALID, copy_flags);
632 }
633
634 int copy_file_fd(const char *from, int fdt, CopyFlags copy_flags) {
635 _cleanup_close_ int fdf = -1;
636 int r;
637
638 assert(from);
639 assert(fdt >= 0);
640
641 fdf = open(from, O_RDONLY|O_CLOEXEC|O_NOCTTY);
642 if (fdf < 0)
643 return -errno;
644
645 r = copy_bytes(fdf, fdt, (uint64_t) -1, copy_flags);
646
647 (void) copy_times(fdf, fdt);
648 (void) copy_xattr(fdf, fdt);
649
650 return r;
651 }
652
653 int copy_file(const char *from, const char *to, int flags, mode_t mode, unsigned chattr_flags, CopyFlags copy_flags) {
654 int fdt = -1, r;
655
656 assert(from);
657 assert(to);
658
659 RUN_WITH_UMASK(0000) {
660 fdt = open(to, flags|O_WRONLY|O_CREAT|O_CLOEXEC|O_NOCTTY, mode);
661 if (fdt < 0)
662 return -errno;
663 }
664
665 if (chattr_flags != 0)
666 (void) chattr_fd(fdt, chattr_flags, (unsigned) -1);
667
668 r = copy_file_fd(from, fdt, copy_flags);
669 if (r < 0) {
670 close(fdt);
671 (void) unlink(to);
672 return r;
673 }
674
675 if (close(fdt) < 0) {
676 unlink_noerrno(to);
677 return -errno;
678 }
679
680 return 0;
681 }
682
683 int copy_file_atomic(const char *from, const char *to, mode_t mode, unsigned chattr_flags, CopyFlags copy_flags) {
684 _cleanup_free_ char *t = NULL;
685 int r;
686
687 assert(from);
688 assert(to);
689
690 r = tempfn_random(to, NULL, &t);
691 if (r < 0)
692 return r;
693
694 r = copy_file(from, t, O_NOFOLLOW|O_EXCL, mode, chattr_flags, copy_flags);
695 if (r < 0)
696 return r;
697
698 if (copy_flags & COPY_REPLACE) {
699 r = renameat(AT_FDCWD, t, AT_FDCWD, to);
700 if (r < 0)
701 r = -errno;
702 } else
703 r = rename_noreplace(AT_FDCWD, t, AT_FDCWD, to);
704 if (r < 0) {
705 (void) unlink(t);
706 return r;
707 }
708
709 return 0;
710 }
711
712 int copy_times(int fdf, int fdt) {
713 struct timespec ut[2];
714 struct stat st;
715 usec_t crtime = 0;
716
717 assert(fdf >= 0);
718 assert(fdt >= 0);
719
720 if (fstat(fdf, &st) < 0)
721 return -errno;
722
723 ut[0] = st.st_atim;
724 ut[1] = st.st_mtim;
725
726 if (futimens(fdt, ut) < 0)
727 return -errno;
728
729 if (fd_getcrtime(fdf, &crtime) >= 0)
730 (void) fd_setcrtime(fdt, crtime);
731
732 return 0;
733 }
734
735 int copy_xattr(int fdf, int fdt) {
736 _cleanup_free_ char *bufa = NULL, *bufb = NULL;
737 size_t sza = 100, szb = 100;
738 ssize_t n;
739 int ret = 0;
740 const char *p;
741
742 for (;;) {
743 bufa = malloc(sza);
744 if (!bufa)
745 return -ENOMEM;
746
747 n = flistxattr(fdf, bufa, sza);
748 if (n == 0)
749 return 0;
750 if (n > 0)
751 break;
752 if (errno != ERANGE)
753 return -errno;
754
755 sza *= 2;
756
757 bufa = mfree(bufa);
758 }
759
760 p = bufa;
761 while (n > 0) {
762 size_t l;
763
764 l = strlen(p);
765 assert(l < (size_t) n);
766
767 if (startswith(p, "user.")) {
768 ssize_t m;
769
770 if (!bufb) {
771 bufb = malloc(szb);
772 if (!bufb)
773 return -ENOMEM;
774 }
775
776 m = fgetxattr(fdf, p, bufb, szb);
777 if (m < 0) {
778 if (errno == ERANGE) {
779 szb *= 2;
780 bufb = mfree(bufb);
781 continue;
782 }
783
784 return -errno;
785 }
786
787 if (fsetxattr(fdt, p, bufb, m, 0) < 0)
788 ret = -errno;
789 }
790
791 p += l + 1;
792 n -= l + 1;
793 }
794
795 return ret;
796 }