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