Add support to read directory in hostfs

This commit is contained in:
LI Qing 2020-08-18 15:23:54 +08:00
parent dcad3ea1d9
commit 7d31cb743c
2 changed files with 54 additions and 11 deletions

@ -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<()> {

@ -1,8 +1,11 @@
#include <sys/stat.h>
#include <errno.h>
#include <fcntl.h>
#include <dirent.h>
#include <libgen.h>
#include <unistd.h>
#include <string.h>
#include <stdbool.h>
#include <stdio.h>
#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[]) {