tiny refactor: convert 'get_file' & 'get_socket' to method

master
WangRunji 6 years ago
parent 6ea04c91b2
commit 1b104029ce

@ -15,7 +15,7 @@ pub fn sys_read(fd: usize, base: *mut u8, len: usize) -> SysResult {
return Err(SysError::EINVAL);
}
let slice = unsafe { slice::from_raw_parts_mut(base, len) };
let len = get_file(&mut proc, fd)?.read(slice)?;
let len = proc.get_file(fd)?.read(slice)?;
Ok(len as isize)
}
@ -32,9 +32,9 @@ pub fn sys_write(fd: usize, base: *const u8, len: usize) -> SysResult {
}
}
pub fn sys_write_file(proc: &mut MutexGuard<'static, Process>, fd: usize, base: *const u8, len: usize) -> SysResult {
pub fn sys_write_file(proc: &mut Process, fd: usize, base: *const u8, len: usize) -> SysResult {
let slice = unsafe { slice::from_raw_parts(base, len) };
let len = get_file(proc, fd)?.write(slice)?;
let len = proc.get_file(fd)?.write(slice)?;
Ok(len as isize)
}
@ -44,7 +44,7 @@ pub fn sys_readv(fd: usize, iov_ptr: *const IoVec, iov_count: usize) -> SysResul
info!("readv: fd: {}, iov: {:#x?}", fd, iovs);
// read all data to a buf
let file = get_file(&mut proc, fd)?;
let file = proc.get_file(fd)?;
let mut buf = iovs.new_buf(true);
let len = file.read(buf.as_mut_slice())?;
// copy data to user
@ -57,7 +57,7 @@ pub fn sys_writev(fd: usize, iov_ptr: *const IoVec, iov_count: usize) -> SysResu
let iovs = IoVecs::check_and_new(iov_ptr, iov_count, &proc.memory_set, false)?;
info!("writev: fd: {}, iovs: {:#x?}", fd, iovs);
let file = get_file(&mut proc, fd)?;
let file = proc.get_file(fd)?;
let buf = iovs.read_all_to_vec();
let len = file.write(buf.as_slice())?;
Ok(len as isize)
@ -134,7 +134,7 @@ pub fn sys_fstat(fd: usize, stat_ptr: *mut Stat) -> SysResult {
if !proc.memory_set.check_mut_ptr(stat_ptr) {
return Err(SysError::EINVAL);
}
let file = get_file(&mut proc, fd)?;
let file = proc.get_file(fd)?;
let stat = Stat::from(file.info()?);
unsafe { stat_ptr.write(stat); }
Ok(0)
@ -150,7 +150,7 @@ pub fn sys_lseek(fd: usize, offset: i64, whence: u8) -> SysResult {
info!("lseek: fd: {}, pos: {:?}", fd, pos);
let mut proc = process();
let file = get_file(&mut proc, fd)?;
let file = proc.get_file(fd)?;
let offset = file.seek(pos)?;
Ok(offset as isize)
}
@ -164,7 +164,7 @@ pub fn sys_getdirentry(fd: usize, dentry_ptr: *mut DirEntry) -> SysResult {
if !proc.memory_set.check_mut_ptr(dentry_ptr) {
return Err(SysError::EINVAL);
}
let file = get_file(&mut proc, fd)?;
let file = proc.get_file(fd)?;
let dentry = unsafe { &mut *dentry_ptr };
if !dentry.check() {
return Err(SysError::EINVAL);
@ -184,19 +184,22 @@ pub fn sys_dup2(fd1: usize, fd2: usize) -> SysResult {
if proc.files.contains_key(&fd2) {
return Err(SysError::EINVAL);
}
let file = get_file(&mut proc, fd1)?.clone();
let file = proc.get_file(fd1)?.clone();
proc.files.insert(fd2, FileLike::File(file));
Ok(0)
}
fn get_file<'a>(proc: &'a mut MutexGuard<'static, Process>, fd: usize) -> Result<&'a mut FileHandle, SysError> {
proc.files.get_mut(&fd).ok_or(SysError::EINVAL).and_then(|f| {
impl Process {
fn get_file(&mut self, fd: usize) -> Result<&mut FileHandle, SysError> {
self.files.get_mut(&fd).ok_or(SysError::EBADF).and_then(|f| {
match f {
FileLike::File(file) => Ok(file),
_ => Err(SysError::EINVAL)
_ => Err(SysError::EBADF)
}
})
}
}
impl From<FsError> for SysError {
fn from(error: FsError) -> Self {

@ -82,15 +82,13 @@ struct SockaddrIn {
sin_zero: [u8; 8],
}
fn get_handle(proc: &mut MutexGuard<'static, Process>, fd: usize) -> Result<SocketHandle, SysError> {
if let Some(file) = proc.files.get(&fd) {
if let FileLike::Socket(handle) = file {
return Ok((*handle).clone());
} else {
Err(SysError::ENOTSOCK)
impl Process {
fn get_handle(&mut self, fd: usize) -> Result<SocketHandle, SysError> {
let file = self.files.get_mut(&fd).ok_or(SysError::EBADF)?;
match file {
FileLike::Socket(handle) => Ok(handle.clone()),
_ => Err(SysError::ENOTSOCK),
}
} else {
Err(SysError::EBADF)
}
}
@ -123,7 +121,7 @@ pub fn sys_connect(fd: usize, addr: *const u8, addrlen: usize) -> SysResult {
iface.poll(&mut proc.sockets);
// TODO: check its type
let tcp_handle = get_handle(&mut proc, fd)?;
let tcp_handle = proc.get_handle(fd)?;
let mut socket = proc.sockets.get::<TcpSocket>(tcp_handle);
// TODO selects non-conflict high port
@ -144,7 +142,7 @@ pub fn sys_connect(fd: usize, addr: *const u8, addrlen: usize) -> SysResult {
}
pub fn sys_write_socket(
proc: &mut MutexGuard<'static, Process>,
proc: &mut Process,
fd: usize,
base: *const u8,
len: usize,
@ -154,7 +152,7 @@ pub fn sys_write_socket(
iface.poll(&mut proc.sockets);
// TODO: check its type
let tcp_handle = get_handle(proc, fd)?;
let tcp_handle = proc.get_handle(fd)?;
let mut socket = proc.sockets.get::<TcpSocket>(tcp_handle);
let slice = unsafe { slice::from_raw_parts(base, len) };
if socket.is_open() {

Loading…
Cancel
Save