Fix close, add dup

This commit is contained in:
Jeremy Soller 2016-09-11 17:31:21 -06:00
parent 951831c4bb
commit 2fffe3ee77
6 changed files with 73 additions and 14 deletions

View file

@ -10,6 +10,10 @@ impl Scheme for DebugScheme {
Ok(0)
}
fn dup(&mut self, _file: usize) -> Result<usize> {
Ok(0)
}
/// Read the file `number` into the `buffer`
///
/// Returns the number of bytes read

View file

@ -33,18 +33,33 @@ impl InitFsScheme {
impl Scheme for InitFsScheme {
fn open(&mut self, path: &[u8], _flags: usize) -> Result<usize> {
let data = self.files.get(path).ok_or(Error::NoEntry)?;
let id = self.next_id;
self.next_id += 1;
self.handles.insert(id, Handle {
data: data,
seek: 0
});
Ok(id)
}
fn dup(&mut self, file: usize) -> Result<usize> {
let (data, seek) = {
let handle = self.handles.get(&file).ok_or(Error::BadFile)?;
(handle.data, handle.seek)
};
let id = self.next_id;
self.next_id += 1;
self.handles.insert(id, Handle {
data: data,
seek: seek
});
Ok(id)
}
/// Read the file `number` into the `buffer`
///
/// Returns the number of bytes read
fn read(&mut self, file: usize, buffer: &mut [u8]) -> Result<usize> {
let mut handle = self.handles.get_mut(&file).ok_or(Error::BadFile)?;
@ -58,14 +73,10 @@ impl Scheme for InitFsScheme {
Ok(i)
}
/// Write the `buffer` to the `file`
///
/// Returns the number of bytes written
fn write(&mut self, _file: usize, _buffer: &[u8]) -> Result<usize> {
Err(Error::NotPermitted)
}
/// Close the file `number`
fn close(&mut self, file: usize) -> Result<()> {
self.handles.remove(&file).ok_or(Error::BadFile).and(Ok(()))
}

View file

@ -113,16 +113,21 @@ pub trait Scheme {
/// Returns a file descriptor or an error
fn open(&mut self, path: &[u8], flags: usize) -> Result<usize>;
/// Duplicate an open file descriptor
///
/// Returns a file descriptor or an error
fn dup(&mut self, file: usize) -> Result<usize>;
/// Read from some file descriptor into the `buffer`
///
/// Returns the number of bytes read
fn read(&mut self, fd: usize, buffer: &mut [u8]) -> Result<usize>;
fn read(&mut self, file: usize, buffer: &mut [u8]) -> Result<usize>;
/// Write the `buffer` to the file descriptor
///
/// Returns the number of bytes written
fn write(&mut self, fd: usize, buffer: &[u8]) -> Result<usize>;
fn write(&mut self, file: usize, buffer: &[u8]) -> Result<usize>;
/// Close the file descriptor
fn close(&mut self, fd: usize) -> Result<()>;
fn close(&mut self, file: usize) -> Result<()>;
}