From 70d7d10eebb07f68a05d236299ce2d131b549a25 Mon Sep 17 00:00:00 2001 From: LI Qing Date: Tue, 18 Aug 2020 16:20:29 +0800 Subject: [PATCH] Add support to mkdir & rmdir in hostfs --- src/libos/src/fs/hostfs.rs | 8 +++++--- test/hostfs/main.c | 21 +++++++++++++++++++++ 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/src/libos/src/fs/hostfs.rs b/src/libos/src/fs/hostfs.rs index 421f5c28..09091ce7 100644 --- a/src/libos/src/fs/hostfs.rs +++ b/src/libos/src/fs/hostfs.rs @@ -127,8 +127,11 @@ impl INode for HNode { FileType::File => { try_std!(fs::File::create(&new_path)); } + FileType::Dir => { + try_std!(fs::create_dir(&new_path)); + } _ => { - warn!("only support creating regular files in HostFS"); + warn!("only support creating regular file or directory in HostFS"); return Err(FsError::PermError); } } @@ -150,8 +153,7 @@ impl INode for HNode { if new_path.is_file() { try_std!(fs::remove_file(new_path)); } else if new_path.is_dir() { - unimplemented!("no remove_dir in sgx_std?") - // fs::remove_dir(new_path)?; + try_std!(fs::remove_dir(new_path)); } else { return Err(FsError::EntryNotFound); } diff --git a/test/hostfs/main.c b/test/hostfs/main.c index d50976af..9adfed1e 100644 --- a/test/hostfs/main.c +++ b/test/hostfs/main.c @@ -155,6 +155,26 @@ static int test_readdir() { return test_hostfs_framework(__test_readdir); } +static int test_mkdir_then_rmdir() { + const char *dir_path = "/host/hostfs_dir"; + struct stat stat_buf; + + if (mkdir(dir_path, 00775) < 0) { + THROW_ERROR("failed to create the dir"); + } + if (stat(dir_path, &stat_buf) < 0) { + THROW_ERROR("failed to stat dir"); + } + if (!S_ISDIR(stat_buf.st_mode)) { + THROW_ERROR("failed to check if it is dir"); + } + + if (rmdir(dir_path) < 0) { + THROW_ERROR("failed to remove the created dir"); + } + return 0; +} + // ============================================================================ // Test suite main // ============================================================================ @@ -163,6 +183,7 @@ static test_case_t test_cases[] = { TEST_CASE(test_write_read), TEST_CASE(test_rename), TEST_CASE(test_readdir), + TEST_CASE(test_mkdir_then_rmdir), }; int main(int argc, const char *argv[]) {