File Coverage

blib/lib/MaxMind/DB/Reader/Role/Sysreader.pm
Criterion Covered Total %
statement 40 43 93.0
branch 8 10 80.0
condition n/a
subroutine 9 10 90.0
pod n/a
total 57 63 90.4


line stmt bran cond sub pod time code
1             package MaxMind::DB::Reader::Role::Sysreader;
2              
3 21     21   268215 use strict;
  21         59  
  21         678  
4 21     21   119 use warnings;
  21         48  
  21         550  
5 21     21   121 use namespace::autoclean;
  21         46  
  21         129  
6 21     21   1317 use autodie;
  21         68  
  21         129  
7              
8             our $VERSION = '1.000014';
9              
10 21     21   114666 use Carp qw( confess );
  21         55  
  21         1620  
11 21     21   153 use MaxMind::DB::Types qw( FileHandle Int );
  21         158  
  21         1204  
12              
13 21     21   138 use Moo::Role;
  21         49  
  21         182  
14              
15             has data_source => (
16             is => 'ro',
17             isa => FileHandle,
18             lazy => 1,
19             builder => '_build_data_source',
20             );
21              
22             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             sub _read {
31 17069     17069   192899 my $self = shift;
32 17069         25382 my $buffer = shift;
33 17069         25946 my $offset = shift;
34 17069         28070 my $wanted_size = shift;
35 17069         26401 my $seek_from_end = shift;
36              
37 17069         276977 my $source = $self->data_source;
38 17069 100       161306 seek $source, $offset, $seek_from_end ? 2 : 0;
39              
40 17069         795514 my $read_offset = 0;
41 17069         28144 while (1) {
42             my $read_size = read(
43             $source,
44 17070         25237 ${$buffer},
  17070         55129  
45             $wanted_size,
46             $read_offset,
47             );
48              
49 17070 50       893182 confess $! unless defined $read_size;
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 100       37516 confess 'Attempted to read past the end of a file/memory buffer'
56             if $read_size == 0;
57              
58 17069 100       50047 return if $wanted_size == $read_size;
59              
60 1         3 $wanted_size -= $read_size;
61 1         3 $read_offset += $read_size;
62             }
63              
64 0         0 return;
65             }
66             ## use critic
67              
68             sub _build_data_source {
69 0     0   0 my $class = ref shift;
70              
71 0         0 die
72             "You must provide a data_source parameter to the constructor for $class";
73             }
74              
75             sub _build_data_source_size {
76 58     58   702 my $self = shift;
77              
78 58 50       944 my @stat = stat( $self->data_source ) or die $!;
79 58         3034 return $stat[7];
80             }
81              
82             1;