2014-10-29 22:28:50 +01:00
|
|
|
/***
|
|
|
|
This file is part of systemd
|
|
|
|
|
|
|
|
Copyright 2014 Ronny Chevalier
|
|
|
|
|
|
|
|
systemd is free software; you can redistribute it and/or modify it
|
|
|
|
under the terms of the GNU Lesser General Public License as published by
|
|
|
|
the Free Software Foundation; either version 2.1 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
systemd is distributed in the hope that it will be useful, but
|
|
|
|
WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
Lesser General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Lesser General Public License
|
|
|
|
along with systemd; If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
***/
|
|
|
|
|
|
|
|
#include <unistd.h>
|
|
|
|
|
2015-10-27 03:01:06 +01:00
|
|
|
#include "alloc-util.h"
|
2014-10-29 22:28:50 +01:00
|
|
|
#include "copy.h"
|
2015-10-25 13:14:12 +01:00
|
|
|
#include "fd-util.h"
|
2014-10-29 22:28:50 +01:00
|
|
|
#include "fileio.h"
|
2015-10-26 21:16:26 +01:00
|
|
|
#include "fs-util.h"
|
2016-03-14 18:18:14 +01:00
|
|
|
#include "log.h"
|
2015-10-24 22:58:24 +02:00
|
|
|
#include "macro.h"
|
2014-10-29 22:28:50 +01:00
|
|
|
#include "mkdir.h"
|
2015-10-24 22:58:24 +02:00
|
|
|
#include "path-util.h"
|
|
|
|
#include "rm-rf.h"
|
|
|
|
#include "string-util.h"
|
2014-10-29 22:28:50 +01:00
|
|
|
#include "strv.h"
|
|
|
|
#include "util.h"
|
|
|
|
|
|
|
|
static void test_copy_file(void) {
|
|
|
|
_cleanup_free_ char *buf = NULL;
|
|
|
|
char fn[] = "/tmp/test-copy_file.XXXXXX";
|
|
|
|
char fn_copy[] = "/tmp/test-copy_file.XXXXXX";
|
|
|
|
size_t sz = 0;
|
|
|
|
int fd;
|
|
|
|
|
2016-03-14 18:18:14 +01:00
|
|
|
log_info("%s", __func__);
|
|
|
|
|
2014-10-29 22:28:50 +01:00
|
|
|
fd = mkostemp_safe(fn, O_RDWR|O_CLOEXEC);
|
|
|
|
assert_se(fd >= 0);
|
|
|
|
close(fd);
|
|
|
|
|
|
|
|
fd = mkostemp_safe(fn_copy, O_RDWR|O_CLOEXEC);
|
|
|
|
assert_se(fd >= 0);
|
|
|
|
close(fd);
|
|
|
|
|
2015-07-07 01:19:25 +02:00
|
|
|
assert_se(write_string_file(fn, "foo bar bar bar foo", WRITE_STRING_FILE_CREATE) == 0);
|
2014-10-29 22:28:50 +01:00
|
|
|
|
2015-01-08 19:15:49 +01:00
|
|
|
assert_se(copy_file(fn, fn_copy, 0, 0644, 0) == 0);
|
2014-10-29 22:28:50 +01:00
|
|
|
|
|
|
|
assert_se(read_full_file(fn_copy, &buf, &sz) == 0);
|
|
|
|
assert_se(streq(buf, "foo bar bar bar foo\n"));
|
2014-11-06 21:19:20 +01:00
|
|
|
assert_se(sz == 20);
|
2014-10-29 22:28:50 +01:00
|
|
|
|
|
|
|
unlink(fn);
|
|
|
|
unlink(fn_copy);
|
|
|
|
}
|
|
|
|
|
2014-11-06 21:19:20 +01:00
|
|
|
static void test_copy_file_fd(void) {
|
|
|
|
char in_fn[] = "/tmp/test-copy-file-fd-XXXXXX";
|
|
|
|
char out_fn[] = "/tmp/test-copy-file-fd-XXXXXX";
|
|
|
|
_cleanup_close_ int in_fd = -1, out_fd = -1;
|
|
|
|
char text[] = "boohoo\nfoo\n\tbar\n";
|
|
|
|
char buf[64] = {0};
|
|
|
|
|
2016-03-14 18:18:14 +01:00
|
|
|
log_info("%s", __func__);
|
|
|
|
|
2014-11-06 21:19:20 +01:00
|
|
|
in_fd = mkostemp_safe(in_fn, O_RDWR);
|
|
|
|
assert_se(in_fd >= 0);
|
|
|
|
out_fd = mkostemp_safe(out_fn, O_RDWR);
|
|
|
|
assert_se(out_fd >= 0);
|
|
|
|
|
2015-07-07 01:19:25 +02:00
|
|
|
assert_se(write_string_file(in_fn, text, WRITE_STRING_FILE_CREATE) == 0);
|
2014-12-12 16:24:33 +01:00
|
|
|
assert_se(copy_file_fd("/a/file/which/does/not/exist/i/guess", out_fd, true) < 0);
|
|
|
|
assert_se(copy_file_fd(in_fn, out_fd, true) >= 0);
|
2014-11-06 21:19:20 +01:00
|
|
|
assert_se(lseek(out_fd, SEEK_SET, 0) == 0);
|
|
|
|
|
|
|
|
assert_se(read(out_fd, buf, sizeof(buf)) == sizeof(text) - 1);
|
|
|
|
assert_se(streq(buf, text));
|
|
|
|
|
|
|
|
unlink(in_fn);
|
|
|
|
unlink(out_fn);
|
|
|
|
}
|
|
|
|
|
2014-10-29 22:28:50 +01:00
|
|
|
static void test_copy_tree(void) {
|
|
|
|
char original_dir[] = "/tmp/test-copy_tree/";
|
|
|
|
char copy_dir[] = "/tmp/test-copy_tree-copy/";
|
|
|
|
char **files = STRV_MAKE("file", "dir1/file", "dir1/dir2/file", "dir1/dir2/dir3/dir4/dir5/file");
|
|
|
|
char **links = STRV_MAKE("link", "file",
|
|
|
|
"link2", "dir1/file");
|
|
|
|
char **p, **link;
|
|
|
|
|
2016-03-14 18:18:14 +01:00
|
|
|
log_info("%s", __func__);
|
|
|
|
|
2015-04-04 11:52:57 +02:00
|
|
|
(void) rm_rf(copy_dir, REMOVE_ROOT|REMOVE_PHYSICAL);
|
|
|
|
(void) rm_rf(original_dir, REMOVE_ROOT|REMOVE_PHYSICAL);
|
2014-10-29 22:28:50 +01:00
|
|
|
|
|
|
|
STRV_FOREACH(p, files) {
|
2015-02-03 02:05:59 +01:00
|
|
|
char *f = strjoina(original_dir, *p);
|
2014-10-29 22:28:50 +01:00
|
|
|
|
|
|
|
assert_se(mkdir_parents(f, 0755) >= 0);
|
2015-07-07 01:19:25 +02:00
|
|
|
assert_se(write_string_file(f, "file", WRITE_STRING_FILE_CREATE) == 0);
|
2014-10-29 22:28:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
STRV_FOREACH_PAIR(link, p, links) {
|
2015-02-03 02:05:59 +01:00
|
|
|
char *f = strjoina(original_dir, *p);
|
|
|
|
char *l = strjoina(original_dir, *link);
|
2014-10-29 22:28:50 +01:00
|
|
|
|
|
|
|
assert_se(mkdir_parents(l, 0755) >= 0);
|
|
|
|
assert_se(symlink(f, l) == 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
assert_se(copy_tree(original_dir, copy_dir, true) == 0);
|
|
|
|
|
|
|
|
STRV_FOREACH(p, files) {
|
|
|
|
_cleanup_free_ char *buf = NULL;
|
|
|
|
size_t sz = 0;
|
2015-02-03 02:05:59 +01:00
|
|
|
char *f = strjoina(copy_dir, *p);
|
2014-10-29 22:28:50 +01:00
|
|
|
|
|
|
|
assert_se(access(f, F_OK) == 0);
|
|
|
|
assert_se(read_full_file(f, &buf, &sz) == 0);
|
|
|
|
assert_se(streq(buf, "file\n"));
|
|
|
|
}
|
|
|
|
|
|
|
|
STRV_FOREACH_PAIR(link, p, links) {
|
|
|
|
_cleanup_free_ char *target = NULL;
|
2015-02-03 02:05:59 +01:00
|
|
|
char *f = strjoina(original_dir, *p);
|
|
|
|
char *l = strjoina(copy_dir, *link);
|
2014-10-29 22:28:50 +01:00
|
|
|
|
|
|
|
assert_se(readlink_and_canonicalize(l, &target) == 0);
|
|
|
|
assert_se(path_equal(f, target));
|
|
|
|
}
|
|
|
|
|
|
|
|
assert_se(copy_tree(original_dir, copy_dir, false) < 0);
|
|
|
|
assert_se(copy_tree("/tmp/inexistent/foo/bar/fsdoi", copy_dir, false) < 0);
|
|
|
|
|
2015-04-04 11:52:57 +02:00
|
|
|
(void) rm_rf(copy_dir, REMOVE_ROOT|REMOVE_PHYSICAL);
|
|
|
|
(void) rm_rf(original_dir, REMOVE_ROOT|REMOVE_PHYSICAL);
|
2014-10-29 22:28:50 +01:00
|
|
|
}
|
|
|
|
|
2015-06-02 18:55:27 +02:00
|
|
|
static void test_copy_bytes(void) {
|
|
|
|
_cleanup_close_pair_ int pipefd[2] = {-1, -1};
|
|
|
|
_cleanup_close_ int infd = -1;
|
|
|
|
int r, r2;
|
|
|
|
char buf[1024], buf2[1024];
|
|
|
|
|
2015-06-23 17:22:40 +02:00
|
|
|
infd = open("/usr/lib/os-release", O_RDONLY|O_CLOEXEC);
|
|
|
|
if (infd < 0)
|
|
|
|
infd = open("/etc/os-release", O_RDONLY|O_CLOEXEC);
|
2015-06-02 18:55:27 +02:00
|
|
|
assert_se(infd >= 0);
|
|
|
|
|
|
|
|
assert_se(pipe2(pipefd, O_CLOEXEC) == 0);
|
|
|
|
|
2015-09-10 18:16:18 +02:00
|
|
|
r = copy_bytes(infd, pipefd[1], (uint64_t) -1, false);
|
2015-06-02 18:55:27 +02:00
|
|
|
assert_se(r == 0);
|
|
|
|
|
|
|
|
r = read(pipefd[0], buf, sizeof(buf));
|
|
|
|
assert_se(r >= 0);
|
|
|
|
|
|
|
|
assert_se(lseek(infd, 0, SEEK_SET) == 0);
|
|
|
|
r2 = read(infd, buf2, sizeof(buf2));
|
|
|
|
assert_se(r == r2);
|
|
|
|
|
|
|
|
assert_se(strneq(buf, buf2, r));
|
|
|
|
|
|
|
|
/* test copy_bytes with invalid descriptors */
|
|
|
|
r = copy_bytes(pipefd[0], pipefd[0], 1, false);
|
|
|
|
assert_se(r == -EBADF);
|
|
|
|
|
|
|
|
r = copy_bytes(pipefd[1], pipefd[1], 1, false);
|
|
|
|
assert_se(r == -EBADF);
|
|
|
|
|
|
|
|
r = copy_bytes(pipefd[1], infd, 1, false);
|
|
|
|
assert_se(r == -EBADF);
|
|
|
|
}
|
|
|
|
|
2016-03-21 14:55:30 +01:00
|
|
|
static void test_copy_bytes_regular_file(const char *src, bool try_reflink, uint64_t max_bytes) {
|
2016-03-14 18:18:14 +01:00
|
|
|
char fn2[] = "/tmp/test-copy-file-XXXXXX";
|
|
|
|
char fn3[] = "/tmp/test-copy-file-XXXXXX";
|
|
|
|
_cleanup_close_ int fd = -1, fd2 = -1, fd3 = -1;
|
|
|
|
int r;
|
|
|
|
struct stat buf, buf2, buf3;
|
|
|
|
|
2016-03-21 14:55:30 +01:00
|
|
|
log_info("%s try_reflink=%s max_bytes=%" PRIu64, __func__, yes_no(try_reflink), max_bytes);
|
2016-03-14 18:18:14 +01:00
|
|
|
|
|
|
|
fd = open(src, O_RDONLY | O_CLOEXEC | O_NOCTTY);
|
|
|
|
assert_se(fd >= 0);
|
|
|
|
|
|
|
|
fd2 = mkostemp_safe(fn2, O_RDWR);
|
|
|
|
assert_se(fd2 >= 0);
|
|
|
|
|
|
|
|
fd3 = mkostemp_safe(fn3, O_WRONLY);
|
|
|
|
assert_se(fd3 >= 0);
|
|
|
|
|
2016-03-15 18:55:50 +01:00
|
|
|
r = copy_bytes(fd, fd2, max_bytes, try_reflink);
|
|
|
|
if (max_bytes == (uint64_t) -1)
|
|
|
|
assert_se(r == 0);
|
|
|
|
else
|
|
|
|
assert_se(IN_SET(r, 0, 1));
|
2016-03-14 18:18:14 +01:00
|
|
|
|
|
|
|
assert_se(lseek(fd2, 0, SEEK_SET) == 0);
|
|
|
|
|
2016-03-15 18:55:50 +01:00
|
|
|
r = copy_bytes(fd2, fd3, max_bytes, try_reflink);
|
|
|
|
if (max_bytes == (uint64_t) -1)
|
|
|
|
assert_se(r == 0);
|
|
|
|
else
|
|
|
|
/* We cannot distinguish between the input being exactly max_bytes
|
|
|
|
* or longer than max_bytes (without trying to read one more byte,
|
|
|
|
* or calling stat, or FION_READ, etc, and we don't want to do any
|
|
|
|
* of that). So we expect "truncation" since we know that file we
|
|
|
|
* are copying is exactly max_bytes bytes. */
|
|
|
|
assert_se(r == 1);
|
2016-03-14 18:18:14 +01:00
|
|
|
|
|
|
|
assert_se(fstat(fd, &buf) == 0);
|
|
|
|
assert_se(fstat(fd2, &buf2) == 0);
|
|
|
|
assert_se(fstat(fd3, &buf3) == 0);
|
|
|
|
|
2016-03-21 14:55:30 +01:00
|
|
|
assert_se((uint64_t) buf2.st_size == MIN((uint64_t) buf.st_size, max_bytes));
|
2016-03-15 18:55:50 +01:00
|
|
|
assert_se(buf3.st_size == buf2.st_size);
|
2016-03-14 18:18:14 +01:00
|
|
|
|
|
|
|
unlink(fn2);
|
|
|
|
unlink(fn3);
|
|
|
|
}
|
|
|
|
|
2014-10-29 22:28:50 +01:00
|
|
|
int main(int argc, char *argv[]) {
|
|
|
|
test_copy_file();
|
2014-11-06 21:19:20 +01:00
|
|
|
test_copy_file_fd();
|
2014-10-29 22:28:50 +01:00
|
|
|
test_copy_tree();
|
2015-06-02 18:55:27 +02:00
|
|
|
test_copy_bytes();
|
2016-03-15 18:55:50 +01:00
|
|
|
test_copy_bytes_regular_file(argv[0], false, (uint64_t) -1);
|
|
|
|
test_copy_bytes_regular_file(argv[0], true, (uint64_t) -1);
|
|
|
|
test_copy_bytes_regular_file(argv[0], false, 1000); /* smaller than copy buffer size */
|
|
|
|
test_copy_bytes_regular_file(argv[0], true, 1000);
|
|
|
|
test_copy_bytes_regular_file(argv[0], false, 32000); /* larger than copy buffer size */
|
|
|
|
test_copy_bytes_regular_file(argv[0], true, 32000);
|
2014-10-29 22:28:50 +01:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|