|
|
@ -59,27 +59,16 @@ fn sys_read(fd: usize, base: *mut u8, len: usize) -> SysResult {
|
|
|
|
// TODO: check ptr
|
|
|
|
// TODO: check ptr
|
|
|
|
info!("read: fd: {}, base: {:?}, len: {:#x}", fd, base, len);
|
|
|
|
info!("read: fd: {}, base: {:?}, len: {:#x}", fd, base, len);
|
|
|
|
let slice = unsafe { slice::from_raw_parts_mut(base, len) };
|
|
|
|
let slice = unsafe { slice::from_raw_parts_mut(base, len) };
|
|
|
|
match fd {
|
|
|
|
let len = get_file(fd)?.lock().read(slice)?;
|
|
|
|
0 => unimplemented!(),
|
|
|
|
Ok(len as i32)
|
|
|
|
1 | 2 => return Err(SysError::InvalidFile),
|
|
|
|
|
|
|
|
_ => { get_file(fd)?.lock().read(slice)?; }
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(0)
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
fn sys_write(fd: usize, base: *const u8, len: usize) -> SysResult {
|
|
|
|
fn sys_write(fd: usize, base: *const u8, len: usize) -> SysResult {
|
|
|
|
// TODO: check ptr
|
|
|
|
// TODO: check ptr
|
|
|
|
info!("write: fd: {}, base: {:?}, len: {:#x}", fd, base, len);
|
|
|
|
info!("write: fd: {}, base: {:?}, len: {:#x}", fd, base, len);
|
|
|
|
let slice = unsafe { slice::from_raw_parts(base, len) };
|
|
|
|
let slice = unsafe { slice::from_raw_parts(base, len) };
|
|
|
|
match fd {
|
|
|
|
let len = get_file(fd)?.lock().write(slice)?;
|
|
|
|
0 => return Err(SysError::InvalidFile),
|
|
|
|
Ok(len as i32)
|
|
|
|
1 | 2 => {
|
|
|
|
|
|
|
|
let s = str::from_utf8(slice).map_err(|_| SysError::InvalidArgument)?;
|
|
|
|
|
|
|
|
print!("{}", s);
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
_ => { get_file(fd)?.lock().write(slice)?; }
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(0)
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
fn sys_open(path: *const u8, flags: usize) -> SysResult {
|
|
|
|
fn sys_open(path: *const u8, flags: usize) -> SysResult {
|
|
|
@ -87,25 +76,22 @@ fn sys_open(path: *const u8, flags: usize) -> SysResult {
|
|
|
|
let path = unsafe { util::from_cstr(path) };
|
|
|
|
let path = unsafe { util::from_cstr(path) };
|
|
|
|
let flags = VfsFlags::from_ucore_flags(flags);
|
|
|
|
let flags = VfsFlags::from_ucore_flags(flags);
|
|
|
|
info!("open: path: {:?}, flags: {:?}", path, flags);
|
|
|
|
info!("open: path: {:?}, flags: {:?}", path, flags);
|
|
|
|
match path {
|
|
|
|
let (fd, inode) = match path {
|
|
|
|
"stdin:" => return Ok(0),
|
|
|
|
"stdin:" => (0, ::fs::STDIN.clone() as Arc<INode>),
|
|
|
|
"stdout:" => return Ok(1),
|
|
|
|
"stdout:" => (1, ::fs::STDOUT.clone() as Arc<INode>),
|
|
|
|
"stderr:" => return Ok(2),
|
|
|
|
_ => {
|
|
|
|
_ => {}
|
|
|
|
let fd = (3..).find(|i| !process().files.contains_key(i)).unwrap();
|
|
|
|
}
|
|
|
|
let inode = ::fs::ROOT_INODE.lookup(path)?;
|
|
|
|
let inode = ::fs::ROOT_INODE.lookup(path)?;
|
|
|
|
(fd, inode)
|
|
|
|
let files = &mut process().files;
|
|
|
|
}
|
|
|
|
let fd = (3..).find(|i| !files.contains_key(i)).unwrap();
|
|
|
|
};
|
|
|
|
let file = File::new(inode, flags.contains(VfsFlags::READABLE), flags.contains(VfsFlags::WRITABLE));
|
|
|
|
let file = File::new(inode, flags.contains(VfsFlags::READABLE), flags.contains(VfsFlags::WRITABLE));
|
|
|
|
files.insert(fd, Arc::new(Mutex::new(file)));
|
|
|
|
process().files.insert(fd, Arc::new(Mutex::new(file)));
|
|
|
|
Ok(fd as i32)
|
|
|
|
Ok(fd as i32)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
fn sys_close(fd: usize) -> SysResult {
|
|
|
|
fn sys_close(fd: usize) -> SysResult {
|
|
|
|
info!("close: fd: {:?}", fd);
|
|
|
|
info!("close: fd: {:?}", fd);
|
|
|
|
if fd < 3 {
|
|
|
|
|
|
|
|
return Ok(0);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
match process().files.remove(&fd) {
|
|
|
|
match process().files.remove(&fd) {
|
|
|
|
Some(_) => Ok(0),
|
|
|
|
Some(_) => Ok(0),
|
|
|
|
None => Err(SysError::InvalidFile),
|
|
|
|
None => Err(SysError::InvalidFile),
|
|
|
|