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