| 1 | package MaxMind::DB::Reader::Role::Sysreader; | ||||||||||
| 2 | |||||||||||
| 3 | 21 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 21 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 21 | ▶ | use namespace::autoclean; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | 21 | ▶ | use autodie; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | |||||||||||
| 8 | 21 | our $VERSION = '1.000014'; | |||||||||
| 9 | |||||||||||
| 10 | 21 | ▶ | use Carp qw( confess ); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 11 | 21 | ▶ | use MaxMind::DB::Types qw( FileHandle Int ); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 12 | |||||||||||
| 13 | 21 | ▶ | use Moo::Role; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 14 | |||||||||||
| 15 | 21 | has data_source => ( | |||||||||
| 16 | is => 'ro', | ||||||||||
| 17 | isa => FileHandle, | ||||||||||
| 18 | lazy => 1, | ||||||||||
| 19 | builder => '_build_data_source', | ||||||||||
| 20 | ); | ||||||||||
| 21 | |||||||||||
| 22 | 21 | has _data_source_size => ( | |||||||||
| 23 | is => 'ro', | ||||||||||
| 24 | isa => Int, | ||||||||||
| 25 | lazy => 1, | ||||||||||
| 26 | builder => '_build_data_source_size', | ||||||||||
| 27 | ); | ||||||||||
| 28 | |||||||||||
| 29 | ## no critic (Subroutines::ProhibitUnusedPrivateSubroutines) | ||||||||||
| 30 | 17069 | ▶ | sub _read { | ||||||||
|
Subroutine
_read: called CC=5 SCAR=16.1
| |||||||||||
| 31 | 17069 | my $self = shift; | |||||||||
| 32 | 17069 | my $buffer = shift; | |||||||||
| 33 | 17069 | my $offset = shift; | |||||||||
| 34 | 17069 | my $wanted_size = shift; | |||||||||
| 35 | 17069 | my $seek_from_end = shift; | |||||||||
| 36 | |||||||||||
| 37 | 17069 | my $source = $self->data_source; | |||||||||
| 38 | 17069 | ▶ | seek $source, $offset, $seek_from_end ? 2 : 0; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 39 | |||||||||||
| 40 | 17069 | my $read_offset = 0; | |||||||||
| 41 | 17069 | while (1) { | |||||||||
| 42 | my $read_size = read( | ||||||||||
| 43 | $source, | ||||||||||
| 44 | 17070 | ${$buffer}, | |||||||||
| 45 | $wanted_size, | ||||||||||
| 46 | $read_offset, | ||||||||||
| 47 | ); | ||||||||||
| 48 | |||||||||||
| 49 | 17070 | ▶ | confess $! unless defined $read_size; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 50 | |||||||||||
| 51 | # This error message doesn't provide much context, but it should only | ||||||||||
| 52 | # be thrown because of a fundamental logic error in the reader code, | ||||||||||
| 53 | # _or_ because the writer generated a database with broken pointers | ||||||||||
| 54 | # and/or broken data elements. | ||||||||||
| 55 | 17070 | ▶ | confess 'Attempted to read past the end of a file/memory buffer' | ||||||||
|
Branch: 1 branch
| |||||||||||
| 56 | if $read_size == 0; | ||||||||||
| 57 | |||||||||||
| 58 | 17069 | ▶ | return if $wanted_size == $read_size; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 59 | |||||||||||
| 60 | 1 | $wanted_size -= $read_size; | |||||||||
| 61 | 1 | $read_offset += $read_size; | |||||||||
| 62 | } | ||||||||||
| 63 | |||||||||||
| 64 | 0 | return; | |||||||||
| 65 | } | ||||||||||
| 66 | ## use critic | ||||||||||
| 67 | |||||||||||
| 68 | 0 | ▶ | sub _build_data_source { | ||||||||
|
Subroutine
_build_data_source: not called CC=1 SCAR=6.9
| |||||||||||
| 69 | 0 | my $class = ref shift; | |||||||||
| 70 | |||||||||||
| 71 | 0 | die | |||||||||
| 72 | "You must provide a data_source parameter to the constructor for $class"; | ||||||||||
| 73 | } | ||||||||||
| 74 | |||||||||||
| 75 | 58 | ▶ | sub _build_data_source_size { | ||||||||
|
Subroutine
_build_data_source_size: called CC=2 SCAR=7.1
| |||||||||||
| 76 | 58 | my $self = shift; | |||||||||
| 77 | |||||||||||
| 78 | 58 | ▶ | my @stat = stat( $self->data_source ) or die $!; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 79 | 58 | return $stat[7]; | |||||||||
| 80 | } | ||||||||||
| 81 | |||||||||||
| 82 | 21 | 1; | |||||||||