Use static_assert

master
WangRunji 7 years ago
parent d84b5d82e6
commit d545541d21

@ -12,6 +12,7 @@ panic = 'abort' # prevent `_Unwind_Resume` link error
[dependencies] [dependencies]
spin = "0.4" spin = "0.4"
bit-set = { default-features = false, version = "0.5" } # default-features contains 'std' bit-set = { default-features = false, version = "0.5" } # default-features contains 'std'
static_assertions = "0.2.5"
[target.ucore.dependencies] [target.ucore.dependencies]
bitflags = "1.0" bitflags = "1.0"

@ -150,6 +150,7 @@ bitflags! {
/// ///
/// Match struct `iobuf` in ucore `kern/fs/iobuf.h` /// Match struct `iobuf` in ucore `kern/fs/iobuf.h`
#[repr(C)] #[repr(C)]
#[derive(Debug)]
struct IoBuf { struct IoBuf {
/// the base addr of buffer (used for Rd/Wr) /// the base addr of buffer (used for Rd/Wr)
base: *mut u8, base: *mut u8,
@ -165,6 +166,7 @@ struct IoBuf {
/// ///
/// Match struct `stat` in ucore `libs/stat.h` /// Match struct `stat` in ucore `libs/stat.h`
#[repr(C)] #[repr(C)]
#[derive(Debug)]
struct Stat { struct Stat {
/// protection mode and file type /// protection mode and file type
mode: u32, mode: u32,
@ -195,7 +197,7 @@ const S_IFBLK: u32 = 050000;
// TODO: Append docs from ucore // TODO: Append docs from ucore
#[repr(C)] #[repr(C)]
pub struct INodeOps { pub struct INodeOps {
magic: u64, magic: u32,
open: extern fn(&mut INode, flags: u32) -> ErrorCode, open: extern fn(&mut INode, flags: u32) -> ErrorCode,
close: extern fn(&mut INode) -> ErrorCode, close: extern fn(&mut INode) -> ErrorCode,
read: extern fn(&mut INode, &mut IoBuf) -> ErrorCode, read: extern fn(&mut INode, &mut IoBuf) -> ErrorCode,
@ -223,7 +225,7 @@ pub enum ErrorCode {
/// Process doesn't exist or otherwise /// Process doesn't exist or otherwise
BAD_PROC = -2 , BAD_PROC = -2 ,
/// Invalid parameter /// Invalid parameter
INVAL = -3 , Invalid = -3 ,
/// Request failed due to memory shortage /// Request failed due to memory shortage
NO_MEM = -4 , NO_MEM = -4 ,
/// Attempt to create a new process beyond /// Attempt to create a new process beyond
@ -249,11 +251,11 @@ pub enum ErrorCode {
/// Device/File is Busy /// Device/File is Busy
BUSY = -15, BUSY = -15,
/// No Such File or Directory /// No Such File or Directory
NOENT = -16, NoEntry = -16,
/// Is a Directory /// Is a Directory
ISDIR = -17, ISDIR = -17,
/// Not a Directory /// Not a Directory
NOTDIR = -18, NotDir = -18,
/// Cross Device-Link /// Cross Device-Link
XDEV = -19, XDEV = -19,
/// Unimplemented Feature /// Unimplemented Feature
@ -287,6 +289,10 @@ impl IoBuf {
self.offset += len as i32; self.offset += len as i32;
self.resident -= len as u32; 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 { impl vfs::Device for Device {
@ -428,7 +434,22 @@ static INODE_OPS: INodeOps = {
unimplemented!(); unimplemented!();
} }
extern fn getdirentry(inode: &mut INode, buf: &mut IoBuf) -> ErrorCode { 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 { extern fn reclaim(inode: &mut INode) -> ErrorCode {
println!("inode.reclaim: {:?}", inode.borrow()); println!("inode.reclaim: {:?}", inode.borrow());
@ -505,4 +526,6 @@ unsafe impl<'a> Alloc for &'a UcoreAllocator {
cprintf!("free %d\n", layout.size()); cprintf!("free %d\n", layout.size());
ucore::kfree(ptr); ucore::kfree(ptr);
} }
} }
assert_eq_size!(ops; INodeOps, [u8; 64]);

@ -13,6 +13,8 @@ extern crate bit_set;
#[cfg(feature = "ucore")] #[cfg(feature = "ucore")]
#[macro_use] #[macro_use]
extern crate bitflags; extern crate bitflags;
#[macro_use]
extern crate static_assertions;
#[cfg(not(test))] #[cfg(not(test))]
macro_rules! eprintln { macro_rules! eprintln {

@ -1,7 +1,7 @@
//! On-disk structures in SFS //! On-disk structures in SFS
use core::slice; use core::slice;
use core::mem::size_of_val; use core::mem::{size_of_val, size_of};
use core::fmt::{Debug, Formatter, Error}; use core::fmt::{Debug, Formatter, Error};
use alloc::str; use alloc::str;
@ -182,16 +182,7 @@ pub enum FileType {
Invalid = 0, File = 1, Dir = 2, Link = 3, Invalid = 0, File = 1, Dir = 2, Link = 3,
} }
#[cfg(test)] const_assert!(o1; size_of::<SuperBlock>() <= BLKSIZE);
mod test { const_assert!(o2; size_of::<DiskINode>() <= BLKSIZE);
use super::*; const_assert!(o3; size_of::<DiskEntry>() <= BLKSIZE);
const_assert!(o4; size_of::<IndirectBlock>() == BLKSIZE);
#[test]
fn struct_size() {
use core::mem::size_of;
assert!(size_of::<SuperBlock>() <= BLKSIZE);
assert!(size_of::<DiskINode>() <= BLKSIZE);
assert!(size_of::<DiskEntry>() <= BLKSIZE);
assert_eq!(size_of::<IndirectBlock>(), BLKSIZE);
}
}

Loading…
Cancel
Save