diff options
| author | Ubuntu <github@matdoes.dev> | 2022-10-18 19:31:27 +0000 |
|---|---|---|
| committer | Ubuntu <github@matdoes.dev> | 2022-10-18 19:31:27 +0000 |
| commit | c0338cac35db6849a0a82a451db0e62d1fc24a0f (patch) | |
| tree | b6310972b1100ca72e289a76515b87fe7554e561 /azalea-auth/src/cache.rs | |
| parent | 4c9de35cc2185b5ee8f844f907a50fa9a46b0bf2 (diff) | |
| download | azalea-drasl-c0338cac35db6849a0a82a451db0e62d1fc24a0f.tar.xz | |
Create cache file directory if it doesn't exist
Diffstat (limited to 'azalea-auth/src/cache.rs')
| -rw-r--r-- | azalea-auth/src/cache.rs | 12 |
1 files changed, 12 insertions, 0 deletions
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<Vec<CachedAccount>, Cache async fn set_entire_cache(cache_file: &Path, cache: Vec<CachedAccount>) -> 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 |
