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