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