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