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