scripts/ubinize-image.sh: add support for adding custom partitions
[lede.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, "https://mirror.netcologne.de/apache.org/$1";
182                 push @mirrors, "https://mirror.aarnet.edu.au/pub/apache/$1";
183                 push @mirrors, "http://mirror.cogentco.com/pub/apache/$1";
184                 push @mirrors, "http://mirror.csclub.uwaterloo.ca/apache/$1";
185                 push @mirrors, "http://mirror.navercorp.com/apache/$1";
186                 push @mirrors, "http://ftp.jaist.ac.jp/pub/apache/$1";
187                 push @mirrors, "ftp://apache.cs.utah.edu/apache.org/$1";
188                 push @mirrors, "ftp://apache.mirrors.ovh.net/ftp.apache.org/dist/$1";
189         } elsif ($mirror =~ /^\@GITHUB\/(.+)$/) {
190                 # give github a few more tries (different mirrors)
191                 for (1 .. 5) {
192                         push @mirrors, "https://raw.githubusercontent.com/$1";
193                 }
194         } elsif ($mirror =~ /^\@GNU\/(.+)$/) {
195                 push @mirrors, "https://mirrors.rit.edu/gnu/$1";
196                 push @mirrors, "https://mirror.netcologne.de/gnu/$1";
197                 push @mirrors, "http://ftp.kddilabs.jp/GNU/gnu/$1";
198                 push @mirrors, "http://www.nic.funet.fi/pub/gnu/gnu/$1";
199                 push @mirrors, "http://mirror.internode.on.net/pub/gnu/$1";
200                 push @mirrors, "http://mirror.navercorp.com/gnu/$1";
201                 push @mirrors, "ftp://mirror.csclub.uwaterloo.ca/gnu/$1";
202                 push @mirrors, "ftp://download.xs4all.nl/pub/gnu/";
203         } elsif ($mirror =~ /^\@SAVANNAH\/(.+)$/) {
204                 push @mirrors, "https://mirror.netcologne.de/savannah/$1";
205                 push @mirrors, "http://mirror.csclub.uwaterloo.ca/nongnu/$1";
206                 push @mirrors, "http://ftp.acc.umu.se/mirror/gnu.org/savannah/$1";
207                 push @mirrors, "http://nongnu.uib.no/$1";
208                 push @mirrors, "http://ftp.igh.cnrs.fr/pub/nongnu/$1";
209                 push @mirrors, "http://public.p-knowledge.co.jp/Savannah-nongnu-mirror/$1";
210                 push @mirrors, "ftp://cdimage.debian.org/mirror/gnu.org/savannah/$1";
211                 push @mirrors, "ftp://ftp.acc.umu.se/mirror/gnu.org/savannah/$1";
212         } elsif ($mirror =~ /^\@KERNEL\/(.+)$/) {
213                 my @extra = ( $1 );
214                 if ($filename =~ /linux-\d+\.\d+(?:\.\d+)?-rc/) {
215                         push @extra, "$extra[0]/testing";
216                 } elsif ($filename =~ /linux-(\d+\.\d+(?:\.\d+)?)/) {
217                         push @extra, "$extra[0]/longterm/v$1";
218                 }               
219                 foreach my $dir (@extra) {
220                         push @mirrors, "https://cdn.kernel.org/pub/$dir";
221                         push @mirrors, "https://mirror.rackspace.com/kernel.org/$dir";
222                         push @mirrors, "http://download.xs4all.nl/ftp.kernel.org/pub/$dir";
223                         push @mirrors, "http://mirrors.mit.edu/kernel/$dir";
224                         push @mirrors, "http://ftp.nara.wide.ad.jp/pub/kernel.org/$dir";
225                         push @mirrors, "http://www.ring.gr.jp/archives/linux/kernel.org/$dir";
226                         push @mirrors, "ftp://ftp.riken.jp/Linux/kernel.org/$dir";
227                         push @mirrors, "ftp://www.mirrorservice.org/sites/ftp.kernel.org/pub/$dir";
228                 }
229     } elsif ($mirror =~ /^\@GNOME\/(.+)$/) {
230                 push @mirrors, "http://mirror.csclub.uwaterloo.ca/gnome/sources/$1";
231                 push @mirrors, "http://ftp.acc.umu.se/pub/GNOME/sources/$1";
232                 push @mirrors, "http://ftp.kaist.ac.kr/gnome/sources/$1";
233                 push @mirrors, "http://www.mirrorservice.org/sites/ftp.gnome.org/pub/GNOME/sources/$1";
234                 push @mirrors, "http://mirror.internode.on.net/pub/gnome/sources/$1";
235                 push @mirrors, "http://ftp.belnet.be/ftp.gnome.org/sources/$1";
236                 push @mirrors, "ftp://ftp.cse.buffalo.edu/pub/Gnome/sources/$1";
237                 push @mirrors, "ftp://ftp.nara.wide.ad.jp/pub/X11/GNOME/sources/$1";
238     }
239     else {
240                 push @mirrors, $mirror;
241         }
242 }
243
244 #push @mirrors, 'http://mirror1.openwrt.org';
245 push @mirrors, 'http://sources.lede-project.org';
246 push @mirrors, 'http://mirror2.openwrt.org/sources';
247 push @mirrors, 'http://downloads.openwrt.org/sources';
248
249 while (!$ok) {
250         my $mirror = shift @mirrors;
251         $mirror or die "No more mirrors to try - giving up.\n";
252
253         download($mirror);
254         -f "$target/$filename" and $ok = 1;
255 }
256
257 $SIG{INT} = \&cleanup;
258