1 /* SPDX-License-Identifier: MIT */
2 /*
3 * Description: run various nop 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_rename(struct io_uring * ring,const char * old,const char * new)16 static int test_rename(struct io_uring *ring, const char *old, const char *new)
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
28 memset(sqe, 0, sizeof(*sqe));
29
30 io_uring_prep_rename(sqe, old, new);
31
32 ret = io_uring_submit(ring);
33 if (ret <= 0) {
34 fprintf(stderr, "sqe submit failed: %d\n", ret);
35 goto err;
36 }
37
38 ret = io_uring_wait_cqe(ring, &cqe);
39 if (ret < 0) {
40 fprintf(stderr, "wait completion %d\n", ret);
41 goto err;
42 }
43 ret = cqe->res;
44 io_uring_cqe_seen(ring, cqe);
45 return ret;
46 err:
47 return 1;
48 }
49
stat_file(const char * buf)50 static int stat_file(const char *buf)
51 {
52 struct stat sb;
53
54 if (!stat(buf, &sb))
55 return 0;
56
57 return errno;
58 }
59
main(int argc,char * argv[])60 int main(int argc, char *argv[])
61 {
62 struct io_uring ring;
63 char src[32] = "./XXXXXX";
64 char dst[32] = "./XXXXXX";
65 int ret;
66
67 if (argc > 1)
68 return 0;
69
70 ret = io_uring_queue_init(1, &ring, 0);
71 if (ret) {
72 fprintf(stderr, "ring setup failed: %d\n", ret);
73 return 1;
74 }
75
76 ret = mkstemp(src);
77 if (ret < 0) {
78 perror("mkstemp");
79 return 1;
80 }
81 close(ret);
82
83 ret = mkstemp(dst);
84 if (ret < 0) {
85 perror("mkstemp");
86 return 1;
87 }
88 close(ret);
89
90 if (stat_file(src) != 0) {
91 perror("stat");
92 return 1;
93 }
94 if (stat_file(dst) != 0) {
95 perror("stat");
96 return 1;
97 }
98
99 ret = test_rename(&ring, src, dst);
100 if (ret < 0) {
101 if (ret == -EBADF || ret == -EINVAL) {
102 fprintf(stdout, "Rename not supported, skipping\n");
103 goto out;
104 }
105 fprintf(stderr, "rename: %s\n", strerror(-ret));
106 goto err;
107 } else if (ret)
108 goto err;
109
110 if (stat_file(src) != ENOENT) {
111 fprintf(stderr, "stat got %s\n", strerror(ret));
112 return 1;
113 }
114
115 if (stat_file(dst) != 0) {
116 perror("stat");
117 return 1;
118 }
119
120 ret = test_rename(&ring, "/x/y/1/2", "/2/1/y/x");
121 if (ret != -ENOENT) {
122 fprintf(stderr, "test_rename invalid failed: %d\n", ret);
123 return ret;
124 }
125 out:
126 unlink(dst);
127 return 0;
128 err:
129 unlink(src);
130 unlink(dst);
131 return 1;
132 }
133