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