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