3 # Copyright (C) 2006 OpenWrt.org
4 # Copyright (C) 2016 LEDE project
6 # This is free software, licensed under the GNU General Public License v2.
7 # See /LICENSE for more information.
16 @ARGV > 2 or die "Syntax: $0 <target dir> <filename> <hash> <url filename> [<mirror> ...]\n";
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);
27 my $check_certificate = $ENV{DOWNLOAD_CHECK_CERTIFICATE
} eq "y";
28 my $custom_tool = $ENV{DOWNLOAD_TOOL_CUSTOM
};
31 $url_filename or $url_filename = $filename;
35 open LM
, "$scriptdir/localmirrors" and do {
38 push @mlist, $_ if $_;
42 open CONFIG
, "<".$ENV{'TOPDIR'}."/.config" and do {
44 /^CONFIG_LOCALMIRROR="(.+)"/ and do {
46 my @local_mirrors = split(/;/, $1);
47 push @mlist, @local_mirrors;
53 my $mirror = $ENV{'DOWNLOAD_MIRROR'};
54 $mirror and push @mlist, split(/;/, $mirror);
61 my $res = `command -v $prog`;
67 my $len = length($file_hash);
70 $len == 64 and return "$ENV{'MKHASH'} sha256";
71 $len == 32 and return "$ENV{'MKHASH'} md5";
76 my $tool_name = shift;
77 my $compare_line = shift;
80 if (open TOOL
, "$tool_name --version 2>/dev/null |") {
81 if (defined(my $line = readline TOOL
)) {
82 $present = 1 if $line =~ /^$compare_line /;
91 $custom_tool =~ tr/"//d;
96 # Try to use curl if available
97 if (tool_present
("curl", "curl")) {
101 # No tool found, fallback to wget
107 my $filename = shift;
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
} || ''),
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
} || ''),
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;
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");
137 return join(" ", $download_tool, $url);
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";
147 my $download_filename = shift;
148 my @additional_mirrors = @_;
152 if ($mirror =~ s!^file://!!) {
153 if (! -d
"$mirror") {
154 print STDERR
"Wrong local cache directory -$mirror-.\n";
159 if (! -d
"$target") {
160 system("mkdir", "-p", "$target/");
163 if (! open TMPDLS
, "find $mirror -follow -name $filename 2>/dev/null |") {
164 print("Failed to search for $filename in $mirror\n");
170 while (defined(my $line = readline TMPDLS
)) {
171 chomp ($link = $line);
173 print("$. or more instances of $filename in $mirror found . Only one instance allowed.\n");
181 print("No instances of $filename found in $mirror.\n");
185 print("Copying $filename from $link\n");
186 copy
($link, "$target/$filename.dl");
189 if (system("cat '$target/$filename.dl' | $hash_cmd > '$target/$filename.hash'")) {
190 print("Failed to generate hash for $filename\n");
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";
199 open MD5SUM
, "| $hash_cmd > '$target/$filename.hash'" or die "Cannot launch $hash_cmd.\n";
201 open OUTPUT
, "> $target/$filename.dl" or die "Cannot create file $target/$filename.dl: $!\n";
203 while (read FETCH_FD
, $buffer, 1048576) {
204 $hash_cmd and print MD5SUM
$buffer;
205 print OUTPUT
$buffer;
207 $hash_cmd and close MD5SUM
;
212 print STDERR
"Download failed.\n";
219 my $sum = `cat "$target/$filename.hash"`;
220 $sum =~ /^(\w+)\s*/ or die "Could not generate file hash\n";
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";
230 unlink "$target/$filename";
231 system("mv", "$target/$filename.dl", "$target/$filename");
237 unlink "$target/$filename.dl";
238 unlink "$target/$filename.hash";
241 @mirrors = localmirrors
();
243 foreach my $mirror (@ARGV) {
244 if ($mirror =~ /^\@SF\/(.+)$/) {
245 # give sourceforge a few more tries, because it redirects to different mirrors
247 push @mirrors, "https://downloads.sourceforge.net/$1";
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)
273 push @mirrors, "https://raw.githubusercontent.com/$1";
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\/(.+)$/) {
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";
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";
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";
325 push @mirrors, $mirror;
329 push @mirrors, 'https://sources.cdn.openwrt.org';
330 push @mirrors, 'https://sources.openwrt.org';
331 push @mirrors, 'https://mirror2.openwrt.org/sources';
333 if (-f
"$target/$filename") {
335 if (system("cat '$target/$filename' | $hash_cmd > '$target/$filename.hash'")) {
336 die "Failed to generate hash for $filename\n";
339 my $sum = `cat "$target/$filename.hash"`;
340 $sum =~ /^(\w+)\s*/ or die "Could not generate file hash\n";
344 exit 0 if $sum eq $file_hash;
346 die "Hash of the local file $filename does not match (file: $sum, requested: $file_hash) - deleting download.\n";
347 unlink "$target/$filename";
351 $download_tool = select_tool
();
353 while (!-f
"$target/$filename") {
354 my $mirror = shift @mirrors;
355 $mirror or die "No more mirrors to try - giving up.\n";
357 download
($mirror, $url_filename, @mirrors);
358 if (!-f
"$target/$filename" && $url_filename ne $filename) {
359 download
($mirror, $filename, @mirrors);
363 $SIG{INT
} = \
&cleanup
;