summaryrefslogtreecommitdiffstats
path: root/perl-install/ftp.pm
blob: 14ef68bf0c98f079ced30d8b0222863d6fa9fc0b (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
87
88
89
90
91
package ftp; # $Id$

use Net::FTP;

use install_any;
use network;
use log;

my %hosts;

1;

sub fromEnv() {
    # using URLPREFIX to find out information if kickstart
    ($ENV{LOGIN}, $ENV{PASSWORD}, $ENV{HOST}, $ENV{PREFIX}) =
      $ENV{URLPREFIX} =~ m|
       ://
       (?: ([^:]*)              # login
           (?: :([^@]*))?       # password
       @)?
       ([^/]*)                	# host
       /?(.*)			# prefix
      |x unless $ENV{HOST};
    
    @ENV{qw(HOST PREFIX LOGIN PASSWORD)};
}

sub new {
    my ($host, $prefix, $login, $password) = @_;
    log::l("ftp::new");
    my @l = do { if ($hosts{"$host$prefix"}) {
	log::l("ftp::new 1");
	@{$hosts{"$host$prefix"}};
    } else {
	log::l("ftp::new 2");
	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;
	while (1) {
	    log::l("ftp::new 3");
	    $ftp = Net::FTP->new(network::resolv($host), %options) or die;
	    $ftp && $ftp->login($login, $password) and last;

	    log::l("login failed, sleeping before trying again");
	    sleep 10;
	}
	$ftp->binary;
	$ftp->cwd($prefix);

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

sub getFile {
    my ($f, @para) = @_;
    foreach (1..2) {
	my ($ftp, $retr) = new(@para ? @para : fromEnv);
	$$retr->close if $$retr;
	$$retr = $ftp->retr($f) and return $$retr;
	rewindGetFile();
    }
}

#-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) = @{$_ || []};
	$ftp->close() if $ftp;
    }

    #- make sure to reconnect to server.
    %hosts = ();
}