From c891e552a0cf4f937a541ff4a220ecec529cbdc2 Mon Sep 17 00:00:00 2001 From: Florian Pritz Date: Fri, 17 Aug 2018 15:12:39 +0200 Subject: Die when XDG_* and HOME env vars are not set Signed-off-by: Florian Pritz --- lib/App/BorgRestore/Settings.pm | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/lib/App/BorgRestore/Settings.pm b/lib/App/BorgRestore/Settings.pm index faad58f..7bf9664 100644 --- a/lib/App/BorgRestore/Settings.pm +++ b/lib/App/BorgRestore/Settings.pm @@ -105,16 +105,21 @@ See LICENSE for the full license text. =cut our $borg_repo = "backup:borg-".hostname; -our $cache_path_base = sprintf("%s/borg-restore.pl", $ENV{XDG_CACHE_HOME} // $ENV{HOME}."/.cache"); +our $cache_path_base; our @backup_prefixes = ( {regex => "^/", replacement => ""}, ); our $sqlite_cache_size = 102400; +my @configfiles; -my @configfiles = ( - sprintf("%s/borg-restore.cfg", $ENV{XDG_CONFIG_HOME} // $ENV{HOME}."/.config"), - "/etc/borg-restore.cfg", -); +if (defined $ENV{XDG_CONFIG_HOME} or defined $ENV{HOME}) { + push @configfiles, sprintf("%s/borg-restore.cfg", $ENV{XDG_CONFIG_HOME} // $ENV{HOME}."/.config"); +} +push @configfiles, "/etc/borg-restore.cfg"; + +if (defined $ENV{XDG_CACHE_HOME} or defined $ENV{HOME}) { + $cache_path_base = sprintf("%s/borg-restore.pl", $ENV{XDG_CACHE_HOME} // $ENV{HOME} ."/.cache"); +} for my $configfile (@configfiles) { $configfile = App::BorgRestore::Helper::untaint($configfile, qr/.*/); @@ -126,6 +131,13 @@ for my $configfile (@configfiles) { } } } + +if (not defined $cache_path_base) { + die "Error: \$cache_path_base is not defined. This is most likely because the\n" + ."environment variables \$HOME and \$XDG_CACHE_HOME are not set. Consider setting\n" + ."the path in the config file or ensure that the variables are set."; +} + $cache_path_base = App::BorgRestore::Helper::untaint($cache_path_base, qr/.*/); sub get_cache_base_dir_path { -- cgit v1.2.3-24-g4f1b