1
0
mirror of https://github.com/pgbackrest/pgbackrest.git synced 2025-07-17 01:12:23 +02:00

Better error handling for missing source file in copy().

This commit is contained in:
David Steele
2014-06-21 10:05:49 -04:00
parent 3af5239800
commit 4a113cae66
3 changed files with 142 additions and 127 deletions

View File

@ -795,8 +795,27 @@ sub copy
{
if (!$bSourceRemote)
{
open($hSourceFile, "<", $strSourceOp)
or confess &log(ERROR, "cannot open ${strSourceOp}: " . $!);
if (!open($hSourceFile, "<", $strSourceOp))
{
my $strError = $!;
my $iErrorCode = COMMAND_ERR_FILE_READ;
if ($!{ENOENT})
{
# $strError = 'file is missing';
$iErrorCode = COMMAND_ERR_FILE_MISSING;
}
$strError = "cannot open source file ${strSourceOp}: " . $strError;
if ($strSourcePathType eq PATH_ABSOLUTE)
{
$self->{oRemote}->write_line(*STDOUT, "block 0");
confess &log(ERROR, $strError, $iErrorCode);
}
confess &log(ERROR, "${strDebug}: " . $strError, $iErrorCode);
}
}
if (!$bDestinationRemote)

View File

@ -264,6 +264,27 @@ sub read_line
return $strLine;
}
####################################################################################################################################
# WRITE_LINE
#
# 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)";
}
}
####################################################################################################################################
# WAIT_PID
#

View File

@ -823,6 +823,8 @@ sub BackRestFileTest
my $strSourcePath = $bSourcePathType ? "db" : "backup";
for (my $bDestinationPathType = 0; $bDestinationPathType <= 1; $bDestinationPathType++)
{
for (my $bSourceMissing = 0; $bSourceMissing <= 1; $bSourceMissing++)
{
my $strDestinationPathType = $bDestinationPathType ? PATH_DB_ABSOLUTE : PATH_BACKUP_ABSOLUTE;
my $strDestinationPath = $bDestinationPathType ? "db" : "backup";
@ -836,7 +838,7 @@ sub BackRestFileTest
&log(INFO, "run ${iRun} - " .
"srcpth " . (defined($strRemote) && $strRemote eq $strSourcePath ? "remote" : "local") .
":${strSourcePath}, srccmp $bSourceCompressed, " .
":${strSourcePath}, srccmp $bSourceCompressed, srcmiss ${bSourceMissing}, " .
"dstpth " . (defined($strRemote) && $strRemote eq $strDestinationPath ? "remote" : "local") .
":${strDestinationPath}, dstcmp $bDestinationCompress");
@ -849,15 +851,20 @@ sub BackRestFileTest
my $strSourceFile = "${strTestPath}/${strSourcePath}/test-source.txt";
my $strDestinationFile = "${strTestPath}/${strDestinationPath}/test-destination.txt";
system("echo 'TESTDATA' > ${strSourceFile}");
my $strSourceHash = $oFile->hash(PATH_ABSOLUTE, $strSourceFile);
# Create the compressed or uncompressed test file
my $strSourceHash;
if (!$bSourceMissing)
{
system("echo 'TESTDATA' > ${strSourceFile}");
$strSourceHash = $oFile->hash(PATH_ABSOLUTE, $strSourceFile);
if ($bSourceCompressed)
{
system("gzip -n ${strSourceFile}");
system("gzip ${strSourceFile}");
$strSourceFile .= ".gz";
}
}
if ($bDestinationCompress)
{
@ -877,69 +884,36 @@ sub BackRestFileTest
# Check for errors after copy
if ($@)
{
# Different remote and destination with different path types should error
# if (($bBackupRemote || $bDbRemote) && ($strSourcePathType ne $strDestinationPathType))
# {
# print " different source and remote for same path not supported\n";
# next;
# }
# If the error was intentional, then also continue
# elsif ($bError)
# {
# my $strError = $oFile->error_get();
#
# if (!defined($strError) || ($strError eq ''))
# {
# confess 'no error message returned';
# }
#
# print " error raised: ${strError}\n";
# next;
# }
# Else this is an unexpected error
# else
# {
confess $@;
# }
my $oMessage = $@;
if (blessed($oMessage))
{
if ($oMessage->isa("BackRest::Exception"))
{
if ($bSourceMissing)
{
next;
}
confess $oMessage->message();
}
else
{
confess 'unknown error object: ' . $oMessage;
}
}
else
{
confess $oMessage;
}
}
# elsif ($bError)
# {
# if ($bConfessError)
# {
# confess "Value was returned instead of exception thrown when confess error is true";
# }
# else
# {
# if ($bReturn)
# {
# confess "true was returned when an error was generated";
# }
# else
# {
# my $strError = $oFile->error_get();
#
# if (!defined($strError) || ($strError eq ''))
# {
# confess 'no error message returned';
# }
#
# print " error returned: ${strError}\n";
# next;
# }
# }
# }
# else
# {
# if (!$bReturn)
# {
# confess "error was returned when no error generated";
# }
#
# print " true was returned\n";
# }
if ($bReturn)
{
if ($bSourceMissing)
{
confess "expected source file missing error";
}
# my $strDestinationFileCheck = $strDestinationFile;
#
# # Check for errors after copy
@ -973,4 +947,5 @@ sub BackRestFileTest
}
}
}
}
}