1 # -*- Mode: cperl; coding: utf-8; cperl-indent-level: 4 -*-
2 # vim: ts=4 sts=4 sw=4:
7 use File::Basename qw(dirname);
8 use File::Path qw(mkpath);
10 use vars qw($connect_to_internet_ok $Ua $Thesite $ThesiteURL $Themethod);
11 @CPAN::FTP::ISA = qw(CPAN::Debug);
18 #-> sub CPAN::FTP::ftp_statistics
19 # if they want to rewrite, they need to pass in a filehandle
22 my $locktype = $fh ? LOCK_EX : LOCK_SH;
23 $fh ||= FileHandle->new;
24 my $file = File::Spec->catfile($CPAN::Config->{cpan_home},"FTPstats.yml");
26 open $fh, "+>>$file" or $CPAN::Frontend->mydie("Could not open '$file': $!");
29 while (!CPAN::_flock($fh, $locktype|LOCK_NB)) {
30 $waitstart ||= localtime();
32 $CPAN::Frontend->mywarn("Waiting for a read lock on '$file' (since $waitstart)\n");
34 $CPAN::Frontend->mysleep($sleep);
37 } elsif ($sleep <=6) {
41 my $stats = eval { CPAN->_yaml_loadfile($file); };
44 if (ref $@ eq "CPAN::Exception::yaml_not_installed") {
45 $CPAN::Frontend->myprint("Warning (usually harmless): $@");
47 } elsif (ref $@ eq "CPAN::Exception::yaml_process_error") {
48 $CPAN::Frontend->mydie($@);
51 $CPAN::Frontend->mydie($@);
57 #-> sub CPAN::FTP::_mytime
59 if (CPAN->has_inst("Time::HiRes")) {
60 return Time::HiRes::time();
66 #-> sub CPAN::FTP::_new_stats
77 #-> sub CPAN::FTP::_add_to_statistics
78 sub _add_to_statistics {
79 my($self,$stats) = @_;
80 my $yaml_module = CPAN::_yaml_module();
81 $self->debug("yaml_module[$yaml_module]") if $CPAN::DEBUG;
82 if ($CPAN::META->has_inst($yaml_module)) {
83 $stats->{thesiteurl} = $ThesiteURL;
84 $stats->{end} = CPAN::FTP::_mytime();
85 my $fh = FileHandle->new;
89 @debug = $time if $sdebug;
90 my $fullstats = $self->_ftp_statistics($fh);
92 $fullstats->{history} ||= [];
93 push @debug, scalar @{$fullstats->{history}} if $sdebug;
94 push @debug, time if $sdebug;
95 push @{$fullstats->{history}}, $stats;
96 # YAML.pm 0.62 is unacceptably slow with 999;
97 # YAML::Syck 0.82 has no noticable performance problem with 999;
98 my $ftpstats_size = $CPAN::Config->{ftpstats_size} || 99;
99 my $ftpstats_period = $CPAN::Config->{ftpstats_period} || 14;
101 @{$fullstats->{history}} > $ftpstats_size
102 || $time - $fullstats->{history}[0]{start} > 86400*$ftpstats_period
104 shift @{$fullstats->{history}}
106 push @debug, scalar @{$fullstats->{history}} if $sdebug;
107 push @debug, time if $sdebug;
108 push @debug, scalar localtime($fullstats->{history}[0]{start}) if $sdebug;
109 # need no eval because if this fails, it is serious
110 my $sfile = File::Spec->catfile($CPAN::Config->{cpan_home},"FTPstats.yml");
111 CPAN->_yaml_dumpfile("$sfile.$$",$fullstats);
113 local $CPAN::DEBUG = 512; # FTP
115 CPAN->debug(sprintf("DEBUG history: before_read[%d]before[%d]at[%d]".
116 "after[%d]at[%d]oldest[%s]dumped backat[%d]",
120 # Win32 cannot rename a file to an existing filename
121 unlink($sfile) if ($^O eq 'MSWin32');
122 _copy_stat($sfile, "$sfile.$$") if -e $sfile;
123 rename "$sfile.$$", $sfile
124 or $CPAN::Frontend->mydie("Could not rename '$sfile.$$' to '$sfile': $!\n");
128 # Copy some stat information (owner, group, mode and) from one file to
130 # This is a utility function which might be moved to a utility repository.
131 #-> sub CPAN::FTP::_copy_stat
133 my($src, $dest) = @_;
134 my @stat = stat($src);
136 $CPAN::Frontend->mywarn("Can't stat '$src': $!\n");
141 chmod $stat[2], $dest
142 or $CPAN::Frontend->mywarn("Can't chmod '$dest' to " . sprintf("0%o", $stat[2]) . ": $!\n");
146 chown $stat[4], $stat[5], $dest
148 my $save_err = $!; # otherwise it's lost in the get... calls
149 $CPAN::Frontend->mywarn("Can't chown '$dest' to " .
150 (getpwuid($stat[4]))[0] . "/" .
151 (getgrgid($stat[5]))[0] . ": $save_err\n"
158 # if file is CHECKSUMS, suggest the place where we got the file to be
159 # checked from, maybe only for young files?
160 #-> sub CPAN::FTP::_recommend_url_for
161 sub _recommend_url_for {
162 my($self, $file) = @_;
163 my $urllist = $self->_get_urllist;
164 if ($file =~ s|/CHECKSUMS(.gz)?$||) {
165 my $fullstats = $self->_ftp_statistics();
166 my $history = $fullstats->{history} || [];
167 while (my $last = pop @$history) {
168 last if $last->{end} - time > 3600; # only young results are interesting
169 next unless $last->{file}; # dirname of nothing dies!
170 next unless $file eq dirname($last->{file});
171 return $last->{thesiteurl};
174 if ($CPAN::Config->{randomize_urllist}
176 rand(1) < $CPAN::Config->{randomize_urllist}
178 $urllist->[int rand scalar @$urllist];
184 #-> sub CPAN::FTP::_get_urllist
187 $CPAN::Config->{urllist} ||= [];
188 unless (ref $CPAN::Config->{urllist} eq 'ARRAY') {
189 $CPAN::Frontend->mywarn("Malformed urllist; ignoring. Configuration file corrupt?\n");
190 $CPAN::Config->{urllist} = [];
192 my @urllist = grep { defined $_ and length $_ } @{$CPAN::Config->{urllist}};
193 for my $u (@urllist) {
194 CPAN->debug("u[$u]") if $CPAN::DEBUG;
195 if (UNIVERSAL::can($u,"text")) {
196 $u->{TEXT} .= "/" unless substr($u->{TEXT},-1) eq "/";
198 $u .= "/" unless substr($u,-1) eq "/";
199 $u = CPAN::URL->new(TEXT => $u, FROM => "USER");
205 #-> sub CPAN::FTP::ftp_get ;
207 my($class,$host,$dir,$file,$target) = @_;
209 qq[Going to fetch file [$file] from dir [$dir]
210 on host [$host] as local [$target]\n]
212 my $ftp = Net::FTP->new($host);
214 $CPAN::Frontend->mywarn(" Could not connect to host '$host' with Net::FTP\n");
217 return 0 unless defined $ftp;
218 $ftp->debug(1) if $CPAN::DEBUG{'FTP'} & $CPAN::DEBUG;
219 $class->debug(qq[Going to login("anonymous","$Config::Config{cf_email}")]);
220 unless ( $ftp->login("anonymous",$Config::Config{'cf_email'}) ) {
221 my $msg = $ftp->message;
222 $CPAN::Frontend->mywarn(" Couldn't login on $host: $msg");
225 unless ( $ftp->cwd($dir) ) {
226 my $msg = $ftp->message;
227 $CPAN::Frontend->mywarn(" Couldn't cwd $dir: $msg");
231 $class->debug(qq[Going to ->get("$file","$target")\n]) if $CPAN::DEBUG;
232 unless ( $ftp->get($file,$target) ) {
233 my $msg = $ftp->message;
234 $CPAN::Frontend->mywarn(" Couldn't fetch $file from $host: $msg");
237 $ftp->quit; # it's ok if this fails
241 # If more accuracy is wanted/needed, Chris Leach sent me this patch...
243 # > *** /install/perl/live/lib/CPAN.pm- Wed Sep 24 13:08:48 1997
244 # > --- /tmp/cp Wed Sep 24 13:26:40 1997
246 # > *** 1562,1567 ****
247 # > --- 1562,1580 ----
248 # > return 1 if substr($url,0,4) eq "file";
249 # > return 1 unless $url =~ m|://([^/]+)|;
251 # > + my $proxy = $CPAN::Config->{'http_proxy'} || $ENV{'http_proxy'};
253 # > + $proxy =~ m|://([^/:]+)|;
255 # > + my $noproxy = $CPAN::Config->{'no_proxy'} || $ENV{'no_proxy'};
256 # > + if ($noproxy) {
257 # > + if ($host !~ /$noproxy$/) {
258 # > + $host = $proxy;
261 # > + $host = $proxy;
264 # > require Net::Ping;
265 # > return 1 unless $Net::Ping::VERSION >= 2;
269 #-> sub CPAN::FTP::localize ;
271 my($self,$file,$aslocal,$force) = @_;
273 Carp::croak( "Usage: ->localize(cpan_file,as_local_file[,$force])" )
274 unless defined $aslocal;
277 my $longmess = Carp::longmess();
278 $self->debug("file[$file] aslocal[$aslocal] force[$force] carplongmess[$longmess]");
280 if ($^O eq 'MacOS') {
281 # Comment by AK on 2000-09-03: Uniq short filenames would be
282 # available in CHECKSUMS file
283 my($name, $path) = File::Basename::fileparse($aslocal, '');
284 if (length($name) > 31) {
295 my $size = 31 - length($suf);
296 while (length($name) > $size) {
300 $aslocal = File::Spec->catfile($path, $name);
304 if (-f $aslocal && -r _ && !($force & 1)) {
306 if ($size = -s $aslocal) {
307 $self->debug("aslocal[$aslocal]size[$size]") if $CPAN::DEBUG;
310 # empty file from a previous unsuccessful attempt to download it
312 $CPAN::Frontend->mydie("Found a zero-length '$aslocal' that I ".
313 "could not remove.");
316 my($maybe_restore) = 0;
318 rename $aslocal, "$aslocal.bak$$";
322 my($aslocal_dir) = dirname($aslocal);
323 # Inheritance is not easier to manage than a few if/else branches
324 if ($CPAN::META->has_usable('LWP::UserAgent')) {
326 CPAN::LWP::UserAgent->config;
327 eval {$Ua = CPAN::LWP::UserAgent->new;}; # Why is has_usable still not fit enough?
329 $CPAN::Frontend->mywarn("CPAN::LWP::UserAgent->new dies with $@\n")
333 $Ua->proxy('ftp', $var)
334 if $var = $CPAN::Config->{ftp_proxy} || $ENV{ftp_proxy};
335 $Ua->proxy('http', $var)
336 if $var = $CPAN::Config->{http_proxy} || $ENV{http_proxy};
338 if $var = $CPAN::Config->{no_proxy} || $ENV{no_proxy};
342 for my $prx (qw(ftp_proxy http_proxy no_proxy)) {
343 $ENV{$prx} = $CPAN::Config->{$prx} if $CPAN::Config->{$prx};
346 # Try the list of urls for each single object. We keep a record
347 # where we did get a file from
348 my(@reordered,$last);
349 my $ccurllist = $self->_get_urllist;
350 $last = $#$ccurllist;
351 if ($force & 2) { # local cpans probably out of date, don't reorder
352 @reordered = (0..$last);
356 (substr($ccurllist->[$b],0,4) eq "file")
358 (substr($ccurllist->[$a],0,4) eq "file")
362 ($ccurllist->[$b] eq $ThesiteURL)
364 ($ccurllist->[$a] eq $ThesiteURL)
369 $self->debug("Themethod[$Themethod]reordered[@reordered]") if $CPAN::DEBUG;
375 ["dleasy", "http","defaultsites"],
376 ["dlhard", "http","defaultsites"],
377 ["dleasy", "ftp", "defaultsites"],
378 ["dlhard", "ftp", "defaultsites"],
379 ["dlhardest","", "defaultsites"],
382 @levels = grep {$_->[0] eq $Themethod} @all_levels;
383 push @levels, grep {$_->[0] ne $Themethod} @all_levels;
385 @levels = @all_levels;
387 @levels = qw/dleasy/ if $^O eq 'MacOS';
389 local $ENV{FTP_PASSIVE} =
390 exists $CPAN::Config->{ftp_passive} ?
391 $CPAN::Config->{ftp_passive} : 1;
393 my $stats = $self->_new_stats($file);
394 for ($CPAN::Config->{connect_to_internet_ok}) {
395 $connect_to_internet_ok = $_ if not defined $connect_to_internet_ok and defined $_;
397 LEVEL: for $levelno (0..$#levels) {
398 my $level_tuple = $levels[$levelno];
399 my($level,$scheme,$sitetag) = @$level_tuple;
400 $self->mymkpath($aslocal_dir) unless $scheme && "file" eq $scheme;
401 my $defaultsites = $sitetag && $sitetag eq "defaultsites";
404 unless (defined $connect_to_internet_ok) {
405 $CPAN::Frontend->myprint(sprintf qq{
406 I would like to connect to one of the following sites to get '%s':
411 join("",map { " ".$_->text."\n" } @CPAN::Defaultsites),
413 my $answer = CPAN::Shell::colorable_makemaker_prompt("Is it OK to try to connect to the Internet?", "yes");
414 if ($answer =~ /^y/i) {
415 $connect_to_internet_ok = 1;
417 $connect_to_internet_ok = 0;
420 if ($connect_to_internet_ok) {
421 @urllist = @CPAN::Defaultsites;
424 # the tricky thing about dying here is that everybody
425 # believes that calls to exists() or all_objects() are
427 require CPAN::Exception::blocked_urllist;
428 die CPAN::Exception::blocked_urllist->new;
431 my @host_seq = $level =~ /dleasy/ ?
432 @reordered : 0..$last; # reordered has file and $Thesiteurl first
433 @urllist = map { $ccurllist->[$_] } @host_seq;
435 $self->debug("synth. urllist[@urllist]") if $CPAN::DEBUG;
436 my $aslocal_tempfile = $aslocal . ".tmp" . $$;
437 if (my $recommend = $self->_recommend_url_for($file)) {
438 @urllist = grep { $_ ne $recommend } @urllist;
439 unshift @urllist, $recommend;
441 $self->debug("synth. urllist[@urllist]") if $CPAN::DEBUG;
442 $ret = $self->hostdlxxx($level,$scheme,\@urllist,$file,$aslocal_tempfile,$stats);
444 CPAN->debug("ret[$ret]aslocal[$aslocal]") if $CPAN::DEBUG;
445 if ($ret eq $aslocal_tempfile) {
446 # if we got it exactly as we asked for, only then we
448 rename $aslocal_tempfile, $aslocal
449 or $CPAN::Frontend->mydie("Error while trying to rename ".
450 "'$ret' to '$aslocal': $!");
455 # utime $now, $now, $aslocal; # too bad, if we do that, we
456 # might alter a local mirror
457 $self->debug("level[$level]") if $CPAN::DEBUG;
460 unlink $aslocal_tempfile;
461 last if $CPAN::Signal; # need to cleanup
465 $stats->{filesize} = -s $ret;
467 $self->debug("before _add_to_statistics") if $CPAN::DEBUG;
468 $self->_add_to_statistics($stats);
469 $self->debug("after _add_to_statistics") if $CPAN::DEBUG;
471 unlink "$aslocal.bak$$";
474 unless ($CPAN::Signal) {
477 if (@{$CPAN::Config->{urllist}}) {
479 qq{Please check, if the URLs I found in your configuration file \(}.
480 join(", ", @{$CPAN::Config->{urllist}}).
483 push @mess, qq{Your urllist is empty!};
485 push @mess, qq{The urllist can be edited.},
486 qq{E.g. with 'o conf urllist push ftp://myurl/'};
487 $CPAN::Frontend->mywarn(Text::Wrap::wrap("","","@mess"). "\n\n");
488 $CPAN::Frontend->mydie("Could not fetch $file\n");
490 if ($maybe_restore) {
491 rename "$aslocal.bak$$", $aslocal;
492 $CPAN::Frontend->myprint("Trying to get away with old file:\n" .
493 $self->ls($aslocal));
500 my($self, $aslocal_dir) = @_;
501 mkpath($aslocal_dir);
502 $CPAN::Frontend->mywarn(qq{Warning: You are not allowed to write into }.
503 qq{directory "$aslocal_dir".
504 I\'ll continue, but if you encounter problems, they may be due
505 to insufficient permissions.\n}) unless -w $aslocal_dir;
513 $h = [ grep /^\Q$scheme\E:/, @$h ] if $scheme;
514 my $method = "host$level";
515 $self->$method($h, @_);
519 my($self,$stats,$method,$url) = @_;
520 push @{$stats->{attempts}}, {
528 sub hostdleasy { #called from hostdlxxx
529 my($self,$host_seq,$file,$aslocal,$stats) = @_;
531 HOSTEASY: for $ro_url (@$host_seq) {
532 $self->_set_attempt($stats,"dleasy",$ro_url);
533 my $url .= "$ro_url$file";
534 $self->debug("localizing perlish[$url]") if $CPAN::DEBUG;
535 if ($url =~ /^file:/) {
537 if ($CPAN::META->has_inst('URI::URL')) {
538 my $u = URI::URL->new($url);
540 } else { # works only on Unix, is poorly constructed, but
541 # hopefully better than nothing.
542 # RFC 1738 says fileurl BNF is
543 # fileurl = "file://" [ host | "localhost" ] "/" fpath
544 # Thanks to "Mark D. Baushke" <mdb@cisco.com> for
546 ($l = $url) =~ s|^file://[^/]*/|/|; # discard the host part
547 $l =~ s|^file:||; # assume they
551 if ! -f $l && $l =~ m|^/\w:|; # e.g. /P:
553 $self->debug("local file[$l]") if $CPAN::DEBUG;
554 if ( -f $l && -r _) {
555 $ThesiteURL = $ro_url;
558 if ($l =~ /(.+)\.gz$/) {
560 if ( -f $ungz && -r _) {
561 $ThesiteURL = $ro_url;
565 # Maybe mirror has compressed it?
567 $self->debug("found compressed $l.gz") if $CPAN::DEBUG;
568 eval { CPAN::Tarzip->new("$l.gz")->gunzip($aslocal) };
570 $ThesiteURL = $ro_url;
574 $CPAN::Frontend->mywarn("Could not find '$l'\n");
576 $self->debug("it was not a file URL") if $CPAN::DEBUG;
577 if ($CPAN::META->has_usable('LWP')) {
578 $CPAN::Frontend->myprint("Fetching with LWP:
582 CPAN::LWP::UserAgent->config;
583 eval { $Ua = CPAN::LWP::UserAgent->new; };
585 $CPAN::Frontend->mywarn("CPAN::LWP::UserAgent->new dies with $@\n");
588 my $res = $Ua->mirror($url, $aslocal);
589 if ($res->is_success) {
590 $ThesiteURL = $ro_url;
592 utime $now, $now, $aslocal; # download time is more
593 # important than upload
596 } elsif ($url !~ /\.gz(?!\n)\Z/) {
597 my $gzurl = "$url.gz";
598 $CPAN::Frontend->myprint("Fetching with LWP:
601 $res = $Ua->mirror($gzurl, "$aslocal.gz");
602 if ($res->is_success) {
603 if (eval {CPAN::Tarzip->new("$aslocal.gz")->gunzip($aslocal)}) {
604 $ThesiteURL = $ro_url;
609 $CPAN::Frontend->myprint(sprintf(
610 "LWP failed with code[%s] message[%s]\n",
614 # Alan Burlison informed me that in firewall environments
615 # Net::FTP can still succeed where LWP fails. So we do not
616 # skip Net::FTP anymore when LWP is available.
619 $CPAN::Frontend->mywarn(" LWP not available\n");
621 return if $CPAN::Signal;
622 if ($url =~ m|^ftp://(.*?)/(.*)/(.*)|) {
623 # that's the nice and easy way thanks to Graham
624 $self->debug("recognized ftp") if $CPAN::DEBUG;
625 my($host,$dir,$getfile) = ($1,$2,$3);
626 if ($CPAN::META->has_usable('Net::FTP')) {
628 $CPAN::Frontend->myprint("Fetching with Net::FTP:
631 $self->debug("getfile[$getfile]dir[$dir]host[$host]" .
632 "aslocal[$aslocal]") if $CPAN::DEBUG;
633 if (CPAN::FTP->ftp_get($host,$dir,$getfile,$aslocal)) {
634 $ThesiteURL = $ro_url;
637 if ($aslocal !~ /\.gz(?!\n)\Z/) {
638 my $gz = "$aslocal.gz";
639 $CPAN::Frontend->myprint("Fetching with Net::FTP
642 if (CPAN::FTP->ftp_get($host,
646 eval{CPAN::Tarzip->new($gz)->gunzip($aslocal)}
648 $ThesiteURL = $ro_url;
654 CPAN->debug("Net::FTP does not count as usable atm") if $CPAN::DEBUG;
658 UNIVERSAL::can($ro_url,"text")
660 $ro_url->{FROM} eq "USER"
662 ##address #17973: default URLs should not try to override
663 ##user-defined URLs just because LWP is not available
664 my $ret = $self->hostdlhard([$ro_url],$file,$aslocal,$stats);
667 return if $CPAN::Signal;
673 my($self,$host_seq,$file,$aslocal,$stats) = @_;
675 # Came back if Net::FTP couldn't establish connection (or
676 # failed otherwise) Maybe they are behind a firewall, but they
677 # gave us a socksified (or other) ftp program...
680 my($devnull) = $CPAN::Config->{devnull} || "";
682 my($aslocal_dir) = dirname($aslocal);
683 mkpath($aslocal_dir);
684 my $some_dl_success = 0;
685 HOSTHARD: for $ro_url (@$host_seq) {
686 $self->_set_attempt($stats,"dlhard",$ro_url);
687 my $url = "$ro_url$file";
688 my($proto,$host,$dir,$getfile);
690 # Courtesy Mark Conty mark_conty@cargill.com change from
691 # if ($url =~ m|^ftp://(.*?)/(.*)/(.*)|) {
693 if ($url =~ m|^([^:]+)://(.*?)/(.*)/(.*)|) {
695 ($proto,$host,$dir,$getfile) = ($1,$2,$3,$4);
697 next HOSTHARD; # who said, we could ftp anything except ftp?
699 next HOSTHARD if $proto eq "file"; # file URLs would have had
700 # success above. Likely a bogus URL
702 $self->debug("localizing funkyftpwise[$url]") if $CPAN::DEBUG;
704 # Try the most capable first and leave ncftp* for last as it only
706 my $proxy_vars = $self->_proxy_vars($ro_url);
707 DLPRG: for my $f (qw(curl wget lynx ncftpget ncftp)) {
708 my $funkyftp = CPAN::HandleConfig->safe_quote($CPAN::Config->{$f});
709 next DLPRG unless defined $funkyftp;
710 next DLPRG if $funkyftp =~ /^\s*$/;
712 my($asl_ungz, $asl_gz);
713 ($asl_ungz = $aslocal) =~ s/\.gz//;
714 $asl_gz = "$asl_ungz.gz";
716 my($src_switch) = "";
718 my($stdout_redir) = " > $asl_ungz";
720 $src_switch = " -source";
721 } elsif ($f eq "ncftp") {
723 } elsif ($f eq "wget") {
724 $src_switch = " -O $asl_ungz";
726 } elsif ($f eq 'curl') {
727 $src_switch = ' -L -f -s -S --netrc-optional';
728 if ($proxy_vars->{http_proxy}) {
729 $src_switch .= qq{ -U "$proxy_vars->{proxy_user}:$proxy_vars->{proxy_pass}" -x "$proxy_vars->{http_proxy}"};
733 if ($f eq "ncftpget") {
734 $chdir = "cd $aslocal_dir && ";
737 $CPAN::Frontend->myprint(
739 Trying with "$funkyftp$src_switch" to get
743 "$chdir$funkyftp$src_switch \"$url\" $devnull$stdout_redir";
744 $self->debug("system[$system]") if $CPAN::DEBUG;
745 my($wstatus) = system($system);
747 # lynx returns 0 when it fails somewhere
749 my $content = do { local *FH;
750 open FH, $asl_ungz or die;
753 if ($content =~ /^<.*(<title>[45]|Error [45])/si) {
754 $CPAN::Frontend->mywarn(qq{
755 No success, the file that lynx has downloaded looks like an error message:
758 $CPAN::Frontend->mysleep(1);
763 $CPAN::Frontend->myprint(qq{
764 No success, the file that lynx has downloaded is an empty file.
773 } elsif ($asl_ungz ne $aslocal) {
774 # test gzip integrity
775 if (eval{CPAN::Tarzip->new($asl_ungz)->gtest}) {
776 # e.g. foo.tar is gzipped --> foo.tar.gz
777 rename $asl_ungz, $aslocal;
780 eval{CPAN::Tarzip->new($asl_gz)->gzip($asl_ungz)};
788 $ThesiteURL = $ro_url;
790 } elsif ($url !~ /\.gz(?!\n)\Z/) {
792 -f $asl_ungz && -s _ == 0;
793 my $gz = "$aslocal.gz";
794 my $gzurl = "$url.gz";
795 $CPAN::Frontend->myprint(
797 Trying with "$funkyftp$src_switch" to get
800 my($system) = "$funkyftp$src_switch \"$url.gz\" $devnull > $asl_gz";
801 $self->debug("system[$system]") if $CPAN::DEBUG;
803 if (($wstatus = system($system)) == 0
807 # test gzip integrity
808 my $ct = eval{CPAN::Tarzip->new($asl_gz)};
809 if ($ct && $ct->gtest) {
810 $ct->gunzip($aslocal);
812 # somebody uncompressed file for us?
813 rename $asl_ungz, $aslocal;
815 $ThesiteURL = $ro_url;
818 unlink $asl_gz if -f $asl_gz;
821 my $estatus = $wstatus >> 8;
822 my $size = -f $aslocal ?
823 ", left\n$aslocal with size ".-s _ :
824 "\nWarning: expected file [$aslocal] doesn't exist";
825 $CPAN::Frontend->myprint(qq{
826 Function system("$system")
827 returned status $estatus (wstat $wstatus)$size
830 return if $CPAN::Signal;
831 } # download/transfer programs (DLPRG)
834 if ($some_dl_success) {
835 Carp::cluck("Warning: doesn't seem we had substantial success downloading '$aslocal'. Don't know how to proceed.");
837 Carp::cluck("Warning: no success downloading '$aslocal'. Giving up on it.");
839 $CPAN::Frontend->mysleep(5);
843 #-> CPAN::FTP::_proxy_vars
847 my $http_proxy = $CPAN::Config->{'http_proxy'} || $ENV{'http_proxy'};
849 my($host) = $url =~ m|://([^/:]+)|;
851 my $noproxy = $CPAN::Config->{'no_proxy'} || $ENV{'no_proxy'} || "";
852 my @noproxy = split /\s*,\s*/, $noproxy;
854 DOMAIN: for my $domain (@noproxy) {
855 if ($host =~ /\Q$domain\E$/) { # cf. LWP::UserAgent
861 $CPAN::Frontend->mywarn(" Could not determine host from http_proxy '$http_proxy'\n");
865 &CPAN::LWP::UserAgent::get_proxy_credentials();
869 http_proxy => $http_proxy
878 my($self,$host_seq,$file,$aslocal,$stats) = @_;
880 return unless @$host_seq;
882 my($aslocal_dir) = dirname($aslocal);
883 mkpath($aslocal_dir);
884 my $ftpbin = $CPAN::Config->{ftp};
885 unless ($ftpbin && length $ftpbin && MM->maybe_command($ftpbin)) {
886 $CPAN::Frontend->myprint("No external ftp command available\n\n");
889 $CPAN::Frontend->mywarn(qq{
890 As a last resort we now switch to the external ftp command '$ftpbin'
893 Doing so often leads to problems that are hard to diagnose.
895 If you're the victim of such problems, please consider unsetting the
896 ftp config variable with
902 $CPAN::Frontend->mysleep(2);
903 HOSTHARDEST: for $ro_url (@$host_seq) {
904 $self->_set_attempt($stats,"dlhardest",$ro_url);
905 my $url = "$ro_url$file";
906 $self->debug("localizing ftpwise[$url]") if $CPAN::DEBUG;
907 unless ($url =~ m|^ftp://(.*?)/(.*)/(.*)|) {
910 my($host,$dir,$getfile) = ($1,$2,$3);
912 my($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,
913 $ctime,$blksize,$blocks) = stat($aslocal);
914 $timestamp = $mtime ||= 0;
915 my($netrc) = CPAN::FTP::netrc->new;
916 my($netrcfile) = $netrc->netrc;
917 my($verbose) = $CPAN::DEBUG{'FTP'} & $CPAN::DEBUG ? " -v" : "";
918 my $targetfile = File::Basename::basename($aslocal);
924 map("cd $_", split /\//, $dir), # RFC 1738
926 "get $getfile $targetfile",
930 CPAN->debug("No ~/.netrc file found") if $CPAN::DEBUG;
931 } elsif ($netrc->hasdefault || $netrc->contains($host)) {
932 CPAN->debug(sprintf("hasdef[%d]cont($host)[%d]",
934 $netrc->contains($host))) if $CPAN::DEBUG;
935 if ($netrc->protected) {
936 my $dialog = join "", map { " $_\n" } @dialog;
938 if ($netrc->contains($host)) {
939 $netrc_explain = "Relying that your .netrc entry for '$host' ".
942 $netrc_explain = "Relying that your default .netrc entry ".
945 $CPAN::Frontend->myprint(qq{
946 Trying with external ftp to get
949 Going to send the dialog
953 $self->talk_ftp("$ftpbin$verbose $host",
955 ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,
956 $atime,$mtime,$ctime,$blksize,$blocks) = stat($aslocal);
958 if ($mtime > $timestamp) {
959 $CPAN::Frontend->myprint("GOT $aslocal\n");
960 $ThesiteURL = $ro_url;
963 $CPAN::Frontend->myprint("Hmm... Still failed!\n");
965 return if $CPAN::Signal;
967 $CPAN::Frontend->mywarn(qq{Your $netrcfile is not }.
968 qq{correctly protected.\n});
971 $CPAN::Frontend->mywarn("Your ~/.netrc neither contains $host
972 nor does it have a default entry\n");
975 # OK, they don't have a valid ~/.netrc. Use 'ftp -n'
976 # then and login manually to host, using e-mail as
978 $CPAN::Frontend->myprint(qq{Issuing "$ftpbin$verbose -n"\n});
982 "user anonymous $Config::Config{'cf_email'}"
984 my $dialog = join "", map { " $_\n" } @dialog;
985 $CPAN::Frontend->myprint(qq{
986 Trying with external ftp to get
988 Going to send the dialog
992 $self->talk_ftp("$ftpbin$verbose -n", @dialog);
993 ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,
994 $atime,$mtime,$ctime,$blksize,$blocks) = stat($aslocal);
996 if ($mtime > $timestamp) {
997 $CPAN::Frontend->myprint("GOT $aslocal\n");
998 $ThesiteURL = $ro_url;
1001 $CPAN::Frontend->myprint("Bad luck... Still failed!\n");
1003 return if $CPAN::Signal;
1004 $CPAN::Frontend->mywarn("Can't access URL $url.\n\n");
1005 $CPAN::Frontend->mysleep(2);
1009 # package CPAN::FTP;
1011 my($self,$command,@dialog) = @_;
1012 my $fh = FileHandle->new;
1013 $fh->open("|$command") or die "Couldn't open ftp: $!";
1014 foreach (@dialog) { $fh->print("$_\n") }
1015 $fh->close; # Wait for process to complete
1017 my $estatus = $wstatus >> 8;
1018 $CPAN::Frontend->myprint(qq{
1019 Subprocess "|$command"
1020 returned status $estatus (wstat $wstatus)
1024 # find2perl needs modularization, too, all the following is stolen
1028 my($self,$name) = @_;
1029 my ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$sizemm,
1030 $atime,$mtime,$ctime,$blksize,$blocks) = lstat($name);
1032 my($perms,%user,%group);
1036 $blocks = int(($blocks + 1) / 2);
1039 $blocks = int(($sizemm + 1023) / 1024);
1042 if (-f _) { $perms = '-'; }
1043 elsif (-d _) { $perms = 'd'; }
1044 elsif (-c _) { $perms = 'c'; $sizemm = &sizemm; }
1045 elsif (-b _) { $perms = 'b'; $sizemm = &sizemm; }
1046 elsif (-p _) { $perms = 'p'; }
1047 elsif (-S _) { $perms = 's'; }
1048 else { $perms = 'l'; $pname .= ' -> ' . readlink($_); }
1050 my(@rwx) = ('---','--x','-w-','-wx','r--','r-x','rw-','rwx');
1051 my(@moname) = qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec);
1052 my $tmpmode = $mode;
1053 my $tmp = $rwx[$tmpmode & 7];
1055 $tmp = $rwx[$tmpmode & 7] . $tmp;
1057 $tmp = $rwx[$tmpmode & 7] . $tmp;
1058 substr($tmp,2,1) =~ tr/-x/Ss/ if -u _;
1059 substr($tmp,5,1) =~ tr/-x/Ss/ if -g _;
1060 substr($tmp,8,1) =~ tr/-x/Tt/ if -k _;
1063 my $user = $user{$uid} || $uid; # too lazy to implement lookup
1064 my $group = $group{$gid} || $gid;
1066 my($sec,$min,$hour,$mday,$mon,$year) = localtime($mtime);
1068 my($moname) = $moname[$mon];
1069 if (-M _ > 365.25 / 2) {
1070 $timeyear = $year + 1900;
1073 $timeyear = sprintf("%02d:%02d", $hour, $min);
1076 sprintf "%5lu %4ld %-10s %2d %-8s %-8s %8s %s %2d %5s %s\n",