From bdc7e9d1454cf4f5b849be2165d9f2a7e1556264 Mon Sep 17 00:00:00 2001 From: Wolfgang Bumiller Date: Thu, 21 Oct 2021 12:42:20 +0200 Subject: [PATCH] proxmox: cleanup files on fsync errors Signed-off-by: Wolfgang Bumiller --- proxmox/src/tools/fs.rs | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/proxmox/src/tools/fs.rs b/proxmox/src/tools/fs.rs index 29233ddf..8e7550a3 100644 --- a/proxmox/src/tools/fs.rs +++ b/proxmox/src/tools/fs.rs @@ -184,7 +184,10 @@ pub fn replace_file>( if fsync { // make sure data is on disk - nix::unistd::fsync(file.as_raw_fd())?; + if let Err(err) = nix::unistd::fsync(file.as_raw_fd()) { + let _ = unistd::unlink(&tmp_path); + bail!("fsync failed: {}", err); + } } if let Err(err) = std::fs::rename(&tmp_path, &path) { @@ -261,7 +264,14 @@ pub fn atomic_open_or_create_file>( })?; if fsync { // make sure the initial_data is on disk - nix::unistd::fsync(file.as_raw_fd())?; + if let Err(err) = nix::unistd::fsync(file.as_raw_fd()) { + let _ = nix::unistd::unlink(&temp_file_name); + bail!( + "fsync of initial data to {:?} failed - {}", + temp_file_name, + err, + ) + } } }