From 8fc9ebfa8c9ffca250c06ba20a0efb76c73a293a Mon Sep 17 00:00:00 2001 From: Laurent Louf Date: Fri, 24 Jul 2020 23:18:29 +0200 Subject: [PATCH 1/5] 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 9ee0ae1..609f9f7 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 583e6ec..a7a2b45 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") From 77cb66d9a3b276702723228d8ec76c8ecd7f798a Mon Sep 17 00:00:00 2001 From: Laurent Louf Date: Sat, 25 Jul 2020 13:51:34 +0200 Subject: [PATCH 2/5] Fix syntax --- src/main.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main.rs b/src/main.rs index a7a2b45..4dc88b9 100644 --- a/src/main.rs +++ b/src/main.rs @@ -249,7 +249,7 @@ fn setup(args: &[String]) -> Setup { let use_audio_cache = !matches.opt_present("disable-audio-cache"); - let cache_directory = matches.opt_str("c").unwrap_or(String::from("")) + 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), From 09bebe5dd7ad1f637f9767a2e12d838949afc3d7 Mon Sep 17 00:00:00 2001 From: Laurent Louf Date: Sat, 25 Jul 2020 13:51:45 +0200 Subject: [PATCH 3/5] Fix types --- src/main.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main.rs b/src/main.rs index 4dc88b9..af41f96 100644 --- a/src/main.rs +++ b/src/main.rs @@ -250,8 +250,8 @@ fn setup(args: &[String]) -> Setup { let use_audio_cache = !matches.opt_present("disable-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( + let system_cache_directory = matches.opt_str("system-cache").unwrap_or(String::from(cache_directory.clone())); + let cache = Some(Cache::new( PathBuf::from(cache_directory), PathBuf::from(system_cache_directory), use_audio_cache)); From c14c254c6b2adab3d7959228feff8026f40789b0 Mon Sep 17 00:00:00 2001 From: Laurent Louf Date: Sun, 26 Jul 2020 16:11:32 +0200 Subject: [PATCH 4/5] Format --- core/src/cache.rs | 5 ++++- src/main.rs | 8 ++++++-- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/core/src/cache.rs b/core/src/cache.rs index 609f9f7..b081831 100644 --- a/core/src/cache.rs +++ b/core/src/cache.rs @@ -76,7 +76,10 @@ impl Cache { impl Cache { fn file_path(&self, file: FileId) -> PathBuf { let name = file.to_base16(); - self.audio_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 af41f96..a4c3816 100644 --- a/src/main.rs +++ b/src/main.rs @@ -250,11 +250,15 @@ fn setup(args: &[String]) -> Setup { let use_audio_cache = !matches.opt_present("disable-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.clone())); + let system_cache_directory = matches + .opt_str("system-cache") + .unwrap_or(String::from(cache_directory.clone())); + let cache = Some(Cache::new( PathBuf::from(cache_directory), PathBuf::from(system_cache_directory), - use_audio_cache)); + use_audio_cache, + )); let initial_volume = matches .opt_str("initial-volume") From b34032690b4cdcf23e409146abe0df17048914a9 Mon Sep 17 00:00:00 2001 From: Laurent Louf Date: Sun, 2 Aug 2020 10:52:09 +0200 Subject: [PATCH 5/5] Don't create directories for the audio cache if it disabled --- core/src/cache.rs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/core/src/cache.rs b/core/src/cache.rs index b081831..1200c89 100644 --- a/core/src/cache.rs +++ b/core/src/cache.rs @@ -28,8 +28,10 @@ fn mkdir_existing(path: &Path) -> io::Result<()> { impl Cache { 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(); + if use_audio_cache == true { + mkdir_existing(&audio_location).unwrap(); + mkdir_existing(&audio_location.join("files")).unwrap(); + } mkdir_existing(&system_location).unwrap(); Cache {