From bb2dcb28bf0e21e74fa5ec8ae78f54ab3f33c977 Mon Sep 17 00:00:00 2001 From: Krzysztof Piotrowski Date: Sat, 15 Apr 2023 22:33:19 +0000 Subject: [PATCH] compute tmpfiles inside atomically write functions Signed-off-by: Krzysztof Piotrowski --- .../tedge_config_cli/tedge_config_location.rs | 6 ---- .../tedge_config_repository.rs | 1 - crates/common/tedge_utils/src/fs.rs | 29 +++++++++---------- crates/core/tedge_agent/src/state.rs | 6 +--- 4 files changed, 15 insertions(+), 27 deletions(-) diff --git a/crates/common/tedge_config/src/tedge_config_cli/tedge_config_location.rs b/crates/common/tedge_config/src/tedge_config_cli/tedge_config_location.rs index b7ea2b630e7..7cab37d0443 100644 --- a/crates/common/tedge_config/src/tedge_config_cli/tedge_config_location.rs +++ b/crates/common/tedge_config/src/tedge_config_cli/tedge_config_location.rs @@ -5,8 +5,6 @@ use camino::Utf8PathBuf; pub const DEFAULT_TEDGE_CONFIG_PATH: &str = "/etc/tedge"; const TEDGE_CONFIG_FILE: &str = "tedge.toml"; -const TEDGE_CONFIG_FILE_TMP: &str = "tedge.toml.tmp"; - /// Information about where `tedge.toml` is located. /// /// Broadly speaking, we distinguish two different locations: @@ -49,10 +47,6 @@ impl TEdgeConfigLocation { pub fn tedge_config_file_path(&self) -> &Utf8Path { &self.tedge_config_file_path } - - pub fn temporary_tedge_config_file_path(&self) -> Utf8PathBuf { - self.tedge_config_root_path.join(TEDGE_CONFIG_FILE_TMP) - } } #[test] diff --git a/crates/common/tedge_config/src/tedge_config_cli/tedge_config_repository.rs b/crates/common/tedge_config/src/tedge_config_cli/tedge_config_repository.rs index c4e59bab2fc..aa75221a4a5 100644 --- a/crates/common/tedge_config/src/tedge_config_cli/tedge_config_repository.rs +++ b/crates/common/tedge_config/src/tedge_config_cli/tedge_config_repository.rs @@ -92,7 +92,6 @@ impl TEdgeConfigRepository { } atomically_write_file_sync( - self.config_location.temporary_tedge_config_file_path(), self.config_location.tedge_config_file_path(), toml.as_bytes(), )?; diff --git a/crates/common/tedge_utils/src/fs.rs b/crates/common/tedge_utils/src/fs.rs index d2fa6f70536..a88d67cdaad 100644 --- a/crates/common/tedge_utils/src/fs.rs +++ b/crates/common/tedge_utils/src/fs.rs @@ -1,20 +1,20 @@ use std::fs as std_fs; use std::io::Write; use std::path::Path; +use std::path::PathBuf; use tokio::fs as tokio_fs; use tokio::io::AsyncWriteExt; /// Write file to filesystem atomically using std::fs synchronously. -pub fn atomically_write_file_sync( - tempfile: impl AsRef, - dest: impl AsRef, - content: &[u8], -) -> std::io::Result<()> { +pub fn atomically_write_file_sync(dest: impl AsRef, content: &[u8]) -> std::io::Result<()> { + let mut tempfile = PathBuf::from(dest.as_ref()); + tempfile.set_extension("tmp"); + let mut file = std_fs::OpenOptions::new() .write(true) .create_new(true) - .open(tempfile.as_ref())?; + .open(&tempfile)?; if let Err(err) = file.write_all(content) { let _ = std_fs::remove_file(tempfile); @@ -23,7 +23,7 @@ pub fn atomically_write_file_sync( file.flush()?; - if let Err(err) = std_fs::rename(tempfile.as_ref(), dest) { + if let Err(err) = std_fs::rename(&tempfile, dest) { let _ = std_fs::remove_file(tempfile); return Err(err); } @@ -33,14 +33,16 @@ pub fn atomically_write_file_sync( /// Write file to filesystem atomically using tokio::fs asynchronously. pub async fn atomically_write_file_async( - tempfile: impl AsRef, dest: impl AsRef, content: &[u8], ) -> std::io::Result<()> { + let mut tempfile = PathBuf::from(dest.as_ref()); + tempfile.set_extension("tmp"); + let mut file = tokio_fs::OpenOptions::new() .write(true) .create_new(true) - .open(tempfile.as_ref()) + .open(&tempfile) .await?; if let Err(err) = file.write_all(content).await { @@ -50,7 +52,7 @@ pub async fn atomically_write_file_async( file.flush().await?; - if let Err(err) = tokio_fs::rename(tempfile.as_ref(), dest).await { + if let Err(err) = tokio_fs::rename(&tempfile, dest).await { tokio_fs::remove_file(tempfile).await?; return Err(err); } @@ -73,7 +75,7 @@ mod tests { let content = "test_data"; - atomically_write_file_async(&temp_path, &destination_path, content.as_bytes()) + atomically_write_file_async(&destination_path, content.as_bytes()) .await .unwrap(); @@ -88,15 +90,12 @@ mod tests { #[test] fn atomically_write_file_file_sync() { let temp_dir = tempdir().unwrap(); - let temp_path = temp_dir.path().join("test1"); let destination_path = temp_dir.path().join("test2"); let content = "test_data"; - let () = - atomically_write_file_sync(&temp_path, &destination_path, content.as_bytes()).unwrap(); + let () = atomically_write_file_sync(&destination_path, content.as_bytes()).unwrap(); - std::fs::File::open(&temp_path).unwrap_err(); if let Ok(destination_content) = std::fs::read(&destination_path) { assert_eq!(destination_content, content.as_bytes()); } else { diff --git a/crates/core/tedge_agent/src/state.rs b/crates/core/tedge_agent/src/state.rs index 08f4ac8286f..f9fb3fef4c8 100644 --- a/crates/core/tedge_agent/src/state.rs +++ b/crates/core/tedge_agent/src/state.rs @@ -46,11 +46,7 @@ impl StateRepository for AgentStateRepository { fs::create_dir(&self.state_repo_root).await?; } - let mut temppath = self.state_repo_path.clone(); - temppath.set_extension("tmp"); - - let () = - atomically_write_file_async(temppath, &self.state_repo_path, toml.as_bytes()).await?; + let () = atomically_write_file_async(&self.state_repo_path, toml.as_bytes()).await?; Ok(()) }