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>
impl<Data: Send + Sync + 'static> InodeMapper<Data>
Sourcepub fn new(data: Data) -> Self
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.
pub fn get_root_inode(&self) -> Inode
Sourcepub fn insert_child<F>(
&mut self,
parent: &Inode,
child: OsString,
value_creator: F,
) -> Result<Inode, InsertError>where
F: Fn(ValueCreatorParams<'_, Data>) -> Data,
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.
Sourcepub fn insert_children<F>(
&mut self,
parent: &Inode,
children: Vec<(OsString, F)>,
) -> Result<Vec<Inode>, InsertError>where
F: Fn(ValueCreatorParams<'_, Data>) -> Data,
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.
Sourcepub fn batch_insert<F, G>(
&mut self,
parent: &Inode,
entries: Vec<(Vec<OsString>, F)>,
default_parent_creator: G,
) -> Result<(), InsertError>
pub fn batch_insert<F, G>( &mut self, parent: &Inode, entries: Vec<(Vec<OsString>, F)>, default_parent_creator: G, ) -> Result<(), InsertError>
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| {})
Sourcepub fn resolve_first(&self, inode: &Inode) -> Option<Vec<OsString>>
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.
pub fn get(&self, inode: &Inode) -> Option<InodeInfo<'_, Data>>
Sourcepub fn resolve(&self, inode: &Inode) -> Option<Vec<Vec<OsString>>>
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.
Sourcepub fn get_data_mut(&mut self, inode: &Inode) -> Option<&mut Data>
pub fn get_data_mut(&mut self, inode: &Inode) -> Option<&mut Data>
Returns mutable access to the data stored for an inode.
Sourcepub fn get_children(&self, parent: &Inode) -> Vec<(&Arc<OsString>, &Inode)>
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.
Sourcepub fn lookup(
&self,
parent: &Inode,
name: &OsStr,
) -> Option<LookupResult<'_, Data>>
pub fn lookup( &self, parent: &Inode, name: &OsStr, ) -> Option<LookupResult<'_, Data>>
Looks up a child inode by its parent inode and name
Sourcepub fn link(
&mut self,
inode: &Inode,
parent: &Inode,
name: OsString,
) -> Result<(), LinkError>
pub fn link( &mut self, inode: &Inode, parent: &Inode, name: OsString, ) -> Result<(), LinkError>
Registers a new hard-link name for an existing inode. Call this only after the backing filesystem has successfully created the link.
Sourcepub fn unlink(&mut self, parent: &Inode, name: &OsStr) -> Option<Inode>
pub fn unlink(&mut self, parent: &Inode, name: &OsStr) -> Option<Inode>
Removes one name while keeping the inode alive for remaining links and outstanding FUSE references. Returns the inode that lost the name.
pub fn has_links(&self, inode: &Inode) -> bool
Sourcepub fn rename(
&mut self,
parent: &Inode,
oldname: &OsStr,
newparent: &Inode,
newname: OsString,
) -> Result<Option<(Inode, Data)>, RenameError>
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
Sourcepub fn exchange(
&mut self,
parent: &Inode,
name: &OsStr,
newparent: &Inode,
newname: &OsStr,
) -> Result<(), RenameError>
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.
Sourcepub fn remove(&mut self, inode: &Inode) -> Option<Data>
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
childrenmap to conserve memory.