From 371f62a3bbecd32eecf8ad2c5455771c7be6de91 Mon Sep 17 00:00:00 2001 From: Florian Pritz Date: Sun, 11 Jun 2017 16:05:49 +0200 Subject: Use File::pushd for temporary chdir Signed-off-by: Florian Pritz --- lib/App/BorgRestore.pm | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) (limited to 'lib/App/BorgRestore.pm') diff --git a/lib/App/BorgRestore.pm b/lib/App/BorgRestore.pm index a3f4838..d250b61 100644 --- a/lib/App/BorgRestore.pm +++ b/lib/App/BorgRestore.pm @@ -14,6 +14,7 @@ use autodie; use Carp; use Cwd qw(abs_path getcwd); use File::Basename; +use File::pushd; use File::Spec; use File::Temp; use Function::Parameters; @@ -330,16 +331,18 @@ method restore($path, $archive, $destination) { my $components_to_strip =()= $path =~ /\//g; $log->debugf("CWD is %s", getcwd()); - $log->debugf("Changing CWD to %s", $destination); - mkdir($destination) unless -d $destination; - chdir($destination) or die "Failed to chdir: $!"; - # TODO chdir back to original after restore - - my $final_destination = abs_path($basename); - $final_destination = App::BorgRestore::Helper::untaint($final_destination, qr(.*)); - $log->debugf("Removing %s", $final_destination); - File::Path::remove_tree($final_destination); - $self->{borg}->restore($components_to_strip, $archive_name, $path); + { + $log->debugf("Changing CWD to %s", $destination); + mkdir($destination) unless -d $destination; + my $workdir = pushd($destination, {untaint_pattern => qr{^(.*)$}}); + + my $final_destination = abs_path($basename); + $final_destination = App::BorgRestore::Helper::untaint($final_destination, qr(.*)); + $log->debugf("Removing %s", $final_destination); + File::Path::remove_tree($final_destination); + $self->{borg}->restore($components_to_strip, $archive_name, $path); + } + $log->debugf("CWD is %s", getcwd()); } =head3 restore_simple -- cgit v1.2.3-24-g4f1b