summaryrefslogtreecommitdiffstats
path: root/src/test
diff options
context:
space:
mode:
authorDaan De Meyer <daan.j.demeyer@gmail.com>2023-06-20 15:22:24 +0200
committerGitHub <noreply@github.com>2023-06-20 15:22:24 +0200
commit1a5f67aba3c31282a2f353921e18cc013816006f (patch)
tree36931cf1ec110b1eec206105b3308a89cf65a7df /src/test
parentupdate TODO (diff)
parentbtrfs-util: Add BTRFS_SNAPSHOT_LOCK_BSD (diff)
downloadsystemd-1a5f67aba3c31282a2f353921e18cc013816006f.tar.xz
systemd-1a5f67aba3c31282a2f353921e18cc013816006f.zip
Merge pull request #27863 from DaanDeMeyer/copy-lock
Add helpers to lock a directory before copying into it
Diffstat (limited to 'src/test')
-rw-r--r--src/test/test-btrfs.c19
-rw-r--r--src/test/test-copy.c21
2 files changed, 38 insertions, 2 deletions
diff --git a/src/test/test-btrfs.c b/src/test/test-btrfs.c
index ca01a8c856..95b7ef25d8 100644
--- a/src/test/test-btrfs.c
+++ b/src/test/test-btrfs.c
@@ -4,6 +4,7 @@
#include "btrfs-util.h"
#include "fd-util.h"
+#include "fs-util.h"
#include "fileio.h"
#include "format-util.h"
#include "log.h"
@@ -62,6 +63,14 @@ int main(int argc, char *argv[]) {
if (r < 0)
log_error_errno(r, "Failed to make snapshot: %m");
+ r = btrfs_subvol_snapshot_at(AT_FDCWD, "/xxxtest", AT_FDCWD, "/xxxtest4", BTRFS_SNAPSHOT_LOCK_BSD);
+ if (r < 0)
+ log_error_errno(r, "Failed to make snapshot: %m");
+ if (r >= 0)
+ assert_se(xopenat_lock(AT_FDCWD, "/xxxtest4", 0, 0, 0, LOCK_BSD, LOCK_EX|LOCK_NB) == -EAGAIN);
+
+ safe_close(r);
+
r = btrfs_subvol_remove("/xxxtest", BTRFS_REMOVE_QUOTA);
if (r < 0)
log_error_errno(r, "Failed to remove subvolume: %m");
@@ -74,6 +83,10 @@ int main(int argc, char *argv[]) {
if (r < 0)
log_error_errno(r, "Failed to remove subvolume: %m");
+ r = btrfs_subvol_remove("/xxxtest4", BTRFS_REMOVE_QUOTA);
+ if (r < 0)
+ log_error_errno(r, "Failed to remove subvolume: %m");
+
r = btrfs_subvol_snapshot_at(AT_FDCWD, "/etc", AT_FDCWD, "/etc2",
BTRFS_SNAPSHOT_READ_ONLY|BTRFS_SNAPSHOT_FALLBACK_COPY);
if (r < 0)
@@ -161,13 +174,15 @@ int main(int argc, char *argv[]) {
if (r < 0)
log_error_errno(r, "Failed to query quota: %m");
- assert_se(quota.referenced_max == 4ULL * 1024 * 1024 * 1024);
+ if (r >= 0)
+ assert_se(quota.referenced_max == 4ULL * 1024 * 1024 * 1024);
r = btrfs_subvol_get_subtree_quota("/xxxquotatest2", 0, &quota);
if (r < 0)
log_error_errno(r, "Failed to query quota: %m");
- assert_se(quota.referenced_max == 5ULL * 1024 * 1024 * 1024);
+ if (r >= 0)
+ assert_se(quota.referenced_max == 5ULL * 1024 * 1024 * 1024);
r = btrfs_subvol_remove("/xxxquotatest", BTRFS_REMOVE_QUOTA|BTRFS_REMOVE_RECURSIVE);
if (r < 0)
diff --git a/src/test/test-copy.c b/src/test/test-copy.c
index f5bcb8756f..72aea4efb6 100644
--- a/src/test/test-copy.c
+++ b/src/test/test-copy.c
@@ -1,5 +1,6 @@
/* SPDX-License-Identifier: LGPL-2.1-or-later */
+#include <sys/file.h>
#include <sys/xattr.h>
#include <unistd.h>
@@ -435,4 +436,24 @@ TEST_RET(copy_holes) {
return 0;
}
+TEST(copy_lock) {
+ _cleanup_(rm_rf_physical_and_freep) char *t = NULL;
+ _cleanup_close_ int tfd = -EBADF, fd = -EBADF;
+
+ assert_se((tfd = mkdtemp_open(NULL, 0, &t)) >= 0);
+ assert_se(mkdirat(tfd, "abc", 0755) >= 0);
+ assert_se(write_string_file_at(tfd, "abc/def", "abc", WRITE_STRING_FILE_CREATE) >= 0);
+
+ assert_se((fd = copy_directory_at(tfd, "abc", tfd, "qed", COPY_LOCK_BSD)) >= 0);
+ assert_se(faccessat(tfd, "qed", F_OK, 0) >= 0);
+ assert_se(faccessat(tfd, "qed/def", F_OK, 0) >= 0);
+ assert_se(xopenat_lock(tfd, "qed", 0, 0, 0, LOCK_BSD, LOCK_EX|LOCK_NB) == -EAGAIN);
+ fd = safe_close(fd);
+
+ assert_se((fd = copy_file_at(tfd, "abc/def", tfd, "poi", 0, 0644, COPY_LOCK_BSD)));
+ assert_se(read_file_at_and_streq(tfd, "poi", "abc\n"));
+ assert_se(xopenat_lock(tfd, "poi", 0, 0, 0, LOCK_BSD, LOCK_EX|LOCK_NB) == -EAGAIN);
+ fd = safe_close(fd);
+}
+
DEFINE_TEST_MAIN(LOG_DEBUG);