use warnings;
use File::Basename;
use File::Copy;
+use Text::ParseWords;
@ARGV > 2 or die "Syntax: $0 <target dir> <filename> <hash> <url filename> [<mirror> ...]\n";
my $len = length($file_hash);
my $cmd;
- $len == 64 and return "openssl dgst -sha256 | sed -e 's,.*= ,,'";
- $len == 32 and do {
- my $cmd = which("md5sum") || which("md5") || die 'no md5 checksum program found, please install md5 or md5sum';
- chomp $cmd;
- return $cmd;
- };
+ $len == 64 and return "mkhash sha256";
+ $len == 32 and return "mkhash md5";
return undef;
}
+sub download_cmd($) {
+ my $url = shift;
+ my $have_curl = 0;
+
+ if (open CURL, '-|', 'curl', '--version') {
+ if (defined(my $line = readline CURL)) {
+ $have_curl = 1 if $line =~ /^curl /;
+ }
+ close CURL;
+ }
+
+ return $have_curl
+ ? (qw(curl -f --connect-timeout 20 --retry 5 --location --insecure), shellwords($ENV{CURL_OPTIONS} || ''), $url)
+ : (qw(wget --tries=5 --timeout=20 --no-check-certificate --output-document=-), shellwords($ENV{WGET_OPTIONS} || ''), $url)
+ ;
+}
+
my $hash_cmd = hash_cmd();
sub download
{
my $mirror = shift;
- my $options = $ENV{WGET_OPTIONS} || "";
$mirror =~ s!/$!!;
}
};
} else {
- open WGET, "wget -t5 --timeout=20 --no-check-certificate $options -O- '$mirror/$url_filename' |" or die "Cannot launch wget.\n";
+ my @cmd = download_cmd("$mirror/$url_filename");
+ open(FETCH_FD, '-|', @cmd) or die "Cannot launch curl or wget.\n";
$hash_cmd and do {
open MD5SUM, "| $hash_cmd > '$target/$filename.hash'" or die "Cannot launch $hash_cmd.\n";
};
open OUTPUT, "> $target/$filename.dl" or die "Cannot create file $target/$filename.dl: $!\n";
my $buffer;
- while (read WGET, $buffer, 1048576) {
+ while (read FETCH_FD, $buffer, 1048576) {
$hash_cmd and print MD5SUM $buffer;
print OUTPUT $buffer;
}
$hash_cmd and close MD5SUM;
- close WGET;
+ close FETCH_FD;
close OUTPUT;
if ($? >> 8) {
$sum = $1;
if ($sum ne $file_hash) {
- print STDERR "MD5 sum of the downloaded file does not match (file: $sum, requested: $file_hash) - deleting download.\n";
+ print STDERR "Hash of the downloaded file does not match (file: $sum, requested: $file_hash) - deleting download.\n";
cleanup();
return;
}
push @mirrors, 'http://mirror2.openwrt.org/sources';
push @mirrors, 'http://downloads.openwrt.org/sources';
-while (!$ok) {
+while (!-f "$target/$filename") {
my $mirror = shift @mirrors;
$mirror or die "No more mirrors to try - giving up.\n";
download($mirror);
- -f "$target/$filename" and $ok = 1;
}
$SIG{INT} = \&cleanup;