From 7a635c3142c6b56d90502b73e62bbdfb2f6de049 Mon Sep 17 00:00:00 2001 From: WangRunji Date: Sun, 6 May 2018 15:51:22 +0800 Subject: [PATCH] Full ErrorCode. Add OpenFlags. --- Cargo.toml | 1 + src/c_interface.rs | 80 +++++++++++++++++++++++++++++++++++++++++++--- src/lib.rs | 3 ++ 3 files changed, 80 insertions(+), 4 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 3d9afb9..0bc10e3 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' +bitflags = "1.0" [features] ucore = [] \ No newline at end of file diff --git a/src/c_interface.rs b/src/c_interface.rs index 03b42cb..defd49a 100644 --- a/src/c_interface.rs +++ b/src/c_interface.rs @@ -62,7 +62,8 @@ pub extern fn sfs_do_mount(dev: *mut Device, fs_store: &mut *mut Fs) -> ErrorCod use self::ucore::*; let fs = unsafe{__alloc_fs(SFS_TYPE)}; cprintf!("fs @ %x\n", fs); - let device = unsafe{ Box::from_raw(dev) }; // TODO: fix unsafe + let mut device = unsafe{ Box::from_raw(dev) }; // TODO: fix unsafe + device.open(); unsafe{&mut (*fs)}.fs = sfs::SimpleFileSystem::open(device).unwrap(); *fs_store = fs; ErrorCode::Ok @@ -96,12 +97,26 @@ pub struct Fs { pub struct Device { blocks: usize, blocksize: usize, - open: extern fn(&mut Device, flags: u32) -> ErrorCode, + open: extern fn(&mut Device, flags: OpenFlags) -> ErrorCode, close: extern fn(&mut Device) -> ErrorCode, io: extern fn(&mut Device, buf: &mut IoBuf, is_write: bool) -> ErrorCode, ioctl: extern fn(&mut Device, op: i32, data: *mut u8) -> ErrorCode, } +bitflags! { + struct OpenFlags: u32 { + // flags for open: choose one of these + const RDONLY = 0 ; // open for reading only + const WRONLY = 1 ; // open for writing only + const RDWR = 2 ; // open for reading and writing; + // then or in any of these: + const CREAT = 0x00000004; // create file if it does not exist + const EXCL = 0x00000008; // error if CREAT and the file exists + const TRUNC = 0x00000010; // truncate file upon open + const APPEND = 0x00000020; // append on each write + } +} + /// A buffer Rd/Wr status record /// /// Match struct `iobuf` in ucore `kern/fs/iobuf.h` @@ -172,8 +187,57 @@ pub struct INodeOps { #[repr(i32)] #[derive(Debug, Eq, PartialEq)] pub enum ErrorCode { - Ok = 0, - Unimplemented = -1, + Unimplemented = -25, + /// No error + Ok = 0 , + /// Unspecified or unknown problem + UNSPECIFIED = -1 , + /// Process doesn't exist or otherwise + BAD_PROC = -2 , + /// Invalid parameter + INVAL = -3 , + /// Request failed due to memory shortage + NO_MEM = -4 , + /// Attempt to create a new process beyond + NO_FREE_PROC= -5 , + /// Memory fault + FAULT = -6 , + /// SWAP READ/WRITE fault + SWAP_FAULT = -7 , + /// Invalid elf file + INVAL_ELF = -8 , + /// Process is killed + KILLED = -9 , + /// Panic Failure + PANIC = -10, + /// Timeout + TIMEOUT = -11, + /// Argument is Too Big + TOO_BIG = -12, + /// No such Device + NO_DEV = -13, + /// Device Not Available + NA_DEV = -14, + /// Device/File is Busy + BUSY = -15, + /// No Such File or Directory + NOENT = -16, + /// Is a Directory + ISDIR = -17, + /// Not a Directory + NOTDIR = -18, + /// Cross Device-Link + XDEV = -19, + /// Unimplemented Feature + UNIMP = -20, + /// Illegal Seek + SEEK = -21, + /// Too Many Files are Open + MAX_OPEN = -22, + /// File/Directory Already Exists + EXISTS = -23, + /// Directory is Not Empty + NOTEMPTY = -24, } // Wrapper functions @@ -200,6 +264,7 @@ impl IoBuf { } } +// FIXME: Must block aligned impl sfs::Device for Device { fn read_at(&mut self, offset: usize, buf: &mut [u8]) -> Option { let mut io_buf = IoBuf { @@ -226,6 +291,13 @@ impl sfs::Device for Device { } } +impl Device { + fn open(&mut self) { + let ret = (self.open)(self, OpenFlags::RDWR); + assert_eq!(ret, ErrorCode::Ok); + } +} + impl INode { fn new() -> *mut Self { use self::ucore::*; diff --git a/src/lib.rs b/src/lib.rs index 2532a4c..709ed0a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -10,6 +10,9 @@ extern crate spin; #[macro_use] extern crate alloc; extern crate bit_set; +#[cfg(feature = "ucore")] +#[macro_use] +extern crate bitflags; #[cfg(not(test))] macro_rules! eprintln {