| 1 | package Data::Embed::Reader; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 9 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 9 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 9 | ▶ | use English qw< -no_match_vars >; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 9 | ▶ | use Fcntl qw< :seek >; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 9 | ▶ | use Log::Log4perl::Tiny qw< :easy :dead_if_first >; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 9 | ▶ | use Storable qw< dclone >; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 9 | ▶ | use Data::Embed::File; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 9 | ▶ | use Data::Embed::Util qw< :constants unescape >; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 9 | our $VERSION = '0.32'; # make indexer happy | |||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | 20 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=6 SCAR=18.6
Pod
documented
| |||||||||||||||||||||||||||||
| 15 | 20 | my $package = shift; | |||||||||||||||||||||||||||
| 16 | 20 | my $input = shift; | |||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | # Undocumented, keep additional parameters around... | ||||||||||||||||||||||||||||
| 19 | 0 | ▶ | my %args = (scalar(@_) && ref($_[0])) ? %{$_[0]} : @_; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
scalar @_ and ref $_[0]
MC/DC0%0/2
scalar @_ and ref $_[0]
scalar @_
ref $_[0]
Truth table33%1/3
A and B
A= scalar @_B= ref $_[0]
| |||||||||||||||||||||||||||||
| 20 | 20 | my $self = bless {args => \%args}, $package; | |||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | # If a GLOB, just assign a default filename for logs and set | ||||||||||||||||||||||||||||
| 23 | # binary mode :raw | ||||||||||||||||||||||||||||
| 24 | 20 | ▶ | if (ref($input) eq 'GLOB') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 25 | 10 | DEBUG $package, ': input is a GLOB'; | |||||||||||||||||||||||||||
| 26 | 10 | $self->{filename} = '<GLOB>'; | |||||||||||||||||||||||||||
| 27 | 10 | ▶ | binmode $input, ":raw" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 28 | or LOGCROAK "binmode() to ':raw' failed"; | ||||||||||||||||||||||||||||
| 29 | 10 | $self->{fh} = $input; | |||||||||||||||||||||||||||
| 30 | } ## end if (ref($input) eq 'GLOB') | ||||||||||||||||||||||||||||
| 31 | else { # otherwise... it's a filename | ||||||||||||||||||||||||||||
| 32 | 10 | DEBUG $package, | |||||||||||||||||||||||||||
| 33 | ': input is a file or other thing that can be open-ed'; | ||||||||||||||||||||||||||||
| 34 | 10 | $self->{filename} = $input; | |||||||||||||||||||||||||||
| 35 | 10 | ▶ | open $self->{fh}, '<', $input | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 36 | or LOGCROAK "open('$input'): $OS_ERROR"; | ||||||||||||||||||||||||||||
| 37 | 10 | binmode $self->{fh}, ':raw'; | |||||||||||||||||||||||||||
| 38 | } ## end else [ if (ref($input) eq 'GLOB')] | ||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | 20 | return $self; | |||||||||||||||||||||||||||
| 41 | } ## end sub new | ||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | 10 | ▶ | sub files { | ||||||||||||||||||||||||||
|
Subroutine
files: called CC=2 SCAR=7.2
Pod
documented
| |||||||||||||||||||||||||||||
| 44 | 10 | my $files = shift->_ensure_index()->{files}; | |||||||||||||||||||||||||||
| 45 | 10 | ▶ | return wantarray() ? @$files : $files; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 46 | } | ||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | 4 | ▶ | sub prefix { | ||||||||||||||||||||||||||
|
Subroutine
prefix: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 49 | 4 | return shift->_ensure_index()->{_prefix}; | |||||||||||||||||||||||||||
| 50 | } | ||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | 0 | ▶ | sub reset { | ||||||||||||||||||||||||||
|
Subroutine
reset: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 53 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 54 | 0 | delete $self->{$_} for qw< files index >; | |||||||||||||||||||||||||||
| 55 | 0 | return $self; | |||||||||||||||||||||||||||
| 56 | } | ||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | ######## PRIVATE METHODS ############################################## | ||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | # Get the index of the embedded files as a L<Data::Embed::File> object. | ||||||||||||||||||||||||||||
| 61 | # You should normally not need this index, because it is parsed | ||||||||||||||||||||||||||||
| 62 | 10 | ▶ | sub _index { return shift->_ensure_index()->{_index}; } | ||||||||||||||||||||||||||
|
Subroutine
_index: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | 24 | ▶ | sub _ensure_index { | ||||||||||||||||||||||||||
|
Subroutine
_ensure_index: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 65 | 24 | my $self = shift; | |||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | # rebuild cache if not in place | ||||||||||||||||||||||||||||
| 68 | 24 | ▶ | if (!exists $self->{files}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 69 | my $index = $self->_load_index() | ||||||||||||||||||||||||||||
| 70 | || { | ||||||||||||||||||||||||||||
| 71 | _prefix => Data::Embed::File->new( | ||||||||||||||||||||||||||||
| 72 | fh => $self->{fh}, | ||||||||||||||||||||||||||||
| 73 | filename => $self->{filename}, | ||||||||||||||||||||||||||||
| 74 | name => 'Data::Embed prefix data', | ||||||||||||||||||||||||||||
| 75 | offset => 0, | ||||||||||||||||||||||||||||
| 76 | length => scalar(__size($self->{fh})), | ||||||||||||||||||||||||||||
| 77 | ), | ||||||||||||||||||||||||||||
| 78 | files => [], | ||||||||||||||||||||||||||||
| 79 | _index => Data::Embed::File->new( | ||||||||||||||||||||||||||||
| 80 | fh => $self->{fh}, | ||||||||||||||||||||||||||||
| 81 | filename => $self->{filename}, | ||||||||||||||||||||||||||||
| 82 | name => 'Data::Embed index', | ||||||||||||||||||||||||||||
| 83 | length => 0, | ||||||||||||||||||||||||||||
| 84 | 20 | ▶ | offset => scalar(__size($self->{fh})), | ||||||||||||||||||||||||||
|
Condition100%2/2
$self->_load_index || {'_prefix', 'Data::Embed::File'->new('fh', $self->{'fh'}, 'filename', $self->{'filename'}, 'name', 'Data::Embed prefix data', 'offset', 0, 'length', scalar __size($self->{'fh'})), 'files', [], '_index', 'Data::Embed::File'->new('fh', $self->{'fh'}, 'filename', $self->{'filename'}, 'name', 'Data::Embed index', 'length', 0, 'offset', scalar __size($self->{'fh'}))}
MC/DC100%1/1
$self->_load_index || {'_prefix', 'Data::Embed::File'->new('fh', $self->{'fh'}, 'filename', $self->{'filename'}, 'name', 'Data::Embed prefix data', 'offset', 0, 'length', scalar __size($self->{'fh'})), 'files', [], '_index', 'Data::Embed::File'->new('fh', $self->{'fh'}, 'filename', $self->{'filename'}, 'name', 'Data::Embed index', 'length', 0, 'offset', scalar __size($self->{'fh'}))}
$self->_load_index
Truth table100%2/2
A
A= $self->_load_index
| |||||||||||||||||||||||||||||
| 85 | ), | ||||||||||||||||||||||||||||
| 86 | }; | ||||||||||||||||||||||||||||
| 87 | 20 | %$self = (%$self, %$index); | |||||||||||||||||||||||||||
| 88 | } ## end if (!exists $self->{files...}) | ||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | # return a reference to $self, for easy chaining | ||||||||||||||||||||||||||||
| 91 | 24 | return $self; | |||||||||||||||||||||||||||
| 92 | } ## end sub _ensure_index | ||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | 20 | ▶ | sub _load_index { | ||||||||||||||||||||||||||
|
Subroutine
_load_index: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||
| 95 | 20 | my $self = shift; | |||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | # read the index section from the end of the file, or bail out | ||||||||||||||||||||||||||||
| 98 | 20 | ▶ | defined(my $index_text = $self->_read_index()) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 99 | or return; | ||||||||||||||||||||||||||||
| 100 | 9 | my $index_length = length($index_text); | |||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | # trim to isolate the data in the section | ||||||||||||||||||||||||||||
| 103 | 9 | my $terminator_length = length TERMINATOR(); | |||||||||||||||||||||||||||
| 104 | 9 | substr $index_text, 0, length(STARTER()), ''; | |||||||||||||||||||||||||||
| 105 | 9 | substr $index_text, -$terminator_length, $terminator_length, ''; | |||||||||||||||||||||||||||
| 106 | 9 | DEBUG "index contents is '$index_text'"; | |||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | # iterate over the index that has been read. Each line in this | ||||||||||||||||||||||||||||
| 109 | # index is assumed to contain a pair length/name | ||||||||||||||||||||||||||||
| 110 | 9 | my $data_length = 0; | |||||||||||||||||||||||||||
| 111 | 9 | my ($fh, $filename) = @{$self}{qw< fh filename >}; | |||||||||||||||||||||||||||
| 112 | my @files = map { | ||||||||||||||||||||||||||||
| 113 | 9 | ▶ | my ($length, $name) = m{\A \s* (\d+) \s+ (\S*) \s*\z}mxs | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 114 | or LOGCROAK "index line is not compliant: >$_<"; | ||||||||||||||||||||||||||||
| 115 | 16 | $name = Data::Embed::Util::unescape($name); | |||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | # the offset at which "this" file lives is equal to the length | ||||||||||||||||||||||||||||
| 118 | # of all data considered so far | ||||||||||||||||||||||||||||
| 119 | 16 | my $offset = $data_length; | |||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | # the addition of this file increases the data length with the | ||||||||||||||||||||||||||||
| 122 | # size of the section, plus two bytes for separating newlines | ||||||||||||||||||||||||||||
| 123 | 16 | $data_length += $length + 2; | |||||||||||||||||||||||||||
| 124 | { | ||||||||||||||||||||||||||||
| 125 | 16 | fh => $fh, | |||||||||||||||||||||||||||
| 126 | filename => $filename, | ||||||||||||||||||||||||||||
| 127 | name => $name, | ||||||||||||||||||||||||||||
| 128 | length => $length, | ||||||||||||||||||||||||||||
| 129 | offset => $offset, # to be adjusted further | ||||||||||||||||||||||||||||
| 130 | }; | ||||||||||||||||||||||||||||
| 131 | } split /\n+/, $index_text; | ||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | # Now we established the full length of the data section, so it's | ||||||||||||||||||||||||||||
| 134 | # possible to adjust all offsets for all files (remember that the | ||||||||||||||||||||||||||||
| 135 | # files are assumed to be at the end of the embedding file) | ||||||||||||||||||||||||||||
| 136 | 9 | my $full_length = __size($fh); | |||||||||||||||||||||||||||
| 137 | 9 | my $offset_correction = $full_length - $index_length - $data_length; | |||||||||||||||||||||||||||
| 138 | 9 | for my $file (@files) { | |||||||||||||||||||||||||||
| 139 | $file = | ||||||||||||||||||||||||||||
| 140 | Data::Embed::File->new(%$file, | ||||||||||||||||||||||||||||
| 141 | 16 | offset => ($file->{offset} + $offset_correction),); | |||||||||||||||||||||||||||
| 142 | } | ||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | # return the files in the index and the index itself, all as | ||||||||||||||||||||||||||||
| 145 | # Data::Embed::File objects for consistency | ||||||||||||||||||||||||||||
| 146 | return { | ||||||||||||||||||||||||||||
| 147 | 9 | _prefix => Data::Embed::File->new( | |||||||||||||||||||||||||||
| 148 | fh => $fh, | ||||||||||||||||||||||||||||
| 149 | filename => $filename, | ||||||||||||||||||||||||||||
| 150 | name => 'Data::Embed prefix data', | ||||||||||||||||||||||||||||
| 151 | length => $offset_correction, | ||||||||||||||||||||||||||||
| 152 | offset => 0, | ||||||||||||||||||||||||||||
| 153 | ), | ||||||||||||||||||||||||||||
| 154 | files => \@files, | ||||||||||||||||||||||||||||
| 155 | _index => Data::Embed::File->new( | ||||||||||||||||||||||||||||
| 156 | fh => $fh, | ||||||||||||||||||||||||||||
| 157 | filename => $filename, | ||||||||||||||||||||||||||||
| 158 | name => 'Data::Embed index', | ||||||||||||||||||||||||||||
| 159 | length => $index_length, | ||||||||||||||||||||||||||||
| 160 | offset => $data_length + $offset_correction, | ||||||||||||||||||||||||||||
| 161 | ), | ||||||||||||||||||||||||||||
| 162 | }; | ||||||||||||||||||||||||||||
| 163 | } ## end sub _load_index | ||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||
| 165 | 51 | ▶ | sub __size { | ||||||||||||||||||||||||||
|
Subroutine
__size: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 166 | 51 | my $fh = shift; | |||||||||||||||||||||||||||
| 167 | 51 | my $size = -s $fh; | |||||||||||||||||||||||||||
| 168 | 51 | ▶ | if (!defined $size) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 169 | 41 | DEBUG "getting size via seek"; | |||||||||||||||||||||||||||
| 170 | 41 | my $current = tell $fh; | |||||||||||||||||||||||||||
| 171 | 41 | seek $fh, 0, SEEK_END; | |||||||||||||||||||||||||||
| 172 | 41 | $size = tell $fh; | |||||||||||||||||||||||||||
| 173 | 41 | DEBUG "size: $size"; | |||||||||||||||||||||||||||
| 174 | 41 | seek $fh, $current, SEEK_SET; | |||||||||||||||||||||||||||
| 175 | } ## end if (!defined $size) | ||||||||||||||||||||||||||||
| 176 | 51 | return $size; | |||||||||||||||||||||||||||
| 177 | } ## end sub __size | ||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||
| 179 | # read the last section of the file, looking for the index | ||||||||||||||||||||||||||||
| 180 | 20 | ▶ | sub _read_index { | ||||||||||||||||||||||||||
|
Subroutine
_read_index: called CC=7 SCAR=19.6
| |||||||||||||||||||||||||||||
| 181 | 20 | my $self = shift; | |||||||||||||||||||||||||||
| 182 | 20 | my ($fh, $filename) = @{$self}{qw< fh filename >}; | |||||||||||||||||||||||||||
| 183 | 20 | DEBUG "_read_index(): fh[$fh] filename[$filename]"; | |||||||||||||||||||||||||||
| 184 | 20 | my $full_length = __size($fh); # length of the whole stream/file | |||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||
| 186 | # look for TERMINATOR at the very end of the file | ||||||||||||||||||||||||||||
| 187 | 20 | my $terminator = TERMINATOR; | |||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||
| 189 | # is there enough data? | ||||||||||||||||||||||||||||
| 190 | 20 | my $terminator_length = length $terminator; | |||||||||||||||||||||||||||
| 191 | 20 | ▶ | return unless $full_length > $terminator_length; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||
| 193 | # read exactly that number of bytes from the end of the file | ||||||||||||||||||||||||||||
| 194 | # and compare with the TERMINATOR | ||||||||||||||||||||||||||||
| 195 | 15 | my $ending = $self->_read(($terminator_length) x 2); | |||||||||||||||||||||||||||
| 196 | 15 | ▶ | return unless $ending eq $terminator; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 197 | 9 | DEBUG "found terminator"; | |||||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||||||
| 199 | # TERMINATOR is in place, this is promising. Now let's look for | ||||||||||||||||||||||||||||
| 200 | # STARTER going backwards in the file | ||||||||||||||||||||||||||||
| 201 | 9 | my $starter = STARTER; | |||||||||||||||||||||||||||
| 202 | 9 | my $readable = $full_length - $terminator_length; | |||||||||||||||||||||||||||
| 203 | 9 | my $chunk_size = 80; # we'll read this number of bytes per time | |||||||||||||||||||||||||||
| 204 | 9 | my $starter_position; # this will tell us where the STARTER begins | |||||||||||||||||||||||||||
| 205 | 9 | ▶ | while ($readable) { # loop until there's stuff to read backwards | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 206 | |||||||||||||||||||||||||||||
| 207 | # how many bytes to read? $chunk_size if possible, what remains | ||||||||||||||||||||||||||||
| 208 | # otherwise | ||||||||||||||||||||||||||||
| 209 | 9 | ▶ | my $n = ($readable > $chunk_size) ? $chunk_size : $readable; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 210 | 9 | my $chunk = $self->_read($n, $n + length $ending); | |||||||||||||||||||||||||||
| 211 | |||||||||||||||||||||||||||||
| 212 | # we're reading backwards, so the new $chunk as to be pre-pended | ||||||||||||||||||||||||||||
| 213 | 9 | $ending = $chunk . $ending; | |||||||||||||||||||||||||||
| 214 | 0 | ▶ | TRACE sub { "ENDING: >$ending<" }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||||||||||||||||||
| 215 | |||||||||||||||||||||||||||||
| 216 | # Look for the STARTER. We have to work on the full $ending | ||||||||||||||||||||||||||||
| 217 | # instead of the shorter last $chunk because the STARTER might | ||||||||||||||||||||||||||||
| 218 | # have been split across two reads | ||||||||||||||||||||||||||||
| 219 | 9 | $starter_position = CORE::index $ending, $starter; | |||||||||||||||||||||||||||
| 220 | |||||||||||||||||||||||||||||
| 221 | # finding the STARTER is a good exit condition | ||||||||||||||||||||||||||||
| 222 | 9 | ▶ | last if $starter_position >= 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 223 | |||||||||||||||||||||||||||||
| 224 | # otherwise note that we already read some bytes and go on | ||||||||||||||||||||||||||||
| 225 | 0 | $readable -= $n; | |||||||||||||||||||||||||||
| 226 | } ## end while ($readable) | ||||||||||||||||||||||||||||
| 227 | |||||||||||||||||||||||||||||
| 228 | # if $starter_position is not valid (i.e. -1) then we did not find | ||||||||||||||||||||||||||||
| 229 | # the STARTER and we exit with a failure (not an exception, the whole | ||||||||||||||||||||||||||||
| 230 | # thing might not be in place at all, although the presence of the | ||||||||||||||||||||||||||||
| 231 | # TERMINATOR is suspect anyway...) | ||||||||||||||||||||||||||||
| 232 | 9 | ▶ | return unless $starter_position >= 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 233 | 9 | DEBUG "found starter"; | |||||||||||||||||||||||||||
| 234 | |||||||||||||||||||||||||||||
| 235 | # trim the available buffer $ending to isolate the index and return it | ||||||||||||||||||||||||||||
| 236 | 9 | substr $ending, 0, $starter_position, ''; | |||||||||||||||||||||||||||
| 237 | 9 | return $ending; | |||||||||||||||||||||||||||
| 238 | } ## end sub _read_index | ||||||||||||||||||||||||||||
| 239 | |||||||||||||||||||||||||||||
| 240 | # read data from the underlying stream, using offsets from the end | ||||||||||||||||||||||||||||
| 241 | # of the stream | ||||||||||||||||||||||||||||
| 242 | 24 | ▶ | sub _read { | ||||||||||||||||||||||||||
|
Subroutine
_read: called CC=6 SCAR=18.6
| |||||||||||||||||||||||||||||
| 243 | 24 | my $self = shift; | |||||||||||||||||||||||||||
| 244 | 24 | my @args = my ($count, $offset_from_end) = @_; | |||||||||||||||||||||||||||
| 245 | 24 | my ($fh, $filename) = @{$self}{qw< fh filename >}; | |||||||||||||||||||||||||||
| 246 | DEBUG | ||||||||||||||||||||||||||||
| 247 | 0 | ▶ | sub { my $args = join ', ', @args; "_read($args) [file: $filename]" }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=2.6
| |||||||||||||||||||||||||||||
| 248 | |||||||||||||||||||||||||||||
| 249 | 24 | ▶ | LOGDIE '_read(): offset from end cannot be less than count' | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 250 | if $offset_from_end < $count; | ||||||||||||||||||||||||||||
| 251 | 24 | DEBUG "seeking $offset_from_end to the end"; | |||||||||||||||||||||||||||
| 252 | 24 | ▶ | seek $fh, -$offset_from_end, SEEK_END | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 253 | or LOGCROAK "seek('$filename'): $OS_ERROR"; | ||||||||||||||||||||||||||||
| 254 | |||||||||||||||||||||||||||||
| 255 | 24 | my $buffer = ''; | |||||||||||||||||||||||||||
| 256 | 24 | ▶ | while ($count) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 257 | 24 | my $chunk; | |||||||||||||||||||||||||||
| 258 | 24 | ▶ | defined(my $nread = read $fh, $chunk, $count) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 259 | or LOGCROAK "read('$filename'): $OS_ERROR"; | ||||||||||||||||||||||||||||
| 260 | 0 | ▶ | TRACE sub { "read $nread bytes, '$chunk'" }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||||||||||||||||||
| 261 | 24 | DEBUG "read $nread out of $count bytes needed"; | |||||||||||||||||||||||||||
| 262 | 24 | ▶ | LOGCROAK "unexpectedly reached end of file" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 263 | unless $nread; | ||||||||||||||||||||||||||||
| 264 | 24 | $buffer .= $chunk; | |||||||||||||||||||||||||||
| 265 | 24 | $count -= $nread; | |||||||||||||||||||||||||||
| 266 | } ## end while ($count) | ||||||||||||||||||||||||||||
| 267 | 24 | return $buffer; | |||||||||||||||||||||||||||
| 268 | } ## end sub _read | ||||||||||||||||||||||||||||
| 269 | |||||||||||||||||||||||||||||
| 270 | 9 | 1; | |||||||||||||||||||||||||||