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.
15 @ARGV > 2 or die "Syntax: $0 <target dir> <filename> <hash> <url filename> [<mirror> ...]\n";
18 my $target = shift @ARGV;
19 my $filename = shift @ARGV;
20 my $file_hash = shift @ARGV;
21 $url_filename = shift @ARGV unless $ARGV[0] =~ /:\/\//;
22 my $scriptdir = dirname($0);
26 $url_filename or $url_filename = $filename;
30 open LM, "$scriptdir/localmirrors" and do {
33 push @mlist, $_ if $_;
37 open CONFIG, "<".$ENV{'TOPDIR'}."/.config" and do {
39 /^CONFIG_LOCALMIRROR="(.+)"/ and do {
41 my @local_mirrors = split(/;/, $1);
42 push @mlist, @local_mirrors;
48 my $mirror = $ENV{'DOWNLOAD_MIRROR'};
49 $mirror and push @mlist, split(/;/, $mirror);
56 my $res = `which $prog`;
58 $res =~ /^no / and return undef;
59 $res =~ /not found/ and return undef;
64 my $len = length($file_hash);
67 $len == 64 and return "openssl dgst -sha256 | sed -e 's,.*= ,,'";
69 my $cmd = which("md5sum") || which("md5") || die 'no md5 checksum program found, please install md5 or md5sum';
76 my $hash_cmd = hash_cmd();
81 my $options = $ENV{WGET_OPTIONS} || "";
85 if ($mirror =~ s!^file://!!) {
87 print STDERR "Wrong local cache directory -$mirror-.\n";
93 system("mkdir", "-p", "$target/");
96 if (! open TMPDLS, "find $mirror -follow -name $filename 2>/dev/null |") {
97 print("Failed to search for $filename in $mirror\n");
103 while (defined(my $line = readline TMPDLS)) {
104 chomp ($link = $line);
106 print("$. or more instances of $filename in $mirror found . Only one instance allowed.\n");
114 print("No instances of $filename found in $mirror.\n");
118 print("Copying $filename from $link\n");
119 copy($link, "$target/$filename.dl");
122 if (system("cat '$target/$filename.dl' | $hash_cmd > '$target/$filename.hash'")) {
123 print("Failed to generate hash for $filename\n");
128 open WGET, "wget -t5 --timeout=20 --no-check-certificate $options -O- '$mirror/$url_filename' |" or die "Cannot launch wget.\n";
130 open MD5SUM, "| $hash_cmd > '$target/$filename.hash'" or die "Cannot launch $hash_cmd.\n";
132 open OUTPUT, "> $target/$filename.dl" or die "Cannot create file $target/$filename.dl: $!\n";
134 while (read WGET, $buffer, 1048576) {
135 $hash_cmd and print MD5SUM $buffer;
136 print OUTPUT $buffer;
138 $hash_cmd and close MD5SUM;
143 print STDERR "Download failed.\n";
150 my $sum = `cat "$target/$filename.hash"`;
151 $sum =~ /^(\w+)\s*/ or die "Could not generate file hash\n";
154 if ($sum ne $file_hash) {
155 print STDERR "MD5 sum of the downloaded file does not match (file: $sum, requested: $file_hash) - deleting download.\n";
161 unlink "$target/$filename";
162 system("mv", "$target/$filename.dl", "$target/$filename");
168 unlink "$target/$filename.dl";
169 unlink "$target/$filename.hash";
172 @mirrors = localmirrors();
174 foreach my $mirror (@ARGV) {
175 if ($mirror =~ /^\@SF\/(.+)$/) {
176 # give sourceforge a few more tries, because it redirects to different mirrors
178 push @mirrors, "http://downloads.sourceforge.net/$1";
180 } elsif ($mirror =~ /^\@APACHE\/(.+)$/) {
181 push @mirrors, "https://mirror.netcologne.de/apache.org/$1";
182 push @mirrors, "https://mirror.aarnet.edu.au/pub/apache/$1";
183 push @mirrors, "http://mirror.cogentco.com/pub/apache/$1";
184 push @mirrors, "http://mirror.csclub.uwaterloo.ca/apache/$1";
185 push @mirrors, "http://mirror.navercorp.com/apache/$1";
186 push @mirrors, "http://ftp.jaist.ac.jp/pub/apache/$1";
187 push @mirrors, "ftp://apache.cs.utah.edu/apache.org/$1";
188 push @mirrors, "ftp://apache.mirrors.ovh.net/ftp.apache.org/dist/$1";
189 } elsif ($mirror =~ /^\@GITHUB\/(.+)$/) {
190 # give github a few more tries (different mirrors)
192 push @mirrors, "https://raw.githubusercontent.com/$1";
194 } elsif ($mirror =~ /^\@GNU\/(.+)$/) {
195 push @mirrors, "https://mirrors.rit.edu/gnu/$1";
196 push @mirrors, "https://mirror.netcologne.de/gnu/$1";
197 push @mirrors, "http://ftp.kddilabs.jp/GNU/gnu/$1";
198 push @mirrors, "http://www.nic.funet.fi/pub/gnu/gnu/$1";
199 push @mirrors, "http://mirror.internode.on.net/pub/gnu/$1";
200 push @mirrors, "http://mirror.navercorp.com/gnu/$1";
201 push @mirrors, "ftp://mirror.csclub.uwaterloo.ca/gnu/$1";
202 push @mirrors, "ftp://download.xs4all.nl/pub/gnu/";
203 } elsif ($mirror =~ /^\@SAVANNAH\/(.+)$/) {
204 push @mirrors, "https://mirror.netcologne.de/savannah/$1";
205 push @mirrors, "http://mirror.csclub.uwaterloo.ca/nongnu/$1";
206 push @mirrors, "http://ftp.acc.umu.se/mirror/gnu.org/savannah/$1";
207 push @mirrors, "http://nongnu.uib.no/$1";
208 push @mirrors, "http://ftp.igh.cnrs.fr/pub/nongnu/$1";
209 push @mirrors, "http://public.p-knowledge.co.jp/Savannah-nongnu-mirror/$1";
210 push @mirrors, "ftp://cdimage.debian.org/mirror/gnu.org/savannah/$1";
211 push @mirrors, "ftp://ftp.acc.umu.se/mirror/gnu.org/savannah/$1";
212 } elsif ($mirror =~ /^\@KERNEL\/(.+)$/) {
214 if ($filename =~ /linux-\d+\.\d+(?:\.\d+)?-rc/) {
215 push @extra, "$extra[0]/testing";
216 } elsif ($filename =~ /linux-(\d+\.\d+(?:\.\d+)?)/) {
217 push @extra, "$extra[0]/longterm/v$1";
219 foreach my $dir (@extra) {
220 push @mirrors, "https://cdn.kernel.org/pub/$dir";
221 push @mirrors, "https://mirror.rackspace.com/kernel.org/$dir";
222 push @mirrors, "http://download.xs4all.nl/ftp.kernel.org/pub/$dir";
223 push @mirrors, "http://mirrors.mit.edu/kernel/$dir";
224 push @mirrors, "http://ftp.nara.wide.ad.jp/pub/kernel.org/$dir";
225 push @mirrors, "http://www.ring.gr.jp/archives/linux/kernel.org/$dir";
226 push @mirrors, "ftp://ftp.riken.jp/Linux/kernel.org/$dir";
227 push @mirrors, "ftp://www.mirrorservice.org/sites/ftp.kernel.org/pub/$dir";
229 } elsif ($mirror =~ /^\@GNOME\/(.+)$/) {
230 push @mirrors, "http://mirror.csclub.uwaterloo.ca/gnome/sources/$1";
231 push @mirrors, "http://ftp.acc.umu.se/pub/GNOME/sources/$1";
232 push @mirrors, "http://ftp.kaist.ac.kr/gnome/sources/$1";
233 push @mirrors, "http://www.mirrorservice.org/sites/ftp.gnome.org/pub/GNOME/sources/$1";
234 push @mirrors, "http://mirror.internode.on.net/pub/gnome/sources/$1";
235 push @mirrors, "http://ftp.belnet.be/ftp.gnome.org/sources/$1";
236 push @mirrors, "ftp://ftp.cse.buffalo.edu/pub/Gnome/sources/$1";
237 push @mirrors, "ftp://ftp.nara.wide.ad.jp/pub/X11/GNOME/sources/$1";
240 push @mirrors, $mirror;
244 #push @mirrors, 'http://mirror1.openwrt.org';
245 push @mirrors, 'http://sources.lede-project.org';
246 push @mirrors, 'http://mirror2.openwrt.org/sources';
247 push @mirrors, 'http://downloads.openwrt.org/sources';
250 my $mirror = shift @mirrors;
251 $mirror or die "No more mirrors to try - giving up.\n";
254 -f "$target/$filename" and $ok = 1;
257 $SIG{INT} = \&cleanup;