]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/test/test-copy.c
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
6 #include "alloc-util.h"
7 #include "chase-symlinks.h"
12 #include "hexdecoct.h"
16 #include "path-util.h"
18 #include "string-util.h"
21 #include "tmpfile-util.h"
22 #include "user-util.h"
24 #include "xattr-util.h"
26 static void test_copy_file(void) {
27 _cleanup_free_
char *buf
= NULL
;
28 char fn
[] = "/tmp/test-copy_file.XXXXXX";
29 char fn_copy
[] = "/tmp/test-copy_file.XXXXXX";
33 log_info("%s", __func__
);
35 fd
= mkostemp_safe(fn
);
39 fd
= mkostemp_safe(fn_copy
);
43 assert_se(write_string_file(fn
, "foo bar bar bar foo", WRITE_STRING_FILE_CREATE
) == 0);
45 assert_se(copy_file(fn
, fn_copy
, 0, 0644, 0, 0, COPY_REFLINK
) == 0);
47 assert_se(read_full_file(fn_copy
, &buf
, &sz
) == 0);
48 assert_se(streq(buf
, "foo bar bar bar foo\n"));
55 static void test_copy_file_fd(void) {
56 char in_fn
[] = "/tmp/test-copy-file-fd-XXXXXX";
57 char out_fn
[] = "/tmp/test-copy-file-fd-XXXXXX";
58 _cleanup_close_
int in_fd
= -1, out_fd
= -1;
59 const char *text
= "boohoo\nfoo\n\tbar\n";
62 log_info("%s", __func__
);
64 in_fd
= mkostemp_safe(in_fn
);
65 assert_se(in_fd
>= 0);
66 out_fd
= mkostemp_safe(out_fn
);
67 assert_se(out_fd
>= 0);
69 assert_se(write_string_file(in_fn
, text
, WRITE_STRING_FILE_CREATE
) == 0);
70 assert_se(copy_file_fd("/a/file/which/does/not/exist/i/guess", out_fd
, COPY_REFLINK
) < 0);
71 assert_se(copy_file_fd(in_fn
, out_fd
, COPY_REFLINK
) >= 0);
72 assert_se(lseek(out_fd
, SEEK_SET
, 0) == 0);
74 assert_se(read(out_fd
, buf
, sizeof buf
) == (ssize_t
) strlen(text
));
75 assert_se(streq(buf
, text
));
81 static void test_copy_tree(void) {
82 char original_dir
[] = "/tmp/test-copy_tree/";
83 char copy_dir
[] = "/tmp/test-copy_tree-copy/";
84 char **files
= STRV_MAKE("file", "dir1/file", "dir1/dir2/file", "dir1/dir2/dir3/dir4/dir5/file");
85 char **symlinks
= STRV_MAKE("link", "file",
86 "link2", "dir1/file");
87 char **hardlinks
= STRV_MAKE("hlink", "file",
88 "hlink2", "dir1/file");
89 const char *unixsockp
;
92 int xattr_worked
= -1; /* xattr support is optional in temporary directories, hence use it if we can,
93 * but don't fail if we can't */
95 log_info("%s", __func__
);
97 (void) rm_rf(copy_dir
, REMOVE_ROOT
|REMOVE_PHYSICAL
);
98 (void) rm_rf(original_dir
, REMOVE_ROOT
|REMOVE_PHYSICAL
);
100 STRV_FOREACH(p
, files
) {
101 _cleanup_free_
char *f
, *c
;
104 assert_se(f
= path_join(original_dir
, *p
));
106 assert_se(mkdir_parents(f
, 0755) >= 0);
107 assert_se(write_string_file(f
, "file", WRITE_STRING_FILE_CREATE
) == 0);
109 assert_se(base64mem(*p
, strlen(*p
), &c
) >= 0);
111 k
= setxattr(f
, "user.testxattr", c
, strlen(c
), 0);
112 assert_se(xattr_worked
< 0 || ((k
>= 0) == !!xattr_worked
));
113 xattr_worked
= k
>= 0;
116 STRV_FOREACH_PAIR(ll
, p
, symlinks
) {
117 _cleanup_free_
char *f
, *l
;
119 assert_se(f
= path_join(original_dir
, *p
));
120 assert_se(l
= path_join(original_dir
, *ll
));
122 assert_se(mkdir_parents(l
, 0755) >= 0);
123 assert_se(symlink(f
, l
) == 0);
126 STRV_FOREACH_PAIR(ll
, p
, hardlinks
) {
127 _cleanup_free_
char *f
, *l
;
129 assert_se(f
= path_join(original_dir
, *p
));
130 assert_se(l
= path_join(original_dir
, *ll
));
132 assert_se(mkdir_parents(l
, 0755) >= 0);
133 assert_se(link(f
, l
) == 0);
136 unixsockp
= strjoina(original_dir
, "unixsock");
137 assert_se(mknod(unixsockp
, S_IFSOCK
|0644, 0) >= 0);
139 assert_se(copy_tree(original_dir
, copy_dir
, UID_INVALID
, GID_INVALID
, COPY_REFLINK
|COPY_MERGE
|COPY_HARDLINKS
) == 0);
141 STRV_FOREACH(p
, files
) {
142 _cleanup_free_
char *buf
, *f
, *c
= NULL
;
146 assert_se(f
= path_join(copy_dir
, *p
));
148 assert_se(access(f
, F_OK
) == 0);
149 assert_se(read_full_file(f
, &buf
, &sz
) == 0);
150 assert_se(streq(buf
, "file\n"));
152 k
= getxattr_malloc(f
, "user.testxattr", &c
, false);
153 assert_se(xattr_worked
< 0 || ((k
>= 0) == !!xattr_worked
));
156 _cleanup_free_
char *d
= NULL
;
158 assert_se(base64mem(*p
, strlen(*p
), &d
) >= 0);
159 assert_se(streq(d
, c
));
163 STRV_FOREACH_PAIR(ll
, p
, symlinks
) {
164 _cleanup_free_
char *target
, *f
, *l
;
166 assert_se(f
= strjoin(original_dir
, *p
));
167 assert_se(l
= strjoin(copy_dir
, *ll
));
169 assert_se(chase_symlinks(l
, NULL
, 0, &target
, NULL
) == 1);
170 assert_se(path_equal(f
, target
));
173 STRV_FOREACH_PAIR(ll
, p
, hardlinks
) {
174 _cleanup_free_
char *f
, *l
;
177 assert_se(f
= strjoin(copy_dir
, *p
));
178 assert_se(l
= strjoin(copy_dir
, *ll
));
180 assert_se(lstat(f
, &a
) >= 0);
181 assert_se(lstat(l
, &b
) >= 0);
183 assert_se(a
.st_ino
== b
.st_ino
);
184 assert_se(a
.st_dev
== b
.st_dev
);
187 unixsockp
= strjoina(copy_dir
, "unixsock");
188 assert_se(stat(unixsockp
, &st
) >= 0);
189 assert_se(S_ISSOCK(st
.st_mode
));
191 assert_se(copy_tree(original_dir
, copy_dir
, UID_INVALID
, GID_INVALID
, COPY_REFLINK
) < 0);
192 assert_se(copy_tree("/tmp/inexistent/foo/bar/fsdoi", copy_dir
, UID_INVALID
, GID_INVALID
, COPY_REFLINK
) < 0);
194 (void) rm_rf(copy_dir
, REMOVE_ROOT
|REMOVE_PHYSICAL
);
195 (void) rm_rf(original_dir
, REMOVE_ROOT
|REMOVE_PHYSICAL
);
198 static void test_copy_bytes(void) {
199 _cleanup_close_pair_
int pipefd
[2] = {-1, -1};
200 _cleanup_close_
int infd
= -1;
202 char buf
[1024], buf2
[1024];
204 infd
= open("/usr/lib/os-release", O_RDONLY
|O_CLOEXEC
);
206 infd
= open("/etc/os-release", O_RDONLY
|O_CLOEXEC
);
207 assert_se(infd
>= 0);
209 assert_se(pipe2(pipefd
, O_CLOEXEC
) == 0);
211 r
= copy_bytes(infd
, pipefd
[1], UINT64_MAX
, 0);
214 r
= read(pipefd
[0], buf
, sizeof(buf
));
217 assert_se(lseek(infd
, 0, SEEK_SET
) == 0);
218 r2
= read(infd
, buf2
, sizeof(buf2
));
221 assert_se(strneq(buf
, buf2
, r
));
223 /* test copy_bytes with invalid descriptors */
224 r
= copy_bytes(pipefd
[0], pipefd
[0], 1, 0);
225 assert_se(r
== -EBADF
);
227 r
= copy_bytes(pipefd
[1], pipefd
[1], 1, 0);
228 assert_se(r
== -EBADF
);
230 r
= copy_bytes(pipefd
[1], infd
, 1, 0);
231 assert_se(r
== -EBADF
);
234 static void test_copy_bytes_regular_file(const char *src
, bool try_reflink
, uint64_t max_bytes
) {
235 char fn2
[] = "/tmp/test-copy-file-XXXXXX";
236 char fn3
[] = "/tmp/test-copy-file-XXXXXX";
237 _cleanup_close_
int fd
= -1, fd2
= -1, fd3
= -1;
239 struct stat buf
, buf2
, buf3
;
241 log_info("%s try_reflink=%s max_bytes=%" PRIu64
, __func__
, yes_no(try_reflink
), max_bytes
);
243 fd
= open(src
, O_RDONLY
| O_CLOEXEC
| O_NOCTTY
);
246 fd2
= mkostemp_safe(fn2
);
249 fd3
= mkostemp_safe(fn3
);
252 r
= copy_bytes(fd
, fd2
, max_bytes
, try_reflink
? COPY_REFLINK
: 0);
253 if (max_bytes
== UINT64_MAX
)
256 assert_se(IN_SET(r
, 0, 1));
258 assert_se(fstat(fd
, &buf
) == 0);
259 assert_se(fstat(fd2
, &buf2
) == 0);
260 assert_se((uint64_t) buf2
.st_size
== MIN((uint64_t) buf
.st_size
, max_bytes
));
262 if (max_bytes
< UINT64_MAX
)
263 /* Make sure the file is now higher than max_bytes */
264 assert_se(ftruncate(fd2
, max_bytes
+ 1) == 0);
266 assert_se(lseek(fd2
, 0, SEEK_SET
) == 0);
268 r
= copy_bytes(fd2
, fd3
, max_bytes
, try_reflink
? COPY_REFLINK
: 0);
269 if (max_bytes
== UINT64_MAX
)
272 /* We cannot distinguish between the input being exactly max_bytes
273 * or longer than max_bytes (without trying to read one more byte,
274 * or calling stat, or FION_READ, etc, and we don't want to do any
275 * of that). So we expect "truncation" since we know that file we
276 * are copying is exactly max_bytes bytes. */
279 assert_se(fstat(fd3
, &buf3
) == 0);
281 if (max_bytes
== UINT64_MAX
)
282 assert_se(buf3
.st_size
== buf2
.st_size
);
284 assert_se((uint64_t) buf3
.st_size
== max_bytes
);
290 static void test_copy_atomic(void) {
291 _cleanup_(rm_rf_physical_and_freep
) char *p
= NULL
;
295 assert_se(mkdtemp_malloc(NULL
, &p
) >= 0);
297 q
= strjoina(p
, "/fstab");
299 r
= copy_file_atomic("/etc/fstab", q
, 0644, 0, 0, COPY_REFLINK
);
300 if (r
== -ENOENT
|| ERRNO_IS_PRIVILEGE(r
))
303 assert_se(copy_file_atomic("/etc/fstab", q
, 0644, 0, 0, COPY_REFLINK
) == -EEXIST
);
305 assert_se(copy_file_atomic("/etc/fstab", q
, 0644, 0, 0, COPY_REPLACE
) >= 0);
308 static void test_copy_proc(void) {
309 _cleanup_(rm_rf_physical_and_freep
) char *p
= NULL
;
310 _cleanup_free_
char *f
= NULL
, *a
= NULL
, *b
= NULL
;
312 /* Check if copying data from /proc/ works correctly, i.e. let's see if https://lwn.net/Articles/846403/ is a problem for us */
314 assert_se(mkdtemp_malloc(NULL
, &p
) >= 0);
315 assert_se(f
= path_join(p
, "version"));
316 assert_se(copy_file("/proc/version", f
, 0, MODE_INVALID
, 0, 0, 0) >= 0);
318 assert_se(read_one_line_file("/proc/version", &a
) >= 0);
319 assert_se(read_one_line_file(f
, &b
) >= 0);
320 assert_se(streq(a
, b
));
321 assert_se(!isempty(a
));
324 int main(int argc
, char *argv
[]) {
325 test_setup_logging(LOG_DEBUG
);
331 test_copy_bytes_regular_file(argv
[0], false, UINT64_MAX
);
332 test_copy_bytes_regular_file(argv
[0], true, UINT64_MAX
);
333 test_copy_bytes_regular_file(argv
[0], false, 1000); /* smaller than copy buffer size */
334 test_copy_bytes_regular_file(argv
[0], true, 1000);
335 test_copy_bytes_regular_file(argv
[0], false, 32000); /* larger than copy buffer size */
336 test_copy_bytes_regular_file(argv
[0], true, 32000);