summaryrefslogtreecommitdiffstats
path: root/perl-install/ftp.pm
blob: 05e892c62b084b0fbe58e3839c562704e3480a1c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package ftp; # $Id$

use Net::FTP;

use network;
use log;

my %hosts;

1;

sub fromEnv() {
    #- now URLPREFIX is generated from what is given by mdk-stage1 which is only this 4 variables.
    $ENV{URLPREFIX} = "ftp://" . ($ENV{LOGIN} && ($ENV{LOGIN} . ($ENV{PASSWORD} && ":$ENV{PASSWORD}") . '@')) .
      "$ENV{HOST}/$ENV{PREFIX}";
    @ENV{qw(HOST PREFIX LOGIN PASSWORD)};
}

sub new {
    my ($host, $prefix, $login, $password) = @_;
    my @l = do { if ($hosts{"$host$prefix"}) {
	@{$hosts{"$host$prefix"}};
    } else {
	my %options = (Passive => 1, Timeout => 60, Port => 21);
	$options{Firewall} = $ENV{PROXY} if $ENV{PROXY};
	$options{Port} = $ENV{PROXYPORT} if $ENV{PROXYPORT};
	unless ($login) {
	    $login = 'anonymous';
	    $password = '-drakx@';
	}

	my $ftp;
	foreach (1..10) {
	    $ftp = Net::FTP->new(network::resolv($host), %options) or die;
	    $ftp && $ftp->login($login, $password) and last;

	    log::l("ftp login failed, sleeping before trying again");
	    sleep 5 * $_;
	}
	$ftp or die "unable to open ftp connection to $host";
	$ftp->binary;
	$ftp->cwd($prefix);

	my @l = ($ftp, \ (my $_retr));
	$hosts{"$host$prefix"} = \@l;
	@l;
    } };
    wantarray() ? @l : $l[0];
}

sub getFile {
    my ($f, @para) = @_;
    $f eq 'XXX' and rewindGetFile(), return; #- special case to force closing connection.
    foreach (1..3) {
	my ($ftp, $retr) = new(@para ? @para : fromEnv());
	eval { $$retr->close if $$retr };
	$@ and rewindGetFile(); #- in case Timeout got us on "->close"
	$$retr = $ftp->retr($f) and return $$retr;
	$ftp->code == 550 and log::l("FTP: 550 file unavailable"), return;
	rewindGetFile();
	log::l("ftp get failed, sleeping before trying again");
	sleep 1;
    }
}

#-sub closeFiles() {
#-    #- close any existing connections
#-    foreach (values %hosts) {
#-	  my $retr = $_->[1] if ref $_;
#-	  $$retr->close if $$retr;
#-	  undef $$retr;
#-    }
#-}

sub rewindGetFile() {
    #- close any existing connection.
    foreach (values %hosts) {
	my ($ftp, $retr) = @{$_ || []};
	#- don't let Timeout kill us!
	eval { $$retr->close } if $$retr;
	eval { $ftp->close } if $ftp;
    }

    #- make sure to reconnect to server.
    %hosts = ();
}
>, 'Bosnian (Bosnia)' => 'Europe/Sarajevo', 'Brezhoneg (Brittany)' => 'Europe/Paris', 'Bulgarian (Bulgaria)' => 'Europe/Sofia', 'Catalan' => 'Europe/Madrid', 'Chinese (China)' => 'Asia/Shanghai', 'Croatian (Bosnia)' => 'Europe/Sarajevo', 'Croatian (Croatia)' => 'Europe/Zagreb', 'Cymraeg (Welsh)' => 'Europe/London', 'Czech' => 'Europe/Prague', 'Danish (Denmark)' => 'Europe/Copenhagen', 'Dutch (Netherlands)' => 'Europe/Amsterdam', 'English (United States)' => 'America/New_York', 'English (United Kingdom)' => 'Europe/London', 'Esperanto' => 'Europe/Warsaw', 'Estonian (Estonia)' => 'Europe/Tallinn', 'Euskara (Basque)' => 'Europe/Madrid', 'Finnish (Finland)' => 'Europe/Helsinki', 'French (France)' => 'Europe/Paris', 'French (Belgium)' => 'Europe/Brussels', 'French (Canada)' => 'Canada/Atlantic', # or Newfoundland ? or Eastern ? 'Gaeilge (Ireland)' => 'Europe/Dublin', 'Galego' => 'Europe/Madrid', 'Georgian (Georgia)' => 'Asia/Yerevan', 'German (Austria)' => 'Europe/Vienna', 'German (Germany)' => 'Europe/Berlin', 'Greek (Greece)' => 'Europe/Athens', 'Greenlandic' => 'Arctic/Longyearbyen', 'Hebrew (Israel)' => 'Asia/Tel_Aviv', 'Hungarian (Hungary)' => 'Europe/Budapest', 'Icelandic (Iceland)' => 'Atlantic/Reykjavik', 'Indonesian (Indonesia)' => 'Asia/Jakarta', 'Iranian (Iran)' => 'Asia/Tehran', 'Italian (Italy)' => 'Europe/Rome', #-'Italian (San Marino)' => 'Europe/San_Marino', #-'Italian (Vatican)' => 'Europe/Vatican', #-'Italian (Switzerland)' => 'Europe/Zurich', 'Japanese (Japon)' => 'Asia/Tokyo', 'Korean (Korea)' => 'Asia/Seoul', 'Latvian (Latvia)' => 'Europe/Riga', 'Lithuanian (Lithuania)' => 'Europe/Vilnius', 'Macedonian (Macedonia)' => 'Europe/Skopje', 'Maori (New Zealand)' => 'Australia/Sydney', 'Norwegian (Bokmaal)' => 'Europe/Oslo', 'Norwegian (Nynorsk)' => 'Europe/Oslo', 'Polish (Poland)' => 'Europe/Warsaw', 'Portuguese (Brazil)' => 'Brazil/East', # most people live on the east coast 'Portuguese (Portugal)' => 'Europe/Lisbon', 'Romanian (Rumania)' => 'Europe/Bucharest', 'Russian (Russia)' => 'Europe/Moscow', 'Serbian (Serbia)' => 'Europe/Belgrade',