Refactor context list

This commit is contained in:
Jeremy Soller 2016-08-18 08:30:45 -06:00
parent 2de2d4cac4
commit 490dd16776
6 changed files with 89 additions and 34 deletions

View file

@ -1,5 +1,6 @@
//! Filesystem syscalls
use context;
use scheme;
use super::{Error, Result};
@ -7,22 +8,32 @@ use super::{Error, Result};
/// Read syscall
pub fn read(fd: usize, buf: &mut [u8]) -> Result<usize> {
println!("Read {}: {}", fd, buf.len());
if let Some(file) = unsafe { &mut ::context::CONTEXT }.files.get(fd) {
println!("{:?}", file);
Ok(0)
if let Some(context_lock) = context::contexts().get(&0) {
let context = context_lock.read();
if let Some(file) = context.files.get(fd) {
println!("{:?}", file);
Ok(0)
} else {
Err(Error::BadFile)
}
} else {
Err(Error::BadFile)
Err(Error::NoProcess)
}
}
/// Write syscall
pub fn write(fd: usize, buf: &[u8]) -> Result<usize> {
println!("Write {}: {}", fd, buf.len());
if let Some(file) = unsafe { &mut ::context::CONTEXT }.files.get(fd) {
println!("{:?}: {:?}", file, ::core::str::from_utf8(buf));
Ok(buf.len())
if let Some(context_lock) = context::contexts().get(&0) {
let context = context_lock.read();
if let Some(file) = context.files.get(fd) {
println!("{:?}: {:?}", file, ::core::str::from_utf8(buf));
Ok(buf.len())
} else {
Err(Error::BadFile)
}
} else {
Err(Error::BadFile)
Err(Error::NoProcess)
}
}
@ -46,13 +57,18 @@ pub fn open(path: &[u8], flags: usize) -> Result<usize> {
}
}?;
if let Some(fd) = unsafe { &mut ::context::CONTEXT }.add_file(::context::file::File {
scheme: 0,
number: file
}) {
Ok(fd)
if let Some(context_lock) = context::contexts().get(&0) {
let mut context = context_lock.write();
if let Some(fd) = context.add_file(::context::file::File {
scheme: 0,
number: file
}) {
Ok(fd)
} else {
Err(Error::TooManyFiles)
}
} else {
Err(Error::TooManyFiles)
Err(Error::NoProcess)
}
}

View file

@ -53,6 +53,8 @@ pub enum Error {
NotPermitted,
/// No such file or directory
NoEntry,
/// No such process
NoProcess,
/// Bad file number
BadFile,
/// Invalid argument
@ -70,6 +72,7 @@ impl From<Error> for usize {
match err {
Error::NotPermitted => 1,
Error::NoEntry => 2,
Error::NoProcess => 3,
Error::BadFile => 9,
Error::InvalidValue => 22,
Error::TooManyFiles => 24,