1004e86ffSlogin use core::any::Any; 22dbef785SGnoCiYeH use core::intrinsics::unlikely; 3004e86ffSlogin 41d37ca6dSDonkey Kane use crate::filesystem::vfs::FSMAKER; 5597ecc08STTaq use crate::libs::rwlock::RwLock; 61d37ca6dSDonkey Kane use crate::{ 71d37ca6dSDonkey Kane driver::base::device::device_number::DeviceNumber, 81d37ca6dSDonkey Kane filesystem::vfs::{core::generate_inode_id, FileType}, 91d37ca6dSDonkey Kane ipc::pipe::LockedPipeInode, 10*924d64deSBrahmaMantra libs::casting::DowncastArc, 111d37ca6dSDonkey Kane libs::spinlock::{SpinLock, SpinLockGuard}, 121d37ca6dSDonkey Kane time::TimeSpec, 131d37ca6dSDonkey Kane }; 14*924d64deSBrahmaMantra 15004e86ffSlogin use alloc::{ 16004e86ffSlogin collections::BTreeMap, 17004e86ffSlogin string::String, 18004e86ffSlogin sync::{Arc, Weak}, 19004e86ffSlogin vec::Vec, 20004e86ffSlogin }; 2191e9d4abSLoGin use system_error::SystemError; 22004e86ffSlogin 23004e86ffSlogin use super::vfs::{ 241d37ca6dSDonkey Kane file::FilePrivateData, syscall::ModeType, FileSystem, FileSystemMaker, FsInfo, IndexNode, 251d37ca6dSDonkey Kane InodeId, Metadata, SpecialNodeData, 26004e86ffSlogin }; 27597ecc08STTaq use super::vfs::{Magic, SuperBlock}; 28004e86ffSlogin 29004e86ffSlogin /// RamFS的inode名称的最大长度 30004e86ffSlogin const RAMFS_MAX_NAMELEN: usize = 64; 31597ecc08STTaq const RAMFS_BLOCK_SIZE: u64 = 512; 32004e86ffSlogin /// @brief 内存文件系统的Inode结构体 33004e86ffSlogin #[derive(Debug)] 34004e86ffSlogin struct LockedRamFSInode(SpinLock<RamFSInode>); 35004e86ffSlogin 36004e86ffSlogin /// @brief 内存文件系统结构体 37004e86ffSlogin #[derive(Debug)] 38004e86ffSlogin pub struct RamFS { 39004e86ffSlogin /// RamFS的root inode 40004e86ffSlogin root_inode: Arc<LockedRamFSInode>, 41597ecc08STTaq super_block: RwLock<SuperBlock>, 42004e86ffSlogin } 43004e86ffSlogin 44004e86ffSlogin /// @brief 内存文件系统的Inode结构体(不包含锁) 45004e86ffSlogin #[derive(Debug)] 46004e86ffSlogin pub struct RamFSInode { 47004e86ffSlogin // parent变量目前只在find函数中使用到 48004e86ffSlogin // 所以只有当inode是文件夹的时候,parent才会生效 49004e86ffSlogin // 对于文件来说,parent就没什么作用了 50004e86ffSlogin // 关于parent的说明: 目录不允许有硬链接 51004e86ffSlogin /// 指向父Inode的弱引用 52004e86ffSlogin parent: Weak<LockedRamFSInode>, 53004e86ffSlogin /// 指向自身的弱引用 54004e86ffSlogin self_ref: Weak<LockedRamFSInode>, 55004e86ffSlogin /// 子Inode的B树 56004e86ffSlogin children: BTreeMap<String, Arc<LockedRamFSInode>>, 57004e86ffSlogin /// 当前inode的数据部分 58004e86ffSlogin data: Vec<u8>, 59004e86ffSlogin /// 当前inode的元数据 60004e86ffSlogin metadata: Metadata, 61004e86ffSlogin /// 指向inode所在的文件系统对象的指针 62004e86ffSlogin fs: Weak<RamFS>, 632dbef785SGnoCiYeH /// 指向特殊节点 642dbef785SGnoCiYeH special_node: Option<SpecialNodeData>, 65004e86ffSlogin } 66004e86ffSlogin 67004e86ffSlogin impl FileSystem for RamFS { 68004e86ffSlogin fn root_inode(&self) -> Arc<dyn super::vfs::IndexNode> { 69004e86ffSlogin return self.root_inode.clone(); 70004e86ffSlogin } 71004e86ffSlogin 72004e86ffSlogin fn info(&self) -> FsInfo { 73004e86ffSlogin return FsInfo { 74004e86ffSlogin blk_dev_id: 0, 75004e86ffSlogin max_name_len: RAMFS_MAX_NAMELEN, 76004e86ffSlogin }; 77004e86ffSlogin } 78004e86ffSlogin 79004e86ffSlogin /// @brief 本函数用于实现动态转换。 80004e86ffSlogin /// 具体的文件系统在实现本函数时,最简单的方式就是:直接返回self 81004e86ffSlogin fn as_any_ref(&self) -> &dyn Any { 82004e86ffSlogin self 83004e86ffSlogin } 841d37ca6dSDonkey Kane 851d37ca6dSDonkey Kane fn name(&self) -> &str { 861d37ca6dSDonkey Kane "ramfs" 871d37ca6dSDonkey Kane } 88597ecc08STTaq 89597ecc08STTaq fn super_block(&self) -> SuperBlock { 90597ecc08STTaq self.super_block.read().clone() 91597ecc08STTaq } 92004e86ffSlogin } 93004e86ffSlogin 94004e86ffSlogin impl RamFS { 95004e86ffSlogin pub fn new() -> Arc<Self> { 96597ecc08STTaq let super_block = SuperBlock::new( 97597ecc08STTaq Magic::RAMFS_MAGIC, 98597ecc08STTaq RAMFS_BLOCK_SIZE, 99597ecc08STTaq RAMFS_MAX_NAMELEN as u64, 100597ecc08STTaq ); 101004e86ffSlogin // 初始化root inode 102004e86ffSlogin let root: Arc<LockedRamFSInode> = Arc::new(LockedRamFSInode(SpinLock::new(RamFSInode { 103004e86ffSlogin parent: Weak::default(), 104004e86ffSlogin self_ref: Weak::default(), 105004e86ffSlogin children: BTreeMap::new(), 106004e86ffSlogin data: Vec::new(), 107004e86ffSlogin metadata: Metadata { 108004e86ffSlogin dev_id: 0, 109004e86ffSlogin inode_id: generate_inode_id(), 110004e86ffSlogin size: 0, 111004e86ffSlogin blk_size: 0, 112004e86ffSlogin blocks: 0, 113004e86ffSlogin atime: TimeSpec::default(), 114004e86ffSlogin mtime: TimeSpec::default(), 115004e86ffSlogin ctime: TimeSpec::default(), 116004e86ffSlogin file_type: FileType::Dir, 1176b4e7a29SLoGin mode: ModeType::from_bits_truncate(0o777), 118004e86ffSlogin nlinks: 1, 119004e86ffSlogin uid: 0, 120004e86ffSlogin gid: 0, 12102343d0bSLoGin raw_dev: DeviceNumber::default(), 122004e86ffSlogin }, 123004e86ffSlogin fs: Weak::default(), 1242dbef785SGnoCiYeH special_node: None, 125004e86ffSlogin }))); 126004e86ffSlogin 127597ecc08STTaq let result: Arc<RamFS> = Arc::new(RamFS { 128597ecc08STTaq root_inode: root, 129597ecc08STTaq super_block: RwLock::new(super_block), 130597ecc08STTaq }); 131004e86ffSlogin 132004e86ffSlogin // 对root inode加锁,并继续完成初始化工作 133004e86ffSlogin let mut root_guard: SpinLockGuard<RamFSInode> = result.root_inode.0.lock(); 134004e86ffSlogin root_guard.parent = Arc::downgrade(&result.root_inode); 135004e86ffSlogin root_guard.self_ref = Arc::downgrade(&result.root_inode); 136004e86ffSlogin root_guard.fs = Arc::downgrade(&result); 137004e86ffSlogin // 释放锁 138004e86ffSlogin drop(root_guard); 139004e86ffSlogin 140004e86ffSlogin return result; 141004e86ffSlogin } 1421d37ca6dSDonkey Kane 1431d37ca6dSDonkey Kane pub fn make_ramfs() -> Result<Arc<dyn FileSystem + 'static>, SystemError> { 1441d37ca6dSDonkey Kane let fs = RamFS::new(); 1451d37ca6dSDonkey Kane return Ok(fs); 146004e86ffSlogin } 1471d37ca6dSDonkey Kane } 1481d37ca6dSDonkey Kane #[distributed_slice(FSMAKER)] 1491d37ca6dSDonkey Kane static RAMFSMAKER: FileSystemMaker = FileSystemMaker::new( 1501d37ca6dSDonkey Kane "ramfs", 1511d37ca6dSDonkey Kane &(RamFS::make_ramfs as fn() -> Result<Arc<dyn FileSystem + 'static>, SystemError>), 1521d37ca6dSDonkey Kane ); 153004e86ffSlogin 154004e86ffSlogin impl IndexNode for LockedRamFSInode { 155a3ef8f8aSGnoCiYeH fn truncate(&self, len: usize) -> Result<(), SystemError> { 156a3ef8f8aSGnoCiYeH let mut inode = self.0.lock(); 157a3ef8f8aSGnoCiYeH 158a3ef8f8aSGnoCiYeH //如果是文件夹,则报错 159a3ef8f8aSGnoCiYeH if inode.metadata.file_type == FileType::Dir { 160a3ef8f8aSGnoCiYeH return Err(SystemError::EINVAL); 161a3ef8f8aSGnoCiYeH } 162a3ef8f8aSGnoCiYeH 163a3ef8f8aSGnoCiYeH //当前文件长度大于_len才进行截断,否则不操作 164a3ef8f8aSGnoCiYeH if inode.data.len() > len { 165a3ef8f8aSGnoCiYeH inode.data.resize(len, 0); 166a3ef8f8aSGnoCiYeH } 167a3ef8f8aSGnoCiYeH return Ok(()); 168a3ef8f8aSGnoCiYeH } 169a3ef8f8aSGnoCiYeH 170a3ef8f8aSGnoCiYeH fn close(&self, _data: &mut FilePrivateData) -> Result<(), SystemError> { 171a3ef8f8aSGnoCiYeH return Ok(()); 172a3ef8f8aSGnoCiYeH } 173a3ef8f8aSGnoCiYeH 174a3ef8f8aSGnoCiYeH fn open( 175a3ef8f8aSGnoCiYeH &self, 176a3ef8f8aSGnoCiYeH _data: &mut FilePrivateData, 177a3ef8f8aSGnoCiYeH _mode: &super::vfs::file::FileMode, 178a3ef8f8aSGnoCiYeH ) -> Result<(), SystemError> { 179a3ef8f8aSGnoCiYeH return Ok(()); 180a3ef8f8aSGnoCiYeH } 181a3ef8f8aSGnoCiYeH 182004e86ffSlogin fn read_at( 183004e86ffSlogin &self, 184004e86ffSlogin offset: usize, 185004e86ffSlogin len: usize, 186004e86ffSlogin buf: &mut [u8], 187004e86ffSlogin _data: &mut FilePrivateData, 188676b8ef6SMork ) -> Result<usize, SystemError> { 189004e86ffSlogin if buf.len() < len { 190676b8ef6SMork return Err(SystemError::EINVAL); 191004e86ffSlogin } 192004e86ffSlogin // 加锁 193004e86ffSlogin let inode: SpinLockGuard<RamFSInode> = self.0.lock(); 194004e86ffSlogin 195004e86ffSlogin // 检查当前inode是否为一个文件夹,如果是的话,就返回错误 196004e86ffSlogin if inode.metadata.file_type == FileType::Dir { 197676b8ef6SMork return Err(SystemError::EISDIR); 198004e86ffSlogin } 199004e86ffSlogin 200004e86ffSlogin let start = inode.data.len().min(offset); 201004e86ffSlogin let end = inode.data.len().min(offset + len); 202004e86ffSlogin 203004e86ffSlogin // buffer空间不足 204004e86ffSlogin if buf.len() < (end - start) { 205676b8ef6SMork return Err(SystemError::ENOBUFS); 206004e86ffSlogin } 207004e86ffSlogin 208004e86ffSlogin // 拷贝数据 209004e86ffSlogin let src = &inode.data[start..end]; 210004e86ffSlogin buf[0..src.len()].copy_from_slice(src); 211004e86ffSlogin return Ok(src.len()); 212004e86ffSlogin } 213004e86ffSlogin 214004e86ffSlogin fn write_at( 215004e86ffSlogin &self, 216004e86ffSlogin offset: usize, 217004e86ffSlogin len: usize, 218004e86ffSlogin buf: &[u8], 219004e86ffSlogin _data: &mut FilePrivateData, 220676b8ef6SMork ) -> Result<usize, SystemError> { 221004e86ffSlogin if buf.len() < len { 222676b8ef6SMork return Err(SystemError::EINVAL); 223004e86ffSlogin } 224004e86ffSlogin 225004e86ffSlogin // 加锁 226004e86ffSlogin let mut inode: SpinLockGuard<RamFSInode> = self.0.lock(); 227004e86ffSlogin 228004e86ffSlogin // 检查当前inode是否为一个文件夹,如果是的话,就返回错误 229004e86ffSlogin if inode.metadata.file_type == FileType::Dir { 230676b8ef6SMork return Err(SystemError::EISDIR); 231004e86ffSlogin } 232004e86ffSlogin 233004e86ffSlogin let data: &mut Vec<u8> = &mut inode.data; 234004e86ffSlogin 235004e86ffSlogin // 如果文件大小比原来的大,那就resize这个数组 236004e86ffSlogin if offset + len > data.len() { 237004e86ffSlogin data.resize(offset + len, 0); 238004e86ffSlogin } 239004e86ffSlogin 240004e86ffSlogin let target = &mut data[offset..offset + len]; 241004e86ffSlogin target.copy_from_slice(&buf[0..len]); 242004e86ffSlogin return Ok(len); 243004e86ffSlogin } 244004e86ffSlogin 245004e86ffSlogin fn fs(&self) -> Arc<dyn FileSystem> { 246004e86ffSlogin return self.0.lock().fs.upgrade().unwrap(); 247004e86ffSlogin } 248004e86ffSlogin 249004e86ffSlogin fn as_any_ref(&self) -> &dyn core::any::Any { 250004e86ffSlogin self 251004e86ffSlogin } 252004e86ffSlogin 253676b8ef6SMork fn metadata(&self) -> Result<Metadata, SystemError> { 254004e86ffSlogin let inode = self.0.lock(); 255004e86ffSlogin let mut metadata = inode.metadata.clone(); 256004e86ffSlogin metadata.size = inode.data.len() as i64; 257004e86ffSlogin 258004e86ffSlogin return Ok(metadata); 259004e86ffSlogin } 260004e86ffSlogin 261676b8ef6SMork fn set_metadata(&self, metadata: &Metadata) -> Result<(), SystemError> { 262004e86ffSlogin let mut inode = self.0.lock(); 263004e86ffSlogin inode.metadata.atime = metadata.atime; 264004e86ffSlogin inode.metadata.mtime = metadata.mtime; 265004e86ffSlogin inode.metadata.ctime = metadata.ctime; 266004e86ffSlogin inode.metadata.mode = metadata.mode; 267004e86ffSlogin inode.metadata.uid = metadata.uid; 268004e86ffSlogin inode.metadata.gid = metadata.gid; 269004e86ffSlogin 270004e86ffSlogin return Ok(()); 271004e86ffSlogin } 272004e86ffSlogin 273676b8ef6SMork fn resize(&self, len: usize) -> Result<(), SystemError> { 274004e86ffSlogin let mut inode = self.0.lock(); 275004e86ffSlogin if inode.metadata.file_type == FileType::File { 276004e86ffSlogin inode.data.resize(len, 0); 277004e86ffSlogin return Ok(()); 278004e86ffSlogin } else { 279676b8ef6SMork return Err(SystemError::EINVAL); 280004e86ffSlogin } 281004e86ffSlogin } 282004e86ffSlogin 283004e86ffSlogin fn create_with_data( 284004e86ffSlogin &self, 285004e86ffSlogin name: &str, 286004e86ffSlogin file_type: FileType, 2876b4e7a29SLoGin mode: ModeType, 288004e86ffSlogin data: usize, 289676b8ef6SMork ) -> Result<Arc<dyn IndexNode>, SystemError> { 290004e86ffSlogin // 获取当前inode 291004e86ffSlogin let mut inode = self.0.lock(); 292004e86ffSlogin // 如果当前inode不是文件夹,则返回 293004e86ffSlogin if inode.metadata.file_type != FileType::Dir { 294676b8ef6SMork return Err(SystemError::ENOTDIR); 295004e86ffSlogin } 296004e86ffSlogin // 如果有重名的,则返回 297004e86ffSlogin if inode.children.contains_key(name) { 298676b8ef6SMork return Err(SystemError::EEXIST); 299004e86ffSlogin } 300004e86ffSlogin 301004e86ffSlogin // 创建inode 302004e86ffSlogin let result: Arc<LockedRamFSInode> = Arc::new(LockedRamFSInode(SpinLock::new(RamFSInode { 303004e86ffSlogin parent: inode.self_ref.clone(), 304004e86ffSlogin self_ref: Weak::default(), 305004e86ffSlogin children: BTreeMap::new(), 306004e86ffSlogin data: Vec::new(), 307004e86ffSlogin metadata: Metadata { 308004e86ffSlogin dev_id: 0, 309004e86ffSlogin inode_id: generate_inode_id(), 310004e86ffSlogin size: 0, 311004e86ffSlogin blk_size: 0, 312004e86ffSlogin blocks: 0, 313004e86ffSlogin atime: TimeSpec::default(), 314004e86ffSlogin mtime: TimeSpec::default(), 315004e86ffSlogin ctime: TimeSpec::default(), 316b5b571e0SLoGin file_type, 317b5b571e0SLoGin mode, 318004e86ffSlogin nlinks: 1, 319004e86ffSlogin uid: 0, 320004e86ffSlogin gid: 0, 32102343d0bSLoGin raw_dev: DeviceNumber::from(data as u32), 322004e86ffSlogin }, 323004e86ffSlogin fs: inode.fs.clone(), 3242dbef785SGnoCiYeH special_node: None, 325004e86ffSlogin }))); 326004e86ffSlogin 327004e86ffSlogin // 初始化inode的自引用的weak指针 328004e86ffSlogin result.0.lock().self_ref = Arc::downgrade(&result); 329004e86ffSlogin 330004e86ffSlogin // 将子inode插入父inode的B树中 331004e86ffSlogin inode.children.insert(String::from(name), result.clone()); 332004e86ffSlogin 333004e86ffSlogin return Ok(result); 334004e86ffSlogin } 335004e86ffSlogin 336676b8ef6SMork fn link(&self, name: &str, other: &Arc<dyn IndexNode>) -> Result<(), SystemError> { 337004e86ffSlogin let other: &LockedRamFSInode = other 338004e86ffSlogin .downcast_ref::<LockedRamFSInode>() 339676b8ef6SMork .ok_or(SystemError::EPERM)?; 340004e86ffSlogin let mut inode: SpinLockGuard<RamFSInode> = self.0.lock(); 341004e86ffSlogin let mut other_locked: SpinLockGuard<RamFSInode> = other.0.lock(); 342004e86ffSlogin 343004e86ffSlogin // 如果当前inode不是文件夹,那么报错 344004e86ffSlogin if inode.metadata.file_type != FileType::Dir { 345676b8ef6SMork return Err(SystemError::ENOTDIR); 346004e86ffSlogin } 347004e86ffSlogin 348004e86ffSlogin // 如果另一个inode是文件夹,那么也报错 349004e86ffSlogin if other_locked.metadata.file_type == FileType::Dir { 350676b8ef6SMork return Err(SystemError::EISDIR); 351004e86ffSlogin } 352004e86ffSlogin 353004e86ffSlogin // 如果当前文件夹下已经有同名文件,也报错。 354004e86ffSlogin if inode.children.contains_key(name) { 355676b8ef6SMork return Err(SystemError::EEXIST); 356004e86ffSlogin } 357004e86ffSlogin 358004e86ffSlogin inode 359004e86ffSlogin .children 360004e86ffSlogin .insert(String::from(name), other_locked.self_ref.upgrade().unwrap()); 361004e86ffSlogin 362004e86ffSlogin // 增加硬链接计数 363004e86ffSlogin other_locked.metadata.nlinks += 1; 364004e86ffSlogin return Ok(()); 365004e86ffSlogin } 366004e86ffSlogin 367676b8ef6SMork fn unlink(&self, name: &str) -> Result<(), SystemError> { 368004e86ffSlogin let mut inode: SpinLockGuard<RamFSInode> = self.0.lock(); 369004e86ffSlogin // 如果当前inode不是目录,那么也没有子目录/文件的概念了,因此要求当前inode的类型是目录 370004e86ffSlogin if inode.metadata.file_type != FileType::Dir { 371676b8ef6SMork return Err(SystemError::ENOTDIR); 372004e86ffSlogin } 373004e86ffSlogin // 不允许删除当前文件夹,也不允许删除上一个目录 374004e86ffSlogin if name == "." || name == ".." { 375676b8ef6SMork return Err(SystemError::ENOTEMPTY); 376004e86ffSlogin } 377004e86ffSlogin 378004e86ffSlogin // 获得要删除的文件的inode 379676b8ef6SMork let to_delete = inode.children.get(name).ok_or(SystemError::ENOENT)?; 380004e86ffSlogin if to_delete.0.lock().metadata.file_type == FileType::Dir { 381676b8ef6SMork return Err(SystemError::EPERM); 382004e86ffSlogin } 383004e86ffSlogin // 减少硬链接计数 384004e86ffSlogin to_delete.0.lock().metadata.nlinks -= 1; 385004e86ffSlogin // 在当前目录中删除这个子目录项 386004e86ffSlogin inode.children.remove(name); 387004e86ffSlogin return Ok(()); 388004e86ffSlogin } 389004e86ffSlogin 390676b8ef6SMork fn rmdir(&self, name: &str) -> Result<(), SystemError> { 391004e86ffSlogin let mut inode: SpinLockGuard<RamFSInode> = self.0.lock(); 392004e86ffSlogin // 如果当前inode不是目录,那么也没有子目录/文件的概念了,因此要求当前inode的类型是目录 393004e86ffSlogin if inode.metadata.file_type != FileType::Dir { 394676b8ef6SMork return Err(SystemError::ENOTDIR); 395004e86ffSlogin } 396004e86ffSlogin // 获得要删除的文件夹的inode 397676b8ef6SMork let to_delete = inode.children.get(name).ok_or(SystemError::ENOENT)?; 398004e86ffSlogin if to_delete.0.lock().metadata.file_type != FileType::Dir { 399676b8ef6SMork return Err(SystemError::ENOTDIR); 400004e86ffSlogin } 401004e86ffSlogin 402004e86ffSlogin to_delete.0.lock().metadata.nlinks -= 1; 403004e86ffSlogin // 在当前目录中删除这个子目录项 404004e86ffSlogin inode.children.remove(name); 405004e86ffSlogin return Ok(()); 406004e86ffSlogin } 407004e86ffSlogin 4089e481b3bSTTaq fn move_to( 409004e86ffSlogin &self, 410004e86ffSlogin old_name: &str, 411004e86ffSlogin target: &Arc<dyn IndexNode>, 412004e86ffSlogin new_name: &str, 413676b8ef6SMork ) -> Result<(), SystemError> { 414*924d64deSBrahmaMantra let inode: Arc<dyn IndexNode> = self.find(old_name)?; 415*924d64deSBrahmaMantra // 修改其对父节点的引用 416*924d64deSBrahmaMantra inode 417*924d64deSBrahmaMantra .downcast_ref::<LockedRamFSInode>() 418*924d64deSBrahmaMantra .ok_or(SystemError::EPERM)? 419*924d64deSBrahmaMantra .0 420*924d64deSBrahmaMantra .lock() 421*924d64deSBrahmaMantra .parent = Arc::downgrade( 422*924d64deSBrahmaMantra &target 423*924d64deSBrahmaMantra .clone() 424*924d64deSBrahmaMantra .downcast_arc::<LockedRamFSInode>() 425*924d64deSBrahmaMantra .ok_or(SystemError::EPERM)?, 426*924d64deSBrahmaMantra ); 427004e86ffSlogin 428004e86ffSlogin // 在新的目录下创建一个硬链接 429*924d64deSBrahmaMantra target.link(new_name, &inode)?; 430*924d64deSBrahmaMantra 431004e86ffSlogin // 取消现有的目录下的这个硬链接 432*924d64deSBrahmaMantra if let Err(e) = self.unlink(old_name) { 433*924d64deSBrahmaMantra // 当操作失败时回退操作 434004e86ffSlogin target.unlink(new_name)?; 435*924d64deSBrahmaMantra return Err(e); 436004e86ffSlogin } 437*924d64deSBrahmaMantra 438004e86ffSlogin return Ok(()); 439004e86ffSlogin } 440004e86ffSlogin 441676b8ef6SMork fn find(&self, name: &str) -> Result<Arc<dyn IndexNode>, SystemError> { 442004e86ffSlogin let inode = self.0.lock(); 443004e86ffSlogin 444004e86ffSlogin if inode.metadata.file_type != FileType::Dir { 445676b8ef6SMork return Err(SystemError::ENOTDIR); 446004e86ffSlogin } 447004e86ffSlogin 448004e86ffSlogin match name { 449004e86ffSlogin "" | "." => { 450676b8ef6SMork return Ok(inode.self_ref.upgrade().ok_or(SystemError::ENOENT)?); 451004e86ffSlogin } 452004e86ffSlogin 453004e86ffSlogin ".." => { 454676b8ef6SMork return Ok(inode.parent.upgrade().ok_or(SystemError::ENOENT)?); 455004e86ffSlogin } 456004e86ffSlogin name => { 457004e86ffSlogin // 在子目录项中查找 458676b8ef6SMork return Ok(inode.children.get(name).ok_or(SystemError::ENOENT)?.clone()); 459004e86ffSlogin } 460004e86ffSlogin } 461004e86ffSlogin } 462004e86ffSlogin 463676b8ef6SMork fn get_entry_name(&self, ino: InodeId) -> Result<String, SystemError> { 464004e86ffSlogin let inode: SpinLockGuard<RamFSInode> = self.0.lock(); 465004e86ffSlogin if inode.metadata.file_type != FileType::Dir { 466676b8ef6SMork return Err(SystemError::ENOTDIR); 467004e86ffSlogin } 468004e86ffSlogin 4696b4e7a29SLoGin match ino.into() { 470004e86ffSlogin 0 => { 471004e86ffSlogin return Ok(String::from(".")); 472004e86ffSlogin } 473004e86ffSlogin 1 => { 474004e86ffSlogin return Ok(String::from("..")); 475004e86ffSlogin } 476004e86ffSlogin ino => { 477004e86ffSlogin // 暴力遍历所有的children,判断inode id是否相同 478004e86ffSlogin // TODO: 优化这里,这个地方性能很差! 479004e86ffSlogin let mut key: Vec<String> = inode 480004e86ffSlogin .children 481004e86ffSlogin .keys() 4826b4e7a29SLoGin .filter(|k| { 4836b4e7a29SLoGin inode 4846b4e7a29SLoGin .children 4856b4e7a29SLoGin .get(*k) 4866b4e7a29SLoGin .unwrap() 4876b4e7a29SLoGin .0 4886b4e7a29SLoGin .lock() 4896b4e7a29SLoGin .metadata 4906b4e7a29SLoGin .inode_id 4916b4e7a29SLoGin .into() 4926b4e7a29SLoGin == ino 4936b4e7a29SLoGin }) 494004e86ffSlogin .cloned() 495004e86ffSlogin .collect(); 496004e86ffSlogin 497004e86ffSlogin match key.len() { 498676b8ef6SMork 0=>{return Err(SystemError::ENOENT);} 499004e86ffSlogin 1=>{return Ok(key.remove(0));} 5006b4e7a29SLoGin _ => panic!("Ramfs get_entry_name: key.len()={key_len}>1, current inode_id={inode_id:?}, to find={to_find:?}", key_len=key.len(), inode_id = inode.metadata.inode_id, to_find=ino) 501004e86ffSlogin } 502004e86ffSlogin } 503004e86ffSlogin } 504004e86ffSlogin } 505004e86ffSlogin 506676b8ef6SMork fn list(&self) -> Result<Vec<String>, SystemError> { 507004e86ffSlogin let info = self.metadata()?; 508004e86ffSlogin if info.file_type != FileType::Dir { 509676b8ef6SMork return Err(SystemError::ENOTDIR); 510004e86ffSlogin } 511004e86ffSlogin 512004e86ffSlogin let mut keys: Vec<String> = Vec::new(); 513004e86ffSlogin keys.push(String::from(".")); 514004e86ffSlogin keys.push(String::from("..")); 515004e86ffSlogin keys.append(&mut self.0.lock().children.keys().cloned().collect()); 516004e86ffSlogin 517004e86ffSlogin return Ok(keys); 518004e86ffSlogin } 5192dbef785SGnoCiYeH 5202dbef785SGnoCiYeH fn mknod( 5212dbef785SGnoCiYeH &self, 5222dbef785SGnoCiYeH filename: &str, 5232dbef785SGnoCiYeH mode: ModeType, 524c566df45SLoGin _dev_t: DeviceNumber, 5252dbef785SGnoCiYeH ) -> Result<Arc<dyn IndexNode>, SystemError> { 5262dbef785SGnoCiYeH let mut inode = self.0.lock(); 5272dbef785SGnoCiYeH if inode.metadata.file_type != FileType::Dir { 5282dbef785SGnoCiYeH return Err(SystemError::ENOTDIR); 5292dbef785SGnoCiYeH } 5302dbef785SGnoCiYeH 5312dbef785SGnoCiYeH // 判断需要创建的类型 5322dbef785SGnoCiYeH if unlikely(mode.contains(ModeType::S_IFREG)) { 5332dbef785SGnoCiYeH // 普通文件 534b5b571e0SLoGin return self.create(filename, FileType::File, mode); 5352dbef785SGnoCiYeH } 5362dbef785SGnoCiYeH 5372dbef785SGnoCiYeH let nod = Arc::new(LockedRamFSInode(SpinLock::new(RamFSInode { 5382dbef785SGnoCiYeH parent: inode.self_ref.clone(), 5392dbef785SGnoCiYeH self_ref: Weak::default(), 5402dbef785SGnoCiYeH children: BTreeMap::new(), 5412dbef785SGnoCiYeH data: Vec::new(), 5422dbef785SGnoCiYeH metadata: Metadata { 5432dbef785SGnoCiYeH dev_id: 0, 5442dbef785SGnoCiYeH inode_id: generate_inode_id(), 5452dbef785SGnoCiYeH size: 0, 5462dbef785SGnoCiYeH blk_size: 0, 5472dbef785SGnoCiYeH blocks: 0, 5482dbef785SGnoCiYeH atime: TimeSpec::default(), 5492dbef785SGnoCiYeH mtime: TimeSpec::default(), 5502dbef785SGnoCiYeH ctime: TimeSpec::default(), 5512dbef785SGnoCiYeH file_type: FileType::Pipe, 552b5b571e0SLoGin mode, 5532dbef785SGnoCiYeH nlinks: 1, 5542dbef785SGnoCiYeH uid: 0, 5552dbef785SGnoCiYeH gid: 0, 55602343d0bSLoGin raw_dev: DeviceNumber::default(), 5572dbef785SGnoCiYeH }, 5582dbef785SGnoCiYeH fs: inode.fs.clone(), 5592dbef785SGnoCiYeH special_node: None, 5602dbef785SGnoCiYeH }))); 5612dbef785SGnoCiYeH 5622dbef785SGnoCiYeH nod.0.lock().self_ref = Arc::downgrade(&nod); 5632dbef785SGnoCiYeH 5642dbef785SGnoCiYeH if mode.contains(ModeType::S_IFIFO) { 5652dbef785SGnoCiYeH nod.0.lock().metadata.file_type = FileType::Pipe; 5662dbef785SGnoCiYeH // 创建pipe文件 5672dbef785SGnoCiYeH let pipe_inode = LockedPipeInode::new(); 5682dbef785SGnoCiYeH // 设置special_node 5692dbef785SGnoCiYeH nod.0.lock().special_node = Some(SpecialNodeData::Pipe(pipe_inode)); 5702dbef785SGnoCiYeH } else if mode.contains(ModeType::S_IFBLK) { 5712dbef785SGnoCiYeH nod.0.lock().metadata.file_type = FileType::BlockDevice; 5722dbef785SGnoCiYeH unimplemented!() 5732dbef785SGnoCiYeH } else if mode.contains(ModeType::S_IFCHR) { 5742dbef785SGnoCiYeH nod.0.lock().metadata.file_type = FileType::CharDevice; 5752dbef785SGnoCiYeH unimplemented!() 5762dbef785SGnoCiYeH } 5772dbef785SGnoCiYeH 5782dbef785SGnoCiYeH inode 5792dbef785SGnoCiYeH .children 5802dbef785SGnoCiYeH .insert(String::from(filename).to_uppercase(), nod.clone()); 5812dbef785SGnoCiYeH Ok(nod) 5822dbef785SGnoCiYeH } 5832dbef785SGnoCiYeH 5842dbef785SGnoCiYeH fn special_node(&self) -> Option<super::vfs::SpecialNodeData> { 5852dbef785SGnoCiYeH return self.0.lock().special_node.clone(); 5862dbef785SGnoCiYeH } 5879e481b3bSTTaq 5889e481b3bSTTaq /// # 用于重命名内存中的文件或目录 5899e481b3bSTTaq fn rename(&self, _old_name: &str, _new_name: &str) -> Result<(), SystemError> { 5909e481b3bSTTaq let old_inode: Arc<dyn IndexNode> = self.find(_old_name)?; 5919e481b3bSTTaq // 在新的目录下创建一个硬链接 5929e481b3bSTTaq self.link(_new_name, &old_inode)?; 5939e481b3bSTTaq 5949e481b3bSTTaq // 取消现有的目录下的这个硬链接 5959e481b3bSTTaq if let Err(err) = self.unlink(_old_name) { 5969e481b3bSTTaq // 如果取消失败,那就取消新的目录下的硬链接 5979e481b3bSTTaq self.unlink(_new_name)?; 5989e481b3bSTTaq return Err(err); 5999e481b3bSTTaq } 6009e481b3bSTTaq 6019e481b3bSTTaq return Ok(()); 6029e481b3bSTTaq } 603004e86ffSlogin } 604