From 92e9adca7d54c4610fd853cd91ac3b6df5e98e1d Mon Sep 17 00:00:00 2001 From: GnoCiYeH Date: Sun, 5 Nov 2023 21:50:29 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8Dreaddir=E4=BB=A5=E5=8F=8A?= =?UTF-8?q?=E8=AF=BB=E7=A3=81=E7=9B=98=E6=97=B6buf=E4=BC=A0=E9=94=99?= =?UTF-8?q?=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- kernel/src/driver/base/block/block_device.rs | 3 ++- kernel/src/filesystem/fat/fs.rs | 2 ++ kernel/src/filesystem/vfs/file.rs | 18 +++++++++++++----- user/dadk/config/dragon_reach-0.1.0.dadk | 2 +- 4 files changed, 18 insertions(+), 7 deletions(-) diff --git a/kernel/src/driver/base/block/block_device.rs b/kernel/src/driver/base/block/block_device.rs index 460f21fc3..42281b7df 100644 --- a/kernel/src/driver/base/block/block_device.rs +++ b/kernel/src/driver/base/block/block_device.rs @@ -292,9 +292,10 @@ pub trait BlockDevice: Device { let count: usize = (range.lba_end - range.lba_start).try_into().unwrap(); let full = multi && range.is_multi() || !multi && range.is_full(); + // 读取整个block作为有效数据 if full { // 调用 BlockDevice::read_at() 直接把引用传进去,不是把整个数组move进去 - self.read_at(range.lba_start, count, buf)?; + self.read_at(range.lba_start, count, buf_slice)?; } else { // 判断块的长度不能超过最大值 if self.blk_size_log2() > BLK_SIZE_LOG2_LIMIT { diff --git a/kernel/src/filesystem/fat/fs.rs b/kernel/src/filesystem/fat/fs.rs index b8a09267c..e633eb8af 100644 --- a/kernel/src/filesystem/fat/fs.rs +++ b/kernel/src/filesystem/fat/fs.rs @@ -1716,6 +1716,8 @@ impl IndexNode for LockedFATInode { } else if mode.contains(ModeType::S_IFCHR) { nod.0.lock().metadata.file_type = FileType::CharDevice; unimplemented!() + } else { + return Err(SystemError::EINVAL); } inode diff --git a/kernel/src/filesystem/vfs/file.rs b/kernel/src/filesystem/vfs/file.rs index 67073af03..dbe4c59bb 100644 --- a/kernel/src/filesystem/vfs/file.rs +++ b/kernel/src/filesystem/vfs/file.rs @@ -269,15 +269,18 @@ impl File { // 如果偏移量为0 if self.offset == 0 { + // 通过list更新readdir_subdirs_name self.readdir_subdirs_name = inode.list()?; self.readdir_subdirs_name.sort(); } // kdebug!("sub_entries={sub_entries:?}"); - if self.readdir_subdirs_name.is_empty() { + + // 已经读到末尾 + if self.offset == self.readdir_subdirs_name.len() { self.offset = 0; return Ok(0); } - let name: String = self.readdir_subdirs_name.remove(0); + let name = &self.readdir_subdirs_name[self.offset]; let sub_inode: Arc = match inode.find(&name) { Ok(i) => i, Err(e) => { @@ -290,17 +293,22 @@ impl File { let name_bytes: &[u8] = name.as_bytes(); - self.offset += 1; - dirent.d_ino = sub_inode.metadata().unwrap().inode_id.into() as u64; - dirent.d_type = sub_inode.metadata().unwrap().file_type.get_file_type_num() as u8; // 根据posix的规定,dirent中的d_name是一个不定长的数组,因此需要unsafe来拷贝数据 unsafe { let ptr = &mut dirent.d_name as *mut u8; + if self.offset > 0 { + // 将上一次读取到的缓冲区清0,避免这一次的数据携带上一次的脏数据 + core::ptr::write_bytes(ptr, 0, self.readdir_subdirs_name[self.offset - 1].len()) + } let buf: &mut [u8] = ::core::slice::from_raw_parts_mut::<'static, u8>(ptr, name_bytes.len()); buf.copy_from_slice(name_bytes); } + self.offset += 1; + dirent.d_ino = sub_inode.metadata().unwrap().inode_id.into() as u64; + dirent.d_type = sub_inode.metadata().unwrap().file_type.get_file_type_num() as u8; + // 计算dirent结构体的大小 let size = (name_bytes.len() + ::core::mem::size_of::() - ::core::mem::size_of_val(&dirent.d_name)) as u64; diff --git a/user/dadk/config/dragon_reach-0.1.0.dadk b/user/dadk/config/dragon_reach-0.1.0.dadk index 800226347..9d35a9c66 100644 --- a/user/dadk/config/dragon_reach-0.1.0.dadk +++ b/user/dadk/config/dragon_reach-0.1.0.dadk @@ -6,7 +6,7 @@ "BuildFromSource": { "Git": { "url" : "https://git.mirrors.dragonos.org/DragonOS-Community/DragonReach.git", - "revision": "67e3e34cb5" + "revision": "d70ac4ed6b" } } },