From 80d72a9853aa8d64c3dc5f8117fc78a50b6f6ca3 Mon Sep 17 00:00:00 2001 From: WangRunji Date: Wed, 7 Nov 2018 13:11:04 +0800 Subject: [PATCH] add simple File struct --- src/file.rs | 29 +++++++++++++++++++++++++++++ src/lib.rs | 1 + 2 files changed, 30 insertions(+) create mode 100644 src/file.rs diff --git a/src/file.rs b/src/file.rs new file mode 100644 index 0000000..0796a3b --- /dev/null +++ b/src/file.rs @@ -0,0 +1,29 @@ +use vfs::{INode, Result}; +use alloc::sync::Arc; + +pub struct File { + inode: Arc, + offset: usize, + readable: bool, + writable: bool, +} + +impl File { + pub fn new(inode: Arc, readable: bool, writable: bool) -> Self { + File { inode, offset: 0, readable, writable } + } + + pub fn read(&mut self, buf: &mut [u8]) -> Result { + assert!(self.readable); + let len = self.inode.read_at(self.offset, buf)?; + self.offset += len; + Ok(len) + } + + pub fn write(&mut self, buf: &[u8]) -> Result { + assert!(self.writable); + let len = self.inode.write_at(self.offset, buf)?; + self.offset += len; + Ok(len) + } +} \ No newline at end of file diff --git a/src/lib.rs b/src/lib.rs index 7916c8e..94ae27b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -25,6 +25,7 @@ mod util; mod blocked_device; mod vfs; mod sfs; +pub mod file; mod structs; #[cfg(test)] mod tests;