Skip to main content

InodeMapper

Struct InodeMapper 

Source
pub struct InodeMapper<Data> { /* private fields */ }
Expand description

Assigns inode numbers and remembers which directory entries name each inode.

A normal insert creates one inode for one (parent, name) pair. After the backing filesystem creates a hard link, call Self::link to attach its second name to the same inode. Data remains the caller’s own data and is not used to detect links.

use easy_fuser::inode_mapping::InodeMapper;
use easy_fuser::types::ROOT_INODE;
use std::ffi::OsStr;

let mut mapper = InodeMapper::new(());
let inode = mapper.insert_child(&ROOT_INODE, "a.txt".into(), |_| ()).unwrap();
mapper.link(&inode, &ROOT_INODE, "b.txt".into()).unwrap();
assert_eq!(mapper.lookup(&ROOT_INODE, OsStr::new("b.txt")).unwrap().inode, &inode);
assert_eq!(mapper.resolve(&inode).unwrap().len(), 2);

Handlers using MappedInode get this behavior automatically when their link operation succeeds.

Implementations§

Source§

impl<Data: Send + Sync + 'static> InodeMapper<Data>

Source

pub fn new(data: Data) -> Self

Creates a new InodeMapper instance with the root inode initialized.

This function initializes the InodeMapper with an empty structure and sets up the root inode with the provided data. The root inode is represented by an empty path.

Source

pub fn get_root_inode(&self) -> Inode

Source

pub fn insert_child<F>( &mut self, parent: &Inode, child: OsString, value_creator: F, ) -> Result<Inode, InsertError>
where F: Fn(ValueCreatorParams<'_, Data>) -> Data,

Safely inserts a child inode into the InodeMapper.

This method checks if the parent exists before inserting the child. It uses a value_creator function to generate the data associated with the new inode.

§Behavior
  • Returns Err(InsertError::ParentNotFound) if the parent doesn’t exist.
  • If successful, returns Ok(Inode) with the newly created or existing child inode.

The value_creator function is called with the new inode, parent inode, child name, and existing data (if any) as arguments.

Source

pub fn insert_children<F>( &mut self, parent: &Inode, children: Vec<(OsString, F)>, ) -> Result<Vec<Inode>, InsertError>
where F: Fn(ValueCreatorParams<'_, Data>) -> Data,

Inserts multiple children into the InodeMapper for a given parent inode.

This method efficiently inserts multiple children at once, optimizing memory allocation for the parent’s children HashMap. It checks if the parent exists before insertion.

§Behavior
  • Returns Err(InsertError::ParentNotFound) if the parent doesn’t exist.
  • If successful, returns Ok(Vec<Inode>) containing the newly created or existing child inodes.
§Performance

Optimizes memory allocation by reserving space in the parent’s children HashMap based on the number of new children to be inserted.

Source

pub fn batch_insert<F, G>( &mut self, parent: &Inode, entries: Vec<(Vec<OsString>, F)>, default_parent_creator: G, ) -> Result<(), InsertError>
where F: Fn(ValueCreatorParams<'_, Data>) -> Data, G: Fn(ValueCreatorParams<'_, Data>) -> Data,

Batch inserts multiple entries into the InodeMapper, creating missing parent directories as needed.

This method efficiently handles the insertion of multiple entries, potentially with nested paths. It sorts entries by path length to ensure parent directories are created before their children.

§Behavior
  • Creates missing parent directories using the default_parent_creator function. (data field will always be null)
  • Inserts entries using the provided value_creator function.
  • Returns Err(InsertError::ParentNotFound) if the initial parent inode doesn’t exist.
§Note

Expects each entry’s path to include the entry name as the last element.

§Caveats

If the closures are not defined in same scope, ther emight be a compiler error concerning lifetimes (eg: implementation of Fn is not general enough) To resolve this problem, always fully qualify the argumentsof the closure (eg: |my_data: ValueCreatorParams<MyType>| {} and not |my_data| {})

Source

pub fn resolve_first(&self, inode: &Inode) -> Option<Vec<OsString>>

Resolves one remembered path to an inode for single-path handlers.

Stops after finding one path. The result is in leaf-to-root order for the legacy component resolver. An inode with no path returns an empty vector.

Source

pub fn get(&self, inode: &Inode) -> Option<InodeInfo<'_, Data>>

Source

pub fn resolve(&self, inode: &Inode) -> Option<Vec<Vec<OsString>>>

Returns every known path to an inode, with components in root-to-leaf order. An existing inode with no directory entries returns an empty vector.

Source

pub fn get_data_mut(&mut self, inode: &Inode) -> Option<&mut Data>

Returns mutable access to the data stored for an inode.

Source

pub fn get_children(&self, parent: &Inode) -> Vec<(&Arc<OsString>, &Inode)>

§Note
  • Does not check if the parent inode exists.
  • Returns an empty vector if the parent has no children or doesn’t exist.
Source

pub fn lookup( &self, parent: &Inode, name: &OsStr, ) -> Option<LookupResult<'_, Data>>

Looks up a child inode by its parent inode and name

Registers a new hard-link name for an existing inode. Call this only after the backing filesystem has successfully created the link.

Removes one name while keeping the inode alive for remaining links and outstanding FUSE references. Returns the inode that lost the name.

Source

pub fn rename( &mut self, parent: &Inode, oldname: &OsStr, newparent: &Inode, newname: OsString, ) -> Result<Option<(Inode, Data)>, RenameError>

Renames a child inode from one parent to another

Source

pub fn exchange( &mut self, parent: &Inode, name: &OsStr, newparent: &Inode, newname: &OsStr, ) -> Result<(), RenameError>

Swaps two existing directory entries after an exchange rename succeeds.

Source

pub fn remove(&mut self, inode: &Inode) -> Option<Data>

Removes an inode and its associated data from the InodeMapper.

This function removes the specified inode from both the inodes and children maps. It also cleans up empty parent entries in the children map.

Note: This operation will cascade to child inodes. If the removed inode has children, they will be removed from the data structure.

Behavior:

  • Panics if we intend to remove ROOT in debug build
  • If the inode doesn’t exist, the function does nothing.
  • If the parent’s children map becomes empty after removal, the parent entry is also removed from the children map to conserve memory.
Source§

impl<Data> InodeMapper<Data>
where Data: HasLookupCount + Send + Sync + 'static,

Source

pub fn prune(&mut self, keep: &HashSet<Vec<OsString>>)

Auto Trait Implementations§

§

impl<Data> Freeze for InodeMapper<Data>
where InodeData<Data>: Freeze,

§

impl<Data> RefUnwindSafe for InodeMapper<Data>
where InodeData<Data>: RefUnwindSafe,

§

impl<Data> Send for InodeMapper<Data>
where InodeData<Data>: Send,

§

impl<Data> Sync for InodeMapper<Data>
where InodeData<Data>: Sync,

§

impl<Data> Unpin for InodeMapper<Data>
where InodeData<Data>: Unpin,

§

impl<Data> UnsafeUnpin for InodeMapper<Data>
where InodeData<Data>: UnsafeUnpin,

§

impl<Data> UnwindSafe for InodeMapper<Data>
where InodeData<Data>: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.