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