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";
77 if (open CURL
, "curl --version 2>/dev/null |") {
78 if (defined(my $line = readline CURL
)) {
79 $have_curl = 1 if $line =~ /^curl /;
85 ?
(qw(curl -f --connect-timeout 20 --retry 5 --location),
86 $check_certificate ?
() : '--insecure',
87 shellwords
($ENV{CURL_OPTIONS
} || ''),
89 : (qw(wget --tries=5 --timeout=20 --output-document=-),
90 $check_certificate ?
() : '--no-check-certificate',
91 shellwords
($ENV{WGET_OPTIONS
} || ''),
96 my $hash_cmd = hash_cmd
();
97 $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";
102 my $download_filename = shift;
106 if ($mirror =~ s!^file://!!) {
107 if (! -d
"$mirror") {
108 print STDERR
"Wrong local cache directory -$mirror-.\n";
113 if (! -d
"$target") {
114 system("mkdir", "-p", "$target/");
117 if (! open TMPDLS
, "find $mirror -follow -name $filename 2>/dev/null |") {
118 print("Failed to search for $filename in $mirror\n");
124 while (defined(my $line = readline TMPDLS
)) {
125 chomp ($link = $line);
127 print("$. or more instances of $filename in $mirror found . Only one instance allowed.\n");
135 print("No instances of $filename found in $mirror.\n");
139 print("Copying $filename from $link\n");
140 copy
($link, "$target/$filename.dl");
143 if (system("cat '$target/$filename.dl' | $hash_cmd > '$target/$filename.hash'")) {
144 print("Failed to generate hash for $filename\n");
149 my @cmd = download_cmd
("$mirror/$download_filename");
150 print STDERR
"+ ".join(" ",@cmd)."\n";
151 open(FETCH_FD
, '-|', @cmd) or die "Cannot launch curl or wget.\n";
153 open MD5SUM
, "| $hash_cmd > '$target/$filename.hash'" or die "Cannot launch $hash_cmd.\n";
155 open OUTPUT
, "> $target/$filename.dl" or die "Cannot create file $target/$filename.dl: $!\n";
157 while (read FETCH_FD
, $buffer, 1048576) {
158 $hash_cmd and print MD5SUM
$buffer;
159 print OUTPUT
$buffer;
161 $hash_cmd and close MD5SUM
;
166 print STDERR
"Download failed.\n";
173 my $sum = `cat "$target/$filename.hash"`;
174 $sum =~ /^(\w+)\s*/ or die "Could not generate file hash\n";
177 if ($sum ne $file_hash) {
178 print STDERR
"Hash of the downloaded file does not match (file: $sum, requested: $file_hash) - deleting download.\n";
184 unlink "$target/$filename";
185 system("mv", "$target/$filename.dl", "$target/$filename");
191 unlink "$target/$filename.dl";
192 unlink "$target/$filename.hash";
195 @mirrors = localmirrors
();
197 foreach my $mirror (@ARGV) {
198 if ($mirror =~ /^\@SF\/(.+)$/) {
199 # give sourceforge a few more tries, because it redirects to different mirrors
201 push @mirrors, "https://downloads.sourceforge.net/$1";
203 } elsif ($mirror =~ /^\@OPENWRT$/) {
204 # use OpenWrt source server directly
205 } elsif ($mirror =~ /^\@DEBIAN\/(.+)$/) {
206 push @mirrors, "https://ftp.debian.org/debian/$1";
207 push @mirrors, "https://mirror.leaseweb.com/debian/$1";
208 push @mirrors, "https://mirror.netcologne.de/debian/$1";
209 } elsif ($mirror =~ /^\@APACHE\/(.+)$/) {
210 push @mirrors, "https://mirror.netcologne.de/apache.org/$1";
211 push @mirrors, "https://mirror.aarnet.edu.au/pub/apache/$1";
212 push @mirrors, "https://mirror.csclub.uwaterloo.ca/apache/$1";
213 push @mirrors, "https://archive.apache.org/dist/$1";
214 push @mirrors, "http://mirror.cogentco.com/pub/apache/$1";
215 push @mirrors, "http://mirror.navercorp.com/apache/$1";
216 push @mirrors, "http://ftp.jaist.ac.jp/pub/apache/$1";
217 push @mirrors, "ftp://apache.cs.utah.edu/apache.org/$1";
218 push @mirrors, "ftp://apache.mirrors.ovh.net/ftp.apache.org/dist/$1";
219 } elsif ($mirror =~ /^\@GITHUB\/(.+)$/) {
220 # give github a few more tries (different mirrors)
222 push @mirrors, "https://raw.githubusercontent.com/$1";
224 } elsif ($mirror =~ /^\@GNU\/(.+)$/) {
225 push @mirrors, "https://mirror.csclub.uwaterloo.ca/gnu/$1";
226 push @mirrors, "https://mirror.netcologne.de/gnu/$1";
227 push @mirrors, "http://ftp.kddilabs.jp/GNU/gnu/$1";
228 push @mirrors, "http://www.nic.funet.fi/pub/gnu/gnu/$1";
229 push @mirrors, "http://mirror.internode.on.net/pub/gnu/$1";
230 push @mirrors, "http://mirror.navercorp.com/gnu/$1";
231 push @mirrors, "ftp://mirrors.rit.edu/gnu/$1";
232 push @mirrors, "ftp://download.xs4all.nl/pub/gnu/$1";
233 push @mirrors, "https://ftp.gnu.org/gnu/$1";
234 } elsif ($mirror =~ /^\@SAVANNAH\/(.+)$/) {
235 push @mirrors, "https://mirror.netcologne.de/savannah/$1";
236 push @mirrors, "https://mirror.csclub.uwaterloo.ca/nongnu/$1";
237 push @mirrors, "http://ftp.acc.umu.se/mirror/gnu.org/savannah/$1";
238 push @mirrors, "http://nongnu.uib.no/$1";
239 push @mirrors, "http://ftp.igh.cnrs.fr/pub/nongnu/$1";
240 push @mirrors, "ftp://cdimage.debian.org/mirror/gnu.org/savannah/$1";
241 push @mirrors, "ftp://ftp.acc.umu.se/mirror/gnu.org/savannah/$1";
242 } elsif ($mirror =~ /^\@KERNEL\/(.+)$/) {
244 if ($filename =~ /linux-\d+\.\d+(?:\.\d+)?-rc/) {
245 push @extra, "$extra[0]/testing";
246 } elsif ($filename =~ /linux-(\d+\.\d+(?:\.\d+)?)/) {
247 push @extra, "$extra[0]/longterm/v$1";
249 foreach my $dir (@extra) {
250 push @mirrors, "https://cdn.kernel.org/pub/$dir";
251 push @mirrors, "https://download.xs4all.nl/ftp.kernel.org/pub/$dir";
252 push @mirrors, "https://mirrors.mit.edu/kernel/$dir";
253 push @mirrors, "http://ftp.nara.wide.ad.jp/pub/kernel.org/$dir";
254 push @mirrors, "http://www.ring.gr.jp/archives/linux/kernel.org/$dir";
255 push @mirrors, "ftp://ftp.riken.jp/Linux/kernel.org/$dir";
256 push @mirrors, "ftp://www.mirrorservice.org/sites/ftp.kernel.org/pub/$dir";
258 } elsif ($mirror =~ /^\@GNOME\/(.+)$/) {
259 push @mirrors, "https://download.gnome.org/sources/$1";
260 push @mirrors, "https://mirror.csclub.uwaterloo.ca/gnome/sources/$1";
261 push @mirrors, "http://ftp.acc.umu.se/pub/GNOME/sources/$1";
262 push @mirrors, "http://ftp.kaist.ac.kr/gnome/sources/$1";
263 push @mirrors, "http://www.mirrorservice.org/sites/ftp.gnome.org/pub/GNOME/sources/$1";
264 push @mirrors, "http://mirror.internode.on.net/pub/gnome/sources/$1";
265 push @mirrors, "http://ftp.belnet.be/ftp.gnome.org/sources/$1";
266 push @mirrors, "ftp://ftp.cse.buffalo.edu/pub/Gnome/sources/$1";
267 push @mirrors, "ftp://ftp.nara.wide.ad.jp/pub/X11/GNOME/sources/$1";
269 push @mirrors, $mirror;
273 push @mirrors, 'https://sources.cdn.openwrt.org';
274 push @mirrors, 'https://sources.openwrt.org';
275 push @mirrors, 'https://mirror2.openwrt.org/sources';
277 if (-f
"$target/$filename") {
279 if (system("cat '$target/$filename' | $hash_cmd > '$target/$filename.hash'")) {
280 die "Failed to generate hash for $filename\n";
283 my $sum = `cat "$target/$filename.hash"`;
284 $sum =~ /^(\w+)\s*/ or die "Could not generate file hash\n";
288 exit 0 if $sum eq $file_hash;
290 die "Hash of the local file $filename does not match (file: $sum, requested: $file_hash) - deleting download.\n";
291 unlink "$target/$filename";
295 while (!-f
"$target/$filename") {
296 my $mirror = shift @mirrors;
297 $mirror or die "No more mirrors to try - giving up.\n";
299 download
($mirror, $url_filename);
300 if (!-f
"$target/$filename" && $url_filename ne $filename) {
301 download
($mirror, $filename);
305 $SIG{INT
} = \
&cleanup
;