Fix the issue about fsync on hostfs's dir

There are no sync methods about untrusted dir, so we do nothing.
This commit is contained in:
LI Qing 2022-07-12 17:52:55 +08:00 committed by volcano
parent 016ef88535
commit 1dc2b517fc
2 changed files with 25 additions and 6 deletions

@ -125,16 +125,24 @@ impl INode for HNode {
} }
fn sync_all(&self) -> Result<()> { fn sync_all(&self) -> Result<()> {
if self.path.is_file() {
let mut guard = self.open_file()?; let mut guard = self.open_file()?;
let file = guard.as_mut().unwrap(); let file = guard.as_mut().unwrap();
try_std!(file.sync_all()); try_std!(file.sync_all());
} else {
warn!("no sync_all method about dir, do nothing");
}
Ok(()) Ok(())
} }
fn sync_data(&self) -> Result<()> { fn sync_data(&self) -> Result<()> {
if self.path.is_file() {
let mut guard = self.open_file()?; let mut guard = self.open_file()?;
let file = guard.as_mut().unwrap(); let file = guard.as_mut().unwrap();
try_std!(file.sync_data()); try_std!(file.sync_data());
} else {
warn!("no sync_data method about dir, do nothing");
}
Ok(()) Ok(())
} }

@ -213,6 +213,7 @@ static int test_truncate() {
static int test_mkdir_then_rmdir() { static int test_mkdir_then_rmdir() {
const char *dir_path = "/host/hostfs_dir"; const char *dir_path = "/host/hostfs_dir";
struct stat stat_buf; struct stat stat_buf;
int dir_fd;
if (mkdir(dir_path, 00775) < 0) { if (mkdir(dir_path, 00775) < 0) {
THROW_ERROR("failed to create the dir"); THROW_ERROR("failed to create the dir");
@ -224,6 +225,16 @@ static int test_mkdir_then_rmdir() {
THROW_ERROR("failed to check if it is dir"); THROW_ERROR("failed to check if it is dir");
} }
dir_fd = open(dir_path, O_RDONLY | O_DIRECTORY);
if (dir_fd < 0) {
THROW_ERROR("failed to open dir");
}
if (fsync(dir_fd) < 0) {
THROW_ERROR("failed to fsync dir");
}
close(dir_fd);
if (rmdir(dir_path) < 0) { if (rmdir(dir_path) < 0) {
THROW_ERROR("failed to remove the created dir"); THROW_ERROR("failed to remove the created dir");
} }