1004e86ffSlogin use core::any::Any; 22dbef785SGnoCiYeH use core::intrinsics::unlikely; 3004e86ffSlogin 41d37ca6dSDonkey Kane use crate::filesystem::vfs::FSMAKER; 5*597ecc08STTaq 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, 101d37ca6dSDonkey Kane libs::spinlock::{SpinLock, SpinLockGuard}, 111d37ca6dSDonkey Kane time::TimeSpec, 121d37ca6dSDonkey Kane }; 13004e86ffSlogin use alloc::{ 14004e86ffSlogin collections::BTreeMap, 15004e86ffSlogin string::String, 16004e86ffSlogin sync::{Arc, Weak}, 17004e86ffSlogin vec::Vec, 18004e86ffSlogin }; 1991e9d4abSLoGin use system_error::SystemError; 20004e86ffSlogin 21004e86ffSlogin use super::vfs::{ 221d37ca6dSDonkey Kane file::FilePrivateData, syscall::ModeType, FileSystem, FileSystemMaker, FsInfo, IndexNode, 231d37ca6dSDonkey Kane InodeId, Metadata, SpecialNodeData, 24004e86ffSlogin }; 25*597ecc08STTaq use super::vfs::{Magic, SuperBlock}; 26004e86ffSlogin 27004e86ffSlogin /// RamFS的inode名称的最大长度 28004e86ffSlogin const RAMFS_MAX_NAMELEN: usize = 64; 29*597ecc08STTaq const RAMFS_BLOCK_SIZE: u64 = 512; 30004e86ffSlogin /// @brief 内存文件系统的Inode结构体 31004e86ffSlogin #[derive(Debug)] 32004e86ffSlogin struct LockedRamFSInode(SpinLock<RamFSInode>); 33004e86ffSlogin 34004e86ffSlogin /// @brief 内存文件系统结构体 35004e86ffSlogin #[derive(Debug)] 36004e86ffSlogin pub struct RamFS { 37004e86ffSlogin /// RamFS的root inode 38004e86ffSlogin root_inode: Arc<LockedRamFSInode>, 39*597ecc08STTaq super_block: RwLock<SuperBlock>, 40004e86ffSlogin } 41004e86ffSlogin 42004e86ffSlogin /// @brief 内存文件系统的Inode结构体(不包含锁) 43004e86ffSlogin #[derive(Debug)] 44004e86ffSlogin pub struct RamFSInode { 45004e86ffSlogin // parent变量目前只在find函数中使用到 46004e86ffSlogin // 所以只有当inode是文件夹的时候,parent才会生效 47004e86ffSlogin // 对于文件来说,parent就没什么作用了 48004e86ffSlogin // 关于parent的说明: 目录不允许有硬链接 49004e86ffSlogin /// 指向父Inode的弱引用 50004e86ffSlogin parent: Weak<LockedRamFSInode>, 51004e86ffSlogin /// 指向自身的弱引用 52004e86ffSlogin self_ref: Weak<LockedRamFSInode>, 53004e86ffSlogin /// 子Inode的B树 54004e86ffSlogin children: BTreeMap<String, Arc<LockedRamFSInode>>, 55004e86ffSlogin /// 当前inode的数据部分 56004e86ffSlogin data: Vec<u8>, 57004e86ffSlogin /// 当前inode的元数据 58004e86ffSlogin metadata: Metadata, 59004e86ffSlogin /// 指向inode所在的文件系统对象的指针 60004e86ffSlogin fs: Weak<RamFS>, 612dbef785SGnoCiYeH /// 指向特殊节点 622dbef785SGnoCiYeH special_node: Option<SpecialNodeData>, 63004e86ffSlogin } 64004e86ffSlogin 65004e86ffSlogin impl FileSystem for RamFS { 66004e86ffSlogin fn root_inode(&self) -> Arc<dyn super::vfs::IndexNode> { 67004e86ffSlogin return self.root_inode.clone(); 68004e86ffSlogin } 69004e86ffSlogin 70004e86ffSlogin fn info(&self) -> FsInfo { 71004e86ffSlogin return FsInfo { 72004e86ffSlogin blk_dev_id: 0, 73004e86ffSlogin max_name_len: RAMFS_MAX_NAMELEN, 74004e86ffSlogin }; 75004e86ffSlogin } 76004e86ffSlogin 77004e86ffSlogin /// @brief 本函数用于实现动态转换。 78004e86ffSlogin /// 具体的文件系统在实现本函数时,最简单的方式就是:直接返回self 79004e86ffSlogin fn as_any_ref(&self) -> &dyn Any { 80004e86ffSlogin self 81004e86ffSlogin } 821d37ca6dSDonkey Kane 831d37ca6dSDonkey Kane fn name(&self) -> &str { 841d37ca6dSDonkey Kane "ramfs" 851d37ca6dSDonkey Kane } 86*597ecc08STTaq 87*597ecc08STTaq fn super_block(&self) -> SuperBlock { 88*597ecc08STTaq self.super_block.read().clone() 89*597ecc08STTaq } 90004e86ffSlogin } 91004e86ffSlogin 92004e86ffSlogin impl RamFS { 93004e86ffSlogin pub fn new() -> Arc<Self> { 94*597ecc08STTaq let super_block = SuperBlock::new( 95*597ecc08STTaq Magic::RAMFS_MAGIC, 96*597ecc08STTaq RAMFS_BLOCK_SIZE, 97*597ecc08STTaq RAMFS_MAX_NAMELEN as u64, 98*597ecc08STTaq ); 99004e86ffSlogin // 初始化root inode 100004e86ffSlogin let root: Arc<LockedRamFSInode> = Arc::new(LockedRamFSInode(SpinLock::new(RamFSInode { 101004e86ffSlogin parent: Weak::default(), 102004e86ffSlogin self_ref: Weak::default(), 103004e86ffSlogin children: BTreeMap::new(), 104004e86ffSlogin data: Vec::new(), 105004e86ffSlogin metadata: Metadata { 106004e86ffSlogin dev_id: 0, 107004e86ffSlogin inode_id: generate_inode_id(), 108004e86ffSlogin size: 0, 109004e86ffSlogin blk_size: 0, 110004e86ffSlogin blocks: 0, 111004e86ffSlogin atime: TimeSpec::default(), 112004e86ffSlogin mtime: TimeSpec::default(), 113004e86ffSlogin ctime: TimeSpec::default(), 114004e86ffSlogin file_type: FileType::Dir, 1156b4e7a29SLoGin mode: ModeType::from_bits_truncate(0o777), 116004e86ffSlogin nlinks: 1, 117004e86ffSlogin uid: 0, 118004e86ffSlogin gid: 0, 11902343d0bSLoGin raw_dev: DeviceNumber::default(), 120004e86ffSlogin }, 121004e86ffSlogin fs: Weak::default(), 1222dbef785SGnoCiYeH special_node: None, 123004e86ffSlogin }))); 124004e86ffSlogin 125*597ecc08STTaq let result: Arc<RamFS> = Arc::new(RamFS { 126*597ecc08STTaq root_inode: root, 127*597ecc08STTaq super_block: RwLock::new(super_block), 128*597ecc08STTaq }); 129004e86ffSlogin 130004e86ffSlogin // 对root inode加锁,并继续完成初始化工作 131004e86ffSlogin let mut root_guard: SpinLockGuard<RamFSInode> = result.root_inode.0.lock(); 132004e86ffSlogin root_guard.parent = Arc::downgrade(&result.root_inode); 133004e86ffSlogin root_guard.self_ref = Arc::downgrade(&result.root_inode); 134004e86ffSlogin root_guard.fs = Arc::downgrade(&result); 135004e86ffSlogin // 释放锁 136004e86ffSlogin drop(root_guard); 137004e86ffSlogin 138004e86ffSlogin return result; 139004e86ffSlogin } 1401d37ca6dSDonkey Kane 1411d37ca6dSDonkey Kane pub fn make_ramfs() -> Result<Arc<dyn FileSystem + 'static>, SystemError> { 1421d37ca6dSDonkey Kane let fs = RamFS::new(); 1431d37ca6dSDonkey Kane return Ok(fs); 144004e86ffSlogin } 1451d37ca6dSDonkey Kane } 1461d37ca6dSDonkey Kane 1471d37ca6dSDonkey Kane #[distributed_slice(FSMAKER)] 1481d37ca6dSDonkey Kane static RAMFSMAKER: FileSystemMaker = FileSystemMaker::new( 1491d37ca6dSDonkey Kane "ramfs", 1501d37ca6dSDonkey Kane &(RamFS::make_ramfs as fn() -> Result<Arc<dyn FileSystem + 'static>, SystemError>), 1511d37ca6dSDonkey Kane ); 152004e86ffSlogin 153004e86ffSlogin impl IndexNode for LockedRamFSInode { 154a3ef8f8aSGnoCiYeH fn truncate(&self, len: usize) -> Result<(), SystemError> { 155a3ef8f8aSGnoCiYeH let mut inode = self.0.lock(); 156a3ef8f8aSGnoCiYeH 157a3ef8f8aSGnoCiYeH //如果是文件夹,则报错 158a3ef8f8aSGnoCiYeH if inode.metadata.file_type == FileType::Dir { 159a3ef8f8aSGnoCiYeH return Err(SystemError::EINVAL); 160a3ef8f8aSGnoCiYeH } 161a3ef8f8aSGnoCiYeH 162a3ef8f8aSGnoCiYeH //当前文件长度大于_len才进行截断,否则不操作 163a3ef8f8aSGnoCiYeH if inode.data.len() > len { 164a3ef8f8aSGnoCiYeH inode.data.resize(len, 0); 165a3ef8f8aSGnoCiYeH } 166a3ef8f8aSGnoCiYeH return Ok(()); 167a3ef8f8aSGnoCiYeH } 168a3ef8f8aSGnoCiYeH 169a3ef8f8aSGnoCiYeH fn close(&self, _data: &mut FilePrivateData) -> Result<(), SystemError> { 170a3ef8f8aSGnoCiYeH return Ok(()); 171a3ef8f8aSGnoCiYeH } 172a3ef8f8aSGnoCiYeH 173a3ef8f8aSGnoCiYeH fn open( 174a3ef8f8aSGnoCiYeH &self, 175a3ef8f8aSGnoCiYeH _data: &mut FilePrivateData, 176a3ef8f8aSGnoCiYeH _mode: &super::vfs::file::FileMode, 177a3ef8f8aSGnoCiYeH ) -> Result<(), SystemError> { 178a3ef8f8aSGnoCiYeH return Ok(()); 179a3ef8f8aSGnoCiYeH } 180a3ef8f8aSGnoCiYeH 181004e86ffSlogin fn read_at( 182004e86ffSlogin &self, 183004e86ffSlogin offset: usize, 184004e86ffSlogin len: usize, 185004e86ffSlogin buf: &mut [u8], 186004e86ffSlogin _data: &mut FilePrivateData, 187676b8ef6SMork ) -> Result<usize, SystemError> { 188004e86ffSlogin if buf.len() < len { 189676b8ef6SMork return Err(SystemError::EINVAL); 190004e86ffSlogin } 191004e86ffSlogin // 加锁 192004e86ffSlogin let inode: SpinLockGuard<RamFSInode> = self.0.lock(); 193004e86ffSlogin 194004e86ffSlogin // 检查当前inode是否为一个文件夹,如果是的话,就返回错误 195004e86ffSlogin if inode.metadata.file_type == FileType::Dir { 196676b8ef6SMork return Err(SystemError::EISDIR); 197004e86ffSlogin } 198004e86ffSlogin 199004e86ffSlogin let start = inode.data.len().min(offset); 200004e86ffSlogin let end = inode.data.len().min(offset + len); 201004e86ffSlogin 202004e86ffSlogin // buffer空间不足 203004e86ffSlogin if buf.len() < (end - start) { 204676b8ef6SMork return Err(SystemError::ENOBUFS); 205004e86ffSlogin } 206004e86ffSlogin 207004e86ffSlogin // 拷贝数据 208004e86ffSlogin let src = &inode.data[start..end]; 209004e86ffSlogin buf[0..src.len()].copy_from_slice(src); 210004e86ffSlogin return Ok(src.len()); 211004e86ffSlogin } 212004e86ffSlogin 213004e86ffSlogin fn write_at( 214004e86ffSlogin &self, 215004e86ffSlogin offset: usize, 216004e86ffSlogin len: usize, 217004e86ffSlogin buf: &[u8], 218004e86ffSlogin _data: &mut FilePrivateData, 219676b8ef6SMork ) -> Result<usize, SystemError> { 220004e86ffSlogin if buf.len() < len { 221676b8ef6SMork return Err(SystemError::EINVAL); 222004e86ffSlogin } 223004e86ffSlogin 224004e86ffSlogin // 加锁 225004e86ffSlogin let mut inode: SpinLockGuard<RamFSInode> = self.0.lock(); 226004e86ffSlogin 227004e86ffSlogin // 检查当前inode是否为一个文件夹,如果是的话,就返回错误 228004e86ffSlogin if inode.metadata.file_type == FileType::Dir { 229676b8ef6SMork return Err(SystemError::EISDIR); 230004e86ffSlogin } 231004e86ffSlogin 232004e86ffSlogin let data: &mut Vec<u8> = &mut inode.data; 233004e86ffSlogin 234004e86ffSlogin // 如果文件大小比原来的大,那就resize这个数组 235004e86ffSlogin if offset + len > data.len() { 236004e86ffSlogin data.resize(offset + len, 0); 237004e86ffSlogin } 238004e86ffSlogin 239004e86ffSlogin let target = &mut data[offset..offset + len]; 240004e86ffSlogin target.copy_from_slice(&buf[0..len]); 241004e86ffSlogin return Ok(len); 242004e86ffSlogin } 243004e86ffSlogin 244004e86ffSlogin fn fs(&self) -> Arc<dyn FileSystem> { 245004e86ffSlogin return self.0.lock().fs.upgrade().unwrap(); 246004e86ffSlogin } 247004e86ffSlogin 248004e86ffSlogin fn as_any_ref(&self) -> &dyn core::any::Any { 249004e86ffSlogin self 250004e86ffSlogin } 251004e86ffSlogin 252676b8ef6SMork fn metadata(&self) -> Result<Metadata, SystemError> { 253004e86ffSlogin let inode = self.0.lock(); 254004e86ffSlogin let mut metadata = inode.metadata.clone(); 255004e86ffSlogin metadata.size = inode.data.len() as i64; 256004e86ffSlogin 257004e86ffSlogin return Ok(metadata); 258004e86ffSlogin } 259004e86ffSlogin 260676b8ef6SMork fn set_metadata(&self, metadata: &Metadata) -> Result<(), SystemError> { 261004e86ffSlogin let mut inode = self.0.lock(); 262004e86ffSlogin inode.metadata.atime = metadata.atime; 263004e86ffSlogin inode.metadata.mtime = metadata.mtime; 264004e86ffSlogin inode.metadata.ctime = metadata.ctime; 265004e86ffSlogin inode.metadata.mode = metadata.mode; 266004e86ffSlogin inode.metadata.uid = metadata.uid; 267004e86ffSlogin inode.metadata.gid = metadata.gid; 268004e86ffSlogin 269004e86ffSlogin return Ok(()); 270004e86ffSlogin } 271004e86ffSlogin 272676b8ef6SMork fn resize(&self, len: usize) -> Result<(), SystemError> { 273004e86ffSlogin let mut inode = self.0.lock(); 274004e86ffSlogin if inode.metadata.file_type == FileType::File { 275004e86ffSlogin inode.data.resize(len, 0); 276004e86ffSlogin return Ok(()); 277004e86ffSlogin } else { 278676b8ef6SMork return Err(SystemError::EINVAL); 279004e86ffSlogin } 280004e86ffSlogin } 281004e86ffSlogin 282004e86ffSlogin fn create_with_data( 283004e86ffSlogin &self, 284004e86ffSlogin name: &str, 285004e86ffSlogin file_type: FileType, 2866b4e7a29SLoGin mode: ModeType, 287004e86ffSlogin data: usize, 288676b8ef6SMork ) -> Result<Arc<dyn IndexNode>, SystemError> { 289004e86ffSlogin // 获取当前inode 290004e86ffSlogin let mut inode = self.0.lock(); 291004e86ffSlogin // 如果当前inode不是文件夹,则返回 292004e86ffSlogin if inode.metadata.file_type != FileType::Dir { 293676b8ef6SMork return Err(SystemError::ENOTDIR); 294004e86ffSlogin } 295004e86ffSlogin // 如果有重名的,则返回 296004e86ffSlogin if inode.children.contains_key(name) { 297676b8ef6SMork return Err(SystemError::EEXIST); 298004e86ffSlogin } 299004e86ffSlogin 300004e86ffSlogin // 创建inode 301004e86ffSlogin let result: Arc<LockedRamFSInode> = Arc::new(LockedRamFSInode(SpinLock::new(RamFSInode { 302004e86ffSlogin parent: inode.self_ref.clone(), 303004e86ffSlogin self_ref: Weak::default(), 304004e86ffSlogin children: BTreeMap::new(), 305004e86ffSlogin data: Vec::new(), 306004e86ffSlogin metadata: Metadata { 307004e86ffSlogin dev_id: 0, 308004e86ffSlogin inode_id: generate_inode_id(), 309004e86ffSlogin size: 0, 310004e86ffSlogin blk_size: 0, 311004e86ffSlogin blocks: 0, 312004e86ffSlogin atime: TimeSpec::default(), 313004e86ffSlogin mtime: TimeSpec::default(), 314004e86ffSlogin ctime: TimeSpec::default(), 315b5b571e0SLoGin file_type, 316b5b571e0SLoGin mode, 317004e86ffSlogin nlinks: 1, 318004e86ffSlogin uid: 0, 319004e86ffSlogin gid: 0, 32002343d0bSLoGin raw_dev: DeviceNumber::from(data as u32), 321004e86ffSlogin }, 322004e86ffSlogin fs: inode.fs.clone(), 3232dbef785SGnoCiYeH special_node: None, 324004e86ffSlogin }))); 325004e86ffSlogin 326004e86ffSlogin // 初始化inode的自引用的weak指针 327004e86ffSlogin result.0.lock().self_ref = Arc::downgrade(&result); 328004e86ffSlogin 329004e86ffSlogin // 将子inode插入父inode的B树中 330004e86ffSlogin inode.children.insert(String::from(name), result.clone()); 331004e86ffSlogin 332004e86ffSlogin return Ok(result); 333004e86ffSlogin } 334004e86ffSlogin 335676b8ef6SMork fn link(&self, name: &str, other: &Arc<dyn IndexNode>) -> Result<(), SystemError> { 336004e86ffSlogin let other: &LockedRamFSInode = other 337004e86ffSlogin .downcast_ref::<LockedRamFSInode>() 338676b8ef6SMork .ok_or(SystemError::EPERM)?; 339004e86ffSlogin let mut inode: SpinLockGuard<RamFSInode> = self.0.lock(); 340004e86ffSlogin let mut other_locked: SpinLockGuard<RamFSInode> = other.0.lock(); 341004e86ffSlogin 342004e86ffSlogin // 如果当前inode不是文件夹,那么报错 343004e86ffSlogin if inode.metadata.file_type != FileType::Dir { 344676b8ef6SMork return Err(SystemError::ENOTDIR); 345004e86ffSlogin } 346004e86ffSlogin 347004e86ffSlogin // 如果另一个inode是文件夹,那么也报错 348004e86ffSlogin if other_locked.metadata.file_type == FileType::Dir { 349676b8ef6SMork return Err(SystemError::EISDIR); 350004e86ffSlogin } 351004e86ffSlogin 352004e86ffSlogin // 如果当前文件夹下已经有同名文件,也报错。 353004e86ffSlogin if inode.children.contains_key(name) { 354676b8ef6SMork return Err(SystemError::EEXIST); 355004e86ffSlogin } 356004e86ffSlogin 357004e86ffSlogin inode 358004e86ffSlogin .children 359004e86ffSlogin .insert(String::from(name), other_locked.self_ref.upgrade().unwrap()); 360004e86ffSlogin 361004e86ffSlogin // 增加硬链接计数 362004e86ffSlogin other_locked.metadata.nlinks += 1; 363004e86ffSlogin return Ok(()); 364004e86ffSlogin } 365004e86ffSlogin 366676b8ef6SMork fn unlink(&self, name: &str) -> Result<(), SystemError> { 367004e86ffSlogin let mut inode: SpinLockGuard<RamFSInode> = self.0.lock(); 368004e86ffSlogin // 如果当前inode不是目录,那么也没有子目录/文件的概念了,因此要求当前inode的类型是目录 369004e86ffSlogin if inode.metadata.file_type != FileType::Dir { 370676b8ef6SMork return Err(SystemError::ENOTDIR); 371004e86ffSlogin } 372004e86ffSlogin // 不允许删除当前文件夹,也不允许删除上一个目录 373004e86ffSlogin if name == "." || name == ".." { 374676b8ef6SMork return Err(SystemError::ENOTEMPTY); 375004e86ffSlogin } 376004e86ffSlogin 377004e86ffSlogin // 获得要删除的文件的inode 378676b8ef6SMork let to_delete = inode.children.get(name).ok_or(SystemError::ENOENT)?; 379004e86ffSlogin if to_delete.0.lock().metadata.file_type == FileType::Dir { 380676b8ef6SMork return Err(SystemError::EPERM); 381004e86ffSlogin } 382004e86ffSlogin // 减少硬链接计数 383004e86ffSlogin to_delete.0.lock().metadata.nlinks -= 1; 384004e86ffSlogin // 在当前目录中删除这个子目录项 385004e86ffSlogin inode.children.remove(name); 386004e86ffSlogin return Ok(()); 387004e86ffSlogin } 388004e86ffSlogin 389676b8ef6SMork fn rmdir(&self, name: &str) -> Result<(), SystemError> { 390004e86ffSlogin let mut inode: SpinLockGuard<RamFSInode> = self.0.lock(); 391004e86ffSlogin // 如果当前inode不是目录,那么也没有子目录/文件的概念了,因此要求当前inode的类型是目录 392004e86ffSlogin if inode.metadata.file_type != FileType::Dir { 393676b8ef6SMork return Err(SystemError::ENOTDIR); 394004e86ffSlogin } 395004e86ffSlogin // 获得要删除的文件夹的inode 396676b8ef6SMork let to_delete = inode.children.get(name).ok_or(SystemError::ENOENT)?; 397004e86ffSlogin if to_delete.0.lock().metadata.file_type != FileType::Dir { 398676b8ef6SMork return Err(SystemError::ENOTDIR); 399004e86ffSlogin } 400004e86ffSlogin 401004e86ffSlogin to_delete.0.lock().metadata.nlinks -= 1; 402004e86ffSlogin // 在当前目录中删除这个子目录项 403004e86ffSlogin inode.children.remove(name); 404004e86ffSlogin return Ok(()); 405004e86ffSlogin } 406004e86ffSlogin 4079e481b3bSTTaq fn move_to( 408004e86ffSlogin &self, 409004e86ffSlogin old_name: &str, 410004e86ffSlogin target: &Arc<dyn IndexNode>, 411004e86ffSlogin new_name: &str, 412676b8ef6SMork ) -> Result<(), SystemError> { 413004e86ffSlogin let old_inode: Arc<dyn IndexNode> = self.find(old_name)?; 414004e86ffSlogin 415004e86ffSlogin // 在新的目录下创建一个硬链接 416004e86ffSlogin target.link(new_name, &old_inode)?; 417004e86ffSlogin // 取消现有的目录下的这个硬链接 418004e86ffSlogin if let Err(err) = self.unlink(old_name) { 419004e86ffSlogin // 如果取消失败,那就取消新的目录下的硬链接 420004e86ffSlogin target.unlink(new_name)?; 421004e86ffSlogin return Err(err); 422004e86ffSlogin } 423004e86ffSlogin return Ok(()); 424004e86ffSlogin } 425004e86ffSlogin 426676b8ef6SMork fn find(&self, name: &str) -> Result<Arc<dyn IndexNode>, SystemError> { 427004e86ffSlogin let inode = self.0.lock(); 428004e86ffSlogin 429004e86ffSlogin if inode.metadata.file_type != FileType::Dir { 430676b8ef6SMork return Err(SystemError::ENOTDIR); 431004e86ffSlogin } 432004e86ffSlogin 433004e86ffSlogin match name { 434004e86ffSlogin "" | "." => { 435676b8ef6SMork return Ok(inode.self_ref.upgrade().ok_or(SystemError::ENOENT)?); 436004e86ffSlogin } 437004e86ffSlogin 438004e86ffSlogin ".." => { 439676b8ef6SMork return Ok(inode.parent.upgrade().ok_or(SystemError::ENOENT)?); 440004e86ffSlogin } 441004e86ffSlogin name => { 442004e86ffSlogin // 在子目录项中查找 443676b8ef6SMork return Ok(inode.children.get(name).ok_or(SystemError::ENOENT)?.clone()); 444004e86ffSlogin } 445004e86ffSlogin } 446004e86ffSlogin } 447004e86ffSlogin 448676b8ef6SMork fn get_entry_name(&self, ino: InodeId) -> Result<String, SystemError> { 449004e86ffSlogin let inode: SpinLockGuard<RamFSInode> = self.0.lock(); 450004e86ffSlogin if inode.metadata.file_type != FileType::Dir { 451676b8ef6SMork return Err(SystemError::ENOTDIR); 452004e86ffSlogin } 453004e86ffSlogin 4546b4e7a29SLoGin match ino.into() { 455004e86ffSlogin 0 => { 456004e86ffSlogin return Ok(String::from(".")); 457004e86ffSlogin } 458004e86ffSlogin 1 => { 459004e86ffSlogin return Ok(String::from("..")); 460004e86ffSlogin } 461004e86ffSlogin ino => { 462004e86ffSlogin // 暴力遍历所有的children,判断inode id是否相同 463004e86ffSlogin // TODO: 优化这里,这个地方性能很差! 464004e86ffSlogin let mut key: Vec<String> = inode 465004e86ffSlogin .children 466004e86ffSlogin .keys() 4676b4e7a29SLoGin .filter(|k| { 4686b4e7a29SLoGin inode 4696b4e7a29SLoGin .children 4706b4e7a29SLoGin .get(*k) 4716b4e7a29SLoGin .unwrap() 4726b4e7a29SLoGin .0 4736b4e7a29SLoGin .lock() 4746b4e7a29SLoGin .metadata 4756b4e7a29SLoGin .inode_id 4766b4e7a29SLoGin .into() 4776b4e7a29SLoGin == ino 4786b4e7a29SLoGin }) 479004e86ffSlogin .cloned() 480004e86ffSlogin .collect(); 481004e86ffSlogin 482004e86ffSlogin match key.len() { 483676b8ef6SMork 0=>{return Err(SystemError::ENOENT);} 484004e86ffSlogin 1=>{return Ok(key.remove(0));} 4856b4e7a29SLoGin _ => 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) 486004e86ffSlogin } 487004e86ffSlogin } 488004e86ffSlogin } 489004e86ffSlogin } 490004e86ffSlogin 491676b8ef6SMork fn list(&self) -> Result<Vec<String>, SystemError> { 492004e86ffSlogin let info = self.metadata()?; 493004e86ffSlogin if info.file_type != FileType::Dir { 494676b8ef6SMork return Err(SystemError::ENOTDIR); 495004e86ffSlogin } 496004e86ffSlogin 497004e86ffSlogin let mut keys: Vec<String> = Vec::new(); 498004e86ffSlogin keys.push(String::from(".")); 499004e86ffSlogin keys.push(String::from("..")); 500004e86ffSlogin keys.append(&mut self.0.lock().children.keys().cloned().collect()); 501004e86ffSlogin 502004e86ffSlogin return Ok(keys); 503004e86ffSlogin } 5042dbef785SGnoCiYeH 5052dbef785SGnoCiYeH fn mknod( 5062dbef785SGnoCiYeH &self, 5072dbef785SGnoCiYeH filename: &str, 5082dbef785SGnoCiYeH mode: ModeType, 509c566df45SLoGin _dev_t: DeviceNumber, 5102dbef785SGnoCiYeH ) -> Result<Arc<dyn IndexNode>, SystemError> { 5112dbef785SGnoCiYeH let mut inode = self.0.lock(); 5122dbef785SGnoCiYeH if inode.metadata.file_type != FileType::Dir { 5132dbef785SGnoCiYeH return Err(SystemError::ENOTDIR); 5142dbef785SGnoCiYeH } 5152dbef785SGnoCiYeH 5162dbef785SGnoCiYeH // 判断需要创建的类型 5172dbef785SGnoCiYeH if unlikely(mode.contains(ModeType::S_IFREG)) { 5182dbef785SGnoCiYeH // 普通文件 519b5b571e0SLoGin return self.create(filename, FileType::File, mode); 5202dbef785SGnoCiYeH } 5212dbef785SGnoCiYeH 5222dbef785SGnoCiYeH let nod = Arc::new(LockedRamFSInode(SpinLock::new(RamFSInode { 5232dbef785SGnoCiYeH parent: inode.self_ref.clone(), 5242dbef785SGnoCiYeH self_ref: Weak::default(), 5252dbef785SGnoCiYeH children: BTreeMap::new(), 5262dbef785SGnoCiYeH data: Vec::new(), 5272dbef785SGnoCiYeH metadata: Metadata { 5282dbef785SGnoCiYeH dev_id: 0, 5292dbef785SGnoCiYeH inode_id: generate_inode_id(), 5302dbef785SGnoCiYeH size: 0, 5312dbef785SGnoCiYeH blk_size: 0, 5322dbef785SGnoCiYeH blocks: 0, 5332dbef785SGnoCiYeH atime: TimeSpec::default(), 5342dbef785SGnoCiYeH mtime: TimeSpec::default(), 5352dbef785SGnoCiYeH ctime: TimeSpec::default(), 5362dbef785SGnoCiYeH file_type: FileType::Pipe, 537b5b571e0SLoGin mode, 5382dbef785SGnoCiYeH nlinks: 1, 5392dbef785SGnoCiYeH uid: 0, 5402dbef785SGnoCiYeH gid: 0, 54102343d0bSLoGin raw_dev: DeviceNumber::default(), 5422dbef785SGnoCiYeH }, 5432dbef785SGnoCiYeH fs: inode.fs.clone(), 5442dbef785SGnoCiYeH special_node: None, 5452dbef785SGnoCiYeH }))); 5462dbef785SGnoCiYeH 5472dbef785SGnoCiYeH nod.0.lock().self_ref = Arc::downgrade(&nod); 5482dbef785SGnoCiYeH 5492dbef785SGnoCiYeH if mode.contains(ModeType::S_IFIFO) { 5502dbef785SGnoCiYeH nod.0.lock().metadata.file_type = FileType::Pipe; 5512dbef785SGnoCiYeH // 创建pipe文件 5522dbef785SGnoCiYeH let pipe_inode = LockedPipeInode::new(); 5532dbef785SGnoCiYeH // 设置special_node 5542dbef785SGnoCiYeH nod.0.lock().special_node = Some(SpecialNodeData::Pipe(pipe_inode)); 5552dbef785SGnoCiYeH } else if mode.contains(ModeType::S_IFBLK) { 5562dbef785SGnoCiYeH nod.0.lock().metadata.file_type = FileType::BlockDevice; 5572dbef785SGnoCiYeH unimplemented!() 5582dbef785SGnoCiYeH } else if mode.contains(ModeType::S_IFCHR) { 5592dbef785SGnoCiYeH nod.0.lock().metadata.file_type = FileType::CharDevice; 5602dbef785SGnoCiYeH unimplemented!() 5612dbef785SGnoCiYeH } 5622dbef785SGnoCiYeH 5632dbef785SGnoCiYeH inode 5642dbef785SGnoCiYeH .children 5652dbef785SGnoCiYeH .insert(String::from(filename).to_uppercase(), nod.clone()); 5662dbef785SGnoCiYeH Ok(nod) 5672dbef785SGnoCiYeH } 5682dbef785SGnoCiYeH 5692dbef785SGnoCiYeH fn special_node(&self) -> Option<super::vfs::SpecialNodeData> { 5702dbef785SGnoCiYeH return self.0.lock().special_node.clone(); 5712dbef785SGnoCiYeH } 5729e481b3bSTTaq 5739e481b3bSTTaq /// # 用于重命名内存中的文件或目录 5749e481b3bSTTaq fn rename(&self, _old_name: &str, _new_name: &str) -> Result<(), SystemError> { 5759e481b3bSTTaq let old_inode: Arc<dyn IndexNode> = self.find(_old_name)?; 5769e481b3bSTTaq // 在新的目录下创建一个硬链接 5779e481b3bSTTaq self.link(_new_name, &old_inode)?; 5789e481b3bSTTaq 5799e481b3bSTTaq // 取消现有的目录下的这个硬链接 5809e481b3bSTTaq if let Err(err) = self.unlink(_old_name) { 5819e481b3bSTTaq // 如果取消失败,那就取消新的目录下的硬链接 5829e481b3bSTTaq self.unlink(_new_name)?; 5839e481b3bSTTaq return Err(err); 5849e481b3bSTTaq } 5859e481b3bSTTaq 5869e481b3bSTTaq return Ok(()); 5879e481b3bSTTaq } 588004e86ffSlogin } 589