Commit 8b14b587 authored by Jeremy Soller's avatar Jeremy Soller Committed by GitHub

Merge pull request #2 from adrianN/remove_scheme_todo

Make enum values List and Disk contain two values
parents f4d6fbb4 ebc5f86e
......@@ -11,9 +11,8 @@ use ahci::disk::Disk;
#[derive(Clone)]
enum Handle {
//TODO: Make these enum variants normal tuples (), not nested tuples (())
List((Vec<u8>, usize)),
Disk((Arc<Mutex<Disk>>, usize))
List(Vec<u8>, usize),
Disk(Arc<Mutex<Disk>>, usize)
}
pub struct DiskScheme {
......@@ -50,7 +49,7 @@ impl Scheme for DiskScheme {
}
let id = self.next_id.fetch_add(1, Ordering::SeqCst);
self.handles.lock().insert(id, Handle::List((list.into_bytes(), 0)));
self.handles.lock().insert(id, Handle::List(list.into_bytes(), 0));
Ok(id)
} else {
Err(Error::new(EISDIR))
......@@ -60,7 +59,7 @@ impl Scheme for DiskScheme {
if let Some(disk) = self.disks.get(i) {
let id = self.next_id.fetch_add(1, Ordering::SeqCst);
self.handles.lock().insert(id, Handle::Disk((disk.clone(), 0)));
self.handles.lock().insert(id, Handle::Disk(disk.clone(), 0));
Ok(id)
} else {
Err(Error::new(ENOENT))
......@@ -86,14 +85,14 @@ impl Scheme for DiskScheme {
fn fstat(&self, id: usize, stat: &mut Stat) -> Result<usize> {
let handles = self.handles.lock();
match *handles.get(&id).ok_or(Error::new(EBADF))? {
Handle::List(ref handle) => {
Handle::List(ref handle, _) => {
stat.st_mode = MODE_DIR;
stat.st_size = handle.0.len() as u64;
stat.st_size = handle.len() as u64;
Ok(0)
},
Handle::Disk(ref handle) => {
Handle::Disk(ref handle, _) => {
stat.st_mode = MODE_FILE;
stat.st_size = handle.0.lock().size();
stat.st_size = handle.lock().size();
Ok(0)
}
}
......@@ -102,15 +101,15 @@ impl Scheme for DiskScheme {
fn read(&self, id: usize, buf: &mut [u8]) -> Result<usize> {
let mut handles = self.handles.lock();
match *handles.get_mut(&id).ok_or(Error::new(EBADF))? {
Handle::List(ref mut handle) => {
let count = (&handle.0[handle.1..]).read(buf).unwrap();
handle.1 += count;
Handle::List(ref mut handle, ref mut size) => {
let count = (&handle[*size..]).read(buf).unwrap();
*size += count;
Ok(count)
},
Handle::Disk(ref mut handle) => {
let mut disk = handle.0.lock();
let count = disk.read((handle.1 as u64)/512, buf)?;
handle.1 += count;
Handle::Disk(ref mut handle, ref mut size) => {
let mut disk = handle.lock();
let count = disk.read((*size as u64)/512, buf)?;
*size += count;
Ok(count)
}
}
......@@ -119,13 +118,13 @@ impl Scheme for DiskScheme {
fn write(&self, id: usize, buf: &[u8]) -> Result<usize> {
let mut handles = self.handles.lock();
match *handles.get_mut(&id).ok_or(Error::new(EBADF))? {
Handle::List(_) => {
Handle::List(_, _) => {
Err(Error::new(EBADF))
},
Handle::Disk(ref mut handle) => {
let mut disk = handle.0.lock();
let count = disk.write((handle.1 as u64)/512, buf)?;
handle.1 += count;
Handle::Disk(ref mut handle, ref mut size) => {
let mut disk = handle.lock();
let count = disk.write((*size as u64)/512, buf)?;
*size += count;
Ok(count)
}
}
......@@ -134,27 +133,27 @@ impl Scheme for DiskScheme {
fn seek(&self, id: usize, pos: usize, whence: usize) -> Result<usize> {
let mut handles = self.handles.lock();
match *handles.get_mut(&id).ok_or(Error::new(EBADF))? {
Handle::List(ref mut handle) => {
let len = handle.0.len() as usize;
handle.1 = match whence {
Handle::List(ref mut handle, ref mut size) => {
let len = handle.len() as usize;
*size = match whence {
SEEK_SET => cmp::min(len, pos),
SEEK_CUR => cmp::max(0, cmp::min(len as isize, handle.1 as isize + pos as isize)) as usize,
SEEK_CUR => cmp::max(0, cmp::min(len as isize, *size as isize + pos as isize)) as usize,
SEEK_END => cmp::max(0, cmp::min(len as isize, len as isize + pos as isize)) as usize,
_ => return Err(Error::new(EINVAL))
};
Ok(handle.1)
Ok(*size)
},
Handle::Disk(ref mut handle) => {
let len = handle.0.lock().size() as usize;
handle.1 = match whence {
Handle::Disk(ref mut handle, ref mut size) => {
let len = handle.lock().size() as usize;
*size = match whence {
SEEK_SET => cmp::min(len, pos),
SEEK_CUR => cmp::max(0, cmp::min(len as isize, handle.1 as isize + pos as isize)) as usize,
SEEK_CUR => cmp::max(0, cmp::min(len as isize, *size as isize + pos as isize)) as usize,
SEEK_END => cmp::max(0, cmp::min(len as isize, len as isize + pos as isize)) as usize,
_ => return Err(Error::new(EINVAL))
};
Ok(handle.1)
Ok(*size)
}
}
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment