]> git.ipfire.org Git - thirdparty/systemd.git/blobdiff - src/test/test-copy.c
Merge pull request #12487 from mschiu77/acer-series-hwdb
[thirdparty/systemd.git] / src / test / test-copy.c
index 68154fc4e82efa40b06eb3ece23367554fb933bb..5f4bc39580de4c893e9465b1cdf193f882184ecc 100644 (file)
@@ -1,21 +1,4 @@
-/***
-  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/>.
-***/
+/* SPDX-License-Identifier: LGPL-2.1+ */
 
 #include <unistd.h>
 
@@ -31,6 +14,9 @@
 #include "rm-rf.h"
 #include "string-util.h"
 #include "strv.h"
+#include "tests.h"
+#include "tmpfile-util.h"
+#include "user-util.h"
 #include "util.h"
 
 static void test_copy_file(void) {
@@ -42,17 +28,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, 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 +57,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);
@@ -125,11 +111,11 @@ 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;
+                _cleanup_free_ char *buf, *f;
+                size_t sz;
 
                 assert_se(f = strappend(copy_dir, *p));
 
@@ -139,12 +125,12 @@ static void test_copy_tree(void) {
         }
 
         STRV_FOREACH_PAIR(link, p, links) {
-                _cleanup_free_ char *target = NULL, *f, *l;
+                _cleanup_free_ char *target, *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(chase_symlinks(l, NULL, 0, &target) == 1);
                 assert_se(path_equal(f, target));
         }
 
@@ -152,8 +138,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 +158,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 +171,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 +193,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,18 +226,38 @@ 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);
 }
 
+static void test_copy_atomic(void) {
+        _cleanup_(rm_rf_physical_and_freep) char *p = NULL;
+        const char *q;
+        int r;
+
+        assert_se(mkdtemp_malloc(NULL, &p) >= 0);
+
+        q = strjoina(p, "/fstab");
+
+        r = copy_file_atomic("/etc/fstab", q, 0644, 0, 0, COPY_REFLINK);
+        if (r == -ENOENT)
+                return;
+
+        assert_se(copy_file_atomic("/etc/fstab", q, 0644, 0, 0, COPY_REFLINK) == -EEXIST);
+
+        assert_se(copy_file_atomic("/etc/fstab", q, 0644, 0, 0, COPY_REPLACE) >= 0);
+}
+
 int main(int argc, char *argv[]) {
+        test_setup_logging(LOG_DEBUG);
+
         test_copy_file();
         test_copy_file_fd();
         test_copy_tree();
@@ -254,6 +268,7 @@ int main(int argc, char *argv[]) {
         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);
+        test_copy_atomic();
 
         return 0;
 }