1 # -*- Mode: cperl; coding: utf-8; cperl-indent-level: 4 -*-
2 # vim: ts=4 sts=4 sw=4:
8 use vars qw($connect_to_internet_ok $Ua $Thesite $ThesiteURL $Themethod);
9 @CPAN::FTP::ISA = qw(CPAN::Debug);
16 #-> sub CPAN::FTP::ftp_statistics
17 # if they want to rewrite, they need to pass in a filehandle
20 my $locktype = $fh ? LOCK_EX : LOCK_SH;
21 $fh ||= FileHandle->new;
22 my $file = File::Spec->catfile($CPAN::Config->{cpan_home},"FTPstats.yml");
23 open $fh, "+>>$file" or $CPAN::Frontend->mydie("Could not open '$file': $!");
26 while (!CPAN::_flock($fh, $locktype|LOCK_NB)) {
27 $waitstart ||= localtime();
29 $CPAN::Frontend->mywarn("Waiting for a read lock on '$file' (since $waitstart)\n");
31 $CPAN::Frontend->mysleep($sleep);
34 } elsif ($sleep <=6) {
38 my $stats = eval { CPAN->_yaml_loadfile($file); };
41 if (ref $@ eq "CPAN::Exception::yaml_not_installed") {
42 $CPAN::Frontend->myprint("Warning (usually harmless): $@");
44 } elsif (ref $@ eq "CPAN::Exception::yaml_process_error") {
45 $CPAN::Frontend->mydie($@);
48 $CPAN::Frontend->mydie($@);
54 #-> sub CPAN::FTP::_mytime
56 if (CPAN->has_inst("Time::HiRes")) {
57 return Time::HiRes::time();
63 #-> sub CPAN::FTP::_new_stats
74 #-> sub CPAN::FTP::_add_to_statistics
75 sub _add_to_statistics {
76 my($self,$stats) = @_;
77 my $yaml_module = CPAN::_yaml_module();
78 $self->debug("yaml_module[$yaml_module]") if $CPAN::DEBUG;
79 if ($CPAN::META->has_inst($yaml_module)) {
80 $stats->{thesiteurl} = $ThesiteURL;
81 $stats->{end} = CPAN::FTP::_mytime();
82 my $fh = FileHandle->new;
86 @debug = $time if $sdebug;
87 my $fullstats = $self->_ftp_statistics($fh);
89 $fullstats->{history} ||= [];
90 push @debug, scalar @{$fullstats->{history}} if $sdebug;
91 push @debug, time if $sdebug;
92 push @{$fullstats->{history}}, $stats;
93 # YAML.pm 0.62 is unacceptably slow with 999;
94 # YAML::Syck 0.82 has no noticable performance problem with 999;
95 my $ftpstats_size = $CPAN::Config->{ftpstats_size} || 99;
96 my $ftpstats_period = $CPAN::Config->{ftpstats_period} || 14;
98 @{$fullstats->{history}} > $ftpstats_size
99 || $time - $fullstats->{history}[0]{start} > 86400*$ftpstats_period
101 shift @{$fullstats->{history}}
103 push @debug, scalar @{$fullstats->{history}} if $sdebug;
104 push @debug, time if $sdebug;
105 push @debug, scalar localtime($fullstats->{history}[0]{start}) if $sdebug;
106 # need no eval because if this fails, it is serious
107 my $sfile = File::Spec->catfile($CPAN::Config->{cpan_home},"FTPstats.yml");
108 CPAN->_yaml_dumpfile("$sfile.$$",$fullstats);
110 local $CPAN::DEBUG = 512; # FTP
112 CPAN->debug(sprintf("DEBUG history: before_read[%d]before[%d]at[%d]".
113 "after[%d]at[%d]oldest[%s]dumped backat[%d]",
117 # Win32 cannot rename a file to an existing filename
118 unlink($sfile) if ($^O eq 'MSWin32');
119 _copy_stat($sfile, "$sfile.$$") if -e $sfile;
120 rename "$sfile.$$", $sfile
121 or $CPAN::Frontend->mydie("Could not rename '$sfile.$$' to '$sfile': $!\n");
125 # Copy some stat information (owner, group, mode and) from one file to
127 # This is a utility function which might be moved to a utility repository.
128 #-> sub CPAN::FTP::_copy_stat
130 my($src, $dest) = @_;
131 my @stat = stat($src);
133 $CPAN::Frontend->mywarn("Can't stat '$src': $!\n");
138 chmod $stat[2], $dest
139 or $CPAN::Frontend->mywarn("Can't chmod '$dest' to " . sprintf("0%o", $stat[2]) . ": $!\n");
143 chown $stat[4], $stat[5], $dest
145 my $save_err = $!; # otherwise it's lost in the get... calls
146 $CPAN::Frontend->mywarn("Can't chown '$dest' to " .
147 (getpwuid($stat[4]))[0] . "/" .
148 (getgrgid($stat[5]))[0] . ": $save_err\n"
155 # if file is CHECKSUMS, suggest the place where we got the file to be
156 # checked from, maybe only for young files?
157 #-> sub CPAN::FTP::_recommend_url_for
158 sub _recommend_url_for {
159 my($self, $file) = @_;
160 my $urllist = $self->_get_urllist;
161 if ($file =~ s|/CHECKSUMS(.gz)?$||) {
162 my $fullstats = $self->_ftp_statistics();
163 my $history = $fullstats->{history} || [];
164 while (my $last = pop @$history) {
165 last if $last->{end} - time > 3600; # only young results are interesting
166 next unless $last->{file}; # dirname of nothing dies!
167 next unless $file eq File::Basename::dirname($last->{file});
168 return $last->{thesiteurl};
171 if ($CPAN::Config->{randomize_urllist}
173 rand(1) < $CPAN::Config->{randomize_urllist}
175 $urllist->[int rand scalar @$urllist];
181 #-> sub CPAN::FTP::_get_urllist
184 $CPAN::Config->{urllist} ||= [];
185 unless (ref $CPAN::Config->{urllist} eq 'ARRAY') {
186 $CPAN::Frontend->mywarn("Malformed urllist; ignoring. Configuration file corrupt?\n");
187 $CPAN::Config->{urllist} = [];
189 my @urllist = grep { defined $_ and length $_ } @{$CPAN::Config->{urllist}};
190 for my $u (@urllist) {
191 CPAN->debug("u[$u]") if $CPAN::DEBUG;
192 if (UNIVERSAL::can($u,"text")) {
193 $u->{TEXT} .= "/" unless substr($u->{TEXT},-1) eq "/";
195 $u .= "/" unless substr($u,-1) eq "/";
196 $u = CPAN::URL->new(TEXT => $u, FROM => "USER");
202 #-> sub CPAN::FTP::ftp_get ;
204 my($class,$host,$dir,$file,$target) = @_;
206 qq[Going to fetch file [$file] from dir [$dir]
207 on host [$host] as local [$target]\n]
209 my $ftp = Net::FTP->new($host);
211 $CPAN::Frontend->mywarn(" Could not connect to host '$host' with Net::FTP\n");
214 return 0 unless defined $ftp;
215 $ftp->debug(1) if $CPAN::DEBUG{'FTP'} & $CPAN::DEBUG;
216 $class->debug(qq[Going to login("anonymous","$Config::Config{cf_email}")]);
217 unless ( $ftp->login("anonymous",$Config::Config{'cf_email'}) ) {
218 my $msg = $ftp->message;
219 $CPAN::Frontend->mywarn(" Couldn't login on $host: $msg");
222 unless ( $ftp->cwd($dir) ) {
223 my $msg = $ftp->message;
224 $CPAN::Frontend->mywarn(" Couldn't cwd $dir: $msg");
228 $class->debug(qq[Going to ->get("$file","$target")\n]) if $CPAN::DEBUG;
229 unless ( $ftp->get($file,$target) ) {
230 my $msg = $ftp->message;
231 $CPAN::Frontend->mywarn(" Couldn't fetch $file from $host: $msg");
234 $ftp->quit; # it's ok if this fails
238 # If more accuracy is wanted/needed, Chris Leach sent me this patch...
240 # > *** /install/perl/live/lib/CPAN.pm- Wed Sep 24 13:08:48 1997
241 # > --- /tmp/cp Wed Sep 24 13:26:40 1997
243 # > *** 1562,1567 ****
244 # > --- 1562,1580 ----
245 # > return 1 if substr($url,0,4) eq "file";
246 # > return 1 unless $url =~ m|://([^/]+)|;
248 # > + my $proxy = $CPAN::Config->{'http_proxy'} || $ENV{'http_proxy'};
250 # > + $proxy =~ m|://([^/:]+)|;
252 # > + my $noproxy = $CPAN::Config->{'no_proxy'} || $ENV{'no_proxy'};
253 # > + if ($noproxy) {
254 # > + if ($host !~ /$noproxy$/) {
255 # > + $host = $proxy;
258 # > + $host = $proxy;
261 # > require Net::Ping;
262 # > return 1 unless $Net::Ping::VERSION >= 2;
266 #-> sub CPAN::FTP::localize ;
268 my($self,$file,$aslocal,$force) = @_;
270 Carp::croak( "Usage: ->localize(cpan_file,as_local_file[,$force])" )
271 unless defined $aslocal;
272 $self->debug("file[$file] aslocal[$aslocal] force[$force]")
275 if ($^O eq 'MacOS') {
276 # Comment by AK on 2000-09-03: Uniq short filenames would be
277 # available in CHECKSUMS file
278 my($name, $path) = File::Basename::fileparse($aslocal, '');
279 if (length($name) > 31) {
290 my $size = 31 - length($suf);
291 while (length($name) > $size) {
295 $aslocal = File::Spec->catfile($path, $name);
299 if (-f $aslocal && -r _ && !($force & 1)) {
301 if ($size = -s $aslocal) {
302 $self->debug("aslocal[$aslocal]size[$size]") if $CPAN::DEBUG;
305 # empty file from a previous unsuccessful attempt to download it
307 $CPAN::Frontend->mydie("Found a zero-length '$aslocal' that I ".
308 "could not remove.");
311 my($maybe_restore) = 0;
313 rename $aslocal, "$aslocal.bak$$";
317 my($aslocal_dir) = File::Basename::dirname($aslocal);
318 $self->mymkpath($aslocal_dir); # too early for file URLs / RT #28438
319 # Inheritance is not easier to manage than a few if/else branches
320 if ($CPAN::META->has_usable('LWP::UserAgent')) {
322 CPAN::LWP::UserAgent->config;
323 eval {$Ua = CPAN::LWP::UserAgent->new;}; # Why is has_usable still not fit enough?
325 $CPAN::Frontend->mywarn("CPAN::LWP::UserAgent->new dies with $@\n")
329 $Ua->proxy('ftp', $var)
330 if $var = $CPAN::Config->{ftp_proxy} || $ENV{ftp_proxy};
331 $Ua->proxy('http', $var)
332 if $var = $CPAN::Config->{http_proxy} || $ENV{http_proxy};
334 if $var = $CPAN::Config->{no_proxy} || $ENV{no_proxy};
338 for my $prx (qw(ftp_proxy http_proxy no_proxy)) {
339 $ENV{$prx} = $CPAN::Config->{$prx} if $CPAN::Config->{$prx};
342 # Try the list of urls for each single object. We keep a record
343 # where we did get a file from
344 my(@reordered,$last);
345 my $ccurllist = $self->_get_urllist;
346 $last = $#$ccurllist;
347 if ($force & 2) { # local cpans probably out of date, don't reorder
348 @reordered = (0..$last);
352 (substr($ccurllist->[$b],0,4) eq "file")
354 (substr($ccurllist->[$a],0,4) eq "file")
358 ($ccurllist->[$b] eq $ThesiteURL)
360 ($ccurllist->[$a] eq $ThesiteURL)
365 $self->debug("Themethod[$Themethod]reordered[@reordered]") if $CPAN::DEBUG;
371 ["dleasy", "http","defaultsites"],
372 ["dlhard", "http","defaultsites"],
373 ["dleasy", "ftp", "defaultsites"],
374 ["dlhard", "ftp", "defaultsites"],
375 ["dlhardest","", "defaultsites"],
378 @levels = grep {$_->[0] eq $Themethod} @all_levels;
379 push @levels, grep {$_->[0] ne $Themethod} @all_levels;
381 @levels = @all_levels;
383 @levels = qw/dleasy/ if $^O eq 'MacOS';
385 local $ENV{FTP_PASSIVE} =
386 exists $CPAN::Config->{ftp_passive} ?
387 $CPAN::Config->{ftp_passive} : 1;
389 my $stats = $self->_new_stats($file);
390 for ($CPAN::Config->{connect_to_internet_ok}) {
391 $connect_to_internet_ok = $_ if not defined $connect_to_internet_ok and defined $_;
393 LEVEL: for $levelno (0..$#levels) {
394 my $level_tuple = $levels[$levelno];
395 my($level,$scheme,$sitetag) = @$level_tuple;
396 my $defaultsites = $sitetag && $sitetag eq "defaultsites";
399 unless (defined $connect_to_internet_ok) {
400 $CPAN::Frontend->myprint(sprintf qq{
401 I would like to connect to one of the following sites to get '%s':
406 join("",map { " ".$_->text."\n" } @CPAN::Defaultsites),
408 my $answer = CPAN::Shell::colorable_makemaker_prompt("Is it OK to try to connect to the Internet?", "yes");
409 if ($answer =~ /^y/i) {
410 $connect_to_internet_ok = 1;
412 $connect_to_internet_ok = 0;
415 if ($connect_to_internet_ok) {
416 @urllist = @CPAN::Defaultsites;
419 $CPAN::Frontend->mywarn(sprintf qq{
421 You have not configured a urllist and did not allow to connect to the
422 internet. I will continue but it is very likely that we will face
423 problems. If this happens, please consider to call either
425 o conf init connect_to_internet_ok
429 Sleeping $sleep seconds now.
431 $CPAN::Frontend->mysleep($sleep);
435 my @host_seq = $level =~ /dleasy/ ?
436 @reordered : 0..$last; # reordered has file and $Thesiteurl first
437 @urllist = map { $ccurllist->[$_] } @host_seq;
439 $self->debug("synth. urllist[@urllist]") if $CPAN::DEBUG;
440 my $aslocal_tempfile = $aslocal . ".tmp" . $$;
441 if (my $recommend = $self->_recommend_url_for($file)) {
442 @urllist = grep { $_ ne $recommend } @urllist;
443 unshift @urllist, $recommend;
445 $self->debug("synth. urllist[@urllist]") if $CPAN::DEBUG;
446 $ret = $self->hostdlxxx($level,$scheme,\@urllist,$file,$aslocal_tempfile,$stats);
448 CPAN->debug("ret[$ret]aslocal[$aslocal]") if $CPAN::DEBUG;
449 if ($ret eq $aslocal_tempfile) {
450 # if we got it exactly as we asked for, only then we
452 rename $aslocal_tempfile, $aslocal
453 or $CPAN::Frontend->mydie("Error while trying to rename ".
454 "'$ret' to '$aslocal': $!");
459 # utime $now, $now, $aslocal; # too bad, if we do that, we
460 # might alter a local mirror
461 $self->debug("level[$level]") if $CPAN::DEBUG;
464 unlink $aslocal_tempfile;
465 last if $CPAN::Signal; # need to cleanup
469 $stats->{filesize} = -s $ret;
471 $self->debug("before _add_to_statistics") if $CPAN::DEBUG;
472 $self->_add_to_statistics($stats);
473 $self->debug("after _add_to_statistics") if $CPAN::DEBUG;
475 unlink "$aslocal.bak$$";
478 unless ($CPAN::Signal) {
481 if (@{$CPAN::Config->{urllist}}) {
483 qq{Please check, if the URLs I found in your configuration file \(}.
484 join(", ", @{$CPAN::Config->{urllist}}).
487 push @mess, qq{Your urllist is empty!};
489 push @mess, qq{The urllist can be edited.},
490 qq{E.g. with 'o conf urllist push ftp://myurl/'};
491 $CPAN::Frontend->mywarn(Text::Wrap::wrap("","","@mess"). "\n\n");
492 $CPAN::Frontend->mywarn("Could not fetch $file\n");
493 $CPAN::Frontend->mysleep(2);
495 if ($maybe_restore) {
496 rename "$aslocal.bak$$", $aslocal;
497 $CPAN::Frontend->myprint("Trying to get away with old file:\n" .
498 $self->ls($aslocal));
505 my($self, $aslocal_dir) = @_;
506 File::Path::mkpath($aslocal_dir);
507 $CPAN::Frontend->mywarn(qq{Warning: You are not allowed to write into }.
508 qq{directory "$aslocal_dir".
509 I\'ll continue, but if you encounter problems, they may be due
510 to insufficient permissions.\n}) unless -w $aslocal_dir;
518 $h = [ grep /^\Q$scheme\E:/, @$h ] if $scheme;
519 my $method = "host$level";
520 $self->$method($h, @_);
524 my($self,$stats,$method,$url) = @_;
525 push @{$stats->{attempts}}, {
534 my($self,$host_seq,$file,$aslocal,$stats) = @_;
536 HOSTEASY: for $ro_url (@$host_seq) {
537 $self->_set_attempt($stats,"dleasy",$ro_url);
538 my $url .= "$ro_url$file";
539 $self->debug("localizing perlish[$url]") if $CPAN::DEBUG;
540 if ($url =~ /^file:/) {
542 if ($CPAN::META->has_inst('URI::URL')) {
543 my $u = URI::URL->new($url);
545 } else { # works only on Unix, is poorly constructed, but
546 # hopefully better than nothing.
547 # RFC 1738 says fileurl BNF is
548 # fileurl = "file://" [ host | "localhost" ] "/" fpath
549 # Thanks to "Mark D. Baushke" <mdb@cisco.com> for
551 ($l = $url) =~ s|^file://[^/]*/|/|; # discard the host part
552 $l =~ s|^file:||; # assume they
556 if ! -f $l && $l =~ m|^/\w:|; # e.g. /P:
558 $self->debug("local file[$l]") if $CPAN::DEBUG;
559 if ( -f $l && -r _) {
560 $ThesiteURL = $ro_url;
563 if ($l =~ /(.+)\.gz$/) {
565 if ( -f $ungz && -r _) {
566 $ThesiteURL = $ro_url;
570 # Maybe mirror has compressed it?
572 $self->debug("found compressed $l.gz") if $CPAN::DEBUG;
573 eval { CPAN::Tarzip->new("$l.gz")->gunzip($aslocal) };
575 $ThesiteURL = $ro_url;
579 $CPAN::Frontend->mywarn("Could not find '$l'\n");
581 $self->debug("it was not a file URL") if $CPAN::DEBUG;
582 if ($CPAN::META->has_usable('LWP')) {
583 $CPAN::Frontend->myprint("Fetching with LWP:
587 CPAN::LWP::UserAgent->config;
588 eval { $Ua = CPAN::LWP::UserAgent->new; };
590 $CPAN::Frontend->mywarn("CPAN::LWP::UserAgent->new dies with $@\n");
593 my $res = $Ua->mirror($url, $aslocal);
594 if ($res->is_success) {
595 $ThesiteURL = $ro_url;
597 utime $now, $now, $aslocal; # download time is more
598 # important than upload
601 } elsif ($url !~ /\.gz(?!\n)\Z/) {
602 my $gzurl = "$url.gz";
603 $CPAN::Frontend->myprint("Fetching with LWP:
606 $res = $Ua->mirror($gzurl, "$aslocal.gz");
607 if ($res->is_success) {
608 if (eval {CPAN::Tarzip->new("$aslocal.gz")->gunzip($aslocal)}) {
609 $ThesiteURL = $ro_url;
614 $CPAN::Frontend->myprint(sprintf(
615 "LWP failed with code[%s] message[%s]\n",
619 # Alan Burlison informed me that in firewall environments
620 # Net::FTP can still succeed where LWP fails. So we do not
621 # skip Net::FTP anymore when LWP is available.
624 $CPAN::Frontend->mywarn(" LWP not available\n");
626 return if $CPAN::Signal;
627 if ($url =~ m|^ftp://(.*?)/(.*)/(.*)|) {
628 # that's the nice and easy way thanks to Graham
629 $self->debug("recognized ftp") if $CPAN::DEBUG;
630 my($host,$dir,$getfile) = ($1,$2,$3);
631 if ($CPAN::META->has_usable('Net::FTP')) {
633 $CPAN::Frontend->myprint("Fetching with Net::FTP:
636 $self->debug("getfile[$getfile]dir[$dir]host[$host]" .
637 "aslocal[$aslocal]") if $CPAN::DEBUG;
638 if (CPAN::FTP->ftp_get($host,$dir,$getfile,$aslocal)) {
639 $ThesiteURL = $ro_url;
642 if ($aslocal !~ /\.gz(?!\n)\Z/) {
643 my $gz = "$aslocal.gz";
644 $CPAN::Frontend->myprint("Fetching with Net::FTP
647 if (CPAN::FTP->ftp_get($host,
651 eval{CPAN::Tarzip->new($gz)->gunzip($aslocal)}
653 $ThesiteURL = $ro_url;
659 CPAN->debug("Net::FTP does not count as usable atm") if $CPAN::DEBUG;
663 UNIVERSAL::can($ro_url,"text")
665 $ro_url->{FROM} eq "USER"
667 ##address #17973: default URLs should not try to override
668 ##user-defined URLs just because LWP is not available
669 my $ret = $self->hostdlhard([$ro_url],$file,$aslocal,$stats);
672 return if $CPAN::Signal;
678 my($self,$host_seq,$file,$aslocal,$stats) = @_;
680 # Came back if Net::FTP couldn't establish connection (or
681 # failed otherwise) Maybe they are behind a firewall, but they
682 # gave us a socksified (or other) ftp program...
685 my($devnull) = $CPAN::Config->{devnull} || "";
687 my($aslocal_dir) = File::Basename::dirname($aslocal);
688 File::Path::mkpath($aslocal_dir);
689 HOSTHARD: for $ro_url (@$host_seq) {
690 $self->_set_attempt($stats,"dlhard",$ro_url);
691 my $url = "$ro_url$file";
692 my($proto,$host,$dir,$getfile);
694 # Courtesy Mark Conty mark_conty@cargill.com change from
695 # if ($url =~ m|^ftp://(.*?)/(.*)/(.*)|) {
697 if ($url =~ m|^([^:]+)://(.*?)/(.*)/(.*)|) {
699 ($proto,$host,$dir,$getfile) = ($1,$2,$3,$4);
701 next HOSTHARD; # who said, we could ftp anything except ftp?
703 next HOSTHARD if $proto eq "file"; # file URLs would have had
704 # success above. Likely a bogus URL
706 $self->debug("localizing funkyftpwise[$url]") if $CPAN::DEBUG;
708 # Try the most capable first and leave ncftp* for last as it only
710 my $proxy_vars = $self->_proxy_vars($ro_url);
711 DLPRG: for my $f (qw(curl wget lynx ncftpget ncftp)) {
712 my $funkyftp = CPAN::HandleConfig->safe_quote($CPAN::Config->{$f});
713 next unless defined $funkyftp;
714 next if $funkyftp =~ /^\s*$/;
716 my($asl_ungz, $asl_gz);
717 ($asl_ungz = $aslocal) =~ s/\.gz//;
718 $asl_gz = "$asl_ungz.gz";
720 my($src_switch) = "";
722 my($stdout_redir) = " > $asl_ungz";
724 $src_switch = " -source";
725 } elsif ($f eq "ncftp") {
727 } elsif ($f eq "wget") {
728 $src_switch = " -O $asl_ungz";
730 } elsif ($f eq 'curl') {
731 $src_switch = ' -L -f -s -S --netrc-optional';
732 if ($proxy_vars->{http_proxy}) {
733 $src_switch .= qq{ -U "$proxy_vars->{proxy_user}:$proxy_vars->{proxy_pass}" -x "$proxy_vars->{http_proxy}"};
737 if ($f eq "ncftpget") {
738 $chdir = "cd $aslocal_dir && ";
741 $CPAN::Frontend->myprint(
743 Trying with "$funkyftp$src_switch" to get
747 "$chdir$funkyftp$src_switch \"$url\" $devnull$stdout_redir";
748 $self->debug("system[$system]") if $CPAN::DEBUG;
749 my($wstatus) = system($system);
751 # lynx returns 0 when it fails somewhere
753 my $content = do { local *FH;
754 open FH, $asl_ungz or die;
757 if ($content =~ /^<.*(<title>[45]|Error [45])/si) {
758 $CPAN::Frontend->mywarn(qq{
759 No success, the file that lynx has downloaded looks like an error message:
762 $CPAN::Frontend->mysleep(1);
766 $CPAN::Frontend->myprint(qq{
767 No success, the file that lynx has downloaded is an empty file.
775 } elsif ($asl_ungz ne $aslocal) {
776 # test gzip integrity
777 if (eval{CPAN::Tarzip->new($asl_ungz)->gtest}) {
778 # e.g. foo.tar is gzipped --> foo.tar.gz
779 rename $asl_ungz, $aslocal;
781 eval{CPAN::Tarzip->new($asl_gz)->gzip($asl_ungz)};
784 $ThesiteURL = $ro_url;
786 } elsif ($url !~ /\.gz(?!\n)\Z/) {
788 -f $asl_ungz && -s _ == 0;
789 my $gz = "$aslocal.gz";
790 my $gzurl = "$url.gz";
791 $CPAN::Frontend->myprint(
793 Trying with "$funkyftp$src_switch" to get
796 my($system) = "$funkyftp$src_switch \"$url.gz\" $devnull > $asl_gz";
797 $self->debug("system[$system]") if $CPAN::DEBUG;
799 if (($wstatus = system($system)) == 0
803 # test gzip integrity
804 my $ct = eval{CPAN::Tarzip->new($asl_gz)};
805 if ($ct && $ct->gtest) {
806 $ct->gunzip($aslocal);
808 # somebody uncompressed file for us?
809 rename $asl_ungz, $aslocal;
811 $ThesiteURL = $ro_url;
814 unlink $asl_gz if -f $asl_gz;
817 my $estatus = $wstatus >> 8;
818 my $size = -f $aslocal ?
819 ", left\n$aslocal with size ".-s _ :
820 "\nWarning: expected file [$aslocal] doesn't exist";
821 $CPAN::Frontend->myprint(qq{
822 Function system("$system")
823 returned status $estatus (wstat $wstatus)$size
826 return if $CPAN::Signal;
827 } # transfer programs
831 #-> CPAN::FTP::_proxy_vars
835 my $http_proxy = $CPAN::Config->{'http_proxy'} || $ENV{'http_proxy'};
837 my($host) = $url =~ m|://([^/:]+)|;
839 my $noproxy = $CPAN::Config->{'no_proxy'} || $ENV{'no_proxy'} || "";
840 my @noproxy = split /\s*,\s*/, $noproxy;
842 DOMAIN: for my $domain (@noproxy) {
843 if ($host =~ /\Q$domain\E$/) { # cf. LWP::UserAgent
849 $CPAN::Frontend->mywarn(" Could not determine host from http_proxy '$http_proxy'\n");
853 &CPAN::LWP::UserAgent::get_proxy_credentials();
857 http_proxy => $http_proxy
866 my($self,$host_seq,$file,$aslocal,$stats) = @_;
868 return unless @$host_seq;
870 my($aslocal_dir) = File::Basename::dirname($aslocal);
871 File::Path::mkpath($aslocal_dir);
872 my $ftpbin = $CPAN::Config->{ftp};
873 unless ($ftpbin && length $ftpbin && MM->maybe_command($ftpbin)) {
874 $CPAN::Frontend->myprint("No external ftp command available\n\n");
877 $CPAN::Frontend->mywarn(qq{
878 As a last ressort we now switch to the external ftp command '$ftpbin'
881 Doing so often leads to problems that are hard to diagnose.
883 If you're the victim of such problems, please consider unsetting the
884 ftp config variable with
890 $CPAN::Frontend->mysleep(2);
891 HOSTHARDEST: for $ro_url (@$host_seq) {
892 $self->_set_attempt($stats,"dlhardest",$ro_url);
893 my $url = "$ro_url$file";
894 $self->debug("localizing ftpwise[$url]") if $CPAN::DEBUG;
895 unless ($url =~ m|^ftp://(.*?)/(.*)/(.*)|) {
898 my($host,$dir,$getfile) = ($1,$2,$3);
900 my($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,
901 $ctime,$blksize,$blocks) = stat($aslocal);
902 $timestamp = $mtime ||= 0;
903 my($netrc) = CPAN::FTP::netrc->new;
904 my($netrcfile) = $netrc->netrc;
905 my($verbose) = $CPAN::DEBUG{'FTP'} & $CPAN::DEBUG ? " -v" : "";
906 my $targetfile = File::Basename::basename($aslocal);
912 map("cd $_", split /\//, $dir), # RFC 1738
914 "get $getfile $targetfile",
918 CPAN->debug("No ~/.netrc file found") if $CPAN::DEBUG;
919 } elsif ($netrc->hasdefault || $netrc->contains($host)) {
920 CPAN->debug(sprintf("hasdef[%d]cont($host)[%d]",
922 $netrc->contains($host))) if $CPAN::DEBUG;
923 if ($netrc->protected) {
924 my $dialog = join "", map { " $_\n" } @dialog;
926 if ($netrc->contains($host)) {
927 $netrc_explain = "Relying that your .netrc entry for '$host' ".
930 $netrc_explain = "Relying that your default .netrc entry ".
933 $CPAN::Frontend->myprint(qq{
934 Trying with external ftp to get
937 Going to send the dialog
941 $self->talk_ftp("$ftpbin$verbose $host",
943 ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,
944 $atime,$mtime,$ctime,$blksize,$blocks) = stat($aslocal);
946 if ($mtime > $timestamp) {
947 $CPAN::Frontend->myprint("GOT $aslocal\n");
948 $ThesiteURL = $ro_url;
951 $CPAN::Frontend->myprint("Hmm... Still failed!\n");
953 return if $CPAN::Signal;
955 $CPAN::Frontend->mywarn(qq{Your $netrcfile is not }.
956 qq{correctly protected.\n});
959 $CPAN::Frontend->mywarn("Your ~/.netrc neither contains $host
960 nor does it have a default entry\n");
963 # OK, they don't have a valid ~/.netrc. Use 'ftp -n'
964 # then and login manually to host, using e-mail as
966 $CPAN::Frontend->myprint(qq{Issuing "$ftpbin$verbose -n"\n});
970 "user anonymous $Config::Config{'cf_email'}"
972 my $dialog = join "", map { " $_\n" } @dialog;
973 $CPAN::Frontend->myprint(qq{
974 Trying with external ftp to get
976 Going to send the dialog
980 $self->talk_ftp("$ftpbin$verbose -n", @dialog);
981 ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,
982 $atime,$mtime,$ctime,$blksize,$blocks) = stat($aslocal);
984 if ($mtime > $timestamp) {
985 $CPAN::Frontend->myprint("GOT $aslocal\n");
986 $ThesiteURL = $ro_url;
989 $CPAN::Frontend->myprint("Bad luck... Still failed!\n");
991 return if $CPAN::Signal;
992 $CPAN::Frontend->mywarn("Can't access URL $url.\n\n");
993 $CPAN::Frontend->mysleep(2);
999 my($self,$command,@dialog) = @_;
1000 my $fh = FileHandle->new;
1001 $fh->open("|$command") or die "Couldn't open ftp: $!";
1002 foreach (@dialog) { $fh->print("$_\n") }
1003 $fh->close; # Wait for process to complete
1005 my $estatus = $wstatus >> 8;
1006 $CPAN::Frontend->myprint(qq{
1007 Subprocess "|$command"
1008 returned status $estatus (wstat $wstatus)
1012 # find2perl needs modularization, too, all the following is stolen
1016 my($self,$name) = @_;
1017 my ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$sizemm,
1018 $atime,$mtime,$ctime,$blksize,$blocks) = lstat($name);
1020 my($perms,%user,%group);
1024 $blocks = int(($blocks + 1) / 2);
1027 $blocks = int(($sizemm + 1023) / 1024);
1030 if (-f _) { $perms = '-'; }
1031 elsif (-d _) { $perms = 'd'; }
1032 elsif (-c _) { $perms = 'c'; $sizemm = &sizemm; }
1033 elsif (-b _) { $perms = 'b'; $sizemm = &sizemm; }
1034 elsif (-p _) { $perms = 'p'; }
1035 elsif (-S _) { $perms = 's'; }
1036 else { $perms = 'l'; $pname .= ' -> ' . readlink($_); }
1038 my(@rwx) = ('---','--x','-w-','-wx','r--','r-x','rw-','rwx');
1039 my(@moname) = qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec);
1040 my $tmpmode = $mode;
1041 my $tmp = $rwx[$tmpmode & 7];
1043 $tmp = $rwx[$tmpmode & 7] . $tmp;
1045 $tmp = $rwx[$tmpmode & 7] . $tmp;
1046 substr($tmp,2,1) =~ tr/-x/Ss/ if -u _;
1047 substr($tmp,5,1) =~ tr/-x/Ss/ if -g _;
1048 substr($tmp,8,1) =~ tr/-x/Tt/ if -k _;
1051 my $user = $user{$uid} || $uid; # too lazy to implement lookup
1052 my $group = $group{$gid} || $gid;
1054 my($sec,$min,$hour,$mday,$mon,$year) = localtime($mtime);
1056 my($moname) = $moname[$mon];
1057 if (-M _ > 365.25 / 2) {
1058 $timeyear = $year + 1900;
1061 $timeyear = sprintf("%02d:%02d", $hour, $min);
1064 sprintf "%5lu %4ld %-10s %2d %-8s %-8s %8s %s %2d %5s %s\n",