Skip to content

Commit 6298f57

Browse files
committed
Remove DirentBytes
1 parent 1e2e83e commit 6298f57

File tree

2 files changed

+22
-19
lines changed

2 files changed

+22
-19
lines changed

easy-fs/src/layout.rs

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -407,10 +407,13 @@ pub struct DirEntry {
407407

408408
pub const DIRENT_SZ: usize = 32;
409409

410-
//pub type DirentBlock = [DirEntry; BLOCK_SZ / DIRENT_SZ];
411-
pub type DirentBytes = [u8; DIRENT_SZ];
412-
413410
impl DirEntry {
411+
pub fn empty() -> Self {
412+
Self {
413+
name: [0u8; NAME_LENGTH_LIMIT + 1],
414+
inode_number: 0,
415+
}
416+
}
414417
pub fn new(name: &str, inode_number: u32) -> Self {
415418
let mut bytes = [0u8; NAME_LENGTH_LIMIT + 1];
416419
&mut bytes[..name.len()].copy_from_slice(name.as_bytes());
@@ -419,18 +422,20 @@ impl DirEntry {
419422
inode_number,
420423
}
421424
}
422-
pub fn into_bytes(&self) -> &DirentBytes {
425+
pub fn as_bytes(&self) -> &[u8] {
423426
unsafe {
424-
&*(self as *const Self as usize as *const DirentBytes)
427+
core::slice::from_raw_parts(
428+
self as *const _ as usize as *const u8,
429+
DIRENT_SZ,
430+
)
425431
}
426432
}
427-
pub fn from_bytes(bytes: &DirentBytes) -> &Self {
428-
unsafe { &*(bytes.as_ptr() as usize as *const Self) }
429-
}
430-
#[allow(unused)]
431-
pub fn from_bytes_mut(bytes: &mut DirentBytes) -> &mut Self {
433+
pub fn as_bytes_mut(&mut self) -> &mut [u8] {
432434
unsafe {
433-
&mut *(bytes.as_mut_ptr() as usize as *mut Self)
435+
core::slice::from_raw_parts_mut(
436+
self as *mut _ as usize as *mut u8,
437+
DIRENT_SZ,
438+
)
434439
}
435440
}
436441
pub fn name(&self) -> &str {

easy-fs/src/vfs.rs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ use super::{
33
DiskInode,
44
DiskInodeType,
55
DirEntry,
6-
DirentBytes,
76
EasyFileSystem,
87
DIRENT_SZ,
98
get_block_cache,
@@ -63,17 +62,16 @@ impl Inode {
6362
// assert it is a directory
6463
assert!(disk_inode.is_dir());
6564
let file_count = (disk_inode.size as usize) / DIRENT_SZ;
66-
let mut dirent_space: DirentBytes = Default::default();
65+
let mut dirent = DirEntry::empty();
6766
for i in 0..file_count {
6867
assert_eq!(
6968
disk_inode.read_at(
7069
DIRENT_SZ * i,
71-
&mut dirent_space,
70+
dirent.as_bytes_mut(),
7271
&self.block_device,
7372
),
7473
DIRENT_SZ,
7574
);
76-
let dirent = DirEntry::from_bytes(&dirent_space);
7775
if dirent.name() == name {
7876
return Some(dirent.inode_number() as u32);
7977
}
@@ -144,7 +142,7 @@ impl Inode {
144142
let dirent = DirEntry::new(name, new_inode_id);
145143
root_inode.write_at(
146144
file_count * DIRENT_SZ,
147-
dirent.into_bytes(),
145+
dirent.as_bytes(),
148146
&self.block_device,
149147
);
150148
});
@@ -164,16 +162,16 @@ impl Inode {
164162
let file_count = (disk_inode.size as usize) / DIRENT_SZ;
165163
let mut v: Vec<String> = Vec::new();
166164
for i in 0..file_count {
167-
let mut dirent_bytes: DirentBytes = Default::default();
165+
let mut dirent = DirEntry::empty();
168166
assert_eq!(
169167
disk_inode.read_at(
170168
i * DIRENT_SZ,
171-
&mut dirent_bytes,
169+
dirent.as_bytes_mut(),
172170
&self.block_device,
173171
),
174172
DIRENT_SZ,
175173
);
176-
v.push(String::from(DirEntry::from_bytes(&dirent_bytes).name()));
174+
v.push(String::from(dirent.name()));
177175
}
178176
v
179177
})

0 commit comments

Comments
 (0)