* checkin with 3.2.0beta0 release header
[BackupPC.git] / lib / BackupPC / Xfer / Smb.pm
index 06c1e4d..a92695a 100644 (file)
@@ -11,7 +11,7 @@
 #   Craig Barratt  <cbarratt@users.sourceforge.net>
 #
 # COPYRIGHT
-#   Copyright (C) 2001  Craig Barratt
+#   Copyright (C) 2001-2007  Craig Barratt
 #
 #   This program is free software; you can redistribute it and/or modify
 #   it under the terms of the GNU General Public License as published by
@@ -29,7 +29,7 @@
 #
 #========================================================================
 #
-# Version 1.6.0_CVS, released 10 Dec 2002.
+# Version 3.2.0beta0, released 5 April 2009.
 #
 # See http://backuppc.sourceforge.net.
 #
 package BackupPC::Xfer::Smb;
 
 use strict;
-
-sub new
-{
-    my($class, $bpc, $args) = @_;
-
-    $args ||= {};
-    my $t = bless {
-        bpc       => $bpc,
-        conf      => { $bpc->Conf },
-        host      => "",
-        hostIP    => "",
-        shareName => "",
-        pipeRH    => undef,
-        pipeWH    => undef,
-        badFiles  => [],
-        %$args,
-    }, $class;
-
-    return $t;
-}
-
-sub args
-{
-    my($t, $args) = @_;
-
-    foreach my $arg ( keys(%$args) ) {
-       $t->{$arg} = $args->{$arg};
-    }
-}
+use Encode qw/from_to encode/;
+use base qw(BackupPC::Xfer::Protocol);
 
 sub useTar
 {
@@ -78,8 +51,9 @@ sub start
     my($t) = @_;
     my $bpc = $t->{bpc};
     my $conf = $t->{conf};
-    my $I_option = $t->{hostIP} eq $t->{host} ? "" : " -I $t->{hostIP}";
-    my($fileList, $optX, $smbClientCmd, $logMsg);
+    my $I_option = $t->{hostIP} eq $t->{host} ? [] : ['-I', $t->{hostIP}];
+    my(@fileList, $X_option, $smbClientCmd, $logMsg);
+    my($timeStampFile);
     local(*SMB);
 
     #
@@ -97,72 +71,67 @@ sub start
         return;
     }
     if ( $t->{type} eq "restore" ) {
-        $smbClientCmd =
-              "$conf->{SmbClientPath} '\\\\$t->{host}\\$t->{shareName}'"
-            . "$I_option -U '$conf->{SmbShareUserName}' -E -N -d 1"
-            . " $conf->{SmbClientArgs}"
-            . " -c 'tarmode full' -Tx -";
+        $smbClientCmd = $conf->{SmbClientRestoreCmd};
         $logMsg = "restore started for share $t->{shareName}";
     } else {
        #
        # Turn $conf->{BackupFilesOnly} and $conf->{BackupFilesExclude}
-       # into a hash of arrays of files
+       # into a hash of arrays of files, and $conf->{SmbShareName}
+       # to an array
        #
-       $conf->{SmbShareName} = [ $conf->{SmbShareName} ]
-                       unless ref($conf->{SmbShareName}) eq "ARRAY";
-       foreach my $param qw(BackupFilesOnly BackupFilesExclude) {
-           next if ( !defined($conf->{$param}) );
-           if ( ref($conf->{$param}) eq "ARRAY" ) {
-               $conf->{$param} = {
-                       $conf->{SmbShareName}[0] => $conf->{$param}
-               };
-           } elsif ( ref($conf->{$param}) eq "HASH" ) {
-               # do nothing
-           } else {
-               $conf->{$param} = {
-                       $conf->{SmbShareName}[0] => [ $conf->{$param} ]
-               };
-           }
-       }
+       $bpc->backupFileConfFix($conf, "SmbShareName");
+
+       $t->{fileIncludeHash} = {};
         if ( defined($conf->{BackupFilesOnly}{$t->{shareName}}) ) {
             foreach my $file ( @{$conf->{BackupFilesOnly}{$t->{shareName}}} ) {
-                $file =~ s/'/\\'/g;
-                $fileList .= "'$file' ";
+                $file = encode($conf->{ClientCharset}, $file)
+                            if ( $conf->{ClientCharset} ne "" );
+               push(@fileList, $file);
+               $t->{fileIncludeHash}{$file} = 1;
             }
         } elsif ( defined($conf->{BackupFilesExclude}{$t->{shareName}}) ) {
             foreach my $file ( @{$conf->{BackupFilesExclude}{$t->{shareName}}} )
             {
-                $file =~ s/'/\\'/g;
-                $fileList .= "'$file' ";
+                $file = encode($conf->{ClientCharset}, $file)
+                            if ( $conf->{ClientCharset} ne "" );
+               push(@fileList, $file);
             }
            #
            # Allow simple wildcards in exclude list by specifying "r" option.
            #
-            $optX = "rX";
+            $X_option = "rX";
         }
         if ( $t->{type} eq "full" ) {
-            $smbClientCmd =
-                  "$conf->{SmbClientPath} '\\\\$t->{host}\\$t->{shareName}'"
-                . "$I_option -U '$conf->{SmbShareUserName}' -E -N -d 1"
-                . " $conf->{SmbClientArgs}"
-                . " -c 'tarmode full'"
-                . " -Tc$optX - $fileList";
+           $smbClientCmd = $conf->{SmbClientFullCmd};
             $logMsg = "full backup started for share $t->{shareName}";
         } else {
-            my $timeStampFile = "$t->{outDir}/timeStamp.level0";
-            open(LEV0, ">$timeStampFile") && close(LEV0);
-            utime($t->{lastFull} - 3600, $t->{lastFull} - 3600, $timeStampFile);
-            $smbClientCmd =
-                  "$conf->{SmbClientPath} '\\\\$t->{host}\\$t->{shareName}'"
-                . "$I_option -U '$conf->{SmbShareUserName}' -E -N -d 1"
-                . " $conf->{SmbClientArgs}"
-                . " -c 'tarmode full'"
-                . " -TcN$optX $timeStampFile - $fileList";
+            $timeStampFile = "$t->{outDir}/timeStamp.level0";
+            open(LEV0, ">", $timeStampFile) && close(LEV0);
+            utime($t->{incrBaseTime} - 3600, $t->{incrBaseTime} - 3600,
+                  $timeStampFile);
+           $smbClientCmd = $conf->{SmbClientIncrCmd};
             $logMsg = "incr backup started back to "
-                        . $bpc->timeStamp($t->{lastFull} - 3600, 0)
-                        . "for share $t->{shareName}";
+                    . $bpc->timeStamp($t->{incrBaseTime} - 3600, 0)
+                    . " (backup #$t->{incrBaseBkupNum}) for share"
+                    . " $t->{shareName}";
         }
     }
+    my $args = {
+       smbClientPath => $conf->{SmbClientPath},
+       host          => $t->{host},
+       hostIP        => $t->{hostIP},
+       client        => $t->{client},
+       shareName     => $t->{shareName},
+       userName      => $conf->{SmbShareUserName},
+       fileList      => \@fileList,
+       I_option      => $I_option,
+       X_option      => $X_option,
+       timeStampFile => $timeStampFile,
+    };
+    from_to($args->{shareName}, "utf8", $conf->{ClientCharset})
+                            if ( $conf->{ClientCharset} ne "" );
+    $smbClientCmd = $bpc->cmdVarSubstitute($smbClientCmd, $args);
+
     if ( !defined($t->{xferPid} = open(SMB, "-|")) ) {
         $t->{_errStr} = "Can't fork to run smbclient";
         return;
@@ -194,15 +163,19 @@ sub start
             open(STDOUT, ">&$t->{pipeWH}");
         }
         #
-        # exec smbclient.
+        # Run smbclient.
         #
-        exec($smbClientCmd);
+       alarm(0);
+        $bpc->cmdExecOrEval($smbClientCmd, $args);
         # should not be reached, but just in case...
         $t->{_errStr} = "Can't exec $conf->{SmbClientPath}";
         return;
     }
-    $t->{XferLOG}->write(\"Running: $smbClientCmd\n");
-    alarm($conf->{SmbClientTimeout});
+    my $str = "Running: " . $bpc->execCmd2ShellCmd(@$smbClientCmd) . "\n";
+    from_to($str, $conf->{ClientCharset}, "utf8")
+                            if ( $conf->{ClientCharset} ne "" );
+    $t->{XferLOG}->write(\$str);
+    alarm($conf->{ClientTimeout});
     $t->{_errStr} = undef;
     return $logMsg;
 }
@@ -224,17 +197,27 @@ sub readOutput
     while ( $t->{smbOut} =~ /(.*?)[\n\r]+(.*)/s ) {
         $_ = $1;
         $t->{smbOut} = $2;
-        $t->{XferLOG}->write(\"$_\n");
+       #
+       # ignore the log file time stamps from smbclient introduced
+       # in version 3.0.0 - don't even write them to the log file.
+       #
+       if ( m{^\[\d+/\d+/\d+ +\d+:\d+:\d+.*\] +(client/cli|lib/util_unistr).*\(\d+\)} ) {
+            $t->{XferLOG}->write(\"$_\n") if ( $t->{logLevel} >= 5 );
+            next;
+        }
         #
         # refresh our inactivity alarm
         #
-        alarm($conf->{SmbClientTimeout});
+        alarm($conf->{ClientTimeout}) if ( !$t->{abort} );
         $t->{lastOutputLine} = $_ if ( !/^$/ );
+
+        from_to($_, $conf->{ClientCharset}, "utf8")
+                            if ( $conf->{ClientCharset} ne "" );
         #
         # This section is highly dependent on the version of smbclient.
         # If you upgrade Samba, make sure that these regexp are still valid.
         #
-        if ( /^\s*(-?\d+) \(\s*\d+\.\d kb\/s\) (.*)$/ ) {
+        if ( /^\s*(-?\d+) \(\s*\d+[.,]\d kb\/s\) (.*)$/ ) {
             my $sambaFileSize = $1;
             my $pcFileName    = $2;
             (my $fileName = $pcFileName) =~ s/\\/\//g;
@@ -242,34 +225,65 @@ sub readOutput
             $fileName =~ s/^\/*//;
             $t->{byteCnt} += $sambaFileSize;
             $t->{fileCnt}++;
+            $t->{XferLOG}->write(\"$_\n") if ( $t->{logLevel} >= 2 );
         } elsif ( /restore tar file (.*) of size (\d+) bytes/ ) {
             $t->{byteCnt} += $2;
             $t->{fileCnt}++;
-        } elsif ( /tar: dumped \d+ files/ ) {
+            $t->{XferLOG}->write(\"$_\n") if ( $t->{logLevel} >= 1 );
+        } elsif ( /^\s*tar: dumped \d+ files/ ) {
             $t->{xferOK} = 1;
-        } elsif ( /^tar: restored \d+ files/ ) {
+            $t->{XferLOG}->write(\"$_\n") if ( $t->{logLevel} >= 0 );
+        } elsif ( /^\s*tar: restored \d+ files/ ) {
             $t->{xferOK} = 1;
-        } elsif ( /^read_socket_with_timeout: timeout read. /i ) {
+            $t->{XferLOG}->write(\"$_\n") if ( $t->{logLevel} >= 0 );
+        } elsif ( /^\s*read_socket_with_timeout: timeout read. /i ) {
             $t->{hostAbort} = 1;
+            $t->{XferLOG}->write(\"$_\n") if ( $t->{logLevel} >= 0 );
         } elsif ( /^code 0 listing /
-                    || /^code 0 opening /
-                    || /^abandoning restore/i
-                    || /^Error: Looping in FIND_NEXT/i
-                    || /^SUCCESS - 0/i
-                    || /^Call timed out: server did not respond/i
+                    || /^\s*code 0 opening /
+                    || /^\s*abandoning restore/i
+                    || /^\s*Error: Looping in FIND_NEXT/i
+                    || /^\s*SUCCESS - 0/i
+                    || /^\s*Call timed out: server did not respond/i
+                   || /^\s*tree connect failed: ERRDOS - ERRnoaccess \(Access denied\.\)/
+                   || /^\s*tree connect failed: NT_STATUS_BAD_NETWORK_NAME/
+                   || /^\s*NT_STATUS_INSUFF_SERVER_RESOURCES listing /
                  ) {
-            $t->{hostError} ||= $_;
+           if ( $t->{hostError} eq "" ) {
+               $t->{XferLOG}->write(\"This backup will fail because: $_\n");
+               $t->{hostError} = $_;
+           }
+            $t->{XferLOG}->write(\"$_\n") if ( $t->{logLevel} >= 0 );
+        } elsif ( /^\s*NT_STATUS_ACCESS_DENIED listing (.*)/
+              || /^\s*ERRDOS - ERRnoaccess \(Access denied\.\) listing (.*)/ ) {
+            $t->{xferErrCnt}++;
+           my $badDir = $1;
+           $badDir =~ s{\\}{/}g;
+           $badDir =~ s{/+}{/}g;
+           $badDir =~ s{/\*$}{};
+           if ( $t->{hostError} eq ""
+                   && ($badDir eq "" || $t->{fileIncludeHash}{$badDir}) ) {
+               $t->{XferLOG}->write(\"This backup will fail because: $_\n");
+               $t->{hostError} ||= $_;
+           }
+            $t->{XferLOG}->write(\"$_\n") if ( $t->{logLevel} >= 0 );
+        } elsif ( /^\s*directory \\/i ) {
+            $t->{XferLOG}->write(\"$_\n") if ( $t->{logLevel} >= 2 );
         } elsif ( /smb: \\>/
-                || /^added interface/i
-                || /^tarmode is now/i
-                || /^Total bytes written/i
-                || /^Domain=/i
+                || /^\s*added interface/i
+                || /^\s*tarmode is now/i
+                || /^\s*Total bytes written/i
+                || /^\s*Domain=/i
                 || /^\([\d\.]* kb\/s\) \(average [\d\.]* kb\/s\)$/i
-                || /^Getting files newer than/i
-                || /^\s+directory \\/i
-                || /^Output is \/dev\/null/i
-                || /^Timezone is/i ) {
+                || /^\s*Getting files newer than/i
+               || /^\s*restore directory \\/i
+                || /^\s*Output is \/dev\/null/i
+                || /^\s*Timezone is/i
+                || /^\s*tar_re_search set/i
+                || /^\s*creating lame (up|low)case table/i
+           ) {
             # ignore these messages
+            $t->{XferLOG}->write(\"$_\n") if ( $t->{logLevel} >= 1 );
         } else {
             $t->{xferErrCnt}++;
             $t->{xferBadShareCnt}++ if ( /^ERRDOS - ERRbadshare/ );
@@ -297,8 +311,12 @@ sub readOutput
                 my $badFile = $1;
                 $badFile =~ s{\\}{/}g;
                 $badFile =~ s{^/}{};
-                push(@{$t->{badFiles}}, "$t->{shareName}/$badFile");
+                push(@{$t->{badFiles}}, {
+                       share => $t->{shareName},
+                       file  => $badFile
+                   });
             }
+            $t->{XferLOG}->write(\"$_\n") if ( $t->{logLevel} >= 1 );
         }
     }
     return 1;
@@ -311,53 +329,4 @@ sub setSelectMask
     vec($$FDreadRef, fileno($t->{pipeSMB}), 1) = 1;
 }
 
-sub errStr
-{
-    my($t) = @_;
-
-    return $t->{_errStr};
-}
-
-sub xferPid
-{
-    my($t) = @_;
-
-    return $t->{xferPid};
-}
-
-sub logMsg
-{
-    my($t, $msg) = @_;
-
-    push(@{$t->{_logMsg}}, $msg);
-}
-
-sub logMsgGet
-{
-    my($t) = @_;
-
-    return shift(@{$t->{_logMsg}});
-}
-
-#
-# Returns a hash ref giving various status information about
-# the transfer.
-#
-sub getStats
-{
-    my($t) = @_;
-
-    return { map { $_ => $t->{$_} }
-            qw(byteCnt fileCnt xferErrCnt xferBadShareCnt xferBadFileCnt
-               xferOK hostAbort hostError lastOutputLine)
-    };
-}
-
-sub getBadFiles
-{
-    my($t) = @_;
-
-    return @{$t->{badFiles}};
-}
-
 1;