Skip to main content

easy_fuser/
session.rs

1use crate::core::FileIdResolver;
2use crate::types::FileIdType;
3use fuser::BackgroundSession;
4use std::collections::HashSet;
5use std::sync::Arc;
6
7/// A session for a mounted FUSE filesystem running in the background.
8///
9/// This struct wraps the `fuser::BackgroundSession` and provides access to the
10/// underlying inode resolver, allowing for manual maintenance tasks like pruning.
11pub struct FuseSession<T: FileIdType> {
12    pub(crate) session: BackgroundSession,
13    pub(crate) resolver: Arc<T::Resolver>,
14}
15
16impl<T: FileIdType> FuseSession<T> {
17    pub(crate) fn new(session: BackgroundSession, resolver: Arc<T::Resolver>) -> Self {
18        Self { session, resolver }
19    }
20
21    /// Creates a lightweight pruner handle that can be sent to other threads.
22    pub fn pruner(&self) -> FusePruner<T> {
23        FusePruner {
24            resolver: self.resolver.clone(),
25        }
26    }
27
28    /// Prune the resolver by removing unreferenced inodes.
29    ///
30    /// The `keep` set contains file IDs that should be preserved even if their lookup count is zero.
31    /// This is useful for cleaning up inodes that are no longer referenced by the kernel but
32    /// were not automatically evicted (e.g., to prevent race conditions).
33    pub fn prune(&self, keep: &HashSet<T>) {
34        self.resolver.prune(keep);
35    }
36
37    /// Join the background session, waiting for the filesystem to unmount.
38    ///
39    /// This method blocks until the filesystem is unmounted.
40    pub fn join(self) {
41        self.session.join()
42    }
43}
44
45/// A lightweight handle for pruning inodes.
46///
47/// This struct can be cloned and sent to other threads to perform background pruning
48/// without holding the main `FuseSession`.
49#[derive(Clone)]
50pub struct FusePruner<T: FileIdType> {
51    resolver: Arc<T::Resolver>,
52}
53
54impl<T: FileIdType> FusePruner<T> {
55    /// Prune the resolver by removing unreferenced inodes.
56    pub fn prune(&self, keep: &HashSet<T>) {
57        self.resolver.prune(keep);
58    }
59}