summaryrefslogtreecommitdiffstats
path: root/dselect/methods/ftp/update.pl
blob: a40f6f01a99b54313d66106ee198a24cbd1c1c10 (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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
#!/usr/bin/perl
#
# Copyright © 1996 Andy Guy <awpguy@acs.ucalgary.ca>
# Copyright © 1998 Martin Schulze <joey@infodrom.north.de>
# Copyright © 1999, 2009 Raphaël Hertzog <hertzog@debian.org>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <https://www.gnu.org/licenses/>.

use strict;
use warnings;

eval q{
    pop @INC if $INC[-1] eq '.';
    use Net::FTP;
};
if ($@) {
    warn "Please install the 'perl' package if you want to use the\n" .
         "FTP access method of dselect.\n\n";
    exit 1;
}

use Dselect::Ftp;

# deal with arguments
my $vardir = $ARGV[0];
my $method = $ARGV[1];
my $option = $ARGV[2];

if ($option eq 'manual') {
    print "Enter package file names or a blank line to finish\n";
    while(1) {
	print 'Enter package file name:';
	my $fn = <STDIN>;
	chomp $fn;
	if ($fn eq '') {
	    exit 0;
	}
	if ( -f $fn ) {
	    system('dpkg', '--merge-avail', $fn);
	} else {
	    print "Could not find $fn, try again\n";
	}
    };
};

#print "vardir: $vardir, method: $method, option: $option\n";

my $arch = qx(dpkg --print-architecture);
$arch='i386' if $?;
chomp $arch;
my $exit = 0;

# get info from control file
read_config("$vardir/methods/ftp/vars");

chdir "$vardir/methods/ftp";

print "Getting Packages files...(stop with ^C)\n\n";

my @pkgfiles;
my $ftp;
my $packages_modified = 0;

sub download {
foreach (@{$CONFIG{site}}) {

   my $site = $_;

        $ftp = do_connect(ftpsite => $_->[0],
                          ftpdir => $_->[1],
                          passive => $_->[3],
                          username => $_->[4],
                          password => $_->[5],
                          useproxy => $CONFIG{use_auth_proxy},
                          proxyhost => $CONFIG{proxyhost},
                          proxylogname => $CONFIG{proxylogname},
                          proxypassword => $CONFIG{proxypassword});

    my @dists = @{$_->[2]};
    PACKAGE:
    foreach my $dist (@dists) {
	my $dir = "$dist/binary-$arch";
	my $must_get = 0;
	my $newest_pack_date;

	# check existing Packages on remote site
	print "\nChecking for Packages file... ";
	$newest_pack_date = do_mdtm ($ftp, "$dir/Packages.gz");
	if (defined $newest_pack_date) {
	    print "$dir/Packages.gz\n";
	} else {
	    $dir = "$dist";
	    $newest_pack_date = do_mdtm ($ftp, "$dir/Packages.gz");
	    if (defined $newest_pack_date) {
		print "$dir/Packages.gz\n";
	    } else {
		print "Couldn't find Packages.gz in $dist/binary-$arch or $dist; ignoring.\n";
		print "Your setup is probably wrong, check the distributions directories,\n";
		print "and try with passive mode enabled/disabled (if you use a proxy/firewall)\n";
		next PACKAGE;
	    }
	}

	# we now have $dir set to point to an existing Packages.gz file

	# check if we already have a Packages file (and get its date)
	$dist =~ tr/\//_/;
	my $file = "Packages.$site->[0].$dist";

	# if not
	if (! -f $file) {
	    # must get one
#	    print "No Packages here; must get it.\n";
	    $must_get = 1;
	} else {
	    # else check last modification date
            my @pack_stat = stat($file);
	    if($newest_pack_date > $pack_stat[9]) {
#		print "Packages has changed; must get it.\n";
		$must_get = 1;
	    } elsif ($newest_pack_date < $pack_stat[9]) {
		print " Our file is newer than theirs; skipping.\n";
	    } else {
		print " Already up-to-date; skipping.\n";
	    }
	}

	if ($must_get) {
	    -f 'Packages.gz' and unlink 'Packages.gz';
	    -f 'Packages' and unlink 'Packages';
	    my $size = 0;

	  TRY_GET_PACKAGES:
	    while (1) {
		if ($size) {
		    print ' Continuing ';
		} else {
		    print ' Getting ';
		}
		print "Packages file from $dir...\n";
		eval {
		    if ($ftp->get("$dir/Packages.gz", 'Packages.gz', $size)) {
			if (system('gunzip', 'Packages.gz')) {
			    print "  Couldn't gunzip Packages.gz, stopped";
			    die 'error';
			}
		    } else {
			print "  Couldn't get Packages.gz from $dir !!! Stopped.";
			die 'error';
		    }
		};
		if ($@) {
		    $size = -s 'Packages.gz';
		    if (ref($ftp)) {
		      $ftp->abort();
		      $ftp->quit();
		    };
		    if (yesno ('y', "Transfer failed at $size: retry at once")) {
                         $ftp = do_connect(ftpsite => $site->[0],
                                           ftpdir => $site->[1],
                                           passive => $site->[3],
                                           username => $site->[4],
                                           password => $site->[5],
                                           useproxy => $CONFIG{use_auth_proxy},
                                           proxyhost => $CONFIG{proxyhost},
                                           proxylogname => $CONFIG{proxylogname},
                                           proxypassword => $CONFIG{proxypassword});

			if ($newest_pack_date != do_mdtm ($ftp, "$dir/Packages.gz")) {
			    print ("Packages file has changed !\n");
			    $size = 0;
			}
			next TRY_GET_PACKAGES;
		    } else {
			die 'error';
		    }
		}
		last TRY_GET_PACKAGES;
	    }

	    if (!rename 'Packages', "Packages.$site->[0].$dist") {
		print "  Couldn't rename Packages to Packages.$site->[0].$dist";
		die 'error';
	    } else {
		# set local Packages file to same date as the one it mirrors
		# to allow comparison to work.
		utime $newest_pack_date, $newest_pack_date, "Packages.$site->[0].$dist";
		$packages_modified = 1;
	    }
	}
	push @pkgfiles, "Packages.$site->[0].$dist";
    }
    $ftp->quit();
 }
}

eval {
    local $SIG{INT} = sub {
	die "interrupted!\n";
    };
    download();
};
if($@) {
    $ftp->quit() if (ref($ftp));
    if($@ =~ /timeout/i) {
	print "FTP TIMEOUT\n";
    } else {
	print "FTP ERROR - $@\n";
    }
    $exit = 1;
};

# Don't clear if nothing changed.
if ($packages_modified) {
    print <<'EOM';

It is a good idea to clear the available list of old packages.
However if you have only downloaded a Package files from non-main
distributions you might not want to do this.

EOM
    if (yesno ('y', 'Do you want to clear available list')) {
	print "Clearing...\n";
	if (system('dpkg', '--clear-avail')) {
	    print 'dpkg --clear-avail failed.';
	    die 'error';
	}
    }
}

if (!$packages_modified) {
    print "No Packages files was updated.\n";
} else {
    foreach my $file (@pkgfiles) {
	if (system('dpkg', '--merge-avail', $file)) {
	    print "Dpkg merge available failed on $file";
	    $exit = 1;
	}
    }
}
exit $exit;