blib/lib/VirtualFS/ISO9660.pm

filter: Statementstmt 82.8% 212 / 256 Branchbran 53.1% 68 / 128 Conditioncond 50.0% 7 / 14 MC/DCmcdc 25.0% 2 / 8 Subroutinesub 84.2% 32 / 38 Podpod 71.4% 5 / 7 totaltotal 72.2% 326 / 451 CC 72 SCAR 18.4
CC
72
Cov
72%
extract_file
20.0
GETC
20.0
__readdir
11.2
CRAP 6.3
↑ Summary
1 #!/usr/bin/perl -l
2 package VirtualFS::ISO9660;
3 1 require 5.005_003; # only tested on 5.8.0.
4  
5 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 ▶use Scalar::Util qw(dualvar);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use Carp qw(carp croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use Fcntl ':mode';
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use Symbol; # need geniosym
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 # for debugging
15 #require Data::Dumper;
16  
17 1 our $VERSION = 0.02;
18  
19 1 our ($SEPARATOR_1, $SEPARATOR_2, $A_CHARACTERS, $D_CHARACTERS);
20 1 ▶{ no strict 'vars';
Subroutine BEGIN: called CC=1 SCAR=0.0
21 1 *SEPARATOR_1 = \ '.';
22 1 *SEPARATOR_2 = \ ';';
23 1 *D_CHARACTERS = \ '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_';
24 1 *A_CHARACTERS = \ q# !"%&'()*+,-./0123456789:;<=>?ABCDEFGHIJKLMNOPQRSTUVWXYZ_#;
25 }
26  
27  
28 # see ECMA-119 for official ISO9660 format (available free of charge)
29 # http://www.ecma-international.org/publications/files/ECMA-ST/Ecma-119.pdf
30  
31 1 ▶use constant { CDROM_SECTOR_SIZE => 2048, VOLUME_DESCRIPTOR_SECTOR => 16 };
Subroutine BEGIN: called CC=1 SCAR=0.0
32  
33 1 ▶sub new {
Subroutine new: called CC=4 SCAR=14.1
Pod documented
34 1 my $class = shift;
35 1 ▶ my $filename = shift or croak "No filename specified for " . __PACKAGE__ . "->new";
Branch: 1 branch
BranchTrueFalseTotal
my $filename = shift() or croak("No filename specified for VirtualFS::ISO9660->new") 1 0 1
36 1 my %options = @_; # rest is in hash format
37 1 ▶ CORE::open (my $fh, '<', $filename) or return; # let *them* handle open failures!
Branch: 1 branch
BranchTrueFalseTotal
CORE::open my $fh, "<", $filename or return 1 0 1
38 1 binmode $fh;
39
40 1 my $buffer;
41 # try not to croak() unless it's the fault of the caller.
42 # that means, among other things, simply return undef (indicating an error)
43 # when the format of the ISO is invalid.
44  
45 # read the boot-record volume descriptor
46 1 ▶ __readsectors($fh, $buffer, VOLUME_DESCRIPTOR_SECTOR) or return;
Branch: 1 branch
BranchTrueFalseTotal
__readsectors($fh, $buffer, 16) or return 1 0 1
47 1 my $voldesc = __extract_voldesc($buffer);
48
49 # read the path table
50 # the path table is, for whatever reason, a brief listing of every directory
51 # on the disc. There are efefctively three copies of this; one has its integers
52 # MSB-first, one has them LSB-first, and the third would be the actual complete
53 # pile of directory entries.
54 __readsectors($fh, $buffer, $voldesc->{lpathlocation},
55 1 int (($voldesc->{pathtablesize} + CDROM_SECTOR_SIZE - 1) / CDROM_SECTOR_SIZE));
56 1 my $pathtree = __build_pathtree(__extract_pathtable($buffer, $voldesc->{pathtablesize}));
57 #print Data::Dumper::Dumper($pathtree);
58 1 bless [$fh, $voldesc, $pathtree], $class;
59 }
60  
61 # open a fake directory handle. $dirh->readdir() will do what you think it would.
62 # opendir(dirh, path);
63 # opendir(dirh, '/foo/bar/baz') opens /foo/bar/baz
64 # opendir(dirh, '/foo/bar/baz/') opens /foo/bar/baz
65 # opendir(dirh, 'foo/bar/baz') opens /foo/bar/baz
66 2 ▶sub opendir {
Subroutine opendir: called CC=4 SCAR=14.0
Pod documented
67 2 my $this = shift;
68 2 my $loc;
69 2 my $treepos = $this->[2];
70 2 my (undef, $path) = @_;
71 2 my @parts = grep {!/^$/} File::Spec->splitdir($path); # ignore blank parts
72 2 ▶ if (@parts) {
Branch: 1 branch
BranchTrueFalseTotal
if (@parts) { } 1 1 2
73 1 for (@parts) {
74 4 ▶ unless ($treepos = $treepos->[1]{+uc}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($treepos = $treepos->[1]{uc $_}) 0 4 4
75 0 $! = "Path part not found: $_";
76 0 return;
77 }
78 }
79 1 $loc = $treepos->[0];
80 } else {
81 # treat the root directory specially
82 1 $loc = $this->[1]{rootdir}{location};
83 }
84  
85 # FIXME: use File::Spec
86 2 $_[0] = VirtualFS::ISO9660::DirHandle->__new($this->[0], $loc, $this, join('/', @parts) );
87 }
88  
89 2 ▶sub open {
Subroutine open: called CC=6 SCAR=19.1
Pod undocumented
90 2 my $this = shift;
91 2 ▶ croak "need 3-argument open" unless @_ == 3;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ == 3 0 2 2
92 2 ▶ croak "2nd arg must be '<'" unless $_[1] eq '<';
Branch: 1 branch
BranchTrueFalseTotal
unless $_[1] eq "<" 0 2 2
93 2 ▶ my @stats = $this->stat($_[2]) or croak "can't stat $_[2]: $!";
Branch: 1 branch
BranchTrueFalseTotal
my(@stats) = $this->stat($_[2]) or croak("can't stat $_[2]: $!") 2 0 2
94 2 ▶ croak "can't open() a directory" if S_ISDIR($stats[2]);
Branch: 1 branch
BranchTrueFalseTotal
if S_ISDIR($stats[2]) 0 2 2
95 2 $_[0] = Symbol::geniosym();
96 2 ▶ tie( *{$_[0]}, 'VirtualFS::ISO9660::FileHandle', $this->[0], $stats[1], $this)
Branch: 1 branch
BranchTrueFalseTotal
tie *{$_[0];}, "VirtualFS::ISO9660::FileHandle", $this->[0], $stats[1], $this and return 1 2 0 2
97 and return 1;
98 }
99  
100 2 ▶sub stat {
Subroutine stat: called CC=8 SCAR=22.1
Pod documented
101 2 my $this = shift;
102 2 my $filename = uc shift; # note the call to uc; ISO9660 names are all UPPERCASE
103 2 my $ref;
104 my $version;
105 # FIXME: use File::Spec
106 2 ▶ $filename = '/'.$filename unless $filename =~ m#^/#;
Branch: 1 branch
BranchTrueFalseTotal
unless $filename =~ m[^/] 2 0 2
107 2 ▶ if ($filename =~ s/;(.*)//) {
Branch: 1 branch
BranchTrueFalseTotal
if ($filename =~ s/;(.*)//) 0 2 2
108 0 $version = $1-1;
109 }
110 2 ▶ unless (exists($this->[4]{$filename})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $this->[4]{$filename}) 2 0 2
111 2 my (undef, $path, undef) = File::Spec->splitpath($filename);
112 2 ▶ $this->opendir(my $dirh, $path) or croak "can't open path $path: $!";
Branch: 1 branch
BranchTrueFalseTotal
$this->opendir(my $dirh, $path) or croak("can't open path ${path}: $!") 2 0 2
113 2 () = $dirh->readdir(); # in list context -- this will read thru the entire dir, populating the cache
114 2 ▶ croak "can't find file $filename" unless exists($this->[4]{$filename});
Branch: 1 branch
BranchTrueFalseTotal
unless exists $this->[4]{$filename} 0 2 2
115 }
116 2 $ref = $this->[4]{$filename};
117 2 ▶ unless (defined($version)) { $version = $#$ref; }
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $version) 2 0 2
118 2 ▶ croak "version $version of $filename doesn't exist" unless defined $ref->[$version];
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ref->[$version] 0 2 2
119 2 $ref = $ref->[$version][1];
120 2 return $this->__stat($ref);
121 }
122  
123  
124 # ============================================================
125 # accessors
126 # ============================================================
127  
128 # $o->identifier()
129 # returns a hash containing the keys 'system', 'volume',
130 # 'volume_set', 'publisher', 'preparer', and 'application',
131 # as well as their corresponding values (of course).
132 # $o->identifier(key)
133 # assuming 'key' matches one of the above keys, returns the
134 # value for that key.
135 # $o->identifier(key1, key2, key3)
136 # assuming that key1,key2,key3 each match one of the above keys,
137 # returns a list containing the values for those keys, in the
138 # same order.
139  
140 1 ▶sub identifier {
Subroutine identifier: called CC=3 SCAR=13.6
Pod documented
141 1 my $this = shift;
142 1 ▶ if (@_ == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 0) { } 1 0 1
143 # return a hashref
144 1 my %h;
145
146 @h{'system', 'volume', 'volume_set', 'publisher', 'preparer', 'application'} =
147 1 @{$this->[1]}{'system_id', 'volume_id', 'volume_set_id', 'publisher_id', 'preparer_id', 'application_id'};
148 1 return %h;
149 } else {
150 0 my @list = @{$this->[1]}{ map "$_\_id", @_ };
151 0 ▶ return wantarray?@list:pop@list;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
152 }
153 }
154  
155  
156 # $o->id_file()
157 # See the 'identifier' method; only, the keys here are:
158 # 'copyright', 'abstract', and 'biblio'.
159  
160 3 ▶sub id_file {
Subroutine id_file: called CC=3 SCAR=11.0
Pod documented
161 3 my $this = shift;
162 3 ▶ if (@_ == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 0) { } 1 2 3
163 # return a hashref
164 1 my %h;
165
166 @h{'copyright', 'abstract', 'biblio'} =
167 1 @{$this->[1]}{'copyright_file', 'abstract_file', 'biblio_file'};
168 1 return %h;
169 } else {
170 2 my @list = @{$this->[1]}{ map "$_\_file", @_ };
171 2 ▶ return wantarray?@list:pop@list;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 2 0 2
172 }
173 }
174  
175 # $o->extract_file()
176 # $o->extract_file('/COPYRIGH', 'to-file');
177 # This is done using CORE::open on the to-file, which means that
178 # in perl 5.8.0 you can do:
179 # $o->extract_file('/COPYRIGH', \$scalar);
180 # and the contents of the file will be extracted into $scalar.
181  
182 0 ▶sub extract_file {
Subroutine extract_file: not called CC=4 SCAR=30.0
Pod undocumented
183 0 my $this = shift;
184 0 ▶ croak 'usage: extract_file(iso-filename, output-filename)' unless @_>=2;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ >= 2 0 0 0
185 0 my $from = shift;
186 0 my $to = shift;
187 0 ▶ $this->open(my $infh, '<', $from) or return; # eh, right now open() will croak anyway.
Branch: 1 branch
BranchTrueFalseTotal
$this->open(my $infh, "<", $from) or return 0 0 0
188 0 CORE::open(my $outfh, '>', $to);
189 0 local $\; # don't let $\ screw with us
190 0 ▶ while(read($infh, my $buf, 4096)) { print $outfh $buf; }
Branch: 1 branch
BranchTrueFalseTotal
if (read $infh, my $buf, 4096) 0 0 0
191 }
192
193 # ============================================================
194 # internal functions
195 # ============================================================
196  
197  
198  
199 # read a sector or sectors from the image
200 # usage: __readsectors(filehandle, buffer, start[, count])
201 # count defaults to 1 if not specified. And don't specify a 0.
202 #
203 # on success, returns 1 (a partial read is considered failure)
204 # on failure, returns undef
205 2 ▶sub __readsectors {
Subroutine __readsectors: called CC=4 SCAR=15.0
206 2 ▶ my $count = $_[3] || 1;
Condition100%2/2
$_[3] || 1
l!l
11
MC/DC100%1/1
$_[3] || 1
$_[3]
Truth table100%2/2
A
A= $_[3]
Aresult
11
00
207 0 ▶ unless (seek($_[0], $_[2] * CDROM_SECTOR_SIZE, 0)) { return }
Branch: 1 branch
BranchTrueFalseTotal
unless (seek $_[0], $_[2] * 2048, 0) 0 2 2
208 2 my $ret = read($_[0], $_[1], $count * CDROM_SECTOR_SIZE);
209 0 ▶ unless ($ret == $count * CDROM_SECTOR_SIZE) { return }
Branch: 1 branch
BranchTrueFalseTotal
unless ($ret == $count * 2048) 0 2 2
210 2 return 1;
211 }
212  
213 # path table record (ECMA-119 section 9.4)
214 # see extract_direntry and extrapolate for basic use
215 5 ▶sub __extract_pathtablerec {
Subroutine __extract_pathtablerec: called CC=4 SCAR=14.1
216 5 my %h;
217 5 ▶ my $sref = ref($_[0])?$_[0]:\$_[0];
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 5 0 5
218 5 my $len = unpack('C', $$sref);
219 5 @h{'LEN-EAR', 'location', 'parent', 'name'} =
220 unpack("x C V v A$len x![v]", $$sref);
221
222 5 ▶ if (ref $_[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[0]) 5 0 5
223 5 my $totallen = 1 + 1 + 4 + 2 + $len + ($len&1);
224 5 ▶ ${$_[1]} -= $totallen if ref $_[1];
Branch: 1 branch
BranchTrueFalseTotal
if ref $_[1] 5 0 5
225 5 substr($$sref, 0, $totallen, '');
226 }
227 5 return \%h;
228 }
229  
230 # extract_pathtable($scalar, $pathtablesize)
231 # extracts all the path table entries from $scalar
232 # also, there'd sure as hell better be $pathtablesize bytes worth of entries
233 # in there...
234 # in scalar context, returns an arrayref
235 1 ▶sub __extract_pathtable {
Subroutine __extract_pathtable: called CC=2 SCAR=6.9
236 1 my @table;
237 1 my $data = shift;
238 1 my $left = shift;
239
240 1 ▶ push @table, __extract_pathtablerec(\$data, \$left)
Branch: 1 branch
BranchTrueFalseTotal
$left > 0 and do { push @table, __extract_pathtablerec(\$data, \$left) } 5 1 6
241 while $left>0;
242  
243 1 return \@table;
244 }
245  
246 # build_pathtree(\@array)
247 # returns a convenient hashref of all the directories.
248 1 ▶sub __build_pathtree {
Subroutine __build_pathtree: called CC=3 SCAR=11.0
249 1 my $h;
250 my @hrefs;
251 1 my $i=0;
252 1 for (@{$_[0]}) {
253 5 ▶ unless (@hrefs) { # special case: the root directory
Branch: 1 branch
BranchTrueFalseTotal
unless (@hrefs) 1 4 5
254 1 $hrefs[0] = $h = [$_->{parent}];
255 1 $i++;
256 1 next;
257 }
258 $hrefs[$_->{parent} - 1][1]{ $_->{name} } =
259 4 $hrefs[$i] = [ $_->{location} ];
260 4 $i++;
261 }
262 1 return $h;
263 }
264  
265 # directory record (ECMA-119 section 9.1)
266 # extract_direntry($scalar)
267 # returns a happy hashref.
268 #
269 # alternatively, you can do:
270 # __extract_direntry(\$scalar)
271 # which, in addition to returning the hashref, eats the directory
272 # entry out of $scalar.
273  
274 8 ▶sub __extract_direntry {
Subroutine __extract_direntry: called CC=3 SCAR=11.8
275 8 my %h;
276 8 ▶ my $sref = ref($_[0])?$_[0]:\$_[0]; # make sure we have a reference to ease unpacking
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 0 8 8
277
278 8 @h{'LEN-DR', 'LEN-EAR', 'location', 'size', 'time', 'flags', 'unitsize',
279 'gapsize', 'volseqnum', 'name'} = unpack(
280 'C C Vx[N] Vx[N] a7 C C C vx[n] C/a', $$sref);
281  
282 # if they gave us a reference, eat the data out of the scalar.
283 0 ▶ if (ref $_[0]) { substr($$sref, 0, $h{'LEN-DR'}, ''); }
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[0]) 0 8 8
284
285 8 return \%h;
286 }
287  
288 # volume descriptor (ECMA-119 section 8)
289 # __extract_voldesc($scalar)
290 1 ▶sub __extract_voldesc {
Subroutine __extract_voldesc: called CC=5 SCAR=20.9
291 1 my %h;
292
293 1 @h{'type', 'stdid', 'version'} =
294 unpack('CA5C', $_[0]);
295
296 # how we grok the rest depends on the type.
297 # 0=Boot record
298 # 1=Primary volume descriptor
299 # 2=Supplementary volume descriptor
300 # 3=Volume partition descriptor
301 # 4-254=RFU
302 # 255=Volume descriptor set terminator
303
304 1 ▶ if ($h{type} == 0) {
Branch: 4 branches
BranchTrueFalseTotal
if ($h{'type'} == 0) { } 0 1 1
elsif ($h{'type'} == 1) { } 1 0 1
elsif ($h{'type'} == 2) { } 0 0 0
elsif ($h{'type'} == 3) { } 0 0 0
305 # section 8.2: boot record
306 0 @h{'sysid','bootid'} = unpack('x7A32A32', $_[0]);
307 } elsif ($h{type} == 1) {
308 # section 8.4: primary volume descriptor
309 1 @h{'system_id', 'volume_id', 'size', 'setsize', 'seqnum', 'blocksize',
310 'pathtablesize', 'lpathlocation', 'optlpathlocation',
311 #'mpathlocation', 'optmpathlocation',
312 'rootdir',
313 'volume_set_id', 'publisher_id', 'preparer_id', 'application_id',
314 'copyright_file', 'abstract_file', 'biblio_file',
315 'create_time', 'modify_time', 'expire_time', 'effective_time',
316 'format_version'} = unpack(q{
317 x7 # skip over the 7 bytes we pulled out at the very beginning
318 x # byte 8 is RFU and should be 0 in the Primary Volume Descriptor
319 # (probably for alignment purposes)
320 A32 # System Identifier
321 A32 # Volume Identifier
322 x8 # RFU, should be 0
323 V # Volume Space Size
324 x[N] # Volume Space Size again, only in Motorola order
325 x32 # another RFU
326 vx[n] # Volume Set Size and its motorola form
327 vx[n] # Volume Sequence Number
328 vx[n] # Logical Block Size
329 Vx[N] # Path Table Size
330 V # Type L path table location
331 V # Type L path table location (Optional)
332 x[N] # Type M path table location
333 x[N] # Type M path table location (Optional)
334 a34 # 'Directory Record for Root Directory' (??? wtf?)
335 A128 # Volume Set Identifier
336 A128 # Publisher Identifier
337 A128 # Data Preparer Identifier
338 A128 # Application Identifier
339 A37 # Copyright File Identifier
340 A37 # Abstract File Identifier
341 A37 # Bibliographic File Identifier
342 a17 # Volume Creation Timestamp
343 a17 # Volume Modification Timestamp
344 a17 # Volume Expiration Timestamp
345 a17 # Volume Effective Timestamp
346 C # File Structure Version
347 x # RFU
348 }, $_[0]);
349
350 1 $h{rootdir} = __extract_direntry($h{rootdir});
351 } elsif ($h{type} == 2) {
352 # section 8.5, Supplementary Volume Descriptor
353 # gahhhh...
354 } elsif ($h{type} == 3) {
355 # section 8.6, Volume Partition Descriptor
356 0 @h{'sysid', 'partition_id', 'partition_location', 'partition_size'} =
357 unpack('x7xA32A32Vx[N]Vx[N]', $_[0]);
358 }
359 1 return \%h;
360 }
361  
362 # $obj->__startpos('/path/to/filename')
363 # returns the offset into the .ISO file where you can find the contents of that
364 # file (for debugging purposes).
365 0 ▶sub __startpos {
Subroutine __startpos: not called CC=2 SCAR=17.9
366 0 my $this = shift;
367 0 my @x = $this->stat($_[0]);
368 0 ▶ return undef unless @x; # no data? give up.
Branch: 1 branch
BranchTrueFalseTotal
unless @x 0 0 0
369 # $x[1] will point to the info object
370 0 return ($x[1]{location} * CDROM_SECTOR_SIZE);
371 }
372  
373 2 ▶sub __stat {
Subroutine __stat: called CC=2 SCAR=7.1
374 2 my $this = shift;
375 2 my $ref = shift;
376
377 2 my $perms = S_IRUSR|S_IRGRP|S_IROTH; # everybody can read
378 # nobody can write (ISO9660 is readonly)
379 # nobody can execute (how's it gonna be executed?)
380
381 2 ▶ if ($ref->{flags} & 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ref->{'flags'} & 2) { } 0 2 2
382 0 $perms |= S_IFDIR;
383 } else {
384 2 $perms |= S_IFREG;
385 }
386
387 return (
388 $this, # "device number", return this object
389 $ref, # "inode number", return the cache ref
390 $perms, # permissions
391 1, # number of hard links
392 0, # uid
393 0, # gid
394 0, # rdev(???)
395 $ref->{size}, # size
396 0, # atime
397 0, # mtime
398 0, # ctime
399 CDROM_SECTOR_SIZE, # blksize
400 2 int(($ref->{size} + CDROM_SECTOR_SIZE - 1) / CDROM_SECTOR_SIZE), # block count
401 );
402 }
403  
404 package VirtualFS::ISO9660::DirHandle;
405  
406 1 ▶use Scalar::Util qw(dualvar);
Subroutine BEGIN: called CC=1 SCAR=0.0
407 1 ▶use constant { CDROM_SECTOR_SIZE => 2048 };
Subroutine BEGIN: called CC=1 SCAR=0.0
408  
409 1 *__extract_direntry = \&VirtualFS::ISO9660::__extract_direntry;
410  
411 # new (iso_filehandle, sector, ISO9660 object, pathname)
412 # pathname won't start with '/', nor will it end with one.
413 2 ▶sub __new {
Subroutine __new: called CC=3 SCAR=11.0
414 2 my $class = shift;
415 2 my ($fromfh, $sector, $parent, $name) = @_;
416
417 2 ▶ CORE::open(my $fh, '<&', $fromfh) or return;
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<&", $fromfh or return 2 0 2
418 2 seek($fh, $sector * CDROM_SECTOR_SIZE, 0);
419 # / (root) and /any/dir/here are different in that the former
420 # ends in /, while the latter does not. This causes confusion.
421  
422 # FIXME: use File::Spec
423 2 ▶ $name = '/'.$name if $name ne '';
Branch: 1 branch
BranchTrueFalseTotal
if $name ne "" 1 1 2
424 2 bless [$fh, $sector, 0, $parent, $name, undef], $class;
425 }
426  
427 0 ▶sub rewinddir {
Subroutine rewinddir: not called CC=1 SCAR=6.9
428 0 my $this = shift;
429 0 $this->[2] = 0;
430 0 seek($this->[0], $this->[1] * CDROM_SECTOR_SIZE, 0);
431 }
432  
433 # merely for completeness
434 0 ▶sub closedir {}
Subroutine closedir: not called CC=1 SCAR=0.0
435  
436 2 ▶sub readdir {
Subroutine readdir: called CC=3 SCAR=11.2
437 2 ▶ if (wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) { } 2 0 2
438 2 my $this = shift;
439 2 my @x;
440 my $x;
441 2 ▶ push @x, $x while $x=$this->__readdir;
Branch: 1 branch
BranchTrueFalseTotal
$x = $this->__readdir and do { push @x, $x } 7 2 9
442 2 return @x;
443 } else {
444 0 goto &__readdir;
445 }
446 }
447  
448 9 ▶sub __readdir {
Subroutine __readdir: called CC=11 SCAR=24.2
449 # $this->
450 # [0] = filehandle of ISO image
451 # [1] = sector to start at
452 # [2] = byte offset within directory
453 # [3] = VirtualFS::ISO9660 object that spawned us (used for caching)
454 # [4] = path of directory, parts separated by '/' and ending with '/'
455 # [5] = total size of directory, undef if we don't know it yet.
456
457 9 my ($buf, $len);
458
459 # check EOF (err, EOD)
460 9 ▶ return if (defined($_[0][5]) && $_[0][5] <= $_[0][2]);
Branch: 1 branch
BranchTrueFalseTotal
if defined $_[0][5] and $_[0][5] <= $_[0][2] 0 9 9
Condition66%2/3
defined $_[0][5] and $_[0][5] <= $_[0][2]
!ll&&!rl&&r
270
MC/DC0%0/2
defined $_[0][5] and $_[0][5] <= $_[0][2]
defined $_[0][5] $_[0][5] <= $_[0][2]
Truth table66%2/3
A and B
A= defined $_[0][5]B= $_[0][5] <= $_[0][2]
ABresult
0X0
100
111
461
462 9 ▶ read($_[0][0], $len, 1)==1 or return; # find out the size of the entry
Branch: 1 branch
BranchTrueFalseTotal
read($_[0][0], $len, 1) == 1 or return 9 0 9
463 9 $len = unpack('C',$len);
464 9 ▶ return unless $len; # I can't find what officially marks the end,
Branch: 1 branch
BranchTrueFalseTotal
unless $len 2 7 9
465 # but this seems to work
466  
467 7 ▶ seek($_[0][0], -1, 1) or return;
Branch: 1 branch
BranchTrueFalseTotal
seek $_[0][0], -1, 1 or return 7 0 7
468 7 my $where = tell($_[0][0]);
469 7 ▶ read($_[0][0], $buf, $len)==$len or return;
Branch: 1 branch
BranchTrueFalseTotal
read($_[0][0], $buf, $len) == $len or return 7 0 7
470 7 $_[0][2] += $len;
471 7 my $info = __extract_direntry($buf);
472 # cache the location of this file for future reference
473
474 # if there's a version (;<number>), extract it.
475 7 ▶ if ($info->{name} =~ s/;(.*)//) {
Branch: 1 branch
BranchTrueFalseTotal
if ($info->{'name'} =~ s/;(.*)//) { } 2 5 7
476 2 $info->{version} = $1-1;
477 } else {
478 5 $info->{version} = dualvar(0, ''); # this is equivalent to, but distinguishable from, an explicit version of 1.
479 }
480 7 $info->{name} =~ s/\.$//; # remove any trailing .'s
481
482 # if $this->[5] is undef, then this is the very first entry in the directory.
483 7 ▶ if ($info->{name} eq "\c@") {
Branch: 2 branches
BranchTrueFalseTotal
if ($info->{'name'} eq "\000") { } 2 5 7
elsif ($info->{'name'} eq "\cA") { } 2 3 5
484 2 $_[0][5] = $info->{size};
485 2 $info->{name} = '.';
486 2 ▶ if ($_[0][4] eq '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($_[0][4] eq "") 1 1 2
487 # special case to cache the root directory
488 1 $_[0][3][4]{'/'}[$info->{version}] = [$where, $info];
489 }
490 } elsif ($info->{name} eq "\cA") {
491 2 $info->{name} = '..';
492 } else {
493 # not a special name; cache this entry.
494 # FIXME: use File::Spec
495 3 $_[0][3][4]{$_[0][4] . '/' . $info->{name}}[$info->{version}] = [$where, $info];
496 }
497 7 return $info->{name};
498 }
499  
500 1 1;
501  
502 package VirtualFS::ISO9660::FileHandle;
503  
504 1 ▶use constant { CDROM_SECTOR_SIZE => 2048 };
Subroutine BEGIN: called CC=1 SCAR=0.0
505  
506 # TIEHANDLE (iso_filehandle, info, ISO9660 object)
507  
508 2 ▶sub TIEHANDLE {
Subroutine TIEHANDLE: called CC=3 SCAR=11.3
509 2 my $class = shift;
510 2 my ($fromfh, $info, $parent) = @_;
511 2 ▶ open(my $fh, '<&', $fromfh) or return;
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<&", $fromfh or return 2 0 2
512 2 ▶ seek($fh, $info->{location} * CDROM_SECTOR_SIZE, 0) or return;
Branch: 1 branch
BranchTrueFalseTotal
seek $fh, $info->{"location"} * 2048, 0 or return 2 0 2
513
514 bless [$fh, $info, $parent,
515 $info->{location} * CDROM_SECTOR_SIZE, # byte 0 is here
516 $info->{location} * CDROM_SECTOR_SIZE + $info->{size} # EOF is here
517 2 ], $class;
518 }
519  
520 # no need to support WRITE -- the ISO format is read-only except when it's being
521 # built from scratch.
522 # Same goes for PRINT and PRINTF.
523  
524 # We need: READ, READLINE, and GETC.
525  
526 0 ▶sub GETC {
Subroutine GETC: not called CC=4 SCAR=30.0
527 0 my $this = shift;
528 0 my $ret;
529 0 my $where = tell($this->[0]);
530 # if we're "outside" the file, fail
531 0 ▶ return undef unless $where >= $this->[3] && $where < $this->[4];
Branch: 1 branch
BranchTrueFalseTotal
unless $where >= $this->[3] and $where < $this->[4] 0 0 0
Condition0%0/3
$where >= $this->[3] and $where < $this->[4]
!ll&&!rl&&r
000
MC/DC0%0/2
$where >= $this->[3] and $where < $this->[4]
$where >= $this->[3] $where < $this->[4]
Truth table0%0/3
A and B
A= $where >= $this->[3]B= $where < $this->[4]
ABresult
0X0
100
111
532 0 ▶ read($this->[0], $ret, 1) == 1 or return;
Branch: 1 branch
BranchTrueFalseTotal
read($this->[0], $ret, 1) == 1 or return 0 0 0
533 0 return $ret;
534 }
535  
536 4 ▶sub READ {
Subroutine READ: called CC=4 SCAR=13.9
537 4 my $this = shift;
538 # READ(buffer, len, offset)
539 4 my (undef,$len,$ofs) = @_;
540 4 ▶ $ofs = 0 unless defined($ofs);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ofs 2 2 4
541 4 my $b = \$_[0];
542 # don't read past the end of our virtual file!
543 4 ▶ if ($len > $this->[4] - tell($this->[0])) { $len = $this->[4] - tell($this->[0]); }
Branch: 1 branch
BranchTrueFalseTotal
if ($len > $this->[4] - tell($this->[0])) 4 0 4
544 # if $len ends up being 0 bytes, bail
545 4 ▶ return 0 unless $len>0;
Branch: 1 branch
BranchTrueFalseTotal
unless $len > 0 2 2 4
546 2 return read($this->[0], $$b, $len, $ofs);
547 }
548  
549 # My wish: That Perl_do_readline (pp_hot.c) was nice enough to provide readline()
550 # on tied filehandles by falling back to $obj->READ. This would do two things:
551 # -> Simplify this object
552 # -> As it is presently implemented, future extensions to how <$fh> handles
553 # $RS or $/ won't work here, as we are effectively reimplementing
554 # Perl_do_readline() here. If Perl_do_readline() worked by calling our
555 # READ method, however, it would work fine.
556  
557 2 ▶sub __READLINE {
Subroutine __READLINE: called CC=6 SCAR=20.7
558 2 my $buf;
559 2 my $len = 0;
560 2 my $rlen;
561
562 # read 4K of data at a time until we get something or run out of file.
563 2 $rlen = $len = READ($_[0], $buf, 4096);
564 2 ▶ until ($rlen==0 || (defined($/) && $buf =~ m[\Q$/]g)) { # the g makes perl set pos()
Branch: 1 branch
BranchTrueFalseTotal
unless ($rlen == 0 or defined $/ and $buf =~ m[\Q$/\E]g) 2 2 4
Condition50%3/6
$rlen == 0 or defined $/ and $buf =~ m[\Q$/\E]g
l!l&&r!l&&!r
202
defined $/ and $buf =~ m[\Q$/\E]g
!ll&&!rl&&r
200
MC/DC33%1/3
$rlen == 0 or defined $/ and $buf =~ m[\Q$/\E]g
$rlen == 0 defined $/ $buf =~ m[\Q$/\E]g
Truth table50%2/4
A or B and C
A= $rlen == 0B= defined $/C= $buf =~ m[\Q$/\E]g
ABCresult
1XX1
0111
00X0
0100
565 2 $len += ($rlen = READ($_[0], $buf, 4096, $len));
566 }
567 2 ▶ return undef if ($len == 0); # no more file!
Branch: 1 branch
BranchTrueFalseTotal
if $len == 0 0 2 2
568 2 ▶ return $buf if ($rlen == 0); # we ate the rest of the file!
Branch: 1 branch
BranchTrueFalseTotal
if $rlen == 0 2 0 2
569 0 $rlen = pos($buf);
570 0 substr($buf, $rlen, $len-$rlen, ''); # eat the rest of the buffer
571 0 seek($_[0][0], $rlen-$len, 1); # and fix the file position
572 0 return $buf;
573 }
574  
575  
576 2 ▶sub READLINE {
Subroutine READLINE: called CC=3 SCAR=17.3
577 2 ▶ if (wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) 0 2 2
578 0 my @lines;
579 my $line;
580 0 ▶ push @lines, $line while defined($line = $_[0]->__READLINE);
Branch: 1 branch
BranchTrueFalseTotal
defined($line = $_[0]->__READLINE) and do { push @lines, $line } 0 0 0
581 0 return @lines;
582 }
583 2 goto &__READLINE;
584 }
585  
586 0 ▶sub STAT {
Subroutine STAT: not called CC=1 SCAR=6.9
587 0 my $this = shift;
588 0 return $this->[2]->__stat($this->[1]);
589 }
↑ Summary