download.pl: use curl in preference to wget
[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 = 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 $url_filename or $url_filename = $filename;
28
29 sub localmirrors {
30 my @mlist;
31 open LM, "$scriptdir/localmirrors" and do {
32 while (<LM>) {
33 chomp $_;
34 push @mlist, $_ if $_;
35 }
36 close LM;
37 };
38 open CONFIG, "<".$ENV{'TOPDIR'}."/.config" and do {
39 while (<CONFIG>) {
40 /^CONFIG_LOCALMIRROR="(.+)"/ and do {
41 chomp;
42 my @local_mirrors = split(/;/, $1);
43 push @mlist, @local_mirrors;
44 };
45 }
46 close CONFIG;
47 };
48
49 my $mirror = $ENV{'DOWNLOAD_MIRROR'};
50 $mirror and push @mlist, split(/;/, $mirror);
51
52 return @mlist;
53 }
54
55 sub which($) {
56 my $prog = shift;
57 my $res = `which $prog`;
58 $res or return undef;
59 $res =~ /^no / and return undef;
60 $res =~ /not found/ and return undef;
61 return $res;
62 }
63
64 sub hash_cmd() {
65 my $len = length($file_hash);
66 my $cmd;
67
68 $len == 64 and return "openssl dgst -sha256 | sed -e 's,.*= ,,'";
69 $len == 32 and do {
70 my $cmd = which("md5sum") || which("md5") || die 'no md5 checksum program found, please install md5 or md5sum';
71 chomp $cmd;
72 return $cmd;
73 };
74 return undef;
75 }
76
77 sub download_cmd($) {
78 my $url = shift;
79 my $have_curl = 0;
80
81 if (open CURL, '-|', 'curl', '--version') {
82 if (defined(my $line = readline CURL)) {
83 $have_curl = 1 if $line =~ /^curl /;
84 }
85 close CURL;
86 }
87
88 return $have_curl
89 ? (qw(curl --connect-timeout 20 --retry 5 --location --insecure), shellwords($ENV{CURL_OPTIONS} || ''), $url)
90 : (qw(wget --tries=5 --timeout=20 --no-check-certificate --output-document=-), shellwords($ENV{WGET_OPTIONS} || ''), $url)
91 ;
92 }
93
94 my $hash_cmd = hash_cmd();
95
96 sub download
97 {
98 my $mirror = 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/$url_filename");
146 open(FETCH_FD, '-|', @cmd) or die "Cannot launch curl or wget.\n";
147 $hash_cmd and do {
148 open MD5SUM, "| $hash_cmd > '$target/$filename.hash'" or die "Cannot launch $hash_cmd.\n";
149 };
150 open OUTPUT, "> $target/$filename.dl" or die "Cannot create file $target/$filename.dl: $!\n";
151 my $buffer;
152 while (read FETCH_FD, $buffer, 1048576) {
153 $hash_cmd and print MD5SUM $buffer;
154 print OUTPUT $buffer;
155 }
156 $hash_cmd and close MD5SUM;
157 close FETCH_FD;
158 close OUTPUT;
159
160 if ($? >> 8) {
161 print STDERR "Download failed.\n";
162 cleanup();
163 return;
164 }
165 }
166
167 $hash_cmd and do {
168 my $sum = `cat "$target/$filename.hash"`;
169 $sum =~ /^(\w+)\s*/ or die "Could not generate file hash\n";
170 $sum = $1;
171
172 if ($sum ne $file_hash) {
173 print STDERR "MD5 sum of the downloaded file does not match (file: $sum, requested: $file_hash) - deleting download.\n";
174 cleanup();
175 return;
176 }
177 };
178
179 unlink "$target/$filename";
180 system("mv", "$target/$filename.dl", "$target/$filename");
181 cleanup();
182 }
183
184 sub cleanup
185 {
186 unlink "$target/$filename.dl";
187 unlink "$target/$filename.hash";
188 }
189
190 @mirrors = localmirrors();
191
192 foreach my $mirror (@ARGV) {
193 if ($mirror =~ /^\@SF\/(.+)$/) {
194 # give sourceforge a few more tries, because it redirects to different mirrors
195 for (1 .. 5) {
196 push @mirrors, "http://downloads.sourceforge.net/$1";
197 }
198 } elsif ($mirror =~ /^\@APACHE\/(.+)$/) {
199 push @mirrors, "https://mirror.netcologne.de/apache.org/$1";
200 push @mirrors, "https://mirror.aarnet.edu.au/pub/apache/$1";
201 push @mirrors, "http://mirror.cogentco.com/pub/apache/$1";
202 push @mirrors, "http://mirror.csclub.uwaterloo.ca/apache/$1";
203 push @mirrors, "http://mirror.navercorp.com/apache/$1";
204 push @mirrors, "http://ftp.jaist.ac.jp/pub/apache/$1";
205 push @mirrors, "ftp://apache.cs.utah.edu/apache.org/$1";
206 push @mirrors, "ftp://apache.mirrors.ovh.net/ftp.apache.org/dist/$1";
207 } elsif ($mirror =~ /^\@GITHUB\/(.+)$/) {
208 # give github a few more tries (different mirrors)
209 for (1 .. 5) {
210 push @mirrors, "https://raw.githubusercontent.com/$1";
211 }
212 } elsif ($mirror =~ /^\@GNU\/(.+)$/) {
213 push @mirrors, "https://mirrors.rit.edu/gnu/$1";
214 push @mirrors, "https://mirror.netcologne.de/gnu/$1";
215 push @mirrors, "http://ftp.kddilabs.jp/GNU/gnu/$1";
216 push @mirrors, "http://www.nic.funet.fi/pub/gnu/gnu/$1";
217 push @mirrors, "http://mirror.internode.on.net/pub/gnu/$1";
218 push @mirrors, "http://mirror.navercorp.com/gnu/$1";
219 push @mirrors, "ftp://mirror.csclub.uwaterloo.ca/gnu/$1";
220 push @mirrors, "ftp://download.xs4all.nl/pub/gnu/";
221 } elsif ($mirror =~ /^\@SAVANNAH\/(.+)$/) {
222 push @mirrors, "https://mirror.netcologne.de/savannah/$1";
223 push @mirrors, "http://mirror.csclub.uwaterloo.ca/nongnu/$1";
224 push @mirrors, "http://ftp.acc.umu.se/mirror/gnu.org/savannah/$1";
225 push @mirrors, "http://nongnu.uib.no/$1";
226 push @mirrors, "http://ftp.igh.cnrs.fr/pub/nongnu/$1";
227 push @mirrors, "http://public.p-knowledge.co.jp/Savannah-nongnu-mirror/$1";
228 push @mirrors, "ftp://cdimage.debian.org/mirror/gnu.org/savannah/$1";
229 push @mirrors, "ftp://ftp.acc.umu.se/mirror/gnu.org/savannah/$1";
230 } elsif ($mirror =~ /^\@KERNEL\/(.+)$/) {
231 my @extra = ( $1 );
232 if ($filename =~ /linux-\d+\.\d+(?:\.\d+)?-rc/) {
233 push @extra, "$extra[0]/testing";
234 } elsif ($filename =~ /linux-(\d+\.\d+(?:\.\d+)?)/) {
235 push @extra, "$extra[0]/longterm/v$1";
236 }
237 foreach my $dir (@extra) {
238 push @mirrors, "https://cdn.kernel.org/pub/$dir";
239 push @mirrors, "https://mirror.rackspace.com/kernel.org/$dir";
240 push @mirrors, "http://download.xs4all.nl/ftp.kernel.org/pub/$dir";
241 push @mirrors, "http://mirrors.mit.edu/kernel/$dir";
242 push @mirrors, "http://ftp.nara.wide.ad.jp/pub/kernel.org/$dir";
243 push @mirrors, "http://www.ring.gr.jp/archives/linux/kernel.org/$dir";
244 push @mirrors, "ftp://ftp.riken.jp/Linux/kernel.org/$dir";
245 push @mirrors, "ftp://www.mirrorservice.org/sites/ftp.kernel.org/pub/$dir";
246 }
247 } elsif ($mirror =~ /^\@GNOME\/(.+)$/) {
248 push @mirrors, "http://mirror.csclub.uwaterloo.ca/gnome/sources/$1";
249 push @mirrors, "http://ftp.acc.umu.se/pub/GNOME/sources/$1";
250 push @mirrors, "http://ftp.kaist.ac.kr/gnome/sources/$1";
251 push @mirrors, "http://www.mirrorservice.org/sites/ftp.gnome.org/pub/GNOME/sources/$1";
252 push @mirrors, "http://mirror.internode.on.net/pub/gnome/sources/$1";
253 push @mirrors, "http://ftp.belnet.be/ftp.gnome.org/sources/$1";
254 push @mirrors, "ftp://ftp.cse.buffalo.edu/pub/Gnome/sources/$1";
255 push @mirrors, "ftp://ftp.nara.wide.ad.jp/pub/X11/GNOME/sources/$1";
256 }
257 else {
258 push @mirrors, $mirror;
259 }
260 }
261
262 #push @mirrors, 'http://mirror1.openwrt.org';
263 push @mirrors, 'http://sources.lede-project.org';
264 push @mirrors, 'http://mirror2.openwrt.org/sources';
265 push @mirrors, 'http://downloads.openwrt.org/sources';
266
267 while (!-f "$target/$filename") {
268 my $mirror = shift @mirrors;
269 $mirror or die "No more mirrors to try - giving up.\n";
270
271 download($mirror);
272 }
273
274 $SIG{INT} = \&cleanup;
275