X-Git-Url: http://git.rot13.org/?p=BackupPC.git;a=blobdiff_plain;f=lib%2FBackupPC%2FLib.pm;h=251f9094e7b8544f6b47ce8129f09a023eefa92f;hp=8cc1e83a054b899d2fe9893e750f64655ceff3a1;hb=5b3e6091d542c2e7445d5dd511cdf6e20aec8b8d;hpb=0697368bbcef14908cd4684cf07744dc840464de diff --git a/lib/BackupPC/Lib.pm b/lib/BackupPC/Lib.pm index 8cc1e83..251f909 100644 --- a/lib/BackupPC/Lib.pm +++ b/lib/BackupPC/Lib.pm @@ -11,7 +11,7 @@ # Craig Barratt # # COPYRIGHT -# Copyright (C) 2001 Craig Barratt +# Copyright (C) 2001-2003 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 2.0.0_CVS, released 18 Jan 2003. +# Version 3.0.0alpha, released 23 Jan 2006. # # See http://backuppc.sourceforge.net. # @@ -40,6 +40,7 @@ package BackupPC::Lib; use strict; use vars qw(%Conf %Lang); +use BackupPC::Storage; use Fcntl qw/:flock/; use Carp; use DirHandle (); @@ -48,31 +49,58 @@ use File::Compare; use Socket; use Cwd; use Digest::MD5; +use Config; sub new { my $class = shift; - my($topDir, $installDir) = @_; + my($topDir, $installDir, $confDir, $noUserCheck) = @_; + + # + # Whether to use filesystem hierarchy standard for file layout. + # If set, text config files are below /etc/BackupPC. + # + my $useFHS = 0; + my $paths; + + # + # Set defaults for $topDir and $installDir. + # + $topDir = '/tera0/backup/BackupPC' if ( $topDir eq "" ); + $installDir = '/usr/local/BackupPC' if ( $installDir eq "" ); + + # + # Pick some initial defaults. For FHS the only critical + # path is the ConfDir, since we get everything else out + # of the main config file. + # + if ( $useFHS ) { + $paths = { + useFHS => $useFHS, + TopDir => $topDir, + BinDir => "$installDir/bin", + LibDir => "$installDir/lib", + ConfDir => $confDir eq "" ? '/etc/BackupPC' : $confDir, + LogDir => '/var/log/BackupPC', + }; + } else { + $paths = { + useFHS => $useFHS, + TopDir => $topDir, + BinDir => "$installDir/bin", + LibDir => "$installDir/lib", + ConfDir => $confDir eq "" ? "$topDir/conf" : $confDir, + LogDir => "$topDir/log", + }; + } my $bpc = bless { - TopDir => $topDir || '/data/BackupPC', - BinDir => $installDir || '/usr/local/BackupPC', - LibDir => $installDir || '/usr/local/BackupPC', - Version => '2.0.0_CVS', - BackupFields => [qw( - num type startTime endTime - nFiles size nFilesExist sizeExist nFilesNew sizeNew - xferErrs xferBadFile xferBadShare tarErrs - compress sizeExistComp sizeNewComp - noFill fillFromNum mangle xferMethod level - )], - RestoreFields => [qw( - num startTime endTime result errorMsg nFiles size - tarCreateErrs xferErrs - )], + %$paths, + Version => '3.0.0alpha', }, $class; - $bpc->{BinDir} .= "/bin"; - $bpc->{LibDir} .= "/lib"; + + $bpc->{storage} = BackupPC::Storage->new($paths); + # # Clean up %ENV and setup other variables. # @@ -83,6 +111,26 @@ sub new print(STDERR $error, "\n"); return; } + + # + # Update the paths based on the config file + # + foreach my $dir ( qw(TopDir BinDir LibDir ConfDir LogDir) ) { + next if ( !defined($bpc->{Conf}{$dir}) ); + $paths->{$dir} = $bpc->{$dir} = $bpc->{Conf}{$dir}; + } + $bpc->{storage}->setPaths($paths); + + # + # Verify we are running as the correct user + # + if ( !$noUserCheck + && $bpc->{Conf}{BackupPCUserVerify} + && $> != (my $uid = (getpwnam($bpc->{Conf}{BackupPCUser}))[2]) ) { + print(STDERR "Wrong user: my userid is $>, instead of $uid" + . " ($bpc->{Conf}{BackupPCUser})\n"); + return; + } return $bpc; } @@ -98,6 +146,30 @@ sub BinDir return $bpc->{BinDir}; } +sub LogDir +{ + my($bpc) = @_; + return $bpc->{LogDir}; +} + +sub ConfDir +{ + my($bpc) = @_; + return $bpc->{ConfDir}; +} + +sub LibDir +{ + my($bpc) = @_; + return $bpc->{LibDir}; +} + +sub useFHS +{ + my($bpc) = @_; + return $bpc->{useFHS}; +} + sub Version { my($bpc) = @_; @@ -118,7 +190,15 @@ sub Lang sub adminJob { - return " admin "; + my($bpc, $num) = @_; + return " admin " if ( !$num ); + return " admin$num "; +} + +sub isAdminJob +{ + my($bpc, $str) = @_; + return $str =~ /^ admin/; } sub trashJob @@ -126,141 +206,131 @@ sub trashJob return " trashClean "; } -sub timeStamp +sub ConfValue { - my($bpc, $t, $noPad) = @_; - my($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) - = localtime($t || time); - $year += 1900; - $mon++; - return "$year/$mon/$mday " . sprintf("%02d:%02d:%02d", $hour, $min, $sec) - . ($noPad ? "" : " "); + my($bpc, $param) = @_; + + return $bpc->{Conf}{$param}; +} + +sub verbose +{ + my($bpc, $param) = @_; + + $bpc->{verbose} = $param if ( defined($param) ); + return $bpc->{verbose}; +} + +sub sigName2num +{ + my($bpc, $sig) = @_; + + if ( !defined($bpc->{SigName2Num}) ) { + my $i = 0; + foreach my $name ( split(' ', $Config{sig_name}) ) { + $bpc->{SigName2Num}{$name} = $i; + $i++; + } + } + return $bpc->{SigName2Num}{$sig}; } # -# An ISO 8601-compliant version of timeStamp. Needed by the -# --newer-mtime argument to GNU tar in BackupPC::Xfer::Tar. -# Also see http://www.w3.org/TR/NOTE-datetime. +# Generate an ISO 8601 format timeStamp (but without the "T"). +# See http://www.w3.org/TR/NOTE-datetime and +# http://www.cl.cam.ac.uk/~mgk25/iso-time.html # -sub timeStampISO +sub timeStamp { my($bpc, $t, $noPad) = @_; my($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime($t || time); - $year += 1900; - $mon++; - return sprintf("%04d-%02d-%02d ", $year, $mon, $mday) - . sprintf("%02d:%02d:%02d", $hour, $min, $sec) - . ($noPad ? "" : " "); + return sprintf("%04d-%02d-%02d %02d:%02d:%02d", + $year + 1900, $mon + 1, $mday, $hour, $min, $sec) + . ($noPad ? "" : " "); } sub BackupInfoRead { my($bpc, $host) = @_; - local(*BK_INFO, *LOCK); - my(@Backups); - - flock(LOCK, LOCK_EX) if open(LOCK, "$bpc->{TopDir}/pc/$host/LOCK"); - if ( open(BK_INFO, "$bpc->{TopDir}/pc/$host/backups") ) { - while ( ) { - s/[\n\r]+//; - next if ( !/^(\d+\t(incr|full)[\d\t]*$)/ ); - $_ = $1; - @{$Backups[@Backups]}{@{$bpc->{BackupFields}}} = split(/\t/); - } - close(BK_INFO); - } - close(LOCK); - return @Backups; + + return $bpc->{storage}->BackupInfoRead($host); } sub BackupInfoWrite { my($bpc, $host, @Backups) = @_; - local(*BK_INFO, *LOCK); - my($i); - - flock(LOCK, LOCK_EX) if open(LOCK, "$bpc->{TopDir}/pc/$host/LOCK"); - unlink("$bpc->{TopDir}/pc/$host/backups.old") - if ( -f "$bpc->{TopDir}/pc/$host/backups.old" ); - rename("$bpc->{TopDir}/pc/$host/backups", - "$bpc->{TopDir}/pc/$host/backups.old") - if ( -f "$bpc->{TopDir}/pc/$host/backups" ); - if ( open(BK_INFO, ">$bpc->{TopDir}/pc/$host/backups") ) { - for ( $i = 0 ; $i < @Backups ; $i++ ) { - my %b = %{$Backups[$i]}; - printf(BK_INFO "%s\n", join("\t", @b{@{$bpc->{BackupFields}}})); - } - close(BK_INFO); - } - close(LOCK); + + return $bpc->{storage}->BackupInfoWrite($host, @Backups); } sub RestoreInfoRead { my($bpc, $host) = @_; - local(*RESTORE_INFO, *LOCK); - my(@Restores); - - flock(LOCK, LOCK_EX) if open(LOCK, "$bpc->{TopDir}/pc/$host/LOCK"); - if ( open(RESTORE_INFO, "$bpc->{TopDir}/pc/$host/restores") ) { - while ( ) { - s/[\n\r]+//; - next if ( !/^(\d+.*)/ ); - $_ = $1; - @{$Restores[@Restores]}{@{$bpc->{RestoreFields}}} = split(/\t/); - } - close(RESTORE_INFO); - } - close(LOCK); - return @Restores; + + return $bpc->{storage}->RestoreInfoRead($host); } sub RestoreInfoWrite { my($bpc, $host, @Restores) = @_; - local(*RESTORE_INFO, *LOCK); - my($i); - - flock(LOCK, LOCK_EX) if open(LOCK, "$bpc->{TopDir}/pc/$host/LOCK"); - unlink("$bpc->{TopDir}/pc/$host/restores.old") - if ( -f "$bpc->{TopDir}/pc/$host/restores.old" ); - rename("$bpc->{TopDir}/pc/$host/restores", - "$bpc->{TopDir}/pc/$host/restores.old") - if ( -f "$bpc->{TopDir}/pc/$host/restores" ); - if ( open(RESTORE_INFO, ">$bpc->{TopDir}/pc/$host/restores") ) { - for ( $i = 0 ; $i < @Restores ; $i++ ) { - my %b = %{$Restores[$i]}; - printf(RESTORE_INFO "%s\n", - join("\t", @b{@{$bpc->{RestoreFields}}})); - } - close(RESTORE_INFO); - } - close(LOCK); + + return $bpc->{storage}->RestoreInfoWrite($host, @Restores); +} + +sub ArchiveInfoRead +{ + my($bpc, $host) = @_; + + return $bpc->{storage}->ArchiveInfoRead($host); +} + +sub ArchiveInfoWrite +{ + my($bpc, $host, @Archives) = @_; + + return $bpc->{storage}->ArchiveInfoWrite($host, @Archives); +} + +sub ConfigDataRead +{ + my($bpc, $host) = @_; + + return $bpc->{storage}->ConfigDataRead($host); +} + +sub ConfigDataWrite +{ + my($bpc, $host, $conf) = @_; + + return $bpc->{storage}->ConfigDataWrite($host, $conf); } sub ConfigRead { my($bpc, $host) = @_; - my($ret, $mesg, $config, @configs); - - $bpc->{Conf} = (); - push(@configs, "$bpc->{TopDir}/conf/config.pl"); - push(@configs, "$bpc->{TopDir}/conf/$host.pl") - if ( $host ne "config" && -f "$bpc->{TopDir}/conf/$host.pl" ); - push(@configs, "$bpc->{TopDir}/pc/$host/config.pl") - if ( defined($host) && -f "$bpc->{TopDir}/pc/$host/config.pl" ); - foreach $config ( @configs ) { - %Conf = (); - if ( !defined($ret = do $config) && ($! || $@) ) { - $mesg = "Couldn't open $config: $!" if ( $! ); - $mesg = "Couldn't execute $config: $@" if ( $@ ); - $mesg =~ s/[\n\r]+//; - return $mesg; - } - %{$bpc->{Conf}} = ( %{$bpc->{Conf} || {}}, %Conf ); + my($ret); + + # + # Read main config file + # + my($mesg, $config) = $bpc->{storage}->ConfigDataRead(); + return $mesg if ( defined($mesg) ); + + $bpc->{Conf} = $config; + + # + # Read host config file + # + if ( $host ne "" ) { + ($mesg, $config) = $bpc->{storage}->ConfigDataRead($host); + return $mesg if ( defined($mesg) ); + $bpc->{Conf} = { %{$bpc->{Conf}}, %$config }; } - return if ( !defined($bpc->{Conf}{Language}) ); + + # + # Load optional perl modules + # if ( defined($bpc->{Conf}{PerlModuleLoad}) ) { # # Load any user-specified perl modules. This is for @@ -272,6 +342,11 @@ sub ConfigRead eval("use $module;"); } } + + # + # Load language file + # + return "No language setting" if ( !defined($bpc->{Conf}{Language}) ); my $langFile = "$bpc->{LibDir}/BackupPC/Lang/$bpc->{Conf}{Language}.pm"; if ( !defined($ret = do $langFile) && ($! || $@) ) { $mesg = "Couldn't open language file $langFile: $!" if ( $! ); @@ -289,7 +364,8 @@ sub ConfigRead sub ConfigMTime { my($bpc) = @_; - return (stat("$bpc->{TopDir}/conf/config.pl"))[9]; + + return $bpc->{storage}->ConfigMTime(); } # @@ -302,46 +378,15 @@ sub ConfigMTime sub HostInfoRead { my($bpc, $host) = @_; - my(%hosts, @hdr, @fld); - local(*HOST_INFO); - - if ( !open(HOST_INFO, "$bpc->{TopDir}/conf/hosts") ) { - print(STDERR $bpc->timeStamp, - "Can't open $bpc->{TopDir}/conf/hosts\n"); - return {}; - } - while ( ) { - s/[\n\r]+//; - s/#.*//; - s/\s+$//; - next if ( /^\s*$/ || !/^([\w\.-\\]+\s+.*)/ ); - # - # Split on white space, except if preceded by \ - # using zero-width negative look-behind assertion - # (always wanted to use one of those). - # - @fld = split(/(?{storage}->HostInfoRead($host); +} + +sub HostInfoWrite +{ + my($bpc, $host) = @_; + + return $bpc->{storage}->HostInfoWrite($host); } # @@ -350,7 +395,8 @@ sub HostInfoRead sub HostsMTime { my($bpc) = @_; - return (stat("$bpc->{TopDir}/conf/hosts"))[9]; + + return $bpc->{storage}->HostsMTime(); } # @@ -369,7 +415,7 @@ sub RmTreeQuiet if ( defined($roots) && length($roots) ) { $roots = [$roots] unless ref $roots; } else { - print "RmTreeQuiet: No root path(s) specified\n"; + print(STDERR "RmTreeQuiet: No root path(s) specified\n"); } chdir($pwd); foreach $root (@{$roots}) { @@ -382,14 +428,17 @@ sub RmTreeQuiet # if ( !unlink($root) ) { if ( -d $root ) { - my $d = DirHandle->new($root) - or print "Can't read $pwd/$root: $!"; - @files = $d->read; - $d->close; - @files = grep $_!~/^\.{1,2}$/, @files; - $bpc->RmTreeQuiet("$pwd/$root", \@files); - chdir($pwd); - rmdir($root) || rmdir($root); + my $d = DirHandle->new($root); + if ( !defined($d) ) { + print(STDERR "Can't read $pwd/$root: $!\n"); + } else { + @files = $d->read; + $d->close; + @files = grep $_!~/^\.{1,2}$/, @files; + $bpc->RmTreeQuiet("$pwd/$root", \@files); + chdir($pwd); + rmdir($root) || rmdir($root); + } } else { unlink($root) || unlink($root); } @@ -425,7 +474,8 @@ sub RmTreeDefer } # -# Empty the trash directory. Returns 0 if it did nothing. +# Empty the trash directory. Returns 0 if it did nothing, 1 if it +# did something, -1 if it failed to remove all the files. # sub RmTreeTrashEmpty { @@ -435,13 +485,15 @@ sub RmTreeTrashEmpty $cwd = $1 if ( $cwd =~ /(.*)/ ); return if ( !-d $trashDir ); - my $d = DirHandle->new($trashDir) - or carp "Can't read $trashDir: $!"; + my $d = DirHandle->new($trashDir) or carp "Can't read $trashDir: $!"; @files = $d->read; $d->close; @files = grep $_!~/^\.{1,2}$/, @files; return 0 if ( !@files ); $bpc->RmTreeQuiet($trashDir, \@files); + foreach my $f ( @files ) { + return -1 if ( -e $f ); + } chdir($cwd) if ( $cwd ); return 1; } @@ -459,7 +511,7 @@ sub ServerConnect # # First try the unix-domain socket # - my $sockFile = "$bpc->{TopDir}/log/BackupPC.sock"; + my $sockFile = "$bpc->{LogDir}/BackupPC.sock"; socket(*FH, PF_UNIX, SOCK_STREAM, 0) || return "unix socket: $!"; if ( !connect(*FH, sockaddr_un($sockFile)) ) { my $err = "unix connect: $!"; @@ -578,6 +630,7 @@ sub File2MD5 $name = $1 if ( $name =~ /(.*)/ ); return ("", 0) if ( $fileSize == 0 ); return ("", -1) if ( !open(N, $name) ); + binmode(N); $md5->reset(); $md5->add($fileSize); if ( $fileSize > 262144 ) { @@ -671,7 +724,8 @@ sub MakeFileLink return -2 if ( !defined($rawFile = $bpc->MD52Path($d, $compress)) ); $rawFile .= "_$i" if ( $i >= 0 ); if ( -f $rawFile ) { - if ( !compare($name, $rawFile) ) { + if ( (stat(_))[3] < $bpc->{Conf}{HardLinkMax} + && !compare($name, $rawFile) ) { unlink($name); return -3 if ( !link($rawFile, $name) ); return 1; @@ -691,7 +745,16 @@ sub MakeFileLink sub CheckHostAlive { my($bpc, $host) = @_; - my($s, $pingCmd); + my($s, $pingCmd, $ret); + + # + # Return success if the ping cmd is undefined or empty. + # + if ( $bpc->{Conf}{PingCmd} eq "" ) { + print(STDERR "CheckHostAlive: return ok because \$Conf{PingCmd}" + . " is empty\n") if ( $bpc->{verbose} ); + return 0; + } my $args = { pingPath => $bpc->{Conf}{PingPath}, @@ -703,16 +766,34 @@ sub CheckHostAlive # Do a first ping in case the PC needs to wakeup # $s = $bpc->cmdSystemOrEval($pingCmd, undef, $args); - return -1 if ( $? ); + if ( $? ) { + print(STDERR "CheckHostAlive: first ping failed ($?, $!)\n") + if ( $bpc->{verbose} ); + return -1; + } # # Do a second ping and get the round-trip time in msec # $s = $bpc->cmdSystemOrEval($pingCmd, undef, $args); - return -1 if ( $? ); - return $1 if ( $s =~ /time=([\d\.]+)\s*ms/i ); - return $1/1000 if ( $s =~ /time=([\d\.]+)\s*usec/i ); - return 0; + if ( $? ) { + print(STDERR "CheckHostAlive: second ping failed ($?, $!)\n") + if ( $bpc->{verbose} ); + return -1; + } + if ( $s =~ /rtt\s*min\/avg\/max\/mdev\s*=\s*[\d.]+\/([\d.]+)\/[\d.]+\/[\d.]+\s*(ms|usec)/i ) { + $ret = $1; + $ret /= 1000 if ( lc($2) eq "usec" ); + } elsif ( $s =~ /time=([\d.]+)\s*(ms|usec)/i ) { + $ret = $1; + $ret /= 1000 if ( lc($2) eq "usec" ); + } else { + print(STDERR "CheckHostAlive: can't extract round-trip time" + . " (not fatal)\n") if ( $bpc->{verbose} ); + $ret = 0; + } + print(STDERR "CheckHostAlive: returning $ret\n") if ( $bpc->{verbose} ); + return $ret; } sub CheckFileSystemUsage @@ -721,6 +802,7 @@ sub CheckFileSystemUsage my($topDir) = $bpc->{TopDir}; my($s, $dfCmd); + return 0 if ( $bpc->{Conf}{DfCmd} eq "" ); my $args = { dfPath => $bpc->{Conf}{DfPath}, topDir => $bpc->{TopDir}, @@ -741,6 +823,15 @@ sub NetBiosInfoGet my($netBiosHostName, $netBiosUserName); my($s, $nmbCmd); + # + # Skip NetBios check if NmbLookupCmd is emtpy + # + if ( $bpc->{Conf}{NmbLookupCmd} eq "" ) { + print(STDERR "NetBiosInfoGet: return $host because \$Conf{NmbLookupCmd}" + . " is empty\n") if ( $bpc->{verbose} ); + return ($host, undef); + } + my $args = { nmbLookupPath => $bpc->{Conf}{NmbLookupPath}, host => $host, @@ -751,29 +842,66 @@ sub NetBiosInfoGet $netBiosHostName ||= $1 if ( $2 eq "00" ); # host is first 00 $netBiosUserName = $1 if ( $2 eq "03" ); # user is last 03 } - return if ( !defined($netBiosHostName) ); - return (lc($netBiosHostName), lc($netBiosUserName)); + if ( !defined($netBiosHostName) ) { + print(STDERR "NetBiosInfoGet: failed: can't parse return string\n") + if ( $bpc->{verbose} ); + return; + } + $netBiosHostName = lc($netBiosHostName); + $netBiosUserName = lc($netBiosUserName); + print(STDERR "NetBiosInfoGet: success, returning host $netBiosHostName," + . " user $netBiosUserName\n") if ( $bpc->{verbose} ); + return ($netBiosHostName, $netBiosUserName); } # # Given a NetBios name lookup the IP address via NetBios. +# In the case of a host returning multiple interfaces we +# return the first IP address that matches the subnet mask. +# If none match the subnet mask (or nmblookup doesn't print +# the subnet mask) then just the first IP address is returned. # sub NetBiosHostIPFind { my($bpc, $host) = @_; my($netBiosHostName, $netBiosUserName); - my($s, $nmbCmd); + my($s, $nmbCmd, $subnet, $ipAddr, $firstIpAddr); + + # + # Skip NetBios lookup if NmbLookupFindHostCmd is emtpy + # + if ( $bpc->{Conf}{NmbLookupFindHostCmd} eq "" ) { + print(STDERR "NetBiosHostIPFind: return $host because" + . " \$Conf{NmbLookupFindHostCmd} is empty\n") + if ( $bpc->{verbose} ); + return $host; + } my $args = { nmbLookupPath => $bpc->{Conf}{NmbLookupPath}, host => $host, }; $nmbCmd = $bpc->cmdVarSubstitute($bpc->{Conf}{NmbLookupFindHostCmd}, $args); - my $resp = $bpc->cmdSystemOrEval($nmbCmd, undef, $args); - if ( $resp =~ /^\s*(\d+\.\d+\.\d+\.\d+)\s+\Q$host/m ) { - return $1; + foreach my $resp ( split(/[\n\r]+/, $bpc->cmdSystemOrEval($nmbCmd, undef, + $args) ) ) { + if ( $resp =~ /querying\s+\Q$host\E\s+on\s+(\d+\.\d+\.\d+\.\d+)/i ) { + $subnet = $1; + $subnet = $1 if ( $subnet =~ /^(.*?)(\.255)+$/ ); + } elsif ( $resp =~ /^\s*(\d+\.\d+\.\d+\.\d+)\s+\Q$host/ ) { + my $ip = $1; + $firstIpAddr = $ip if ( !defined($firstIpAddr) ); + $ipAddr = $ip if ( !defined($ipAddr) && $ip =~ /^\Q$subnet/ ); + } + } + $ipAddr = $firstIpAddr if ( !defined($ipAddr) ); + if ( defined($ipAddr) ) { + print(STDERR "NetBiosHostIPFind: found IP address $ipAddr for" + . " host $host\n") if ( $bpc->{verbose} ); + return $ipAddr; } else { - return; + print(STDERR "NetBiosHostIPFind: couldn't find IP address for" + . " host $host\n") if ( $bpc->{verbose} ); + return; } } @@ -902,7 +1030,7 @@ sub cmdVarSubstitute $arg =~ s{\$(\w+)(\+?)}{ exists($vars->{$1}) && ref($vars->{$1}) ne "ARRAY" ? ($2 eq "+" ? $bpc->shellEscape($vars->{$1}) : $vars->{$1}) - : "\$$1" + : "\$$1$2" }eg; # # Now replicate any array arguments; this just works for just one @@ -936,10 +1064,24 @@ sub cmdExecOrEval if ( (ref($cmd) eq "ARRAY" ? $cmd->[0] : $cmd) =~ /^\&/ ) { $cmd = join(" ", $cmd) if ( ref($cmd) eq "ARRAY" ); - eval($cmd) + print(STDERR "cmdExecOrEval: about to eval perl code $cmd\n") + if ( $bpc->{verbose} ); + eval($cmd); + print(STDERR "Perl code fragment for exec shouldn't return!!\n"); + exit(1); } else { $cmd = [split(/\s+/, $cmd)] if ( ref($cmd) ne "ARRAY" ); - exec(@$cmd); + print(STDERR "cmdExecOrEval: about to exec ", + $bpc->execCmd2ShellCmd(@$cmd), "\n") + if ( $bpc->{verbose} ); + alarm(0); + $cmd = [map { m/(.*)/ } @$cmd]; # untaint + # + # force list-form of exec(), ie: no shell even for 1 arg + # + exec { $cmd->[0] } @$cmd; + print(STDERR "Exec failed for @$cmd\n"); + exit(1); } } @@ -955,21 +1097,28 @@ sub cmdExecOrEval # # Also, $? should be set when the CHILD pipe is closed. # -sub cmdSystemOrEval +sub cmdSystemOrEvalLong { - my($bpc, $cmd, $stdoutCB, @args) = @_; - my($pid, $out); + my($bpc, $cmd, $stdoutCB, $ignoreStderr, $pidHandlerCB, @args) = @_; + my($pid, $out, $allOut); local(*CHILD); if ( (ref($cmd) eq "ARRAY" ? $cmd->[0] : $cmd) =~ /^\&/ ) { $cmd = join(" ", $cmd) if ( ref($cmd) eq "ARRAY" ); - my $out = eval($cmd); + print(STDERR "cmdSystemOrEval: about to eval perl code $cmd\n") + if ( $bpc->{verbose} ); + $out = eval($cmd); $$stdoutCB .= $out if ( ref($stdoutCB) eq 'SCALAR' ); &$stdoutCB($out) if ( ref($stdoutCB) eq 'CODE' ); + print(STDERR "cmdSystemOrEval: finished: got output $out\n") + if ( $bpc->{verbose} ); return $out if ( !defined($stdoutCB) ); return; } else { $cmd = [split(/\s+/, $cmd)] if ( ref($cmd) ne "ARRAY" ); + print(STDERR "cmdSystemOrEval: about to system ", + $bpc->execCmd2ShellCmd(@$cmd), "\n") + if ( $bpc->{verbose} ); if ( !defined($pid = open(CHILD, "-|")) ) { my $err = "Can't fork to run @$cmd\n"; $? = 1; @@ -978,14 +1127,32 @@ sub cmdSystemOrEval return $err if ( !defined($stdoutCB) ); return; } + binmode(CHILD); if ( !$pid ) { # # This is the child # close(STDERR); - open(STDERR, ">&STDOUT"); - exec(@$cmd); + if ( $ignoreStderr ) { + open(STDERR, ">", "/dev/null"); + } else { + open(STDERR, ">&STDOUT"); + } + alarm(0); + $cmd = [map { m/(.*)/ } @$cmd]; # untaint + # + # force list-form of exec(), ie: no shell even for 1 arg + # + exec { $cmd->[0] } @$cmd; + print(STDERR "Exec of @$cmd failed\n"); + exit(1); } + + # + # Notify caller of child's pid + # + &$pidHandlerCB($pid) if ( ref($pidHandlerCB) eq "CODE" ); + # # The parent gathers the output from the child # @@ -993,11 +1160,58 @@ sub cmdSystemOrEval $$stdoutCB .= $_ if ( ref($stdoutCB) eq 'SCALAR' ); &$stdoutCB($_) if ( ref($stdoutCB) eq 'CODE' ); $out .= $_ if ( !defined($stdoutCB) ); + $allOut .= $_ if ( $bpc->{verbose} ); } $? = 0; close(CHILD); } + print(STDERR "cmdSystemOrEval: finished: got output $allOut\n") + if ( $bpc->{verbose} ); return $out; } +# +# The shorter version that sets $ignoreStderr = 0, ie: merges stdout +# and stderr together. +# +sub cmdSystemOrEval +{ + my($bpc, $cmd, $stdoutCB, @args) = @_; + + return $bpc->cmdSystemOrEvalLong($cmd, $stdoutCB, 0, undef, @args); +} + +# +# Promotes $conf->{BackupFilesOnly}, $conf->{BackupFilesExclude} +# to hashes and $conf->{$shareName} to an array. +# +sub backupFileConfFix +{ + my($bpc, $conf, $shareName) = @_; + + $conf->{$shareName} = [ $conf->{$shareName} ] + if ( ref($conf->{$shareName}) ne "ARRAY" ); + foreach my $param qw(BackupFilesOnly BackupFilesExclude) { + next if ( !defined($conf->{$param}) ); + if ( ref($conf->{$param}) eq "HASH" ) { + # + # A "*" entry means wildcard - it is the default for + # all shares. Replicate the "*" entry for all shares, + # but still allow override of specific entries. + # + next if ( !defined($conf->{$param}{"*"}) ); + $conf->{$param} = { + map({ $_ => $conf->{$param}{"*"} } + @{$conf->{$shareName}}), + %{$conf->{$param}} + }; + } else { + $conf->{$param} = [ $conf->{$param} ] + if ( ref($conf->{$param}) ne "ARRAY" ); + $conf->{$param} = { map { $_ => $conf->{$param} } + @{$conf->{$shareName}} }; + } + } +} + 1;