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";
29 $url_filename or $url_filename = $filename;
33 open LM
, "$scriptdir/localmirrors" and do {
36 push @mlist, $_ if $_;
40 open CONFIG
, "<".$ENV{'TOPDIR'}."/.config" and do {
42 /^CONFIG_LOCALMIRROR="(.+)"/ and do {
44 my @local_mirrors = split(/;/, $1);
45 push @mlist, @local_mirrors;
51 my $mirror = $ENV{'DOWNLOAD_MIRROR'};
52 $mirror and push @mlist, split(/;/, $mirror);
59 my $res = `command -v $prog`;
65 my $len = length($file_hash);
68 $len == 64 and return "$ENV{'MKHASH'} sha256";
69 $len == 32 and return "$ENV{'MKHASH'} md5";
74 my $tool_name = shift;
75 my $compare_line = shift;
78 if (open TOOL
, "$tool_name --version 2>/dev/null |") {
79 if (defined(my $line = readline TOOL
)) {
80 $present = 1 if $line =~ /^$compare_line /;
91 my $additional_mirrors = join(" ", map "$_/$filename", @_);
93 my @chArray = ('a'..'z', 'A'..'Z', 0..9);
94 my $rfn = join '', "${filename}_", map{ $chArray[int rand @chArray] } 0..9;
96 if (tool_present
('aria2c', 'aria2')) {
98 return join(" ", "[ -d $ENV{'TMPDIR'}/aria2c ] || mkdir $ENV{'TMPDIR'}/aria2c;",
99 "touch $ENV{'TMPDIR'}/aria2c/${rfn}_spp;",
100 qw(aria2c --stderr -c -x2 -s10 -j10 -k1M), $url, $additional_mirrors,
101 $check_certificate ?
() : '--check-certificate=false',
102 "--server-stat-of=$ENV{'TMPDIR'}/aria2c/${rfn}_spp",
103 "--server-stat-if=$ENV{'TMPDIR'}/aria2c/${rfn}_spp",
104 "-d $ENV{'TMPDIR'}/aria2c -o $rfn;",
105 "cat $ENV{'TMPDIR'}/aria2c/$rfn;",
106 "rm $ENV{'TMPDIR'}/aria2c/$rfn $ENV{'TMPDIR'}/aria2c/${rfn}_spp");
107 } elsif (tool_present
('curl', 'curl')) {
108 return (qw(curl -f --connect-timeout 20 --retry 5 --location),
109 $check_certificate ?
() : '--insecure',
110 shellwords
($ENV{CURL_OPTIONS
} || ''),
113 return (qw(wget --tries=5 --timeout=20 --output-document=-),
114 $check_certificate ?
() : '--no-check-certificate',
115 shellwords
($ENV{WGET_OPTIONS
} || ''),
120 my $hash_cmd = hash_cmd
();
121 $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";
126 my $download_filename = shift;
127 my @additional_mirrors = @_;
131 if ($mirror =~ s!^file://!!) {
132 if (! -d
"$mirror") {
133 print STDERR
"Wrong local cache directory -$mirror-.\n";
138 if (! -d
"$target") {
139 system("mkdir", "-p", "$target/");
142 if (! open TMPDLS
, "find $mirror -follow -name $filename 2>/dev/null |") {
143 print("Failed to search for $filename in $mirror\n");
149 while (defined(my $line = readline TMPDLS
)) {
150 chomp ($link = $line);
152 print("$. or more instances of $filename in $mirror found . Only one instance allowed.\n");
160 print("No instances of $filename found in $mirror.\n");
164 print("Copying $filename from $link\n");
165 copy
($link, "$target/$filename.dl");
168 if (system("cat '$target/$filename.dl' | $hash_cmd > '$target/$filename.hash'")) {
169 print("Failed to generate hash for $filename\n");
174 my @cmd = download_cmd
("$mirror/$download_filename", $download_filename, @additional_mirrors);
175 print STDERR
"+ ".join(" ",@cmd)."\n";
176 open(FETCH_FD
, '-|', @cmd) or die "Cannot launch aria2c, curl or wget.\n";
178 open MD5SUM
, "| $hash_cmd > '$target/$filename.hash'" or die "Cannot launch $hash_cmd.\n";
180 open OUTPUT
, "> $target/$filename.dl" or die "Cannot create file $target/$filename.dl: $!\n";
182 while (read FETCH_FD
, $buffer, 1048576) {
183 $hash_cmd and print MD5SUM
$buffer;
184 print OUTPUT
$buffer;
186 $hash_cmd and close MD5SUM
;
191 print STDERR
"Download failed.\n";
198 my $sum = `cat "$target/$filename.hash"`;
199 $sum =~ /^(\w+)\s*/ or die "Could not generate file hash\n";
202 if ($sum ne $file_hash) {
203 print STDERR
"Hash of the downloaded file does not match (file: $sum, requested: $file_hash) - deleting download.\n";
209 unlink "$target/$filename";
210 system("mv", "$target/$filename.dl", "$target/$filename");
216 unlink "$target/$filename.dl";
217 unlink "$target/$filename.hash";
220 @mirrors = localmirrors
();
222 foreach my $mirror (@ARGV) {
223 if ($mirror =~ /^\@SF\/(.+)$/) {
224 # give sourceforge a few more tries, because it redirects to different mirrors
226 push @mirrors, "https://downloads.sourceforge.net/$1";
228 } elsif ($mirror =~ /^\@OPENWRT$/) {
229 # use OpenWrt source server directly
230 } elsif ($mirror =~ /^\@DEBIAN\/(.+)$/) {
231 push @mirrors, "https://ftp.debian.org/debian/$1";
232 push @mirrors, "https://mirror.leaseweb.com/debian/$1";
233 push @mirrors, "https://mirror.netcologne.de/debian/$1";
234 push @mirrors, "https://mirrors.tuna.tsinghua.edu.cn/debian/$1";
235 push @mirrors, "https://mirrors.ustc.edu.cn/debian/$1"
236 } elsif ($mirror =~ /^\@APACHE\/(.+)$/) {
237 push @mirrors, "https://mirror.netcologne.de/apache.org/$1";
238 push @mirrors, "https://mirror.aarnet.edu.au/pub/apache/$1";
239 push @mirrors, "https://mirror.csclub.uwaterloo.ca/apache/$1";
240 push @mirrors, "https://archive.apache.org/dist/$1";
241 push @mirrors, "http://mirror.cogentco.com/pub/apache/$1";
242 push @mirrors, "http://mirror.navercorp.com/apache/$1";
243 push @mirrors, "http://ftp.jaist.ac.jp/pub/apache/$1";
244 push @mirrors, "ftp://apache.cs.utah.edu/apache.org/$1";
245 push @mirrors, "ftp://apache.mirrors.ovh.net/ftp.apache.org/dist/$1";
246 push @mirrors, "https://mirrors.tuna.tsinghua.edu.cn/apache/$1";
247 push @mirrors, "https://mirrors.ustc.edu.cn/apache/$1";
248 } elsif ($mirror =~ /^\@GITHUB\/(.+)$/) {
249 # give github a few more tries (different mirrors)
251 push @mirrors, "https://raw.githubusercontent.com/$1";
253 } elsif ($mirror =~ /^\@GNU\/(.+)$/) {
254 push @mirrors, "https://mirror.csclub.uwaterloo.ca/gnu/$1";
255 push @mirrors, "https://mirror.netcologne.de/gnu/$1";
256 push @mirrors, "http://ftp.kddilabs.jp/GNU/gnu/$1";
257 push @mirrors, "http://www.nic.funet.fi/pub/gnu/gnu/$1";
258 push @mirrors, "http://mirror.internode.on.net/pub/gnu/$1";
259 push @mirrors, "http://mirror.navercorp.com/gnu/$1";
260 push @mirrors, "ftp://mirrors.rit.edu/gnu/$1";
261 push @mirrors, "ftp://download.xs4all.nl/pub/gnu/$1";
262 push @mirrors, "https://ftp.gnu.org/gnu/$1";
263 push @mirrors, "https://mirrors.tuna.tsinghua.edu.cn/gnu/$1";
264 push @mirrors, "https://mirrors.ustc.edu.cn/gnu/$1";
265 } elsif ($mirror =~ /^\@SAVANNAH\/(.+)$/) {
266 push @mirrors, "https://mirror.netcologne.de/savannah/$1";
267 push @mirrors, "https://mirror.csclub.uwaterloo.ca/nongnu/$1";
268 push @mirrors, "http://ftp.acc.umu.se/mirror/gnu.org/savannah/$1";
269 push @mirrors, "http://nongnu.uib.no/$1";
270 push @mirrors, "http://ftp.igh.cnrs.fr/pub/nongnu/$1";
271 push @mirrors, "ftp://cdimage.debian.org/mirror/gnu.org/savannah/$1";
272 push @mirrors, "ftp://ftp.acc.umu.se/mirror/gnu.org/savannah/$1";
273 } elsif ($mirror =~ /^\@KERNEL\/(.+)$/) {
275 if ($filename =~ /linux-\d+\.\d+(?:\.\d+)?-rc/) {
276 push @extra, "$extra[0]/testing";
277 } elsif ($filename =~ /linux-(\d+\.\d+(?:\.\d+)?)/) {
278 push @extra, "$extra[0]/longterm/v$1";
280 foreach my $dir (@extra) {
281 push @mirrors, "https://cdn.kernel.org/pub/$dir";
282 push @mirrors, "https://download.xs4all.nl/ftp.kernel.org/pub/$dir";
283 push @mirrors, "https://mirrors.mit.edu/kernel/$dir";
284 push @mirrors, "http://ftp.nara.wide.ad.jp/pub/kernel.org/$dir";
285 push @mirrors, "http://www.ring.gr.jp/archives/linux/kernel.org/$dir";
286 push @mirrors, "ftp://ftp.riken.jp/Linux/kernel.org/$dir";
287 push @mirrors, "ftp://www.mirrorservice.org/sites/ftp.kernel.org/pub/$dir";
288 push @mirrors, "https://mirrors.tuna.tsinghua.edu.cn/kernel/$dir";
289 push @mirrors, "https://mirrors.ustc.edu.cn/kernel.org/$dir";
291 } elsif ($mirror =~ /^\@GNOME\/(.+)$/) {
292 push @mirrors, "https://download.gnome.org/sources/$1";
293 push @mirrors, "https://mirror.csclub.uwaterloo.ca/gnome/sources/$1";
294 push @mirrors, "http://ftp.acc.umu.se/pub/GNOME/sources/$1";
295 push @mirrors, "http://ftp.kaist.ac.kr/gnome/sources/$1";
296 push @mirrors, "http://www.mirrorservice.org/sites/ftp.gnome.org/pub/GNOME/sources/$1";
297 push @mirrors, "http://mirror.internode.on.net/pub/gnome/sources/$1";
298 push @mirrors, "http://ftp.belnet.be/ftp.gnome.org/sources/$1";
299 push @mirrors, "ftp://ftp.cse.buffalo.edu/pub/Gnome/sources/$1";
300 push @mirrors, "ftp://ftp.nara.wide.ad.jp/pub/X11/GNOME/sources/$1";
301 push @mirrors, "https://mirrors.ustc.edu.cn/gnome/sources/$1";
303 push @mirrors, $mirror;
307 push @mirrors, 'https://sources.cdn.openwrt.org';
308 push @mirrors, 'https://sources.openwrt.org';
309 push @mirrors, 'https://mirror2.openwrt.org/sources';
311 if (-f
"$target/$filename") {
313 if (system("cat '$target/$filename' | $hash_cmd > '$target/$filename.hash'")) {
314 die "Failed to generate hash for $filename\n";
317 my $sum = `cat "$target/$filename.hash"`;
318 $sum =~ /^(\w+)\s*/ or die "Could not generate file hash\n";
322 exit 0 if $sum eq $file_hash;
324 die "Hash of the local file $filename does not match (file: $sum, requested: $file_hash) - deleting download.\n";
325 unlink "$target/$filename";
329 while (!-f
"$target/$filename") {
330 my $mirror = shift @mirrors;
331 $mirror or die "No more mirrors to try - giving up.\n";
333 download
($mirror, $url_filename, @mirrors);
334 if (!-f
"$target/$filename" && $url_filename ne $filename) {
335 download
($mirror, $filename, @mirrors);
339 $SIG{INT
} = \
&cleanup
;