@@ -72,7 +72,6 @@ type IndirectBlock = [u32; BLOCK_SZ / 4];
7272type DataBlock = [ u8 ; BLOCK_SZ ] ;
7373
7474#[ repr( C ) ]
75- /// Only support level-1 indirect now, **indirect2** field is always 0.
7675pub struct DiskInode {
7776 pub size : u32 ,
7877 pub direct : [ u32 ; INODE_DIRECT_COUNT ] ,
@@ -235,32 +234,6 @@ impl DiskInode {
235234 }
236235 } ) ;
237236 }
238-
239- /*
240- pub fn clear_size(&mut self, block_device: &Arc<dyn BlockDevice>) -> Vec<u32> {
241- let mut v: Vec<u32> = Vec::new();
242- let blocks = self.blocks() as usize;
243- self.size = 0;
244- for i in 0..blocks.min(INODE_DIRECT_COUNT) {
245- v.push(self.direct[i]);
246- self.direct[i] = 0;
247- }
248- if blocks > INODE_DIRECT_COUNT {
249- get_block_cache(
250- self.indirect1 as usize,
251- Arc::clone(block_device),
252- )
253- .lock()
254- .modify(0, |indirect_block: &mut IndirectBlock| {
255- for i in 0..blocks - INODE_DIRECT_COUNT {
256- v.push(indirect_block[i]);
257- indirect_block[i] = 0;
258- }
259- });
260- }
261- v
262- }
263- */
264237
265238 /// Clear size to zero and return blocks that should be deallocated.
266239 ///
@@ -434,10 +407,13 @@ pub struct DirEntry {
434407
435408pub const DIRENT_SZ : usize = 32 ;
436409
437- //pub type DirentBlock = [DirEntry; BLOCK_SZ / DIRENT_SZ];
438- pub type DirentBytes = [ u8 ; DIRENT_SZ ] ;
439-
440410impl DirEntry {
411+ pub fn empty ( ) -> Self {
412+ Self {
413+ name : [ 0u8 ; NAME_LENGTH_LIMIT + 1 ] ,
414+ inode_number : 0 ,
415+ }
416+ }
441417 pub fn new ( name : & str , inode_number : u32 ) -> Self {
442418 let mut bytes = [ 0u8 ; NAME_LENGTH_LIMIT + 1 ] ;
443419 & mut bytes[ ..name. len ( ) ] . copy_from_slice ( name. as_bytes ( ) ) ;
@@ -446,18 +422,20 @@ impl DirEntry {
446422 inode_number,
447423 }
448424 }
449- pub fn into_bytes ( & self ) -> & DirentBytes {
425+ pub fn as_bytes ( & self ) -> & [ u8 ] {
450426 unsafe {
451- & * ( 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+ )
452431 }
453432 }
454- pub fn from_bytes ( bytes : & DirentBytes ) -> & Self {
455- unsafe { & * ( bytes. as_ptr ( ) as usize as * const Self ) }
456- }
457- #[ allow( unused) ]
458- pub fn from_bytes_mut ( bytes : & mut DirentBytes ) -> & mut Self {
433+ pub fn as_bytes_mut ( & mut self ) -> & mut [ u8 ] {
459434 unsafe {
460- & 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+ )
461439 }
462440 }
463441 pub fn name ( & self ) -> & str {
0 commit comments