]> git.ipfire.org Git - thirdparty/systemd.git/blobdiff - src/test/test-copy.c
Add SPDX license identifiers to source files under the LGPL
[thirdparty/systemd.git] / src / test / test-copy.c
index 68154fc4e82efa40b06eb3ece23367554fb933bb..d277b78c5b3bf6f0321a73f9b5d28a6390701447 100644 (file)
@@ -1,3 +1,4 @@
+/* SPDX-License-Identifier: LGPL-2.1+ */
 /***
   This file is part of systemd
 
@@ -31,6 +32,7 @@
 #include "rm-rf.h"
 #include "string-util.h"
 #include "strv.h"
+#include "user-util.h"
 #include "util.h"
 
 static void test_copy_file(void) {
@@ -42,17 +44,17 @@ static void test_copy_file(void) {
 
         log_info("%s", __func__);
 
-        fd = mkostemp_safe(fn, O_RDWR|O_CLOEXEC);
+        fd = mkostemp_safe(fn);
         assert_se(fd >= 0);
         close(fd);
 
-        fd = mkostemp_safe(fn_copy, O_RDWR|O_CLOEXEC);
+        fd = mkostemp_safe(fn_copy);
         assert_se(fd >= 0);
         close(fd);
 
         assert_se(write_string_file(fn, "foo bar bar bar foo", WRITE_STRING_FILE_CREATE) == 0);
 
-        assert_se(copy_file(fn, fn_copy, 0, 0644, 0) == 0);
+        assert_se(copy_file(fn, fn_copy, 0, 0644, 0, COPY_REFLINK) == 0);
 
         assert_se(read_full_file(fn_copy, &buf, &sz) == 0);
         assert_se(streq(buf, "foo bar bar bar foo\n"));
@@ -71,14 +73,14 @@ static void test_copy_file_fd(void) {
 
         log_info("%s", __func__);
 
-        in_fd = mkostemp_safe(in_fn, O_RDWR);
+        in_fd = mkostemp_safe(in_fn);
         assert_se(in_fd >= 0);
-        out_fd = mkostemp_safe(out_fn, O_RDWR);
+        out_fd = mkostemp_safe(out_fn);
         assert_se(out_fd >= 0);
 
         assert_se(write_string_file(in_fn, text, WRITE_STRING_FILE_CREATE) == 0);
-        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);
+        assert_se(copy_file_fd("/a/file/which/does/not/exist/i/guess", out_fd, COPY_REFLINK) < 0);
+        assert_se(copy_file_fd(in_fn, out_fd, COPY_REFLINK) >= 0);
         assert_se(lseek(out_fd, SEEK_SET, 0) == 0);
 
         assert_se(read(out_fd, buf, sizeof(buf)) == sizeof(text) - 1);
@@ -106,7 +108,7 @@ static void test_copy_tree(void) {
         STRV_FOREACH(p, files) {
                 _cleanup_free_ char *f;
 
-                assert_se(f = strappend(original_dir, *p));
+                assert_se((f = strappend(original_dir, *p)));
 
                 assert_se(mkdir_parents(f, 0755) >= 0);
                 assert_se(write_string_file(f, "file", WRITE_STRING_FILE_CREATE) == 0);
@@ -115,8 +117,8 @@ static void test_copy_tree(void) {
         STRV_FOREACH_PAIR(link, p, links) {
                 _cleanup_free_ char *f, *l;
 
-                assert_se(f = strappend(original_dir, *p));
-                assert_se(l = strappend(original_dir, *link));
+                assert_se((f = strappend(original_dir, *p)));
+                assert_se((l = strappend(original_dir, *link)));
 
                 assert_se(mkdir_parents(l, 0755) >= 0);
                 assert_se(symlink(f, l) == 0);
@@ -125,13 +127,13 @@ static void test_copy_tree(void) {
         unixsockp = strjoina(original_dir, "unixsock");
         assert_se(mknod(unixsockp, S_IFSOCK|0644, 0) >= 0);
 
-        assert_se(copy_tree(original_dir, copy_dir, true) == 0);
+        assert_se(copy_tree(original_dir, copy_dir, UID_INVALID, GID_INVALID, COPY_REFLINK|COPY_MERGE) == 0);
 
         STRV_FOREACH(p, files) {
                 _cleanup_free_ char *buf = NULL, *f;
                 size_t sz = 0;
 
-                assert_se(f = strappend(copy_dir, *p));
+                assert_se((f = strappend(copy_dir, *p)));
 
                 assert_se(access(f, F_OK) == 0);
                 assert_se(read_full_file(f, &buf, &sz) == 0);
@@ -141,10 +143,10 @@ static void test_copy_tree(void) {
         STRV_FOREACH_PAIR(link, p, links) {
                 _cleanup_free_ char *target = NULL, *f, *l;
 
-                assert_se(f = strjoin(original_dir, *p, NULL));
-                assert_se(l = strjoin(copy_dir, *link, NULL));
+                assert_se((f = strjoin(original_dir, *p)));
+                assert_se((l = strjoin(copy_dir, *link)));
 
-                assert_se(readlink_and_canonicalize(l, &target) == 0);
+                assert_se(readlink_and_canonicalize(l, NULL, &target) == 0);
                 assert_se(path_equal(f, target));
         }
 
@@ -152,8 +154,8 @@ static void test_copy_tree(void) {
         assert_se(stat(unixsockp, &st) >= 0);
         assert_se(S_ISSOCK(st.st_mode));
 
-        assert_se(copy_tree(original_dir, copy_dir, false) < 0);
-        assert_se(copy_tree("/tmp/inexistent/foo/bar/fsdoi", copy_dir, false) < 0);
+        assert_se(copy_tree(original_dir, copy_dir, UID_INVALID, GID_INVALID, COPY_REFLINK) < 0);
+        assert_se(copy_tree("/tmp/inexistent/foo/bar/fsdoi", copy_dir, UID_INVALID, GID_INVALID, COPY_REFLINK) < 0);
 
         (void) rm_rf(copy_dir, REMOVE_ROOT|REMOVE_PHYSICAL);
         (void) rm_rf(original_dir, REMOVE_ROOT|REMOVE_PHYSICAL);
@@ -172,7 +174,7 @@ static void test_copy_bytes(void) {
 
         assert_se(pipe2(pipefd, O_CLOEXEC) == 0);
 
-        r = copy_bytes(infd, pipefd[1], (uint64_t) -1, false);
+        r = copy_bytes(infd, pipefd[1], (uint64_t) -1, 0);
         assert_se(r == 0);
 
         r = read(pipefd[0], buf, sizeof(buf));
@@ -185,13 +187,13 @@ static void test_copy_bytes(void) {
         assert_se(strneq(buf, buf2, r));
 
         /* test copy_bytes with invalid descriptors */
-        r = copy_bytes(pipefd[0], pipefd[0], 1, false);
+        r = copy_bytes(pipefd[0], pipefd[0], 1, 0);
         assert_se(r == -EBADF);
 
-        r = copy_bytes(pipefd[1], pipefd[1], 1, false);
+        r = copy_bytes(pipefd[1], pipefd[1], 1, 0);
         assert_se(r == -EBADF);
 
-        r = copy_bytes(pipefd[1], infd, 1, false);
+        r = copy_bytes(pipefd[1], infd, 1, 0);
         assert_se(r == -EBADF);
 }
 
@@ -207,21 +209,29 @@ static void test_copy_bytes_regular_file(const char *src, bool try_reflink, uint
         fd = open(src, O_RDONLY | O_CLOEXEC | O_NOCTTY);
         assert_se(fd >= 0);
 
-        fd2 = mkostemp_safe(fn2, O_RDWR);
+        fd2 = mkostemp_safe(fn2);
         assert_se(fd2 >= 0);
 
-        fd3 = mkostemp_safe(fn3, O_WRONLY);
+        fd3 = mkostemp_safe(fn3);
         assert_se(fd3 >= 0);
 
-        r = copy_bytes(fd, fd2, max_bytes, try_reflink);
+        r = copy_bytes(fd, fd2, max_bytes, try_reflink ? COPY_REFLINK : 0);
         if (max_bytes == (uint64_t) -1)
                 assert_se(r == 0);
         else
                 assert_se(IN_SET(r, 0, 1));
 
+        assert_se(fstat(fd, &buf) == 0);
+        assert_se(fstat(fd2, &buf2) == 0);
+        assert_se((uint64_t) buf2.st_size == MIN((uint64_t) buf.st_size, max_bytes));
+
+        if (max_bytes < (uint64_t) -1)
+                /* Make sure the file is now higher than max_bytes */
+                assert_se(ftruncate(fd2, max_bytes + 1) == 0);
+
         assert_se(lseek(fd2, 0, SEEK_SET) == 0);
 
-        r = copy_bytes(fd2, fd3, max_bytes, try_reflink);
+        r = copy_bytes(fd2, fd3, max_bytes, try_reflink ? COPY_REFLINK : 0);
         if (max_bytes == (uint64_t) -1)
                 assert_se(r == 0);
         else
@@ -232,12 +242,12 @@ static void test_copy_bytes_regular_file(const char *src, bool try_reflink, uint
                  * are copying is exactly max_bytes bytes. */
                 assert_se(r == 1);
 
-        assert_se(fstat(fd, &buf) == 0);
-        assert_se(fstat(fd2, &buf2) == 0);
         assert_se(fstat(fd3, &buf3) == 0);
 
-        assert_se((uint64_t) buf2.st_size == MIN((uint64_t) buf.st_size, max_bytes));
-        assert_se(buf3.st_size == buf2.st_size);
+        if (max_bytes == (uint64_t) -1)
+                assert_se(buf3.st_size == buf2.st_size);
+        else
+                assert_se((uint64_t) buf3.st_size == max_bytes);
 
         unlink(fn2);
         unlink(fn3);