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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
use crate::protocol::node;
use crate::protocol::prelude::*;
use crate::protocol::read::fuse_read_in_v7p1;
#[cfg(test)]
mod readdir_test;
pub struct ReaddirRequest<'a> {
header: &'a fuse_kernel::fuse_in_header,
raw: fuse_kernel::fuse_read_in,
}
impl ReaddirRequest<'_> {
pub fn node_id(&self) -> u64 {
self.header.nodeid
}
pub fn handle(&self) -> u64 {
self.raw.fh
}
pub fn offset(&self) -> u64 {
self.raw.offset
}
pub fn size(&self) -> u32 {
self.raw.size
}
pub fn lock_owner(&self) -> Option<u64> {
if self.raw.read_flags & fuse_kernel::FUSE_READ_LOCKOWNER == 0 {
return None;
}
Some(self.raw.lock_owner)
}
pub fn flags(&self) -> u32 {
self.raw.flags
}
}
impl<'a> fuse_io::DecodeRequest<'a> for ReaddirRequest<'a> {
fn decode_request(
mut dec: fuse_io::RequestDecoder<'a>,
) -> io::Result<Self> {
let header = dec.header();
if header.opcode == fuse_kernel::FUSE_READDIRPLUS {
let raw: &'a fuse_kernel::fuse_read_in = dec.next_sized()?;
return Ok(Self { header, raw: *raw });
}
debug_assert!(header.opcode == fuse_kernel::FUSE_READDIR);
if dec.version().minor() < 9 {
let raw: &'a fuse_read_in_v7p1 = dec.next_sized()?;
return Ok(Self {
header,
raw: fuse_kernel::fuse_read_in {
fh: raw.fh,
offset: raw.offset,
size: raw.size,
read_flags: 0,
lock_owner: 0,
flags: 0,
padding: 0,
},
});
}
let raw: &'a fuse_kernel::fuse_read_in = dec.next_sized()?;
Ok(Self { header, raw: *raw })
}
}
#[allow(dead_code)]
const FUSE_NAME_MAX_CFG_LINUX: usize = 1024;
#[allow(dead_code)]
const FUSE_NAME_MAX_CFG_FREEBSD: usize = 255;
#[cfg(target_os = "linux")]
pub const FUSE_NAME_MAX: usize = FUSE_NAME_MAX_CFG_LINUX;
#[cfg(target_os = "freebsd")]
pub const FUSE_NAME_MAX: usize = FUSE_NAME_MAX_CFG_FREEBSD;
const MAX_RESPONSE_SIZE: usize = 4096;
pub struct ReaddirResponse<'a> {
phantom: PhantomData<&'a ()>,
opcode: fuse_kernel::Opcode,
response_size: usize,
max_response_size: usize,
buf: [u64; MAX_RESPONSE_SIZE / 8],
}
impl<'a> ReaddirResponse<'a> {
pub fn new(request: &ReaddirRequest) -> Self {
ReaddirResponse {
phantom: PhantomData,
opcode: request.header.opcode,
response_size: 0,
max_response_size: cmp::min(
request.raw.size,
MAX_RESPONSE_SIZE as u32,
) as usize,
buf: unsafe { mem::uninitialized() },
}
}
pub fn push(
&mut self,
node_id: node::NodeId,
offset: u64,
name: &CStr,
) -> Option<Dirent> {
let name = name.to_bytes();
let name_len = name.len();
assert!(name_len > 0);
let op_readdirplus = self.opcode == fuse_kernel::FUSE_READDIRPLUS;
let padding_len = (8 - (name_len % 8)) % 8;
let overhead = padding_len
+ (if op_readdirplus {
size_of::<fuse_kernel::fuse_direntplus>()
} else {
size_of::<fuse_kernel::fuse_dirent>()
});
let buf_p = match self.buf_alloc(overhead.saturating_add(name_len)) {
Some(p) => p,
None => return None,
};
let entry = unsafe {
let mut dirent_p: *mut u8 = buf_p;
let entry_out_p: *mut u8 = buf_p;
if op_readdirplus {
dirent_p =
dirent_p.add(size_of::<fuse_kernel::fuse_entry_out>());
ptr::write_bytes(
entry_out_p,
0,
size_of::<fuse_kernel::fuse_entry_out>(),
);
}
ptr::write_bytes(
dirent_p,
0,
size_of::<fuse_kernel::fuse_dirent>(),
);
let name_p: *mut u8 =
dirent_p.add(size_of::<fuse_kernel::fuse_dirent>());
let padding_p: *mut u8 = name_p.add(name_len);
let name_ref = std::slice::from_raw_parts_mut(name_p, name_len);
name_ref.copy_from_slice(name);
if padding_len > 0 {
ptr::write_bytes(padding_p, 0, padding_len);
}
Dirent {
node_id,
node: if dirent_p == entry_out_p {
None
} else {
Some(&mut *(entry_out_p as *mut node::Node))
},
dirent: &mut *(dirent_p as *mut fuse_kernel::fuse_dirent),
name: name_ref,
}
};
entry.dirent.ino = node_id.get();
entry.dirent.off = offset;
entry.dirent.namelen = name_len as u32;
entry.dirent.r#type = node::NodeKind::UNKNOWN.raw();
Some(entry)
}
fn buf_alloc(&mut self, size: usize) -> Option<*mut u8> {
debug_assert_eq!(size % 8, 0);
let new_response_size: usize;
match self.response_size.checked_add(size) {
None => return None,
Some(x) => new_response_size = x,
};
if new_response_size > self.max_response_size {
return None;
}
let buf_p = self.buf.as_ptr() as *mut u8;
let out = unsafe { buf_p.add(self.response_size) };
self.response_size = new_response_size;
Some(out)
}
}
impl<'a> fmt::Debug for ReaddirResponse<'a> {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
let dirents: Vec<Dirent<'a>> = Vec::new();
fmt.debug_struct("ReaddirResponse")
.field("dirents", &dirents)
.finish()
}
}
impl fuse_io::EncodeResponse for ReaddirResponse<'_> {
fn encode_response<'a, Chan: fuse_io::Channel>(
&'a self,
enc: fuse_io::ResponseEncoder<Chan>,
) -> std::io::Result<()> {
enc.encode_bytes(unsafe {
std::slice::from_raw_parts(
self.buf.as_ptr() as *const u8,
self.response_size,
)
})
}
}
pub struct Dirent<'a> {
node_id: node::NodeId,
node: Option<&'a mut node::Node>,
dirent: &'a mut fuse_kernel::fuse_dirent,
name: &'a [u8],
}
impl Dirent<'_> {
pub fn node_id(&self) -> node::NodeId {
match self.node {
None => self.node_id,
Some(ref node) => match node.id() {
None => self.node_id,
Some(id) => id,
},
}
}
pub fn set_node_id(&mut self, node_id: node::NodeId) {
self.node_id = node_id;
self.dirent.ino = node_id.get();
match self.node {
None => {},
Some(ref mut node) => match node.id() {
None => {},
Some(_) => node.set_id(node_id),
},
}
}
pub fn offset(&self) -> u64 {
self.dirent.off
}
pub fn set_offset(&mut self, offset: u64) {
self.dirent.off = offset;
}
pub fn node_kind(&self) -> node::NodeKind {
let dirent_type = node::NodeKind::new(self.dirent.r#type);
match self.node {
None => dirent_type,
Some(ref node) => match node.id() {
None => dirent_type,
Some(_) => node.kind(),
},
}
}
pub fn set_node_kind(&mut self, node_kind: node::NodeKind) {
self.dirent.r#type = node_kind.raw();
match self.node {
None => {},
Some(ref mut node) => match node.id() {
None => {},
Some(_) => node.set_kind(node_kind),
},
}
}
pub fn node_mut(&mut self) -> Option<&mut node::Node> {
match &mut self.node {
None => None,
Some(node) => {
if node.id() == None {
node.set_id(self.node_id);
node.set_kind(node::NodeKind::new(self.dirent.r#type));
}
Some(node)
},
}
}
}
impl Drop for Dirent<'_> {
fn drop(&mut self) {
match self.node {
None => {},
Some(ref node) => match node.id() {
None => {},
Some(node_id) => {
self.dirent.ino = node_id.get();
self.dirent.r#type = node.kind().raw();
},
},
}
}
}
impl fmt::Debug for Dirent<'_> {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
fmt.debug_struct("Dirent")
.field("node_id", &self.node_id())
.field("offset", &self.dirent.off)
.field("node_kind", &self.node_kind())
.field("node", &self.node)
.field("name", &self.name)
.finish()
}
}