From 8fc9ebfa8c9ffca250c06ba20a0efb76c73a293a Mon Sep 17 00:00:00 2001 From: Laurent Louf Date: Fri, 24 Jul 2020 23:18:29 +0200 Subject: [PATCH] Add the option to specify the system cache for credentials and volume files and adapt Cache to use two cache directories instead of one --- core/src/cache.rs | 21 ++++++++++++--------- src/main.rs | 14 +++++++++++--- 2 files changed, 23 insertions(+), 12 deletions(-) diff --git a/core/src/cache.rs b/core/src/cache.rs index 9ee0ae18..609f9f74 100644 --- a/core/src/cache.rs +++ b/core/src/cache.rs @@ -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 { @@ -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 { @@ -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 { diff --git a/src/main.rs b/src/main.rs index 583e6ece..a7a2b454 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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")