]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/test/test-copy.c
6ad157ef1c5a83d78f9c31563ff3e5a5c5b93c40
[thirdparty/systemd.git] / src / test / test-copy.c
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <sys/xattr.h>
4 #include <unistd.h>
5
6 #include "alloc-util.h"
7 #include "chase-symlinks.h"
8 #include "copy.h"
9 #include "fd-util.h"
10 #include "fileio.h"
11 #include "fs-util.h"
12 #include "hexdecoct.h"
13 #include "log.h"
14 #include "macro.h"
15 #include "mkdir.h"
16 #include "path-util.h"
17 #include "rm-rf.h"
18 #include "string-util.h"
19 #include "strv.h"
20 #include "tests.h"
21 #include "tmpfile-util.h"
22 #include "user-util.h"
23 #include "xattr-util.h"
24
25 TEST(copy_file) {
26 _cleanup_free_ char *buf = NULL;
27 char fn[] = "/tmp/test-copy_file.XXXXXX";
28 char fn_copy[] = "/tmp/test-copy_file.XXXXXX";
29 size_t sz = 0;
30 int fd;
31
32 fd = mkostemp_safe(fn);
33 assert_se(fd >= 0);
34 close(fd);
35
36 fd = mkostemp_safe(fn_copy);
37 assert_se(fd >= 0);
38 close(fd);
39
40 assert_se(write_string_file(fn, "foo bar bar bar foo", WRITE_STRING_FILE_CREATE) == 0);
41
42 assert_se(copy_file(fn, fn_copy, 0, 0644, 0, 0, COPY_REFLINK) == 0);
43
44 assert_se(read_full_file(fn_copy, &buf, &sz) == 0);
45 assert_se(streq(buf, "foo bar bar bar foo\n"));
46 assert_se(sz == 20);
47
48 unlink(fn);
49 unlink(fn_copy);
50 }
51
52 static bool read_file_at_and_streq(int dir_fd, const char *path, const char *expected) {
53 _cleanup_free_ char *buf = NULL;
54
55 assert_se(read_full_file_at(dir_fd, path, &buf, NULL) == 0);
56 return streq(buf, expected);
57 }
58
59 TEST(copy_tree_replace_file) {
60 _cleanup_free_ char *src = NULL, *dst = NULL;
61
62 assert_se(tempfn_random("/tmp/test-copy_file.XXXXXX", NULL, &src) >= 0);
63 assert_se(tempfn_random("/tmp/test-copy_file.XXXXXX", NULL, &dst) >= 0);
64
65 assert_se(write_string_file(src, "bar bar", WRITE_STRING_FILE_CREATE) == 0);
66 assert_se(write_string_file(dst, "foo foo foo", WRITE_STRING_FILE_CREATE) == 0);
67
68 /* The file exists- now overwrite original contents, and test the COPY_REPLACE flag. */
69
70 assert_se(copy_tree(src, dst, UID_INVALID, GID_INVALID, COPY_REFLINK) == -EEXIST);
71
72 assert_se(read_file_at_and_streq(AT_FDCWD, dst, "foo foo foo\n"));
73
74 assert_se(copy_tree(src, dst, UID_INVALID, GID_INVALID, COPY_REFLINK|COPY_REPLACE) == 0);
75
76 assert_se(read_file_at_and_streq(AT_FDCWD, dst, "bar bar\n"));
77 }
78
79 TEST(copy_tree_replace_dirs) {
80 _cleanup_(rm_rf_physical_and_freep) char *srcp = NULL, *dstp = NULL;
81 _cleanup_close_ int src = -1, dst = -1;
82
83 /* Create the random source/destination directories */
84 assert_se((src = mkdtemp_open(NULL, 0, &srcp)) >= 0);
85 assert_se((dst = mkdtemp_open(NULL, 0, &dstp)) >= 0);
86
87 /* Populate some data to differentiate the files. */
88 assert_se(write_string_file_at(src, "foo", "src file 1", WRITE_STRING_FILE_CREATE) >= 0);
89 assert_se(write_string_file_at(src, "bar", "src file 2", WRITE_STRING_FILE_CREATE) == 0);
90
91 assert_se(write_string_file_at(dst, "foo", "dest file 1", WRITE_STRING_FILE_CREATE) == 0);
92 assert_se(write_string_file_at(dst, "bar", "dest file 2", WRITE_STRING_FILE_CREATE) == 0);
93
94 /* Copying without COPY_REPLACE should fail because the destination file already exists. */
95 assert_se(copy_tree_at(src, ".", dst, ".", UID_INVALID, GID_INVALID, COPY_REFLINK) == -EEXIST);
96
97 assert_se(read_file_at_and_streq(src, "foo", "src file 1\n"));
98 assert_se(read_file_at_and_streq(src, "bar", "src file 2\n"));
99 assert_se(read_file_at_and_streq(dst, "foo", "dest file 1\n"));
100 assert_se(read_file_at_and_streq(dst, "bar", "dest file 2\n"));
101
102 assert_se(copy_tree_at(src, ".", dst, ".", UID_INVALID, GID_INVALID, COPY_REFLINK|COPY_REPLACE|COPY_MERGE) == 0);
103
104 assert_se(read_file_at_and_streq(src, "foo", "src file 1\n"));
105 assert_se(read_file_at_and_streq(src, "bar", "src file 2\n"));
106 assert_se(read_file_at_and_streq(dst, "foo", "src file 1\n"));
107 assert_se(read_file_at_and_streq(dst, "bar", "src file 2\n"));
108 }
109
110 TEST(copy_file_fd) {
111 char in_fn[] = "/tmp/test-copy-file-fd-XXXXXX";
112 char out_fn[] = "/tmp/test-copy-file-fd-XXXXXX";
113 _cleanup_close_ int in_fd = -1, out_fd = -1;
114 const char *text = "boohoo\nfoo\n\tbar\n";
115 char buf[64] = {};
116
117 in_fd = mkostemp_safe(in_fn);
118 assert_se(in_fd >= 0);
119 out_fd = mkostemp_safe(out_fn);
120 assert_se(out_fd >= 0);
121
122 assert_se(write_string_file(in_fn, text, WRITE_STRING_FILE_CREATE) == 0);
123 assert_se(copy_file_fd("/a/file/which/does/not/exist/i/guess", out_fd, COPY_REFLINK) < 0);
124 assert_se(copy_file_fd(in_fn, out_fd, COPY_REFLINK) >= 0);
125 assert_se(lseek(out_fd, SEEK_SET, 0) == 0);
126
127 assert_se(read(out_fd, buf, sizeof buf) == (ssize_t) strlen(text));
128 assert_se(streq(buf, text));
129
130 unlink(in_fn);
131 unlink(out_fn);
132 }
133
134 TEST(copy_tree) {
135 char original_dir[] = "/tmp/test-copy_tree/";
136 char copy_dir[] = "/tmp/test-copy_tree-copy/";
137 char **files = STRV_MAKE("file", "dir1/file", "dir1/dir2/file", "dir1/dir2/dir3/dir4/dir5/file");
138 char **symlinks = STRV_MAKE("link", "file",
139 "link2", "dir1/file");
140 char **hardlinks = STRV_MAKE("hlink", "file",
141 "hlink2", "dir1/file");
142 const char *unixsockp;
143 struct stat st;
144 int xattr_worked = -1; /* xattr support is optional in temporary directories, hence use it if we can,
145 * but don't fail if we can't */
146
147 (void) rm_rf(copy_dir, REMOVE_ROOT|REMOVE_PHYSICAL);
148 (void) rm_rf(original_dir, REMOVE_ROOT|REMOVE_PHYSICAL);
149
150 STRV_FOREACH(p, files) {
151 _cleanup_free_ char *f, *c;
152 int k;
153
154 assert_se(f = path_join(original_dir, *p));
155
156 assert_se(write_string_file(f, "file", WRITE_STRING_FILE_CREATE|WRITE_STRING_FILE_MKDIR_0755) == 0);
157
158 assert_se(base64mem(*p, strlen(*p), &c) >= 0);
159
160 k = setxattr(f, "user.testxattr", c, strlen(c), 0);
161 assert_se(xattr_worked < 0 || ((k >= 0) == !!xattr_worked));
162 xattr_worked = k >= 0;
163 }
164
165 STRV_FOREACH_PAIR(ll, p, symlinks) {
166 _cleanup_free_ char *f, *l;
167
168 assert_se(f = path_join(original_dir, *p));
169 assert_se(l = path_join(original_dir, *ll));
170
171 assert_se(mkdir_parents(l, 0755) >= 0);
172 assert_se(symlink(f, l) == 0);
173 }
174
175 STRV_FOREACH_PAIR(ll, p, hardlinks) {
176 _cleanup_free_ char *f, *l;
177
178 assert_se(f = path_join(original_dir, *p));
179 assert_se(l = path_join(original_dir, *ll));
180
181 assert_se(mkdir_parents(l, 0755) >= 0);
182 assert_se(link(f, l) == 0);
183 }
184
185 unixsockp = strjoina(original_dir, "unixsock");
186 assert_se(mknod(unixsockp, S_IFSOCK|0644, 0) >= 0);
187
188 assert_se(copy_tree(original_dir, copy_dir, UID_INVALID, GID_INVALID, COPY_REFLINK|COPY_MERGE|COPY_HARDLINKS) == 0);
189
190 STRV_FOREACH(p, files) {
191 _cleanup_free_ char *buf, *f, *c = NULL;
192 size_t sz;
193 int k;
194
195 assert_se(f = path_join(copy_dir, *p));
196
197 assert_se(access(f, F_OK) == 0);
198 assert_se(read_full_file(f, &buf, &sz) == 0);
199 assert_se(streq(buf, "file\n"));
200
201 k = lgetxattr_malloc(f, "user.testxattr", &c);
202 assert_se(xattr_worked < 0 || ((k >= 0) == !!xattr_worked));
203
204 if (k >= 0) {
205 _cleanup_free_ char *d = NULL;
206
207 assert_se(base64mem(*p, strlen(*p), &d) >= 0);
208 assert_se(streq(d, c));
209 }
210 }
211
212 STRV_FOREACH_PAIR(ll, p, symlinks) {
213 _cleanup_free_ char *target, *f, *l;
214
215 assert_se(f = strjoin(original_dir, *p));
216 assert_se(l = strjoin(copy_dir, *ll));
217
218 assert_se(chase_symlinks(l, NULL, 0, &target, NULL) == 1);
219 assert_se(path_equal(f, target));
220 }
221
222 STRV_FOREACH_PAIR(ll, p, hardlinks) {
223 _cleanup_free_ char *f, *l;
224 struct stat a, b;
225
226 assert_se(f = strjoin(copy_dir, *p));
227 assert_se(l = strjoin(copy_dir, *ll));
228
229 assert_se(lstat(f, &a) >= 0);
230 assert_se(lstat(l, &b) >= 0);
231
232 assert_se(a.st_ino == b.st_ino);
233 assert_se(a.st_dev == b.st_dev);
234 }
235
236 unixsockp = strjoina(copy_dir, "unixsock");
237 assert_se(stat(unixsockp, &st) >= 0);
238 assert_se(S_ISSOCK(st.st_mode));
239
240 assert_se(copy_tree(original_dir, copy_dir, UID_INVALID, GID_INVALID, COPY_REFLINK) < 0);
241 assert_se(copy_tree("/tmp/inexistent/foo/bar/fsdoi", copy_dir, UID_INVALID, GID_INVALID, COPY_REFLINK) < 0);
242
243 (void) rm_rf(copy_dir, REMOVE_ROOT|REMOVE_PHYSICAL);
244 (void) rm_rf(original_dir, REMOVE_ROOT|REMOVE_PHYSICAL);
245 }
246
247 TEST(copy_bytes) {
248 _cleanup_close_pair_ int pipefd[2] = {-1, -1};
249 _cleanup_close_ int infd = -1;
250 int r, r2;
251 char buf[1024], buf2[1024];
252
253 infd = open("/usr/lib/os-release", O_RDONLY|O_CLOEXEC);
254 if (infd < 0)
255 infd = open("/etc/os-release", O_RDONLY|O_CLOEXEC);
256 assert_se(infd >= 0);
257
258 assert_se(pipe2(pipefd, O_CLOEXEC) == 0);
259
260 r = copy_bytes(infd, pipefd[1], UINT64_MAX, 0);
261 assert_se(r == 0);
262
263 r = read(pipefd[0], buf, sizeof(buf));
264 assert_se(r >= 0);
265
266 assert_se(lseek(infd, 0, SEEK_SET) == 0);
267 r2 = read(infd, buf2, sizeof(buf2));
268 assert_se(r == r2);
269
270 assert_se(strneq(buf, buf2, r));
271
272 /* test copy_bytes with invalid descriptors */
273 r = copy_bytes(pipefd[0], pipefd[0], 1, 0);
274 assert_se(r == -EBADF);
275
276 r = copy_bytes(pipefd[1], pipefd[1], 1, 0);
277 assert_se(r == -EBADF);
278
279 r = copy_bytes(pipefd[1], infd, 1, 0);
280 assert_se(r == -EBADF);
281 }
282
283 static void test_copy_bytes_regular_file_one(const char *src, bool try_reflink, uint64_t max_bytes) {
284 char fn2[] = "/tmp/test-copy-file-XXXXXX";
285 char fn3[] = "/tmp/test-copy-file-XXXXXX";
286 _cleanup_close_ int fd = -1, fd2 = -1, fd3 = -1;
287 int r;
288 struct stat buf, buf2, buf3;
289
290 log_info("%s try_reflink=%s max_bytes=%" PRIu64, __func__, yes_no(try_reflink), max_bytes);
291
292 fd = open(src, O_RDONLY | O_CLOEXEC | O_NOCTTY);
293 assert_se(fd >= 0);
294
295 fd2 = mkostemp_safe(fn2);
296 assert_se(fd2 >= 0);
297
298 fd3 = mkostemp_safe(fn3);
299 assert_se(fd3 >= 0);
300
301 r = copy_bytes(fd, fd2, max_bytes, try_reflink ? COPY_REFLINK : 0);
302 if (max_bytes == UINT64_MAX)
303 assert_se(r == 0);
304 else
305 assert_se(IN_SET(r, 0, 1));
306
307 assert_se(fstat(fd, &buf) == 0);
308 assert_se(fstat(fd2, &buf2) == 0);
309 assert_se((uint64_t) buf2.st_size == MIN((uint64_t) buf.st_size, max_bytes));
310
311 if (max_bytes < UINT64_MAX)
312 /* Make sure the file is now higher than max_bytes */
313 assert_se(ftruncate(fd2, max_bytes + 1) == 0);
314
315 assert_se(lseek(fd2, 0, SEEK_SET) == 0);
316
317 r = copy_bytes(fd2, fd3, max_bytes, try_reflink ? COPY_REFLINK : 0);
318 if (max_bytes == UINT64_MAX)
319 assert_se(r == 0);
320 else
321 /* We cannot distinguish between the input being exactly max_bytes
322 * or longer than max_bytes (without trying to read one more byte,
323 * or calling stat, or FION_READ, etc, and we don't want to do any
324 * of that). So we expect "truncation" since we know that file we
325 * are copying is exactly max_bytes bytes. */
326 assert_se(r == 1);
327
328 assert_se(fstat(fd3, &buf3) == 0);
329
330 if (max_bytes == UINT64_MAX)
331 assert_se(buf3.st_size == buf2.st_size);
332 else
333 assert_se((uint64_t) buf3.st_size == max_bytes);
334
335 unlink(fn2);
336 unlink(fn3);
337 }
338
339 TEST(copy_bytes_regular_file) {
340 test_copy_bytes_regular_file_one(saved_argv[0], false, UINT64_MAX);
341 test_copy_bytes_regular_file_one(saved_argv[0], true, UINT64_MAX);
342 test_copy_bytes_regular_file_one(saved_argv[0], false, 1000); /* smaller than copy buffer size */
343 test_copy_bytes_regular_file_one(saved_argv[0], true, 1000);
344 test_copy_bytes_regular_file_one(saved_argv[0], false, 32000); /* larger than copy buffer size */
345 test_copy_bytes_regular_file_one(saved_argv[0], true, 32000);
346 }
347
348 TEST(copy_atomic) {
349 _cleanup_(rm_rf_physical_and_freep) char *p = NULL;
350 const char *q;
351 int r;
352
353 assert_se(mkdtemp_malloc(NULL, &p) >= 0);
354
355 q = strjoina(p, "/fstab");
356
357 r = copy_file_atomic("/etc/fstab", q, 0644, 0, 0, COPY_REFLINK);
358 if (r == -ENOENT || ERRNO_IS_PRIVILEGE(r))
359 return;
360
361 assert_se(copy_file_atomic("/etc/fstab", q, 0644, 0, 0, COPY_REFLINK) == -EEXIST);
362
363 assert_se(copy_file_atomic("/etc/fstab", q, 0644, 0, 0, COPY_REPLACE) >= 0);
364 }
365
366 TEST(copy_proc) {
367 _cleanup_(rm_rf_physical_and_freep) char *p = NULL;
368 _cleanup_free_ char *f = NULL, *a = NULL, *b = NULL;
369
370 /* Check if copying data from /proc/ works correctly, i.e. let's see if https://lwn.net/Articles/846403/ is a problem for us */
371
372 assert_se(mkdtemp_malloc(NULL, &p) >= 0);
373 assert_se(f = path_join(p, "version"));
374 assert_se(copy_file("/proc/version", f, 0, MODE_INVALID, 0, 0, 0) >= 0);
375
376 assert_se(read_one_line_file("/proc/version", &a) >= 0);
377 assert_se(read_one_line_file(f, &b) >= 0);
378 assert_se(streq(a, b));
379 assert_se(!isempty(a));
380 }
381
382 TEST_RET(copy_holes) {
383 char fn[] = "/var/tmp/test-copy-hole-fd-XXXXXX";
384 char fn_copy[] = "/var/tmp/test-copy-hole-fd-XXXXXX";
385 struct stat stat;
386 off_t blksz;
387 int r, fd, fd_copy;
388 char *buf;
389
390 fd = mkostemp_safe(fn);
391 assert_se(fd >= 0);
392
393 fd_copy = mkostemp_safe(fn_copy);
394 assert_se(fd_copy >= 0);
395
396 r = RET_NERRNO(fallocate(fd, FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE, 0, 1));
397 if (ERRNO_IS_NOT_SUPPORTED(r))
398 return log_tests_skipped("Filesystem doesn't support hole punching");
399 assert_se(r >= 0);
400
401 assert_se(fstat(fd, &stat) >= 0);
402 blksz = stat.st_blksize;
403 buf = alloca_safe(blksz);
404 memset(buf, 1, blksz);
405
406 /* We need to make sure to create hole in multiples of the block size, otherwise filesystems (btrfs)
407 * might silently truncate/extend the holes. */
408
409 assert_se(lseek(fd, blksz, SEEK_CUR) >= 0);
410 assert_se(write(fd, buf, blksz) >= 0);
411 assert_se(lseek(fd, 0, SEEK_END) == 2 * blksz);
412 /* Only ftruncate() can create holes at the end of a file. */
413 assert_se(ftruncate(fd, 3 * blksz) >= 0);
414 assert_se(lseek(fd, 0, SEEK_SET) >= 0);
415
416 assert_se(copy_bytes(fd, fd_copy, UINT64_MAX, COPY_HOLES) >= 0);
417
418 /* Test that the hole starts at the beginning of the file. */
419 assert_se(lseek(fd_copy, 0, SEEK_HOLE) == 0);
420 /* Test that the hole has the expected size. */
421 assert_se(lseek(fd_copy, 0, SEEK_DATA) == blksz);
422 assert_se(lseek(fd_copy, blksz, SEEK_HOLE) == 2 * blksz);
423 assert_se(lseek(fd_copy, 2 * blksz, SEEK_DATA) < 0 && errno == ENXIO);
424
425 /* Test that the copied file has the correct size. */
426 assert_se(fstat(fd_copy, &stat) >= 0);
427 assert_se(stat.st_size == 3 * blksz);
428
429 close(fd);
430 close(fd_copy);
431
432 unlink(fn);
433 unlink(fn_copy);
434
435 return 0;
436 }
437
438 DEFINE_TEST_MAIN(LOG_DEBUG);