diff --git a/src/libos/src/fs/hostfs.rs b/src/libos/src/fs/hostfs.rs index 09091ce7..6bf0fda1 100644 --- a/src/libos/src/fs/hostfs.rs +++ b/src/libos/src/fs/hostfs.rs @@ -90,7 +90,15 @@ impl INode for HNode { } fn poll(&self) -> Result { - unimplemented!() + let metadata = try_std!(self.path.metadata()); + if !metadata.is_file() { + return Err(FsError::NotFile); + } + Ok(PollStatus { + read: true, + write: !metadata.permissions().readonly(), + error: false, + }) } fn metadata(&self) -> Result { @@ -104,12 +112,16 @@ impl INode for HNode { } fn sync_all(&self) -> Result<()> { - warn!("HostFS: sync_all() is unimplemented"); + let mut guard = self.open_file()?; + let file = guard.as_mut().unwrap(); + try_std!(file.sync_all()); Ok(()) } fn sync_data(&self) -> Result<()> { - warn!("HostFS: sync_data() is unimplemented"); + let mut guard = self.open_file()?; + let file = guard.as_mut().unwrap(); + try_std!(file.sync_data()); Ok(()) } diff --git a/test/hostfs/main.c b/test/hostfs/main.c index 611c00fc..7e4a3149 100644 --- a/test/hostfs/main.c +++ b/test/hostfs/main.c @@ -53,6 +53,50 @@ static int __test_write_read(const char *file_path) { return 0; } +static int __test_write_fdatasync_read(const char *file_path) { + char *write_str = "Write to hostfs and fdatasync successfully!"; + 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"); + } + if (fdatasync(fd) < 0) { + THROW_ERROR("failed to sync data into file"); + } + close(fd); + + if (fs_check_file_content(file_path, write_str) < 0) { + THROW_ERROR("failed to check file content"); + } + return 0; +} + +static int __test_write_fsync_read(const char *file_path) { + char *write_str = "Write to hostfs and fsync successfully!"; + 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"); + } + if (fsync(fd) < 0) { + THROW_ERROR("failed to sync all into file"); + } + close(fd); + + if (fs_check_file_content(file_path, write_str) < 0) { + THROW_ERROR("failed to check file content"); + } + return 0; +} + static int __test_rename(const char *file_path) { char *rename_path = "/host/hostfs_rename.txt"; struct stat stat_buf; @@ -131,6 +175,14 @@ static int test_write_read() { return test_hostfs_framework(__test_write_read); } +static int test_write_fdatasync_read() { + return test_hostfs_framework(__test_write_fdatasync_read); +} + +static int test_write_fsync_read() { + return test_hostfs_framework(__test_write_fsync_read); +} + static int test_rename() { return test_hostfs_framework(__test_rename); } @@ -165,6 +217,8 @@ static int test_mkdir_then_rmdir() { static test_case_t test_cases[] = { TEST_CASE(test_write_read), + TEST_CASE(test_write_fdatasync_read), + TEST_CASE(test_write_fsync_read), TEST_CASE(test_rename), TEST_CASE(test_readdir), TEST_CASE(test_mkdir_then_rmdir),