Add support to read directory in hostfs
This commit is contained in:
parent
dcad3ea1d9
commit
7d31cb743c
@ -184,17 +184,14 @@ impl INode for HNode {
|
|||||||
if !self.path.is_dir() {
|
if !self.path.is_dir() {
|
||||||
return Err(FsError::NotDir);
|
return Err(FsError::NotDir);
|
||||||
}
|
}
|
||||||
unimplemented!("no read_dir in sgx_std?")
|
if let Some(entry) = try_std!(self.path.read_dir()).nth(id) {
|
||||||
// FIXME: read_dir
|
try_std!(entry)
|
||||||
|
.file_name()
|
||||||
// self.path
|
.into_string()
|
||||||
// .read_dir()
|
.map_err(|_| FsError::InvalidParam)
|
||||||
// .map_err(|_| FsError::NotDir)?
|
} else {
|
||||||
// .nth(id)
|
return Err(FsError::EntryNotFound);
|
||||||
// .map_err(|_| FsError::EntryNotFound)?
|
}
|
||||||
// .file_name()
|
|
||||||
// .into_string()
|
|
||||||
// .map_err(|_| FsError::InvalidParam)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn io_control(&self, cmd: u32, data: usize) -> Result<()> {
|
fn io_control(&self, cmd: u32, data: usize) -> Result<()> {
|
||||||
|
@ -1,8 +1,11 @@
|
|||||||
#include <sys/stat.h>
|
#include <sys/stat.h>
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
|
#include <dirent.h>
|
||||||
|
#include <libgen.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
#include <stdbool.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include "test.h"
|
#include "test.h"
|
||||||
|
|
||||||
@ -85,6 +88,44 @@ static int __test_rename(const char *file_path) {
|
|||||||
return 0;
|
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 *);
|
typedef int(*test_hostfs_func_t)(const char *);
|
||||||
|
|
||||||
static int test_hostfs_framework(test_hostfs_func_t fn) {
|
static int test_hostfs_framework(test_hostfs_func_t fn) {
|
||||||
@ -110,6 +151,10 @@ static int test_rename() {
|
|||||||
return test_hostfs_framework(__test_rename);
|
return test_hostfs_framework(__test_rename);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static int test_readdir() {
|
||||||
|
return test_hostfs_framework(__test_readdir);
|
||||||
|
}
|
||||||
|
|
||||||
// ============================================================================
|
// ============================================================================
|
||||||
// Test suite main
|
// Test suite main
|
||||||
// ============================================================================
|
// ============================================================================
|
||||||
@ -117,6 +162,7 @@ static int test_rename() {
|
|||||||
static test_case_t test_cases[] = {
|
static test_case_t test_cases[] = {
|
||||||
TEST_CASE(test_write_read),
|
TEST_CASE(test_write_read),
|
||||||
TEST_CASE(test_rename),
|
TEST_CASE(test_rename),
|
||||||
|
TEST_CASE(test_readdir),
|
||||||
};
|
};
|
||||||
|
|
||||||
int main(int argc, const char *argv[]) {
|
int main(int argc, const char *argv[]) {
|
||||||
|
Loading…
Reference in New Issue
Block a user