]>
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" |
07630cea | 27 | #include "macro.h" |
641d1f99 | 28 | #include "mkdir.h" |
07630cea LP |
29 | #include "path-util.h" |
30 | #include "rm-rf.h" | |
31 | #include "string-util.h" | |
641d1f99 | 32 | #include "strv.h" |
641d1f99 RC |
33 | #include "util.h" |
34 | ||
35 | static void test_copy_file(void) { | |
36 | _cleanup_free_ char *buf = NULL; | |
37 | char fn[] = "/tmp/test-copy_file.XXXXXX"; | |
38 | char fn_copy[] = "/tmp/test-copy_file.XXXXXX"; | |
39 | size_t sz = 0; | |
40 | int fd; | |
41 | ||
42 | fd = mkostemp_safe(fn, O_RDWR|O_CLOEXEC); | |
43 | assert_se(fd >= 0); | |
44 | close(fd); | |
45 | ||
46 | fd = mkostemp_safe(fn_copy, O_RDWR|O_CLOEXEC); | |
47 | assert_se(fd >= 0); | |
48 | close(fd); | |
49 | ||
4c1fc3e4 | 50 | assert_se(write_string_file(fn, "foo bar bar bar foo", WRITE_STRING_FILE_CREATE) == 0); |
641d1f99 | 51 | |
f2068bcc | 52 | assert_se(copy_file(fn, fn_copy, 0, 0644, 0) == 0); |
641d1f99 RC |
53 | |
54 | assert_se(read_full_file(fn_copy, &buf, &sz) == 0); | |
55 | assert_se(streq(buf, "foo bar bar bar foo\n")); | |
cda134ab | 56 | assert_se(sz == 20); |
641d1f99 RC |
57 | |
58 | unlink(fn); | |
59 | unlink(fn_copy); | |
60 | } | |
61 | ||
cda134ab LP |
62 | static void test_copy_file_fd(void) { |
63 | char in_fn[] = "/tmp/test-copy-file-fd-XXXXXX"; | |
64 | char out_fn[] = "/tmp/test-copy-file-fd-XXXXXX"; | |
65 | _cleanup_close_ int in_fd = -1, out_fd = -1; | |
66 | char text[] = "boohoo\nfoo\n\tbar\n"; | |
67 | char buf[64] = {0}; | |
68 | ||
69 | in_fd = mkostemp_safe(in_fn, O_RDWR); | |
70 | assert_se(in_fd >= 0); | |
71 | out_fd = mkostemp_safe(out_fn, O_RDWR); | |
72 | assert_se(out_fd >= 0); | |
73 | ||
4c1fc3e4 | 74 | assert_se(write_string_file(in_fn, text, WRITE_STRING_FILE_CREATE) == 0); |
7430ec6a LP |
75 | assert_se(copy_file_fd("/a/file/which/does/not/exist/i/guess", out_fd, true) < 0); |
76 | assert_se(copy_file_fd(in_fn, out_fd, true) >= 0); | |
cda134ab LP |
77 | assert_se(lseek(out_fd, SEEK_SET, 0) == 0); |
78 | ||
79 | assert_se(read(out_fd, buf, sizeof(buf)) == sizeof(text) - 1); | |
80 | assert_se(streq(buf, text)); | |
81 | ||
82 | unlink(in_fn); | |
83 | unlink(out_fn); | |
84 | } | |
85 | ||
641d1f99 RC |
86 | static void test_copy_tree(void) { |
87 | char original_dir[] = "/tmp/test-copy_tree/"; | |
88 | char copy_dir[] = "/tmp/test-copy_tree-copy/"; | |
89 | char **files = STRV_MAKE("file", "dir1/file", "dir1/dir2/file", "dir1/dir2/dir3/dir4/dir5/file"); | |
90 | char **links = STRV_MAKE("link", "file", | |
91 | "link2", "dir1/file"); | |
92 | char **p, **link; | |
93 | ||
c6878637 LP |
94 | (void) rm_rf(copy_dir, REMOVE_ROOT|REMOVE_PHYSICAL); |
95 | (void) rm_rf(original_dir, REMOVE_ROOT|REMOVE_PHYSICAL); | |
641d1f99 RC |
96 | |
97 | STRV_FOREACH(p, files) { | |
63c372cb | 98 | char *f = strjoina(original_dir, *p); |
641d1f99 RC |
99 | |
100 | assert_se(mkdir_parents(f, 0755) >= 0); | |
4c1fc3e4 | 101 | assert_se(write_string_file(f, "file", WRITE_STRING_FILE_CREATE) == 0); |
641d1f99 RC |
102 | } |
103 | ||
104 | STRV_FOREACH_PAIR(link, p, links) { | |
63c372cb LP |
105 | char *f = strjoina(original_dir, *p); |
106 | char *l = strjoina(original_dir, *link); | |
641d1f99 RC |
107 | |
108 | assert_se(mkdir_parents(l, 0755) >= 0); | |
109 | assert_se(symlink(f, l) == 0); | |
110 | } | |
111 | ||
112 | assert_se(copy_tree(original_dir, copy_dir, true) == 0); | |
113 | ||
114 | STRV_FOREACH(p, files) { | |
115 | _cleanup_free_ char *buf = NULL; | |
116 | size_t sz = 0; | |
63c372cb | 117 | char *f = strjoina(copy_dir, *p); |
641d1f99 RC |
118 | |
119 | assert_se(access(f, F_OK) == 0); | |
120 | assert_se(read_full_file(f, &buf, &sz) == 0); | |
121 | assert_se(streq(buf, "file\n")); | |
122 | } | |
123 | ||
124 | STRV_FOREACH_PAIR(link, p, links) { | |
125 | _cleanup_free_ char *target = NULL; | |
63c372cb LP |
126 | char *f = strjoina(original_dir, *p); |
127 | char *l = strjoina(copy_dir, *link); | |
641d1f99 RC |
128 | |
129 | assert_se(readlink_and_canonicalize(l, &target) == 0); | |
130 | assert_se(path_equal(f, target)); | |
131 | } | |
132 | ||
133 | assert_se(copy_tree(original_dir, copy_dir, false) < 0); | |
134 | assert_se(copy_tree("/tmp/inexistent/foo/bar/fsdoi", copy_dir, false) < 0); | |
135 | ||
c6878637 LP |
136 | (void) rm_rf(copy_dir, REMOVE_ROOT|REMOVE_PHYSICAL); |
137 | (void) rm_rf(original_dir, REMOVE_ROOT|REMOVE_PHYSICAL); | |
641d1f99 RC |
138 | } |
139 | ||
ad5ecc11 ZJS |
140 | static void test_copy_bytes(void) { |
141 | _cleanup_close_pair_ int pipefd[2] = {-1, -1}; | |
142 | _cleanup_close_ int infd = -1; | |
143 | int r, r2; | |
144 | char buf[1024], buf2[1024]; | |
145 | ||
4f36d400 DJL |
146 | infd = open("/usr/lib/os-release", O_RDONLY|O_CLOEXEC); |
147 | if (infd < 0) | |
148 | infd = open("/etc/os-release", O_RDONLY|O_CLOEXEC); | |
ad5ecc11 ZJS |
149 | assert_se(infd >= 0); |
150 | ||
151 | assert_se(pipe2(pipefd, O_CLOEXEC) == 0); | |
152 | ||
59f448cf | 153 | r = copy_bytes(infd, pipefd[1], (uint64_t) -1, false); |
ad5ecc11 ZJS |
154 | assert_se(r == 0); |
155 | ||
156 | r = read(pipefd[0], buf, sizeof(buf)); | |
157 | assert_se(r >= 0); | |
158 | ||
159 | assert_se(lseek(infd, 0, SEEK_SET) == 0); | |
160 | r2 = read(infd, buf2, sizeof(buf2)); | |
161 | assert_se(r == r2); | |
162 | ||
163 | assert_se(strneq(buf, buf2, r)); | |
164 | ||
165 | /* test copy_bytes with invalid descriptors */ | |
166 | r = copy_bytes(pipefd[0], pipefd[0], 1, false); | |
167 | assert_se(r == -EBADF); | |
168 | ||
169 | r = copy_bytes(pipefd[1], pipefd[1], 1, false); | |
170 | assert_se(r == -EBADF); | |
171 | ||
172 | r = copy_bytes(pipefd[1], infd, 1, false); | |
173 | assert_se(r == -EBADF); | |
174 | } | |
175 | ||
641d1f99 RC |
176 | int main(int argc, char *argv[]) { |
177 | test_copy_file(); | |
cda134ab | 178 | test_copy_file_fd(); |
641d1f99 | 179 | test_copy_tree(); |
ad5ecc11 | 180 | test_copy_bytes(); |
641d1f99 RC |
181 | |
182 | return 0; | |
183 | } |