]>
Commit | Line | Data |
---|---|---|
53e1b683 | 1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
641d1f99 RC |
2 | |
3 | #include <unistd.h> | |
4 | ||
b5efdb8a | 5 | #include "alloc-util.h" |
641d1f99 | 6 | #include "copy.h" |
3ffd4af2 | 7 | #include "fd-util.h" |
641d1f99 | 8 | #include "fileio.h" |
f4f15635 | 9 | #include "fs-util.h" |
ce33fdda | 10 | #include "log.h" |
07630cea | 11 | #include "macro.h" |
641d1f99 | 12 | #include "mkdir.h" |
07630cea LP |
13 | #include "path-util.h" |
14 | #include "rm-rf.h" | |
15 | #include "string-util.h" | |
641d1f99 | 16 | #include "strv.h" |
6d7c4033 | 17 | #include "tests.h" |
e4de7287 | 18 | #include "tmpfile-util.h" |
d01cd401 | 19 | #include "user-util.h" |
641d1f99 RC |
20 | #include "util.h" |
21 | ||
22 | static void test_copy_file(void) { | |
23 | _cleanup_free_ char *buf = NULL; | |
24 | char fn[] = "/tmp/test-copy_file.XXXXXX"; | |
25 | char fn_copy[] = "/tmp/test-copy_file.XXXXXX"; | |
26 | size_t sz = 0; | |
27 | int fd; | |
28 | ||
ce33fdda ZJS |
29 | log_info("%s", __func__); |
30 | ||
646853bd | 31 | fd = mkostemp_safe(fn); |
641d1f99 RC |
32 | assert_se(fd >= 0); |
33 | close(fd); | |
34 | ||
646853bd | 35 | fd = mkostemp_safe(fn_copy); |
641d1f99 RC |
36 | assert_se(fd >= 0); |
37 | close(fd); | |
38 | ||
4c1fc3e4 | 39 | assert_se(write_string_file(fn, "foo bar bar bar foo", WRITE_STRING_FILE_CREATE) == 0); |
641d1f99 | 40 | |
8a016c74 | 41 | assert_se(copy_file(fn, fn_copy, 0, 0644, 0, 0, COPY_REFLINK) == 0); |
641d1f99 RC |
42 | |
43 | assert_se(read_full_file(fn_copy, &buf, &sz) == 0); | |
44 | assert_se(streq(buf, "foo bar bar bar foo\n")); | |
cda134ab | 45 | assert_se(sz == 20); |
641d1f99 RC |
46 | |
47 | unlink(fn); | |
48 | unlink(fn_copy); | |
49 | } | |
50 | ||
cda134ab LP |
51 | static void test_copy_file_fd(void) { |
52 | char in_fn[] = "/tmp/test-copy-file-fd-XXXXXX"; | |
53 | char out_fn[] = "/tmp/test-copy-file-fd-XXXXXX"; | |
54 | _cleanup_close_ int in_fd = -1, out_fd = -1; | |
55 | char text[] = "boohoo\nfoo\n\tbar\n"; | |
56 | char buf[64] = {0}; | |
57 | ||
ce33fdda ZJS |
58 | log_info("%s", __func__); |
59 | ||
646853bd | 60 | in_fd = mkostemp_safe(in_fn); |
cda134ab | 61 | assert_se(in_fd >= 0); |
646853bd | 62 | out_fd = mkostemp_safe(out_fn); |
cda134ab LP |
63 | assert_se(out_fd >= 0); |
64 | ||
4c1fc3e4 | 65 | assert_se(write_string_file(in_fn, text, WRITE_STRING_FILE_CREATE) == 0); |
1c876927 LP |
66 | assert_se(copy_file_fd("/a/file/which/does/not/exist/i/guess", out_fd, COPY_REFLINK) < 0); |
67 | assert_se(copy_file_fd(in_fn, out_fd, COPY_REFLINK) >= 0); | |
cda134ab LP |
68 | assert_se(lseek(out_fd, SEEK_SET, 0) == 0); |
69 | ||
70 | assert_se(read(out_fd, buf, sizeof(buf)) == sizeof(text) - 1); | |
71 | assert_se(streq(buf, text)); | |
72 | ||
73 | unlink(in_fn); | |
74 | unlink(out_fn); | |
75 | } | |
76 | ||
641d1f99 RC |
77 | static void test_copy_tree(void) { |
78 | char original_dir[] = "/tmp/test-copy_tree/"; | |
79 | char copy_dir[] = "/tmp/test-copy_tree-copy/"; | |
80 | char **files = STRV_MAKE("file", "dir1/file", "dir1/dir2/file", "dir1/dir2/dir3/dir4/dir5/file"); | |
81 | char **links = STRV_MAKE("link", "file", | |
82 | "link2", "dir1/file"); | |
83 | char **p, **link; | |
0e2b2cac LP |
84 | const char *unixsockp; |
85 | struct stat st; | |
641d1f99 | 86 | |
ce33fdda ZJS |
87 | log_info("%s", __func__); |
88 | ||
c6878637 LP |
89 | (void) rm_rf(copy_dir, REMOVE_ROOT|REMOVE_PHYSICAL); |
90 | (void) rm_rf(original_dir, REMOVE_ROOT|REMOVE_PHYSICAL); | |
641d1f99 RC |
91 | |
92 | STRV_FOREACH(p, files) { | |
2397bc43 LP |
93 | _cleanup_free_ char *f; |
94 | ||
dcd6361e | 95 | assert_se(f = strappend(original_dir, *p)); |
641d1f99 RC |
96 | |
97 | assert_se(mkdir_parents(f, 0755) >= 0); | |
4c1fc3e4 | 98 | assert_se(write_string_file(f, "file", WRITE_STRING_FILE_CREATE) == 0); |
641d1f99 RC |
99 | } |
100 | ||
101 | STRV_FOREACH_PAIR(link, p, links) { | |
2397bc43 LP |
102 | _cleanup_free_ char *f, *l; |
103 | ||
dcd6361e ZJS |
104 | assert_se(f = strappend(original_dir, *p)); |
105 | assert_se(l = strappend(original_dir, *link)); | |
641d1f99 RC |
106 | |
107 | assert_se(mkdir_parents(l, 0755) >= 0); | |
108 | assert_se(symlink(f, l) == 0); | |
109 | } | |
110 | ||
0e2b2cac LP |
111 | unixsockp = strjoina(original_dir, "unixsock"); |
112 | assert_se(mknod(unixsockp, S_IFSOCK|0644, 0) >= 0); | |
113 | ||
d01cd401 | 114 | assert_se(copy_tree(original_dir, copy_dir, UID_INVALID, GID_INVALID, COPY_REFLINK|COPY_MERGE) == 0); |
641d1f99 RC |
115 | |
116 | STRV_FOREACH(p, files) { | |
9421cab6 ZJS |
117 | _cleanup_free_ char *buf, *f; |
118 | size_t sz; | |
2397bc43 | 119 | |
dcd6361e | 120 | assert_se(f = strappend(copy_dir, *p)); |
641d1f99 RC |
121 | |
122 | assert_se(access(f, F_OK) == 0); | |
123 | assert_se(read_full_file(f, &buf, &sz) == 0); | |
124 | assert_se(streq(buf, "file\n")); | |
125 | } | |
126 | ||
127 | STRV_FOREACH_PAIR(link, p, links) { | |
9421cab6 | 128 | _cleanup_free_ char *target, *f, *l; |
2397bc43 | 129 | |
dcd6361e ZJS |
130 | assert_se(f = strjoin(original_dir, *p)); |
131 | assert_se(l = strjoin(copy_dir, *link)); | |
641d1f99 | 132 | |
842e456e | 133 | assert_se(chase_symlinks(l, NULL, 0, &target) == 1); |
641d1f99 RC |
134 | assert_se(path_equal(f, target)); |
135 | } | |
136 | ||
0e2b2cac LP |
137 | unixsockp = strjoina(copy_dir, "unixsock"); |
138 | assert_se(stat(unixsockp, &st) >= 0); | |
139 | assert_se(S_ISSOCK(st.st_mode)); | |
140 | ||
d01cd401 LP |
141 | assert_se(copy_tree(original_dir, copy_dir, UID_INVALID, GID_INVALID, COPY_REFLINK) < 0); |
142 | assert_se(copy_tree("/tmp/inexistent/foo/bar/fsdoi", copy_dir, UID_INVALID, GID_INVALID, COPY_REFLINK) < 0); | |
641d1f99 | 143 | |
c6878637 LP |
144 | (void) rm_rf(copy_dir, REMOVE_ROOT|REMOVE_PHYSICAL); |
145 | (void) rm_rf(original_dir, REMOVE_ROOT|REMOVE_PHYSICAL); | |
641d1f99 RC |
146 | } |
147 | ||
ad5ecc11 ZJS |
148 | static void test_copy_bytes(void) { |
149 | _cleanup_close_pair_ int pipefd[2] = {-1, -1}; | |
150 | _cleanup_close_ int infd = -1; | |
151 | int r, r2; | |
152 | char buf[1024], buf2[1024]; | |
153 | ||
4f36d400 DJL |
154 | infd = open("/usr/lib/os-release", O_RDONLY|O_CLOEXEC); |
155 | if (infd < 0) | |
156 | infd = open("/etc/os-release", O_RDONLY|O_CLOEXEC); | |
ad5ecc11 ZJS |
157 | assert_se(infd >= 0); |
158 | ||
159 | assert_se(pipe2(pipefd, O_CLOEXEC) == 0); | |
160 | ||
1c876927 | 161 | r = copy_bytes(infd, pipefd[1], (uint64_t) -1, 0); |
ad5ecc11 ZJS |
162 | assert_se(r == 0); |
163 | ||
164 | r = read(pipefd[0], buf, sizeof(buf)); | |
165 | assert_se(r >= 0); | |
166 | ||
167 | assert_se(lseek(infd, 0, SEEK_SET) == 0); | |
168 | r2 = read(infd, buf2, sizeof(buf2)); | |
169 | assert_se(r == r2); | |
170 | ||
171 | assert_se(strneq(buf, buf2, r)); | |
172 | ||
173 | /* test copy_bytes with invalid descriptors */ | |
1c876927 | 174 | r = copy_bytes(pipefd[0], pipefd[0], 1, 0); |
ad5ecc11 ZJS |
175 | assert_se(r == -EBADF); |
176 | ||
1c876927 | 177 | r = copy_bytes(pipefd[1], pipefd[1], 1, 0); |
ad5ecc11 ZJS |
178 | assert_se(r == -EBADF); |
179 | ||
1c876927 | 180 | r = copy_bytes(pipefd[1], infd, 1, 0); |
ad5ecc11 ZJS |
181 | assert_se(r == -EBADF); |
182 | } | |
183 | ||
fa13cf9e | 184 | static void test_copy_bytes_regular_file(const char *src, bool try_reflink, uint64_t max_bytes) { |
ce33fdda ZJS |
185 | char fn2[] = "/tmp/test-copy-file-XXXXXX"; |
186 | char fn3[] = "/tmp/test-copy-file-XXXXXX"; | |
187 | _cleanup_close_ int fd = -1, fd2 = -1, fd3 = -1; | |
188 | int r; | |
189 | struct stat buf, buf2, buf3; | |
190 | ||
fa13cf9e | 191 | log_info("%s try_reflink=%s max_bytes=%" PRIu64, __func__, yes_no(try_reflink), max_bytes); |
ce33fdda ZJS |
192 | |
193 | fd = open(src, O_RDONLY | O_CLOEXEC | O_NOCTTY); | |
194 | assert_se(fd >= 0); | |
195 | ||
646853bd | 196 | fd2 = mkostemp_safe(fn2); |
ce33fdda ZJS |
197 | assert_se(fd2 >= 0); |
198 | ||
646853bd | 199 | fd3 = mkostemp_safe(fn3); |
ce33fdda ZJS |
200 | assert_se(fd3 >= 0); |
201 | ||
1c876927 | 202 | r = copy_bytes(fd, fd2, max_bytes, try_reflink ? COPY_REFLINK : 0); |
7a827fcb ZJS |
203 | if (max_bytes == (uint64_t) -1) |
204 | assert_se(r == 0); | |
205 | else | |
206 | assert_se(IN_SET(r, 0, 1)); | |
ce33fdda | 207 | |
6fbdf424 ZJS |
208 | assert_se(fstat(fd, &buf) == 0); |
209 | assert_se(fstat(fd2, &buf2) == 0); | |
210 | assert_se((uint64_t) buf2.st_size == MIN((uint64_t) buf.st_size, max_bytes)); | |
211 | ||
212 | if (max_bytes < (uint64_t) -1) | |
213 | /* Make sure the file is now higher than max_bytes */ | |
214 | assert_se(ftruncate(fd2, max_bytes + 1) == 0); | |
215 | ||
ce33fdda ZJS |
216 | assert_se(lseek(fd2, 0, SEEK_SET) == 0); |
217 | ||
1c876927 | 218 | r = copy_bytes(fd2, fd3, max_bytes, try_reflink ? COPY_REFLINK : 0); |
7a827fcb ZJS |
219 | if (max_bytes == (uint64_t) -1) |
220 | assert_se(r == 0); | |
221 | else | |
222 | /* We cannot distinguish between the input being exactly max_bytes | |
223 | * or longer than max_bytes (without trying to read one more byte, | |
224 | * or calling stat, or FION_READ, etc, and we don't want to do any | |
225 | * of that). So we expect "truncation" since we know that file we | |
226 | * are copying is exactly max_bytes bytes. */ | |
227 | assert_se(r == 1); | |
ce33fdda | 228 | |
ce33fdda ZJS |
229 | assert_se(fstat(fd3, &buf3) == 0); |
230 | ||
6fbdf424 ZJS |
231 | if (max_bytes == (uint64_t) -1) |
232 | assert_se(buf3.st_size == buf2.st_size); | |
233 | else | |
234 | assert_se((uint64_t) buf3.st_size == max_bytes); | |
ce33fdda ZJS |
235 | |
236 | unlink(fn2); | |
237 | unlink(fn3); | |
238 | } | |
239 | ||
ec6bdf72 LP |
240 | static void test_copy_atomic(void) { |
241 | _cleanup_(rm_rf_physical_and_freep) char *p = NULL; | |
242 | const char *q; | |
243 | int r; | |
244 | ||
245 | assert_se(mkdtemp_malloc(NULL, &p) >= 0); | |
246 | ||
247 | q = strjoina(p, "/fstab"); | |
248 | ||
8a016c74 | 249 | r = copy_file_atomic("/etc/fstab", q, 0644, 0, 0, COPY_REFLINK); |
ec6bdf72 LP |
250 | if (r == -ENOENT) |
251 | return; | |
252 | ||
8a016c74 | 253 | assert_se(copy_file_atomic("/etc/fstab", q, 0644, 0, 0, COPY_REFLINK) == -EEXIST); |
ec6bdf72 | 254 | |
8a016c74 | 255 | assert_se(copy_file_atomic("/etc/fstab", q, 0644, 0, 0, COPY_REPLACE) >= 0); |
ec6bdf72 LP |
256 | } |
257 | ||
641d1f99 | 258 | int main(int argc, char *argv[]) { |
6d7c4033 | 259 | test_setup_logging(LOG_DEBUG); |
ec6bdf72 | 260 | |
641d1f99 | 261 | test_copy_file(); |
cda134ab | 262 | test_copy_file_fd(); |
641d1f99 | 263 | test_copy_tree(); |
ad5ecc11 | 264 | test_copy_bytes(); |
7a827fcb ZJS |
265 | test_copy_bytes_regular_file(argv[0], false, (uint64_t) -1); |
266 | test_copy_bytes_regular_file(argv[0], true, (uint64_t) -1); | |
267 | test_copy_bytes_regular_file(argv[0], false, 1000); /* smaller than copy buffer size */ | |
268 | test_copy_bytes_regular_file(argv[0], true, 1000); | |
269 | test_copy_bytes_regular_file(argv[0], false, 32000); /* larger than copy buffer size */ | |
270 | test_copy_bytes_regular_file(argv[0], true, 32000); | |
ec6bdf72 | 271 | test_copy_atomic(); |
641d1f99 RC |
272 | |
273 | return 0; | |
274 | } |