blib/lib/App/Pasar.pm

filter: Statementstmt 71.8% 46 / 64 Branchbran 52.3% 22 / 42 Conditioncond 50.0% 7 / 14 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 63.6% 7 / 11 Podpod 0.0% 0 / 4 totaltotal 57.3% 82 / 143 CC 29 SCAR 31.5
CC
29
Cov
62%
main
46.4
usage
12.0
list
8.4
CRAP 23.4
↑ Summary
1 1 ▶use v5.36;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 package App::Pasar 0.02;
3 1 ▶use Archive::Asar 0.02 ();
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use Getopt::Long qw(GetOptionsFromArray :config gnu_getopt);
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 0 ▶sub version() {
Subroutine version: not called CC=1 SCAR=6.9
Pod undocumented
7 0 say "pasar (" . __PACKAGE__ . ") " . __PACKAGE__->VERSION;
8 0 exit;
9 }
10  
11 0 ▶sub usage($err) {
Subroutine usage: not called CC=3 SCAR=24.8
Pod undocumented
12 0 ▶ print { $err ? \*STDERR : \*STDOUT } <<~"_EOT_";
Branch: 1 branch
BranchTrueFalseTotal
$err ? : 0 0 0
13 Usage: $0 OPTION... FILE [DIRECTORY]
14 Extract data from Electron ASAR archive files.
15  
16 -l, --list list contents of archive FILE
17 -x, --extract recursively extract contents of FILE to DIRECTORY
18 -u DIR, --unpacked=DIR read archive files marked "unpacked" from DIR
19 instead; if DIR is the empty string, "unpacked"
20 files are skipped instead
21 -h, --help display this help and exit
22 --version output version information and exit
23  
24 Either -l/--list or -x/--extract must be given.
25 _EOT_
26 0 ▶ exit($err ? 1 : 0);
Branch: 1 branch
BranchTrueFalseTotal
$err ? : 0 0 0
27 }
28  
29 1 ▶sub list($asar) {
Subroutine list: called CC=8 SCAR=21.3
Pod undocumented
30 10 ▶ my sub esc($x, $harder) {
Subroutine esc: called CC=2 SCAR=7.6
31 10 ▶ $x =~ s{([/\\])}{\\$1}g if $harder;
Branch: 1 branch
BranchTrueFalseTotal
if $harder 10 0 10
32 0 $x =~ s{([\x00-\x1f])}{ sprintf '\\x%02x', ord $1 }eg;
33 10 $x
34 }
35  
36 8 ▶ my sub get_node($path, $node) {
Subroutine get_node: called CC=3 SCAR=11.2
37 8 for my $p (@$path) {
38 10 ▶ $node = $node->{files}{$p} // return undef;
Condition50%1/2
$node->{"files"}{$p} // (return undef)
l!l
100
MC/DC0%0/1
$node->{"files"}{$p} // (return undef)
$node->{"files"}{$p}
Truth table50%1/2
A
A= $node->{"files"}{$p}
Aresult
11
00
39 }
40 $node
41 8 }
42  
43 1 my $index = $asar->index_raw;
44  
45 1 my $listing = '';
46 1 my @stack = [];
47 1 ▶ while (@stack) {
Branch: 1 branch
BranchTrueFalseTotal
if (@stack) 8 1 9
48 8 my $path = pop @stack;
49 8 my $node = get_node $path, $index;
50  
51 8 my $name = join '/', map esc($_, 1), @$path;
52 8 ▶ if (exists $node->{link}) {
Branch: 2 branches
BranchTrueFalseTotal
if (exists $node->{'link'}) { } 0 8 8
elsif (exists $node->{'files'}) { } 3 5 8
53 0 $listing .= "LINK $name -> " . esc($node->{link}, 0) . "\n";
54 } elsif (exists $node->{files}) {
55 3 push @stack, map [@$path, $_], sort { $b cmp $a } keys $node->{files}->%*;
56 3 ▶ my $unpacked = $node->{unpacked} ? ' (unpacked)' : '';
Branch: 1 branch
BranchTrueFalseTotal
$node->{'unpacked'} ? : 0 3 3
57 3 ▶ $listing .= "DIR $name$unpacked\n" if @$path;
Branch: 1 branch
BranchTrueFalseTotal
if @$path 2 1 3
58 } else {
59 5 ▶ my $size = exists $node->{size} ? ' ' . (0 + $node->{size}) : '';
Branch: 1 branch
BranchTrueFalseTotal
exists $node->{'size'} ? : 5 0 5
60 5 ▶ my $unpacked = $node->{unpacked} ? ' (unpacked)' : '';
Branch: 1 branch
BranchTrueFalseTotal
$node->{'unpacked'} ? : 0 5 5
61 5 $listing .= "FILE $name$size$unpacked\n";
62 }
63 }
64  
65 $listing
66 1 }
67  
68 2 ▶sub main(@args) {
Subroutine main: called CC=17 SCAR=38.4
Pod undocumented
69 GetOptionsFromArray(
70 \@args,
71 'create|c' => \my $opt_create,
72 'list|l' => \my $opt_list,
73 'extract|x' => \my $opt_extract,
74 'unpacked|u=s' => \my $opt_unpacked,
75 0 ▶ 'help|h' => sub (@) { usage 0 },
Subroutine __ANON__: not called CC=1 SCAR=6.9
76 0 ▶ 'version' => sub (@) { version },
Subroutine __ANON__: not called CC=1 SCAR=6.9
77 2 ▶ ) or usage 1;
Branch: 1 branch
BranchTrueFalseTotal
&GetOptionsFromArray(\@args, "create|c", \my $opt_create, "list|l", \my $opt_list, "extract|x", \my $opt_extract, "unpacked|u=s", \my $opt_unpacked, "help|h", sub (@) { usage(0); } , "version", sub (@) { version(); } ) or usage(1) 2 0 2
78 2 ▶ $opt_create || $opt_list || $opt_extract
Branch: 1 branch
BranchTrueFalseTotal
$opt_create or $opt_list or $opt_extract or die "$0: either --create, --list, or --extract must be given\n" 2 0 2
Condition66%4/6
$opt_create or $opt_list or $opt_extract
l!l&&r!l&&!r
110
$opt_create or $opt_list
l!l&&r!l&&!r
011
MC/DC0%0/3
$opt_create or $opt_list or $opt_extract
$opt_create $opt_list $opt_extract
Truth table50%2/4
A or B or C
A= $opt_createB= $opt_listC= $opt_extract
ABCresult
1XX1
01X1
0011
0000
79 or die "$0: either --create, --list, or --extract must be given\n";
80  
81 2 ▶ @args or die "$0: no archive name\n";
Branch: 1 branch
BranchTrueFalseTotal
@args or die "$0: no archive name\n" 2 0 2
82 2 my $asarfile = shift @args;
83  
84 2 my $asar;
85 2 ▶ if ($opt_create) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt_create) 0 2 2
86 0 $asar = Archive::Asar->new_empty($asarfile);
87 0 for my $arg (@args) {
88 0 my @path = grep $_ ne '.', split m{[/\\]+}, $arg, -1;
89 0 ▶ if (grep $_ eq '..', @path) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep(($_ eq ".."), @path)) 0 0 0
90 0 die "$0: can't embed paths containing '..': $arg\n";
91 }
92 0 $asar->ingest(\@path, $arg);
93 }
94 0 $asar->write_to_file($asarfile);
95 }
96  
97 2 ▶ if ($opt_list) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt_list) 1 1 2
98 1 ▶ $asar //= Archive::Asar->new_from_file($asarfile);
Condition33%1/3
$asar //= "Archive::Asar"->new_from_file($asarfile)
l!l&&r!l&&!r
010
MC/DC0%0/2
$asar //= "Archive::Asar"->new_from_file($asarfile)
$asar "Archive::Asar"->new_from_file($asarfile)
Truth table33%1/3
A //= B
A= $asarB= "Archive::Asar"->new_from_file($asarfile)
ABresult
1X1
011
000
99 1 print list($asar);
100 }
101  
102 2 ▶ if ($opt_extract) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt_extract) 1 1 2
103 1 ▶ $asar //= Archive::Asar->new_from_file($asarfile);
Condition33%1/3
$asar //= "Archive::Asar"->new_from_file($asarfile)
l!l&&r!l&&!r
010
MC/DC0%0/2
$asar //= "Archive::Asar"->new_from_file($asarfile)
$asar "Archive::Asar"->new_from_file($asarfile)
Truth table33%1/3
A //= B
A= $asarB= "Archive::Asar"->new_from_file($asarfile)
ABresult
1X1
011
000
104 1 ▶ my $outdir = @args ? shift @args : '.';
Branch: 1 branch
BranchTrueFalseTotal
@args ? : 1 0 1
105 1 my %opts;
106 1 ▶ if (defined $opt_unpacked) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $opt_unpacked) { } 0 1 1
elsif (-d "$asarfile.unpacked") { } 0 1 1
107 0 ▶ $opts{unpacked_dir} = length $opt_unpacked ? $opt_unpacked : undef;
Branch: 1 branch
BranchTrueFalseTotal
length $opt_unpacked ? : 0 0 0
108 } elsif (-d "$asarfile.unpacked") {
109 0 $opts{unpacked_dir} = "$asarfile.unpacked";
110 }
111  
112 1 $asar->extract_to($outdir, \%opts);
113 }
114 }
115  
116 1 1
117 __END__
↑ Summary