scripts/download.pl: fix support for aria2c download tool on macos
[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 = join(" ", map "$_/$filename", @_);
79
80 my @chArray = ('a'..'z', 'A'..'Z', 0..9);
81 my $rfn = join '', "${filename}_", map{ $chArray[int rand @chArray] } 0..9;
82 if (open CURL, '-|', 'curl', '--version') {
83 if (defined(my $line = readline CURL)) {
84 $have_curl = 1 if $line =~ /^curl /;
85 }
86 close CURL;
87 }
88 if (open ARIA2C, '-|', 'aria2c', '--version') {
89 if (defined(my $line = readline ARIA2C)) {
90 $have_aria2c = 1 if $line =~ /^aria2 /;
91 }
92 close ARIA2C;
93 }
94
95 if ($have_aria2c) {
96 @mirrors=();
97 return join(" ", "[ -d $ENV{'TMPDIR'}/aria2c ] || mkdir $ENV{'TMPDIR'}/aria2c;",
98 "touch $ENV{'TMPDIR'}/aria2c/${rfn}_spp;",
99 qw(aria2c --stderr -c -x2 -s10 -j10 -k1M), $url, $additional_mirrors,
100 $check_certificate ? () : '--check-certificate=false',
101 "--server-stat-of=$ENV{'TMPDIR'}/aria2c/${rfn}_spp",
102 "--server-stat-if=$ENV{'TMPDIR'}/aria2c/${rfn}_spp",
103 "-d $ENV{'TMPDIR'}/aria2c -o $rfn;",
104 "cat $ENV{'TMPDIR'}/aria2c/$rfn;",
105 "rm $ENV{'TMPDIR'}/aria2c/$rfn $ENV{'TMPDIR'}/aria2c/${rfn}_spp");
106 } elsif ($have_curl) {
107 return (qw(curl -f --connect-timeout 20 --retry 5 --location),
108 $check_certificate ? () : '--insecure',
109 shellwords($ENV{CURL_OPTIONS} || ''),
110 $url);
111 } else {
112 return (qw(wget --tries=5 --timeout=20 --output-document=-),
113 $check_certificate ? () : '--no-check-certificate',
114 shellwords($ENV{WGET_OPTIONS} || ''),
115 $url);
116 }
117 }
118
119 my $hash_cmd = hash_cmd();
120 $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";
121
122 sub download
123 {
124 my $mirror = shift;
125 my $download_filename = shift;
126 my @additional_mirrors = @_;
127
128 $mirror =~ s!/$!!;
129
130 if ($mirror =~ s!^file://!!) {
131 if (! -d "$mirror") {
132 print STDERR "Wrong local cache directory -$mirror-.\n";
133 cleanup();
134 return;
135 }
136
137 if (! -d "$target") {
138 system("mkdir", "-p", "$target/");
139 }
140
141 if (! open TMPDLS, "find $mirror -follow -name $filename 2>/dev/null |") {
142 print("Failed to search for $filename in $mirror\n");
143 return;
144 }
145
146 my $link;
147
148 while (defined(my $line = readline TMPDLS)) {
149 chomp ($link = $line);
150 if ($. > 1) {
151 print("$. or more instances of $filename in $mirror found . Only one instance allowed.\n");
152 return;
153 }
154 }
155
156 close TMPDLS;
157
158 if (! $link) {
159 print("No instances of $filename found in $mirror.\n");
160 return;
161 }
162
163 print("Copying $filename from $link\n");
164 copy($link, "$target/$filename.dl");
165
166 $hash_cmd and do {
167 if (system("cat '$target/$filename.dl' | $hash_cmd > '$target/$filename.hash'")) {
168 print("Failed to generate hash for $filename\n");
169 return;
170 }
171 };
172 } else {
173 my @cmd = download_cmd("$mirror/$download_filename", $download_filename, @additional_mirrors);
174 print STDERR "+ ".join(" ",@cmd)."\n";
175 open(FETCH_FD, '-|', @cmd) or die "Cannot launch aria2c, curl or wget.\n";
176 $hash_cmd and do {
177 open MD5SUM, "| $hash_cmd > '$target/$filename.hash'" or die "Cannot launch $hash_cmd.\n";
178 };
179 open OUTPUT, "> $target/$filename.dl" or die "Cannot create file $target/$filename.dl: $!\n";
180 my $buffer;
181 while (read FETCH_FD, $buffer, 1048576) {
182 $hash_cmd and print MD5SUM $buffer;
183 print OUTPUT $buffer;
184 }
185 $hash_cmd and close MD5SUM;
186 close FETCH_FD;
187 close OUTPUT;
188
189 if ($? >> 8) {
190 print STDERR "Download failed.\n";
191 cleanup();
192 return;
193 }
194 }
195
196 $hash_cmd and do {
197 my $sum = `cat "$target/$filename.hash"`;
198 $sum =~ /^(\w+)\s*/ or die "Could not generate file hash\n";
199 $sum = $1;
200
201 if ($sum ne $file_hash) {
202 print STDERR "Hash of the downloaded file does not match (file: $sum, requested: $file_hash) - deleting download.\n";
203 cleanup();
204 return;
205 }
206 };
207
208 unlink "$target/$filename";
209 system("mv", "$target/$filename.dl", "$target/$filename");
210 cleanup();
211 }
212
213 sub cleanup
214 {
215 unlink "$target/$filename.dl";
216 unlink "$target/$filename.hash";
217 }
218
219 @mirrors = localmirrors();
220
221 foreach my $mirror (@ARGV) {
222 if ($mirror =~ /^\@SF\/(.+)$/) {
223 # give sourceforge a few more tries, because it redirects to different mirrors
224 for (1 .. 5) {
225 push @mirrors, "https://downloads.sourceforge.net/$1";
226 }
227 } elsif ($mirror =~ /^\@OPENWRT$/) {
228 # use OpenWrt source server directly
229 } elsif ($mirror =~ /^\@DEBIAN\/(.+)$/) {
230 push @mirrors, "https://ftp.debian.org/debian/$1";
231 push @mirrors, "https://mirror.leaseweb.com/debian/$1";
232 push @mirrors, "https://mirror.netcologne.de/debian/$1";
233 } elsif ($mirror =~ /^\@APACHE\/(.+)$/) {
234 push @mirrors, "https://mirror.netcologne.de/apache.org/$1";
235 push @mirrors, "https://mirror.aarnet.edu.au/pub/apache/$1";
236 push @mirrors, "https://mirror.csclub.uwaterloo.ca/apache/$1";
237 push @mirrors, "https://archive.apache.org/dist/$1";
238 push @mirrors, "http://mirror.cogentco.com/pub/apache/$1";
239 push @mirrors, "http://mirror.navercorp.com/apache/$1";
240 push @mirrors, "http://ftp.jaist.ac.jp/pub/apache/$1";
241 push @mirrors, "ftp://apache.cs.utah.edu/apache.org/$1";
242 push @mirrors, "ftp://apache.mirrors.ovh.net/ftp.apache.org/dist/$1";
243 } elsif ($mirror =~ /^\@GITHUB\/(.+)$/) {
244 # give github a few more tries (different mirrors)
245 for (1 .. 5) {
246 push @mirrors, "https://raw.githubusercontent.com/$1";
247 }
248 } elsif ($mirror =~ /^\@GNU\/(.+)$/) {
249 push @mirrors, "https://mirror.csclub.uwaterloo.ca/gnu/$1";
250 push @mirrors, "https://mirror.netcologne.de/gnu/$1";
251 push @mirrors, "http://ftp.kddilabs.jp/GNU/gnu/$1";
252 push @mirrors, "http://www.nic.funet.fi/pub/gnu/gnu/$1";
253 push @mirrors, "http://mirror.internode.on.net/pub/gnu/$1";
254 push @mirrors, "http://mirror.navercorp.com/gnu/$1";
255 push @mirrors, "ftp://mirrors.rit.edu/gnu/$1";
256 push @mirrors, "ftp://download.xs4all.nl/pub/gnu/$1";
257 push @mirrors, "https://ftp.gnu.org/gnu/$1";
258 } elsif ($mirror =~ /^\@SAVANNAH\/(.+)$/) {
259 push @mirrors, "https://mirror.netcologne.de/savannah/$1";
260 push @mirrors, "https://mirror.csclub.uwaterloo.ca/nongnu/$1";
261 push @mirrors, "http://ftp.acc.umu.se/mirror/gnu.org/savannah/$1";
262 push @mirrors, "http://nongnu.uib.no/$1";
263 push @mirrors, "http://ftp.igh.cnrs.fr/pub/nongnu/$1";
264 push @mirrors, "ftp://cdimage.debian.org/mirror/gnu.org/savannah/$1";
265 push @mirrors, "ftp://ftp.acc.umu.se/mirror/gnu.org/savannah/$1";
266 } elsif ($mirror =~ /^\@KERNEL\/(.+)$/) {
267 my @extra = ( $1 );
268 if ($filename =~ /linux-\d+\.\d+(?:\.\d+)?-rc/) {
269 push @extra, "$extra[0]/testing";
270 } elsif ($filename =~ /linux-(\d+\.\d+(?:\.\d+)?)/) {
271 push @extra, "$extra[0]/longterm/v$1";
272 }
273 foreach my $dir (@extra) {
274 push @mirrors, "https://cdn.kernel.org/pub/$dir";
275 push @mirrors, "https://download.xs4all.nl/ftp.kernel.org/pub/$dir";
276 push @mirrors, "https://mirrors.mit.edu/kernel/$dir";
277 push @mirrors, "http://ftp.nara.wide.ad.jp/pub/kernel.org/$dir";
278 push @mirrors, "http://www.ring.gr.jp/archives/linux/kernel.org/$dir";
279 push @mirrors, "ftp://ftp.riken.jp/Linux/kernel.org/$dir";
280 push @mirrors, "ftp://www.mirrorservice.org/sites/ftp.kernel.org/pub/$dir";
281 }
282 } elsif ($mirror =~ /^\@GNOME\/(.+)$/) {
283 push @mirrors, "https://download.gnome.org/sources/$1";
284 push @mirrors, "https://mirror.csclub.uwaterloo.ca/gnome/sources/$1";
285 push @mirrors, "http://ftp.acc.umu.se/pub/GNOME/sources/$1";
286 push @mirrors, "http://ftp.kaist.ac.kr/gnome/sources/$1";
287 push @mirrors, "http://www.mirrorservice.org/sites/ftp.gnome.org/pub/GNOME/sources/$1";
288 push @mirrors, "http://mirror.internode.on.net/pub/gnome/sources/$1";
289 push @mirrors, "http://ftp.belnet.be/ftp.gnome.org/sources/$1";
290 push @mirrors, "ftp://ftp.cse.buffalo.edu/pub/Gnome/sources/$1";
291 push @mirrors, "ftp://ftp.nara.wide.ad.jp/pub/X11/GNOME/sources/$1";
292 } else {
293 push @mirrors, $mirror;
294 }
295 }
296
297 push @mirrors, 'https://sources.cdn.openwrt.org';
298 push @mirrors, 'https://sources.openwrt.org';
299 push @mirrors, 'https://mirror2.openwrt.org/sources';
300
301 if (-f "$target/$filename") {
302 $hash_cmd and do {
303 if (system("cat '$target/$filename' | $hash_cmd > '$target/$filename.hash'")) {
304 die "Failed to generate hash for $filename\n";
305 }
306
307 my $sum = `cat "$target/$filename.hash"`;
308 $sum =~ /^(\w+)\s*/ or die "Could not generate file hash\n";
309 $sum = $1;
310
311 cleanup();
312 exit 0 if $sum eq $file_hash;
313
314 die "Hash of the local file $filename does not match (file: $sum, requested: $file_hash) - deleting download.\n";
315 unlink "$target/$filename";
316 };
317 }
318
319 while (!-f "$target/$filename") {
320 my $mirror = shift @mirrors;
321 $mirror or die "No more mirrors to try - giving up.\n";
322
323 download($mirror, $url_filename, @mirrors);
324 if (!-f "$target/$filename" && $url_filename ne $filename) {
325 download($mirror, $filename, @mirrors);
326 }
327 }
328
329 $SIG{INT} = \&cleanup;