2014-06-07 04:16:24 +03:00
|
|
|
####################################################################################################################################
|
|
|
|
# REMOTE MODULE
|
|
|
|
####################################################################################################################################
|
2014-06-07 23:25:20 +03:00
|
|
|
package BackRest::Remote;
|
2014-06-07 04:16:24 +03:00
|
|
|
|
|
|
|
use threads;
|
|
|
|
use strict;
|
|
|
|
use warnings;
|
|
|
|
use Carp;
|
|
|
|
|
|
|
|
use Moose;
|
2014-06-30 00:23:34 +03:00
|
|
|
use Thread::Queue;
|
2014-06-07 04:16:24 +03:00
|
|
|
use Net::OpenSSH;
|
|
|
|
use File::Basename;
|
2014-06-30 00:23:34 +03:00
|
|
|
use IO::Handle;
|
2014-06-15 02:50:54 +03:00
|
|
|
use POSIX ":sys_wait_h";
|
2014-06-15 22:56:45 +03:00
|
|
|
use IO::Compress::Gzip qw(gzip $GzipError);
|
|
|
|
use IO::Uncompress::Gunzip qw(gunzip $GunzipError);
|
2014-06-15 23:53:20 +03:00
|
|
|
|
2014-06-07 18:51:27 +03:00
|
|
|
use lib dirname($0) . "/../lib";
|
|
|
|
use BackRest::Exception;
|
2014-06-07 23:13:41 +03:00
|
|
|
use BackRest::Utility;
|
2014-06-07 04:16:24 +03:00
|
|
|
|
2014-06-15 02:50:54 +03:00
|
|
|
####################################################################################################################################
|
|
|
|
# Remote xfer default block size constant
|
|
|
|
####################################################################################################################################
|
|
|
|
use constant
|
|
|
|
{
|
2014-07-01 01:35:05 +03:00
|
|
|
DEFAULT_BLOCK_SIZE => 1048576
|
2014-06-15 02:50:54 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
####################################################################################################################################
|
|
|
|
# Module variables
|
|
|
|
####################################################################################################################################
|
2014-06-07 04:16:24 +03:00
|
|
|
# Protocol strings
|
2014-06-07 23:06:46 +03:00
|
|
|
has strGreeting => (is => 'ro', default => 'PG_BACKREST_REMOTE');
|
2014-06-07 04:16:24 +03:00
|
|
|
|
|
|
|
# Command strings
|
|
|
|
has strCommand => (is => 'bare');
|
|
|
|
|
|
|
|
# Module variables
|
|
|
|
has strHost => (is => 'bare'); # Host host
|
|
|
|
has strUser => (is => 'bare'); # User user
|
|
|
|
has oSSH => (is => 'bare'); # SSH object
|
|
|
|
|
|
|
|
# Process variables
|
2014-06-15 23:53:20 +03:00
|
|
|
has pId => (is => 'bare'); # Process Id
|
|
|
|
has hIn => (is => 'bare'); # Input stream
|
|
|
|
has hOut => (is => 'bare'); # Output stream
|
|
|
|
has hErr => (is => 'bare'); # Error stream
|
2014-06-07 04:16:24 +03:00
|
|
|
|
2014-06-30 00:23:34 +03:00
|
|
|
# Thread variables
|
|
|
|
has iThreadIdx => (is => 'bare'); # Thread index
|
|
|
|
has oThread => (is => 'bare'); # Thread object
|
|
|
|
has oThreadQueue => (is => 'bare'); # Thread queue object
|
|
|
|
has oThreadResult => (is => 'bare'); # Thread result object
|
|
|
|
|
2014-06-15 02:50:54 +03:00
|
|
|
# Block size
|
|
|
|
has iBlockSize => (is => 'bare', default => DEFAULT_BLOCK_SIZE); # Set block size to default
|
2014-06-13 04:56:20 +03:00
|
|
|
|
2014-06-07 04:16:24 +03:00
|
|
|
####################################################################################################################################
|
|
|
|
# CONSTRUCTOR
|
|
|
|
####################################################################################################################################
|
|
|
|
sub BUILD
|
|
|
|
{
|
|
|
|
my $self = shift;
|
2014-06-15 22:56:45 +03:00
|
|
|
|
2014-06-07 23:06:46 +03:00
|
|
|
$self->{strGreeting} .= " " . version_get();
|
2014-06-07 04:16:24 +03:00
|
|
|
|
|
|
|
if (defined($self->{strHost}))
|
|
|
|
{
|
|
|
|
# User must be defined
|
|
|
|
if (!defined($self->{strUser}))
|
|
|
|
{
|
|
|
|
confess &log(ASSERT, "strUser must be defined");
|
|
|
|
}
|
|
|
|
|
|
|
|
# User must be defined
|
|
|
|
if (!defined($self->{strCommand}))
|
|
|
|
{
|
|
|
|
confess &log(ASSERT, "strCommand must be defined");
|
|
|
|
}
|
|
|
|
|
|
|
|
# Set SSH Options
|
|
|
|
my $strOptionSSHRequestTTY = "RequestTTY=yes";
|
|
|
|
my $strOptionSSHCompression = "Compression=no";
|
|
|
|
|
2014-06-07 22:30:13 +03:00
|
|
|
&log(TRACE, "connecting to remote ssh host " . $self->{strHost});
|
2014-06-07 04:16:24 +03:00
|
|
|
|
|
|
|
# Make SSH connection
|
|
|
|
$self->{oSSH} = Net::OpenSSH->new($self->{strHost}, timeout => 300, user => $self->{strUser},
|
|
|
|
master_opts => [-o => $strOptionSSHCompression, -o => $strOptionSSHRequestTTY]);
|
|
|
|
|
|
|
|
$self->{oSSH}->error and confess &log(ERROR, "unable to connect to $self->{strHost}: " . $self->{oSSH}->error);
|
|
|
|
|
|
|
|
# Execute remote command
|
|
|
|
($self->{hIn}, $self->{hOut}, $self->{hErr}, $self->{pId}) = $self->{oSSH}->open3($self->{strCommand});
|
2014-06-07 22:30:13 +03:00
|
|
|
|
2014-06-07 22:01:29 +03:00
|
|
|
$self->greeting_read();
|
2014-06-30 00:23:34 +03:00
|
|
|
|
|
|
|
# print "BUILT THREAD " . (defined($self->{iThreadIdx}) ? $self->{iThreadIdx} : 'undef') . "\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
$self->{oThreadQueue} = Thread::Queue->new();
|
|
|
|
$self->{oThreadResult} = Thread::Queue->new();
|
|
|
|
$self->{oThread} = threads->create(\&binary_xfer_thread, $self);
|
|
|
|
}
|
|
|
|
|
|
|
|
####################################################################################################################################
|
|
|
|
# thread_kill
|
|
|
|
####################################################################################################################################
|
|
|
|
sub thread_kill
|
|
|
|
{
|
|
|
|
my $self = shift;
|
|
|
|
|
|
|
|
if (defined($self->{oThread}))
|
|
|
|
{
|
|
|
|
# if (defined($self->{strHost}))
|
|
|
|
# {
|
|
|
|
# print "DEM THREAD " . (defined($self->{iThreadIdx}) ? $self->{iThreadIdx} : 'undef') . "\n";
|
|
|
|
# }
|
|
|
|
|
|
|
|
$self->{oThreadQueue}->enqueue(undef);
|
|
|
|
$self->{oThread}->join();
|
|
|
|
$self->{oThread} = undef;
|
2014-06-07 04:16:24 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-30 00:23:34 +03:00
|
|
|
####################################################################################################################################
|
|
|
|
# DESTRUCTOR
|
|
|
|
####################################################################################################################################
|
|
|
|
sub DEMOLISH
|
|
|
|
{
|
|
|
|
my $self = shift;
|
|
|
|
|
|
|
|
$self->thread_kill();
|
|
|
|
}
|
|
|
|
|
2014-06-07 04:16:24 +03:00
|
|
|
####################################################################################################################################
|
|
|
|
# CLONE
|
|
|
|
####################################################################################################################################
|
|
|
|
sub clone
|
|
|
|
{
|
|
|
|
my $self = shift;
|
2014-06-30 00:23:34 +03:00
|
|
|
my $iThreadIdx = shift;
|
2014-06-07 04:16:24 +03:00
|
|
|
|
2014-06-22 03:08:49 +03:00
|
|
|
return BackRest::Remote->new
|
2014-06-07 04:16:24 +03:00
|
|
|
(
|
|
|
|
strCommand => $self->{strCommand},
|
2014-06-22 03:08:49 +03:00
|
|
|
strHost => $self->{strHost},
|
|
|
|
strUser => $self->{strUser},
|
2014-06-30 00:23:34 +03:00
|
|
|
iBlockSize => $self->{iBlockSize},
|
|
|
|
iThreadIdx => $iThreadIdx
|
2014-06-07 04:16:24 +03:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
####################################################################################################################################
|
|
|
|
# GREETING_READ
|
2014-06-15 23:53:20 +03:00
|
|
|
#
|
|
|
|
# Read the greeting and make sure it is as expected.
|
2014-06-07 04:16:24 +03:00
|
|
|
####################################################################################################################################
|
|
|
|
sub greeting_read
|
|
|
|
{
|
|
|
|
my $self = shift;
|
|
|
|
|
|
|
|
# Make sure that the remote is running the right version
|
2014-06-15 02:50:54 +03:00
|
|
|
if ($self->read_line($self->{hOut}) ne $self->{strGreeting})
|
2014-06-07 04:16:24 +03:00
|
|
|
{
|
|
|
|
confess &log(ERROR, "remote version mismatch");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
####################################################################################################################################
|
|
|
|
# GREETING_WRITE
|
2014-06-15 23:53:20 +03:00
|
|
|
#
|
|
|
|
# Send a greeting to the master process.
|
2014-06-07 04:16:24 +03:00
|
|
|
####################################################################################################################################
|
|
|
|
sub greeting_write
|
|
|
|
{
|
|
|
|
my $self = shift;
|
|
|
|
|
|
|
|
if (!syswrite(*STDOUT, "$self->{strGreeting}\n"))
|
|
|
|
{
|
|
|
|
confess "unable to write greeting";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-07 18:51:27 +03:00
|
|
|
####################################################################################################################################
|
|
|
|
# STRING_WRITE
|
2014-06-15 23:53:20 +03:00
|
|
|
#
|
|
|
|
# Write a string.
|
2014-06-07 18:51:27 +03:00
|
|
|
####################################################################################################################################
|
|
|
|
sub string_write
|
|
|
|
{
|
|
|
|
my $self = shift;
|
|
|
|
my $hOut = shift;
|
|
|
|
my $strBuffer = shift;
|
2014-06-07 22:30:13 +03:00
|
|
|
|
2014-06-07 18:51:27 +03:00
|
|
|
$strBuffer =~ s/\n/\n\./g;
|
2014-06-07 22:30:13 +03:00
|
|
|
|
2014-06-07 18:51:27 +03:00
|
|
|
if (!syswrite($hOut, "." . $strBuffer))
|
|
|
|
{
|
|
|
|
confess "unable to write string";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-15 02:50:54 +03:00
|
|
|
####################################################################################################################################
|
|
|
|
# PIPE_TO_STRING Function
|
|
|
|
#
|
|
|
|
# Copies data from a file handle into a string.
|
|
|
|
####################################################################################################################################
|
|
|
|
sub pipe_to_string
|
|
|
|
{
|
|
|
|
my $self = shift;
|
|
|
|
my $hOut = shift;
|
|
|
|
|
|
|
|
my $strBuffer;
|
|
|
|
my $hString = IO::String->new($strBuffer);
|
|
|
|
$self->binary_xfer($hOut, $hString);
|
|
|
|
|
|
|
|
return $strBuffer;
|
|
|
|
}
|
|
|
|
|
2014-06-07 18:51:27 +03:00
|
|
|
####################################################################################################################################
|
|
|
|
# ERROR_WRITE
|
2014-06-15 23:53:20 +03:00
|
|
|
#
|
|
|
|
# Write errors with error codes in protocol format, otherwise write to stderr and exit with error.
|
2014-06-07 18:51:27 +03:00
|
|
|
####################################################################################################################################
|
|
|
|
sub error_write
|
|
|
|
{
|
|
|
|
my $self = shift;
|
|
|
|
my $oMessage = shift;
|
2014-06-07 22:30:13 +03:00
|
|
|
|
2014-06-07 18:51:27 +03:00
|
|
|
my $iCode;
|
|
|
|
my $strMessage;
|
2014-06-07 22:30:13 +03:00
|
|
|
|
2014-06-07 18:51:27 +03:00
|
|
|
if (blessed($oMessage))
|
|
|
|
{
|
2014-06-07 22:30:13 +03:00
|
|
|
if ($oMessage->isa("BackRest::Exception"))
|
2014-06-07 18:51:27 +03:00
|
|
|
{
|
|
|
|
$iCode = $oMessage->code();
|
|
|
|
$strMessage = $oMessage->message();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-06-15 02:50:54 +03:00
|
|
|
syswrite(*STDERR, 'unknown error object: ' . $oMessage);
|
|
|
|
exit 1;
|
2014-06-07 18:51:27 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-06-15 02:50:54 +03:00
|
|
|
syswrite(*STDERR, $oMessage);
|
|
|
|
exit 1;
|
2014-06-07 18:51:27 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (defined($strMessage))
|
|
|
|
{
|
|
|
|
$self->string_write(*STDOUT, trim($strMessage));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!syswrite(*STDOUT, "\nERROR" . (defined($iCode) ? " $iCode" : "") . "\n"))
|
|
|
|
{
|
|
|
|
confess "unable to write error";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-15 02:50:54 +03:00
|
|
|
####################################################################################################################################
|
|
|
|
# READ_LINE
|
2014-06-15 23:53:20 +03:00
|
|
|
#
|
|
|
|
# Read a line.
|
2014-06-15 02:50:54 +03:00
|
|
|
####################################################################################################################################
|
|
|
|
sub read_line
|
|
|
|
{
|
|
|
|
my $self = shift;
|
|
|
|
my $hIn = shift;
|
|
|
|
my $bError = shift;
|
2014-06-15 22:56:45 +03:00
|
|
|
|
2014-06-15 02:50:54 +03:00
|
|
|
my $strLine;
|
|
|
|
my $strChar;
|
|
|
|
my $iByteIn;
|
2014-06-15 16:32:11 +03:00
|
|
|
|
2014-06-15 02:50:54 +03:00
|
|
|
while (1)
|
|
|
|
{
|
|
|
|
$iByteIn = sysread($hIn, $strChar, 1);
|
2014-06-15 22:56:45 +03:00
|
|
|
|
2014-06-15 02:50:54 +03:00
|
|
|
if (!defined($iByteIn) || $iByteIn != 1)
|
|
|
|
{
|
2014-06-15 16:32:11 +03:00
|
|
|
$self->wait_pid();
|
|
|
|
|
2014-06-15 02:50:54 +03:00
|
|
|
if (defined($bError) and !$bError)
|
|
|
|
{
|
|
|
|
return undef;
|
|
|
|
}
|
2014-06-15 22:56:45 +03:00
|
|
|
|
2014-06-15 02:50:54 +03:00
|
|
|
confess &log(ERROR, "unable to read 1 byte" . (defined($!) ? ": " . $! : ""));
|
|
|
|
}
|
2014-06-15 22:56:45 +03:00
|
|
|
|
2014-06-15 02:50:54 +03:00
|
|
|
if ($strChar eq "\n")
|
|
|
|
{
|
|
|
|
last;
|
|
|
|
}
|
|
|
|
|
|
|
|
$strLine .= $strChar;
|
|
|
|
}
|
2014-06-15 22:56:45 +03:00
|
|
|
|
2014-06-15 02:50:54 +03:00
|
|
|
return $strLine;
|
|
|
|
}
|
|
|
|
|
2014-06-21 17:05:49 +03:00
|
|
|
####################################################################################################################################
|
|
|
|
# WRITE_LINE
|
2014-06-22 03:08:49 +03:00
|
|
|
#
|
2014-06-21 17:05:49 +03:00
|
|
|
# Write a line data
|
|
|
|
####################################################################################################################################
|
|
|
|
sub write_line
|
|
|
|
{
|
|
|
|
my $self = shift;
|
|
|
|
my $hOut = shift;
|
|
|
|
my $strBuffer = shift;
|
|
|
|
|
|
|
|
$strBuffer = $strBuffer . "\n";
|
|
|
|
|
|
|
|
my $iLineOut = syswrite($hOut, $strBuffer, length($strBuffer));
|
|
|
|
|
|
|
|
if (!defined($iLineOut) || $iLineOut != length($strBuffer))
|
|
|
|
{
|
|
|
|
confess "unable to write " . length($strBuffer) . " byte(s)";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-15 16:32:11 +03:00
|
|
|
####################################################################################################################################
|
|
|
|
# WAIT_PID
|
2014-06-22 03:08:49 +03:00
|
|
|
#
|
2014-06-15 23:53:20 +03:00
|
|
|
# See if the remote process has terminated unexpectedly.
|
2014-06-15 16:32:11 +03:00
|
|
|
####################################################################################################################################
|
|
|
|
sub wait_pid
|
|
|
|
{
|
|
|
|
my $self = shift;
|
|
|
|
|
|
|
|
if (defined($self->{pId}) && waitpid($self->{pId}, WNOHANG) != 0)
|
|
|
|
{
|
|
|
|
my $strError = "no error on stderr";
|
2014-06-15 22:56:45 +03:00
|
|
|
|
2014-06-15 16:32:11 +03:00
|
|
|
if (!defined($self->{hErr}))
|
|
|
|
{
|
|
|
|
$strError = "no error captured because stderr is already closed";
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$strError = $self->pipe_to_string($self->{hErr});
|
|
|
|
}
|
|
|
|
|
|
|
|
$self->{pId} = undef;
|
|
|
|
$self->{hIn} = undef;
|
|
|
|
$self->{hOut} = undef;
|
|
|
|
$self->{hErr} = undef;
|
2014-06-15 22:56:45 +03:00
|
|
|
|
2014-06-15 16:32:11 +03:00
|
|
|
confess &log(ERROR, "remote process terminated: ${strError}");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-30 00:23:34 +03:00
|
|
|
####################################################################################################################################
|
|
|
|
# BINARY_XFER_THREAD
|
|
|
|
#
|
|
|
|
# De/Compresses data on a thread.
|
|
|
|
####################################################################################################################################
|
|
|
|
sub binary_xfer_thread
|
|
|
|
{
|
|
|
|
my $self = shift;
|
|
|
|
|
|
|
|
while (my $strMessage = $self->{oThreadQueue}->dequeue())
|
|
|
|
{
|
|
|
|
my @stryMessage = split(':', $strMessage);
|
|
|
|
my @strHandle = split(',', $stryMessage[1]);
|
|
|
|
|
|
|
|
my $hIn = IO::Handle->new_from_fd($strHandle[0], '<');
|
|
|
|
my $hOut = IO::Handle->new_from_fd($strHandle[1], '>');
|
|
|
|
|
|
|
|
$self->{oThreadResult}->enqueue('running');
|
|
|
|
|
|
|
|
if ($stryMessage[0] eq 'compress')
|
|
|
|
{
|
|
|
|
gzip($hIn => $hOut)
|
|
|
|
or confess &log(ERROR, "unable to compress: " . $GzipError);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
gunzip($hIn => $hOut)
|
|
|
|
or die confess &log(ERROR, "unable to uncompress: " . $GunzipError);
|
|
|
|
}
|
|
|
|
|
|
|
|
close($hOut);
|
2014-07-13 16:13:19 +03:00
|
|
|
|
|
|
|
$self->{oThreadResult}->enqueue('complete');
|
2014-06-30 00:23:34 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-13 04:56:20 +03:00
|
|
|
####################################################################################################################################
|
|
|
|
# BINARY_XFER
|
|
|
|
#
|
|
|
|
# Copies data from one file handle to another, optionally compressing or decompressing the data in stream.
|
|
|
|
####################################################################################################################################
|
|
|
|
sub binary_xfer
|
|
|
|
{
|
|
|
|
my $self = shift;
|
|
|
|
my $hIn = shift;
|
|
|
|
my $hOut = shift;
|
|
|
|
my $strRemote = shift;
|
2014-06-21 15:42:30 +03:00
|
|
|
my $bSourceCompressed = shift;
|
|
|
|
my $bDestinationCompress = shift;
|
2014-06-13 04:56:20 +03:00
|
|
|
|
2014-07-01 01:35:05 +03:00
|
|
|
# If no remote is defined then set to none
|
2014-06-21 15:42:30 +03:00
|
|
|
if (!defined($strRemote))
|
|
|
|
{
|
|
|
|
$strRemote = 'none';
|
|
|
|
}
|
2014-07-01 01:35:05 +03:00
|
|
|
# Only set compression defaults when remote is defined
|
2014-06-21 15:42:30 +03:00
|
|
|
else
|
|
|
|
{
|
|
|
|
$bSourceCompressed = defined($bSourceCompressed) ? $bSourceCompressed : false;
|
|
|
|
$bDestinationCompress = defined($bDestinationCompress) ? $bDestinationCompress : false;
|
|
|
|
}
|
2014-06-15 02:50:54 +03:00
|
|
|
|
2014-07-01 01:35:05 +03:00
|
|
|
# Working variables
|
2014-06-15 02:50:54 +03:00
|
|
|
my $iBlockSize = $self->{iBlockSize};
|
2014-06-13 04:56:20 +03:00
|
|
|
my $iBlockIn;
|
2014-06-28 18:47:21 +03:00
|
|
|
my $iBlockInTotal = $iBlockSize;
|
2014-06-15 02:50:54 +03:00
|
|
|
my $iBlockOut;
|
2014-06-28 18:47:21 +03:00
|
|
|
my $iBlockTotal = 0;
|
2014-06-15 02:50:54 +03:00
|
|
|
my $strBlockHeader;
|
2014-06-13 04:56:20 +03:00
|
|
|
my $strBlock;
|
2014-06-15 22:56:45 +03:00
|
|
|
my $oGzip;
|
|
|
|
my $hPipeIn;
|
|
|
|
my $hPipeOut;
|
|
|
|
my $pId;
|
2014-07-13 16:13:19 +03:00
|
|
|
my $bThreadRunning = false;
|
2014-06-15 22:56:45 +03:00
|
|
|
|
2014-06-21 15:42:30 +03:00
|
|
|
# Both the in and out streams must be defined
|
2014-06-15 16:32:11 +03:00
|
|
|
if (!defined($hIn) || !defined($hOut))
|
|
|
|
{
|
|
|
|
confess &log(ASSERT, "hIn or hOut is not defined");
|
|
|
|
}
|
2014-06-13 04:56:20 +03:00
|
|
|
|
2014-07-01 01:35:05 +03:00
|
|
|
# If this is output and the source is not already compressed
|
2014-06-21 15:42:30 +03:00
|
|
|
if ($strRemote eq "out" && !$bSourceCompressed)
|
2014-06-15 22:56:45 +03:00
|
|
|
{
|
2014-07-01 01:35:05 +03:00
|
|
|
# Increase the blocksize since we are compressing
|
|
|
|
$iBlockSize *= 4;
|
|
|
|
|
|
|
|
# Open the in/out pipes
|
2014-06-15 22:56:45 +03:00
|
|
|
pipe $hPipeOut, $hPipeIn;
|
2014-06-15 23:53:20 +03:00
|
|
|
|
2014-07-01 01:35:05 +03:00
|
|
|
# Queue the compression job with the thread
|
2014-06-30 00:23:34 +03:00
|
|
|
$self->{oThreadQueue}->enqueue("compress:" . fileno($hIn) . ',' . fileno($hPipeIn));
|
2014-06-15 22:56:45 +03:00
|
|
|
|
2014-07-01 01:35:05 +03:00
|
|
|
# Wait for the thread to acknowledge that it has duplicated the file handles
|
2014-06-30 00:23:34 +03:00
|
|
|
my $strMessage = $self->{oThreadResult}->dequeue();
|
2014-06-15 23:53:20 +03:00
|
|
|
|
2014-07-01 01:35:05 +03:00
|
|
|
# Close input pipe so that thread has the only copy, reset hIn to hPipeOut
|
2014-06-30 00:23:34 +03:00
|
|
|
if ($strMessage eq 'running')
|
|
|
|
{
|
2014-06-15 22:56:45 +03:00
|
|
|
close($hPipeIn);
|
2014-06-30 00:23:34 +03:00
|
|
|
$hIn = $hPipeOut;
|
|
|
|
}
|
2014-07-01 01:35:05 +03:00
|
|
|
# If any other message is returned then error
|
2014-06-30 00:23:34 +03:00
|
|
|
else
|
|
|
|
{
|
2014-07-13 16:13:19 +03:00
|
|
|
confess "unknown thread message while waiting for running: $strMessage";
|
2014-06-15 22:56:45 +03:00
|
|
|
}
|
2014-07-13 16:13:19 +03:00
|
|
|
|
|
|
|
$bThreadRunning = true;
|
2014-06-15 22:56:45 +03:00
|
|
|
}
|
2014-06-21 15:42:30 +03:00
|
|
|
# Spawn a child process to do decompression
|
|
|
|
elsif ($strRemote eq "in" && !$bDestinationCompress)
|
2014-06-15 22:56:45 +03:00
|
|
|
{
|
2014-07-01 01:35:05 +03:00
|
|
|
# Open the in/out pipes
|
2014-06-30 00:23:34 +03:00
|
|
|
pipe $hPipeOut, $hPipeIn;
|
2014-06-15 23:53:20 +03:00
|
|
|
|
2014-07-01 01:35:05 +03:00
|
|
|
# Queue the decompression job with the thread
|
2014-06-30 00:23:34 +03:00
|
|
|
$self->{oThreadQueue}->enqueue("decompress:" . fileno($hPipeOut) . ',' . fileno($hOut));
|
2014-06-15 22:56:45 +03:00
|
|
|
|
2014-07-01 01:35:05 +03:00
|
|
|
# Wait for the thread to acknowledge that it has duplicated the file handles
|
2014-06-30 00:23:34 +03:00
|
|
|
my $strMessage = $self->{oThreadResult}->dequeue();
|
2014-06-15 22:56:45 +03:00
|
|
|
|
2014-07-01 01:35:05 +03:00
|
|
|
# Close output pipe so that thread has the only copy, reset hOut to hPipeIn
|
2014-06-30 00:23:34 +03:00
|
|
|
if ($strMessage eq 'running')
|
|
|
|
{
|
2014-06-15 22:56:45 +03:00
|
|
|
close($hPipeOut);
|
2014-06-30 00:23:34 +03:00
|
|
|
$hOut = $hPipeIn;
|
|
|
|
}
|
2014-07-01 01:35:05 +03:00
|
|
|
# If any other message is returned then error
|
2014-06-30 00:23:34 +03:00
|
|
|
else
|
|
|
|
{
|
2014-07-13 16:13:19 +03:00
|
|
|
confess "unknown thread message while waiting for running: $strMessage";
|
2014-06-15 22:56:45 +03:00
|
|
|
}
|
2014-07-13 16:13:19 +03:00
|
|
|
|
|
|
|
$bThreadRunning = true;
|
2014-06-15 22:56:45 +03:00
|
|
|
}
|
|
|
|
|
2014-06-15 02:50:54 +03:00
|
|
|
while (1)
|
2014-06-13 04:56:20 +03:00
|
|
|
{
|
|
|
|
if ($strRemote eq 'in')
|
|
|
|
{
|
2014-06-28 18:47:21 +03:00
|
|
|
if ($iBlockInTotal == $iBlockSize)
|
2014-06-13 04:56:20 +03:00
|
|
|
{
|
2014-06-28 18:47:21 +03:00
|
|
|
$strBlockHeader = $self->read_line($hIn);
|
|
|
|
|
|
|
|
if ($strBlockHeader !~ /^block [0-9]+$/)
|
|
|
|
{
|
|
|
|
$self->wait_pid();
|
|
|
|
confess "unable to read block header ${strBlockHeader}";
|
|
|
|
}
|
2014-06-28 21:32:34 +03:00
|
|
|
|
2014-06-28 18:47:21 +03:00
|
|
|
$iBlockInTotal = 0;
|
|
|
|
$iBlockTotal += 1;
|
2014-06-13 04:56:20 +03:00
|
|
|
}
|
2014-06-15 02:50:54 +03:00
|
|
|
|
2014-06-15 23:53:20 +03:00
|
|
|
$iBlockSize = trim(substr($strBlockHeader, index($strBlockHeader, " ") + 1));
|
2014-06-15 22:56:45 +03:00
|
|
|
|
2014-06-15 02:50:54 +03:00
|
|
|
if ($iBlockSize != 0)
|
|
|
|
{
|
2014-06-28 18:47:21 +03:00
|
|
|
$iBlockIn = sysread($hIn, $strBlock, $iBlockSize - $iBlockInTotal);
|
2014-06-28 21:32:34 +03:00
|
|
|
|
2014-06-28 18:47:21 +03:00
|
|
|
if (!defined($iBlockIn))
|
2014-06-15 02:50:54 +03:00
|
|
|
{
|
2014-06-30 00:23:34 +03:00
|
|
|
my $strError = $!;
|
|
|
|
|
2014-06-28 18:47:21 +03:00
|
|
|
$self->wait_pid();
|
|
|
|
confess "unable to read block #${iBlockTotal}/${iBlockSize} bytes from remote" .
|
2014-06-30 00:23:34 +03:00
|
|
|
(defined($strError) ? ": ${strError}" : "");
|
2014-06-15 02:50:54 +03:00
|
|
|
}
|
2014-06-28 18:47:21 +03:00
|
|
|
|
|
|
|
$iBlockInTotal += $iBlockIn;
|
2014-06-15 02:50:54 +03:00
|
|
|
}
|
|
|
|
else
|
2014-06-13 04:56:20 +03:00
|
|
|
{
|
2014-06-15 02:50:54 +03:00
|
|
|
$iBlockIn = 0;
|
2014-06-13 04:56:20 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-06-15 22:56:45 +03:00
|
|
|
$iBlockIn = sysread($hIn, $strBlock, $iBlockSize);
|
2014-06-15 23:53:20 +03:00
|
|
|
|
2014-06-15 22:56:45 +03:00
|
|
|
if (!defined($iBlockIn))
|
2014-06-13 04:56:20 +03:00
|
|
|
{
|
2014-06-28 18:47:21 +03:00
|
|
|
$self->wait_pid();
|
2014-06-15 22:56:45 +03:00
|
|
|
confess &log(ERROR, "unable to read");
|
2014-06-13 04:56:20 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-15 02:50:54 +03:00
|
|
|
if ($strRemote eq 'out')
|
2014-06-13 04:56:20 +03:00
|
|
|
{
|
2014-06-15 02:50:54 +03:00
|
|
|
$strBlockHeader = "block ${iBlockIn}\n";
|
2014-06-15 22:56:45 +03:00
|
|
|
|
2014-06-15 02:50:54 +03:00
|
|
|
$iBlockOut = syswrite($hOut, $strBlockHeader);
|
2014-06-15 22:56:45 +03:00
|
|
|
|
2014-06-15 02:50:54 +03:00
|
|
|
if (!defined($iBlockOut) || $iBlockOut != length($strBlockHeader))
|
2014-06-13 04:56:20 +03:00
|
|
|
{
|
2014-06-28 18:47:21 +03:00
|
|
|
$self->wait_pid();
|
2014-06-15 02:50:54 +03:00
|
|
|
confess "unable to write block header";
|
2014-06-13 04:56:20 +03:00
|
|
|
}
|
2014-06-15 02:50:54 +03:00
|
|
|
}
|
2014-06-13 04:56:20 +03:00
|
|
|
|
2014-06-15 02:50:54 +03:00
|
|
|
if ($iBlockIn > 0)
|
|
|
|
{
|
|
|
|
$iBlockOut = syswrite($hOut, $strBlock, $iBlockIn);
|
2014-06-13 04:56:20 +03:00
|
|
|
|
|
|
|
if (!defined($iBlockOut) || $iBlockOut != $iBlockIn)
|
|
|
|
{
|
2014-06-28 18:47:21 +03:00
|
|
|
$self->wait_pid();
|
2014-06-13 04:56:20 +03:00
|
|
|
confess "unable to write ${iBlockIn} bytes" . (defined($!) ? ": " . $! : "");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-06-15 02:50:54 +03:00
|
|
|
last;
|
2014-06-13 04:56:20 +03:00
|
|
|
}
|
|
|
|
}
|
2014-06-15 22:56:45 +03:00
|
|
|
|
2014-07-13 16:13:19 +03:00
|
|
|
if ($bThreadRunning)
|
2014-06-15 22:56:45 +03:00
|
|
|
{
|
2014-07-13 16:13:19 +03:00
|
|
|
# Make sure the de/compress pipes are closed
|
|
|
|
if ($strRemote eq "out" && !$bSourceCompressed)
|
|
|
|
{
|
|
|
|
close($hPipeOut);
|
|
|
|
}
|
|
|
|
elsif ($strRemote eq "in" && !$bDestinationCompress)
|
|
|
|
{
|
|
|
|
close($hPipeIn);
|
|
|
|
}
|
|
|
|
|
|
|
|
# Wait for the thread to acknowledge that it has completed
|
|
|
|
my $strMessage = $self->{oThreadResult}->dequeue();
|
|
|
|
|
|
|
|
if ($strMessage eq 'complete')
|
|
|
|
{
|
|
|
|
}
|
|
|
|
# If any other message is returned then error
|
|
|
|
else
|
|
|
|
{
|
|
|
|
confess "unknown thread message while waiting for complete: $strMessage";
|
|
|
|
}
|
2014-06-15 22:56:45 +03:00
|
|
|
}
|
2014-06-13 04:56:20 +03:00
|
|
|
}
|
|
|
|
|
2014-06-07 18:51:27 +03:00
|
|
|
####################################################################################################################################
|
|
|
|
# OUTPUT_READ
|
2014-06-15 23:53:20 +03:00
|
|
|
#
|
|
|
|
# Read output from the remote process.
|
2014-06-07 18:51:27 +03:00
|
|
|
####################################################################################################################################
|
|
|
|
sub output_read
|
|
|
|
{
|
|
|
|
my $self = shift;
|
2014-06-15 02:50:54 +03:00
|
|
|
my $bOutputRequired = shift;
|
|
|
|
my $strErrorPrefix = shift;
|
2014-06-07 18:51:27 +03:00
|
|
|
|
|
|
|
my $strLine;
|
|
|
|
my $strOutput;
|
|
|
|
my $bError = false;
|
|
|
|
my $iErrorCode;
|
2014-06-15 02:50:54 +03:00
|
|
|
my $strError;
|
2014-06-07 18:51:27 +03:00
|
|
|
|
2014-06-15 23:53:20 +03:00
|
|
|
# Read output lines
|
2014-06-15 02:50:54 +03:00
|
|
|
while ($strLine = $self->read_line($self->{hOut}, false))
|
|
|
|
{
|
2014-06-07 18:51:27 +03:00
|
|
|
if ($strLine =~ /^ERROR.*/)
|
|
|
|
{
|
|
|
|
$bError = true;
|
2014-06-07 22:30:13 +03:00
|
|
|
|
2014-06-15 02:50:54 +03:00
|
|
|
$iErrorCode = (split(' ', $strLine))[1];
|
2014-06-07 22:30:13 +03:00
|
|
|
|
2014-06-07 18:51:27 +03:00
|
|
|
last;
|
|
|
|
}
|
|
|
|
|
2014-06-15 02:50:54 +03:00
|
|
|
if ($strLine =~ /^OK$/)
|
2014-06-07 18:51:27 +03:00
|
|
|
{
|
|
|
|
last;
|
|
|
|
}
|
|
|
|
|
2014-06-15 02:50:54 +03:00
|
|
|
$strOutput .= (defined($strOutput) ? "\n" : "") . substr($strLine, 1);
|
|
|
|
}
|
|
|
|
|
2014-06-15 23:53:20 +03:00
|
|
|
# Check if the process has exited abnormally
|
|
|
|
$self->wait_pid();
|
|
|
|
|
|
|
|
# Raise any errors
|
2014-06-15 02:50:54 +03:00
|
|
|
if ($bError)
|
|
|
|
{
|
|
|
|
confess &log(ERROR, (defined($strErrorPrefix) ? "${strErrorPrefix}" : "") .
|
|
|
|
(defined($strOutput) ? ": ${strOutput}" : ""), $iErrorCode);
|
|
|
|
}
|
2014-06-15 22:56:45 +03:00
|
|
|
|
2014-06-15 23:53:20 +03:00
|
|
|
# If output is required and there is no output, raise exception
|
2014-06-15 02:50:54 +03:00
|
|
|
if ($bOutputRequired && !defined($strOutput))
|
|
|
|
{
|
|
|
|
confess &log(ERROR, (defined($strErrorPrefix) ? "${strErrorPrefix}: " : "") . "output is not defined");
|
2014-06-07 18:51:27 +03:00
|
|
|
}
|
|
|
|
|
2014-06-15 23:53:20 +03:00
|
|
|
# Return output
|
2014-06-15 02:50:54 +03:00
|
|
|
return $strOutput;
|
2014-06-07 18:51:27 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
####################################################################################################################################
|
|
|
|
# OUTPUT_WRITE
|
2014-06-15 23:53:20 +03:00
|
|
|
#
|
|
|
|
# Write output for the master process.
|
2014-06-07 18:51:27 +03:00
|
|
|
####################################################################################################################################
|
|
|
|
sub output_write
|
|
|
|
{
|
|
|
|
my $self = shift;
|
|
|
|
my $strOutput = shift;
|
2014-06-07 22:30:13 +03:00
|
|
|
|
2014-06-15 02:50:54 +03:00
|
|
|
if (defined($strOutput))
|
|
|
|
{
|
|
|
|
$self->string_write(*STDOUT, "${strOutput}");
|
2014-06-07 18:51:27 +03:00
|
|
|
|
2014-06-15 02:50:54 +03:00
|
|
|
if (!syswrite(*STDOUT, "\n"))
|
|
|
|
{
|
|
|
|
confess "unable to write output";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!syswrite(*STDOUT, "OK\n"))
|
2014-06-07 18:51:27 +03:00
|
|
|
{
|
|
|
|
confess "unable to write output";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
####################################################################################################################################
|
2014-06-07 22:01:29 +03:00
|
|
|
# COMMAND_PARAM_STRING
|
2014-06-15 23:53:20 +03:00
|
|
|
#
|
|
|
|
# Output command parameters in the hash as a string (used for debugging).
|
2014-06-07 18:51:27 +03:00
|
|
|
####################################################################################################################################
|
2014-06-07 22:01:29 +03:00
|
|
|
sub command_param_string
|
2014-06-07 18:51:27 +03:00
|
|
|
{
|
|
|
|
my $self = shift;
|
2014-06-07 22:01:29 +03:00
|
|
|
my $oParamHashRef = shift;
|
2014-06-07 22:30:13 +03:00
|
|
|
|
2014-06-07 22:01:29 +03:00
|
|
|
my $strParamList;
|
2014-06-07 22:30:13 +03:00
|
|
|
|
2014-06-07 22:01:29 +03:00
|
|
|
foreach my $strParam (sort(keys $oParamHashRef))
|
2014-06-07 18:51:27 +03:00
|
|
|
{
|
2014-06-07 22:01:29 +03:00
|
|
|
$strParamList .= (defined($strParamList) ? "," : "") . "${strParam}=" .
|
|
|
|
(defined(${$oParamHashRef}{"${strParam}"}) ? ${$oParamHashRef}{"${strParam}"} : "[undef]");
|
2014-06-07 18:51:27 +03:00
|
|
|
}
|
2014-06-07 22:01:29 +03:00
|
|
|
|
|
|
|
return $strParamList;
|
|
|
|
}
|
|
|
|
|
|
|
|
####################################################################################################################################
|
|
|
|
# COMMAND_READ
|
2014-06-15 23:53:20 +03:00
|
|
|
#
|
|
|
|
# Read command sent by the master process.
|
2014-06-07 22:01:29 +03:00
|
|
|
####################################################################################################################################
|
|
|
|
sub command_read
|
|
|
|
{
|
|
|
|
my $self = shift;
|
|
|
|
my $oParamHashRef = shift;
|
|
|
|
|
|
|
|
my $strLine;
|
|
|
|
my $strCommand;
|
|
|
|
|
2014-06-15 02:50:54 +03:00
|
|
|
while ($strLine = $self->read_line(*STDIN))
|
2014-06-07 18:51:27 +03:00
|
|
|
{
|
2014-06-07 22:01:29 +03:00
|
|
|
if (!defined($strCommand))
|
|
|
|
{
|
|
|
|
if ($strLine =~ /:$/)
|
|
|
|
{
|
|
|
|
$strCommand = substr($strLine, 0, length($strLine) - 1);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$strCommand = $strLine;
|
|
|
|
last;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if ($strLine eq 'end')
|
|
|
|
{
|
|
|
|
last;
|
|
|
|
}
|
2014-06-07 22:30:13 +03:00
|
|
|
|
2014-06-07 22:01:29 +03:00
|
|
|
my $iPos = index($strLine, "=");
|
2014-06-07 22:30:13 +03:00
|
|
|
|
2014-06-07 22:01:29 +03:00
|
|
|
if ($iPos == -1)
|
|
|
|
{
|
|
|
|
confess "param \"${strLine}\" is missing = character";
|
|
|
|
}
|
2014-06-07 22:30:13 +03:00
|
|
|
|
2014-06-07 22:01:29 +03:00
|
|
|
my $strParam = substr($strLine, 0, $iPos);
|
|
|
|
my $strValue = substr($strLine, $iPos + 1);
|
2014-06-07 22:30:13 +03:00
|
|
|
|
2014-06-07 22:01:29 +03:00
|
|
|
${$oParamHashRef}{"${strParam}"} = ${strValue};
|
|
|
|
}
|
2014-06-07 18:51:27 +03:00
|
|
|
}
|
|
|
|
|
2014-06-07 22:01:29 +03:00
|
|
|
return $strCommand;
|
2014-06-07 18:51:27 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
####################################################################################################################################
|
|
|
|
# COMMAND_WRITE
|
2014-06-15 23:53:20 +03:00
|
|
|
#
|
|
|
|
# Send command to remote process.
|
2014-06-07 18:51:27 +03:00
|
|
|
####################################################################################################################################
|
|
|
|
sub command_write
|
|
|
|
{
|
|
|
|
my $self = shift;
|
|
|
|
my $strCommand = shift;
|
2014-06-07 22:01:29 +03:00
|
|
|
my $oParamRef = shift;
|
|
|
|
|
|
|
|
my $strOutput = $strCommand;
|
|
|
|
|
|
|
|
if (defined($oParamRef))
|
|
|
|
{
|
|
|
|
$strOutput = "${strCommand}:\n";
|
2014-06-07 22:30:13 +03:00
|
|
|
|
2014-06-07 22:01:29 +03:00
|
|
|
foreach my $strParam (sort(keys $oParamRef))
|
|
|
|
{
|
|
|
|
if ($strParam =~ /=/)
|
|
|
|
{
|
|
|
|
confess &log(ASSERT, "param \"${strParam}\" cannot contain = character");
|
|
|
|
}
|
|
|
|
|
|
|
|
my $strValue = ${$oParamRef}{"${strParam}"};
|
|
|
|
|
|
|
|
if ($strParam =~ /\n\$/)
|
|
|
|
{
|
|
|
|
confess &log(ASSERT, "param \"${strParam}\" value cannot end with LF");
|
|
|
|
}
|
2014-06-07 22:30:13 +03:00
|
|
|
|
2014-06-07 22:01:29 +03:00
|
|
|
if (defined(${strValue}))
|
|
|
|
{
|
|
|
|
$strOutput .= "${strParam}=${strValue}\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$strOutput .= "end";
|
|
|
|
}
|
2014-06-07 18:51:27 +03:00
|
|
|
|
2014-06-15 02:50:54 +03:00
|
|
|
&log(TRACE, "Remote->command_write:\n" . $strOutput);
|
2014-06-07 20:15:55 +03:00
|
|
|
|
2014-06-07 22:01:29 +03:00
|
|
|
if (!syswrite($self->{hIn}, "${strOutput}\n"))
|
2014-06-07 18:51:27 +03:00
|
|
|
{
|
|
|
|
confess "unable to write command";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-07 20:15:55 +03:00
|
|
|
####################################################################################################################################
|
|
|
|
# COMMAND_EXECUTE
|
2014-06-15 23:53:20 +03:00
|
|
|
#
|
|
|
|
# Send command to remote process and wait for output.
|
2014-06-07 20:15:55 +03:00
|
|
|
####################################################################################################################################
|
|
|
|
sub command_execute
|
|
|
|
{
|
|
|
|
my $self = shift;
|
|
|
|
my $strCommand = shift;
|
2014-06-13 04:56:20 +03:00
|
|
|
my $oParamRef = shift;
|
2014-06-15 02:50:54 +03:00
|
|
|
my $bOutputRequired = shift;
|
2014-06-07 20:15:55 +03:00
|
|
|
my $strErrorPrefix = shift;
|
|
|
|
|
2014-06-13 04:56:20 +03:00
|
|
|
$self->command_write($strCommand, $oParamRef);
|
2014-06-07 22:30:13 +03:00
|
|
|
|
2014-06-15 02:50:54 +03:00
|
|
|
return $self->output_read($bOutputRequired, $strErrorPrefix);
|
2014-06-07 20:15:55 +03:00
|
|
|
}
|
|
|
|
|
2014-06-07 04:16:24 +03:00
|
|
|
no Moose;
|
|
|
|
__PACKAGE__->meta->make_immutable;
|