2017-06-09 23:51:41 +02:00
|
|
|
####################################################################################################################################
|
2019-06-26 14:24:58 +02:00
|
|
|
# C Storage Interface
|
2017-06-09 23:51:41 +02:00
|
|
|
####################################################################################################################################
|
2019-06-26 14:24:58 +02:00
|
|
|
package pgBackRest::Storage::Storage;
|
2017-06-09 23:51:41 +02:00
|
|
|
use parent 'pgBackRest::Storage::Base';
|
|
|
|
|
|
|
|
use strict;
|
|
|
|
use warnings FATAL => qw(all);
|
|
|
|
use Carp qw(confess);
|
|
|
|
use English '-no_match_vars';
|
|
|
|
|
|
|
|
use File::Basename qw(dirname);
|
2019-06-26 14:24:58 +02:00
|
|
|
use Fcntl qw(:mode);
|
|
|
|
use File::stat qw{lstat};
|
|
|
|
use JSON::PP;
|
2017-06-09 23:51:41 +02:00
|
|
|
|
|
|
|
use pgBackRest::Common::Exception;
|
2019-06-26 14:24:58 +02:00
|
|
|
use pgBackRest::Common::Io::Handle;
|
2017-06-09 23:51:41 +02:00
|
|
|
use pgBackRest::Common::Log;
|
2017-11-06 19:51:12 +02:00
|
|
|
use pgBackRest::Storage::Base;
|
2019-06-26 14:24:58 +02:00
|
|
|
use pgBackRest::Storage::StorageRead;
|
|
|
|
use pgBackRest::Storage::StorageWrite;
|
2017-06-09 23:51:41 +02:00
|
|
|
|
|
|
|
####################################################################################################################################
|
|
|
|
# new
|
|
|
|
####################################################################################################################################
|
|
|
|
sub new
|
|
|
|
{
|
|
|
|
my $class = shift;
|
|
|
|
|
2019-06-26 14:24:58 +02:00
|
|
|
# Create the class hash
|
|
|
|
my $self = {};
|
|
|
|
bless $self, $class;
|
|
|
|
|
2017-06-09 23:51:41 +02:00
|
|
|
# Assign function parameters, defaults, and log debug info
|
|
|
|
(
|
2019-06-26 14:24:58 +02:00
|
|
|
my $strOperation,
|
|
|
|
$self->{strType},
|
|
|
|
$self->{strPath},
|
|
|
|
$self->{lBufferMax},
|
|
|
|
$self->{strDefaultPathMode},
|
|
|
|
$self->{strDefaultFileMode},
|
2017-06-09 23:51:41 +02:00
|
|
|
) =
|
|
|
|
logDebugParam
|
|
|
|
(
|
|
|
|
__PACKAGE__ . '->new', \@_,
|
2019-06-26 14:24:58 +02:00
|
|
|
{name => 'strType'},
|
|
|
|
{name => 'strPath', optional => true},
|
|
|
|
{name => 'lBufferMax', optional => true, default => 65536},
|
2017-06-09 23:51:41 +02:00
|
|
|
{name => 'strDefaultPathMode', optional => true, default => '0750'},
|
|
|
|
{name => 'strDefaultFileMode', optional => true, default => '0640'},
|
|
|
|
);
|
|
|
|
|
2019-06-26 14:24:58 +02:00
|
|
|
# Create C storage object
|
|
|
|
$self->{oStorageC} = pgBackRest::LibC::Storage->new($self->{strType}, $self->{strPath});
|
2017-06-09 23:51:41 +02:00
|
|
|
|
2019-06-26 14:24:58 +02:00
|
|
|
# Get encryption settings
|
|
|
|
if ($self->{strType} eq '<REPO>')
|
2017-11-06 19:51:12 +02:00
|
|
|
{
|
2019-06-26 14:24:58 +02:00
|
|
|
$self->{strCipherType} = $self->{oStorageC}->cipherType();
|
|
|
|
$self->{strCipherPass} = $self->{oStorageC}->cipherPass();
|
2017-11-06 19:51:12 +02:00
|
|
|
}
|
2017-06-09 23:51:41 +02:00
|
|
|
|
2019-06-26 14:24:58 +02:00
|
|
|
# Create JSON object
|
|
|
|
$self->{oJSON} = JSON::PP->new()->allow_nonref();
|
2017-06-09 23:51:41 +02:00
|
|
|
|
|
|
|
# Return from function and log return values if any
|
|
|
|
return logDebugReturn
|
|
|
|
(
|
|
|
|
$strOperation,
|
|
|
|
{name => 'self', value => $self}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
####################################################################################################################################
|
2019-06-26 14:24:58 +02:00
|
|
|
# Check if file exists (not a path)
|
2017-06-09 23:51:41 +02:00
|
|
|
####################################################################################################################################
|
|
|
|
sub exists
|
|
|
|
{
|
|
|
|
my $self = shift;
|
|
|
|
|
|
|
|
# Assign function parameters, defaults, and log debug info
|
|
|
|
my
|
|
|
|
(
|
|
|
|
$strOperation,
|
|
|
|
$strFileExp,
|
|
|
|
) =
|
|
|
|
logDebugParam
|
|
|
|
(
|
|
|
|
__PACKAGE__ . '->exists', \@_,
|
|
|
|
{name => 'strFileExp'},
|
|
|
|
);
|
|
|
|
|
|
|
|
# Check exists
|
2019-06-26 14:24:58 +02:00
|
|
|
my $bExists = $self->{oStorageC}->exists($strFileExp);
|
|
|
|
|
|
|
|
# Return from function and log return values if any
|
|
|
|
return logDebugReturn
|
|
|
|
(
|
|
|
|
$strOperation,
|
|
|
|
{name => 'bExists', value => $bExists ? true : false}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
####################################################################################################################################
|
|
|
|
# Read a buffer from storage all at once
|
|
|
|
####################################################################################################################################
|
|
|
|
sub get
|
|
|
|
{
|
|
|
|
my $self = shift;
|
|
|
|
|
|
|
|
# Assign function parameters, defaults, and log debug info
|
|
|
|
my
|
|
|
|
(
|
|
|
|
$strOperation,
|
|
|
|
$xFile,
|
|
|
|
$strCipherPass,
|
|
|
|
) =
|
|
|
|
logDebugParam
|
|
|
|
(
|
|
|
|
__PACKAGE__ . '->get', \@_,
|
|
|
|
{name => 'xFile', required => false, trace => true},
|
|
|
|
{name => 'strCipherPass', optional => true, default => $self->cipherPassUser(), redact => true},
|
|
|
|
);
|
|
|
|
|
|
|
|
# Is this an IO object or a file expression? If file expression, then open the file and pass passphrase if one is defined or
|
|
|
|
# if the repo has a user passphrase defined - else pass undef
|
|
|
|
my $oFileIo = defined($xFile) ? (ref($xFile) ? $xFile : $self->openRead($xFile, {strCipherPass => $strCipherPass})) : undef;
|
|
|
|
|
|
|
|
# Get the file contents
|
|
|
|
my $bEmpty = false;
|
|
|
|
my $tContent = $self->{oStorageC}->get($oFileIo->{oStorageCRead});
|
|
|
|
|
|
|
|
if (defined($tContent) && length($tContent) == 0)
|
|
|
|
{
|
|
|
|
$tContent = undef;
|
|
|
|
$bEmpty = true;
|
|
|
|
}
|
2017-06-09 23:51:41 +02:00
|
|
|
|
|
|
|
# Return from function and log return values if any
|
|
|
|
return logDebugReturn
|
|
|
|
(
|
|
|
|
$strOperation,
|
2019-06-26 14:24:58 +02:00
|
|
|
{name => 'rtContent', value => defined($tContent) || $bEmpty ? \$tContent : undef, trace => true},
|
2017-06-09 23:51:41 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
####################################################################################################################################
|
2019-06-26 14:24:58 +02:00
|
|
|
# Calculate sha1 hash and size of file. If special encryption settings are required, then the file objects from openRead/openWrite
|
|
|
|
# must be passed instead of file names.
|
2017-06-09 23:51:41 +02:00
|
|
|
####################################################################################################################################
|
|
|
|
sub hashSize
|
|
|
|
{
|
|
|
|
my $self = shift;
|
|
|
|
|
|
|
|
# Assign function parameters, defaults, and log debug info
|
|
|
|
my
|
|
|
|
(
|
|
|
|
$strOperation,
|
|
|
|
$xFileExp,
|
2018-09-18 17:39:48 +02:00
|
|
|
$bIgnoreMissing,
|
2017-06-09 23:51:41 +02:00
|
|
|
) =
|
|
|
|
logDebugParam
|
|
|
|
(
|
|
|
|
__PACKAGE__ . '->hashSize', \@_,
|
|
|
|
{name => 'xFileExp'},
|
2018-09-18 17:39:48 +02:00
|
|
|
{name => 'bIgnoreMissing', optional => true, default => false},
|
2017-06-09 23:51:41 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
# Set operation variables
|
|
|
|
my $strHash;
|
|
|
|
my $lSize;
|
|
|
|
|
|
|
|
# Is this an IO object or a file expression?
|
2019-06-26 14:24:58 +02:00
|
|
|
my $oFileIo = ref($xFileExp) ? $xFileExp : $self->openRead($xFileExp, {bIgnoreMissing => $bIgnoreMissing});
|
2017-06-09 23:51:41 +02:00
|
|
|
|
2019-06-26 14:24:58 +02:00
|
|
|
# Add size and sha filters
|
|
|
|
$oFileIo->{oStorageCRead}->filterAdd(COMMON_IO_HANDLE, undef);
|
|
|
|
$oFileIo->{oStorageCRead}->filterAdd(STORAGE_FILTER_SHA, undef);
|
2017-06-09 23:51:41 +02:00
|
|
|
|
2019-06-26 14:24:58 +02:00
|
|
|
# Read the file and set results if it exists
|
|
|
|
if ($self->{oStorageC}->readDrain($oFileIo->{oStorageCRead}))
|
|
|
|
{
|
|
|
|
$strHash = $oFileIo->result(STORAGE_FILTER_SHA);
|
|
|
|
$lSize = $oFileIo->result(COMMON_IO_HANDLE);
|
2017-06-09 23:51:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
# Return from function and log return values if any
|
|
|
|
return logDebugReturn
|
|
|
|
(
|
|
|
|
$strOperation,
|
|
|
|
{name => 'strHash', value => $strHash},
|
|
|
|
{name => 'lSize', value => $lSize}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
####################################################################################################################################
|
2019-06-26 14:24:58 +02:00
|
|
|
# Get information for path/file
|
2017-06-09 23:51:41 +02:00
|
|
|
####################################################################################################################################
|
|
|
|
sub info
|
|
|
|
{
|
|
|
|
my $self = shift;
|
|
|
|
|
|
|
|
# Assign function parameters, defaults, and log debug info
|
|
|
|
my
|
|
|
|
(
|
|
|
|
$strOperation,
|
|
|
|
$strPathFileExp,
|
|
|
|
$bIgnoreMissing,
|
|
|
|
) =
|
|
|
|
logDebugParam
|
|
|
|
(
|
2019-06-26 14:24:58 +02:00
|
|
|
__PACKAGE__ . '->info', \@_,
|
2017-06-09 23:51:41 +02:00
|
|
|
{name => 'strPathFileExp'},
|
2017-07-25 18:44:38 +02:00
|
|
|
{name => 'bIgnoreMissing', optional => true, default => false},
|
2017-06-09 23:51:41 +02:00
|
|
|
);
|
|
|
|
|
2019-06-26 14:24:58 +02:00
|
|
|
my $rhInfo;
|
|
|
|
my $strJson = $self->{oStorageC}->info($strPathFileExp, $bIgnoreMissing);
|
|
|
|
|
|
|
|
if (defined($strJson))
|
|
|
|
{
|
|
|
|
$rhInfo = $self->{oJSON}->decode($strJson);
|
|
|
|
}
|
2017-06-09 23:51:41 +02:00
|
|
|
|
|
|
|
# Return from function and log return values if any
|
|
|
|
return logDebugReturn
|
|
|
|
(
|
|
|
|
$strOperation,
|
2019-06-26 14:24:58 +02:00
|
|
|
{name => 'rhInfo', value => $rhInfo, trace => true}
|
2017-06-09 23:51:41 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
####################################################################################################################################
|
|
|
|
# linkCreate - create a link
|
|
|
|
####################################################################################################################################
|
|
|
|
sub linkCreate
|
|
|
|
{
|
|
|
|
my $self = shift;
|
|
|
|
|
|
|
|
# Assign function parameters, defaults, and log debug info
|
|
|
|
my
|
|
|
|
(
|
|
|
|
$strOperation,
|
|
|
|
$strSourcePathFileExp,
|
|
|
|
$strDestinationLinkExp,
|
|
|
|
$bHard,
|
|
|
|
$bRelative,
|
|
|
|
$bPathCreate,
|
|
|
|
$bIgnoreExists,
|
|
|
|
) =
|
|
|
|
logDebugParam
|
|
|
|
(
|
|
|
|
__PACKAGE__ . '->linkCreate', \@_,
|
|
|
|
{name => 'strSourcePathFileExp'},
|
|
|
|
{name => 'strDestinationLinkExp'},
|
|
|
|
{name => 'bHard', optional=> true, default => false},
|
|
|
|
{name => 'bRelative', optional=> true, default => false},
|
|
|
|
{name => 'bPathCreate', optional=> true, default => true},
|
|
|
|
{name => 'bIgnoreExists', optional => true, default => false},
|
|
|
|
);
|
|
|
|
|
|
|
|
# Get source and destination paths
|
|
|
|
my $strSourcePathFile = $self->pathGet($strSourcePathFileExp);
|
|
|
|
my $strDestinationLink = $self->pathGet($strDestinationLinkExp);
|
|
|
|
|
|
|
|
# Generate relative path if requested
|
|
|
|
if ($bRelative)
|
|
|
|
{
|
|
|
|
# Determine how much of the paths are common
|
|
|
|
my @strySource = split('/', $strSourcePathFile);
|
|
|
|
my @stryDestination = split('/', $strDestinationLink);
|
|
|
|
|
|
|
|
while (defined($strySource[0]) && defined($stryDestination[0]) && $strySource[0] eq $stryDestination[0])
|
|
|
|
{
|
|
|
|
shift(@strySource);
|
|
|
|
shift(@stryDestination);
|
|
|
|
}
|
|
|
|
|
|
|
|
# Add relative path sections
|
|
|
|
$strSourcePathFile = '';
|
|
|
|
|
|
|
|
for (my $iIndex = 0; $iIndex < @stryDestination - 1; $iIndex++)
|
|
|
|
{
|
|
|
|
$strSourcePathFile .= '../';
|
|
|
|
}
|
|
|
|
|
|
|
|
# Add path to source
|
|
|
|
$strSourcePathFile .= join('/', @strySource);
|
|
|
|
|
|
|
|
logDebugMisc
|
|
|
|
(
|
|
|
|
$strOperation, 'apply relative path',
|
|
|
|
{name => 'strSourcePathFile', value => $strSourcePathFile, trace => true}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-06-26 14:24:58 +02:00
|
|
|
if (!($bHard ? link($strSourcePathFile, $strDestinationLink) : symlink($strSourcePathFile, $strDestinationLink)))
|
|
|
|
{
|
|
|
|
my $strMessage = "unable to create link '${strDestinationLink}'";
|
|
|
|
|
|
|
|
# If parent path or source is missing
|
|
|
|
if ($OS_ERROR{ENOENT})
|
|
|
|
{
|
|
|
|
# Check if source is missing
|
|
|
|
if (!$self->exists($strSourcePathFile))
|
|
|
|
{
|
|
|
|
confess &log(ERROR, "${strMessage} because source '${strSourcePathFile}' does not exist", ERROR_FILE_MISSING);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$bPathCreate)
|
|
|
|
{
|
|
|
|
confess &log(ERROR, "${strMessage} because parent does not exist", ERROR_PATH_MISSING);
|
|
|
|
}
|
|
|
|
|
|
|
|
# Create parent path
|
|
|
|
$self->pathCreate(dirname($strDestinationLink), {bIgnoreExists => true, bCreateParent => true});
|
|
|
|
|
|
|
|
# Create link
|
|
|
|
$self->linkCreate($strSourcePathFile, $strDestinationLink, {bHard => $bHard});
|
|
|
|
}
|
|
|
|
# Else if link already exists
|
|
|
|
elsif ($OS_ERROR{EEXIST})
|
|
|
|
{
|
|
|
|
if (!$bIgnoreExists)
|
|
|
|
{
|
|
|
|
confess &log(ERROR, "${strMessage} because it already exists", ERROR_PATH_EXISTS);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
logErrorResult(ERROR_PATH_CREATE, ${strMessage}, $OS_ERROR);
|
|
|
|
}
|
|
|
|
}
|
2017-06-09 23:51:41 +02:00
|
|
|
|
|
|
|
# Return from function and log return values if any
|
|
|
|
return logDebugReturn($strOperation);
|
|
|
|
}
|
|
|
|
|
|
|
|
####################################################################################################################################
|
2019-06-26 14:24:58 +02:00
|
|
|
# List all files/paths in path
|
2017-06-09 23:51:41 +02:00
|
|
|
####################################################################################################################################
|
|
|
|
sub list
|
|
|
|
{
|
|
|
|
my $self = shift;
|
|
|
|
|
|
|
|
# Assign function parameters, defaults, and log debug info
|
|
|
|
my
|
|
|
|
(
|
|
|
|
$strOperation,
|
|
|
|
$strPathExp,
|
|
|
|
$strExpression,
|
|
|
|
$strSortOrder,
|
|
|
|
$bIgnoreMissing,
|
|
|
|
) =
|
|
|
|
logDebugParam
|
|
|
|
(
|
|
|
|
__PACKAGE__ . '->list', \@_,
|
2019-01-16 18:49:12 +02:00
|
|
|
{name => 'strPathExp', required => false},
|
2017-06-09 23:51:41 +02:00
|
|
|
{name => 'strExpression', optional => true},
|
|
|
|
{name => 'strSortOrder', optional => true, default => 'forward'},
|
|
|
|
{name => 'bIgnoreMissing', optional => true, default => false},
|
|
|
|
);
|
|
|
|
|
|
|
|
# Get file list
|
2019-06-26 14:24:58 +02:00
|
|
|
my $rstryFileList = [];
|
|
|
|
my $strFileList = $self->{oStorageC}->list($strPathExp, $bIgnoreMissing, $strSortOrder eq 'forward', $strExpression);
|
2017-06-09 23:51:41 +02:00
|
|
|
|
2019-06-26 14:24:58 +02:00
|
|
|
if (defined($strFileList) && $strFileList ne '[]')
|
2017-06-09 23:51:41 +02:00
|
|
|
{
|
2019-06-26 14:24:58 +02:00
|
|
|
$rstryFileList = $self->{oJSON}->decode($strFileList);
|
2017-06-09 23:51:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
# Return from function and log return values if any
|
|
|
|
return logDebugReturn
|
|
|
|
(
|
|
|
|
$strOperation,
|
|
|
|
{name => 'stryFileList', value => $rstryFileList}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
####################################################################################################################################
|
2019-06-26 14:24:58 +02:00
|
|
|
# Build path/file/link manifest starting with base path and including all subpaths
|
2017-06-09 23:51:41 +02:00
|
|
|
####################################################################################################################################
|
|
|
|
sub manifest
|
|
|
|
{
|
|
|
|
my $self = shift;
|
|
|
|
|
|
|
|
# Assign function parameters, defaults, and log debug info
|
|
|
|
my
|
|
|
|
(
|
|
|
|
$strOperation,
|
|
|
|
$strPathExp,
|
2018-08-31 22:06:40 +02:00
|
|
|
$strFilter,
|
2017-06-09 23:51:41 +02:00
|
|
|
) =
|
|
|
|
logDebugParam
|
|
|
|
(
|
|
|
|
__PACKAGE__ . '->manifest', \@_,
|
|
|
|
{name => 'strPathExp'},
|
2018-08-31 22:06:40 +02:00
|
|
|
{name => 'strFilter', optional => true, trace => true},
|
2017-06-09 23:51:41 +02:00
|
|
|
);
|
|
|
|
|
2019-06-26 14:24:58 +02:00
|
|
|
my $hManifest = $self->{oJSON}->decode($self->{oStorageC}->manifest($strPathExp, $strFilter));
|
2017-06-09 23:51:41 +02:00
|
|
|
|
|
|
|
# Return from function and log return values if any
|
|
|
|
return logDebugReturn
|
|
|
|
(
|
|
|
|
$strOperation,
|
|
|
|
{name => 'hManifest', value => $hManifest, trace => true}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
####################################################################################################################################
|
|
|
|
# move - move path/file
|
|
|
|
####################################################################################################################################
|
|
|
|
sub move
|
|
|
|
{
|
|
|
|
my $self = shift;
|
|
|
|
|
|
|
|
# Assign function parameters, defaults, and log debug info
|
|
|
|
my
|
|
|
|
(
|
|
|
|
$strOperation,
|
2019-06-26 14:24:58 +02:00
|
|
|
$strSourceFileExp,
|
|
|
|
$strDestinationFileExp,
|
2017-06-09 23:51:41 +02:00
|
|
|
$bPathCreate,
|
|
|
|
) =
|
|
|
|
logDebugParam
|
|
|
|
(
|
|
|
|
__PACKAGE__ . '->move', \@_,
|
2019-06-26 14:24:58 +02:00
|
|
|
{name => 'strSourceFileExp'},
|
|
|
|
{name => 'strDestinationFileExp'},
|
2017-06-09 23:51:41 +02:00
|
|
|
);
|
|
|
|
|
2019-06-26 14:24:58 +02:00
|
|
|
# Get source and destination paths
|
|
|
|
my $strSourceFile = $self->pathGet($strSourceFileExp);
|
|
|
|
my $strDestinationFile = $self->pathGet($strDestinationFileExp);
|
|
|
|
|
|
|
|
# Move the file
|
|
|
|
if (!rename($strSourceFile, $strDestinationFile))
|
|
|
|
{
|
|
|
|
logErrorResult(ERROR_FILE_MOVE, "unable to move '${strSourceFile}' to '${strDestinationFile}'", $OS_ERROR);
|
|
|
|
}
|
2017-06-09 23:51:41 +02:00
|
|
|
|
|
|
|
# Return from function and log return values if any
|
2019-06-26 14:24:58 +02:00
|
|
|
return logDebugReturn($strOperation);
|
2017-06-09 23:51:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
####################################################################################################################################
|
2019-06-26 14:24:58 +02:00
|
|
|
# Open file for reading
|
2017-06-09 23:51:41 +02:00
|
|
|
####################################################################################################################################
|
|
|
|
sub openRead
|
|
|
|
{
|
|
|
|
my $self = shift;
|
|
|
|
|
|
|
|
# Assign function parameters, defaults, and log debug info
|
|
|
|
my
|
|
|
|
(
|
|
|
|
$strOperation,
|
|
|
|
$xFileExp,
|
|
|
|
$bIgnoreMissing,
|
|
|
|
$rhyFilter,
|
2017-11-06 19:51:12 +02:00
|
|
|
$strCipherPass,
|
2017-06-09 23:51:41 +02:00
|
|
|
) =
|
|
|
|
logDebugParam
|
|
|
|
(
|
|
|
|
__PACKAGE__ . '->openRead', \@_,
|
|
|
|
{name => 'xFileExp'},
|
|
|
|
{name => 'bIgnoreMissing', optional => true, default => false},
|
|
|
|
{name => 'rhyFilter', optional => true},
|
2019-06-26 14:24:58 +02:00
|
|
|
{name => 'strCipherPass', optional => true, default => $self->cipherPassUser(), redact => true},
|
2017-06-09 23:51:41 +02:00
|
|
|
);
|
|
|
|
|
2017-11-06 19:51:12 +02:00
|
|
|
# Open the file
|
2019-06-26 14:24:58 +02:00
|
|
|
my $oFileIo = pgBackRest::LibC::StorageRead->new($self->{oStorageC}, $xFileExp, $bIgnoreMissing);
|
2017-06-09 23:51:41 +02:00
|
|
|
|
2019-06-26 14:24:58 +02:00
|
|
|
# If cipher is set then decryption is the first filter applied to the read
|
|
|
|
if (defined($self->cipherType()))
|
2017-06-09 23:51:41 +02:00
|
|
|
{
|
2019-06-26 14:24:58 +02:00
|
|
|
$oFileIo->filterAdd(STORAGE_FILTER_CIPHER_BLOCK, $self->{oJSON}->encode([false, $self->cipherType(), $strCipherPass]));
|
|
|
|
}
|
2017-11-06 19:51:12 +02:00
|
|
|
|
2019-06-26 14:24:58 +02:00
|
|
|
# Apply any other filters
|
|
|
|
if (defined($rhyFilter))
|
|
|
|
{
|
|
|
|
foreach my $rhFilter (@{$rhyFilter})
|
2017-11-06 19:51:12 +02:00
|
|
|
{
|
2019-06-26 14:24:58 +02:00
|
|
|
$oFileIo->filterAdd(
|
|
|
|
$rhFilter->{strClass}, defined($rhFilter->{rxyParam}) ? $self->{oJSON}->encode($rhFilter->{rxyParam}) : undef);
|
2017-06-09 23:51:41 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
# Return from function and log return values if any
|
|
|
|
return logDebugReturn
|
|
|
|
(
|
|
|
|
$strOperation,
|
2019-06-26 14:24:58 +02:00
|
|
|
{name => 'oFileIo', value => new pgBackRest::Storage::StorageRead($self, $oFileIo), trace => true},
|
2017-06-09 23:51:41 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
####################################################################################################################################
|
2019-06-26 14:24:58 +02:00
|
|
|
# Open file for writing
|
2017-06-09 23:51:41 +02:00
|
|
|
####################################################################################################################################
|
|
|
|
sub openWrite
|
|
|
|
{
|
|
|
|
my $self = shift;
|
|
|
|
|
|
|
|
# Assign function parameters, defaults, and log debug info
|
|
|
|
my
|
|
|
|
(
|
|
|
|
$strOperation,
|
|
|
|
$xFileExp,
|
|
|
|
$strMode,
|
|
|
|
$strUser,
|
|
|
|
$strGroup,
|
|
|
|
$lTimestamp,
|
|
|
|
$bAtomic,
|
|
|
|
$bPathCreate,
|
|
|
|
$rhyFilter,
|
2017-11-06 19:51:12 +02:00
|
|
|
$strCipherPass,
|
2017-06-09 23:51:41 +02:00
|
|
|
) =
|
|
|
|
logDebugParam
|
|
|
|
(
|
|
|
|
__PACKAGE__ . '->openWrite', \@_,
|
|
|
|
{name => 'xFileExp'},
|
|
|
|
{name => 'strMode', optional => true, default => $self->{strDefaultFileMode}},
|
|
|
|
{name => 'strUser', optional => true},
|
|
|
|
{name => 'strGroup', optional => true},
|
2019-06-26 14:24:58 +02:00
|
|
|
{name => 'lTimestamp', optional => true, default => '0'},
|
2017-06-09 23:51:41 +02:00
|
|
|
{name => 'bAtomic', optional => true, default => false},
|
|
|
|
{name => 'bPathCreate', optional => true, default => false},
|
|
|
|
{name => 'rhyFilter', optional => true},
|
2019-06-26 14:24:58 +02:00
|
|
|
{name => 'strCipherPass', optional => true, default => $self->cipherPassUser(), redact => true},
|
2017-06-09 23:51:41 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
# Open the file
|
2019-06-26 14:24:58 +02:00
|
|
|
my $oFileIo = pgBackRest::LibC::StorageWrite->new(
|
|
|
|
$self->{oStorageC}, $xFileExp, oct($strMode), $strUser, $strGroup, $lTimestamp, $bAtomic, $bPathCreate);
|
2017-11-06 19:51:12 +02:00
|
|
|
|
|
|
|
# Apply any other filters
|
2017-06-09 23:51:41 +02:00
|
|
|
if (defined($rhyFilter))
|
|
|
|
{
|
2019-06-26 14:24:58 +02:00
|
|
|
foreach my $rhFilter (@{$rhyFilter})
|
2017-06-09 23:51:41 +02:00
|
|
|
{
|
2019-06-26 14:24:58 +02:00
|
|
|
$oFileIo->filterAdd(
|
|
|
|
$rhFilter->{strClass}, defined($rhFilter->{rxyParam}) ? $self->{oJSON}->encode($rhFilter->{rxyParam}) : undef);
|
2017-06-09 23:51:41 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-26 14:24:58 +02:00
|
|
|
# If cipher is set then encryption is the last filter applied to the write
|
|
|
|
if (defined($self->cipherType()))
|
|
|
|
{
|
|
|
|
$oFileIo->filterAdd(STORAGE_FILTER_CIPHER_BLOCK, $self->{oJSON}->encode([true, $self->cipherType(), $strCipherPass]));
|
|
|
|
}
|
|
|
|
|
2017-06-09 23:51:41 +02:00
|
|
|
# Return from function and log return values if any
|
|
|
|
return logDebugReturn
|
|
|
|
(
|
|
|
|
$strOperation,
|
2019-06-26 14:24:58 +02:00
|
|
|
{name => 'oFileIo', value => new pgBackRest::Storage::StorageWrite($self, $oFileIo), trace => true},
|
2017-06-09 23:51:41 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
####################################################################################################################################
|
2019-06-26 14:24:58 +02:00
|
|
|
# Change ownership of path/file
|
2017-06-09 23:51:41 +02:00
|
|
|
####################################################################################################################################
|
|
|
|
sub owner
|
|
|
|
{
|
|
|
|
my $self = shift;
|
|
|
|
|
|
|
|
# Assign function parameters, defaults, and log debug info
|
|
|
|
my
|
|
|
|
(
|
|
|
|
$strOperation,
|
|
|
|
$strPathFileExp,
|
|
|
|
$strUser,
|
|
|
|
$strGroup
|
|
|
|
) =
|
|
|
|
logDebugParam
|
|
|
|
(
|
|
|
|
__PACKAGE__ . '->owner', \@_,
|
|
|
|
{name => 'strPathFileExp'},
|
|
|
|
{name => 'strUser', required => false},
|
|
|
|
{name => 'strGroup', required => false}
|
|
|
|
);
|
|
|
|
|
2019-06-26 14:24:58 +02:00
|
|
|
# Only proceed if user or group was specified
|
|
|
|
if (defined($strUser) || defined($strGroup))
|
|
|
|
{
|
|
|
|
my $strPathFile = $self->pathGet($strPathFileExp);
|
|
|
|
my $strMessage = "unable to set ownership for '${strPathFile}'";
|
|
|
|
my $iUserId;
|
|
|
|
my $iGroupId;
|
|
|
|
|
|
|
|
# If the user or group is not defined then get it by stat'ing the file. This is because the chown function requires that
|
|
|
|
# both user and group be set.
|
|
|
|
my $oStat = lstat($strPathFile);
|
|
|
|
|
|
|
|
if (!defined($oStat))
|
|
|
|
{
|
|
|
|
confess &log(ERROR, "unable to stat '${strPathFile}': No such file or directory", ERROR_FILE_MISSING);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!defined($strUser))
|
|
|
|
{
|
|
|
|
$iUserId = $oStat->uid;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!defined($strGroup))
|
|
|
|
{
|
|
|
|
$iGroupId = $oStat->gid;
|
|
|
|
}
|
|
|
|
|
|
|
|
# Lookup user if specified
|
|
|
|
if (defined($strUser))
|
|
|
|
{
|
|
|
|
$iUserId = getpwnam($strUser);
|
|
|
|
|
|
|
|
if (!defined($iUserId))
|
|
|
|
{
|
|
|
|
logErrorResult(ERROR_FILE_OWNER, "${strMessage} because user '${strUser}' does not exist");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
# Lookup group if specified
|
|
|
|
if (defined($strGroup))
|
|
|
|
{
|
|
|
|
$iGroupId = getgrnam($strGroup);
|
|
|
|
|
|
|
|
if (!defined($iGroupId))
|
|
|
|
{
|
|
|
|
logErrorResult(ERROR_FILE_OWNER, "${strMessage} because group '${strGroup}' does not exist");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
# Set ownership on the file if the user or group would be changed
|
|
|
|
if ($iUserId != $oStat->uid || $iGroupId != $oStat->gid)
|
|
|
|
{
|
|
|
|
if (!chown($iUserId, $iGroupId, $strPathFile))
|
|
|
|
{
|
|
|
|
logErrorResult(ERROR_FILE_OWNER, "${strMessage}", $OS_ERROR);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-06-09 23:51:41 +02:00
|
|
|
|
|
|
|
# Return from function and log return values if any
|
|
|
|
return logDebugReturn
|
|
|
|
(
|
|
|
|
$strOperation
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
####################################################################################################################################
|
2019-06-26 14:24:58 +02:00
|
|
|
# Generate an absolute path from an absolute base path and a relative path
|
|
|
|
####################################################################################################################################
|
|
|
|
sub pathAbsolute
|
|
|
|
{
|
|
|
|
my $self = shift;
|
|
|
|
|
|
|
|
# Assign function parameters, defaults, and log debug info
|
|
|
|
my
|
|
|
|
(
|
|
|
|
$strOperation,
|
|
|
|
$strBasePath,
|
|
|
|
$strPath
|
|
|
|
) =
|
|
|
|
logDebugParam
|
|
|
|
(
|
|
|
|
__PACKAGE__ . '->pathAbsolute', \@_,
|
|
|
|
{name => 'strBasePath', trace => true},
|
|
|
|
{name => 'strPath', trace => true}
|
|
|
|
);
|
|
|
|
|
|
|
|
# Working variables
|
|
|
|
my $strAbsolutePath;
|
|
|
|
|
|
|
|
# If the path is already absolute
|
|
|
|
if (index($strPath, '/') == 0)
|
|
|
|
{
|
|
|
|
$strAbsolutePath = $strPath;
|
|
|
|
}
|
|
|
|
# Else make it absolute using the base path
|
|
|
|
else
|
|
|
|
{
|
|
|
|
# Make sure the absolute path is really absolute
|
|
|
|
if (index($strBasePath, '/') != 0 || index($strBasePath, '/..') != -1)
|
|
|
|
{
|
|
|
|
confess &log(ERROR, "${strBasePath} is not an absolute path", ERROR_PATH_TYPE);
|
|
|
|
}
|
|
|
|
|
|
|
|
while (index($strPath, '..') == 0)
|
|
|
|
{
|
|
|
|
$strBasePath = dirname($strBasePath);
|
|
|
|
$strPath = substr($strPath, 2);
|
|
|
|
|
|
|
|
if (index($strPath, '/') == 0)
|
|
|
|
{
|
|
|
|
$strPath = substr($strPath, 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$strAbsolutePath = "${strBasePath}/${strPath}";
|
|
|
|
}
|
|
|
|
|
|
|
|
# Make sure the result is really an absolute path
|
|
|
|
if (index($strAbsolutePath, '/') != 0 || index($strAbsolutePath, '/..') != -1)
|
|
|
|
{
|
|
|
|
confess &log(ERROR, "result ${strAbsolutePath} was not an absolute path", ERROR_PATH_TYPE);
|
|
|
|
}
|
|
|
|
|
|
|
|
# Return from function and log return values if any
|
|
|
|
return logDebugReturn
|
|
|
|
(
|
|
|
|
$strOperation,
|
|
|
|
{name => 'strAbsolutePath', value => $strAbsolutePath, trace => true}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
####################################################################################################################################
|
|
|
|
# Create a path
|
2017-06-09 23:51:41 +02:00
|
|
|
####################################################################################################################################
|
|
|
|
sub pathCreate
|
|
|
|
{
|
|
|
|
my $self = shift;
|
|
|
|
|
|
|
|
# Assign function parameters, defaults, and log debug info
|
|
|
|
my
|
|
|
|
(
|
|
|
|
$strOperation,
|
|
|
|
$strPathExp,
|
|
|
|
$strMode,
|
|
|
|
$bIgnoreExists,
|
|
|
|
$bCreateParent,
|
|
|
|
) =
|
|
|
|
logDebugParam
|
|
|
|
(
|
|
|
|
__PACKAGE__ . '->pathCreate', \@_,
|
|
|
|
{name => 'strPathExp'},
|
2019-06-26 14:24:58 +02:00
|
|
|
{name => 'strMode', optional => true},
|
2017-06-09 23:51:41 +02:00
|
|
|
{name => 'bIgnoreExists', optional => true, default => false},
|
|
|
|
{name => 'bCreateParent', optional => true, default => false},
|
|
|
|
);
|
|
|
|
|
|
|
|
# Create path
|
2019-06-26 14:24:58 +02:00
|
|
|
$self->{oStorageC}->pathCreate($strPathExp, $strMode, $bIgnoreExists, $bCreateParent);
|
2017-06-09 23:51:41 +02:00
|
|
|
|
|
|
|
# Return from function and log return values if any
|
|
|
|
return logDebugReturn
|
|
|
|
(
|
|
|
|
$strOperation
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
####################################################################################################################################
|
2019-06-26 14:24:58 +02:00
|
|
|
# Check if path exists
|
2017-06-09 23:51:41 +02:00
|
|
|
####################################################################################################################################
|
|
|
|
sub pathExists
|
|
|
|
{
|
|
|
|
my $self = shift;
|
|
|
|
|
|
|
|
# Assign function parameters, defaults, and log debug info
|
|
|
|
my
|
|
|
|
(
|
|
|
|
$strOperation,
|
|
|
|
$strPathExp,
|
|
|
|
) =
|
|
|
|
logDebugParam
|
|
|
|
(
|
|
|
|
__PACKAGE__ . '->pathExists', \@_,
|
|
|
|
{name => 'strPathExp'},
|
|
|
|
);
|
|
|
|
|
|
|
|
# Check exists
|
2019-06-26 14:24:58 +02:00
|
|
|
my $bExists = $self->{oStorageC}->pathExists($strPathExp);
|
2017-06-09 23:51:41 +02:00
|
|
|
|
|
|
|
# Return from function and log return values if any
|
|
|
|
return logDebugReturn
|
|
|
|
(
|
|
|
|
$strOperation,
|
2019-06-26 14:24:58 +02:00
|
|
|
{name => 'bExists', value => $bExists ? true : false}
|
2017-06-09 23:51:41 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
####################################################################################################################################
|
2019-06-26 14:24:58 +02:00
|
|
|
# Resolve a path expression into an absolute path
|
2017-06-09 23:51:41 +02:00
|
|
|
####################################################################################################################################
|
|
|
|
sub pathGet
|
|
|
|
{
|
|
|
|
my $self = shift;
|
|
|
|
|
|
|
|
# Assign function parameters, defaults, and log debug info
|
|
|
|
my
|
|
|
|
(
|
|
|
|
$strOperation,
|
2019-06-26 14:24:58 +02:00
|
|
|
$strPathExp,
|
2017-06-09 23:51:41 +02:00
|
|
|
) =
|
|
|
|
logDebugParam
|
2019-06-26 14:24:58 +02:00
|
|
|
(
|
|
|
|
__PACKAGE__ . '->pathGet', \@_,
|
|
|
|
{name => 'strPathExp'},
|
|
|
|
);
|
2017-06-09 23:51:41 +02:00
|
|
|
|
2019-06-26 14:24:58 +02:00
|
|
|
# Check exists
|
|
|
|
my $strPath = $self->{oStorageC}->pathGet($strPathExp);
|
2017-06-09 23:51:41 +02:00
|
|
|
|
|
|
|
# Return from function and log return values if any
|
|
|
|
return logDebugReturn
|
|
|
|
(
|
|
|
|
$strOperation,
|
|
|
|
{name => 'strPath', value => $strPath, trace => true}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-06-26 14:24:58 +02:00
|
|
|
####################################################################################################################################
|
|
|
|
# Remove path and all files below it
|
|
|
|
####################################################################################################################################
|
|
|
|
sub pathRemove
|
|
|
|
{
|
|
|
|
my $self = shift;
|
|
|
|
|
|
|
|
# Assign function parameters, defaults, and log debug info
|
|
|
|
my
|
|
|
|
(
|
|
|
|
$strOperation,
|
|
|
|
$strPathExp,
|
|
|
|
$bIgnoreMissing,
|
|
|
|
$bRecurse,
|
|
|
|
) =
|
|
|
|
logDebugParam
|
|
|
|
(
|
|
|
|
__PACKAGE__ . '->pathRemove', \@_,
|
|
|
|
{name => 'strPathExp'},
|
|
|
|
{name => 'bIgnoreMissing', optional => true, default => true},
|
|
|
|
{name => 'bRecurse', optional => true, default => false},
|
|
|
|
);
|
|
|
|
|
|
|
|
$self->{oStorageC}->pathRemove($strPathExp, $bIgnoreMissing, $bRecurse);
|
|
|
|
|
|
|
|
# Return from function and log return values if any
|
|
|
|
return logDebugReturn($strOperation);
|
|
|
|
}
|
|
|
|
|
2017-06-09 23:51:41 +02:00
|
|
|
####################################################################################################################################
|
2018-05-01 17:05:37 +02:00
|
|
|
# Sync path so newly added file entries are not lost
|
2017-06-09 23:51:41 +02:00
|
|
|
####################################################################################################################################
|
|
|
|
sub pathSync
|
|
|
|
{
|
|
|
|
my $self = shift;
|
|
|
|
|
|
|
|
# Assign function parameters, defaults, and log debug info
|
|
|
|
my
|
|
|
|
(
|
|
|
|
$strOperation,
|
|
|
|
$strPathExp,
|
|
|
|
) =
|
|
|
|
logDebugParam
|
|
|
|
(
|
|
|
|
__PACKAGE__ . '->pathSync', \@_,
|
|
|
|
{name => 'strPathExp'},
|
|
|
|
);
|
|
|
|
|
2019-06-26 14:24:58 +02:00
|
|
|
$self->{oStorageC}->pathSync($strPathExp);
|
2017-06-09 23:51:41 +02:00
|
|
|
|
|
|
|
# Return from function and log return values if any
|
|
|
|
return logDebugReturn($strOperation);
|
|
|
|
}
|
|
|
|
|
|
|
|
####################################################################################################################################
|
2019-06-26 14:24:58 +02:00
|
|
|
# put - writes a buffer out to storage all at once
|
|
|
|
####################################################################################################################################
|
|
|
|
sub put
|
|
|
|
{
|
|
|
|
my $self = shift;
|
|
|
|
|
|
|
|
# Assign function parameters, defaults, and log debug info
|
|
|
|
my
|
|
|
|
(
|
|
|
|
$strOperation,
|
|
|
|
$xFile,
|
|
|
|
$xContent,
|
|
|
|
$strCipherPass,
|
|
|
|
) =
|
|
|
|
logDebugParam
|
|
|
|
(
|
|
|
|
__PACKAGE__ . '->put', \@_,
|
|
|
|
{name => 'xFile', trace => true},
|
|
|
|
{name => 'xContent', required => false, trace => true},
|
|
|
|
{name => 'strCipherPass', optional => true, default => $self->cipherPassUser(), trace => true, redact => true},
|
|
|
|
);
|
|
|
|
|
|
|
|
# Is this an IO object or a file expression? If file expression, then open the file and pass passphrase if one is defined or if
|
|
|
|
# the repo has a user passphrase defined - else pass undef
|
|
|
|
my $oFileIo = ref($xFile) ? $xFile : $self->openWrite($xFile, {strCipherPass => $strCipherPass});
|
|
|
|
|
|
|
|
# Write the content
|
|
|
|
my $lSize = $self->{oStorageC}->put($oFileIo->{oStorageCWrite}, ref($xContent) ? $$xContent : $xContent);
|
|
|
|
|
|
|
|
# Return from function and log return values if any
|
|
|
|
return logDebugReturn
|
|
|
|
(
|
|
|
|
$strOperation,
|
|
|
|
{name => 'lSize', value => $lSize, trace => true},
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
####################################################################################################################################
|
|
|
|
# Remove file
|
2017-06-09 23:51:41 +02:00
|
|
|
####################################################################################################################################
|
|
|
|
sub remove
|
|
|
|
{
|
|
|
|
my $self = shift;
|
|
|
|
|
|
|
|
# Assign function parameters, defaults, and log debug info
|
|
|
|
my
|
|
|
|
(
|
|
|
|
$strOperation,
|
2019-06-26 14:24:58 +02:00
|
|
|
$xFileExp,
|
2017-06-09 23:51:41 +02:00
|
|
|
$bIgnoreMissing,
|
|
|
|
) =
|
|
|
|
logDebugParam
|
|
|
|
(
|
|
|
|
__PACKAGE__ . '->remove', \@_,
|
2019-06-26 14:24:58 +02:00
|
|
|
{name => 'xFileExp'},
|
2017-06-09 23:51:41 +02:00
|
|
|
{name => 'bIgnoreMissing', optional => true, default => true},
|
|
|
|
);
|
|
|
|
|
2019-06-26 14:24:58 +02:00
|
|
|
foreach my $strFileExp (ref($xFileExp) ? @{$xFileExp} : ($xFileExp))
|
2017-06-09 23:51:41 +02:00
|
|
|
{
|
2019-06-26 14:24:58 +02:00
|
|
|
$self->{oStorageC}->remove($strFileExp, $bIgnoreMissing);
|
2017-06-09 23:51:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
# Return from function and log return values if any
|
2019-06-26 14:24:58 +02:00
|
|
|
return logDebugReturn($strOperation);
|
2017-06-09 23:51:41 +02:00
|
|
|
}
|
|
|
|
|
2017-11-06 19:51:12 +02:00
|
|
|
####################################################################################################################################
|
|
|
|
# encrypted - determine if the file is encrypted or not
|
|
|
|
####################################################################################################################################
|
|
|
|
sub encrypted
|
|
|
|
{
|
|
|
|
my $self = shift;
|
|
|
|
|
|
|
|
# Assign function parameters, defaults, and log debug info
|
|
|
|
my
|
|
|
|
(
|
|
|
|
$strOperation,
|
2019-06-26 14:24:58 +02:00
|
|
|
$strFileExp,
|
2017-11-06 19:51:12 +02:00
|
|
|
$bIgnoreMissing,
|
|
|
|
) =
|
|
|
|
logDebugParam
|
|
|
|
(
|
|
|
|
__PACKAGE__ . '->encrypted', \@_,
|
2019-06-26 14:24:58 +02:00
|
|
|
{name => 'strFileExp'},
|
2017-11-06 19:51:12 +02:00
|
|
|
{name => 'bIgnoreMissing', optional => true, default => false},
|
|
|
|
);
|
|
|
|
|
|
|
|
my $bEncrypted = false;
|
|
|
|
|
|
|
|
# Open the file via the driver
|
2019-06-26 14:24:58 +02:00
|
|
|
my $oFileIo = new pgBackRest::Storage::StorageRead(
|
|
|
|
$self, pgBackRest::LibC::StorageRead->new($self->{oStorageC}, $strFileExp, $bIgnoreMissing));
|
2017-11-06 19:51:12 +02:00
|
|
|
|
|
|
|
# If the file does not exist because we're ignoring missing (else it would error before this is executed) then determine if it
|
|
|
|
# should be encrypted based on the repo
|
2019-06-26 14:24:58 +02:00
|
|
|
if (!$oFileIo->open())
|
2017-11-06 19:51:12 +02:00
|
|
|
{
|
2019-06-26 14:24:58 +02:00
|
|
|
if (defined($self->cipherType()))
|
2017-11-06 19:51:12 +02:00
|
|
|
{
|
|
|
|
$bEncrypted = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
# If the file does exist, then read the magic signature
|
2019-06-26 14:24:58 +02:00
|
|
|
my $tMagicSignature = '';
|
|
|
|
my $lSizeRead = $oFileIo->read(\$tMagicSignature, length(CIPHER_MAGIC));
|
|
|
|
$oFileIo->close();
|
2017-11-06 19:51:12 +02:00
|
|
|
|
2019-06-26 14:24:58 +02:00
|
|
|
if (substr($tMagicSignature, 0, length(CIPHER_MAGIC)) eq CIPHER_MAGIC)
|
2017-11-06 19:51:12 +02:00
|
|
|
{
|
|
|
|
$bEncrypted = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
# Return from function and log return values if any
|
|
|
|
return logDebugReturn
|
|
|
|
(
|
|
|
|
$strOperation,
|
|
|
|
{name => 'bEncrypted', value => $bEncrypted}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
####################################################################################################################################
|
2019-08-26 18:05:36 +02:00
|
|
|
# encryptionValid - determine if encryption set properly based on the value passed
|
2017-11-06 19:51:12 +02:00
|
|
|
####################################################################################################################################
|
|
|
|
sub encryptionValid
|
|
|
|
{
|
|
|
|
my $self = shift;
|
|
|
|
|
|
|
|
# Assign function parameters, defaults, and log debug info
|
|
|
|
my
|
|
|
|
(
|
|
|
|
$strOperation,
|
|
|
|
$bEncrypted,
|
|
|
|
) =
|
|
|
|
logDebugParam
|
|
|
|
(
|
|
|
|
__PACKAGE__ . '->encryptionValid', \@_,
|
|
|
|
{name => 'bEncrypted'},
|
|
|
|
);
|
|
|
|
|
2019-06-26 14:24:58 +02:00
|
|
|
my $bValid = ($bEncrypted && defined($self->cipherType())) || (!$bEncrypted && !defined($self->cipherType()));
|
2017-11-06 19:51:12 +02:00
|
|
|
|
|
|
|
# Return from function and log return values if any
|
|
|
|
return logDebugReturn
|
|
|
|
(
|
|
|
|
$strOperation,
|
2019-06-26 14:24:58 +02:00
|
|
|
{name => 'bValid', value => $bValid ? true : false}
|
2017-11-06 19:51:12 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2017-06-09 23:51:41 +02:00
|
|
|
####################################################################################################################################
|
|
|
|
# Getters
|
|
|
|
####################################################################################################################################
|
2019-06-26 14:24:58 +02:00
|
|
|
sub capability {shift->type() eq STORAGE_POSIX}
|
|
|
|
sub type {shift->{oStorageC}->type()}
|
2017-11-06 19:51:12 +02:00
|
|
|
sub cipherType {shift->{strCipherType}}
|
2019-06-26 14:24:58 +02:00
|
|
|
sub cipherPassUser {shift->{strCipherPass}}
|
2017-06-09 23:51:41 +02:00
|
|
|
|
|
|
|
1;
|