Add the option to specify the system cache for credentials and volume files and adapt Cache to use two cache directories instead of one

This commit is contained in:
Laurent Louf 2020-07-24 23:18:29 +02:00
parent e3abb87da1
commit 8fc9ebfa8c
2 changed files with 23 additions and 12 deletions

View file

@ -11,7 +11,8 @@ use crate::volume::Volume;
#[derive(Clone)]
pub struct Cache {
root: PathBuf,
audio_root: PathBuf,
system_root: PathBuf,
use_audio_cache: bool,
}
@ -26,12 +27,14 @@ fn mkdir_existing(path: &Path) -> io::Result<()> {
}
impl Cache {
pub fn new(location: PathBuf, use_audio_cache: bool) -> Cache {
mkdir_existing(&location).unwrap();
mkdir_existing(&location.join("files")).unwrap();
pub fn new(audio_location: PathBuf, system_location: PathBuf, use_audio_cache: bool) -> Cache {
mkdir_existing(&audio_location).unwrap();
mkdir_existing(&audio_location.join("files")).unwrap();
mkdir_existing(&system_location).unwrap();
Cache {
root: location,
audio_root: audio_location,
system_root: system_location,
use_audio_cache: use_audio_cache,
}
}
@ -39,7 +42,7 @@ impl Cache {
impl Cache {
fn credentials_path(&self) -> PathBuf {
self.root.join("credentials.json")
self.system_root.join("credentials.json")
}
pub fn credentials(&self) -> Option<Credentials> {
@ -53,10 +56,10 @@ impl Cache {
}
}
// cache volume to root/volume
// cache volume to system_root/volume
impl Cache {
fn volume_path(&self) -> PathBuf {
self.root.join("volume")
self.system_root.join("volume")
}
pub fn volume(&self) -> Option<u16> {
@ -73,7 +76,7 @@ impl Cache {
impl Cache {
fn file_path(&self, file: FileId) -> PathBuf {
let name = file.to_base16();
self.root.join("files").join(&name[0..2]).join(&name[2..])
self.audio_root.join("files").join(&name[0..2]).join(&name[2..])
}
pub fn file(&self, file: FileId) -> Option<File> {

View file

@ -97,6 +97,11 @@ fn setup(args: &[String]) -> Setup {
"cache",
"Path to a directory where files will be cached.",
"CACHE",
).optopt(
"",
"system-cache",
"Path to a directory where system files (credentials, volume) will be cached. Can be different from cache option value",
"SYTEMCACHE",
).optflag("", "disable-audio-cache", "Disable caching of the audio data.")
.reqopt("n", "name", "Device name", "NAME")
.optopt("", "device-type", "Displayed device type", "DEVICE_TYPE")
@ -244,9 +249,12 @@ fn setup(args: &[String]) -> Setup {
let use_audio_cache = !matches.opt_present("disable-audio-cache");
let cache = matches
.opt_str("c")
.map(|cache_location| Cache::new(PathBuf::from(cache_location), use_audio_cache));
let cache_directory = matches.opt_str("c").unwrap_or(String::from(""))
let system_cache_directory = matches.opt_str("system-cache").unwrap_or(String::from(cache_directory))
let cache = Cache::new(
PathBuf::from(cache_directory),
PathBuf::from(system_cache_directory),
use_audio_cache));
let initial_volume = matches
.opt_str("initial-volume")