Skip to main content

zoi_core/
cache.rs

1use std::fs;
2use std::path::PathBuf;
3
4use anyhow::Result;
5
6/// Returns the root directory for Zoi's cache.
7///
8/// # Errors
9///
10/// Returns an error if the user's home directory cannot be determined.
11pub fn get_cache_root() -> Result<PathBuf> {
12    crate::utils::get_user_cache_dir()
13}
14
15/// Returns the root directory for Zoi's archive cache.
16///
17/// # Errors
18///
19/// Returns an error if the cache root directory cannot be determined.
20pub fn get_archive_cache_root() -> Result<PathBuf> {
21    let cache_root = get_cache_root()?;
22    Ok(cache_root.join("archives"))
23}
24
25/// Returns the root directory for Zoi's package definition cache.
26///
27/// # Errors
28///
29/// Returns an error if the cache root directory cannot be determined.
30pub fn get_pkgdef_cache_root() -> Result<PathBuf> {
31    let cache_root = get_cache_root()?;
32    Ok(cache_root.join("pkgdefs"))
33}
34
35/// Returns a list of candidate URLs for a given URL, including configured
36/// mirrors.
37pub fn mirror_candidate_urls(url: &str) -> Vec<String> {
38    let mut urls = vec![url.to_string()];
39    let Ok(config) = crate::config::read_config() else {
40        return urls;
41    };
42
43    let Some(filename) =
44        url.split('/').next_back().filter(|part| !part.is_empty())
45    else {
46        return urls;
47    };
48
49    for mirror in config.cache_mirrors {
50        urls.push(format!("{}/{}", mirror.trim_end_matches('/'), filename));
51    }
52
53    urls
54}
55
56/// Clears the entire Zoi cache.
57///
58/// # Errors
59///
60/// Returns an error if the cache directory cannot be removed.
61pub fn clear(dry_run: bool) -> Result<()> {
62    let cache_dir = get_cache_root()?;
63    if cache_dir.exists() {
64        if dry_run {
65            println!(
66                "(Dry-run) Would remove cache directory: {}",
67                cache_dir.display()
68            );
69        } else {
70            println!("Removing cache directory: {}", cache_dir.display());
71            fs::remove_dir_all(cache_dir)?;
72        }
73    } else {
74        println!("Cache directory does not exist. Nothing to clean.");
75    }
76    Ok(())
77}
78
79/// Clears only the archive cache.
80///
81/// # Errors
82///
83/// Returns an error if the archive cache directory cannot be removed.
84pub fn clear_archives(dry_run: bool) -> Result<()> {
85    let archive_cache_dir = get_archive_cache_root()?;
86    if archive_cache_dir.exists() {
87        if dry_run {
88            println!(
89                "(Dry-run) Would remove archive cache directory: {}",
90                archive_cache_dir.display()
91            );
92        } else {
93            println!(
94                "Removing archive cache directory: {}",
95                archive_cache_dir.display()
96            );
97            fs::remove_dir_all(archive_cache_dir)?;
98        }
99    } else {
100        println!("Archive cache directory does not exist. Nothing to clean.");
101    }
102    Ok(())
103}