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, "http://ftp.tudelft.nl/apache/$1";
182 push @mirrors, "http://apache.openmirror.de/$1";
183 push @mirrors, "http://mirrors.ocf.berkeley.edu/apache/$1";
184 push @mirrors, "http://mirror.cc.columbia.edu/pub/software/apache/$1";
185 push @mirrors, "http://ftp.jaist.ac.jp/pub/apache/$1";
186 } elsif ($mirror =~ /^\@GITHUB\/(.+)$/) {
187 # give github a few more tries (different mirrors)
189 push @mirrors, "https://raw.githubusercontent.com/$1";
191 } elsif ($mirror =~ /^\@GNU\/(.+)$/) {
192 push @mirrors, "http://ftpmirror.gnu.org/$1";
193 push @mirrors, "http://ftp.gnu.org/pub/gnu/$1";
194 push @mirrors, "ftp://ftp.belnet.be/mirror/ftp.gnu.org/gnu/$1";
195 push @mirrors, "ftp://ftp.mirror.nl/pub/mirror/gnu/$1";
196 push @mirrors, "http://mirror.switch.ch/ftp/mirror/gnu/$1";
197 } elsif ($mirror =~ /^\@SAVANNAH\/(.+)$/) {
198 push @mirrors, "http://download.savannah.gnu.org/releases/$1";
199 push @mirrors, "http://nongnu.uib.no/$1";
200 push @mirrors, "http://ftp.igh.cnrs.fr/pub/nongnu/$1";
201 push @mirrors, "http://download-mirror.savannah.gnu.org/releases/$1";
202 } elsif ($mirror =~ /^\@KERNEL\/(.+)$/) {
204 if ($filename =~ /linux-\d+\.\d+(?:\.\d+)?-rc/) {
205 push @extra, "$extra[0]/testing";
206 } elsif ($filename =~ /linux-(\d+\.\d+(?:\.\d+)?)/) {
207 push @extra, "$extra[0]/longterm/v$1";
209 foreach my $dir (@extra) {
210 push @mirrors, "https://kernel.org/pub/$dir";
211 push @mirrors, "ftp://kernel.org/pub/$dir";
213 } elsif ($mirror =~ /^\@GNOME\/(.+)$/) {
214 push @mirrors, "http://ftp.gnome.org/pub/GNOME/sources/$1";
215 push @mirrors, "http://www.mirrorservice.org/sites/ftp.gnome.org/pub/GNOME/sources/$1";
216 push @mirrors, "http://fr2.rpmfind.net/linux/gnome.org/sources/$1";
217 push @mirrors, "http://ftp.acc.umu.se/pub/GNOME/sources/$1";
218 push @mirrors, "http://ftp.belnet.be/ftp.gnome.org/sources/$1";
219 push @mirrors, "ftp://ftp.cse.buffalo.edu/pub/Gnome/sources/$1";
220 push @mirrors, "ftp://ftp.nara.wide.ad.jp/pub/X11/GNOME/sources/$1";
223 push @mirrors, $mirror;
227 #push @mirrors, 'http://mirror1.openwrt.org';
228 push @mirrors, 'http://sources.lede-project.org';
229 push @mirrors, 'http://mirror2.openwrt.org/sources';
230 push @mirrors, 'http://downloads.openwrt.org/sources';
233 my $mirror = shift @mirrors;
234 $mirror or die "No more mirrors to try - giving up.\n";
237 -f "$target/$filename" and $ok = 1;
240 $SIG{INT} = \&cleanup;