1004e86ffSlogin use core::any::Any; 22dbef785SGnoCiYeH use core::intrinsics::unlikely; 3004e86ffSlogin 4*1d37ca6dSDonkey Kane use crate::filesystem::vfs::FSMAKER; 5*1d37ca6dSDonkey Kane use crate::{ 6*1d37ca6dSDonkey Kane driver::base::device::device_number::DeviceNumber, 7*1d37ca6dSDonkey Kane filesystem::vfs::{core::generate_inode_id, FileType}, 8*1d37ca6dSDonkey Kane ipc::pipe::LockedPipeInode, 9*1d37ca6dSDonkey Kane libs::spinlock::{SpinLock, SpinLockGuard}, 10*1d37ca6dSDonkey Kane time::TimeSpec, 11*1d37ca6dSDonkey Kane }; 12004e86ffSlogin use alloc::{ 13004e86ffSlogin collections::BTreeMap, 14004e86ffSlogin string::String, 15004e86ffSlogin sync::{Arc, Weak}, 16004e86ffSlogin vec::Vec, 17004e86ffSlogin }; 1891e9d4abSLoGin use system_error::SystemError; 19004e86ffSlogin 20004e86ffSlogin use super::vfs::{ 21*1d37ca6dSDonkey Kane file::FilePrivateData, syscall::ModeType, FileSystem, FileSystemMaker, FsInfo, IndexNode, 22*1d37ca6dSDonkey Kane InodeId, Metadata, SpecialNodeData, 23004e86ffSlogin }; 24004e86ffSlogin 25004e86ffSlogin /// RamFS的inode名称的最大长度 26004e86ffSlogin const RAMFS_MAX_NAMELEN: usize = 64; 27004e86ffSlogin 28004e86ffSlogin /// @brief 内存文件系统的Inode结构体 29004e86ffSlogin #[derive(Debug)] 30004e86ffSlogin struct LockedRamFSInode(SpinLock<RamFSInode>); 31004e86ffSlogin 32004e86ffSlogin /// @brief 内存文件系统结构体 33004e86ffSlogin #[derive(Debug)] 34004e86ffSlogin pub struct RamFS { 35004e86ffSlogin /// RamFS的root inode 36004e86ffSlogin root_inode: Arc<LockedRamFSInode>, 37004e86ffSlogin } 38004e86ffSlogin 39004e86ffSlogin /// @brief 内存文件系统的Inode结构体(不包含锁) 40004e86ffSlogin #[derive(Debug)] 41004e86ffSlogin pub struct RamFSInode { 42004e86ffSlogin // parent变量目前只在find函数中使用到 43004e86ffSlogin // 所以只有当inode是文件夹的时候,parent才会生效 44004e86ffSlogin // 对于文件来说,parent就没什么作用了 45004e86ffSlogin // 关于parent的说明: 目录不允许有硬链接 46004e86ffSlogin /// 指向父Inode的弱引用 47004e86ffSlogin parent: Weak<LockedRamFSInode>, 48004e86ffSlogin /// 指向自身的弱引用 49004e86ffSlogin self_ref: Weak<LockedRamFSInode>, 50004e86ffSlogin /// 子Inode的B树 51004e86ffSlogin children: BTreeMap<String, Arc<LockedRamFSInode>>, 52004e86ffSlogin /// 当前inode的数据部分 53004e86ffSlogin data: Vec<u8>, 54004e86ffSlogin /// 当前inode的元数据 55004e86ffSlogin metadata: Metadata, 56004e86ffSlogin /// 指向inode所在的文件系统对象的指针 57004e86ffSlogin fs: Weak<RamFS>, 582dbef785SGnoCiYeH /// 指向特殊节点 592dbef785SGnoCiYeH special_node: Option<SpecialNodeData>, 60004e86ffSlogin } 61004e86ffSlogin 62004e86ffSlogin impl FileSystem for RamFS { 63004e86ffSlogin fn root_inode(&self) -> Arc<dyn super::vfs::IndexNode> { 64004e86ffSlogin return self.root_inode.clone(); 65004e86ffSlogin } 66004e86ffSlogin 67004e86ffSlogin fn info(&self) -> FsInfo { 68004e86ffSlogin return FsInfo { 69004e86ffSlogin blk_dev_id: 0, 70004e86ffSlogin max_name_len: RAMFS_MAX_NAMELEN, 71004e86ffSlogin }; 72004e86ffSlogin } 73004e86ffSlogin 74004e86ffSlogin /// @brief 本函数用于实现动态转换。 75004e86ffSlogin /// 具体的文件系统在实现本函数时,最简单的方式就是:直接返回self 76004e86ffSlogin fn as_any_ref(&self) -> &dyn Any { 77004e86ffSlogin self 78004e86ffSlogin } 79*1d37ca6dSDonkey Kane 80*1d37ca6dSDonkey Kane fn name(&self) -> &str { 81*1d37ca6dSDonkey Kane "ramfs" 82*1d37ca6dSDonkey Kane } 83004e86ffSlogin } 84004e86ffSlogin 85004e86ffSlogin impl RamFS { 86004e86ffSlogin pub fn new() -> Arc<Self> { 87004e86ffSlogin // 初始化root inode 88004e86ffSlogin let root: Arc<LockedRamFSInode> = Arc::new(LockedRamFSInode(SpinLock::new(RamFSInode { 89004e86ffSlogin parent: Weak::default(), 90004e86ffSlogin self_ref: Weak::default(), 91004e86ffSlogin children: BTreeMap::new(), 92004e86ffSlogin data: Vec::new(), 93004e86ffSlogin metadata: Metadata { 94004e86ffSlogin dev_id: 0, 95004e86ffSlogin inode_id: generate_inode_id(), 96004e86ffSlogin size: 0, 97004e86ffSlogin blk_size: 0, 98004e86ffSlogin blocks: 0, 99004e86ffSlogin atime: TimeSpec::default(), 100004e86ffSlogin mtime: TimeSpec::default(), 101004e86ffSlogin ctime: TimeSpec::default(), 102004e86ffSlogin file_type: FileType::Dir, 1036b4e7a29SLoGin mode: ModeType::from_bits_truncate(0o777), 104004e86ffSlogin nlinks: 1, 105004e86ffSlogin uid: 0, 106004e86ffSlogin gid: 0, 10702343d0bSLoGin raw_dev: DeviceNumber::default(), 108004e86ffSlogin }, 109004e86ffSlogin fs: Weak::default(), 1102dbef785SGnoCiYeH special_node: None, 111004e86ffSlogin }))); 112004e86ffSlogin 113004e86ffSlogin let result: Arc<RamFS> = Arc::new(RamFS { root_inode: root }); 114004e86ffSlogin 115004e86ffSlogin // 对root inode加锁,并继续完成初始化工作 116004e86ffSlogin let mut root_guard: SpinLockGuard<RamFSInode> = result.root_inode.0.lock(); 117004e86ffSlogin root_guard.parent = Arc::downgrade(&result.root_inode); 118004e86ffSlogin root_guard.self_ref = Arc::downgrade(&result.root_inode); 119004e86ffSlogin root_guard.fs = Arc::downgrade(&result); 120004e86ffSlogin // 释放锁 121004e86ffSlogin drop(root_guard); 122004e86ffSlogin 123004e86ffSlogin return result; 124004e86ffSlogin } 125*1d37ca6dSDonkey Kane 126*1d37ca6dSDonkey Kane pub fn make_ramfs() -> Result<Arc<dyn FileSystem + 'static>, SystemError> { 127*1d37ca6dSDonkey Kane let fs = RamFS::new(); 128*1d37ca6dSDonkey Kane return Ok(fs); 129004e86ffSlogin } 130*1d37ca6dSDonkey Kane } 131*1d37ca6dSDonkey Kane 132*1d37ca6dSDonkey Kane #[distributed_slice(FSMAKER)] 133*1d37ca6dSDonkey Kane static RAMFSMAKER: FileSystemMaker = FileSystemMaker::new( 134*1d37ca6dSDonkey Kane "ramfs", 135*1d37ca6dSDonkey Kane &(RamFS::make_ramfs as fn() -> Result<Arc<dyn FileSystem + 'static>, SystemError>), 136*1d37ca6dSDonkey Kane ); 137004e86ffSlogin 138004e86ffSlogin impl IndexNode for LockedRamFSInode { 139a3ef8f8aSGnoCiYeH fn truncate(&self, len: usize) -> Result<(), SystemError> { 140a3ef8f8aSGnoCiYeH let mut inode = self.0.lock(); 141a3ef8f8aSGnoCiYeH 142a3ef8f8aSGnoCiYeH //如果是文件夹,则报错 143a3ef8f8aSGnoCiYeH if inode.metadata.file_type == FileType::Dir { 144a3ef8f8aSGnoCiYeH return Err(SystemError::EINVAL); 145a3ef8f8aSGnoCiYeH } 146a3ef8f8aSGnoCiYeH 147a3ef8f8aSGnoCiYeH //当前文件长度大于_len才进行截断,否则不操作 148a3ef8f8aSGnoCiYeH if inode.data.len() > len { 149a3ef8f8aSGnoCiYeH inode.data.resize(len, 0); 150a3ef8f8aSGnoCiYeH } 151a3ef8f8aSGnoCiYeH return Ok(()); 152a3ef8f8aSGnoCiYeH } 153a3ef8f8aSGnoCiYeH 154a3ef8f8aSGnoCiYeH fn close(&self, _data: &mut FilePrivateData) -> Result<(), SystemError> { 155a3ef8f8aSGnoCiYeH return Ok(()); 156a3ef8f8aSGnoCiYeH } 157a3ef8f8aSGnoCiYeH 158a3ef8f8aSGnoCiYeH fn open( 159a3ef8f8aSGnoCiYeH &self, 160a3ef8f8aSGnoCiYeH _data: &mut FilePrivateData, 161a3ef8f8aSGnoCiYeH _mode: &super::vfs::file::FileMode, 162a3ef8f8aSGnoCiYeH ) -> Result<(), SystemError> { 163a3ef8f8aSGnoCiYeH return Ok(()); 164a3ef8f8aSGnoCiYeH } 165a3ef8f8aSGnoCiYeH 166004e86ffSlogin fn read_at( 167004e86ffSlogin &self, 168004e86ffSlogin offset: usize, 169004e86ffSlogin len: usize, 170004e86ffSlogin buf: &mut [u8], 171004e86ffSlogin _data: &mut FilePrivateData, 172676b8ef6SMork ) -> Result<usize, SystemError> { 173004e86ffSlogin if buf.len() < len { 174676b8ef6SMork return Err(SystemError::EINVAL); 175004e86ffSlogin } 176004e86ffSlogin // 加锁 177004e86ffSlogin let inode: SpinLockGuard<RamFSInode> = self.0.lock(); 178004e86ffSlogin 179004e86ffSlogin // 检查当前inode是否为一个文件夹,如果是的话,就返回错误 180004e86ffSlogin if inode.metadata.file_type == FileType::Dir { 181676b8ef6SMork return Err(SystemError::EISDIR); 182004e86ffSlogin } 183004e86ffSlogin 184004e86ffSlogin let start = inode.data.len().min(offset); 185004e86ffSlogin let end = inode.data.len().min(offset + len); 186004e86ffSlogin 187004e86ffSlogin // buffer空间不足 188004e86ffSlogin if buf.len() < (end - start) { 189676b8ef6SMork return Err(SystemError::ENOBUFS); 190004e86ffSlogin } 191004e86ffSlogin 192004e86ffSlogin // 拷贝数据 193004e86ffSlogin let src = &inode.data[start..end]; 194004e86ffSlogin buf[0..src.len()].copy_from_slice(src); 195004e86ffSlogin return Ok(src.len()); 196004e86ffSlogin } 197004e86ffSlogin 198004e86ffSlogin fn write_at( 199004e86ffSlogin &self, 200004e86ffSlogin offset: usize, 201004e86ffSlogin len: usize, 202004e86ffSlogin buf: &[u8], 203004e86ffSlogin _data: &mut FilePrivateData, 204676b8ef6SMork ) -> Result<usize, SystemError> { 205004e86ffSlogin if buf.len() < len { 206676b8ef6SMork return Err(SystemError::EINVAL); 207004e86ffSlogin } 208004e86ffSlogin 209004e86ffSlogin // 加锁 210004e86ffSlogin let mut inode: SpinLockGuard<RamFSInode> = self.0.lock(); 211004e86ffSlogin 212004e86ffSlogin // 检查当前inode是否为一个文件夹,如果是的话,就返回错误 213004e86ffSlogin if inode.metadata.file_type == FileType::Dir { 214676b8ef6SMork return Err(SystemError::EISDIR); 215004e86ffSlogin } 216004e86ffSlogin 217004e86ffSlogin let data: &mut Vec<u8> = &mut inode.data; 218004e86ffSlogin 219004e86ffSlogin // 如果文件大小比原来的大,那就resize这个数组 220004e86ffSlogin if offset + len > data.len() { 221004e86ffSlogin data.resize(offset + len, 0); 222004e86ffSlogin } 223004e86ffSlogin 224004e86ffSlogin let target = &mut data[offset..offset + len]; 225004e86ffSlogin target.copy_from_slice(&buf[0..len]); 226004e86ffSlogin return Ok(len); 227004e86ffSlogin } 228004e86ffSlogin 229004e86ffSlogin fn fs(&self) -> Arc<dyn FileSystem> { 230004e86ffSlogin return self.0.lock().fs.upgrade().unwrap(); 231004e86ffSlogin } 232004e86ffSlogin 233004e86ffSlogin fn as_any_ref(&self) -> &dyn core::any::Any { 234004e86ffSlogin self 235004e86ffSlogin } 236004e86ffSlogin 237676b8ef6SMork fn metadata(&self) -> Result<Metadata, SystemError> { 238004e86ffSlogin let inode = self.0.lock(); 239004e86ffSlogin let mut metadata = inode.metadata.clone(); 240004e86ffSlogin metadata.size = inode.data.len() as i64; 241004e86ffSlogin 242004e86ffSlogin return Ok(metadata); 243004e86ffSlogin } 244004e86ffSlogin 245676b8ef6SMork fn set_metadata(&self, metadata: &Metadata) -> Result<(), SystemError> { 246004e86ffSlogin let mut inode = self.0.lock(); 247004e86ffSlogin inode.metadata.atime = metadata.atime; 248004e86ffSlogin inode.metadata.mtime = metadata.mtime; 249004e86ffSlogin inode.metadata.ctime = metadata.ctime; 250004e86ffSlogin inode.metadata.mode = metadata.mode; 251004e86ffSlogin inode.metadata.uid = metadata.uid; 252004e86ffSlogin inode.metadata.gid = metadata.gid; 253004e86ffSlogin 254004e86ffSlogin return Ok(()); 255004e86ffSlogin } 256004e86ffSlogin 257676b8ef6SMork fn resize(&self, len: usize) -> Result<(), SystemError> { 258004e86ffSlogin let mut inode = self.0.lock(); 259004e86ffSlogin if inode.metadata.file_type == FileType::File { 260004e86ffSlogin inode.data.resize(len, 0); 261004e86ffSlogin return Ok(()); 262004e86ffSlogin } else { 263676b8ef6SMork return Err(SystemError::EINVAL); 264004e86ffSlogin } 265004e86ffSlogin } 266004e86ffSlogin 267004e86ffSlogin fn create_with_data( 268004e86ffSlogin &self, 269004e86ffSlogin name: &str, 270004e86ffSlogin file_type: FileType, 2716b4e7a29SLoGin mode: ModeType, 272004e86ffSlogin data: usize, 273676b8ef6SMork ) -> Result<Arc<dyn IndexNode>, SystemError> { 274004e86ffSlogin // 获取当前inode 275004e86ffSlogin let mut inode = self.0.lock(); 276004e86ffSlogin // 如果当前inode不是文件夹,则返回 277004e86ffSlogin if inode.metadata.file_type != FileType::Dir { 278676b8ef6SMork return Err(SystemError::ENOTDIR); 279004e86ffSlogin } 280004e86ffSlogin // 如果有重名的,则返回 281004e86ffSlogin if inode.children.contains_key(name) { 282676b8ef6SMork return Err(SystemError::EEXIST); 283004e86ffSlogin } 284004e86ffSlogin 285004e86ffSlogin // 创建inode 286004e86ffSlogin let result: Arc<LockedRamFSInode> = Arc::new(LockedRamFSInode(SpinLock::new(RamFSInode { 287004e86ffSlogin parent: inode.self_ref.clone(), 288004e86ffSlogin self_ref: Weak::default(), 289004e86ffSlogin children: BTreeMap::new(), 290004e86ffSlogin data: Vec::new(), 291004e86ffSlogin metadata: Metadata { 292004e86ffSlogin dev_id: 0, 293004e86ffSlogin inode_id: generate_inode_id(), 294004e86ffSlogin size: 0, 295004e86ffSlogin blk_size: 0, 296004e86ffSlogin blocks: 0, 297004e86ffSlogin atime: TimeSpec::default(), 298004e86ffSlogin mtime: TimeSpec::default(), 299004e86ffSlogin ctime: TimeSpec::default(), 300004e86ffSlogin file_type: file_type, 301004e86ffSlogin mode: mode, 302004e86ffSlogin nlinks: 1, 303004e86ffSlogin uid: 0, 304004e86ffSlogin gid: 0, 30502343d0bSLoGin raw_dev: DeviceNumber::from(data as u32), 306004e86ffSlogin }, 307004e86ffSlogin fs: inode.fs.clone(), 3082dbef785SGnoCiYeH special_node: None, 309004e86ffSlogin }))); 310004e86ffSlogin 311004e86ffSlogin // 初始化inode的自引用的weak指针 312004e86ffSlogin result.0.lock().self_ref = Arc::downgrade(&result); 313004e86ffSlogin 314004e86ffSlogin // 将子inode插入父inode的B树中 315004e86ffSlogin inode.children.insert(String::from(name), result.clone()); 316004e86ffSlogin 317004e86ffSlogin return Ok(result); 318004e86ffSlogin } 319004e86ffSlogin 320676b8ef6SMork fn link(&self, name: &str, other: &Arc<dyn IndexNode>) -> Result<(), SystemError> { 321004e86ffSlogin let other: &LockedRamFSInode = other 322004e86ffSlogin .downcast_ref::<LockedRamFSInode>() 323676b8ef6SMork .ok_or(SystemError::EPERM)?; 324004e86ffSlogin let mut inode: SpinLockGuard<RamFSInode> = self.0.lock(); 325004e86ffSlogin let mut other_locked: SpinLockGuard<RamFSInode> = other.0.lock(); 326004e86ffSlogin 327004e86ffSlogin // 如果当前inode不是文件夹,那么报错 328004e86ffSlogin if inode.metadata.file_type != FileType::Dir { 329676b8ef6SMork return Err(SystemError::ENOTDIR); 330004e86ffSlogin } 331004e86ffSlogin 332004e86ffSlogin // 如果另一个inode是文件夹,那么也报错 333004e86ffSlogin if other_locked.metadata.file_type == FileType::Dir { 334676b8ef6SMork return Err(SystemError::EISDIR); 335004e86ffSlogin } 336004e86ffSlogin 337004e86ffSlogin // 如果当前文件夹下已经有同名文件,也报错。 338004e86ffSlogin if inode.children.contains_key(name) { 339676b8ef6SMork return Err(SystemError::EEXIST); 340004e86ffSlogin } 341004e86ffSlogin 342004e86ffSlogin inode 343004e86ffSlogin .children 344004e86ffSlogin .insert(String::from(name), other_locked.self_ref.upgrade().unwrap()); 345004e86ffSlogin 346004e86ffSlogin // 增加硬链接计数 347004e86ffSlogin other_locked.metadata.nlinks += 1; 348004e86ffSlogin return Ok(()); 349004e86ffSlogin } 350004e86ffSlogin 351676b8ef6SMork fn unlink(&self, name: &str) -> Result<(), SystemError> { 352004e86ffSlogin let mut inode: SpinLockGuard<RamFSInode> = self.0.lock(); 353004e86ffSlogin // 如果当前inode不是目录,那么也没有子目录/文件的概念了,因此要求当前inode的类型是目录 354004e86ffSlogin if inode.metadata.file_type != FileType::Dir { 355676b8ef6SMork return Err(SystemError::ENOTDIR); 356004e86ffSlogin } 357004e86ffSlogin // 不允许删除当前文件夹,也不允许删除上一个目录 358004e86ffSlogin if name == "." || name == ".." { 359676b8ef6SMork return Err(SystemError::ENOTEMPTY); 360004e86ffSlogin } 361004e86ffSlogin 362004e86ffSlogin // 获得要删除的文件的inode 363676b8ef6SMork let to_delete = inode.children.get(name).ok_or(SystemError::ENOENT)?; 364004e86ffSlogin if to_delete.0.lock().metadata.file_type == FileType::Dir { 365676b8ef6SMork return Err(SystemError::EPERM); 366004e86ffSlogin } 367004e86ffSlogin // 减少硬链接计数 368004e86ffSlogin to_delete.0.lock().metadata.nlinks -= 1; 369004e86ffSlogin // 在当前目录中删除这个子目录项 370004e86ffSlogin inode.children.remove(name); 371004e86ffSlogin return Ok(()); 372004e86ffSlogin } 373004e86ffSlogin 374676b8ef6SMork fn rmdir(&self, name: &str) -> Result<(), SystemError> { 375004e86ffSlogin let mut inode: SpinLockGuard<RamFSInode> = self.0.lock(); 376004e86ffSlogin // 如果当前inode不是目录,那么也没有子目录/文件的概念了,因此要求当前inode的类型是目录 377004e86ffSlogin if inode.metadata.file_type != FileType::Dir { 378676b8ef6SMork return Err(SystemError::ENOTDIR); 379004e86ffSlogin } 380004e86ffSlogin // 获得要删除的文件夹的inode 381676b8ef6SMork let to_delete = inode.children.get(name).ok_or(SystemError::ENOENT)?; 382004e86ffSlogin if to_delete.0.lock().metadata.file_type != FileType::Dir { 383676b8ef6SMork return Err(SystemError::ENOTDIR); 384004e86ffSlogin } 385004e86ffSlogin 386004e86ffSlogin to_delete.0.lock().metadata.nlinks -= 1; 387004e86ffSlogin // 在当前目录中删除这个子目录项 388004e86ffSlogin inode.children.remove(name); 389004e86ffSlogin return Ok(()); 390004e86ffSlogin } 391004e86ffSlogin 3929e481b3bSTTaq fn move_to( 393004e86ffSlogin &self, 394004e86ffSlogin old_name: &str, 395004e86ffSlogin target: &Arc<dyn IndexNode>, 396004e86ffSlogin new_name: &str, 397676b8ef6SMork ) -> Result<(), SystemError> { 398004e86ffSlogin let old_inode: Arc<dyn IndexNode> = self.find(old_name)?; 399004e86ffSlogin 400004e86ffSlogin // 在新的目录下创建一个硬链接 401004e86ffSlogin target.link(new_name, &old_inode)?; 402004e86ffSlogin // 取消现有的目录下的这个硬链接 403004e86ffSlogin if let Err(err) = self.unlink(old_name) { 404004e86ffSlogin // 如果取消失败,那就取消新的目录下的硬链接 405004e86ffSlogin target.unlink(new_name)?; 406004e86ffSlogin return Err(err); 407004e86ffSlogin } 408004e86ffSlogin return Ok(()); 409004e86ffSlogin } 410004e86ffSlogin 411676b8ef6SMork fn find(&self, name: &str) -> Result<Arc<dyn IndexNode>, SystemError> { 412004e86ffSlogin let inode = self.0.lock(); 413004e86ffSlogin 414004e86ffSlogin if inode.metadata.file_type != FileType::Dir { 415676b8ef6SMork return Err(SystemError::ENOTDIR); 416004e86ffSlogin } 417004e86ffSlogin 418004e86ffSlogin match name { 419004e86ffSlogin "" | "." => { 420676b8ef6SMork return Ok(inode.self_ref.upgrade().ok_or(SystemError::ENOENT)?); 421004e86ffSlogin } 422004e86ffSlogin 423004e86ffSlogin ".." => { 424676b8ef6SMork return Ok(inode.parent.upgrade().ok_or(SystemError::ENOENT)?); 425004e86ffSlogin } 426004e86ffSlogin name => { 427004e86ffSlogin // 在子目录项中查找 428676b8ef6SMork return Ok(inode.children.get(name).ok_or(SystemError::ENOENT)?.clone()); 429004e86ffSlogin } 430004e86ffSlogin } 431004e86ffSlogin } 432004e86ffSlogin 433676b8ef6SMork fn get_entry_name(&self, ino: InodeId) -> Result<String, SystemError> { 434004e86ffSlogin let inode: SpinLockGuard<RamFSInode> = self.0.lock(); 435004e86ffSlogin if inode.metadata.file_type != FileType::Dir { 436676b8ef6SMork return Err(SystemError::ENOTDIR); 437004e86ffSlogin } 438004e86ffSlogin 4396b4e7a29SLoGin match ino.into() { 440004e86ffSlogin 0 => { 441004e86ffSlogin return Ok(String::from(".")); 442004e86ffSlogin } 443004e86ffSlogin 1 => { 444004e86ffSlogin return Ok(String::from("..")); 445004e86ffSlogin } 446004e86ffSlogin ino => { 447004e86ffSlogin // 暴力遍历所有的children,判断inode id是否相同 448004e86ffSlogin // TODO: 优化这里,这个地方性能很差! 449004e86ffSlogin let mut key: Vec<String> = inode 450004e86ffSlogin .children 451004e86ffSlogin .keys() 4526b4e7a29SLoGin .filter(|k| { 4536b4e7a29SLoGin inode 4546b4e7a29SLoGin .children 4556b4e7a29SLoGin .get(*k) 4566b4e7a29SLoGin .unwrap() 4576b4e7a29SLoGin .0 4586b4e7a29SLoGin .lock() 4596b4e7a29SLoGin .metadata 4606b4e7a29SLoGin .inode_id 4616b4e7a29SLoGin .into() 4626b4e7a29SLoGin == ino 4636b4e7a29SLoGin }) 464004e86ffSlogin .cloned() 465004e86ffSlogin .collect(); 466004e86ffSlogin 467004e86ffSlogin match key.len() { 468676b8ef6SMork 0=>{return Err(SystemError::ENOENT);} 469004e86ffSlogin 1=>{return Ok(key.remove(0));} 4706b4e7a29SLoGin _ => 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) 471004e86ffSlogin } 472004e86ffSlogin } 473004e86ffSlogin } 474004e86ffSlogin } 475004e86ffSlogin 476676b8ef6SMork fn list(&self) -> Result<Vec<String>, SystemError> { 477004e86ffSlogin let info = self.metadata()?; 478004e86ffSlogin if info.file_type != FileType::Dir { 479676b8ef6SMork return Err(SystemError::ENOTDIR); 480004e86ffSlogin } 481004e86ffSlogin 482004e86ffSlogin let mut keys: Vec<String> = Vec::new(); 483004e86ffSlogin keys.push(String::from(".")); 484004e86ffSlogin keys.push(String::from("..")); 485004e86ffSlogin keys.append(&mut self.0.lock().children.keys().cloned().collect()); 486004e86ffSlogin 487004e86ffSlogin return Ok(keys); 488004e86ffSlogin } 4892dbef785SGnoCiYeH 4902dbef785SGnoCiYeH fn mknod( 4912dbef785SGnoCiYeH &self, 4922dbef785SGnoCiYeH filename: &str, 4932dbef785SGnoCiYeH mode: ModeType, 494c566df45SLoGin _dev_t: DeviceNumber, 4952dbef785SGnoCiYeH ) -> Result<Arc<dyn IndexNode>, SystemError> { 4962dbef785SGnoCiYeH let mut inode = self.0.lock(); 4972dbef785SGnoCiYeH if inode.metadata.file_type != FileType::Dir { 4982dbef785SGnoCiYeH return Err(SystemError::ENOTDIR); 4992dbef785SGnoCiYeH } 5002dbef785SGnoCiYeH 5012dbef785SGnoCiYeH // 判断需要创建的类型 5022dbef785SGnoCiYeH if unlikely(mode.contains(ModeType::S_IFREG)) { 5032dbef785SGnoCiYeH // 普通文件 5042dbef785SGnoCiYeH return Ok(self.create(filename, FileType::File, mode)?); 5052dbef785SGnoCiYeH } 5062dbef785SGnoCiYeH 5072dbef785SGnoCiYeH let nod = Arc::new(LockedRamFSInode(SpinLock::new(RamFSInode { 5082dbef785SGnoCiYeH parent: inode.self_ref.clone(), 5092dbef785SGnoCiYeH self_ref: Weak::default(), 5102dbef785SGnoCiYeH children: BTreeMap::new(), 5112dbef785SGnoCiYeH data: Vec::new(), 5122dbef785SGnoCiYeH metadata: Metadata { 5132dbef785SGnoCiYeH dev_id: 0, 5142dbef785SGnoCiYeH inode_id: generate_inode_id(), 5152dbef785SGnoCiYeH size: 0, 5162dbef785SGnoCiYeH blk_size: 0, 5172dbef785SGnoCiYeH blocks: 0, 5182dbef785SGnoCiYeH atime: TimeSpec::default(), 5192dbef785SGnoCiYeH mtime: TimeSpec::default(), 5202dbef785SGnoCiYeH ctime: TimeSpec::default(), 5212dbef785SGnoCiYeH file_type: FileType::Pipe, 5222dbef785SGnoCiYeH mode: mode, 5232dbef785SGnoCiYeH nlinks: 1, 5242dbef785SGnoCiYeH uid: 0, 5252dbef785SGnoCiYeH gid: 0, 52602343d0bSLoGin raw_dev: DeviceNumber::default(), 5272dbef785SGnoCiYeH }, 5282dbef785SGnoCiYeH fs: inode.fs.clone(), 5292dbef785SGnoCiYeH special_node: None, 5302dbef785SGnoCiYeH }))); 5312dbef785SGnoCiYeH 5322dbef785SGnoCiYeH nod.0.lock().self_ref = Arc::downgrade(&nod); 5332dbef785SGnoCiYeH 5342dbef785SGnoCiYeH if mode.contains(ModeType::S_IFIFO) { 5352dbef785SGnoCiYeH nod.0.lock().metadata.file_type = FileType::Pipe; 5362dbef785SGnoCiYeH // 创建pipe文件 5372dbef785SGnoCiYeH let pipe_inode = LockedPipeInode::new(); 5382dbef785SGnoCiYeH // 设置special_node 5392dbef785SGnoCiYeH nod.0.lock().special_node = Some(SpecialNodeData::Pipe(pipe_inode)); 5402dbef785SGnoCiYeH } else if mode.contains(ModeType::S_IFBLK) { 5412dbef785SGnoCiYeH nod.0.lock().metadata.file_type = FileType::BlockDevice; 5422dbef785SGnoCiYeH unimplemented!() 5432dbef785SGnoCiYeH } else if mode.contains(ModeType::S_IFCHR) { 5442dbef785SGnoCiYeH nod.0.lock().metadata.file_type = FileType::CharDevice; 5452dbef785SGnoCiYeH unimplemented!() 5462dbef785SGnoCiYeH } 5472dbef785SGnoCiYeH 5482dbef785SGnoCiYeH inode 5492dbef785SGnoCiYeH .children 5502dbef785SGnoCiYeH .insert(String::from(filename).to_uppercase(), nod.clone()); 5512dbef785SGnoCiYeH Ok(nod) 5522dbef785SGnoCiYeH } 5532dbef785SGnoCiYeH 5542dbef785SGnoCiYeH fn special_node(&self) -> Option<super::vfs::SpecialNodeData> { 5552dbef785SGnoCiYeH return self.0.lock().special_node.clone(); 5562dbef785SGnoCiYeH } 5579e481b3bSTTaq 5589e481b3bSTTaq /// # 用于重命名内存中的文件或目录 5599e481b3bSTTaq fn rename(&self, _old_name: &str, _new_name: &str) -> Result<(), SystemError> { 5609e481b3bSTTaq let old_inode: Arc<dyn IndexNode> = self.find(_old_name)?; 5619e481b3bSTTaq // 在新的目录下创建一个硬链接 5629e481b3bSTTaq self.link(_new_name, &old_inode)?; 5639e481b3bSTTaq 5649e481b3bSTTaq // 取消现有的目录下的这个硬链接 5659e481b3bSTTaq if let Err(err) = self.unlink(_old_name) { 5669e481b3bSTTaq // 如果取消失败,那就取消新的目录下的硬链接 5679e481b3bSTTaq self.unlink(_new_name)?; 5689e481b3bSTTaq return Err(err); 5699e481b3bSTTaq } 5709e481b3bSTTaq 5719e481b3bSTTaq return Ok(()); 5729e481b3bSTTaq } 573004e86ffSlogin } 574