tokio/fs/open_options/
uring_open_options.rs1use std::{io, os::unix::fs::OpenOptionsExt};
2
3#[cfg(test)]
4use super::mock_open_options::MockOpenOptions as StdOpenOptions;
5#[cfg(not(test))]
6use std::fs::OpenOptions as StdOpenOptions;
7
8#[derive(Debug, Clone)]
9pub(crate) struct UringOpenOptions {
10 pub(crate) read: bool,
11 pub(crate) write: bool,
12 pub(crate) append: bool,
13 pub(crate) truncate: bool,
14 pub(crate) create: bool,
15 pub(crate) create_new: bool,
16 pub(crate) mode: libc::mode_t,
17 pub(crate) custom_flags: libc::c_int,
18}
19
20impl UringOpenOptions {
21 pub(crate) fn new() -> Self {
22 Self {
23 read: false,
24 write: false,
25 append: false,
26 truncate: false,
27 create: false,
28 create_new: false,
29 mode: 0o666,
30 custom_flags: 0,
31 }
32 }
33
34 pub(crate) fn append(&mut self, append: bool) -> &mut Self {
35 self.append = append;
36 self
37 }
38
39 pub(crate) fn create(&mut self, create: bool) -> &mut Self {
40 self.create = create;
41 self
42 }
43
44 pub(crate) fn create_new(&mut self, create_new: bool) -> &mut Self {
45 self.create_new = create_new;
46 self
47 }
48
49 pub(crate) fn read(&mut self, read: bool) -> &mut Self {
50 self.read = read;
51 self
52 }
53
54 pub(crate) fn write(&mut self, write: bool) -> &mut Self {
55 self.write = write;
56 self
57 }
58
59 pub(crate) fn truncate(&mut self, truncate: bool) -> &mut Self {
60 self.truncate = truncate;
61 self
62 }
63
64 pub(crate) fn mode(&mut self, mode: u32) -> &mut Self {
65 self.mode = mode as libc::mode_t;
66 self
67 }
68
69 pub(crate) fn custom_flags(&mut self, flags: i32) -> &mut Self {
70 self.custom_flags = flags;
71 self
72 }
73
74 pub(crate) fn access_mode(&self) -> io::Result<libc::c_int> {
76 match (self.read, self.write, self.append) {
77 (true, false, false) => Ok(libc::O_RDONLY),
78 (false, true, false) => Ok(libc::O_WRONLY),
79 (true, true, false) => Ok(libc::O_RDWR),
80 (false, _, true) => Ok(libc::O_WRONLY | libc::O_APPEND),
81 (true, _, true) => Ok(libc::O_RDWR | libc::O_APPEND),
82 (false, false, false) => Err(io::Error::from_raw_os_error(libc::EINVAL)),
83 }
84 }
85
86 pub(crate) fn creation_mode(&self) -> io::Result<libc::c_int> {
88 match (self.write, self.append) {
89 (true, false) => {}
90 (false, false) => {
91 if self.truncate || self.create || self.create_new {
92 return Err(io::Error::from_raw_os_error(libc::EINVAL));
93 }
94 }
95 (_, true) => {
96 if self.truncate && !self.create_new {
97 return Err(io::Error::from_raw_os_error(libc::EINVAL));
98 }
99 }
100 }
101
102 Ok(match (self.create, self.truncate, self.create_new) {
103 (false, false, false) => 0,
104 (true, false, false) => libc::O_CREAT,
105 (false, true, false) => libc::O_TRUNC,
106 (true, true, false) => libc::O_CREAT | libc::O_TRUNC,
107 (_, _, true) => libc::O_CREAT | libc::O_EXCL,
108 })
109 }
110}
111
112impl From<UringOpenOptions> for StdOpenOptions {
113 fn from(value: UringOpenOptions) -> Self {
114 let mut std = StdOpenOptions::new();
115
116 std.append(value.append);
117 std.create(value.create);
118 std.create_new(value.create_new);
119 std.read(value.read);
120 std.truncate(value.truncate);
121 std.write(value.write);
122
123 std.mode(value.mode);
124 std.custom_flags(value.custom_flags);
125
126 std
127 }
128}