]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/basic/copy.c
copy: return the right error when we can't open a file
[thirdparty/systemd.git] / src / basic / copy.c
CommitLineData
849958d1
LP
1/***
2 This file is part of systemd.
3
4 Copyright 2014 Lennart Poettering
5
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
10
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
18***/
19
11c3a366
TA
20#include <dirent.h>
21#include <errno.h>
22#include <fcntl.h>
23#include <stddef.h>
24#include <stdio.h>
25#include <stdlib.h>
26#include <string.h>
cda134ab 27#include <sys/sendfile.h>
11c3a366 28#include <sys/stat.h>
e6bd041c 29#include <sys/xattr.h>
11c3a366
TA
30#include <time.h>
31#include <unistd.h>
cda134ab 32
b5efdb8a 33#include "alloc-util.h"
d7c7c334 34#include "btrfs-util.h"
c8b3094d 35#include "chattr-util.h"
3ffd4af2 36#include "copy.h"
a0956174 37#include "dirent-util.h"
3ffd4af2 38#include "fd-util.h"
0d39fa9c 39#include "fileio.h"
f4f15635 40#include "fs-util.h"
c004493c 41#include "io-util.h"
11c3a366 42#include "macro.h"
a44202e9 43#include "missing.h"
07630cea 44#include "string-util.h"
8420fa3a 45#include "strv.h"
93cc7779 46#include "time-util.h"
affb60b1 47#include "umask-util.h"
89a5a90c 48#include "xattr-util.h"
849958d1 49
00a8cf77 50#define COPY_BUFFER_SIZE (16*1024u)
f2cbe59e 51
a44202e9
ZJS
52static ssize_t try_copy_file_range(int fd_in, loff_t *off_in,
53 int fd_out, loff_t *off_out,
54 size_t len,
55 unsigned int flags) {
56 static int have = -1;
57 ssize_t r;
58
59 if (have == false)
60 return -ENOSYS;
61
62 r = copy_file_range(fd_in, off_in, fd_out, off_out, len, flags);
63 if (_unlikely_(have < 0))
64 have = r >= 0 || errno != ENOSYS;
65 if (r >= 0)
66 return r;
67 else
68 return -errno;
69}
70
59f448cf 71int copy_bytes(int fdf, int fdt, uint64_t max_bytes, bool try_reflink) {
a44202e9 72 bool try_cfr = true, try_sendfile = true, try_splice = true;
0254b455 73 int r;
7c2da2ca 74 size_t m = SSIZE_MAX; /* that is the maximum that sendfile and c_f_r accept */
cda134ab 75
849958d1
LP
76 assert(fdf >= 0);
77 assert(fdt >= 0);
78
0254b455 79 /* Try btrfs reflinks first. */
c622fbdb
LP
80 if (try_reflink &&
81 max_bytes == (uint64_t) -1 &&
82 lseek(fdf, 0, SEEK_CUR) == 0 &&
83 lseek(fdt, 0, SEEK_CUR) == 0) {
84
0254b455
LP
85 r = btrfs_reflink(fdf, fdt);
86 if (r >= 0)
f6d9c616 87 return 0; /* we copied the whole thing, hence hit EOF, return 0 */
0254b455
LP
88 }
89
849958d1 90 for (;;) {
cda134ab 91 ssize_t n;
93240d3a 92
59f448cf 93 if (max_bytes != (uint64_t) -1) {
93240d3a 94 if (max_bytes <= 0)
f6d9c616 95 return 1; /* return > 0 if we hit the max_bytes limit */
93240d3a 96
d219849e
ZJS
97 if (m > max_bytes)
98 m = max_bytes;
93240d3a
LP
99 }
100
a44202e9
ZJS
101 /* First try copy_file_range(), unless we already tried */
102 if (try_cfr) {
103 n = try_copy_file_range(fdf, NULL, fdt, NULL, m, 0u);
104 if (n < 0) {
6402d5c6 105 if (!IN_SET(n, -EINVAL, -ENOSYS, -EXDEV, -EBADF))
a44202e9
ZJS
106 return n;
107
108 try_cfr = false;
109 /* use fallback below */
110 } else if (n == 0) /* EOF */
111 break;
112 else
113 /* Success! */
114 goto next;
115 }
116
cda134ab
LP
117 /* First try sendfile(), unless we already tried */
118 if (try_sendfile) {
cda134ab
LP
119 n = sendfile(fdt, fdf, NULL, m);
120 if (n < 0) {
00a8cf77 121 if (!IN_SET(errno, EINVAL, ENOSYS))
cda134ab
LP
122 return -errno;
123
124 try_sendfile = false;
125 /* use fallback below */
126 } else if (n == 0) /* EOF */
127 break;
00a8cf77 128 else
81d20007
LP
129 /* Success! */
130 goto next;
131 }
132
00a8cf77 133 /* Then try splice, unless we already tried */
81d20007 134 if (try_splice) {
f6d9c616 135 n = splice(fdf, NULL, fdt, NULL, m, 0);
81d20007 136 if (n < 0) {
00a8cf77 137 if (!IN_SET(errno, EINVAL, ENOSYS))
81d20007
LP
138 return -errno;
139
140 try_splice = false;
141 /* use fallback below */
142 } else if (n == 0) /* EOF */
143 break;
00a8cf77 144 else
81d20007 145 /* Success! */
cda134ab
LP
146 goto next;
147 }
148
149 /* As a fallback just copy bits by hand */
150 {
00a8cf77 151 uint8_t buf[MIN(m, COPY_BUFFER_SIZE)];
849958d1 152
00a8cf77 153 n = read(fdf, buf, sizeof buf);
cda134ab
LP
154 if (n < 0)
155 return -errno;
156 if (n == 0) /* EOF */
157 break;
158
0254b455 159 r = loop_write(fdt, buf, (size_t) n, false);
553acb7b
ZJS
160 if (r < 0)
161 return r;
cda134ab 162 }
93240d3a 163
cda134ab 164 next:
59f448cf
LP
165 if (max_bytes != (uint64_t) -1) {
166 assert(max_bytes >= (uint64_t) n);
93240d3a
LP
167 max_bytes -= n;
168 }
00a8cf77
ZJS
169 /* sendfile accepts at most SSIZE_MAX-offset bytes to copy,
170 * so reduce our maximum by the amount we already copied,
171 * but don't go below our copy buffer size, unless we are
172 * close the the limit of bytes we are allowed to copy. */
173 m = MAX(MIN(COPY_BUFFER_SIZE, max_bytes), m - n);
849958d1
LP
174 }
175
f6d9c616 176 return 0; /* return 0 if we hit EOF earlier than the size limit */
849958d1
LP
177}
178
179static int fd_copy_symlink(int df, const char *from, const struct stat *st, int dt, const char *to) {
180 _cleanup_free_ char *target = NULL;
181 int r;
182
183 assert(from);
184 assert(st);
185 assert(to);
186
187 r = readlinkat_malloc(df, from, &target);
188 if (r < 0)
189 return r;
190
e156347e 191 if (symlinkat(target, dt, to) < 0)
849958d1 192 return -errno;
849958d1
LP
193
194 if (fchownat(dt, to, st->st_uid, st->st_gid, AT_SYMLINK_NOFOLLOW) < 0)
195 return -errno;
196
197 return 0;
198}
199
200static int fd_copy_regular(int df, const char *from, const struct stat *st, int dt, const char *to) {
201 _cleanup_close_ int fdf = -1, fdt = -1;
ebd93cb6 202 struct timespec ts[2];
849958d1
LP
203 int r, q;
204
205 assert(from);
206 assert(st);
207 assert(to);
208
209 fdf = openat(df, from, O_RDONLY|O_CLOEXEC|O_NOCTTY|O_NOFOLLOW);
210 if (fdf < 0)
211 return -errno;
212
213 fdt = openat(dt, to, O_WRONLY|O_CREAT|O_EXCL|O_CLOEXEC|O_NOCTTY|O_NOFOLLOW, st->st_mode & 07777);
e156347e 214 if (fdt < 0)
849958d1 215 return -errno;
849958d1 216
59f448cf 217 r = copy_bytes(fdf, fdt, (uint64_t) -1, true);
849958d1
LP
218 if (r < 0) {
219 unlinkat(dt, to, 0);
220 return r;
221 }
222
223 if (fchown(fdt, st->st_uid, st->st_gid) < 0)
224 r = -errno;
225
226 if (fchmod(fdt, st->st_mode & 07777) < 0)
227 r = -errno;
228
ebd93cb6
LP
229 ts[0] = st->st_atim;
230 ts[1] = st->st_mtim;
231 (void) futimens(fdt, ts);
232
e6bd041c
LP
233 (void) copy_xattr(fdf, fdt);
234
849958d1
LP
235 q = close(fdt);
236 fdt = -1;
237
238 if (q < 0) {
239 r = -errno;
240 unlinkat(dt, to, 0);
241 }
242
243 return r;
244}
245
246static int fd_copy_fifo(int df, const char *from, const struct stat *st, int dt, const char *to) {
247 int r;
248
249 assert(from);
250 assert(st);
251 assert(to);
252
253 r = mkfifoat(dt, to, st->st_mode & 07777);
e156347e 254 if (r < 0)
849958d1 255 return -errno;
849958d1
LP
256
257 if (fchownat(dt, to, st->st_uid, st->st_gid, AT_SYMLINK_NOFOLLOW) < 0)
258 r = -errno;
259
260 if (fchmodat(dt, to, st->st_mode & 07777, 0) < 0)
261 r = -errno;
262
263 return r;
264}
265
266static int fd_copy_node(int df, const char *from, const struct stat *st, int dt, const char *to) {
267 int r;
268
269 assert(from);
270 assert(st);
271 assert(to);
272
273 r = mknodat(dt, to, st->st_mode, st->st_rdev);
e156347e 274 if (r < 0)
849958d1 275 return -errno;
849958d1
LP
276
277 if (fchownat(dt, to, st->st_uid, st->st_gid, AT_SYMLINK_NOFOLLOW) < 0)
278 r = -errno;
279
280 if (fchmodat(dt, to, st->st_mode & 07777, 0) < 0)
281 r = -errno;
282
283 return r;
284}
285
d7c7c334
LP
286static int fd_copy_directory(
287 int df,
288 const char *from,
289 const struct stat *st,
290 int dt,
291 const char *to,
292 dev_t original_device,
293 bool merge) {
294
849958d1
LP
295 _cleanup_close_ int fdf = -1, fdt = -1;
296 _cleanup_closedir_ DIR *d = NULL;
297 struct dirent *de;
298 bool created;
299 int r;
300
849958d1
LP
301 assert(st);
302 assert(to);
303
d7c7c334
LP
304 if (from)
305 fdf = openat(df, from, O_RDONLY|O_DIRECTORY|O_CLOEXEC|O_NOCTTY|O_NOFOLLOW);
306 else
307 fdf = fcntl(df, F_DUPFD_CLOEXEC, 3);
b498c53d
LP
308 if (fdf < 0)
309 return -errno;
849958d1
LP
310
311 d = fdopendir(fdf);
312 if (!d)
313 return -errno;
314 fdf = -1;
315
316 r = mkdirat(dt, to, st->st_mode & 07777);
317 if (r >= 0)
318 created = true;
e156347e 319 else if (errno == EEXIST && merge)
849958d1
LP
320 created = false;
321 else
322 return -errno;
323
324 fdt = openat(dt, to, O_RDONLY|O_DIRECTORY|O_CLOEXEC|O_NOCTTY|O_NOFOLLOW);
325 if (fdt < 0)
326 return -errno;
327
2c455af4
LP
328 r = 0;
329
849958d1 330 if (created) {
ebd93cb6
LP
331 struct timespec ut[2] = {
332 st->st_atim,
333 st->st_mtim
334 };
335
849958d1
LP
336 if (fchown(fdt, st->st_uid, st->st_gid) < 0)
337 r = -errno;
338
339 if (fchmod(fdt, st->st_mode & 07777) < 0)
340 r = -errno;
e6bd041c 341
ebd93cb6
LP
342 (void) futimens(fdt, ut);
343 (void) copy_xattr(dirfd(d), fdt);
849958d1
LP
344 }
345
8420fa3a 346 FOREACH_DIRENT_ALL(de, d, return -errno) {
849958d1
LP
347 struct stat buf;
348 int q;
349
8420fa3a
LP
350 if (STR_IN_SET(de->d_name, ".", ".."))
351 continue;
352
849958d1
LP
353 if (fstatat(dirfd(d), de->d_name, &buf, AT_SYMLINK_NOFOLLOW) < 0) {
354 r = -errno;
355 continue;
356 }
357
358 if (buf.st_dev != original_device)
359 continue;
360
361 if (S_ISREG(buf.st_mode))
362 q = fd_copy_regular(dirfd(d), de->d_name, &buf, fdt, de->d_name);
363 else if (S_ISDIR(buf.st_mode))
e156347e 364 q = fd_copy_directory(dirfd(d), de->d_name, &buf, fdt, de->d_name, original_device, merge);
849958d1
LP
365 else if (S_ISLNK(buf.st_mode))
366 q = fd_copy_symlink(dirfd(d), de->d_name, &buf, fdt, de->d_name);
367 else if (S_ISFIFO(buf.st_mode))
368 q = fd_copy_fifo(dirfd(d), de->d_name, &buf, fdt, de->d_name);
0e2b2cac 369 else if (S_ISBLK(buf.st_mode) || S_ISCHR(buf.st_mode) || S_ISSOCK(buf.st_mode))
849958d1
LP
370 q = fd_copy_node(dirfd(d), de->d_name, &buf, fdt, de->d_name);
371 else
15411c0c 372 q = -EOPNOTSUPP;
849958d1 373
e156347e
LP
374 if (q == -EEXIST && merge)
375 q = 0;
376
849958d1
LP
377 if (q < 0)
378 r = q;
379 }
380
381 return r;
382}
383
f2cbe59e 384int copy_tree_at(int fdf, const char *from, int fdt, const char *to, bool merge) {
849958d1
LP
385 struct stat st;
386
387 assert(from);
388 assert(to);
389
f2cbe59e 390 if (fstatat(fdf, from, &st, AT_SYMLINK_NOFOLLOW) < 0)
849958d1
LP
391 return -errno;
392
393 if (S_ISREG(st.st_mode))
f2cbe59e 394 return fd_copy_regular(fdf, from, &st, fdt, to);
849958d1 395 else if (S_ISDIR(st.st_mode))
f2cbe59e 396 return fd_copy_directory(fdf, from, &st, fdt, to, st.st_dev, merge);
849958d1 397 else if (S_ISLNK(st.st_mode))
f2cbe59e 398 return fd_copy_symlink(fdf, from, &st, fdt, to);
849958d1 399 else if (S_ISFIFO(st.st_mode))
f2cbe59e 400 return fd_copy_fifo(fdf, from, &st, fdt, to);
0e2b2cac 401 else if (S_ISBLK(st.st_mode) || S_ISCHR(st.st_mode) || S_ISSOCK(st.st_mode))
f2cbe59e 402 return fd_copy_node(fdf, from, &st, fdt, to);
849958d1 403 else
15411c0c 404 return -EOPNOTSUPP;
849958d1
LP
405}
406
f2cbe59e
LP
407int copy_tree(const char *from, const char *to, bool merge) {
408 return copy_tree_at(AT_FDCWD, from, AT_FDCWD, to, merge);
409}
410
411int copy_directory_fd(int dirfd, const char *to, bool merge) {
d7c7c334
LP
412
413 struct stat st;
414
415 assert(dirfd >= 0);
416 assert(to);
417
418 if (fstat(dirfd, &st) < 0)
419 return -errno;
420
421 if (!S_ISDIR(st.st_mode))
422 return -ENOTDIR;
423
424 return fd_copy_directory(dirfd, NULL, &st, AT_FDCWD, to, st.st_dev, merge);
425}
426
7430ec6a 427int copy_file_fd(const char *from, int fdt, bool try_reflink) {
cda134ab 428 _cleanup_close_ int fdf = -1;
e6bd041c 429 int r;
849958d1
LP
430
431 assert(from);
cda134ab 432 assert(fdt >= 0);
849958d1
LP
433
434 fdf = open(from, O_RDONLY|O_CLOEXEC|O_NOCTTY);
435 if (fdf < 0)
436 return -errno;
437
59f448cf 438 r = copy_bytes(fdf, fdt, (uint64_t) -1, try_reflink);
e6bd041c
LP
439
440 (void) copy_times(fdf, fdt);
441 (void) copy_xattr(fdf, fdt);
442
443 return r;
cda134ab
LP
444}
445
45030287 446int copy_file(const char *from, const char *to, int flags, mode_t mode, unsigned chattr_flags) {
a7f7d1bd 447 int fdt = -1, r;
cda134ab
LP
448
449 assert(from);
450 assert(to);
451
ebd93cb6
LP
452 RUN_WITH_UMASK(0000) {
453 fdt = open(to, flags|O_WRONLY|O_CREAT|O_CLOEXEC|O_NOCTTY, mode);
454 if (fdt < 0)
455 return -errno;
456 }
849958d1 457
f2068bcc 458 if (chattr_flags != 0)
1ed8f8c1 459 (void) chattr_fd(fdt, chattr_flags, (unsigned) -1);
f2068bcc 460
7430ec6a 461 r = copy_file_fd(from, fdt, true);
849958d1 462 if (r < 0) {
cda134ab 463 close(fdt);
849958d1
LP
464 unlink(to);
465 return r;
466 }
467
cda134ab
LP
468 if (close(fdt) < 0) {
469 unlink_noerrno(to);
470 return -errno;
849958d1
LP
471 }
472
473 return 0;
474}
e6bd041c 475
45030287 476int copy_file_atomic(const char *from, const char *to, mode_t mode, bool replace, unsigned chattr_flags) {
a7f7d1bd 477 _cleanup_free_ char *t = NULL;
ebd93cb6
LP
478 int r;
479
480 assert(from);
481 assert(to);
482
14bcf25c 483 r = tempfn_random(to, NULL, &t);
ebd93cb6
LP
484 if (r < 0)
485 return r;
486
f2068bcc 487 r = copy_file(from, t, O_NOFOLLOW|O_EXCL, mode, chattr_flags);
ebd93cb6
LP
488 if (r < 0)
489 return r;
490
f85ef957
AC
491 if (replace) {
492 r = renameat(AT_FDCWD, t, AT_FDCWD, to);
493 if (r < 0)
494 r = -errno;
495 } else
496 r = rename_noreplace(AT_FDCWD, t, AT_FDCWD, to);
497 if (r < 0) {
498 (void) unlink_noerrno(t);
499 return r;
ebd93cb6
LP
500 }
501
502 return 0;
503}
504
e6bd041c
LP
505int copy_times(int fdf, int fdt) {
506 struct timespec ut[2];
507 struct stat st;
a7f7d1bd 508 usec_t crtime = 0;
e6bd041c
LP
509
510 assert(fdf >= 0);
511 assert(fdt >= 0);
512
513 if (fstat(fdf, &st) < 0)
514 return -errno;
515
516 ut[0] = st.st_atim;
517 ut[1] = st.st_mtim;
518
519 if (futimens(fdt, ut) < 0)
520 return -errno;
521
522 if (fd_getcrtime(fdf, &crtime) >= 0)
523 (void) fd_setcrtime(fdt, crtime);
524
525 return 0;
526}
527
528int copy_xattr(int fdf, int fdt) {
529 _cleanup_free_ char *bufa = NULL, *bufb = NULL;
530 size_t sza = 100, szb = 100;
531 ssize_t n;
532 int ret = 0;
533 const char *p;
534
535 for (;;) {
536 bufa = malloc(sza);
537 if (!bufa)
538 return -ENOMEM;
539
540 n = flistxattr(fdf, bufa, sza);
541 if (n == 0)
542 return 0;
543 if (n > 0)
544 break;
545 if (errno != ERANGE)
546 return -errno;
547
548 sza *= 2;
549
97b11eed 550 bufa = mfree(bufa);
e6bd041c
LP
551 }
552
553 p = bufa;
554 while (n > 0) {
555 size_t l;
556
557 l = strlen(p);
558 assert(l < (size_t) n);
559
560 if (startswith(p, "user.")) {
561 ssize_t m;
562
563 if (!bufb) {
564 bufb = malloc(szb);
565 if (!bufb)
566 return -ENOMEM;
567 }
568
569 m = fgetxattr(fdf, p, bufb, szb);
570 if (m < 0) {
571 if (errno == ERANGE) {
572 szb *= 2;
97b11eed 573 bufb = mfree(bufb);
e6bd041c
LP
574 continue;
575 }
576
577 return -errno;
578 }
579
580 if (fsetxattr(fdt, p, bufb, m, 0) < 0)
581 ret = -errno;
582 }
583
584 p += l + 1;
585 n -= l + 1;
586 }
587
588 return ret;
589}