download.pl: use http://sources.lede-project.org as download mirror
[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
15 @ARGV > 2 or die "Syntax: $0 <target dir> <filename> <hash> <url filename> [<mirror> ...]\n";
16
17 my $url_filename;
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);
23 my @mirrors;
24 my $ok;
25
26 $url_filename or $url_filename = $filename;
27
28 sub localmirrors {
29 my @mlist;
30 open LM, "$scriptdir/localmirrors" and do {
31 while (<LM>) {
32 chomp $_;
33 push @mlist, $_ if $_;
34 }
35 close LM;
36 };
37 open CONFIG, "<".$ENV{'TOPDIR'}."/.config" and do {
38 while (<CONFIG>) {
39 /^CONFIG_LOCALMIRROR="(.+)"/ and do {
40 chomp;
41 my @local_mirrors = split(/;/, $1);
42 push @mlist, @local_mirrors;
43 };
44 }
45 close CONFIG;
46 };
47
48 my $mirror = $ENV{'DOWNLOAD_MIRROR'};
49 $mirror and push @mlist, split(/;/, $mirror);
50
51 return @mlist;
52 }
53
54 sub which($) {
55 my $prog = shift;
56 my $res = `which $prog`;
57 $res or return undef;
58 $res =~ /^no / and return undef;
59 $res =~ /not found/ and return undef;
60 return $res;
61 }
62
63 sub hash_cmd() {
64 my $len = length($file_hash);
65 my $cmd;
66
67 $len == 64 and return "openssl dgst -sha256 | sed -e 's,.*= ,,'";
68 $len == 32 and do {
69 my $cmd = which("md5sum") || which("md5") || die 'no md5 checksum program found, please install md5 or md5sum';
70 chomp $cmd;
71 return $cmd;
72 };
73 return undef;
74 }
75
76 my $hash_cmd = hash_cmd();
77
78 sub download
79 {
80 my $mirror = shift;
81 my $options = $ENV{WGET_OPTIONS} || "";
82
83 $mirror =~ s!/$!!;
84
85 if ($mirror =~ s!^file://!!) {
86 if (! -d "$mirror") {
87 print STDERR "Wrong local cache directory -$mirror-.\n";
88 cleanup();
89 return;
90 }
91
92 if (! -d "$target") {
93 system("mkdir", "-p", "$target/");
94 }
95
96 if (! open TMPDLS, "find $mirror -follow -name $filename 2>/dev/null |") {
97 print("Failed to search for $filename in $mirror\n");
98 return;
99 }
100
101 my $link;
102
103 while (defined(my $line = readline TMPDLS)) {
104 chomp ($link = $line);
105 if ($. > 1) {
106 print("$. or more instances of $filename in $mirror found . Only one instance allowed.\n");
107 return;
108 }
109 }
110
111 close TMPDLS;
112
113 if (! $link) {
114 print("No instances of $filename found in $mirror.\n");
115 return;
116 }
117
118 print("Copying $filename from $link\n");
119 copy($link, "$target/$filename.dl");
120
121 $hash_cmd and do {
122 if (system("cat '$target/$filename.dl' | $hash_cmd > '$target/$filename.hash'")) {
123 print("Failed to generate hash for $filename\n");
124 return;
125 }
126 };
127 } else {
128 open WGET, "wget -t5 --timeout=20 --no-check-certificate $options -O- '$mirror/$url_filename' |" or die "Cannot launch wget.\n";
129 $hash_cmd and do {
130 open MD5SUM, "| $hash_cmd > '$target/$filename.hash'" or die "Cannot launch $hash_cmd.\n";
131 };
132 open OUTPUT, "> $target/$filename.dl" or die "Cannot create file $target/$filename.dl: $!\n";
133 my $buffer;
134 while (read WGET, $buffer, 1048576) {
135 $hash_cmd and print MD5SUM $buffer;
136 print OUTPUT $buffer;
137 }
138 $hash_cmd and close MD5SUM;
139 close WGET;
140 close OUTPUT;
141
142 if ($? >> 8) {
143 print STDERR "Download failed.\n";
144 cleanup();
145 return;
146 }
147 }
148
149 $hash_cmd and do {
150 my $sum = `cat "$target/$filename.hash"`;
151 $sum =~ /^(\w+)\s*/ or die "Could not generate file hash\n";
152 $sum = $1;
153
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";
156 cleanup();
157 return;
158 }
159 };
160
161 unlink "$target/$filename";
162 system("mv", "$target/$filename.dl", "$target/$filename");
163 cleanup();
164 }
165
166 sub cleanup
167 {
168 unlink "$target/$filename.dl";
169 unlink "$target/$filename.hash";
170 }
171
172 @mirrors = localmirrors();
173
174 foreach my $mirror (@ARGV) {
175 if ($mirror =~ /^\@SF\/(.+)$/) {
176 # give sourceforge a few more tries, because it redirects to different mirrors
177 for (1 .. 5) {
178 push @mirrors, "http://downloads.sourceforge.net/$1";
179 }
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)
188 for (1 .. 5) {
189 push @mirrors, "https://raw.githubusercontent.com/$1";
190 }
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\/(.+)$/) {
203 my @extra = ( $1 );
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";
208 }
209 foreach my $dir (@extra) {
210 push @mirrors, "https://kernel.org/pub/$dir";
211 push @mirrors, "ftp://kernel.org/pub/$dir";
212 }
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";
221 }
222 else {
223 push @mirrors, $mirror;
224 }
225 }
226
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';
231
232 while (!$ok) {
233 my $mirror = shift @mirrors;
234 $mirror or die "No more mirrors to try - giving up.\n";
235
236 download($mirror);
237 -f "$target/$filename" and $ok = 1;
238 }
239
240 $SIG{INT} = \&cleanup;
241