Skip to main content

rattler_cache/
lib.rs

1#[cfg(not(target_arch = "wasm32"))]
2use std::path::{Path, PathBuf};
3
4#[cfg(not(target_arch = "wasm32"))]
5pub mod package_cache;
6#[cfg(not(target_arch = "wasm32"))]
7pub mod run_exports_cache;
8
9#[cfg(not(target_arch = "wasm32"))]
10pub mod validation;
11
12mod consts;
13pub use consts::{PACKAGE_CACHE_DIR, REPODATA_CACHE_DIR, RUN_EXPORTS_CACHE_DIR};
14
15/// Determines the default cache directory for rattler.
16/// It first checks the environment variable `RATTLER_CACHE_DIR`.
17/// If not set, it falls back to the standard cache directory provided by `dirs::cache_dir()/rattler/cache`.
18#[cfg(not(target_arch = "wasm32"))]
19pub fn default_cache_dir() -> anyhow::Result<PathBuf> {
20    std::env::var("RATTLER_CACHE_DIR")
21        .map(PathBuf::from)
22        .or_else(|_| {
23            dirs::cache_dir()
24                .ok_or_else(|| {
25                    anyhow::anyhow!("could not determine cache directory for current platform")
26                })
27                // Append `rattler/cache` to the cache directory
28                .map(|mut p| {
29                    p.push("rattler");
30                    p.push("cache");
31                    p
32                })
33        })
34}
35
36/// Creates the cache directory if it doesn't exist and excludes it from backups.
37///
38/// This function:
39/// 1. Creates the directory and all parent directories if they don't exist
40/// 2. Creates a `CACHEDIR.TAG` file to exclude from backup tools (borg, restic, etc.)
41/// 3. On macOS, marks the directory as excluded from Time Machine
42///
43/// This is idempotent - calling it multiple times on the same directory is safe.
44#[cfg(not(target_arch = "wasm32"))]
45pub fn ensure_cache_dir(path: &Path) -> std::io::Result<()> {
46    fs_err::create_dir_all(path)?;
47    rattler_conda_types::backup::exclude_from_backups(path)?;
48    Ok(())
49}