Export/VFAT/Geometry.pm

filter: Statementstmt 81.7% 134 / 164 Branchbran 53.0% 52 / 98 Conditioncond 48.7% 40 / 82 MC/DCmcdc 19.1% 9 / 47 Subroutinesub 89.8% 44 / 49 Podpod 100% 40 / 40 totaltotal 66.4% 319 / 480 CC 82 SCAR 50.8
CC
82
Cov
66%
new
320.5
unpack
42.0
get_cluster_alignment_of_device_alignment
5.0
CRAP 160.6
« Export/ELF.pm ↑ Summary Export/Linux.pm »
1 package Sys::Export::VFAT::Geometry;
2  
3 # ABSTRACT: Calculate addresses and sizes of structures within a FAT filesystem
4 5 our $VERSION = '0.006'; # VERSION
5  
6  
7 5 ▶use v5.28;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 5 ▶use experimental qw( signatures );
Subroutine BEGIN: called CC=1 SCAR=0.0
10 5 ▶use Sys::Export qw( isa_hash isa_int isa_pow2 round_up_to_pow2 round_up_to_multiple );
Subroutine BEGIN: called CC=1 SCAR=0.0
11 5 ▶use Scalar::Util qw( dualvar );
Subroutine BEGIN: called CC=1 SCAR=0.0
12 5 ▶use POSIX 'ceil';
Subroutine BEGIN: called CC=1 SCAR=0.0
13 5 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 5 our @CARP_NOT= qw( Sys::Export::VFAT );
15 5 ▶use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
16 5 FAT12 => dualvar(12, "FAT12"),
17 FAT16 => dualvar(16, "FAT16"),
18 FAT32 => dualvar(32, "FAT32"),
19 FAT12_MAX_CLUSTERS => 4085-1,
20 FAT12_IDEAL_MAX_CLUSTERS => 4085-16, # docs recommend 16 away from cutoff on either side
21 FAT16_MIN_CLUSTERS => 4085,
22 FAT16_IDEAL_MIN_CLUSTERS => 4085+16,
23 FAT16_MAX_CLUSTERS => 65525-1,
24 FAT16_IDEAL_MAX_CLUSTERS => 65525-16,
25 FAT32_MIN_CLUSTERS => 65525,
26 FAT32_IDEAL_MIN_CLUSTERS => 65525+16,
27 FAT32_MAX_CLUSTERS => 0xFFFFFF5, # can't allow 0xFFFFFF7 to be allocatable ID
28 5 };
29 5 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
30 5 our @EXPORT_OK= qw( FAT12 FAT16 FAT32 FAT12_MAX_CLUSTERS FAT12_IDEAL_MAX_CLUSTERS
31 FAT16_MIN_CLUSTERS FAT16_IDEAL_MIN_CLUSTERS FAT16_MAX_CLUSTERS FAT16_IDEAL_MAX_CLUSTERS
32 FAT32_MIN_CLUSTERS FAT32_IDEAL_MIN_CLUSTERS FAT32_MAX_CLUSTERS );
33  
34  
35 378 ▶sub new($class, @attrs) {
Subroutine new: called CC=64 SCAR=57.7
Pod documented
36 0 ▶ my %attrs= @attrs == 1 && isa_hash $attrs[0]? %{$attrs[0]} : @attrs;
Branch: 1 branch
BranchTrueFalseTotal
@attrs == 1 && &isa_hash($attrs[0]) ? : 0 378 378
Condition33%1/3
@attrs == 1 and &isa_hash($attrs[0])
!ll&&!rl&&r
37800
MC/DC0%0/2
@attrs == 1 and &isa_hash($attrs[0])
@attrs == 1 &isa_hash($attrs[0])
Truth table33%1/3
A and B
A= @attrs == 1B= &isa_hash($attrs[0])
ABresult
0X0
100
111
37 my ($bytes_per_sector, $sectors_per_cluster, $fat_count, $reserved_sector_count,
38 $fat_sector_count, $root_dirent_count, $cluster_count, $total_sector_count,
39 $min_bits, $volume_offset, $align_clusters
40 378 ) = delete @attrs{qw(
41 bytes_per_sector sectors_per_cluster fat_count reserved_sector_count
42 fat_sector_count root_dirent_count cluster_count total_sector_count
43 min_bits volume_offset align_clusters
44 )};
45 378 ▶ !defined $align_clusters or isa_pow2($align_clusters)
Branch: 1 branch
BranchTrueFalseTotal
not defined $align_clusters or &isa_pow2($align_clusters) or croak("align_clusters must be a power of 2 (was $align_clusters)") 378 0 378
Condition66%2/3
not defined $align_clusters or &isa_pow2($align_clusters)
l!l&&r!l&&!r
1212570
MC/DC0%0/2
not defined $align_clusters or &isa_pow2($align_clusters)
not defined $align_clusters &isa_pow2($align_clusters)
Truth table66%2/3
not(A) or B
A= not defined $align_clustersB= &isa_pow2($align_clusters)
ABresult
1X1
011
000
46 or croak "align_clusters must be a power of 2 (was $align_clusters)";
47 378 ▶ $volume_offset //= 0;
Condition100%2/2
$volume_offset //= 0
l!l
3762
MC/DC100%1/1
$volume_offset //= 0
$volume_offset
Truth table100%2/2
A
A= $volume_offset
Aresult
11
00
48 378 ▶ isa_int($volume_offset) && $volume_offset >= 0
Branch: 1 branch
BranchTrueFalseTotal
&isa_int($volume_offset) and $volume_offset >= 0 or croak("volume_offset must be a non-negative integer") 378 0 378
Condition33%1/3
&isa_int($volume_offset) and $volume_offset >= 0
!ll&&!rl&&r
00378
MC/DC0%0/2
&isa_int($volume_offset) and $volume_offset >= 0
&isa_int($volume_offset) $volume_offset >= 0
Truth table33%1/3
A and B
A= &isa_int($volume_offset)B= $volume_offset >= 0
ABresult
0X0
100
111
49 or croak "volume_offset must be a non-negative integer";
50  
51 378 ▶ $bytes_per_sector //= 512;
Condition100%2/2
$bytes_per_sector //= 512
l!l
3771
MC/DC100%1/1
$bytes_per_sector //= 512
$bytes_per_sector
Truth table100%2/2
A
A= $bytes_per_sector
Aresult
11
00
52 378 ▶ isa_pow2($bytes_per_sector) && 512 <= $bytes_per_sector && $bytes_per_sector <= 4096
Branch: 1 branch
BranchTrueFalseTotal
&isa_pow2($bytes_per_sector) and 512 <= $bytes_per_sector and $bytes_per_sector <= 4096 or croak("Invalid bytes_per_sector $bytes_per_sector") 378 0 378
Condition33%2/6
&isa_pow2($bytes_per_sector) and 512 <= $bytes_per_sector and $bytes_per_sector <= 4096
!ll&&!rl&&r
00378
&isa_pow2($bytes_per_sector) and 512 <= $bytes_per_sector
!ll&&!rl&&r
00378
MC/DC0%0/3
&isa_pow2($bytes_per_sector) and 512 <= $bytes_per_sector and $bytes_per_sector <= 4096
&isa_pow2($bytes_per_sector) 512 <= $bytes_per_sector $bytes_per_sector <= 4096
Truth table25%1/4
A and B and C
A= &isa_pow2($bytes_per_sector)B= 512 <= $bytes_per_sectorC= $bytes_per_sector <= 4096
ABCresult
0XX0
10X0
1100
1111
53 or croak "Invalid bytes_per_sector $bytes_per_sector";
54  
55 # Default sectors_per_cluster to whatever makes 4K
56 378 ▶ $sectors_per_cluster //= ($bytes_per_sector >= 4096? 1 : 4096 / $bytes_per_sector);
Branch: 1 branch
BranchTrueFalseTotal
$bytes_per_sector >= 4096 ? : 0 1 1
Condition100%2/2
$sectors_per_cluster //= $bytes_per_sector >= 4096 ? 1 : 4096 / $bytes_per_sector
l!l
3771
MC/DC100%1/1
$sectors_per_cluster //= $bytes_per_sector >= 4096 ? 1 : 4096 / $bytes_per_sector
$sectors_per_cluster
Truth table100%2/2
A
A= $sectors_per_cluster
Aresult
11
00
57 378 ▶ isa_pow2($sectors_per_cluster) && $sectors_per_cluster <= 128
Branch: 1 branch
BranchTrueFalseTotal
&isa_pow2($sectors_per_cluster) and $sectors_per_cluster <= 128 or croak("Invalid sectors_per_cluster $sectors_per_cluster") 378 0 378
Condition33%1/3
&isa_pow2($sectors_per_cluster) and $sectors_per_cluster <= 128
!ll&&!rl&&r
00378
MC/DC0%0/2
&isa_pow2($sectors_per_cluster) and $sectors_per_cluster <= 128
&isa_pow2($sectors_per_cluster) $sectors_per_cluster <= 128
Truth table33%1/3
A and B
A= &isa_pow2($sectors_per_cluster)B= $sectors_per_cluster <= 128
ABresult
0X0
100
111
58 or croak "Invalid sectors_per_cluster $sectors_per_cluster";
59 378 my $cluster_size= $bytes_per_sector * $sectors_per_cluster;
60 378 ▶ $cluster_size <= 32*1024
Branch: 1 branch
BranchTrueFalseTotal
$cluster_size <= 32768 or carp("Warning: bytes_per_sector * sectors_per_cluster > 32KiB which is not valid for some drivers") 378 0 378
61 or carp "Warning: bytes_per_sector * sectors_per_cluster > 32KiB which is not valid for some drivers";
62  
63 # Default fat_count to 2 unless specified otherwise
64 378 ▶ $fat_count //= 2;
Condition100%2/2
$fat_count //= 2
l!l
121257
MC/DC100%1/1
$fat_count //= 2
$fat_count
Truth table100%2/2
A
A= $fat_count
Aresult
11
00
65 378 ▶ isa_int $fat_count && 0 < $fat_count && $fat_count <= 255
Branch: 1 branch
BranchTrueFalseTotal
&isa_int($fat_count) and 0 < $fat_count and $fat_count <= 255 or croak("Invalid fat_count $fat_count") 378 0 378
Condition33%2/6
&isa_int($fat_count) and 0 < $fat_count and $fat_count <= 255
!ll&&!rl&&r
00378
&isa_int($fat_count) and 0 < $fat_count
!ll&&!rl&&r
00378
MC/DC0%0/3
&isa_int($fat_count) and 0 < $fat_count and $fat_count <= 255
&isa_int($fat_count) 0 < $fat_count $fat_count <= 255
Truth table25%1/4
A and B and C
A= &isa_int($fat_count)B= 0 < $fat_countC= $fat_count <= 255
ABCresult
0XX0
10X0
1100
1111
66 or croak "Invalid fat_count $fat_count";
67  
68 378 my $self= bless {
69 bytes_per_sector => $bytes_per_sector,
70 sectors_per_cluster => $sectors_per_cluster,
71 fat_count => $fat_count,
72 volume_offset => $volume_offset,
73 };
74
75 # From here down, we are either determining cluster_count from other properties,
76 # or deriving other properties from cluster_count.
77 378 my $bits;
78 378 ▶ if (defined $reserved_sector_count && defined $fat_sector_count
Branch: 2 branches
BranchTrueFalseTotal
if (defined $reserved_sector_count and defined $fat_sector_count and defined $root_dirent_count and defined $total_sector_count) { } 0 378 378
elsif (defined $cluster_count) { } 378 0 378
Condition33%3/9
defined $reserved_sector_count and defined $fat_sector_count and defined $root_dirent_count and defined $total_sector_count
!ll&&!rl&&r
37800
defined $reserved_sector_count and defined $fat_sector_count and defined $root_dirent_count
!ll&&!rl&&r
37800
defined $reserved_sector_count and defined $fat_sector_count
!ll&&!rl&&r
37800
MC/DC0%0/4
defined $reserved_sector_count and defined $fat_sector_count and defined $root_dirent_count and defined $total_sector_count
defined $reserved_sector_count defined $fat_sector_count defined $root_dirent_count defined $total_sector_count
Truth table20%1/5
A and B and C and D
A= defined $reserved_sector_countB= defined $fat_sector_countC= defined $root_dirent_countD= defined $total_sector_count
ABCDresult
0XXX0
10XX0
110X0
11100
11111
79 && defined $root_dirent_count && defined $total_sector_count
80 ) {
81 # All main properties of the geometry are defined.
82 0 my $root_sector_count= int(($root_dirent_count + ($self->dirent_per_sector-1)) / $self->dirent_per_sector);
83 0 my $data_sectors= $total_sector_count - $reserved_sector_count - $fat_count * $fat_sector_count - $root_sector_count;
84 0 my $calc_cluster_count= int($data_sectors / $sectors_per_cluster);
85 0 ▶ croak "Supplied cluster_count disagrees with computed value"
Branch: 1 branch
BranchTrueFalseTotal
if defined $cluster_count and $cluster_count != $calc_cluster_count 0 0 0
Condition0%0/3
defined $cluster_count and $cluster_count != $calc_cluster_count
!ll&&!rl&&r
000
MC/DC0%0/2
defined $cluster_count and $cluster_count != $calc_cluster_count
defined $cluster_count $cluster_count != $calc_cluster_count
Truth table0%0/3
A and B
A= defined $cluster_countB= $cluster_count != $calc_cluster_count
ABresult
0X0
100
111
86 if defined $cluster_count && $cluster_count != $calc_cluster_count;
87 0 ▶ $cluster_count //= $calc_cluster_count;
Condition0%0/3
$cluster_count //= $calc_cluster_count
l!l&&r!l&&!r
000
MC/DC0%0/2
$cluster_count //= $calc_cluster_count
$cluster_count $calc_cluster_count
Truth table0%0/3
A //= B
A= $cluster_countB= $calc_cluster_count
ABresult
1X1
011
000
88 0 ▶ $bits= $cluster_count < FAT16_MIN_CLUSTERS? FAT12
Branch: 2 branches
BranchTrueFalseTotal
$cluster_count < 4085 ? : 0 0 0
$cluster_count < 65525 ? : 0 0 0
89 : $cluster_count < FAT32_MIN_CLUSTERS? FAT16
90 : FAT32;
91 }
92 elsif (defined $cluster_count) {
93 378 ▶ isa_int $cluster_count && $cluster_count > 0
Branch: 1 branch
BranchTrueFalseTotal
&isa_int($cluster_count) and $cluster_count > 0 or croak("Invalid cluster_count '${cluster_count}'") 378 0 378
Condition33%1/3
&isa_int($cluster_count) and $cluster_count > 0
!ll&&!rl&&r
00378
MC/DC0%0/2
&isa_int($cluster_count) and $cluster_count > 0
&isa_int($cluster_count) $cluster_count > 0
Truth table33%1/3
A and B
A= &isa_int($cluster_count)B= $cluster_count > 0
ABresult
0X0
100
111
94 or croak "Invalid cluster_count '$cluster_count'";
95 # FAT docs recommend avoiding numbers near the boundary of FAT12/FAT16/FAT32 to avoid
96 # other people's math errors. But, allow the caller to disable this adjustment.
97 378 ▶ unless (delete $attrs{exact_cluster_count}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (delete $attrs{"exact_cluster_count"}) 378 0 378
98 378 ▶ if ($cluster_count >= FAT12_IDEAL_MAX_CLUSTERS && $cluster_count < FAT16_IDEAL_MIN_CLUSTERS) {
Branch: 2 branches
BranchTrueFalseTotal
if ($cluster_count >= 4069 and $cluster_count < 4101) { } 0 378 378
elsif ($cluster_count >= 65509 and $cluster_count < 65541) { } 0 715 715
Condition66%2/3
$cluster_count >= 4069 and $cluster_count < 4101
!ll&&!rl&&r
296820
MC/DC0%0/2
$cluster_count >= 4069 and $cluster_count < 4101
$cluster_count >= 4069 $cluster_count < 4101
Truth table66%2/3
A and B
A= $cluster_count >= 4069B= $cluster_count < 4101
ABresult
0X0
100
111
99 0 $cluster_count= FAT16_IDEAL_MIN_CLUSTERS;
100 ▶ } elsif ($cluster_count >= FAT16_IDEAL_MAX_CLUSTERS && $cluster_count < FAT32_IDEAL_MIN_CLUSTERS) {
Condition66%2/3
$cluster_count >= 65509 and $cluster_count < 65541
!ll&&!rl&&r
337410
MC/DC0%0/2
$cluster_count >= 65509 and $cluster_count < 65541
$cluster_count >= 65509 $cluster_count < 65541
Truth table66%2/3
A and B
A= $cluster_count >= 65509B= $cluster_count < 65541
ABresult
0X0
100
111
101 0 $cluster_count= FAT32_IDEAL_MIN_CLUSTERS;
102 }
103 }
104 # These are the official boundary numbers that determine the filesystem type
105 378 ▶ $min_bits //= FAT12;
Condition50%1/2
$min_bits //= "FAT12"
l!l
0378
MC/DC0%0/1
$min_bits //= "FAT12"
$min_bits
Truth table50%1/2
A
A= $min_bits
Aresult
11
00
106 378 ▶ $bits= $cluster_count < FAT16_MIN_CLUSTERS? FAT12
Branch: 2 branches
BranchTrueFalseTotal
$cluster_count < 4085 ? : 296 82 378
$cluster_count < 65525 ? : 41 41 82
107 : $cluster_count < FAT32_MIN_CLUSTERS? FAT16
108 : FAT32;
109 378 ▶ if ($bits < $min_bits) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bits < $min_bits) 0 378 378
110 0 $bits= $min_bits;
111 # Increase to the minimum number of clusters if a specific number of bits
112 # was requested.
113 0 ▶ $cluster_count= ($bits == FAT16)? FAT16_IDEAL_MIN_CLUSTERS : FAT32_IDEAL_MIN_CLUSTERS;
Branch: 1 branch
BranchTrueFalseTotal
$bits == 16 ? : 0 0 0
114 }
115 }
116 else {
117 0 croak "Not enough attributes supplied to determine geometry";
118 }
119 378 $self->{cluster_count}= $cluster_count;
120 378 $self->{bits}= $bits;
121  
122 # Check how many sectors are occupied by each allocation table
123 378 my $fat_byte_count= ( ($cluster_count + 2) * $bits + 7 ) >> 3; # round up to bytes
124 378 ▶ if (defined $fat_sector_count) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $fat_sector_count) { } 0 378 378
125 0 ▶ $fat_sector_count * $bytes_per_sector >= $fat_byte_count
Branch: 1 branch
BranchTrueFalseTotal
$fat_sector_count * $bytes_per_sector >= $fat_byte_count or croak("Invalid fat_sector_count, smaller than $fat_byte_count bytes") 0 0 0
126 or croak "Invalid fat_sector_count, smaller than $fat_byte_count bytes";
127 } else {
128 378 $fat_sector_count= int(($fat_byte_count + ($bytes_per_sector - 1)) / $bytes_per_sector);
129 }
130 378 $self->{fat_sector_count}= $fat_sector_count;
131  
132 # Check how many sectors are occupied by root directory entries
133 # For fat12/16, The FAT spec document suggests 512 as a good default
134 # Allow the user to supply the actual number of root entries and then we round that.
135 378 my $used_root_dirent_count= delete $attrs{used_root_dirent_count};
136 378 ▶ if ($bits < FAT32) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bits < 32) { } 337 41 378
137 337 ▶ if (defined $root_dirent_count) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $root_dirent_count) { } 1 336 337
138 1 ▶ $root_dirent_count >= 1 && $root_dirent_count < 0xFFFF
Branch: 1 branch
BranchTrueFalseTotal
$root_dirent_count >= 1 and $root_dirent_count < 65535 or croak("Invalid root_dirent_count for FAT12/16") 1 0 1
Condition33%1/3
$root_dirent_count >= 1 and $root_dirent_count < 65535
!ll&&!rl&&r
001
MC/DC0%0/2
$root_dirent_count >= 1 and $root_dirent_count < 65535
$root_dirent_count >= 1 $root_dirent_count < 65535
Truth table33%1/3
A and B
A= $root_dirent_count >= 1B= $root_dirent_count < 65535
ABresult
0X0
100
111
139 or croak "Invalid root_dirent_count for FAT12/16";
140 } else {
141 336 ▶ $root_dirent_count= $used_root_dirent_count // 512;
Condition100%2/2
$used_root_dirent_count // 512
l!l
25581
MC/DC100%1/1
$used_root_dirent_count // 512
$used_root_dirent_count
Truth table100%2/2
A
A= $used_root_dirent_count
Aresult
11
00
142 # Round up to as many as fit in this number of sectors
143 336 my $remainder= ($root_dirent_count & ($self->dirent_per_sector - 1));
144 336 ▶ $root_dirent_count += ($self->dirent_per_sector - $remainder)
Branch: 1 branch
BranchTrueFalseTotal
if $remainder 248 88 336
145 if $remainder;
146 }
147
148 337 ▶ ($reserved_sector_count //= 1) == 1
Branch: 1 branch
BranchTrueFalseTotal
($reserved_sector_count //= 1) == 1 or croak("reserved_sector_count should be 1 for FAT12/16") 337 0 337
Condition50%1/2
$reserved_sector_count //= 1
l!l
0337
MC/DC0%0/1
$reserved_sector_count //= 1
$reserved_sector_count
Truth table50%1/2
A
A= $reserved_sector_count
Aresult
11
00
149 or croak "reserved_sector_count should be 1 for FAT12/16";
150 } else {
151 41 ▶ ($root_dirent_count //= 0) == 0
Branch: 1 branch
BranchTrueFalseTotal
($root_dirent_count //= 0) == 0 or croak("root_dirent_count must be zero for FAT32") 41 0 41
Condition50%1/2
$root_dirent_count //= 0
l!l
041
MC/DC0%0/1
$root_dirent_count //= 0
$root_dirent_count
Truth table50%1/2
A
A= $root_dirent_count
Aresult
11
00
152 or croak "root_dirent_count must be zero for FAT32";
153  
154 41 ▶ $reserved_sector_count //= 32;
Condition50%1/2
$reserved_sector_count //= 32
l!l
041
MC/DC0%0/1
$reserved_sector_count //= 32
$reserved_sector_count
Truth table50%1/2
A
A= $reserved_sector_count
Aresult
11
00
155 41 ▶ isa_int $reserved_sector_count && $reserved_sector_count >= 2
Branch: 1 branch
BranchTrueFalseTotal
&isa_int($reserved_sector_count) and $reserved_sector_count >= 2 or croak("reserved_sector_count must be greater than 2 for FAT32") 41 0 41
Condition33%1/3
&isa_int($reserved_sector_count) and $reserved_sector_count >= 2
!ll&&!rl&&r
0041
MC/DC0%0/2
&isa_int($reserved_sector_count) and $reserved_sector_count >= 2
&isa_int($reserved_sector_count) $reserved_sector_count >= 2
Truth table33%1/3
A and B
A= &isa_int($reserved_sector_count)B= $reserved_sector_count >= 2
ABresult
0X0
100
111
156 or croak "reserved_sector_count must be greater than 2 for FAT32";
157 }
158  
159 # If caller requested alignment of clusters, figure that out
160 378 ▶ if (defined $align_clusters && $align_clusters > $bytes_per_sector) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $align_clusters and $align_clusters > $bytes_per_sector) 220 158 378
Condition100%3/3
defined $align_clusters and $align_clusters > $bytes_per_sector
!ll&&!rl&&r
12137220
MC/DC100%2/2
defined $align_clusters and $align_clusters > $bytes_per_sector
defined $align_clusters $align_clusters > $bytes_per_sector
Truth table100%3/3
A and B
A= defined $align_clustersB= $align_clusters > $bytes_per_sector
ABresult
0X0
100
111
161 # there's a method for this, but avoid caching things yet
162 220 my $data_addr= $volume_offset + $bytes_per_sector * (
163 $reserved_sector_count
164 + ($fat_count*$fat_sector_count)
165 + ceil($root_dirent_count / $self->dirent_per_sector)
166 );
167 # If the cluster size is greater or equal to the requested alignment, ensure the
168 # data start falls on that boundary.
169 # If the cluster size is smaller than the requested alignment, ensure the data start
170 # falls on a cluster boundary so that some number of clusters will equal the alignment.
171 220 ▶ my $align= ($cluster_size >= $align_clusters)? $align_clusters : $cluster_size;
Branch: 1 branch
BranchTrueFalseTotal
$cluster_size >= $align_clusters ? : 184 36 220
172 220 ▶ if (my $ofs= $data_addr & ($align-1)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $ofs = $data_addr & $align - 1) 162 58 220
173 162 my $shift_n_sectors= ($align - $ofs) / $bytes_per_sector;
174 #say sprintf "# remainder=0x%X, cluster_size=$cluster_size align_clusters=$align_clusters $align-$ofs=".($align-$ofs)." shift %d sectors", $ofs, $shift_n_sectors;
175 162 ▶ if ($bits < FAT32) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bits < 32) { } 149 13 162
176 # Reserved sectors should be 1, so expand number of root entries
177 149 $root_dirent_count += $shift_n_sectors * $self->dirent_per_sector;
178 } else {
179 # Add however many reserved sectors we need
180 13 $reserved_sector_count += $shift_n_sectors;
181 }
182 }
183 }
184 378 $self->{root_dirent_count}= $root_dirent_count;
185 378 $self->{reserved_sector_count}= $reserved_sector_count;
186
187 378 ▶ carp "Unused constructor parameters: ".join(' ', keys %attrs)
Branch: 1 branch
BranchTrueFalseTotal
if keys %attrs 0 378 378
188 if keys %attrs;
189 378 $self;
190 }
191  
192  
193 23345 ▶sub volume_offset($self) { $self->{volume_offset} }
Subroutine volume_offset: called CC=1 SCAR=0.0
Pod documented
194  
195 23587 ▶sub bytes_per_sector($self) { $self->{bytes_per_sector} }
Subroutine bytes_per_sector: called CC=1 SCAR=0.0
Pod documented
196 22073 ▶sub sectors_per_cluster($self) { $self->{sectors_per_cluster} }
Subroutine sectors_per_cluster: called CC=1 SCAR=0.0
Pod documented
197 31338 ▶sub bytes_per_cluster($self) { $self->{bytes_per_sector} * $self->{sectors_per_cluster} }
Subroutine bytes_per_cluster: called CC=1 SCAR=0.0
Pod documented
198 1513 ▶sub dirent_per_sector($self) { $self->{bytes_per_sector} / 32 }
Subroutine dirent_per_sector: called CC=1 SCAR=0.0
Pod documented
199 1 ▶sub dirent_per_cluster($self) { $self->{bytes_per_sector} * $self->{sectors_per_cluster} / 32 }
Subroutine dirent_per_cluster: called CC=1 SCAR=0.0
Pod documented
200  
201  
202 406 ▶sub bits($self) { $self->{bits} }
Subroutine bits: called CC=1 SCAR=0.0
Pod documented
203 530 ▶sub reserved_sector_count($self) { $self->{reserved_sector_count} }
Subroutine reserved_sector_count: called CC=1 SCAR=0.0
Pod documented
204 62 ▶sub reserved_size($self) { $self->{reserved_sector_count} * $self->bytes_per_sector }
Subroutine reserved_size: called CC=1 SCAR=0.0
Pod documented
205 503 ▶sub fat_count($self) { $self->{fat_count} }
Subroutine fat_count: called CC=1 SCAR=0.0
Pod documented
206 590 ▶sub fat_sector_count($self) { $self->{fat_sector_count} }
Subroutine fat_sector_count: called CC=1 SCAR=0.0
Pod documented
207 124 ▶sub fat_size($self) { $self->{fat_sector_count} * $self->bytes_per_sector }
Subroutine fat_size: called CC=1 SCAR=0.0
Pod documented
208 22592 ▶sub cluster_count($self) { $self->{cluster_count} }
Subroutine cluster_count: called CC=1 SCAR=0.0
Pod documented
209 0 ▶sub min_cluster_id($self) { 2 }
Subroutine min_cluster_id: not called CC=1 SCAR=6.9
Pod documented
210 22112 ▶sub max_cluster_id($self) { $self->cluster_count + 1 }
Subroutine max_cluster_id: called CC=1 SCAR=0.0
Pod documented
211 591 ▶sub root_dirent_count($self) { $self->{root_dirent_count} }
Subroutine root_dirent_count: called CC=1 SCAR=0.0
Pod documented
212 559 ▶sub root_dir_sector_count($self) { ceil($self->root_dirent_count / $self->dirent_per_sector) }
Subroutine root_dir_sector_count: called CC=1 SCAR=0.0
Pod documented
213 60 ▶sub root_dir_size($self) { $self->root_dir_sector_count * $self->bytes_per_sector }
Subroutine root_dir_size: called CC=1 SCAR=0.0
Pod documented
214  
215 378 ▶sub root_dir_start_sector($self) {
Subroutine root_dir_start_sector: called CC=1 SCAR=0.0
Pod documented
216 378 $self->reserved_sector_count + $self->fat_count * $self->fat_sector_count
217 }
218 0 ▶sub root_dir_offset($self) { $self->root_dir_start_sector * $self->bytes_per_sector }
Subroutine root_dir_offset: not called CC=1 SCAR=6.9
Pod documented
219 23539 ▶sub data_start_sector($self) {
Subroutine data_start_sector: called CC=2 SCAR=6.9
Pod documented
220 23539 ▶ $self->{data_start_sector} //= $self->root_dir_start_sector + $self->root_dir_sector_count;
Condition100%2/2
$self->{"data_start_sector"} //= $self->root_dir_start_sector + $self->root_dir_sector_count
l!l
23161378
MC/DC100%1/1
$self->{"data_start_sector"} //= $self->root_dir_start_sector + $self->root_dir_sector_count
$self->{"data_start_sector"}
Truth table100%2/2
A
A= $self->{"data_start_sector"}
Aresult
11
00
221 }
222 270 ▶sub data_limit_sector($self) {
Subroutine data_limit_sector: called CC=1 SCAR=0.0
Pod documented
223 270 $self->data_start_sector + $self->cluster_count * $self->sectors_per_cluster
224 }
225 1370 ▶sub data_start_offset($self) { $self->data_start_sector * $self->bytes_per_sector }
Subroutine data_start_offset: called CC=1 SCAR=0.0
Pod documented
226 270 ▶sub data_limit_offset($self) { $self->data_limit_sector * $self->bytes_per_sector }
Subroutine data_limit_offset: called CC=1 SCAR=0.0
Pod documented
227 1369 ▶sub data_start_device_offset($self) { $self->volume_offset + $self->data_start_offset }
Subroutine data_start_device_offset: called CC=1 SCAR=0.0
Pod documented
228 270 ▶sub data_limit_device_offset($self) { $self->volume_offset + $self->data_limit_offset }
Subroutine data_limit_device_offset: called CC=1 SCAR=0.0
Pod documented
229  
230 0 ▶sub data_sector_count($self) {
Subroutine data_sector_count: not called CC=1 SCAR=6.9
Pod documented
231 0 $self->total_sector_count - $self->data_start_sector;
232 }
233  
234 526 ▶sub total_sector_count($self) {
Subroutine total_sector_count: called CC=2 SCAR=6.9
Pod documented
235 526 ▶ $self->{total_sector_count} //= $self->data_start_sector
Condition100%2/2
$self->{"total_sector_count"} //= $self->data_start_sector + $self->cluster_count * $self->sectors_per_cluster
l!l
326200
MC/DC100%1/1
$self->{"total_sector_count"} //= $self->data_start_sector + $self->cluster_count * $self->sectors_per_cluster
$self->{"total_sector_count"}
Truth table100%2/2
A
A= $self->{"total_sector_count"}
Aresult
11
00
236 + $self->cluster_count * $self->sectors_per_cluster;
237 }
238 98 ▶sub total_size($self) { $self->total_sector_count * $self->bytes_per_sector }
Subroutine total_size: called CC=1 SCAR=0.0
Pod documented
239  
240  
241 21564 ▶sub get_cluster_start_sector($self, $cluster_id) {
Subroutine get_cluster_start_sector: called CC=3 SCAR=11.7
Pod documented
242 21564 ▶ croak "Cluster 0 and 1 are reserved" if $cluster_id < 2;
Branch: 1 branch
BranchTrueFalseTotal
if $cluster_id < 2 0 21564 21564
243 21564 ▶ croak "Cluster $cluster_id beyond end of volume" if $cluster_id > $self->max_cluster_id;
Branch: 1 branch
BranchTrueFalseTotal
if $cluster_id > $self->max_cluster_id 0 21564 21564
244 21564 return $self->data_start_sector + ($cluster_id-2) * $self->sectors_per_cluster;
245 }
246 21564 ▶sub get_cluster_offset($self, $cluster_id) {
Subroutine get_cluster_offset: called CC=1 SCAR=0.0
Pod documented
247 21564 $self->get_cluster_start_sector($cluster_id) * $self->bytes_per_sector;
248 }
249 21557 ▶sub get_cluster_device_offset($self, $cluster_id) {
Subroutine get_cluster_device_offset: called CC=1 SCAR=0.0
Pod documented
250 21557 $self->volume_offset + $self->get_cluster_offset($cluster_id);
251 }
252  
253 7 ▶sub get_cluster_of_sector($self, $sector_idx) {
Subroutine get_cluster_of_sector: called CC=3 SCAR=11.4
Pod documented
254 7 ▶ return undef if $sector_idx < $self->data_start_sector;
Branch: 1 branch
BranchTrueFalseTotal
if $sector_idx < $self->data_start_sector 0 7 7
255 7 my $cluster= int(($sector_idx - $self->data_start_sector) / $self->sectors_per_cluster);
256 7 ▶ return undef if $cluster >= $self->cluster_count;
Branch: 1 branch
BranchTrueFalseTotal
if $cluster >= $self->cluster_count 0 7 7
257 7 return $cluster + 2;
258 }
259 7 ▶sub get_cluster_of_offset($self, $offset) {
Subroutine get_cluster_of_offset: called CC=1 SCAR=0.0
Pod documented
260 7 $self->get_cluster_of_sector(int($offset / $self->bytes_per_sector));
261 }
262 0 ▶sub get_cluster_of_device_offset($self, $addr) {
Subroutine get_cluster_of_device_offset: not called CC=1 SCAR=6.9
Pod documented
263 0 $self->get_cluster_of_offset($addr - $self->volume_offset);
264 }
265  
266  
267 7 ▶sub get_cluster_extent_of_volume_extent($self, $offset, $size) {
Subroutine get_cluster_extent_of_volume_extent: called CC=4 SCAR=14.5
Pod documented
268 7 my $cl_start= $self->get_cluster_of_offset($offset);
269 7 ▶ $cl_start
Branch: 1 branch
BranchTrueFalseTotal
$cl_start or croak("Offset $offset falls outside of cluster data region") 7 0 7
270 or croak "Offset $offset falls outside of cluster data region";
271 7 ▶ $self->get_cluster_offset($cl_start) == $offset
Branch: 1 branch
BranchTrueFalseTotal
$self->get_cluster_offset($cl_start) == $offset or croak("FAT_offset not aligned to a cluster boundary") 7 0 7
272 or croak "FAT_offset not aligned to a cluster boundary";
273 7 my $cl_cnt= ceil($size / $self->bytes_per_cluster);
274 7 ▶ $cl_start + $cl_cnt <= $self->max_cluster_id+1
Branch: 1 branch
BranchTrueFalseTotal
$cl_start + $cl_cnt <= $self->max_cluster_id + 1 or croak("byte range ($offset, $size) exceeds final cluster of volume") 7 0 7
275 or croak "byte range ($offset, $size) exceeds final cluster of volume";
276 7 return ($cl_start, $cl_cnt);
277 }
278  
279  
280 7 ▶sub get_cluster_extent_of_device_extent($self, $addr, $size) {
Subroutine get_cluster_extent_of_device_extent: called CC=1 SCAR=0.0
Pod documented
281 7 $self->get_cluster_extent_of_volume_extent($addr - $self->volume_offset, $size);
282 }
283  
284  
285 587 ▶sub get_cluster_alignment_of_device_alignment($self, $align) {
Subroutine get_cluster_alignment_of_device_alignment: called CC=5 SCAR=16.2
Pod documented
286 587 my $cluster_size= $self->bytes_per_cluster;
287 # If the cluster size is greater or equal to the requested alignment, verify that the
288 # data start (plus the volume offset that was implicitly added to every volume address)
289 # meets that alignment. If so, every cluster is aligned and the return value is (1,0)
290 587 ▶ if ($cluster_size >= $align) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cluster_size >= $align) 262 325 587
291 262 ▶ croak "Clusters are not aligned to $align"
Branch: 1 branch
BranchTrueFalseTotal
if $self->data_start_device_offset & $align - 1 0 262 262
292 if $self->data_start_device_offset & ($align-1);
293 262 return (1,0);
294 }
295 # Otherwise, make sure the data_start (plus implied volume offset) is aligned to
296 # cluster_size, so then some multiple of clusters will reach the requested alignment.
297 325 ▶ croak "Clusters are not aligned to $cluster_size"
Branch: 1 branch
BranchTrueFalseTotal
if $self->data_start_device_offset & $cluster_size - 1 0 325 325
298 if $self->data_start_device_offset & ($cluster_size-1);
299 # The cluster alignment will be whatever multiple of clusters equals the byte alignment.
300 # This will be at least 2.
301 325 my $cl_align= $align / $cluster_size;
302 # How many bytes away from alignment is the beginning of ficticious cluster 0?
303 325 my $ofs_of_cl0= ($self->data_start_device_offset - ($cluster_size*2)) & ($align-1);
304 # If not aligned, add the rest of the distance to the next alignment.
305 325 ▶ my $cl_ofs= !$ofs_of_cl0? 0 : ($align - $ofs_of_cl0) / $cluster_size;
Branch: 1 branch
BranchTrueFalseTotal
!$ofs_of_cl0 ? : 94 231 325
306 325 return ($cl_align, $cl_ofs);
307 }
308  
309  
310 0 ▶sub unpack {
Subroutine unpack: not called CC=6 SCAR=37.4
Pod documented
311 0 my $class= shift;
312 0 ▶ my $buf_ref= ref $_[0] eq 'SCALAR'? $_[0] : \$_[0];
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq 'SCALAR' ? : 0 0 0
313 0 ▶ my %attrs= ref $_[1] eq 'HASH'? %{$_[1]} : @_[1..$#_];
Branch: 1 branch
BranchTrueFalseTotal
ref $_[1] eq 'HASH' ? : 0 0 0
314 0 ▶ length($$buf_ref) >= 512 or croak "Pass at least the entire first sector to 'unpack'";
Branch: 1 branch
BranchTrueFalseTotal
length $$buf_ref >= 512 or croak("Pass at least the entire first sector to 'unpack'") 0 0 0
315 # According to the official spec, the only way to know whether you have FAT32 or FAT16
316 # is to calculate the count of clusters available in the data region, which this module
317 # implements in the constructor. However, in order to unpack all of the fields of
318 # sector0, you have to know whether it is FAT16 or FAT32 because FAT32 moves some of the
319 # fields. But you need those extended fields to calculate whether it is FAT32 or not...
320 # It's sort of a bullshit circular dependency when clearly you could use the
321 # BPB_RootEntCnt to know whether it was FAT32 or not, since FAT32 will *always* be 0 and
322 # the previous generations *can't* be 0.
323 # Anyway, instead of uniform single-pass field unpacking, we get this:
324 0 state %fields= qw(
325 bytes_per_sector @11v
326 sectors_per_cluster @13C
327 reserved_sector_count @14v
328 fat_count @16C
329 root_dirent_count @17v
330 fat_sector_count @22v
331 fat_sector_count32 @36V
332 total_sector_count @19v
333 total_sector_count32 @32V
334 );
335 0 state @fields= keys %fields;
336 0 state $packstr= join ' ', values %fields;
337  
338 0 @attrs{@fields}= unpack $packstr, $$buf_ref;
339 0 for (qw( fat_sector_count total_sector_count )) {
340 0 my $_32= delete $attrs{$_.'32'};
341 0 ▶ $attrs{$_} ||= $_32;
Condition0%0/3
$attrs{$_} ||= $_32
l!l&&r!l&&!r
000
MC/DC0%0/2
$attrs{$_} ||= $_32
$attrs{$_} $_32
Truth table0%0/3
A ||= B
A= $attrs{$_}B= $_32
ABresult
1X1
011
000
342 }
343 0 return $class->new(%attrs);
344 }
345  
346 # Avoiding dependency on namespace::clean
347 5 delete @{Sys::Export::VFAT::Geometry::}{qw(
348 carp confess croak ceil dualvar
349 isa_hash isa_int isa_pow2 round_up_to_multiple round_up_to_pow2
350 )};
351 5 1;
352  
353 __END__
« Export/ELF.pm ↑ Summary Export/Linux.pm »