Work on Inode Cache

This commit is contained in:
oupson 2022-10-06 16:28:39 +02:00
parent 665cdd8b37
commit 918f815a5b
Signed by: oupson
GPG Key ID: 3BD88615552EFCB7
3 changed files with 42 additions and 32 deletions

View File

@ -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<u64, PathBuf>;
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<PathBuf> {
// 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<P>(path: P, ino: u64) -> FileAttr
where
P: AsRef<Path>,
@ -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);

View File

@ -0,0 +1,31 @@
use std::{collections::BTreeMap, path::PathBuf};
pub(crate) type InodeCache = BTreeMap<u64, PathBuf>;
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)
}
}

View File

@ -1,4 +1,6 @@
mod encrypted_filesystem;
mod inode_cache;
pub mod error;
pub use encrypted_filesystem::EncryptedFs;