fix split_path. add test for getcwd, mkdir, rmdir, chdir.
This commit is contained in:
parent
002d1f1dd2
commit
e095e8a4f0
@ -248,6 +248,7 @@ pub fn do_dup3(old_fd: FileDesc, new_fd: FileDesc, flags: u32) -> Result<FileDes
|
||||
}
|
||||
|
||||
pub fn do_sync() -> 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)
|
||||
}
|
||||
|
||||
|
@ -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
|
||||
|
||||
|
5
test/mkdir/Makefile
Normal file
5
test/mkdir/Makefile
Normal file
@ -0,0 +1,5 @@
|
||||
include ../test_common.mk
|
||||
|
||||
EXTRA_C_FLAGS :=
|
||||
EXTRA_LINK_FLAGS :=
|
||||
BIN_ARGS :=
|
67
test/mkdir/main.c
Normal file
67
test/mkdir/main.c
Normal file
@ -0,0 +1,67 @@
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
#include <unistd.h>
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
#include <errno.h>
|
||||
|
||||
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;
|
||||
}
|
Loading…
Reference in New Issue
Block a user