]>
Commit | Line | Data |
---|---|---|
db9ecf05 | 1 | /* SPDX-License-Identifier: LGPL-2.1-or-later */ |
641d1f99 | 2 | |
83412d39 | 3 | #include <sys/xattr.h> |
641d1f99 RC |
4 | #include <unistd.h> |
5 | ||
b5efdb8a | 6 | #include "alloc-util.h" |
f4351959 | 7 | #include "chase-symlinks.h" |
641d1f99 | 8 | #include "copy.h" |
3ffd4af2 | 9 | #include "fd-util.h" |
641d1f99 | 10 | #include "fileio.h" |
f4f15635 | 11 | #include "fs-util.h" |
83412d39 | 12 | #include "hexdecoct.h" |
ce33fdda | 13 | #include "log.h" |
07630cea | 14 | #include "macro.h" |
641d1f99 | 15 | #include "mkdir.h" |
07630cea LP |
16 | #include "path-util.h" |
17 | #include "rm-rf.h" | |
18 | #include "string-util.h" | |
641d1f99 | 19 | #include "strv.h" |
6d7c4033 | 20 | #include "tests.h" |
e4de7287 | 21 | #include "tmpfile-util.h" |
d01cd401 | 22 | #include "user-util.h" |
83412d39 | 23 | #include "xattr-util.h" |
641d1f99 | 24 | |
4f7452a8 | 25 | TEST(copy_file) { |
641d1f99 RC |
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 | ||
646853bd | 32 | fd = mkostemp_safe(fn); |
641d1f99 RC |
33 | assert_se(fd >= 0); |
34 | close(fd); | |
35 | ||
646853bd | 36 | fd = mkostemp_safe(fn_copy); |
641d1f99 RC |
37 | assert_se(fd >= 0); |
38 | close(fd); | |
39 | ||
4c1fc3e4 | 40 | assert_se(write_string_file(fn, "foo bar bar bar foo", WRITE_STRING_FILE_CREATE) == 0); |
641d1f99 | 41 | |
8a016c74 | 42 | assert_se(copy_file(fn, fn_copy, 0, 0644, 0, 0, COPY_REFLINK) == 0); |
641d1f99 RC |
43 | |
44 | assert_se(read_full_file(fn_copy, &buf, &sz) == 0); | |
45 | assert_se(streq(buf, "foo bar bar bar foo\n")); | |
cda134ab | 46 | assert_se(sz == 20); |
641d1f99 RC |
47 | |
48 | unlink(fn); | |
49 | unlink(fn_copy); | |
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 AW |
69 | |
70 | assert_se(copy_tree(src, dst, UID_INVALID, GID_INVALID, COPY_REFLINK) == -EEXIST); | |
71 | ||
dcb3d093 | 72 | assert_se(read_file_at_and_streq(AT_FDCWD, dst, "foo foo foo\n")); |
92240955 AW |
73 | |
74 | assert_se(copy_tree(src, dst, UID_INVALID, GID_INVALID, COPY_REFLINK|COPY_REPLACE) == 0); | |
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) { | |
dcb3d093 | 80 | _cleanup_(rm_rf_physical_and_closep) int src = -1, dst = -1; |
92240955 AW |
81 | |
82 | /* Create the random source/destination directories */ | |
dcb3d093 DDM |
83 | assert_se((src = mkdtemp_open(NULL, 0, NULL)) >= 0); |
84 | assert_se((dst = mkdtemp_open(NULL, 0, NULL)) >= 0); | |
92240955 AW |
85 | |
86 | /* Populate some data to differentiate the files. */ | |
dcb3d093 DDM |
87 | assert_se(write_string_file_at(src, "foo", "src file 1", WRITE_STRING_FILE_CREATE) >= 0); |
88 | assert_se(write_string_file_at(src, "bar", "src file 2", WRITE_STRING_FILE_CREATE) == 0); | |
92240955 | 89 | |
dcb3d093 DDM |
90 | assert_se(write_string_file_at(dst, "foo", "dest file 1", WRITE_STRING_FILE_CREATE) == 0); |
91 | assert_se(write_string_file_at(dst, "bar", "dest file 2", WRITE_STRING_FILE_CREATE) == 0); | |
92240955 AW |
92 | |
93 | /* Copying without COPY_REPLACE should fail because the destination file already exists. */ | |
dcb3d093 DDM |
94 | assert_se(copy_tree_at(src, ".", dst, ".", UID_INVALID, GID_INVALID, COPY_REFLINK) == -EEXIST); |
95 | ||
96 | assert_se(read_file_at_and_streq(src, "foo", "src file 1\n")); | |
97 | assert_se(read_file_at_and_streq(src, "bar", "src file 2\n")); | |
98 | assert_se(read_file_at_and_streq(dst, "foo", "dest file 1\n")); | |
99 | assert_se(read_file_at_and_streq(dst, "bar", "dest file 2\n")); | |
100 | ||
101 | assert_se(copy_tree_at(src, ".", dst, ".", UID_INVALID, GID_INVALID, COPY_REFLINK|COPY_REPLACE|COPY_MERGE) == 0); | |
102 | ||
103 | assert_se(read_file_at_and_streq(src, "foo", "src file 1\n")); | |
104 | assert_se(read_file_at_and_streq(src, "bar", "src file 2\n")); | |
105 | assert_se(read_file_at_and_streq(dst, "foo", "src file 1\n")); | |
106 | assert_se(read_file_at_and_streq(dst, "bar", "src file 2\n")); | |
92240955 AW |
107 | } |
108 | ||
4f7452a8 | 109 | TEST(copy_file_fd) { |
cda134ab LP |
110 | char in_fn[] = "/tmp/test-copy-file-fd-XXXXXX"; |
111 | char out_fn[] = "/tmp/test-copy-file-fd-XXXXXX"; | |
112 | _cleanup_close_ int in_fd = -1, out_fd = -1; | |
2aa07538 ZJS |
113 | const char *text = "boohoo\nfoo\n\tbar\n"; |
114 | char buf[64] = {}; | |
cda134ab | 115 | |
646853bd | 116 | in_fd = mkostemp_safe(in_fn); |
cda134ab | 117 | assert_se(in_fd >= 0); |
646853bd | 118 | out_fd = mkostemp_safe(out_fn); |
cda134ab LP |
119 | assert_se(out_fd >= 0); |
120 | ||
4c1fc3e4 | 121 | assert_se(write_string_file(in_fn, text, WRITE_STRING_FILE_CREATE) == 0); |
1c876927 LP |
122 | assert_se(copy_file_fd("/a/file/which/does/not/exist/i/guess", out_fd, COPY_REFLINK) < 0); |
123 | assert_se(copy_file_fd(in_fn, out_fd, COPY_REFLINK) >= 0); | |
cda134ab LP |
124 | assert_se(lseek(out_fd, SEEK_SET, 0) == 0); |
125 | ||
2aa07538 | 126 | assert_se(read(out_fd, buf, sizeof buf) == (ssize_t) strlen(text)); |
cda134ab LP |
127 | assert_se(streq(buf, text)); |
128 | ||
129 | unlink(in_fn); | |
130 | unlink(out_fn); | |
131 | } | |
132 | ||
4f7452a8 | 133 | TEST(copy_tree) { |
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"); | |
0e2b2cac LP |
141 | const char *unixsockp; |
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) { | |
83412d39 LP |
150 | _cleanup_free_ char *f, *c; |
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) { |
2397bc43 LP |
165 | _cleanup_free_ char *f, *l; |
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 LP |
174 | STRV_FOREACH_PAIR(ll, p, hardlinks) { |
175 | _cleanup_free_ char *f, *l; | |
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 | ||
dd480f78 | 187 | assert_se(copy_tree(original_dir, copy_dir, UID_INVALID, GID_INVALID, COPY_REFLINK|COPY_MERGE|COPY_HARDLINKS) == 0); |
641d1f99 RC |
188 | |
189 | STRV_FOREACH(p, files) { | |
83412d39 | 190 | _cleanup_free_ char *buf, *f, *c = NULL; |
9421cab6 | 191 | size_t sz; |
83412d39 | 192 | int k; |
2397bc43 | 193 | |
b910cc72 | 194 | assert_se(f = path_join(copy_dir, *p)); |
641d1f99 RC |
195 | |
196 | assert_se(access(f, F_OK) == 0); | |
197 | assert_se(read_full_file(f, &buf, &sz) == 0); | |
198 | assert_se(streq(buf, "file\n")); | |
83412d39 | 199 | |
c53e07e2 | 200 | k = lgetxattr_malloc(f, "user.testxattr", &c); |
83412d39 LP |
201 | assert_se(xattr_worked < 0 || ((k >= 0) == !!xattr_worked)); |
202 | ||
203 | if (k >= 0) { | |
204 | _cleanup_free_ char *d = NULL; | |
205 | ||
206 | assert_se(base64mem(*p, strlen(*p), &d) >= 0); | |
207 | assert_se(streq(d, c)); | |
208 | } | |
641d1f99 RC |
209 | } |
210 | ||
dd480f78 | 211 | STRV_FOREACH_PAIR(ll, p, symlinks) { |
9421cab6 | 212 | _cleanup_free_ char *target, *f, *l; |
2397bc43 | 213 | |
dcd6361e | 214 | assert_se(f = strjoin(original_dir, *p)); |
dd480f78 | 215 | assert_se(l = strjoin(copy_dir, *ll)); |
641d1f99 | 216 | |
a5648b80 | 217 | assert_se(chase_symlinks(l, NULL, 0, &target, NULL) == 1); |
641d1f99 RC |
218 | assert_se(path_equal(f, target)); |
219 | } | |
220 | ||
dd480f78 LP |
221 | STRV_FOREACH_PAIR(ll, p, hardlinks) { |
222 | _cleanup_free_ char *f, *l; | |
223 | struct stat a, b; | |
224 | ||
225 | assert_se(f = strjoin(copy_dir, *p)); | |
226 | assert_se(l = strjoin(copy_dir, *ll)); | |
227 | ||
228 | assert_se(lstat(f, &a) >= 0); | |
229 | assert_se(lstat(l, &b) >= 0); | |
230 | ||
231 | assert_se(a.st_ino == b.st_ino); | |
232 | assert_se(a.st_dev == b.st_dev); | |
233 | } | |
234 | ||
0e2b2cac LP |
235 | unixsockp = strjoina(copy_dir, "unixsock"); |
236 | assert_se(stat(unixsockp, &st) >= 0); | |
237 | assert_se(S_ISSOCK(st.st_mode)); | |
238 | ||
d01cd401 LP |
239 | assert_se(copy_tree(original_dir, copy_dir, UID_INVALID, GID_INVALID, COPY_REFLINK) < 0); |
240 | assert_se(copy_tree("/tmp/inexistent/foo/bar/fsdoi", copy_dir, UID_INVALID, GID_INVALID, COPY_REFLINK) < 0); | |
641d1f99 | 241 | |
c6878637 LP |
242 | (void) rm_rf(copy_dir, REMOVE_ROOT|REMOVE_PHYSICAL); |
243 | (void) rm_rf(original_dir, REMOVE_ROOT|REMOVE_PHYSICAL); | |
641d1f99 RC |
244 | } |
245 | ||
4f7452a8 | 246 | TEST(copy_bytes) { |
ad5ecc11 ZJS |
247 | _cleanup_close_pair_ int pipefd[2] = {-1, -1}; |
248 | _cleanup_close_ int infd = -1; | |
249 | int r, r2; | |
250 | char buf[1024], buf2[1024]; | |
251 | ||
4f36d400 DJL |
252 | infd = open("/usr/lib/os-release", O_RDONLY|O_CLOEXEC); |
253 | if (infd < 0) | |
254 | infd = open("/etc/os-release", O_RDONLY|O_CLOEXEC); | |
ad5ecc11 ZJS |
255 | assert_se(infd >= 0); |
256 | ||
257 | assert_se(pipe2(pipefd, O_CLOEXEC) == 0); | |
258 | ||
f5fbe71d | 259 | r = copy_bytes(infd, pipefd[1], UINT64_MAX, 0); |
ad5ecc11 ZJS |
260 | assert_se(r == 0); |
261 | ||
262 | r = read(pipefd[0], buf, sizeof(buf)); | |
263 | assert_se(r >= 0); | |
264 | ||
265 | assert_se(lseek(infd, 0, SEEK_SET) == 0); | |
266 | r2 = read(infd, buf2, sizeof(buf2)); | |
267 | assert_se(r == r2); | |
268 | ||
269 | assert_se(strneq(buf, buf2, r)); | |
270 | ||
271 | /* test copy_bytes with invalid descriptors */ | |
1c876927 | 272 | r = copy_bytes(pipefd[0], pipefd[0], 1, 0); |
ad5ecc11 ZJS |
273 | assert_se(r == -EBADF); |
274 | ||
1c876927 | 275 | r = copy_bytes(pipefd[1], pipefd[1], 1, 0); |
ad5ecc11 ZJS |
276 | assert_se(r == -EBADF); |
277 | ||
1c876927 | 278 | r = copy_bytes(pipefd[1], infd, 1, 0); |
ad5ecc11 ZJS |
279 | assert_se(r == -EBADF); |
280 | } | |
281 | ||
4f7452a8 | 282 | static void test_copy_bytes_regular_file_one(const char *src, bool try_reflink, uint64_t max_bytes) { |
ce33fdda ZJS |
283 | char fn2[] = "/tmp/test-copy-file-XXXXXX"; |
284 | char fn3[] = "/tmp/test-copy-file-XXXXXX"; | |
285 | _cleanup_close_ int fd = -1, fd2 = -1, fd3 = -1; | |
286 | int r; | |
287 | struct stat buf, buf2, buf3; | |
288 | ||
fa13cf9e | 289 | log_info("%s try_reflink=%s max_bytes=%" PRIu64, __func__, yes_no(try_reflink), max_bytes); |
ce33fdda ZJS |
290 | |
291 | fd = open(src, O_RDONLY | O_CLOEXEC | O_NOCTTY); | |
292 | assert_se(fd >= 0); | |
293 | ||
646853bd | 294 | fd2 = mkostemp_safe(fn2); |
ce33fdda ZJS |
295 | assert_se(fd2 >= 0); |
296 | ||
646853bd | 297 | fd3 = mkostemp_safe(fn3); |
ce33fdda ZJS |
298 | assert_se(fd3 >= 0); |
299 | ||
1c876927 | 300 | r = copy_bytes(fd, fd2, max_bytes, try_reflink ? COPY_REFLINK : 0); |
f5fbe71d | 301 | if (max_bytes == UINT64_MAX) |
7a827fcb ZJS |
302 | assert_se(r == 0); |
303 | else | |
304 | assert_se(IN_SET(r, 0, 1)); | |
ce33fdda | 305 | |
6fbdf424 ZJS |
306 | assert_se(fstat(fd, &buf) == 0); |
307 | assert_se(fstat(fd2, &buf2) == 0); | |
308 | assert_se((uint64_t) buf2.st_size == MIN((uint64_t) buf.st_size, max_bytes)); | |
309 | ||
f5fbe71d | 310 | if (max_bytes < UINT64_MAX) |
6fbdf424 ZJS |
311 | /* Make sure the file is now higher than max_bytes */ |
312 | assert_se(ftruncate(fd2, max_bytes + 1) == 0); | |
313 | ||
ce33fdda ZJS |
314 | assert_se(lseek(fd2, 0, SEEK_SET) == 0); |
315 | ||
1c876927 | 316 | r = copy_bytes(fd2, fd3, max_bytes, try_reflink ? COPY_REFLINK : 0); |
f5fbe71d | 317 | if (max_bytes == UINT64_MAX) |
7a827fcb ZJS |
318 | assert_se(r == 0); |
319 | else | |
320 | /* We cannot distinguish between the input being exactly max_bytes | |
321 | * or longer than max_bytes (without trying to read one more byte, | |
322 | * or calling stat, or FION_READ, etc, and we don't want to do any | |
323 | * of that). So we expect "truncation" since we know that file we | |
324 | * are copying is exactly max_bytes bytes. */ | |
325 | assert_se(r == 1); | |
ce33fdda | 326 | |
ce33fdda ZJS |
327 | assert_se(fstat(fd3, &buf3) == 0); |
328 | ||
f5fbe71d | 329 | if (max_bytes == UINT64_MAX) |
6fbdf424 ZJS |
330 | assert_se(buf3.st_size == buf2.st_size); |
331 | else | |
332 | assert_se((uint64_t) buf3.st_size == max_bytes); | |
ce33fdda ZJS |
333 | |
334 | unlink(fn2); | |
335 | unlink(fn3); | |
336 | } | |
337 | ||
4f7452a8 JJ |
338 | TEST(copy_bytes_regular_file) { |
339 | test_copy_bytes_regular_file_one(saved_argv[0], false, UINT64_MAX); | |
340 | test_copy_bytes_regular_file_one(saved_argv[0], true, UINT64_MAX); | |
341 | test_copy_bytes_regular_file_one(saved_argv[0], false, 1000); /* smaller than copy buffer size */ | |
342 | test_copy_bytes_regular_file_one(saved_argv[0], true, 1000); | |
343 | test_copy_bytes_regular_file_one(saved_argv[0], false, 32000); /* larger than copy buffer size */ | |
344 | test_copy_bytes_regular_file_one(saved_argv[0], true, 32000); | |
345 | } | |
346 | ||
347 | TEST(copy_atomic) { | |
ec6bdf72 LP |
348 | _cleanup_(rm_rf_physical_and_freep) char *p = NULL; |
349 | const char *q; | |
350 | int r; | |
351 | ||
352 | assert_se(mkdtemp_malloc(NULL, &p) >= 0); | |
353 | ||
354 | q = strjoina(p, "/fstab"); | |
355 | ||
8a016c74 | 356 | r = copy_file_atomic("/etc/fstab", q, 0644, 0, 0, COPY_REFLINK); |
3c14dc61 | 357 | if (r == -ENOENT || ERRNO_IS_PRIVILEGE(r)) |
ec6bdf72 LP |
358 | return; |
359 | ||
8a016c74 | 360 | assert_se(copy_file_atomic("/etc/fstab", q, 0644, 0, 0, COPY_REFLINK) == -EEXIST); |
ec6bdf72 | 361 | |
8a016c74 | 362 | assert_se(copy_file_atomic("/etc/fstab", q, 0644, 0, 0, COPY_REPLACE) >= 0); |
ec6bdf72 LP |
363 | } |
364 | ||
4f7452a8 | 365 | TEST(copy_proc) { |
ee1aa61c LP |
366 | _cleanup_(rm_rf_physical_and_freep) char *p = NULL; |
367 | _cleanup_free_ char *f = NULL, *a = NULL, *b = NULL; | |
368 | ||
369 | /* Check if copying data from /proc/ works correctly, i.e. let's see if https://lwn.net/Articles/846403/ is a problem for us */ | |
370 | ||
371 | assert_se(mkdtemp_malloc(NULL, &p) >= 0); | |
372 | assert_se(f = path_join(p, "version")); | |
f5fbe71d | 373 | assert_se(copy_file("/proc/version", f, 0, MODE_INVALID, 0, 0, 0) >= 0); |
ee1aa61c LP |
374 | |
375 | assert_se(read_one_line_file("/proc/version", &a) >= 0); | |
376 | assert_se(read_one_line_file(f, &b) >= 0); | |
377 | assert_se(streq(a, b)); | |
57ac6959 | 378 | assert_se(!isempty(a)); |
ee1aa61c LP |
379 | } |
380 | ||
12727c2b DDM |
381 | TEST_RET(copy_holes) { |
382 | char fn[] = "/var/tmp/test-copy-hole-fd-XXXXXX"; | |
383 | char fn_copy[] = "/var/tmp/test-copy-hole-fd-XXXXXX"; | |
384 | struct stat stat; | |
f82f0b99 | 385 | off_t blksz; |
12727c2b | 386 | int r, fd, fd_copy; |
2a97a4b3 | 387 | char *buf; |
12727c2b DDM |
388 | |
389 | fd = mkostemp_safe(fn); | |
390 | assert_se(fd >= 0); | |
391 | ||
392 | fd_copy = mkostemp_safe(fn_copy); | |
f67b4351 | 393 | assert_se(fd_copy >= 0); |
12727c2b DDM |
394 | |
395 | r = RET_NERRNO(fallocate(fd, FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE, 0, 1)); | |
396 | if (ERRNO_IS_NOT_SUPPORTED(r)) | |
397 | return log_tests_skipped("Filesystem doesn't support hole punching"); | |
398 | assert_se(r >= 0); | |
399 | ||
f82f0b99 DDM |
400 | assert_se(fstat(fd, &stat) >= 0); |
401 | blksz = stat.st_blksize; | |
d66b77b4 | 402 | buf = alloca_safe(blksz); |
403 | memset(buf, 1, blksz); | |
12727c2b | 404 | |
f82f0b99 DDM |
405 | /* We need to make sure to create hole in multiples of the block size, otherwise filesystems (btrfs) |
406 | * might silently truncate/extend the holes. */ | |
407 | ||
408 | assert_se(lseek(fd, blksz, SEEK_CUR) >= 0); | |
409 | assert_se(write(fd, buf, blksz) >= 0); | |
410 | assert_se(lseek(fd, 0, SEEK_END) == 2 * blksz); | |
411 | /* Only ftruncate() can create holes at the end of a file. */ | |
412 | assert_se(ftruncate(fd, 3 * blksz) >= 0); | |
12727c2b DDM |
413 | assert_se(lseek(fd, 0, SEEK_SET) >= 0); |
414 | ||
415 | assert_se(copy_bytes(fd, fd_copy, UINT64_MAX, COPY_HOLES) >= 0); | |
416 | ||
417 | /* Test that the hole starts at the beginning of the file. */ | |
418 | assert_se(lseek(fd_copy, 0, SEEK_HOLE) == 0); | |
419 | /* Test that the hole has the expected size. */ | |
f82f0b99 DDM |
420 | assert_se(lseek(fd_copy, 0, SEEK_DATA) == blksz); |
421 | assert_se(lseek(fd_copy, blksz, SEEK_HOLE) == 2 * blksz); | |
422 | assert_se(lseek(fd_copy, 2 * blksz, SEEK_DATA) < 0 && errno == ENXIO); | |
12727c2b DDM |
423 | |
424 | /* Test that the copied file has the correct size. */ | |
425 | assert_se(fstat(fd_copy, &stat) >= 0); | |
f82f0b99 | 426 | assert_se(stat.st_size == 3 * blksz); |
12727c2b DDM |
427 | |
428 | close(fd); | |
429 | close(fd_copy); | |
430 | ||
431 | unlink(fn); | |
432 | unlink(fn_copy); | |
433 | ||
434 | return 0; | |
435 | } | |
436 | ||
4f7452a8 | 437 | DEFINE_TEST_MAIN(LOG_DEBUG); |