test-copy.c revision f2068bcce01db31cdc9422f44185f3b49c04d2ce
641d1f99b8c4c5427a1fedcb4740586a130ac6cfRonny Chevalier This file is part of systemd
641d1f99b8c4c5427a1fedcb4740586a130ac6cfRonny Chevalier Copyright 2014 Ronny Chevalier
641d1f99b8c4c5427a1fedcb4740586a130ac6cfRonny Chevalier systemd is free software; you can redistribute it and/or modify it
641d1f99b8c4c5427a1fedcb4740586a130ac6cfRonny Chevalier under the terms of the GNU Lesser General Public License as published by
641d1f99b8c4c5427a1fedcb4740586a130ac6cfRonny Chevalier the Free Software Foundation; either version 2.1 of the License, or
641d1f99b8c4c5427a1fedcb4740586a130ac6cfRonny Chevalier (at your option) any later version.
641d1f99b8c4c5427a1fedcb4740586a130ac6cfRonny Chevalier systemd is distributed in the hope that it will be useful, but
641d1f99b8c4c5427a1fedcb4740586a130ac6cfRonny Chevalier WITHOUT ANY WARRANTY; without even the implied warranty of
641d1f99b8c4c5427a1fedcb4740586a130ac6cfRonny Chevalier MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
641d1f99b8c4c5427a1fedcb4740586a130ac6cfRonny Chevalier Lesser General Public License for more details.
641d1f99b8c4c5427a1fedcb4740586a130ac6cfRonny Chevalier You should have received a copy of the GNU Lesser General Public License
641d1f99b8c4c5427a1fedcb4740586a130ac6cfRonny Chevalier along with systemd; If not, see <http://www.gnu.org/licenses/>.
641d1f99b8c4c5427a1fedcb4740586a130ac6cfRonny Chevalierstatic void test_copy_file(void) {
641d1f99b8c4c5427a1fedcb4740586a130ac6cfRonny Chevalier char fn_copy[] = "/tmp/test-copy_file.XXXXXX";
641d1f99b8c4c5427a1fedcb4740586a130ac6cfRonny Chevalier fd = mkostemp_safe(fn_copy, O_RDWR|O_CLOEXEC);
641d1f99b8c4c5427a1fedcb4740586a130ac6cfRonny Chevalier assert_se(write_string_file(fn, "foo bar bar bar foo") == 0);
f2068bcce01db31cdc9422f44185f3b49c04d2ceLennart Poettering assert_se(copy_file(fn, fn_copy, 0, 0644, 0) == 0);
641d1f99b8c4c5427a1fedcb4740586a130ac6cfRonny Chevalier assert_se(read_full_file(fn_copy, &buf, &sz) == 0);
641d1f99b8c4c5427a1fedcb4740586a130ac6cfRonny Chevalier assert_se(streq(buf, "foo bar bar bar foo\n"));
cda134ab1eac84f874aacf8e885a07112a7fd5ceLennart Poetteringstatic void test_copy_file_fd(void) {
cda134ab1eac84f874aacf8e885a07112a7fd5ceLennart Poettering char in_fn[] = "/tmp/test-copy-file-fd-XXXXXX";
cda134ab1eac84f874aacf8e885a07112a7fd5ceLennart Poettering char out_fn[] = "/tmp/test-copy-file-fd-XXXXXX";
cda134ab1eac84f874aacf8e885a07112a7fd5ceLennart Poettering _cleanup_close_ int in_fd = -1, out_fd = -1;
cda134ab1eac84f874aacf8e885a07112a7fd5ceLennart Poettering assert_se(write_string_file(in_fn, text) == 0);
7430ec6ac08f2c0416d9f806964c46b30f3862b2Lennart Poettering assert_se(copy_file_fd("/a/file/which/does/not/exist/i/guess", out_fd, true) < 0);
7430ec6ac08f2c0416d9f806964c46b30f3862b2Lennart Poettering assert_se(copy_file_fd(in_fn, out_fd, true) >= 0);
cda134ab1eac84f874aacf8e885a07112a7fd5ceLennart Poettering assert_se(lseek(out_fd, SEEK_SET, 0) == 0);
cda134ab1eac84f874aacf8e885a07112a7fd5ceLennart Poettering assert_se(read(out_fd, buf, sizeof(buf)) == sizeof(text) - 1);
641d1f99b8c4c5427a1fedcb4740586a130ac6cfRonny Chevalierstatic void test_copy_tree(void) {
641d1f99b8c4c5427a1fedcb4740586a130ac6cfRonny Chevalier char original_dir[] = "/tmp/test-copy_tree/";
641d1f99b8c4c5427a1fedcb4740586a130ac6cfRonny Chevalier char copy_dir[] = "/tmp/test-copy_tree-copy/";
641d1f99b8c4c5427a1fedcb4740586a130ac6cfRonny Chevalier char **files = STRV_MAKE("file", "dir1/file", "dir1/dir2/file", "dir1/dir2/dir3/dir4/dir5/file");
641d1f99b8c4c5427a1fedcb4740586a130ac6cfRonny Chevalier rm_rf_dangerous(copy_dir, false, true, false);
641d1f99b8c4c5427a1fedcb4740586a130ac6cfRonny Chevalier rm_rf_dangerous(original_dir, false, true, false);
641d1f99b8c4c5427a1fedcb4740586a130ac6cfRonny Chevalier assert_se(write_string_file(f, "file") == 0);
641d1f99b8c4c5427a1fedcb4740586a130ac6cfRonny Chevalier assert_se(copy_tree(original_dir, copy_dir, true) == 0);
641d1f99b8c4c5427a1fedcb4740586a130ac6cfRonny Chevalier assert_se(read_full_file(f, &buf, &sz) == 0);
641d1f99b8c4c5427a1fedcb4740586a130ac6cfRonny Chevalier assert_se(readlink_and_canonicalize(l, &target) == 0);
641d1f99b8c4c5427a1fedcb4740586a130ac6cfRonny Chevalier assert_se(copy_tree(original_dir, copy_dir, false) < 0);
641d1f99b8c4c5427a1fedcb4740586a130ac6cfRonny Chevalier assert_se(copy_tree("/tmp/inexistent/foo/bar/fsdoi", copy_dir, false) < 0);
641d1f99b8c4c5427a1fedcb4740586a130ac6cfRonny Chevalier rm_rf_dangerous(copy_dir, false, true, false);
641d1f99b8c4c5427a1fedcb4740586a130ac6cfRonny Chevalier rm_rf_dangerous(original_dir, false, true, false);