]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/test/test-copy.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
5 #include "alloc-util.h"
13 #include "path-util.h"
15 #include "string-util.h"
18 #include "user-util.h"
21 static void test_copy_file(void) {
22 _cleanup_free_
char *buf
= NULL
;
23 char fn
[] = "/tmp/test-copy_file.XXXXXX";
24 char fn_copy
[] = "/tmp/test-copy_file.XXXXXX";
28 log_info("%s", __func__
);
30 fd
= mkostemp_safe(fn
);
34 fd
= mkostemp_safe(fn_copy
);
38 assert_se(write_string_file(fn
, "foo bar bar bar foo", WRITE_STRING_FILE_CREATE
) == 0);
40 assert_se(copy_file(fn
, fn_copy
, 0, 0644, 0, COPY_REFLINK
) == 0);
42 assert_se(read_full_file(fn_copy
, &buf
, &sz
) == 0);
43 assert_se(streq(buf
, "foo bar bar bar foo\n"));
50 static void test_copy_file_fd(void) {
51 char in_fn
[] = "/tmp/test-copy-file-fd-XXXXXX";
52 char out_fn
[] = "/tmp/test-copy-file-fd-XXXXXX";
53 _cleanup_close_
int in_fd
= -1, out_fd
= -1;
54 char text
[] = "boohoo\nfoo\n\tbar\n";
57 log_info("%s", __func__
);
59 in_fd
= mkostemp_safe(in_fn
);
60 assert_se(in_fd
>= 0);
61 out_fd
= mkostemp_safe(out_fn
);
62 assert_se(out_fd
>= 0);
64 assert_se(write_string_file(in_fn
, text
, WRITE_STRING_FILE_CREATE
) == 0);
65 assert_se(copy_file_fd("/a/file/which/does/not/exist/i/guess", out_fd
, COPY_REFLINK
) < 0);
66 assert_se(copy_file_fd(in_fn
, out_fd
, COPY_REFLINK
) >= 0);
67 assert_se(lseek(out_fd
, SEEK_SET
, 0) == 0);
69 assert_se(read(out_fd
, buf
, sizeof(buf
)) == sizeof(text
) - 1);
70 assert_se(streq(buf
, text
));
76 static void test_copy_tree(void) {
77 char original_dir
[] = "/tmp/test-copy_tree/";
78 char copy_dir
[] = "/tmp/test-copy_tree-copy/";
79 char **files
= STRV_MAKE("file", "dir1/file", "dir1/dir2/file", "dir1/dir2/dir3/dir4/dir5/file");
80 char **links
= STRV_MAKE("link", "file",
81 "link2", "dir1/file");
83 const char *unixsockp
;
86 log_info("%s", __func__
);
88 (void) rm_rf(copy_dir
, REMOVE_ROOT
|REMOVE_PHYSICAL
);
89 (void) rm_rf(original_dir
, REMOVE_ROOT
|REMOVE_PHYSICAL
);
91 STRV_FOREACH(p
, files
) {
92 _cleanup_free_
char *f
;
94 assert_se(f
= strappend(original_dir
, *p
));
96 assert_se(mkdir_parents(f
, 0755) >= 0);
97 assert_se(write_string_file(f
, "file", WRITE_STRING_FILE_CREATE
) == 0);
100 STRV_FOREACH_PAIR(link
, p
, links
) {
101 _cleanup_free_
char *f
, *l
;
103 assert_se(f
= strappend(original_dir
, *p
));
104 assert_se(l
= strappend(original_dir
, *link
));
106 assert_se(mkdir_parents(l
, 0755) >= 0);
107 assert_se(symlink(f
, l
) == 0);
110 unixsockp
= strjoina(original_dir
, "unixsock");
111 assert_se(mknod(unixsockp
, S_IFSOCK
|0644, 0) >= 0);
113 assert_se(copy_tree(original_dir
, copy_dir
, UID_INVALID
, GID_INVALID
, COPY_REFLINK
|COPY_MERGE
) == 0);
115 STRV_FOREACH(p
, files
) {
116 _cleanup_free_
char *buf
, *f
;
119 assert_se(f
= strappend(copy_dir
, *p
));
121 assert_se(access(f
, F_OK
) == 0);
122 assert_se(read_full_file(f
, &buf
, &sz
) == 0);
123 assert_se(streq(buf
, "file\n"));
126 STRV_FOREACH_PAIR(link
, p
, links
) {
127 _cleanup_free_
char *target
, *f
, *l
;
129 assert_se(f
= strjoin(original_dir
, *p
));
130 assert_se(l
= strjoin(copy_dir
, *link
));
132 assert_se(chase_symlinks(l
, NULL
, 0, &target
) == 1);
133 assert_se(path_equal(f
, target
));
136 unixsockp
= strjoina(copy_dir
, "unixsock");
137 assert_se(stat(unixsockp
, &st
) >= 0);
138 assert_se(S_ISSOCK(st
.st_mode
));
140 assert_se(copy_tree(original_dir
, copy_dir
, UID_INVALID
, GID_INVALID
, COPY_REFLINK
) < 0);
141 assert_se(copy_tree("/tmp/inexistent/foo/bar/fsdoi", copy_dir
, UID_INVALID
, GID_INVALID
, COPY_REFLINK
) < 0);
143 (void) rm_rf(copy_dir
, REMOVE_ROOT
|REMOVE_PHYSICAL
);
144 (void) rm_rf(original_dir
, REMOVE_ROOT
|REMOVE_PHYSICAL
);
147 static void test_copy_bytes(void) {
148 _cleanup_close_pair_
int pipefd
[2] = {-1, -1};
149 _cleanup_close_
int infd
= -1;
151 char buf
[1024], buf2
[1024];
153 infd
= open("/usr/lib/os-release", O_RDONLY
|O_CLOEXEC
);
155 infd
= open("/etc/os-release", O_RDONLY
|O_CLOEXEC
);
156 assert_se(infd
>= 0);
158 assert_se(pipe2(pipefd
, O_CLOEXEC
) == 0);
160 r
= copy_bytes(infd
, pipefd
[1], (uint64_t) -1, 0);
163 r
= read(pipefd
[0], buf
, sizeof(buf
));
166 assert_se(lseek(infd
, 0, SEEK_SET
) == 0);
167 r2
= read(infd
, buf2
, sizeof(buf2
));
170 assert_se(strneq(buf
, buf2
, r
));
172 /* test copy_bytes with invalid descriptors */
173 r
= copy_bytes(pipefd
[0], pipefd
[0], 1, 0);
174 assert_se(r
== -EBADF
);
176 r
= copy_bytes(pipefd
[1], pipefd
[1], 1, 0);
177 assert_se(r
== -EBADF
);
179 r
= copy_bytes(pipefd
[1], infd
, 1, 0);
180 assert_se(r
== -EBADF
);
183 static void test_copy_bytes_regular_file(const char *src
, bool try_reflink
, uint64_t max_bytes
) {
184 char fn2
[] = "/tmp/test-copy-file-XXXXXX";
185 char fn3
[] = "/tmp/test-copy-file-XXXXXX";
186 _cleanup_close_
int fd
= -1, fd2
= -1, fd3
= -1;
188 struct stat buf
, buf2
, buf3
;
190 log_info("%s try_reflink=%s max_bytes=%" PRIu64
, __func__
, yes_no(try_reflink
), max_bytes
);
192 fd
= open(src
, O_RDONLY
| O_CLOEXEC
| O_NOCTTY
);
195 fd2
= mkostemp_safe(fn2
);
198 fd3
= mkostemp_safe(fn3
);
201 r
= copy_bytes(fd
, fd2
, max_bytes
, try_reflink
? COPY_REFLINK
: 0);
202 if (max_bytes
== (uint64_t) -1)
205 assert_se(IN_SET(r
, 0, 1));
207 assert_se(fstat(fd
, &buf
) == 0);
208 assert_se(fstat(fd2
, &buf2
) == 0);
209 assert_se((uint64_t) buf2
.st_size
== MIN((uint64_t) buf
.st_size
, max_bytes
));
211 if (max_bytes
< (uint64_t) -1)
212 /* Make sure the file is now higher than max_bytes */
213 assert_se(ftruncate(fd2
, max_bytes
+ 1) == 0);
215 assert_se(lseek(fd2
, 0, SEEK_SET
) == 0);
217 r
= copy_bytes(fd2
, fd3
, max_bytes
, try_reflink
? COPY_REFLINK
: 0);
218 if (max_bytes
== (uint64_t) -1)
221 /* We cannot distinguish between the input being exactly max_bytes
222 * or longer than max_bytes (without trying to read one more byte,
223 * or calling stat, or FION_READ, etc, and we don't want to do any
224 * of that). So we expect "truncation" since we know that file we
225 * are copying is exactly max_bytes bytes. */
228 assert_se(fstat(fd3
, &buf3
) == 0);
230 if (max_bytes
== (uint64_t) -1)
231 assert_se(buf3
.st_size
== buf2
.st_size
);
233 assert_se((uint64_t) buf3
.st_size
== max_bytes
);
239 static void test_copy_atomic(void) {
240 _cleanup_(rm_rf_physical_and_freep
) char *p
= NULL
;
244 assert_se(mkdtemp_malloc(NULL
, &p
) >= 0);
246 q
= strjoina(p
, "/fstab");
248 r
= copy_file_atomic("/etc/fstab", q
, 0644, 0, COPY_REFLINK
);
252 assert_se(copy_file_atomic("/etc/fstab", q
, 0644, 0, COPY_REFLINK
) == -EEXIST
);
254 assert_se(copy_file_atomic("/etc/fstab", q
, 0644, 0, COPY_REPLACE
) >= 0);
257 int main(int argc
, char *argv
[]) {
258 test_setup_logging(LOG_DEBUG
);
264 test_copy_bytes_regular_file(argv
[0], false, (uint64_t) -1);
265 test_copy_bytes_regular_file(argv
[0], true, (uint64_t) -1);
266 test_copy_bytes_regular_file(argv
[0], false, 1000); /* smaller than copy buffer size */
267 test_copy_bytes_regular_file(argv
[0], true, 1000);
268 test_copy_bytes_regular_file(argv
[0], false, 32000); /* larger than copy buffer size */
269 test_copy_bytes_regular_file(argv
[0], true, 32000);