From 3097ff2ac393acf36fcd9291260f7cd13dae6ebb Mon Sep 17 00:00:00 2001 From: DustInDark Date: Fri, 24 Dec 2021 08:48:38 +0900 Subject: [PATCH] added process case of no exist config files #347 --- src/detections/configs.rs | 30 ++++++++++++++++++++++++++++-- src/detections/utils.rs | 9 ++++++--- 2 files changed, 34 insertions(+), 5 deletions(-) diff --git a/src/detections/configs.rs b/src/detections/configs.rs index 7f5c37cd..9122ac25 100644 --- a/src/detections/configs.rs +++ b/src/detections/configs.rs @@ -114,8 +114,17 @@ impl TargetEventIds { fn load_target_ids(path: &str) -> TargetEventIds { let mut ret = TargetEventIds::new(); - let lines = utils::read_txt(path).unwrap(); // ファイルが存在しなければエラーとする - for line in lines { + let lines = utils::read_txt(path); // ファイルが存在しなければエラーとする + if lines.is_err() { + AlertMessage::alert( + &mut BufWriter::new(std::io::stderr().lock()), + &lines.as_ref().unwrap_err(), + ) + .ok(); + return ret; + } + + for line in lines.unwrap() { if line.is_empty() { continue; } @@ -226,6 +235,14 @@ fn load_eventkey_alias(path: &str) -> EventKeyAliasConfig { let mut config = EventKeyAliasConfig::new(); let read_result = utils::read_csv(path); + if read_result.is_err() { + AlertMessage::alert( + &mut BufWriter::new(std::io::stderr().lock()), + &read_result.as_ref().unwrap_err(), + ) + .ok(); + return config; + } // eventkey_aliasが読み込めなかったらエラーで終了とする。 read_result.unwrap().into_iter().for_each(|line| { if line.len() != 2 { @@ -290,6 +307,15 @@ fn load_eventcode_info(path: &str) -> EventInfoConfig { let mut infodata = EventInfo::new(); let mut config = EventInfoConfig::new(); let read_result = utils::read_csv(path); + if read_result.is_err() { + AlertMessage::alert( + &mut BufWriter::new(std::io::stderr().lock()), + &read_result.as_ref().unwrap_err(), + ) + .ok(); + return config; + } + // timeline_event_infoが読み込めなかったらエラーで終了とする。 read_result.unwrap().into_iter().for_each(|line| { if line.len() != 4 { diff --git a/src/detections/utils.rs b/src/detections/utils.rs index 4ecf3733..82299bd8 100644 --- a/src/detections/utils.rs +++ b/src/detections/utils.rs @@ -76,11 +76,14 @@ pub fn read_txt(filename: &str) -> Result, String> { } pub fn read_csv(filename: &str) -> Result>, String> { - let mut f = File::open(filename).expect("File not found!!!"); + let f = File::open(filename); + if f.is_err() { + return Result::Err(format!("Cannot open file. [file:{}]", filename)); + } let mut contents: String = String::new(); let mut ret = vec![]; - let read_res = f.read_to_string(&mut contents); - if f.read_to_string(&mut contents).is_err() { + let read_res = f.unwrap().read_to_string(&mut contents); + if read_res.is_err() { return Result::Err(read_res.unwrap_err().to_string()); }