test-copy.c revision 3ffd4af22052963e7a29431721ee204e634bea75
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering/***
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering This file is part of systemd
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering Copyright 2014 Ronny Chevalier
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering systemd is free software; you can redistribute it and/or modify it
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering under the terms of the GNU Lesser General Public License as published by
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering the Free Software Foundation; either version 2.1 of the License, or
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering (at your option) any later version.
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering systemd is distributed in the hope that it will be useful, but
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering WITHOUT ANY WARRANTY; without even the implied warranty of
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering Lesser General Public License for more details.
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering You should have received a copy of the GNU Lesser General Public License
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering along with systemd; If not, see <http://www.gnu.org/licenses/>.
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering***/
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering#include <unistd.h>
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
b5efdb8af40ea759a1ea584c1bc44ecc81dd00ceLennart Poettering#include "copy.h"
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering#include "fd-util.h"
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering#include "fileio.h"
b5efdb8af40ea759a1ea584c1bc44ecc81dd00ceLennart Poettering#include "macro.h"
be808ea083fa07271116b4519c3c27fd20c5f077Tom Gundersen#include "mkdir.h"
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering#include "path-util.h"
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering#include "rm-rf.h"
9df3ba6c6cb65eecec06f39dfe85a3596cedac4eTom Gundersen#include "string-util.h"
9df3ba6c6cb65eecec06f39dfe85a3596cedac4eTom Gundersen#include "strv.h"
9df3ba6c6cb65eecec06f39dfe85a3596cedac4eTom Gundersen#include "util.h"
9df3ba6c6cb65eecec06f39dfe85a3596cedac4eTom Gundersen
be808ea083fa07271116b4519c3c27fd20c5f077Tom Gundersenstatic void test_copy_file(void) {
be808ea083fa07271116b4519c3c27fd20c5f077Tom Gundersen _cleanup_free_ char *buf = NULL;
be808ea083fa07271116b4519c3c27fd20c5f077Tom Gundersen char fn[] = "/tmp/test-copy_file.XXXXXX";
be808ea083fa07271116b4519c3c27fd20c5f077Tom Gundersen char fn_copy[] = "/tmp/test-copy_file.XXXXXX";
be808ea083fa07271116b4519c3c27fd20c5f077Tom Gundersen size_t sz = 0;
be808ea083fa07271116b4519c3c27fd20c5f077Tom Gundersen int fd;
be808ea083fa07271116b4519c3c27fd20c5f077Tom Gundersen
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering fd = mkostemp_safe(fn, O_RDWR|O_CLOEXEC);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering assert_se(fd >= 0);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering close(fd);
4e945a6f7971fd7d1f6b2c62ee3afdaff3c95ce4Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering fd = mkostemp_safe(fn_copy, O_RDWR|O_CLOEXEC);
0dd25fb9f005d8ab7ac4bc10a609d00569f8c56aLennart Poettering assert_se(fd >= 0);
3c0cf502796be355431d4a64d738e75f543aa51dLennart Poettering close(fd);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
eed857b71702f8551b46b66b31fa0d08583cf23cLennart Poettering assert_se(write_string_file(fn, "foo bar bar bar foo", WRITE_STRING_FILE_CREATE) == 0);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering assert_se(copy_file(fn, fn_copy, 0, 0644, 0) == 0);
4e945a6f7971fd7d1f6b2c62ee3afdaff3c95ce4Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering assert_se(read_full_file(fn_copy, &buf, &sz) == 0);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering assert_se(streq(buf, "foo bar bar bar foo\n"));
eed857b71702f8551b46b66b31fa0d08583cf23cLennart Poettering assert_se(sz == 20);
eed857b71702f8551b46b66b31fa0d08583cf23cLennart Poettering
eed857b71702f8551b46b66b31fa0d08583cf23cLennart Poettering unlink(fn);
eed857b71702f8551b46b66b31fa0d08583cf23cLennart Poettering unlink(fn_copy);
eed857b71702f8551b46b66b31fa0d08583cf23cLennart Poettering}
eed857b71702f8551b46b66b31fa0d08583cf23cLennart Poettering
eed857b71702f8551b46b66b31fa0d08583cf23cLennart Poetteringstatic void test_copy_file_fd(void) {
eed857b71702f8551b46b66b31fa0d08583cf23cLennart Poettering char in_fn[] = "/tmp/test-copy-file-fd-XXXXXX";
eed857b71702f8551b46b66b31fa0d08583cf23cLennart Poettering char out_fn[] = "/tmp/test-copy-file-fd-XXXXXX";
eed857b71702f8551b46b66b31fa0d08583cf23cLennart Poettering _cleanup_close_ int in_fd = -1, out_fd = -1;
eed857b71702f8551b46b66b31fa0d08583cf23cLennart Poettering char text[] = "boohoo\nfoo\n\tbar\n";
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering char buf[64] = {0};
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering in_fd = mkostemp_safe(in_fn, O_RDWR);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering assert_se(in_fd >= 0);
91b14d6ff362b938a72db17b095ee9903d07381bTom Gundersen out_fd = mkostemp_safe(out_fn, O_RDWR);
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering assert_se(out_fd >= 0);
f4461e5641d53f27d6e76e0607bdaa9c0c58c1f6Lennart Poettering
f4461e5641d53f27d6e76e0607bdaa9c0c58c1f6Lennart Poettering assert_se(write_string_file(in_fn, text, WRITE_STRING_FILE_CREATE) == 0);
be808ea083fa07271116b4519c3c27fd20c5f077Tom Gundersen assert_se(copy_file_fd("/a/file/which/does/not/exist/i/guess", out_fd, true) < 0);
d74fb368b18f0fbd9a4fe6f15691bbea7f3c4a01Tom Gundersen assert_se(copy_file_fd(in_fn, out_fd, true) >= 0);
4e945a6f7971fd7d1f6b2c62ee3afdaff3c95ce4Lennart Poettering assert_se(lseek(out_fd, SEEK_SET, 0) == 0);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering assert_se(read(out_fd, buf, sizeof(buf)) == sizeof(text) - 1);
9df3ba6c6cb65eecec06f39dfe85a3596cedac4eTom Gundersen assert_se(streq(buf, text));
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering unlink(in_fn);
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering unlink(out_fn);
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering}
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering
eed857b71702f8551b46b66b31fa0d08583cf23cLennart Poetteringstatic void test_copy_tree(void) {
eed857b71702f8551b46b66b31fa0d08583cf23cLennart Poettering char original_dir[] = "/tmp/test-copy_tree/";
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering char copy_dir[] = "/tmp/test-copy_tree-copy/";
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering char **files = STRV_MAKE("file", "dir1/file", "dir1/dir2/file", "dir1/dir2/dir3/dir4/dir5/file");
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering char **links = STRV_MAKE("link", "file",
eed857b71702f8551b46b66b31fa0d08583cf23cLennart Poettering "link2", "dir1/file");
eed857b71702f8551b46b66b31fa0d08583cf23cLennart Poettering char **p, **link;
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering (void) rm_rf(copy_dir, REMOVE_ROOT|REMOVE_PHYSICAL);
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering (void) rm_rf(original_dir, REMOVE_ROOT|REMOVE_PHYSICAL);
eed857b71702f8551b46b66b31fa0d08583cf23cLennart Poettering
eed857b71702f8551b46b66b31fa0d08583cf23cLennart Poettering STRV_FOREACH(p, files) {
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering char *f = strjoina(original_dir, *p);
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering assert_se(mkdir_parents(f, 0755) >= 0);
4e945a6f7971fd7d1f6b2c62ee3afdaff3c95ce4Lennart Poettering assert_se(write_string_file(f, "file", WRITE_STRING_FILE_CREATE) == 0);
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering }
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering STRV_FOREACH_PAIR(link, p, links) {
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering char *f = strjoina(original_dir, *p);
4e945a6f7971fd7d1f6b2c62ee3afdaff3c95ce4Lennart Poettering char *l = strjoina(original_dir, *link);
4e945a6f7971fd7d1f6b2c62ee3afdaff3c95ce4Lennart Poettering
4e945a6f7971fd7d1f6b2c62ee3afdaff3c95ce4Lennart Poettering assert_se(mkdir_parents(l, 0755) >= 0);
4e945a6f7971fd7d1f6b2c62ee3afdaff3c95ce4Lennart Poettering assert_se(symlink(f, l) == 0);
3e684349c2cead2e6fd2f816c34eb17daba23a49Lennart Poettering }
3e684349c2cead2e6fd2f816c34eb17daba23a49Lennart Poettering
3e684349c2cead2e6fd2f816c34eb17daba23a49Lennart Poettering assert_se(copy_tree(original_dir, copy_dir, true) == 0);
4e945a6f7971fd7d1f6b2c62ee3afdaff3c95ce4Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering STRV_FOREACH(p, files) {
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering _cleanup_free_ char *buf = NULL;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering size_t sz = 0;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering char *f = strjoina(copy_dir, *p);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering assert_se(access(f, F_OK) == 0);
91b14d6ff362b938a72db17b095ee9903d07381bTom Gundersen assert_se(read_full_file(f, &buf, &sz) == 0);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering assert_se(streq(buf, "file\n"));
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering }
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
91b14d6ff362b938a72db17b095ee9903d07381bTom Gundersen STRV_FOREACH_PAIR(link, p, links) {
91b14d6ff362b938a72db17b095ee9903d07381bTom Gundersen _cleanup_free_ char *target = NULL;
cab5b05903096e1c9cf5575ccc73f89d15c8db69Lennart Poettering char *f = strjoina(original_dir, *p);
91b14d6ff362b938a72db17b095ee9903d07381bTom Gundersen char *l = strjoina(copy_dir, *link);
91b14d6ff362b938a72db17b095ee9903d07381bTom Gundersen
91b14d6ff362b938a72db17b095ee9903d07381bTom Gundersen assert_se(readlink_and_canonicalize(l, &target) == 0);
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering assert_se(path_equal(f, target));
91b14d6ff362b938a72db17b095ee9903d07381bTom Gundersen }
91b14d6ff362b938a72db17b095ee9903d07381bTom Gundersen
3e684349c2cead2e6fd2f816c34eb17daba23a49Lennart Poettering assert_se(copy_tree(original_dir, copy_dir, false) < 0);
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering assert_se(copy_tree("/tmp/inexistent/foo/bar/fsdoi", copy_dir, false) < 0);
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering
91b14d6ff362b938a72db17b095ee9903d07381bTom Gundersen (void) rm_rf(copy_dir, REMOVE_ROOT|REMOVE_PHYSICAL);
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering (void) rm_rf(original_dir, REMOVE_ROOT|REMOVE_PHYSICAL);
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering}
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poetteringstatic void test_copy_bytes(void) {
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering _cleanup_close_pair_ int pipefd[2] = {-1, -1};
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering _cleanup_close_ int infd = -1;
87f5a19343acf8ba697acc5a62bdb1a2b8c9eda3Lennart Poettering int r, r2;
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering char buf[1024], buf2[1024];
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering infd = open("/usr/lib/os-release", O_RDONLY|O_CLOEXEC);
91b14d6ff362b938a72db17b095ee9903d07381bTom Gundersen if (infd < 0)
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering infd = open("/etc/os-release", O_RDONLY|O_CLOEXEC);
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering assert_se(infd >= 0);
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering
91b14d6ff362b938a72db17b095ee9903d07381bTom Gundersen assert_se(pipe2(pipefd, O_CLOEXEC) == 0);
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering r = copy_bytes(infd, pipefd[1], (uint64_t) -1, false);
91b14d6ff362b938a72db17b095ee9903d07381bTom Gundersen assert_se(r == 0);
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering r = read(pipefd[0], buf, sizeof(buf));
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering assert_se(r >= 0);
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering
eed857b71702f8551b46b66b31fa0d08583cf23cLennart Poettering assert_se(lseek(infd, 0, SEEK_SET) == 0);
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering r2 = read(infd, buf2, sizeof(buf2));
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering assert_se(r == r2);
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering assert_se(strneq(buf, buf2, r));
eed857b71702f8551b46b66b31fa0d08583cf23cLennart Poettering
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering /* test copy_bytes with invalid descriptors */
eed857b71702f8551b46b66b31fa0d08583cf23cLennart Poettering r = copy_bytes(pipefd[0], pipefd[0], 1, false);
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering assert_se(r == -EBADF);
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering r = copy_bytes(pipefd[1], pipefd[1], 1, false);
eed857b71702f8551b46b66b31fa0d08583cf23cLennart Poettering assert_se(r == -EBADF);
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering
eed857b71702f8551b46b66b31fa0d08583cf23cLennart Poettering r = copy_bytes(pipefd[1], infd, 1, false);
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering assert_se(r == -EBADF);
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering}
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poetteringint main(int argc, char *argv[]) {
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering test_copy_file();
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering test_copy_file_fd();
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering test_copy_tree();
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering test_copy_bytes();
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering return 0;
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering}
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering