1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 Copyright © 2014 Ronny Chevalier
8 #include "alloc-util.h"
16 #include "path-util.h"
18 #include "string-util.h"
20 #include "user-util.h"
23 #if 0 /// UNNEEDED by elogind
24 static void test_copy_file(void) {
25 _cleanup_free_ char *buf = NULL;
26 char fn[] = "/tmp/test-copy_file.XXXXXX";
27 char fn_copy[] = "/tmp/test-copy_file.XXXXXX";
31 log_info("%s", __func__);
33 fd = mkostemp_safe(fn);
37 fd = mkostemp_safe(fn_copy);
41 assert_se(write_string_file(fn, "foo bar bar bar foo", WRITE_STRING_FILE_CREATE) == 0);
43 assert_se(copy_file(fn, fn_copy, 0, 0644, 0, COPY_REFLINK) == 0);
45 assert_se(read_full_file(fn_copy, &buf, &sz) == 0);
46 assert_se(streq(buf, "foo bar bar bar foo\n"));
53 static void test_copy_file_fd(void) {
54 char in_fn[] = "/tmp/test-copy-file-fd-XXXXXX";
55 char out_fn[] = "/tmp/test-copy-file-fd-XXXXXX";
56 _cleanup_close_ int in_fd = -1, out_fd = -1;
57 char text[] = "boohoo\nfoo\n\tbar\n";
60 log_info("%s", __func__);
62 in_fd = mkostemp_safe(in_fn);
63 assert_se(in_fd >= 0);
64 out_fd = mkostemp_safe(out_fn);
65 assert_se(out_fd >= 0);
67 assert_se(write_string_file(in_fn, text, WRITE_STRING_FILE_CREATE) == 0);
68 assert_se(copy_file_fd("/a/file/which/does/not/exist/i/guess", out_fd, COPY_REFLINK) < 0);
69 assert_se(copy_file_fd(in_fn, out_fd, COPY_REFLINK) >= 0);
70 assert_se(lseek(out_fd, SEEK_SET, 0) == 0);
72 assert_se(read(out_fd, buf, sizeof(buf)) == sizeof(text) - 1);
73 assert_se(streq(buf, text));
79 static void test_copy_tree(void) {
80 char original_dir[] = "/tmp/test-copy_tree/";
81 char copy_dir[] = "/tmp/test-copy_tree-copy/";
82 char **files = STRV_MAKE("file", "dir1/file", "dir1/dir2/file", "dir1/dir2/dir3/dir4/dir5/file");
83 char **links = STRV_MAKE("link", "file",
84 "link2", "dir1/file");
86 const char *unixsockp;
89 log_info("%s", __func__);
91 (void) rm_rf(copy_dir, REMOVE_ROOT|REMOVE_PHYSICAL);
92 (void) rm_rf(original_dir, REMOVE_ROOT|REMOVE_PHYSICAL);
94 STRV_FOREACH(p, files) {
95 _cleanup_free_ char *f;
97 assert_se(f = strappend(original_dir, *p));
99 assert_se(mkdir_parents(f, 0755) >= 0);
100 assert_se(write_string_file(f, "file", WRITE_STRING_FILE_CREATE) == 0);
103 STRV_FOREACH_PAIR(link, p, links) {
104 _cleanup_free_ char *f, *l;
106 assert_se(f = strappend(original_dir, *p));
107 assert_se(l = strappend(original_dir, *link));
109 assert_se(mkdir_parents(l, 0755) >= 0);
110 assert_se(symlink(f, l) == 0);
113 unixsockp = strjoina(original_dir, "unixsock");
114 assert_se(mknod(unixsockp, S_IFSOCK|0644, 0) >= 0);
116 assert_se(copy_tree(original_dir, copy_dir, UID_INVALID, GID_INVALID, COPY_REFLINK|COPY_MERGE) == 0);
118 STRV_FOREACH(p, files) {
119 _cleanup_free_ char *buf, *f;
122 assert_se(f = strappend(copy_dir, *p));
124 assert_se(access(f, F_OK) == 0);
125 assert_se(read_full_file(f, &buf, &sz) == 0);
126 assert_se(streq(buf, "file\n"));
129 STRV_FOREACH_PAIR(link, p, links) {
130 _cleanup_free_ char *target, *f, *l;
132 assert_se(f = strjoin(original_dir, *p));
133 assert_se(l = strjoin(copy_dir, *link));
135 assert_se(chase_symlinks(l, NULL, 0, &target) == 1);
136 assert_se(path_equal(f, target));
139 unixsockp = strjoina(copy_dir, "unixsock");
140 assert_se(stat(unixsockp, &st) >= 0);
141 assert_se(S_ISSOCK(st.st_mode));
143 assert_se(copy_tree(original_dir, copy_dir, UID_INVALID, GID_INVALID, COPY_REFLINK) < 0);
144 assert_se(copy_tree("/tmp/inexistent/foo/bar/fsdoi", copy_dir, UID_INVALID, GID_INVALID, COPY_REFLINK) < 0);
146 (void) rm_rf(copy_dir, REMOVE_ROOT|REMOVE_PHYSICAL);
147 (void) rm_rf(original_dir, REMOVE_ROOT|REMOVE_PHYSICAL);
151 static void test_copy_bytes(void) {
152 _cleanup_close_pair_ int pipefd[2] = {-1, -1};
153 _cleanup_close_ int infd = -1;
155 char buf[1024], buf2[1024];
157 infd = open("/usr/lib/os-release", O_RDONLY|O_CLOEXEC);
159 infd = open("/etc/os-release", O_RDONLY|O_CLOEXEC);
160 assert_se(infd >= 0);
162 assert_se(pipe2(pipefd, O_CLOEXEC) == 0);
164 r = copy_bytes(infd, pipefd[1], (uint64_t) -1, 0);
167 r = read(pipefd[0], buf, sizeof(buf));
170 assert_se(lseek(infd, 0, SEEK_SET) == 0);
171 r2 = read(infd, buf2, sizeof(buf2));
174 assert_se(strneq(buf, buf2, r));
176 /* test copy_bytes with invalid descriptors */
177 r = copy_bytes(pipefd[0], pipefd[0], 1, 0);
178 assert_se(r == -EBADF);
180 r = copy_bytes(pipefd[1], pipefd[1], 1, 0);
181 assert_se(r == -EBADF);
183 r = copy_bytes(pipefd[1], infd, 1, 0);
184 assert_se(r == -EBADF);
187 static void test_copy_bytes_regular_file(const char *src, bool try_reflink, uint64_t max_bytes) {
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;
192 struct stat buf, buf2, buf3;
194 log_info("%s try_reflink=%s max_bytes=%" PRIu64, __func__, yes_no(try_reflink), max_bytes);
196 fd = open(src, O_RDONLY | O_CLOEXEC | O_NOCTTY);
199 fd2 = mkostemp_safe(fn2);
202 fd3 = mkostemp_safe(fn3);
205 r = copy_bytes(fd, fd2, max_bytes, try_reflink ? COPY_REFLINK : 0);
206 if (max_bytes == (uint64_t) -1)
209 assert_se(IN_SET(r, 0, 1));
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));
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);
219 assert_se(lseek(fd2, 0, SEEK_SET) == 0);
221 r = copy_bytes(fd2, fd3, max_bytes, try_reflink ? COPY_REFLINK : 0);
222 if (max_bytes == (uint64_t) -1)
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. */
232 assert_se(fstat(fd3, &buf3) == 0);
234 if (max_bytes == (uint64_t) -1)
235 assert_se(buf3.st_size == buf2.st_size);
237 assert_se((uint64_t) buf3.st_size == max_bytes);
243 int main(int argc, char *argv[]) {
245 #if 0 /// UNNEEDED by elogind
251 test_copy_bytes_regular_file(argv[0], false, (uint64_t) -1);
252 test_copy_bytes_regular_file(argv[0], true, (uint64_t) -1);
253 test_copy_bytes_regular_file(argv[0], false, 1000); /* smaller than copy buffer size */
254 test_copy_bytes_regular_file(argv[0], true, 1000);
255 test_copy_bytes_regular_file(argv[0], false, 32000); /* larger than copy buffer size */
256 test_copy_bytes_regular_file(argv[0], true, 32000);