From 7d31cb743c874b93f5e5f805eb7b6af624cbf0f3 Mon Sep 17 00:00:00 2001 From: LI Qing Date: Tue, 18 Aug 2020 15:23:54 +0800 Subject: [PATCH] Add support to read directory in hostfs --- src/libos/src/fs/hostfs.rs | 19 +++++++--------- test/hostfs/main.c | 46 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+), 11 deletions(-) diff --git a/src/libos/src/fs/hostfs.rs b/src/libos/src/fs/hostfs.rs index ebf14610..421f5c28 100644 --- a/src/libos/src/fs/hostfs.rs +++ b/src/libos/src/fs/hostfs.rs @@ -184,17 +184,14 @@ impl INode for HNode { if !self.path.is_dir() { return Err(FsError::NotDir); } - unimplemented!("no read_dir in sgx_std?") - // FIXME: read_dir - - // self.path - // .read_dir() - // .map_err(|_| FsError::NotDir)? - // .nth(id) - // .map_err(|_| FsError::EntryNotFound)? - // .file_name() - // .into_string() - // .map_err(|_| FsError::InvalidParam) + if let Some(entry) = try_std!(self.path.read_dir()).nth(id) { + try_std!(entry) + .file_name() + .into_string() + .map_err(|_| FsError::InvalidParam) + } else { + return Err(FsError::EntryNotFound); + } } fn io_control(&self, cmd: u32, data: usize) -> Result<()> { diff --git a/test/hostfs/main.c b/test/hostfs/main.c index 1f2abc26..d50976af 100644 --- a/test/hostfs/main.c +++ b/test/hostfs/main.c @@ -1,8 +1,11 @@ #include #include #include +#include +#include #include #include +#include #include #include "test.h" @@ -85,6 +88,44 @@ static int __test_rename(const char *file_path) { return 0; } +static int __test_readdir(const char *file_path) { + struct dirent *dp; + DIR *dirp; + char base_buf[128] = { 0 }; + char *base_name; + bool found = false; + int ret; + + ret = snprintf(base_buf, sizeof(base_buf), "%s", file_path); + if (ret >= sizeof(base_buf) || ret < 0) { + THROW_ERROR("failed to copy file path to the base buffer"); + } + base_name = basename(base_buf); + + dirp = opendir("/host"); + if (dirp == NULL) { + THROW_ERROR("failed to open host directory"); + } + while (1) { + errno = 0; + dp = readdir(dirp); + if (dp == NULL) { + if (errno != 0) { + THROW_ERROR("faild to call readdir"); + } + break; + } + if (strncmp(base_name, dp->d_name, strlen(base_name)) == 0) { + found = true; + } + } + if (!found) { + THROW_ERROR("faild to read file entry"); + } + closedir(dirp); + return 0; +} + typedef int(*test_hostfs_func_t)(const char *); static int test_hostfs_framework(test_hostfs_func_t fn) { @@ -110,6 +151,10 @@ static int test_rename() { return test_hostfs_framework(__test_rename); } +static int test_readdir() { + return test_hostfs_framework(__test_readdir); +} + // ============================================================================ // Test suite main // ============================================================================ @@ -117,6 +162,7 @@ static int test_rename() { static test_case_t test_cases[] = { TEST_CASE(test_write_read), TEST_CASE(test_rename), + TEST_CASE(test_readdir), }; int main(int argc, const char *argv[]) {