diff options
author | Luca Matei Pintilie <luca@lucamatei.com> | 2023-09-18 17:58:17 +0200 |
---|---|---|
committer | Thomas Voss <mail@thomasvoss.com> | 2023-09-18 19:48:03 +0200 |
commit | 6f392071ed416f1767e209f1d6c98b1d70447337 (patch) | |
tree | 443c3a16e2c3e5cb2c0cc517e812ad56024f1c71 | |
parent | 9e979cef7217519e116a4d54b60bbbf21348a0aa (diff) |
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 <luca@lucamatei.com>
-rw-r--r-- | src/main.rs | 28 |
1 files 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::<PathBuf>() +} + 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::<PathBuf>(), + 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 = [ |