Export/VFAT.pm

filter: Statementstmt 88.6% 383 / 432 Branchbran 59.0% 144 / 244 Conditioncond 54.6% 100 / 183 MC/DCmcdc 24.2% 25 / 103 Subroutinesub 89.3% 42 / 47 Podpod 100% 19 / 19 totaltotal 69.3% 713 / 1028 CC 213 SCAR 37.2
CC
213
Cov
73%
add
139.0
_optimize_geometry
60.7
_pack_directory
40.7
CRAP 41.4
« Export/Unix/WriteFS.pm ↑ Summary Export/ISO9660.pm »
1 package Sys::Export::VFAT;
2 # ABSTRACT: Write minimal FAT12/16/32 filesystems with control over stored file extents
3 3 our $VERSION = '0.006'; # VERSION
4  
5 3 ▶use v5.26;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use experimental qw( signatures );
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use Fcntl qw( S_IFDIR S_ISDIR S_ISREG );
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use Scalar::Util qw( blessed dualvar refaddr weaken );
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶use List::Util qw( min max );
Subroutine BEGIN: called CC=1 SCAR=0.0
11 3 ▶use POSIX 'ceil';
Subroutine BEGIN: called CC=1 SCAR=0.0
12 3 ▶use Sys::Export::LogAny '$log';
Subroutine BEGIN: called CC=1 SCAR=0.0
13 3 ▶use Encode qw( encode decode );
Subroutine BEGIN: called CC=1 SCAR=0.0
14 3 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 3 our @CARP_NOT= qw( Sys::Export Sys::Export::Unix );
16 3 ▶use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
17 3 ATTR_READONLY => dualvar(0x01, 'ATTR_READONLY'),
18 ATTR_HIDDEN => dualvar(0x02, 'ATTR_HIDDEN'),
19 ATTR_SYSTEM => dualvar(0x04, 'ATTR_SYSTEM'),
20 ATTR_VOLUME_ID => dualvar(0x08, 'ATTR_VOLUME_ID'),
21 ATTR_DIRECTORY => dualvar(0x10, 'ATTR_DIRECTORY'),
22 ATTR_ARCHIVE => dualvar(0x20, 'ATTR_ARCHIVE'),
23 ATTR_LONG_NAME => dualvar(0x0F, 'ATTR_LONG_NAME'),
24 ATTR_LONG_NAME_MASK => 0x3F,
25 3 };
26 3 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
27 3 our @EXPORT_OK= qw( FAT12 FAT16 FAT32 ATTR_READONLY ATTR_HIDDEN ATTR_SYSTEM ATTR_ARCHIVE
28 ATTR_VOLUME_ID ATTR_DIRECTORY is_valid_longname is_valid_shortname is_valid_volume_label
29 build_shortname remove_invalid_shortname_chars );
30 3 ▶use Sys::Export qw( isa_hash isa_array isa_handle isa_int isa_pow2 expand_stat_shorthand write_file_extent );
Subroutine BEGIN: called CC=1 SCAR=0.0
31 3 ▶use Sys::Export::VFAT::Geometry qw( FAT12 FAT16 FAT32 );
Subroutine BEGIN: called CC=1 SCAR=0.0
32 3 require Sys::Export::VFAT::AllocationTable;
33 3 require Sys::Export::VFAT::File;
34 3 require Sys::Export::VFAT::Directory;
35  
36  
37 31 ▶sub new($class, @attrs) {
Subroutine new: called CC=5 SCAR=16.7
Pod documented
38 my %attrs= @attrs != 1? @attrs
39 0 ▶ : isa_hash $attrs[0]? %{$attrs[0]}
Branch: 3 branches
BranchTrueFalseTotal
@attrs != 1 ? : 5 26 31
&isa_hash($attrs[0]) ? : 0 26 26
&isa_handle($attrs[0]) ? : 26 0 26
40 : isa_handle $attrs[0]? ( filehandle => $attrs[0] )
41 : ( filename => $attrs[0] );
42 31 my $self= bless {}, $class;
43 31 $self->{root}= $self->_new_dir('/', undef, undef);
44 # keep root dir separate from subdirs
45 31 delete $self->{_subdirs}{refaddr $self->{root}};
46 # apply other attributes
47 31 $self->$_($attrs{$_}) for keys %attrs;
48 31 $self;
49 }
50  
51 # Create dir, and store a strong reference in ->{_subdirs}
52 65 ▶sub _new_dir($self, $name, $parent, $file) {
Subroutine _new_dir: called CC=1 SCAR=0.0
53 65 my $dir= Sys::Export::VFAT::Directory->new(name => $name, parent => $parent, file => $file);
54 65 $self->{_subdirs}{refaddr $dir}= $dir;
55 65 $dir;
56 }
57  
58  
59 0 ▶sub filename { @_ > 1? ($_[0]{filename}= $_[1]) : $_[0]{filename} }
Subroutine filename: not called CC=2 SCAR=17.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
@_ > 1 ? : 0 0 0
60 93 ▶sub filehandle { @_ > 1? ($_[0]{filehandle}= $_[1]) : $_[0]{filehandle} }
Subroutine filehandle: called CC=2 SCAR=6.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
@_ > 1 ? : 31 62 93
61  
62  
63 21262 ▶sub root($self) { $self->{root} }
Subroutine root: called CC=1 SCAR=0.0
Pod documented
64 212 ▶sub geometry($self) { $self->{geometry} }
Subroutine geometry: called CC=1 SCAR=0.0
Pod documented
65 33 ▶sub allocation_table($self) { $self->{allocation_table} }
Subroutine allocation_table: called CC=1 SCAR=0.0
Pod documented
66  
67 310 ▶sub volume_offset($self, @val) {
Subroutine volume_offset: called CC=6 SCAR=18.0
Pod documented
68 310 ▶ if ($self->{geometry}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"geometry"}) 31 279 310
69 31 ▶ croak "Geometry already decided" if @val;
Branch: 1 branch
BranchTrueFalseTotal
if @val 0 31 31
70 31 return $self->{geometry}->volume_offset;
71 }
72 279 ▶ if (@val) {
Branch: 1 branch
BranchTrueFalseTotal
if (@val) 20 259 279
73 20 ▶ croak "volume_offset must be a multiple of 512" if $val[0] & 511;
Branch: 1 branch
BranchTrueFalseTotal
if $val[0] & 511 0 20 20
74 20 return $self->{volume_offset}= $val[0];
75 }
76 259 ▶ $self->{volume_offset} // 0
Condition100%2/2
$self->{"volume_offset"} // 0
l!l
19069
MC/DC100%1/1
$self->{"volume_offset"} // 0
$self->{"volume_offset"}
Truth table100%2/2
A
A= $self->{"volume_offset"}
Aresult
11
00
77 }
78  
79  
80 256 ▶sub min_bits($self, @val) {
Subroutine min_bits: called CC=3 SCAR=16.5
Pod documented
81 256 ▶ if (@val) {
Branch: 1 branch
BranchTrueFalseTotal
if (@val) 0 256 256
82 0 ▶ croak "Geometry already decided" if $self->{geometry};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"geometry"} 0 0 0
83 0 $self->{min_bits}= $val[0];
84 }
85 $self->{min_bits}
86 256 }
87  
88 34 ▶sub bytes_per_sector($self, @val) {
Subroutine bytes_per_sector: called CC=6 SCAR=20.3
Pod documented
89 34 ▶ if ($self->{geometry}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"geometry"}) 0 34 34
90 0 ▶ croak "Geometry already decided" if @val;
Branch: 1 branch
BranchTrueFalseTotal
if @val 0 0 0
91 0 return $self->{geometry}->bytes_per_sector;
92 }
93 34 ▶ if (@val) {
Branch: 1 branch
BranchTrueFalseTotal
if (@val) 3 31 34
94 3 ▶ croak "bytes_per_sector must be a power of 2" unless isa_pow2 $val[0];
Branch: 1 branch
BranchTrueFalseTotal
unless &isa_pow2($val[0]) 0 3 3
95 3 return $self->{bytes_per_sector}= $val[0];
96 }
97 31 ▶ $self->{bytes_per_sector} // 512
Condition100%2/2
$self->{"bytes_per_sector"} // 512
l!l
328
MC/DC100%1/1
$self->{"bytes_per_sector"} // 512
$self->{"bytes_per_sector"}
Truth table100%2/2
A
A= $self->{"bytes_per_sector"}
Aresult
11
00
98 }
99  
100 37 ▶sub sectors_per_cluster($self, @val) {
Subroutine sectors_per_cluster: called CC=6 SCAR=22.1
Pod documented
101 37 ▶ if ($self->{geometry}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"geometry"}) 0 37 37
102 0 ▶ croak "Geometry already decided" if @val;
Branch: 1 branch
BranchTrueFalseTotal
if @val 0 0 0
103 0 return $self->{geometry}->sectors_per_cluster;
104 }
105 37 ▶ if (@val) {
Branch: 1 branch
BranchTrueFalseTotal
if (@val) 3 34 37
106 3 ▶ croak "sectors_per_cluster must be a power of 2, and 128 or less" unless isa_pow2 $val[0] && $val[0] <= 128;
Branch: 1 branch
BranchTrueFalseTotal
unless &isa_pow2($val[0]) and $val[0] <= 128 0 3 3
Condition33%1/3
&isa_pow2($val[0]) and $val[0] <= 128
!ll&&!rl&&r
003
MC/DC0%0/2
&isa_pow2($val[0]) and $val[0] <= 128
&isa_pow2($val[0]) $val[0] <= 128
Truth table33%1/3
A and B
A= &isa_pow2($val[0])B= $val[0] <= 128
ABresult
0X0
100
111
107 3 return $self->{sectors_per_cluster}= $val[0];
108 }
109 $self->{sectors_per_cluster}
110 34 }
111  
112 256 ▶sub fat_count($self, @val) {
Subroutine fat_count: called CC=6 SCAR=29.7
Pod documented
113 256 ▶ if ($self->{geometry}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"geometry"}) 0 256 256
114 0 ▶ croak "Geometry already decided" if @val;
Branch: 1 branch
BranchTrueFalseTotal
if @val 0 0 0
115 0 return $self->{geometry}->fat_count;
116 }
117 256 ▶ if (@val) {
Branch: 1 branch
BranchTrueFalseTotal
if (@val) 0 256 256
118 0 ▶ croak "fat_count must be positive" unless isa_int $val[0] && $val[0] > 0;
Branch: 1 branch
BranchTrueFalseTotal
unless &isa_int($val[0]) and $val[0] > 0 0 0 0
Condition0%0/3
&isa_int($val[0]) and $val[0] > 0
!ll&&!rl&&r
000
MC/DC0%0/2
&isa_int($val[0]) and $val[0] > 0
&isa_int($val[0]) $val[0] > 0
Truth table0%0/3
A and B
A= &isa_int($val[0])B= $val[0] > 0
ABresult
0X0
100
111
119 0 return $self->{fat_count}= $val[0];
120 }
121 $self->{fat_count}
122 256 }
123  
124 0 ▶sub free_space($self, @val) {
Subroutine free_space: not called CC=4 SCAR=30.0
Pod documented
125 0 ▶ if (@val) {
Branch: 1 branch
BranchTrueFalseTotal
if (@val) 0 0 0
126 0 ▶ croak "Geometry already decided" if $self->{geometry};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"geometry"} 0 0 0
127 0 return $self->{free_space}= $val[0];
128 }
129 0 ▶ $self->{free_space} // 0
Condition0%0/2
$self->{"free_space"} // 0
l!l
00
MC/DC0%0/1
$self->{"free_space"} // 0
$self->{"free_space"}
Truth table0%0/2
A
A= $self->{"free_space"}
Aresult
11
00
130 }
131  
132 62 ▶sub volume_label($self, @val) {
Subroutine volume_label: called CC=3 SCAR=16.5
Pod documented
133 62 ▶ if (@val) {
Branch: 1 branch
BranchTrueFalseTotal
if (@val) 0 62 62
134 0 ▶ croak "Invalid volume label '$val[0]'" unless is_valid_volume_label($val[0]);
Branch: 1 branch
BranchTrueFalseTotal
unless is_valid_volume_label($val[0]) 0 0 0
135 0 return $self->{volume_label}= $val[0];
136 }
137 $self->{volume_label}
138 62 }
139  
140 # The smallest conceivable address where the data region could start
141 1 ▶sub _minimum_offset_to_data {
Subroutine _minimum_offset_to_data: called CC=1 SCAR=0.0
142 1 state $minimum_offset_to_data= Sys::Export::VFAT::Geometry->new(
143 bytes_per_sector => 512,
144 sectors_per_cluster => 1,
145 fat_count => 1,
146 root_dirent_count => 1,
147 cluster_count => 1
148 )->data_start_offset;
149 }
150  
151  
152 10517 ▶sub add($self, $spec) {
Subroutine add: called CC=40 SCAR=49.3
Pod documented
153 10517 ▶ $spec= { expand_stat_shorthand($spec) }
Branch: 1 branch
BranchTrueFalseTotal
if &isa_array($spec) 10516 1 10517
154 if isa_array $spec;
155  
156 defined $spec->{uname} or defined $spec->{name}
157 10517 ▶ or croak "Require 'uname' or 'name'";
Branch: 1 branch
BranchTrueFalseTotal
defined $spec->{"uname"} or defined $spec->{"name"} or croak("Require 'uname' or 'name'") 10517 0 10517
Condition33%1/3
defined $spec->{"uname"} or defined $spec->{"name"}
l!l&&r!l&&!r
0105170
MC/DC0%0/2
defined $spec->{"uname"} or defined $spec->{"name"}
defined $spec->{"uname"} defined $spec->{"name"}
Truth table33%1/3
A or B
A= defined $spec->{"uname"}B= defined $spec->{"name"}
ABresult
1X1
011
000
158 10517 ▶ defined $spec->{mode} or croak "Require 'mode'";
Branch: 1 branch
BranchTrueFalseTotal
defined $spec->{"mode"} or croak("Require 'mode'") 10517 0 10517
159  
160 # If user supplied uname, use that as a more official source of Unicode
161 10517 ▶ my $path= $spec->{uname} // decode('UTF-8', $spec->{name}, Encode::FB_CROAK | Encode::LEAVE_SRC);
Condition33%1/3
$spec->{"uname"} // &decode("UTF-8", $spec->{"name"}, 9)
l!l&&r!l&&!r
0105170
MC/DC0%0/2
$spec->{"uname"} // &decode("UTF-8", $spec->{"name"}, 9)
$spec->{"uname"} &decode("UTF-8", $spec->{"name"}, 9)
Truth table33%1/3
A // B
A= $spec->{"uname"}B= &decode("UTF-8", $spec->{"name"}, 9)
ABresult
1X1
011
000
162 10517 $path =~ s,^/,,; # remove leading slash, if any
163  
164 10517 my @path= grep length, split '/', $path;
165 10517 my $leaf= pop @path;
166  
167 # Walk through the tree based on the case-folded path
168 10517 my $parent= $self->root;
169 10517 for (@path) {
170 98362 my $ent= $parent->entry($_);
171 98362 ▶ if ($ent) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ent) { } 98329 33 98362
172 croak $ent->name." is not a directory, while attempting to add '$path'"
173 98329 ▶ unless $ent->{dir};
Branch: 1 branch
BranchTrueFalseTotal
unless $ent->{"dir"} 0 98329 98329
174 } else { # Auto-create directory. Autovivication is indicated by ->{file} = undef
175 33 $ent= $parent->add($_, undef);
176 33 ▶ my $name= ($parent == $self->root? '' : $parent->name)."/$_";
Branch: 1 branch
BranchTrueFalseTotal
$parent == $self->root ? : 3 30 33
177 33 weaken($ent->{dir}= $self->_new_dir($name, $parent, undef));
178 }
179 98362 $parent= $ent->{dir};
180 }
181  
182 # did user supply FAT attribute bitmask?
183 10517 ▶ my $flags= $spec->{FAT_flags} // do {
Condition33%1/3
$spec->{"FAT_flags"} // do { (!($spec->{"mode"} & 256) ? "ATTR_READONLY" : 0) | (defined $leaf && $leaf =~ /^\./u ? "ATTR_HIDDEN" : 0) }
l!l&&r!l&&!r
0105170
MC/DC0%0/2
$spec->{"FAT_flags"} // do { (!($spec->{"mode"} & 256) ? "ATTR_READONLY" : 0) | (defined $leaf && $leaf =~ /^\./u ? "ATTR_HIDDEN" : 0) }
$spec->{"FAT_flags"} do { (!($spec->{"mode"} & 256) ? "ATTR_READONLY" : 0) | (defined $leaf && $leaf =~ /^\./u ? "ATTR_HIDDEN" : 0) }
Truth table33%1/3
A // B
A= $spec->{"FAT_flags"}B= do { (!($spec->{"mode"} & 256) ? "ATTR_READONLY" : 0) | (defined $leaf && $leaf =~ /^\./u ? "ATTR_HIDDEN" : 0) }
ABresult
1X1
011
000
184 # readonly determined by user -write bit of 'mode'
185 10517 ▶ (!($spec->{mode} & 0400)? ATTR_READONLY : 0)
Branch: 2 branches
BranchTrueFalseTotal
!($spec->{'mode'} & 256) ? : 0 10517 10517
defined $leaf && $leaf =~ /^\./u ? : 0 10517 10517
Condition33%1/3
defined $leaf and $leaf =~ /^\./u
!ll&&!rl&&r
0105170
MC/DC0%0/2
defined $leaf and $leaf =~ /^\./u
defined $leaf $leaf =~ /^\./u
Truth table33%1/3
A and B
A= defined $leafB= $leaf =~ /^\./u
ABresult
0X0
100
111
186 # hidden determined by leading '.' in filename
187 | (defined $leaf && $leaf =~ /^\./? ATTR_HIDDEN : 0)
188 };
189 10517 my $file;
190 10517 ▶ if (S_ISREG($spec->{mode})) {
Branch: 2 branches
BranchTrueFalseTotal
if (S_ISREG($spec->{'mode'})) { } 10516 1 10517
elsif (S_ISDIR($spec->{'mode'})) { } 1 0 1
191 my ($size, $offset, $align, $data_ref)
192 10516 = @{$spec}{qw( size device_offset device_align data )};
193 10516 ▶ $data_ref= do { my $x= $data_ref; \$x }
Branch: 1 branch
BranchTrueFalseTotal
if defined $data_ref and not ref $data_ref 1573 8943 10516
Condition66%2/3
defined $data_ref and not ref $data_ref
!ll&&!rl&&r
089431573
MC/DC50%1/2
defined $data_ref and not ref $data_ref
defined $data_ref not ref $data_ref
Truth table66%2/3
A and not(B)
A= defined $data_refB= not ref $data_ref
ABresult
0X0
100
111
194 if defined $data_ref && !ref $data_ref;
195 10516 ▶ if ($size) {
Branch: 2 branches
BranchTrueFalseTotal
if ($size) { } 0 10516 10516
elsif (defined $data_ref) { } 10516 0 10516
196 # ensure data matches
197 0 ▶ croak "File $path has size=$size but lacks 'data' attribute"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $data_ref 0 0 0
198 unless defined $data_ref;
199 0 ▶ croak "File $path ->{data} length disagrees with ->{size}"
Branch: 1 branch
BranchTrueFalseTotal
unless length $$data_ref == $size 0 0 0
200 unless length($$data_ref) == $size;
201 } elsif (defined $data_ref) {
202 10516 ▶ $size //= length($$data_ref);
Condition33%1/3
$size //= length $$data_ref
l!l&&r!l&&!r
0105160
MC/DC0%0/2
$size //= length $$data_ref
$size length $$data_ref
Truth table33%1/3
A //= B
A= $sizeB= length $$data_ref
ABresult
1X1
011
000
203 }
204 # must be a power of 2
205 10516 ▶ croak "Invalid device_align $align for '$path', must be a power of 2"
Branch: 1 branch
BranchTrueFalseTotal
if defined $align and not &isa_pow2($align) 0 10516 10516
Condition66%2/3
defined $align and not &isa_pow2($align)
!ll&&!rl&&r
10479370
MC/DC0%0/2
defined $align and not &isa_pow2($align)
defined $align not &isa_pow2($align)
Truth table66%2/3
A and not(B)
A= defined $alignB= not &isa_pow2($align)
ABresult
0X0
100
111
206 if defined $align && !isa_pow2 $align;
207 # Sanity check device_offset before we get too far along
208 10516 ▶ if (defined $offset) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $offset) 1 10515 10516
209 1 ▶ $align //= 512;
Condition50%1/2
$align //= 512
l!l
01
MC/DC0%0/1
$align //= 512
$align
Truth table50%1/2
A
A= $align
Aresult
11
00
210 # must fall in the data area
211 1 ▶ $offset > $self->volume_offset + _minimum_offset_to_data
Branch: 1 branch
BranchTrueFalseTotal
$offset > $self->volume_offset + _minimum_offset_to_data() and not $offset & $align - 1 or croak("Invalid device_offset '${offset}' for file '${path}'") 1 0 1
Condition33%1/3
$offset > $self->volume_offset + _minimum_offset_to_data() and not $offset & $align - 1
!ll&&!rl&&r
001
MC/DC0%0/2
$offset > $self->volume_offset + _minimum_offset_to_data() and not $offset & $align - 1
$offset > $self->volume_offset + _minimum_offset_to_data() not $offset & $align - 1
Truth table33%1/3
A and not(B)
A= $offset > $self->volume_offset + _minimum_offset_to_data()B= not $offset & $align - 1
ABresult
0X0
100
111
212 # must be a multiple of at least 512 (probably more)
213 && !($offset & ($align-1))
214 or croak "Invalid device_offset '$offset' for file '$path'";
215 }
216 $file= Sys::Export::VFAT::File->new(
217 name => "/$path", size => $size, flags => $flags,
218 align => $align, device_offset => $offset, data => $data_ref,
219 10516 $spec->%{qw( atime btime mtime )},
220 );
221 } elsif (S_ISDIR($spec->{mode})) {
222 1 $flags |= ATTR_DIRECTORY;
223 # If adding this directory overtop a previous auto-vivified directory, the ->{file}
224 # will be empty and we can just update it.
225 1 my $cur= $parent->entry($leaf);
226 croak "Attempt to add duplicate directory $leaf"
227 1 ▶ if $cur && $cur->{file};
Branch: 1 branch
BranchTrueFalseTotal
if $cur and $cur->{"file"} 0 1 1
Condition33%1/3
$cur and $cur->{"file"}
!ll&&!rl&&r
100
MC/DC0%0/2
$cur and $cur->{"file"}
$cur $cur->{"file"}
Truth table33%1/3
A and B
A= $curB= $cur->{"file"}
ABresult
0X0
100
111
228 $file= Sys::Export::VFAT::File->new(
229 1 name => "/$path", size => 0, flags => $flags, $spec->%{qw( atime btime mtime )},
230 );
231 1 ▶ if ($cur) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cur) 0 1 1
232 0 $cur->{file}= $file;
233 0 $cur->{dir}{file}= $file;
234 0 $log->debugf("updated attributes of %s", $path);
235 0 return $cur;
236 }
237 # otherwise, add this file to a directory entry
238 }
239 else {
240 # TODO: add conditional symlink support via hardlinks
241 0 croak "Can only export files or directories into VFAT"
242 }
243  
244 # this also checks for name collisions on shortname
245 10517 my $ent= $parent->add($leaf, $file, shortname => $spec->{FAT_shortname});
246 # If the dirent is a directory, also add a directory object to the dirent
247 10517 ▶ if ($file->is_dir) {
Branch: 1 branch
BranchTrueFalseTotal
if ($file->is_dir) 1 10516 10517
248 # the directory object also gets a reference to its file object.
249 1 weaken($ent->{dir}= $self->_new_dir("/$path", $parent, $file));
250 }
251  
252 $log->debugf("added %s longname=%s shortname=%s %s",
253 $path, $ent->{name}, $ent->{shortname}//'', join(' ',
254 !$ent->{file}? ('size=0 (empty file)')
255 : $ent->{file}->is_dir? ('DIR')
256 : (
257 (defined $ent->{file}->size? sprintf("size=0x%X", $ent->{file}->size) : 'size=<undef>'),
258 (defined $ent->{file}->align? sprintf("device_align=0x%X", $ent->{file}->align) : ()),
259 10517 ▶ (defined $ent->{file}->device_offset? sprintf("device_offset=0x%X", $ent->{file}->device_offset) : ())
Branch: 6 branches
BranchTrueFalseTotal
if $Sys::Export::VFAT::log->is_debug 0 10517 10517
!$ent->{'file'} ? : 0 0 0
$ent->{'file'}->is_dir ? : 0 0 0
defined $ent->{'file'}->size ? : 0 0 0
defined $ent->{'file'}->align ? : 0 0 0
defined $ent->{'file'}->device_offset ? : 0 0 0
Condition0%0/2
$ent->{"shortname"} // ""
l!l
00
MC/DC0%0/1
$ent->{"shortname"} // ""
$ent->{"shortname"}
Truth table0%0/2
A
A= $ent->{"shortname"}
Aresult
11
00
260 )
261 ))
262 if $log->is_debug;
263  
264 10517 $ent->{file};
265 }
266  
267  
268 31 ▶sub finish($self) {
Subroutine finish: called CC=11 SCAR=29.5
Pod documented
269 31 my $root= $self->root;
270 31 $log->debug('begin VFAT::finish');
271 # Find out the size of every directory, and build ->{_allocs}, ->{_dir_allocs} and ->{_special_allocs}
272 31 $self->_calc_dir_size($_) for $root, values $self->{_subdirs}->%*;
273 # calculate what geometry gives us the best size, when rounding each file to that cluster
274 # size vs. the size of the FAT it generates
275 my ($geom, $alloc)= $self->_optimize_geometry
276 or croak join("\n", "No geometry options can meet your device_offset / device_align requests:",
277 map "$_: $self->{_optimize_geometry_fail_reason}{$_}",
278 0 sort { $a <=> $b } keys $self->{_optimize_geometry_fail_reason}->%*
279 31 ▶ );
Branch: 1 branch
BranchTrueFalseTotal
() = $self->_optimize_geometry or croak(join("\n", "No geometry options can meet your device_offset / device_align requests:", map((qq[${_}: $self->{"_optimize_geometry_fail_reason"}{$_}]), (sort {$a <=> $b;} keys %{$$self{"_optimize_geometry_fail_reason"}})))) 31 0 31
280 31 $self->{geometry}= $geom;
281 31 $self->{allocation_table}= $alloc;
282 31 $self->_commit_allocation; # copy cluster IDs into each of the File objects
283 # Pack directories now that all file cluster ids are known
284 31 $self->_pack_directory($_) for $root, values $self->{_subdirs}->%*;
285  
286 31 my $fh= $self->filehandle;
287 31 ▶ if (!$fh) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($fh) 0 31 31
288 0 ▶ defined $self->filename or croak "Must set filename or filehandle attributes";
Branch: 1 branch
BranchTrueFalseTotal
defined $self->filename or croak("Must set filename or filehandle attributes") 0 0 0
289 0 ▶ open $fh, '+>', $self->filename
Branch: 1 branch
BranchTrueFalseTotal
open $fh, "+>", $self->filename or croak("open: $!") 0 0 0
290 or croak "open: $!";
291 }
292 # check size
293 31 ▶ if (-s $fh < $geom->volume_offset + $geom->total_size) {
Branch: 1 branch
BranchTrueFalseTotal
if (-s $fh < $geom->volume_offset + $geom->total_size) 31 0 31
294 31 $log->debugf('resize output file to %s', $geom->volume_offset + $geom->total_size);
295 31 ▶ truncate($fh, $geom->volume_offset + $geom->total_size)
Branch: 1 branch
BranchTrueFalseTotal
truncate $fh, $geom->volume_offset + $geom->total_size or croak("truncate: $!") 31 0 31
296 or croak "truncate: $!";
297 }
298 31 $self->_write_filesystem($fh, $geom, $alloc);
299 31 ▶ unless ($self->filehandle) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->filehandle) 0 31 31
300 0 ▶ $fh->close or croak "close: $!";
Branch: 1 branch
BranchTrueFalseTotal
$fh->close or croak("close: $!") 0 0 0
301 }
302 31 $log->debug('end VFAT::finish');
303 31 1;
304 }
305  
306 0 ▶sub _log_hexdump($buf) {
Subroutine _log_hexdump: not called CC=2 SCAR=17.9
307 $log->tracef('%04X'.(" %02x"x16), $_, unpack 'C*', substr($buf, $_*16, 16))
308 0 for 0..ceil(length($buf) / 16);
309 }
310  
311 31 ▶sub _write_filesystem($self, $fh, $geom, $alloc) {
Subroutine _write_filesystem: called CC=13 SCAR=26.9
312 31 ▶ ($alloc->max_cluster_id//-1) == ($geom->max_cluster_id//-1)
Branch: 1 branch
BranchTrueFalseTotal
($alloc->max_cluster_id // -1) == ($geom->max_cluster_id // -1) or croak(q[Max element of 'fat_entries' should be ] . $geom->max_cluster_id . ', but was ' . $alloc->max_cluster_id) 31 0 31
Condition50%2/4
$alloc->max_cluster_id // -1
l!l
310
$geom->max_cluster_id // -1
l!l
310
MC/DC0%0/1
$alloc->max_cluster_id // -1
$alloc->max_cluster_id
MC/DC0%0/1
$geom->max_cluster_id // -1
$geom->max_cluster_id
Truth table50%1/2
A
A= $alloc->max_cluster_id
Aresult
11
00
Truth table50%1/2
A
A= $geom->max_cluster_id
Aresult
11
00
313 or croak "Max element of 'fat_entries' should be ".$geom->max_cluster_id.", but was ".$alloc->max_cluster_id;
314 # Pack the boot sector and other reserved sectors
315 31 my $buf= $self->_pack_reserved_sectors;
316 31 my $ofs= $self->volume_offset;
317 31 write_file_extent($fh, $ofs, $geom->reserved_size, \$buf, 0, 'reserved sectors');
318 31 $ofs += $geom->reserved_size;
319 # Pack the allocation tables
320 31 $buf= $self->_pack_allocation_table($alloc);
321 # store a copy of this into each of the regions occupied by fats
322 31 ▶ for (my $i= 0; $i < $geom->fat_count; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $geom->fat_count) 62 31 93
323 62 write_file_extent($fh, $ofs, $geom->fat_size, \$buf, 0, "fat table $i");
324 62 $ofs += $geom->fat_size;
325 }
326 # For FAT12/FAT16, write the root directory entries
327 31 ▶ if ($geom->bits < FAT32) {
Branch: 1 branch
BranchTrueFalseTotal
if ($geom->bits < "FAT32") 30 1 31
328 30 my $rootf= $self->root->file;
329 die "BUG: mis-sized FAT16 root directory"
330 if !$rootf->size || ($rootf->size & 31)
331 30 ▶ || length ${$rootf->data} != $rootf->size
Branch: 1 branch
BranchTrueFalseTotal
if not $rootf->size or $rootf->size & 31 or length ${$rootf->data;} != $rootf->size or $rootf->size > $geom->root_dir_size 0 30 30
Condition33%3/9
not $rootf->size or $rootf->size & 31 or length ${$rootf->data;} != $rootf->size or $rootf->size > $geom->root_dir_size
l!l&&r!l&&!r
0030
not $rootf->size or $rootf->size & 31 or length ${$rootf->data;} != $rootf->size
l!l&&r!l&&!r
0030
not $rootf->size or $rootf->size & 31
l!l&&r!l&&!r
0030
MC/DC0%0/4
not $rootf->size or $rootf->size & 31 or length ${$rootf->data;} != $rootf->size or $rootf->size > $geom->root_dir_size
not $rootf->size $rootf->size & 31 length ${$rootf->data;} != $rootf->size $rootf->size > $geom->root_dir_size
Truth table20%1/5
not(A) or B or C or D
A= not $rootf->sizeB= $rootf->size & 31C= length ${$rootf->data;} != $rootf->sizeD= $rootf->size > $geom->root_dir_size
ABCDresult
1XXX1
01XX1
001X1
00011
00000
332 || $rootf->size > $geom->root_dir_size;
333 30 write_file_extent($fh, $ofs, $geom->root_dir_size, $rootf->data, 0, 'root dir');
334 }
335 # The files and dirs have all been assigned clusters by _optimize_geometry
336 31 for my $cl (sort { $a <=> $b } keys $alloc->chains->%*) {
337 10551 my ($invlist, $file)= $alloc->chains->{$cl}->@{'invlist','file'};
338 10551 my $data= $file->data;
339 # Given an inversion list describing the allocated clusters for this file,
340 # write the relevant chunks of the file to those cluster data areas.
341 10551 ▶ $log->debugf("writing '%s' at cluster %s", $file->name, _render_invlist($invlist))
Branch: 1 branch
BranchTrueFalseTotal
if $Sys::Export::VFAT::log->is_debug 0 10551 10551
342 if $log->is_debug;
343 10551 my $data_ofs= 0;
344 10551 ▶ for (my $i= 0; $i < @$invlist; $i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$invlist) 10551 10551 21102
345 10551 my ($cl_start, $cl_lim)= @{$invlist}[$i, $i+1];
346 10551 my $size= ($cl_lim-$cl_start) * $geom->bytes_per_cluster;
347 10551 write_file_extent($fh, $geom->get_cluster_device_offset($cl_start), $size, $data, $data_ofs);
348 10551 $data_ofs += $size;
349 }
350 }
351 }
352 0 ▶sub _render_invlist($il) {
Subroutine _render_invlist: not called CC=2 SCAR=17.9
353 0 ▶ join ',',
Branch: 1 branch
BranchTrueFalseTotal
$$il[$_ * 2] == $$il[$_ * 2 + 1] - 1 ? : 0 0 0
354 map +($il->[$_*2] == $il->[$_*2+1]-1? $il->[$_*2] : $il->[$_*2] . '-' . $il->[$_*2+1]),
355 0 .. int($#$il/2)
356 }
357  
358  
359 10556 ▶sub is_valid_longname($name) {
Subroutine is_valid_longname: called CC=2 SCAR=9.2
Pod documented
360 # characters permitted for LFN are all letters numbers and $%'-_@~`!(){}^#&+,;=[].
361 # and space and all codepoints above 0x7F.
362 # they may not begin with space, and cannot exceed 255 chars.
363 10556 ▶ !!($name !~ /^(\.+\.?)\z/ # dot and dotdot are reserved
Condition33%1/3
!($name =~ /^(\.+\.?)\z/u) && $name =~ /^ [^\x00-\x20\x22\x2A\x2F\x3A\x3C\x3E\x3F\x5C\x7C\x7F] [^\x00-\x1F\x22\x2A\x2F\x3A\x3C\x3E\x3F\x5C\x7C]{0,254} \z/ux
!ll&&!rl&&r
0010556
MC/DC0%0/2
!($name =~ /^(\.+\.?)\z/u) && $name =~ /^ [^\x00-\x20\x22\x2A\x2F\x3A\x3C\x3E\x3F\x5C\x7C\x7F] [^\x00-\x1F\x22\x2A\x2F\x3A\x3C\x3E\x3F\x5C\x7C]{0,254} \z/ux
!($name =~ /^(\.+\.?)\z/u) $name =~ /^ [^\x00-\x20\x22\x2A\x2F\x3A\x3C\x3E\x3F\x5C\x7C\x7F] [^\x00-\x1F\x22\x2A\x2F\x3A\x3C\x3E\x3F\x5C\x7C]{0,254} \z/ux
Truth table33%1/3
not(A) && B
A= !($name =~ /^(\.+\.?)\z/u)B= $name =~ /^ [^\x00-\x20\x22\x2A\x2F\x3A\x3C\x3E\x3F\x5C\x7C\x7F] [^\x00-\x1F\x22\x2A\x2F\x3A\x3C\x3E\x3F\x5C\x7C]{0,254} \z/ux
ABresult
0X0
100
111
364 && $name =~ /^
365 [^\x00-\x20\x22\x2A\x2F\x3A\x3C\x3E\x3F\x5C\x7C\x7F]
366 [^\x00-\x1F\x22\x2A\x2F\x3A\x3C\x3E\x3F\x5C\x7C]{0,254}
367 \z/x);
368 }
369  
370 10602 ▶sub is_valid_shortname($name) {
Subroutine is_valid_shortname: called CC=2 SCAR=6.9
Pod documented
371 10602 ▶ !!($name eq uc $name
Condition100%3/3
$name eq uc $name && $name =~ /^ [\x21\x23-\x29\x2D\x30-\x39\x40-\x5A\x5E-\x7B\x7D\x7E\x80-\xFF] [\x20\x21\x23-\x29\x2D\x30-\x39\x40-\x5A\x5E-\x7B\x7D\x7E\x80-\xFF]{0,7} (?: \. ( [\x21\x23-\x29\x2D\x30-\x39\x40-\x5A\x5E-\x7B\x7D-\xFF] [\x20\x21\x23-\x29\x2D\x30-\x39\x40-\x5A\x5E-\x7B\x7D-\xFF]{0,2} )? )? \z/ux
!ll&&!rl&&r
361010556
MC/DC100%2/2
$name eq uc $name && $name =~ /^ [\x21\x23-\x29\x2D\x30-\x39\x40-\x5A\x5E-\x7B\x7D\x7E\x80-\xFF] [\x20\x21\x23-\x29\x2D\x30-\x39\x40-\x5A\x5E-\x7B\x7D\x7E\x80-\xFF]{0,7} (?: \. ( [\x21\x23-\x29\x2D\x30-\x39\x40-\x5A\x5E-\x7B\x7D-\xFF] [\x20\x21\x23-\x29\x2D\x30-\x39\x40-\x5A\x5E-\x7B\x7D-\xFF]{0,2} )? )? \z/ux
$name eq uc $name $name =~ /^ [\x21\x23-\x29\x2D\x30-\x39\x40-\x5A\x5E-\x7B\x7D\x7E\x80-\xFF] [\x20\x21\x23-\x29\x2D\x30-\x39\x40-\x5A\x5E-\x7B\x7D\x7E\x80-\xFF]{0,7} (?: \. ( [\x21\x23-\x29\x2D\x30-\x39\x40-\x5A\x5E-\x7B\x7D-\xFF] [\x20\x21\x23-\x29\x2D\x30-\x39\x40-\x5A\x5E-\x7B\x7D-\xFF]{0,2} )? )? \z/ux
Truth table100%3/3
A && B
A= $name eq uc $nameB= $name =~ /^ [\x21\x23-\x29\x2D\x30-\x39\x40-\x5A\x5E-\x7B\x7D\x7E\x80-\xFF] [\x20\x21\x23-\x29\x2D\x30-\x39\x40-\x5A\x5E-\x7B\x7D\x7E\x80-\xFF]{0,7} (?: \. ( [\x21\x23-\x29\x2D\x30-\x39\x40-\x5A\x5E-\x7B\x7D-\xFF] [\x20\x21\x23-\x29\x2D\x30-\x39\x40-\x5A\x5E-\x7B\x7D-\xFF]{0,2} )? )? \z/ux
ABresult
0X0
100
111
372 && $name =~ /^
373 [\x21\x23-\x29\x2D\x30-\x39\x40-\x5A\x5E-\x7B\x7D\x7E\x80-\xFF]
374 [\x20\x21\x23-\x29\x2D\x30-\x39\x40-\x5A\x5E-\x7B\x7D\x7E\x80-\xFF]{0,7}
375 (?: \.
376 ( [\x21\x23-\x29\x2D\x30-\x39\x40-\x5A\x5E-\x7B\x7D-\xFF]
377 [\x20\x21\x23-\x29\x2D\x30-\x39\x40-\x5A\x5E-\x7B\x7D-\xFF]{0,2}
378 )?
379 )?
380 \z/x);
381 }
382  
383 0 ▶sub is_valid_volume_label($name) {
Subroutine is_valid_volume_label: not called CC=1 SCAR=6.9
Pod documented
384 # same as shortname but no '.' and space is allowed
385 0 !!($name =~ /^
386 [\x21\x23-\x29\x2D\x30-\x39\x40-\x5A\x5E-\x7B\x7D-\xFF]
387 [\x20\x21\x23-\x29\x2D\x30-\x39\x40-\x5A\x5E-\x7B\x7D-\xFF]{0,10}
388 \z/x);
389 }
390  
391 72 ▶sub remove_invalid_shortname_chars($name, $replacement='_') {
Subroutine remove_invalid_shortname_chars: called CC=2 SCAR=6.9
Pod documented
392 72 $name =~ tr/a-z/A-Z/; # perform 'uc' but only for the ASCII range
393 72 $name =~ s/[^\x20\x21\x23-\x29\x2D\x30-\x39\x40-\x5A\x5E-\x7B\x7D\x7E\x80-\xFF]+/$replacement/gr;
394 }
395  
396 31 ▶sub _optimize_geometry($self) {
Subroutine _optimize_geometry: called CC=42 SCAR=41.1
397 # calculate what geometry gives us the best size, when rounding each file to that cluster
398 # size vs. the size of the FAT it generates, and also meting the needs of alignment requests
399 31 my $root= $self->root;
400 31 my (@offsets, @aligned, @others);
401 31 my %seen= ( refaddr($root) => 1 );
402 31 for my $dir ($root, values $self->{_subdirs}->%*) {
403 65 for my $ent ($dir->entries->@*) {
404 # entry may have a directory ref and not a direct file ref
405 10550 ▶ my $file= $ent->{file} //= $ent->{dir} && $ent->{dir}->file;
Condition50%3/6
$ent->{"file"} //= $ent->{"dir"} && $ent->{"dir"}->file
l!l&&r!l&&!r
10517330
$ent->{'dir'} && $ent->{'dir'}->file
!ll&&!rl&&r
0033
MC/DC0%0/3
$ent->{"file"} //= $ent->{"dir"} && $ent->{"dir"}->file
$ent->{"file"} $ent->{'dir'} $ent->{'dir'}->file
Truth table0%0/4
A //= B && C
A= $ent->{"file"}B= $ent->{'dir'}C= $ent->{'dir'}->file
ABCresult
1XX1
0111
00X0
0100
406 10550 ▶ next unless $file && !$seen{refaddr $file}++;
Branch: 1 branch
BranchTrueFalseTotal
unless $file and not $seen{builtin::refaddr($file)}++ 0 10550 10550
Condition33%1/3
$file and not $seen{builtin::refaddr($file)}++
!ll&&!rl&&r
0010550
MC/DC0%0/2
$file and not $seen{builtin::refaddr($file)}++
$file not $seen{builtin::refaddr($file)}++
Truth table33%1/3
A and not(B)
A= $fileB= not $seen{builtin::refaddr($file)}++
ABresult
0X0
100
111
407 10550 ▶ push @{$file->device_offset? \@offsets : $file->align? \@aligned : \@others}, $file;
Branch: 2 branches
BranchTrueFalseTotal
$file->device_offset ? : 1 10549 10550
$file->align ? : 37 10512 10549
408 }
409 }
410 31 $log->debugf("_optimize_geometry offsets=%d aligned=%d others=%d",
411 scalar @offsets, scalar @aligned, scalar @others);
412 # provide stable results
413 0 @offsets= sort { $a->device_offset <=> $b->device_offset } @offsets;
414 31 @aligned= sort { fc $a->name cmp fc $b->name } @aligned;
415 31 @others= sort { fc $a->name cmp fc $b->name } @others;
416 31 my $min_ofs= min(map $_->device_offset, @offsets);
417 31 my $max_ofs= max(map $_->device_offset + $_->size, @offsets);
418 31 my $max_align= max(0, map $_->align, @aligned);
419 31 my $root_dirent_used= $root->file->size / 32;
420 31 ▶ isa_int $root_dirent_used && $root_dirent_used >= 1
Branch: 1 branch
BranchTrueFalseTotal
&isa_int($root_dirent_used) and $root_dirent_used >= 1 or die "BUG: root must always have one entry" 31 0 31
Condition33%1/3
&isa_int($root_dirent_used) and $root_dirent_used >= 1
!ll&&!rl&&r
0031
MC/DC0%0/2
&isa_int($root_dirent_used) and $root_dirent_used >= 1
&isa_int($root_dirent_used) $root_dirent_used >= 1
Truth table33%1/3
A and B
A= &isa_int($root_dirent_used)B= $root_dirent_used >= 1
ABresult
0X0
100
111
421 or die "BUG: root must always have one entry";
422 31 my $bytes_per_sector= $self->bytes_per_sector;
423 31 my %fail_reason;
424 my $best;
425 # If the user defined sectors_per_cluster, we only have one option.
426 # Otherwise iterate through all of them to find the best.
427 31 ▶ my @spc= defined $self->sectors_per_cluster? ( $self->sectors_per_cluster )
Branch: 1 branch
BranchTrueFalseTotal
defined $self->sectors_per_cluster ? : 3 28 31
428 : (1,2,4,8,16,32,64,128);
429 31 cluster_size: for my $sectors_per_cluster (@spc) {
430 227 my $cluster_size= $sectors_per_cluster * $bytes_per_sector;
431 227 ▶ isa_pow2 $cluster_size or die "BUG: cluster_size not a power of 2";
Branch: 1 branch
BranchTrueFalseTotal
&isa_pow2($cluster_size) or die "BUG: cluster_size not a power of 2" 227 0 227
432 # Avoid triggering warning about incompatible cluster sizes if a good cluster size was
433 # already found.
434 227 ▶ last if $best && $cluster_size > 32*1024;
Branch: 1 branch
BranchTrueFalseTotal
if $best and $cluster_size > 32768 28 199 227
Condition100%3/3
$best and $cluster_size > 32768
!ll&&!rl&&r
3116828
MC/DC100%2/2
$best and $cluster_size > 32768
$best $cluster_size > 32768
Truth table100%3/3
A and B
A= $bestB= $cluster_size > 32768
ABresult
0X0
100
111
435 # Count total sectors used by ->{size} of files and dirs.
436 # Don't add root dir until we know it will be FAT32
437 199 my $clusters= 0;
438 199 for (@offsets, @aligned, @others) {
439 20000 $clusters += ceil($_->size / $cluster_size);
440 }
441 199 $log->tracef("with sectors_per_cluster=%d, would require at least %d clusters",
442 $sectors_per_cluster, $clusters);
443 199 ▶ $clusters ||= 1;
Condition100%2/2
$clusters ||= 1
l!l
18514
MC/DC100%1/1
$clusters ||= 1
$clusters
Truth table100%2/2
A
A= $clusters
Aresult
11
00
444 199 my ($reserved, $root_clusters_added);
445 # If file alignment is a larger power of 2 than cluster_size, then as long as data_start
446 # is aligned to cluster_size there will be a cluster that can satisfy the alignment.
447 # If file alignment is a smaller power of 2 than cluster_size, then as long as
448 # data_start is aligned to the file alignment, every cluster can satisfy the alignment.
449 199 my $align= min($cluster_size, $max_align);
450 199 ▶ if ($align) {
Branch: 2 branches
BranchTrueFalseTotal
if ($align) { } 133 66 199
elsif (@offsets) { } 7 59 66
451 # But wait, does every device_offset meet this alignment? If not, give up.
452 133 for (@offsets) {
453 0 ▶ if ($_->device_offset & ($align-1)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_->device_offset & $align - 1) 0 0 0
454 0 $fail_reason{$sectors_per_cluster}= "device_offset ".$_->device_offset
455 ." of ".$_->name." conflicts with your alignment request of $align";
456 0 next cluster_size;
457 }
458 }
459 }
460 elsif (@offsets) {
461 # If not aligning clusters to pow2, might need to align to device_offset.
462 # First, every device_offset must have the same remainder modulo cluster_size.
463 7 my ($remainder, $prev);
464 7 for (@offsets) {
465 7 my $r= $_->device_offset & ($cluster_size-1);
466 7 ▶ if (!defined $remainder) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $remainder) { } 7 0 7
elsif ($remainder != $r) { } 0 0 0
467 7 $remainder= $r;
468 7 $prev= $_;
469 } elsif ($remainder != $r) {
470 0 $fail_reason{$sectors_per_cluster}= "file $_->{name} device_offset "
471 .$_->device_offset." modulo cluster_size $cluster_size conflicts with"
472 ." file ".$prev->name." device_offset ".$prev->device_offset;
473 0 next cluster_size;
474 }
475 }
476 7 ▶ if ($remainder) {
Branch: 1 branch
BranchTrueFalseTotal
if ($remainder) { } 0 7 7
477 0 $align= [ $cluster_size, $remainder ];
478 } else {
479 7 $align= $cluster_size;
480 }
481 }
482 again_with_more_clusters: {
483 # If this number of clusters pushes us into FAT32, also need to add the root directory
484 # clusters to the count.
485 256 ▶ if (!$root_clusters_added
Branch: 1 branch
BranchTrueFalseTotal
if (not $root_clusters_added and $clusters > 65509) 1 255 256
Condition66%2/3
not $root_clusters_added and $clusters > 65509
!ll&&!rl&&r
02551
MC/DC50%1/2
not $root_clusters_added and $clusters > 65509
not $root_clusters_added $clusters > 65509
Truth table66%2/3
not(A) and B
A= not $root_clusters_addedB= $clusters > 65509
ABresult
0X0
100
111
486 && $clusters > Sys::Export::VFAT::Geometry::FAT16_IDEAL_MAX_CLUSTERS()
487 ) {
488 1 $root_clusters_added= ceil($root->file->size / $cluster_size);
489 1 $clusters += $root_clusters_added;
490 1 $log->tracef("reached FAT32 threshold, adding %s clusters for root dir", $root_clusters_added);
491 }
492 256 my $geom= Sys::Export::VFAT::Geometry->new(
493 volume_offset => $self->volume_offset,
494 (align_clusters => $align)x!!$align,
495 bytes_per_sector => $bytes_per_sector,
496 sectors_per_cluster => $sectors_per_cluster,
497 fat_count => $self->fat_count,
498 cluster_count => $clusters,
499 used_root_dirent_count => $root_dirent_used,
500 min_bits => $self->min_bits,
501 );
502 256 $log->debugf("testing clusters=%d size=0x%X data_region=0x%X-%X min_ofs=0x%X max_ofs=0x%X",
503 $clusters, $cluster_size, $geom->data_start_device_offset, $geom->data_limit_device_offset,
504 $min_ofs, $max_ofs);
505 256 ▶ if (@offsets || @aligned) {
Branch: 1 branch
BranchTrueFalseTotal
if (@offsets or @aligned) 197 59 256
Condition100%3/3
@offsets or @aligned
l!l&&r!l&&!r
1418359
MC/DC100%2/2
@offsets or @aligned
@offsets @aligned
Truth table100%3/3
A or B
A= @offsetsB= @aligned
ABresult
1X1
011
000
506 # tables are too large? Try again with larger clusters.
507 197 ▶ if (defined $min_ofs && $min_ofs < $geom->data_start_device_offset) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $min_ofs and $min_ofs < $geom->data_start_device_offset) 0 197 197
Condition66%2/3
defined $min_ofs and $min_ofs < $geom->data_start_device_offset
!ll&&!rl&&r
183140
MC/DC0%0/2
defined $min_ofs and $min_ofs < $geom->data_start_device_offset
defined $min_ofs $min_ofs < $geom->data_start_device_offset
Truth table66%2/3
A and B
A= defined $min_ofsB= $min_ofs < $geom->data_start_device_offset
ABresult
0X0
100
111
508 0 $fail_reason{$sectors_per_cluster}= "FAT tables too large for requested device_offset $min_ofs";
509 0 next cluster_size;
510 }
511 # Not enough clusters? Try again with more.
512 197 ▶ if (defined $max_ofs && $max_ofs > $geom->data_limit_device_offset) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $max_ofs and $max_ofs > $geom->data_limit_device_offset) 7 190 197
Condition100%3/3
defined $max_ofs and $max_ofs > $geom->data_limit_device_offset
!ll&&!rl&&r
18377
MC/DC100%2/2
defined $max_ofs and $max_ofs > $geom->data_limit_device_offset
defined $max_ofs $max_ofs > $geom->data_limit_device_offset
Truth table100%3/3
A and B
A= defined $max_ofsB= $max_ofs > $geom->data_limit_device_offset
ABresult
0X0
100
111
513 # This might overshoot a bit since the tables also grow and push forward the
514 # whole data area.
515 7 $clusters= ceil(($max_ofs - $geom->data_start_device_offset) / $cluster_size);
516 7 goto again_with_more_clusters;
517 }
518 }
519 # Now verify we have enough clusters by actually alocating them
520 249 my $alloc= Sys::Export::VFAT::AllocationTable->new;
521 249 my %assignment;
522 249 ▶ unless (eval {
Branch: 1 branch
BranchTrueFalseTotal
unless (eval { do { $self->_alloc_file($geom, $alloc, $_) foreach (@offsets, @aligned, @others, $geom->bits == "FAT32" ? $root->file : ()); 1 } }) 0 249 249
523 $self->_alloc_file($geom, $alloc, $_)
524 249 ▶ for @offsets, @aligned, @others, ($geom->bits == FAT32? ($root->file) : ());
Branch: 1 branch
BranchTrueFalseTotal
$geom->bits == 32 ? : 1 248 249
525 249 1
526 }) {
527 0 chomp($fail_reason{$sectors_per_cluster}= "$@");
528 0 next cluster_size;
529 }
530 249 ▶ if ($alloc->max_used_cluster_id > $geom->max_cluster_id) {
Branch: 1 branch
BranchTrueFalseTotal
if ($alloc->max_used_cluster_id > $geom->max_cluster_id) 50 199 249
531 50 $clusters= $alloc->max_used_cluster_id-1;
532 50 goto again_with_more_clusters;
533 }
534 # Allocation worked, so clamp the allocator to this nmber of sectors
535 199 $alloc->max_cluster_id($geom->max_cluster_id);
536 # Is this the smallest option so far?
537 199 ▶ if (!$best || $best->{geom}->total_sector_count > $geom->total_sector_count) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $best or $best->{"geom"}->total_sector_count > $geom->total_sector_count) 32 167 199
Condition100%3/3
not $best or $best->{"geom"}->total_sector_count > $geom->total_sector_count
l!l&&r!l&&!r
311167
MC/DC100%2/2
not $best or $best->{"geom"}->total_sector_count > $geom->total_sector_count
not $best $best->{"geom"}->total_sector_count > $geom->total_sector_count
Truth table100%3/3
not(A) or B
A= not $bestB= $best->{"geom"}->total_sector_count > $geom->total_sector_count
ABresult
1X1
011
000
538 32 $best= { geom => $geom, alloc => $alloc, cluster_assignment => \%assignment };
539 }
540 }
541 } continue {
542 $log->tracef("%s", $fail_reason{$sectors_per_cluster})
543 199 ▶ if defined $fail_reason{$sectors_per_cluster};
Branch: 1 branch
BranchTrueFalseTotal
if defined $fail_reason{$sectors_per_cluster} 0 199 199
544 }
545 31 ▶ if (!$best) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($best) 0 31 31
546 0 $log->debug("no cluster size works");
547 0 $self->{_optimize_geometry_fail_reason}= \%fail_reason;
548 0 return;
549 }
550 31 $log->debugf("best cluster_size is %d", $best->{geom}->bytes_per_cluster);
551 31 return @{$best}{'geom','alloc'};
552 }
553  
554 # reserve clusters for a file according to the align/offset needs of that file
555 20101 ▶sub _alloc_file($self, $geom, $alloc, $file) {
Subroutine _alloc_file: called CC=7 SCAR=20.4
556 0 ▶ my $sz= $file->size or do { carp "Attempt to allocate zero-length file"; return };
Branch: 1 branch
BranchTrueFalseTotal
my $sz = $file->size or do { carp("Attempt to allocate zero-length file"); return } 20101 0 20101
557 20101 my $cl_count= POSIX::ceil($sz / $geom->bytes_per_cluster);
558 20101 my $cl_start;
559 20101 ▶ if ($file->device_offset) {
Branch: 2 branches
BranchTrueFalseTotal
if ($file->device_offset) { } 7 20094 20101
elsif ($file->align) { } 359 19735 20094
560 7 my ($cl, $n)= $geom->get_cluster_extent_of_device_extent($file->device_offset, $sz);
561 7 ▶ $cl_start= $alloc->alloc_range($cl, $cl_count)
Condition50%1/2
$alloc->alloc_range($cl, $cl_count) // croak("Can't allocate $cl_count clusters from offset " . $file->device_offset)
l!l
70
MC/DC0%0/1
$alloc->alloc_range($cl, $cl_count) // croak("Can't allocate $cl_count clusters from offset " . $file->device_offset)
$alloc->alloc_range($cl, $cl_count)
Truth table50%1/2
A
A= $alloc->alloc_range($cl, $cl_count)
Aresult
11
00
562 // croak "Can't allocate $cl_count clusters from offset ".$file->device_offset;
563 } elsif ($file->align) {
564 359 my ($mul, $ofs)= $geom->get_cluster_alignment_of_device_alignment($file->align);
565 359 ▶ $cl_start= $alloc->alloc_contiguous($cl_count, $mul, $ofs)
Condition50%1/2
$alloc->alloc_contiguous($cl_count, $mul, $ofs) // croak("Can't allocate $cl_count clusters aligned to " . $file->align)
l!l
3590
MC/DC0%0/1
$alloc->alloc_contiguous($cl_count, $mul, $ofs) // croak("Can't allocate $cl_count clusters aligned to " . $file->align)
$alloc->alloc_contiguous($cl_count, $mul, $ofs)
Truth table50%1/2
A
A= $alloc->alloc_contiguous($cl_count, $mul, $ofs)
Aresult
11
00
566 // croak "Can't allocate $cl_count clusters aligned to ".$file->align;
567 } else {
568 19735 ▶ $cl_start= $alloc->alloc($cl_count)
Condition50%1/2
$alloc->alloc($cl_count) // croak("Can't allocate $cl_count clusters")
l!l
197350
MC/DC0%0/1
$alloc->alloc($cl_count) // croak("Can't allocate $cl_count clusters")
$alloc->alloc($cl_count)
Truth table50%1/2
A
A= $alloc->alloc($cl_count)
Aresult
11
00
569 // croak "Can't allocate $cl_count clusters";
570 }
571 20101 $alloc->{chains}{$cl_start}{file}= $file;
572 20101 $cl_start;
573 }
574  
575 # store the cluster and device offset into the File objects
576 31 ▶sub _commit_allocation($self) {
Subroutine _commit_allocation: called CC=4 SCAR=14.0
577 31 my $alloc= $self->allocation_table;
578 31 my $geom= $self->geometry;
579 # Apply file cluster IDs to the File objects
580 31 for (values $alloc->chains->%*) {
581 10551 my $file= $_->{file};
582 10551 $file->{cluster}= $_->{invlist}[0];
583 # Only set offset if file is contiguous
584 $file->{device_offset} //= $geom->get_cluster_device_offset($file->{cluster})
585 10551 ▶ if 2 == @{$_->{invlist}};
Branch: 1 branch
BranchTrueFalseTotal
if 2 == @{$_->{"invlist"};} 10551 0 10551
Condition66%2/3
$file->{"device_offset"} //= $geom->get_cluster_device_offset($file->{"cluster"})
l!l&&r!l&&!r
1105500
MC/DC0%0/2
$file->{"device_offset"} //= $geom->get_cluster_device_offset($file->{"cluster"})
$file->{"device_offset"} $geom->get_cluster_device_offset($file->{"cluster"})
Truth table66%2/3
A //= B
A= $file->{"device_offset"}B= $geom->get_cluster_device_offset($file->{"cluster"})
ABresult
1X1
011
000
586 }
587 }
588  
589 3 our @sector0_fields_common= (
590 [ BS_jmpBoot => 0, 3, 'a3', '' ],
591 [ BS_OEMName => 3, 8, 'a8', 'MSWIN4.1' ],
592 [ BPB_BytsPerSec => 11, 2, 'v' ],
593 [ BPB_SecPerClus => 13, 1, 'C' ],
594 [ BPB_RsvdSecCnt => 14, 2, 'v' ],
595 [ BPB_NumFATs => 16, 1, 'C' ],
596 [ BPB_RootEntCnt => 17, 2, 'v' ],
597 [ BPB_TotSec16 => 19, 2, 'v' ],
598 [ BPB_Media => 21, 1, 'C', 0xF8 ],
599 [ BPB_FATSz16 => 22, 2, 'v' ],
600 [ BPB_SecPerTrk => 24, 2, 'v', 0 ],
601 [ BPB_NumHeads => 26, 2, 'v', 0 ],
602 [ BPB_HiddSec => 28, 4, 'V', 0 ],
603 [ BPB_TotSec32 => 32, 4, 'V' ]
604 );
605 3 our @sector0_fat16_fields= (
606 @sector0_fields_common,
607 [ BS_DrvNum => 36, 1, 'C', 0x80 ],
608 [ BS_Reserved1 => 37, 1, 'C', 0 ],
609 [ BS_BootSig => 38, 1, 'C', 0x29 ],
610 [ BS_VolID => 39, 4, 'V' ],
611 [ BS_VolLab => 43, 11, 'A11', 'NO NAME' ],
612 [ BS_FilSysType => 54, 8, 'A8' ],
613 [ _signature => 510, 2, 'v', 0xAA55 ],
614 );
615 3 our @sector0_fat32_fields= (
616 @sector0_fields_common,
617 [ BPB_FATSz32 => 36, 4, 'V' ],
618 [ BPB_ExtFlags => 40, 2, 'v', 0 ],
619 [ BPB_FSVer => 42, 2, 'v', 0 ],
620 [ BPB_RootClus => 44, 4, 'V' ],
621 [ BPB_FSInfo => 48, 2, 'v' ],
622 [ BPB_BkBootSec => 50, 2, 'v', 0 ],
623 [ BPB_Reserved => 52, 12, 'a12', '' ],
624 [ BS_DrvNum => 64, 1, 'C', 0x80 ],
625 [ BS_Reserved1 => 65, 1, 'C', 0 ],
626 [ BS_BootSig => 66, 1, 'C', 0x29 ],
627 [ BS_VolID => 67, 4, 'V' ],
628 [ BS_VolLab => 71, 11, 'A11', 'NO NAME' ],
629 [ BS_FilSysType => 82, 8, 'A8' ],
630 [ _signature => 510, 2, 'v', 0xAA55 ],
631 );
632 3 our @fat32_fsinfo_fields= (
633 [ FSI_LeadSig => 0, 4, 'V', 0x41615252 ],
634 [ FSI_Reserved1 => 4,480, 'a480', '' ],
635 [ FSI_StrucSig => 484, 4, 'V', 0x61417272 ],
636 [ FSI_Free_Count => 488, 4, 'V', 0xFFFFFFFF ],
637 [ FSI_Nxt_Free => 492, 4, 'V', 0xFFFFFFFF ],
638 [ FSI_Reserved2 => 496, 12, 'a12', '' ],
639 [ FSI_TrailSig => 508, 4, 'V', 0xAA550000 ],
640 );
641 34 ▶sub _append_pack_args($pack, $vals, $ofs, $fields, $attrs) {
Subroutine _append_pack_args: called CC=4 SCAR=14.5
642 34 for (@$fields) {
643 700 push @$pack, '@'.($ofs+$_->[1]).$_->[3];
644 700 ▶ push @$vals, $attrs->{$_->[0]} // $_->[4]
Condition60%3/5
($$attrs{$_->[0]} // $_->[4]) // croak("No value supplied for $_->[0], and no default")
l!l
7000
$$attrs{$_->[0]} // $_->[4]
l!l&&r!l&&!r
3303700
MC/DC0%0/2
($$attrs{$_->[0]} // $_->[4]) // croak("No value supplied for $_->[0], and no default")
$$attrs{$_->[0]} $_->[4]
Truth table66%2/3
A // B
A= $$attrs{$_->[0]}B= $_->[4]
ABresult
1X1
011
000
645 // croak "No value supplied for $_->[0], and no default";
646 }
647 }
648  
649 31947 ▶sub _epoch_to_fat_date_time($epoch) {
Subroutine _epoch_to_fat_date_time: called CC=2 SCAR=6.9
650 31947 my @lt = localtime($epoch);
651 31947 my $year = $lt[5] + 1900;
652 31947 my $mon = $lt[4] + 1;
653 31947 my $mday = $lt[3];
654 31947 my $hour = $lt[2];
655 31947 my $min = $lt[1];
656 31947 my $sec = int($lt[0] / 2); # 2-second resolution
657  
658 31947 ▶ $year = 1980 if $year < 1980;
Branch: 1 branch
BranchTrueFalseTotal
if $year < 1980 0 31947 31947
659 31947 my $fat_date = (($year - 1980) << 9) | ($mon << 5) | $mday;
660 31947 my $fat_time = ($hour << 11) | ($min << 5) | $sec;
661 31947 my $fat_frac = ($epoch * 100) % 200;
662 31947 return ($fat_date, $fat_time, $fat_frac);
663 }
664  
665 # This packs the boot sector and all the "reserved" sectors that appear before the
666 # beginning of the allocation tables.
667 31 ▶sub _pack_reserved_sectors($self, %attrs) {
Subroutine _pack_reserved_sectors: called CC=13 SCAR=26.9
668 31 my (@pack, @vals);
669 31 my $geom= $self->geometry;
670 31 $attrs{BPB_BytsPerSec}= $geom->bytes_per_sector;
671 31 $attrs{BPB_SecPerClus}= $geom->sectors_per_cluster;
672 31 $attrs{BPB_RsvdSecCnt}= $geom->reserved_sector_count;
673 31 $attrs{BPB_NumFATs}= $geom->fat_count;
674 31 $attrs{BPB_RootEntCnt}= $geom->root_dirent_count;
675 31 ▶ $attrs{BS_VolLab} //= $self->volume_label;
Condition33%1/3
$attrs{"BS_VolLab"} //= $self->volume_label
l!l&&r!l&&!r
0031
MC/DC0%0/2
$attrs{"BS_VolLab"} //= $self->volume_label
$attrs{"BS_VolLab"} $self->volume_label
Truth table33%1/3
A //= B
A= $attrs{"BS_VolLab"}B= $self->volume_label
ABresult
1X1
011
000
676 31 ▶ $attrs{BS_VolID} //= time & 0xFFFFFFFF;
Condition50%1/2
$attrs{"BS_VolID"} //= time & 4294967295
l!l
031
MC/DC0%0/1
$attrs{"BS_VolID"} //= time & 4294967295
$attrs{"BS_VolID"}
Truth table50%1/2
A
A= $attrs{"BS_VolID"}
Aresult
11
00
677 31 ▶ if ($geom->bits < FAT32) {
Branch: 1 branch
BranchTrueFalseTotal
if ($geom->bits < 32) { } 30 1 31
678 30 ▶ $attrs{BPB_FATSz16}= $geom->fat_sector_count < 0x10000? $geom->fat_sector_count : 0;
Branch: 1 branch
BranchTrueFalseTotal
$geom->fat_sector_count < 65536 ? : 30 0 30
679 30 ▶ $attrs{BPB_FATSz32}= $geom->fat_sector_count < 0x10000? 0 : $geom->fat_sector_count;
Branch: 1 branch
BranchTrueFalseTotal
$geom->fat_sector_count < 65536 ? : 30 0 30
680 30 ▶ $attrs{BPB_TotSec16}= $geom->total_sector_count < 0x10000? $geom->total_sector_count : 0;
Branch: 1 branch
BranchTrueFalseTotal
$geom->total_sector_count < 65536 ? : 30 0 30
681 30 ▶ $attrs{BPB_TotSec32}= $geom->total_sector_count < 0x10000? 0 : $geom->total_sector_count;
Branch: 1 branch
BranchTrueFalseTotal
$geom->total_sector_count < 65536 ? : 30 0 30
682 30 ▶ $attrs{BS_FilSysType}= $geom->bits == 12? 'FAT12' : 'FAT16';
Branch: 1 branch
BranchTrueFalseTotal
$geom->bits == 12 ? : 29 1 30
683 30 _append_pack_args(\@pack, \@vals, 0, \@sector0_fat16_fields, \%attrs);
684 } else {
685 # Did the user specify location of fsinfo? If not, default to sector 1
686 1 ▶ $attrs{BPB_FSInfo} //= 1;
Condition50%1/2
$attrs{"BPB_FSInfo"} //= 1
l!l
01
MC/DC0%0/1
$attrs{"BPB_FSInfo"} //= 1
$attrs{"BPB_FSInfo"}
Truth table50%1/2
A
A= $attrs{"BPB_FSInfo"}
Aresult
11
00
687 1 ▶ $attrs{BPB_BkBootSec} //= 2;
Condition50%1/2
$attrs{"BPB_BkBootSec"} //= 2
l!l
01
MC/DC0%0/1
$attrs{"BPB_BkBootSec"} //= 2
$attrs{"BPB_BkBootSec"}
Truth table50%1/2
A
A= $attrs{"BPB_BkBootSec"}
Aresult
11
00
688 1 $attrs{BPB_FATSz16}= 0;
689 1 $attrs{BPB_FATSz32}= $geom->fat_sector_count;
690 1 $attrs{BPB_RootClus}= $self->root->file->cluster;
691 1 $attrs{BPB_TotSec16}= 0;
692 1 $attrs{BPB_TotSec32}= $geom->total_sector_count;
693 1 $attrs{BS_FilSysType}= "FAT";
694 1 _append_pack_args(\@pack, \@vals, 0, \@sector0_fat32_fields, \%attrs);
695
696 # FSInfo struct, location is configurable
697 1 my $fsi_ofs= $attrs{BPB_FSInfo} * $attrs{BPB_BytsPerSec};
698 1 ▶ $attrs{FSI_Free_Count} //= $self->allocation_table->free_cluster_count;
Condition33%1/3
$attrs{"FSI_Free_Count"} //= $self->allocation_table->free_cluster_count
l!l&&r!l&&!r
010
MC/DC0%0/2
$attrs{"FSI_Free_Count"} //= $self->allocation_table->free_cluster_count
$attrs{"FSI_Free_Count"} $self->allocation_table->free_cluster_count
Truth table33%1/3
A //= B
A= $attrs{"FSI_Free_Count"}B= $self->allocation_table->free_cluster_count
ABresult
1X1
011
000
699 1 ▶ $attrs{FSI_Nxt_Free} //= $self->allocation_table->first_free_cluster;
Condition33%1/3
$attrs{"FSI_Nxt_Free"} //= $self->allocation_table->first_free_cluster
l!l&&r!l&&!r
001
MC/DC0%0/2
$attrs{"FSI_Nxt_Free"} //= $self->allocation_table->first_free_cluster
$attrs{"FSI_Nxt_Free"} $self->allocation_table->first_free_cluster
Truth table33%1/3
A //= B
A= $attrs{"FSI_Nxt_Free"}B= $self->allocation_table->first_free_cluster
ABresult
1X1
011
000
700 1 _append_pack_args(\@pack, \@vals, $fsi_ofs, \@fat32_fsinfo_fields, \%attrs);
701  
702 # Backup copy of boot sector
703 1 my $bk_ofs= $attrs{BPB_BkBootSec} * $attrs{BPB_BytsPerSec};
704 1 _append_pack_args(\@pack, \@vals, $bk_ofs, \@sector0_fat32_fields, \%attrs);
705 1 _append_pack_args(\@pack, \@vals, $bk_ofs+$fsi_ofs, \@fat32_fsinfo_fields, \%attrs);
706 }
707 31 pack join(' ', @pack), @vals;
708 }
709  
710 # This packs one allocation table and returns the buffer
711 31 ▶sub _pack_allocation_table($self, $alloc) {
Subroutine _pack_allocation_table: called CC=10 SCAR=23.1
712 31 my $fat= [ $alloc->fat->@* ];
713 31 ▶ croak "Allocation table used more clusters than configured in geometry"
Branch: 1 branch
BranchTrueFalseTotal
if $self->geometry->max_cluster_id < ($alloc->max_cluster_id // $alloc->max_used_cluster_id) 0 31 31
Condition33%1/3
$alloc->max_cluster_id // $alloc->max_used_cluster_id
l!l&&r!l&&!r
3100
MC/DC0%0/2
$alloc->max_cluster_id // $alloc->max_used_cluster_id
$alloc->max_cluster_id $alloc->max_used_cluster_id
Truth table33%1/3
A // B
A= $alloc->max_cluster_idB= $alloc->max_used_cluster_id
ABresult
1X1
011
000
714 if $self->geometry->max_cluster_id < ($alloc->max_cluster_id // $alloc->max_used_cluster_id);
715 31 my $max= $self->geometry->max_cluster_id;
716 31 $#$fat= $max;
717 31 $fat->[$_]= 0x0FFFFFFF for 0,1;
718 31 ▶ $fat->[$_] //= 0 for 2..$max; # prevent warnings in pack
Condition100%2/2
$fat->[$_] //= 0
l!l
737031334
MC/DC100%1/1
$fat->[$_] //= 0
$fat->[$_]
Truth table100%2/2
A
A= $fat->[$_]
Aresult
11
00
719 31 ▶ if ($self->geometry->bits == 32) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->geometry->bits == 32) { } 1 30 31
elsif ($self->geometry->bits == 16) { } 1 29 30
720 1 return pack 'V*', @$fat;
721 } elsif ($self->geometry->bits == 16) {
722 1 return pack 'v*', @$fat;
723 } else {
724 # 12 bits per entry, pack in groups of 3 bytes, little-endian
725 29 my $buf= "\xFF\xFF\xFF";
726 29 ▶ for (my $i= 2; $i+1 <= $max; $i+= 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i + 1 <= $max) 1957 29 1986
727 1957 my $v= ($fat->[$i] & 0xFFF) | ( ($fat->[$i+1] & 0xFFF) << 12 );
728 1957 $buf .= pack 'vC', $v, ($v >> 16);
729 }
730 29 ▶ $buf .= pack 'v', $fat->[$max] & 0xFFF unless $max & 1;
Branch: 1 branch
BranchTrueFalseTotal
unless $max & 1 13 16 29
731 29 return $buf;
732 }
733 }
734  
735 # This calculates the encoded size of one directory
736 65 ▶sub _calc_dir_size($self, $dir) {
Subroutine _calc_dir_size: called CC=8 SCAR=21.8
737 # If an autovivified directoy lacks a ->{file}, create it.
738 65 ▶ $dir->{file} //= Sys::Export::VFAT::File->new(name => $dir->name, flags => ATTR_DIRECTORY);
Condition66%2/3
$dir->{"file"} //= "Sys::Export::VFAT::File"->new("name", $dir->name, "flags", "ATTR_DIRECTORY")
l!l&&r!l&&!r
1640
MC/DC0%0/2
$dir->{"file"} //= "Sys::Export::VFAT::File"->new("name", $dir->name, "flags", "ATTR_DIRECTORY")
$dir->{"file"} "Sys::Export::VFAT::File"->new("name", $dir->name, "flags", "ATTR_DIRECTORY")
Truth table66%2/3
A //= B
A= $dir->{"file"}B= "Sys::Export::VFAT::File"->new("name", $dir->name, "flags", "ATTR_DIRECTORY")
ABresult
1X1
011
000
739 65 my $ents= $dir->entries;
740 # Need the 8.3 name in order to know whether it matches the long name
741 65 $dir->build_shortnames;
742 # root dir has a volume label ent, and all other dirs have '.' and '..'
743 65 ▶ my $n= @$ents + ($dir->is_root? 1 : 2);
Branch: 1 branch
BranchTrueFalseTotal
$dir->is_root ? : 31 34 65
744 65 for (@$ents) {
745 # Add LFN entries
746 10550 ▶ if ($_->{name} ne $_->{shortname}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_->{"name"} ne $_->{"shortname"}) 36 10514 10550
747 36 my $utf16= encode('UTF-16LE', $_->{name}, Encode::FB_CROAK|Encode::LEAVE_SRC);
748 36 $n += ceil(length($utf16) / 26);
749 }
750 }
751 $log->debugf("dir %s has %d real entries, %d LFN entries, size=%d ents=%s",
752 $dir->name, scalar(@$ents), $n-scalar @$ents, $n*32,
753 0 ▶ [ map +( $_->{name} eq $_->{shortname}? $_->{name} : [ @{$_}{'name','shortname'} ] ), @$ents ])
Branch: 2 branches
BranchTrueFalseTotal
if $Sys::Export::VFAT::log->is_debug 0 65 65
$_->{'name'} eq $_->{'shortname'} ? : 0 0 0
754 if $log->is_debug;
755 65 ▶ croak "Directory ".$dir->name." exceeds maximum entry count ($n >= 65536)"
Branch: 1 branch
BranchTrueFalseTotal
if $n >= 65536 0 65 65
756 if $n >= 65536;
757 65 $dir->file->{size}= $n * 32; # always 32 bytes per dirent
758 }
759  
760 # Pack one directory and return the buffer
761 65 ▶sub _pack_directory($self, $dir) {
Subroutine _pack_directory: called CC=32 SCAR=37.1
762 65 my $data= '';
763 65 ▶ my @special= $dir->is_root? (
Branch: 1 branch
BranchTrueFalseTotal
$dir->is_root ? : 31 34 65
Condition50%1/2
$self->volume_label // "NO NAME "
l!l
031
MC/DC0%0/1
$self->volume_label // "NO NAME "
$self->volume_label
Truth table50%1/2
A
A= $self->volume_label
Aresult
11
00
764 { short11 => $self->volume_label // 'NO NAME ', flags => ATTR_VOLUME_ID },
765 ) : (
766 { short11 => ".", flags => ATTR_DIRECTORY, file => $dir->file },
767 { short11 => '..', flags => ATTR_DIRECTORY, file => $dir->parent->file },
768 );
769 65 for my $ent (@special, sort { lc $a->{name} cmp lc $b->{name} } $dir->entries->@*) {
770 10649 my ($name, $file, $shortname, $short11)= @{$ent}{qw( name file shortname short11 )};
771 10649 ▶ $log->tracef("encoding dirent short=%-12s long=%s cluster=%s",
Condition83%5/6
$shortname // $short11
l!l&&r!l&&!r
10550990
$file && $file->cluster
!ll&&!rl&&r
31310615
MC/DC0%0/2
$shortname // $short11
$shortname $short11
MC/DC100%2/2
$file && $file->cluster
$file $file->cluster
Truth table66%2/3
A // B
A= $shortnameB= $short11
ABresult
1X1
011
000
Truth table100%3/3
A && B
A= $fileB= $file->cluster
ABresult
0X0
100
111
772 $shortname//$short11, $name, $file && $file->cluster);
773  
774 10649 ▶ unless (length $short11) {
Branch: 1 branch
BranchTrueFalseTotal
unless (length $short11) 10550 99 10649
775 10550 my ($base, $ext)= split /\./, $shortname;
776 10550 ▶ $short11= pack 'A8 A3', $base, ($ext//'');
Condition100%2/2
$ext // ""
l!l
15748976
MC/DC100%1/1
$ext // ""
$ext
Truth table100%2/2
A
A= $ext
Aresult
11
00
777 }
778 10649 $short11 =~ s/^\xE5/\x05/; # \xE5 may occur in some charsets, and needs escaped
779  
780 # Need Long-File-Name entries?
781 10649 ▶ if (defined $name && $name ne $shortname) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $name and $name ne $shortname) 36 10613 10649
Condition100%3/3
defined $name and $name ne $shortname
!ll&&!rl&&r
991051436
MC/DC100%2/2
defined $name and $name ne $shortname
defined $name $name ne $shortname
Truth table100%3/3
A and B
A= defined $nameB= $name ne $shortname
ABresult
0X0
100
111
782 # Checksum for directory shortname, used to verify long name parts
783 36 my $cksum= 0;
784 $cksum= ((($cksum >> 1) | ($cksum << 7)) + $_) & 0xFF
785 36 for unpack 'C*', $short11;
786 # Each dirent holds up to 26 bytes (13 chars) of the long name
787 36 my @chars= unpack 'v*', encode('UTF-16LE', $name, Encode::FB_CROAK|Encode::LEAVE_SRC);
788 # short final chunk is padded with \0\uFFFF*
789 36 ▶ if (my $remainder= @chars % 13) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $remainder = @chars % 13) 36 0 36
790 36 push @chars, 0;
791 36 push @chars, (0xFFFF)x(12 - $remainder);
792 }
793 36 my $last= ceil(@chars/13) - 1;
794 36 for my $i (reverse 0..$last) {
795 37 my $ofs= $i*13;
796 37 ▶ my $seq= ($i + 1) | (($i == $last) ? 0x40 : 0x00);
Branch: 1 branch
BranchTrueFalseTotal
$i == $last ? : 36 1 37
797 37 $data .= pack('C v5 C C C v6 v v2',
798 $seq, # sequence and end-flag
799 @chars[$ofs .. $ofs+4], # first 5 chars
800 0x0F, 0x00, $cksum, # attr = LFN, type = 0
801 @chars[$ofs+5 .. $ofs+10], # next 6 chars
802 0, # no cluster number
803 @chars[$ofs+11 .. $ofs+12] # last 2 chars
804 );
805 }
806 }
807  
808 10649 ▶ my $mtime= $ent->{mtime} // ($file && $file->mtime) // time;
Condition50%4/8
($ent->{"mtime"} // $file && $file->mtime) // time
l!l
010649
$ent->{"mtime"} // $file && $file->mtime
l!l&&r!l&&!r
0010649
$file and $file->mtime
!ll&&!rl&&r
31106180
MC/DC0%0/3
($ent->{"mtime"} // $file && $file->mtime) // time
$ent->{"mtime"} $file $file->mtime
Truth table50%2/4
A // B and C
A= $ent->{"mtime"}B= $fileC= $file->mtime
ABCresult
1XX1
0111
00X0
0100
809 10649 ▶ my $atime= $ent->{atime} // ($file && $file->atime) // $mtime;
Condition44%4/9
($ent->{"atime"} // $file && $file->atime) // $mtime
l!l&&r!l&&!r
0106490
$ent->{"atime"} // $file && $file->atime
l!l&&r!l&&!r
0010649
$file and $file->atime
!ll&&!rl&&r
31106180
MC/DC0%0/4
($ent->{"atime"} // $file && $file->atime) // $mtime
$ent->{"atime"} $file $file->atime $mtime
Truth table33%2/6
A // B and C // D
A= $ent->{"atime"}B= $fileC= $file->atimeD= $mtime
ABCDresult
1XXX1
011X1
00X11
01011
00X00
01000
810 10649 ▶ my $btime= $ent->{btime} // ($file && $file->btime) // $mtime;
Condition44%4/9
($ent->{"btime"} // $file && $file->btime) // $mtime
l!l&&r!l&&!r
0106490
$ent->{"btime"} // $file && $file->btime
l!l&&r!l&&!r
0010649
$file and $file->btime
!ll&&!rl&&r
31106180
MC/DC0%0/4
($ent->{"btime"} // $file && $file->btime) // $mtime
$ent->{"btime"} $file $file->btime $mtime
Truth table33%2/6
A // B and C // D
A= $ent->{"btime"}B= $fileC= $file->btimeD= $mtime
ABCDresult
1XXX1
011X1
00X11
01011
00X00
01000
811 10649 my ($wdate, $wtime) = _epoch_to_fat_date_time($mtime);
812 10649 my ($cdate, $ctime, $ctime_frac)= _epoch_to_fat_date_time($btime);
813 10649 my ($adate) = _epoch_to_fat_date_time($atime);
814 10649 ▶ my $flags= $ent->{flags} // ($file && $file->flags) // 0;
Condition62%5/8
($ent->{"flags"} // $file && $file->flags) // 0
l!l
106490
$ent->{"flags"} // $file && $file->flags
l!l&&r!l&&!r
99105500
$file and $file->flags
!ll&&!rl&&r
01051634
MC/DC66%2/3
($ent->{"flags"} // $file && $file->flags) // 0
$ent->{"flags"} $file $file->flags
Truth table75%3/4
A // B and C
A= $ent->{"flags"}B= $fileC= $file->flags
ABCresult
1XX1
0111
00X0
0100
815 # References to the root dir are always encoded as cluster zero, even on FAT32
816 # where the root dir actually lives at a nonzero cluster.
817 # Volume label also doesn't need a cluster id. Nor do empty files.
818 10649 my $cluster= 0;
819 10649 ▶ if ($file && $file != $self->root->file) {
Branch: 1 branch
BranchTrueFalseTotal
if ($file and $file != $self->root->file) 10614 35 10649
Condition100%3/3
$file and $file != $self->root->file
!ll&&!rl&&r
31410614
MC/DC100%2/2
$file and $file != $self->root->file
$file $file != $self->root->file
Truth table100%3/3
A and B
A= $fileB= $file != $self->root->file
ABresult
0X0
100
111
820 10614 ▶ $cluster= $file->cluster // croak "File ".$file->name." lacks a defined cluster id";
Condition50%1/2
$file->cluster // croak('File ' . $file->name . ' lacks a defined cluster id')
l!l
106140
MC/DC0%0/1
$file->cluster // croak('File ' . $file->name . ' lacks a defined cluster id')
$file->cluster
Truth table50%1/2
A
A= $file->cluster
Aresult
11
00
821 }
822 # Directories always written as size = 0
823 10649 ▶ my $size= !$file? 0 : $file->is_dir? 0 : $file->size;
Branch: 2 branches
BranchTrueFalseTotal
!$file ? : 31 10618 10649
$file->is_dir ? : 102 10516 10618
824 10649 $log->tracef(" with encoded size=%d cluster=%d", $size, $cluster);
825 10649 $data .= pack('A11 C C C v v v v v v v V',
826 $short11, $flags, 0, #NT_reserved
827 $ctime_frac, $ctime, $cdate, $adate,
828 $cluster >> 16, $wtime, $wdate, $cluster, $size);
829 }
830 65 ▶ die "BUG: calculated dir size ".$dir->file->size." != data length ".length($data)
Branch: 1 branch
BranchTrueFalseTotal
unless $dir->file->size == length $data 0 65 65
831 unless $dir->file->size == length $data;
832 # Dir must be padded to length of sector/cluster with entries whose name begins with \x00
833 # but that will happen automatically later as the data is appended to the file.
834 65 $dir->file->{data}= \$data;
835 }
836  
837 # Avoiding dependency on namespace::clean
838 3 delete @{Sys::Export::VFAT::}{qw(
839 carp croak confess encode decode min max ceil blessed dualvar refaddr weaken S_ISDIR S_ISREG
840 expand_stat_shorthand isa_array isa_handle isa_hash isa_int isa_pow2 write_file_extent
841 )};
842 3 1;
843  
844 __END__
« Export/Unix/WriteFS.pm ↑ Summary Export/ISO9660.pm »