1004e86ffSlogin use core::any::Any; 22dbef785SGnoCiYeH use core::intrinsics::unlikely; 3004e86ffSlogin 4004e86ffSlogin use alloc::{ 5004e86ffSlogin collections::BTreeMap, 6004e86ffSlogin string::String, 7004e86ffSlogin sync::{Arc, Weak}, 8004e86ffSlogin vec::Vec, 9004e86ffSlogin }; 1091e9d4abSLoGin use system_error::SystemError; 11004e86ffSlogin 12004e86ffSlogin use crate::{ 13*c566df45SLoGin driver::base::device::device_number::DeviceNumber, 14004e86ffSlogin filesystem::vfs::{core::generate_inode_id, FileType}, 152dbef785SGnoCiYeH ipc::pipe::LockedPipeInode, 16004e86ffSlogin libs::spinlock::{SpinLock, SpinLockGuard}, 1778bf93f0SYJwu2023 time::TimeSpec, 18004e86ffSlogin }; 19004e86ffSlogin 20004e86ffSlogin use super::vfs::{ 216b4e7a29SLoGin file::FilePrivateData, syscall::ModeType, FileSystem, FsInfo, IndexNode, InodeId, Metadata, 2240609970SGnoCiYeH 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 } 79004e86ffSlogin } 80004e86ffSlogin 81004e86ffSlogin impl RamFS { 82004e86ffSlogin pub fn new() -> Arc<Self> { 83004e86ffSlogin // 初始化root inode 84004e86ffSlogin let root: Arc<LockedRamFSInode> = Arc::new(LockedRamFSInode(SpinLock::new(RamFSInode { 85004e86ffSlogin parent: Weak::default(), 86004e86ffSlogin self_ref: Weak::default(), 87004e86ffSlogin children: BTreeMap::new(), 88004e86ffSlogin data: Vec::new(), 89004e86ffSlogin metadata: Metadata { 90004e86ffSlogin dev_id: 0, 91004e86ffSlogin inode_id: generate_inode_id(), 92004e86ffSlogin size: 0, 93004e86ffSlogin blk_size: 0, 94004e86ffSlogin blocks: 0, 95004e86ffSlogin atime: TimeSpec::default(), 96004e86ffSlogin mtime: TimeSpec::default(), 97004e86ffSlogin ctime: TimeSpec::default(), 98004e86ffSlogin file_type: FileType::Dir, 996b4e7a29SLoGin mode: ModeType::from_bits_truncate(0o777), 100004e86ffSlogin nlinks: 1, 101004e86ffSlogin uid: 0, 102004e86ffSlogin gid: 0, 103004e86ffSlogin raw_dev: 0, 104004e86ffSlogin }, 105004e86ffSlogin fs: Weak::default(), 1062dbef785SGnoCiYeH special_node: None, 107004e86ffSlogin }))); 108004e86ffSlogin 109004e86ffSlogin let result: Arc<RamFS> = Arc::new(RamFS { root_inode: root }); 110004e86ffSlogin 111004e86ffSlogin // 对root inode加锁,并继续完成初始化工作 112004e86ffSlogin let mut root_guard: SpinLockGuard<RamFSInode> = result.root_inode.0.lock(); 113004e86ffSlogin root_guard.parent = Arc::downgrade(&result.root_inode); 114004e86ffSlogin root_guard.self_ref = Arc::downgrade(&result.root_inode); 115004e86ffSlogin root_guard.fs = Arc::downgrade(&result); 116004e86ffSlogin // 释放锁 117004e86ffSlogin drop(root_guard); 118004e86ffSlogin 119004e86ffSlogin return result; 120004e86ffSlogin } 121004e86ffSlogin } 122004e86ffSlogin 123004e86ffSlogin impl IndexNode for LockedRamFSInode { 124a3ef8f8aSGnoCiYeH fn truncate(&self, len: usize) -> Result<(), SystemError> { 125a3ef8f8aSGnoCiYeH let mut inode = self.0.lock(); 126a3ef8f8aSGnoCiYeH 127a3ef8f8aSGnoCiYeH //如果是文件夹,则报错 128a3ef8f8aSGnoCiYeH if inode.metadata.file_type == FileType::Dir { 129a3ef8f8aSGnoCiYeH return Err(SystemError::EINVAL); 130a3ef8f8aSGnoCiYeH } 131a3ef8f8aSGnoCiYeH 132a3ef8f8aSGnoCiYeH //当前文件长度大于_len才进行截断,否则不操作 133a3ef8f8aSGnoCiYeH if inode.data.len() > len { 134a3ef8f8aSGnoCiYeH inode.data.resize(len, 0); 135a3ef8f8aSGnoCiYeH } 136a3ef8f8aSGnoCiYeH return Ok(()); 137a3ef8f8aSGnoCiYeH } 138a3ef8f8aSGnoCiYeH 139a3ef8f8aSGnoCiYeH fn close(&self, _data: &mut FilePrivateData) -> Result<(), SystemError> { 140a3ef8f8aSGnoCiYeH return Ok(()); 141a3ef8f8aSGnoCiYeH } 142a3ef8f8aSGnoCiYeH 143a3ef8f8aSGnoCiYeH fn open( 144a3ef8f8aSGnoCiYeH &self, 145a3ef8f8aSGnoCiYeH _data: &mut FilePrivateData, 146a3ef8f8aSGnoCiYeH _mode: &super::vfs::file::FileMode, 147a3ef8f8aSGnoCiYeH ) -> Result<(), SystemError> { 148a3ef8f8aSGnoCiYeH return Ok(()); 149a3ef8f8aSGnoCiYeH } 150a3ef8f8aSGnoCiYeH 151004e86ffSlogin fn read_at( 152004e86ffSlogin &self, 153004e86ffSlogin offset: usize, 154004e86ffSlogin len: usize, 155004e86ffSlogin buf: &mut [u8], 156004e86ffSlogin _data: &mut FilePrivateData, 157676b8ef6SMork ) -> Result<usize, SystemError> { 158004e86ffSlogin if buf.len() < len { 159676b8ef6SMork return Err(SystemError::EINVAL); 160004e86ffSlogin } 161004e86ffSlogin // 加锁 162004e86ffSlogin let inode: SpinLockGuard<RamFSInode> = self.0.lock(); 163004e86ffSlogin 164004e86ffSlogin // 检查当前inode是否为一个文件夹,如果是的话,就返回错误 165004e86ffSlogin if inode.metadata.file_type == FileType::Dir { 166676b8ef6SMork return Err(SystemError::EISDIR); 167004e86ffSlogin } 168004e86ffSlogin 169004e86ffSlogin let start = inode.data.len().min(offset); 170004e86ffSlogin let end = inode.data.len().min(offset + len); 171004e86ffSlogin 172004e86ffSlogin // buffer空间不足 173004e86ffSlogin if buf.len() < (end - start) { 174676b8ef6SMork return Err(SystemError::ENOBUFS); 175004e86ffSlogin } 176004e86ffSlogin 177004e86ffSlogin // 拷贝数据 178004e86ffSlogin let src = &inode.data[start..end]; 179004e86ffSlogin buf[0..src.len()].copy_from_slice(src); 180004e86ffSlogin return Ok(src.len()); 181004e86ffSlogin } 182004e86ffSlogin 183004e86ffSlogin fn write_at( 184004e86ffSlogin &self, 185004e86ffSlogin offset: usize, 186004e86ffSlogin len: usize, 187004e86ffSlogin buf: &[u8], 188004e86ffSlogin _data: &mut FilePrivateData, 189676b8ef6SMork ) -> Result<usize, SystemError> { 190004e86ffSlogin if buf.len() < len { 191676b8ef6SMork return Err(SystemError::EINVAL); 192004e86ffSlogin } 193004e86ffSlogin 194004e86ffSlogin // 加锁 195004e86ffSlogin let mut inode: SpinLockGuard<RamFSInode> = self.0.lock(); 196004e86ffSlogin 197004e86ffSlogin // 检查当前inode是否为一个文件夹,如果是的话,就返回错误 198004e86ffSlogin if inode.metadata.file_type == FileType::Dir { 199676b8ef6SMork return Err(SystemError::EISDIR); 200004e86ffSlogin } 201004e86ffSlogin 202004e86ffSlogin let data: &mut Vec<u8> = &mut inode.data; 203004e86ffSlogin 204004e86ffSlogin // 如果文件大小比原来的大,那就resize这个数组 205004e86ffSlogin if offset + len > data.len() { 206004e86ffSlogin data.resize(offset + len, 0); 207004e86ffSlogin } 208004e86ffSlogin 209004e86ffSlogin let target = &mut data[offset..offset + len]; 210004e86ffSlogin target.copy_from_slice(&buf[0..len]); 211004e86ffSlogin return Ok(len); 212004e86ffSlogin } 213004e86ffSlogin 214004e86ffSlogin fn fs(&self) -> Arc<dyn FileSystem> { 215004e86ffSlogin return self.0.lock().fs.upgrade().unwrap(); 216004e86ffSlogin } 217004e86ffSlogin 218004e86ffSlogin fn as_any_ref(&self) -> &dyn core::any::Any { 219004e86ffSlogin self 220004e86ffSlogin } 221004e86ffSlogin 222676b8ef6SMork fn metadata(&self) -> Result<Metadata, SystemError> { 223004e86ffSlogin let inode = self.0.lock(); 224004e86ffSlogin let mut metadata = inode.metadata.clone(); 225004e86ffSlogin metadata.size = inode.data.len() as i64; 226004e86ffSlogin 227004e86ffSlogin return Ok(metadata); 228004e86ffSlogin } 229004e86ffSlogin 230676b8ef6SMork fn set_metadata(&self, metadata: &Metadata) -> Result<(), SystemError> { 231004e86ffSlogin let mut inode = self.0.lock(); 232004e86ffSlogin inode.metadata.atime = metadata.atime; 233004e86ffSlogin inode.metadata.mtime = metadata.mtime; 234004e86ffSlogin inode.metadata.ctime = metadata.ctime; 235004e86ffSlogin inode.metadata.mode = metadata.mode; 236004e86ffSlogin inode.metadata.uid = metadata.uid; 237004e86ffSlogin inode.metadata.gid = metadata.gid; 238004e86ffSlogin 239004e86ffSlogin return Ok(()); 240004e86ffSlogin } 241004e86ffSlogin 242676b8ef6SMork fn resize(&self, len: usize) -> Result<(), SystemError> { 243004e86ffSlogin let mut inode = self.0.lock(); 244004e86ffSlogin if inode.metadata.file_type == FileType::File { 245004e86ffSlogin inode.data.resize(len, 0); 246004e86ffSlogin return Ok(()); 247004e86ffSlogin } else { 248676b8ef6SMork return Err(SystemError::EINVAL); 249004e86ffSlogin } 250004e86ffSlogin } 251004e86ffSlogin 252004e86ffSlogin fn create_with_data( 253004e86ffSlogin &self, 254004e86ffSlogin name: &str, 255004e86ffSlogin file_type: FileType, 2566b4e7a29SLoGin mode: ModeType, 257004e86ffSlogin data: usize, 258676b8ef6SMork ) -> Result<Arc<dyn IndexNode>, SystemError> { 259004e86ffSlogin // 获取当前inode 260004e86ffSlogin let mut inode = self.0.lock(); 261004e86ffSlogin // 如果当前inode不是文件夹,则返回 262004e86ffSlogin if inode.metadata.file_type != FileType::Dir { 263676b8ef6SMork return Err(SystemError::ENOTDIR); 264004e86ffSlogin } 265004e86ffSlogin // 如果有重名的,则返回 266004e86ffSlogin if inode.children.contains_key(name) { 267676b8ef6SMork return Err(SystemError::EEXIST); 268004e86ffSlogin } 269004e86ffSlogin 270004e86ffSlogin // 创建inode 271004e86ffSlogin let result: Arc<LockedRamFSInode> = Arc::new(LockedRamFSInode(SpinLock::new(RamFSInode { 272004e86ffSlogin parent: inode.self_ref.clone(), 273004e86ffSlogin self_ref: Weak::default(), 274004e86ffSlogin children: BTreeMap::new(), 275004e86ffSlogin data: Vec::new(), 276004e86ffSlogin metadata: Metadata { 277004e86ffSlogin dev_id: 0, 278004e86ffSlogin inode_id: generate_inode_id(), 279004e86ffSlogin size: 0, 280004e86ffSlogin blk_size: 0, 281004e86ffSlogin blocks: 0, 282004e86ffSlogin atime: TimeSpec::default(), 283004e86ffSlogin mtime: TimeSpec::default(), 284004e86ffSlogin ctime: TimeSpec::default(), 285004e86ffSlogin file_type: file_type, 286004e86ffSlogin mode: mode, 287004e86ffSlogin nlinks: 1, 288004e86ffSlogin uid: 0, 289004e86ffSlogin gid: 0, 290004e86ffSlogin raw_dev: data, 291004e86ffSlogin }, 292004e86ffSlogin fs: inode.fs.clone(), 2932dbef785SGnoCiYeH special_node: None, 294004e86ffSlogin }))); 295004e86ffSlogin 296004e86ffSlogin // 初始化inode的自引用的weak指针 297004e86ffSlogin result.0.lock().self_ref = Arc::downgrade(&result); 298004e86ffSlogin 299004e86ffSlogin // 将子inode插入父inode的B树中 300004e86ffSlogin inode.children.insert(String::from(name), result.clone()); 301004e86ffSlogin 302004e86ffSlogin return Ok(result); 303004e86ffSlogin } 304004e86ffSlogin 305676b8ef6SMork fn link(&self, name: &str, other: &Arc<dyn IndexNode>) -> Result<(), SystemError> { 306004e86ffSlogin let other: &LockedRamFSInode = other 307004e86ffSlogin .downcast_ref::<LockedRamFSInode>() 308676b8ef6SMork .ok_or(SystemError::EPERM)?; 309004e86ffSlogin let mut inode: SpinLockGuard<RamFSInode> = self.0.lock(); 310004e86ffSlogin let mut other_locked: SpinLockGuard<RamFSInode> = other.0.lock(); 311004e86ffSlogin 312004e86ffSlogin // 如果当前inode不是文件夹,那么报错 313004e86ffSlogin if inode.metadata.file_type != FileType::Dir { 314676b8ef6SMork return Err(SystemError::ENOTDIR); 315004e86ffSlogin } 316004e86ffSlogin 317004e86ffSlogin // 如果另一个inode是文件夹,那么也报错 318004e86ffSlogin if other_locked.metadata.file_type == FileType::Dir { 319676b8ef6SMork return Err(SystemError::EISDIR); 320004e86ffSlogin } 321004e86ffSlogin 322004e86ffSlogin // 如果当前文件夹下已经有同名文件,也报错。 323004e86ffSlogin if inode.children.contains_key(name) { 324676b8ef6SMork return Err(SystemError::EEXIST); 325004e86ffSlogin } 326004e86ffSlogin 327004e86ffSlogin inode 328004e86ffSlogin .children 329004e86ffSlogin .insert(String::from(name), other_locked.self_ref.upgrade().unwrap()); 330004e86ffSlogin 331004e86ffSlogin // 增加硬链接计数 332004e86ffSlogin other_locked.metadata.nlinks += 1; 333004e86ffSlogin return Ok(()); 334004e86ffSlogin } 335004e86ffSlogin 336676b8ef6SMork fn unlink(&self, name: &str) -> Result<(), SystemError> { 337004e86ffSlogin let mut inode: SpinLockGuard<RamFSInode> = self.0.lock(); 338004e86ffSlogin // 如果当前inode不是目录,那么也没有子目录/文件的概念了,因此要求当前inode的类型是目录 339004e86ffSlogin if inode.metadata.file_type != FileType::Dir { 340676b8ef6SMork return Err(SystemError::ENOTDIR); 341004e86ffSlogin } 342004e86ffSlogin // 不允许删除当前文件夹,也不允许删除上一个目录 343004e86ffSlogin if name == "." || name == ".." { 344676b8ef6SMork return Err(SystemError::ENOTEMPTY); 345004e86ffSlogin } 346004e86ffSlogin 347004e86ffSlogin // 获得要删除的文件的inode 348676b8ef6SMork let to_delete = inode.children.get(name).ok_or(SystemError::ENOENT)?; 349004e86ffSlogin if to_delete.0.lock().metadata.file_type == FileType::Dir { 350676b8ef6SMork return Err(SystemError::EPERM); 351004e86ffSlogin } 352004e86ffSlogin // 减少硬链接计数 353004e86ffSlogin to_delete.0.lock().metadata.nlinks -= 1; 354004e86ffSlogin // 在当前目录中删除这个子目录项 355004e86ffSlogin inode.children.remove(name); 356004e86ffSlogin return Ok(()); 357004e86ffSlogin } 358004e86ffSlogin 359676b8ef6SMork fn rmdir(&self, name: &str) -> Result<(), SystemError> { 360004e86ffSlogin let mut inode: SpinLockGuard<RamFSInode> = self.0.lock(); 361004e86ffSlogin // 如果当前inode不是目录,那么也没有子目录/文件的概念了,因此要求当前inode的类型是目录 362004e86ffSlogin if inode.metadata.file_type != FileType::Dir { 363676b8ef6SMork return Err(SystemError::ENOTDIR); 364004e86ffSlogin } 365004e86ffSlogin // 获得要删除的文件夹的inode 366676b8ef6SMork let to_delete = inode.children.get(name).ok_or(SystemError::ENOENT)?; 367004e86ffSlogin if to_delete.0.lock().metadata.file_type != FileType::Dir { 368676b8ef6SMork return Err(SystemError::ENOTDIR); 369004e86ffSlogin } 370004e86ffSlogin 371004e86ffSlogin to_delete.0.lock().metadata.nlinks -= 1; 372004e86ffSlogin // 在当前目录中删除这个子目录项 373004e86ffSlogin inode.children.remove(name); 374004e86ffSlogin return Ok(()); 375004e86ffSlogin } 376004e86ffSlogin 377004e86ffSlogin fn move_( 378004e86ffSlogin &self, 379004e86ffSlogin old_name: &str, 380004e86ffSlogin target: &Arc<dyn IndexNode>, 381004e86ffSlogin new_name: &str, 382676b8ef6SMork ) -> Result<(), SystemError> { 383004e86ffSlogin let old_inode: Arc<dyn IndexNode> = self.find(old_name)?; 384004e86ffSlogin 385004e86ffSlogin // 在新的目录下创建一个硬链接 386004e86ffSlogin target.link(new_name, &old_inode)?; 387004e86ffSlogin // 取消现有的目录下的这个硬链接 388004e86ffSlogin if let Err(err) = self.unlink(old_name) { 389004e86ffSlogin // 如果取消失败,那就取消新的目录下的硬链接 390004e86ffSlogin target.unlink(new_name)?; 391004e86ffSlogin return Err(err); 392004e86ffSlogin } 393004e86ffSlogin return Ok(()); 394004e86ffSlogin } 395004e86ffSlogin 396676b8ef6SMork fn find(&self, name: &str) -> Result<Arc<dyn IndexNode>, SystemError> { 397004e86ffSlogin let inode = self.0.lock(); 398004e86ffSlogin 399004e86ffSlogin if inode.metadata.file_type != FileType::Dir { 400676b8ef6SMork return Err(SystemError::ENOTDIR); 401004e86ffSlogin } 402004e86ffSlogin 403004e86ffSlogin match name { 404004e86ffSlogin "" | "." => { 405676b8ef6SMork return Ok(inode.self_ref.upgrade().ok_or(SystemError::ENOENT)?); 406004e86ffSlogin } 407004e86ffSlogin 408004e86ffSlogin ".." => { 409676b8ef6SMork return Ok(inode.parent.upgrade().ok_or(SystemError::ENOENT)?); 410004e86ffSlogin } 411004e86ffSlogin name => { 412004e86ffSlogin // 在子目录项中查找 413676b8ef6SMork return Ok(inode.children.get(name).ok_or(SystemError::ENOENT)?.clone()); 414004e86ffSlogin } 415004e86ffSlogin } 416004e86ffSlogin } 417004e86ffSlogin 418676b8ef6SMork fn get_entry_name(&self, ino: InodeId) -> Result<String, SystemError> { 419004e86ffSlogin let inode: SpinLockGuard<RamFSInode> = self.0.lock(); 420004e86ffSlogin if inode.metadata.file_type != FileType::Dir { 421676b8ef6SMork return Err(SystemError::ENOTDIR); 422004e86ffSlogin } 423004e86ffSlogin 4246b4e7a29SLoGin match ino.into() { 425004e86ffSlogin 0 => { 426004e86ffSlogin return Ok(String::from(".")); 427004e86ffSlogin } 428004e86ffSlogin 1 => { 429004e86ffSlogin return Ok(String::from("..")); 430004e86ffSlogin } 431004e86ffSlogin ino => { 432004e86ffSlogin // 暴力遍历所有的children,判断inode id是否相同 433004e86ffSlogin // TODO: 优化这里,这个地方性能很差! 434004e86ffSlogin let mut key: Vec<String> = inode 435004e86ffSlogin .children 436004e86ffSlogin .keys() 4376b4e7a29SLoGin .filter(|k| { 4386b4e7a29SLoGin inode 4396b4e7a29SLoGin .children 4406b4e7a29SLoGin .get(*k) 4416b4e7a29SLoGin .unwrap() 4426b4e7a29SLoGin .0 4436b4e7a29SLoGin .lock() 4446b4e7a29SLoGin .metadata 4456b4e7a29SLoGin .inode_id 4466b4e7a29SLoGin .into() 4476b4e7a29SLoGin == ino 4486b4e7a29SLoGin }) 449004e86ffSlogin .cloned() 450004e86ffSlogin .collect(); 451004e86ffSlogin 452004e86ffSlogin match key.len() { 453676b8ef6SMork 0=>{return Err(SystemError::ENOENT);} 454004e86ffSlogin 1=>{return Ok(key.remove(0));} 4556b4e7a29SLoGin _ => 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) 456004e86ffSlogin } 457004e86ffSlogin } 458004e86ffSlogin } 459004e86ffSlogin } 460004e86ffSlogin 461676b8ef6SMork fn list(&self) -> Result<Vec<String>, SystemError> { 462004e86ffSlogin let info = self.metadata()?; 463004e86ffSlogin if info.file_type != FileType::Dir { 464676b8ef6SMork return Err(SystemError::ENOTDIR); 465004e86ffSlogin } 466004e86ffSlogin 467004e86ffSlogin let mut keys: Vec<String> = Vec::new(); 468004e86ffSlogin keys.push(String::from(".")); 469004e86ffSlogin keys.push(String::from("..")); 470004e86ffSlogin keys.append(&mut self.0.lock().children.keys().cloned().collect()); 471004e86ffSlogin 472004e86ffSlogin return Ok(keys); 473004e86ffSlogin } 4742dbef785SGnoCiYeH 4752dbef785SGnoCiYeH fn mknod( 4762dbef785SGnoCiYeH &self, 4772dbef785SGnoCiYeH filename: &str, 4782dbef785SGnoCiYeH mode: ModeType, 479*c566df45SLoGin _dev_t: DeviceNumber, 4802dbef785SGnoCiYeH ) -> Result<Arc<dyn IndexNode>, SystemError> { 4812dbef785SGnoCiYeH let mut inode = self.0.lock(); 4822dbef785SGnoCiYeH if inode.metadata.file_type != FileType::Dir { 4832dbef785SGnoCiYeH return Err(SystemError::ENOTDIR); 4842dbef785SGnoCiYeH } 4852dbef785SGnoCiYeH 4862dbef785SGnoCiYeH // 判断需要创建的类型 4872dbef785SGnoCiYeH if unlikely(mode.contains(ModeType::S_IFREG)) { 4882dbef785SGnoCiYeH // 普通文件 4892dbef785SGnoCiYeH return Ok(self.create(filename, FileType::File, mode)?); 4902dbef785SGnoCiYeH } 4912dbef785SGnoCiYeH 4922dbef785SGnoCiYeH let nod = Arc::new(LockedRamFSInode(SpinLock::new(RamFSInode { 4932dbef785SGnoCiYeH parent: inode.self_ref.clone(), 4942dbef785SGnoCiYeH self_ref: Weak::default(), 4952dbef785SGnoCiYeH children: BTreeMap::new(), 4962dbef785SGnoCiYeH data: Vec::new(), 4972dbef785SGnoCiYeH metadata: Metadata { 4982dbef785SGnoCiYeH dev_id: 0, 4992dbef785SGnoCiYeH inode_id: generate_inode_id(), 5002dbef785SGnoCiYeH size: 0, 5012dbef785SGnoCiYeH blk_size: 0, 5022dbef785SGnoCiYeH blocks: 0, 5032dbef785SGnoCiYeH atime: TimeSpec::default(), 5042dbef785SGnoCiYeH mtime: TimeSpec::default(), 5052dbef785SGnoCiYeH ctime: TimeSpec::default(), 5062dbef785SGnoCiYeH file_type: FileType::Pipe, 5072dbef785SGnoCiYeH mode: mode, 5082dbef785SGnoCiYeH nlinks: 1, 5092dbef785SGnoCiYeH uid: 0, 5102dbef785SGnoCiYeH gid: 0, 5112dbef785SGnoCiYeH raw_dev: 0, 5122dbef785SGnoCiYeH }, 5132dbef785SGnoCiYeH fs: inode.fs.clone(), 5142dbef785SGnoCiYeH special_node: None, 5152dbef785SGnoCiYeH }))); 5162dbef785SGnoCiYeH 5172dbef785SGnoCiYeH nod.0.lock().self_ref = Arc::downgrade(&nod); 5182dbef785SGnoCiYeH 5192dbef785SGnoCiYeH if mode.contains(ModeType::S_IFIFO) { 5202dbef785SGnoCiYeH nod.0.lock().metadata.file_type = FileType::Pipe; 5212dbef785SGnoCiYeH // 创建pipe文件 5222dbef785SGnoCiYeH let pipe_inode = LockedPipeInode::new(); 5232dbef785SGnoCiYeH // 设置special_node 5242dbef785SGnoCiYeH nod.0.lock().special_node = Some(SpecialNodeData::Pipe(pipe_inode)); 5252dbef785SGnoCiYeH } else if mode.contains(ModeType::S_IFBLK) { 5262dbef785SGnoCiYeH nod.0.lock().metadata.file_type = FileType::BlockDevice; 5272dbef785SGnoCiYeH unimplemented!() 5282dbef785SGnoCiYeH } else if mode.contains(ModeType::S_IFCHR) { 5292dbef785SGnoCiYeH nod.0.lock().metadata.file_type = FileType::CharDevice; 5302dbef785SGnoCiYeH unimplemented!() 5312dbef785SGnoCiYeH } 5322dbef785SGnoCiYeH 5332dbef785SGnoCiYeH inode 5342dbef785SGnoCiYeH .children 5352dbef785SGnoCiYeH .insert(String::from(filename).to_uppercase(), nod.clone()); 5362dbef785SGnoCiYeH Ok(nod) 5372dbef785SGnoCiYeH } 5382dbef785SGnoCiYeH 5392dbef785SGnoCiYeH fn special_node(&self) -> Option<super::vfs::SpecialNodeData> { 5402dbef785SGnoCiYeH return self.0.lock().special_node.clone(); 5412dbef785SGnoCiYeH } 542004e86ffSlogin } 543