summaryrefslogtreecommitdiff
path: root/src/test/test-fs-util.c
diff options
context:
space:
mode:
authorDaan De Meyer <daan.j.demeyer@gmail.com>2023-03-14 09:55:04 +0100
committerDaan De Meyer <daan.j.demeyer@gmail.com>2023-03-14 09:55:04 +0100
commit7486f9c3421a5cd011c72a8c8d9c8b1cf62410ca (patch)
tree1a1fabfe61fa795fe8a3a9efae4d327fd1f3c2f4 /src/test/test-fs-util.c
parentd77d42ed3ae95ee035dce4707777b077d1a9bf8b (diff)
downloadsystemd-7486f9c3421a5cd011c72a8c8d9c8b1cf62410ca.tar.gz
fs-util: Add xopenat()
xopenat() will create directories if O_DIRECTORY and O_CREAT are specified. Note that this is not an atomic operation.
Diffstat (limited to 'src/test/test-fs-util.c')
-rw-r--r--src/test/test-fs-util.c25
1 files changed, 25 insertions, 0 deletions
diff --git a/src/test/test-fs-util.c b/src/test/test-fs-util.c
index c009424a2b..065ad3c6e7 100644
--- a/src/test/test-fs-util.c
+++ b/src/test/test-fs-util.c
@@ -1155,6 +1155,31 @@ TEST(openat_report_new) {
assert_se(b);
}
+TEST(xopenat) {
+ _cleanup_close_ int tfd = -EBADF, fd = -EBADF;
+ _cleanup_(rm_rf_physical_and_freep) char *t = NULL;
+
+ assert_se((tfd = mkdtemp_open(NULL, 0, &t)) >= 0);
+
+ /* Test that xopenat() creates directories if O_DIRECTORY is specified. */
+
+ assert_se((fd = xopenat(tfd, "abc", O_DIRECTORY|O_CREAT|O_EXCL|O_CLOEXEC, 0755)) >= 0);
+ assert_se((fd_verify_directory(fd) >= 0));
+ fd = safe_close(fd);
+
+ assert_se(xopenat(tfd, "abc", O_DIRECTORY|O_CREAT|O_EXCL|O_CLOEXEC, 0755) == -EEXIST);
+
+ assert_se((fd = xopenat(tfd, "abc", O_DIRECTORY|O_CREAT|O_CLOEXEC, 0755)) >= 0);
+ assert_se((fd_verify_directory(fd) >= 0));
+ fd = safe_close(fd);
+
+ /* Test that xopenat() creates regular files if O_DIRECTORY is not specified. */
+
+ assert_se((fd = xopenat(tfd, "def", O_CREAT|O_EXCL|O_CLOEXEC, 0644)) >= 0);
+ assert_se(fd_verify_regular(fd) >= 0);
+ fd = safe_close(fd);
+}
+
static int intro(void) {
arg_test_dir = saved_argv[1];
return EXIT_SUCCESS;