Export/VFAT/AllocationTable.pm

filter: Statementstmt 90.6% 116 / 128 Branchbran 61.7% 58 / 94 Conditioncond 38.0% 19 / 50 MC/DCmcdc 12.5% 4 / 32 Subroutinesub 95.6% 22 / 23 Podpod 100% 13 / 13 totaltotal 68.2% 232 / 340 CC 74 SCAR 26.5
CC
74
Cov
71%
_invlist_alloc
32.4
alloc
22.8
pack
15.4
CRAP 14.2
« Export.pm ↑ Summary Export/LazyFileData.pm »
1 package Sys::Export::VFAT::AllocationTable;
2  
3 # ABSTRACT: Track which FAT clusters are used, and by what
4 4 our $VERSION = '0.006'; # VERSION
5  
6  
7 # Element 0 of the FAT is used for an inversion list of which sectors are allocated.
8 # It's not as good as a tree, but should perform well when the typical use case is
9 # to pack files end to end without fragmentation.
10 4 ▶use v5.26;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 4 ▶use experimental qw( signatures );
Subroutine BEGIN: called CC=1 SCAR=0.0
13 4 ▶use Scalar::Util 'refaddr';
Subroutine BEGIN: called CC=1 SCAR=0.0
14 4 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 4 ▶use Sys::Export qw( isa_int );
Subroutine BEGIN: called CC=1 SCAR=0.0
16 4 ▶use Sys::Export::VFAT::Geometry qw( FAT12_MAX_CLUSTERS FAT16_MAX_CLUSTERS FAT32_MAX_CLUSTERS );
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18  
19 33 ▶sub fat { $_[0]{fat} }
Subroutine fat: called CC=1 SCAR=0.0
Pod documented
20 0 ▶sub _invlist { $_[0]{_invlist} }
Subroutine _invlist: not called CC=1 SCAR=6.9
21 10613 ▶sub chains { $_[0]{chains} }
Subroutine chains: called CC=1 SCAR=0.0
Pod documented
22 634 ▶sub max_cluster_id { @_ > 1? $_[0]->set_max_cluster_id($_[1]) : $_[0]{max_cluster_id} }
Subroutine max_cluster_id: called CC=2 SCAR=6.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
@_ > 1 ? : 202 432 634
23 506 ▶sub max_used_cluster_id { $_[0]{_invlist}[-1] - 1 }
Subroutine max_used_cluster_id: called CC=1 SCAR=0.0
Pod documented
24  
25  
26 5 ▶sub first_free_cluster {
Subroutine first_free_cluster: called CC=3 SCAR=11.1
Pod documented
27 5 my ($first_free, $max)= ($_[0]{_invlist}[1], $_[0]{max_cluster_id});
28 5 ▶ defined $max && $first_free > $max? undef : $first_free;
Branch: 1 branch
BranchTrueFalseTotal
defined $max && $first_free > $max ? : 1 4 5
Condition66%2/3
defined $max and $first_free > $max
!ll&&!rl&&r
401
MC/DC50%1/2
defined $max and $first_free > $max
defined $max $first_free > $max
Truth table66%2/3
A and B
A= defined $maxB= $first_free > $max
ABresult
0X0
100
111
29 }
30  
31  
32 202 ▶sub set_max_cluster_id($self, $val) {
Subroutine set_max_cluster_id: called CC=5 SCAR=17.2
Pod documented
33 202 ▶ if (defined $val) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $val) 201 1 202
34 201 my $min= $self->max_used_cluster_id;
35 201 ▶ croak "Cannot set max_cluster_id less than max_used_cluster_id"
Branch: 1 branch
BranchTrueFalseTotal
if defined $min and $val < $min 0 201 201
Condition33%1/3
defined $min and $val < $min
!ll&&!rl&&r
02010
MC/DC0%0/2
defined $min and $val < $min
defined $min $val < $min
Truth table33%1/3
A and B
A= defined $minB= $val < $min
ABresult
0X0
100
111
36 if defined $min && $val < $min;
37 201 ▶ croak "Cannot set max_cluster_id less than 2"
Branch: 1 branch
BranchTrueFalseTotal
if $val < 2 0 201 201
38 if $val < 2;
39 }
40 202 $self->{max_cluster_id}= $val;
41 }
42  
43  
44 8 ▶sub free_cluster_count($self) {
Subroutine free_cluster_count: called CC=3 SCAR=11.0
Pod documented
45 8 my ($sum, $inv, $max)= (0, $self->{_invlist}, $self->{max_cluster_id});
46 8 ▶ for (my $i= 1; $i < $#$inv; $i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $inv->$#*) 2 8 10
47 2 $sum += $inv->[$i+1] - $inv->[$i];
48 }
49 8 ▶ $sum += $max - ($inv->[-1]-1) if defined $max;
Branch: 1 branch
BranchTrueFalseTotal
if defined $max 3 5 8
50 8 return $sum;
51 }
52  
53  
54 3 ▶sub get_chain($self, $cl_id) {
Subroutine get_chain: called CC=1 SCAR=0.0
Pod documented
55 3 $self->{chains}{$cl_id};
56 }
57  
58  
59 250 ▶sub new($class, @attrs) {
Subroutine new: called CC=7 SCAR=26.4
Pod documented
60 0 ▶ my %attrs= @attrs == 1 && ref $attrs[0] eq 'HASH'? %{$attrs[0]} : @attrs;
Branch: 1 branch
BranchTrueFalseTotal
@attrs == 1 && ref $attrs[0] eq 'HASH' ? : 0 250 250
Condition33%1/3
@attrs == 1 and ref $attrs[0] eq "HASH"
!ll&&!rl&&r
25000
MC/DC0%0/2
@attrs == 1 and ref $attrs[0] eq "HASH"
@attrs == 1 ref $attrs[0] eq "HASH"
Truth table33%1/3
A and B
A= @attrs == 1B= ref $attrs[0] eq "HASH"
ABresult
0X0
100
111
61 250 my $max_cluster_id= delete $attrs{max_cluster_id};
62 250 ▶ croak "Invalid max_cluster_id"
Branch: 1 branch
BranchTrueFalseTotal
unless not defined $max_cluster_id or &isa_int($max_cluster_id) and $max_cluster_id >= 2 0 250 250
Condition16%1/6
not defined $max_cluster_id or &isa_int($max_cluster_id) and $max_cluster_id >= 2
l!l&&r!l&&!r
25000
&isa_int($max_cluster_id) and $max_cluster_id >= 2
!ll&&!rl&&r
000
MC/DC0%0/3
not defined $max_cluster_id or &isa_int($max_cluster_id) and $max_cluster_id >= 2
not defined $max_cluster_id &isa_int($max_cluster_id) $max_cluster_id >= 2
Truth table25%1/4
not(A) or B and C
A= not defined $max_cluster_idB= &isa_int($max_cluster_id)C= $max_cluster_id >= 2
ABCresult
1XX1
0111
00X0
0100
63 unless !defined $max_cluster_id or isa_int $max_cluster_id && $max_cluster_id >= 2;
64 250 ▶ carp "Unrecognized attributes ".join(', ', keys %attrs)
Branch: 1 branch
BranchTrueFalseTotal
if keys %attrs 0 250 250
65 if keys %attrs;
66  
67 250 bless {
68 max_cluster_id => $max_cluster_id,
69 fat => [],
70 _invlist => [ 0, 2 ], # mark 0-1 as allocated, to remove empty-list edge cases
71 chains => {},
72 }, $class;
73 }
74  
75  
76 19736 ▶sub alloc($self, $count) {
Subroutine alloc: called CC=16 SCAR=31.3
Pod documented
77 19736 ▶ return 0 unless $count;
Branch: 1 branch
BranchTrueFalseTotal
unless $count 0 19736 19736
78 19736 ▶ croak "Cluster count must be an unsigned integer"
Branch: 1 branch
BranchTrueFalseTotal
unless &isa_int($count) and $count > 0 0 19736 19736
Condition33%1/3
&isa_int($count) and $count > 0
!ll&&!rl&&r
0019736
MC/DC0%0/2
&isa_int($count) and $count > 0
&isa_int($count) $count > 0
Truth table33%1/3
A and B
A= &isa_int($count)B= $count > 0
ABresult
0X0
100
111
79 unless isa_int $count && $count > 0;
80 19736 my $inv= $self->{_invlist};
81 19736 ▶ my $lim= $self->{max_cluster_id}? $self->{max_cluster_id}+1 : undef;
Branch: 1 branch
BranchTrueFalseTotal
$self->{'max_cluster_id'} ? : 0 19736 19736
82 # If there are enough free sectors, this basically just removes gaps in the inversion list.
83 19736 ▶ for (my $i= 1; $i < @$inv; $i+= 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$inv) 19737 0 19737
84 19737 my ($from, $upto)= @{$inv}[$i,$i+1];
85 19737 ▶ my $n= defined $upto? ($upto - $from) : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $upto ? : 1 19736 19737
86 19737 ▶ if (!defined $n || $n >= $count) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $n or $n >= $count) 19736 1 19737
Condition66%2/3
not defined $n or $n >= $count
l!l&&r!l&&!r
1973601
MC/DC50%1/2
not defined $n or $n >= $count
not defined $n $n >= $count
Truth table66%2/3
not(A) or B
A= not defined $nB= $n >= $count
ABresult
1X1
011
000
87 19736 my @result;
88 19736 ▶ if (!defined $n) { # allocate from final region up to max sector
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $n) { } 19736 0 19736
elsif ($n == $count) { } 0 0 0
89 19736 ▶ last if defined $lim && $lim - $from < $count;
Branch: 1 branch
BranchTrueFalseTotal
if defined $lim and $lim - $from < $count 0 19736 19736
Condition33%1/3
defined $lim and $lim - $from < $count
!ll&&!rl&&r
1973600
MC/DC0%0/2
defined $lim and $lim - $from < $count
defined $lim $lim - $from < $count
Truth table33%1/3
A and B
A= defined $limB= $lim - $from < $count
ABresult
0X0
100
111
90 19736 @result= (splice(@$inv, 1, $i, $from + $count), $from+$count);
91 }
92 elsif ($n == $count) { # result comes from exactly the gaps between other allocation
93 0 @result= splice(@$inv, 1, $i+1);
94 }
95 else { # result comes from partial gap
96 0 @result= (splice(@$inv, 1, $i-1), $from, $from+$count);
97 0 $inv->[1]= $from + $count;
98 }
99 # and build the cluster chain in the FAT
100 19736 my $prev= 0;
101 19736 ▶ for (my $j= 0; $j < @result; $j += 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($j < @result) 19737 19736 39473
102 19737 for ($result[$j] .. ($result[$j+1]-1)) {
103 82896 ▶ $self->{fat}[$prev]= $_ if $prev;
Branch: 1 branch
BranchTrueFalseTotal
if $prev 63160 19736 82896
104 82896 $prev= $_;
105 }
106 }
107 19736 $self->{fat}[$prev]= 0x0FFFFFFF;
108 19736 $self->{chains}{$result[0]}{invlist}= \@result;
109 19736 return $result[0];
110 }
111 1 $count -= $n;
112 }
113 0 return undef; # not enough available
114 }
115  
116  
117 8 ▶sub alloc_range($self, $cluster_id, $count) {
Subroutine alloc_range: called CC=6 SCAR=22.0
Pod documented
118 8 ▶ return 0 unless $count;
Branch: 1 branch
BranchTrueFalseTotal
unless $count 0 8 8
119 8 ▶ croak "Cluster count must be an unsigned integer"
Branch: 1 branch
BranchTrueFalseTotal
unless &isa_int($count) and $count > 0 0 8 8
Condition33%1/3
&isa_int($count) and $count > 0
!ll&&!rl&&r
008
MC/DC0%0/2
&isa_int($count) and $count > 0
&isa_int($count) $count > 0
Truth table33%1/3
A and B
A= &isa_int($count)B= $count > 0
ABresult
0X0
100
111
120 unless isa_int $count && $count > 0;
121 8 ▶ croak "Invalid cluster id '$cluster_id'"
Branch: 1 branch
BranchTrueFalseTotal
unless &isa_int($cluster_id) and $cluster_id >= 2 0 8 8
Condition33%1/3
&isa_int($cluster_id) and $cluster_id >= 2
!ll&&!rl&&r
008
MC/DC0%0/2
&isa_int($cluster_id) and $cluster_id >= 2
&isa_int($cluster_id) $cluster_id >= 2
Truth table33%1/3
A and B
A= &isa_int($cluster_id)B= $cluster_id >= 2
ABresult
0X0
100
111
122 unless isa_int $cluster_id && $cluster_id >= 2;
123 8 return $self->_alloc_range($cluster_id, $cluster_id + $count);
124 }
125  
126  
127 360 ▶sub alloc_contiguous($self, $count, $align=1, $align_ofs=0) {
Subroutine alloc_contiguous: called CC=11 SCAR=25.0
Pod documented
128 360 ▶ return 0 unless $count;
Branch: 1 branch
BranchTrueFalseTotal
unless $count 0 360 360
129 360 ▶ croak "Cluster count must be an unsigned integer"
Branch: 1 branch
BranchTrueFalseTotal
unless &isa_int($count) and $count > 0 0 360 360
Condition33%1/3
&isa_int($count) and $count > 0
!ll&&!rl&&r
00360
MC/DC0%0/2
&isa_int($count) and $count > 0
&isa_int($count) $count > 0
Truth table33%1/3
A and B
A= &isa_int($count)B= $count > 0
ABresult
0X0
100
111
130 unless isa_int $count && $count > 0;
131 360 my $inv= $self->{_invlist};
132 360 ▶ for (my $i= 1; $i < @$inv; $i+=2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$inv) 437 0 437
133 437 my ($from, $upto)= @{$inv}[$i, $i+1];
134 437 my $start= $from;
135 # Align start addr
136 437 ▶ if ($align > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($align > 1) 281 156 437
137 281 my $remainder= ($start - $align_ofs) & ($align-1);
138 281 ▶ $start += $align - $remainder if $remainder;
Branch: 1 branch
BranchTrueFalseTotal
if $remainder 252 29 281
139 }
140 # Is the range large enough?
141 437 ▶ next if defined $upto && $upto - $start < $count;
Branch: 1 branch
BranchTrueFalseTotal
if defined $upto and $upto - $start < $count 77 360 437
Condition66%2/3
defined $upto and $upto - $start < $count
!ll&&!rl&&r
360077
MC/DC50%1/2
defined $upto and $upto - $start < $count
defined $upto $upto - $start < $count
Truth table66%2/3
A and B
A= defined $uptoB= $upto - $start < $count
ABresult
0X0
100
111
142 360 return $self->_alloc_range($start, $start+$count, $i);
143 }
144 0 return undef;
145 }
146  
147 # add the range ($start, $lim) to an inversion list where idx is pointed at
148 # the first range that wasn't entirely before $start
149 368 ▶sub _invlist_alloc($inv, $start, $lim, $idx=undef) {
Subroutine _invlist_alloc: called CC=13 SCAR=34.8
150 368 ▶ unless (defined $idx) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $idx) 8 360 368
151 8 ▶ for ($idx= 0; $idx < $#$inv && $inv->[$idx+1] <= $start; $idx++) {}
Branch: 1 branch
BranchTrueFalseTotal
if ($idx < $inv->$#* and $$inv[$idx + 1] <= $start) 8 8 16
Condition66%2/3
$idx < $inv->$#* and $$inv[$idx + 1] <= $start
!ll&&!rl&&r
808
MC/DC50%1/2
$idx < $inv->$#* and $$inv[$idx + 1] <= $start
$idx < $inv->$#* $$inv[$idx + 1] <= $start
Truth table66%2/3
A and B
A= $idx < $inv->$#*B= $$inv[$idx + 1] <= $start
ABresult
0X0
100
111
152 # here, [idx] is less/eq start, and [idx+1] is not (or doesn't exist)
153 # If idx is even, it means start fell within an allocated range
154 8 ▶ return 0 unless $idx & 1;
Branch: 1 branch
BranchTrueFalseTotal
unless $idx & 1 0 8 8
155 }
156 368 my $from_edge= $inv->[$idx] == $start;
157 # allocating at the end
158 368 ▶ if ($idx == $#$inv) {
Branch: 1 branch
BranchTrueFalseTotal
if ($idx == $inv->$#*) { } 368 0 368
159 # max_cluster_id was checked by caller
160 368 ▶ $from_edge? ($inv->[$idx]= $lim)
Branch: 1 branch
BranchTrueFalseTotal
$from_edge ? : 184 184 368
161 : push(@$inv, $start, $lim);
162 }
163 else {
164 # does 'lim' exceed the gap between allocations?
165 0 ▶ return 0 if $lim > $inv->[$idx+1];
Branch: 1 branch
BranchTrueFalseTotal
if $lim > $$inv[$idx + 1] 0 0 0
166 0 my $to_edge= $lim == $inv->[$idx+1];
167 0 ▶ $from_edge && $to_edge? splice(@$inv, $idx, 2)
Branch: 3 branches
BranchTrueFalseTotal
$from_edge && $to_edge ? : 0 0 0
$from_edge ? : 0 0 0
$to_edge ? : 0 0 0
Condition0%0/3
$from_edge and $to_edge
!ll&&!rl&&r
000
MC/DC0%0/2
$from_edge and $to_edge
$from_edge $to_edge
Truth table0%0/3
A and B
A= $from_edgeB= $to_edge
ABresult
0X0
100
111
168 : $from_edge? ($inv->[$idx]= $lim)
169 : $to_edge? ($inv->[$idx+1]= $start)
170 : splice(@$inv, $idx+1, 0, $start, $lim);
171 }
172 368 return 1;
173 }
174  
175 368 ▶sub _alloc_range($self, $cl_start, $cl_lim, $invlist_idx=undef) {
Subroutine _alloc_range: called CC=6 SCAR=19.2
176 368 ▶ return undef if $self->max_cluster_id && $cl_lim-1 > $self->max_cluster_id;
Branch: 1 branch
BranchTrueFalseTotal
if $self->max_cluster_id and $cl_lim - 1 > $self->max_cluster_id 0 368 368
Condition33%1/3
$self->max_cluster_id and $cl_lim - 1 > $self->max_cluster_id
!ll&&!rl&&r
36800
MC/DC0%0/2
$self->max_cluster_id and $cl_lim - 1 > $self->max_cluster_id
$self->max_cluster_id $cl_lim - 1 > $self->max_cluster_id
Truth table33%1/3
A and B
A= $self->max_cluster_idB= $cl_lim - 1 > $self->max_cluster_id
ABresult
0X0
100
111
177 368 ▶ return undef unless _invlist_alloc($self->{_invlist}, $cl_start, $cl_lim, $invlist_idx);
Branch: 1 branch
BranchTrueFalseTotal
unless _invlist_alloc($self->{"_invlist"}, $cl_start, $cl_lim, $invlist_idx) 0 368 368
178 # Build the cluster chain in the FAT
179 $self->{fat}[$_]= $_+1
180 368 for $cl_start .. $cl_lim-2;
181 368 $self->{fat}[$cl_lim-1]= 0x0FFFFFFF;
182 # An allocation inversion list of one segment
183 368 $self->{chains}{$cl_start}{invlist}= [ $cl_start, $cl_lim ];
184 368 return $cl_start;
185 }
186  
187  
188 1 ▶sub pack($self, $bits=undef) {
Subroutine pack: called CC=12 SCAR=27.3
Pod documented
189 1 my $fat= $self->fat;
190 1 ▶ my $max= $self->max_cluster_id // $self->max_used_cluster_id;
Condition33%1/3
$self->max_cluster_id // $self->max_used_cluster_id
l!l&&r!l&&!r
010
MC/DC0%0/2
$self->max_cluster_id // $self->max_used_cluster_id
$self->max_cluster_id $self->max_used_cluster_id
Truth table33%1/3
A // B
A= $self->max_cluster_idB= $self->max_used_cluster_id
ABresult
1X1
011
000
191 1 my $cl_count= $max-1; # excluding clusters 0 and 1
192 1 ▶ croak "Max cluster ID exceeds FAT32 max" if $cl_count > FAT32_MAX_CLUSTERS;
Branch: 1 branch
BranchTrueFalseTotal
if $cl_count > 268435445 0 1 1
193 1 ▶ carp "Truncating table to cluster id $max" if $max < $#$fat;
Branch: 1 branch
BranchTrueFalseTotal
if $max < $fat->$#* 0 1 1
194 1 $#$fat= $max;
195 1 $fat->[$_]= 0x0FFFFFFF for 0,1;
196 1 ▶ $fat->[$_] //= 0 for 2..$max; # prevent warnings in pack
Condition50%1/2
$fat->[$_] //= 0
l!l
390
MC/DC0%0/1
$fat->[$_] //= 0
$fat->[$_]
Truth table50%1/2
A
A= $fat->[$_]
Aresult
11
00
197 1 ▶ if ($cl_count > FAT16_MAX_CLUSTERS) {
Branch: 2 branches
BranchTrueFalseTotal
if ($cl_count > 65524) { } 0 1 1
elsif ($cl_count > 4084) { } 0 1 1
198 0 return pack 'V*', @$fat;
199 } elsif ($cl_count > FAT12_MAX_CLUSTERS) {
200 0 return pack 'v*', @$fat;
201 } else {
202 # 12 bits per entry, pack in groups of 3 bytes, little-endian
203 1 my $buf= "\xFF\xFF\xFF";
204 1 ▶ for (my $i= 2; $i+1 <= $max; $i+= 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i + 1 <= $max) 19 1 20
205 19 my $v= ($fat->[$i] & 0xFFF) | ( ($fat->[$i+1] & 0xFFF) << 12 );
206 19 $buf .= pack 'vC', $v, ($v >> 16);
207 }
208 1 ▶ $buf .= pack 'v', $fat->[$max] & 0xFFF unless $max & 1;
Branch: 1 branch
BranchTrueFalseTotal
unless $max & 1 1 0 1
209 1 return $buf;
210 }
211 }
212  
213 # Avoiding dependency on namespace::clean
214 4 delete @{Sys::Export::VFAT::AllocationTable::}{qw( carp confess croak refaddr isa_int )};
215 4 1;
216  
217 __END__
« Export.pm ↑ Summary Export/LazyFileData.pm »