From c0338cac35db6849a0a82a451db0e62d1fc24a0f Mon Sep 17 00:00:00 2001 From: Ubuntu Date: Tue, 18 Oct 2022 19:31:27 +0000 Subject: Create cache file directory if it doesn't exist --- azalea-auth/src/auth.rs | 2 +- azalea-auth/src/cache.rs | 12 ++++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) (limited to 'azalea-auth/src') diff --git a/azalea-auth/src/auth.rs b/azalea-auth/src/auth.rs index 0cc36fcf..ec20d31e 100644 --- a/azalea-auth/src/auth.rs +++ b/azalea-auth/src/auth.rs @@ -130,7 +130,7 @@ pub async fn auth(email: &str, opts: AuthOpts) -> Result }, ) .await { - log::warn!("Error while caching auth data: {}", e); + log::error!("{}", e); } } } diff --git a/azalea-auth/src/cache.rs b/azalea-auth/src/cache.rs index e84c60fd..404b05ff 100644 --- a/azalea-auth/src/cache.rs +++ b/azalea-auth/src/cache.rs @@ -13,6 +13,8 @@ pub enum CacheError { Read(std::io::Error), #[error("Failed to write cache file: {0}")] Write(std::io::Error), + #[error("Failed to create cache file directory: {0}")] + MkDir(std::io::Error), #[error("Failed to parse cache file: {0}")] Parse(serde_json::Error), } @@ -73,6 +75,16 @@ async fn get_entire_cache(cache_file: &Path) -> Result, Cache async fn set_entire_cache(cache_file: &Path, cache: Vec) -> Result<(), CacheError> { log::trace!("saving cache: {:?}", cache); + if !cache_file.exists() { + let cache_file_parent = cache_file + .parent() + .expect("Cache file is root directory and also doesn't exist."); + log::debug!( + "Making cache file parent directory at {}", + cache_file_parent.to_string_lossy() + ); + std::fs::create_dir_all(cache_file_parent).map_err(CacheError::MkDir)?; + } let mut cache_file = File::create(cache_file).await.map_err(CacheError::Write)?; let cache = serde_json::to_string_pretty(&cache).map_err(CacheError::Parse)?; cache_file -- cgit v1.2.3