diff --git a/src/fs/common.rs b/src/fs/common.rs deleted file mode 100644 index d859ec2..0000000 --- a/src/fs/common.rs +++ /dev/null @@ -1,172 +0,0 @@ -use std::collections::HashMap; -use std::ffi::OsStr; -use std::fmt::Debug; -use std::path::PathBuf; -use std::time::SystemTime; - -use anyhow::anyhow; -use async_trait::async_trait; -use fuser::{FileAttr, FileType, FUSE_ROOT_ID, TimeOrNow}; -use tracing::debug; - -use crate::common::LocalPath; -use crate::fs::inode::Inode; -use crate::prelude::*; - -pub trait CommonEntry { - fn get_ino(&self) -> Inode; - fn get_name(&self) -> &OsStr; - fn get_local_path(&self) -> &LocalPath; - fn get_attr(&self) -> &FileAttr; -} - -#[async_trait] -pub trait CommonFilesystem { - fn get_entries(&self) -> &HashMap; - fn get_entries_mut(&mut self) -> &mut HashMap; - fn get_children(&self) -> &HashMap>; - fn get_children_mut(&mut self) -> &mut HashMap>; - fn get_root_path(&self) -> LocalPath; - - fn generate_ino(&self) -> Inode { - Inode::new(self.get_entries().len() as u64 + 1) //TODO: check if this is working or if concurrency is a problem - } - - fn get_path_from_ino(&self, ino: impl Into) -> Option { - let ino = ino.into(); - debug!("get_path_from_ino: {}", ino); - let res = self.get_entry(ino)?.get_local_path().clone(); - debug!("get_path_from_ino: {}:{:?}", ino, res); - Some(res) - } - - fn get_full_path_from_ino(&self, ino: impl Into) -> Option { - let ino = ino.into(); - debug!("get_full_path_from_ino: {}", ino); - if ino == FUSE_ROOT_ID.into() { - return Some(self.get_root_path()); - } - let parent = self.get_parent_ino(ino); - if let Some(parent) = parent { - let path: PathBuf = self.get_full_path_from_ino(parent)?.into(); - let buf: LocalPath = path - .join::(self.get_path_from_ino(ino)?.into()) - .into(); - debug!("get_full_path_from_ino: {}:{:?}", ino, buf); - return Some(buf); - } - match self.get_path_from_ino(ino) { - Some(path) => Some(path.clone()), - None => None, - } - } - - fn get_child_with_path( - &self, - parent: impl Into, - path: impl AsRef, - ) -> Option { - let parent = parent.into(); - let path = path.as_ref(); - debug!("get_child_with_path: {}:{:?}", parent, path); - let children = self.get_children().get(&parent)?; - let mut res = None; - for child in children { - let child_path: &OsStr = self.get_entry(*child)?.get_local_path().into(); - if child_path == path { - res = Some(*child); - break; - } - } - debug!("get_child_with_path: {}:{:?}", parent, res); - res - } - - fn get_parent_ino(&self, ino: impl Into) -> Option { - let ino = ino.into(); - debug!("get_parent_ino: {}", ino); - if ino == FUSE_ROOT_ID.into() { - return None; - } - let mut parent = None; - for (parent_ino, child_inos) in self.get_children().iter() { - if child_inos.contains(&ino) { - parent = Some(*parent_ino); - break; - } - } - parent - } - - fn convert_to_system_time(mtime: TimeOrNow) -> SystemTime { - let mtime = match mtime { - TimeOrNow::SpecificTime(t) => t, - TimeOrNow::Now => SystemTime::now(), - }; - mtime - } - - fn get_entry(&self, ino: impl Into) -> Option<&Entry> { - self.get_entries().get(&ino.into()) - } - fn get_entry_mut(&mut self, ino: impl Into) -> Option<&mut Entry> { - self.get_entries_mut().get_mut(&ino.into()) - } - fn get_entry_r(&self, ino: impl Into) -> Result<&Entry> { - self.get_entries() - .get(&ino.into()) - .ok_or(anyhow!("Entry not found").into()) - } - - async fn add_file_entry( - &mut self, - parent: impl Into + Send, - name: &OsStr, - mode: u16, - size: u64, - ) -> Result { - let parent = parent.into(); - debug!("add_file_entry: {}:{:?}; {}", parent, name, mode); - - let ino = self - .add_entry_new(name, mode, FileType::RegularFile, parent, size) - .await?; - - Ok(ino) - } - - async fn add_entry_new( - &mut self, - name: &OsStr, - mode: u16, - file_type: FileType, - parent_ino: impl Into + Send + Debug, - size: u64, - ) -> Result; - - fn add_entry( - &mut self, - entry: Entry, - parent_ino: impl Into + Debug, - ) -> Inode - where Entry: Debug { - let ino = entry.get_ino(); - self.get_entries_mut().insert( - ino, entry, - ); - - self.add_child(parent_ino, &ino); - ino - } - - fn add_child(&mut self, parent_ino: impl Into, ino: impl Into) { - let parents_child_list = self - .get_children_mut() - .entry(parent_ino.into()) - .or_default(); - let ino: Inode = ino.into(); - if !parents_child_list.contains(&ino) { - parents_child_list.push(ino); - } - } -} diff --git a/src/fs/mod.rs b/src/fs/mod.rs index 73b2649..fad62f0 100644 --- a/src/fs/mod.rs +++ b/src/fs/mod.rs @@ -1,7 +1,5 @@ -pub use common::*; pub use inode::*; -mod common; mod inode; pub mod drive;