1004e86ffSlogin use core::any::Any; 2*2dbef785SGnoCiYeH use core::intrinsics::unlikely; 3004e86ffSlogin 4004e86ffSlogin use alloc::{ 5004e86ffSlogin collections::BTreeMap, 6004e86ffSlogin string::String, 7004e86ffSlogin sync::{Arc, Weak}, 8004e86ffSlogin vec::Vec, 9004e86ffSlogin }; 10004e86ffSlogin 11004e86ffSlogin use crate::{ 12004e86ffSlogin filesystem::vfs::{core::generate_inode_id, FileType}, 13*2dbef785SGnoCiYeH ipc::pipe::LockedPipeInode, 14004e86ffSlogin libs::spinlock::{SpinLock, SpinLockGuard}, 1578bf93f0SYJwu2023 syscall::SystemError, 1678bf93f0SYJwu2023 time::TimeSpec, 17004e86ffSlogin }; 18004e86ffSlogin 19004e86ffSlogin use super::vfs::{ 206b4e7a29SLoGin file::FilePrivateData, syscall::ModeType, FileSystem, FsInfo, IndexNode, InodeId, Metadata, 21*2dbef785SGnoCiYeH PollStatus, SpecialNodeData, 22004e86ffSlogin }; 23004e86ffSlogin 24004e86ffSlogin /// RamFS的inode名称的最大长度 25004e86ffSlogin const RAMFS_MAX_NAMELEN: usize = 64; 26004e86ffSlogin 27004e86ffSlogin /// @brief 内存文件系统的Inode结构体 28004e86ffSlogin #[derive(Debug)] 29004e86ffSlogin struct LockedRamFSInode(SpinLock<RamFSInode>); 30004e86ffSlogin 31004e86ffSlogin /// @brief 内存文件系统结构体 32004e86ffSlogin #[derive(Debug)] 33004e86ffSlogin pub struct RamFS { 34004e86ffSlogin /// RamFS的root inode 35004e86ffSlogin root_inode: Arc<LockedRamFSInode>, 36004e86ffSlogin } 37004e86ffSlogin 38004e86ffSlogin /// @brief 内存文件系统的Inode结构体(不包含锁) 39004e86ffSlogin #[derive(Debug)] 40004e86ffSlogin pub struct RamFSInode { 41004e86ffSlogin // parent变量目前只在find函数中使用到 42004e86ffSlogin // 所以只有当inode是文件夹的时候,parent才会生效 43004e86ffSlogin // 对于文件来说,parent就没什么作用了 44004e86ffSlogin // 关于parent的说明: 目录不允许有硬链接 45004e86ffSlogin /// 指向父Inode的弱引用 46004e86ffSlogin parent: Weak<LockedRamFSInode>, 47004e86ffSlogin /// 指向自身的弱引用 48004e86ffSlogin self_ref: Weak<LockedRamFSInode>, 49004e86ffSlogin /// 子Inode的B树 50004e86ffSlogin children: BTreeMap<String, Arc<LockedRamFSInode>>, 51004e86ffSlogin /// 当前inode的数据部分 52004e86ffSlogin data: Vec<u8>, 53004e86ffSlogin /// 当前inode的元数据 54004e86ffSlogin metadata: Metadata, 55004e86ffSlogin /// 指向inode所在的文件系统对象的指针 56004e86ffSlogin fs: Weak<RamFS>, 57*2dbef785SGnoCiYeH /// 指向特殊节点 58*2dbef785SGnoCiYeH special_node: Option<SpecialNodeData>, 59004e86ffSlogin } 60004e86ffSlogin 61004e86ffSlogin impl FileSystem for RamFS { 62004e86ffSlogin fn root_inode(&self) -> Arc<dyn super::vfs::IndexNode> { 63004e86ffSlogin return self.root_inode.clone(); 64004e86ffSlogin } 65004e86ffSlogin 66004e86ffSlogin fn info(&self) -> FsInfo { 67004e86ffSlogin return FsInfo { 68004e86ffSlogin blk_dev_id: 0, 69004e86ffSlogin max_name_len: RAMFS_MAX_NAMELEN, 70004e86ffSlogin }; 71004e86ffSlogin } 72004e86ffSlogin 73004e86ffSlogin /// @brief 本函数用于实现动态转换。 74004e86ffSlogin /// 具体的文件系统在实现本函数时,最简单的方式就是:直接返回self 75004e86ffSlogin fn as_any_ref(&self) -> &dyn Any { 76004e86ffSlogin self 77004e86ffSlogin } 78004e86ffSlogin } 79004e86ffSlogin 80004e86ffSlogin impl RamFS { 81004e86ffSlogin pub fn new() -> Arc<Self> { 82004e86ffSlogin // 初始化root inode 83004e86ffSlogin let root: Arc<LockedRamFSInode> = Arc::new(LockedRamFSInode(SpinLock::new(RamFSInode { 84004e86ffSlogin parent: Weak::default(), 85004e86ffSlogin self_ref: Weak::default(), 86004e86ffSlogin children: BTreeMap::new(), 87004e86ffSlogin data: Vec::new(), 88004e86ffSlogin metadata: Metadata { 89004e86ffSlogin dev_id: 0, 90004e86ffSlogin inode_id: generate_inode_id(), 91004e86ffSlogin size: 0, 92004e86ffSlogin blk_size: 0, 93004e86ffSlogin blocks: 0, 94004e86ffSlogin atime: TimeSpec::default(), 95004e86ffSlogin mtime: TimeSpec::default(), 96004e86ffSlogin ctime: TimeSpec::default(), 97004e86ffSlogin file_type: FileType::Dir, 986b4e7a29SLoGin mode: ModeType::from_bits_truncate(0o777), 99004e86ffSlogin nlinks: 1, 100004e86ffSlogin uid: 0, 101004e86ffSlogin gid: 0, 102004e86ffSlogin raw_dev: 0, 103004e86ffSlogin }, 104004e86ffSlogin fs: Weak::default(), 105*2dbef785SGnoCiYeH special_node: None, 106004e86ffSlogin }))); 107004e86ffSlogin 108004e86ffSlogin let result: Arc<RamFS> = Arc::new(RamFS { root_inode: root }); 109004e86ffSlogin 110004e86ffSlogin // 对root inode加锁,并继续完成初始化工作 111004e86ffSlogin let mut root_guard: SpinLockGuard<RamFSInode> = result.root_inode.0.lock(); 112004e86ffSlogin root_guard.parent = Arc::downgrade(&result.root_inode); 113004e86ffSlogin root_guard.self_ref = Arc::downgrade(&result.root_inode); 114004e86ffSlogin root_guard.fs = Arc::downgrade(&result); 115004e86ffSlogin // 释放锁 116004e86ffSlogin drop(root_guard); 117004e86ffSlogin 118004e86ffSlogin return result; 119004e86ffSlogin } 120004e86ffSlogin } 121004e86ffSlogin 122004e86ffSlogin impl IndexNode for LockedRamFSInode { 123a3ef8f8aSGnoCiYeH fn truncate(&self, len: usize) -> Result<(), SystemError> { 124a3ef8f8aSGnoCiYeH let mut inode = self.0.lock(); 125a3ef8f8aSGnoCiYeH 126a3ef8f8aSGnoCiYeH //如果是文件夹,则报错 127a3ef8f8aSGnoCiYeH if inode.metadata.file_type == FileType::Dir { 128a3ef8f8aSGnoCiYeH return Err(SystemError::EINVAL); 129a3ef8f8aSGnoCiYeH } 130a3ef8f8aSGnoCiYeH 131a3ef8f8aSGnoCiYeH //当前文件长度大于_len才进行截断,否则不操作 132a3ef8f8aSGnoCiYeH if inode.data.len() > len { 133a3ef8f8aSGnoCiYeH inode.data.resize(len, 0); 134a3ef8f8aSGnoCiYeH } 135a3ef8f8aSGnoCiYeH return Ok(()); 136a3ef8f8aSGnoCiYeH } 137a3ef8f8aSGnoCiYeH 138a3ef8f8aSGnoCiYeH fn close(&self, _data: &mut FilePrivateData) -> Result<(), SystemError> { 139a3ef8f8aSGnoCiYeH return Ok(()); 140a3ef8f8aSGnoCiYeH } 141a3ef8f8aSGnoCiYeH 142a3ef8f8aSGnoCiYeH fn open( 143a3ef8f8aSGnoCiYeH &self, 144a3ef8f8aSGnoCiYeH _data: &mut FilePrivateData, 145a3ef8f8aSGnoCiYeH _mode: &super::vfs::file::FileMode, 146a3ef8f8aSGnoCiYeH ) -> Result<(), SystemError> { 147a3ef8f8aSGnoCiYeH return Ok(()); 148a3ef8f8aSGnoCiYeH } 149a3ef8f8aSGnoCiYeH 150004e86ffSlogin fn read_at( 151004e86ffSlogin &self, 152004e86ffSlogin offset: usize, 153004e86ffSlogin len: usize, 154004e86ffSlogin buf: &mut [u8], 155004e86ffSlogin _data: &mut FilePrivateData, 156676b8ef6SMork ) -> Result<usize, SystemError> { 157004e86ffSlogin if buf.len() < len { 158676b8ef6SMork return Err(SystemError::EINVAL); 159004e86ffSlogin } 160004e86ffSlogin // 加锁 161004e86ffSlogin let inode: SpinLockGuard<RamFSInode> = self.0.lock(); 162004e86ffSlogin 163004e86ffSlogin // 检查当前inode是否为一个文件夹,如果是的话,就返回错误 164004e86ffSlogin if inode.metadata.file_type == FileType::Dir { 165676b8ef6SMork return Err(SystemError::EISDIR); 166004e86ffSlogin } 167004e86ffSlogin 168004e86ffSlogin let start = inode.data.len().min(offset); 169004e86ffSlogin let end = inode.data.len().min(offset + len); 170004e86ffSlogin 171004e86ffSlogin // buffer空间不足 172004e86ffSlogin if buf.len() < (end - start) { 173676b8ef6SMork return Err(SystemError::ENOBUFS); 174004e86ffSlogin } 175004e86ffSlogin 176004e86ffSlogin // 拷贝数据 177004e86ffSlogin let src = &inode.data[start..end]; 178004e86ffSlogin buf[0..src.len()].copy_from_slice(src); 179004e86ffSlogin return Ok(src.len()); 180004e86ffSlogin } 181004e86ffSlogin 182004e86ffSlogin fn write_at( 183004e86ffSlogin &self, 184004e86ffSlogin offset: usize, 185004e86ffSlogin len: usize, 186004e86ffSlogin buf: &[u8], 187004e86ffSlogin _data: &mut FilePrivateData, 188676b8ef6SMork ) -> Result<usize, SystemError> { 189004e86ffSlogin if buf.len() < len { 190676b8ef6SMork return Err(SystemError::EINVAL); 191004e86ffSlogin } 192004e86ffSlogin 193004e86ffSlogin // 加锁 194004e86ffSlogin let mut inode: SpinLockGuard<RamFSInode> = self.0.lock(); 195004e86ffSlogin 196004e86ffSlogin // 检查当前inode是否为一个文件夹,如果是的话,就返回错误 197004e86ffSlogin if inode.metadata.file_type == FileType::Dir { 198676b8ef6SMork return Err(SystemError::EISDIR); 199004e86ffSlogin } 200004e86ffSlogin 201004e86ffSlogin let data: &mut Vec<u8> = &mut inode.data; 202004e86ffSlogin 203004e86ffSlogin // 如果文件大小比原来的大,那就resize这个数组 204004e86ffSlogin if offset + len > data.len() { 205004e86ffSlogin data.resize(offset + len, 0); 206004e86ffSlogin } 207004e86ffSlogin 208004e86ffSlogin let target = &mut data[offset..offset + len]; 209004e86ffSlogin target.copy_from_slice(&buf[0..len]); 210004e86ffSlogin return Ok(len); 211004e86ffSlogin } 212004e86ffSlogin 213676b8ef6SMork fn poll(&self) -> Result<PollStatus, SystemError> { 214004e86ffSlogin // 加锁 215004e86ffSlogin let inode: SpinLockGuard<RamFSInode> = self.0.lock(); 216004e86ffSlogin 217004e86ffSlogin // 检查当前inode是否为一个文件夹,如果是的话,就返回错误 218004e86ffSlogin if inode.metadata.file_type == FileType::Dir { 219676b8ef6SMork return Err(SystemError::EISDIR); 220004e86ffSlogin } 221004e86ffSlogin 222cde5492fSlogin return Ok(PollStatus::READ | PollStatus::WRITE); 223004e86ffSlogin } 224004e86ffSlogin 225004e86ffSlogin fn fs(&self) -> Arc<dyn FileSystem> { 226004e86ffSlogin return self.0.lock().fs.upgrade().unwrap(); 227004e86ffSlogin } 228004e86ffSlogin 229004e86ffSlogin fn as_any_ref(&self) -> &dyn core::any::Any { 230004e86ffSlogin self 231004e86ffSlogin } 232004e86ffSlogin 233676b8ef6SMork fn metadata(&self) -> Result<Metadata, SystemError> { 234004e86ffSlogin let inode = self.0.lock(); 235004e86ffSlogin let mut metadata = inode.metadata.clone(); 236004e86ffSlogin metadata.size = inode.data.len() as i64; 237004e86ffSlogin 238004e86ffSlogin return Ok(metadata); 239004e86ffSlogin } 240004e86ffSlogin 241676b8ef6SMork fn set_metadata(&self, metadata: &Metadata) -> Result<(), SystemError> { 242004e86ffSlogin let mut inode = self.0.lock(); 243004e86ffSlogin inode.metadata.atime = metadata.atime; 244004e86ffSlogin inode.metadata.mtime = metadata.mtime; 245004e86ffSlogin inode.metadata.ctime = metadata.ctime; 246004e86ffSlogin inode.metadata.mode = metadata.mode; 247004e86ffSlogin inode.metadata.uid = metadata.uid; 248004e86ffSlogin inode.metadata.gid = metadata.gid; 249004e86ffSlogin 250004e86ffSlogin return Ok(()); 251004e86ffSlogin } 252004e86ffSlogin 253676b8ef6SMork fn resize(&self, len: usize) -> Result<(), SystemError> { 254004e86ffSlogin let mut inode = self.0.lock(); 255004e86ffSlogin if inode.metadata.file_type == FileType::File { 256004e86ffSlogin inode.data.resize(len, 0); 257004e86ffSlogin return Ok(()); 258004e86ffSlogin } else { 259676b8ef6SMork return Err(SystemError::EINVAL); 260004e86ffSlogin } 261004e86ffSlogin } 262004e86ffSlogin 263004e86ffSlogin fn create_with_data( 264004e86ffSlogin &self, 265004e86ffSlogin name: &str, 266004e86ffSlogin file_type: FileType, 2676b4e7a29SLoGin mode: ModeType, 268004e86ffSlogin data: usize, 269676b8ef6SMork ) -> Result<Arc<dyn IndexNode>, SystemError> { 270004e86ffSlogin // 获取当前inode 271004e86ffSlogin let mut inode = self.0.lock(); 272004e86ffSlogin // 如果当前inode不是文件夹,则返回 273004e86ffSlogin if inode.metadata.file_type != FileType::Dir { 274676b8ef6SMork return Err(SystemError::ENOTDIR); 275004e86ffSlogin } 276004e86ffSlogin // 如果有重名的,则返回 277004e86ffSlogin if inode.children.contains_key(name) { 278676b8ef6SMork return Err(SystemError::EEXIST); 279004e86ffSlogin } 280004e86ffSlogin 281004e86ffSlogin // 创建inode 282004e86ffSlogin let result: Arc<LockedRamFSInode> = Arc::new(LockedRamFSInode(SpinLock::new(RamFSInode { 283004e86ffSlogin parent: inode.self_ref.clone(), 284004e86ffSlogin self_ref: Weak::default(), 285004e86ffSlogin children: BTreeMap::new(), 286004e86ffSlogin data: Vec::new(), 287004e86ffSlogin metadata: Metadata { 288004e86ffSlogin dev_id: 0, 289004e86ffSlogin inode_id: generate_inode_id(), 290004e86ffSlogin size: 0, 291004e86ffSlogin blk_size: 0, 292004e86ffSlogin blocks: 0, 293004e86ffSlogin atime: TimeSpec::default(), 294004e86ffSlogin mtime: TimeSpec::default(), 295004e86ffSlogin ctime: TimeSpec::default(), 296004e86ffSlogin file_type: file_type, 297004e86ffSlogin mode: mode, 298004e86ffSlogin nlinks: 1, 299004e86ffSlogin uid: 0, 300004e86ffSlogin gid: 0, 301004e86ffSlogin raw_dev: data, 302004e86ffSlogin }, 303004e86ffSlogin fs: inode.fs.clone(), 304*2dbef785SGnoCiYeH special_node: None, 305004e86ffSlogin }))); 306004e86ffSlogin 307004e86ffSlogin // 初始化inode的自引用的weak指针 308004e86ffSlogin result.0.lock().self_ref = Arc::downgrade(&result); 309004e86ffSlogin 310004e86ffSlogin // 将子inode插入父inode的B树中 311004e86ffSlogin inode.children.insert(String::from(name), result.clone()); 312004e86ffSlogin 313004e86ffSlogin return Ok(result); 314004e86ffSlogin } 315004e86ffSlogin 316676b8ef6SMork fn link(&self, name: &str, other: &Arc<dyn IndexNode>) -> Result<(), SystemError> { 317004e86ffSlogin let other: &LockedRamFSInode = other 318004e86ffSlogin .downcast_ref::<LockedRamFSInode>() 319676b8ef6SMork .ok_or(SystemError::EPERM)?; 320004e86ffSlogin let mut inode: SpinLockGuard<RamFSInode> = self.0.lock(); 321004e86ffSlogin let mut other_locked: SpinLockGuard<RamFSInode> = other.0.lock(); 322004e86ffSlogin 323004e86ffSlogin // 如果当前inode不是文件夹,那么报错 324004e86ffSlogin if inode.metadata.file_type != FileType::Dir { 325676b8ef6SMork return Err(SystemError::ENOTDIR); 326004e86ffSlogin } 327004e86ffSlogin 328004e86ffSlogin // 如果另一个inode是文件夹,那么也报错 329004e86ffSlogin if other_locked.metadata.file_type == FileType::Dir { 330676b8ef6SMork return Err(SystemError::EISDIR); 331004e86ffSlogin } 332004e86ffSlogin 333004e86ffSlogin // 如果当前文件夹下已经有同名文件,也报错。 334004e86ffSlogin if inode.children.contains_key(name) { 335676b8ef6SMork return Err(SystemError::EEXIST); 336004e86ffSlogin } 337004e86ffSlogin 338004e86ffSlogin inode 339004e86ffSlogin .children 340004e86ffSlogin .insert(String::from(name), other_locked.self_ref.upgrade().unwrap()); 341004e86ffSlogin 342004e86ffSlogin // 增加硬链接计数 343004e86ffSlogin other_locked.metadata.nlinks += 1; 344004e86ffSlogin return Ok(()); 345004e86ffSlogin } 346004e86ffSlogin 347676b8ef6SMork fn unlink(&self, name: &str) -> Result<(), SystemError> { 348004e86ffSlogin let mut inode: SpinLockGuard<RamFSInode> = self.0.lock(); 349004e86ffSlogin // 如果当前inode不是目录,那么也没有子目录/文件的概念了,因此要求当前inode的类型是目录 350004e86ffSlogin if inode.metadata.file_type != FileType::Dir { 351676b8ef6SMork return Err(SystemError::ENOTDIR); 352004e86ffSlogin } 353004e86ffSlogin // 不允许删除当前文件夹,也不允许删除上一个目录 354004e86ffSlogin if name == "." || name == ".." { 355676b8ef6SMork return Err(SystemError::ENOTEMPTY); 356004e86ffSlogin } 357004e86ffSlogin 358004e86ffSlogin // 获得要删除的文件的inode 359676b8ef6SMork let to_delete = inode.children.get(name).ok_or(SystemError::ENOENT)?; 360004e86ffSlogin if to_delete.0.lock().metadata.file_type == FileType::Dir { 361676b8ef6SMork return Err(SystemError::EPERM); 362004e86ffSlogin } 363004e86ffSlogin // 减少硬链接计数 364004e86ffSlogin to_delete.0.lock().metadata.nlinks -= 1; 365004e86ffSlogin // 在当前目录中删除这个子目录项 366004e86ffSlogin inode.children.remove(name); 367004e86ffSlogin return Ok(()); 368004e86ffSlogin } 369004e86ffSlogin 370676b8ef6SMork fn rmdir(&self, name: &str) -> Result<(), SystemError> { 371004e86ffSlogin let mut inode: SpinLockGuard<RamFSInode> = self.0.lock(); 372004e86ffSlogin // 如果当前inode不是目录,那么也没有子目录/文件的概念了,因此要求当前inode的类型是目录 373004e86ffSlogin if inode.metadata.file_type != FileType::Dir { 374676b8ef6SMork return Err(SystemError::ENOTDIR); 375004e86ffSlogin } 376004e86ffSlogin // 获得要删除的文件夹的inode 377676b8ef6SMork let to_delete = inode.children.get(name).ok_or(SystemError::ENOENT)?; 378004e86ffSlogin if to_delete.0.lock().metadata.file_type != FileType::Dir { 379676b8ef6SMork return Err(SystemError::ENOTDIR); 380004e86ffSlogin } 381004e86ffSlogin 382004e86ffSlogin to_delete.0.lock().metadata.nlinks -= 1; 383004e86ffSlogin // 在当前目录中删除这个子目录项 384004e86ffSlogin inode.children.remove(name); 385004e86ffSlogin return Ok(()); 386004e86ffSlogin } 387004e86ffSlogin 388004e86ffSlogin fn move_( 389004e86ffSlogin &self, 390004e86ffSlogin old_name: &str, 391004e86ffSlogin target: &Arc<dyn IndexNode>, 392004e86ffSlogin new_name: &str, 393676b8ef6SMork ) -> Result<(), SystemError> { 394004e86ffSlogin let old_inode: Arc<dyn IndexNode> = self.find(old_name)?; 395004e86ffSlogin 396004e86ffSlogin // 在新的目录下创建一个硬链接 397004e86ffSlogin target.link(new_name, &old_inode)?; 398004e86ffSlogin // 取消现有的目录下的这个硬链接 399004e86ffSlogin if let Err(err) = self.unlink(old_name) { 400004e86ffSlogin // 如果取消失败,那就取消新的目录下的硬链接 401004e86ffSlogin target.unlink(new_name)?; 402004e86ffSlogin return Err(err); 403004e86ffSlogin } 404004e86ffSlogin return Ok(()); 405004e86ffSlogin } 406004e86ffSlogin 407676b8ef6SMork fn find(&self, name: &str) -> Result<Arc<dyn IndexNode>, SystemError> { 408004e86ffSlogin let inode = self.0.lock(); 409004e86ffSlogin 410004e86ffSlogin if inode.metadata.file_type != FileType::Dir { 411676b8ef6SMork return Err(SystemError::ENOTDIR); 412004e86ffSlogin } 413004e86ffSlogin 414004e86ffSlogin match name { 415004e86ffSlogin "" | "." => { 416676b8ef6SMork return Ok(inode.self_ref.upgrade().ok_or(SystemError::ENOENT)?); 417004e86ffSlogin } 418004e86ffSlogin 419004e86ffSlogin ".." => { 420676b8ef6SMork return Ok(inode.parent.upgrade().ok_or(SystemError::ENOENT)?); 421004e86ffSlogin } 422004e86ffSlogin name => { 423004e86ffSlogin // 在子目录项中查找 424676b8ef6SMork return Ok(inode.children.get(name).ok_or(SystemError::ENOENT)?.clone()); 425004e86ffSlogin } 426004e86ffSlogin } 427004e86ffSlogin } 428004e86ffSlogin 429676b8ef6SMork fn get_entry_name(&self, ino: InodeId) -> Result<String, SystemError> { 430004e86ffSlogin let inode: SpinLockGuard<RamFSInode> = self.0.lock(); 431004e86ffSlogin if inode.metadata.file_type != FileType::Dir { 432676b8ef6SMork return Err(SystemError::ENOTDIR); 433004e86ffSlogin } 434004e86ffSlogin 4356b4e7a29SLoGin match ino.into() { 436004e86ffSlogin 0 => { 437004e86ffSlogin return Ok(String::from(".")); 438004e86ffSlogin } 439004e86ffSlogin 1 => { 440004e86ffSlogin return Ok(String::from("..")); 441004e86ffSlogin } 442004e86ffSlogin ino => { 443004e86ffSlogin // 暴力遍历所有的children,判断inode id是否相同 444004e86ffSlogin // TODO: 优化这里,这个地方性能很差! 445004e86ffSlogin let mut key: Vec<String> = inode 446004e86ffSlogin .children 447004e86ffSlogin .keys() 4486b4e7a29SLoGin .filter(|k| { 4496b4e7a29SLoGin inode 4506b4e7a29SLoGin .children 4516b4e7a29SLoGin .get(*k) 4526b4e7a29SLoGin .unwrap() 4536b4e7a29SLoGin .0 4546b4e7a29SLoGin .lock() 4556b4e7a29SLoGin .metadata 4566b4e7a29SLoGin .inode_id 4576b4e7a29SLoGin .into() 4586b4e7a29SLoGin == ino 4596b4e7a29SLoGin }) 460004e86ffSlogin .cloned() 461004e86ffSlogin .collect(); 462004e86ffSlogin 463004e86ffSlogin match key.len() { 464676b8ef6SMork 0=>{return Err(SystemError::ENOENT);} 465004e86ffSlogin 1=>{return Ok(key.remove(0));} 4666b4e7a29SLoGin _ => 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) 467004e86ffSlogin } 468004e86ffSlogin } 469004e86ffSlogin } 470004e86ffSlogin } 471004e86ffSlogin 472676b8ef6SMork fn list(&self) -> Result<Vec<String>, SystemError> { 473004e86ffSlogin let info = self.metadata()?; 474004e86ffSlogin if info.file_type != FileType::Dir { 475676b8ef6SMork return Err(SystemError::ENOTDIR); 476004e86ffSlogin } 477004e86ffSlogin 478004e86ffSlogin let mut keys: Vec<String> = Vec::new(); 479004e86ffSlogin keys.push(String::from(".")); 480004e86ffSlogin keys.push(String::from("..")); 481004e86ffSlogin keys.append(&mut self.0.lock().children.keys().cloned().collect()); 482004e86ffSlogin 483004e86ffSlogin return Ok(keys); 484004e86ffSlogin } 485*2dbef785SGnoCiYeH 486*2dbef785SGnoCiYeH fn mknod( 487*2dbef785SGnoCiYeH &self, 488*2dbef785SGnoCiYeH filename: &str, 489*2dbef785SGnoCiYeH mode: ModeType, 490*2dbef785SGnoCiYeH _dev_t: crate::driver::base::device::DeviceNumber, 491*2dbef785SGnoCiYeH ) -> Result<Arc<dyn IndexNode>, SystemError> { 492*2dbef785SGnoCiYeH let mut inode = self.0.lock(); 493*2dbef785SGnoCiYeH if inode.metadata.file_type != FileType::Dir { 494*2dbef785SGnoCiYeH return Err(SystemError::ENOTDIR); 495*2dbef785SGnoCiYeH } 496*2dbef785SGnoCiYeH 497*2dbef785SGnoCiYeH // 判断需要创建的类型 498*2dbef785SGnoCiYeH if unlikely(mode.contains(ModeType::S_IFREG)) { 499*2dbef785SGnoCiYeH // 普通文件 500*2dbef785SGnoCiYeH return Ok(self.create(filename, FileType::File, mode)?); 501*2dbef785SGnoCiYeH } 502*2dbef785SGnoCiYeH 503*2dbef785SGnoCiYeH let nod = Arc::new(LockedRamFSInode(SpinLock::new(RamFSInode { 504*2dbef785SGnoCiYeH parent: inode.self_ref.clone(), 505*2dbef785SGnoCiYeH self_ref: Weak::default(), 506*2dbef785SGnoCiYeH children: BTreeMap::new(), 507*2dbef785SGnoCiYeH data: Vec::new(), 508*2dbef785SGnoCiYeH metadata: Metadata { 509*2dbef785SGnoCiYeH dev_id: 0, 510*2dbef785SGnoCiYeH inode_id: generate_inode_id(), 511*2dbef785SGnoCiYeH size: 0, 512*2dbef785SGnoCiYeH blk_size: 0, 513*2dbef785SGnoCiYeH blocks: 0, 514*2dbef785SGnoCiYeH atime: TimeSpec::default(), 515*2dbef785SGnoCiYeH mtime: TimeSpec::default(), 516*2dbef785SGnoCiYeH ctime: TimeSpec::default(), 517*2dbef785SGnoCiYeH file_type: FileType::Pipe, 518*2dbef785SGnoCiYeH mode: mode, 519*2dbef785SGnoCiYeH nlinks: 1, 520*2dbef785SGnoCiYeH uid: 0, 521*2dbef785SGnoCiYeH gid: 0, 522*2dbef785SGnoCiYeH raw_dev: 0, 523*2dbef785SGnoCiYeH }, 524*2dbef785SGnoCiYeH fs: inode.fs.clone(), 525*2dbef785SGnoCiYeH special_node: None, 526*2dbef785SGnoCiYeH }))); 527*2dbef785SGnoCiYeH 528*2dbef785SGnoCiYeH nod.0.lock().self_ref = Arc::downgrade(&nod); 529*2dbef785SGnoCiYeH 530*2dbef785SGnoCiYeH if mode.contains(ModeType::S_IFIFO) { 531*2dbef785SGnoCiYeH nod.0.lock().metadata.file_type = FileType::Pipe; 532*2dbef785SGnoCiYeH // 创建pipe文件 533*2dbef785SGnoCiYeH let pipe_inode = LockedPipeInode::new(); 534*2dbef785SGnoCiYeH // 设置special_node 535*2dbef785SGnoCiYeH nod.0.lock().special_node = Some(SpecialNodeData::Pipe(pipe_inode)); 536*2dbef785SGnoCiYeH } else if mode.contains(ModeType::S_IFBLK) { 537*2dbef785SGnoCiYeH nod.0.lock().metadata.file_type = FileType::BlockDevice; 538*2dbef785SGnoCiYeH unimplemented!() 539*2dbef785SGnoCiYeH } else if mode.contains(ModeType::S_IFCHR) { 540*2dbef785SGnoCiYeH nod.0.lock().metadata.file_type = FileType::CharDevice; 541*2dbef785SGnoCiYeH unimplemented!() 542*2dbef785SGnoCiYeH } 543*2dbef785SGnoCiYeH 544*2dbef785SGnoCiYeH inode 545*2dbef785SGnoCiYeH .children 546*2dbef785SGnoCiYeH .insert(String::from(filename).to_uppercase(), nod.clone()); 547*2dbef785SGnoCiYeH Ok(nod) 548*2dbef785SGnoCiYeH } 549*2dbef785SGnoCiYeH 550*2dbef785SGnoCiYeH fn special_node(&self) -> Option<super::vfs::SpecialNodeData> { 551*2dbef785SGnoCiYeH return self.0.lock().special_node.clone(); 552*2dbef785SGnoCiYeH } 553004e86ffSlogin } 554