2014-02-03 03:03:05 +03:00
|
|
|
####################################################################################################################################
|
|
|
|
# FILE MODULE
|
|
|
|
####################################################################################################################################
|
2016-04-14 15:30:54 +02:00
|
|
|
package pgBackRest::File;
|
2014-02-03 03:03:05 +03:00
|
|
|
|
|
|
|
use strict;
|
2015-03-03 07:57:20 +02:00
|
|
|
use warnings FATAL => qw(all);
|
|
|
|
use Carp qw(confess);
|
2016-09-06 15:44:50 +02:00
|
|
|
use English '-no_match_vars';
|
2014-06-02 23:26:37 +03:00
|
|
|
|
2015-06-14 00:25:49 +02:00
|
|
|
use Exporter qw(import);
|
2015-08-29 20:20:46 +02:00
|
|
|
our @EXPORT = qw();
|
2016-01-21 04:28:40 +02:00
|
|
|
use Fcntl qw(:mode :flock O_RDONLY O_WRONLY O_CREAT);
|
2015-01-03 23:49:26 +02:00
|
|
|
use File::Basename qw(dirname basename);
|
2014-06-21 18:05:29 +03:00
|
|
|
use File::Copy qw(cp);
|
2014-06-22 23:05:00 +03:00
|
|
|
use File::Path qw(make_path remove_tree);
|
2014-06-02 23:26:37 +03:00
|
|
|
use File::stat;
|
2015-06-17 18:53:33 +02:00
|
|
|
use IO::Handle;
|
2014-02-03 03:03:05 +03:00
|
|
|
|
2016-04-14 15:30:54 +02:00
|
|
|
use pgBackRest::Common::Exception;
|
|
|
|
use pgBackRest::Common::Log;
|
|
|
|
use pgBackRest::Common::String;
|
|
|
|
use pgBackRest::Common::Wait;
|
|
|
|
use pgBackRest::FileCommon;
|
2016-08-24 18:39:27 +02:00
|
|
|
use pgBackRest::Protocol::Common;
|
2016-09-04 15:22:53 +02:00
|
|
|
use pgBackRest::Version;
|
2014-02-03 03:03:05 +03:00
|
|
|
|
2014-06-06 06:51:27 +03:00
|
|
|
####################################################################################################################################
|
2015-08-29 20:20:46 +02:00
|
|
|
# PATH_GET constants
|
2014-06-06 06:51:27 +03:00
|
|
|
####################################################################################################################################
|
2015-08-29 20:20:46 +02:00
|
|
|
use constant PATH_ABSOLUTE => 'absolute';
|
|
|
|
push @EXPORT, qw(PATH_ABSOLUTE);
|
|
|
|
use constant PATH_DB => 'db';
|
|
|
|
push @EXPORT, qw(PATH_DB);
|
|
|
|
use constant PATH_DB_ABSOLUTE => 'db:absolute';
|
|
|
|
push @EXPORT, qw(PATH_DB_ABSOLUTE);
|
|
|
|
use constant PATH_BACKUP => 'backup';
|
|
|
|
push @EXPORT, qw(PATH_BACKUP);
|
|
|
|
use constant PATH_BACKUP_ABSOLUTE => 'backup:absolute';
|
|
|
|
push @EXPORT, qw(PATH_BACKUP_ABSOLUTE);
|
|
|
|
use constant PATH_BACKUP_CLUSTER => 'backup:cluster';
|
|
|
|
push @EXPORT, qw(PATH_BACKUP_CLUSTER);
|
|
|
|
use constant PATH_BACKUP_TMP => 'backup:tmp';
|
|
|
|
push @EXPORT, qw(PATH_BACKUP_TMP);
|
|
|
|
use constant PATH_BACKUP_ARCHIVE => 'backup:archive';
|
|
|
|
push @EXPORT, qw(PATH_BACKUP_ARCHIVE);
|
|
|
|
use constant PATH_BACKUP_ARCHIVE_OUT => 'backup:archive:out';
|
|
|
|
push @EXPORT, qw(PATH_BACKUP_ARCHIVE_OUT);
|
2015-03-12 18:15:19 +02:00
|
|
|
|
2014-06-07 23:06:46 +03:00
|
|
|
####################################################################################################################################
|
2015-08-29 20:20:46 +02:00
|
|
|
# STD pipe constants
|
2014-06-07 23:06:46 +03:00
|
|
|
####################################################################################################################################
|
2015-08-29 20:20:46 +02:00
|
|
|
use constant PIPE_STDIN => '<STDIN>';
|
|
|
|
push @EXPORT, qw(PIPE_STDIN);
|
|
|
|
use constant PIPE_STDOUT => '<STDOUT>';
|
|
|
|
push @EXPORT, qw(PIPE_STDOUT);
|
|
|
|
use constant PIPE_STDERR => '<STDERR>';
|
|
|
|
push @EXPORT, qw(PIPE_STDERR);
|
2015-03-12 18:15:19 +02:00
|
|
|
|
2014-02-07 00:37:37 +03:00
|
|
|
####################################################################################################################################
|
2015-08-29 20:20:46 +02:00
|
|
|
# new
|
2014-02-07 00:37:37 +03:00
|
|
|
####################################################################################################################################
|
2014-10-10 22:13:28 +03:00
|
|
|
sub new
|
2014-02-03 03:03:05 +03:00
|
|
|
{
|
2014-10-10 22:13:28 +03:00
|
|
|
my $class = shift;
|
|
|
|
|
|
|
|
# Create the class hash
|
|
|
|
my $self = {};
|
|
|
|
bless $self, $class;
|
|
|
|
|
2015-08-29 20:20:46 +02:00
|
|
|
# Assign function parameters, defaults, and log debug info
|
|
|
|
(
|
|
|
|
my $strOperation,
|
|
|
|
$self->{strStanza},
|
2016-12-23 15:30:34 +02:00
|
|
|
$self->{strRepoPath},
|
2015-08-29 20:20:46 +02:00
|
|
|
$self->{oProtocol},
|
|
|
|
$self->{strDefaultPathMode},
|
|
|
|
$self->{strDefaultFileMode},
|
|
|
|
) =
|
|
|
|
logDebugParam
|
|
|
|
(
|
2016-08-11 23:32:28 +02:00
|
|
|
__PACKAGE__ . '->new', \@_,
|
2015-08-29 20:20:46 +02:00
|
|
|
{name => 'strStanza', required => false},
|
2016-12-23 15:30:34 +02:00
|
|
|
{name => 'strRepoPath'},
|
2015-08-29 20:20:46 +02:00
|
|
|
{name => 'oProtocol'},
|
|
|
|
{name => 'strDefaultPathMode', default => '0750'},
|
|
|
|
{name => 'strDefaultFileMode', default => '0640'},
|
|
|
|
);
|
|
|
|
|
2014-10-10 22:13:28 +03:00
|
|
|
# Default compression extension to gz
|
2017-01-27 17:02:00 +02:00
|
|
|
$self->{strCompressExtension} = COMPRESS_EXT;
|
2014-10-10 22:13:28 +03:00
|
|
|
|
2015-08-29 20:20:46 +02:00
|
|
|
# Return from function and log return values if any
|
|
|
|
return logDebugReturn
|
|
|
|
(
|
|
|
|
$strOperation,
|
|
|
|
{name => 'self', value => $self}
|
|
|
|
);
|
2014-02-03 03:03:05 +03:00
|
|
|
}
|
|
|
|
|
2014-04-28 16:13:25 +03:00
|
|
|
####################################################################################################################################
|
2015-08-29 20:20:46 +02:00
|
|
|
# pathTypeGet
|
2014-04-28 16:13:25 +03:00
|
|
|
####################################################################################################################################
|
2015-08-29 20:20:46 +02:00
|
|
|
sub pathTypeGet
|
2014-02-03 03:03:05 +03:00
|
|
|
{
|
2014-02-06 05:39:08 +03:00
|
|
|
my $self = shift;
|
2015-08-29 20:20:46 +02:00
|
|
|
|
|
|
|
# Assign function parameters, defaults, and log debug info
|
|
|
|
my
|
|
|
|
(
|
|
|
|
$strOperation,
|
|
|
|
$strType
|
|
|
|
) =
|
|
|
|
logDebugParam
|
|
|
|
(
|
2016-08-11 23:32:28 +02:00
|
|
|
__PACKAGE__ . '->pathTypeGet', \@_,
|
2015-08-29 20:20:46 +02:00
|
|
|
{name => 'strType', trace => true}
|
|
|
|
);
|
|
|
|
|
|
|
|
my $strPath;
|
2014-02-07 00:37:37 +03:00
|
|
|
|
2014-06-04 05:02:56 +03:00
|
|
|
# If absolute type
|
2014-06-04 02:03:03 +03:00
|
|
|
if ($strType eq PATH_ABSOLUTE)
|
|
|
|
{
|
2015-08-29 20:20:46 +02:00
|
|
|
$strPath = PATH_ABSOLUTE;
|
2014-06-04 02:03:03 +03:00
|
|
|
}
|
2014-06-04 05:02:56 +03:00
|
|
|
# If db type
|
2014-06-04 02:03:03 +03:00
|
|
|
elsif ($strType =~ /^db(\:.*){0,1}/)
|
2014-02-03 03:03:05 +03:00
|
|
|
{
|
2015-08-29 20:20:46 +02:00
|
|
|
$strPath = PATH_DB;
|
2014-02-03 03:03:05 +03:00
|
|
|
}
|
|
|
|
# Else if backup type
|
|
|
|
elsif ($strType =~ /^backup(\:.*){0,1}/)
|
|
|
|
{
|
2015-08-29 20:20:46 +02:00
|
|
|
$strPath = PATH_BACKUP;
|
|
|
|
}
|
2016-12-23 15:30:34 +02:00
|
|
|
# else error when path type not recognized
|
2015-08-29 20:20:46 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
confess &log(ASSERT, "no known path types in '${strType}'");
|
2014-02-03 03:03:05 +03:00
|
|
|
}
|
2014-02-07 00:37:37 +03:00
|
|
|
|
2015-08-29 20:20:46 +02:00
|
|
|
# Return from function and log return values if any
|
|
|
|
return logDebugReturn
|
|
|
|
(
|
|
|
|
$strOperation,
|
|
|
|
{name => 'strPath', value => $strPath, trace => true}
|
|
|
|
);
|
2014-02-03 03:03:05 +03:00
|
|
|
}
|
|
|
|
|
2014-06-04 02:03:03 +03:00
|
|
|
####################################################################################################################################
|
2015-08-29 20:20:46 +02:00
|
|
|
# pathGet
|
2014-06-04 02:03:03 +03:00
|
|
|
####################################################################################################################################
|
2015-08-29 20:20:46 +02:00
|
|
|
sub pathGet
|
2014-02-03 03:03:05 +03:00
|
|
|
{
|
2014-02-06 05:39:08 +03:00
|
|
|
my $self = shift;
|
2015-08-29 20:20:46 +02:00
|
|
|
|
|
|
|
# Assign function parameters, defaults, and log debug info
|
|
|
|
my
|
|
|
|
(
|
|
|
|
$strOperation,
|
|
|
|
$strType, # Base type of the path to get (PATH_DB_ABSOLUTE, PATH_BACKUP_TMP, etc)
|
|
|
|
$strFile, # File to append to the base path (can include a path as well)
|
|
|
|
$bTemp # Return the temp file for this path type - only some types have temp files
|
|
|
|
) =
|
|
|
|
logDebugParam
|
|
|
|
(
|
2016-08-11 23:32:28 +02:00
|
|
|
__PACKAGE__ . '->pathGet', \@_,
|
2015-08-29 20:20:46 +02:00
|
|
|
{name => 'strType', trace => true},
|
|
|
|
{name => 'strFile', required => false, trace => true},
|
|
|
|
{name => 'bTemp', default => false, trace => true}
|
|
|
|
);
|
2014-02-03 03:03:05 +03:00
|
|
|
|
2016-12-23 15:30:34 +02:00
|
|
|
# Path to be returned
|
|
|
|
my $strPath;
|
|
|
|
|
2016-07-30 00:43:59 +02:00
|
|
|
# Is this an absolute path type?
|
2014-06-04 02:03:03 +03:00
|
|
|
my $bAbsolute = $strType =~ /.*absolute.*/;
|
|
|
|
|
2016-07-30 00:43:59 +02:00
|
|
|
# Make sure a temp file is valid for this type and file
|
|
|
|
if ($bTemp)
|
2014-02-03 03:03:05 +03:00
|
|
|
{
|
2016-07-30 00:43:59 +02:00
|
|
|
# Only allow temp files for PATH_BACKUP_ARCHIVE, PATH_BACKUP_ARCHIVE_OUT, PATH_BACKUP_TMP and any absolute path
|
|
|
|
if (!($strType eq PATH_BACKUP_ARCHIVE || $strType eq PATH_BACKUP_ARCHIVE_OUT || $strType eq PATH_BACKUP_TMP || $bAbsolute))
|
|
|
|
{
|
2016-12-23 15:30:34 +02:00
|
|
|
confess &log(ASSERT, "temp file not supported for path type '${strType}'");
|
2016-07-30 00:27:35 +02:00
|
|
|
}
|
2014-02-03 03:03:05 +03:00
|
|
|
|
2016-07-30 00:43:59 +02:00
|
|
|
# The file must be defined
|
|
|
|
if (!defined($strFile))
|
2016-07-30 00:27:35 +02:00
|
|
|
{
|
2016-12-23 15:30:34 +02:00
|
|
|
confess &log(ASSERT, 'strFile must be defined when temp file specified');
|
2016-07-30 00:43:59 +02:00
|
|
|
}
|
2014-06-02 00:23:33 +03:00
|
|
|
}
|
|
|
|
|
2014-06-04 02:03:03 +03:00
|
|
|
# Get absolute path
|
|
|
|
if ($bAbsolute)
|
2014-02-03 03:03:05 +03:00
|
|
|
{
|
2016-12-23 15:30:34 +02:00
|
|
|
# File must defined when the path is absolute since in effect there is no path
|
|
|
|
if (!defined($strFile))
|
2016-07-30 00:43:59 +02:00
|
|
|
{
|
2016-12-23 15:30:34 +02:00
|
|
|
confess &log(ASSERT, 'strFile must be defined for absolute path');
|
2016-07-30 00:43:59 +02:00
|
|
|
}
|
|
|
|
|
2016-12-23 15:30:34 +02:00
|
|
|
# Make sure that the file starts with /, otherwise it will actually be relative
|
|
|
|
if ($strFile !~ /^\/.*/)
|
2014-03-28 22:20:36 +03:00
|
|
|
{
|
2016-12-23 15:30:34 +02:00
|
|
|
confess &log(ASSERT, "absolute path ${strType}:${strFile} must start with /");
|
2014-03-28 22:20:36 +03:00
|
|
|
}
|
2014-02-03 03:03:05 +03:00
|
|
|
}
|
2016-12-23 15:30:34 +02:00
|
|
|
# Else get backup path
|
|
|
|
elsif ($strType eq PATH_BACKUP)
|
2014-02-05 21:10:36 +03:00
|
|
|
{
|
2016-12-23 15:30:34 +02:00
|
|
|
$strPath = $self->{strRepoPath};
|
2014-02-05 21:10:36 +03:00
|
|
|
}
|
2016-12-23 15:30:34 +02:00
|
|
|
# Else process path types that require a stanza
|
|
|
|
else
|
2014-02-03 03:03:05 +03:00
|
|
|
{
|
2016-12-23 15:30:34 +02:00
|
|
|
# All paths in this section will in the repo path
|
|
|
|
$strPath = $self->{strRepoPath};
|
2015-03-12 18:15:19 +02:00
|
|
|
|
2016-12-23 15:30:34 +02:00
|
|
|
# Make sure the stanza is defined since remaining path types require it
|
|
|
|
if (!defined($self->{strStanza}))
|
2015-06-14 00:25:49 +02:00
|
|
|
{
|
2016-12-23 15:30:34 +02:00
|
|
|
confess &log(ASSERT, 'strStanza not defined');
|
2015-06-14 00:25:49 +02:00
|
|
|
}
|
|
|
|
|
2016-12-23 15:30:34 +02:00
|
|
|
# Get the backup tmp path
|
|
|
|
if ($strType eq PATH_BACKUP_TMP)
|
2014-02-03 03:03:05 +03:00
|
|
|
{
|
2016-12-23 15:30:34 +02:00
|
|
|
$strPath .= "/temp/$self->{strStanza}.tmp";
|
|
|
|
}
|
|
|
|
# Else get archive paths
|
|
|
|
elsif ($strType eq PATH_BACKUP_ARCHIVE_OUT || $strType eq PATH_BACKUP_ARCHIVE)
|
|
|
|
{
|
|
|
|
$strPath .= "/archive/$self->{strStanza}";
|
2014-02-05 23:56:05 +03:00
|
|
|
|
2016-12-23 15:30:34 +02:00
|
|
|
# Get archive path
|
|
|
|
if ($strType eq PATH_BACKUP_ARCHIVE)
|
2014-02-03 03:03:05 +03:00
|
|
|
{
|
2016-12-23 15:30:34 +02:00
|
|
|
# If file is not defined nothing further to do
|
|
|
|
if (defined($strFile))
|
|
|
|
{
|
|
|
|
my $strArchiveId = (split('/', $strFile))[0];
|
|
|
|
|
|
|
|
# If file is defined after archive id path is split out
|
|
|
|
if (defined((split('/', $strFile))[1]))
|
|
|
|
{
|
|
|
|
$strPath .= "/${strArchiveId}";
|
|
|
|
$strFile = (split('/', $strFile))[1];
|
|
|
|
|
|
|
|
# If this is a WAL segment then put it into a subdirectory
|
|
|
|
if (substr(basename($strFile), 0, 24) =~ /^([0-F]){24}$/)
|
|
|
|
{
|
|
|
|
$strPath .= '/' . substr($strFile, 0, 16);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-06-14 00:25:49 +02:00
|
|
|
}
|
2016-12-23 15:30:34 +02:00
|
|
|
# Else get archive out path
|
|
|
|
else
|
2015-06-14 00:25:49 +02:00
|
|
|
{
|
2016-12-23 15:30:34 +02:00
|
|
|
$strPath .= '/out';
|
2014-02-03 03:03:05 +03:00
|
|
|
}
|
2015-03-12 18:15:19 +02:00
|
|
|
}
|
2016-12-23 15:30:34 +02:00
|
|
|
# Else get backup cluster
|
|
|
|
elsif ($strType eq PATH_BACKUP_CLUSTER)
|
|
|
|
{
|
|
|
|
$strPath .= "/backup/$self->{strStanza}";
|
|
|
|
}
|
|
|
|
# Else error when path type not recognized
|
2015-03-12 18:15:19 +02:00
|
|
|
else
|
|
|
|
{
|
2016-12-23 15:30:34 +02:00
|
|
|
confess &log(ASSERT, "no known path types in '${strType}'");
|
2015-03-12 18:15:19 +02:00
|
|
|
}
|
2014-02-05 23:56:05 +03:00
|
|
|
}
|
2014-02-04 03:03:17 +03:00
|
|
|
|
2016-12-23 15:30:34 +02:00
|
|
|
# Combine path and file
|
|
|
|
$strPath .= (defined($strFile) ? (defined($strPath) ? '/' : '') . $strFile : '');
|
|
|
|
|
|
|
|
# Add temp extension
|
|
|
|
$strPath .= $bTemp ? '.' . BACKREST_EXE . '.tmp' : '';
|
2014-02-03 03:03:05 +03:00
|
|
|
|
2016-12-23 15:30:34 +02:00
|
|
|
# Return from function and log return values if any
|
|
|
|
return logDebugReturn
|
|
|
|
(
|
|
|
|
$strOperation,
|
|
|
|
{name => 'strPath', value => $strPath, trace => true}
|
|
|
|
);
|
2014-02-03 03:03:05 +03:00
|
|
|
}
|
|
|
|
|
2014-06-04 04:22:07 +03:00
|
|
|
####################################################################################################################################
|
2015-08-29 20:20:46 +02:00
|
|
|
# isRemote
|
2014-06-04 04:22:07 +03:00
|
|
|
#
|
2014-06-07 04:16:24 +03:00
|
|
|
# Determine whether the path type is remote
|
2014-06-04 04:22:07 +03:00
|
|
|
####################################################################################################################################
|
2015-08-29 20:20:46 +02:00
|
|
|
sub isRemote
|
2014-06-04 04:22:07 +03:00
|
|
|
{
|
|
|
|
my $self = shift;
|
|
|
|
|
2015-08-29 20:20:46 +02:00
|
|
|
# Assign function parameters, defaults, and log debug info
|
|
|
|
my
|
|
|
|
(
|
|
|
|
$strOperation,
|
|
|
|
$strPathType
|
|
|
|
) =
|
|
|
|
logDebugParam
|
|
|
|
(
|
2016-08-11 23:32:28 +02:00
|
|
|
__PACKAGE__ . '->isRemote', \@_,
|
2015-08-29 20:20:46 +02:00
|
|
|
{name => 'strPathType', trace => true}
|
|
|
|
);
|
|
|
|
|
2016-08-24 18:39:27 +02:00
|
|
|
my $bRemote = $self->{oProtocol}->isRemote() && $self->{oProtocol}->remoteTypeTest($self->pathTypeGet($strPathType));
|
2015-08-29 20:20:46 +02:00
|
|
|
|
|
|
|
# Return from function and log return values if any
|
|
|
|
return logDebugReturn
|
|
|
|
(
|
|
|
|
$strOperation,
|
|
|
|
{name => 'bRemote', value => $bRemote, trace => true}
|
|
|
|
);
|
2014-06-04 04:22:07 +03:00
|
|
|
}
|
|
|
|
|
2014-02-03 03:03:05 +03:00
|
|
|
####################################################################################################################################
|
2015-08-29 20:20:46 +02:00
|
|
|
# linkCreate
|
2014-02-03 03:03:05 +03:00
|
|
|
####################################################################################################################################
|
2015-08-29 20:20:46 +02:00
|
|
|
sub linkCreate
|
2014-02-03 03:03:05 +03:00
|
|
|
{
|
2014-02-06 05:39:08 +03:00
|
|
|
my $self = shift;
|
2014-02-07 00:37:37 +03:00
|
|
|
|
2015-08-29 20:20:46 +02:00
|
|
|
# Assign function parameters, defaults, and log debug info
|
|
|
|
my
|
|
|
|
(
|
|
|
|
$strOperation,
|
|
|
|
$strSourcePathType,
|
|
|
|
$strSourceFile,
|
|
|
|
$strDestinationPathType,
|
|
|
|
$strDestinationFile,
|
|
|
|
$bHard,
|
|
|
|
$bRelative,
|
|
|
|
$bPathCreate
|
|
|
|
) =
|
|
|
|
logDebugParam
|
|
|
|
(
|
2016-08-11 23:32:28 +02:00
|
|
|
__PACKAGE__ . '->linkCreate', \@_,
|
2015-08-29 20:20:46 +02:00
|
|
|
{name => 'strSourcePathType'},
|
|
|
|
{name => 'strSourceFile'},
|
|
|
|
{name => 'strDestinationPathType'},
|
|
|
|
{name => 'strDestinationFile'},
|
|
|
|
{name => 'bHard', default => false},
|
|
|
|
{name => 'bRelative', default => false},
|
|
|
|
{name => 'bPathCreate', default => true}
|
|
|
|
);
|
2014-06-04 18:58:30 +03:00
|
|
|
|
2014-12-21 17:11:17 +02:00
|
|
|
# Source and destination path types must be the same (e.g. both PATH_DB or both PATH_BACKUP, etc.)
|
2015-08-29 20:20:46 +02:00
|
|
|
if ($self->pathTypeGet($strSourcePathType) ne $self->pathTypeGet($strDestinationPathType))
|
2014-02-04 03:03:17 +03:00
|
|
|
{
|
2014-09-16 15:15:49 +03:00
|
|
|
confess &log(ASSERT, 'path types must be equal in link create');
|
2014-02-04 03:03:17 +03:00
|
|
|
}
|
|
|
|
|
2014-02-05 18:40:49 +03:00
|
|
|
# Generate source and destination files
|
2015-08-29 20:20:46 +02:00
|
|
|
my $strSource = $self->pathGet($strSourcePathType, $strSourceFile);
|
|
|
|
my $strDestination = $self->pathGet($strDestinationPathType, $strDestinationFile);
|
2014-02-03 03:03:05 +03:00
|
|
|
|
2015-08-29 20:20:46 +02:00
|
|
|
# Run remotely
|
|
|
|
if ($self->isRemote($strSourcePathType))
|
2014-02-03 03:03:05 +03:00
|
|
|
{
|
2016-08-11 23:32:28 +02:00
|
|
|
confess &log(ASSERT, "${strOperation}: remote operation not supported");
|
2014-02-03 03:03:05 +03:00
|
|
|
}
|
2015-08-29 20:20:46 +02:00
|
|
|
# Run locally
|
|
|
|
else
|
2014-02-03 03:03:05 +03:00
|
|
|
{
|
2015-08-29 20:20:46 +02:00
|
|
|
# If the destination path is backup and does not exist, create it
|
2016-04-14 01:05:31 +02:00
|
|
|
# ??? This should only happen when the link create errors
|
2015-08-29 20:20:46 +02:00
|
|
|
if ($bPathCreate && $self->pathTypeGet($strDestinationPathType) eq PATH_BACKUP)
|
2014-02-03 03:03:05 +03:00
|
|
|
{
|
2016-12-10 16:06:45 +02:00
|
|
|
filePathCreate(dirname($strDestination), undef, true);
|
2014-02-03 03:03:05 +03:00
|
|
|
}
|
2015-08-29 20:20:46 +02:00
|
|
|
|
|
|
|
unless (-e $strSource)
|
2014-02-03 03:03:05 +03:00
|
|
|
{
|
2015-08-29 20:20:46 +02:00
|
|
|
if (-e $strSource . ".$self->{strCompressExtension}")
|
|
|
|
{
|
|
|
|
$strSource .= ".$self->{strCompressExtension}";
|
|
|
|
$strDestination .= ".$self->{strCompressExtension}";
|
|
|
|
}
|
|
|
|
else
|
2014-02-05 18:40:49 +03:00
|
|
|
{
|
2015-08-29 20:20:46 +02:00
|
|
|
# Error when a hardlink will be created on a missing file
|
|
|
|
if ($bHard)
|
|
|
|
{
|
|
|
|
confess &log(ASSERT, "unable to find ${strSource}(.$self->{strCompressExtension}) for link");
|
|
|
|
}
|
2014-02-05 18:40:49 +03:00
|
|
|
}
|
2014-02-03 03:03:05 +03:00
|
|
|
}
|
2014-02-07 00:37:37 +03:00
|
|
|
|
2015-08-29 20:20:46 +02:00
|
|
|
# Generate relative path if requested
|
|
|
|
if ($bRelative)
|
2014-02-05 18:40:49 +03:00
|
|
|
{
|
2016-12-03 16:56:05 +02:00
|
|
|
# Determine how much of the paths are common
|
|
|
|
my @strySource = split('/', $strSource);
|
|
|
|
my @stryDestination = split('/', $strDestination);
|
2015-08-29 20:20:46 +02:00
|
|
|
|
2016-12-03 16:56:05 +02:00
|
|
|
while (defined($strySource[0]) && defined($stryDestination[0]) && $strySource[0] eq $stryDestination[0])
|
2015-08-29 20:20:46 +02:00
|
|
|
{
|
2016-12-03 16:56:05 +02:00
|
|
|
shift(@strySource);
|
|
|
|
shift(@stryDestination);
|
2015-08-29 20:20:46 +02:00
|
|
|
}
|
|
|
|
|
2016-12-03 16:56:05 +02:00
|
|
|
# Add relative path sections
|
|
|
|
$strSource = '';
|
|
|
|
|
|
|
|
for (my $iIndex = 0; $iIndex < @stryDestination - 1; $iIndex++)
|
|
|
|
{
|
|
|
|
$strSource .= '../';
|
|
|
|
}
|
|
|
|
|
|
|
|
# Add path to source
|
|
|
|
$strSource .= join('/', @strySource);
|
|
|
|
|
2015-08-29 20:20:46 +02:00
|
|
|
logDebugMisc
|
|
|
|
(
|
|
|
|
$strOperation, 'apply relative path',
|
|
|
|
{name => 'strSource', value => $strSource, trace => true}
|
|
|
|
);
|
2014-02-05 18:40:49 +03:00
|
|
|
}
|
|
|
|
|
2014-12-21 17:11:17 +02:00
|
|
|
if ($bHard)
|
|
|
|
{
|
|
|
|
link($strSource, $strDestination)
|
|
|
|
or confess &log(ERROR, "unable to create hardlink from ${strSource} to ${strDestination}");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
symlink($strSource, $strDestination)
|
|
|
|
or confess &log(ERROR, "unable to create symlink from ${strSource} to ${strDestination}");
|
|
|
|
}
|
2014-02-04 03:03:17 +03:00
|
|
|
}
|
2015-08-29 20:20:46 +02:00
|
|
|
|
|
|
|
# Return from function and log return values if any
|
2016-08-11 23:32:28 +02:00
|
|
|
return logDebugReturn($strOperation);
|
2014-02-03 03:03:05 +03:00
|
|
|
}
|
|
|
|
|
2014-02-05 02:48:39 +03:00
|
|
|
####################################################################################################################################
|
2015-08-29 20:20:46 +02:00
|
|
|
# move
|
2014-02-05 02:48:39 +03:00
|
|
|
#
|
|
|
|
# Moves a file locally or remotely.
|
|
|
|
####################################################################################################################################
|
2014-06-04 02:03:03 +03:00
|
|
|
sub move
|
2014-02-05 02:48:39 +03:00
|
|
|
{
|
2014-02-06 05:39:08 +03:00
|
|
|
my $self = shift;
|
2014-02-05 21:10:36 +03:00
|
|
|
|
2015-08-29 20:20:46 +02:00
|
|
|
# Assign function parameters, defaults, and log debug info
|
|
|
|
my
|
|
|
|
(
|
|
|
|
$strOperation,
|
|
|
|
$strSourcePathType,
|
|
|
|
$strSourceFile,
|
|
|
|
$strDestinationPathType,
|
|
|
|
$strDestinationFile,
|
2016-12-10 16:06:45 +02:00
|
|
|
$bDestinationPathCreate,
|
|
|
|
$bPathSync,
|
2015-08-29 20:20:46 +02:00
|
|
|
) =
|
|
|
|
logDebugParam
|
|
|
|
(
|
2016-08-11 23:32:28 +02:00
|
|
|
__PACKAGE__ . '->move', \@_,
|
2015-08-29 20:20:46 +02:00
|
|
|
{name => 'strSourcePathType'},
|
|
|
|
{name => 'strSourceFile', required => false},
|
|
|
|
{name => 'strDestinationPathType'},
|
|
|
|
{name => 'strDestinationFile'},
|
2016-12-10 16:06:45 +02:00
|
|
|
{name => 'bDestinationPathCreate', default => false},
|
|
|
|
{name => 'bPathSync', default => false},
|
2015-08-29 20:20:46 +02:00
|
|
|
);
|
2014-06-21 21:19:03 +03:00
|
|
|
|
|
|
|
# Source and destination path types must be the same
|
2015-08-29 20:20:46 +02:00
|
|
|
if ($self->pathTypeGet($strSourcePathType) ne $self->pathTypeGet($strSourcePathType))
|
2014-02-05 21:10:36 +03:00
|
|
|
{
|
2015-08-29 20:20:46 +02:00
|
|
|
confess &log(ASSERT, 'source and destination path types must be equal');
|
2014-02-05 21:10:36 +03:00
|
|
|
}
|
|
|
|
|
2015-08-29 20:20:46 +02:00
|
|
|
# Set operation variables
|
|
|
|
my $strPathOpSource = $self->pathGet($strSourcePathType, $strSourceFile);
|
|
|
|
my $strPathOpDestination = $self->pathGet($strDestinationPathType, $strDestinationFile);
|
|
|
|
|
2014-02-05 02:48:39 +03:00
|
|
|
# Run remotely
|
2015-08-29 20:20:46 +02:00
|
|
|
if ($self->isRemote($strSourcePathType))
|
2014-02-05 02:48:39 +03:00
|
|
|
{
|
2016-08-11 23:32:28 +02:00
|
|
|
confess &log(ASSERT, "${strOperation}: remote operation not supported");
|
2014-02-05 02:48:39 +03:00
|
|
|
}
|
|
|
|
# Run locally
|
|
|
|
else
|
|
|
|
{
|
2016-12-10 16:06:45 +02:00
|
|
|
fileMove($strPathOpSource, $strPathOpDestination, $bDestinationPathCreate, $bPathSync);
|
2014-02-05 02:48:39 +03:00
|
|
|
}
|
2015-08-29 20:20:46 +02:00
|
|
|
|
|
|
|
# Return from function and log return values if any
|
|
|
|
return logDebugReturn
|
|
|
|
(
|
|
|
|
$strOperation
|
|
|
|
);
|
2014-02-05 02:48:39 +03:00
|
|
|
}
|
|
|
|
|
2014-06-21 22:17:09 +03:00
|
|
|
####################################################################################################################################
|
2015-08-29 20:20:46 +02:00
|
|
|
# compress
|
2014-06-21 22:17:09 +03:00
|
|
|
####################################################################################################################################
|
|
|
|
sub compress
|
|
|
|
{
|
|
|
|
my $self = shift;
|
|
|
|
|
2015-08-29 20:20:46 +02:00
|
|
|
# Assign function parameters, defaults, and log debug info
|
|
|
|
my
|
|
|
|
(
|
|
|
|
$strOperation,
|
|
|
|
$strPathType,
|
2016-04-14 01:09:35 +02:00
|
|
|
$strFile,
|
|
|
|
$bRemoveSource
|
2015-08-29 20:20:46 +02:00
|
|
|
) =
|
|
|
|
logDebugParam
|
|
|
|
(
|
2016-08-11 23:32:28 +02:00
|
|
|
__PACKAGE__ . '->compress', \@_,
|
2015-08-29 20:20:46 +02:00
|
|
|
{name => 'strPathType'},
|
2016-04-14 01:09:35 +02:00
|
|
|
{name => 'strFile'},
|
|
|
|
{name => 'bRemoveSource', default => true}
|
2015-08-29 20:20:46 +02:00
|
|
|
);
|
2014-06-21 22:17:09 +03:00
|
|
|
|
2015-08-29 20:20:46 +02:00
|
|
|
# Set operation variables
|
|
|
|
my $strPathOp = $self->pathGet($strPathType, $strFile);
|
2014-06-21 22:17:09 +03:00
|
|
|
|
|
|
|
# Run remotely
|
2015-08-29 20:20:46 +02:00
|
|
|
if ($self->isRemote($strPathType))
|
2014-06-21 22:17:09 +03:00
|
|
|
{
|
2016-08-11 23:32:28 +02:00
|
|
|
confess &log(ASSERT, "${strOperation}: remote operation not supported");
|
2014-06-21 22:17:09 +03:00
|
|
|
}
|
|
|
|
# Run locally
|
|
|
|
else
|
|
|
|
{
|
2015-03-01 20:41:35 +02:00
|
|
|
# Use copy to compress the file
|
|
|
|
$self->copy($strPathType, $strFile, $strPathType, "${strFile}.gz", false, true);
|
2014-06-22 03:08:49 +03:00
|
|
|
|
2014-06-29 17:53:39 +03:00
|
|
|
# Remove the old file
|
2016-04-14 01:09:35 +02:00
|
|
|
if ($bRemoveSource)
|
|
|
|
{
|
|
|
|
fileRemove($strPathOp);
|
|
|
|
}
|
2014-06-21 22:17:09 +03:00
|
|
|
}
|
2015-08-29 20:20:46 +02:00
|
|
|
|
|
|
|
# Return from function and log return values if any
|
|
|
|
return logDebugReturn
|
|
|
|
(
|
|
|
|
$strOperation
|
|
|
|
);
|
2014-06-21 22:17:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
####################################################################################################################################
|
2015-08-29 20:20:46 +02:00
|
|
|
# pathCreate
|
2014-06-21 22:17:09 +03:00
|
|
|
#
|
|
|
|
# Creates a path locally or remotely.
|
|
|
|
####################################################################################################################################
|
2015-08-29 20:20:46 +02:00
|
|
|
sub pathCreate
|
2014-06-21 22:17:09 +03:00
|
|
|
{
|
|
|
|
my $self = shift;
|
|
|
|
|
2015-08-29 20:20:46 +02:00
|
|
|
# Assign function parameters, defaults, and log debug info
|
|
|
|
my
|
|
|
|
(
|
|
|
|
$strOperation,
|
|
|
|
$strPathType,
|
|
|
|
$strPath,
|
|
|
|
$strMode,
|
2016-04-15 04:50:02 +02:00
|
|
|
$bIgnoreExists,
|
|
|
|
$bCreateParents
|
2015-08-29 20:20:46 +02:00
|
|
|
) =
|
|
|
|
logDebugParam
|
|
|
|
(
|
2016-08-11 23:32:28 +02:00
|
|
|
__PACKAGE__ . '->pathCreate', \@_,
|
2015-08-29 20:20:46 +02:00
|
|
|
{name => 'strPathType'},
|
|
|
|
{name => 'strPath', required => false},
|
|
|
|
{name => 'strMode', default => '0750'},
|
2016-04-15 04:50:02 +02:00
|
|
|
{name => 'bIgnoreExists', default => false},
|
|
|
|
{name => 'bCreateParents', default => false}
|
2015-08-29 20:20:46 +02:00
|
|
|
);
|
2014-06-21 22:17:09 +03:00
|
|
|
|
2015-08-29 20:20:46 +02:00
|
|
|
# Set operation variables
|
|
|
|
my $strPathOp = $self->pathGet($strPathType, $strPath);
|
2014-06-21 22:17:09 +03:00
|
|
|
|
2016-12-04 00:34:51 +02:00
|
|
|
# Run remotely
|
2015-08-29 20:20:46 +02:00
|
|
|
if ($self->isRemote($strPathType))
|
2014-06-21 22:17:09 +03:00
|
|
|
{
|
2016-12-04 00:34:51 +02:00
|
|
|
$self->{oProtocol}->cmdExecute(OP_FILE_PATH_CREATE, [$strPathOp, $strMode, $bIgnoreExists, $bCreateParents]);
|
2014-06-21 22:17:09 +03:00
|
|
|
}
|
2016-12-04 00:34:51 +02:00
|
|
|
# Run locally
|
2014-06-21 22:17:09 +03:00
|
|
|
else
|
|
|
|
{
|
2016-04-15 04:50:02 +02:00
|
|
|
filePathCreate($strPathOp, $strMode, $bIgnoreExists, $bCreateParents);
|
2014-06-21 22:17:09 +03:00
|
|
|
}
|
2015-08-29 20:20:46 +02:00
|
|
|
|
|
|
|
# Return from function and log return values if any
|
|
|
|
return logDebugReturn
|
|
|
|
(
|
|
|
|
$strOperation
|
|
|
|
);
|
2014-06-21 22:17:09 +03:00
|
|
|
}
|
|
|
|
|
2016-12-10 16:06:45 +02:00
|
|
|
|
|
|
|
####################################################################################################################################
|
|
|
|
# pathSync
|
|
|
|
####################################################################################################################################
|
|
|
|
sub pathSync
|
|
|
|
{
|
|
|
|
my $self = shift;
|
|
|
|
|
|
|
|
# Assign function parameters, defaults, and log debug info
|
|
|
|
my
|
|
|
|
(
|
|
|
|
$strOperation,
|
|
|
|
$strPathType,
|
|
|
|
$strPath,
|
|
|
|
$bRecursive,
|
|
|
|
) =
|
|
|
|
logDebugParam
|
|
|
|
(
|
|
|
|
__PACKAGE__ . '->pathSync', \@_,
|
|
|
|
{name => 'strPathType'},
|
|
|
|
{name => 'strPath', required => false},
|
|
|
|
{name => 'bRecursive', default => false},
|
|
|
|
);
|
|
|
|
|
|
|
|
# Remote not implemented
|
|
|
|
if ($self->isRemote($strPathType))
|
|
|
|
{
|
|
|
|
confess &log(ASSERT, "${strOperation}: remote operation not supported");
|
|
|
|
}
|
|
|
|
|
|
|
|
# Sync all paths in the tree
|
|
|
|
if ($bRecursive)
|
|
|
|
{
|
|
|
|
my $oManifest = $self->manifest($strPathType, $strPath);
|
|
|
|
|
|
|
|
# Iterate all files in the manifest
|
|
|
|
foreach my $strFile (sort(keys(%{$oManifest})))
|
|
|
|
{
|
|
|
|
# Only sync if this is a directory
|
|
|
|
if ($oManifest->{$strFile}{type} eq 'd')
|
|
|
|
{
|
|
|
|
# If current directory
|
|
|
|
if ($strFile eq '.')
|
|
|
|
{
|
|
|
|
$self->pathSync($strPathType, $strPath);
|
|
|
|
}
|
|
|
|
# Else a subdirectory
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$self->pathSync($strPathType, (defined($strPath) ? "${strPath}/" : '') . $strFile);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
# Only sync the specified path
|
|
|
|
else
|
|
|
|
{
|
|
|
|
filePathSync($self->pathGet($strPathType, $strPath));
|
|
|
|
}
|
|
|
|
|
|
|
|
# Return from function and log return values if any
|
|
|
|
return logDebugReturn($strOperation);
|
|
|
|
}
|
|
|
|
|
2014-06-15 22:56:45 +03:00
|
|
|
####################################################################################################################################
|
2015-08-29 20:20:46 +02:00
|
|
|
# exists
|
|
|
|
#
|
|
|
|
# Checks for the existence of a file, but does not imply that the file is readable/writeable.
|
2014-06-15 22:56:45 +03:00
|
|
|
#
|
|
|
|
# Return: true if file exists, false otherwise
|
|
|
|
####################################################################################################################################
|
|
|
|
sub exists
|
|
|
|
{
|
|
|
|
my $self = shift;
|
|
|
|
|
2015-08-29 20:20:46 +02:00
|
|
|
# Assign function parameters, defaults, and log debug info
|
|
|
|
my
|
|
|
|
(
|
|
|
|
$strOperation,
|
|
|
|
$strPathType,
|
|
|
|
$strPath
|
|
|
|
) =
|
|
|
|
logDebugParam
|
|
|
|
(
|
2016-08-11 23:32:28 +02:00
|
|
|
__PACKAGE__ . '->exists', \@_,
|
2015-08-29 20:20:46 +02:00
|
|
|
{name => 'strPathType'},
|
|
|
|
{name => 'strPath', required => false}
|
|
|
|
);
|
2014-06-15 22:56:45 +03:00
|
|
|
|
2015-08-29 20:20:46 +02:00
|
|
|
# Set operation variables
|
|
|
|
my $strPathOp = $self->pathGet($strPathType, $strPath);
|
2016-12-04 00:34:51 +02:00
|
|
|
my $bExists;
|
2014-06-15 22:56:45 +03:00
|
|
|
|
|
|
|
# Run remotely
|
2015-08-29 20:20:46 +02:00
|
|
|
if ($self->isRemote($strPathType))
|
2014-06-15 22:56:45 +03:00
|
|
|
{
|
2016-12-04 00:34:51 +02:00
|
|
|
$bExists = $self->{oProtocol}->cmdExecute(OP_FILE_EXISTS, [$strPathOp], true);
|
2014-06-15 22:56:45 +03:00
|
|
|
}
|
|
|
|
# Run locally
|
|
|
|
else
|
|
|
|
{
|
2016-04-14 01:09:35 +02:00
|
|
|
$bExists = fileExists($strPathOp);
|
2014-06-15 22:56:45 +03:00
|
|
|
}
|
|
|
|
|
2015-08-29 20:20:46 +02:00
|
|
|
# Return from function and log return values if any
|
|
|
|
return logDebugReturn
|
|
|
|
(
|
|
|
|
$strOperation,
|
|
|
|
{name => 'bExists', value => $bExists}
|
|
|
|
);
|
2014-06-15 22:56:45 +03:00
|
|
|
}
|
|
|
|
|
2014-06-22 02:16:55 +03:00
|
|
|
####################################################################################################################################
|
2015-08-29 20:20:46 +02:00
|
|
|
# remove
|
2014-06-22 02:16:55 +03:00
|
|
|
####################################################################################################################################
|
2015-01-07 19:58:21 +02:00
|
|
|
sub remove
|
2014-06-22 02:16:55 +03:00
|
|
|
{
|
|
|
|
my $self = shift;
|
|
|
|
|
2015-08-29 20:20:46 +02:00
|
|
|
# Assign function parameters, defaults, and log debug info
|
|
|
|
my
|
|
|
|
(
|
|
|
|
$strOperation,
|
|
|
|
$strPathType,
|
|
|
|
$strPath,
|
|
|
|
$bTemp,
|
2016-12-10 16:06:45 +02:00
|
|
|
$bIgnoreMissing,
|
|
|
|
$bPathSync,
|
2015-08-29 20:20:46 +02:00
|
|
|
) =
|
|
|
|
logDebugParam
|
|
|
|
(
|
2016-08-11 23:32:28 +02:00
|
|
|
__PACKAGE__ . '->remove', \@_,
|
2015-08-29 20:20:46 +02:00
|
|
|
{name => 'strPathType'},
|
|
|
|
{name => 'strPath'},
|
|
|
|
{name => 'bTemp', required => false},
|
2016-12-10 16:06:45 +02:00
|
|
|
{name => 'bIgnoreMissing', default => true},
|
|
|
|
{name => 'bPathSync', default => false},
|
2015-08-29 20:20:46 +02:00
|
|
|
);
|
2014-09-30 02:08:08 +03:00
|
|
|
|
2014-06-22 02:16:55 +03:00
|
|
|
# Set operation variables
|
2015-08-29 20:20:46 +02:00
|
|
|
my $strPathOp = $self->pathGet($strPathType, $strPath, $bTemp);
|
2015-01-07 19:58:21 +02:00
|
|
|
my $bRemoved = true;
|
2014-06-22 02:16:55 +03:00
|
|
|
|
2015-01-07 19:58:21 +02:00
|
|
|
# Run remotely
|
2015-08-29 20:20:46 +02:00
|
|
|
if ($self->isRemote($strPathType))
|
2014-06-22 02:16:55 +03:00
|
|
|
{
|
2016-08-11 23:32:28 +02:00
|
|
|
confess &log(ASSERT, "${strOperation}: remote operation not supported");
|
2014-06-22 02:16:55 +03:00
|
|
|
}
|
2015-01-07 19:58:21 +02:00
|
|
|
# Run locally
|
2014-06-22 02:16:55 +03:00
|
|
|
else
|
|
|
|
{
|
2016-12-10 16:06:45 +02:00
|
|
|
$bRemoved = fileRemove($strPathOp, $bIgnoreMissing, $bPathSync);
|
2015-01-07 19:58:21 +02:00
|
|
|
}
|
|
|
|
|
2015-08-29 20:20:46 +02:00
|
|
|
# Return from function and log return values if any
|
|
|
|
return logDebugReturn
|
|
|
|
(
|
|
|
|
$strOperation,
|
|
|
|
{name => 'bRemoved', value => $bRemoved}
|
|
|
|
);
|
2015-01-07 19:58:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
####################################################################################################################################
|
2015-08-29 20:20:46 +02:00
|
|
|
# hash
|
2015-01-08 23:36:45 +02:00
|
|
|
####################################################################################################################################
|
|
|
|
sub hash
|
|
|
|
{
|
|
|
|
my $self = shift;
|
2015-03-01 02:07:29 +02:00
|
|
|
|
2015-08-29 20:20:46 +02:00
|
|
|
# Assign function parameters, defaults, and log debug info
|
|
|
|
my
|
|
|
|
(
|
|
|
|
$strOperation,
|
|
|
|
$strPathType,
|
|
|
|
$strFile,
|
|
|
|
$bCompressed,
|
|
|
|
$strHashType
|
|
|
|
) =
|
|
|
|
logDebugParam
|
|
|
|
(
|
2016-08-11 23:32:28 +02:00
|
|
|
__PACKAGE__ . '->hash', \@_,
|
2015-08-29 20:20:46 +02:00
|
|
|
{name => 'strPathType'},
|
|
|
|
{name => 'strFile'},
|
|
|
|
{name => 'bCompressed', required => false},
|
|
|
|
{name => 'strHashType', required => false}
|
|
|
|
);
|
|
|
|
|
|
|
|
my ($strHash) = $self->hashSize($strPathType, $strFile, $bCompressed, $strHashType);
|
|
|
|
|
|
|
|
# Return from function and log return values if any
|
|
|
|
return logDebugReturn
|
|
|
|
(
|
|
|
|
$strOperation,
|
|
|
|
{name => 'strHash', value => $strHash, trace => true}
|
|
|
|
);
|
2015-03-01 02:07:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
####################################################################################################################################
|
2015-08-29 20:20:46 +02:00
|
|
|
# hashSize
|
2015-03-01 02:07:29 +02:00
|
|
|
####################################################################################################################################
|
2015-08-29 20:20:46 +02:00
|
|
|
sub hashSize
|
2015-03-01 02:07:29 +02:00
|
|
|
{
|
|
|
|
my $self = shift;
|
|
|
|
|
2015-08-29 20:20:46 +02:00
|
|
|
# Assign function parameters, defaults, and log debug info
|
|
|
|
my
|
|
|
|
(
|
|
|
|
$strOperation,
|
|
|
|
$strPathType,
|
|
|
|
$strFile,
|
|
|
|
$bCompressed,
|
|
|
|
$strHashType
|
|
|
|
) =
|
|
|
|
logDebugParam
|
|
|
|
(
|
2016-08-11 23:32:28 +02:00
|
|
|
__PACKAGE__ . '->hashSize', \@_,
|
2015-08-29 20:20:46 +02:00
|
|
|
{name => 'strPathType'},
|
|
|
|
{name => 'strFile'},
|
|
|
|
{name => 'bCompressed', default => false},
|
|
|
|
{name => 'strHashType', default => 'sha1'}
|
|
|
|
);
|
2015-01-08 23:36:45 +02:00
|
|
|
|
|
|
|
# Set operation variables
|
2015-08-29 20:20:46 +02:00
|
|
|
my $strFileOp = $self->pathGet($strPathType, $strFile);
|
2015-01-08 23:36:45 +02:00
|
|
|
my $strHash;
|
2015-03-01 02:07:29 +02:00
|
|
|
my $iSize = 0;
|
2015-01-08 23:36:45 +02:00
|
|
|
|
2015-08-29 20:20:46 +02:00
|
|
|
if ($self->isRemote($strPathType))
|
2015-01-08 23:36:45 +02:00
|
|
|
{
|
2016-08-11 23:32:28 +02:00
|
|
|
confess &log(ASSERT, "${strOperation}: remote operation not supported");
|
2015-01-08 23:36:45 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-04-14 01:09:35 +02:00
|
|
|
($strHash, $iSize) = fileHashSize($strFileOp, $bCompressed, $strHashType, $self->{oProtocol});
|
2015-01-08 23:36:45 +02:00
|
|
|
}
|
|
|
|
|
2015-08-29 20:20:46 +02:00
|
|
|
# Return from function and log return values if any
|
|
|
|
return logDebugReturn
|
|
|
|
(
|
|
|
|
$strOperation,
|
|
|
|
{name => 'strHash', value => $strHash},
|
|
|
|
{name => 'iSize', value => $iSize}
|
|
|
|
);
|
2015-01-08 23:36:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
####################################################################################################################################
|
2015-08-29 20:20:46 +02:00
|
|
|
# owner
|
2015-01-07 19:58:21 +02:00
|
|
|
####################################################################################################################################
|
|
|
|
sub owner
|
|
|
|
{
|
|
|
|
my $self = shift;
|
|
|
|
|
2015-08-29 20:20:46 +02:00
|
|
|
# Assign function parameters, defaults, and log debug info
|
|
|
|
my
|
|
|
|
(
|
|
|
|
$strOperation,
|
|
|
|
$strPathType,
|
|
|
|
$strFile,
|
|
|
|
$strUser,
|
|
|
|
$strGroup
|
|
|
|
) =
|
|
|
|
logDebugParam
|
|
|
|
(
|
2016-08-11 23:32:28 +02:00
|
|
|
__PACKAGE__ . '->owner', \@_,
|
2015-08-29 20:20:46 +02:00
|
|
|
{name => 'strPathType'},
|
|
|
|
{name => 'strFile'},
|
2017-01-05 22:19:56 +02:00
|
|
|
{name => 'strUser', required => false},
|
|
|
|
{name => 'strGroup', required => false}
|
2015-08-29 20:20:46 +02:00
|
|
|
);
|
2014-06-22 02:16:55 +03:00
|
|
|
|
2015-08-29 20:20:46 +02:00
|
|
|
# Set operation variables
|
|
|
|
my $strFileOp = $self->pathGet($strPathType, $strFile);
|
2014-06-22 02:16:55 +03:00
|
|
|
|
2017-01-05 22:19:56 +02:00
|
|
|
# Run remotely
|
2015-08-29 20:20:46 +02:00
|
|
|
if ($self->isRemote($strPathType))
|
2015-01-07 19:58:21 +02:00
|
|
|
{
|
2016-08-11 23:32:28 +02:00
|
|
|
confess &log(ASSERT, "${strOperation}: remote operation not supported");
|
2015-01-07 19:58:21 +02:00
|
|
|
}
|
2017-01-05 22:19:56 +02:00
|
|
|
# Run locally
|
2015-01-07 19:58:21 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
my $iUserId;
|
|
|
|
my $iGroupId;
|
|
|
|
|
2017-01-05 22:19:56 +02:00
|
|
|
# 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.
|
|
|
|
if (!(defined($strUser) && defined($strGroup)))
|
2014-09-30 02:08:08 +03:00
|
|
|
{
|
2017-01-05 22:19:56 +02:00
|
|
|
my $oStat = fileStat($strFileOp);
|
|
|
|
|
|
|
|
if (!defined($strUser))
|
|
|
|
{
|
|
|
|
$iUserId = $oStat->uid;
|
|
|
|
}
|
2015-01-07 19:58:21 +02:00
|
|
|
|
2017-01-05 22:19:56 +02:00
|
|
|
if (!defined($strGroup))
|
2015-01-07 19:58:21 +02:00
|
|
|
{
|
2017-01-05 22:19:56 +02:00
|
|
|
$iGroupId = $oStat->gid;
|
2015-01-07 19:58:21 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-05 22:19:56 +02:00
|
|
|
# Lookup user if specified
|
2015-01-07 19:58:21 +02:00
|
|
|
if (defined($strUser))
|
|
|
|
{
|
|
|
|
$iUserId = getpwnam($strUser);
|
2017-01-05 22:19:56 +02:00
|
|
|
|
|
|
|
if (!defined($iUserId))
|
|
|
|
{
|
|
|
|
confess &log(ERROR, "user '${strUser}' does not exist", ERROR_USER_MISSING);
|
|
|
|
}
|
2014-09-30 02:08:08 +03:00
|
|
|
}
|
2014-06-22 02:16:55 +03:00
|
|
|
|
2017-01-05 22:19:56 +02:00
|
|
|
# Lookup group if specified
|
2015-01-07 19:58:21 +02:00
|
|
|
if (defined($strGroup))
|
|
|
|
{
|
|
|
|
$iGroupId = getgrnam($strGroup);
|
2017-01-05 22:19:56 +02:00
|
|
|
|
|
|
|
if (!defined($iGroupId))
|
|
|
|
{
|
|
|
|
confess &log(ERROR, "group '${strGroup}' does not exist", ERROR_GROUP_MISSING);
|
|
|
|
}
|
2015-01-07 19:58:21 +02:00
|
|
|
}
|
2017-01-05 22:19:56 +02:00
|
|
|
|
|
|
|
# Set ownership on the file
|
|
|
|
if (!chown($iUserId, $iGroupId, $strFileOp))
|
2015-01-07 19:58:21 +02:00
|
|
|
{
|
2017-01-05 22:19:56 +02:00
|
|
|
my $strError = $!;
|
2014-06-22 02:16:55 +03:00
|
|
|
|
2017-01-05 22:19:56 +02:00
|
|
|
if (fileExists($strFileOp))
|
|
|
|
{
|
|
|
|
confess &log(ERROR,
|
|
|
|
"unable to set ownership for '${strFileOp}'" . (defined($strError) ? ": $strError" : ''), ERROR_FILE_OWNER);
|
|
|
|
}
|
|
|
|
|
|
|
|
confess &log(ERROR, "${strFile} does not exist", ERROR_FILE_MISSING);
|
|
|
|
}
|
2014-06-22 02:16:55 +03:00
|
|
|
}
|
2015-08-29 20:20:46 +02:00
|
|
|
|
|
|
|
# Return from function and log return values if any
|
|
|
|
return logDebugReturn
|
|
|
|
(
|
|
|
|
$strOperation
|
|
|
|
);
|
2014-06-22 02:16:55 +03:00
|
|
|
}
|
|
|
|
|
2014-06-05 23:40:05 +03:00
|
|
|
####################################################################################################################################
|
2015-08-29 20:20:46 +02:00
|
|
|
# list
|
2014-06-05 23:40:05 +03:00
|
|
|
####################################################################################################################################
|
2014-06-22 01:19:37 +03:00
|
|
|
sub list
|
2014-06-05 23:40:05 +03:00
|
|
|
{
|
|
|
|
my $self = shift;
|
2014-02-03 03:03:05 +03:00
|
|
|
|
2015-08-29 20:20:46 +02:00
|
|
|
# Assign function parameters, defaults, and log debug info
|
|
|
|
my
|
|
|
|
(
|
|
|
|
$strOperation,
|
|
|
|
$strPathType,
|
|
|
|
$strPath,
|
|
|
|
$strExpression,
|
|
|
|
$strSortOrder,
|
|
|
|
$bIgnoreMissing
|
|
|
|
) =
|
|
|
|
logDebugParam
|
|
|
|
(
|
2016-08-11 23:32:28 +02:00
|
|
|
__PACKAGE__ . '->list', \@_,
|
2015-08-29 20:20:46 +02:00
|
|
|
{name => 'strPathType'},
|
|
|
|
{name => 'strPath', required => false},
|
|
|
|
{name => 'strExpression', required => false},
|
|
|
|
{name => 'strSortOrder', default => 'forward'},
|
|
|
|
{name => 'bIgnoreMissing', default => false}
|
|
|
|
);
|
2014-02-05 16:21:27 +03:00
|
|
|
|
2014-06-22 01:19:37 +03:00
|
|
|
# Set operation variables
|
2015-08-29 20:20:46 +02:00
|
|
|
my $strPathOp = $self->pathGet($strPathType, $strPath);
|
2014-06-22 01:19:37 +03:00
|
|
|
my @stryFileList;
|
2014-02-03 03:03:05 +03:00
|
|
|
|
2014-06-22 01:19:37 +03:00
|
|
|
# Run remotely
|
2015-08-29 20:20:46 +02:00
|
|
|
if ($self->isRemote($strPathType))
|
2014-06-16 01:32:43 +03:00
|
|
|
{
|
2016-12-04 00:34:51 +02:00
|
|
|
@stryFileList = $self->{oProtocol}->cmdExecute(
|
|
|
|
OP_FILE_LIST, [$strPathOp, $strExpression, $strSortOrder, $bIgnoreMissing]);
|
2014-06-06 06:51:27 +03:00
|
|
|
}
|
2014-06-22 01:19:37 +03:00
|
|
|
# Run locally
|
|
|
|
else
|
2014-02-03 03:03:05 +03:00
|
|
|
{
|
2016-04-14 01:09:35 +02:00
|
|
|
@stryFileList = fileList($strPathOp, $strExpression, $strSortOrder, $bIgnoreMissing);
|
2014-06-22 01:19:37 +03:00
|
|
|
}
|
2014-06-15 02:50:54 +03:00
|
|
|
|
2015-08-29 20:20:46 +02:00
|
|
|
# Return from function and log return values if any
|
|
|
|
return logDebugReturn
|
|
|
|
(
|
|
|
|
$strOperation,
|
|
|
|
{name => 'stryFileList', value => \@stryFileList}
|
|
|
|
);
|
2014-06-22 01:19:37 +03:00
|
|
|
}
|
2014-06-21 19:22:25 +03:00
|
|
|
|
2015-01-06 20:08:56 +02:00
|
|
|
####################################################################################################################################
|
2015-08-29 20:20:46 +02:00
|
|
|
# wait
|
2015-01-06 20:08:56 +02:00
|
|
|
#
|
|
|
|
# Wait until the next second. This is done in the file object because it must be performed on whichever side the db is on, local or
|
|
|
|
# remote. This function is used to make sure that no files are copied in the same second as the manifest is created. The reason is
|
|
|
|
# that the db might modify they file again in the same second as the copy and that change will not be visible to a subsequent
|
|
|
|
# incremental backup using timestamp/size to determine deltas.
|
|
|
|
####################################################################################################################################
|
|
|
|
sub wait
|
|
|
|
{
|
|
|
|
my $self = shift;
|
|
|
|
|
2015-08-29 20:20:46 +02:00
|
|
|
# Assign function parameters, defaults, and log debug info
|
|
|
|
my
|
|
|
|
(
|
|
|
|
$strOperation,
|
2016-04-14 01:09:35 +02:00
|
|
|
$strPathType,
|
|
|
|
$bWait
|
2015-08-29 20:20:46 +02:00
|
|
|
) =
|
|
|
|
logDebugParam
|
|
|
|
(
|
2016-08-11 23:32:28 +02:00
|
|
|
__PACKAGE__ . '->wait', \@_,
|
2016-04-14 01:09:35 +02:00
|
|
|
{name => 'strPathType'},
|
|
|
|
{name => 'bWait', default => true}
|
2015-08-29 20:20:46 +02:00
|
|
|
);
|
2015-01-06 20:08:56 +02:00
|
|
|
|
|
|
|
# Second when the function was called
|
|
|
|
my $lTimeBegin;
|
|
|
|
|
|
|
|
# Run remotely
|
2015-08-29 20:20:46 +02:00
|
|
|
if ($self->isRemote($strPathType))
|
2015-01-06 20:08:56 +02:00
|
|
|
{
|
2016-12-04 00:34:51 +02:00
|
|
|
$lTimeBegin = $self->{oProtocol}->cmdExecute(OP_FILE_WAIT, [$bWait], true);
|
2015-01-06 20:08:56 +02:00
|
|
|
}
|
|
|
|
# Run locally
|
|
|
|
else
|
|
|
|
{
|
|
|
|
# Wait the remainder of the current second
|
2016-04-14 01:09:35 +02:00
|
|
|
$lTimeBegin = waitRemainder($bWait);
|
2015-01-06 20:08:56 +02:00
|
|
|
}
|
|
|
|
|
2015-08-29 20:20:46 +02:00
|
|
|
# Return from function and log return values if any
|
|
|
|
return logDebugReturn
|
|
|
|
(
|
|
|
|
$strOperation,
|
|
|
|
{name => 'lTimeBegin', value => $lTimeBegin, trace => true}
|
|
|
|
);
|
2015-01-06 20:08:56 +02:00
|
|
|
}
|
|
|
|
|
2014-06-22 01:19:37 +03:00
|
|
|
####################################################################################################################################
|
2015-08-29 20:20:46 +02:00
|
|
|
# manifest
|
2014-06-22 01:19:37 +03:00
|
|
|
#
|
|
|
|
# Builds a path/file manifest starting with the base path and including all subpaths. The manifest contains all the information
|
|
|
|
# needed to perform a backup or a delta with a previous backup.
|
|
|
|
####################################################################################################################################
|
|
|
|
sub manifest
|
|
|
|
{
|
|
|
|
my $self = shift;
|
2014-06-21 19:22:25 +03:00
|
|
|
|
2015-08-29 20:20:46 +02:00
|
|
|
# Assign function parameters, defaults, and log debug info
|
|
|
|
my
|
|
|
|
(
|
|
|
|
$strOperation,
|
|
|
|
$strPathType,
|
|
|
|
$strPath,
|
|
|
|
) =
|
|
|
|
logDebugParam
|
|
|
|
(
|
2016-08-11 23:32:28 +02:00
|
|
|
__PACKAGE__ . '->manifest', \@_,
|
2015-08-29 20:20:46 +02:00
|
|
|
{name => 'strPathType'},
|
|
|
|
{name => 'strPath', required => false},
|
|
|
|
);
|
2014-06-21 19:22:25 +03:00
|
|
|
|
2015-08-29 20:20:46 +02:00
|
|
|
# Set operation variables
|
|
|
|
my $strPathOp = $self->pathGet($strPathType, $strPath);
|
2017-01-10 20:08:03 +02:00
|
|
|
my $hManifest;
|
2014-06-21 19:22:25 +03:00
|
|
|
|
2014-06-22 01:19:37 +03:00
|
|
|
# Run remotely
|
2015-08-29 20:20:46 +02:00
|
|
|
if ($self->isRemote($strPathType))
|
2014-06-22 01:19:37 +03:00
|
|
|
{
|
2016-12-04 00:34:51 +02:00
|
|
|
$hManifest = $self->{oProtocol}->cmdExecute(OP_FILE_MANIFEST, [$strPathOp], true);
|
2014-02-03 03:03:05 +03:00
|
|
|
}
|
2014-06-22 01:19:37 +03:00
|
|
|
# Run locally
|
|
|
|
else
|
|
|
|
{
|
2017-01-10 20:08:03 +02:00
|
|
|
$hManifest = fileManifest($strPathOp);
|
2014-06-22 01:19:37 +03:00
|
|
|
}
|
2015-08-29 20:20:46 +02:00
|
|
|
|
|
|
|
# Return from function and log return values if any
|
|
|
|
return logDebugReturn
|
|
|
|
(
|
2016-11-30 21:36:39 +02:00
|
|
|
$strOperation,
|
|
|
|
{name => 'hManifest', value => $hManifest, trace => true}
|
2015-08-29 20:20:46 +02:00
|
|
|
);
|
2014-06-22 01:19:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
####################################################################################################################################
|
2015-08-29 20:20:46 +02:00
|
|
|
# copy
|
2014-06-22 01:19:37 +03:00
|
|
|
#
|
|
|
|
# Copies a file from one location to another:
|
|
|
|
#
|
|
|
|
# * source and destination can be local or remote
|
|
|
|
# * wire and output compression/decompression are supported
|
|
|
|
# * intermediate temp files are used to prevent partial copies
|
2014-12-30 18:59:57 +02:00
|
|
|
# * modification time, mode, and ownership can be set on destination file
|
2014-06-22 01:19:37 +03:00
|
|
|
# * destination path can optionally be created
|
|
|
|
####################################################################################################################################
|
|
|
|
sub copy
|
|
|
|
{
|
|
|
|
my $self = shift;
|
2015-08-29 20:20:46 +02:00
|
|
|
|
|
|
|
# Assign function parameters, defaults, and log debug info
|
|
|
|
my
|
|
|
|
(
|
|
|
|
$strOperation,
|
|
|
|
$strSourcePathType,
|
|
|
|
$strSourceFile,
|
|
|
|
$strDestinationPathType,
|
|
|
|
$strDestinationFile,
|
|
|
|
$bSourceCompressed,
|
|
|
|
$bDestinationCompress,
|
|
|
|
$bIgnoreMissingSource,
|
|
|
|
$lModificationTime,
|
|
|
|
$strMode,
|
|
|
|
$bDestinationPathCreate,
|
|
|
|
$strUser,
|
|
|
|
$strGroup,
|
2016-12-10 16:06:45 +02:00
|
|
|
$bAppendChecksum,
|
|
|
|
$bPathSync,
|
2016-12-13 01:47:17 +02:00
|
|
|
$strExtraFunction,
|
2015-08-29 20:20:46 +02:00
|
|
|
) =
|
|
|
|
logDebugParam
|
|
|
|
(
|
2016-08-11 23:32:28 +02:00
|
|
|
__PACKAGE__ . '->copy', \@_,
|
2015-08-29 20:20:46 +02:00
|
|
|
{name => 'strSourcePathType'},
|
|
|
|
{name => 'strSourceFile', required => false},
|
|
|
|
{name => 'strDestinationPathType'},
|
|
|
|
{name => 'strDestinationFile', required => false},
|
|
|
|
{name => 'bSourceCompressed', default => false},
|
|
|
|
{name => 'bDestinationCompress', default => false},
|
|
|
|
{name => 'bIgnoreMissingSource', default => false},
|
|
|
|
{name => 'lModificationTime', required => false},
|
|
|
|
{name => 'strMode', default => '0640'},
|
|
|
|
{name => 'bDestinationPathCreate', default => false},
|
|
|
|
{name => 'strUser', required => false},
|
|
|
|
{name => 'strGroup', required => false},
|
2016-12-10 16:06:45 +02:00
|
|
|
{name => 'bAppendChecksum', default => false},
|
|
|
|
{name => 'bPathSync', default => false},
|
2016-12-13 01:47:17 +02:00
|
|
|
{name => 'strExtraFunction', required => false},
|
2015-08-29 20:20:46 +02:00
|
|
|
);
|
2014-06-22 01:19:37 +03:00
|
|
|
|
|
|
|
# Set working variables
|
2015-08-29 20:20:46 +02:00
|
|
|
my $bSourceRemote = $self->isRemote($strSourcePathType) || $strSourcePathType eq PIPE_STDIN;
|
|
|
|
my $bDestinationRemote = $self->isRemote($strDestinationPathType) || $strDestinationPathType eq PIPE_STDOUT;
|
2014-06-22 01:19:37 +03:00
|
|
|
my $strSourceOp = $strSourcePathType eq PIPE_STDIN ?
|
2015-08-29 20:20:46 +02:00
|
|
|
$strSourcePathType : $self->pathGet($strSourcePathType, $strSourceFile);
|
2014-06-22 01:19:37 +03:00
|
|
|
my $strDestinationOp = $strDestinationPathType eq PIPE_STDOUT ?
|
2015-08-29 20:20:46 +02:00
|
|
|
$strDestinationPathType : $self->pathGet($strDestinationPathType, $strDestinationFile);
|
2014-06-22 01:19:37 +03:00
|
|
|
my $strDestinationTmpOp = $strDestinationPathType eq PIPE_STDOUT ?
|
2015-08-29 20:20:46 +02:00
|
|
|
undef : $self->pathGet($strDestinationPathType, $strDestinationFile, true);
|
2016-12-13 01:47:17 +02:00
|
|
|
my $fnExtra = defined($strExtraFunction) ?
|
|
|
|
eval("\\&${strExtraFunction}") : undef; ## no critic (BuiltinFunctions::ProhibitStringyEval)
|
2014-06-22 01:19:37 +03:00
|
|
|
|
2015-02-28 22:54:49 +02:00
|
|
|
# Checksum and size variables
|
|
|
|
my $strChecksum = undef;
|
|
|
|
my $iFileSize = undef;
|
2016-12-13 01:47:17 +02:00
|
|
|
my $rExtra = undef;
|
2015-03-01 02:07:29 +02:00
|
|
|
my $bResult = true;
|
2015-02-28 22:54:49 +02:00
|
|
|
|
2014-06-22 01:19:37 +03:00
|
|
|
# Open the source and destination files (if needed)
|
|
|
|
my $hSourceFile;
|
|
|
|
my $hDestinationFile;
|
|
|
|
|
|
|
|
if (!$bSourceRemote)
|
|
|
|
{
|
2015-04-01 21:58:33 +02:00
|
|
|
if (!sysopen($hSourceFile, $strSourceOp, O_RDONLY))
|
2014-06-22 01:19:37 +03:00
|
|
|
{
|
|
|
|
my $strError = $!;
|
2016-09-06 15:15:22 +02:00
|
|
|
my $iErrorCode = ERROR_FILE_READ;
|
2014-06-22 01:19:37 +03:00
|
|
|
|
|
|
|
if ($!{ENOENT})
|
|
|
|
{
|
|
|
|
# $strError = 'file is missing';
|
2016-09-06 15:15:22 +02:00
|
|
|
$iErrorCode = ERROR_FILE_MISSING;
|
2014-06-22 01:19:37 +03:00
|
|
|
|
|
|
|
if ($bIgnoreMissingSource && $strDestinationPathType ne PIPE_STDOUT)
|
|
|
|
{
|
2015-02-28 22:54:49 +02:00
|
|
|
return false, undef, undef;
|
2014-06-22 01:19:37 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$strError = "cannot open source file ${strSourceOp}: " . $strError;
|
|
|
|
|
|
|
|
if ($strSourcePathType eq PATH_ABSOLUTE)
|
|
|
|
{
|
|
|
|
if ($strDestinationPathType eq PIPE_STDOUT)
|
|
|
|
{
|
2015-08-05 14:43:41 +02:00
|
|
|
$self->{oProtocol}->binaryXferAbort();
|
2014-06-22 01:19:37 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-29 20:20:46 +02:00
|
|
|
confess &log(ERROR, $strError, $iErrorCode);
|
2014-06-22 01:19:37 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$bDestinationRemote)
|
|
|
|
{
|
2016-01-21 04:28:40 +02:00
|
|
|
my $iCreateFlag = O_WRONLY | O_CREAT;
|
2015-08-29 20:20:46 +02:00
|
|
|
|
2014-06-24 01:54:00 +03:00
|
|
|
# Open the destination temp file
|
2015-08-29 20:20:46 +02:00
|
|
|
if (!sysopen($hDestinationFile, $strDestinationTmpOp, $iCreateFlag))
|
2014-06-24 01:54:00 +03:00
|
|
|
{
|
2015-04-07 13:34:37 +02:00
|
|
|
if ($bDestinationPathCreate)
|
2014-06-24 01:54:00 +03:00
|
|
|
{
|
2016-04-14 01:09:35 +02:00
|
|
|
filePathCreate(dirname($strDestinationTmpOp), undef, true, true);
|
2014-06-24 01:54:00 +03:00
|
|
|
}
|
|
|
|
|
2015-08-29 20:20:46 +02:00
|
|
|
if (!$bDestinationPathCreate || !sysopen($hDestinationFile, $strDestinationTmpOp, $iCreateFlag))
|
2014-06-24 01:54:00 +03:00
|
|
|
{
|
2015-04-07 13:34:37 +02:00
|
|
|
my $strError = "unable to open ${strDestinationTmpOp}: " . $!;
|
2016-09-06 15:15:22 +02:00
|
|
|
my $iErrorCode = ERROR_FILE_READ;
|
2015-04-07 13:34:37 +02:00
|
|
|
|
2016-04-14 01:09:35 +02:00
|
|
|
if (!fileExists(dirname($strDestinationTmpOp)))
|
2014-06-24 01:54:00 +03:00
|
|
|
{
|
2015-05-22 20:49:14 +02:00
|
|
|
$strError = dirname($strDestinationTmpOp) . ' destination path does not exist';
|
2016-09-06 15:15:22 +02:00
|
|
|
$iErrorCode = ERROR_FILE_MISSING;
|
2014-06-24 01:54:00 +03:00
|
|
|
}
|
|
|
|
|
2016-09-06 15:15:22 +02:00
|
|
|
if (!($bDestinationPathCreate && $iErrorCode == ERROR_FILE_MISSING))
|
2016-09-05 01:29:30 +02:00
|
|
|
{
|
|
|
|
confess &log(ERROR, $strError, $iErrorCode);
|
|
|
|
}
|
2014-06-24 01:54:00 +03:00
|
|
|
}
|
|
|
|
}
|
2015-08-29 20:20:46 +02:00
|
|
|
|
|
|
|
# Now lock the file to be sure nobody else is operating on it
|
|
|
|
if (!flock($hDestinationFile, LOCK_EX | LOCK_NB))
|
|
|
|
{
|
2016-04-14 01:09:35 +02:00
|
|
|
confess &log(ERROR, "unable to acquire exclusive lock on ${strDestinationTmpOp}", ERROR_LOCK_ACQUIRE);
|
2015-08-29 20:20:46 +02:00
|
|
|
}
|
2014-06-22 01:19:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
# If source or destination are remote
|
|
|
|
if ($bSourceRemote || $bDestinationRemote)
|
|
|
|
{
|
|
|
|
# Build the command and open the local file
|
|
|
|
my $hFile;
|
|
|
|
my $hIn,
|
|
|
|
my $hOut;
|
|
|
|
my $strRemote;
|
2016-08-11 23:32:28 +02:00
|
|
|
my $strRemoteOp;
|
2016-12-04 00:34:51 +02:00
|
|
|
my $bController = false;
|
2014-06-22 01:19:37 +03:00
|
|
|
|
|
|
|
# If source is remote and destination is local
|
|
|
|
if ($bSourceRemote && !$bDestinationRemote)
|
|
|
|
{
|
|
|
|
$hOut = $hDestinationFile;
|
2016-08-11 23:32:28 +02:00
|
|
|
$strRemoteOp = OP_FILE_COPY_OUT;
|
2014-06-22 01:19:37 +03:00
|
|
|
$strRemote = 'in';
|
|
|
|
|
2015-10-08 17:43:56 +02:00
|
|
|
if ($strSourcePathType ne PIPE_STDIN)
|
2014-06-22 01:19:37 +03:00
|
|
|
{
|
2016-12-13 01:47:17 +02:00
|
|
|
$self->{oProtocol}->cmdWrite($strRemoteOp,
|
|
|
|
[$strSourceOp, undef, $bSourceCompressed, $bDestinationCompress, undef, undef, undef, undef, undef, undef,
|
|
|
|
undef, undef, $strExtraFunction]);
|
2016-12-04 00:34:51 +02:00
|
|
|
|
|
|
|
$bController = true;
|
2014-06-22 01:19:37 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
# Else if source is local and destination is remote
|
|
|
|
elsif (!$bSourceRemote && $bDestinationRemote)
|
|
|
|
{
|
|
|
|
$hIn = $hSourceFile;
|
2016-08-11 23:32:28 +02:00
|
|
|
$strRemoteOp = OP_FILE_COPY_IN;
|
2014-06-22 01:19:37 +03:00
|
|
|
$strRemote = 'out';
|
|
|
|
|
2015-10-08 17:43:56 +02:00
|
|
|
if ($strDestinationPathType ne PIPE_STDOUT)
|
2014-06-22 01:19:37 +03:00
|
|
|
{
|
2016-12-04 00:34:51 +02:00
|
|
|
$self->{oProtocol}->cmdWrite(
|
|
|
|
$strRemoteOp,
|
|
|
|
[undef, $strDestinationOp, $bSourceCompressed, $bDestinationCompress, undef, undef, $strMode,
|
2016-12-13 01:47:17 +02:00
|
|
|
$bDestinationPathCreate, $strUser, $strGroup, $bAppendChecksum, $bPathSync, $strExtraFunction]);
|
2014-12-30 18:59:57 +02:00
|
|
|
|
2016-12-04 00:34:51 +02:00
|
|
|
$bController = true;
|
2014-06-22 01:19:37 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
# Else source and destination are remote
|
|
|
|
else
|
|
|
|
{
|
2016-08-11 23:32:28 +02:00
|
|
|
$strRemoteOp = OP_FILE_COPY;
|
2014-06-22 01:19:37 +03:00
|
|
|
|
2016-12-04 00:34:51 +02:00
|
|
|
$self->{oProtocol}->cmdWrite(
|
|
|
|
$strRemoteOp,
|
|
|
|
[$strSourceOp, $strDestinationOp, $bSourceCompressed, $bDestinationCompress, $bIgnoreMissingSource, undef,
|
2016-12-13 01:47:17 +02:00
|
|
|
$strMode, $bDestinationPathCreate, $strUser, $strGroup, $bAppendChecksum, $bPathSync, $strExtraFunction]);
|
2014-06-22 01:19:37 +03:00
|
|
|
|
2016-12-04 00:34:51 +02:00
|
|
|
$bController = true;
|
2014-06-22 01:19:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
# Transfer the file (skip this for copies where both sides are remote)
|
2016-08-11 23:32:28 +02:00
|
|
|
if ($strRemoteOp ne OP_FILE_COPY)
|
2014-06-22 01:19:37 +03:00
|
|
|
{
|
2016-12-13 01:47:17 +02:00
|
|
|
($strChecksum, $iFileSize, $rExtra) =
|
|
|
|
$self->{oProtocol}->binaryXfer($hIn, $hOut, $strRemote, $bSourceCompressed, $bDestinationCompress, undef, $fnExtra);
|
2014-06-22 01:19:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
# If this is the controlling process then wait for OK from remote
|
2016-12-04 00:34:51 +02:00
|
|
|
if ($bController)
|
2014-06-22 01:19:37 +03:00
|
|
|
{
|
|
|
|
# Test for an error when reading output
|
|
|
|
my $strOutput;
|
2014-06-22 03:08:49 +03:00
|
|
|
|
2014-06-22 01:19:37 +03:00
|
|
|
eval
|
|
|
|
{
|
2016-12-13 01:47:17 +02:00
|
|
|
($bResult, my $strResultChecksum, my $iResultFileSize, my $rResultExtra) =
|
2016-12-04 00:34:51 +02:00
|
|
|
$self->{oProtocol}->outputRead(true, $bIgnoreMissingSource);
|
2015-03-01 02:07:29 +02:00
|
|
|
|
|
|
|
# Check the result of the remote call
|
2016-12-04 00:34:51 +02:00
|
|
|
if ($bResult)
|
2015-03-01 02:07:29 +02:00
|
|
|
{
|
2015-03-02 05:23:52 +02:00
|
|
|
# If the operation was purely remote, get checksum/size
|
2016-08-11 23:32:28 +02:00
|
|
|
if ($strRemoteOp eq OP_FILE_COPY ||
|
|
|
|
$strRemoteOp eq OP_FILE_COPY_IN && $bSourceCompressed && !$bDestinationCompress)
|
2015-03-01 02:07:29 +02:00
|
|
|
{
|
2015-03-02 05:23:52 +02:00
|
|
|
# Checksum shouldn't already be set
|
|
|
|
if (defined($strChecksum) || defined($iFileSize))
|
|
|
|
{
|
|
|
|
confess &log(ASSERT, "checksum and size are already defined, but shouldn't be");
|
|
|
|
}
|
|
|
|
|
2016-12-04 00:34:51 +02:00
|
|
|
$strChecksum = $strResultChecksum;
|
|
|
|
$iFileSize = $iResultFileSize;
|
2016-12-13 01:47:17 +02:00
|
|
|
$rExtra = $rResultExtra;
|
2015-03-01 02:07:29 +02:00
|
|
|
}
|
|
|
|
}
|
2014-06-22 03:08:49 +03:00
|
|
|
|
2016-09-06 15:44:50 +02:00
|
|
|
return true;
|
|
|
|
}
|
2014-06-22 01:19:37 +03:00
|
|
|
# If there is an error then evaluate
|
2016-09-06 15:44:50 +02:00
|
|
|
or do
|
2014-06-22 01:19:37 +03:00
|
|
|
{
|
2016-09-06 15:44:50 +02:00
|
|
|
my $oException = $EVAL_ERROR;
|
2014-06-22 03:08:49 +03:00
|
|
|
|
2016-09-06 15:15:22 +02:00
|
|
|
# Ignore error if source file was missing and missing file exception was returned and bIgnoreMissingSource is set
|
2017-01-27 16:56:26 +02:00
|
|
|
if ($bIgnoreMissingSource && $strRemote eq 'in' && exceptionCode($oException) == ERROR_FILE_MISSING)
|
2014-06-22 01:19:37 +03:00
|
|
|
{
|
2016-04-14 01:09:35 +02:00
|
|
|
close($hDestinationFile)
|
|
|
|
or confess &log(ERROR, "cannot close file ${strDestinationTmpOp}");
|
|
|
|
fileRemove($strDestinationTmpOp);
|
2014-06-22 01:19:37 +03:00
|
|
|
|
2016-12-04 00:34:51 +02:00
|
|
|
$bResult = false;
|
2014-06-22 01:19:37 +03:00
|
|
|
}
|
2016-12-04 00:34:51 +02:00
|
|
|
else
|
|
|
|
{
|
2016-11-30 21:36:39 +02:00
|
|
|
confess $oException;
|
2016-12-04 00:34:51 +02:00
|
|
|
}
|
2016-09-06 15:44:50 +02:00
|
|
|
};
|
2014-06-22 01:19:37 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
# Else this is a local operation
|
2014-06-05 23:40:05 +03:00
|
|
|
else
|
2014-02-05 19:35:09 +03:00
|
|
|
{
|
2015-03-02 05:23:52 +02:00
|
|
|
# If the source is not compressed and the destination is then compress
|
|
|
|
if (!$bSourceCompressed && $bDestinationCompress)
|
|
|
|
{
|
2016-12-13 01:47:17 +02:00
|
|
|
($strChecksum, $iFileSize, $rExtra) =
|
|
|
|
$self->{oProtocol}->binaryXfer($hSourceFile, $hDestinationFile, 'out', false, true, false, $fnExtra);
|
2015-03-02 05:23:52 +02:00
|
|
|
}
|
2014-06-21 18:05:29 +03:00
|
|
|
# If the source is compressed and the destination is not then decompress
|
2015-03-02 05:23:52 +02:00
|
|
|
elsif ($bSourceCompressed && !$bDestinationCompress)
|
2014-06-21 18:05:29 +03:00
|
|
|
{
|
2016-12-13 01:47:17 +02:00
|
|
|
($strChecksum, $iFileSize, $rExtra) =
|
|
|
|
$self->{oProtocol}->binaryXfer($hSourceFile, $hDestinationFile, 'in', true, false, false, $fnExtra);
|
2014-06-21 18:05:29 +03:00
|
|
|
}
|
2016-12-13 01:47:17 +02:00
|
|
|
# Else both sides are compressed, so copy capturing checksum
|
2015-03-02 05:23:52 +02:00
|
|
|
elsif ($bSourceCompressed)
|
2014-06-21 18:05:29 +03:00
|
|
|
{
|
2016-12-13 01:47:17 +02:00
|
|
|
($strChecksum, $iFileSize, $rExtra) =
|
|
|
|
$self->{oProtocol}->binaryXfer($hSourceFile, $hDestinationFile, 'out', true, true, false, $fnExtra);
|
2014-06-21 18:05:29 +03:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-12-13 01:47:17 +02:00
|
|
|
($strChecksum, $iFileSize, $rExtra) =
|
|
|
|
$self->{oProtocol}->binaryXfer($hSourceFile, $hDestinationFile, 'in', false, true, false, $fnExtra);
|
2014-06-21 18:05:29 +03:00
|
|
|
}
|
2014-02-05 19:35:09 +03:00
|
|
|
}
|
2014-02-07 00:37:37 +03:00
|
|
|
|
2016-12-04 00:34:51 +02:00
|
|
|
if ($bResult)
|
|
|
|
{
|
2016-11-30 21:36:39 +02:00
|
|
|
# Close the source file (if local)
|
|
|
|
if (defined($hSourceFile))
|
|
|
|
{
|
|
|
|
close($hSourceFile) or confess &log(ERROR, "cannot close file ${strSourceOp}");
|
|
|
|
}
|
2014-06-06 06:51:27 +03:00
|
|
|
|
2016-11-30 21:36:39 +02:00
|
|
|
# Sync and close the destination file (if local)
|
|
|
|
if (defined($hDestinationFile))
|
|
|
|
{
|
|
|
|
$hDestinationFile->sync()
|
|
|
|
or confess &log(ERROR, "unable to sync ${strDestinationTmpOp}", ERROR_FILE_SYNC);
|
2015-06-17 18:53:33 +02:00
|
|
|
|
2016-11-30 21:36:39 +02:00
|
|
|
close($hDestinationFile)
|
|
|
|
or confess &log(ERROR, "cannot close file ${strDestinationTmpOp}");
|
|
|
|
}
|
2014-06-06 06:51:27 +03:00
|
|
|
|
2016-11-30 21:36:39 +02:00
|
|
|
# Checksum and file size should be set if the destination is not remote
|
2016-12-04 00:34:51 +02:00
|
|
|
if (!(!$bSourceRemote && $bDestinationRemote && $bSourceCompressed) &&
|
2016-11-30 21:36:39 +02:00
|
|
|
(!defined($strChecksum) || !defined($iFileSize)))
|
2014-06-29 17:53:39 +03:00
|
|
|
{
|
2016-11-30 21:36:39 +02:00
|
|
|
confess &log(ASSERT, 'checksum or file size not set');
|
2014-06-29 17:53:39 +03:00
|
|
|
}
|
2014-06-06 05:42:47 +03:00
|
|
|
|
2016-11-30 21:36:39 +02:00
|
|
|
# Where the destination is local, set mode, modification time, and perform move to final location
|
2016-12-04 00:34:51 +02:00
|
|
|
if (!$bDestinationRemote)
|
2014-06-29 17:53:39 +03:00
|
|
|
{
|
2016-11-30 21:36:39 +02:00
|
|
|
# Set the file Mode if required
|
|
|
|
if (defined($strMode))
|
|
|
|
{
|
|
|
|
chmod(oct($strMode), $strDestinationTmpOp)
|
|
|
|
or confess &log(ERROR, "unable to set mode for local ${strDestinationTmpOp}");
|
|
|
|
}
|
2014-06-06 05:42:47 +03:00
|
|
|
|
2016-11-30 21:36:39 +02:00
|
|
|
# Set the file modification time if required
|
|
|
|
if (defined($lModificationTime))
|
|
|
|
{
|
|
|
|
utime($lModificationTime, $lModificationTime, $strDestinationTmpOp)
|
|
|
|
or confess &log(ERROR, "unable to set time for local ${strDestinationTmpOp}");
|
|
|
|
}
|
2014-12-30 18:59:57 +02:00
|
|
|
|
2016-11-30 21:36:39 +02:00
|
|
|
# set user and/or group if required
|
|
|
|
if (defined($strUser) || defined($strGroup))
|
2015-03-01 23:42:27 +02:00
|
|
|
{
|
2016-11-30 21:36:39 +02:00
|
|
|
$self->owner(PATH_ABSOLUTE, $strDestinationTmpOp, $strUser, $strGroup);
|
2015-03-01 23:42:27 +02:00
|
|
|
}
|
2016-11-30 21:36:39 +02:00
|
|
|
|
|
|
|
# Replace checksum in destination filename (if exists)
|
|
|
|
if ($bAppendChecksum)
|
2015-03-01 23:42:27 +02:00
|
|
|
{
|
2016-11-30 21:36:39 +02:00
|
|
|
# Replace destination filename
|
|
|
|
if ($bDestinationCompress)
|
|
|
|
{
|
|
|
|
$strDestinationOp =
|
|
|
|
substr($strDestinationOp, 0, length($strDestinationOp) - length($self->{strCompressExtension}) - 1) .
|
|
|
|
'-' . $strChecksum . '.' . $self->{strCompressExtension};
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$strDestinationOp .= '-' . $strChecksum;
|
|
|
|
}
|
2015-03-01 23:42:27 +02:00
|
|
|
}
|
|
|
|
|
2016-11-30 21:36:39 +02:00
|
|
|
# Move the file from tmp to final destination
|
2016-12-10 16:06:45 +02:00
|
|
|
fileMove($strDestinationTmpOp, $strDestinationOp, $bDestinationPathCreate, $bPathSync);
|
2016-11-30 21:36:39 +02:00
|
|
|
}
|
2016-12-04 00:34:51 +02:00
|
|
|
}
|
2014-02-07 00:37:37 +03:00
|
|
|
|
2015-08-29 20:20:46 +02:00
|
|
|
# Return from function and log return values if any
|
|
|
|
return logDebugReturn
|
|
|
|
(
|
|
|
|
$strOperation,
|
|
|
|
{name => 'bResult', value => $bResult, trace => true},
|
|
|
|
{name => 'strChecksum', value => $strChecksum, trace => true},
|
2016-12-13 01:47:17 +02:00
|
|
|
{name => 'iFileSize', value => $iFileSize, trace => true},
|
|
|
|
{name => '$rExtra', value => $rExtra, trace => true},
|
2015-08-29 20:20:46 +02:00
|
|
|
);
|
2014-02-03 03:03:05 +03:00
|
|
|
}
|
2014-06-02 00:23:33 +03:00
|
|
|
|
2014-10-10 22:13:28 +03:00
|
|
|
1;
|