From a078c407ca809bb9e2768a6cb259a891bf2e9549 Mon Sep 17 00:00:00 2001 From: Tim Bruijnzeels Date: Fri, 18 Feb 2022 12:32:13 +0100 Subject: [PATCH] Rename and document file functions for readability. --- src/commons/eventsourcing/kv.rs | 4 ++-- src/commons/util/file.rs | 8 ++++++-- src/upgrades/mod.rs | 2 +- 3 files changed, 9 insertions(+), 5 deletions(-) diff --git a/src/commons/eventsourcing/kv.rs b/src/commons/eventsourcing/kv.rs index 4df7d94e..ce80c53c 100644 --- a/src/commons/eventsourcing/kv.rs +++ b/src/commons/eventsourcing/kv.rs @@ -97,7 +97,7 @@ impl KeyValueStore { KeyValueStore::Disk(disk_store) => { // If this is a new store then initialise the disk and set the version if !disk_store.base.exists() { - file::create_dir(&disk_store.base)?; + file::create_dir_all(&disk_store.base)?; store.version_set_current()?; } } @@ -392,7 +392,7 @@ impl KeyValueStoreDiskImpl { pub fn wipe(&self) -> Result<(), KeyValueError> { if self.base.exists() { file::remove_dir_all(&self.base)?; - file::create_dir(&self.base)?; + file::create_dir_all(&self.base)?; } Ok(()) } diff --git a/src/commons/util/file.rs b/src/commons/util/file.rs index f62d20e7..32328186 100644 --- a/src/commons/util/file.rs +++ b/src/commons/util/file.rs @@ -22,11 +22,12 @@ use crate::commons::{ pub fn sub_dir(base: &Path, name: &str) -> Result { let mut full_path = base.to_path_buf(); full_path.push(name); - create_dir(&full_path)?; + create_dir_all(&full_path)?; Ok(full_path) } -pub fn create_dir(dir: &Path) -> Result<(), KrillIoError> { +/// Creates a dir and any parent dirs which were missing. +pub fn create_dir_all(dir: &Path) -> Result<(), KrillIoError> { if !dir.is_dir() { fs::create_dir_all(dir) .map_err(|e| KrillIoError::new(format!("could not create dir: {}", dir.to_string_lossy()), e))?; @@ -34,6 +35,7 @@ pub fn create_dir(dir: &Path) -> Result<(), KrillIoError> { Ok(()) } +/// Removes a dir and all its content. pub fn remove_dir_all(dir: &Path) -> Result<(), KrillIoError> { if dir.exists() { fs::remove_dir_all(dir) @@ -42,6 +44,8 @@ pub fn remove_dir_all(dir: &Path) -> Result<(), KrillIoError> { Ok(()) } +/// Creates a new File or opens an exiting one. If the file did not exist, the dir dir path +/// will be created if it did not exist yet. pub fn create_file_with_path(path: &Path) -> Result { if !path.exists() { if let Some(parent) = path.parent() { diff --git a/src/upgrades/mod.rs b/src/upgrades/mod.rs index 8fb47eed..9c0e9457 100644 --- a/src/upgrades/mod.rs +++ b/src/upgrades/mod.rs @@ -321,7 +321,7 @@ pub async fn prepare_upgrade_data_migrations( } else if versions.from < KrillVersion::release(0, 9, 0) { let upgrade_data_dir = config.upgrade_data_dir(); if !upgrade_data_dir.exists() { - file::create_dir(&upgrade_data_dir)?; + file::create_dir_all(&upgrade_data_dir)?; } // Get a lock to ensure that only one process can run this migration