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
use std::fmt;
use std::num::NonZeroU64;
use std::time::Duration;
use crate::internal::fuse_kernel;
#[repr(C)]
#[derive(Eq, PartialEq, Clone, Copy)]
pub struct NodeId(NonZeroU64);
impl NodeId {
pub const ROOT: NodeId =
NodeId(unsafe { NonZeroU64::new_unchecked(fuse_kernel::FUSE_ROOT_ID) });
pub fn new(id: u64) -> Option<NodeId> {
NonZeroU64::new(id).map(|bits| NodeId(bits))
}
pub fn get(&self) -> u64 {
self.0.get()
}
}
impl fmt::Debug for NodeId {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
self.0.fmt(fmt)
}
}
impl fmt::Display for NodeId {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
self.0.fmt(fmt)
}
}
impl fmt::Binary for NodeId {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
self.0.fmt(fmt)
}
}
impl fmt::LowerHex for NodeId {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
self.0.fmt(fmt)
}
}
impl fmt::UpperHex for NodeId {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
self.0.fmt(fmt)
}
}
#[derive(Eq, PartialEq, Clone, Copy)]
pub struct NodeKind(u32);
const DT_UNKNOWN: u32 = 0;
const DT_FIFO: u32 = 1;
const DT_CHR: u32 = 2;
const DT_DIR: u32 = 4;
const DT_BLK: u32 = 6;
const DT_REG: u32 = 8;
const DT_LNK: u32 = 10;
const DT_SOCK: u32 = 12;
const DT_WHT: u32 = 14;
impl NodeKind {
pub const UNKNOWN: NodeKind = NodeKind(DT_UNKNOWN);
pub const FIFO: NodeKind = NodeKind(DT_FIFO);
pub const CHR: NodeKind = NodeKind(DT_CHR);
pub const DIR: NodeKind = NodeKind(DT_DIR);
pub const BLK: NodeKind = NodeKind(DT_BLK);
pub const REG: NodeKind = NodeKind(DT_REG);
pub const LNK: NodeKind = NodeKind(DT_LNK);
pub const SOCK: NodeKind = NodeKind(DT_SOCK);
pub const WHT: NodeKind = NodeKind(DT_WHT);
pub(crate) fn new(kind: u32) -> Self {
Self(kind)
}
pub(crate) fn raw(&self) -> u32 {
self.0
}
}
impl fmt::Debug for NodeKind {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
fmt::Display::fmt(self, fmt)
}
}
impl fmt::Display for NodeKind {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
match *self {
NodeKind::UNKNOWN => fmt.write_str("UNKNOWN"),
NodeKind::FIFO => fmt.write_str("FIFO"),
NodeKind::CHR => fmt.write_str("CHR"),
NodeKind::DIR => fmt.write_str("DIR"),
NodeKind::BLK => fmt.write_str("BLK"),
NodeKind::REG => fmt.write_str("REG"),
NodeKind::LNK => fmt.write_str("LNK"),
NodeKind::SOCK => fmt.write_str("SOCK"),
NodeKind::WHT => fmt.write_str("WHT"),
_ => write!(fmt, "{:#010X}", self.0),
}
}
}
impl fmt::Binary for NodeKind {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
self.0.fmt(fmt)
}
}
impl fmt::LowerHex for NodeKind {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
self.0.fmt(fmt)
}
}
impl fmt::UpperHex for NodeKind {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
self.0.fmt(fmt)
}
}
pub struct NodeAttr(fuse_kernel::fuse_attr);
impl NodeAttr {
pub fn size(&self) -> u64 {
self.0.size
}
pub fn set_size(&mut self, size: u64) {
self.0.size = size;
}
pub fn blocks(&self) -> u64 {
self.0.blocks
}
pub fn set_blocks(&mut self, blocks: u64) {
self.0.blocks = blocks;
}
pub fn atime(&self) -> Duration {
Duration::new(self.0.atime, self.0.atimensec)
}
pub fn set_atime(&mut self, atime: Duration) {
self.0.atime = atime.as_secs();
self.0.atimensec = atime.subsec_nanos();
}
pub fn mtime(&self) -> Duration {
Duration::new(self.0.mtime, self.0.mtimensec)
}
pub fn set_mtime(&mut self, mtime: Duration) {
self.0.mtime = mtime.as_secs();
self.0.mtimensec = mtime.subsec_nanos();
}
pub fn ctime(&self) -> Duration {
Duration::new(self.0.ctime, self.0.ctimensec)
}
pub fn set_ctime(&mut self, ctime: Duration) {
self.0.ctime = ctime.as_secs();
self.0.ctimensec = ctime.subsec_nanos();
}
pub fn set_mode(&mut self, mode: u32) {
self.0.mode = mode;
}
pub fn set_nlink(&mut self, nlink: u32) {
self.0.nlink = nlink;
}
pub fn set_user_id(&mut self, user_id: u32) {
self.0.uid = user_id;
}
pub fn set_group_id(&mut self, group_id: u32) {
self.0.gid = group_id;
}
pub fn set_rdev(&mut self, rdev: u32) {
self.0.rdev = rdev;
}
pub fn set_blksize(&mut self, blksize: u32) {
self.0.blksize = blksize;
}
pub(crate) fn new_ref(raw: &fuse_kernel::fuse_attr) -> &Self {
let p = raw as *const fuse_kernel::fuse_attr as *const Self;
unsafe { &*p }
}
pub(crate) fn new_ref_mut(raw: &mut fuse_kernel::fuse_attr) -> &mut Self {
let p = raw as *mut fuse_kernel::fuse_attr as *mut Self;
unsafe { &mut *p }
}
}
impl fmt::Debug for NodeAttr {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
fmt.debug_struct("NodeAttr")
.field("size", &self.0.size)
.field("blocks", &self.0.blocks)
.field("atime", &self.atime())
.field("mtime", &self.mtime())
.field("ctime", &self.ctime())
.field("mode", &self.0.mode)
.field("nlink", &self.0.nlink)
.field("uid", &self.0.uid)
.field("gid", &self.0.gid)
.field("rdev", &self.0.rdev)
.field("blksize", &self.0.blksize)
.finish()
}
}
pub struct Node(fuse_kernel::fuse_entry_out);
impl Node {
pub(crate) fn new_ref(raw: &fuse_kernel::fuse_entry_out) -> &Node {
unsafe { &*(raw as *const fuse_kernel::fuse_entry_out as *const Node) }
}
pub(crate) fn new_ref_mut(
raw: &mut fuse_kernel::fuse_entry_out,
) -> &mut Node {
unsafe { &mut *(raw as *mut fuse_kernel::fuse_entry_out as *mut Node) }
}
pub fn id(&self) -> Option<NodeId> {
NodeId::new(self.0.nodeid)
}
pub fn set_id(&mut self, id: NodeId) {
self.0.nodeid = id.0.get();
self.0.attr.ino = id.0.get();
}
pub fn generation(&self) -> u64 {
self.0.generation
}
pub fn set_generation(&mut self, generation: u64) {
self.0.generation = generation;
}
pub fn kind(&self) -> NodeKind {
let mode = self.0.attr.mode;
NodeKind((mode >> 12) & 0xF)
}
pub fn set_kind(&mut self, kind: NodeKind) {
let old_mode = self.0.attr.mode;
let new_mode = (kind.0 & 0xF) << 12 | (old_mode & 0xFFF);
self.0.attr.mode = new_mode;
}
pub fn cache_timeout(&self) -> Duration {
Duration::new(self.0.entry_valid, self.0.entry_valid_nsec)
}
pub fn set_cache_timeout(&mut self, cache_timeout: Duration) {
self.0.entry_valid = cache_timeout.as_secs();
self.0.entry_valid_nsec = cache_timeout.subsec_nanos();
}
pub fn attr(&self) -> &NodeAttr {
NodeAttr::new_ref(&self.0.attr)
}
pub fn attr_mut(&mut self) -> &mut NodeAttr {
NodeAttr::new_ref_mut(&mut self.0.attr)
}
pub fn attr_cache_timeout(&self) -> Duration {
Duration::new(self.0.attr_valid, self.0.attr_valid_nsec)
}
pub fn set_attr_cache_timeout(&mut self, d: Duration) {
self.0.attr_valid = d.as_secs();
self.0.attr_valid_nsec = d.subsec_nanos();
}
}
impl fmt::Debug for Node {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
fmt.debug_struct("Node")
.field("id", &self.0.nodeid)
.finish()
}
}