The test is relative basic: tests only success, EEXISTS, ENOENT (no parent dir). Signed-off-by: Dmitry Kadashev <dkadashev@xxxxxxxxx> --- .gitignore | 1 + test/Makefile | 2 + test/mkdir.c | 105 ++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 108 insertions(+) create mode 100644 test/mkdir.c diff --git a/.gitignore b/.gitignore index 53c076f..0a49dad 100644 --- a/.gitignore +++ b/.gitignore @@ -68,6 +68,7 @@ /test/link-timeout /test/link_drain /test/madvise +/test/mkdir /test/nop /test/nop-all-sizes /test/open-close diff --git a/test/Makefile b/test/Makefile index f457ac7..718bca9 100644 --- a/test/Makefile +++ b/test/Makefile @@ -66,6 +66,7 @@ test_targets += \ link-timeout \ link_drain \ madvise \ + mkdir \ nop \ nop-all-sizes \ open-close \ @@ -186,6 +187,7 @@ test_srcs := \ link.c \ link_drain.c \ madvise.c \ + mkdir.c \ nop-all-sizes.c \ nop.c \ open-close.c \ diff --git a/test/mkdir.c b/test/mkdir.c new file mode 100644 index 0000000..88bb9b6 --- /dev/null +++ b/test/mkdir.c @@ -0,0 +1,105 @@ +/* SPDX-License-Identifier: MIT */ +/* + * Description: test io_uring mkdirat handling + */ +#include <fcntl.h> +#include <stdio.h> +#include <string.h> +#include <sys/stat.h> +#include <sys/types.h> +#include <unistd.h> + +#include "liburing.h" + +static int test_mkdirat(struct io_uring *ring, const char *fn) +{ + int ret; + struct io_uring_sqe *sqe; + struct io_uring_cqe *cqe; + + sqe = io_uring_get_sqe(ring); + if (!sqe) { + fprintf(stderr, "sqe get failed\n"); + goto err; + } + io_uring_prep_mkdirat(sqe, AT_FDCWD, fn, 0700); + + ret = io_uring_submit(ring); + if (ret != 1) { + fprintf(stderr, "submit failed: %d\n", ret); + goto err; + } + + ret = io_uring_wait_cqes(ring, &cqe, 1, 0, 0); + if (ret) { + fprintf(stderr, "wait_cqe failed: %d\n", ret); + goto err; + } + ret = cqe->res; + io_uring_cqe_seen(ring, cqe); + return ret; +err: + return 1; +} + +static int stat_file(const char *buf) +{ + struct stat sb; + + if (!stat(buf, &sb)) + return 0; + + return errno; +} + +int main(int argc, char *argv[]) +{ + static const char fn[] = "io_uring-mkdirat-test"; + int ret; + struct io_uring ring; + + ret = io_uring_queue_init(8, &ring, 0); + if (ret) { + fprintf(stderr, "queue init failed: %d\n", ret); + return ret; + } + + ret = test_mkdirat(&ring, fn); + if (ret < 0) { + if (ret == -EBADF || ret == -EINVAL) { + fprintf(stdout, "mkdirat not supported, skipping\n"); + goto out; + } + fprintf(stderr, "mkdirat: %s\n", strerror(-ret)); + goto err; + } else if (ret) { + goto err; + } + + if (stat_file(fn)) { + perror("stat"); + goto err; + } + + ret = test_mkdirat(&ring, fn); + if (ret != -EEXIST) { + fprintf(stderr, "test_mkdirat already exists failed: %d\n", ret); + goto err1; + } + + ret = test_mkdirat(&ring, "surely/this/wont/exist"); + if (ret != -ENOENT) { + fprintf(stderr, "test_mkdirat no parent failed: %d\n", ret); + goto err1; + } + +out: + unlinkat(AT_FDCWD, fn, AT_REMOVEDIR); + io_uring_queue_exit(&ring); + return 0; +err1: + unlinkat(AT_FDCWD, fn, AT_REMOVEDIR); +err: + io_uring_queue_exit(&ring); + return 1; +} -- 2.29.2