From d7304d5d4a6c0b6ccd6ebac8311e62da5ba52772 Mon Sep 17 00:00:00 2001 From: WangRunji Date: Tue, 1 May 2018 00:33:20 +0800 Subject: [PATCH] Add C-interface structures. Impl Drop for Dirty. --- src/c_interface.rs | 70 ++++++++++++++++++++++++++++++++++++++++++++++ src/dirty.rs | 7 +++++ src/lib.rs | 1 + src/structs.rs | 2 ++ 4 files changed, 80 insertions(+) create mode 100644 src/c_interface.rs diff --git a/src/c_interface.rs b/src/c_interface.rs new file mode 100644 index 0000000..0b235fe --- /dev/null +++ b/src/c_interface.rs @@ -0,0 +1,70 @@ +//! C Interfaces for ucore + +/// Abstract low-level file. +/// +/// Match struct `inode` in ucore `kern/fs/vfs/inode.h` +#[repr(C)] +struct INode { + // TODO: full the struct +} + +/// A buffer Rd/Wr status record +/// +/// Match struct `iobuf` in ucore `kern/fs/iobuf.h` +#[repr(C)] +struct IoBuf { + /// the base addr of buffer (used for Rd/Wr) + base: *mut u8, + /// current Rd/Wr position in buffer, will have been incremented by the amount transferred + offset: i32, + /// the length of buffer (used for Rd/Wr) + len: u32, + /// current resident length need to Rd/Wr, will have been decremented by the amount transferred. + resident: u32, +} + +/// Information about a file +/// +/// Match struct `stat` in ucore `libs/stat.h` +#[repr(C)] +struct Stat { + /// protection mode and file type + mode: u32, + /// number of hard links + nlinks: u32, + /// number of blocks file is using + blocks: u32, + /// file size (bytes) + size: u32, +} + +const S_IFMT : u32 = 070000; // mask for type of file +const S_IFREG: u32 = 010000; // ordinary regular file +const S_IFDIR: u32 = 020000; // directory +const S_IFLNK: u32 = 030000; // symbolic link +const S_IFCHR: u32 = 040000; // character device +const S_IFBLK: u32 = 050000; // block device + +/// Abstract operations on a inode. +/// +/// Match struct `inode_ops` in ucore `kern/fs/vfs/inode.h` +// TODO: Append docs from ucore +#[repr(C)] +struct INodeOps { + magic: u64, + open: extern fn(*mut INode, flags: u32) -> i32, + close: extern fn(*mut INode) -> i32, + read: extern fn(*mut INode, *mut IoBuf) -> i32, + write: extern fn(*mut INode, *mut IoBuf) -> i32, + fstat: extern fn(*mut INode, *mut Stat) -> i32, + fsync: extern fn(*mut INode) -> i32, + namefile: extern fn(*mut INode, *mut IoBuf) -> i32, + getdirentry: extern fn(*mut INode, *mut IoBuf) -> i32, + reclaim: extern fn(*mut INode) -> i32, + gettype: extern fn(*mut INode, type_store: *mut u32) -> i32, + tryseek: extern fn(*mut INode, pos: i32) -> i32, + truncate: extern fn(*mut INode, len: i32) -> i32, + create: extern fn(*mut INode, name: *const u8, excl: bool, inode_store: *mut *mut INode) -> i32, + lookup: extern fn(*mut INode, path: *mut u8, inode_store: *mut *mut INode) -> i32, + ioctl: extern fn(*mut INode, op: i32, data: *mut u8) -> i32, +} \ No newline at end of file diff --git a/src/dirty.rs b/src/dirty.rs index 667c9b7..8e7eab9 100644 --- a/src/dirty.rs +++ b/src/dirty.rs @@ -45,3 +45,10 @@ impl DerefMut for Dirty { } } +impl Drop for Dirty { + /// Guard it is not dirty when dropping + fn drop(&mut self) { + assert!(!self.dirty, "data dirty when dropping"); + } +} + diff --git a/src/lib.rs b/src/lib.rs index c0735cf..bb5692b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -13,5 +13,6 @@ mod dirty; mod vfs; mod sfs; mod structs; +mod c_interface; #[cfg(test)] mod tests; \ No newline at end of file diff --git a/src/structs.rs b/src/structs.rs index 7b4dca3..1bd5927 100644 --- a/src/structs.rs +++ b/src/structs.rs @@ -1,3 +1,5 @@ +//! On-disk structures in SFS + use core::slice; use core::mem::size_of_val;