Add sync_all, sync_data and poll for hostfs file

This commit is contained in:
LI Qing 2021-02-03 09:50:55 +08:00 committed by Zongmin.Gu
parent 7122529b6b
commit 3dbf050904
2 changed files with 69 additions and 3 deletions

@ -90,7 +90,15 @@ impl INode for HNode {
}
fn poll(&self) -> Result<PollStatus> {
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<Metadata> {
@ -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(())
}

@ -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),