2018-11-07 09:34:31 +04:00
|
|
|
//! System call
|
2018-06-03 19:05:43 +04:00
|
|
|
|
2018-12-20 08:27:21 +04:00
|
|
|
use simple_filesystem::{INode, file::File, FileInfo, FileType, FsError};
|
2018-11-07 09:34:31 +04:00
|
|
|
use core::{slice, str};
|
2018-11-19 13:21:06 +04:00
|
|
|
use alloc::{sync::Arc, vec::Vec, string::String};
|
2018-11-07 20:21:20 +04:00
|
|
|
use spin::Mutex;
|
2018-11-19 13:21:06 +04:00
|
|
|
use log::*;
|
|
|
|
use bitflags::bitflags;
|
|
|
|
use crate::arch::interrupt::TrapFrame;
|
|
|
|
use crate::process::*;
|
|
|
|
use crate::thread;
|
|
|
|
use crate::util;
|
2018-05-12 23:41:41 +04:00
|
|
|
|
2018-11-07 09:34:31 +04:00
|
|
|
/// System call dispatcher
|
2018-11-15 19:41:22 +04:00
|
|
|
pub fn syscall(id: usize, args: [usize; 6], tf: &mut TrapFrame) -> i32 {
|
2018-11-08 13:32:44 +04:00
|
|
|
let ret = match id {
|
2018-11-07 09:34:31 +04:00
|
|
|
// file
|
|
|
|
100 => sys_open(args[0] as *const u8, args[1]),
|
|
|
|
101 => sys_close(args[0]),
|
|
|
|
102 => sys_read(args[0], args[1] as *mut u8, args[2]),
|
|
|
|
103 => sys_write(args[0], args[1] as *const u8, args[2]),
|
|
|
|
030 => sys_putc(args[0] as u8 as char),
|
|
|
|
// 104 => sys_seek(),
|
2018-11-07 19:32:22 +04:00
|
|
|
110 => sys_fstat(args[0], args[1] as *mut Stat),
|
2018-11-07 09:34:31 +04:00
|
|
|
// 111 => sys_fsync(),
|
|
|
|
// 121 => sys_getcwd(),
|
2018-11-08 12:56:01 +04:00
|
|
|
128 => sys_getdirentry(args[0], args[1] as *mut DirEntry),
|
2018-11-07 20:21:20 +04:00
|
|
|
130 => sys_dup(args[0], args[1]),
|
2018-11-07 09:34:31 +04:00
|
|
|
|
|
|
|
// process
|
2018-11-07 20:21:20 +04:00
|
|
|
001 => sys_exit(args[0] as i32),
|
2018-11-07 09:34:31 +04:00
|
|
|
002 => sys_fork(tf),
|
|
|
|
003 => sys_wait(args[0], args[1] as *mut i32),
|
2018-11-15 19:41:22 +04:00
|
|
|
004 => sys_exec(args[0] as *const u8, args[1] as usize, args[2] as *const *const u8, tf),
|
2018-11-07 09:34:31 +04:00
|
|
|
// 005 => sys_clone(),
|
|
|
|
010 => sys_yield(),
|
|
|
|
011 => sys_sleep(args[0]),
|
|
|
|
012 => sys_kill(args[0]),
|
|
|
|
017 => sys_get_time(),
|
|
|
|
018 => sys_getpid(),
|
|
|
|
255 => sys_lab6_set_priority(args[0]),
|
|
|
|
|
|
|
|
// memory
|
|
|
|
// 020 => sys_mmap(),
|
|
|
|
// 021 => sys_munmap(),
|
|
|
|
// 022 => sys_shmem(),
|
|
|
|
// 031 => sys_pgdir(),
|
2018-05-12 23:41:41 +04:00
|
|
|
|
|
|
|
_ => {
|
2018-07-14 14:42:58 +04:00
|
|
|
error!("unknown syscall id: {:#x?}, args: {:x?}", id, args);
|
2018-11-19 13:21:06 +04:00
|
|
|
crate::trap::error(tf);
|
2018-05-19 20:22:52 +04:00
|
|
|
}
|
2018-11-08 13:32:44 +04:00
|
|
|
};
|
|
|
|
match ret {
|
|
|
|
Ok(code) => code,
|
2018-12-20 09:20:00 +04:00
|
|
|
Err(err) => -(err as i32),
|
2018-05-12 23:41:41 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-08 13:32:44 +04:00
|
|
|
fn sys_read(fd: usize, base: *mut u8, len: usize) -> SysResult {
|
2018-11-07 20:21:20 +04:00
|
|
|
// TODO: check ptr
|
2018-11-07 09:34:31 +04:00
|
|
|
info!("read: fd: {}, base: {:?}, len: {:#x}", fd, base, len);
|
|
|
|
let slice = unsafe { slice::from_raw_parts_mut(base, len) };
|
2018-11-11 19:08:59 +04:00
|
|
|
let len = get_file(fd)?.lock().read(slice)?;
|
|
|
|
Ok(len as i32)
|
2018-11-07 09:34:31 +04:00
|
|
|
}
|
|
|
|
|
2018-11-08 13:32:44 +04:00
|
|
|
fn sys_write(fd: usize, base: *const u8, len: usize) -> SysResult {
|
2018-11-07 20:21:20 +04:00
|
|
|
// TODO: check ptr
|
2018-05-21 12:30:30 +04:00
|
|
|
info!("write: fd: {}, base: {:?}, len: {:#x}", fd, base, len);
|
|
|
|
let slice = unsafe { slice::from_raw_parts(base, len) };
|
2018-11-11 19:08:59 +04:00
|
|
|
let len = get_file(fd)?.lock().write(slice)?;
|
|
|
|
Ok(len as i32)
|
2018-05-21 12:30:30 +04:00
|
|
|
}
|
|
|
|
|
2018-11-08 13:32:44 +04:00
|
|
|
fn sys_open(path: *const u8, flags: usize) -> SysResult {
|
2018-11-07 20:21:20 +04:00
|
|
|
// TODO: check ptr
|
2018-05-21 12:30:30 +04:00
|
|
|
let path = unsafe { util::from_cstr(path) };
|
2018-11-07 09:34:31 +04:00
|
|
|
let flags = VfsFlags::from_ucore_flags(flags);
|
2018-05-21 12:30:30 +04:00
|
|
|
info!("open: path: {:?}, flags: {:?}", path, flags);
|
2018-11-11 19:08:59 +04:00
|
|
|
let (fd, inode) = match path {
|
2018-11-19 13:21:06 +04:00
|
|
|
"stdin:" => (0, crate::fs::STDIN.clone() as Arc<INode>),
|
|
|
|
"stdout:" => (1, crate::fs::STDOUT.clone() as Arc<INode>),
|
2018-11-11 19:08:59 +04:00
|
|
|
_ => {
|
|
|
|
let fd = (3..).find(|i| !process().files.contains_key(i)).unwrap();
|
2018-11-19 13:21:06 +04:00
|
|
|
let inode = crate::fs::ROOT_INODE.lookup(path)?;
|
2018-11-11 19:08:59 +04:00
|
|
|
(fd, inode)
|
|
|
|
}
|
|
|
|
};
|
2018-11-07 09:34:31 +04:00
|
|
|
let file = File::new(inode, flags.contains(VfsFlags::READABLE), flags.contains(VfsFlags::WRITABLE));
|
2018-11-11 19:08:59 +04:00
|
|
|
process().files.insert(fd, Arc::new(Mutex::new(file)));
|
2018-11-08 13:32:44 +04:00
|
|
|
Ok(fd as i32)
|
2018-05-21 12:30:30 +04:00
|
|
|
}
|
|
|
|
|
2018-11-08 13:32:44 +04:00
|
|
|
fn sys_close(fd: usize) -> SysResult {
|
2018-05-21 12:30:30 +04:00
|
|
|
info!("close: fd: {:?}", fd);
|
2018-11-07 09:34:31 +04:00
|
|
|
match process().files.remove(&fd) {
|
2018-11-08 13:32:44 +04:00
|
|
|
Some(_) => Ok(0),
|
2018-12-20 09:20:00 +04:00
|
|
|
None => Err(SysError::Badf),
|
2018-11-07 09:34:31 +04:00
|
|
|
}
|
2018-05-21 12:30:30 +04:00
|
|
|
}
|
|
|
|
|
2018-11-08 13:32:44 +04:00
|
|
|
fn sys_fstat(fd: usize, stat_ptr: *mut Stat) -> SysResult {
|
2018-11-07 20:21:20 +04:00
|
|
|
// TODO: check ptr
|
|
|
|
info!("fstat: {}", fd);
|
2018-11-08 13:32:44 +04:00
|
|
|
let file = get_file(fd)?;
|
|
|
|
let stat = Stat::from(file.lock().info()?);
|
2018-11-07 19:32:22 +04:00
|
|
|
unsafe { stat_ptr.write(stat); }
|
2018-11-08 13:32:44 +04:00
|
|
|
Ok(0)
|
2018-11-07 19:32:22 +04:00
|
|
|
}
|
|
|
|
|
2018-11-08 12:56:01 +04:00
|
|
|
/// entry_id = dentry.offset / 256
|
|
|
|
/// dentry.name = entry_name
|
|
|
|
/// dentry.offset += 256
|
2018-11-08 13:32:44 +04:00
|
|
|
fn sys_getdirentry(fd: usize, dentry_ptr: *mut DirEntry) -> SysResult {
|
2018-11-08 12:56:01 +04:00
|
|
|
// TODO: check ptr
|
|
|
|
info!("getdirentry: {}", fd);
|
2018-11-08 13:32:44 +04:00
|
|
|
let file = get_file(fd)?;
|
2018-11-08 12:56:01 +04:00
|
|
|
let dentry = unsafe { &mut *dentry_ptr };
|
2018-11-08 13:32:44 +04:00
|
|
|
if !dentry.check() {
|
2018-12-20 09:20:00 +04:00
|
|
|
return Err(SysError::Inval);
|
2018-11-08 13:32:44 +04:00
|
|
|
}
|
|
|
|
let info = file.lock().info()?;
|
|
|
|
if info.type_ != FileType::Dir || info.size <= dentry.entry_id() {
|
2018-12-20 09:20:00 +04:00
|
|
|
return Err(SysError::Inval);
|
2018-11-08 13:32:44 +04:00
|
|
|
}
|
|
|
|
let name = file.lock().get_entry(dentry.entry_id())?;
|
2018-11-08 12:56:01 +04:00
|
|
|
dentry.set_name(name.as_str());
|
2018-11-08 13:32:44 +04:00
|
|
|
Ok(0)
|
2018-11-08 12:56:01 +04:00
|
|
|
}
|
|
|
|
|
2018-11-08 13:32:44 +04:00
|
|
|
fn sys_dup(fd1: usize, fd2: usize) -> SysResult {
|
2018-11-07 20:21:20 +04:00
|
|
|
info!("dup: {} {}", fd1, fd2);
|
2018-11-08 13:32:44 +04:00
|
|
|
let file = get_file(fd1)?;
|
2018-11-07 20:21:20 +04:00
|
|
|
if process().files.contains_key(&fd2) {
|
2018-12-20 09:20:00 +04:00
|
|
|
return Err(SysError::Badf);
|
2018-11-07 20:21:20 +04:00
|
|
|
}
|
|
|
|
process().files.insert(fd2, file.clone());
|
2018-11-08 13:32:44 +04:00
|
|
|
Ok(0)
|
2018-05-21 12:30:30 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Fork the current process. Return the child's PID.
|
2018-11-08 13:32:44 +04:00
|
|
|
fn sys_fork(tf: &TrapFrame) -> SysResult {
|
2018-12-17 19:54:13 +04:00
|
|
|
let context = process().fork(tf);
|
2018-11-05 18:16:45 +04:00
|
|
|
//memory_set_map_swappable(context.get_memory_set_mut());
|
2018-11-17 08:58:40 +04:00
|
|
|
let pid = processor().manager().add(context, thread::current().id());
|
2018-11-05 17:31:04 +04:00
|
|
|
//memory_set_map_swappable(processor.get_context_mut(pid).get_memory_set_mut());
|
2018-11-03 17:45:03 +04:00
|
|
|
info!("fork: {} -> {}", thread::current().id(), pid);
|
2018-11-08 13:32:44 +04:00
|
|
|
Ok(pid as i32)
|
2018-05-21 12:30:30 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Wait the process exit.
|
|
|
|
/// Return the PID. Store exit code to `code` if it's not null.
|
2018-11-08 13:32:44 +04:00
|
|
|
fn sys_wait(pid: usize, code: *mut i32) -> SysResult {
|
2018-11-07 20:21:20 +04:00
|
|
|
// TODO: check ptr
|
2018-11-03 17:45:03 +04:00
|
|
|
loop {
|
2018-11-19 13:21:06 +04:00
|
|
|
use alloc::vec;
|
2018-11-03 17:45:03 +04:00
|
|
|
let wait_procs = match pid {
|
2018-11-14 14:36:47 +04:00
|
|
|
0 => processor().manager().get_children(thread::current().id()),
|
2018-11-03 17:45:03 +04:00
|
|
|
_ => vec![pid],
|
|
|
|
};
|
|
|
|
if wait_procs.is_empty() {
|
2018-11-08 13:32:44 +04:00
|
|
|
return Ok(-1);
|
2018-11-03 17:45:03 +04:00
|
|
|
}
|
|
|
|
for pid in wait_procs {
|
|
|
|
match processor().manager().get_status(pid) {
|
|
|
|
Some(Status::Exited(exit_code)) => {
|
|
|
|
if !code.is_null() {
|
|
|
|
unsafe { code.write(exit_code as i32); }
|
|
|
|
}
|
2018-11-17 08:58:40 +04:00
|
|
|
processor().manager().remove(pid);
|
2018-11-03 17:45:03 +04:00
|
|
|
info!("wait: {} -> {}", thread::current().id(), pid);
|
2018-11-08 13:32:44 +04:00
|
|
|
return Ok(0);
|
2018-11-03 17:45:03 +04:00
|
|
|
}
|
2018-11-08 13:32:44 +04:00
|
|
|
None => return Ok(-1),
|
2018-11-03 17:45:03 +04:00
|
|
|
_ => {}
|
2018-05-22 21:35:46 +04:00
|
|
|
}
|
2018-07-14 08:28:55 +04:00
|
|
|
}
|
2018-11-01 17:10:19 +04:00
|
|
|
info!("wait: {} -> {}, sleep", thread::current().id(), pid);
|
2018-11-03 17:45:03 +04:00
|
|
|
if pid == 0 {
|
2018-11-13 18:52:11 +04:00
|
|
|
processor().manager().wait_child(thread::current().id());
|
|
|
|
processor().yield_now();
|
2018-11-03 17:45:03 +04:00
|
|
|
} else {
|
|
|
|
processor().manager().wait(thread::current().id(), pid);
|
|
|
|
processor().yield_now();
|
|
|
|
}
|
2018-05-21 12:30:30 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-15 19:41:22 +04:00
|
|
|
fn sys_exec(name: *const u8, argc: usize, argv: *const *const u8, tf: &mut TrapFrame) -> SysResult {
|
|
|
|
// TODO: check ptr
|
|
|
|
let name = if name.is_null() { "" } else { unsafe { util::from_cstr(name) } };
|
|
|
|
info!("exec: {:?}, argc: {}, argv: {:?}", name, argc, argv);
|
|
|
|
// Copy args to kernel
|
|
|
|
let args: Vec<String> = unsafe {
|
|
|
|
slice::from_raw_parts(argv, argc).iter()
|
|
|
|
.map(|&arg| String::from(util::from_cstr(arg)))
|
|
|
|
.collect()
|
|
|
|
};
|
|
|
|
|
|
|
|
// Read program file
|
|
|
|
let path = args[0].as_str();
|
2018-11-19 13:21:06 +04:00
|
|
|
let inode = crate::fs::ROOT_INODE.lookup(path)?;
|
2018-11-15 19:41:22 +04:00
|
|
|
let size = inode.info()?.size;
|
|
|
|
let mut buf = Vec::with_capacity(size);
|
|
|
|
unsafe { buf.set_len(size); }
|
|
|
|
inode.read_at(0, buf.as_mut_slice())?;
|
|
|
|
|
|
|
|
// Make new Context
|
|
|
|
let iter = args.iter().map(|s| s.as_str());
|
2018-12-13 22:37:51 +04:00
|
|
|
let mut context = Process::new_user(buf.as_slice(), iter);
|
2018-11-15 19:41:22 +04:00
|
|
|
|
|
|
|
// Activate new page table
|
|
|
|
unsafe { context.memory_set.activate(); }
|
|
|
|
|
|
|
|
// Modify the TrapFrame
|
|
|
|
*tf = unsafe { context.arch.get_init_tf() };
|
|
|
|
|
|
|
|
// Swap Context but keep KStack
|
|
|
|
::core::mem::swap(&mut process().kstack, &mut context.kstack);
|
|
|
|
::core::mem::swap(process(), &mut *context);
|
|
|
|
|
|
|
|
Ok(0)
|
|
|
|
}
|
|
|
|
|
2018-11-08 13:32:44 +04:00
|
|
|
fn sys_yield() -> SysResult {
|
2018-05-31 16:26:25 +04:00
|
|
|
thread::yield_now();
|
2018-11-08 13:32:44 +04:00
|
|
|
Ok(0)
|
2018-05-21 12:30:30 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Kill the process
|
2018-11-08 13:32:44 +04:00
|
|
|
fn sys_kill(pid: usize) -> SysResult {
|
2018-11-13 15:56:17 +04:00
|
|
|
info!("{} killed: {}", thread::current().id(), pid);
|
2018-11-03 17:45:03 +04:00
|
|
|
processor().manager().exit(pid, 0x100);
|
|
|
|
if pid == thread::current().id() {
|
|
|
|
processor().yield_now();
|
|
|
|
}
|
2018-11-08 13:32:44 +04:00
|
|
|
Ok(0)
|
2018-05-21 12:30:30 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Get the current process id
|
2018-11-08 13:32:44 +04:00
|
|
|
fn sys_getpid() -> SysResult {
|
|
|
|
Ok(thread::current().id() as i32)
|
2018-05-21 12:30:30 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Exit the current process
|
2018-11-08 13:32:44 +04:00
|
|
|
fn sys_exit(exit_code: i32) -> SysResult {
|
2018-11-03 17:45:03 +04:00
|
|
|
let pid = thread::current().id();
|
2018-11-07 20:21:20 +04:00
|
|
|
info!("exit: {}, code: {}", pid, exit_code);
|
|
|
|
processor().manager().exit(pid, exit_code as usize);
|
2018-11-03 17:45:03 +04:00
|
|
|
processor().yield_now();
|
|
|
|
unreachable!();
|
2018-05-21 12:30:30 +04:00
|
|
|
}
|
|
|
|
|
2018-11-08 13:32:44 +04:00
|
|
|
fn sys_sleep(time: usize) -> SysResult {
|
2018-11-17 08:58:40 +04:00
|
|
|
if time >= 1 << 31 {
|
|
|
|
thread::park();
|
|
|
|
} else {
|
|
|
|
use core::time::Duration;
|
|
|
|
thread::sleep(Duration::from_millis(time as u64 * 10));
|
|
|
|
}
|
2018-11-08 13:32:44 +04:00
|
|
|
Ok(0)
|
2018-05-21 12:30:30 +04:00
|
|
|
}
|
|
|
|
|
2018-11-08 13:32:44 +04:00
|
|
|
fn sys_get_time() -> SysResult {
|
2018-11-19 13:21:06 +04:00
|
|
|
unsafe { Ok(crate::trap::TICK as i32) }
|
2018-05-21 12:30:30 +04:00
|
|
|
}
|
|
|
|
|
2018-11-08 13:32:44 +04:00
|
|
|
fn sys_lab6_set_priority(priority: usize) -> SysResult {
|
2018-11-03 17:45:03 +04:00
|
|
|
let pid = thread::current().id();
|
|
|
|
processor().manager().set_priority(pid, priority as u8);
|
2018-11-08 13:32:44 +04:00
|
|
|
Ok(0)
|
2018-05-23 08:41:13 +04:00
|
|
|
}
|
|
|
|
|
2018-11-08 13:32:44 +04:00
|
|
|
fn sys_putc(c: char) -> SysResult {
|
2018-07-14 08:28:55 +04:00
|
|
|
print!("{}", c);
|
2018-11-08 13:32:44 +04:00
|
|
|
Ok(0)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn get_file(fd: usize) -> Result<&'static Arc<Mutex<File>>, SysError> {
|
2018-12-20 09:20:00 +04:00
|
|
|
process().files.get(&fd).ok_or(SysError::Badf)
|
2018-11-08 13:32:44 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
pub type SysResult = Result<i32, SysError>;
|
|
|
|
|
|
|
|
#[repr(i32)]
|
2018-12-02 15:44:05 +04:00
|
|
|
#[derive(Debug)]
|
2018-11-08 13:32:44 +04:00
|
|
|
pub enum SysError {
|
2018-12-20 09:20:00 +04:00
|
|
|
// ucore compatible error code, which is a modified version of the ones used in linux
|
|
|
|
// name conversion E_XXXXX -> SysError::Xxxxx
|
|
|
|
// see https://github.com/oscourse-tsinghua/ucore_plus/blob/master/ucore/src/libs-user-ucore/common/error.h
|
|
|
|
// we only add current used errors here
|
2018-12-20 10:03:11 +04:00
|
|
|
Noent = 2,// No such file or directory
|
2018-12-20 09:20:00 +04:00
|
|
|
Badf = 9,// Invaild fd number
|
2018-12-20 10:03:11 +04:00
|
|
|
Nomem = 12,// Out of memory, also used as no device space in ucore
|
|
|
|
Exist = 17,// File exists
|
|
|
|
Xdev = 18,// Cross-device link
|
|
|
|
Notdir = 20,// Fd is not a directory
|
|
|
|
Isdir = 21,// Fd is a directory
|
2018-12-20 09:20:00 +04:00
|
|
|
Inval = 22,// Invalid argument.
|
2018-12-20 10:03:11 +04:00
|
|
|
Unimp = 35,// Not implemented operation
|
2018-12-20 09:20:00 +04:00
|
|
|
|
2018-12-20 10:03:11 +04:00
|
|
|
#[allow(dead_code)]
|
2018-12-20 09:20:00 +04:00
|
|
|
Unknown = 65535,// A really really unknown error.
|
2018-11-08 13:32:44 +04:00
|
|
|
}
|
|
|
|
|
2018-12-20 08:27:21 +04:00
|
|
|
impl From<FsError> for SysError {
|
2018-12-20 09:20:00 +04:00
|
|
|
fn from(error: FsError) -> Self {
|
|
|
|
match error {
|
2018-12-20 10:03:11 +04:00
|
|
|
FsError::NotSupported => SysError::Unimp,
|
|
|
|
FsError::NotFile => SysError::Isdir,
|
|
|
|
FsError::IsDir => SysError::Isdir,
|
|
|
|
FsError::NotDir => SysError::Notdir,
|
|
|
|
FsError::EntryNotFound => SysError::Noent,
|
|
|
|
FsError::EntryExist => SysError::Exist,
|
|
|
|
FsError::NotSameFs => SysError::Xdev,
|
|
|
|
FsError::InvalidParam => SysError::Inval,
|
|
|
|
FsError::NoDeviceSpace => SysError::Nomem,
|
|
|
|
FsError::DirRemoved => SysError::Noent,
|
|
|
|
FsError::DirNotEmpty => SysError::Isdir,// It should be E_NOTEMPTY in linux, but in ucore it is equal to E_Isdir
|
|
|
|
FsError::WrongFs => SysError::Inval,
|
2018-12-20 09:20:00 +04:00
|
|
|
}
|
2018-11-08 13:32:44 +04:00
|
|
|
}
|
2018-05-17 18:15:26 +04:00
|
|
|
}
|
|
|
|
|
2018-11-07 09:34:31 +04:00
|
|
|
bitflags! {
|
|
|
|
struct VfsFlags: usize {
|
|
|
|
// WARNING: different from origin uCore
|
|
|
|
const READABLE = 1 << 0;
|
|
|
|
const WRITABLE = 1 << 1;
|
|
|
|
/// create file if it does not exist
|
|
|
|
const CREATE = 1 << 2;
|
|
|
|
/// error if O_CREAT and the file exists
|
|
|
|
const EXCLUSIVE = 1 << 3;
|
|
|
|
/// truncate file upon open
|
|
|
|
const TRUNCATE = 1 << 4;
|
|
|
|
/// append on each write
|
|
|
|
const APPEND = 1 << 5;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl VfsFlags {
|
|
|
|
fn from_ucore_flags(f: usize) -> Self {
|
|
|
|
assert_ne!(f & 0b11, 0b11);
|
|
|
|
Self::from_bits_truncate(f + 1)
|
|
|
|
}
|
|
|
|
}
|
2018-11-07 19:32:22 +04:00
|
|
|
|
2018-11-08 12:56:01 +04:00
|
|
|
#[repr(C)]
|
|
|
|
struct DirEntry {
|
|
|
|
offset: u32,
|
|
|
|
name: [u8; 256],
|
|
|
|
}
|
|
|
|
|
|
|
|
impl DirEntry {
|
|
|
|
fn check(&self) -> bool {
|
|
|
|
self.offset % 256 == 0
|
|
|
|
}
|
|
|
|
fn entry_id(&self) -> usize {
|
|
|
|
(self.offset / 256) as usize
|
|
|
|
}
|
|
|
|
fn set_name(&mut self, name: &str) {
|
|
|
|
self.name[..name.len()].copy_from_slice(name.as_bytes());
|
|
|
|
self.name[name.len()] = 0;
|
|
|
|
self.offset += 256;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-07 19:32:22 +04:00
|
|
|
#[repr(C)]
|
|
|
|
struct Stat {
|
|
|
|
/// protection mode and file type
|
|
|
|
mode: StatMode,
|
|
|
|
/// number of hard links
|
|
|
|
nlinks: u32,
|
|
|
|
/// number of blocks file is using
|
|
|
|
blocks: u32,
|
|
|
|
/// file size (bytes)
|
|
|
|
size: u32,
|
|
|
|
}
|
|
|
|
|
|
|
|
bitflags! {
|
|
|
|
struct StatMode: u32 {
|
|
|
|
const NULL = 0;
|
|
|
|
/// ordinary regular file
|
|
|
|
const FILE = 0o10000;
|
|
|
|
/// directory
|
|
|
|
const DIR = 0o20000;
|
|
|
|
/// symbolic link
|
|
|
|
const LINK = 0o30000;
|
|
|
|
/// character device
|
|
|
|
const CHAR = 0o40000;
|
|
|
|
/// block device
|
|
|
|
const BLOCK = 0o50000;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<FileInfo> for Stat {
|
|
|
|
fn from(info: FileInfo) -> Self {
|
|
|
|
Stat {
|
|
|
|
mode: match info.type_ {
|
|
|
|
FileType::File => StatMode::FILE,
|
|
|
|
FileType::Dir => StatMode::DIR,
|
2018-12-18 08:53:56 +04:00
|
|
|
// _ => StatMode::NULL,
|
|
|
|
//Note: we should mark FileType as #[non_exhaustive]
|
|
|
|
// but it is currently not implemented for enum
|
|
|
|
// see rust-lang/rust#44109
|
2018-11-07 19:32:22 +04:00
|
|
|
},
|
|
|
|
nlinks: info.nlinks as u32,
|
|
|
|
blocks: info.blocks as u32,
|
|
|
|
size: info.size as u32,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|