#
#========================================================================
#
-# Version 1.5.0, released 2 Aug 2002.
+# Version 1.6.0_CVS, released 10 Dec 2002.
#
# See http://backuppc.sourceforge.net.
#
use strict;
-use vars qw(%Conf);
+use vars qw(%Conf %Lang);
use Fcntl qw/:flock/;
use Carp;
use DirHandle ();
sub new
{
my $class = shift;
- my($topDir) = @_;
- my $self = bless {
- TopDir => $topDir || '__TOPDIR__',
- BinDir => '__INSTALLDIR__/bin',
- Version => '1.5.0',
+ my($topDir, $installDir) = @_;
+
+ my $bpc = bless {
+ TopDir => $topDir || '/data/BackupPC',
+ BinDir => $installDir || '/usr/local/BackupPC',
+ LibDir => $installDir || '/usr/local/BackupPC',
+ Version => '1.6.0_CVS',
BackupFields => [qw(
num type startTime endTime
nFiles size nFilesExist sizeExist nFilesNew sizeNew
xferErrs xferBadFile xferBadShare tarErrs
compress sizeExistComp sizeNewComp
- noFill fillFromNum mangle
+ noFill fillFromNum mangle xferMethod level
)],
RestoreFields => [qw(
num startTime endTime result errorMsg nFiles size
tarCreateErrs xferErrs
)],
}, $class;
+ $bpc->{BinDir} .= "/bin";
+ $bpc->{LibDir} .= "/lib";
#
# Clean up %ENV and setup other variables.
#
delete @ENV{qw(IFS CDPATH ENV BASH_ENV)};
- $self->{PoolDir} = "$self->{TopDir}/pool";
- $self->{CPoolDir} = "$self->{TopDir}/cpool";
- if ( defined(my $error = $self->ConfigRead()) ) {
+ $bpc->{PoolDir} = "$bpc->{TopDir}/pool";
+ $bpc->{CPoolDir} = "$bpc->{TopDir}/cpool";
+ if ( defined(my $error = $bpc->ConfigRead()) ) {
print(STDERR $error, "\n");
return;
}
- return $self;
+ return $bpc;
}
sub TopDir
{
- my($self) = @_;
- return $self->{TopDir};
+ my($bpc) = @_;
+ return $bpc->{TopDir};
}
sub BinDir
{
- my($self) = @_;
- return $self->{BinDir};
+ my($bpc) = @_;
+ return $bpc->{BinDir};
}
sub Version
{
- my($self) = @_;
- return $self->{Version};
+ my($bpc) = @_;
+ return $bpc->{Version};
}
sub Conf
{
- my($self) = @_;
- return %{$self->{Conf}};
+ my($bpc) = @_;
+ return %{$bpc->{Conf}};
+}
+
+sub Lang
+{
+ my($bpc) = @_;
+ return $bpc->{Lang};
}
sub adminJob
sub timeStamp
{
- my($self, $t, $noPad) = @_;
+ my($bpc, $t, $noPad) = @_;
my($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)
= localtime($t || time);
$year += 1900;
#
sub timeStampISO
{
- my($self, $t, $noPad) = @_;
+ my($bpc, $t, $noPad) = @_;
my($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)
= localtime($t || time);
$year += 1900;
sub BackupInfoRead
{
- my($self, $host) = @_;
+ my($bpc, $host) = @_;
local(*BK_INFO, *LOCK);
my(@Backups);
- flock(LOCK, LOCK_EX) if open(LOCK, "$self->{TopDir}/pc/$host/LOCK");
- if ( open(BK_INFO, "$self->{TopDir}/pc/$host/backups") ) {
+ flock(LOCK, LOCK_EX) if open(LOCK, "$bpc->{TopDir}/pc/$host/LOCK");
+ if ( open(BK_INFO, "$bpc->{TopDir}/pc/$host/backups") ) {
while ( <BK_INFO> ) {
s/[\n\r]+//;
next if ( !/^(\d+\t(incr|full)[\d\t]*$)/ );
$_ = $1;
- @{$Backups[@Backups]}{@{$self->{BackupFields}}} = split(/\t/);
+ @{$Backups[@Backups]}{@{$bpc->{BackupFields}}} = split(/\t/);
}
close(BK_INFO);
}
sub BackupInfoWrite
{
- my($self, $host, @Backups) = @_;
+ my($bpc, $host, @Backups) = @_;
local(*BK_INFO, *LOCK);
my($i);
- flock(LOCK, LOCK_EX) if open(LOCK, "$self->{TopDir}/pc/$host/LOCK");
- unlink("$self->{TopDir}/pc/$host/backups.old")
- if ( -f "$self->{TopDir}/pc/$host/backups.old" );
- rename("$self->{TopDir}/pc/$host/backups",
- "$self->{TopDir}/pc/$host/backups.old")
- if ( -f "$self->{TopDir}/pc/$host/backups" );
- if ( open(BK_INFO, ">$self->{TopDir}/pc/$host/backups") ) {
+ 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{@{$self->{BackupFields}}}));
+ printf(BK_INFO "%s\n", join("\t", @b{@{$bpc->{BackupFields}}}));
}
close(BK_INFO);
}
sub RestoreInfoRead
{
- my($self, $host) = @_;
+ my($bpc, $host) = @_;
local(*RESTORE_INFO, *LOCK);
my(@Restores);
- flock(LOCK, LOCK_EX) if open(LOCK, "$self->{TopDir}/pc/$host/LOCK");
- if ( open(RESTORE_INFO, "$self->{TopDir}/pc/$host/restores") ) {
+ flock(LOCK, LOCK_EX) if open(LOCK, "$bpc->{TopDir}/pc/$host/LOCK");
+ if ( open(RESTORE_INFO, "$bpc->{TopDir}/pc/$host/restores") ) {
while ( <RESTORE_INFO> ) {
s/[\n\r]+//;
next if ( !/^(\d+.*)/ );
$_ = $1;
- @{$Restores[@Restores]}{@{$self->{RestoreFields}}} = split(/\t/);
+ @{$Restores[@Restores]}{@{$bpc->{RestoreFields}}} = split(/\t/);
}
close(RESTORE_INFO);
}
sub RestoreInfoWrite
{
- my($self, $host, @Restores) = @_;
+ my($bpc, $host, @Restores) = @_;
local(*RESTORE_INFO, *LOCK);
my($i);
- flock(LOCK, LOCK_EX) if open(LOCK, "$self->{TopDir}/pc/$host/LOCK");
- unlink("$self->{TopDir}/pc/$host/restores.old")
- if ( -f "$self->{TopDir}/pc/$host/restores.old" );
- rename("$self->{TopDir}/pc/$host/restores",
- "$self->{TopDir}/pc/$host/restores.old")
- if ( -f "$self->{TopDir}/pc/$host/restores" );
- if ( open(RESTORE_INFO, ">$self->{TopDir}/pc/$host/restores") ) {
+ 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{@{$self->{RestoreFields}}}));
+ join("\t", @b{@{$bpc->{RestoreFields}}}));
}
close(RESTORE_INFO);
}
sub ConfigRead
{
- my($self, $host) = @_;
+ my($bpc, $host) = @_;
my($ret, $mesg, $config, @configs);
- $self->{Conf} = ();
- push(@configs, "$self->{TopDir}/conf/config.pl");
- push(@configs, "$self->{TopDir}/pc/$host/config.pl")
- if ( defined($host) && -f "$self->{TopDir}/pc/$host/config.pl" );
+ $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 =~ s/[\n\r]+//;
return $mesg;
}
- %{$self->{Conf}} = ( %{$self->{Conf} || {}}, %Conf );
+ %{$bpc->{Conf}} = ( %{$bpc->{Conf} || {}}, %Conf );
+ }
+ return if ( !defined($bpc->{Conf}{Language}) );
+ if ( defined($bpc->{Conf}{PerlModuleLoad}) ) {
+ #
+ # Load any user-specified perl modules. This is for
+ # optional user-defined extensions.
+ #
+ $bpc->{Conf}{PerlModuleLoad} = [$bpc->{Conf}{PerlModuleLoad}]
+ if ( ref($bpc->{Conf}{PerlModuleLoad}) ne "ARRAY" );
+ foreach my $module ( @{$bpc->{Conf}{PerlModuleLoad}} ) {
+ eval("use $module;");
+ }
}
+ my $langFile = "$bpc->{LibDir}/BackupPC/Lang/$bpc->{Conf}{Language}.pm";
+ if ( !defined($ret = do $langFile) && ($! || $@) ) {
+ $mesg = "Couldn't open language file $langFile: $!" if ( $! );
+ $mesg = "Couldn't execute language file $langFile: $@" if ( $@ );
+ $mesg =~ s/[\n\r]+//;
+ return $mesg;
+ }
+ $bpc->{Lang} = \%Lang;
return;
}
#
sub ConfigMTime
{
- my($self) = @_;
- return (stat("$self->{TopDir}/conf/config.pl"))[9];
+ my($bpc) = @_;
+ return (stat("$bpc->{TopDir}/conf/config.pl"))[9];
}
#
-# Returns information from the host file in $self->{TopDir}/conf/hosts.
+# Returns information from the host file in $bpc->{TopDir}/conf/hosts.
# With no argument a ref to a hash of hosts is returned. Each
# hash contains fields as specified in the hosts file. With an
# argument a ref to a single hash is returned with information
#
sub HostInfoRead
{
- my($self, $host) = @_;
+ my($bpc, $host) = @_;
my(%hosts, @hdr, @fld);
local(*HOST_INFO);
- if ( !open(HOST_INFO, "$self->{TopDir}/conf/hosts") ) {
- print(STDERR $self->timeStamp,
- "Can't open $self->{TopDir}/conf/hosts\n");
- return;
+ if ( !open(HOST_INFO, "$bpc->{TopDir}/conf/hosts") ) {
+ print(STDERR $bpc->timeStamp,
+ "Can't open $bpc->{TopDir}/conf/hosts\n");
+ return {};
}
while ( <HOST_INFO> ) {
s/[\n\r]+//;
s/#.*//;
+ s/\s+$//;
next if ( /^\s*$/ || !/^([\w\.-]+\s+.*)/ );
@fld = split(/\s+/, $1);
if ( @hdr ) {
if ( defined($host) ) {
next if ( lc($fld[0]) ne $host );
@{$hosts{lc($fld[0])}}{@hdr} = @fld;
+ close(HOST_INFO);
return \%hosts;
} else {
@{$hosts{lc($fld[0])}}{@hdr} = @fld;
#
sub HostsMTime
{
- my($self) = @_;
- return (stat("$self->{TopDir}/conf/hosts"))[9];
+ my($bpc) = @_;
+ return (stat("$bpc->{TopDir}/conf/hosts"))[9];
}
#
#
sub RmTreeQuiet
{
- my($self, $pwd, $roots) = @_;
+ my($bpc, $pwd, $roots) = @_;
my(@files, $root);
if ( defined($roots) && length($roots) ) {
@files = $d->read;
$d->close;
@files = grep $_!~/^\.{1,2}$/, @files;
- $self->RmTreeQuiet("$pwd/$root", \@files);
+ $bpc->RmTreeQuiet("$pwd/$root", \@files);
chdir($pwd);
rmdir($root) || rmdir($root);
} else {
#
sub RmTreeDefer
{
- my($self, $trashDir, $file) = @_;
+ my($bpc, $trashDir, $file) = @_;
my($i, $f);
return if ( !-e $file );
my($d) = $1;
my($f) = $2;
my($cwd) = Cwd::fastcwd();
- $self->RmTreeQuiet($d, $f);
+ $cwd = $1 if ( $cwd =~ /(.*)/ );
+ $bpc->RmTreeQuiet($d, $f);
chdir($cwd) if ( $cwd );
}
}
#
sub RmTreeTrashEmpty
{
- my($self, $trashDir) = @_;
+ my($bpc, $trashDir) = @_;
my(@files);
my($cwd) = Cwd::fastcwd();
+ $cwd = $1 if ( $cwd =~ /(.*)/ );
return if ( !-d $trashDir );
my $d = DirHandle->new($trashDir)
or carp "Can't read $trashDir: $!";
$d->close;
@files = grep $_!~/^\.{1,2}$/, @files;
return 0 if ( !@files );
- $self->RmTreeQuiet($trashDir, \@files);
+ $bpc->RmTreeQuiet($trashDir, \@files);
chdir($cwd) if ( $cwd );
return 1;
}
#
sub ServerConnect
{
- my($self, $host, $port, $justConnect) = @_;
+ my($bpc, $host, $port, $justConnect) = @_;
local(*FH);
- return if ( defined($self->{ServerFD}) );
+ return if ( defined($bpc->{ServerFD}) );
#
# First try the unix-domain socket
#
- my $sockFile = "$self->{TopDir}/log/BackupPC.sock";
+ my $sockFile = "$bpc->{TopDir}/log/BackupPC.sock";
socket(*FH, PF_UNIX, SOCK_STREAM, 0) || return "unix socket: $!";
if ( !connect(*FH, sockaddr_un($sockFile)) ) {
my $err = "unix connect: $!";
}
}
my($oldFH) = select(*FH); $| = 1; select($oldFH);
- $self->{ServerFD} = *FH;
+ $bpc->{ServerFD} = *FH;
return if ( $justConnect );
#
# Read the seed that we need for our MD5 message digest. See
# ServerMesg below.
#
- sysread($self->{ServerFD}, $self->{ServerSeed}, 1024);
- $self->{ServerMesgCnt} = 0;
+ sysread($bpc->{ServerFD}, $bpc->{ServerSeed}, 1024);
+ $bpc->{ServerMesgCnt} = 0;
return;
}
#
sub ServerOK
{
- my($self) = @_;
+ my($bpc) = @_;
- return 0 if ( !defined($self->{ServerFD}) );
- vec(my $FDread, fileno($self->{ServerFD}), 1) = 1;
+ return 0 if ( !defined($bpc->{ServerFD}) );
+ vec(my $FDread, fileno($bpc->{ServerFD}), 1) = 1;
my $ein = $FDread;
return 0 if ( select(my $rout = $FDread, undef, $ein, 0.0) < 0 );
- return 1 if ( !vec($rout, fileno($self->{ServerFD}), 1) );
+ return 1 if ( !vec($rout, fileno($bpc->{ServerFD}), 1) );
}
#
#
sub ServerDisconnect
{
- my($self) = @_;
- return if ( !defined($self->{ServerFD}) );
- close($self->{ServerFD});
- delete($self->{ServerFD});
+ my($bpc) = @_;
+ return if ( !defined($bpc->{ServerFD}) );
+ close($bpc->{ServerFD});
+ delete($bpc->{ServerFD});
}
#
#
sub ServerMesg
{
- my($self, $mesg) = @_;
- return if ( !defined(my $fh = $self->{ServerFD}) );
+ my($bpc, $mesg) = @_;
+ return if ( !defined(my $fh = $bpc->{ServerFD}) );
my $md5 = Digest::MD5->new;
- $md5->add($self->{ServerSeed} . $self->{ServerMesgCnt}
- . $self->{Conf}{ServerMesgSecret} . $mesg);
+ $md5->add($bpc->{ServerSeed} . $bpc->{ServerMesgCnt}
+ . $bpc->{Conf}{ServerMesgSecret} . $mesg);
print($fh $md5->b64digest . " $mesg\n");
- $self->{ServerMesgCnt}++;
+ $bpc->{ServerMesgCnt}++;
return <$fh>;
}
#
sub ChildInit
{
- my($self) = @_;
+ my($bpc) = @_;
close(STDERR);
open(STDERR, ">&STDOUT");
select(STDERR); $| = 1;
select(STDOUT); $| = 1;
- $ENV{PATH} = $self->{Conf}{MyPath};
+ $ENV{PATH} = $bpc->{Conf}{MyPath};
}
#
#
sub File2MD5
{
- my($self, $md5, $name) = @_;
+ my($bpc, $md5, $name) = @_;
my($data, $fileSize);
local(*N);
#
sub Buffer2MD5
{
- my($self, $md5, $fileSize, $dataRef) = @_;
+ my($bpc, $md5, $fileSize, $dataRef) = @_;
$md5->reset();
$md5->add($fileSize);
#
sub MD52Path
{
- my($self, $d, $compress, $poolDir) = @_;
+ my($bpc, $d, $compress, $poolDir) = @_;
return if ( $d !~ m{(.)(.)(.)(.*)} );
- $poolDir = ($compress ? $self->{CPoolDir} : $self->{PoolDir})
+ $poolDir = ($compress ? $bpc->{CPoolDir} : $bpc->{PoolDir})
if ( !defined($poolDir) );
return "$poolDir/$1/$2/$3/$1$2$3$4";
}
#
-# For each file, check if the file exists in $self->{TopDir}/pool.
+# For each file, check if the file exists in $bpc->{TopDir}/pool.
# If so, remove the file and make a hardlink to the file in
# the pool. Otherwise, if the newFile flag is set, make a
# hardlink in the pool to the new file.
#
sub MakeFileLink
{
- my($self, $name, $d, $newFile, $compress) = @_;
+ my($bpc, $name, $d, $newFile, $compress) = @_;
my($i, $rawFile);
return -1 if ( !-f $name );
for ( $i = -1 ; ; $i++ ) {
- return -2 if ( !defined($rawFile = $self->MD52Path($d, $compress)) );
+ return -2 if ( !defined($rawFile = $bpc->MD52Path($d, $compress)) );
$rawFile .= "_$i" if ( $i >= 0 );
if ( -f $rawFile ) {
if ( !compare($name, $rawFile) ) {
sub CheckHostAlive
{
- my($self, $host) = @_;
- my($s, $pingArgs);
+ my($bpc, $host) = @_;
+ my($s, $pingCmd);
- $pingArgs = $self->{Conf}{PingArgs};
- #
- # Merge variables into $pingArgs
- #
- my $vars = {
- host => $host,
+ my $args = {
+ pingPath => $bpc->{Conf}{PingPath},
+ host => $host,
};
- $pingArgs =~ s/\$(\w+)/defined($vars->{$1})
- ? $self->shellEscape($vars->{$1})
- : \$$1/eg;
+ $pingCmd = $bpc->cmdVarSubstitute($bpc->{Conf}{PingCmd}, $args);
+
#
# Do a first ping in case the PC needs to wakeup
#
- $s = `$self->{Conf}{PingPath} $pingArgs 2>&1`;
+ $s = $bpc->cmdSystemOrEval($pingCmd, undef, $args);
return -1 if ( $? );
+
#
# Do a second ping and get the round-trip time in msec
#
- $s = `$self->{Conf}{PingPath} $pingArgs 2>&1`;
+ $s = $bpc->cmdSystemOrEval($pingCmd, undef, $args);
return -1 if ( $? );
- return $1 if ( $s !~ /time=([\d\.]+)\s*ms/ );
- return $1/1000 if ( $s !~ /time=([\d\.]+)\s*usec/ );
+ return $1 if ( $s =~ /time=([\d\.]+)\s*ms/i );
+ return $1/1000 if ( $s =~ /time=([\d\.]+)\s*usec/i );
return 0;
}
sub CheckFileSystemUsage
{
- my($self) = @_;
- my($topDir) = $self->{TopDir};
- my($s);
-
- if ( $^O eq "solaris" ) {
- $s = `$self->{Conf}{DfPath} -k $topDir 2>&1`;
- return 0 if ( $? || $s !~ /(\d+)%/s );
- return $1;
- } elsif ( $^O eq "sunos" ) {
- $s = `$self->{Conf}{DfPath} $topDir 2>&1`;
- return 0 if ( $? || $s !~ /(\d+)%/s );
- return $1;
- } elsif ( $^O eq "linux" ) {
- $s = `$self->{Conf}{DfPath} $topDir 2>&1`;
- return 0 if ( $? || $s !~ /(\d+)%/s );
- return $1;
- } else {
- return 0;
- }
+ my($bpc) = @_;
+ my($topDir) = $bpc->{TopDir};
+ my($s, $dfCmd);
+
+ my $args = {
+ dfPath => $bpc->{Conf}{DfPath},
+ topDir => $bpc->{TopDir},
+ };
+ $dfCmd = $bpc->cmdVarSubstitute($bpc->{Conf}{DfCmd}, $args);
+ $s = $bpc->cmdSystemOrEval($dfCmd, undef, $args);
+ return 0 if ( $? || $s !~ /(\d+)%/s );
+ return $1;
}
sub NetBiosInfoGet
{
- my($self, $host) = @_;
+ my($bpc, $host) = @_;
my($netBiosHostName, $netBiosUserName);
+ my($s, $nmbCmd);
- foreach ( split(/[\n\r]+/, `$self->{Conf}{NmbLookupPath} -A $host 2>&1`) ) {
- next if ( !/([\w-]+)\s*<(\w{2})\> - .*<ACTIVE>/i );
+ my $args = {
+ nmbLookupPath => $bpc->{Conf}{NmbLookupPath},
+ host => $host,
+ };
+ $nmbCmd = $bpc->cmdVarSubstitute($bpc->{Conf}{NmbLookupCmd}, $args);
+ foreach ( split(/[\n\r]+/, $bpc->cmdSystemOrEval($nmbCmd, undef, $args)) ) {
+ next if ( !/^\s*([\w\s-]+?)\s*<(\w{2})\> - .*<ACTIVE>/i );
$netBiosHostName ||= $1 if ( $2 eq "00" ); # host is first 00
$netBiosUserName = $1 if ( $2 eq "03" ); # user is last 03
}
sub fileNameEltMangle
{
- my($self, $name) = @_;
+ my($bpc, $name) = @_;
+ return "" if ( $name eq "" );
$name =~ s{([%/\n\r])}{sprintf("%%%02x", ord($1))}eg;
return "f$name";
}
#
sub fileNameMangle
{
- my($self, $name) = @_;
+ my($bpc, $name) = @_;
- $name =~ s{/([^/]+)}{"/" . $self->fileNameEltMangle($1)}eg;
- $name =~ s{^([^/]+)}{$self->fileNameEltMangle($1)}eg;
+ $name =~ s{/([^/]+)}{"/" . $bpc->fileNameEltMangle($1)}eg;
+ $name =~ s{^([^/]+)}{$bpc->fileNameEltMangle($1)}eg;
return $name;
}
#
sub fileNameUnmangle
{
- my($self, $name) = @_;
+ my($bpc, $name) = @_;
$name =~ s{/f}{/}g;
$name =~ s{^f}{};
#
sub shellEscape
{
- my($self, $cmd) = @_;
+ my($bpc, $cmd) = @_;
$cmd =~ s/([][;&()<>{}|^\n\r\t *\$\\'"`?])/\\$1/g;
return $cmd;
}
+#
+# Do variable substitution prior to execution of a command.
+#
+sub cmdVarSubstitute
+{
+ my($bpc, $template, $vars) = @_;
+ my(@cmd);
+
+ #
+ # Return without any substitution if the first entry starts with "&",
+ # indicating this is perl code.
+ #
+ if ( (ref($template) eq "ARRAY" ? $template->[0] : $template) =~ /^\&/ ) {
+ return $template;
+ }
+ $template = [split(/\s+/, $template)] if ( ref($template) ne "ARRAY" );
+ #
+ # Merge variables into @tarClientCmd
+ #
+ foreach my $arg ( @$template ) {
+ #
+ # Replace scalar variables first
+ #
+ $arg =~ s{\$(\w+)(\+?)}{
+ defined($vars->{$1}) && ref($vars->{$1}) ne "ARRAY"
+ ? ($2 eq "+" ? $bpc->shellEscape($vars->{$1}) : $vars->{$1})
+ : "\$$1"
+ }eg;
+ #
+ # Now replicate any array arguments; this just works for just one
+ # array var in each argument.
+ #
+ if ( $arg =~ m{(.*)\$(\w+)(\+?)(.*)} && ref($vars->{$2}) eq "ARRAY" ) {
+ my $pre = $1;
+ my $var = $2;
+ my $esc = $3;
+ my $post = $4;
+ foreach my $v ( @{$vars->{$var}} ) {
+ $v = $bpc->shellEscape($v) if ( $esc eq "+" );
+ push(@cmd, "$pre$v$post");
+ }
+ } else {
+ push(@cmd, $arg);
+ }
+ }
+ return \@cmd;
+}
+
+#
+# Exec or eval a command. $cmd is either a string on an array ref.
+#
+# @args are optional arguments for the eval() case; they are not used
+# for exec().
+#
+sub cmdExecOrEval
+{
+ my($bpc, $cmd, @args) = @_;
+
+ if ( (ref($cmd) eq "ARRAY" ? $cmd->[0] : $cmd) =~ /^\&/ ) {
+ $cmd = join(" ", $cmd) if ( ref($cmd) eq "ARRAY" );
+ eval($cmd)
+ } else {
+ $cmd = [split(/\s+/, $cmd)] if ( ref($cmd) ne "ARRAY" );
+ exec(@$cmd);
+ }
+}
+
+#
+# System or eval a command. $cmd is either a string on an array ref.
+# $stdoutCB is a callback for output generated by the command. If it
+# is undef then output is returned. If it is a code ref then the function
+# is called with each piece of output as an argument. If it is a scalar
+# ref the output is appended to this variable.
+#
+# @args are optional arguments for the eval() case; they are not used
+# for system().
+#
+# Also, $? should be set when the CHILD pipe is closed.
+#
+sub cmdSystemOrEval
+{
+ my($bpc, $cmd, $stdoutCB, @args) = @_;
+ my($pid, $out);
+ local(*CHILD);
+
+ if ( (ref($cmd) eq "ARRAY" ? $cmd->[0] : $cmd) =~ /^\&/ ) {
+ $cmd = join(" ", $cmd) if ( ref($cmd) eq "ARRAY" );
+ my $out = eval($cmd);
+ $$stdoutCB .= $out if ( ref($stdoutCB) eq 'SCALAR' );
+ &$stdoutCB($out) if ( ref($stdoutCB) eq 'CODE' );
+ return $out if ( !defined($stdoutCB) );
+ return;
+ } else {
+ $cmd = [split(/\s+/, $cmd)] if ( ref($cmd) ne "ARRAY" );
+ if ( !defined($pid = open(CHILD, "-|")) ) {
+ my $err = "Can't fork to run @$cmd\n";
+ $? = 1;
+ $$stdoutCB .= $err if ( ref($stdoutCB) eq 'SCALAR' );
+ &$stdoutCB($err) if ( ref($stdoutCB) eq 'CODE' );
+ return $err if ( !defined($stdoutCB) );
+ return;
+ }
+ if ( !$pid ) {
+ #
+ # This is the child
+ #
+ close(STDERR);
+ open(STDERR, ">&STDOUT");
+ exec(@$cmd);
+ }
+ #
+ # The parent gathers the output from the child
+ #
+ while ( <CHILD> ) {
+ $$stdoutCB .= $_ if ( ref($stdoutCB) eq 'SCALAR' );
+ &$stdoutCB($_) if ( ref($stdoutCB) eq 'CODE' );
+ $out .= $_ if ( !defined($stdoutCB) );
+ }
+ $? = 0;
+ close(CHILD);
+ }
+ return $out;
+}
+
1;