From a6be644ebf2615daa0f28ac67a393cc2c9fd26db Mon Sep 17 00:00:00 2001 From: andy Date: Sun, 8 Oct 2023 14:59:45 -0300 Subject: [PATCH] create log and patch directories if they're missing --- src/bin/main.rs | 11 +++++++++++ src/patch/patch.rs | 17 ++++++++++++++++- 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/src/bin/main.rs b/src/bin/main.rs index 0a3ada0..5e4372a 100644 --- a/src/bin/main.rs +++ b/src/bin/main.rs @@ -15,6 +15,17 @@ use elseware::entity::item::{NewItemEntity, ItemDetail, InventoryItemEntity}; use elseware::entity::item; fn setup_logger() { + match std::fs::create_dir("log") { + Ok(()) => {}, + Err(e) => { + match e.kind() { + std::io::ErrorKind::AlreadyExists => {}, // false error + _ => { // real error + panic!("Failed to create \"log\" directory.\n{:?}", e); + }, + } + }, + }; let colors = fern::colors::ColoredLevelConfig::new() .error(fern::colors::Color::Red) .warn(fern::colors::Color::Yellow) diff --git a/src/patch/patch.rs b/src/patch/patch.rs index 26441f8..8221c2c 100644 --- a/src/patch/patch.rs +++ b/src/patch/patch.rs @@ -217,7 +217,22 @@ impl ServerState for PatchServerState { } fn load_patch_dir(basedir: &str, patchbase: &str, file_ids: &mut HashMap) -> PatchFileTree { - let paths = fs::read_dir(basedir).expect("could not read directory"); + let paths = { + match fs::read_dir(basedir) { + Ok(p) => p, + Err(e) => { + match e.kind() { + std::io::ErrorKind::NotFound => { // attempt to create the missing directory + match std::fs::create_dir(basedir) { + Ok(_) => fs::read_dir(basedir).expect("could not read newly created directory"), // created patch directory successfully. return it to paths + Err(ee) => panic!("Failed to create directory \"{}\".\n{:?}", basedir, ee), // we already know the path doesnt exist so no need to check for AlreadyExists error. panic + } + }, + _ => panic!("Unable to read directory \"{}\".\n{:?}", basedir, e), + } + }, + } + }; let mut files = Vec::new(); let mut dirs = Vec::new();