From 6f392071ed416f1767e209f1d6c98b1d70447337 Mon Sep 17 00:00:00 2001 From: Luca Matei Pintilie Date: Mon, 18 Sep 2023 17:58:17 +0200 Subject: Support empty XDG_CACHE_HOME environmental variable Follow-up from e5b6e24a621, fixes a scenario in which XDG_CACHE_HOME is set, but it's set to an empty string instead of a path. From the documentation >If $XDG_CACHE_HOME is [...] empty, a default equal to $HOME/.cache should be used. https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html Signed-off-by: Luca Matei Pintilie --- src/main.rs | 28 +++++++++++++++++++--------- 1 file changed, 19 insertions(+), 9 deletions(-) diff --git a/src/main.rs b/src/main.rs index 6c74669..c1f1311 100644 --- a/src/main.rs +++ b/src/main.rs @@ -108,6 +108,17 @@ fn main() { require!(work()) } +fn get_default_config_path() -> PathBuf { + [ + &env::var("HOME").unwrap_or_else(|_| { + err!("One of the XDG_CACHE_HOME or HOME variables must be set"); + }), + ".cache", + ] + .iter() + .collect::() +} + fn work() -> Result<(), io::Error> { let (flags, rest) = match Flags::parse() { Ok(a) => a, @@ -182,15 +193,14 @@ fn work() -> Result<(), io::Error> { .as_nanos() .to_string(); let cache_base = match env::var("XDG_CACHE_HOME") { - Ok(s) => PathBuf::from(s), - _ => [ - &env::var("HOME").unwrap_or_else(|_| { - err!("One of the XDG_CACHE_HOME or HOME variables must be set"); - }), - ".cache", - ] - .iter() - .collect::(), + Ok(s) => { + if s.is_empty() { + get_default_config_path() + } else { + PathBuf::from(s) + } + } + _ => get_default_config_path(), }; let mmv_name = option_env!("MMV_NAME").unwrap_or(MMV_DEFAULT_NAME); cache_dir = [ -- cgit v1.2.3