diff --git a/src/libos/src/fs/mod.rs b/src/libos/src/fs/mod.rs index 823149c5..2941fafd 100644 --- a/src/libos/src/fs/mod.rs +++ b/src/libos/src/fs/mod.rs @@ -248,6 +248,7 @@ pub fn do_dup3(old_fd: FileDesc, new_fd: FileDesc, flags: u32) -> Result Result<(), Error> { + info!("sync:"); ROOT_INODE.fs().sync()?; Ok(()) } @@ -255,6 +256,8 @@ pub fn do_sync() -> Result<(), Error> { pub fn do_chdir(path: &str) -> Result<(), Error> { let current_ref = process::get_current(); let mut current_process = current_ref.lock().unwrap(); + info!("chdir: path: {:?}", path); + let inode = current_process.lookup_inode(path)?; let info = inode.metadata()?; if info.type_ != FileType::Dir { @@ -364,7 +367,10 @@ impl Process { fn split_path(path: &str) -> (&str, &str) { let mut split = path.trim_end_matches('/').rsplitn(2, '/'); let file_name = split.next().unwrap(); - let dir_path = split.next().unwrap_or("."); + let mut dir_path = split.next().unwrap_or("."); + if dir_path == "" { + dir_path = "/"; + } (dir_path, file_name) } diff --git a/test/Makefile b/test/Makefile index 5075bddb..398744d5 100644 --- a/test/Makefile +++ b/test/Makefile @@ -4,7 +4,7 @@ PROJECT_DIR := $(realpath $(CUR_DIR)/../) # Dependencies: need to be compiled but not to run by any Makefile target TEST_DEPS := dev_null # Tests: need to be compiled and run by test-% target -TESTS := empty argv hello_world malloc file getpid spawn pipe time truncate readdir +TESTS := empty argv hello_world malloc file getpid spawn pipe time truncate readdir mkdir # Benchmarks: need to be compiled and run by bench-% target BENCHES := spawn_and_exit_latency pipe_throughput diff --git a/test/mkdir/Makefile b/test/mkdir/Makefile new file mode 100644 index 00000000..9e1b6dec --- /dev/null +++ b/test/mkdir/Makefile @@ -0,0 +1,5 @@ +include ../test_common.mk + +EXTRA_C_FLAGS := +EXTRA_LINK_FLAGS := +BIN_ARGS := diff --git a/test/mkdir/main.c b/test/mkdir/main.c new file mode 100644 index 00000000..a5c83b8d --- /dev/null +++ b/test/mkdir/main.c @@ -0,0 +1,67 @@ +#include +#include +#include +#include +#include +#include + +int main(int argc, const char* argv[]) { + + const int BUF_SIZE = 10; + char buf[10]; + int ret; + + char* cwd = getcwd(buf, BUF_SIZE); + if(cwd != buf) { + printf("failed to getcwd\n"); + return -1; + } + + const char expect_cwd[] = "/"; + if(strcmp(buf, expect_cwd)) { + printf("incorrect cwd \"%s\". expect \"%s\".\n", buf, expect_cwd); + return -1; + } + + const char DIR_NAME[] = "test_dir"; + const char DIR_PATH[] = "/test_dir"; + const int DIR_MODE = 0664; + ret = mkdir(DIR_NAME, DIR_MODE); + if(ret < 0) { + printf("failed to mkdir \"%s\"", DIR_NAME); + return ret; + } + + ret = chdir(DIR_NAME); + if(ret < 0) { + printf("failed to chdir to \"%s\"", DIR_NAME); + return ret; + } + + cwd = getcwd(buf, BUF_SIZE); + if(cwd != buf) { + printf("failed to getcwd\n"); + return -1; + } + + if(strcmp(buf, DIR_PATH)) { + printf("incorrect cwd \"%s\". expect \"%s\".\n", buf, DIR_PATH); + return -1; + } + + ret = rmdir(DIR_PATH); + if(ret < 0) { + printf("failed to rmdir \"%s\"", DIR_PATH); + return ret; + } + + struct stat stat_buf; + ret = stat(DIR_PATH, &stat_buf); + if(!(ret < 0 && errno == ENOENT)) { + printf("stat on \"%s\" should return ENOENT", DIR_PATH); + return ret; + } + + printf("getcwd, mkdir, rmdir, chdir test successful\n"); + return 0; +}