From 918f815a5b3023bfd7db2c164e08e1585461fe4c Mon Sep 17 00:00:00 2001 From: oupson Date: Thu, 6 Oct 2022 16:28:39 +0200 Subject: [PATCH] Work on Inode Cache --- rustcryptfs-linux/src/encrypted_filesystem.rs | 41 ++++--------------- rustcryptfs-linux/src/inode_cache.rs | 31 ++++++++++++++ rustcryptfs-linux/src/lib.rs | 2 + 3 files changed, 42 insertions(+), 32 deletions(-) create mode 100644 rustcryptfs-linux/src/inode_cache.rs diff --git a/rustcryptfs-linux/src/encrypted_filesystem.rs b/rustcryptfs-linux/src/encrypted_filesystem.rs index 2f8cdf7..7b340ce 100644 --- a/rustcryptfs-linux/src/encrypted_filesystem.rs +++ b/rustcryptfs-linux/src/encrypted_filesystem.rs @@ -12,12 +12,13 @@ use std::{ use fuser::{FileAttr, FileType, Filesystem, FUSE_ROOT_ID}; use rustcryptfs_lib::GocryptFs; -use crate::error::Result; +use crate::{ + error::Result, + inode_cache::{InodeCache, InodeCacheExt}, +}; const BLOCK_SIZE: u64 = 4096; -type InodeCache = BTreeMap; - pub struct EncryptedFs { fs: GocryptFs, inode_cache: InodeCache, @@ -48,13 +49,6 @@ impl EncryptedFs { fuser::mount2(self, mountpoint, &[]).unwrap(); } - fn get_path(&self, ino: u64) -> Option { - // TODO CHECK PERM - - // TODO AVOID CLONE - self.inode_cache.get(&ino).map(|p| p.clone()) - } - fn get_real_size(size: u64) -> u64 { if size == 0 { 0 @@ -86,6 +80,10 @@ impl EncryptedFs { } } + fn get_path(&self, ino: u64) -> Option<&PathBuf> { + self.inode_cache.get_path(ino) + } + fn get_attr

(path: P, ino: u64) -> FileAttr where P: AsRef, @@ -120,27 +118,6 @@ impl EncryptedFs { } } -trait InodeCacheExt { - fn get_or_insert_inode(&mut self, file_path: PathBuf) -> (u64, PathBuf); -} - -impl InodeCacheExt for InodeCache { - // TODO Try to avoid clone - fn get_or_insert_inode(&mut self, file_path: PathBuf) -> (u64, PathBuf) { - if let Some((ino, path)) = { - self.iter() - .find_map(|(i, p)| if p.eq(&file_path) { Some((i, p)) } else { None }) - } { - (*ino, path.clone()) - } else { - let ino = self.len() as u64 + 1; - self.insert(ino, file_path); - - (ino, self.get(&ino).unwrap().clone()) - } - } -} - impl Filesystem for EncryptedFs { fn access(&mut self, _req: &fuser::Request<'_>, ino: u64, mask: i32, reply: fuser::ReplyEmpty) { if let Some(path) = self.get_path(ino) { @@ -200,7 +177,7 @@ impl Filesystem for EncryptedFs { offset: i64, mut reply: fuser::ReplyDirectory, ) { - if let Some(folder_path) = &self.get_path(ino) { + if let Some(folder_path) = &self.inode_cache.get_path(ino).cloned() { let iv = std::fs::read(folder_path.join("gocryptfs.diriv")).unwrap(); let dir_decoder = self.fs.filename_decoder().get_decoder_for_dir(&iv); diff --git a/rustcryptfs-linux/src/inode_cache.rs b/rustcryptfs-linux/src/inode_cache.rs new file mode 100644 index 0000000..be91d95 --- /dev/null +++ b/rustcryptfs-linux/src/inode_cache.rs @@ -0,0 +1,31 @@ +use std::{collections::BTreeMap, path::PathBuf}; + +pub(crate) type InodeCache = BTreeMap; + +pub(crate) trait InodeCacheExt { + fn get_or_insert_inode(&mut self, file_path: PathBuf) -> (u64, PathBuf); + + fn get_path(&self, ino: u64) -> Option<&PathBuf>; +} + +impl InodeCacheExt for InodeCache { + // TODO Try to avoid clone + fn get_or_insert_inode(&mut self, file_path: PathBuf) -> (u64, PathBuf) { + if let Some((ino, path)) = + self.iter() + .find_map(|(i, p)| if p.eq(&file_path) { Some((i, p)) } else { None }) + { + (*ino, path.clone()) + } else { + let ino = self.len() as u64 + 1; + self.insert(ino, file_path); + + (ino, self.get(&ino).unwrap().clone()) + } + } + + fn get_path(&self, ino: u64) -> Option<&PathBuf> { + // TODO CHECK PERM + self.get(&ino) + } +} diff --git a/rustcryptfs-linux/src/lib.rs b/rustcryptfs-linux/src/lib.rs index 5d59b1c..8e1278d 100644 --- a/rustcryptfs-linux/src/lib.rs +++ b/rustcryptfs-linux/src/lib.rs @@ -1,4 +1,6 @@ mod encrypted_filesystem; +mod inode_cache; + pub mod error; pub use encrypted_filesystem::EncryptedFs; \ No newline at end of file