Line data Source code
1 : //! Enum-dispatch to the `OpenOptions` type of the respective [`super::IoEngineKind`];
2 :
3 : use super::IoEngineKind;
4 : use std::{os::fd::OwnedFd, path::Path};
5 :
6 80639 : #[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 80433 : fn default() -> Self {
15 80433 : match super::io_engine::get() {
16 78702 : IoEngineKind::StdFs => Self::StdFs(std::fs::OpenOptions::new()),
17 : #[cfg(target_os = "linux")]
18 : IoEngineKind::TokioEpollUring => {
19 1731 : Self::TokioEpollUring(tokio_epoll_uring::ops::open_at::OpenOptions::new())
20 : }
21 : }
22 80433 : }
23 : }
24 :
25 : impl OpenOptions {
26 80433 : pub fn new() -> OpenOptions {
27 80433 : Self::default()
28 80433 : }
29 :
30 49609 : pub fn read(&mut self, read: bool) -> &mut OpenOptions {
31 49609 : match self {
32 48481 : OpenOptions::StdFs(x) => {
33 48481 : let _ = x.read(read);
34 48481 : }
35 : #[cfg(target_os = "linux")]
36 1128 : OpenOptions::TokioEpollUring(x) => {
37 1128 : let _ = x.read(read);
38 1128 : }
39 : }
40 49609 : self
41 49609 : }
42 :
43 36543 : pub fn write(&mut self, write: bool) -> &mut OpenOptions {
44 36543 : match self {
45 35668 : OpenOptions::StdFs(x) => {
46 35668 : let _ = x.write(write);
47 35668 : }
48 : #[cfg(target_os = "linux")]
49 875 : OpenOptions::TokioEpollUring(x) => {
50 875 : let _ = x.write(write);
51 875 : }
52 : }
53 36543 : self
54 36543 : }
55 :
56 101892 : pub fn create(&mut self, create: bool) -> &mut OpenOptions {
57 101892 : match self {
58 99793 : OpenOptions::StdFs(x) => {
59 99793 : let _ = x.create(create);
60 99793 : }
61 : #[cfg(target_os = "linux")]
62 2099 : OpenOptions::TokioEpollUring(x) => {
63 2099 : let _ = x.create(create);
64 2099 : }
65 : }
66 101892 : self
67 101892 : }
68 :
69 95105 : pub fn create_new(&mut self, create_new: bool) -> &mut OpenOptions {
70 95105 : match self {
71 93073 : OpenOptions::StdFs(x) => {
72 93073 : let _ = x.create_new(create_new);
73 93073 : }
74 : #[cfg(target_os = "linux")]
75 2032 : OpenOptions::TokioEpollUring(x) => {
76 2032 : let _ = x.create_new(create_new);
77 2032 : }
78 : }
79 95105 : self
80 95105 : }
81 :
82 96177 : pub fn truncate(&mut self, truncate: bool) -> &mut OpenOptions {
83 96177 : match self {
84 94348 : OpenOptions::StdFs(x) => {
85 94348 : let _ = x.truncate(truncate);
86 94348 : }
87 : #[cfg(target_os = "linux")]
88 1829 : OpenOptions::TokioEpollUring(x) => {
89 1829 : let _ = x.truncate(truncate);
90 1829 : }
91 : }
92 96177 : self
93 96177 : }
94 :
95 284948 : pub(in crate::virtual_file) async fn open(&self, path: &Path) -> std::io::Result<OwnedFd> {
96 284948 : match self {
97 193025 : OpenOptions::StdFs(x) => x.open(path).map(|file| file.into()),
98 : #[cfg(target_os = "linux")]
99 91923 : OpenOptions::TokioEpollUring(x) => {
100 91923 : let system = tokio_epoll_uring::thread_local_system().await;
101 91927 : system.open(path, x).await.map_err(|e| match e {
102 0 : tokio_epoll_uring::Error::Op(e) => e,
103 0 : tokio_epoll_uring::Error::System(system) => {
104 0 : std::io::Error::new(std::io::ErrorKind::Other, system)
105 : }
106 91923 : })
107 : }
108 : }
109 284948 : }
110 : }
111 :
112 : impl std::os::unix::prelude::OpenOptionsExt for OpenOptions {
113 0 : fn mode(&mut self, mode: u32) -> &mut OpenOptions {
114 0 : match self {
115 0 : OpenOptions::StdFs(x) => {
116 0 : let _ = x.mode(mode);
117 0 : }
118 : #[cfg(target_os = "linux")]
119 0 : OpenOptions::TokioEpollUring(x) => {
120 0 : let _ = x.mode(mode);
121 0 : }
122 : }
123 0 : self
124 0 : }
125 :
126 0 : fn custom_flags(&mut self, flags: i32) -> &mut OpenOptions {
127 0 : match self {
128 0 : OpenOptions::StdFs(x) => {
129 0 : let _ = x.custom_flags(flags);
130 0 : }
131 : #[cfg(target_os = "linux")]
132 0 : OpenOptions::TokioEpollUring(x) => {
133 0 : let _ = x.custom_flags(flags);
134 0 : }
135 : }
136 0 : self
137 0 : }
138 : }
|