1 # -*- Mode: cperl; coding: utf-8; cperl-indent-level: 2 -*-
4 use vars qw($VERSION @ISA $BUGHUNTING);
7 $VERSION = sprintf "%.2f", substr(q$Rev: 319 $,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;
17 $CPAN::Frontend->mydie("file[$file] doesn't match /\\.(bz2|gz|zip|tgz)\$/")
18 unless $file =~ /\.(bz2|gz|zip|tgz)$/i;
19 my $me = { FILE => $file };
21 } elsif ($file =~ /\.bz2$/i) {
22 unless ($me->{UNGZIPPRG} = $CPAN::Config->{bzip2}) {
24 if ($CPAN::META->has_inst("File::Which")) {
25 $bzip2 = File::Which::which("bzip2");
28 $me->{UNGZIPPRG} = $bzip2;
30 $CPAN::Frontend->mydie(qq{
31 CPAN.pm needs the external program bzip2 in order to handle '$file'.
32 Please install it now and run 'o conf init' to register it as external
38 # yes, we let gzip figure it out in *any* other case
39 $me->{UNGZIPPRG} = $CPAN::Config->{gzip};
46 my $write = $self->{FILE};
47 if ($CPAN::META->has_inst("Compress::Zlib")) {
49 $fhw = FileHandle->new($read)
50 or $CPAN::Frontend->mydie("Could not open $read: $!");
52 my $gz = Compress::Zlib::gzopen($write, "wb")
53 or $CPAN::Frontend->mydie("Cannot gzopen $write: $! (pwd is $cwd)\n");
55 while read($fhw,$buffer,4096) > 0 ;
60 system(qq{$self->{UNGZIPPRG} -c "$read" > "$write"})==0;
66 my($self,$write) = @_;
67 my $read = $self->{FILE};
68 if ($CPAN::META->has_inst("Compress::Zlib")) {
70 $fhw = FileHandle->new(">$write")
71 or $CPAN::Frontend->mydie("Could not open >$write: $!");
72 my $gz = Compress::Zlib::gzopen($read, "rb")
73 or $CPAN::Frontend->mydie("Cannot gzopen $read: $!\n");
75 while $gz->gzread($buffer) > 0 ;
76 $CPAN::Frontend->mydie("Error reading from $read: $!\n")
77 if $gz->gzerror != Compress::Zlib::Z_STREAM_END();
82 system(qq{$self->{UNGZIPPRG} -dc "$read" > "$write"})==0;
89 my $read = $self->{FILE};
90 # After I had reread the documentation in zlib.h, I discovered that
91 # uncompressed files do not lead to an gzerror (anymore?).
92 if ( $CPAN::META->has_inst("Compress::Zlib") ) {
95 my $gz = Compress::Zlib::gzopen($read, "rb")
96 or $CPAN::Frontend->mydie(sprintf("Cannot gzopen %s: %s\n",
98 $Compress::Zlib::gzerrno));
99 while ($gz->gzread($buffer) > 0 ){
100 $len += length($buffer);
103 my $err = $gz->gzerror;
104 my $success = ! $err || $err == Compress::Zlib::Z_STREAM_END();
105 if ($len == -s $read){
107 CPAN->debug("hit an uncompressed file") if $CPAN::DEBUG;
110 CPAN->debug("err[$err]success[$success]") if $CPAN::DEBUG;
113 return system(qq{$self->{UNGZIPPRG} -dt "$read"})==0;
119 my($class,$file) = @_;
121 $class->debug("file[$file]");
122 if ($CPAN::META->has_inst("Compress::Zlib")) {
123 my $gz = Compress::Zlib::gzopen($file,"rb") or
124 die "Could not gzopen $file";
125 $ret = bless {GZ => $gz}, $class;
127 my $pipe = "$CPAN::Config->{gzip} -dc $file |";
128 my $fh = FileHandle->new($pipe) or die "Could not pipe[$pipe]: $!";
130 $ret = bless {FH => $fh}, $class;
138 if (exists $self->{GZ}) {
139 my $gz = $self->{GZ};
140 my($line,$bytesread);
141 $bytesread = $gz->gzreadline($line);
142 return undef if $bytesread <= 0;
145 my $fh = $self->{FH};
152 my($self,$ref,$length,$offset) = @_;
153 die "read with offset not implemented" if defined $offset;
154 if (exists $self->{GZ}) {
155 my $gz = $self->{GZ};
156 my $byteread = $gz->gzread($$ref,$length);# 30eaf79e8b446ef52464b5422da328a8
159 my $fh = $self->{FH};
160 return read($fh,$$ref,$length);
167 if (exists $self->{GZ}) {
168 my $gz = $self->{GZ};
169 $gz->gzclose() if defined $gz; # hard to say if it is allowed
170 # to be undef ever. AK, 2000-09
172 my $fh = $self->{FH};
173 $fh->close if defined $fh;
181 my $file = $self->{FILE};
184 if (0) { # makes changing order easier
185 } elsif ($BUGHUNTING){
187 } elsif (MM->maybe_command($self->{UNGZIPPRG})
189 MM->maybe_command($CPAN::Config->{'tar'})) {
190 # should be default until Archive::Tar handles bzip2
193 $CPAN::META->has_inst("Archive::Tar")
195 $CPAN::META->has_inst("Compress::Zlib") ) {
196 if ($file =~ /\.bz2$/) {
197 $CPAN::Frontend->mydie(qq{
198 Archive::Tar lacks support for bz2. Can't continue.
203 $CPAN::Frontend->mydie(qq{
204 CPAN.pm needs either the external programs tar, gzip and bzip2
205 installed. Can't continue.
208 if ($prefer==1) { # 1 => external gzip+tar
210 my $is_compressed = $self->gtest();
211 if ($is_compressed) {
212 $system = qq{$self->{UNGZIPPRG} -dc }.
213 qq{< "$file" | $CPAN::Config->{tar} xvf -};
215 $system = qq{$CPAN::Config->{tar} xvf "$file"};
217 if (system($system) != 0) {
218 # people find the most curious tar binaries that cannot handle
220 if ($is_compressed) {
221 (my $ungzf = $file) =~ s/\.gz(?!\n)\Z//;
222 $ungzf = File::Basename::basename($ungzf);
223 my $ct = CPAN::Tarzip->new($file);
224 if ($ct->gunzip($ungzf)) {
225 $CPAN::Frontend->myprint(qq{Uncompressed $file successfully\n});
227 $CPAN::Frontend->mydie(qq{Couldn\'t uncompress $file\n});
231 $system = qq{$CPAN::Config->{tar} xvf "$file"};
232 $CPAN::Frontend->myprint(qq{Using Tar:$system:\n});
233 if (system($system)==0) {
234 $CPAN::Frontend->myprint(qq{Untarred $file successfully\n});
236 $CPAN::Frontend->mydie(qq{Couldn\'t untar $file\n});
242 } elsif ($prefer==2) { # 2 => modules
243 my $tar = Archive::Tar->new($file,1);
244 my $af; # archive file
247 # RCS 1.337 had this code, it turned out unacceptable slow but
248 # it revealed a bug in Archive::Tar. Code is only here to hunt
249 # the bug again. It should never be enabled in published code.
250 # GDGraph3d-0.53 was an interesting case according to Larry
252 warn(">>>Bughunting code enabled<<< " x 20);
253 for $af ($tar->list_files) {
254 if ($af =~ m!^(/|\.\./)!) {
255 $CPAN::Frontend->mydie("ALERT: Archive contains ".
256 "illegal member [$af]");
258 $CPAN::Frontend->myprint("$af\n");
259 $tar->extract($af); # slow but effective for finding the bug
260 return if $CPAN::Signal;
263 for $af ($tar->list_files) {
264 if ($af =~ m!^(/|\.\./)!) {
265 $CPAN::Frontend->mydie("ALERT: Archive contains ".
266 "illegal member [$af]");
268 $CPAN::Frontend->myprint("$af\n");
270 return if $CPAN::Signal;
272 $tar->extract(@af) or
273 $CPAN::Frontend->mydie("Could not untar with Archive::Tar.");
276 Mac::BuildTools::convert_files([$tar->list_files], 1)
285 my $file = $self->{FILE};
286 if ($CPAN::META->has_inst("Archive::Zip")) {
287 # blueprint of the code from Archive::Zip::Tree::extractTree();
288 my $zip = Archive::Zip->new();
290 $status = $zip->read($file);
291 die "Read of file[$file] failed\n" if $status != Archive::Zip::AZ_OK();
292 $CPAN::META->debug("Successfully read file[$file]") if $CPAN::DEBUG;
293 my @members = $zip->members();
294 for my $member ( @members ) {
295 my $af = $member->fileName();
296 if ($af =~ m!^(/|\.\./)!) {
297 $CPAN::Frontend->mydie("ALERT: Archive contains ".
298 "illegal member [$af]");
300 $status = $member->extractToFileNamed( $af );
301 $CPAN::META->debug("af[$af]status[$status]") if $CPAN::DEBUG;
302 die "Extracting of file[$af] from zipfile[$file] failed\n" if
303 $status != Archive::Zip::AZ_OK();
304 return if $CPAN::Signal;
308 my $unzip = $CPAN::Config->{unzip} or
309 $CPAN::Frontend->mydie("Cannot unzip, no unzip program available");
310 my @system = ($unzip, $file);
311 return system(@system) == 0;