* Several improvements to restore: cancel now reports the correct
[BackupPC.git] / lib / BackupPC / Xfer / Tar.pm
index 6d313d4..c4f9c6a 100644 (file)
@@ -29,7 +29,7 @@
 #
 #========================================================================
 #
-# Version 1.5.0, released 2 Aug 2002.
+# Version 2.0.0beta3, released 1 Jun 2003.
 #
 # See http://backuppc.sourceforge.net.
 #
@@ -43,6 +43,7 @@ sub new
 {
     my($class, $bpc, $args) = @_;
 
+    $args ||= {};
     my $t = bless {
         bpc       => $bpc,
         conf      => { $bpc->Conf },
@@ -58,16 +59,30 @@ sub new
     return $t;
 }
 
+sub args
+{
+    my($t, $args) = @_;
+
+    foreach my $arg ( keys(%$args) ) {
+       $t->{$arg} = $args->{$arg};
+    }
+}
+
+sub useTar
+{
+    return 1;
+}
+
 sub start
 {
     my($t) = @_;
     my $bpc = $t->{bpc};
     my $conf = $t->{conf};
-    my(@fileList, @tarClientCmd, $logMsg, $incrDate);
+    my(@fileList, $tarClientCmd, $logMsg, $incrDate);
     local(*TAR);
 
     if ( $t->{type} eq "restore" ) {
-        push(@tarClientCmd, split(/ +/, $conf->{TarClientRestoreCmd}));
+       $tarClientCmd = $conf->{TarClientRestoreCmd};
         $logMsg = "restore started below directory $t->{shareName}";
        #
        # restores are considered to work unless we see they fail
@@ -110,52 +125,37 @@ sub start
         } else {
            push(@fileList, ".");
         }
+       if ( ref($conf->{TarClientCmd}) eq "ARRAY" ) {
+           $tarClientCmd = $conf->{TarClientCmd};
+       } else {
+           $tarClientCmd = [split(/ +/, $conf->{TarClientCmd})];
+       }
+       my $args;
         if ( $t->{type} eq "full" ) {
-           push(@tarClientCmd,
-                   split(/ +/, $conf->{TarClientCmd}),
-                   split(/ +/, $conf->{TarFullArgs})
-           );
+           $args = $conf->{TarFullArgs};
             $logMsg = "full backup started for directory $t->{shareName}";
         } else {
             $incrDate = $bpc->timeStampISO($t->{lastFull} - 3600, 1);
-           push(@tarClientCmd,
-                   split(/ +/, $conf->{TarClientCmd}),
-                   split(/ +/, $conf->{TarIncrArgs})
-           );
+           $args = $conf->{TarIncrArgs};
             $logMsg = "incr backup started back to $incrDate for directory"
                     . " $t->{shareName}";
         }
+       push(@$tarClientCmd, split(/ +/, $args));
     }
     #
     # Merge variables into @tarClientCmd
     #
-    my $vars = {
+    my $args = {
         host      => $t->{host},
         hostIP    => $t->{hostIP},
+        client    => $t->{client},
         incrDate  => $incrDate,
         shareName => $t->{shareName},
+       fileList  => \@fileList,
         tarPath   => $conf->{TarClientPath},
         sshPath   => $conf->{SshPath},
     };
-    my @cmd = @tarClientCmd;
-    @tarClientCmd = ();
-    foreach my $arg ( @cmd ) {
-       next if ( $arg =~ /^\s*$/ );
-       if ( $arg =~ /^\$fileList(\+?)/ ) {
-           my $esc = $1 eq "+";
-           foreach $arg ( @fileList ) {
-               $arg = $bpc->shellEscape($arg) if ( $esc );
-               push(@tarClientCmd, $arg);
-           }
-       } else {
-           $arg =~ s{\$(\w+)(\+?)}{
-               defined($vars->{$1})
-                   ? ($2 eq "+" ? $bpc->shellEscape($vars->{$1}) : $vars->{$1})
-                   : "\$$1"
-           }eg;
-           push(@tarClientCmd, $arg);
-       }
-    }
+    $tarClientCmd = $bpc->cmdVarSubstitute($tarClientCmd, $args);
     if ( !defined($t->{xferPid} = open(TAR, "-|")) ) {
         $t->{_errStr} = "Can't fork to run tar";
         return;
@@ -189,13 +189,14 @@ sub start
         #
         # Run the tar command
         #
-        exec(@tarClientCmd);
+       $bpc->cmdExecOrEval($tarClientCmd, $args);
         # should not be reached, but just in case...
-        $t->{_errStr} = "Can't exec @tarClientCmd";
+        $t->{_errStr} = "Can't exec @$tarClientCmd";
         return;
     }
-    $t->{XferLOG}->write(\"Running: @tarClientCmd\n");
-    alarm($conf->{SmbClientTimeout});
+    my $str = "Running: " . $bpc->execCmd2ShellCmd(@$tarClientCmd) . "\n";
+    $t->{XferLOG}->write(\"Running: @$tarClientCmd\n");
+    alarm($conf->{ClientTimeout});
     $t->{_errStr} = undef;
     return $logMsg;
 }
@@ -224,7 +225,7 @@ sub readOutput
         #
         # refresh our inactivity alarm
         #
-        alarm($conf->{SmbClientTimeout});
+        alarm($conf->{ClientTimeout});
         $t->{lastOutputLine} = $_ if ( !/^$/ );
         if ( /^Total bytes written: / ) {
             $t->{xferOK} = 1;
@@ -262,7 +263,7 @@ sub xferPid
 {
     my($t) = @_;
 
-    return $t->{xferPid};
+    return ($t->{xferPid});
 }
 
 sub logMsg