|
|
@ -39,9 +39,9 @@ pub fn sys_write_file(proc: &mut Process, fd: usize, base: *const u8, len: usize
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
pub fn sys_readv(fd: usize, iov_ptr: *const IoVec, iov_count: usize) -> SysResult {
|
|
|
|
pub fn sys_readv(fd: usize, iov_ptr: *const IoVec, iov_count: usize) -> SysResult {
|
|
|
|
|
|
|
|
info!("readv: fd: {}, iov: {:?}, count: {}", fd, iov_ptr, iov_count);
|
|
|
|
let mut proc = process();
|
|
|
|
let mut proc = process();
|
|
|
|
let mut iovs = IoVecs::check_and_new(iov_ptr, iov_count, &proc.memory_set, true)?;
|
|
|
|
let mut iovs = IoVecs::check_and_new(iov_ptr, iov_count, &proc.memory_set, true)?;
|
|
|
|
info!("readv: fd: {}, iov: {:#x?}", fd, iovs);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// read all data to a buf
|
|
|
|
// read all data to a buf
|
|
|
|
let file = proc.get_file(fd)?;
|
|
|
|
let file = proc.get_file(fd)?;
|
|
|
@ -53,9 +53,9 @@ pub fn sys_readv(fd: usize, iov_ptr: *const IoVec, iov_count: usize) -> SysResul
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
pub fn sys_writev(fd: usize, iov_ptr: *const IoVec, iov_count: usize) -> SysResult {
|
|
|
|
pub fn sys_writev(fd: usize, iov_ptr: *const IoVec, iov_count: usize) -> SysResult {
|
|
|
|
|
|
|
|
info!("writev: fd: {}, iov: {:?}, count: {}", fd, iov_ptr, iov_count);
|
|
|
|
let mut proc = process();
|
|
|
|
let mut proc = process();
|
|
|
|
let iovs = IoVecs::check_and_new(iov_ptr, iov_count, &proc.memory_set, false)?;
|
|
|
|
let iovs = IoVecs::check_and_new(iov_ptr, iov_count, &proc.memory_set, false)?;
|
|
|
|
info!("writev: fd: {}, iovs: {:#x?}", fd, iovs);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let file = proc.get_file(fd)?;
|
|
|
|
let file = proc.get_file(fd)?;
|
|
|
|
let buf = iovs.read_all_to_vec();
|
|
|
|
let buf = iovs.read_all_to_vec();
|
|
|
@ -116,28 +116,34 @@ pub fn sys_close(fd: usize) -> SysResult {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
pub fn sys_stat(path: *const u8, stat_ptr: *mut Stat) -> SysResult {
|
|
|
|
pub fn sys_stat(path: *const u8, stat_ptr: *mut Stat) -> SysResult {
|
|
|
|
|
|
|
|
warn!("stat is partial implemented as lstat");
|
|
|
|
|
|
|
|
sys_lstat(path, stat_ptr)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pub fn sys_fstat(fd: usize, stat_ptr: *mut Stat) -> SysResult {
|
|
|
|
|
|
|
|
info!("fstat: fd: {}", fd);
|
|
|
|
let mut proc = process();
|
|
|
|
let mut proc = process();
|
|
|
|
let path = unsafe { proc.memory_set.check_and_clone_cstr(path) }
|
|
|
|
|
|
|
|
.ok_or(SysError::EINVAL)?;
|
|
|
|
|
|
|
|
if !proc.memory_set.check_mut_ptr(stat_ptr) {
|
|
|
|
if !proc.memory_set.check_mut_ptr(stat_ptr) {
|
|
|
|
return Err(SysError::EINVAL);
|
|
|
|
return Err(SysError::EINVAL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
info!("stat: path: {}", path);
|
|
|
|
let file = proc.get_file(fd)?;
|
|
|
|
|
|
|
|
let stat = Stat::from(file.info()?);
|
|
|
|
let inode = ROOT_INODE.lookup(path.as_str())?;
|
|
|
|
// TODO: handle symlink
|
|
|
|
let stat = Stat::from(inode.metadata()?);
|
|
|
|
|
|
|
|
unsafe { stat_ptr.write(stat); }
|
|
|
|
unsafe { stat_ptr.write(stat); }
|
|
|
|
Ok(0)
|
|
|
|
Ok(0)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
pub fn sys_fstat(fd: usize, stat_ptr: *mut Stat) -> SysResult {
|
|
|
|
pub fn sys_lstat(path: *const u8, stat_ptr: *mut Stat) -> SysResult {
|
|
|
|
info!("fstat: fd: {}", fd);
|
|
|
|
|
|
|
|
let mut proc = process();
|
|
|
|
let mut proc = process();
|
|
|
|
|
|
|
|
let path = unsafe { proc.memory_set.check_and_clone_cstr(path) }
|
|
|
|
|
|
|
|
.ok_or(SysError::EINVAL)?;
|
|
|
|
if !proc.memory_set.check_mut_ptr(stat_ptr) {
|
|
|
|
if !proc.memory_set.check_mut_ptr(stat_ptr) {
|
|
|
|
return Err(SysError::EINVAL);
|
|
|
|
return Err(SysError::EINVAL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
let file = proc.get_file(fd)?;
|
|
|
|
info!("lstat: path: {}", path);
|
|
|
|
let stat = Stat::from(file.info()?);
|
|
|
|
|
|
|
|
|
|
|
|
let inode = ROOT_INODE.lookup(path.as_str())?;
|
|
|
|
|
|
|
|
let stat = Stat::from(inode.metadata()?);
|
|
|
|
unsafe { stat_ptr.write(stat); }
|
|
|
|
unsafe { stat_ptr.write(stat); }
|
|
|
|
Ok(0)
|
|
|
|
Ok(0)
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -157,27 +163,27 @@ pub fn sys_lseek(fd: usize, offset: i64, whence: u8) -> SysResult {
|
|
|
|
Ok(offset as isize)
|
|
|
|
Ok(offset as isize)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// entry_id = dentry.offset / 256
|
|
|
|
pub fn sys_getdents64(fd: usize, buf: *mut LinuxDirent64, buf_size: usize) -> SysResult {
|
|
|
|
/// dentry.name = entry_name
|
|
|
|
info!("getdents64: fd: {}, ptr: {:?}, buf_size: {}", fd, buf, buf_size);
|
|
|
|
/// dentry.offset += 256
|
|
|
|
|
|
|
|
pub fn sys_getdirentry(fd: usize, dentry_ptr: *mut DirEntry) -> SysResult {
|
|
|
|
|
|
|
|
info!("getdirentry: {}", fd);
|
|
|
|
|
|
|
|
let mut proc = process();
|
|
|
|
let mut proc = process();
|
|
|
|
if !proc.memory_set.check_mut_ptr(dentry_ptr) {
|
|
|
|
if !proc.memory_set.check_mut_array(buf as *mut u8, buf_size) {
|
|
|
|
return Err(SysError::EINVAL);
|
|
|
|
return Err(SysError::EINVAL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
let file = proc.get_file(fd)?;
|
|
|
|
let file = proc.get_file(fd)?;
|
|
|
|
let dentry = unsafe { &mut *dentry_ptr };
|
|
|
|
|
|
|
|
if !dentry.check() {
|
|
|
|
|
|
|
|
return Err(SysError::EINVAL);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
let info = file.info()?;
|
|
|
|
let info = file.info()?;
|
|
|
|
if info.type_ != FileType::Dir || info.size <= dentry.entry_id() {
|
|
|
|
if info.type_ != FileType::Dir {
|
|
|
|
return Err(SysError::EINVAL);
|
|
|
|
return Err(SysError::ENOTDIR);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
let name = file.get_entry(dentry.entry_id())?;
|
|
|
|
let mut writer = unsafe { DirentBufWriter::new(buf, buf_size) };
|
|
|
|
dentry.set_name(name.as_str());
|
|
|
|
loop {
|
|
|
|
Ok(0)
|
|
|
|
let name = match file.read_entry() {
|
|
|
|
|
|
|
|
Err(FsError::EntryNotFound) => break,
|
|
|
|
|
|
|
|
r => r,
|
|
|
|
|
|
|
|
}?;
|
|
|
|
|
|
|
|
let ok = writer.try_write(0, 0, name.as_str());
|
|
|
|
|
|
|
|
if !ok { break; }
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(writer.written_size as isize)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
pub fn sys_dup2(fd1: usize, fd2: usize) -> SysResult {
|
|
|
|
pub fn sys_dup2(fd1: usize, fd2: usize) -> SysResult {
|
|
|
@ -260,23 +266,57 @@ impl OpenFlags {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#[repr(C)]
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct DirEntry {
|
|
|
|
#[repr(packed)] // Don't use 'C'. Or its size will align up to 8 bytes.
|
|
|
|
offset: u32,
|
|
|
|
pub struct LinuxDirent64 {
|
|
|
|
name: [u8; 256],
|
|
|
|
/// Inode number
|
|
|
|
|
|
|
|
ino: u64,
|
|
|
|
|
|
|
|
/// Offset to next structure
|
|
|
|
|
|
|
|
offset: u64,
|
|
|
|
|
|
|
|
/// Size of this dirent
|
|
|
|
|
|
|
|
reclen: u16,
|
|
|
|
|
|
|
|
/// File type
|
|
|
|
|
|
|
|
type_: u8,
|
|
|
|
|
|
|
|
/// Filename (null-terminated)
|
|
|
|
|
|
|
|
name: [u8; 0],
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
impl DirEntry {
|
|
|
|
struct DirentBufWriter {
|
|
|
|
fn check(&self) -> bool {
|
|
|
|
ptr: *mut LinuxDirent64,
|
|
|
|
self.offset % 256 == 0
|
|
|
|
rest_size: usize,
|
|
|
|
}
|
|
|
|
written_size: usize,
|
|
|
|
fn entry_id(&self) -> usize {
|
|
|
|
}
|
|
|
|
(self.offset / 256) as usize
|
|
|
|
|
|
|
|
}
|
|
|
|
impl DirentBufWriter {
|
|
|
|
fn set_name(&mut self, name: &str) {
|
|
|
|
unsafe fn new(buf: *mut LinuxDirent64, size: usize) -> Self {
|
|
|
|
self.name[..name.len()].copy_from_slice(name.as_bytes());
|
|
|
|
DirentBufWriter {
|
|
|
|
self.name[name.len()] = 0;
|
|
|
|
ptr: buf,
|
|
|
|
self.offset += 256;
|
|
|
|
rest_size: size,
|
|
|
|
|
|
|
|
written_size: 0,
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
fn try_write(&mut self, inode: u64, type_: u8, name: &str) -> bool {
|
|
|
|
|
|
|
|
let len = ::core::mem::size_of::<LinuxDirent64>() + name.len() + 1;
|
|
|
|
|
|
|
|
let len = (len + 7) / 8 * 8; // align up
|
|
|
|
|
|
|
|
if self.rest_size < len {
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
let dent = LinuxDirent64 {
|
|
|
|
|
|
|
|
ino: inode,
|
|
|
|
|
|
|
|
offset: 0,
|
|
|
|
|
|
|
|
reclen: len as u16,
|
|
|
|
|
|
|
|
type_,
|
|
|
|
|
|
|
|
name: [],
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
unsafe {
|
|
|
|
|
|
|
|
self.ptr.write(dent);
|
|
|
|
|
|
|
|
let name_ptr = self.ptr.add(1) as _;
|
|
|
|
|
|
|
|
util::write_cstr(name_ptr, name);
|
|
|
|
|
|
|
|
self.ptr = (self.ptr as *const u8).add(len) as _;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
self.rest_size -= len;
|
|
|
|
|
|
|
|
self.written_size += len;
|
|
|
|
|
|
|
|
true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|