]>
Commit | Line | Data |
---|---|---|
db9ecf05 | 1 | /* SPDX-License-Identifier: LGPL-2.1-or-later */ |
641d1f99 | 2 | |
01428141 | 3 | #include <sys/file.h> |
83412d39 | 4 | #include <sys/xattr.h> |
641d1f99 RC |
5 | #include <unistd.h> |
6 | ||
b5efdb8a | 7 | #include "alloc-util.h" |
f461a28d | 8 | #include "chase.h" |
641d1f99 | 9 | #include "copy.h" |
3ffd4af2 | 10 | #include "fd-util.h" |
641d1f99 | 11 | #include "fileio.h" |
f4f15635 | 12 | #include "fs-util.h" |
83412d39 | 13 | #include "hexdecoct.h" |
c2dfcbd4 | 14 | #include "io-util.h" |
ce33fdda | 15 | #include "log.h" |
07630cea | 16 | #include "macro.h" |
641d1f99 | 17 | #include "mkdir.h" |
07630cea | 18 | #include "path-util.h" |
c2dfcbd4 | 19 | #include "random-util.h" |
07630cea LP |
20 | #include "rm-rf.h" |
21 | #include "string-util.h" | |
641d1f99 | 22 | #include "strv.h" |
6d7c4033 | 23 | #include "tests.h" |
e4de7287 | 24 | #include "tmpfile-util.h" |
d01cd401 | 25 | #include "user-util.h" |
83412d39 | 26 | #include "xattr-util.h" |
641d1f99 | 27 | |
4f7452a8 | 28 | TEST(copy_file) { |
641d1f99 | 29 | _cleanup_free_ char *buf = NULL; |
596b44b1 DT |
30 | _cleanup_(unlink_tempfilep) char fn[] = "/tmp/test-copy_file.XXXXXX"; |
31 | _cleanup_(unlink_tempfilep) char fn_copy[] = "/tmp/test-copy_file.XXXXXX"; | |
641d1f99 RC |
32 | size_t sz = 0; |
33 | int fd; | |
34 | ||
646853bd | 35 | fd = mkostemp_safe(fn); |
641d1f99 RC |
36 | assert_se(fd >= 0); |
37 | close(fd); | |
38 | ||
646853bd | 39 | fd = mkostemp_safe(fn_copy); |
641d1f99 RC |
40 | assert_se(fd >= 0); |
41 | close(fd); | |
42 | ||
4c1fc3e4 | 43 | assert_se(write_string_file(fn, "foo bar bar bar foo", WRITE_STRING_FILE_CREATE) == 0); |
641d1f99 | 44 | |
7c2f5495 | 45 | assert_se(copy_file(fn, fn_copy, 0, 0644, COPY_REFLINK) == 0); |
641d1f99 RC |
46 | |
47 | assert_se(read_full_file(fn_copy, &buf, &sz) == 0); | |
c79e88b3 | 48 | ASSERT_STREQ(buf, "foo bar bar bar foo\n"); |
cda134ab | 49 | assert_se(sz == 20); |
641d1f99 RC |
50 | } |
51 | ||
dcb3d093 | 52 | static bool read_file_at_and_streq(int dir_fd, const char *path, const char *expected) { |
92240955 AW |
53 | _cleanup_free_ char *buf = NULL; |
54 | ||
dcb3d093 DDM |
55 | assert_se(read_full_file_at(dir_fd, path, &buf, NULL) == 0); |
56 | return streq(buf, expected); | |
92240955 AW |
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 | ||
5162b2a1 | 68 | /* The file exists- now overwrite original contents, and test the COPY_REPLACE flag. */ |
92240955 | 69 | |
ad6fae7f | 70 | assert_se(copy_tree(src, dst, UID_INVALID, GID_INVALID, COPY_REFLINK, NULL, NULL) == -EEXIST); |
92240955 | 71 | |
dcb3d093 | 72 | assert_se(read_file_at_and_streq(AT_FDCWD, dst, "foo foo foo\n")); |
92240955 | 73 | |
ad6fae7f | 74 | assert_se(copy_tree(src, dst, UID_INVALID, GID_INVALID, COPY_REFLINK|COPY_REPLACE, NULL, NULL) == 0); |
92240955 | 75 | |
dcb3d093 | 76 | assert_se(read_file_at_and_streq(AT_FDCWD, dst, "bar bar\n")); |
92240955 AW |
77 | } |
78 | ||
79 | TEST(copy_tree_replace_dirs) { | |
c9a1202e | 80 | _cleanup_(rm_rf_physical_and_freep) char *srcp = NULL, *dstp = NULL; |
5bb1d7fb | 81 | _cleanup_close_ int src = -EBADF, dst = -EBADF; |
92240955 AW |
82 | |
83 | /* Create the random source/destination directories */ | |
c9a1202e DDM |
84 | assert_se((src = mkdtemp_open(NULL, 0, &srcp)) >= 0); |
85 | assert_se((dst = mkdtemp_open(NULL, 0, &dstp)) >= 0); | |
92240955 AW |
86 | |
87 | /* Populate some data to differentiate the files. */ | |
dcb3d093 DDM |
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); | |
92240955 | 90 | |
dcb3d093 DDM |
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); | |
92240955 AW |
93 | |
94 | /* Copying without COPY_REPLACE should fail because the destination file already exists. */ | |
ad6fae7f | 95 | assert_se(copy_tree_at(src, ".", dst, ".", UID_INVALID, GID_INVALID, COPY_REFLINK, NULL, NULL) == -EEXIST); |
dcb3d093 DDM |
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 | ||
ad6fae7f | 102 | assert_se(copy_tree_at(src, ".", dst, ".", UID_INVALID, GID_INVALID, COPY_REFLINK|COPY_REPLACE|COPY_MERGE, NULL, NULL) == 0); |
dcb3d093 DDM |
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")); | |
92240955 AW |
108 | } |
109 | ||
4f7452a8 | 110 | TEST(copy_file_fd) { |
596b44b1 DT |
111 | _cleanup_(unlink_tempfilep) char in_fn[] = "/tmp/test-copy-file-fd-XXXXXX"; |
112 | _cleanup_(unlink_tempfilep) char out_fn[] = "/tmp/test-copy-file-fd-XXXXXX"; | |
254d1313 | 113 | _cleanup_close_ int in_fd = -EBADF, out_fd = -EBADF; |
2aa07538 ZJS |
114 | const char *text = "boohoo\nfoo\n\tbar\n"; |
115 | char buf[64] = {}; | |
cda134ab | 116 | |
646853bd | 117 | in_fd = mkostemp_safe(in_fn); |
cda134ab | 118 | assert_se(in_fd >= 0); |
646853bd | 119 | out_fd = mkostemp_safe(out_fn); |
cda134ab LP |
120 | assert_se(out_fd >= 0); |
121 | ||
4c1fc3e4 | 122 | assert_se(write_string_file(in_fn, text, WRITE_STRING_FILE_CREATE) == 0); |
1c876927 LP |
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); | |
cda134ab LP |
125 | assert_se(lseek(out_fd, SEEK_SET, 0) == 0); |
126 | ||
2aa07538 | 127 | assert_se(read(out_fd, buf, sizeof buf) == (ssize_t) strlen(text)); |
c79e88b3 | 128 | ASSERT_STREQ(buf, text); |
cda134ab LP |
129 | } |
130 | ||
4f7452a8 | 131 | TEST(copy_tree) { |
b63bd125 | 132 | _cleanup_hashmap_free_ Hashmap *denylist = NULL; |
a424958a | 133 | _cleanup_free_ char *cp = NULL; |
3c14dc61 TM |
134 | char original_dir[] = "/tmp/test-copy_tree/"; |
135 | char copy_dir[] = "/tmp/test-copy_tree-copy/"; | |
641d1f99 | 136 | char **files = STRV_MAKE("file", "dir1/file", "dir1/dir2/file", "dir1/dir2/dir3/dir4/dir5/file"); |
dd480f78 LP |
137 | char **symlinks = STRV_MAKE("link", "file", |
138 | "link2", "dir1/file"); | |
139 | char **hardlinks = STRV_MAKE("hlink", "file", | |
140 | "hlink2", "dir1/file"); | |
a424958a | 141 | const char *unixsockp, *ignorep; |
0e2b2cac | 142 | struct stat st; |
83412d39 LP |
143 | int xattr_worked = -1; /* xattr support is optional in temporary directories, hence use it if we can, |
144 | * but don't fail if we can't */ | |
641d1f99 | 145 | |
c6878637 LP |
146 | (void) rm_rf(copy_dir, REMOVE_ROOT|REMOVE_PHYSICAL); |
147 | (void) rm_rf(original_dir, REMOVE_ROOT|REMOVE_PHYSICAL); | |
641d1f99 RC |
148 | |
149 | STRV_FOREACH(p, files) { | |
3d41b6b8 | 150 | _cleanup_free_ char *f = NULL, *c = NULL; |
83412d39 | 151 | int k; |
2397bc43 | 152 | |
b910cc72 | 153 | assert_se(f = path_join(original_dir, *p)); |
641d1f99 | 154 | |
8ff5fa16 | 155 | assert_se(write_string_file(f, "file", WRITE_STRING_FILE_CREATE|WRITE_STRING_FILE_MKDIR_0755) == 0); |
83412d39 LP |
156 | |
157 | assert_se(base64mem(*p, strlen(*p), &c) >= 0); | |
158 | ||
159 | k = setxattr(f, "user.testxattr", c, strlen(c), 0); | |
160 | assert_se(xattr_worked < 0 || ((k >= 0) == !!xattr_worked)); | |
161 | xattr_worked = k >= 0; | |
641d1f99 RC |
162 | } |
163 | ||
dd480f78 | 164 | STRV_FOREACH_PAIR(ll, p, symlinks) { |
3d41b6b8 | 165 | _cleanup_free_ char *f = NULL, *l = NULL; |
2397bc43 | 166 | |
b910cc72 | 167 | assert_se(f = path_join(original_dir, *p)); |
dd480f78 | 168 | assert_se(l = path_join(original_dir, *ll)); |
641d1f99 RC |
169 | |
170 | assert_se(mkdir_parents(l, 0755) >= 0); | |
171 | assert_se(symlink(f, l) == 0); | |
172 | } | |
173 | ||
dd480f78 | 174 | STRV_FOREACH_PAIR(ll, p, hardlinks) { |
3d41b6b8 | 175 | _cleanup_free_ char *f = NULL, *l = NULL; |
dd480f78 LP |
176 | |
177 | assert_se(f = path_join(original_dir, *p)); | |
178 | assert_se(l = path_join(original_dir, *ll)); | |
179 | ||
180 | assert_se(mkdir_parents(l, 0755) >= 0); | |
181 | assert_se(link(f, l) == 0); | |
182 | } | |
183 | ||
0e2b2cac LP |
184 | unixsockp = strjoina(original_dir, "unixsock"); |
185 | assert_se(mknod(unixsockp, S_IFSOCK|0644, 0) >= 0); | |
186 | ||
a424958a DDM |
187 | ignorep = strjoina(original_dir, "ignore/file"); |
188 | assert_se(write_string_file(ignorep, "ignore", WRITE_STRING_FILE_CREATE|WRITE_STRING_FILE_MKDIR_0755) == 0); | |
189 | assert_se(RET_NERRNO(stat(ignorep, &st)) >= 0); | |
190 | assert_se(cp = memdup(&st, sizeof(st))); | |
b63bd125 | 191 | assert_se(hashmap_ensure_put(&denylist, &inode_hash_ops, cp, INT_TO_PTR(DENY_INODE)) >= 0); |
a424958a DDM |
192 | TAKE_PTR(cp); |
193 | ||
ad6fae7f | 194 | assert_se(copy_tree(original_dir, copy_dir, UID_INVALID, GID_INVALID, COPY_REFLINK|COPY_MERGE|COPY_HARDLINKS, denylist, NULL) == 0); |
641d1f99 RC |
195 | |
196 | STRV_FOREACH(p, files) { | |
3d41b6b8 | 197 | _cleanup_free_ char *buf = NULL, *f = NULL, *c = NULL; |
9421cab6 | 198 | size_t sz; |
83412d39 | 199 | int k; |
2397bc43 | 200 | |
b910cc72 | 201 | assert_se(f = path_join(copy_dir, *p)); |
641d1f99 RC |
202 | |
203 | assert_se(access(f, F_OK) == 0); | |
204 | assert_se(read_full_file(f, &buf, &sz) == 0); | |
c79e88b3 | 205 | ASSERT_STREQ(buf, "file\n"); |
83412d39 | 206 | |
c53e07e2 | 207 | k = lgetxattr_malloc(f, "user.testxattr", &c); |
83412d39 LP |
208 | assert_se(xattr_worked < 0 || ((k >= 0) == !!xattr_worked)); |
209 | ||
210 | if (k >= 0) { | |
211 | _cleanup_free_ char *d = NULL; | |
212 | ||
213 | assert_se(base64mem(*p, strlen(*p), &d) >= 0); | |
c79e88b3 | 214 | ASSERT_STREQ(d, c); |
83412d39 | 215 | } |
641d1f99 RC |
216 | } |
217 | ||
dd480f78 | 218 | STRV_FOREACH_PAIR(ll, p, symlinks) { |
3d41b6b8 | 219 | _cleanup_free_ char *target = NULL, *f = NULL, *l = NULL; |
2397bc43 | 220 | |
dcd6361e | 221 | assert_se(f = strjoin(original_dir, *p)); |
dd480f78 | 222 | assert_se(l = strjoin(copy_dir, *ll)); |
641d1f99 | 223 | |
f461a28d | 224 | assert_se(chase(l, NULL, 0, &target, NULL) == 1); |
641d1f99 RC |
225 | assert_se(path_equal(f, target)); |
226 | } | |
227 | ||
dd480f78 | 228 | STRV_FOREACH_PAIR(ll, p, hardlinks) { |
3d41b6b8 | 229 | _cleanup_free_ char *f = NULL, *l = NULL; |
dd480f78 LP |
230 | struct stat a, b; |
231 | ||
232 | assert_se(f = strjoin(copy_dir, *p)); | |
233 | assert_se(l = strjoin(copy_dir, *ll)); | |
234 | ||
235 | assert_se(lstat(f, &a) >= 0); | |
236 | assert_se(lstat(l, &b) >= 0); | |
237 | ||
238 | assert_se(a.st_ino == b.st_ino); | |
239 | assert_se(a.st_dev == b.st_dev); | |
240 | } | |
241 | ||
0e2b2cac LP |
242 | unixsockp = strjoina(copy_dir, "unixsock"); |
243 | assert_se(stat(unixsockp, &st) >= 0); | |
244 | assert_se(S_ISSOCK(st.st_mode)); | |
245 | ||
ad6fae7f DDM |
246 | assert_se(copy_tree(original_dir, copy_dir, UID_INVALID, GID_INVALID, COPY_REFLINK, denylist, NULL) < 0); |
247 | assert_se(copy_tree("/tmp/inexistent/foo/bar/fsdoi", copy_dir, UID_INVALID, GID_INVALID, COPY_REFLINK, denylist, NULL) < 0); | |
a424958a DDM |
248 | |
249 | ignorep = strjoina(copy_dir, "ignore/file"); | |
250 | assert_se(RET_NERRNO(access(ignorep, F_OK)) == -ENOENT); | |
641d1f99 | 251 | |
c6878637 LP |
252 | (void) rm_rf(copy_dir, REMOVE_ROOT|REMOVE_PHYSICAL); |
253 | (void) rm_rf(original_dir, REMOVE_ROOT|REMOVE_PHYSICAL); | |
641d1f99 RC |
254 | } |
255 | ||
0f938a02 YW |
256 | TEST(copy_tree_at_symlink) { |
257 | _cleanup_(rm_rf_physical_and_freep) char *t = NULL; | |
258 | _cleanup_close_ int tfd = -EBADF, fd = -EBADF; | |
259 | _cleanup_free_ char *p = NULL, *q = NULL; | |
260 | const char *expect = "hgoehogefoobar"; | |
261 | ||
262 | tfd = mkdtemp_open(NULL, O_PATH, &t); | |
263 | assert_se(tfd >= 0); | |
264 | ||
265 | assert_se(symlinkat(expect, tfd, "from") >= 0); | |
266 | ||
267 | assert_se(copy_tree_at(tfd, "from", tfd, "to_1", UID_INVALID, GID_INVALID, 0, NULL, NULL) >= 0); | |
268 | assert_se(readlinkat_malloc(tfd, "to_1", &p) >= 0); | |
c79e88b3 | 269 | ASSERT_STREQ(p, expect); |
0f938a02 YW |
270 | p = mfree(p); |
271 | ||
272 | assert_se(q = path_join(t, "from")); | |
273 | assert_se(copy_tree_at(AT_FDCWD, q, tfd, "to_2", UID_INVALID, GID_INVALID, 0, NULL, NULL) >= 0); | |
274 | assert_se(readlinkat_malloc(tfd, "to_2", &p) >= 0); | |
c79e88b3 | 275 | ASSERT_STREQ(p, expect); |
0f938a02 YW |
276 | p = mfree(p); |
277 | q = mfree(q); | |
278 | ||
279 | fd = openat(tfd, "from", O_CLOEXEC | O_PATH | O_NOFOLLOW); | |
280 | assert_se(fd >= 0); | |
281 | assert_se(copy_tree_at(fd, NULL, tfd, "to_3", UID_INVALID, GID_INVALID, 0, NULL, NULL) >= 0); | |
282 | assert_se(readlinkat_malloc(tfd, "to_3", &p) >= 0); | |
c79e88b3 | 283 | ASSERT_STREQ(p, expect); |
0f938a02 YW |
284 | p = mfree(p); |
285 | ||
286 | assert_se(copy_tree_at(fd, "", tfd, "to_4", UID_INVALID, GID_INVALID, 0, NULL, NULL) >= 0); | |
287 | assert_se(readlinkat_malloc(tfd, "to_4", &p) >= 0); | |
c79e88b3 | 288 | ASSERT_STREQ(p, expect); |
0f938a02 YW |
289 | p = mfree(p); |
290 | fd = safe_close(fd); | |
291 | } | |
292 | ||
ae4088b2 | 293 | TEST_RET(copy_bytes) { |
71136404 | 294 | _cleanup_close_pair_ int pipefd[2] = EBADF_PAIR; |
254d1313 | 295 | _cleanup_close_ int infd = -EBADF; |
ad5ecc11 ZJS |
296 | int r, r2; |
297 | char buf[1024], buf2[1024]; | |
298 | ||
4f36d400 DJL |
299 | infd = open("/usr/lib/os-release", O_RDONLY|O_CLOEXEC); |
300 | if (infd < 0) | |
301 | infd = open("/etc/os-release", O_RDONLY|O_CLOEXEC); | |
ae4088b2 DDM |
302 | if (infd < 0) |
303 | return log_tests_skipped_errno(errno, "Could not open /usr/lib/os-release or /etc/os-release: %m"); | |
ad5ecc11 ZJS |
304 | |
305 | assert_se(pipe2(pipefd, O_CLOEXEC) == 0); | |
306 | ||
f5fbe71d | 307 | r = copy_bytes(infd, pipefd[1], UINT64_MAX, 0); |
ad5ecc11 ZJS |
308 | assert_se(r == 0); |
309 | ||
310 | r = read(pipefd[0], buf, sizeof(buf)); | |
311 | assert_se(r >= 0); | |
312 | ||
313 | assert_se(lseek(infd, 0, SEEK_SET) == 0); | |
314 | r2 = read(infd, buf2, sizeof(buf2)); | |
315 | assert_se(r == r2); | |
316 | ||
317 | assert_se(strneq(buf, buf2, r)); | |
318 | ||
319 | /* test copy_bytes with invalid descriptors */ | |
1c876927 | 320 | r = copy_bytes(pipefd[0], pipefd[0], 1, 0); |
ad5ecc11 ZJS |
321 | assert_se(r == -EBADF); |
322 | ||
1c876927 | 323 | r = copy_bytes(pipefd[1], pipefd[1], 1, 0); |
ad5ecc11 ZJS |
324 | assert_se(r == -EBADF); |
325 | ||
1c876927 | 326 | r = copy_bytes(pipefd[1], infd, 1, 0); |
ad5ecc11 | 327 | assert_se(r == -EBADF); |
ae4088b2 DDM |
328 | |
329 | return 0; | |
ad5ecc11 ZJS |
330 | } |
331 | ||
4f7452a8 | 332 | static void test_copy_bytes_regular_file_one(const char *src, bool try_reflink, uint64_t max_bytes) { |
596b44b1 DT |
333 | _cleanup_(unlink_tempfilep) char fn2[] = "/tmp/test-copy-file-XXXXXX"; |
334 | _cleanup_(unlink_tempfilep) char fn3[] = "/tmp/test-copy-file-XXXXXX"; | |
254d1313 | 335 | _cleanup_close_ int fd = -EBADF, fd2 = -EBADF, fd3 = -EBADF; |
ce33fdda ZJS |
336 | int r; |
337 | struct stat buf, buf2, buf3; | |
338 | ||
fa13cf9e | 339 | log_info("%s try_reflink=%s max_bytes=%" PRIu64, __func__, yes_no(try_reflink), max_bytes); |
ce33fdda | 340 | |
e63d0703 | 341 | fd = open(src, O_CLOEXEC | O_PATH); |
ce33fdda ZJS |
342 | assert_se(fd >= 0); |
343 | ||
646853bd | 344 | fd2 = mkostemp_safe(fn2); |
ce33fdda ZJS |
345 | assert_se(fd2 >= 0); |
346 | ||
646853bd | 347 | fd3 = mkostemp_safe(fn3); |
ce33fdda ZJS |
348 | assert_se(fd3 >= 0); |
349 | ||
1c876927 | 350 | r = copy_bytes(fd, fd2, max_bytes, try_reflink ? COPY_REFLINK : 0); |
f5fbe71d | 351 | if (max_bytes == UINT64_MAX) |
7a827fcb ZJS |
352 | assert_se(r == 0); |
353 | else | |
354 | assert_se(IN_SET(r, 0, 1)); | |
ce33fdda | 355 | |
6fbdf424 ZJS |
356 | assert_se(fstat(fd, &buf) == 0); |
357 | assert_se(fstat(fd2, &buf2) == 0); | |
358 | assert_se((uint64_t) buf2.st_size == MIN((uint64_t) buf.st_size, max_bytes)); | |
359 | ||
f5fbe71d | 360 | if (max_bytes < UINT64_MAX) |
6fbdf424 ZJS |
361 | /* Make sure the file is now higher than max_bytes */ |
362 | assert_se(ftruncate(fd2, max_bytes + 1) == 0); | |
363 | ||
ce33fdda ZJS |
364 | assert_se(lseek(fd2, 0, SEEK_SET) == 0); |
365 | ||
1c876927 | 366 | r = copy_bytes(fd2, fd3, max_bytes, try_reflink ? COPY_REFLINK : 0); |
f5fbe71d | 367 | if (max_bytes == UINT64_MAX) |
7a827fcb ZJS |
368 | assert_se(r == 0); |
369 | else | |
370 | /* We cannot distinguish between the input being exactly max_bytes | |
371 | * or longer than max_bytes (without trying to read one more byte, | |
372 | * or calling stat, or FION_READ, etc, and we don't want to do any | |
373 | * of that). So we expect "truncation" since we know that file we | |
374 | * are copying is exactly max_bytes bytes. */ | |
375 | assert_se(r == 1); | |
ce33fdda | 376 | |
ce33fdda ZJS |
377 | assert_se(fstat(fd3, &buf3) == 0); |
378 | ||
f5fbe71d | 379 | if (max_bytes == UINT64_MAX) |
6fbdf424 ZJS |
380 | assert_se(buf3.st_size == buf2.st_size); |
381 | else | |
382 | assert_se((uint64_t) buf3.st_size == max_bytes); | |
ce33fdda ZJS |
383 | } |
384 | ||
4f7452a8 JJ |
385 | TEST(copy_bytes_regular_file) { |
386 | test_copy_bytes_regular_file_one(saved_argv[0], false, UINT64_MAX); | |
387 | test_copy_bytes_regular_file_one(saved_argv[0], true, UINT64_MAX); | |
388 | test_copy_bytes_regular_file_one(saved_argv[0], false, 1000); /* smaller than copy buffer size */ | |
389 | test_copy_bytes_regular_file_one(saved_argv[0], true, 1000); | |
390 | test_copy_bytes_regular_file_one(saved_argv[0], false, 32000); /* larger than copy buffer size */ | |
391 | test_copy_bytes_regular_file_one(saved_argv[0], true, 32000); | |
392 | } | |
393 | ||
394 | TEST(copy_atomic) { | |
ec6bdf72 LP |
395 | _cleanup_(rm_rf_physical_and_freep) char *p = NULL; |
396 | const char *q; | |
397 | int r; | |
398 | ||
399 | assert_se(mkdtemp_malloc(NULL, &p) >= 0); | |
400 | ||
401 | q = strjoina(p, "/fstab"); | |
402 | ||
7c2f5495 | 403 | r = copy_file_atomic("/etc/fstab", q, 0644, COPY_REFLINK); |
3c14dc61 | 404 | if (r == -ENOENT || ERRNO_IS_PRIVILEGE(r)) |
ec6bdf72 LP |
405 | return; |
406 | ||
7c2f5495 | 407 | assert_se(copy_file_atomic("/etc/fstab", q, 0644, COPY_REFLINK) == -EEXIST); |
ec6bdf72 | 408 | |
7c2f5495 | 409 | assert_se(copy_file_atomic("/etc/fstab", q, 0644, COPY_REPLACE) >= 0); |
ec6bdf72 LP |
410 | } |
411 | ||
4f7452a8 | 412 | TEST(copy_proc) { |
ee1aa61c LP |
413 | _cleanup_(rm_rf_physical_and_freep) char *p = NULL; |
414 | _cleanup_free_ char *f = NULL, *a = NULL, *b = NULL; | |
415 | ||
416 | /* Check if copying data from /proc/ works correctly, i.e. let's see if https://lwn.net/Articles/846403/ is a problem for us */ | |
417 | ||
418 | assert_se(mkdtemp_malloc(NULL, &p) >= 0); | |
419 | assert_se(f = path_join(p, "version")); | |
7c2f5495 | 420 | assert_se(copy_file("/proc/version", f, 0, MODE_INVALID, 0) >= 0); |
ee1aa61c LP |
421 | |
422 | assert_se(read_one_line_file("/proc/version", &a) >= 0); | |
423 | assert_se(read_one_line_file(f, &b) >= 0); | |
c79e88b3 | 424 | ASSERT_STREQ(a, b); |
57ac6959 | 425 | assert_se(!isempty(a)); |
ee1aa61c LP |
426 | } |
427 | ||
12727c2b | 428 | TEST_RET(copy_holes) { |
596b44b1 DT |
429 | _cleanup_(unlink_tempfilep) char fn[] = "/var/tmp/test-copy-hole-fd-XXXXXX"; |
430 | _cleanup_(unlink_tempfilep) char fn_copy[] = "/var/tmp/test-copy-hole-fd-XXXXXX"; | |
12727c2b | 431 | struct stat stat; |
f82f0b99 | 432 | off_t blksz; |
12727c2b | 433 | int r, fd, fd_copy; |
2a97a4b3 | 434 | char *buf; |
12727c2b DDM |
435 | |
436 | fd = mkostemp_safe(fn); | |
437 | assert_se(fd >= 0); | |
438 | ||
439 | fd_copy = mkostemp_safe(fn_copy); | |
f67b4351 | 440 | assert_se(fd_copy >= 0); |
12727c2b DDM |
441 | |
442 | r = RET_NERRNO(fallocate(fd, FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE, 0, 1)); | |
443 | if (ERRNO_IS_NOT_SUPPORTED(r)) | |
444 | return log_tests_skipped("Filesystem doesn't support hole punching"); | |
445 | assert_se(r >= 0); | |
446 | ||
681e39c1 | 447 | ASSERT_OK_ERRNO(fstat(fd, &stat)); |
f82f0b99 | 448 | blksz = stat.st_blksize; |
d66b77b4 | 449 | buf = alloca_safe(blksz); |
450 | memset(buf, 1, blksz); | |
12727c2b | 451 | |
f82f0b99 DDM |
452 | /* We need to make sure to create hole in multiples of the block size, otherwise filesystems (btrfs) |
453 | * might silently truncate/extend the holes. */ | |
454 | ||
455 | assert_se(lseek(fd, blksz, SEEK_CUR) >= 0); | |
456 | assert_se(write(fd, buf, blksz) >= 0); | |
457 | assert_se(lseek(fd, 0, SEEK_END) == 2 * blksz); | |
458 | /* Only ftruncate() can create holes at the end of a file. */ | |
459 | assert_se(ftruncate(fd, 3 * blksz) >= 0); | |
12727c2b DDM |
460 | assert_se(lseek(fd, 0, SEEK_SET) >= 0); |
461 | ||
462 | assert_se(copy_bytes(fd, fd_copy, UINT64_MAX, COPY_HOLES) >= 0); | |
463 | ||
464 | /* Test that the hole starts at the beginning of the file. */ | |
465 | assert_se(lseek(fd_copy, 0, SEEK_HOLE) == 0); | |
466 | /* Test that the hole has the expected size. */ | |
f82f0b99 DDM |
467 | assert_se(lseek(fd_copy, 0, SEEK_DATA) == blksz); |
468 | assert_se(lseek(fd_copy, blksz, SEEK_HOLE) == 2 * blksz); | |
469 | assert_se(lseek(fd_copy, 2 * blksz, SEEK_DATA) < 0 && errno == ENXIO); | |
12727c2b DDM |
470 | |
471 | /* Test that the copied file has the correct size. */ | |
681e39c1 | 472 | ASSERT_OK_ERRNO(fstat(fd_copy, &stat)); |
f82f0b99 | 473 | assert_se(stat.st_size == 3 * blksz); |
12727c2b DDM |
474 | |
475 | close(fd); | |
476 | close(fd_copy); | |
477 | ||
12727c2b DDM |
478 | return 0; |
479 | } | |
480 | ||
c2dfcbd4 MC |
481 | TEST_RET(copy_holes_with_gaps) { |
482 | _cleanup_(rm_rf_physical_and_freep) char *t = NULL; | |
483 | _cleanup_close_ int tfd = -EBADF, fd = -EBADF, fd_copy = -EBADF; | |
484 | struct stat st; | |
485 | off_t blksz; | |
486 | char *buf; | |
487 | int r; | |
488 | ||
489 | assert_se((tfd = mkdtemp_open(NULL, 0, &t)) >= 0); | |
490 | assert_se((fd = openat(tfd, "src", O_CREAT | O_RDWR, 0600)) >= 0); | |
491 | assert_se((fd_copy = openat(tfd, "dst", O_CREAT | O_WRONLY, 0600)) >= 0); | |
492 | ||
681e39c1 | 493 | ASSERT_OK_ERRNO(fstat(fd, &st)); |
c2dfcbd4 MC |
494 | blksz = st.st_blksize; |
495 | buf = alloca_safe(blksz); | |
496 | memset(buf, 1, blksz); | |
497 | ||
498 | /* Create a file with: | |
499 | * - hole of 1 block | |
500 | * - data of 2 block | |
501 | * - hole of 2 blocks | |
502 | * - data of 1 block | |
503 | * | |
504 | * Since sparse files are based on blocks and not bytes, we need to make | |
505 | * sure that the holes are aligned to the block size. | |
506 | */ | |
507 | ||
508 | r = RET_NERRNO(fallocate(fd, FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE, 0, blksz)); | |
509 | if (ERRNO_IS_NOT_SUPPORTED(r)) | |
510 | return log_tests_skipped("Filesystem doesn't support hole punching"); | |
511 | ||
512 | assert_se(lseek(fd, blksz, SEEK_CUR) >= 0); | |
e22c60a9 MY |
513 | assert_se(loop_write(fd, buf, blksz) >= 0); |
514 | assert_se(loop_write(fd, buf, blksz) >= 0); | |
c2dfcbd4 | 515 | assert_se(lseek(fd, 2 * blksz, SEEK_CUR) >= 0); |
e22c60a9 | 516 | assert_se(loop_write(fd, buf, blksz) >= 0); |
c2dfcbd4 MC |
517 | assert_se(lseek(fd, 0, SEEK_SET) >= 0); |
518 | assert_se(fsync(fd) >= 0); | |
519 | ||
520 | /* Copy to the start of the second hole */ | |
521 | assert_se(copy_bytes(fd, fd_copy, 3 * blksz, COPY_HOLES) >= 0); | |
681e39c1 | 522 | ASSERT_OK_ERRNO(fstat(fd_copy, &st)); |
c2dfcbd4 MC |
523 | assert_se(st.st_size == 3 * blksz); |
524 | ||
525 | /* Copy to the middle of the second hole */ | |
526 | assert_se(lseek(fd, 0, SEEK_SET) >= 0); | |
527 | assert_se(lseek(fd_copy, 0, SEEK_SET) >= 0); | |
528 | assert_se(ftruncate(fd_copy, 0) >= 0); | |
529 | assert_se(copy_bytes(fd, fd_copy, 4 * blksz, COPY_HOLES) >= 0); | |
681e39c1 | 530 | ASSERT_OK_ERRNO(fstat(fd_copy, &st)); |
c2dfcbd4 MC |
531 | assert_se(st.st_size == 4 * blksz); |
532 | ||
533 | /* Copy to the end of the second hole */ | |
534 | assert_se(lseek(fd, 0, SEEK_SET) >= 0); | |
535 | assert_se(lseek(fd_copy, 0, SEEK_SET) >= 0); | |
536 | assert_se(ftruncate(fd_copy, 0) >= 0); | |
537 | assert_se(copy_bytes(fd, fd_copy, 5 * blksz, COPY_HOLES) >= 0); | |
681e39c1 | 538 | ASSERT_OK_ERRNO(fstat(fd_copy, &st)); |
c2dfcbd4 MC |
539 | assert_se(st.st_size == 5 * blksz); |
540 | ||
541 | /* Copy everything */ | |
542 | assert_se(lseek(fd, 0, SEEK_SET) >= 0); | |
543 | assert_se(lseek(fd_copy, 0, SEEK_SET) >= 0); | |
544 | assert_se(ftruncate(fd_copy, 0) >= 0); | |
545 | assert_se(copy_bytes(fd, fd_copy, UINT64_MAX, COPY_HOLES) >= 0); | |
681e39c1 | 546 | ASSERT_OK_ERRNO(fstat(fd_copy, &st)); |
c2dfcbd4 MC |
547 | assert_se(st.st_size == 6 * blksz); |
548 | ||
549 | return 0; | |
550 | } | |
551 | ||
01428141 DDM |
552 | TEST(copy_lock) { |
553 | _cleanup_(rm_rf_physical_and_freep) char *t = NULL; | |
554 | _cleanup_close_ int tfd = -EBADF, fd = -EBADF; | |
555 | ||
556 | assert_se((tfd = mkdtemp_open(NULL, 0, &t)) >= 0); | |
557 | assert_se(mkdirat(tfd, "abc", 0755) >= 0); | |
558 | assert_se(write_string_file_at(tfd, "abc/def", "abc", WRITE_STRING_FILE_CREATE) >= 0); | |
559 | ||
560 | assert_se((fd = copy_directory_at(tfd, "abc", tfd, "qed", COPY_LOCK_BSD)) >= 0); | |
561 | assert_se(faccessat(tfd, "qed", F_OK, 0) >= 0); | |
562 | assert_se(faccessat(tfd, "qed/def", F_OK, 0) >= 0); | |
e40b11be | 563 | assert_se(xopenat_lock(tfd, "qed", 0, LOCK_BSD, LOCK_EX|LOCK_NB) == -EAGAIN); |
01428141 DDM |
564 | fd = safe_close(fd); |
565 | ||
566 | assert_se((fd = copy_file_at(tfd, "abc/def", tfd, "poi", 0, 0644, COPY_LOCK_BSD))); | |
567 | assert_se(read_file_at_and_streq(tfd, "poi", "abc\n")); | |
e40b11be | 568 | assert_se(xopenat_lock(tfd, "poi", 0, LOCK_BSD, LOCK_EX|LOCK_NB) == -EAGAIN); |
01428141 DDM |
569 | fd = safe_close(fd); |
570 | } | |
571 | ||
72ef2a61 YW |
572 | TEST(copy_verify_linked) { |
573 | _cleanup_(rm_rf_physical_and_freep) char *t = NULL; | |
574 | _cleanup_close_ int tfd = -EBADF, fd_1 = -EBADF, fd_2 = -EBADF; | |
575 | ||
576 | tfd = mkdtemp_open(NULL, O_PATH, &t); | |
577 | assert_se(tfd >= 0); | |
578 | ||
579 | assert_se(write_string_file_at(tfd, "hoge", "bar bar", WRITE_STRING_FILE_CREATE) >= 0); | |
580 | ||
581 | fd_1 = openat(tfd, "hoge", O_CLOEXEC | O_NOCTTY | O_RDONLY); | |
582 | assert_se(fd_1 >= 0); | |
583 | fd_2 = openat(tfd, "hoge", O_CLOEXEC | O_NOCTTY | O_RDONLY); | |
584 | assert_se(fd_2 >= 0); | |
585 | assert_se(unlinkat(tfd, "hoge", 0) >= 0); | |
586 | ||
587 | assert_se(copy_file_at(fd_1, NULL, tfd, "to_1", 0, 0644, 0) >= 0); | |
588 | assert_se(read_file_at_and_streq(tfd, "to_1", "bar bar\n")); | |
589 | ||
590 | assert_se(copy_file_at(fd_2, NULL, tfd, "to_2", O_EXCL, 0644, COPY_VERIFY_LINKED) == -EIDRM); | |
591 | assert_se(faccessat(tfd, "to_2", F_OK, AT_SYMLINK_NOFOLLOW) < 0 && errno == ENOENT); | |
592 | } | |
593 | ||
4f7452a8 | 594 | DEFINE_TEST_MAIN(LOG_DEBUG); |