From ca7c5129b2eadabed826cd82b6c291c0cb68cda9 Mon Sep 17 00:00:00 2001 From: BlackDex Date: Wed, 6 Nov 2019 15:47:56 +0100 Subject: [PATCH 1/3] Fixed issue #709 creating icon_cache directory. When the icon_cache directory doesn't exists yet, and the first icon catched is a miss this .miss file was not able to be created since the directory was only created during a valid icon download. --- src/api/icons.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/api/icons.rs b/src/api/icons.rs index e8e4d1c..f403c69 100644 --- a/src/api/icons.rs +++ b/src/api/icons.rs @@ -104,6 +104,9 @@ fn get_icon(domain: &str) -> Vec { return FALLBACK_ICON.to_vec(); } + // Create icon_cache_folder before fetching + create_dir_all(&CONFIG.icon_cache_folder()).expect("Error creating icon cache"); + // Get the icon, or fallback in case of error match download_icon(&domain) { Ok(icon) => { @@ -395,8 +398,6 @@ fn download_icon(domain: &str) -> Result, Error> { } fn save_icon(path: &str, icon: &[u8]) { - create_dir_all(&CONFIG.icon_cache_folder()).expect("Error creating icon cache"); - if let Ok(mut f) = File::create(path) { f.write_all(icon).expect("Error writing icon file"); }; From 0ff7fd939e9afe2197bb53eb37bce7d2b62f0b7f Mon Sep 17 00:00:00 2001 From: BlackDex Date: Wed, 6 Nov 2019 20:21:47 +0100 Subject: [PATCH 2/3] Next attempt for issue #709 fix Now creates icon cache directory at startup. And it also creates the directory if it went missing during runtime. Also modified the icon_save/mark_negcache to be one. --- src/api/icons.rs | 26 ++++++++++++++------------ src/main.rs | 11 +++++++++-- 2 files changed, 23 insertions(+), 14 deletions(-) diff --git a/src/api/icons.rs b/src/api/icons.rs index f403c69..0bac31c 100644 --- a/src/api/icons.rs +++ b/src/api/icons.rs @@ -104,9 +104,6 @@ fn get_icon(domain: &str) -> Vec { return FALLBACK_ICON.to_vec(); } - // Create icon_cache_folder before fetching - create_dir_all(&CONFIG.icon_cache_folder()).expect("Error creating icon cache"); - // Get the icon, or fallback in case of error match download_icon(&domain) { Ok(icon) => { @@ -115,7 +112,9 @@ fn get_icon(domain: &str) -> Vec { } Err(e) => { error!("Error downloading icon: {:?}", e); - mark_negcache(&path); + let miss_indicator = path.to_owned() + ".miss"; + let empty_icon = Vec::new(); + save_icon(&miss_indicator, &empty_icon); FALLBACK_ICON.to_vec() } } @@ -171,11 +170,6 @@ fn icon_is_negcached(path: &str) -> bool { } } -fn mark_negcache(path: &str) { - let miss_indicator = path.to_owned() + ".miss"; - File::create(&miss_indicator).expect("Error creating negative cache marker"); -} - fn icon_is_expired(path: &str) -> bool { let expired = file_is_expired(path, CONFIG.icon_cache_ttl()); expired.unwrap_or(true) @@ -398,9 +392,17 @@ fn download_icon(domain: &str) -> Result, Error> { } fn save_icon(path: &str, icon: &[u8]) { - if let Ok(mut f) = File::create(path) { - f.write_all(icon).expect("Error writing icon file"); - }; + match File::create(path) { + Ok(mut f) => { + f.write_all(icon).expect("Error writing icon file"); + } + Err(ref e) if e.kind() == std::io::ErrorKind::NotFound => { + create_dir_all(&CONFIG.icon_cache_folder()).expect("Error creating icon cache"); + } + Err(e) => { + info!("Icon save error: {:?}", e); + } + } } fn _header_map() -> HeaderMap { diff --git a/src/main.rs b/src/main.rs index c23eca6..862b53b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -25,6 +25,7 @@ extern crate num_derive; use std::{ path::Path, process::{exit, Command}, + fs::create_dir_all, }; #[macro_use] @@ -52,6 +53,8 @@ fn main() { check_web_vault(); migrations::run_migrations(); + create_icon_cache_folder(); + launch_rocket(); } @@ -129,8 +132,7 @@ fn check_db() { let path = Path::new(&url); if let Some(parent) = path.parent() { - use std::fs; - if fs::create_dir_all(parent).is_err() { + if create_dir_all(parent).is_err() { error!("Error creating database directory"); exit(1); } @@ -148,6 +150,11 @@ fn check_db() { db::get_connection().expect("Can't connect to DB"); } +fn create_icon_cache_folder() { + // Try to create the icon cache folder, and generate an error if it could not. + create_dir_all(&CONFIG.icon_cache_folder()).expect("Error creating icon cache directory"); +} + fn check_rsa_keys() { // If the RSA keys don't exist, try to create them if !util::file_exists(&CONFIG.private_rsa_key()) || !util::file_exists(&CONFIG.public_rsa_key()) { From 2ffc3eac4da67c82db0d7dbc361295a047139499 Mon Sep 17 00:00:00 2001 From: BlackDex Date: Wed, 6 Nov 2019 20:34:52 +0100 Subject: [PATCH 3/3] Clippy fix --- src/api/icons.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/api/icons.rs b/src/api/icons.rs index 0bac31c..a6f63b5 100644 --- a/src/api/icons.rs +++ b/src/api/icons.rs @@ -112,7 +112,7 @@ fn get_icon(domain: &str) -> Vec { } Err(e) => { error!("Error downloading icon: {:?}", e); - let miss_indicator = path.to_owned() + ".miss"; + let miss_indicator = path + ".miss"; let empty_icon = Vec::new(); save_icon(&miss_indicator, &empty_icon); FALLBACK_ICON.to_vec()