download.pl: add aria2c support
[openwrt/staging/hauke.git] / scripts / download.pl
1 #!/usr/bin/env perl
2 #
3 # Copyright (C) 2006 OpenWrt.org
4 # Copyright (C) 2016 LEDE project
5 #
6 # This is free software, licensed under the GNU General Public License v2.
7 # See /LICENSE for more information.
8 #
9
10 use strict;
11 use warnings;
12 use File::Basename;
13 use File::Copy;
14 use Text::ParseWords;
15
16 @ARGV > 2 or die "Syntax: $0 <target dir> <filename> <hash> <url filename> [<mirror> ...]\n";
17
18 my $url_filename;
19 my $target = glob(shift @ARGV);
20 my $filename = shift @ARGV;
21 my $file_hash = shift @ARGV;
22 $url_filename = shift @ARGV unless $ARGV[0] =~ /:\/\//;
23 my $scriptdir = dirname($0);
24 my @mirrors;
25 my $ok;
26
27 my $check_certificate = $ENV{DOWNLOAD_CHECK_CERTIFICATE} eq "y";
28
29 $url_filename or $url_filename = $filename;
30
31 sub localmirrors {
32 my @mlist;
33 open LM, "$scriptdir/localmirrors" and do {
34 while (<LM>) {
35 chomp $_;
36 push @mlist, $_ if $_;
37 }
38 close LM;
39 };
40 open CONFIG, "<".$ENV{'TOPDIR'}."/.config" and do {
41 while (<CONFIG>) {
42 /^CONFIG_LOCALMIRROR="(.+)"/ and do {
43 chomp;
44 my @local_mirrors = split(/;/, $1);
45 push @mlist, @local_mirrors;
46 };
47 }
48 close CONFIG;
49 };
50
51 my $mirror = $ENV{'DOWNLOAD_MIRROR'};
52 $mirror and push @mlist, split(/;/, $mirror);
53
54 return @mlist;
55 }
56
57 sub which($) {
58 my $prog = shift;
59 my $res = `command -v $prog`;
60 $res or return undef;
61 return $res;
62 }
63
64 sub hash_cmd() {
65 my $len = length($file_hash);
66 my $cmd;
67
68 $len == 64 and return "$ENV{'MKHASH'} sha256";
69 $len == 32 and return "$ENV{'MKHASH'} md5";
70 return undef;
71 }
72
73 sub download_cmd {
74 my $url = shift;
75 my $have_curl = 0;
76 my $have_aria2c = 0;
77 my $filename = shift;
78 my @additional_mirrors = @_;
79 my $mirrors_url = "'$url'";
80
81 my @chArray = ('a'..'z', 'A'..'Z', 0..9);
82 my $rfn = join '', map{ $chArray[int rand @chArray] } 0..9;
83 if (open CURL, '-|', 'curl', '--version') {
84 if (defined(my $line = readline CURL)) {
85 $have_curl = 1 if $line =~ /^curl /;
86 }
87 close CURL;
88 }
89 if (open ARIA2C, '-|', 'aria2c', '--version') {
90 if (defined(my $line = readline ARIA2C)) {
91 $have_aria2c = 1 if $line =~ /^aria2 /;
92 }
93 close ARIA2C;
94 }
95
96 for my $mirror (@additional_mirrors ) {
97 $mirrors_url = $mirrors_url ." '$mirror /$filename'";
98 }
99
100 if ($have_aria2c) {
101 return join(" ", "touch /dev/shm/${rfn}_spp;",
102 qw(aria2c --stderr -c -x2 -s10 -j10 -k1M), $mirrors_url ,
103 $check_certificate ? () : '--check-certificate=false',
104 "--server-stat-of=/dev/shm/${rfn}_spp",
105 "--server-stat-if=/dev/shm/${rfn}_spp",
106 "-d /dev/shm -o $rfn;",
107 "cat /dev/shm/$rfn;", "rm /dev/shm/$rfn /dev/shm/${rfn}_spp");
108 } elsif ($have_curl) {
109 return (qw(curl -f --connect-timeout 20 --retry 5 --location),
110 $check_certificate ? () : '--insecure',
111 shellwords($ENV{CURL_OPTIONS} || ''),
112 $url);
113 } else {
114 return (qw(wget --tries=5 --timeout=20 --output-document=-),
115 $check_certificate ? () : '--no-check-certificate',
116 shellwords($ENV{WGET_OPTIONS} || ''),
117 $url);
118 }
119 }
120
121 my $hash_cmd = hash_cmd();
122 $hash_cmd or ($file_hash eq "skip") or die "Cannot find appropriate hash command, ensure the provided hash is either a MD5 or SHA256 checksum.\n";
123
124 sub download
125 {
126 my $mirror = shift;
127 my $download_filename = shift;
128 my @additional_mirrors = @_;
129
130 $mirror =~ s!/$!!;
131
132 if ($mirror =~ s!^file://!!) {
133 if (! -d "$mirror") {
134 print STDERR "Wrong local cache directory -$mirror-.\n";
135 cleanup();
136 return;
137 }
138
139 if (! -d "$target") {
140 system("mkdir", "-p", "$target/");
141 }
142
143 if (! open TMPDLS, "find $mirror -follow -name $filename 2>/dev/null |") {
144 print("Failed to search for $filename in $mirror\n");
145 return;
146 }
147
148 my $link;
149
150 while (defined(my $line = readline TMPDLS)) {
151 chomp ($link = $line);
152 if ($. > 1) {
153 print("$. or more instances of $filename in $mirror found . Only one instance allowed.\n");
154 return;
155 }
156 }
157
158 close TMPDLS;
159
160 if (! $link) {
161 print("No instances of $filename found in $mirror.\n");
162 return;
163 }
164
165 print("Copying $filename from $link\n");
166 copy($link, "$target/$filename.dl");
167
168 $hash_cmd and do {
169 if (system("cat '$target/$filename.dl' | $hash_cmd > '$target/$filename.hash'")) {
170 print("Failed to generate hash for $filename\n");
171 return;
172 }
173 };
174 } else {
175 my @cmd = download_cmd("$mirror/$download_filename", $download_filename, @additional_mirrors);
176 print STDERR "+ ".join(" ",@cmd)."\n";
177 open(FETCH_FD, '-|', @cmd) or die "Cannot launch aria2c, curl or wget.\n";
178 $hash_cmd and do {
179 open MD5SUM, "| $hash_cmd > '$target/$filename.hash'" or die "Cannot launch $hash_cmd.\n";
180 };
181 open OUTPUT, "> $target/$filename.dl" or die "Cannot create file $target/$filename.dl: $!\n";
182 my $buffer;
183 while (read FETCH_FD, $buffer, 1048576) {
184 $hash_cmd and print MD5SUM $buffer;
185 print OUTPUT $buffer;
186 }
187 $hash_cmd and close MD5SUM;
188 close FETCH_FD;
189 close OUTPUT;
190
191 if ($? >> 8) {
192 print STDERR "Download failed.\n";
193 cleanup();
194 return;
195 }
196 }
197
198 $hash_cmd and do {
199 my $sum = `cat "$target/$filename.hash"`;
200 $sum =~ /^(\w+)\s*/ or die "Could not generate file hash\n";
201 $sum = $1;
202
203 if ($sum ne $file_hash) {
204 print STDERR "Hash of the downloaded file does not match (file: $sum, requested: $file_hash) - deleting download.\n";
205 cleanup();
206 return;
207 }
208 };
209
210 unlink "$target/$filename";
211 system("mv", "$target/$filename.dl", "$target/$filename");
212 cleanup();
213 }
214
215 sub cleanup
216 {
217 unlink "$target/$filename.dl";
218 unlink "$target/$filename.hash";
219 }
220
221 @mirrors = localmirrors();
222
223 foreach my $mirror (@ARGV) {
224 if ($mirror =~ /^\@SF\/(.+)$/) {
225 # give sourceforge a few more tries, because it redirects to different mirrors
226 for (1 .. 5) {
227 push @mirrors, "https://downloads.sourceforge.net/$1";
228 }
229 } elsif ($mirror =~ /^\@OPENWRT$/) {
230 # use OpenWrt source server directly
231 } elsif ($mirror =~ /^\@DEBIAN\/(.+)$/) {
232 push @mirrors, "https://ftp.debian.org/debian/$1";
233 push @mirrors, "https://mirror.leaseweb.com/debian/$1";
234 push @mirrors, "https://mirror.netcologne.de/debian/$1";
235 } elsif ($mirror =~ /^\@APACHE\/(.+)$/) {
236 push @mirrors, "https://mirror.netcologne.de/apache.org/$1";
237 push @mirrors, "https://mirror.aarnet.edu.au/pub/apache/$1";
238 push @mirrors, "https://mirror.csclub.uwaterloo.ca/apache/$1";
239 push @mirrors, "https://archive.apache.org/dist/$1";
240 push @mirrors, "http://mirror.cogentco.com/pub/apache/$1";
241 push @mirrors, "http://mirror.navercorp.com/apache/$1";
242 push @mirrors, "http://ftp.jaist.ac.jp/pub/apache/$1";
243 push @mirrors, "ftp://apache.cs.utah.edu/apache.org/$1";
244 push @mirrors, "ftp://apache.mirrors.ovh.net/ftp.apache.org/dist/$1";
245 } elsif ($mirror =~ /^\@GITHUB\/(.+)$/) {
246 # give github a few more tries (different mirrors)
247 for (1 .. 5) {
248 push @mirrors, "https://raw.githubusercontent.com/$1";
249 }
250 } elsif ($mirror =~ /^\@GNU\/(.+)$/) {
251 push @mirrors, "https://mirror.csclub.uwaterloo.ca/gnu/$1";
252 push @mirrors, "https://mirror.netcologne.de/gnu/$1";
253 push @mirrors, "http://ftp.kddilabs.jp/GNU/gnu/$1";
254 push @mirrors, "http://www.nic.funet.fi/pub/gnu/gnu/$1";
255 push @mirrors, "http://mirror.internode.on.net/pub/gnu/$1";
256 push @mirrors, "http://mirror.navercorp.com/gnu/$1";
257 push @mirrors, "ftp://mirrors.rit.edu/gnu/$1";
258 push @mirrors, "ftp://download.xs4all.nl/pub/gnu/$1";
259 push @mirrors, "https://ftp.gnu.org/gnu/$1";
260 } elsif ($mirror =~ /^\@SAVANNAH\/(.+)$/) {
261 push @mirrors, "https://mirror.netcologne.de/savannah/$1";
262 push @mirrors, "https://mirror.csclub.uwaterloo.ca/nongnu/$1";
263 push @mirrors, "http://ftp.acc.umu.se/mirror/gnu.org/savannah/$1";
264 push @mirrors, "http://nongnu.uib.no/$1";
265 push @mirrors, "http://ftp.igh.cnrs.fr/pub/nongnu/$1";
266 push @mirrors, "ftp://cdimage.debian.org/mirror/gnu.org/savannah/$1";
267 push @mirrors, "ftp://ftp.acc.umu.se/mirror/gnu.org/savannah/$1";
268 } elsif ($mirror =~ /^\@KERNEL\/(.+)$/) {
269 my @extra = ( $1 );
270 if ($filename =~ /linux-\d+\.\d+(?:\.\d+)?-rc/) {
271 push @extra, "$extra[0]/testing";
272 } elsif ($filename =~ /linux-(\d+\.\d+(?:\.\d+)?)/) {
273 push @extra, "$extra[0]/longterm/v$1";
274 }
275 foreach my $dir (@extra) {
276 push @mirrors, "https://cdn.kernel.org/pub/$dir";
277 push @mirrors, "https://download.xs4all.nl/ftp.kernel.org/pub/$dir";
278 push @mirrors, "https://mirrors.mit.edu/kernel/$dir";
279 push @mirrors, "http://ftp.nara.wide.ad.jp/pub/kernel.org/$dir";
280 push @mirrors, "http://www.ring.gr.jp/archives/linux/kernel.org/$dir";
281 push @mirrors, "ftp://ftp.riken.jp/Linux/kernel.org/$dir";
282 push @mirrors, "ftp://www.mirrorservice.org/sites/ftp.kernel.org/pub/$dir";
283 }
284 } elsif ($mirror =~ /^\@GNOME\/(.+)$/) {
285 push @mirrors, "https://download.gnome.org/sources/$1";
286 push @mirrors, "https://mirror.csclub.uwaterloo.ca/gnome/sources/$1";
287 push @mirrors, "http://ftp.acc.umu.se/pub/GNOME/sources/$1";
288 push @mirrors, "http://ftp.kaist.ac.kr/gnome/sources/$1";
289 push @mirrors, "http://www.mirrorservice.org/sites/ftp.gnome.org/pub/GNOME/sources/$1";
290 push @mirrors, "http://mirror.internode.on.net/pub/gnome/sources/$1";
291 push @mirrors, "http://ftp.belnet.be/ftp.gnome.org/sources/$1";
292 push @mirrors, "ftp://ftp.cse.buffalo.edu/pub/Gnome/sources/$1";
293 push @mirrors, "ftp://ftp.nara.wide.ad.jp/pub/X11/GNOME/sources/$1";
294 } else {
295 push @mirrors, $mirror;
296 }
297 }
298
299 push @mirrors, 'https://sources.cdn.openwrt.org';
300 push @mirrors, 'https://sources.openwrt.org';
301 push @mirrors, 'https://mirror2.openwrt.org/sources';
302
303 if (-f "$target/$filename") {
304 $hash_cmd and do {
305 if (system("cat '$target/$filename' | $hash_cmd > '$target/$filename.hash'")) {
306 die "Failed to generate hash for $filename\n";
307 }
308
309 my $sum = `cat "$target/$filename.hash"`;
310 $sum =~ /^(\w+)\s*/ or die "Could not generate file hash\n";
311 $sum = $1;
312
313 cleanup();
314 exit 0 if $sum eq $file_hash;
315
316 die "Hash of the local file $filename does not match (file: $sum, requested: $file_hash) - deleting download.\n";
317 unlink "$target/$filename";
318 };
319 }
320
321 while (!-f "$target/$filename") {
322 my $mirror = shift @mirrors;
323 $mirror or die "No more mirrors to try - giving up.\n";
324
325 download($mirror, $url_filename, @mirrors);
326 if (!-f "$target/$filename" && $url_filename ne $filename) {
327 download($mirror, $filename, @mirrors);
328 }
329 @mirrors=();
330 }
331
332 $SIG{INT} = \&cleanup;