From d8cdef2febfdaabca20d79e59ea687dacf19c587 Mon Sep 17 00:00:00 2001 From: blek Date: Sat, 21 Oct 2023 01:34:09 +1000 Subject: [PATCH] try to also create the config directory --- filed/src/env.rs | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/filed/src/env.rs b/filed/src/env.rs index 0c4c6ea..39f60cb 100644 --- a/filed/src/env.rs +++ b/filed/src/env.rs @@ -62,18 +62,30 @@ pub fn loadenv() -> Result> { confpath: { let spath: String = get_var("CONF_FILE").unwrap_or("/etc/filed/filed.toml".into()); let path = Path::new(&spath); - let mut dirpath = path.clone().components(); + let mut dirpath = path.components(); dirpath.next_back(); let dirpath = dirpath.as_path(); if ! path.is_file() { - if dirpath.is_dir() { - log::info!("Config file does not exist, but found config directory. Trying to write the example"); - let wrote = fs::write(path, DEFAULT_CONFIG.clone()); + if ! dirpath.is_dir() { + log::info!("The config file directory does not exist. Trying to create it"); + + let created = fs::create_dir_all(dirpath); + if created.is_err() { + log::warn!("Could not create the config directory: {:?}", created.unwrap_err()); + } else { + log::info!("Created the config directory"); + } + } + + if dirpath.is_dir() { + log::info!("Config file does not exist, trying to write the example"); + + let wrote = fs::write(path, DEFAULT_CONFIG); if wrote.is_err() { - log::info!("Could not write example because of the following error: {:?}", wrote.unwrap_err()); + log::warn!("Could not write example because of the following error: {:?}", wrote.unwrap_err()); log::info!("Giving up"); } else { log::info!("Wrote example to {}", spath);