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 = 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), shellwords($ENV{CURL_OPTIONS} || ''), $url)
86 : (qw(wget --tries=5 --timeout=20 --output-document=-), shellwords($ENV{WGET_OPTIONS} || ''), $url)
90 my $hash_cmd = hash_cmd();
98 if ($mirror =~ s!^file://!!) {
100 print STDERR "Wrong local cache directory -$mirror-.\n";
105 if (! -d "$target") {
106 system("mkdir", "-p", "$target/");
109 if (! open TMPDLS, "find $mirror -follow -name $filename 2>/dev/null |") {
110 print("Failed to search for $filename in $mirror\n");
116 while (defined(my $line = readline TMPDLS)) {
117 chomp ($link = $line);
119 print("$. or more instances of $filename in $mirror found . Only one instance allowed.\n");
127 print("No instances of $filename found in $mirror.\n");
131 print("Copying $filename from $link\n");
132 copy($link, "$target/$filename.dl");
135 if (system("cat '$target/$filename.dl' | $hash_cmd > '$target/$filename.hash'")) {
136 print("Failed to generate hash for $filename\n");
141 my @cmd = download_cmd("$mirror/$url_filename");
142 open(FETCH_FD, '-|', @cmd) or die "Cannot launch curl or wget.\n";
144 open MD5SUM, "| $hash_cmd > '$target/$filename.hash'" or die "Cannot launch $hash_cmd.\n";
146 open OUTPUT, "> $target/$filename.dl" or die "Cannot create file $target/$filename.dl: $!\n";
148 while (read FETCH_FD, $buffer, 1048576) {
149 $hash_cmd and print MD5SUM $buffer;
150 print OUTPUT $buffer;
152 $hash_cmd and close MD5SUM;
157 print STDERR "Download failed.\n";
164 my $sum = `cat "$target/$filename.hash"`;
165 $sum =~ /^(\w+)\s*/ or die "Could not generate file hash\n";
168 if ($sum ne $file_hash) {
169 print STDERR "MD5 sum of the downloaded file does not match (file: $sum, requested: $file_hash) - deleting download.\n";
175 unlink "$target/$filename";
176 system("mv", "$target/$filename.dl", "$target/$filename");
182 unlink "$target/$filename.dl";
183 unlink "$target/$filename.hash";
186 @mirrors = localmirrors();
188 foreach my $mirror (@ARGV) {
189 if ($mirror =~ /^\@SF\/(.+)$/) {
190 # give sourceforge a few more tries, because it redirects to different mirrors
192 # push @mirrors, "http://downloads.sourceforge.net/$1";
194 } elsif ($mirror =~ /^\@APACHE\/(.+)$/) {
195 push @mirrors, "https://mirror.netcologne.de/apache.org/$1";
196 push @mirrors, "https://mirror.aarnet.edu.au/pub/apache/$1";
197 } elsif ($mirror =~ /^\@GITHUB\/(.+)$/) {
198 # give github a few more tries (different mirrors)
200 push @mirrors, "https://raw.githubusercontent.com/$1";
202 } elsif ($mirror =~ /^\@GNU\/(.+)$/) {
203 push @mirrors, "https://mirrors.rit.edu/gnu/$1";
204 push @mirrors, "https://mirror.netcologne.de/gnu/$1";
205 } elsif ($mirror =~ /^\@SAVANNAH\/(.+)$/) {
206 push @mirrors, "https://mirror.netcologne.de/savannah/$1";
207 } elsif ($mirror =~ /^\@KERNEL\/(.+)$/) {
209 if ($filename =~ /linux-\d+\.\d+(?:\.\d+)?-rc/) {
210 push @extra, "$extra[0]/testing";
211 } elsif ($filename =~ /linux-(\d+\.\d+(?:\.\d+)?)/) {
212 push @extra, "$extra[0]/longterm/v$1";
214 foreach my $dir (@extra) {
215 push @mirrors, "https://cdn.kernel.org/pub/$dir";
216 push @mirrors, "https://mirror.rackspace.com/kernel.org/$dir";
218 } elsif ($mirror =~ /^\@KERNEL_LIBRE\/(.+)$/) {
220 if ($filename =~ /linux-libre-\d+\.\d+(?:\.\d+)?-rc-gnu/) {
221 push @extra, "$extra[0]/testing";
222 } elsif ($filename =~ /linux-libre-(\d+\.\d+(?:\.\d+)?)-gnu/) {
223 push @extra, "$extra[0]/v$1";
225 foreach my $dir (@extra) {
226 push @mirrors, "https://linux-libre.fsfla.org/pub/linux-libre/releases/$dir";
228 } elsif ($mirror =~ /^\@GNOME\/(.+)$/) {
229 push @mirrors, "https://mirror.csclub.uwaterloo.ca/gnome/sources/$1";
232 push @mirrors, $mirror;
236 push @mirrors, 'https://librecmc.org/librecmc/downloads/sources/v1.4';
238 while (!-f "$target/$filename") {
239 my $mirror = shift @mirrors;
240 $mirror or die "No more mirrors to try - giving up.\n";
245 $SIG{INT} = \&cleanup;