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