v1.5 branch refresh based upon upstream master @ c8677ca89e53e3be7988d54280fce166cc894a7e
[librecmc/librecmc.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 "mkhash sha256";
69         $len == 32 and return "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') {
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), shellwords($ENV{CURL_OPTIONS} || ''), $url)
86                 : (qw(wget --tries=5 --timeout=20 --output-document=-), shellwords($ENV{WGET_OPTIONS} || ''), $url)
87         ;
88 }
89
90 my $hash_cmd = hash_cmd();
91
92 sub download
93 {
94         my $mirror = shift;
95
96         $mirror =~ s!/$!!;
97
98         if ($mirror =~ s!^file://!!) {
99                 if (! -d "$mirror") {
100                         print STDERR "Wrong local cache directory -$mirror-.\n";
101                         cleanup();
102                         return;
103                 }
104
105                 if (! -d "$target") {
106                         system("mkdir", "-p", "$target/");
107                 }
108
109                 if (! open TMPDLS, "find $mirror -follow -name $filename 2>/dev/null |") {
110                         print("Failed to search for $filename in $mirror\n");
111                         return;
112                 }
113
114                 my $link;
115
116                 while (defined(my $line = readline TMPDLS)) {
117                         chomp ($link = $line);
118                         if ($. > 1) {
119                                 print("$. or more instances of $filename in $mirror found . Only one instance allowed.\n");
120                                 return;
121                         }
122                 }
123
124                 close TMPDLS;
125
126                 if (! $link) {
127                         print("No instances of $filename found in $mirror.\n");
128                         return;
129                 }
130
131                 print("Copying $filename from $link\n");
132                 copy($link, "$target/$filename.dl");
133
134                 $hash_cmd and do {
135                         if (system("cat '$target/$filename.dl' | $hash_cmd > '$target/$filename.hash'")) {
136                                 print("Failed to generate hash for $filename\n");
137                                 return;
138                         }
139                 };
140         } else {
141                 my @cmd = download_cmd("$mirror/$url_filename");
142                 open(FETCH_FD, '-|', @cmd) or die "Cannot launch curl or wget.\n";
143                 $hash_cmd and do {
144                         open MD5SUM, "| $hash_cmd > '$target/$filename.hash'" or die "Cannot launch $hash_cmd.\n";
145                 };
146                 open OUTPUT, "> $target/$filename.dl" or die "Cannot create file $target/$filename.dl: $!\n";
147                 my $buffer;
148                 while (read FETCH_FD, $buffer, 1048576) {
149                         $hash_cmd and print MD5SUM $buffer;
150                         print OUTPUT $buffer;
151                 }
152                 $hash_cmd and close MD5SUM;
153                 close FETCH_FD;
154                 close OUTPUT;
155
156                 if ($? >> 8) {
157                         print STDERR "Download failed.\n";
158                         cleanup();
159                         return;
160                 }
161         }
162
163         $hash_cmd and do {
164                 my $sum = `cat "$target/$filename.hash"`;
165                 $sum =~ /^(\w+)\s*/ or die "Could not generate file hash\n";
166                 $sum = $1;
167
168                 if ($sum ne $file_hash) {
169                         print STDERR "MD5 sum of the downloaded file does not match (file: $sum, requested: $file_hash) - deleting download.\n";
170                         cleanup();
171                         return;
172                 }
173         };
174
175         unlink "$target/$filename";
176         system("mv", "$target/$filename.dl", "$target/$filename");
177         cleanup();
178 }
179
180 sub cleanup
181 {
182         unlink "$target/$filename.dl";
183         unlink "$target/$filename.hash";
184 }
185
186 @mirrors = localmirrors();
187
188 foreach my $mirror (@ARGV) {
189         if ($mirror =~ /^\@SF\/(.+)$/) {
190                 # give sourceforge a few more tries, because it redirects to different mirrors
191                 #for (1 .. 5) {
192                 #       push @mirrors, "http://downloads.sourceforge.net/$1";
193                 #}
194         } elsif ($mirror =~ /^\@APACHE\/(.+)$/) {
195                 push @mirrors, "https://mirror.netcologne.de/apache.org/$1";
196                 push @mirrors, "https://mirror.aarnet.edu.au/pub/apache/$1";
197         } elsif ($mirror =~ /^\@GNU\/(.+)$/) {
198                 push @mirrors, "https://mirrors.rit.edu/gnu/$1";
199                 push @mirrors, "https://mirror.netcologne.de/gnu/$1";
200         } elsif ($mirror =~ /^\@SAVANNAH\/(.+)$/) {
201                 push @mirrors, "https://mirror.netcologne.de/savannah/$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://cdn.kernel.org/pub/$dir";
211                         push @mirrors, "https://mirror.rackspace.com/kernel.org/$dir";
212                 }
213     } elsif ($mirror =~ /^\@KERNEL_LIBRE\/(.+)$/) {
214                 my @extra = ( $1 );
215                 if ($filename =~ /linux-libre-\d+\.\d+(?:\.\d+)?-rc-gnu/) {
216                         push @extra, "$extra[0]/testing";
217                 } elsif ($filename =~ /linux-libre-(\d+\.\d+(?:\.\d+)?)-gnu/) {
218                         push @extra, "$extra[0]/v$1";
219                 }
220                 foreach my $dir (@extra) {
221                         push @mirrors, "https://linux-libre.fsfla.org/pub/linux-libre/releases/$dir";
222                 }
223     } elsif ($mirror =~ /^\@GNOME\/(.+)$/) {
224                 push @mirrors, "https://mirror.csclub.uwaterloo.ca/gnome/sources/$1";
225     }
226     else {
227                 push @mirrors, $mirror;
228         }
229 }
230
231 push @mirrors, 'https://librecmc.org/librecmc/downloads/sources/v1.5';
232
233 while (!-f "$target/$filename") {
234         my $mirror = shift @mirrors;
235         $mirror or die "No more mirrors to try - giving up.\n";
236
237         download($mirror);
238 }
239
240 $SIG{INT} = \&cleanup;
241