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};

/// Attributes about a file.
///
/// This type is ABI-compatible with `fuse_attr`.
#[derive(Debug, Default, Clone, Copy)]
#[repr(transparent)]
pub struct FileAttr(fuse_attr);

impl FileAttr {
    /// Return the inode number.
    pub fn ino(&self) -> u64 {
        self.0.ino
    }

    /// Set the inode number.
    pub fn set_ino(&mut self, ino: u64) {
        self.0.ino = ino;
    }

    /// Return the size of content.
    pub fn size(&self) -> u64 {
        self.0.size
    }

    /// Set the size of content.
    pub fn set_size(&mut self, size: u64) {
        self.0.size = size;
    }

    /// Return the permission of the inode.
    pub fn mode(&self) -> u32 {
        self.0.mode
    }

    /// Set the permission of the inode.
    pub fn set_mode(&mut self, mode: u32) {
        self.0.mode = mode;
    }

    /// Return the number of hard links.
    pub fn nlink(&self) -> u32 {
        self.0.nlink
    }

    /// Set the number of hard links.
    pub fn set_nlink(&mut self, nlink: u32) {
        self.0.nlink = nlink
    }

    /// Return the user ID.
    pub fn uid(&self) -> u32 {
        self.0.uid
    }

    /// Set the user ID.
    pub fn set_uid(&mut self, uid: u32) {
        self.0.uid = uid;
    }

    /// Return the group ID.
    pub fn gid(&self) -> u32 {
        self.0.gid
    }

    /// Set the group ID.
    pub fn set_gid(&mut self, gid: u32) {
        self.0.gid = gid;
    }

    /// Return the device ID.
    pub fn rdev(&self) -> u32 {
        self.0.rdev
    }

    /// Set the device ID.
    pub fn set_rdev(&mut self, rdev: u32) {
        self.0.rdev = rdev;
    }

    /// Return the block size.
    pub fn blksize(&self) -> u32 {
        self.0.blksize
    }

    /// Set the block size.
    pub fn set_blksize(&mut self, blksize: u32) {
        self.0.blksize = blksize;
    }

    /// Return the number of allocated blocks.
    pub fn blocks(&self) -> u64 {
        self.0.blocks
    }

    /// Set the number of allocated blocks.
    pub fn set_blocks(&mut self, blocks: u64) {
        self.0.blocks = blocks;
    }

    /// Return the last accessed time.
    pub fn atime(&self) -> (u64, u32) {
        (self.0.atime, self.0.atimensec)
    }

    /// Set the last accessed time.
    pub fn set_atime(&mut self, sec: u64, nsec: u32) {
        self.0.atime = sec;
        self.0.atimensec = nsec;
    }

    /// Return the last modification time.
    pub fn mtime(&self) -> (u64, u32) {
        (self.0.mtime, self.0.mtimensec)
    }

    /// Set the last modification time.
    pub fn set_mtime(&mut self, sec: u64, nsec: u32) {
        self.0.mtime = sec;
        self.0.mtimensec = nsec;
    }

    /// Return the last created time.
    pub fn ctime(&self) -> (u64, u32) {
        (self.0.ctime, self.0.ctimensec)
    }

    /// Set the last created time.
    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
    }
}

/// File lock information.
///
/// This type is ABI-compatible with `fuse_file_lock`.
#[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) }
    }

    /// Return the type of lock.
    pub fn typ(&self) -> u32 {
        self.0.typ
    }

    /// Set the type of lock.
    pub fn set_typ(&mut self, typ: u32) {
        self.0.typ = typ;
    }

    /// Return the starting offset for lock.
    pub fn start(&self) -> u64 {
        self.0.start
    }

    /// Set the starting offset for lock.
    pub fn set_start(&mut self, start: u64) {
        self.0.start = start;
    }

    /// Return the ending offset for lock.
    pub fn end(&self) -> u64 {
        self.0.end
    }

    /// Set the ending offset for lock.
    pub fn set_end(&mut self, end: u64) {
        self.0.end = end;
    }

    /// Return the process ID blocking the lock.
    pub fn pid(&self) -> u32 {
        self.0.pid
    }

    /// Set the process ID blocking the lock.
    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 {}

/// Filesystem statistics.
///
/// This type is ABI-compatible with `fuse_kstatfs`.
#[derive(Debug, Clone, Copy, Default)]
#[repr(transparent)]
pub struct StatFs(fuse_kstatfs);

impl StatFs {
    /// Return the block size.
    pub fn bsize(&self) -> u32 {
        self.0.bsize
    }

    /// Set the block size.
    pub fn set_bsize(&mut self, bsize: u32) {
        self.0.bsize = bsize;
    }

    /// Return the fragment size.
    pub fn frsize(&self) -> u32 {
        self.0.frsize
    }

    /// Set the fragment size.
    pub fn set_frsize(&mut self, frsize: u32) {
        self.0.frsize = frsize;
    }

    /// Return the number of blocks in the filesystem.
    pub fn blocks(&self) -> u64 {
        self.0.blocks
    }

    /// Return the number of blocks in the filesystem.
    pub fn set_blocks(&mut self, blocks: u64) {
        self.0.blocks = blocks;
    }

    /// Return the number of free blocks.
    pub fn bfree(&self) -> u64 {
        self.0.bfree
    }

    /// Return the number of free blocks.
    pub fn set_bfree(&mut self, bfree: u64) {
        self.0.bfree = bfree;
    }

    /// Return the number of free blocks for non-priviledge users.
    pub fn bavail(&self) -> u64 {
        self.0.bavail
    }

    /// Set the number of free blocks for non-priviledge users.
    pub fn set_bavail(&mut self, bavail: u64) {
        self.0.bavail = bavail;
    }

    /// Return the number of inodes.
    pub fn files(&self) -> u64 {
        self.0.files
    }

    /// Set the number of inodes.
    pub fn set_files(&mut self, files: u64) {
        self.0.files = files;
    }

    /// Return the number of free inodes.
    pub fn ffree(&self) -> u64 {
        self.0.ffree
    }

    /// Set the number of free inodes.
    pub fn set_ffree(&mut self, ffree: u64) {
        self.0.ffree = ffree;
    }

    /// Return the maximum length of file names.
    pub fn namelen(&self) -> u32 {
        self.0.namelen
    }

    /// Return the maximum length of file names.
    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],
        }))
    }
}

/// A forget information.
///
/// This type is ABI-compatible with `fuse_forget_one`.
#[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,
        })
    }

    /// Return the inode number of the target inode.
    pub fn ino(&self) -> u64 {
        self.0.nodeid
    }

    /// Return the released lookup count of the target inode.
    pub fn nlookup(&self) -> u64 {
        self.0.nlookup
    }
}