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 80160 : #[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 79954 : fn default() -> Self {
15 79954 : match super::io_engine::get() {
16 0 : IoEngine::NotSet => panic!("io engine not set"),
17 78223 : IoEngine::StdFs => Self::StdFs(std::fs::OpenOptions::new()),
18 : #[cfg(target_os = "linux")]
19 : IoEngine::TokioEpollUring => {
20 1731 : Self::TokioEpollUring(tokio_epoll_uring::ops::open_at::OpenOptions::new())
21 : }
22 : }
23 79954 : }
24 : }
25 :
26 : impl OpenOptions {
27 79954 : pub fn new() -> OpenOptions {
28 79954 : Self::default()
29 79954 : }
30 :
31 48643 : pub fn read(&mut self, read: bool) -> &mut OpenOptions {
32 48643 : match self {
33 47515 : OpenOptions::StdFs(x) => {
34 47515 : let _ = x.read(read);
35 47515 : }
36 : #[cfg(target_os = "linux")]
37 1128 : OpenOptions::TokioEpollUring(x) => {
38 1128 : let _ = x.read(read);
39 1128 : }
40 : }
41 48643 : self
42 48643 : }
43 :
44 37013 : pub fn write(&mut self, write: bool) -> &mut OpenOptions {
45 37013 : match self {
46 36138 : OpenOptions::StdFs(x) => {
47 36138 : let _ = x.write(write);
48 36138 : }
49 : #[cfg(target_os = "linux")]
50 875 : OpenOptions::TokioEpollUring(x) => {
51 875 : let _ = x.write(write);
52 875 : }
53 : }
54 37013 : self
55 37013 : }
56 :
57 101373 : pub fn create(&mut self, create: bool) -> &mut OpenOptions {
58 101373 : match self {
59 99274 : OpenOptions::StdFs(x) => {
60 99274 : let _ = x.create(create);
61 99274 : }
62 : #[cfg(target_os = "linux")]
63 2099 : OpenOptions::TokioEpollUring(x) => {
64 2099 : let _ = x.create(create);
65 2099 : }
66 : }
67 101373 : self
68 101373 : }
69 :
70 95136 : pub fn create_new(&mut self, create_new: bool) -> &mut OpenOptions {
71 95136 : match self {
72 93104 : OpenOptions::StdFs(x) => {
73 93104 : let _ = x.create_new(create_new);
74 93104 : }
75 : #[cfg(target_os = "linux")]
76 2032 : OpenOptions::TokioEpollUring(x) => {
77 2032 : let _ = x.create_new(create_new);
78 2032 : }
79 : }
80 95136 : self
81 95136 : }
82 :
83 95675 : pub fn truncate(&mut self, truncate: bool) -> &mut OpenOptions {
84 95675 : match self {
85 93846 : OpenOptions::StdFs(x) => {
86 93846 : let _ = x.truncate(truncate);
87 93846 : }
88 : #[cfg(target_os = "linux")]
89 1829 : OpenOptions::TokioEpollUring(x) => {
90 1829 : let _ = x.truncate(truncate);
91 1829 : }
92 : }
93 95675 : self
94 95675 : }
95 :
96 273737 : pub(in crate::virtual_file) async fn open(&self, path: &Path) -> std::io::Result<OwnedFd> {
97 273737 : match self {
98 180315 : OpenOptions::StdFs(x) => x.open(path).map(|file| file.into()),
99 : #[cfg(target_os = "linux")]
100 93422 : OpenOptions::TokioEpollUring(x) => {
101 93422 : let system = tokio_epoll_uring::thread_local_system().await;
102 93424 : 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 93422 : })
108 : }
109 : }
110 273737 : }
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 : }
|