1 # -*- Mode: cperl; coding: utf-8; cperl-indent-level: 2 -*-
4 use vars qw($VERSION @ISA $BUGHUNTING);
7 $VERSION = sprintf "%.6f", substr(q$Rev: 659 $,4)/1000000 + 5.4;
8 # module is internal to CPAN.pm
10 @ISA = qw(CPAN::Debug);
11 $BUGHUNTING = 0; # released code must have turned off
13 # it's ok if file doesn't exist, it just matters if it is .gz or .bz2
15 my($class,$file) = @_;
16 $CPAN::Frontend->mydie("new called without arg") unless defined $file;
18 # nonono, we get e.g. 01mailrc.txt uncompressed if only wget is available
19 $CPAN::Frontend->mydie("file[$file] doesn't match /\\.(bz2|gz|zip|tgz)\$/")
20 unless $file =~ /\.(bz2|gz|zip|tgz)$/i;
22 my $me = { FILE => $file };
24 } elsif ($file =~ /\.bz2$/i) {
25 unless ($me->{UNGZIPPRG} = $CPAN::Config->{bzip2}) {
27 if ($CPAN::META->has_inst("File::Which")) {
28 $bzip2 = File::Which::which("bzip2");
31 $me->{UNGZIPPRG} = $bzip2;
33 $CPAN::Frontend->mydie(qq{
34 CPAN.pm needs the external program bzip2 in order to handle '$file'.
35 Please install it now and run 'o conf init' to register it as external
41 # yes, we let gzip figure it out in *any* other case
42 $me->{UNGZIPPRG} = $CPAN::Config->{gzip};
49 my $write = $self->{FILE};
50 if ($CPAN::META->has_inst("Compress::Zlib")) {
52 $fhw = FileHandle->new($read)
53 or $CPAN::Frontend->mydie("Could not open $read: $!");
55 my $gz = Compress::Zlib::gzopen($write, "wb")
56 or $CPAN::Frontend->mydie("Cannot gzopen $write: $! (pwd is $cwd)\n");
58 while read($fhw,$buffer,4096) > 0 ;
63 system(qq{$self->{UNGZIPPRG} -c "$read" > "$write"})==0;
69 my($self,$write) = @_;
70 my $read = $self->{FILE};
71 if ($CPAN::META->has_inst("Compress::Zlib")) {
73 $fhw = FileHandle->new(">$write")
74 or $CPAN::Frontend->mydie("Could not open >$write: $!");
75 my $gz = Compress::Zlib::gzopen($read, "rb")
76 or $CPAN::Frontend->mydie("Cannot gzopen $read: $!\n");
78 while $gz->gzread($buffer) > 0 ;
79 $CPAN::Frontend->mydie("Error reading from $read: $!\n")
80 if $gz->gzerror != Compress::Zlib::Z_STREAM_END();
85 system(qq{$self->{UNGZIPPRG} -dc "$read" > "$write"})==0;
92 return $self->{GTEST} if exists $self->{GTEST};
93 my $read = $self->{FILE} or die;
95 # After I had reread the documentation in zlib.h, I discovered that
96 # uncompressed files do not lead to an gzerror (anymore?).
97 if ( $CPAN::META->has_inst("Compress::Zlib") ) {
100 my $gz = Compress::Zlib::gzopen($read, "rb")
101 or $CPAN::Frontend->mydie(sprintf("Cannot gzopen %s: %s\n",
103 $Compress::Zlib::gzerrno));
104 while ($gz->gzread($buffer) > 0 ){
105 $len += length($buffer);
108 my $err = $gz->gzerror;
109 $success = ! $err || $err == Compress::Zlib::Z_STREAM_END();
110 if ($len == -s $read){
112 CPAN->debug("hit an uncompressed file") if $CPAN::DEBUG;
115 CPAN->debug("err[$err]success[$success]") if $CPAN::DEBUG;
117 $success = 0==system(qq{$self->{UNGZIPPRG} -qdt "$read"});
119 return $self->{GTEST} = $success;
124 my($class,$file) = @_;
126 $class->debug("file[$file]");
127 my $self = $class->new($file);
129 } elsif (!$self->gtest) {
130 my $fh = FileHandle->new($file) or die "Could not open file[$file]: $!";
133 } elsif ($CPAN::META->has_inst("Compress::Zlib")) {
134 my $gz = Compress::Zlib::gzopen($file,"rb") or
135 die "Could not gzopen $file";
138 my $pipe = "$CPAN::Config->{gzip} -dc $file |";
139 my $fh = FileHandle->new($pipe) or die "Could not pipe[$pipe]: $!";
149 if (exists $self->{GZ}) {
150 my $gz = $self->{GZ};
151 my($line,$bytesread);
152 $bytesread = $gz->gzreadline($line);
153 return undef if $bytesread <= 0;
156 my $fh = $self->{FH};
163 my($self,$ref,$length,$offset) = @_;
164 die "read with offset not implemented" if defined $offset;
165 if (exists $self->{GZ}) {
166 my $gz = $self->{GZ};
167 my $byteread = $gz->gzread($$ref,$length);# 30eaf79e8b446ef52464b5422da328a8
170 my $fh = $self->{FH};
171 return read($fh,$$ref,$length);
178 if (exists $self->{GZ}) {
179 my $gz = $self->{GZ};
180 $gz->gzclose() if defined $gz; # hard to say if it is allowed
181 # to be undef ever. AK, 2000-09
183 my $fh = $self->{FH};
184 $fh->close if defined $fh;
192 my $file = $self->{FILE};
195 if (0) { # makes changing order easier
196 } elsif ($BUGHUNTING){
198 } elsif (MM->maybe_command($self->{UNGZIPPRG})
200 MM->maybe_command($CPAN::Config->{'tar'})) {
201 # should be default until Archive::Tar handles bzip2
204 $CPAN::META->has_inst("Archive::Tar")
206 $CPAN::META->has_inst("Compress::Zlib") ) {
207 if ($file =~ /\.bz2$/) {
208 $CPAN::Frontend->mydie(qq{
209 Archive::Tar lacks support for bz2. Can't continue.
214 $CPAN::Frontend->mydie(qq{
215 CPAN.pm needs either the external programs tar, gzip and bzip2
216 installed. Can't continue.
219 if ($prefer==1) { # 1 => external gzip+tar
221 my $is_compressed = $self->gtest();
222 if ($is_compressed) {
223 $system = qq{$self->{UNGZIPPRG} -dc }.
224 qq{< "$file" | $CPAN::Config->{tar} xvf -};
226 $system = qq{$CPAN::Config->{tar} xvf "$file"};
228 if (system($system) != 0) {
229 # people find the most curious tar binaries that cannot handle
231 if ($is_compressed) {
232 (my $ungzf = $file) =~ s/\.gz(?!\n)\Z//;
233 $ungzf = File::Basename::basename($ungzf);
234 my $ct = CPAN::Tarzip->new($file);
235 if ($ct->gunzip($ungzf)) {
236 $CPAN::Frontend->myprint(qq{Uncompressed $file successfully\n});
238 $CPAN::Frontend->mydie(qq{Couldn\'t uncompress $file\n});
242 $system = qq{$CPAN::Config->{tar} xvf "$file"};
243 $CPAN::Frontend->myprint(qq{Using Tar:$system:\n});
244 if (system($system)==0) {
245 $CPAN::Frontend->myprint(qq{Untarred $file successfully\n});
247 $CPAN::Frontend->mydie(qq{Couldn\'t untar $file\n});
253 } elsif ($prefer==2) { # 2 => modules
254 my $tar = Archive::Tar->new($file,1);
255 my $af; # archive file
258 # RCS 1.337 had this code, it turned out unacceptable slow but
259 # it revealed a bug in Archive::Tar. Code is only here to hunt
260 # the bug again. It should never be enabled in published code.
261 # GDGraph3d-0.53 was an interesting case according to Larry
263 warn(">>>Bughunting code enabled<<< " x 20);
264 for $af ($tar->list_files) {
265 if ($af =~ m!^(/|\.\./)!) {
266 $CPAN::Frontend->mydie("ALERT: Archive contains ".
267 "illegal member [$af]");
269 $CPAN::Frontend->myprint("$af\n");
270 $tar->extract($af); # slow but effective for finding the bug
271 return if $CPAN::Signal;
274 for $af ($tar->list_files) {
275 if ($af =~ m!^(/|\.\./)!) {
276 $CPAN::Frontend->mydie("ALERT: Archive contains ".
277 "illegal member [$af]");
279 $CPAN::Frontend->myprint("$af\n");
281 return if $CPAN::Signal;
283 $tar->extract(@af) or
284 $CPAN::Frontend->mydie("Could not untar with Archive::Tar.");
287 Mac::BuildTools::convert_files([$tar->list_files], 1)
296 my $file = $self->{FILE};
297 if ($CPAN::META->has_inst("Archive::Zip")) {
298 # blueprint of the code from Archive::Zip::Tree::extractTree();
299 my $zip = Archive::Zip->new();
301 $status = $zip->read($file);
302 die "Read of file[$file] failed\n" if $status != Archive::Zip::AZ_OK();
303 $CPAN::META->debug("Successfully read file[$file]") if $CPAN::DEBUG;
304 my @members = $zip->members();
305 for my $member ( @members ) {
306 my $af = $member->fileName();
307 if ($af =~ m!^(/|\.\./)!) {
308 $CPAN::Frontend->mydie("ALERT: Archive contains ".
309 "illegal member [$af]");
311 $status = $member->extractToFileNamed( $af );
312 $CPAN::META->debug("af[$af]status[$status]") if $CPAN::DEBUG;
313 die "Extracting of file[$af] from zipfile[$file] failed\n" if
314 $status != Archive::Zip::AZ_OK();
315 return if $CPAN::Signal;
319 my $unzip = $CPAN::Config->{unzip} or
320 $CPAN::Frontend->mydie("Cannot unzip, no unzip program available");
321 my @system = ($unzip, $file);
322 return system(@system) == 0;