]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/basic/copy.c
copy: return the right error when we can't open a file
[thirdparty/systemd.git] / src / basic / copy.c
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
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>
27 #include <sys/sendfile.h>
28 #include <sys/stat.h>
29 #include <sys/xattr.h>
30 #include <time.h>
31 #include <unistd.h>
32
33 #include "alloc-util.h"
34 #include "btrfs-util.h"
35 #include "chattr-util.h"
36 #include "copy.h"
37 #include "dirent-util.h"
38 #include "fd-util.h"
39 #include "fileio.h"
40 #include "fs-util.h"
41 #include "io-util.h"
42 #include "macro.h"
43 #include "missing.h"
44 #include "string-util.h"
45 #include "strv.h"
46 #include "time-util.h"
47 #include "umask-util.h"
48 #include "xattr-util.h"
49
50 #define COPY_BUFFER_SIZE (16*1024u)
51
52 static 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
71 int copy_bytes(int fdf, int fdt, uint64_t max_bytes, bool try_reflink) {
72 bool try_cfr = true, try_sendfile = true, try_splice = true;
73 int r;
74 size_t m = SSIZE_MAX; /* that is the maximum that sendfile and c_f_r accept */
75
76 assert(fdf >= 0);
77 assert(fdt >= 0);
78
79 /* Try btrfs reflinks first. */
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
85 r = btrfs_reflink(fdf, fdt);
86 if (r >= 0)
87 return 0; /* we copied the whole thing, hence hit EOF, return 0 */
88 }
89
90 for (;;) {
91 ssize_t n;
92
93 if (max_bytes != (uint64_t) -1) {
94 if (max_bytes <= 0)
95 return 1; /* return > 0 if we hit the max_bytes limit */
96
97 if (m > max_bytes)
98 m = max_bytes;
99 }
100
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) {
105 if (!IN_SET(n, -EINVAL, -ENOSYS, -EXDEV, -EBADF))
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
117 /* First try sendfile(), unless we already tried */
118 if (try_sendfile) {
119 n = sendfile(fdt, fdf, NULL, m);
120 if (n < 0) {
121 if (!IN_SET(errno, EINVAL, ENOSYS))
122 return -errno;
123
124 try_sendfile = false;
125 /* use fallback below */
126 } else if (n == 0) /* EOF */
127 break;
128 else
129 /* Success! */
130 goto next;
131 }
132
133 /* Then try splice, unless we already tried */
134 if (try_splice) {
135 n = splice(fdf, NULL, fdt, NULL, m, 0);
136 if (n < 0) {
137 if (!IN_SET(errno, EINVAL, ENOSYS))
138 return -errno;
139
140 try_splice = false;
141 /* use fallback below */
142 } else if (n == 0) /* EOF */
143 break;
144 else
145 /* Success! */
146 goto next;
147 }
148
149 /* As a fallback just copy bits by hand */
150 {
151 uint8_t buf[MIN(m, COPY_BUFFER_SIZE)];
152
153 n = read(fdf, buf, sizeof buf);
154 if (n < 0)
155 return -errno;
156 if (n == 0) /* EOF */
157 break;
158
159 r = loop_write(fdt, buf, (size_t) n, false);
160 if (r < 0)
161 return r;
162 }
163
164 next:
165 if (max_bytes != (uint64_t) -1) {
166 assert(max_bytes >= (uint64_t) n);
167 max_bytes -= n;
168 }
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);
174 }
175
176 return 0; /* return 0 if we hit EOF earlier than the size limit */
177 }
178
179 static 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
191 if (symlinkat(target, dt, to) < 0)
192 return -errno;
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
200 static 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;
202 struct timespec ts[2];
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);
214 if (fdt < 0)
215 return -errno;
216
217 r = copy_bytes(fdf, fdt, (uint64_t) -1, true);
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
229 ts[0] = st->st_atim;
230 ts[1] = st->st_mtim;
231 (void) futimens(fdt, ts);
232
233 (void) copy_xattr(fdf, fdt);
234
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
246 static 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);
254 if (r < 0)
255 return -errno;
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
266 static 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);
274 if (r < 0)
275 return -errno;
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
286 static 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
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
301 assert(st);
302 assert(to);
303
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);
308 if (fdf < 0)
309 return -errno;
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;
319 else if (errno == EEXIST && merge)
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
328 r = 0;
329
330 if (created) {
331 struct timespec ut[2] = {
332 st->st_atim,
333 st->st_mtim
334 };
335
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;
341
342 (void) futimens(fdt, ut);
343 (void) copy_xattr(dirfd(d), fdt);
344 }
345
346 FOREACH_DIRENT_ALL(de, d, return -errno) {
347 struct stat buf;
348 int q;
349
350 if (STR_IN_SET(de->d_name, ".", ".."))
351 continue;
352
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))
364 q = fd_copy_directory(dirfd(d), de->d_name, &buf, fdt, de->d_name, original_device, merge);
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);
369 else if (S_ISBLK(buf.st_mode) || S_ISCHR(buf.st_mode) || S_ISSOCK(buf.st_mode))
370 q = fd_copy_node(dirfd(d), de->d_name, &buf, fdt, de->d_name);
371 else
372 q = -EOPNOTSUPP;
373
374 if (q == -EEXIST && merge)
375 q = 0;
376
377 if (q < 0)
378 r = q;
379 }
380
381 return r;
382 }
383
384 int copy_tree_at(int fdf, const char *from, int fdt, const char *to, bool merge) {
385 struct stat st;
386
387 assert(from);
388 assert(to);
389
390 if (fstatat(fdf, from, &st, AT_SYMLINK_NOFOLLOW) < 0)
391 return -errno;
392
393 if (S_ISREG(st.st_mode))
394 return fd_copy_regular(fdf, from, &st, fdt, to);
395 else if (S_ISDIR(st.st_mode))
396 return fd_copy_directory(fdf, from, &st, fdt, to, st.st_dev, merge);
397 else if (S_ISLNK(st.st_mode))
398 return fd_copy_symlink(fdf, from, &st, fdt, to);
399 else if (S_ISFIFO(st.st_mode))
400 return fd_copy_fifo(fdf, from, &st, fdt, to);
401 else if (S_ISBLK(st.st_mode) || S_ISCHR(st.st_mode) || S_ISSOCK(st.st_mode))
402 return fd_copy_node(fdf, from, &st, fdt, to);
403 else
404 return -EOPNOTSUPP;
405 }
406
407 int copy_tree(const char *from, const char *to, bool merge) {
408 return copy_tree_at(AT_FDCWD, from, AT_FDCWD, to, merge);
409 }
410
411 int copy_directory_fd(int dirfd, const char *to, bool merge) {
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
427 int copy_file_fd(const char *from, int fdt, bool try_reflink) {
428 _cleanup_close_ int fdf = -1;
429 int r;
430
431 assert(from);
432 assert(fdt >= 0);
433
434 fdf = open(from, O_RDONLY|O_CLOEXEC|O_NOCTTY);
435 if (fdf < 0)
436 return -errno;
437
438 r = copy_bytes(fdf, fdt, (uint64_t) -1, try_reflink);
439
440 (void) copy_times(fdf, fdt);
441 (void) copy_xattr(fdf, fdt);
442
443 return r;
444 }
445
446 int copy_file(const char *from, const char *to, int flags, mode_t mode, unsigned chattr_flags) {
447 int fdt = -1, r;
448
449 assert(from);
450 assert(to);
451
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 }
457
458 if (chattr_flags != 0)
459 (void) chattr_fd(fdt, chattr_flags, (unsigned) -1);
460
461 r = copy_file_fd(from, fdt, true);
462 if (r < 0) {
463 close(fdt);
464 unlink(to);
465 return r;
466 }
467
468 if (close(fdt) < 0) {
469 unlink_noerrno(to);
470 return -errno;
471 }
472
473 return 0;
474 }
475
476 int copy_file_atomic(const char *from, const char *to, mode_t mode, bool replace, unsigned chattr_flags) {
477 _cleanup_free_ char *t = NULL;
478 int r;
479
480 assert(from);
481 assert(to);
482
483 r = tempfn_random(to, NULL, &t);
484 if (r < 0)
485 return r;
486
487 r = copy_file(from, t, O_NOFOLLOW|O_EXCL, mode, chattr_flags);
488 if (r < 0)
489 return r;
490
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;
500 }
501
502 return 0;
503 }
504
505 int copy_times(int fdf, int fdt) {
506 struct timespec ut[2];
507 struct stat st;
508 usec_t crtime = 0;
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
528 int 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
550 bufa = mfree(bufa);
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;
573 bufb = mfree(bufb);
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 }