Use static_assert

master
WangRunji 7 years ago
parent d84b5d82e6
commit d545541d21

@ -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"

@ -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());
@ -506,3 +527,5 @@ unsafe impl<'a> Alloc for &'a UcoreAllocator {
ucore::kfree(ptr);
}
}
assert_eq_size!(ops; INodeOps, [u8; 64]);

@ -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 {

@ -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::<SuperBlock>() <= BLKSIZE);
assert!(size_of::<DiskINode>() <= BLKSIZE);
assert!(size_of::<DiskEntry>() <= BLKSIZE);
assert_eq!(size_of::<IndirectBlock>(), BLKSIZE);
}
}
const_assert!(o1; size_of::<SuperBlock>() <= BLKSIZE);
const_assert!(o2; size_of::<DiskINode>() <= BLKSIZE);
const_assert!(o3; size_of::<DiskEntry>() <= BLKSIZE);
const_assert!(o4; size_of::<IndirectBlock>() == BLKSIZE);

Loading…
Cancel
Save