| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition66%2/3
defined $max and $first_free > $max
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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition33%1/3
defined $min and $val < $min
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
| |||||||||||||||||||||||||||||||||||||||||||
| 36 | if defined $min && $val < $min; | ||||||||||||||||||||||||||||||||||||||||||
| 37 | 201 | ▶ | croak "Cannot set max_cluster_id less than 2" | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 47 | 2 | $sum += $inv->[$i+1] - $inv->[$i]; | |||||||||||||||||||||||||||||||||||||||||
| 48 | } | ||||||||||||||||||||||||||||||||||||||||||
| 49 | 8 | ▶ | $sum += $max - ($inv->[-1]-1) if defined $max; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition33%1/3
@attrs == 1 and ref $attrs[0] eq "HASH"
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"
| |||||||||||||||||||||||||||||||||||||||||||
| 61 | 250 | my $max_cluster_id= delete $attrs{max_cluster_id}; | |||||||||||||||||||||||||||||||||||||||||
| 62 | 250 | ▶ | croak "Invalid max_cluster_id" | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition16%1/6
not defined $max_cluster_id or &isa_int($max_cluster_id) and $max_cluster_id >= 2
&isa_int($max_cluster_id) and $max_cluster_id >= 2
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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 78 | 19736 | ▶ | croak "Cluster count must be an unsigned integer" | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
&isa_int($count) and $count > 0
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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 84 | 19737 | my ($from, $upto)= @{$inv}[$i,$i+1]; | |||||||||||||||||||||||||||||||||||||||||
| 85 | 19737 | ▶ | my $n= defined $upto? ($upto - $from) : undef; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 86 | 19737 | ▶ | if (!defined $n || $n >= $count) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
not defined $n or $n >= $count
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
| |||||||||||||||||||||||||||||||||||||||||||
| 87 | 19736 | my @result; | |||||||||||||||||||||||||||||||||||||||||
| 88 | 19736 | ▶ | if (!defined $n) { # allocate from final region up to max sector | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 89 | 19736 | ▶ | last if defined $lim && $lim - $from < $count; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $lim and $lim - $from < $count
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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 102 | 19737 | for ($result[$j] .. ($result[$j+1]-1)) { | |||||||||||||||||||||||||||||||||||||||||
| 103 | 82896 | ▶ | $self->{fat}[$prev]= $_ if $prev; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 119 | 8 | ▶ | croak "Cluster count must be an unsigned integer" | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
&isa_int($count) and $count > 0
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
| |||||||||||||||||||||||||||||||||||||||||||
| 120 | unless isa_int $count && $count > 0; | ||||||||||||||||||||||||||||||||||||||||||
| 121 | 8 | ▶ | croak "Invalid cluster id '$cluster_id'" | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
&isa_int($cluster_id) and $cluster_id >= 2
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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 129 | 360 | ▶ | croak "Cluster count must be an unsigned integer" | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
&isa_int($count) and $count > 0
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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 137 | 281 | my $remainder= ($start - $align_ofs) & ($align-1); | |||||||||||||||||||||||||||||||||||||||||
| 138 | 281 | ▶ | $start += $align - $remainder if $remainder; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 139 | } | ||||||||||||||||||||||||||||||||||||||||||
| 140 | # Is the range large enough? | ||||||||||||||||||||||||||||||||||||||||||
| 141 | 437 | ▶ | next if defined $upto && $upto - $start < $count; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $upto and $upto - $start < $count
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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 151 | 8 | ▶ | for ($idx= 0; $idx < $#$inv && $inv->[$idx+1] <= $start; $idx++) {} | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$idx < $inv->$#* and $$inv[$idx + 1] <= $start
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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 155 | } | ||||||||||||||||||||||||||||||||||||||||||
| 156 | 368 | my $from_edge= $inv->[$idx] == $start; | |||||||||||||||||||||||||||||||||||||||||
| 157 | # allocating at the end | ||||||||||||||||||||||||||||||||||||||||||
| 158 | 368 | ▶ | if ($idx == $#$inv) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 159 | # max_cluster_id was checked by caller | ||||||||||||||||||||||||||||||||||||||||||
| 160 | 368 | ▶ | $from_edge? ($inv->[$idx]= $lim) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 166 | 0 | my $to_edge= $lim == $inv->[$idx+1]; | |||||||||||||||||||||||||||||||||||||||||
| 167 | 0 | ▶ | $from_edge && $to_edge? splice(@$inv, $idx, 2) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
Condition0%0/3
$from_edge and $to_edge
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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition33%1/3
$self->max_cluster_id and $cl_lim - 1 > $self->max_cluster_id
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
| |||||||||||||||||||||||||||||||||||||||||||
| 177 | 368 | ▶ | return undef unless _invlist_alloc($self->{_invlist}, $cl_start, $cl_lim, $invlist_idx); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 193 | 1 | ▶ | carp "Truncating table to cluster id $max" if $max < $#$fat; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
MC/DC0%0/1
$fat->[$_] //= 0
$fat->[$_]
Truth table50%1/2
A
A= $fat->[$_]
| |||||||||||||||||||||||||||||||||||||||||||
| 197 | 1 | ▶ | if ($cl_count > FAT16_MAX_CLUSTERS) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||||||||||||||||