1
0
mirror of https://github.com/pgbackrest/pgbackrest.git synced 2025-06-12 23:37:43 +02:00

move() is working.

This commit is contained in:
David Steele 2014-06-21 14:19:03 -04:00
parent 80206a28e9
commit 936b8d0db0
2 changed files with 137 additions and 103 deletions

View File

@ -487,47 +487,37 @@ sub move
my $strDestinationFile = shift; my $strDestinationFile = shift;
my $bDestinationPathCreate = shift; my $bDestinationPathCreate = shift;
# Get the root path for the file list # Set defaults
my $strErrorPrefix = "File->move"; $bDestinationPathCreate = defined($bDestinationPathCreate) ? $bDestinationPathCreate : false;
my $bRemote = $self->is_remote($strSourcePathType);
$bDestinationPathCreate = defined($bDestinationPathCreate) ? $bDestinationPathCreate : true;
&log(TRACE, "${strErrorPrefix}: " . ($bRemote ? "remote" : "local") .
" ${strSourcePathType}" . (defined($strSourceFile) ? ":${strSourceFile}" : "") .
" to ${strDestinationPathType}" . (defined($strDestinationFile) ? ":${strDestinationFile}" : "") .
", dest_path_create = " . ($bDestinationPathCreate ? "true" : "false"));
# Get source and desination files
if ($self->path_type_get($strSourcePathType) ne $self->path_type_get($strSourcePathType))
{
confess &log(ASSERT, "source and destination path types must be equal");
}
# Set operation variables
my $strPathOpSource = $self->path_get($strSourcePathType, $strSourceFile); my $strPathOpSource = $self->path_get($strSourcePathType, $strSourceFile);
my $strPathOpDestination = $self->path_get($strDestinationPathType, $strDestinationFile); my $strPathOpDestination = $self->path_get($strDestinationPathType, $strDestinationFile);
# Run remotely # Set operation and debug strings
if ($bRemote) my $strOperation = OP_FILE_MOVE;
my $strDebug = "${strSourcePathType}" . (defined($strSourceFile) ? ":${strSourceFile}" : "") .
" to ${strDestinationPathType}" . (defined($strDestinationFile) ? ":${strDestinationFile}" : "") .
", destination_path_create = " . ($bDestinationPathCreate ? "true" : "false");
&log(DEBUG, "${strOperation}: ${strDebug}");
# Source and destination path types must be the same
if ($self->path_type_get($strSourcePathType) ne $self->path_type_get($strSourcePathType))
{ {
my $strCommand = $self->{strCommand} . confess &log(ASSERT, "${strDebug}: source and destination path types must be equal");
($bDestinationPathCreate ? " --dest-path-create" : "") . }
" move ${strPathOpSource} ${strPathOpDestination}";
# Run via SSH # Run remotely
my $oSSH = $self->remote_get($strSourcePathType); if ($self->is_remote($strSourcePathType))
my $strOutput = $oSSH->capture($strCommand); {
confess "${strDebug}: remote operation not supported";
# Handle any errors
if ($oSSH->error)
{
confess &log(ERROR, "${strErrorPrefix} remote (${strCommand}): " . (defined($strOutput) ? $strOutput : $oSSH->error));
}
} }
# Run locally # Run locally
else else
{ {
# If the destination path does not exist, create it # If the destination path does not exist, create it or error out
unless (-e dirname($strPathOpDestination)) if (!$self->exists($strDestinationPathType, dirname($strDestinationFile)))
{ {
if ($bDestinationPathCreate) if ($bDestinationPathCreate)
{ {
@ -543,16 +533,16 @@ sub move
exit (COMMAND_ERR_PATH_MISSING); exit (COMMAND_ERR_PATH_MISSING);
} }
confess &log(ERROR, "${strErrorPrefix}: " . $strError); confess &log(ERROR, "${strDebug}: " . $strError);
} }
} }
if (!rename($strPathOpSource, $strPathOpDestination)) if (!rename($strPathOpSource, $strPathOpDestination))
{ {
my $strError = "${strPathOpSource} could not be moved:" . $!; my $strError = "${strPathOpSource} could not be moved: " . $!;
my $iErrorCode = COMMAND_ERR_FILE_MOVE; my $iErrorCode = COMMAND_ERR_FILE_MOVE;
unless (-e $strPathOpSource) if (!$self->exists($strSourcePathType, dirname($strSourceFile)))
{ {
$strError = "${strPathOpSource} does not exist"; $strError = "${strPathOpSource} does not exist";
$iErrorCode = COMMAND_ERR_FILE_MISSING; $iErrorCode = COMMAND_ERR_FILE_MISSING;
@ -564,7 +554,7 @@ sub move
exit ($iErrorCode); exit ($iErrorCode);
} }
confess &log(ERROR, "${strErrorPrefix}: " . $strError); confess &log(ERROR, "${strDebug}: " . $strError);
} }
} }
} }
@ -608,7 +598,7 @@ sub exists
# Build debug string # Build debug string
$strDebug = "${strOperation}: local: " . $strDebug; $strDebug = "${strOperation}: local: " . $strDebug;
&log(DEBUG, ${strDebug}); &log(DEBUG, ${strDebug});
# Stat the file/path to determine if it exists # Stat the file/path to determine if it exists
my $oStat = lstat($strPathOp); my $oStat = lstat($strPathOp);

View File

@ -25,6 +25,36 @@ use BackRest::Remote;
use Exporter qw(import); use Exporter qw(import);
our @EXPORT = qw(BackRestFileTest); our @EXPORT = qw(BackRestFileTest);
my $strTestPath;
my $strHost;
my $strUserBackRest;
####################################################################################################################################
# BackRestFileTestSetup
####################################################################################################################################
sub BackRestFileTestSetup
{
my $bDropOnly = shift;
# Remove the backrest private directory
system("ssh ${strUserBackRest}\@${strHost} 'rm -rf ${strTestPath}/private'") == 0 or die 'unable to remove test/private path';
# Remove the test directory
system("rm -rf ${strTestPath}") == 0 or die 'unable to drop test path';
if (defined($bDropOnly) || !$bDropOnly)
{
# Create the test directory
system("mkdir ${strTestPath}") == 0 or confess "Unable to create test directory";
# Create the backrest private directory
system("ssh backrest\@${strHost} 'mkdir -m 700 ${strTestPath}/private'") == 0 or die 'unable to create test/private path';
}
}
####################################################################################################################################
# BackRestFileTest
####################################################################################################################################
sub BackRestFileTest sub BackRestFileTest
{ {
my $strTest = shift; my $strTest = shift;
@ -36,14 +66,15 @@ sub BackRestFileTest
} }
# Setup test paths # Setup test paths
my $strTestPath = dirname(abs_path($0)) . "/test"; $strTestPath = dirname(abs_path($0)) . "/test";
my $iRun; my $iRun;
my $strStanza = "db"; my $strStanza = "db";
my $strCommand = "/Users/dsteele/pg_backrest/bin/pg_backrest_remote.pl"; my $strCommand = "/Users/dsteele/pg_backrest/bin/pg_backrest_remote.pl";
my $strHost = "127.0.0.1"; $strHost = "127.0.0.1";
my $strUser = getpwuid($<); my $strUser = getpwuid($<);
my $strGroup = getgrgid($(); my $strGroup = getgrgid($();
$strUserBackRest = 'backrest';
# Print test parameters # Print test parameters
&log(INFO, "Testing with test_path = ${strTestPath}, host = ${strHost}, user = ${strUser}, group = ${strGroup}"); &log(INFO, "Testing with test_path = ${strTestPath}, host = ${strHost}, user = ${strUser}, group = ${strGroup}");
@ -177,82 +208,93 @@ sub BackRestFileTest
&log(INFO, "--------------------------------------------------------------------------------"); &log(INFO, "--------------------------------------------------------------------------------");
&log(INFO, "Test File->move()\n"); &log(INFO, "Test File->move()\n");
for (my $bRemote = 0; $bRemote <= 1; $bRemote++) for (my $bRemote = 0; $bRemote <= 0; $bRemote++)
{ {
# Create the file object
my $oFile = BackRest::File->new my $oFile = BackRest::File->new
( (
strStanza => $strStanza, strStanza => "db",
bNoCompression => true, strBackupClusterPath => undef,
strCommand => $strCommand,
strBackupClusterPath => ${strTestPath},
strBackupPath => ${strTestPath}, strBackupPath => ${strTestPath},
strBackupHost => $bRemote ? $strHost : undef, strRemote => $bRemote ? 'backup' : undef,
strBackupUser => $bRemote ? $strUser : undef oRemote => $bRemote ? $oRemote : undef
); );
# Loop through source exists # Loop through source exists
for (my $bSourceExists = 0; $bSourceExists <= 1; $bSourceExists++) for (my $bSourceExists = 0; $bSourceExists <= 1; $bSourceExists++)
{ {
# Loop through destination exists # Loop through source errors
for (my $bDestinationExists = 0; $bDestinationExists <= 1; $bDestinationExists++) for (my $bSourceError = 0; $bSourceError <= 1; $bSourceError++)
{
# Loop through destination exists
for (my $bDestinationExists = 0; $bDestinationExists <= 1; $bDestinationExists++)
{
# Loop through source errors
for (my $bDestinationError = 0; $bDestinationError <= 1; $bDestinationError++)
{
# Loop through create
for (my $bCreate = 0; $bCreate <= $bDestinationExists; $bCreate++)
{
$iRun++;
&log(INFO, "run ${iRun} - " .
"remote $bRemote" .
", src_exists $bSourceExists, src_error $bSourceError" .
", dst_exists $bDestinationExists, dst_error $bDestinationError, dst_create $bCreate");
# Setup test directory
BackRestFileTestSetup();
my $strSourceFile = "${strTestPath}/test.txt";
my $strDestinationFile = "${strTestPath}/test-dest.txt";
if ($bSourceError)
{ {
# Loop through create $strSourceFile = "${strTestPath}/private/test.txt";
for (my $bCreate = 0; $bCreate <= $bDestinationExists; $bCreate++)
{
$iRun++;
&log(INFO, "run ${iRun} - " .
"remote $bRemote, src_exists $bSourceExists, dst_exists $bDestinationExists, create $bCreate");
# 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 $strSourceFile = "${strTestPath}/test.txt";
my $strDestinationFile = "${strTestPath}/test-dest.txt";
if ($bCreate)
{
$strDestinationFile = "${strTestPath}/sub/test-dest.txt"
}
if ($bSourceExists)
{
system("echo 'TESTDATA' > ${strSourceFile}");
}
if (!$bDestinationExists)
{
$strDestinationFile = "error" . $strDestinationFile;
}
# Execute in eval in case of error
eval
{
$oFile->move(PATH_BACKUP_ABSOLUTE, $strSourceFile, PATH_BACKUP_ABSOLUTE, $strDestinationFile, $bCreate);
};
if ($@)
{
if (!$bSourceExists || !$bDestinationExists)
{
next;
}
confess "error raised: " . $@ . "\n";
}
if (!$bSourceExists || !$bDestinationExists)
{
confess "error should have been raised";
}
unless (-e $strDestinationFile)
{
confess "file was not moved";
}
}
} }
elsif ($bSourceExists)
{
system("echo 'TESTDATA' > ${strSourceFile}");
}
if ($bDestinationError)
{
$strSourceFile = "${strTestPath}/private/test.txt";
}
elsif (!$bDestinationExists)
{
$strDestinationFile = "${strTestPath}/sub/test-dest.txt";
}
# Execute in eval in case of error
eval
{
$oFile->move(PATH_BACKUP_ABSOLUTE, $strSourceFile, PATH_BACKUP_ABSOLUTE, $strDestinationFile, $bCreate);
};
if ($@)
{
if (!$bSourceExists || (!$bDestinationExists && !$bCreate) || $bSourceError || $bDestinationError)
{
next;
}
confess "error raised: " . $@ . "\n";
}
if (!$bSourceExists || (!$bDestinationExists && !$bCreate) || $bSourceError || $bDestinationError)
{
confess "error should have been raised";
}
unless (-e $strDestinationFile)
{
confess "file was not moved";
}
}
}
}
}
} }
} }
} }
@ -962,3 +1004,5 @@ sub BackRestFileTest
} }
} }
} }
1;