download.pl: add aria2c support
[openwrt/staging/stintel.git] / scripts / download.pl
index e13405033f0d13bae01b9489725035b0e05aeb33..4dec985fd1af86605602c1f26307b4c74ff82926 100755 (executable)
@@ -24,6 +24,8 @@ my $scriptdir = dirname($0);
 my @mirrors;
 my $ok;
 
+my $check_certificate = $ENV{DOWNLOAD_CHECK_CERTIFICATE} eq "y";
+
 $url_filename or $url_filename = $filename;
 
 sub localmirrors {
@@ -54,10 +56,8 @@ sub localmirrors {
 
 sub which($) {
        my $prog = shift;
-       my $res = `which $prog`;
+       my $res = `command -v $prog`;
        $res or return undef;
-       $res =~ /^no / and return undef;
-       $res =~ /not found/ and return undef;
        return $res;
 }
 
@@ -65,26 +65,57 @@ sub hash_cmd() {
        my $len = length($file_hash);
        my $cmd;
 
-       $len == 64 and return "mkhash sha256";
-       $len == 32 and return "mkhash md5";
+       $len == 64 and return "$ENV{'MKHASH'} sha256";
+       $len == 32 and return "$ENV{'MKHASH'} md5";
        return undef;
 }
 
-sub download_cmd($) {
+sub download_cmd {
        my $url = shift;
        my $have_curl = 0;
+       my $have_aria2c = 0;
+       my $filename = shift;
+       my @additional_mirrors = @_;
+       my $mirrors_url = "'$url'";
 
+       my @chArray = ('a'..'z', 'A'..'Z', 0..9);
+       my $rfn = join '', map{ $chArray[int rand @chArray] } 0..9;
        if (open CURL, '-|', 'curl', '--version') {
                if (defined(my $line = readline CURL)) {
                        $have_curl = 1 if $line =~ /^curl /;
                }
                close CURL;
        }
+       if (open ARIA2C, '-|', 'aria2c', '--version') {
+               if (defined(my $line = readline ARIA2C)) {
+                       $have_aria2c = 1 if $line =~ /^aria2 /;
+               }
+               close ARIA2C;
+       }
+
+       for my $mirror (@additional_mirrors ) {
+               $mirrors_url = $mirrors_url ." '$mirror /$filename'";
+       }
 
-       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)
-       ;
+       if ($have_aria2c) {
+               return join(" ", "touch /dev/shm/${rfn}_spp;",
+                       qw(aria2c --stderr -c -x2 -s10 -j10 -k1M), $mirrors_url ,
+                       $check_certificate ? () : '--check-certificate=false',
+                       "--server-stat-of=/dev/shm/${rfn}_spp",
+                       "--server-stat-if=/dev/shm/${rfn}_spp",
+                       "-d /dev/shm -o $rfn;",
+                       "cat /dev/shm/$rfn;", "rm /dev/shm/$rfn /dev/shm/${rfn}_spp");
+       } elsif ($have_curl) {
+               return (qw(curl -f --connect-timeout 20 --retry 5 --location),
+                       $check_certificate ? () : '--insecure',
+                       shellwords($ENV{CURL_OPTIONS} || ''),
+                       $url);
+       } else {
+               return (qw(wget --tries=5 --timeout=20 --output-document=-),
+                       $check_certificate ? () : '--no-check-certificate',
+                       shellwords($ENV{WGET_OPTIONS} || ''),
+                       $url);
+       }
 }
 
 my $hash_cmd = hash_cmd();
@@ -94,6 +125,7 @@ sub download
 {
        my $mirror = shift;
        my $download_filename = shift;
+       my @additional_mirrors = @_;
 
        $mirror =~ s!/$!!;
 
@@ -140,9 +172,9 @@ sub download
                        }
                };
        } else {
-               my @cmd = download_cmd("$mirror/$download_filename");
+               my @cmd = download_cmd("$mirror/$download_filename", $download_filename, @additional_mirrors);
                print STDERR "+ ".join(" ",@cmd)."\n";
-               open(FETCH_FD, '-|', @cmd) or die "Cannot launch curl or wget.\n";
+               open(FETCH_FD, '-|', @cmd) or die "Cannot launch aria2c, curl or wget.\n";
                $hash_cmd and do {
                        open MD5SUM, "| $hash_cmd > '$target/$filename.hash'" or die "Cannot launch $hash_cmd.\n";
                };
@@ -196,6 +228,10 @@ foreach my $mirror (@ARGV) {
                }
        } elsif ($mirror =~ /^\@OPENWRT$/) {
                # use OpenWrt source server directly
+       } elsif ($mirror =~ /^\@DEBIAN\/(.+)$/) {
+               push @mirrors, "https://ftp.debian.org/debian/$1";
+               push @mirrors, "https://mirror.leaseweb.com/debian/$1";
+               push @mirrors, "https://mirror.netcologne.de/debian/$1";
        } elsif ($mirror =~ /^\@APACHE\/(.+)$/) {
                push @mirrors, "https://mirror.netcologne.de/apache.org/$1";
                push @mirrors, "https://mirror.aarnet.edu.au/pub/apache/$1";
@@ -219,14 +255,14 @@ foreach my $mirror (@ARGV) {
                push @mirrors, "http://mirror.internode.on.net/pub/gnu/$1";
                push @mirrors, "http://mirror.navercorp.com/gnu/$1";
                push @mirrors, "ftp://mirrors.rit.edu/gnu/$1";
-               push @mirrors, "ftp://download.xs4all.nl/pub/gnu/";
+               push @mirrors, "ftp://download.xs4all.nl/pub/gnu/$1";
+               push @mirrors, "https://ftp.gnu.org/gnu/$1";
        } elsif ($mirror =~ /^\@SAVANNAH\/(.+)$/) {
                push @mirrors, "https://mirror.netcologne.de/savannah/$1";
                push @mirrors, "https://mirror.csclub.uwaterloo.ca/nongnu/$1";
                push @mirrors, "http://ftp.acc.umu.se/mirror/gnu.org/savannah/$1";
                push @mirrors, "http://nongnu.uib.no/$1";
                push @mirrors, "http://ftp.igh.cnrs.fr/pub/nongnu/$1";
-               push @mirrors, "http://public.p-knowledge.co.jp/Savannah-nongnu-mirror/$1";
                push @mirrors, "ftp://cdimage.debian.org/mirror/gnu.org/savannah/$1";
                push @mirrors, "ftp://ftp.acc.umu.se/mirror/gnu.org/savannah/$1";
        } elsif ($mirror =~ /^\@KERNEL\/(.+)$/) {
@@ -238,7 +274,6 @@ foreach my $mirror (@ARGV) {
                }
                foreach my $dir (@extra) {
                        push @mirrors, "https://cdn.kernel.org/pub/$dir";
-                       push @mirrors, "https://mirror.rackspace.com/kernel.org/pub/$dir";
                        push @mirrors, "https://download.xs4all.nl/ftp.kernel.org/pub/$dir";
                        push @mirrors, "https://mirrors.mit.edu/kernel/$dir";
                        push @mirrors, "http://ftp.nara.wide.ad.jp/pub/kernel.org/$dir";
@@ -247,6 +282,7 @@ foreach my $mirror (@ARGV) {
                        push @mirrors, "ftp://www.mirrorservice.org/sites/ftp.kernel.org/pub/$dir";
                }
        } elsif ($mirror =~ /^\@GNOME\/(.+)$/) {
+               push @mirrors, "https://download.gnome.org/sources/$1";
                push @mirrors, "https://mirror.csclub.uwaterloo.ca/gnome/sources/$1";
                push @mirrors, "http://ftp.acc.umu.se/pub/GNOME/sources/$1";
                push @mirrors, "http://ftp.kaist.ac.kr/gnome/sources/$1";
@@ -286,11 +322,11 @@ while (!-f "$target/$filename") {
        my $mirror = shift @mirrors;
        $mirror or die "No more mirrors to try - giving up.\n";
 
-       download($mirror, $url_filename);
+       download($mirror, $url_filename, @mirrors);
        if (!-f "$target/$filename" && $url_filename ne $filename) {
-               download($mirror, $filename);
+               download($mirror, $filename, @mirrors);
        }
+       @mirrors=();
 }
 
 $SIG{INT} = \&cleanup;
-