test-copy.c revision 4c1fc3e404d648c70bd2f50ac50aeac6ece8872e
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering This file is part of systemd
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering Copyright 2014 Ronny Chevalier
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering systemd is free software; you can redistribute it and/or modify it
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering under the terms of the GNU Lesser General Public License as published by
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering the Free Software Foundation; either version 2.1 of the License, or
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering (at your option) any later version.
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering systemd is distributed in the hope that it will be useful, but
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering WITHOUT ANY WARRANTY; without even the implied warranty of
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering Lesser General Public License for more details.
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering You should have received a copy of the GNU Lesser General Public License
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering along with systemd; If not, see <http://www.gnu.org/licenses/>.
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poetteringstatic void test_copy_file(void) {
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering char fn[] = "/tmp/test-copy_file.XXXXXX";
f2cbe59e113f08549949a76ac5b9b3972df4cc30Lennart Poettering char fn_copy[] = "/tmp/test-copy_file.XXXXXX";
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering fd = mkostemp_safe(fn, O_RDWR|O_CLOEXEC);
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen fd = mkostemp_safe(fn_copy, O_RDWR|O_CLOEXEC);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering assert_se(write_string_file(fn, "foo bar bar bar foo", WRITE_STRING_FILE_CREATE) == 0);
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering assert_se(copy_file(fn, fn_copy, 0, 0644, 0) == 0);
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering assert_se(read_full_file(fn_copy, &buf, &sz) == 0);
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering assert_se(streq(buf, "foo bar bar bar foo\n"));
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poetteringstatic void test_copy_file_fd(void) {
e56056e93d33619a3acf13e483900b4f8938228fThomas Hindoe Paaboel Andersen char in_fn[] = "/tmp/test-copy-file-fd-XXXXXX";
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering char out_fn[] = "/tmp/test-copy-file-fd-XXXXXX";
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering _cleanup_close_ int in_fd = -1, out_fd = -1;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering assert_se(write_string_file(in_fn, text, WRITE_STRING_FILE_CREATE) == 0);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering assert_se(copy_file_fd("/a/file/which/does/not/exist/i/guess", out_fd, true) < 0);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering assert_se(copy_file_fd(in_fn, out_fd, true) >= 0);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering assert_se(lseek(out_fd, SEEK_SET, 0) == 0);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering assert_se(read(out_fd, buf, sizeof(buf)) == sizeof(text) - 1);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poetteringstatic void test_copy_tree(void) {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering char original_dir[] = "/tmp/test-copy_tree/";
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering char copy_dir[] = "/tmp/test-copy_tree-copy/";
56159e0d918e9a9be07988133bb2847779325de0Lennart Poettering char **files = STRV_MAKE("file", "dir1/file", "dir1/dir2/file", "dir1/dir2/dir3/dir4/dir5/file");
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen (void) rm_rf(copy_dir, REMOVE_ROOT|REMOVE_PHYSICAL);
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen (void) rm_rf(original_dir, REMOVE_ROOT|REMOVE_PHYSICAL);
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen assert_se(write_string_file(f, "file", WRITE_STRING_FILE_CREATE) == 0);
e56056e93d33619a3acf13e483900b4f8938228fThomas Hindoe Paaboel Andersen assert_se(mkdir_parents(l, 0755) >= 0);
5b30bef856e89a571df57b7b953e9a1409d9acedLennart Poettering assert_se(copy_tree(original_dir, copy_dir, true) == 0);
5b30bef856e89a571df57b7b953e9a1409d9acedLennart Poettering assert_se(read_full_file(f, &buf, &sz) == 0);
e56056e93d33619a3acf13e483900b4f8938228fThomas Hindoe Paaboel Andersen char *f = strjoina(original_dir, *p);
e56056e93d33619a3acf13e483900b4f8938228fThomas Hindoe Paaboel Andersen char *l = strjoina(copy_dir, *link);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering assert_se(readlink_and_canonicalize(l, &target) == 0);
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering assert_se(copy_tree(original_dir, copy_dir, false) < 0);
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering assert_se(copy_tree("/tmp/inexistent/foo/bar/fsdoi", copy_dir, false) < 0);
10f9c75519671e7c7ab8993b54fe22da7c2d0c38Lennart Poettering (void) rm_rf(copy_dir, REMOVE_ROOT|REMOVE_PHYSICAL);
10f9c75519671e7c7ab8993b54fe22da7c2d0c38Lennart Poettering (void) rm_rf(original_dir, REMOVE_ROOT|REMOVE_PHYSICAL);
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poetteringstatic void test_copy_bytes(void) {
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering _cleanup_close_pair_ int pipefd[2] = {-1, -1};
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering infd = open("/usr/lib/os-release", O_RDONLY|O_CLOEXEC);
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering infd = open("/etc/os-release", O_RDONLY|O_CLOEXEC);
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering assert_se(pipe2(pipefd, O_CLOEXEC) == 0);
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering r = copy_bytes(infd, pipefd[1], (off_t) -1, false);
56159e0d918e9a9be07988133bb2847779325de0Lennart Poettering assert_se(lseek(infd, 0, SEEK_SET) == 0);
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering /* test copy_bytes with invalid descriptors */
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering r = copy_bytes(pipefd[0], pipefd[0], 1, false);
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering r = copy_bytes(pipefd[1], pipefd[1], 1, false);
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering r = copy_bytes(pipefd[1], infd, 1, false);