Export.pm

filter: Statementstmt 80.7% 147 / 182 Branchbran 54.9% 67 / 122 Conditioncond 44.6% 21 / 47 MC/DCmcdc 21.4% 6 / 28 Subroutinesub 79.5% 39 / 49 Podpod 92.8% 26 / 28 totaltotal 67.1% 306 / 456 CC 78 SCAR 27.1
CC
78
Cov
67%
write_file_extent
52.5
BEGIN
22.1
expand_stat_shorthand
15.3
CRAP 15.0
« Export/ISO9660Hybrid.pm ↑ Summary Export/VFAT/AllocationTable.pm »
1 package Sys::Export;
2  
3 19 our $VERSION = '0.006'; # VERSION
4 # ABSTRACT: Export a subset of an OS file tree, for chroot/initrd
5  
6 19 ▶use v5.26;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 19 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 19 ▶use experimental qw( signatures );
Subroutine BEGIN: called CC=1 SCAR=0.0
9 19 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 19 ▶use Scalar::Util qw( blessed looks_like_number );
Subroutine BEGIN: called CC=1 SCAR=0.0
11 19 ▶use Sys::Export::LogAny '$log';
Subroutine BEGIN: called CC=1 SCAR=0.0
12 19 ▶use Exporter ();
Subroutine BEGIN: called CC=1 SCAR=0.0
13 19 ▶use Fcntl ':mode';
Subroutine BEGIN: called CC=1 SCAR=0.0
14 19 ▶BEGIN {
Subroutine BEGIN: called CC=11 SCAR=31.0
15 # Fcntl happily exports macros that don't exist, then fails at runtime.
16 # Replace non-existent test macros with 'false', and nonexistent modes with 0.
17 # But, on MSWin32, the constant for S_IFLNK is 0x4000 and just isn't defined in Fcntl
18 # because the headers don't define it.
19 19 for (qw( S_ISREG S_ISDIR S_ISLNK S_ISBLK S_ISCHR S_ISFIFO S_ISSOCK S_ISWHT
20 S_IFREG S_IFDIR S_IFLNK S_IFBLK S_IFCHR S_IFIFO S_IFSOCK S_IFWHT S_IFMT )
21 ) {
22 323 ▶ next if eval { __PACKAGE__->can($_)->($_ =~ /_IS/? (0) : ()); 1 };
Branch: 2 branches
BranchTrueFalseTotal
if eval { do { __PACKAGE__->can($_)->($_ =~ /_IS/u ? 0 : ()); 1 } } 285 38 323
$_ =~ /_IS/u ? : 152 171 323
23 38 delete ${Sys::Export::}{$_};
24 38 ▶ if ($^O eq 'MSWin32' && $_ eq 'S_IFLNK') {
Branch: 2 branches
BranchTrueFalseTotal
if ($^O eq 'MSWin32' and $_ eq 'S_IFLNK') { } 0 38 38
elsif ($^O eq 'MSWin32' and $_ eq 'S_ISLNK') { } 0 38 38
Condition33%1/3
$^O eq "MSWin32" and $_ eq "S_IFLNK"
!ll&&!rl&&r
3800
MC/DC0%0/2
$^O eq "MSWin32" and $_ eq "S_IFLNK"
$^O eq "MSWin32" $_ eq "S_IFLNK"
Truth table33%1/3
A and B
A= $^O eq "MSWin32"B= $_ eq "S_IFLNK"
ABresult
0X0
100
111
25 0 ▶ eval 'sub Sys::Export::S_IFLNK { 0x6000 } 1' or die "S_IFLNK $@";
Branch: 1 branch
BranchTrueFalseTotal
eval "sub Sys::Export::S_IFLNK { 0x6000 } 1" or die "S_IFLNK $@" 0 0 0
26 ▶ } elsif ($^O eq 'MSWin32' && $_ eq 'S_ISLNK') {
Condition33%1/3
$^O eq "MSWin32" and $_ eq "S_ISLNK"
!ll&&!rl&&r
3800
MC/DC0%0/2
$^O eq "MSWin32" and $_ eq "S_ISLNK"
$^O eq "MSWin32" $_ eq "S_ISLNK"
Truth table33%1/3
A and B
A= $^O eq "MSWin32"B= $_ eq "S_ISLNK"
ABresult
0X0
100
111
27 0 ▶ eval 'sub Sys::Export::S_ISLNK { S_IFMT($_[0]) == 0x6000 } 1' or die "S_ISLNK $@";
Branch: 1 branch
BranchTrueFalseTotal
eval "sub Sys::Export::S_ISLNK { S_IFMT(\$_[0]) == 0x6000 } 1" or die "S_ISLNK $@" 0 0 0
28 } else {
29 0 ▶ eval "sub Sys::Export::$_ { 0 } 1" or die "$@";
Subroutine S_IFWHT: called CC=1 SCAR=0.6
Subroutine S_ISWHT: not called CC=1 SCAR=0.6
Pod undocumented
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
eval "sub Sys::Export::$_ { 0 } 1" or die "$@" 38 0 38
30 }
31 }
32 }
33 19 our @EXPORT_OK= qw(
34 isa_exporter isa_export_dst isa_userdb isa_user isa_group exporter isa_hash isa_array isa_int
35 isa_handle isa_pow2 isa_data_ref round_up_to_pow2 round_up_to_multiple map_or_load_file filedata
36 add skip find which finish rewrite_path rewrite_user rewrite_group expand_stat_shorthand
37 write_file_extent _pack _unpack
38 S_ISREG S_ISDIR S_ISLNK S_ISBLK S_ISCHR S_ISFIFO S_ISSOCK S_ISWHT
39 S_IFREG S_IFDIR S_IFLNK S_IFBLK S_IFCHR S_IFIFO S_IFSOCK S_IFWHT S_IFMT
40 );
41 19 our %EXPORT_TAGS= (
42 basic_methods => [qw( exporter add skip find which finish rewrite_path rewrite_user rewrite_group filedata )],
43 isa => [qw( isa_exporter isa_export_dst isa_userdb isa_user isa_group isa_hash isa_array isa_handle isa_int isa_pow2 isa_data_ref )],
44 stat_modes => [qw( S_IFREG S_IFDIR S_IFLNK S_IFBLK S_IFCHR S_IFIFO S_IFSOCK S_IFWHT S_IFMT )],
45 stat_tests => [qw( S_ISREG S_ISDIR S_ISLNK S_ISBLK S_ISCHR S_ISFIFO S_ISSOCK S_ISWHT )],
46 );
47 19 my ($is_module_name, $require_module);
48  
49 # optional dependency on Module::Runtime. This way if there's any bug in my cheap
50 # substitute, the fix is to just install the official module.
51 19 ▶if (eval { require Module::Runtime; }) {
Branch: 1 branch
BranchTrueFalseTotal
if (eval { do { require Module::Runtime } }) { } 19 0 19
52 19 $is_module_name= \&Module::Runtime::is_module_name;
53 19 $require_module= \&Module::Runtime::require_module;
54 } else {
55 0 ▶ $is_module_name= sub { $_[0] =~ /^[A-Z_a-z][0-9A-Z_a-z]*(?:::[0-9A-Z_a-z]+)*\z/ };
Subroutine __ANON__: not called CC=1 SCAR=6.9
56 0 ▶ $require_module= sub { require( ($_[0] =~ s{::}{/}gr).'.pm' ) };
Subroutine __ANON__: not called CC=1 SCAR=6.9
57 }
58  
59  
60 47 ▶sub import {
Subroutine import: called CC=6 SCAR=18.3
61 47 my $class= $_[0];
62 47 my $caller= caller;
63 47 my %ctor_opts;
64 47 ▶ for (my $i= 1; $i < $#_; ++$i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $#_) 125 47 172
65 125 ▶ if (ref $_[$i] eq 'HASH') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $_[$i] eq 'HASH') { } 0 125 125
elsif ($_[$i] =~ /^-(type|src|dst|tmp|src_userdb|dst_userdb|rewrite_path|rewrite_user|rewrite_group)\z/u) { } 3 122 125
66 0 %ctor_opts= ( %ctor_opts, %{ splice(@_, $i--, 1) } );
67 }
68 elsif ($_[$i] =~ /^-(type|src|dst|tmp|src_userdb|dst_userdb|rewrite_path|rewrite_user|rewrite_group)\z/) {
69 3 $ctor_opts{$1}= (splice @_, $i--, 2)[1];
70 }
71 }
72 47 ▶ if (keys %ctor_opts) {
Branch: 1 branch
BranchTrueFalseTotal
if (keys %ctor_opts) 1 46 47
73 1 init_global_exporter(%ctor_opts);
74 # caller requested the global exporter instance, so also include the standard methods
75 # unless it looks like they were more selective about what to import.
76 1 ▶ push @_, 'exporter', ':basic_methods'
Branch: 1 branch
BranchTrueFalseTotal
unless grep /^(add|:.*methods)\z/u, @_ 1 0 1
77 unless grep /^(add|:.*methods)\z/, @_;
78 }
79 47 goto \&Exporter::import;
80 }
81  
82 19 our $exporter;
83 0 ▶sub exporter { $exporter }
Subroutine exporter: not called CC=1 SCAR=6.9
Pod documented
84  
85 19 our %osname_to_class= (
86 linux => 'Linux',
87 );
88  
89 1 ▶sub init_global_exporter(%config) {
Subroutine init_global_exporter: called CC=6 SCAR=20.5
Pod documented
90 1 ▶ my $type= delete $config{type} // $^O;
Condition33%1/3
delete $config{"type"} // $^O
l!l&&r!l&&!r
100
MC/DC0%0/2
delete $config{"type"} // $^O
delete $config{"type"} $^O
Truth table33%1/3
A // B
A= delete $config{"type"}B= $^O
ABresult
1X1
011
000
91 # remap known OS names
92 1 ▶ my $class= $osname_to_class{$type} // $type;
Condition33%1/3
$Sys::Export::osname_to_class{$type} // $type
l!l&&r!l&&!r
010
MC/DC0%0/2
$Sys::Export::osname_to_class{$type} // $type
$Sys::Export::osname_to_class{$type} $type
Truth table33%1/3
A // B
A= $Sys::Export::osname_to_class{$type}B= $type
ABresult
1X1
011
000
93 # prefix bare names with namespace
94 1 ▶ $class= "Sys::Export::$class" unless $class =~ /::/;
Branch: 1 branch
BranchTrueFalseTotal
unless $class =~ /::/u 1 0 1
95 1 ▶ $is_module_name->($class) or croak "Invalid module name '$class'";
Branch: 1 branch
BranchTrueFalseTotal
&$is_module_name($class) or croak("Invalid module name '${class}'") 1 0 1
96 # if it fails, die with 'croak'
97 1 ▶ eval { $require_module->($class) } or croak "$@";
Branch: 1 branch
BranchTrueFalseTotal
eval { do { &$require_module($class) } } or croak("$@") 1 0 1
98 # now construct one
99 1 $exporter= $class->new(%config);
100 }
101  
102  
103 3 ▶sub add { $exporter->add(@_) }
Subroutine add: called CC=1 SCAR=0.0
Pod documented
104 1 ▶sub skip { $exporter->skip(@_) }
Subroutine skip: called CC=1 SCAR=0.0
Pod documented
105 1 ▶sub find { $exporter->src_find(@_) }
Subroutine find: called CC=1 SCAR=0.0
Pod documented
106 0 ▶sub which :prototype($) { $exporter->src_which(@_) }
Subroutine which: not called CC=1 SCAR=6.9
Pod documented
107 1 ▶sub finish { $exporter->finish(@_) }
Subroutine finish: called CC=1 SCAR=0.0
Pod documented
108 0 ▶sub rewrite_path { $exporter->rewrite_path(@_) }
Subroutine rewrite_path: not called CC=1 SCAR=6.9
Pod documented
109 0 ▶sub rewrite_user { $exporter->rewrite_user(@_) }
Subroutine rewrite_user: not called CC=1 SCAR=6.9
Pod documented
110 0 ▶sub rewrite_group { $exporter->rewrite_group(@_) }
Subroutine rewrite_group: not called CC=1 SCAR=6.9
Pod documented
111  
112  
113 10645 ▶sub isa_hash :prototype($) { ref $_[0] eq 'HASH' }
Subroutine isa_hash: called CC=1 SCAR=0.0
Pod documented
114 21106 ▶sub isa_array :prototype($) { ref $_[0] eq 'ARRAY' }
Subroutine isa_array: called CC=1 SCAR=0.0
Pod documented
115 35 ▶sub isa_handle :prototype($) { ref $_[0] eq 'GLOB' || (ref $_[0] && (ref $_[0])->isa('IO::Handle')) }
Subroutine isa_handle: called CC=3 SCAR=15.1
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq "GLOB" or ref $_[0] and ref($_[0])->isa("IO::Handle") 0 35 35
Condition33%2/6
ref $_[0] eq 'GLOB' || ref $_[0] && ref($_[0])->isa('IO::Handle')
l!l&&r!l&&!r
0350
ref $_[0] and ref($_[0])->isa("IO::Handle")
!ll&&!rl&&r
0035
MC/DC0%0/3
ref $_[0] eq 'GLOB' || ref $_[0] && ref($_[0])->isa('IO::Handle')
ref $_[0] eq 'GLOB' ref $_[0] ref($_[0])->isa("IO::Handle")
Truth table25%1/4
A || B and C
A= ref $_[0] eq 'GLOB'B= ref $_[0]C= ref($_[0])->isa("IO::Handle")
ABCresult
1XX1
0111
00X0
0100
116 31861 ▶sub isa_int :prototype($) { looks_like_number($_[0]) && int($_[0]) == $_[0] }
Subroutine isa_int: called CC=2 SCAR=6.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
&looks_like_number($_[0]) and int $_[0] == $_[0] 21318 10543 31861
117 0 ▶sub isa_exporter :prototype($) { blessed($_[0]) && $_[0]->isa('Sys::Export::Exporter') }
Subroutine isa_exporter: not called CC=2 SCAR=17.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($_[0]) and $_[0]->isa("Sys::Export::Exporter") 0 0 0
118 14 ▶sub isa_export_dst :prototype($) { blessed($_[0]) && $_[0]->can('add') && $_[0]->can('finish') }
Subroutine isa_export_dst: called CC=3 SCAR=11.1
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($_[0]) and $_[0]->can("add") and $_[0]->can("finish") 7 7 14
Condition66%2/3
builtin::blessed($_[0]) and $_[0]->can("add")
!ll&&!rl&&r
077
MC/DC50%1/2
builtin::blessed($_[0]) and $_[0]->can("add")
builtin::blessed($_[0]) $_[0]->can("add")
Truth table66%2/3
A and B
A= builtin::blessed($_[0])B= $_[0]->can("add")
ABresult
0X0
100
111
119 11 ▶sub isa_userdb :prototype($) { blessed($_[0]) && $_[0]->can('user') && $_[0]->can('group') }
Subroutine isa_userdb: called CC=3 SCAR=11.1
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($_[0]) and $_[0]->can("user") and $_[0]->can("group") 6 5 11
Condition66%2/3
builtin::blessed($_[0]) and $_[0]->can("user")
!ll&&!rl&&r
506
MC/DC50%1/2
builtin::blessed($_[0]) and $_[0]->can("user")
builtin::blessed($_[0]) $_[0]->can("user")
Truth table66%2/3
A and B
A= builtin::blessed($_[0])B= $_[0]->can("user")
ABresult
0X0
100
111
120 18 ▶sub isa_user :prototype($) { blessed($_[0]) && $_[0]->isa('Sys::Export::Unix::UserDB::User') }
Subroutine isa_user: called CC=2 SCAR=6.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($_[0]) and $_[0]->isa("Sys::Export::Unix::UserDB::User") 3 15 18
121 18 ▶sub isa_group :prototype($) { blessed($_[0]) && $_[0]->isa('Sys::Export::Unix::UserDB::Group') }
Subroutine isa_group: called CC=2 SCAR=6.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($_[0]) and $_[0]->isa("Sys::Export::Unix::UserDB::Group") 4 14 18
122 1330 ▶sub isa_pow2 :prototype($) { !($_[0] & ($_[0]-1)) }
Subroutine isa_pow2: called CC=1 SCAR=0.0
Pod documented
123 24 ▶sub isa_data_ref :prototype($) { ref $_[0] eq 'SCALAR' || blessed($_[0]) && $_[0]->can('as_scalarref') }
Subroutine isa_data_ref: called CC=3 SCAR=17.3
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq "SCALAR" or builtin::blessed($_[0]) and $_[0]->can("as_scalarref") 24 0 24
Condition16%1/6
ref $_[0] eq 'SCALAR' || builtin::blessed($_[0]) && $_[0]->can('as_scalarref')
l!l&&r!l&&!r
2400
builtin::blessed($_[0]) and $_[0]->can("as_scalarref")
!ll&&!rl&&r
000
MC/DC0%0/3
ref $_[0] eq 'SCALAR' || builtin::blessed($_[0]) && $_[0]->can('as_scalarref')
ref $_[0] eq 'SCALAR' builtin::blessed($_[0]) $_[0]->can("as_scalarref")
Truth table25%1/4
A || B and C
A= ref $_[0] eq 'SCALAR'B= builtin::blessed($_[0])C= $_[0]->can("as_scalarref")
ABCresult
1XX1
0111
00X0
0100
124  
125  
126 4 ▶sub _parse_major_minor_data($attrs, $data) {
Subroutine _parse_major_minor_data: called CC=2 SCAR=6.9
127 4 ▶ @{$attrs}{'rdev_major','rdev_minor'}= isa_array $data? @$data : split(/[,:]/, $data);
Branch: 1 branch
BranchTrueFalseTotal
&isa_array($data) ? : 2 2 4
128 }
129 our %_mode_alias= (
130 10523 ▶ file => [ S_IFREG, sub { 0666 & ~umask } ],
Subroutine __ANON__: called CC=1 SCAR=0.0
131 2 ▶ dir => [ S_IFDIR, sub { 0777 & ~umask } ],
Subroutine __ANON__: called CC=1 SCAR=0.0
132 1 ▶ sym => [ S_IFLNK, sub { 0777 } ],
Subroutine __ANON__: called CC=1 SCAR=0.0
133 1 ▶ blk => [ S_IFBLK, sub { 0666 & ~umask }, \&_parse_major_minor_data, ],
Subroutine __ANON__: called CC=1 SCAR=0.0
134 1 ▶ chr => [ S_IFCHR, sub { 0666 & ~umask }, \&_parse_major_minor_data, ],
Subroutine __ANON__: called CC=1 SCAR=0.0
135 1 ▶ fifo => [ S_IFIFO, sub { 0666 & ~umask } ],
Subroutine __ANON__: called CC=1 SCAR=0.0
136 1 ▶ sock => [ S_IFSOCK, sub { 0666 & ~umask } ],
Subroutine __ANON__: called CC=1 SCAR=0.0
137 );
138 19 our @_mode_by_int;
139 19 $_mode_by_int[$_->[0]]= $_ for values %_mode_alias;
140 19 $_mode_by_int[0]= undef; # don't map 0 to any mode
141  
142 10543 ▶sub expand_stat_shorthand {
Subroutine expand_stat_shorthand: called CC=14 SCAR=27.3
Pod documented
143 10543 ▶ @_= @{$_[0]} if @_ == 1 && isa_array $_[0];
Branch: 1 branch
BranchTrueFalseTotal
if @_ == 1 and &isa_array($_[0]) 10534 9 10543
Condition66%2/3
@_ == 1 and &isa_array($_[0])
!ll&&!rl&&r
9010534
MC/DC50%1/2
@_ == 1 and &isa_array($_[0])
@_ == 1 &isa_array($_[0])
Truth table66%2/3
A and B
A= @_ == 1B= &isa_array($_[0])
ABresult
0X0
100
111
144 10543 ▶ my %attrs= @_ > 2 && isa_hash $_[-1]? %{ pop @_ } : ();
Branch: 1 branch
BranchTrueFalseTotal
@_ > 2 && &isa_hash($_[-1]) ? : 46 10497 10543
Condition100%3/3
@_ > 2 and &isa_hash($_[-1])
!ll&&!rl&&r
71049046
MC/DC100%2/2
@_ > 2 and &isa_hash($_[-1])
@_ > 2 &isa_hash($_[-1])
Truth table100%3/3
A and B
A= @_ > 2B= &isa_hash($_[-1])
ABresult
0X0
100
111
145 10543 my ($mode, $name, $data)= @_;
146 10543 my $mode_desc;
147 10543 ▶ if (isa_int $mode) {
Branch: 1 branch
BranchTrueFalseTotal
if (&isa_int($mode)) { } 0 10543 10543
148 0 ▶ $mode_desc= $_mode_by_int[$mode & S_IFMT]
Branch: 1 branch
BranchTrueFalseTotal
$mode_desc = $Sys::Export::_mode_by_int[$mode & S_IFMT()] or carp(sprintf("Numeric mode %x doesn't match any known node types", $mode)) 0 0 0
149 or carp sprintf("Numeric mode %x doesn't match any known node types", $mode);
150 }
151 else {
152 10543 ▶ $mode =~ /^([a-z]+)([0-7]+)?\z/
Branch: 1 branch
BranchTrueFalseTotal
$mode =~ /^([a-z]+)([0-7]+)?\z/u or croak("Invalid mode '${mode}': expected number, or prefix file/dir/sym/blk/chr/fifo/sock followed by octal permissions") 10543 0 10543
153 or croak "Invalid mode '$mode': expected number, or prefix file/dir/sym/blk/chr/fifo/sock followed by octal permissions";
154 10543 ▶ $mode_desc= $_mode_alias{$1}
Branch: 1 branch
BranchTrueFalseTotal
$mode_desc = $Sys::Export::_mode_alias{$1} or croak("Unknown mode alias '$1'") 10543 0 10543
155 or croak "Unknown mode alias '$1'";
156 10543 ▶ $mode= $mode_desc->[0] | (defined $2? oct($2) : $mode_desc->[1]->());
Branch: 1 branch
BranchTrueFalseTotal
defined $2 ? : 13 10530 10543
157 }
158 10543 $attrs{mode}= $mode;
159 10543 ▶ length $name or croak "Name must be nonzero length";
Branch: 1 branch
BranchTrueFalseTotal
length $name or croak("Name must be nonzero length") 10543 0 10543
160 10543 $attrs{name}= $name;
161 10543 ▶ if (defined $data) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $data) 10533 10 10543
162 10533 ▶ if ($mode_desc && $mode_desc->[2]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mode_desc and $mode_desc->[2]) { } 4 10529 10533
Condition66%2/3
$mode_desc and $mode_desc->[2]
!ll&&!rl&&r
0105294
MC/DC50%1/2
$mode_desc and $mode_desc->[2]
$mode_desc $mode_desc->[2]
Truth table66%2/3
A and B
A= $mode_descB= $mode_desc->[2]
ABresult
0X0
100
111
163 4 $mode_desc->[2]->(\%attrs, $data);
164 } else {
165 10529 $attrs{data}= $data;
166 }
167 }
168 10543 return %attrs;
169 }
170  
171  
172 0 ▶sub round_up_to_pow2($n) {
Subroutine round_up_to_pow2: not called CC=3 SCAR=24.8
Pod documented
173 0 ▶ croak "Not defined for negative numbers" unless $n > 0;
Branch: 1 branch
BranchTrueFalseTotal
unless $n > 0 0 0 0
174 0 ▶ return 1 if $n <= 1;
Branch: 1 branch
BranchTrueFalseTotal
if $n <= 1 0 0 0
175 0 --$n;
176 0 $n |= $n >> 1;
177 0 $n |= $n >> 2;
178 0 $n |= $n >> 4;
179 0 $n |= $n >> 8;
180 0 $n |= $n >> 16;
181 0 $n |= $n >> 32;
182 0 return $n+1;
183 }
184  
185 62 ▶sub round_up_to_multiple($n, $pow2) {
Subroutine round_up_to_multiple: called CC=2 SCAR=7.1
Pod documented
186 62 ▶ croak "Not defined for negative numbers" unless $n > 0;
Branch: 1 branch
BranchTrueFalseTotal
unless $n > 0 0 62 62
187 62 my $mask= $pow2-1;
188 62 return ($n + $mask) & ~$mask;
189 }
190  
191  
192 19 ▶if (eval { require File::Map; }) {
Branch: 1 branch
BranchTrueFalseTotal
if (eval { do { require File::Map } }) { } 0 19 19
193 0 ▶ eval q{
Branch: 1 branch
BranchTrueFalseTotal
eval "\n sub map_or_load_file(\$filename, \$offset=0, \$length=undef) {\n my \$buf;\n defined \$length? File::Map::map_file(\$buf, \$filename, \"<\", \$offset, \$length)\n : File::Map::map_file(\$buf, \$filename, \"<\", \$offset, \$length);\n return \\\$buf;\n }\n 1;\n " or die "$@" 0 0 0
194 sub map_or_load_file($filename, $offset=0, $length=undef) {
195 my $buf;
196 defined $length? File::Map::map_file($buf, $filename, "<", $offset, $length)
197 : File::Map::map_file($buf, $filename, "<", $offset, $length);
198 return \$buf;
199 }
200 1;
201 } or die "$@";
202 } else {
203 19 *map_or_load_file= *_load_file;
204 }
205  
206 26 ▶sub _load_file($filename, $offset= 0, $length= undef) {
Subroutine _load_file: called CC=10 SCAR=26.5
207 26 ▶ open my $fh, "<:raw", $filename
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<:raw", $filename or die "open($filename): $!" 26 0 26
208 or die "open($filename): $!";
209 26 my $size= -s $fh;
210 26 ▶ croak "Offset beyond end of file ($filename, $offset > $size)" if $offset > $size;
Branch: 1 branch
BranchTrueFalseTotal
if $offset > $size 0 26 26
211 26 ▶ $length //= $size - $offset;
Condition50%1/2
$length //= $size - $offset
l!l
260
MC/DC0%0/1
$length //= $size - $offset
$length
Truth table50%1/2
A
A= $length
Aresult
11
00
212 26 my $buf= '';
213 26 ▶ if ($length) {
Branch: 1 branch
BranchTrueFalseTotal
if ($length) 26 0 26
214 26 ▶ if ($offset > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($offset > 0) 0 26 26
215 0 ▶ sysseek($fh, $offset, 0) == $offset
Branch: 1 branch
BranchTrueFalseTotal
sysseek($fh, $offset, 0) == $offset or croak("sysseek($filename, $offset): $!") 0 0 0
216 or croak "sysseek($filename, $offset): $!";
217 }
218 26 ▶ sysread($fh, $buf, $length) == $size
Branch: 1 branch
BranchTrueFalseTotal
sysread($fh, $buf, $length) == $size or die "sysread($filename, $size): $!" 26 0 26
219 or die "sysread($filename, $size): $!";
220 }
221 26 \$buf;
222 }
223  
224  
225 23 ▶sub filedata {
Subroutine filedata: called CC=1 SCAR=0.0
Pod documented
226 23 state $loaded= require Sys::Export::LazyFileData;
227 23 Sys::Export::LazyFileData->new(@_);
228 }
229  
230  
231 10751 ▶sub write_file_extent($fh, $addr, $size, $data_ref, $ofs=0, $descrip=undef) {
Subroutine write_file_extent: called CC=20 SCAR=39.6
Pod documented
232 10751 ▶ $log->tracef("write %s at 0x%X-0x%X from buf size 0x%X%s",
Branch: 2 branches
BranchTrueFalseTotal
if $Sys::Export::log->is_trace 0 10751 10751
$ofs ? : 0 0 0
Condition0%0/2
$descrip // "blocks"
l!l
00
MC/DC0%0/1
$descrip // "blocks"
$descrip
Truth table0%0/2
A
A= $descrip
Aresult
11
00
233 $descrip//'blocks', $addr, $addr+$size, length($$data_ref), $ofs? sprintf(" ofs 0x%X", $ofs) : ''
234 ) if $log->is_trace;
235 10751 ▶ return unless $size > 0;
Branch: 1 branch
BranchTrueFalseTotal
unless $size > 0 0 10751 10751
236 10751 ▶ if (defined $addr) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $addr) 10751 0 10751
237 10751 ▶ my $reached= sysseek($fh, $addr, 0) // croak "sysseek($addr): $!";
Condition50%1/2
sysseek($fh, $addr, 0) // croak("sysseek($addr): $!")
l!l
107510
MC/DC0%0/1
sysseek($fh, $addr, 0) // croak("sysseek($addr): $!")
sysseek($fh, $addr, 0)
Truth table50%1/2
A
A= sysseek($fh, $addr, 0)
Aresult
11
00
238 10751 ▶ $reached == $addr or croak "sysseek($addr) arrived at $reached instead of $addr";
Branch: 1 branch
BranchTrueFalseTotal
$reached == $addr or croak("sysseek($addr) arrived at $reached instead of $addr") 10751 0 10751
239 }
240 10751 ▶ $ofs //= 0;
Condition50%1/2
$ofs //= 0
l!l
107510
MC/DC0%0/1
$ofs //= 0
$ofs
Truth table50%1/2
A
A= $ofs
Aresult
11
00
241 10751 ▶ my $avail= $data_ref? (length($$data_ref) - $ofs) : 0;
Branch: 1 branch
BranchTrueFalseTotal
$data_ref ? : 10751 0 10751
242 10751 my $second;
243 # always write full size, padding with zeroes
244 10751 ▶ if ($avail < $size) {
Branch: 1 branch
BranchTrueFalseTotal
if ($avail < $size) 1747 9004 10751
245 # If the scalar is particularly large, do two writes instead of reallocating the buffer.
246 1747 ▶ if ($avail > 0x100000) {
Branch: 1 branch
BranchTrueFalseTotal
if ($avail > 1048576) { } 0 1747 1747
247 0 my $first_write= $avail - ($avail & 0xFFF);
248 0 $second= pack 'a'.($size-$first_write), substr($$data_ref, $first_write);
249 0 $size= $first_write;
250 } else {
251 1747 ▶ my $data= pack 'a'.$size, ($avail > 0? substr($$data_ref, $ofs) : '');
Branch: 1 branch
BranchTrueFalseTotal
$avail > 0 ? : 1747 0 1747
252 1747 $data_ref= \$data;
253 1747 $ofs= 0;
254 }
255 }
256 10751 my $wrote= syswrite($fh, $$data_ref, $size, $ofs);
257 10751 ▶ croak "syswrite: $!" if !defined $wrote;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $wrote 0 10751 10751
258 10751 ▶ croak "Unexpected short write ($wrote != $size)" if $wrote != $size;
Branch: 1 branch
BranchTrueFalseTotal
if $wrote != $size 0 10751 10751
259 10751 ▶ if (length $second) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $second) 0 10751 10751
260 0 $wrote= syswrite($fh, $second);
261 0 ▶ croak "syswrite: $!" if !defined $wrote;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $wrote 0 0 0
262 0 ▶ croak "Unexpected short write ($wrote != $size)" if $wrote != length($second);
Branch: 1 branch
BranchTrueFalseTotal
if $wrote != length $second 0 0 0
263 }
264 10751 return 1;
265 }
266  
267 19 ▶if (eval { pack('Q<', 1) }) {
Branch: 1 branch
BranchTrueFalseTotal
if (eval { do { "\cA\000\000\000\000\000\000\000" } }) { } 19 0 19
268 19 *_pack= \*CORE::pack;
269 19 *_unpack= \*CORE::unpack;
270 } else {
271 0 eval <<'END';
272 # On perl without 64-bit support, replace all 'Q' with 32-bit operations
273 # This does not handle full pack syntax, just what is used in this module collection.
274 sub _pack {
275 my $fmt= shift;
276 my $new_fmt= '';
277 my @new_args;
278 require Math::BigInt;
279 my $mask32= Math::BigInt->new('4294967295');
280 for (split / +/, $fmt) {
281 if ($_ eq 'Q>') {
282 # Convert a 64-bit integer into two 32-bit big-endian arguments
283 $new_fmt .= 'NN';
284 my $qw= Math::BigInt->new(shift);
285 push @new_args, ($qw >> 32)->numify(), ($qw & $mask32)->numify();
286 } elsif ($_ eq 'Q<') {
287 # Convert 64-bit integer into two 32-bit little-endian arguments
288 $new_fmt .= 'VV';
289 my $qw= Math::BigInt->new(shift);
290 push @new_args, ($qw & $mask32)->numify(), ($qw >> 32)->numify();
291 } else {
292 $new_fmt .= $_;
293 push @new_args, shift;
294 }
295 }
296 return pack $new_fmt, @new_args;
297 }
298 # This does not handle full unpack syntax, just what is used in this module collection.
299 sub _unpack {
300 my $fmt= shift;
301 my $new_fmt= '';
302 my @replacements;
303 require Math::BigInt;
304 my @fields= split / +/, $fmt;
305 for (reverse 0 .. $#fields) {
306 if ($fields[$_] eq 'Q>') {
307 $new_fmt .= 'a8';
308 push @replacements, [ $_, sub {
309 my ($h,$l)= unpack('NN', $_[0]);
310 (Math::BigInt->new($h) << 32) | $l
311 }];
312 } elsif ($fields[$_] eq 'Q<') {
313 $new_fmt .= 'a8';
314 push @replacements, [ $_, sub {
315 my ($l, $h)= unpack('VV', $_[0]);
316 (Math::BigInt->new($h) << 32) | $l
317 }];
318 }
319 }
320 my @vals= unpack $new_fmt;
321 for (@replacements) {
322 $vals[$_->[0]]= $_->[1]->($vals[$_->[0]]);
323 }
324 return @vals;
325 }
326 END
327 }
328  
329 19 1;
330  
331 __END__
« Export/ISO9660Hybrid.pm ↑ Summary Export/VFAT/AllocationTable.pm »