]>
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" | |
23 | #include "path-util.h" | |
24 | #include "fileio.h" | |
25 | #include "mkdir.h" | |
26 | #include "strv.h" | |
27 | #include "macro.h" | |
28 | #include "util.h" | |
c6878637 | 29 | #include "rm-rf.h" |
641d1f99 RC |
30 | |
31 | static void test_copy_file(void) { | |
32 | _cleanup_free_ char *buf = NULL; | |
33 | char fn[] = "/tmp/test-copy_file.XXXXXX"; | |
34 | char fn_copy[] = "/tmp/test-copy_file.XXXXXX"; | |
35 | size_t sz = 0; | |
36 | int fd; | |
37 | ||
38 | fd = mkostemp_safe(fn, O_RDWR|O_CLOEXEC); | |
39 | assert_se(fd >= 0); | |
40 | close(fd); | |
41 | ||
42 | fd = mkostemp_safe(fn_copy, O_RDWR|O_CLOEXEC); | |
43 | assert_se(fd >= 0); | |
44 | close(fd); | |
45 | ||
46 | assert_se(write_string_file(fn, "foo bar bar bar foo") == 0); | |
47 | ||
f2068bcc | 48 | assert_se(copy_file(fn, fn_copy, 0, 0644, 0) == 0); |
641d1f99 RC |
49 | |
50 | assert_se(read_full_file(fn_copy, &buf, &sz) == 0); | |
51 | assert_se(streq(buf, "foo bar bar bar foo\n")); | |
cda134ab | 52 | assert_se(sz == 20); |
641d1f99 RC |
53 | |
54 | unlink(fn); | |
55 | unlink(fn_copy); | |
56 | } | |
57 | ||
cda134ab LP |
58 | static void test_copy_file_fd(void) { |
59 | char in_fn[] = "/tmp/test-copy-file-fd-XXXXXX"; | |
60 | char out_fn[] = "/tmp/test-copy-file-fd-XXXXXX"; | |
61 | _cleanup_close_ int in_fd = -1, out_fd = -1; | |
62 | char text[] = "boohoo\nfoo\n\tbar\n"; | |
63 | char buf[64] = {0}; | |
64 | ||
65 | in_fd = mkostemp_safe(in_fn, O_RDWR); | |
66 | assert_se(in_fd >= 0); | |
67 | out_fd = mkostemp_safe(out_fn, O_RDWR); | |
68 | assert_se(out_fd >= 0); | |
69 | ||
70 | assert_se(write_string_file(in_fn, text) == 0); | |
7430ec6a LP |
71 | assert_se(copy_file_fd("/a/file/which/does/not/exist/i/guess", out_fd, true) < 0); |
72 | assert_se(copy_file_fd(in_fn, out_fd, true) >= 0); | |
cda134ab LP |
73 | assert_se(lseek(out_fd, SEEK_SET, 0) == 0); |
74 | ||
75 | assert_se(read(out_fd, buf, sizeof(buf)) == sizeof(text) - 1); | |
76 | assert_se(streq(buf, text)); | |
77 | ||
78 | unlink(in_fn); | |
79 | unlink(out_fn); | |
80 | } | |
81 | ||
641d1f99 RC |
82 | static void test_copy_tree(void) { |
83 | char original_dir[] = "/tmp/test-copy_tree/"; | |
84 | char copy_dir[] = "/tmp/test-copy_tree-copy/"; | |
85 | char **files = STRV_MAKE("file", "dir1/file", "dir1/dir2/file", "dir1/dir2/dir3/dir4/dir5/file"); | |
86 | char **links = STRV_MAKE("link", "file", | |
87 | "link2", "dir1/file"); | |
88 | char **p, **link; | |
89 | ||
c6878637 LP |
90 | (void) rm_rf(copy_dir, REMOVE_ROOT|REMOVE_PHYSICAL); |
91 | (void) rm_rf(original_dir, REMOVE_ROOT|REMOVE_PHYSICAL); | |
641d1f99 RC |
92 | |
93 | STRV_FOREACH(p, files) { | |
63c372cb | 94 | char *f = strjoina(original_dir, *p); |
641d1f99 RC |
95 | |
96 | assert_se(mkdir_parents(f, 0755) >= 0); | |
97 | assert_se(write_string_file(f, "file") == 0); | |
98 | } | |
99 | ||
100 | STRV_FOREACH_PAIR(link, p, links) { | |
63c372cb LP |
101 | char *f = strjoina(original_dir, *p); |
102 | char *l = strjoina(original_dir, *link); | |
641d1f99 RC |
103 | |
104 | assert_se(mkdir_parents(l, 0755) >= 0); | |
105 | assert_se(symlink(f, l) == 0); | |
106 | } | |
107 | ||
108 | assert_se(copy_tree(original_dir, copy_dir, true) == 0); | |
109 | ||
110 | STRV_FOREACH(p, files) { | |
111 | _cleanup_free_ char *buf = NULL; | |
112 | size_t sz = 0; | |
63c372cb | 113 | char *f = strjoina(copy_dir, *p); |
641d1f99 RC |
114 | |
115 | assert_se(access(f, F_OK) == 0); | |
116 | assert_se(read_full_file(f, &buf, &sz) == 0); | |
117 | assert_se(streq(buf, "file\n")); | |
118 | } | |
119 | ||
120 | STRV_FOREACH_PAIR(link, p, links) { | |
121 | _cleanup_free_ char *target = NULL; | |
63c372cb LP |
122 | char *f = strjoina(original_dir, *p); |
123 | char *l = strjoina(copy_dir, *link); | |
641d1f99 RC |
124 | |
125 | assert_se(readlink_and_canonicalize(l, &target) == 0); | |
126 | assert_se(path_equal(f, target)); | |
127 | } | |
128 | ||
129 | assert_se(copy_tree(original_dir, copy_dir, false) < 0); | |
130 | assert_se(copy_tree("/tmp/inexistent/foo/bar/fsdoi", copy_dir, false) < 0); | |
131 | ||
c6878637 LP |
132 | (void) rm_rf(copy_dir, REMOVE_ROOT|REMOVE_PHYSICAL); |
133 | (void) rm_rf(original_dir, REMOVE_ROOT|REMOVE_PHYSICAL); | |
641d1f99 RC |
134 | } |
135 | ||
136 | int main(int argc, char *argv[]) { | |
137 | test_copy_file(); | |
cda134ab | 138 | test_copy_file_fd(); |
641d1f99 RC |
139 | test_copy_tree(); |
140 | ||
141 | return 0; | |
142 | } |