.github/dzil-binary/win-dlls.pl

filter: Statementstmt 100% 56 / 56 Branchbran 88.8% 16 / 18 Conditioncond 66.6% 2 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 100% 9 / 9 Podpod n/a totaltotal 94.3% 83 / 88 CC 13 SCAR 12.7
CC
13
Cov
96%
index_dirs
6.0
resolve
4.0
run
3.0
CRAP 3.6
« blib/lib/Dist/Zilla/Plugin/Author/GETTY/GiteaMeta.pm ↑ Summary blib/lib/Pod/Weaver/Section/Author/GETTY/Legal.pm »
1 #!/usr/bin/env perl
2 # Windows: the DLLs a packed binary links that lib-gate.pl would flag as foreign,
3 # looked up in the given directories only (the packing perl's own), for pp -l.
4 # win-dlls.pl <binary> <dir>...
5 # Prints one <dir>/<file> per line, nothing when nothing is missing. A found DLL's
6 # own imports are resolved the same way. A name in none of the directories is
7 # named on STDERR and left out: lib-gate.pl then fails it.
8 package DzilBinary::WinDlls;
9 79 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 54 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 54 ▶use File::Basename ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12 54 ▶use File::Spec ();
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 # the same judgement as the gate: its inspection, allowlist and foreign()
15 29 ▶require(File::Spec->catfile(File::Basename::dirname(File::Spec->rel2abs(__FILE__)), 'lib-gate.pl'))
Branch: 1 branch
BranchTrueFalseTotal
unless defined &DzilBinary::LibGate::inspect_binary 0 29 29
16 unless defined &DzilBinary::LibGate::inspect_binary;
17  
18 # lc file name => <dir>/<file as spelled on disk>, the first directory holding it
19 # winning (objdump's names are lower-cased by linked()).
20 9 ▶sub index_dirs {
Subroutine index_dirs: called CC=6 SCAR=17.9
21 9 my (@dirs) = @_;
22 9 my %at;
23 9 for my $d (@dirs) {
24 20 ▶ opendir my $dh, $d or do { print STDERR "win-dlls: cannot read $d: $!\n"; next };
Branch: 1 branch
BranchTrueFalseTotal
opendir my $dh, $d or do { print STDERR "win-dlls: cannot read ${d}: $!\n"; next } 43 2 45
25 18 for my $f (sort readdir $dh) {
26 65 my $p = "$d/$f";
27 65 ▶ $at{lc $f} //= $p if -f $p;
Branch: 1 branch
BranchTrueFalseTotal
if -f $p 52 38 90
Condition66%2/3
$at{lc $f} //= $p
l!l&&r!l&&!r
2250
MC/DC0%0/2
$at{lc $f} //= $p
$at{lc $f} $p
Truth table66%2/3
A //= B
A= $at{lc $f}B= $p
ABresult
1X1
011
000
28 }
29 18 closedir $dh;
30 }
31 9 return \%at;
32 }
33  
34 # Until nothing new is flagged: every name foreign() flags for %$links/@$packed
35 # is looked up in %$at; a found DLL counts as packed and its own imports join
36 # %links. Returns ([paths found], [names found nowhere]), both sorted.
37 17 ▶sub resolve {
Subroutine resolve: called CC=4 SCAR=13.9
38 17 my ($links, $packed, $at) = @_;
39 17 my %links = %$links;
40 17 my @packed = @$packed;
41 17 my (%tried, %found, @missing);
42 17 ▶ while (my @new = grep { !$tried{lc $_}++ }
Branch: 1 branch
BranchTrueFalseTotal
if (my(@new) = grep({not $tried{lc $_}++;} DzilBinary::LibGate::foreign(\%links, [DzilBinary::LibGate::allowlist("windows", \%links)], \@packed))) 24 14 38
43 DzilBinary::LibGate::foreign(\%links, [DzilBinary::LibGate::allowlist('windows', \%links)], \@packed)) {
44 24 for my $name (@new) {
45 24 my $path = $at->{lc $name};
46 24 ▶ if (!defined $path) { push @missing, $name; next }
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $path) 3 21 24
47 21 $found{$path} = 1;
48 21 push @packed, File::Basename::basename($path);
49 21 $links{$path} = DzilBinary::LibGate::linked('windows', $path);
50 }
51 }
52 14 return ([sort keys %found], [sort @missing]);
53 }
54  
55 24 ▶sub run {
Subroutine run: called CC=3 SCAR=11.0
56 24 my ($archive, @dirs) = @_;
57 24 local $| = 1;
58 24 ▶ my $seen = DzilBinary::LibGate::inspect_binary($archive, 'windows', sub { });
Subroutine __ANON__: called CC=1 SCAR=0.0
59 7 my ($found, $missing) = resolve($seen->{links}, $seen->{packed}, index_dirs(@dirs));
60 4 ▶ print STDERR "win-dlls: in none of ", join(', ', @dirs), ": @$missing (lib-gate.pl fails them)\n" if @$missing;
Branch: 1 branch
BranchTrueFalseTotal
if @$missing 1 3 4
61 4 print "$_\n" for @$found;
62 4 return 0;
63 }
64  
65 25 ▶sub main {
Subroutine main: called CC=3 SCAR=11.0
66 25 my ($archive, @dirs) = @_;
67 25 ▶ die "usage: win-dlls.pl <binary> <dir>...\n" unless @dirs;
Branch: 1 branch
BranchTrueFalseTotal
unless @dirs 1 24 25
68 24 my $rc = eval { run($archive, @dirs) };
69 5 ▶ if ($@) { print STDERR $@; return 2 }
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 1 4 5
70 4 return $rc;
71 }
72  
73 29 ▶exit main(@ARGV) unless caller;
Branch: 1 branch
BranchTrueFalseTotal
unless caller 0 29 29
74 29 1;
« blib/lib/Dist/Zilla/Plugin/Author/GETTY/GiteaMeta.pm ↑ Summary blib/lib/Pod/Weaver/Section/Author/GETTY/Legal.pm »