From 52bd212e4357a755fd8b680be47f1ab68c822945 Mon Sep 17 00:00:00 2001 From: JasonLG1979 Date: Tue, 26 Oct 2021 22:06:52 -0500 Subject: [PATCH 1/4] Add disable credential cache flag As mentioned in https://github.com/librespot-org/librespot/discussions/870, this allows someone who would otherwise like to take advantage of audio file and volume caching to disable credential caching. --- core/src/cache.rs | 29 +++++++++++++++++++---------- src/main.rs | 17 +++++++++++++---- 2 files changed, 32 insertions(+), 14 deletions(-) diff --git a/core/src/cache.rs b/core/src/cache.rs index 612b7c39..20270e3e 100644 --- a/core/src/cache.rs +++ b/core/src/cache.rs @@ -238,29 +238,38 @@ pub struct RemoveFileError(()); impl Cache { pub fn new>( - system_location: Option

, - audio_location: Option

, + credentials: Option

, + volume: Option

, + audio: Option

, size_limit: Option, ) -> io::Result { - if let Some(location) = &system_location { + let mut size_limiter = None; + + if let Some(location) = &credentials { fs::create_dir_all(location)?; } - let mut size_limiter = None; + let credentials_location = credentials + .as_ref() + .map(|p| p.as_ref().join("credentials.json")); - if let Some(location) = &audio_location { + if let Some(location) = &volume { fs::create_dir_all(location)?; + } + + let volume_location = volume.as_ref().map(|p| p.as_ref().join("volume")); + + if let Some(location) = &audio { + fs::create_dir_all(location)?; + if let Some(limit) = size_limit { let limiter = FsSizeLimiter::new(location.as_ref(), limit); + size_limiter = Some(Arc::new(limiter)); } } - let audio_location = audio_location.map(|p| p.as_ref().to_owned()); - let volume_location = system_location.as_ref().map(|p| p.as_ref().join("volume")); - let credentials_location = system_location - .as_ref() - .map(|p| p.as_ref().join("credentials.json")); + let audio_location = audio.map(|p| p.as_ref().to_owned()); let cache = Cache { credentials_location, diff --git a/src/main.rs b/src/main.rs index a3522e8c..c60b2887 100644 --- a/src/main.rs +++ b/src/main.rs @@ -203,6 +203,7 @@ fn get_setup(args: &[String]) -> Setup { const DEVICE: &str = "device"; const DEVICE_TYPE: &str = "device-type"; const DISABLE_AUDIO_CACHE: &str = "disable-audio-cache"; + const DISABLE_CREDENTIAL_CACHE: &str = "disable-credential-cache"; const DISABLE_DISCOVERY: &str = "disable-discovery"; const DISABLE_GAPLESS: &str = "disable-gapless"; const DITHER: &str = "dither"; @@ -256,6 +257,7 @@ fn get_setup(args: &[String]) -> Setup { "Limits the size of the cache for audio files.", "SIZE" ).optflag("", DISABLE_AUDIO_CACHE, "Disable caching of the audio data.") + .optflag("", DISABLE_CREDENTIAL_CACHE, "Disable caching of credentials.") .optopt("n", NAME, "Device name.", "NAME") .optopt("", DEVICE_TYPE, "Displayed device type. Defaults to 'Speaker'.", "TYPE") .optopt( @@ -560,10 +562,11 @@ fn get_setup(args: &[String]) -> Setup { let cache = { let audio_dir; - let system_dir; + let cred_dir; + let volume_dir; if matches.opt_present(DISABLE_AUDIO_CACHE) { audio_dir = None; - system_dir = matches + volume_dir = matches .opt_str(SYSTEM_CACHE) .or_else(|| matches.opt_str(CACHE)) .map(|p| p.into()); @@ -572,12 +575,18 @@ fn get_setup(args: &[String]) -> Setup { audio_dir = cache_dir .as_ref() .map(|p| AsRef::::as_ref(p).join("files")); - system_dir = matches + volume_dir = matches .opt_str(SYSTEM_CACHE) .or(cache_dir) .map(|p| p.into()); } + if matches.opt_present(DISABLE_CREDENTIAL_CACHE) { + cred_dir = None; + } else { + cred_dir = volume_dir.clone(); + } + let limit = if audio_dir.is_some() { matches .opt_str(CACHE_SIZE_LIMIT) @@ -593,7 +602,7 @@ fn get_setup(args: &[String]) -> Setup { None }; - match Cache::new(system_dir, audio_dir, limit) { + match Cache::new(cred_dir, volume_dir, audio_dir, limit) { Ok(cache) => Some(cache), Err(e) => { warn!("Cannot create cache: {}", e); From 9152ca81593d5083fa4da5f787f19696aab516fc Mon Sep 17 00:00:00 2001 From: JasonLG1979 Date: Tue, 26 Oct 2021 22:18:10 -0500 Subject: [PATCH 2/4] Update changelog --- CHANGELOG.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6e362ae6..678880eb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +### Added +- [cache] Add `disable-credential-cache` flag (breaking). + ## [0.3.1] - 2021-10-24 ### Changed From e543ef72ede07b26f6bbb49b9109db8f3bec6c6b Mon Sep 17 00:00:00 2001 From: JasonLG1979 Date: Wed, 27 Oct 2021 10:14:40 -0500 Subject: [PATCH 3/4] Clean up cache logic in main --- src/main.rs | 40 +++++++++++++++++----------------------- 1 file changed, 17 insertions(+), 23 deletions(-) diff --git a/src/main.rs b/src/main.rs index c60b2887..ae3258a1 100644 --- a/src/main.rs +++ b/src/main.rs @@ -561,31 +561,25 @@ fn get_setup(args: &[String]) -> Setup { }; let cache = { - let audio_dir; - let cred_dir; - let volume_dir; - if matches.opt_present(DISABLE_AUDIO_CACHE) { - audio_dir = None; - volume_dir = matches - .opt_str(SYSTEM_CACHE) - .or_else(|| matches.opt_str(CACHE)) - .map(|p| p.into()); - } else { - let cache_dir = matches.opt_str(CACHE); - audio_dir = cache_dir - .as_ref() - .map(|p| AsRef::::as_ref(p).join("files")); - volume_dir = matches - .opt_str(SYSTEM_CACHE) - .or(cache_dir) - .map(|p| p.into()); - } + let volume_dir = matches + .opt_str(SYSTEM_CACHE) + .or_else(|| matches.opt_str(CACHE)) + .map(|p| p.into()); - if matches.opt_present(DISABLE_CREDENTIAL_CACHE) { - cred_dir = None; + let cred_dir = if matches.opt_present(DISABLE_CREDENTIAL_CACHE) { + None } else { - cred_dir = volume_dir.clone(); - } + volume_dir.clone() + }; + + let audio_dir = if matches.opt_present(DISABLE_AUDIO_CACHE) { + None + } else { + matches + .opt_str(CACHE) + .as_ref() + .map(|p| AsRef::::as_ref(p).join("files")) + }; let limit = if audio_dir.is_some() { matches From 9e017119bb2aa24793c23c056c8c531fe7a8bceb Mon Sep 17 00:00:00 2001 From: JasonLG1979 Date: Wed, 27 Oct 2021 14:47:33 -0500 Subject: [PATCH 4/4] Address review change request --- core/src/cache.rs | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/core/src/cache.rs b/core/src/cache.rs index 20270e3e..da2ad022 100644 --- a/core/src/cache.rs +++ b/core/src/cache.rs @@ -238,28 +238,28 @@ pub struct RemoveFileError(()); impl Cache { pub fn new>( - credentials: Option

, - volume: Option

, - audio: Option

, + credentials_path: Option

, + volume_path: Option

, + audio_path: Option

, size_limit: Option, ) -> io::Result { let mut size_limiter = None; - if let Some(location) = &credentials { + if let Some(location) = &credentials_path { fs::create_dir_all(location)?; } - let credentials_location = credentials + let credentials_location = credentials_path .as_ref() .map(|p| p.as_ref().join("credentials.json")); - if let Some(location) = &volume { + if let Some(location) = &volume_path { fs::create_dir_all(location)?; } - let volume_location = volume.as_ref().map(|p| p.as_ref().join("volume")); + let volume_location = volume_path.as_ref().map(|p| p.as_ref().join("volume")); - if let Some(location) = &audio { + if let Some(location) = &audio_path { fs::create_dir_all(location)?; if let Some(limit) = size_limit { @@ -269,7 +269,7 @@ impl Cache { } } - let audio_location = audio.map(|p| p.as_ref().to_owned()); + let audio_location = audio_path.map(|p| p.as_ref().to_owned()); let cache = Cache { credentials_location,