add simple File struct

master
WangRunji 6 years ago
parent 1fbda15575
commit 80d72a9853

@ -0,0 +1,29 @@
use vfs::{INode, Result};
use alloc::sync::Arc;
pub struct File {
inode: Arc<INode>,
offset: usize,
readable: bool,
writable: bool,
}
impl File {
pub fn new(inode: Arc<INode>, readable: bool, writable: bool) -> Self {
File { inode, offset: 0, readable, writable }
}
pub fn read(&mut self, buf: &mut [u8]) -> Result<usize> {
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<usize> {
assert!(self.writable);
let len = self.inode.write_at(self.offset, buf)?;
self.offset += len;
Ok(len)
}
}

@ -25,6 +25,7 @@ mod util;
mod blocked_device;
mod vfs;
mod sfs;
pub mod file;
mod structs;
#[cfg(test)]
mod tests;

Loading…
Cancel
Save