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