pgbackrest.git / summary / log / commit / refs

commit    4a113cae66e8ef4ac83b6089ca2f3fe4c82ff185
Author:   David Steele <github@thelabyrinth.net>
Date:     Sat Jun 21 14:05:49 2014 +0000

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


lib/BackRest/File.pm | 23 +++- lib/BackRest/Remote.pm | 21 ++++ test/lib/BackRestTest/FileTest.pm | 223 +++++++++++++++++--------------------- 3 files changed, 141 insertions(+), 126 deletions(-) diff --git a/lib/BackRest/File.pm b/lib/BackRest/File.pm index e86e98906..b10611f25 100644 --- a/lib/BackRest/File.pm +++ b/lib/BackRest/File.pm @@ -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) diff --git a/lib/BackRest/Remote.pm b/lib/BackRest/Remote.pm index 98737ad45..3c6aba296 100644 --- a/lib/BackRest/Remote.pm +++ b/lib/BackRest/Remote.pm @@ -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 # diff --git a/test/lib/BackRestTest/FileTest.pm b/test/lib/BackRestTest/FileTest.pm index 7f82eb6aa..1f078b7c0 100755 --- a/test/lib/BackRestTest/FileTest.pm +++ b/test/lib/BackRestTest/FileTest.pm @@ -824,146 +824,121 @@ sub BackRestFileTest for (my $bDestinationPathType = 0; $bDestinationPathType <= 1; $bDestinationPathType++) { - my $strDestinationPathType = $bDestinationPathType ? PATH_DB_ABSOLUTE : PATH_BACKUP_ABSOLUTE; - my $strDestinationPath = $bDestinationPathType ? "db" : "backup"; - - $iRun++; + for (my $bSourceMissing = 0; $bSourceMissing <= 1; $bSourceMissing++) + { + my $strDestinationPathType = $bDestinationPathType ? PATH_DB_ABSOLUTE : PATH_BACKUP_ABSOLUTE; + my $strDestinationPath = $bDestinationPathType ? "db" : "backup"; - # if ($iRun != 27) - # { - # next; - # } + $iRun++; - &log(INFO, "run ${iRun} - " . - "srcpth " . (defined($strRemote) && $strRemote eq $strSourcePath ? "remote" : "local") . - ":${strSourcePath}, srccmp $bSourceCompressed, " . - "dstpth " . (defined($strRemote) && $strRemote eq $strDestinationPath ? "remote" : "local") . - ":${strDestinationPath}, dstcmp $bDestinationCompress"); + # if ($iRun != 27) + # { + # next; + # } - # Drop the old test directory and create a new one - system("rm -rf test"); - system("mkdir -p test/lock") == 0 or confess "Unable to create test/lock directory"; - system("mkdir -p test/backup") == 0 or confess "Unable to create test/backup directory"; - system("mkdir -p test/db") == 0 or confess "Unable to create test/db directory"; + &log(INFO, "run ${iRun} - " . + "srcpth " . (defined($strRemote) && $strRemote eq $strSourcePath ? "remote" : "local") . + ":${strSourcePath}, srccmp $bSourceCompressed, srcmiss ${bSourceMissing}, " . + "dstpth " . (defined($strRemote) && $strRemote eq $strDestinationPath ? "remote" : "local") . + ":${strDestinationPath}, dstcmp $bDestinationCompress"); - my $strSourceFile = "${strTestPath}/${strSourcePath}/test-source.txt"; - my $strDestinationFile = "${strTestPath}/${strDestinationPath}/test-destination.txt"; + # Drop the old test directory and create a new one + system("rm -rf test"); + system("mkdir -p test/lock") == 0 or confess "Unable to create test/lock directory"; + system("mkdir -p test/backup") == 0 or confess "Unable to create test/backup directory"; + system("mkdir -p test/db") == 0 or confess "Unable to create test/db directory"; - system("echo 'TESTDATA' > ${strSourceFile}"); - my $strSourceHash = $oFile->hash(PATH_ABSOLUTE, $strSourceFile); + my $strSourceFile = "${strTestPath}/${strSourcePath}/test-source.txt"; + my $strDestinationFile = "${strTestPath}/${strDestinationPath}/test-destination.txt"; - # Create the compressed or uncompressed test file - if ($bSourceCompressed) - { - system("gzip -n ${strSourceFile}"); - $strSourceFile .= ".gz"; - } + # 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 ${strSourceFile}"); + $strSourceFile .= ".gz"; + } + } - if ($bDestinationCompress) - { - $strDestinationFile .= ".gz"; - } + if ($bDestinationCompress) + { + $strDestinationFile .= ".gz"; + } - # Run file copy in an eval block because some errors are expected - my $bReturn; + # Run file copy in an eval block because some errors are expected + my $bReturn; - eval - { - $bReturn = $oFile->copy($strSourcePathType, $strSourceFile, - $strDestinationPathType, $strDestinationFile, - $bSourceCompressed, $bDestinationCompress); - }; + eval + { + $bReturn = $oFile->copy($strSourcePathType, $strSourceFile, + $strDestinationPathType, $strDestinationFile, + $bSourceCompressed, $bDestinationCompress); + }; - # 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 $@; - # } - } - # 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) - { - # my $strDestinationFileCheck = $strDestinationFile; - # - # # Check for errors after copy - # if ($bDestinationCompress) - # { - # $strDestinationFileCheck .= ".gz"; - # } - - unless (-e $strDestinationFile) + # Check for errors after copy + if ($@) { - confess "could not find destination file ${strDestinationFile}"; + 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; + } } - if ($bDestinationCompress) + if ($bReturn) { - system("gzip -d ${strDestinationFile}") == 0 or die "could not decompress ${strDestinationFile}"; - $strDestinationFile = substr($strDestinationFile, 0, length($strDestinationFile) - 3); - } + if ($bSourceMissing) + { + confess "expected source file missing error"; + } + # my $strDestinationFileCheck = $strDestinationFile; + # + # # Check for errors after copy + # if ($bDestinationCompress) + # { + # $strDestinationFileCheck .= ".gz"; + # } - my $strDestinationHash = $oFile->hash(PATH_ABSOLUTE, $strDestinationFile); + unless (-e $strDestinationFile) + { + confess "could not find destination file ${strDestinationFile}"; + } + + if ($bDestinationCompress) + { + system("gzip -d ${strDestinationFile}") == 0 or die "could not decompress ${strDestinationFile}"; + $strDestinationFile = substr($strDestinationFile, 0, length($strDestinationFile) - 3); + } - if ($strSourceHash ne $strDestinationHash) - { - confess "source ${strSourceHash} and destination ${strDestinationHash} file hashes do not match"; + my $strDestinationHash = $oFile->hash(PATH_ABSOLUTE, $strDestinationFile); + + if ($strSourceHash ne $strDestinationHash) + { + confess "source ${strSourceHash} and destination ${strDestinationHash} file hashes do not match"; + } } } } [parent: 3af52398006b]