-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathio_uring_cat.c
More file actions
74 lines (72 loc) · 1.7 KB
/
Copy pathio_uring_cat.c
File metadata and controls
74 lines (72 loc) · 1.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#define _GNU_SOURCE
#define _POSIX_C_SOURCE 200809L
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <liburing.h>
#include <string.h>
#include <errno.h>
#define BUFSZ 8192
int main(int argc, char **argv)
{
#if !defined(__linux__)
fprintf(stderr, "io_uring_cat is Linux-only\n");
return 1;
#else
if (argc < 2) {
fprintf(stderr, "usage: %s file\n", argv[0]);
return 1;
}
const char *path = argv[1];
int fd = open(path, O_RDONLY);
if (fd < 0) {
perror("open");
return 1;
}
struct io_uring ring;
if (io_uring_queue_init(8, &ring, 0) < 0) {
perror("io_uring_queue_init");
close(fd);
return 1;
}
char *buf = malloc(BUFSZ);
if (!buf) {
close(fd);
io_uring_queue_exit(&ring);
return 1;
}
off_t off = 0;
for (;;) {
struct io_uring_sqe *sqe = io_uring_get_sqe(&ring);
if (!sqe) {
io_uring_submit(&ring);
continue;
}
io_uring_prep_read(sqe, fd, buf, BUFSZ, off);
io_uring_sqe_set_data(sqe, buf);
if (io_uring_submit(&ring) < 0) {
perror("io_uring_submit");
break;
}
struct io_uring_cqe *cqe;
if (io_uring_wait_cqe(&ring, &cqe) < 0) {
perror("io_uring_wait_cqe");
break;
}
int res = cqe->res;
if (res <= 0) {
io_uring_cqe_seen(&ring, cqe);
break;
}
write(STDOUT_FILENO, buf, res);
off += res;
io_uring_cqe_seen(&ring, cqe);
if (res < BUFSZ) break;
}
free(buf);
io_uring_queue_exit(&ring);
close(fd);
return 0;
#endif
}