2016-06-24 14:12:58 +02:00
|
|
|
####################################################################################################################################
|
|
|
|
# CommonTest.pm - Common globals used for testing
|
|
|
|
####################################################################################################################################
|
|
|
|
package pgBackRestTest::Common::FileTest;
|
|
|
|
|
|
|
|
####################################################################################################################################
|
|
|
|
# Perl includes
|
|
|
|
####################################################################################################################################
|
|
|
|
use strict;
|
|
|
|
use warnings FATAL => qw(all);
|
|
|
|
use Carp qw(confess);
|
|
|
|
|
|
|
|
use Cwd qw(abs_path cwd);
|
|
|
|
use Exporter qw(import);
|
|
|
|
our @EXPORT = qw();
|
|
|
|
use File::Basename qw(dirname);
|
|
|
|
use File::Copy qw(move);
|
|
|
|
use File::Path qw(remove_tree);
|
|
|
|
use IO::Select;
|
|
|
|
use IPC::Open3;
|
|
|
|
use POSIX ':sys_wait_h';
|
|
|
|
use Symbol 'gensym';
|
|
|
|
|
|
|
|
use pgBackRest::Common::Ini;
|
|
|
|
use pgBackRest::Common::Log;
|
|
|
|
use pgBackRest::Common::String;
|
|
|
|
use pgBackRest::Common::Wait;
|
|
|
|
use pgBackRest::Config::Config;
|
|
|
|
use pgBackRest::Manifest;
|
2019-06-26 14:24:58 +02:00
|
|
|
use pgBackRest::Storage::Base;
|
2016-06-24 14:12:58 +02:00
|
|
|
|
|
|
|
use pgBackRestTest::Common::ExecuteTest;
|
2017-06-12 16:52:32 +02:00
|
|
|
use pgBackRestTest::Common::HostGroupTest;
|
2016-06-24 14:12:58 +02:00
|
|
|
use pgBackRestTest::Common::LogTest;
|
|
|
|
use pgBackRestTest::Common::VmTest;
|
2017-06-12 16:52:32 +02:00
|
|
|
use pgBackRestTest::Env::Host::HostBaseTest;
|
2017-05-12 22:43:04 +02:00
|
|
|
use pgBackRestTest::Env::Host::HostBackupTest;
|
|
|
|
use pgBackRestTest::Env::Host::HostDbCommonTest;
|
|
|
|
use pgBackRestTest::Env::Host::HostDbTest;
|
2017-06-12 16:52:32 +02:00
|
|
|
use pgBackRestTest::Env::Host::HostS3Test;
|
2016-06-24 14:12:58 +02:00
|
|
|
|
|
|
|
####################################################################################################################################
|
|
|
|
# testLinkCreate
|
|
|
|
#
|
|
|
|
# Create a symlink
|
|
|
|
####################################################################################################################################
|
|
|
|
sub testLinkCreate
|
|
|
|
{
|
|
|
|
my $strLink = shift;
|
|
|
|
my $strDestination = shift;
|
|
|
|
|
|
|
|
# Create the file
|
|
|
|
symlink($strDestination, $strLink)
|
|
|
|
or confess "unable to link ${strLink} to ${strDestination}";
|
|
|
|
}
|
|
|
|
|
|
|
|
push(@EXPORT, qw(testLinkCreate));
|
|
|
|
|
|
|
|
####################################################################################################################################
|
|
|
|
# testPathMode
|
|
|
|
#
|
|
|
|
# Set mode of an existing path.
|
|
|
|
####################################################################################################################################
|
|
|
|
sub testPathMode
|
|
|
|
{
|
|
|
|
my $strPath = shift;
|
|
|
|
my $strMode = shift;
|
|
|
|
|
|
|
|
# Set the mode
|
|
|
|
chmod(oct($strMode), $strPath)
|
|
|
|
or confess 'unable to set mode ${strMode} for ${strPath}';
|
|
|
|
}
|
|
|
|
|
|
|
|
push(@EXPORT, qw(testPathMode));
|
|
|
|
|
|
|
|
####################################################################################################################################
|
|
|
|
# testPathRemove
|
|
|
|
#
|
|
|
|
# Remove a path and all subpaths.
|
|
|
|
####################################################################################################################################
|
|
|
|
sub testPathRemove
|
|
|
|
{
|
|
|
|
my $strPath = shift;
|
|
|
|
my $bSuppressError = shift;
|
|
|
|
|
|
|
|
executeTest('sudo rm -rf ' . $strPath, {bSuppressError => $bSuppressError});
|
|
|
|
}
|
|
|
|
|
|
|
|
push(@EXPORT, qw(testPathRemove));
|
|
|
|
|
|
|
|
####################################################################################################################################
|
|
|
|
# testFileCreate
|
|
|
|
#
|
|
|
|
# Create a file specifying content, mode, and time.
|
|
|
|
####################################################################################################################################
|
|
|
|
sub testFileCreate
|
|
|
|
{
|
|
|
|
my $strFile = shift;
|
|
|
|
my $strContent = shift;
|
|
|
|
my $lTime = shift;
|
|
|
|
my $strMode = shift;
|
|
|
|
|
|
|
|
# Open the file and save strContent to it
|
|
|
|
my $hFile = shift;
|
|
|
|
|
|
|
|
open($hFile, '>', $strFile)
|
|
|
|
or confess "unable to open ${strFile} for writing";
|
|
|
|
|
2018-07-05 21:40:50 +02:00
|
|
|
if (defined($strContent) && $strContent ne '')
|
|
|
|
{
|
|
|
|
syswrite($hFile, $strContent)
|
|
|
|
or confess "unable to write to ${strFile}: $!";
|
|
|
|
}
|
2016-06-24 14:12:58 +02:00
|
|
|
|
|
|
|
close($hFile);
|
|
|
|
|
|
|
|
# Set the time
|
|
|
|
if (defined($lTime))
|
|
|
|
{
|
|
|
|
utime($lTime, $lTime, $strFile)
|
|
|
|
or confess 'unable to set time ${lTime} for ${strPath}';
|
|
|
|
}
|
|
|
|
|
|
|
|
# Set the mode
|
|
|
|
chmod(oct(defined($strMode) ? $strMode : '0600'), $strFile)
|
|
|
|
or confess 'unable to set mode ${strMode} for ${strFile}';
|
|
|
|
}
|
|
|
|
|
|
|
|
push(@EXPORT, qw(testFileCreate));
|
|
|
|
|
|
|
|
####################################################################################################################################
|
|
|
|
# testFileRemove
|
|
|
|
#
|
|
|
|
# Remove a file.
|
|
|
|
####################################################################################################################################
|
|
|
|
sub testFileRemove
|
|
|
|
{
|
|
|
|
my $strFile = shift;
|
|
|
|
|
|
|
|
unlink($strFile)
|
|
|
|
or confess "unable to remove ${strFile}: $!";
|
|
|
|
}
|
|
|
|
|
|
|
|
push(@EXPORT, qw(testFileRemove));
|
|
|
|
|
2017-06-09 23:51:41 +02:00
|
|
|
####################################################################################################################################
|
|
|
|
# forceStorageMode - force mode on a file or path
|
|
|
|
####################################################################################################################################
|
|
|
|
sub forceStorageMode
|
|
|
|
{
|
|
|
|
# Assign function parameters, defaults, and log debug info
|
|
|
|
my
|
|
|
|
(
|
|
|
|
$strOperation,
|
|
|
|
$oStorage,
|
|
|
|
$strPathExp,
|
|
|
|
$strMode,
|
|
|
|
$bRecurse
|
|
|
|
) =
|
|
|
|
logDebugParam
|
|
|
|
(
|
|
|
|
__PACKAGE__ . '::forceStorageMode', \@_,
|
|
|
|
{name => 'oStorage'},
|
|
|
|
{name => 'strPathExp'},
|
|
|
|
{name => 'strMode'},
|
|
|
|
{name => 'bRecurse', optional => true, default => false},
|
|
|
|
);
|
|
|
|
|
2017-06-12 16:52:32 +02:00
|
|
|
# Mode commands are ignored on S3
|
2019-06-26 14:24:58 +02:00
|
|
|
if ($oStorage->type() ne STORAGE_S3)
|
2017-06-12 16:52:32 +02:00
|
|
|
{
|
|
|
|
executeTest('sudo chmod ' . ($bRecurse ? '-R ' : '') . "${strMode} " . $oStorage->pathGet($strPathExp));
|
|
|
|
}
|
2017-06-09 23:51:41 +02:00
|
|
|
|
|
|
|
# Return from function and log return values if any
|
|
|
|
return logDebugReturn($strOperation);
|
|
|
|
}
|
|
|
|
|
|
|
|
push(@EXPORT, qw(forceStorageMode));
|
|
|
|
|
|
|
|
####################################################################################################################################
|
|
|
|
# forceStorageMove - force move a directory or file
|
|
|
|
####################################################################################################################################
|
|
|
|
sub forceStorageMove
|
|
|
|
{
|
|
|
|
# Assign function parameters, defaults, and log debug info
|
|
|
|
my
|
|
|
|
(
|
|
|
|
$strOperation,
|
|
|
|
$oStorage,
|
|
|
|
$strSourcePathExp,
|
|
|
|
$strDestinationPathExp,
|
2017-11-06 19:51:12 +02:00
|
|
|
$bRecurse,
|
2017-06-09 23:51:41 +02:00
|
|
|
) =
|
|
|
|
logDebugParam
|
|
|
|
(
|
|
|
|
__PACKAGE__ . '->forceStorageMove', \@_,
|
|
|
|
{name => 'oStorage'},
|
|
|
|
{name => 'strSourcePathExp'},
|
|
|
|
{name => 'strDestinationPathExp'},
|
2017-11-06 19:51:12 +02:00
|
|
|
{name => 'bRecurse', optional => true, default => true},
|
2017-06-09 23:51:41 +02:00
|
|
|
);
|
|
|
|
|
2017-06-12 16:52:32 +02:00
|
|
|
# If S3 then use storage commands to remove
|
2019-06-26 14:24:58 +02:00
|
|
|
if ($oStorage->type() eq STORAGE_S3)
|
2017-06-12 16:52:32 +02:00
|
|
|
{
|
|
|
|
hostGroupGet()->hostGet(HOST_S3)->executeS3(
|
2017-11-06 19:51:12 +02:00
|
|
|
'mv' . ($bRecurse ? ' --recursive' : '') . ' s3://' . HOST_S3_BUCKET . $oStorage->pathGet($strSourcePathExp) .
|
2017-06-12 16:52:32 +02:00
|
|
|
' s3://' . HOST_S3_BUCKET . $oStorage->pathGet($strDestinationPathExp));
|
|
|
|
}
|
|
|
|
# Else remove using filesystem commands
|
|
|
|
else
|
|
|
|
{
|
|
|
|
executeTest('sudo mv ' . $oStorage->pathGet($strSourcePathExp) . ' ' . $oStorage->pathGet($strDestinationPathExp));
|
|
|
|
}
|
2017-06-09 23:51:41 +02:00
|
|
|
|
|
|
|
# Return from function and log return values if any
|
|
|
|
return logDebugReturn($strOperation);
|
|
|
|
}
|
|
|
|
|
|
|
|
push(@EXPORT, qw(forceStorageMove));
|
|
|
|
|
|
|
|
####################################################################################################################################
|
|
|
|
# forceStorageOwner - force ownership on a file or path
|
|
|
|
####################################################################################################################################
|
|
|
|
sub forceStorageOwner
|
|
|
|
{
|
|
|
|
# Assign function parameters, defaults, and log debug info
|
|
|
|
my
|
|
|
|
(
|
|
|
|
$strOperation,
|
|
|
|
$oStorage,
|
|
|
|
$strPathExp,
|
|
|
|
$strOwner,
|
|
|
|
$bRecurse
|
|
|
|
) =
|
|
|
|
logDebugParam
|
|
|
|
(
|
|
|
|
__PACKAGE__ . '::forceStorageOwner', \@_,
|
|
|
|
{name => 'oStorage'},
|
|
|
|
{name => 'strPathExp'},
|
|
|
|
{name => 'strOwner'},
|
|
|
|
{name => 'bRecurse', optional => true, default => false},
|
|
|
|
);
|
|
|
|
|
2019-06-26 14:24:58 +02:00
|
|
|
# Owner commands are ignored on S3
|
|
|
|
if ($oStorage->type() ne STORAGE_S3)
|
2017-06-12 16:52:32 +02:00
|
|
|
{
|
|
|
|
executeTest('sudo chown ' . ($bRecurse ? '-R ' : '') . "${strOwner} " . $oStorage->pathGet($strPathExp));
|
|
|
|
}
|
2017-06-09 23:51:41 +02:00
|
|
|
|
|
|
|
# Return from function and log return values if any
|
|
|
|
return logDebugReturn($strOperation);
|
|
|
|
}
|
|
|
|
|
|
|
|
push(@EXPORT, qw(forceStorageOwner));
|
|
|
|
|
|
|
|
####################################################################################################################################
|
|
|
|
# forceStorageRemove - force remove a file or path from storage
|
|
|
|
####################################################################################################################################
|
|
|
|
sub forceStorageRemove
|
|
|
|
{
|
|
|
|
# Assign function parameters, defaults, and log debug info
|
|
|
|
my
|
|
|
|
(
|
|
|
|
$strOperation,
|
|
|
|
$oStorage,
|
|
|
|
$strPathExp,
|
|
|
|
$bRecurse
|
|
|
|
) =
|
|
|
|
logDebugParam
|
|
|
|
(
|
|
|
|
__PACKAGE__ . '->forceStorageRemove', \@_,
|
|
|
|
{name => 'oStorage'},
|
|
|
|
{name => 'strPathExp'},
|
|
|
|
{name => 'bRecurse', optional => true, default => false},
|
|
|
|
);
|
|
|
|
|
2017-06-12 16:52:32 +02:00
|
|
|
# If S3 then use storage commands to remove
|
2019-06-26 14:24:58 +02:00
|
|
|
if ($oStorage->type() eq STORAGE_S3)
|
2017-06-12 16:52:32 +02:00
|
|
|
{
|
2019-06-26 14:24:58 +02:00
|
|
|
my $oInfo = $oStorage->info($strPathExp, {bIgnoreMissing => true});
|
|
|
|
|
|
|
|
if (defined($oInfo) && $oInfo->{type} eq 'f')
|
|
|
|
{
|
|
|
|
$oStorage->remove($strPathExp);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$oStorage->pathRemove($strPathExp, {bRecurse => true});
|
|
|
|
}
|
2017-06-12 16:52:32 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
executeTest('sudo rm -f' . ($bRecurse ? 'r ' : ' ') . $oStorage->pathGet($strPathExp));
|
|
|
|
}
|
2017-06-09 23:51:41 +02:00
|
|
|
|
|
|
|
# Return from function and log return values if any
|
|
|
|
return logDebugReturn($strOperation);
|
|
|
|
}
|
|
|
|
|
|
|
|
push(@EXPORT, qw(forceStorageRemove));
|
|
|
|
|
2016-06-24 14:12:58 +02:00
|
|
|
1;
|