diff options
author | Lukasz Woznicki <75632179+makr11st@users.noreply.github.com> | 2021-08-04 14:28:37 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-08-04 14:28:37 +0100 |
commit | 2ccbf41390d24a8df5bb1c8914ac70e429b2d9e4 (patch) | |
tree | ea73b28c2657786f2d86e45db3d5ec3bc18a815e /tedge_config | |
parent | f942b1c83d6c2db376bae4682b25f9bea1c0a90f (diff) |
Add async version of atomically write file and move it to tedge_utils (#364)
* Add async version of atomically write file and move it to tedge_utils
Signed-off-by: Lukasz Woznicki <lukasz.woznicki@softwareag.com>
Diffstat (limited to 'tedge_config')
-rw-r--r-- | tedge_config/Cargo.toml | 1 | ||||
-rw-r--r-- | tedge_config/src/tedge_config_repository.rs | 26 |
2 files changed, 4 insertions, 23 deletions
diff --git a/tedge_config/Cargo.toml b/tedge_config/Cargo.toml index a1e635a5..903eb9c0 100644 --- a/tedge_config/Cargo.toml +++ b/tedge_config/Cargo.toml @@ -7,6 +7,7 @@ edition = "2018" [dependencies] certificate = { path = "../common/certificate" } serde = { version = "1.0", features = ["derive"] } +tedge_utils = { path = "../common/tedge_utils" } tempfile = "3.2" thiserror = "1.0" toml = "0.5" diff --git a/tedge_config/src/tedge_config_repository.rs b/tedge_config/src/tedge_config_repository.rs index 26597189..790efb29 100644 --- a/tedge_config/src/tedge_config_repository.rs +++ b/tedge_config/src/tedge_config_repository.rs @@ -1,7 +1,7 @@ use crate::*; use std::fs; -use std::io::Write; -use std::path::{Path, PathBuf}; +use std::path::PathBuf; +use tedge_utils::fs::atomically_write_file_sync; /// TEdgeConfigRepository is resposible for loading and storing TEdgeConfig entities. /// @@ -34,7 +34,7 @@ impl ConfigRepository<TEdgeConfig> for TEdgeConfigRepository { let () = fs::create_dir(self.config_location.tedge_config_root_path())?; } - let () = atomically_write_file( + let () = atomically_write_file_sync( self.config_location.temporary_tedge_config_file_path(), self.config_location.tedge_config_file_path(), toml.as_bytes(), @@ -43,26 +43,6 @@ impl ConfigRepository<TEdgeConfig> for TEdgeConfigRepository { } } -fn atomically_write_file( - tempfile: impl AsRef<Path>, - dest: impl AsRef<Path>, - content: &[u8], -) -> std::io::Result<()> { - let mut file = fs::OpenOptions::new() - .write(true) - .create_new(true) - .open(tempfile.as_ref())?; - if let Err(err) = file.write_all(content) { - let _ = fs::remove_file(tempfile); - return Err(err); - } - if let Err(err) = fs::rename(tempfile.as_ref(), dest) { - let _ = fs::remove_file(tempfile); - return Err(err); - } - Ok(()) -} - impl TEdgeConfigRepository { pub fn new(config_location: TEdgeConfigLocation) -> Self { let config_defaults = TEdgeConfigDefaults::from(&config_location); |