99708c456fd6a222924ecb362924582b260c7110
[openwrt/openwrt.git] / scripts / download.pl
1 #!/usr/bin/env perl
2 #
3 # Copyright (C) 2006 OpenWrt.org
4 # Copyright (C) 2016 LEDE project
5 #
6 # This is free software, licensed under the GNU General Public License v2.
7 # See /LICENSE for more information.
8 #
9
10 use strict;
11 use warnings;
12 use File::Basename;
13 use File::Copy;
14 use Text::ParseWords;
15
16 @ARGV > 2 or die "Syntax: $0 <target dir> <filename> <hash> <url filename> [<mirror> ...]\n";
17
18 my $url_filename;
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);
24 my @mirrors;
25 my $ok;
26
27 my $check_certificate = $ENV{DOWNLOAD_CHECK_CERTIFICATE} eq "y";
28
29 $url_filename or $url_filename = $filename;
30
31 sub localmirrors {
32 my @mlist;
33 open LM, "$scriptdir/localmirrors" and do {
34 while (<LM>) {
35 chomp $_;
36 push @mlist, $_ if $_;
37 }
38 close LM;
39 };
40 open CONFIG, "<".$ENV{'TOPDIR'}."/.config" and do {
41 while (<CONFIG>) {
42 /^CONFIG_LOCALMIRROR="(.+)"/ and do {
43 chomp;
44 my @local_mirrors = split(/;/, $1);
45 push @mlist, @local_mirrors;
46 };
47 }
48 close CONFIG;
49 };
50
51 my $mirror = $ENV{'DOWNLOAD_MIRROR'};
52 $mirror and push @mlist, split(/;/, $mirror);
53
54 return @mlist;
55 }
56
57 sub which($) {
58 my $prog = shift;
59 my $res = `which $prog`;
60 $res or return undef;
61 $res =~ /^no / and return undef;
62 $res =~ /not found/ and return undef;
63 return $res;
64 }
65
66 sub hash_cmd() {
67 my $len = length($file_hash);
68 my $cmd;
69
70 $len == 64 and return "mkhash sha256";
71 $len == 32 and return "mkhash md5";
72 return undef;
73 }
74
75 sub download_cmd($) {
76 my $url = shift;
77 my $have_curl = 0;
78
79 if (open CURL, '-|', 'curl', '--version') {
80 if (defined(my $line = readline CURL)) {
81 $have_curl = 1 if $line =~ /^curl /;
82 }
83 close CURL;
84 }
85
86 return $have_curl
87 ? (qw(curl -f --connect-timeout 20 --retry 5 --location), $check_certificate ? '' : '--insecure', shellwords($ENV{CURL_OPTIONS} || ''), $url)
88 : (qw(wget --tries=5 --timeout=20 --output-document=-), $check_certificate ? '' : '--no-check-certificate', shellwords($ENV{WGET_OPTIONS} || ''), $url)
89 ;
90 }
91
92 my $hash_cmd = hash_cmd();
93 $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";
94
95 sub download
96 {
97 my $mirror = shift;
98 my $download_filename = shift;
99
100 $mirror =~ s!/$!!;
101
102 if ($mirror =~ s!^file://!!) {
103 if (! -d "$mirror") {
104 print STDERR "Wrong local cache directory -$mirror-.\n";
105 cleanup();
106 return;
107 }
108
109 if (! -d "$target") {
110 system("mkdir", "-p", "$target/");
111 }
112
113 if (! open TMPDLS, "find $mirror -follow -name $filename 2>/dev/null |") {
114 print("Failed to search for $filename in $mirror\n");
115 return;
116 }
117
118 my $link;
119
120 while (defined(my $line = readline TMPDLS)) {
121 chomp ($link = $line);
122 if ($. > 1) {
123 print("$. or more instances of $filename in $mirror found . Only one instance allowed.\n");
124 return;
125 }
126 }
127
128 close TMPDLS;
129
130 if (! $link) {
131 print("No instances of $filename found in $mirror.\n");
132 return;
133 }
134
135 print("Copying $filename from $link\n");
136 copy($link, "$target/$filename.dl");
137
138 $hash_cmd and do {
139 if (system("cat '$target/$filename.dl' | $hash_cmd > '$target/$filename.hash'")) {
140 print("Failed to generate hash for $filename\n");
141 return;
142 }
143 };
144 } else {
145 my @cmd = download_cmd("$mirror/$download_filename");
146 print STDERR "+ ".join(" ",@cmd)."\n";
147 open(FETCH_FD, '-|', @cmd) or die "Cannot launch curl or wget.\n";
148 $hash_cmd and do {
149 open MD5SUM, "| $hash_cmd > '$target/$filename.hash'" or die "Cannot launch $hash_cmd.\n";
150 };
151 open OUTPUT, "> $target/$filename.dl" or die "Cannot create file $target/$filename.dl: $!\n";
152 my $buffer;
153 while (read FETCH_FD, $buffer, 1048576) {
154 $hash_cmd and print MD5SUM $buffer;
155 print OUTPUT $buffer;
156 }
157 $hash_cmd and close MD5SUM;
158 close FETCH_FD;
159 close OUTPUT;
160
161 if ($? >> 8) {
162 print STDERR "Download failed.\n";
163 cleanup();
164 return;
165 }
166 }
167
168 $hash_cmd and do {
169 my $sum = `cat "$target/$filename.hash"`;
170 $sum =~ /^(\w+)\s*/ or die "Could not generate file hash\n";
171 $sum = $1;
172
173 if ($sum ne $file_hash) {
174 print STDERR "Hash of the downloaded file does not match (file: $sum, requested: $file_hash) - deleting download.\n";
175 cleanup();
176 return;
177 }
178 };
179
180 unlink "$target/$filename";
181 system("mv", "$target/$filename.dl", "$target/$filename");
182 cleanup();
183 }
184
185 sub cleanup
186 {
187 unlink "$target/$filename.dl";
188 unlink "$target/$filename.hash";
189 }
190
191 @mirrors = localmirrors();
192
193 foreach my $mirror (@ARGV) {
194 if ($mirror =~ /^\@SF\/(.+)$/) {
195 # give sourceforge a few more tries, because it redirects to different mirrors
196 for (1 .. 5) {
197 push @mirrors, "https://downloads.sourceforge.net/$1";
198 }
199 } elsif ($mirror =~ /^\@OPENWRT$/) {
200 # use OpenWrt source server directly
201 } elsif ($mirror =~ /^\@APACHE\/(.+)$/) {
202 push @mirrors, "https://mirror.netcologne.de/apache.org/$1";
203 push @mirrors, "https://mirror.aarnet.edu.au/pub/apache/$1";
204 push @mirrors, "https://mirror.csclub.uwaterloo.ca/apache/$1";
205 push @mirrors, "https://archive.apache.org/dist/$1";
206 push @mirrors, "http://mirror.cogentco.com/pub/apache/$1";
207 push @mirrors, "http://mirror.navercorp.com/apache/$1";
208 push @mirrors, "http://ftp.jaist.ac.jp/pub/apache/$1";
209 push @mirrors, "ftp://apache.cs.utah.edu/apache.org/$1";
210 push @mirrors, "ftp://apache.mirrors.ovh.net/ftp.apache.org/dist/$1";
211 } elsif ($mirror =~ /^\@GITHUB\/(.+)$/) {
212 # give github a few more tries (different mirrors)
213 for (1 .. 5) {
214 push @mirrors, "https://raw.githubusercontent.com/$1";
215 }
216 } elsif ($mirror =~ /^\@GNU\/(.+)$/) {
217 push @mirrors, "https://mirror.csclub.uwaterloo.ca/gnu/$1";
218 push @mirrors, "https://mirror.netcologne.de/gnu/$1";
219 push @mirrors, "http://ftp.kddilabs.jp/GNU/gnu/$1";
220 push @mirrors, "http://www.nic.funet.fi/pub/gnu/gnu/$1";
221 push @mirrors, "http://mirror.internode.on.net/pub/gnu/$1";
222 push @mirrors, "http://mirror.navercorp.com/gnu/$1";
223 push @mirrors, "ftp://mirrors.rit.edu/gnu/$1";
224 push @mirrors, "ftp://download.xs4all.nl/pub/gnu/";
225 } elsif ($mirror =~ /^\@SAVANNAH\/(.+)$/) {
226 push @mirrors, "https://mirror.netcologne.de/savannah/$1";
227 push @mirrors, "https://mirror.csclub.uwaterloo.ca/nongnu/$1";
228 push @mirrors, "http://ftp.acc.umu.se/mirror/gnu.org/savannah/$1";
229 push @mirrors, "http://nongnu.uib.no/$1";
230 push @mirrors, "http://ftp.igh.cnrs.fr/pub/nongnu/$1";
231 push @mirrors, "ftp://cdimage.debian.org/mirror/gnu.org/savannah/$1";
232 push @mirrors, "ftp://ftp.acc.umu.se/mirror/gnu.org/savannah/$1";
233 } elsif ($mirror =~ /^\@KERNEL\/(.+)$/) {
234 my @extra = ( $1 );
235 if ($filename =~ /linux-\d+\.\d+(?:\.\d+)?-rc/) {
236 push @extra, "$extra[0]/testing";
237 } elsif ($filename =~ /linux-(\d+\.\d+(?:\.\d+)?)/) {
238 push @extra, "$extra[0]/longterm/v$1";
239 }
240 foreach my $dir (@extra) {
241 push @mirrors, "https://cdn.kernel.org/pub/$dir";
242 push @mirrors, "https://download.xs4all.nl/ftp.kernel.org/pub/$dir";
243 push @mirrors, "https://mirrors.mit.edu/kernel/$dir";
244 push @mirrors, "http://ftp.nara.wide.ad.jp/pub/kernel.org/$dir";
245 push @mirrors, "http://www.ring.gr.jp/archives/linux/kernel.org/$dir";
246 push @mirrors, "ftp://ftp.riken.jp/Linux/kernel.org/$dir";
247 push @mirrors, "ftp://www.mirrorservice.org/sites/ftp.kernel.org/pub/$dir";
248 }
249 } elsif ($mirror =~ /^\@GNOME\/(.+)$/) {
250 push @mirrors, "https://mirror.csclub.uwaterloo.ca/gnome/sources/$1";
251 push @mirrors, "http://ftp.acc.umu.se/pub/GNOME/sources/$1";
252 push @mirrors, "http://ftp.kaist.ac.kr/gnome/sources/$1";
253 push @mirrors, "http://www.mirrorservice.org/sites/ftp.gnome.org/pub/GNOME/sources/$1";
254 push @mirrors, "http://mirror.internode.on.net/pub/gnome/sources/$1";
255 push @mirrors, "http://ftp.belnet.be/ftp.gnome.org/sources/$1";
256 push @mirrors, "ftp://ftp.cse.buffalo.edu/pub/Gnome/sources/$1";
257 push @mirrors, "ftp://ftp.nara.wide.ad.jp/pub/X11/GNOME/sources/$1";
258 } else {
259 push @mirrors, $mirror;
260 }
261 }
262
263 push @mirrors, 'https://sources.cdn.openwrt.org';
264 push @mirrors, 'https://sources.openwrt.org';
265 push @mirrors, 'https://mirror2.openwrt.org/sources';
266
267 if (-f "$target/$filename") {
268 $hash_cmd and do {
269 if (system("cat '$target/$filename' | $hash_cmd > '$target/$filename.hash'")) {
270 die "Failed to generate hash for $filename\n";
271 }
272
273 my $sum = `cat "$target/$filename.hash"`;
274 $sum =~ /^(\w+)\s*/ or die "Could not generate file hash\n";
275 $sum = $1;
276
277 cleanup();
278 exit 0 if $sum eq $file_hash;
279
280 die "Hash of the local file $filename does not match (file: $sum, requested: $file_hash) - deleting download.\n";
281 unlink "$target/$filename";
282 };
283 }
284
285 while (!-f "$target/$filename") {
286 my $mirror = shift @mirrors;
287 $mirror or die "No more mirrors to try - giving up.\n";
288
289 download($mirror, $url_filename);
290 if (!-f "$target/$filename" && $url_filename ne $filename) {
291 download($mirror, $filename);
292 }
293 }
294
295 $SIG{INT} = \&cleanup;
296