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 18093 : fn default() -> Self {
15 18093 : match super::io_engine::get() {
16 0 : IoEngine::NotSet => panic!("io engine not set"),
17 9042 : IoEngine::StdFs => Self::StdFs(std::fs::OpenOptions::new()),
18 : #[cfg(target_os = "linux")]
19 : IoEngine::TokioEpollUring => {
20 9051 : Self::TokioEpollUring(tokio_epoll_uring::ops::open_at::OpenOptions::new())
21 : }
22 : }
23 18093 : }
24 : }
25 :
26 : impl OpenOptions {
27 18093 : pub fn new() -> OpenOptions {
28 18093 : Self::default()
29 18093 : }
30 :
31 12234 : pub fn read(&mut self, read: bool) -> &mut OpenOptions {
32 12234 : match self {
33 6117 : OpenOptions::StdFs(x) => {
34 6117 : let _ = x.read(read);
35 6117 : }
36 : #[cfg(target_os = "linux")]
37 6117 : OpenOptions::TokioEpollUring(x) => {
38 6117 : let _ = x.read(read);
39 6117 : }
40 : }
41 12234 : self
42 12234 : }
43 :
44 9705 : pub fn write(&mut self, write: bool) -> &mut OpenOptions {
45 9705 : match self {
46 4848 : OpenOptions::StdFs(x) => {
47 4848 : let _ = x.write(write);
48 4848 : }
49 : #[cfg(target_os = "linux")]
50 4857 : OpenOptions::TokioEpollUring(x) => {
51 4857 : let _ = x.write(write);
52 4857 : }
53 : }
54 9705 : self
55 9705 : }
56 :
57 25710 : pub fn create(&mut self, create: bool) -> &mut OpenOptions {
58 25710 : match self {
59 12846 : OpenOptions::StdFs(x) => {
60 12846 : let _ = x.create(create);
61 12846 : }
62 : #[cfg(target_os = "linux")]
63 12864 : OpenOptions::TokioEpollUring(x) => {
64 12864 : let _ = x.create(create);
65 12864 : }
66 : }
67 25710 : self
68 25710 : }
69 :
70 18945 : pub fn create_new(&mut self, create_new: bool) -> &mut OpenOptions {
71 18945 : match self {
72 9468 : OpenOptions::StdFs(x) => {
73 9468 : let _ = x.create_new(create_new);
74 9468 : }
75 : #[cfg(target_os = "linux")]
76 9477 : OpenOptions::TokioEpollUring(x) => {
77 9477 : let _ = x.create_new(create_new);
78 9477 : }
79 : }
80 18945 : self
81 18945 : }
82 :
83 21876 : pub fn truncate(&mut self, truncate: bool) -> &mut OpenOptions {
84 21876 : match self {
85 10929 : OpenOptions::StdFs(x) => {
86 10929 : let _ = x.truncate(truncate);
87 10929 : }
88 : #[cfg(target_os = "linux")]
89 10947 : OpenOptions::TokioEpollUring(x) => {
90 10947 : let _ = x.truncate(truncate);
91 10947 : }
92 : }
93 21876 : self
94 21876 : }
95 :
96 583787 : pub(in crate::virtual_file) async fn open(&self, path: &Path) -> std::io::Result<OwnedFd> {
97 583787 : match self {
98 295242 : OpenOptions::StdFs(x) => x.open(path).map(|file| file.into()),
99 : #[cfg(target_os = "linux")]
100 288545 : OpenOptions::TokioEpollUring(x) => {
101 288545 : let system = super::io_engine::tokio_epoll_uring_ext::thread_local_system().await;
102 288546 : 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 288545 : })
108 : }
109 : }
110 583787 : }
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 : }
|