Line data Source code
1 : //! Enum-dispatch to the `OpenOptions` type of the respective [`super::IoEngineKind`];
2 :
3 : use super::io_engine::IoEngine;
4 : use std::{os::fd::OwnedFd, path::Path};
5 :
6 : #[derive(Debug, Clone)]
7 : pub enum OpenOptions {
8 : StdFs(std::fs::OpenOptions),
9 : #[cfg(target_os = "linux")]
10 : TokioEpollUring(tokio_epoll_uring::ops::open_at::OpenOptions),
11 : }
12 :
13 : impl Default for OpenOptions {
14 6119 : fn default() -> Self {
15 6119 : match super::io_engine::get() {
16 0 : IoEngine::NotSet => panic!("io engine not set"),
17 3058 : IoEngine::StdFs => Self::StdFs(std::fs::OpenOptions::new()),
18 : #[cfg(target_os = "linux")]
19 : IoEngine::TokioEpollUring => {
20 3061 : Self::TokioEpollUring(tokio_epoll_uring::ops::open_at::OpenOptions::new())
21 : }
22 : }
23 6119 : }
24 : }
25 :
26 : impl OpenOptions {
27 6119 : pub fn new() -> OpenOptions {
28 6119 : Self::default()
29 6119 : }
30 :
31 4120 : pub fn read(&mut self, read: bool) -> &mut OpenOptions {
32 4120 : match self {
33 2060 : OpenOptions::StdFs(x) => {
34 2060 : let _ = x.read(read);
35 2060 : }
36 : #[cfg(target_os = "linux")]
37 2060 : OpenOptions::TokioEpollUring(x) => {
38 2060 : let _ = x.read(read);
39 2060 : }
40 : }
41 4120 : self
42 4120 : }
43 :
44 3277 : pub fn write(&mut self, write: bool) -> &mut OpenOptions {
45 3277 : match self {
46 1637 : OpenOptions::StdFs(x) => {
47 1637 : let _ = x.write(write);
48 1637 : }
49 : #[cfg(target_os = "linux")]
50 1640 : OpenOptions::TokioEpollUring(x) => {
51 1640 : let _ = x.write(write);
52 1640 : }
53 : }
54 3277 : self
55 3277 : }
56 :
57 8676 : pub fn create(&mut self, create: bool) -> &mut OpenOptions {
58 8676 : match self {
59 4335 : OpenOptions::StdFs(x) => {
60 4335 : let _ = x.create(create);
61 4335 : }
62 : #[cfg(target_os = "linux")]
63 4341 : OpenOptions::TokioEpollUring(x) => {
64 4341 : let _ = x.create(create);
65 4341 : }
66 : }
67 8676 : self
68 8676 : }
69 :
70 6427 : pub fn create_new(&mut self, create_new: bool) -> &mut OpenOptions {
71 6427 : match self {
72 3212 : OpenOptions::StdFs(x) => {
73 3212 : let _ = x.create_new(create_new);
74 3212 : }
75 : #[cfg(target_os = "linux")]
76 3215 : OpenOptions::TokioEpollUring(x) => {
77 3215 : let _ = x.create_new(create_new);
78 3215 : }
79 : }
80 6427 : self
81 6427 : }
82 :
83 7402 : pub fn truncate(&mut self, truncate: bool) -> &mut OpenOptions {
84 7402 : match self {
85 3698 : OpenOptions::StdFs(x) => {
86 3698 : let _ = x.truncate(truncate);
87 3698 : }
88 : #[cfg(target_os = "linux")]
89 3704 : OpenOptions::TokioEpollUring(x) => {
90 3704 : let _ = x.truncate(truncate);
91 3704 : }
92 : }
93 7402 : self
94 7402 : }
95 :
96 193523 : pub(in crate::virtual_file) async fn open(&self, path: &Path) -> std::io::Result<OwnedFd> {
97 193523 : match self {
98 98361 : OpenOptions::StdFs(x) => x.open(path).map(|file| file.into()),
99 : #[cfg(target_os = "linux")]
100 95162 : OpenOptions::TokioEpollUring(x) => {
101 95162 : let system = super::io_engine::tokio_epoll_uring_ext::thread_local_system().await;
102 95162 : system.open(path, x).await.map_err(|e| match e {
103 0 : tokio_epoll_uring::Error::Op(e) => e,
104 0 : tokio_epoll_uring::Error::System(system) => {
105 0 : std::io::Error::new(std::io::ErrorKind::Other, system)
106 : }
107 95162 : })
108 : }
109 : }
110 193523 : }
111 : }
112 :
113 : impl std::os::unix::prelude::OpenOptionsExt for OpenOptions {
114 0 : fn mode(&mut self, mode: u32) -> &mut OpenOptions {
115 0 : match self {
116 0 : OpenOptions::StdFs(x) => {
117 0 : let _ = x.mode(mode);
118 0 : }
119 : #[cfg(target_os = "linux")]
120 0 : OpenOptions::TokioEpollUring(x) => {
121 0 : let _ = x.mode(mode);
122 0 : }
123 : }
124 0 : self
125 0 : }
126 :
127 0 : fn custom_flags(&mut self, flags: i32) -> &mut OpenOptions {
128 0 : match self {
129 0 : OpenOptions::StdFs(x) => {
130 0 : let _ = x.custom_flags(flags);
131 0 : }
132 : #[cfg(target_os = "linux")]
133 0 : OpenOptions::TokioEpollUring(x) => {
134 0 : let _ = x.custom_flags(flags);
135 0 : }
136 : }
137 0 : self
138 0 : }
139 : }
|