cache: Add Option --cache-dir and use restic/rustic cache dir

This commit is contained in:
Alexander Weiss 2022-06-27 10:29:19 +02:00
parent caca900972
commit f3e8bc4627
2 changed files with 35 additions and 16 deletions

View File

@ -18,15 +18,8 @@ pub struct CachedBackend<BE: WriteBackend> {
}
impl<BE: WriteBackend> CachedBackend<BE> {
pub fn new(be: BE, id: Id, create: bool) -> Self {
Self {
be,
cache: Cache::new(id, create).unwrap(),
}
}
pub fn cache(&self) -> &Option<Cache> {
&self.cache
pub fn new(be: BE, cache: Option<Cache>) -> Self {
Self { be, cache }
}
}
@ -136,13 +129,26 @@ pub struct Cache {
}
impl Cache {
pub fn new(id: Id, create: bool) -> Result<Option<Self>> {
let mut path = cache_dir().ok_or_else(|| anyhow!("no cache dir"))?;
path.push("restic");
pub fn new(id: Id, path: Option<PathBuf>, create: bool) -> Result<Option<Self>> {
let mut path = path.unwrap_or(cache_dir().ok_or_else(|| anyhow!("no cache dir"))?);
// first try if a restic cache dir exists and if yes, use it
let mut path_restic = path.clone();
path_restic.push("restic");
if path_restic.exists() && cachedir::is_tagged(&path_restic)? {
path_restic.push(id.to_hex());
if path_restic.exists() {
return Ok(Some(Self { path: path_restic }));
}
}
// else use rustic cache dir
path.push("rustic");
if create {
fs::create_dir_all(&path)?;
cachedir::ensure_tag(&path)?;
} else if !cachedir::is_tagged(&path)? {
} else if !path.exists() || !cachedir::is_tagged(&path)? {
return Ok(None);
}
@ -154,6 +160,10 @@ impl Cache {
Ok(Some(Self { path }))
}
pub fn location(&self) -> &str {
self.path.to_str().unwrap()
}
fn dir(&self, tpe: FileType, id: &Id) -> PathBuf {
let hex_id = id.to_hex();
self.path.join(tpe.name()).join(&hex_id[0..2])

View File

@ -4,7 +4,7 @@ use anyhow::{bail, Result};
use clap::{Parser, Subcommand};
use crate::backend::{
CachedBackend, ChooseBackend, DecryptBackend, DecryptReadBackend, FileType, ReadBackend,
Cache, CachedBackend, ChooseBackend, DecryptBackend, DecryptReadBackend, FileType, ReadBackend,
};
use crate::repo::ConfigFile;
@ -49,6 +49,11 @@ struct Opts {
#[clap(long)]
no_cache: bool,
/// Use this thir as cache dir. If not given, rustic searches for restic cache dirs
/// and rustic cache dirs
#[clap(long, parse(from_os_str), conflicts_with = "no-cache")]
cache_dir: Option<PathBuf>,
#[clap(subcommand)]
command: Command,
}
@ -121,8 +126,12 @@ pub async fn execute() -> Result<()> {
let key = get_key(&be, args.password_file).await?;
let dbe = DecryptBackend::new(&be, key.clone());
let config: ConfigFile = dbe.get_file(&config_ids[0]).await?;
let be_cached = CachedBackend::new(be.clone(), config.id, !args.no_cache);
let cache = be_cached.cache().clone();
let cache = Cache::new(config.id, args.cache_dir, !args.no_cache)?;
match &cache {
None => v1!("using no cache"),
Some(cache) => v1!("using cache at {}", cache.location()),
}
let be_cached = CachedBackend::new(be.clone(), cache.clone());
let dbe = DecryptBackend::new(&be_cached, key.clone());
(cmd, key, dbe, cache, be, config)
}