diff options
author | Danilo Bargen <mail@dbrgn.ch> | 2022-09-28 23:31:40 +0200 |
---|---|---|
committer | Danilo Bargen <mail@dbrgn.ch> | 2022-10-01 12:18:44 +0200 |
commit | c44faf2b0919e2eab408a24ef9d06d9382572435 (patch) | |
tree | 72e0b4ed81cedc27745cd78fea44d7e83b2d4962 | |
parent | 3f528ba5cb2724502a847f9b9dc798dc7714df19 (diff) |
Lazy creation of cache directory
-rw-r--r-- | src/cache.rs | 55 | ||||
-rw-r--r-- | src/main.rs | 19 |
2 files changed, 45 insertions, 29 deletions
diff --git a/src/cache.rs b/src/cache.rs index b7fab2a..57b1a30 100644 --- a/src/cache.rs +++ b/src/cache.rs @@ -86,40 +86,49 @@ pub enum CacheFreshness { } impl Cache { - pub fn new<P>(platform: PlatformType, cache_dir: P) -> Result<Self> + pub fn new<P>(platform: PlatformType, cache_dir: P) -> Self where P: Into<PathBuf>, { + Self { + platform, + cache_dir: cache_dir.into(), + } + } + + pub fn cache_dir(&self) -> &Path { + &self.cache_dir + } + + /// Make sure that the cache directory exists and is a directory. + /// If necessary, create the directory. + fn ensure_cache_dir_exists(&self) -> Result<()> { // Check whether `cache_dir` exists and is a directory - let cache_dir = cache_dir.into(); - let (cache_dir_exists, cache_dir_is_dir) = cache_dir + let (cache_dir_exists, cache_dir_is_dir) = self + .cache_dir .metadata() .map_or((false, false), |md| (true, md.is_dir())); ensure!( !cache_dir_exists || cache_dir_is_dir, "Cache directory path `{}` is not a directory", - cache_dir.display(), + self.cache_dir.display(), ); - // If necessary, create cache directory if !cache_dir_exists { - // Try to create the complete directory path - fs::create_dir_all(&cache_dir).with_context(|| { + // If missing, try to create the complete directory path + fs::create_dir_all(&self.cache_dir).with_context(|| { format!( "Cache directory path `{}` cannot be created", - cache_dir.display(), + self.cache_dir.display(), ) })?; eprintln!( "Successfully created cache directory path `{}`.", - cache_dir.display(), + self.cache_dir.display(), ); } - Ok(Self { - platform, - cache_dir, - }) + Ok(()) } fn pages_dir(&self) -> PathBuf { @@ -155,6 +164,8 @@ impl Cache { /// Update the pages cache from the specified URL. pub fn update(&self, archive_url: &str) -> Result<()> { + self.ensure_cache_dir_exists()?; + // First, download the compressed data let bytes: Vec<u8> = Self::download(archive_url)?; @@ -345,14 +356,14 @@ impl Cache { pages } - /// Delete the cache directory. - pub fn clear(&self) -> Result<()> { - // Check preconditions - ensure!( - self.cache_dir.exists(), - "Cache path ({}) does not exist.", - self.cache_dir.display(), - ); + /// Delete the cache directory + /// + /// Returns true if the cache was deleted and false if the cache dir did + /// not exist. + pub fn clear(&self) -> Result<bool> { + if !self.cache_dir.exists() { + return Ok(false); + } ensure!( self.cache_dir.is_dir(), "Cache path ({}) is not a directory.", @@ -374,7 +385,7 @@ impl Cache { } } - Ok(()) + Ok(true) } } diff --git a/src/main.rs b/src/main.rs index 9c8fe58..ffacae2 100644 --- a/src/main.rs +++ b/src/main.rs @@ -112,12 +112,20 @@ fn check_cache(cache: &Cache, args: &Args, enable_styles: bool) -> CheckCacheRes /// Clear the cache fn clear_cache(cache: &Cache, quietly: bool, enable_styles: bool) { - cache.clear().unwrap_or_else(|e| { + let cache_dir_found = cache.clear().unwrap_or_else(|e| { print_error(enable_styles, &e.context("Could not clear cache")); process::exit(1); }); if !quietly { - eprintln!("Successfully deleted cache."); + let cache_dir = cache.cache_dir().display(); + if cache_dir_found { + eprintln!("Successfully cleared cache at `{}`.", cache_dir); + } else { + eprintln!( + "Cache directory not found at `{}`, nothing to do.", + cache_dir + ); + } } } @@ -324,11 +332,8 @@ fn main() { }; } - // Initialize cache - let cache = Cache::new(platform, &config.directories.cache_dir.path).unwrap_or_else(|e| { - print_error(enable_styles, &e.context("Could not initialize cache")); - process::exit(1); - }); + // Instantiate cache. This will not yet create the cache directory! + let cache = Cache::new(platform, &config.directories.cache_dir.path); // Clear cache, pass through if args.clear_cache { |