chiark / gitweb /
Apply updates from upstream
[elogind.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 "user-util.h"
49 //#include "xattr-util.h"
50
51 #define COPY_BUFFER_SIZE (16*1024u)
52
53 static ssize_t try_copy_file_range(int fd_in, loff_t *off_in,
54                                    int fd_out, loff_t *off_out,
55                                    size_t len,
56                                    unsigned int flags) {
57         static int have = -1;
58         ssize_t r;
59
60         if (have == false)
61                 return -ENOSYS;
62
63         r = copy_file_range(fd_in, off_in, fd_out, off_out, len, flags);
64         if (_unlikely_(have < 0))
65                 have = r >= 0 || errno != ENOSYS;
66         if (r >= 0)
67                 return r;
68         else
69                 return -errno;
70 }
71
72 int copy_bytes(int fdf, int fdt, uint64_t max_bytes, CopyFlags copy_flags) {
73         bool try_cfr = true, try_sendfile = true, try_splice = true;
74         int r;
75         size_t m = SSIZE_MAX; /* that is the maximum that sendfile and c_f_r accept */
76
77         assert(fdf >= 0);
78         assert(fdt >= 0);
79
80 #if 0 /// UNNEEDED by elogind
81         /* Try btrfs reflinks first. */
82         if ((copy_flags & COPY_REFLINK) &&
83             max_bytes == (uint64_t) -1 &&
84             lseek(fdf, 0, SEEK_CUR) == 0 &&
85             lseek(fdt, 0, SEEK_CUR) == 0) {
86
87                 r = btrfs_reflink(fdf, fdt);
88                 if (r >= 0)
89                         return 0; /* we copied the whole thing, hence hit EOF, return 0 */
90         }
91 #endif // 0
92
93         for (;;) {
94                 ssize_t n;
95
96                 if (max_bytes != (uint64_t) -1) {
97                         if (max_bytes <= 0)
98                                 return 1; /* return > 0 if we hit the max_bytes limit */
99
100                         if (m > max_bytes)
101                                 m = max_bytes;
102                 }
103
104                 /* First try copy_file_range(), unless we already tried */
105                 if (try_cfr) {
106                         n = try_copy_file_range(fdf, NULL, fdt, NULL, m, 0u);
107                         if (n < 0) {
108                                 if (!IN_SET(n, -EINVAL, -ENOSYS, -EXDEV, -EBADF))
109                                         return n;
110
111                                 try_cfr = false;
112                                 /* use fallback below */
113                         } else if (n == 0) /* EOF */
114                                 break;
115                         else
116                                 /* Success! */
117                                 goto next;
118                 }
119
120                 /* First try sendfile(), unless we already tried */
121                 if (try_sendfile) {
122                         n = sendfile(fdt, fdf, NULL, m);
123                         if (n < 0) {
124                                 if (!IN_SET(errno, EINVAL, ENOSYS))
125                                         return -errno;
126
127                                 try_sendfile = false;
128                                 /* use fallback below */
129                         } else if (n == 0) /* EOF */
130                                 break;
131                         else
132                                 /* Success! */
133                                 goto next;
134                 }
135
136                 /* Then try splice, unless we already tried */
137                 if (try_splice) {
138                         n = splice(fdf, NULL, fdt, NULL, m, 0);
139                         if (n < 0) {
140                                 if (!IN_SET(errno, EINVAL, ENOSYS))
141                                         return -errno;
142
143                                 try_splice = false;
144                                 /* use fallback below */
145                         } else if (n == 0) /* EOF */
146                                 break;
147                         else
148                                 /* Success! */
149                                 goto next;
150                 }
151
152                 /* As a fallback just copy bits by hand */
153                 {
154                         uint8_t buf[MIN(m, COPY_BUFFER_SIZE)];
155
156                         n = read(fdf, buf, sizeof buf);
157                         if (n < 0)
158                                 return -errno;
159                         if (n == 0) /* EOF */
160                                 break;
161
162                         r = loop_write(fdt, buf, (size_t) n, false);
163                         if (r < 0)
164                                 return r;
165                 }
166
167         next:
168                 if (max_bytes != (uint64_t) -1) {
169                         assert(max_bytes >= (uint64_t) n);
170                         max_bytes -= n;
171                 }
172                 /* sendfile accepts at most SSIZE_MAX-offset bytes to copy,
173                  * so reduce our maximum by the amount we already copied,
174                  * but don't go below our copy buffer size, unless we are
175                  * close the limit of bytes we are allowed to copy. */
176                 m = MAX(MIN(COPY_BUFFER_SIZE, max_bytes), m - n);
177         }
178
179         return 0; /* return 0 if we hit EOF earlier than the size limit */
180 }
181
182 #if 0 /// UNNEEDED by elogind
183 static int fd_copy_symlink(
184                 int df,
185                 const char *from,
186                 const struct stat *st,
187                 int dt,
188                 const char *to,
189                 uid_t override_uid,
190                 gid_t override_gid,
191                 CopyFlags copy_flags) {
192
193         _cleanup_free_ char *target = NULL;
194         int r;
195
196         assert(from);
197         assert(st);
198         assert(to);
199
200         r = readlinkat_malloc(df, from, &target);
201         if (r < 0)
202                 return r;
203
204         if (symlinkat(target, dt, to) < 0)
205                 return -errno;
206
207         if (fchownat(dt, to,
208                      uid_is_valid(override_uid) ? override_uid : st->st_uid,
209                      gid_is_valid(override_gid) ? override_gid : st->st_gid,
210                      AT_SYMLINK_NOFOLLOW) < 0)
211                 return -errno;
212
213         return 0;
214 }
215
216 static int fd_copy_regular(
217                 int df,
218                 const char *from,
219                 const struct stat *st,
220                 int dt,
221                 const char *to,
222                 uid_t override_uid,
223                 gid_t override_gid,
224                 CopyFlags copy_flags) {
225
226         _cleanup_close_ int fdf = -1, fdt = -1;
227         struct timespec ts[2];
228         int r, q;
229
230         assert(from);
231         assert(st);
232         assert(to);
233
234         fdf = openat(df, from, O_RDONLY|O_CLOEXEC|O_NOCTTY|O_NOFOLLOW);
235         if (fdf < 0)
236                 return -errno;
237
238         fdt = openat(dt, to, O_WRONLY|O_CREAT|O_EXCL|O_CLOEXEC|O_NOCTTY|O_NOFOLLOW, st->st_mode & 07777);
239         if (fdt < 0)
240                 return -errno;
241
242         r = copy_bytes(fdf, fdt, (uint64_t) -1, copy_flags);
243         if (r < 0) {
244                 unlinkat(dt, to, 0);
245                 return r;
246         }
247
248         if (fchown(fdt,
249                    uid_is_valid(override_uid) ? override_uid : st->st_uid,
250                    gid_is_valid(override_gid) ? override_gid : st->st_gid) < 0)
251                 r = -errno;
252
253         if (fchmod(fdt, st->st_mode & 07777) < 0)
254                 r = -errno;
255
256         ts[0] = st->st_atim;
257         ts[1] = st->st_mtim;
258         (void) futimens(fdt, ts);
259         (void) copy_xattr(fdf, fdt);
260
261         q = close(fdt);
262         fdt = -1;
263
264         if (q < 0) {
265                 r = -errno;
266                 unlinkat(dt, to, 0);
267         }
268
269         return r;
270 }
271
272 static int fd_copy_fifo(
273                 int df,
274                 const char *from,
275                 const struct stat *st,
276                 int dt,
277                 const char *to,
278                 uid_t override_uid,
279                 gid_t override_gid,
280                 CopyFlags copy_flags) {
281         int r;
282
283         assert(from);
284         assert(st);
285         assert(to);
286
287         r = mkfifoat(dt, to, st->st_mode & 07777);
288         if (r < 0)
289                 return -errno;
290
291         if (fchownat(dt, to,
292                      uid_is_valid(override_uid) ? override_uid : st->st_uid,
293                      gid_is_valid(override_gid) ? override_gid : st->st_gid,
294                      AT_SYMLINK_NOFOLLOW) < 0)
295                 r = -errno;
296
297         if (fchmodat(dt, to, st->st_mode & 07777, 0) < 0)
298                 r = -errno;
299
300         return r;
301 }
302
303 static int fd_copy_node(
304                 int df,
305                 const char *from,
306                 const struct stat *st,
307                 int dt,
308                 const char *to,
309                 uid_t override_uid,
310                 gid_t override_gid,
311                 CopyFlags copy_flags) {
312         int r;
313
314         assert(from);
315         assert(st);
316         assert(to);
317
318         r = mknodat(dt, to, st->st_mode, st->st_rdev);
319         if (r < 0)
320                 return -errno;
321
322         if (fchownat(dt, to,
323                      uid_is_valid(override_uid) ? override_uid : st->st_uid,
324                      gid_is_valid(override_gid) ? override_gid : st->st_gid,
325                      AT_SYMLINK_NOFOLLOW) < 0)
326                 r = -errno;
327
328         if (fchmodat(dt, to, st->st_mode & 07777, 0) < 0)
329                 r = -errno;
330
331         return r;
332 }
333
334 static int fd_copy_directory(
335                 int df,
336                 const char *from,
337                 const struct stat *st,
338                 int dt,
339                 const char *to,
340                 dev_t original_device,
341                 uid_t override_uid,
342                 gid_t override_gid,
343                 CopyFlags copy_flags) {
344
345         _cleanup_close_ int fdf = -1, fdt = -1;
346         _cleanup_closedir_ DIR *d = NULL;
347         struct dirent *de;
348         bool created;
349         int r;
350
351         assert(st);
352         assert(to);
353
354         if (from)
355                 fdf = openat(df, from, O_RDONLY|O_DIRECTORY|O_CLOEXEC|O_NOCTTY|O_NOFOLLOW);
356         else
357                 fdf = fcntl(df, F_DUPFD_CLOEXEC, 3);
358         if (fdf < 0)
359                 return -errno;
360
361         d = fdopendir(fdf);
362         if (!d)
363                 return -errno;
364         fdf = -1;
365
366         r = mkdirat(dt, to, st->st_mode & 07777);
367         if (r >= 0)
368                 created = true;
369         else if (errno == EEXIST && (copy_flags & COPY_MERGE))
370                 created = false;
371         else
372                 return -errno;
373
374         fdt = openat(dt, to, O_RDONLY|O_DIRECTORY|O_CLOEXEC|O_NOCTTY|O_NOFOLLOW);
375         if (fdt < 0)
376                 return -errno;
377
378         r = 0;
379
380         FOREACH_DIRENT_ALL(de, d, return -errno) {
381                 struct stat buf;
382                 int q;
383
384                 if (dot_or_dot_dot(de->d_name))
385                         continue;
386
387                 if (fstatat(dirfd(d), de->d_name, &buf, AT_SYMLINK_NOFOLLOW) < 0) {
388                         r = -errno;
389                         continue;
390                 }
391
392                 if (buf.st_dev != original_device)
393                         continue;
394
395                 if (S_ISREG(buf.st_mode))
396                         q = fd_copy_regular(dirfd(d), de->d_name, &buf, fdt, de->d_name, override_uid, override_gid, copy_flags);
397                 else if (S_ISDIR(buf.st_mode))
398                         q = fd_copy_directory(dirfd(d), de->d_name, &buf, fdt, de->d_name, original_device, override_uid, override_gid, copy_flags);
399                 else if (S_ISLNK(buf.st_mode))
400                         q = fd_copy_symlink(dirfd(d), de->d_name, &buf, fdt, de->d_name, override_uid, override_gid, copy_flags);
401                 else if (S_ISFIFO(buf.st_mode))
402                         q = fd_copy_fifo(dirfd(d), de->d_name, &buf, fdt, de->d_name, override_uid, override_gid, copy_flags);
403                 else if (S_ISBLK(buf.st_mode) || S_ISCHR(buf.st_mode) || S_ISSOCK(buf.st_mode))
404                         q = fd_copy_node(dirfd(d), de->d_name, &buf, fdt, de->d_name, override_uid, override_gid, copy_flags);
405                 else
406                         q = -EOPNOTSUPP;
407
408                 if (q == -EEXIST && (copy_flags & COPY_MERGE))
409                         q = 0;
410
411                 if (q < 0)
412                         r = q;
413         }
414
415         if (created) {
416                 struct timespec ut[2] = {
417                         st->st_atim,
418                         st->st_mtim
419                 };
420
421                 if (fchown(fdt,
422                            uid_is_valid(override_uid) ? override_uid : st->st_uid,
423                            gid_is_valid(override_gid) ? override_gid : st->st_gid) < 0)
424                         r = -errno;
425
426                 if (fchmod(fdt, st->st_mode & 07777) < 0)
427                         r = -errno;
428
429                 (void) copy_xattr(dirfd(d), fdt);
430                 (void) futimens(fdt, ut);
431         }
432
433         return r;
434 }
435
436 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) {
437         struct stat st;
438
439         assert(from);
440         assert(to);
441
442         if (fstatat(fdf, from, &st, AT_SYMLINK_NOFOLLOW) < 0)
443                 return -errno;
444
445         if (S_ISREG(st.st_mode))
446                 return fd_copy_regular(fdf, from, &st, fdt, to, override_uid, override_gid, copy_flags);
447         else if (S_ISDIR(st.st_mode))
448                 return fd_copy_directory(fdf, from, &st, fdt, to, st.st_dev, override_uid, override_gid, copy_flags);
449         else if (S_ISLNK(st.st_mode))
450                 return fd_copy_symlink(fdf, from, &st, fdt, to, override_uid, override_gid, copy_flags);
451         else if (S_ISFIFO(st.st_mode))
452                 return fd_copy_fifo(fdf, from, &st, fdt, to, override_uid, override_gid, copy_flags);
453         else if (S_ISBLK(st.st_mode) || S_ISCHR(st.st_mode) || S_ISSOCK(st.st_mode))
454                 return fd_copy_node(fdf, from, &st, fdt, to, override_uid, override_gid, copy_flags);
455         else
456                 return -EOPNOTSUPP;
457 }
458
459 int copy_tree(const char *from, const char *to, uid_t override_uid, gid_t override_gid, CopyFlags copy_flags) {
460         return copy_tree_at(AT_FDCWD, from, AT_FDCWD, to, override_uid, override_gid, copy_flags);
461 }
462
463 int copy_directory_fd(int dirfd, const char *to, CopyFlags copy_flags) {
464         struct stat st;
465
466         assert(dirfd >= 0);
467         assert(to);
468
469         if (fstat(dirfd, &st) < 0)
470                 return -errno;
471
472         if (!S_ISDIR(st.st_mode))
473                 return -ENOTDIR;
474
475         return fd_copy_directory(dirfd, NULL, &st, AT_FDCWD, to, st.st_dev, UID_INVALID, GID_INVALID, copy_flags);
476 }
477
478 int copy_directory(const char *from, const char *to, CopyFlags copy_flags) {
479         struct stat st;
480
481         assert(from);
482         assert(to);
483
484         if (lstat(from, &st) < 0)
485                 return -errno;
486
487         if (!S_ISDIR(st.st_mode))
488                 return -ENOTDIR;
489
490         return fd_copy_directory(AT_FDCWD, from, &st, AT_FDCWD, to, st.st_dev, UID_INVALID, GID_INVALID, copy_flags);
491 }
492
493 int copy_file_fd(const char *from, int fdt, CopyFlags copy_flags) {
494         _cleanup_close_ int fdf = -1;
495         int r;
496
497         assert(from);
498         assert(fdt >= 0);
499
500         fdf = open(from, O_RDONLY|O_CLOEXEC|O_NOCTTY);
501         if (fdf < 0)
502                 return -errno;
503
504         r = copy_bytes(fdf, fdt, (uint64_t) -1, copy_flags);
505
506         (void) copy_times(fdf, fdt);
507         (void) copy_xattr(fdf, fdt);
508
509         return r;
510 }
511
512 int copy_file(const char *from, const char *to, int flags, mode_t mode, unsigned chattr_flags, CopyFlags copy_flags) {
513         int fdt = -1, r;
514
515         assert(from);
516         assert(to);
517
518         RUN_WITH_UMASK(0000) {
519                 fdt = open(to, flags|O_WRONLY|O_CREAT|O_CLOEXEC|O_NOCTTY, mode);
520                 if (fdt < 0)
521                         return -errno;
522         }
523
524         if (chattr_flags != 0)
525                 (void) chattr_fd(fdt, chattr_flags, (unsigned) -1);
526
527         r = copy_file_fd(from, fdt, copy_flags);
528         if (r < 0) {
529                 close(fdt);
530                 unlink(to);
531                 return r;
532         }
533
534         if (close(fdt) < 0) {
535                 unlink_noerrno(to);
536                 return -errno;
537         }
538
539         return 0;
540 }
541
542 int copy_file_atomic(const char *from, const char *to, mode_t mode, unsigned chattr_flags, CopyFlags copy_flags) {
543         _cleanup_free_ char *t = NULL;
544         int r;
545
546         assert(from);
547         assert(to);
548
549         r = tempfn_random(to, NULL, &t);
550         if (r < 0)
551                 return r;
552
553         r = copy_file(from, t, O_NOFOLLOW|O_EXCL, mode, chattr_flags, copy_flags);
554         if (r < 0)
555                 return r;
556
557         if (copy_flags & COPY_REPLACE) {
558                 r = renameat(AT_FDCWD, t, AT_FDCWD, to);
559                 if (r < 0)
560                         r = -errno;
561         } else
562                 r = rename_noreplace(AT_FDCWD, t, AT_FDCWD, to);
563         if (r < 0) {
564                 (void) unlink(t);
565                 return r;
566         }
567
568         return 0;
569 }
570
571 int copy_times(int fdf, int fdt) {
572         struct timespec ut[2];
573         struct stat st;
574         usec_t crtime = 0;
575
576         assert(fdf >= 0);
577         assert(fdt >= 0);
578
579         if (fstat(fdf, &st) < 0)
580                 return -errno;
581
582         ut[0] = st.st_atim;
583         ut[1] = st.st_mtim;
584
585         if (futimens(fdt, ut) < 0)
586                 return -errno;
587
588         if (fd_getcrtime(fdf, &crtime) >= 0)
589                 (void) fd_setcrtime(fdt, crtime);
590
591         return 0;
592 }
593
594 int copy_xattr(int fdf, int fdt) {
595         _cleanup_free_ char *bufa = NULL, *bufb = NULL;
596         size_t sza = 100, szb = 100;
597         ssize_t n;
598         int ret = 0;
599         const char *p;
600
601         for (;;) {
602                 bufa = malloc(sza);
603                 if (!bufa)
604                         return -ENOMEM;
605
606                 n = flistxattr(fdf, bufa, sza);
607                 if (n == 0)
608                         return 0;
609                 if (n > 0)
610                         break;
611                 if (errno != ERANGE)
612                         return -errno;
613
614                 sza *= 2;
615
616                 bufa = mfree(bufa);
617         }
618
619         p = bufa;
620         while (n > 0) {
621                 size_t l;
622
623                 l = strlen(p);
624                 assert(l < (size_t) n);
625
626                 if (startswith(p, "user.")) {
627                         ssize_t m;
628
629                         if (!bufb) {
630                                 bufb = malloc(szb);
631                                 if (!bufb)
632                                         return -ENOMEM;
633                         }
634
635                         m = fgetxattr(fdf, p, bufb, szb);
636                         if (m < 0) {
637                                 if (errno == ERANGE) {
638                                         szb *= 2;
639                                         bufb = mfree(bufb);
640                                         continue;
641                                 }
642
643                                 return -errno;
644                         }
645
646                         if (fsetxattr(fdt, p, bufb, m, 0) < 0)
647                                 ret = -errno;
648                 }
649
650                 p += l + 1;
651                 n -= l + 1;
652         }
653
654         return ret;
655 }
656 #endif // 0