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