From d545541d219b200442e809e9139d4aad60d0c54c Mon Sep 17 00:00:00 2001 From: WangRunji Date: Mon, 7 May 2018 02:11:26 +0800 Subject: [PATCH] Use static_assert --- Cargo.toml | 1 + src/c_interface.rs | 35 +++++++++++++++++++++++++++++------ src/lib.rs | 2 ++ src/structs.rs | 19 +++++-------------- 4 files changed, 37 insertions(+), 20 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 08b9800..a882a5b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -12,6 +12,7 @@ panic = 'abort' # prevent `_Unwind_Resume` link error [dependencies] spin = "0.4" bit-set = { default-features = false, version = "0.5" } # default-features contains 'std' +static_assertions = "0.2.5" [target.ucore.dependencies] bitflags = "1.0" diff --git a/src/c_interface.rs b/src/c_interface.rs index fd80ccc..8dfbac7 100644 --- a/src/c_interface.rs +++ b/src/c_interface.rs @@ -150,6 +150,7 @@ bitflags! { /// /// Match struct `iobuf` in ucore `kern/fs/iobuf.h` #[repr(C)] +#[derive(Debug)] struct IoBuf { /// the base addr of buffer (used for Rd/Wr) base: *mut u8, @@ -165,6 +166,7 @@ struct IoBuf { /// /// Match struct `stat` in ucore `libs/stat.h` #[repr(C)] +#[derive(Debug)] struct Stat { /// protection mode and file type mode: u32, @@ -195,7 +197,7 @@ const S_IFBLK: u32 = 050000; // TODO: Append docs from ucore #[repr(C)] pub struct INodeOps { - magic: u64, + magic: u32, open: extern fn(&mut INode, flags: u32) -> ErrorCode, close: extern fn(&mut INode) -> ErrorCode, read: extern fn(&mut INode, &mut IoBuf) -> ErrorCode, @@ -223,7 +225,7 @@ pub enum ErrorCode { /// Process doesn't exist or otherwise BAD_PROC = -2 , /// Invalid parameter - INVAL = -3 , + Invalid = -3 , /// Request failed due to memory shortage NO_MEM = -4 , /// Attempt to create a new process beyond @@ -249,11 +251,11 @@ pub enum ErrorCode { /// Device/File is Busy BUSY = -15, /// No Such File or Directory - NOENT = -16, + NoEntry = -16, /// Is a Directory ISDIR = -17, /// Not a Directory - NOTDIR = -18, + NotDir = -18, /// Cross Device-Link XDEV = -19, /// Unimplemented Feature @@ -287,6 +289,10 @@ impl IoBuf { self.offset += len as i32; self.resident -= len as u32; } + fn write(&mut self, data: &[u8]) { + self.as_mut()[..data.len()].copy_from_slice(data); + self.skip(data.len()); + } } impl vfs::Device for Device { @@ -428,7 +434,22 @@ static INODE_OPS: INodeOps = { unimplemented!(); } extern fn getdirentry(inode: &mut INode, buf: &mut IoBuf) -> ErrorCode { - unimplemented!(); + const ENTRY_SIZE: usize = 256; + println!("{:#x?}", buf); + if inode.borrow().info().unwrap().type_ != vfs::FileType::Dir { + return ErrorCode::NotDir; + } + if buf.offset as usize % ENTRY_SIZE != 0 { + return ErrorCode::Invalid; + } + let id = buf.offset as usize / ENTRY_SIZE; + let names = inode.borrow().list().unwrap(); + println!("offset = {}", buf.offset); + if id >= names.len() { + return ErrorCode::NoEntry; + } + buf.write(names[id].as_ref()); + ErrorCode::Ok } extern fn reclaim(inode: &mut INode) -> ErrorCode { println!("inode.reclaim: {:?}", inode.borrow()); @@ -505,4 +526,6 @@ unsafe impl<'a> Alloc for &'a UcoreAllocator { cprintf!("free %d\n", layout.size()); ucore::kfree(ptr); } -} \ No newline at end of file +} + +assert_eq_size!(ops; INodeOps, [u8; 64]); \ No newline at end of file diff --git a/src/lib.rs b/src/lib.rs index ed22d91..6ca8455 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -13,6 +13,8 @@ extern crate bit_set; #[cfg(feature = "ucore")] #[macro_use] extern crate bitflags; +#[macro_use] +extern crate static_assertions; #[cfg(not(test))] macro_rules! eprintln { diff --git a/src/structs.rs b/src/structs.rs index 8ff605b..7a08064 100644 --- a/src/structs.rs +++ b/src/structs.rs @@ -1,7 +1,7 @@ //! On-disk structures in SFS use core::slice; -use core::mem::size_of_val; +use core::mem::{size_of_val, size_of}; use core::fmt::{Debug, Formatter, Error}; use alloc::str; @@ -182,16 +182,7 @@ pub enum FileType { Invalid = 0, File = 1, Dir = 2, Link = 3, } -#[cfg(test)] -mod test { - use super::*; - - #[test] - fn struct_size() { - use core::mem::size_of; - assert!(size_of::() <= BLKSIZE); - assert!(size_of::() <= BLKSIZE); - assert!(size_of::() <= BLKSIZE); - assert_eq!(size_of::(), BLKSIZE); - } -} \ No newline at end of file +const_assert!(o1; size_of::() <= BLKSIZE); +const_assert!(o2; size_of::() <= BLKSIZE); +const_assert!(o3; size_of::() <= BLKSIZE); +const_assert!(o4; size_of::() == BLKSIZE);