Fix #725 by checking mode in chdir
This commit is contained in:
parent
1041027772
commit
324dbd52e4
|
@ -3,8 +3,10 @@ use core::sync::atomic::Ordering;
|
||||||
|
|
||||||
use context;
|
use context;
|
||||||
use scheme;
|
use scheme;
|
||||||
use syscall::data::Packet;
|
use syscall;
|
||||||
|
use syscall::data::{Packet, Stat};
|
||||||
use syscall::error::*;
|
use syscall::error::*;
|
||||||
|
use syscall::flag::{MODE_DIR, MODE_FILE};
|
||||||
|
|
||||||
pub fn file_op(a: usize, fd: usize, c: usize, d: usize) -> Result<usize> {
|
pub fn file_op(a: usize, fd: usize, c: usize, d: usize) -> Result<usize> {
|
||||||
let (file, pid, uid, gid) = {
|
let (file, pid, uid, gid) = {
|
||||||
|
@ -47,12 +49,21 @@ pub fn file_op_mut_slice(a: usize, fd: usize, slice: &mut [u8]) -> Result<usize>
|
||||||
|
|
||||||
/// Change the current working directory
|
/// Change the current working directory
|
||||||
pub fn chdir(path: &[u8]) -> Result<usize> {
|
pub fn chdir(path: &[u8]) -> Result<usize> {
|
||||||
let contexts = context::contexts();
|
let fd = open(path, 0)?;
|
||||||
let context_lock = contexts.current().ok_or(Error::new(ESRCH))?;
|
let mut stat = Stat::default();
|
||||||
let context = context_lock.read();
|
let stat_res = file_op_mut_slice(syscall::number::SYS_FSTAT, fd, &mut stat);
|
||||||
let canonical = context.canonicalize(path);
|
let _ = close(fd);
|
||||||
*context.cwd.lock() = canonical;
|
stat_res?;
|
||||||
Ok(0)
|
if stat.st_mode & (MODE_FILE | MODE_DIR) == MODE_DIR {
|
||||||
|
let contexts = context::contexts();
|
||||||
|
let context_lock = contexts.current().ok_or(Error::new(ESRCH))?;
|
||||||
|
let context = context_lock.read();
|
||||||
|
let canonical = context.canonicalize(path);
|
||||||
|
*context.cwd.lock() = canonical;
|
||||||
|
Ok(0)
|
||||||
|
} else {
|
||||||
|
Err(Error::new(ENOTDIR))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Get the current working directory
|
/// Get the current working directory
|
||||||
|
|
Loading…
Reference in a new issue