]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/basic/copy.c
fs-util: unify code we use to check if dirent's d_name is "." or ".."
[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 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 FOREACH_DIRENT_ALL(de, d, return -errno) {
331 struct stat buf;
332 int q;
333
334 if (dot_or_dot_dot(de->d_name))
335 continue;
336
337 if (fstatat(dirfd(d), de->d_name, &buf, AT_SYMLINK_NOFOLLOW) < 0) {
338 r = -errno;
339 continue;
340 }
341
342 if (buf.st_dev != original_device)
343 continue;
344
345 if (S_ISREG(buf.st_mode))
346 q = fd_copy_regular(dirfd(d), de->d_name, &buf, fdt, de->d_name);
347 else if (S_ISDIR(buf.st_mode))
348 q = fd_copy_directory(dirfd(d), de->d_name, &buf, fdt, de->d_name, original_device, merge);
349 else if (S_ISLNK(buf.st_mode))
350 q = fd_copy_symlink(dirfd(d), de->d_name, &buf, fdt, de->d_name);
351 else if (S_ISFIFO(buf.st_mode))
352 q = fd_copy_fifo(dirfd(d), de->d_name, &buf, fdt, de->d_name);
353 else if (S_ISBLK(buf.st_mode) || S_ISCHR(buf.st_mode) || S_ISSOCK(buf.st_mode))
354 q = fd_copy_node(dirfd(d), de->d_name, &buf, fdt, de->d_name);
355 else
356 q = -EOPNOTSUPP;
357
358 if (q == -EEXIST && merge)
359 q = 0;
360
361 if (q < 0)
362 r = q;
363 }
364
365 if (created) {
366 struct timespec ut[2] = {
367 st->st_atim,
368 st->st_mtim
369 };
370
371 if (fchown(fdt, st->st_uid, st->st_gid) < 0)
372 r = -errno;
373
374 if (fchmod(fdt, st->st_mode & 07777) < 0)
375 r = -errno;
376
377 (void) copy_xattr(dirfd(d), fdt);
378 (void) futimens(fdt, ut);
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 struct stat st;
413
414 assert(dirfd >= 0);
415 assert(to);
416
417 if (fstat(dirfd, &st) < 0)
418 return -errno;
419
420 if (!S_ISDIR(st.st_mode))
421 return -ENOTDIR;
422
423 return fd_copy_directory(dirfd, NULL, &st, AT_FDCWD, to, st.st_dev, merge);
424 }
425
426 int copy_directory(const char *from, const char *to, bool merge) {
427 struct stat st;
428
429 assert(from);
430 assert(to);
431
432 if (lstat(from, &st) < 0)
433 return -errno;
434
435 if (!S_ISDIR(st.st_mode))
436 return -ENOTDIR;
437
438 return fd_copy_directory(AT_FDCWD, from, &st, AT_FDCWD, to, st.st_dev, merge);
439 }
440
441 int copy_file_fd(const char *from, int fdt, bool try_reflink) {
442 _cleanup_close_ int fdf = -1;
443 int r;
444
445 assert(from);
446 assert(fdt >= 0);
447
448 fdf = open(from, O_RDONLY|O_CLOEXEC|O_NOCTTY);
449 if (fdf < 0)
450 return -errno;
451
452 r = copy_bytes(fdf, fdt, (uint64_t) -1, try_reflink);
453
454 (void) copy_times(fdf, fdt);
455 (void) copy_xattr(fdf, fdt);
456
457 return r;
458 }
459
460 int copy_file(const char *from, const char *to, int flags, mode_t mode, unsigned chattr_flags) {
461 int fdt = -1, r;
462
463 assert(from);
464 assert(to);
465
466 RUN_WITH_UMASK(0000) {
467 fdt = open(to, flags|O_WRONLY|O_CREAT|O_CLOEXEC|O_NOCTTY, mode);
468 if (fdt < 0)
469 return -errno;
470 }
471
472 if (chattr_flags != 0)
473 (void) chattr_fd(fdt, chattr_flags, (unsigned) -1);
474
475 r = copy_file_fd(from, fdt, true);
476 if (r < 0) {
477 close(fdt);
478 unlink(to);
479 return r;
480 }
481
482 if (close(fdt) < 0) {
483 unlink_noerrno(to);
484 return -errno;
485 }
486
487 return 0;
488 }
489
490 int copy_file_atomic(const char *from, const char *to, mode_t mode, bool replace, unsigned chattr_flags) {
491 _cleanup_free_ char *t = NULL;
492 int r;
493
494 assert(from);
495 assert(to);
496
497 r = tempfn_random(to, NULL, &t);
498 if (r < 0)
499 return r;
500
501 r = copy_file(from, t, O_NOFOLLOW|O_EXCL, mode, chattr_flags);
502 if (r < 0)
503 return r;
504
505 if (replace) {
506 r = renameat(AT_FDCWD, t, AT_FDCWD, to);
507 if (r < 0)
508 r = -errno;
509 } else
510 r = rename_noreplace(AT_FDCWD, t, AT_FDCWD, to);
511 if (r < 0) {
512 (void) unlink_noerrno(t);
513 return r;
514 }
515
516 return 0;
517 }
518
519 int copy_times(int fdf, int fdt) {
520 struct timespec ut[2];
521 struct stat st;
522 usec_t crtime = 0;
523
524 assert(fdf >= 0);
525 assert(fdt >= 0);
526
527 if (fstat(fdf, &st) < 0)
528 return -errno;
529
530 ut[0] = st.st_atim;
531 ut[1] = st.st_mtim;
532
533 if (futimens(fdt, ut) < 0)
534 return -errno;
535
536 if (fd_getcrtime(fdf, &crtime) >= 0)
537 (void) fd_setcrtime(fdt, crtime);
538
539 return 0;
540 }
541
542 int copy_xattr(int fdf, int fdt) {
543 _cleanup_free_ char *bufa = NULL, *bufb = NULL;
544 size_t sza = 100, szb = 100;
545 ssize_t n;
546 int ret = 0;
547 const char *p;
548
549 for (;;) {
550 bufa = malloc(sza);
551 if (!bufa)
552 return -ENOMEM;
553
554 n = flistxattr(fdf, bufa, sza);
555 if (n == 0)
556 return 0;
557 if (n > 0)
558 break;
559 if (errno != ERANGE)
560 return -errno;
561
562 sza *= 2;
563
564 bufa = mfree(bufa);
565 }
566
567 p = bufa;
568 while (n > 0) {
569 size_t l;
570
571 l = strlen(p);
572 assert(l < (size_t) n);
573
574 if (startswith(p, "user.")) {
575 ssize_t m;
576
577 if (!bufb) {
578 bufb = malloc(szb);
579 if (!bufb)
580 return -ENOMEM;
581 }
582
583 m = fgetxattr(fdf, p, bufb, szb);
584 if (m < 0) {
585 if (errno == ERANGE) {
586 szb *= 2;
587 bufb = mfree(bufb);
588 continue;
589 }
590
591 return -errno;
592 }
593
594 if (fsetxattr(fdt, p, bufb, m, 0) < 0)
595 ret = -errno;
596 }
597
598 p += l + 1;
599 n -= l + 1;
600 }
601
602 return ret;
603 }