Add context and file structs

This commit is contained in:
Jeremy Soller 2016-08-14 18:16:56 -06:00
parent 49739d47e8
commit 4e270bb807
9 changed files with 88 additions and 14 deletions

View file

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

View file

@ -53,8 +53,12 @@ pub enum Error {
NotPermitted,
/// No such file or directory
NoEntry,
/// Bad file number
BadFile,
/// Invalid argument
InvalidValue,
/// Too many open files
TooManyFiles,
/// Syscall not implemented
NoCall
}
@ -66,7 +70,9 @@ impl From<Error> for usize {
match err {
Error::NotPermitted => 1,
Error::NoEntry => 2,
Error::BadFile => 9,
Error::InvalidValue => 22,
Error::TooManyFiles => 24,
Error::NoCall => 38
}
}