1 # -*- Mode: cperl; coding: utf-8; cperl-indent-level: 2 -*-
4 use vars qw($VERSION @ISA $BUGHUNTING);
7 $VERSION = sprintf "%.2f", substr(q$Rev: 336 $,4)/100;
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 my $read = $self->{FILE};
93 # After I had reread the documentation in zlib.h, I discovered that
94 # uncompressed files do not lead to an gzerror (anymore?).
95 if ( $CPAN::META->has_inst("Compress::Zlib") ) {
98 my $gz = Compress::Zlib::gzopen($read, "rb")
99 or $CPAN::Frontend->mydie(sprintf("Cannot gzopen %s: %s\n",
101 $Compress::Zlib::gzerrno));
102 while ($gz->gzread($buffer) > 0 ){
103 $len += length($buffer);
106 my $err = $gz->gzerror;
107 my $success = ! $err || $err == Compress::Zlib::Z_STREAM_END();
108 if ($len == -s $read){
110 CPAN->debug("hit an uncompressed file") if $CPAN::DEBUG;
113 CPAN->debug("err[$err]success[$success]") if $CPAN::DEBUG;
116 return system(qq{$self->{UNGZIPPRG} -dt "$read"})==0;
122 my($class,$file) = @_;
124 $class->debug("file[$file]");
125 if ($CPAN::META->has_inst("Compress::Zlib")) {
126 my $gz = Compress::Zlib::gzopen($file,"rb") or
127 die "Could not gzopen $file";
128 $ret = bless {GZ => $gz}, $class;
130 my $pipe = "$CPAN::Config->{gzip} -dc $file |";
131 my $fh = FileHandle->new($pipe) or die "Could not pipe[$pipe]: $!";
133 $ret = bless {FH => $fh}, $class;
141 if (exists $self->{GZ}) {
142 my $gz = $self->{GZ};
143 my($line,$bytesread);
144 $bytesread = $gz->gzreadline($line);
145 return undef if $bytesread <= 0;
148 my $fh = $self->{FH};
155 my($self,$ref,$length,$offset) = @_;
156 die "read with offset not implemented" if defined $offset;
157 if (exists $self->{GZ}) {
158 my $gz = $self->{GZ};
159 my $byteread = $gz->gzread($$ref,$length);# 30eaf79e8b446ef52464b5422da328a8
162 my $fh = $self->{FH};
163 return read($fh,$$ref,$length);
170 if (exists $self->{GZ}) {
171 my $gz = $self->{GZ};
172 $gz->gzclose() if defined $gz; # hard to say if it is allowed
173 # to be undef ever. AK, 2000-09
175 my $fh = $self->{FH};
176 $fh->close if defined $fh;
184 my $file = $self->{FILE};
187 if (0) { # makes changing order easier
188 } elsif ($BUGHUNTING){
190 } elsif (MM->maybe_command($self->{UNGZIPPRG})
192 MM->maybe_command($CPAN::Config->{'tar'})) {
193 # should be default until Archive::Tar handles bzip2
196 $CPAN::META->has_inst("Archive::Tar")
198 $CPAN::META->has_inst("Compress::Zlib") ) {
199 if ($file =~ /\.bz2$/) {
200 $CPAN::Frontend->mydie(qq{
201 Archive::Tar lacks support for bz2. Can't continue.
206 $CPAN::Frontend->mydie(qq{
207 CPAN.pm needs either the external programs tar, gzip and bzip2
208 installed. Can't continue.
211 if ($prefer==1) { # 1 => external gzip+tar
213 my $is_compressed = $self->gtest();
214 if ($is_compressed) {
215 $system = qq{$self->{UNGZIPPRG} -dc }.
216 qq{< "$file" | $CPAN::Config->{tar} xvf -};
218 $system = qq{$CPAN::Config->{tar} xvf "$file"};
220 if (system($system) != 0) {
221 # people find the most curious tar binaries that cannot handle
223 if ($is_compressed) {
224 (my $ungzf = $file) =~ s/\.gz(?!\n)\Z//;
225 $ungzf = File::Basename::basename($ungzf);
226 my $ct = CPAN::Tarzip->new($file);
227 if ($ct->gunzip($ungzf)) {
228 $CPAN::Frontend->myprint(qq{Uncompressed $file successfully\n});
230 $CPAN::Frontend->mydie(qq{Couldn\'t uncompress $file\n});
234 $system = qq{$CPAN::Config->{tar} xvf "$file"};
235 $CPAN::Frontend->myprint(qq{Using Tar:$system:\n});
236 if (system($system)==0) {
237 $CPAN::Frontend->myprint(qq{Untarred $file successfully\n});
239 $CPAN::Frontend->mydie(qq{Couldn\'t untar $file\n});
245 } elsif ($prefer==2) { # 2 => modules
246 my $tar = Archive::Tar->new($file,1);
247 my $af; # archive file
250 # RCS 1.337 had this code, it turned out unacceptable slow but
251 # it revealed a bug in Archive::Tar. Code is only here to hunt
252 # the bug again. It should never be enabled in published code.
253 # GDGraph3d-0.53 was an interesting case according to Larry
255 warn(">>>Bughunting code enabled<<< " x 20);
256 for $af ($tar->list_files) {
257 if ($af =~ m!^(/|\.\./)!) {
258 $CPAN::Frontend->mydie("ALERT: Archive contains ".
259 "illegal member [$af]");
261 $CPAN::Frontend->myprint("$af\n");
262 $tar->extract($af); # slow but effective for finding the bug
263 return if $CPAN::Signal;
266 for $af ($tar->list_files) {
267 if ($af =~ m!^(/|\.\./)!) {
268 $CPAN::Frontend->mydie("ALERT: Archive contains ".
269 "illegal member [$af]");
271 $CPAN::Frontend->myprint("$af\n");
273 return if $CPAN::Signal;
275 $tar->extract(@af) or
276 $CPAN::Frontend->mydie("Could not untar with Archive::Tar.");
279 Mac::BuildTools::convert_files([$tar->list_files], 1)
288 my $file = $self->{FILE};
289 if ($CPAN::META->has_inst("Archive::Zip")) {
290 # blueprint of the code from Archive::Zip::Tree::extractTree();
291 my $zip = Archive::Zip->new();
293 $status = $zip->read($file);
294 die "Read of file[$file] failed\n" if $status != Archive::Zip::AZ_OK();
295 $CPAN::META->debug("Successfully read file[$file]") if $CPAN::DEBUG;
296 my @members = $zip->members();
297 for my $member ( @members ) {
298 my $af = $member->fileName();
299 if ($af =~ m!^(/|\.\./)!) {
300 $CPAN::Frontend->mydie("ALERT: Archive contains ".
301 "illegal member [$af]");
303 $status = $member->extractToFileNamed( $af );
304 $CPAN::META->debug("af[$af]status[$status]") if $CPAN::DEBUG;
305 die "Extracting of file[$af] from zipfile[$file] failed\n" if
306 $status != Archive::Zip::AZ_OK();
307 return if $CPAN::Signal;
311 my $unzip = $CPAN::Config->{unzip} or
312 $CPAN::Frontend->mydie("Cannot unzip, no unzip program available");
313 my @system = ($unzip, $file);
314 return system(@system) == 0;