1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
use std::{io::Result, os::unix::fs::OpenOptionsExt, path::Path};
use super::File;
use crate::runtime::syscall;
#[derive(Debug)]
pub struct OpenOptions {
read: bool,
write: bool,
append: bool,
truncate: bool,
create: bool,
create_new: bool,
mode: u32,
custom_flags: i32,
}
impl OpenOptions {
pub fn new() -> Self {
Self {
read: false,
write: false,
append: false,
truncate: false,
create: false,
create_new: false,
mode: 0o666,
custom_flags: 0,
}
}
pub fn read(&mut self, read: bool) -> &mut Self {
self.read = read;
self
}
pub fn write(&mut self, write: bool) -> &mut Self {
self.write = write;
self
}
pub fn append(&mut self, append: bool) -> &mut Self {
self.append = append;
self
}
pub fn truncate(&mut self, truncate: bool) -> &mut Self {
self.truncate = truncate;
self
}
pub fn create(&mut self, create: bool) -> &mut Self {
self.create = create;
self
}
pub fn create_new(&mut self, create_new: bool) -> &mut Self {
self.create_new = create_new;
self
}
pub async fn open<P: AsRef<Path>>(&self, path: P) -> Result<File> {
let path = path.as_ref();
syscall::open(path, self.flags(), self.mode)
.await
.map(File::from)
}
}
impl OpenOptions {
fn flags(&self) -> libc::c_int {
let mut flags = match (self.read, self.write, self.append) {
(true, _, true) => libc::O_RDWR | libc::O_APPEND,
(true, true, false) => libc::O_RDWR,
(true, false, false) => libc::O_RDONLY,
(false, _, true) => libc::O_WRONLY | libc::O_APPEND,
(false, true, false) => libc::O_WRONLY,
(false, false, false) => 0,
};
if self.create_new {
flags |= libc::O_CREAT | libc::O_EXCL;
} else {
if self.create {
flags |= libc::O_CREAT;
}
if self.truncate {
flags |= libc::O_TRUNC;
}
}
flags |= self.custom_flags & !libc::O_ACCMODE;
flags
}
}
impl Default for OpenOptions {
fn default() -> Self {
Self::new()
}
}
impl OpenOptionsExt for OpenOptions {
fn mode(&mut self, mode: u32) -> &mut Self {
self.mode = mode;
self
}
fn custom_flags(&mut self, flags: i32) -> &mut Self {
self.custom_flags = flags;
self
}
}