From 96876b29357b50d00bd65bf42d9d59b4ff16326f Mon Sep 17 00:00:00 2001 From: LI Qing Date: Thu, 12 Mar 2020 15:54:51 +0800 Subject: [PATCH] Add rename for hostfs --- src/libos/src/fs/hostfs.rs | 8 +- test/hostfs/main.c | 147 ++++++++++++++++++++++++++++--------- 2 files changed, 119 insertions(+), 36 deletions(-) diff --git a/src/libos/src/fs/hostfs.rs b/src/libos/src/fs/hostfs.rs index 0e7b5a6d..2d023110 100644 --- a/src/libos/src/fs/hostfs.rs +++ b/src/libos/src/fs/hostfs.rs @@ -156,7 +156,13 @@ impl INode for HNode { } fn move_(&self, old_name: &str, target: &Arc, new_name: &str) -> Result<()> { - unimplemented!() + let old_path = self.path.join(old_name); + let new_path = { + let target = target.downcast_ref::().ok_or(FsError::NotSameFs)?; + target.path.join(new_name) + }; + try_std!(fs::rename(&old_path, &new_path)); + Ok(()) } fn find(&self, name: &str) -> Result> { diff --git a/test/hostfs/main.c b/test/hostfs/main.c index 5c63a536..aabfa36f 100644 --- a/test/hostfs/main.c +++ b/test/hostfs/main.c @@ -1,44 +1,121 @@ -#include -#include +#include +#include #include #include #include #include +#include "test.h" -int main(int argc, const char* argv[]) { - int fd, len; - // TODO: rewrite this test so that it does not need to depend on sample.txt -#if 0 - char read_buf[128] = {0}; - // read - if ((fd = open("/host/hostfs/sample.txt", O_RDONLY)) < 0) { - printf("ERROR: failed to open a file for read\n"); - return -1; - } - if ((len = read(fd, read_buf, sizeof(read_buf) - 1)) <= 0) { - printf("ERROR: failed to read from the file\n"); - return -1; +// ============================================================================ +// Helper function +// ============================================================================ + +static int create_file(const char *file_path) { + int fd; + int flags = O_RDONLY | O_CREAT| O_TRUNC; + int mode = 00666; + fd = open(file_path, flags, mode); + if (fd < 0) { + THROW_ERROR("failed to create a file"); } close(fd); - - if (strcmp("HostFS works!", read_buf) != 0) { - printf("ERROR: the message read from the file is not expected\n"); - return -1; - } - printf("Read file from hostfs successfully!\n"); -#endif - // write - if ((fd = open("/host/hostfs_test_write.txt", O_WRONLY | O_CREAT)) < 0) { - printf("ERROR: failed to open a file for write\n"); - return -1; - } - const char WRITE_STR[] = "Write to hostfs successfully!"; - if ((len = write(fd, WRITE_STR, sizeof(WRITE_STR))) <= 0) { - printf("ERROR: failed to write to the file\n"); - return -1; - } - close(fd); - - printf("Write file to hostfs finished. Please check its content.\n"); return 0; } + +static int remove_file(const char *file_path) { + int ret; + ret = unlink(file_path); + if (ret < 0) { + THROW_ERROR("failed to unlink the created file"); + } + return 0; +} + +// ============================================================================ +// Test cases for hostfs +// ============================================================================ + +static int __test_write_read(const char *file_path) { + char *write_str = "Write to hostfs successfully!"; + char read_buf[128] = { 0 }; + int fd; + + fd = open(file_path, O_WRONLY); + if (fd < 0) { + THROW_ERROR("failed to open a file to write"); + } + if (write(fd, write_str, strlen(write_str)) <= 0) { + THROW_ERROR("failed to write to the file"); + } + close(fd); + fd = open(file_path, O_RDONLY); + if (fd < 0) { + THROW_ERROR("failed to open a file to read"); + } + if (read(fd, read_buf, sizeof(read_buf)) != strlen(write_str)) { + THROW_ERROR("failed to read to the file"); + } + if (strcmp(write_str, read_buf) != 0) { + THROW_ERROR("the message read from the file is not as it was written"); + } + close(fd); + return 0; +} + +static int __test_rename(const char *file_path) { + char *rename_path = "/host/hostfs_rename.txt"; + struct stat stat_buf; + int ret; + + ret = rename(file_path, rename_path); + if (ret < 0) { + THROW_ERROR("failed to rename"); + } + ret = stat(file_path, &stat_buf); + if (!(ret < 0 && errno == ENOENT)) { + THROW_ERROR("stat should return ENOENT"); + } + ret = stat(rename_path, &stat_buf); + if (ret < 0) { + THROW_ERROR("failed to stat the file"); + } + if (rename(rename_path, file_path) < 0) { + THROW_ERROR("failed to rename back"); + } + return 0; +} + +typedef int(*test_hostfs_func_t)(const char *); + +static int test_hostfs_framework(test_hostfs_func_t fn) { + const char *file_path = "/host/hostfs_test.txt"; + + if (create_file(file_path) < 0) + return -1; + if (fn(file_path) < 0) + return -1; + if (remove_file(file_path) < 0) + return -1; + return 0; +} + +static int test_write_read() { + return test_hostfs_framework(__test_write_read); +} + +static int test_rename() { + return test_hostfs_framework(__test_rename); +} + +// ============================================================================ +// Test suite main +// ============================================================================ + +static test_case_t test_cases[] = { + TEST_CASE(test_write_read), + TEST_CASE(test_rename), +}; + +int main(int argc, const char *argv[]) { + return test_suite_run(test_cases, ARRAY_SIZE(test_cases)); +}