cpm/Installer/Unpacker.pm untested

filter: Statementstmt 0.0% 0 / 174 Branchbran 0.0% 0 / 96 Conditioncond 0.0% 0 / 42 MC/DCmcdc 0.0% 0 / 26 Subroutinesub 0.0% 0 / 25 Podpod 0.0% 0 / 13 totaltotal 0.0% 0 / 376 CC 49 SCAR 40.0
CC
49
Cov
0%
_init_untar
182.0
_untar_bad
56.0
_unzip_module
56.0
CRAP 54.4
1 package App::cpm::Installer::Unpacker;
2  
3 # Based on https://github.com/miyagawa/cpanminus/blob/7b574ede70cebce3709743ec1727f90d745e8580/Menlo-Legacy/lib/Menlo/CLI/Compat.pm#L2756-L2891
4 use v5.24;
5 use warnings;
6 use experimental qw(lexical_subs signatures);
7  
8 use App::cpm::Util ();
9 use File::Basename ();
10 use File::Temp ();
11 use File::Which ();
12 use IPC::Run3 ();
13  
14 sub run3 ($cmd, $outfile = undef) {
15 my $out;
16 IPC::Run3::run3 $cmd, \undef, ($outfile ? $outfile : \$out), \my $err;
17 return ($?, $out, $err);
18 }
19  
20 sub new ($class, %argv) {
21 my $self = bless \%argv, $class;
22 $self->_init_untar;
23 $self->_init_unzip;
24 $self;
25 }
26  
27 sub unpack ($self, $file) {
28 my $method = $file =~ /\.zip$/ ? $self->{method}{unzip} : $self->{method}{untar};
29 $self->$method($file);
30 }
31  
32 sub describe ($self) {
33 my %describe = (
34 map { ($_, $self->{$_}) }
35 grep $self->{$_},
36 qw(Archive::Tar unzip Archive::Zip),
37 );
38 if ($self->{tar}) {
39 $describe{tar} = sprintf "%s (%s%s)",
40 $self->{tar},
41 $self->{tar_kind},
42 $self->{tar_bad} ? ", bad" : "",
43 ;
44 }
45 \%describe;
46 }
47  
48 sub _init_untar ($self) {
49 my $tar = $self->{tar} = File::Which::which('gtar') || File::Which::which("tar");
50 if ($tar) {
51 my ($exit, $out, $err) = run3 [$tar, '--version'];
52 if ($out =~ /bsdtar/) {
53 $self->{tar_kind} = 'bsd';
54 } elsif ($out =~ /GNU/) {
55 $self->{tar_kind} = 'gnu';
56 } elsif ($^O eq 'openbsd') {
57 $self->{tar_kind} = 'openbsd';
58 } else {
59 $self->{tar_kind} = 'unknown';
60 }
61 $self->{tar_bad} = 1 if $out =~ /GNU.*1\.13/i || $^O eq 'MSWin32' || $^O eq 'solaris' || $^O eq 'hpux';
62 }
63  
64 if ($tar and !$self->{tar_bad}) {
65 $self->{method}{untar} = *_untar;
66 return if !$self->{_init_all};
67 }
68  
69 if ($tar) {
70 $self->{method}{untar} = *_untar_bad;
71 return if !$self->{_init_all};
72 }
73  
74 if (eval { require Archive::Tar }) {
75 $self->{"Archive::Tar"} = Archive::Tar->VERSION;
76 $self->{method}{untar} = *_untar_module;
77 return if !$self->{_init_all};
78 }
79  
80 return if $self->{_init_all};
81 $self->{method}{untar} = sub ($, @) { die "There is no backend for untar" };
82 }
83  
84 sub _init_unzip ($self) {
85 my $unzip = $self->{unzip} = File::Which::which("unzip");
86 if ($unzip) {
87 $self->{method}{unzip} = *_unzip;
88 return if !$self->{_init_all};
89 }
90  
91 if (eval { require Archive::Zip }) {
92 $self->{"Archive::Zip"} = Archive::Zip->VERSION;
93 $self->{method}{unzip} = *_unzip_module;
94 return if !$self->{_init_all};
95 }
96  
97 return if $self->{_init_all};
98 $self->{method}{unzip} = sub ($, @) { die "There is no backend for unzip" };
99 }
100  
101 sub _untar ($self, $file) {
102 my $wantarray = wantarray;
103  
104 my ($exit, $out, $err);
105 {
106 my $ar = $file =~ /\.bz2$/ ? 'j' : 'z';
107 ($exit, $out, $err) = run3 [$self->{tar}, "${ar}tf", $file];
108 last if $exit != 0;
109 my $root = $self->_find_tarroot(split /\r?\n/, $out);
110 my @no_same_owner = $self->{tar_kind} eq 'openbsd' ? () : ('-o');
111 ($exit, $out, $err) = run3 [$self->{tar}, "${ar}xf", $file, @no_same_owner];
112 return $root if $exit == 0 and -d $root;
113 }
114 return if !$wantarray;
115 return (undef, $err || $out);
116 }
117  
118 sub _untar_bad ($self, $file) {
119 my $wantarray = wantarray;
120 my ($exit, $out, $err);
121 {
122 my $ar = $file =~ /\.bz2$/ ? \&App::cpm::Util::bunzip2 : \&App::cpm::Util::gunzip;
123 my $temp = File::Temp->new(SUFFIX => '.tar', EXLOCK => 0);
124 my $ok;
125 ($ok, $err) = $ar->($file, $temp->filename);
126 if (!$ok) {
127 last;
128 }
129  
130 # XXX /usr/bin/tar: Cannot connect to C: resolve failed
131 my @opt = $^O eq 'MSWin32' && $self->{tar_kind} ne "bsd" ? ('--force-local') : ();
132 ($exit, $out, $err) = run3 [$self->{tar}, @opt, "-tf", $temp->filename];
133 last if $exit != 0 || !$out;
134 my $root = $self->_find_tarroot(split /\r?\n/, $out);
135 ($exit, $out, $err) = run3 [$self->{tar}, @opt, "-xf", $temp->filename, "-o"];
136 return $root if $exit == 0 and -d $root;
137 }
138 return if !$wantarray;
139 return (undef, $err || $out);
140 }
141  
142 sub _untar_module ($self, $file) {
143 my $wantarray = wantarray;
144 no warnings 'once';
145 local $Archive::Tar::WARN = 0;
146 my $t = Archive::Tar->new;
147 {
148 my $ok = $t->read($file);
149 last if !$ok;
150 my $root = $self->_find_tarroot($t->list_files);
151 my @file = $t->extract;
152 return $root if @file and -d $root;
153 }
154 return if !$wantarray;
155 return (undef, $t->error);
156 }
157  
158 sub _find_tarroot ($self, $root, @others) {
159 FILE: {
160 chomp $root;
161 $root =~ s!^\./!!;
162 $root =~ s{^(.+?)/.*$}{$1};
163 if (!length $root) { # archive had ./ as the first entry, so try again
164 $root = shift @others;
165 redo FILE if $root;
166 }
167 }
168 $root;
169 }
170  
171 sub _unzip ($self, $file) {
172 my $wantarray = wantarray;
173  
174 my ($exit, $out, $err);
175 {
176 ($exit, $out, $err) = run3 [$self->{unzip}, '-t', $file];
177 last if $exit != 0;
178 my $root = $self->_find_ziproot(split /\r?\n/, $out);
179 ($exit, $out, $err) = run3 [$self->{unzip}, '-q', $file];
180 return $root if $exit == 0 and -d $root;
181 }
182 return if !$wantarray;
183 return (undef, $err || $out);
184 }
185  
186 sub _unzip_module ($self, $file) {
187 my $wantarray = wantarray;
188  
189 no warnings 'once';
190 my $err = ''; local $Archive::Zip::ErrorHandler = sub (@argv) { $err .= "@argv" };
191 my $zip = Archive::Zip->new;
192 UNZIP: {
193 my $status = $zip->read($file);
194 last UNZIP if $status != Archive::Zip::AZ_OK();
195 for my $member ($zip->members) {
196 my $af = $member->fileName;
197 next if $af =~ m!^(/|\.\./)!;
198 my $status = $member->extractToFileNamed($af);
199 last UNZIP if $status != Archive::Zip::AZ_OK();
200 }
201 my ($root) = $zip->membersMatching(qr{^[^/]+/$});
202 last UNZIP if !$root;
203 $root = $root->fileName;
204 $root =~ s{/$}{};
205 return $root if -d $root;
206 }
207 return if !$wantarray;
208 return (undef, $err);
209 }
210  
211 sub _find_ziproot ($self, $, $root, @others) {
212 FILE: {
213 chomp $root;
214 if ($root !~ s{^\s+testing:\s+([^/]+)/.*?\s+OK$}{$1}) {
215 $root = shift @others;
216 redo FILE if $root;
217 }
218 }
219 $root;
220 }
221  
222 1;