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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
use crate::internal::types::ProtocolVersion;
use crate::protocol::prelude::*;
#[cfg(test)]
mod cuse_init_test;
pub struct CuseInitRequest<'a> {
phantom: PhantomData<&'a ()>,
version: ProtocolVersion,
flags: CuseInitFlags,
}
impl CuseInitRequest<'_> {
pub fn version(&self) -> ProtocolVersion {
self.version
}
pub fn flags(&self) -> &CuseInitFlags {
&self.flags
}
pub fn flags_mut(&mut self) -> &mut CuseInitFlags {
&mut self.flags
}
}
impl fmt::Debug for CuseInitRequest<'_> {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
fmt.debug_struct("CuseInitRequest")
.field("version", &self.version)
.field("flags", &self.flags)
.finish()
}
}
impl<'a> fuse_io::DecodeRequest<'a> for CuseInitRequest<'_> {
fn decode_request(
mut dec: fuse_io::RequestDecoder<'a>,
) -> Result<Self, Error> {
debug_assert!(dec.header().opcode == fuse_kernel::CUSE_INIT);
let raw: &'a fuse_kernel::cuse_init_in = dec.next_sized()?;
Ok(CuseInitRequest {
phantom: PhantomData,
version: ProtocolVersion::new(raw.major, raw.minor),
flags: CuseInitFlags::from_bits(raw.flags),
})
}
}
pub struct CuseInitResponse {
raw: fuse_kernel::cuse_init_out,
flags: CuseInitFlags,
}
impl CuseInitResponse {
pub fn new() -> CuseInitResponse {
Self {
raw: fuse_kernel::cuse_init_out {
major: 0,
minor: 0,
unused: 0,
flags: 0,
max_read: 0,
max_write: 0,
dev_major: 0,
dev_minor: 0,
spare: [0; 10],
},
flags: CuseInitFlags::new(),
}
}
pub(crate) fn version(&self) -> ProtocolVersion {
ProtocolVersion::new(self.raw.major, self.raw.minor)
}
pub(crate) fn set_version(&mut self, v: ProtocolVersion) {
self.raw.major = v.major();
self.raw.minor = v.minor();
}
pub fn flags(&self) -> &CuseInitFlags {
&self.flags
}
pub fn flags_mut(&mut self) -> &mut CuseInitFlags {
&mut self.flags
}
pub fn max_read(&self) -> u32 {
self.raw.max_read
}
pub fn set_max_read(&mut self, max_read: u32) {
self.raw.max_read = max_read;
}
pub fn max_write(&self) -> u32 {
self.raw.max_write
}
pub fn set_max_write(&mut self, max_write: u32) {
self.raw.max_write = max_write;
}
pub fn dev_major(&self) -> u32 {
self.raw.dev_major
}
pub fn set_dev_major(&mut self, dev_major: u32) {
self.raw.dev_major = dev_major;
}
pub fn dev_minor(&self) -> u32 {
self.raw.dev_minor
}
pub fn set_dev_minor(&mut self, dev_minor: u32) {
self.raw.dev_minor = dev_minor;
}
}
impl fmt::Debug for CuseInitResponse {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
fmt.debug_struct("CuseInitResponse")
.field("flags", self.flags())
.field("max_read", &self.max_read())
.field("max_write", &self.max_write())
.field("dev_major", &self.dev_major())
.field("dev_minor", &self.dev_minor())
.finish()
}
}
impl CuseInitResponse {
pub(crate) fn encode_response<'a, Chan: fuse_io::Channel>(
&'a self,
enc: fuse_io::ResponseEncoder<Chan>,
maybe_device_name: Option<&[u8]>,
) -> Result<(), Chan::Error> {
let mut out = self.raw;
out.flags = self.flags.to_bits();
let out_buf: &[u8] = unsafe {
slice::from_raw_parts(
(&out as *const fuse_kernel::cuse_init_out) as *const u8,
size_of::<fuse_kernel::cuse_init_out>(),
)
};
match maybe_device_name {
None => enc.encode_bytes(out_buf),
Some(device_name) => {
enc.encode_bytes_4(out_buf, b"DEVNAME=", device_name, b"\x00")
},
}
}
}
bitflags_struct! {
pub struct CuseInitFlags(u32);
fuse_kernel::CUSE_UNRESTRICTED_IOCTL: unrestricted_ioctl,
}