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 $url_filename or $url_filename = $filename;
31 open LM, "$scriptdir/localmirrors" and do {
34 push @mlist, $_ if $_;
38 open CONFIG, "<".$ENV{'TOPDIR'}."/.config" and do {
40 /^CONFIG_LOCALMIRROR="(.+)"/ and do {
42 my @local_mirrors = split(/;/, $1);
43 push @mlist, @local_mirrors;
49 my $mirror = $ENV{'DOWNLOAD_MIRROR'};
50 $mirror and push @mlist, split(/;/, $mirror);
57 my $res = `which $prog`;
59 $res =~ /^no / and return undef;
60 $res =~ /not found/ and return undef;
65 my $len = length($file_hash);
68 $len == 64 and return "mkhash sha256";
69 $len == 32 and return "mkhash md5";
77 if (open CURL, '-|', 'curl', '--version') {
78 if (defined(my $line = readline CURL)) {
79 $have_curl = 1 if $line =~ /^curl /;
85 ? (qw(curl -f --connect-timeout 20 --retry 5 --location --insecure), shellwords($ENV{CURL_OPTIONS} || ''), $url)
86 : (qw(wget --tries=5 --timeout=20 --no-check-certificate --output-document=-), shellwords($ENV{WGET_OPTIONS} || ''), $url)
90 my $hash_cmd = hash_cmd();
91 $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";
99 if ($mirror =~ s!^file://!!) {
100 if (! -d "$mirror") {
101 print STDERR "Wrong local cache directory -$mirror-.\n";
106 if (! -d "$target") {
107 system("mkdir", "-p", "$target/");
110 if (! open TMPDLS, "find $mirror -follow -name $filename 2>/dev/null |") {
111 print("Failed to search for $filename in $mirror\n");
117 while (defined(my $line = readline TMPDLS)) {
118 chomp ($link = $line);
120 print("$. or more instances of $filename in $mirror found . Only one instance allowed.\n");
128 print("No instances of $filename found in $mirror.\n");
132 print("Copying $filename from $link\n");
133 copy($link, "$target/$filename.dl");
136 if (system("cat '$target/$filename.dl' | $hash_cmd > '$target/$filename.hash'")) {
137 print("Failed to generate hash for $filename\n");
142 my @cmd = download_cmd("$mirror/$url_filename");
143 print STDERR "+ ".join(" ",@cmd)."\n";
144 open(FETCH_FD, '-|', @cmd) or die "Cannot launch curl or wget.\n";
146 open MD5SUM, "| $hash_cmd > '$target/$filename.hash'" or die "Cannot launch $hash_cmd.\n";
148 open OUTPUT, "> $target/$filename.dl" or die "Cannot create file $target/$filename.dl: $!\n";
150 while (read FETCH_FD, $buffer, 1048576) {
151 $hash_cmd and print MD5SUM $buffer;
152 print OUTPUT $buffer;
154 $hash_cmd and close MD5SUM;
159 print STDERR "Download failed.\n";
166 my $sum = `cat "$target/$filename.hash"`;
167 $sum =~ /^(\w+)\s*/ or die "Could not generate file hash\n";
170 if ($sum ne $file_hash) {
171 print STDERR "Hash of the downloaded file does not match (file: $sum, requested: $file_hash) - deleting download.\n";
177 unlink "$target/$filename";
178 system("mv", "$target/$filename.dl", "$target/$filename");
184 unlink "$target/$filename.dl";
185 unlink "$target/$filename.hash";
188 @mirrors = localmirrors();
190 foreach my $mirror (@ARGV) {
191 if ($mirror =~ /^\@SF\/(.+)$/) {
192 # give sourceforge a few more tries, because it redirects to different mirrors
194 push @mirrors, "https://downloads.sourceforge.net/$1";
196 } elsif ($mirror =~ /^\@APACHE\/(.+)$/) {
197 push @mirrors, "https://mirror.netcologne.de/apache.org/$1";
198 push @mirrors, "https://mirror.aarnet.edu.au/pub/apache/$1";
199 push @mirrors, "https://mirror.csclub.uwaterloo.ca/apache/$1";
200 push @mirrors, "https://archive.apache.org/dist/$1";
201 push @mirrors, "http://mirror.cogentco.com/pub/apache/$1";
202 push @mirrors, "http://mirror.navercorp.com/apache/$1";
203 push @mirrors, "http://ftp.jaist.ac.jp/pub/apache/$1";
204 push @mirrors, "ftp://apache.cs.utah.edu/apache.org/$1";
205 push @mirrors, "ftp://apache.mirrors.ovh.net/ftp.apache.org/dist/$1";
206 } elsif ($mirror =~ /^\@GITHUB\/(.+)$/) {
207 # give github a few more tries (different mirrors)
209 push @mirrors, "https://raw.githubusercontent.com/$1";
211 } elsif ($mirror =~ /^\@GNU\/(.+)$/) {
212 push @mirrors, "https://mirror.csclub.uwaterloo.ca/gnu/$1";
213 push @mirrors, "https://mirror.netcologne.de/gnu/$1";
214 push @mirrors, "http://ftp.kddilabs.jp/GNU/gnu/$1";
215 push @mirrors, "http://www.nic.funet.fi/pub/gnu/gnu/$1";
216 push @mirrors, "http://mirror.internode.on.net/pub/gnu/$1";
217 push @mirrors, "http://mirror.navercorp.com/gnu/$1";
218 push @mirrors, "ftp://mirrors.rit.edu/gnu/$1";
219 push @mirrors, "ftp://download.xs4all.nl/pub/gnu/";
220 } elsif ($mirror =~ /^\@SAVANNAH\/(.+)$/) {
221 push @mirrors, "https://mirror.netcologne.de/savannah/$1";
222 push @mirrors, "https://mirror.csclub.uwaterloo.ca/nongnu/$1";
223 push @mirrors, "http://ftp.acc.umu.se/mirror/gnu.org/savannah/$1";
224 push @mirrors, "http://nongnu.uib.no/$1";
225 push @mirrors, "http://ftp.igh.cnrs.fr/pub/nongnu/$1";
226 push @mirrors, "http://public.p-knowledge.co.jp/Savannah-nongnu-mirror/$1";
227 push @mirrors, "ftp://cdimage.debian.org/mirror/gnu.org/savannah/$1";
228 push @mirrors, "ftp://ftp.acc.umu.se/mirror/gnu.org/savannah/$1";
229 } elsif ($mirror =~ /^\@KERNEL\/(.+)$/) {
231 if ($filename =~ /linux-\d+\.\d+(?:\.\d+)?-rc/) {
232 push @extra, "$extra[0]/testing";
233 } elsif ($filename =~ /linux-(\d+\.\d+(?:\.\d+)?)/) {
234 push @extra, "$extra[0]/longterm/v$1";
236 foreach my $dir (@extra) {
237 push @mirrors, "https://cdn.kernel.org/pub/$dir";
238 push @mirrors, "https://mirror.rackspace.com/kernel.org/pub/$dir";
239 push @mirrors, "https://download.xs4all.nl/ftp.kernel.org/pub/$dir";
240 push @mirrors, "https://mirrors.mit.edu/kernel/$dir";
241 push @mirrors, "http://ftp.nara.wide.ad.jp/pub/kernel.org/$dir";
242 push @mirrors, "http://www.ring.gr.jp/archives/linux/kernel.org/$dir";
243 push @mirrors, "ftp://ftp.riken.jp/Linux/kernel.org/$dir";
244 push @mirrors, "ftp://www.mirrorservice.org/sites/ftp.kernel.org/pub/$dir";
246 } elsif ($mirror =~ /^\@GNOME\/(.+)$/) {
247 push @mirrors, "https://mirror.csclub.uwaterloo.ca/gnome/sources/$1";
248 push @mirrors, "http://ftp.acc.umu.se/pub/GNOME/sources/$1";
249 push @mirrors, "http://ftp.kaist.ac.kr/gnome/sources/$1";
250 push @mirrors, "http://www.mirrorservice.org/sites/ftp.gnome.org/pub/GNOME/sources/$1";
251 push @mirrors, "http://mirror.internode.on.net/pub/gnome/sources/$1";
252 push @mirrors, "http://ftp.belnet.be/ftp.gnome.org/sources/$1";
253 push @mirrors, "ftp://ftp.cse.buffalo.edu/pub/Gnome/sources/$1";
254 push @mirrors, "ftp://ftp.nara.wide.ad.jp/pub/X11/GNOME/sources/$1";
257 push @mirrors, $mirror;
261 #push @mirrors, 'https://mirror1.openwrt.org';
262 push @mirrors, 'https://sources.openwrt.org';
263 push @mirrors, 'https://mirror2.openwrt.org/sources';
265 while (!-f "$target/$filename") {
266 my $mirror = shift @mirrors;
267 $mirror or die "No more mirrors to try - giving up.\n";
272 $SIG{INT} = \&cleanup;