1 /* SPDX-License-Identifier: MIT */
2 /*
3 * Description: run various unlink tests
4 *
5 */
6 #include <errno.h>
7 #include <stdio.h>
8 #include <unistd.h>
9 #include <stdlib.h>
10 #include <string.h>
11 #include <fcntl.h>
12 #include <sys/stat.h>
13
14 #include "liburing.h"
15
test_unlink(struct io_uring * ring,const char * old)16 static int test_unlink(struct io_uring *ring, const char *old)
17 {
18 struct io_uring_cqe *cqe;
19 struct io_uring_sqe *sqe;
20 int ret;
21
22 sqe = io_uring_get_sqe(ring);
23 if (!sqe) {
24 fprintf(stderr, "get sqe failed\n");
25 goto err;
26 }
27 io_uring_prep_unlink(sqe, old, 0);
28
29 ret = io_uring_submit(ring);
30 if (ret <= 0) {
31 fprintf(stderr, "sqe submit failed: %d\n", ret);
32 goto err;
33 }
34
35 ret = io_uring_wait_cqe(ring, &cqe);
36 if (ret < 0) {
37 fprintf(stderr, "wait completion %d\n", ret);
38 goto err;
39 }
40 ret = cqe->res;
41 io_uring_cqe_seen(ring, cqe);
42 return ret;
43 err:
44 return 1;
45 }
46
stat_file(const char * buf)47 static int stat_file(const char *buf)
48 {
49 struct stat sb;
50
51 if (!stat(buf, &sb))
52 return 0;
53
54 return errno;
55 }
56
main(int argc,char * argv[])57 int main(int argc, char *argv[])
58 {
59 struct io_uring ring;
60 char buf[32] = "./XXXXXX";
61 int ret;
62
63 if (argc > 1)
64 return 0;
65
66 ret = io_uring_queue_init(1, &ring, 0);
67 if (ret) {
68 fprintf(stderr, "ring setup failed: %d\n", ret);
69 return 1;
70 }
71
72 ret = mkstemp(buf);
73 if (ret < 0) {
74 perror("mkstemp");
75 return 1;
76 }
77 close(ret);
78
79 if (stat_file(buf) != 0) {
80 perror("stat");
81 return 1;
82 }
83
84 ret = test_unlink(&ring, buf);
85 if (ret < 0) {
86 if (ret == -EBADF || ret == -EINVAL) {
87 fprintf(stdout, "Unlink not supported, skipping\n");
88 unlink(buf);
89 return 0;
90 }
91 fprintf(stderr, "rename: %s\n", strerror(-ret));
92 goto err;
93 } else if (ret)
94 goto err;
95
96 ret = stat_file(buf);
97 if (ret != ENOENT) {
98 fprintf(stderr, "stat got %s\n", strerror(ret));
99 return 1;
100 }
101
102 ret = test_unlink(&ring, "/3/2/3/1/z/y");
103 if (ret != -ENOENT) {
104 fprintf(stderr, "invalid unlink got %s\n", strerror(-ret));
105 return 1;
106 }
107
108 return 0;
109 err:
110 unlink(buf);
111 return 1;
112 }
113