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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
use crate::kernel::{fuse_attr, fuse_file_lock, fuse_forget_one, fuse_kstatfs};
use std::{convert::TryFrom, error, fmt};
#[derive(Debug, Default, Clone, Copy)]
#[repr(transparent)]
pub struct FileAttr(fuse_attr);
impl FileAttr {
pub fn ino(&self) -> u64 {
self.0.ino
}
pub fn set_ino(&mut self, ino: u64) {
self.0.ino = ino;
}
pub fn size(&self) -> u64 {
self.0.size
}
pub fn set_size(&mut self, size: u64) {
self.0.size = size;
}
pub fn mode(&self) -> u32 {
self.0.mode
}
pub fn set_mode(&mut self, mode: u32) {
self.0.mode = mode;
}
pub fn nlink(&self) -> u32 {
self.0.nlink
}
pub fn set_nlink(&mut self, nlink: u32) {
self.0.nlink = nlink
}
pub fn uid(&self) -> u32 {
self.0.uid
}
pub fn set_uid(&mut self, uid: u32) {
self.0.uid = uid;
}
pub fn gid(&self) -> u32 {
self.0.gid
}
pub fn set_gid(&mut self, gid: u32) {
self.0.gid = gid;
}
pub fn rdev(&self) -> u32 {
self.0.rdev
}
pub fn set_rdev(&mut self, rdev: u32) {
self.0.rdev = rdev;
}
pub fn blksize(&self) -> u32 {
self.0.blksize
}
pub fn set_blksize(&mut self, blksize: u32) {
self.0.blksize = blksize;
}
pub fn blocks(&self) -> u64 {
self.0.blocks
}
pub fn set_blocks(&mut self, blocks: u64) {
self.0.blocks = blocks;
}
pub fn atime(&self) -> (u64, u32) {
(self.0.atime, self.0.atimensec)
}
pub fn set_atime(&mut self, sec: u64, nsec: u32) {
self.0.atime = sec;
self.0.atimensec = nsec;
}
pub fn mtime(&self) -> (u64, u32) {
(self.0.mtime, self.0.mtimensec)
}
pub fn set_mtime(&mut self, sec: u64, nsec: u32) {
self.0.mtime = sec;
self.0.mtimensec = nsec;
}
pub fn ctime(&self) -> (u64, u32) {
(self.0.ctime, self.0.ctimensec)
}
pub fn set_ctime(&mut self, sec: u64, nsec: u32) {
self.0.ctime = sec;
self.0.ctimensec = nsec;
}
}
impl TryFrom<libc::stat> for FileAttr {
type Error = std::num::TryFromIntError;
fn try_from(attr: libc::stat) -> Result<Self, Self::Error> {
Ok(Self(fuse_attr {
ino: u64::try_from(attr.st_ino).map_err(Self::Error::from)?,
size: u64::try_from(attr.st_size).map_err(Self::Error::from)?,
blocks: u64::try_from(attr.st_blocks).map_err(Self::Error::from)?,
atime: u64::try_from(attr.st_atime).map_err(Self::Error::from)?,
mtime: u64::try_from(attr.st_mtime).map_err(Self::Error::from)?,
ctime: u64::try_from(attr.st_ctime).map_err(Self::Error::from)?,
atimensec: u32::try_from(attr.st_atime_nsec).map_err(Self::Error::from)?,
mtimensec: u32::try_from(attr.st_mtime_nsec).map_err(Self::Error::from)?,
ctimensec: u32::try_from(attr.st_ctime_nsec).map_err(Self::Error::from)?,
mode: u32::try_from(attr.st_mode).map_err(Self::Error::from)?,
nlink: u32::try_from(attr.st_nlink).map_err(Self::Error::from)?,
uid: u32::try_from(attr.st_uid).map_err(Self::Error::from)?,
gid: u32::try_from(attr.st_gid).map_err(Self::Error::from)?,
rdev: u32::try_from(attr.st_gid).map_err(Self::Error::from)?,
blksize: u32::try_from(attr.st_blksize).map_err(Self::Error::from)?,
padding: 0,
}))
}
}
impl FileAttr {
pub(crate) fn into_inner(self) -> fuse_attr {
self.0
}
}
#[derive(Debug, Copy, Clone, Default)]
#[repr(transparent)]
pub struct FileLock(fuse_file_lock);
impl FileLock {
pub(crate) fn new(attr: &fuse_file_lock) -> &Self {
unsafe { &*(attr as *const fuse_file_lock as *const Self) }
}
pub fn typ(&self) -> u32 {
self.0.typ
}
pub fn set_typ(&mut self, typ: u32) {
self.0.typ = typ;
}
pub fn start(&self) -> u64 {
self.0.start
}
pub fn set_start(&mut self, start: u64) {
self.0.start = start;
}
pub fn end(&self) -> u64 {
self.0.end
}
pub fn set_end(&mut self, end: u64) {
self.0.end = end;
}
pub fn pid(&self) -> u32 {
self.0.pid
}
pub fn set_pid(&mut self, pid: u32) {
self.0.pid = pid;
}
pub(crate) fn into_inner(self) -> fuse_file_lock {
self.0
}
}
impl TryFrom<libc::flock> for FileLock {
type Error = InvalidFileLock;
#[allow(clippy::cast_sign_loss)]
fn try_from(lk: libc::flock) -> Result<Self, Self::Error> {
const F_RDLCK: u32 = libc::F_RDLCK as u32;
const F_WRLCK: u32 = libc::F_WRLCK as u32;
const F_UNLCK: u32 = libc::F_UNLCK as u32;
let lock_type = lk.l_type as u32;
let (start, end) = match lock_type {
F_UNLCK => (0, 0),
F_RDLCK | F_WRLCK => {
let start = lk.l_start as u64;
let end = if lk.l_len == 0 {
std::u64::MAX
} else {
start + (lk.l_len as u64) - 1
};
(start, end)
}
_ => return Err(InvalidFileLock(())),
};
Ok(Self(fuse_file_lock {
start,
end,
typ: lock_type,
pid: lk.l_pid as u32,
}))
}
}
#[doc(hidden)]
#[derive(Debug)]
pub struct InvalidFileLock(());
impl From<std::convert::Infallible> for InvalidFileLock {
fn from(infallible: std::convert::Infallible) -> Self {
match infallible {}
}
}
impl From<std::num::TryFromIntError> for InvalidFileLock {
fn from(_: std::num::TryFromIntError) -> Self {
Self(())
}
}
impl fmt::Display for InvalidFileLock {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "invalid file lock")
}
}
impl error::Error for InvalidFileLock {}
#[derive(Debug, Clone, Copy, Default)]
#[repr(transparent)]
pub struct StatFs(fuse_kstatfs);
impl StatFs {
pub fn bsize(&self) -> u32 {
self.0.bsize
}
pub fn set_bsize(&mut self, bsize: u32) {
self.0.bsize = bsize;
}
pub fn frsize(&self) -> u32 {
self.0.frsize
}
pub fn set_frsize(&mut self, frsize: u32) {
self.0.frsize = frsize;
}
pub fn blocks(&self) -> u64 {
self.0.blocks
}
pub fn set_blocks(&mut self, blocks: u64) {
self.0.blocks = blocks;
}
pub fn bfree(&self) -> u64 {
self.0.bfree
}
pub fn set_bfree(&mut self, bfree: u64) {
self.0.bfree = bfree;
}
pub fn bavail(&self) -> u64 {
self.0.bavail
}
pub fn set_bavail(&mut self, bavail: u64) {
self.0.bavail = bavail;
}
pub fn files(&self) -> u64 {
self.0.files
}
pub fn set_files(&mut self, files: u64) {
self.0.files = files;
}
pub fn ffree(&self) -> u64 {
self.0.ffree
}
pub fn set_ffree(&mut self, ffree: u64) {
self.0.ffree = ffree;
}
pub fn namelen(&self) -> u32 {
self.0.namelen
}
pub fn set_namelen(&mut self, namelen: u32) {
self.0.namelen = namelen;
}
pub(crate) fn into_inner(self) -> fuse_kstatfs {
self.0
}
}
impl TryFrom<libc::statvfs> for StatFs {
type Error = std::num::TryFromIntError;
fn try_from(st: libc::statvfs) -> Result<Self, Self::Error> {
Ok(Self(fuse_kstatfs {
bsize: u32::try_from(st.f_bsize).map_err(Self::Error::from)?,
frsize: u32::try_from(st.f_frsize).map_err(Self::Error::from)?,
blocks: u64::try_from(st.f_blocks).map_err(Self::Error::from)?,
bfree: u64::try_from(st.f_bfree).map_err(Self::Error::from)?,
bavail: u64::try_from(st.f_bavail).map_err(Self::Error::from)?,
files: u64::try_from(st.f_files).map_err(Self::Error::from)?,
ffree: u64::try_from(st.f_ffree).map_err(Self::Error::from)?,
namelen: u32::try_from(st.f_namemax).map_err(Self::Error::from)?,
padding: 0,
spare: [0u32; 6],
}))
}
}
#[derive(Debug)]
#[repr(transparent)]
pub struct Forget(fuse_forget_one);
impl Forget {
pub(crate) const fn new(ino: u64, nlookup: u64) -> Self {
Self(fuse_forget_one {
nodeid: ino,
nlookup,
})
}
pub fn ino(&self) -> u64 {
self.0.nodeid
}
pub fn nlookup(&self) -> u64 {
self.0.nlookup
}
}