]> git.ipfire.org Git - thirdparty/systemd.git/blame - 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
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
61233823 172 * close the limit of bytes we are allowed to copy. */
00a8cf77 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
8420fa3a 330 FOREACH_DIRENT_ALL(de, d, return -errno) {
849958d1
LP
331 struct stat buf;
332 int q;
333
49bfc877 334 if (dot_or_dot_dot(de->d_name))
8420fa3a
LP
335 continue;
336
849958d1
LP
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))
e156347e 348 q = fd_copy_directory(dirfd(d), de->d_name, &buf, fdt, de->d_name, original_device, merge);
849958d1
LP
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);
0e2b2cac 353 else if (S_ISBLK(buf.st_mode) || S_ISCHR(buf.st_mode) || S_ISSOCK(buf.st_mode))
849958d1
LP
354 q = fd_copy_node(dirfd(d), de->d_name, &buf, fdt, de->d_name);
355 else
15411c0c 356 q = -EOPNOTSUPP;
849958d1 357
e156347e
LP
358 if (q == -EEXIST && merge)
359 q = 0;
360
849958d1
LP
361 if (q < 0)
362 r = q;
363 }
364
3b8483c0
LP
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
849958d1
LP
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 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
9a50e3ca
LP
426int 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
7430ec6a 441int copy_file_fd(const char *from, int fdt, bool try_reflink) {
cda134ab 442 _cleanup_close_ int fdf = -1;
e6bd041c 443 int r;
849958d1
LP
444
445 assert(from);
cda134ab 446 assert(fdt >= 0);
849958d1
LP
447
448 fdf = open(from, O_RDONLY|O_CLOEXEC|O_NOCTTY);
449 if (fdf < 0)
450 return -errno;
451
59f448cf 452 r = copy_bytes(fdf, fdt, (uint64_t) -1, try_reflink);
e6bd041c
LP
453
454 (void) copy_times(fdf, fdt);
455 (void) copy_xattr(fdf, fdt);
456
457 return r;
cda134ab
LP
458}
459
45030287 460int copy_file(const char *from, const char *to, int flags, mode_t mode, unsigned chattr_flags) {
a7f7d1bd 461 int fdt = -1, r;
cda134ab
LP
462
463 assert(from);
464 assert(to);
465
ebd93cb6
LP
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 }
849958d1 471
f2068bcc 472 if (chattr_flags != 0)
1ed8f8c1 473 (void) chattr_fd(fdt, chattr_flags, (unsigned) -1);
f2068bcc 474
7430ec6a 475 r = copy_file_fd(from, fdt, true);
849958d1 476 if (r < 0) {
cda134ab 477 close(fdt);
849958d1
LP
478 unlink(to);
479 return r;
480 }
481
cda134ab
LP
482 if (close(fdt) < 0) {
483 unlink_noerrno(to);
484 return -errno;
849958d1
LP
485 }
486
487 return 0;
488}
e6bd041c 489
45030287 490int copy_file_atomic(const char *from, const char *to, mode_t mode, bool replace, unsigned chattr_flags) {
a7f7d1bd 491 _cleanup_free_ char *t = NULL;
ebd93cb6
LP
492 int r;
493
494 assert(from);
495 assert(to);
496
14bcf25c 497 r = tempfn_random(to, NULL, &t);
ebd93cb6
LP
498 if (r < 0)
499 return r;
500
f2068bcc 501 r = copy_file(from, t, O_NOFOLLOW|O_EXCL, mode, chattr_flags);
ebd93cb6
LP
502 if (r < 0)
503 return r;
504
f85ef957
AC
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;
ebd93cb6
LP
514 }
515
516 return 0;
517}
518
e6bd041c
LP
519int copy_times(int fdf, int fdt) {
520 struct timespec ut[2];
521 struct stat st;
a7f7d1bd 522 usec_t crtime = 0;
e6bd041c
LP
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
542int 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
97b11eed 564 bufa = mfree(bufa);
e6bd041c
LP
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;
97b11eed 587 bufb = mfree(bufb);
e6bd041c
LP
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}