1
0
mirror of https://github.com/pgbackrest/pgbackrest.git synced 2024-12-14 10:13:05 +02:00

Cleaner remote implementation.

This commit is contained in:
David Steele 2014-06-07 13:15:55 -04:00
parent 1bc80c50ed
commit e07a6b4e3f
3 changed files with 114 additions and 39 deletions

View File

@ -1129,33 +1129,27 @@ sub exists
# Set error prefix, remote, and path
my $bExists = true;
my $strErrorPrefix = "File->exists";
my $bRemote = $self->is_remote($strPathType);
my $strPathOp = $self->path_get($strPathType, $strPath);
&log(TRACE, "${strErrorPrefix}: " . ($bRemote ? "remote" : "local") . " ${strPathType}:${strPathOp}");
my $strErrorPrefix = "File->exists";
my $strTrace = "${strPathType}:${strPathOp}";
# Run remotely
if ($bRemote)
{
my $strCommand = "EXISTS:${strPathOp}";
$self->{oRemote}->command_write($strCommand);
my $strCommandOptions = "${strPathOp}";
$strTrace = "${strErrorPrefix}: remote ($strCommandOptions): " . $strTrace;
# Run it remotely
my ($strOutput, $bError, $iErrorCode) = $self->{oRemote}->output_read();
&log(TRACE, $strTrace);
# Capture any errors
if ($bError)
{
confess &log(ERROR, "${strErrorPrefix} remote (${strCommand}): " . $strOutput);
}
$bExists = $strOutput eq "Y";
$bExists = $self->{oRemote}->command_execute("EXISTS", $strCommandOptions, $strTrace) eq "Y";
}
# Run locally
else
{
&log(TRACE, "${strErrorPrefix}: ${strTrace}");
# Stat the file/path to determine if it exists
my $oStat = lstat($strPathOp);
@ -1165,7 +1159,14 @@ sub exists
# If the error is not entry missing, then throw error
if (!$!{ENOENT})
{
confess &log(ERROR, $!, COMMAND_ERR_FILE_READ);
if ($strPathType eq PATH_ABSOLUTE)
{
confess &log(ERROR, $!, COMMAND_ERR_FILE_READ);
}
else
{
confess &log(ERROR, "${strErrorPrefix}: ${strTrace}: " . $!, COMMAND_ERR_FILE_READ);
}
}
$bExists = false;

View File

@ -182,6 +182,8 @@ sub output_read
{
my $self = shift;
&log(TRACE, "Remote->output_read");
my $strLine;
my $strOutput;
my $bError = false;
@ -192,6 +194,9 @@ sub output_read
if ($strLine =~ /^ERROR.*/)
{
$bError = true;
$iErrorCode = (split(' ', trim($strLine)))[1];
last;
}
@ -259,11 +264,37 @@ sub command_write
my $strCommand = shift;
my $strOptions = shift;
&log(TRACE, "Remote->command_write: $strCommand" . (defined($strOptions) ? ":$strOptions" : ""));
if (!syswrite($self->{hIn}, "$strCommand" . (defined($strOptions) ? ":${strOptions}" : "") . "\n"))
{
confess "unable to write command";
}
}
####################################################################################################################################
# COMMAND_EXECUTE
####################################################################################################################################
sub command_execute
{
my $self = shift;
my $strCommand = shift;
my $strOptions = shift;
my $strErrorPrefix = shift;
$self->command_write($strCommand, $strOptions);
my ($strOutput, $bError, $iErrorCode) = $self->output_read();
# Capture any errors
if ($bError)
{
confess &log(ERROR, (defined($strErrorPrefix) ? "${strErrorPrefix}" : "") .
(defined($strOutput) ? ": ${strOutput}" : ""), $iErrorCode);
}
return $strOutput;
}
no Moose;
__PACKAGE__->meta->make_immutable;

View File

@ -9,12 +9,13 @@
use strict;
use warnings;
use english;
use Carp;
use File::Basename;
use Cwd 'abs_path';
use File::stat;
use Fcntl ':mode';
use Scalar::Util 'blessed';
use lib dirname($0) . "/..";
use pg_backrest_utility;
@ -49,6 +50,8 @@ sub BackRestFileTest
&log(INFO, "FILE MODULE ********************************************************************");
system("ssh backrest\@${strHost} 'rm -rf ${strTestPath}/private'");
#-------------------------------------------------------------------------------------------------------------------------------
# Create remote
#-------------------------------------------------------------------------------------------------------------------------------
@ -707,34 +710,74 @@ sub BackRestFileTest
# Loop through exists
for (my $bExists = 0; $bExists <= 1; $bExists++)
{
$iRun++;
&log(INFO, "run ${iRun} - " .
"remote $bRemote, exists $bExists");
# Drop the old test directory and create a new one
system("rm -rf test");
system("mkdir test") == 0 or confess "Unable to create test directory";
my $strFile = "${strTestPath}/test.txt";
if ($bExists)
# Loop through exists
for (my $bError = 0; $bError <= $bExists; $bError++)
{
system("echo 'TESTDATA' > ${strFile}");
}
$iRun++;
# Execute in eval in case of error
eval
{
if ($oFile->exists(PATH_BACKUP_ABSOLUTE, $strFile) != $bExists)
&log(INFO, "run ${iRun} - " .
"remote $bRemote, exists $bExists, error ${bError}");
# Drop the old test directory and create a new one
system("rm -rf test");
system("mkdir test") == 0 or confess "Unable to create test directory";
my $strFile = "${strTestPath}/test.txt";
if ($bError)
{
confess "bExists is set to ${bExists}, but exists() returned " . !$bExists;
system("ssh backrest\@${strHost} 'mkdir -m 700 ${strTestPath}/private'");
$strFile = "${strTestPath}/private/test.txt";
}
elsif ($bExists)
{
system("echo 'TESTDATA' > ${strFile}");
}
};
if ($@)
{
confess "error raised: " . $@ . "\n";
# Execute in eval in case of error
eval
{
if ($oFile->exists(PATH_BACKUP_ABSOLUTE, $strFile) != $bExists)
{
confess "bExists is set to ${bExists}, but exists() returned " . !$bExists;
}
};
if ($bError)
{
system("ssh backrest\@${strHost} 'rm -rf ${strTestPath}/private'");
}
if ($@)
{
my $oMessage = $@;
my $iCode;
my $strMessage;
if (blessed($oMessage))
{
if ($oMessage->isa("BackRest::Exception"))
{
$iCode = $oMessage->code();
$strMessage = $oMessage->message();
}
else
{
confess 'unknown error object';
}
}
else
{
$strMessage = $oMessage;
}
if ($bError && defined($iCode) && $iCode == COMMAND_ERR_FILE_READ)
{
next;
}
confess "error raised: " . $strMessage . "\n";
}
}
}
}