1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
|
####################################################################################################################################
# Basic Handle IO
####################################################################################################################################
package pgBackRestTest::Common::Io::Handle;
use parent 'pgBackRestTest::Common::Io::Base';
use strict;
use warnings FATAL => qw(all);
use Carp qw(confess);
use English '-no_match_vars';
use Exporter qw(import);
our @EXPORT = qw();
use pgBackRestDoc::Common::Exception;
use pgBackRestDoc::Common::Log;
####################################################################################################################################
# Package name constant
####################################################################################################################################
use constant COMMON_IO_HANDLE => __PACKAGE__;
push @EXPORT, qw(COMMON_IO_HANDLE);
####################################################################################################################################
# new
####################################################################################################################################
sub new
{
my $class = shift;
# Assign function parameters, defaults, and log debug info
my
(
$strOperation,
$strId,
$fhRead,
$fhWrite,
) =
logDebugParam
(
__PACKAGE__ . '->new', \@_,
{name => 'strId', trace => true},
{name => 'fhRead', required => false, trace => true},
{name => 'fhWrite', required => false, trace => true},
);
# Create class
my $self = $class->SUPER::new($strId);
bless $self, $class;
# Set handles
$self->handleReadSet($fhRead) if defined($fhRead);
$self->handleWriteSet($fhWrite) if defined($fhWrite);
# Size tracks number of bytes read and written
$self->{lSize} = 0;
# Initialize EOF to false
$self->eofSet(false);
# Return from function and log return values if any
return logDebugReturn
(
$strOperation,
{name => 'self', value => $self}
);
}
####################################################################################################################################
# eof - have reads reached eof?
#
# Note that there may be more bytes to be read but this is set to true whenever there is a zero read and back to false on a
# non-zero read.
####################################################################################################################################
sub eof
{
return shift->{bEOF};
}
####################################################################################################################################
# eofSet - set eof
####################################################################################################################################
sub eofSet
{
my $self = shift;
my $bEOF = shift;
$self->{bEOF} = $bEOF;
}
####################################################################################################################################
# read - read data from handle
####################################################################################################################################
sub read
{
my $self = shift;
my $rtBuffer = shift;
my $iSize = shift;
# Read the block
my $iActualSize;
eval
{
$iActualSize = sysread($self->handleRead(), $$rtBuffer, $iSize, defined($$rtBuffer) ? length($$rtBuffer) : 0);
return true;
}
or do
{
$self->error(ERROR_FILE_READ, 'unable to read from ' . $self->id(), $EVAL_ERROR);
};
# Report any errors
# uncoverable branch true - all errors seem to be caught by the handler above but check for error here just in case
defined($iActualSize)
or $self->error(ERROR_FILE_READ, 'unable to read from ' . $self->id(), $OS_ERROR);
# Update size
$self->{lSize} += $iActualSize;
# Update EOF
$self->eofSet($iActualSize == 0 ? true : false);
return $iActualSize;
}
####################################################################################################################################
# write - write data to handle
####################################################################################################################################
sub write
{
my $self = shift;
my $rtBuffer = shift;
# Write the block
my $iActualSize;
eval
{
$iActualSize = syswrite($self->handleWrite(), $$rtBuffer);
return true;
}
or do
{
$self->error(ERROR_FILE_WRITE, 'unable to write to ' . $self->id(), $EVAL_ERROR);
};
# Report any errors
# uncoverable branch true - all errors seem to be caught by the handler above but check for error here just in case
defined($iActualSize)
or $self->error(ERROR_FILE_WRITE, 'unable to write to ' . $self->id(), $OS_ERROR);
# Update size
$self->{lSize} += $iActualSize;
return $iActualSize;
}
####################################################################################################################################
# close - record read/write size
####################################################################################################################################
sub close
{
my $self = shift;
# Set bytes read and written
if (defined($self->{lSize}))
{
$self->resultSet(COMMON_IO_HANDLE, $self->{lSize});
undef($self->{lSize});
}
return true;
}
####################################################################################################################################
# Getters/Setters
####################################################################################################################################
sub handleRead {return shift->{fhHandleRead}}
sub handleReadSet {my $self = shift; $self->{fhHandleRead} = shift}
sub handleWrite {return shift->{fhHandleWrite}}
sub handleWriteSet {my $self = shift; $self->{fhHandleWrite} = shift}
sub size {shift->{lSize}}
1;
|