scripts/download.pl: fix sha256 hash command (#21931)
[openwrt/openwrt.git] / scripts / download.pl
1 #!/usr/bin/env perl
2 #
3 # Copyright (C) 2006 OpenWrt.org
4 #
5 # This is free software, licensed under the GNU General Public License v2.
6 # See /LICENSE for more information.
7 #
8
9 use strict;
10 use warnings;
11 use File::Basename;
12 use File::Copy;
13
14 @ARGV > 2 or die "Syntax: $0 <target dir> <filename> <hash> <url filename> [<mirror> ...]\n";
15
16 my $url_filename;
17 my $target = shift @ARGV;
18 my $filename = shift @ARGV;
19 my $file_hash = shift @ARGV;
20 $url_filename = shift @ARGV unless $ARGV[0] =~ /:\/\//;
21 my $scriptdir = dirname($0);
22 my @mirrors;
23 my $ok;
24
25 $url_filename or $url_filename = $filename;
26
27 sub localmirrors {
28 my @mlist;
29 open LM, "$scriptdir/localmirrors" and do {
30 while (<LM>) {
31 chomp $_;
32 push @mlist, $_ if $_;
33 }
34 close LM;
35 };
36 open CONFIG, "<".$ENV{'TOPDIR'}."/.config" and do {
37 while (<CONFIG>) {
38 /^CONFIG_LOCALMIRROR="(.+)"/ and do {
39 chomp;
40 my @local_mirrors = split(/;/, $1);
41 push @mlist, @local_mirrors;
42 };
43 }
44 close CONFIG;
45 };
46
47 my $mirror = $ENV{'DOWNLOAD_MIRROR'};
48 $mirror and push @mlist, split(/;/, $mirror);
49
50 return @mlist;
51 }
52
53 sub which($) {
54 my $prog = shift;
55 my $res = `which $prog`;
56 $res or return undef;
57 $res =~ /^no / and return undef;
58 $res =~ /not found/ and return undef;
59 return $res;
60 }
61
62 sub hash_cmd() {
63 my $len = length($file_hash);
64 my $cmd;
65
66 $len == 64 and return "openssl dgst -sha256 | sed -e 's,.*= ,,'";
67 $len == 32 and do {
68 my $cmd = which("md5sum") || which("md5") || die 'no md5 checksum program found, please install md5 or md5sum';
69 chomp $cmd;
70 return $cmd;
71 };
72 return undef;
73 }
74
75 my $hash_cmd = hash_cmd();
76
77 sub download
78 {
79 my $mirror = shift;
80 my $options = $ENV{WGET_OPTIONS} || "";
81
82 $mirror =~ s!/$!!;
83
84 if ($mirror =~ s!^file://!!) {
85 if (! -d "$mirror") {
86 print STDERR "Wrong local cache directory -$mirror-.\n";
87 cleanup();
88 return;
89 }
90
91 if (! -d "$target") {
92 system("mkdir", "-p", "$target/");
93 }
94
95 if (! open TMPDLS, "find $mirror -follow -name $filename 2>/dev/null |") {
96 print("Failed to search for $filename in $mirror\n");
97 return;
98 }
99
100 my $link;
101
102 while (defined(my $line = readline TMPDLS)) {
103 chomp ($link = $line);
104 if ($. > 1) {
105 print("$. or more instances of $filename in $mirror found . Only one instance allowed.\n");
106 return;
107 }
108 }
109
110 close TMPDLS;
111
112 if (! $link) {
113 print("No instances of $filename found in $mirror.\n");
114 return;
115 }
116
117 print("Copying $filename from $link\n");
118 copy($link, "$target/$filename.dl");
119
120 $hash_cmd and do {
121 if (system("cat '$target/$filename.dl' | $hash_cmd > '$target/$filename.hash'")) {
122 print("Failed to generate hash for $filename\n");
123 return;
124 }
125 };
126 } else {
127 open WGET, "wget -t5 --timeout=20 --no-check-certificate $options -O- '$mirror/$url_filename' |" or die "Cannot launch wget.\n";
128 $hash_cmd and do {
129 open MD5SUM, "| $hash_cmd > '$target/$filename.hash'" or die "Cannot launch $hash_cmd.\n";
130 };
131 open OUTPUT, "> $target/$filename.dl" or die "Cannot create file $target/$filename.dl: $!\n";
132 my $buffer;
133 while (read WGET, $buffer, 1048576) {
134 $hash_cmd and print MD5SUM $buffer;
135 print OUTPUT $buffer;
136 }
137 $hash_cmd and close MD5SUM;
138 close WGET;
139 close OUTPUT;
140
141 if ($? >> 8) {
142 print STDERR "Download failed.\n";
143 cleanup();
144 return;
145 }
146 }
147
148 $hash_cmd and do {
149 my $sum = `cat "$target/$filename.hash"`;
150 $sum =~ /^(\w+)\s*/ or die "Could not generate file hash\n";
151 $sum = $1;
152
153 if ($sum ne $file_hash) {
154 print STDERR "MD5 sum of the downloaded file does not match (file: $sum, requested: $file_hash) - deleting download.\n";
155 cleanup();
156 return;
157 }
158 };
159
160 unlink "$target/$filename";
161 system("mv", "$target/$filename.dl", "$target/$filename");
162 cleanup();
163 }
164
165 sub cleanup
166 {
167 unlink "$target/$filename.dl";
168 unlink "$target/$filename.hash";
169 }
170
171 @mirrors = localmirrors();
172
173 foreach my $mirror (@ARGV) {
174 if ($mirror =~ /^\@SF\/(.+)$/) {
175 # give sourceforge a few more tries, because it redirects to different mirrors
176 for (1 .. 5) {
177 push @mirrors, "http://downloads.sourceforge.net/$1";
178 }
179 } elsif ($mirror =~ /^\@APACHE\/(.+)$/) {
180 push @mirrors, "http://ftp.tudelft.nl/apache/$1";
181 push @mirrors, "http://apache.openmirror.de/$1";
182 push @mirrors, "http://mirrors.ocf.berkeley.edu/apache/$1";
183 push @mirrors, "http://mirror.cc.columbia.edu/pub/software/apache/$1";
184 push @mirrors, "http://ftp.jaist.ac.jp/pub/apache/$1";
185 } elsif ($mirror =~ /^\@GNU\/(.+)$/) {
186 push @mirrors, "http://ftpmirror.gnu.org/$1";
187 push @mirrors, "http://ftp.gnu.org/pub/gnu/$1";
188 push @mirrors, "ftp://ftp.belnet.be/mirror/ftp.gnu.org/gnu/$1";
189 push @mirrors, "ftp://ftp.mirror.nl/pub/mirror/gnu/$1";
190 push @mirrors, "http://mirror.switch.ch/ftp/mirror/gnu/$1";
191 } elsif ($mirror =~ /^\@SAVANNAH\/(.+)$/) {
192 push @mirrors, "http://download.savannah.gnu.org/releases/$1";
193 push @mirrors, "http://nongnu.uib.no/$1";
194 push @mirrors, "http://ftp.igh.cnrs.fr/pub/nongnu/$1";
195 push @mirrors, "http://download-mirror.savannah.gnu.org/releases/$1";
196 } elsif ($mirror =~ /^\@KERNEL\/(.+)$/) {
197 my @extra = ( $1 );
198 if ($filename =~ /linux-\d+\.\d+(?:\.\d+)?-rc/) {
199 push @extra, "$extra[0]/testing";
200 } elsif ($filename =~ /linux-(\d+\.\d+(?:\.\d+)?)/) {
201 push @extra, "$extra[0]/longterm/v$1";
202 }
203 foreach my $dir (@extra) {
204 push @mirrors, "https://kernel.org/pub/$dir";
205 push @mirrors, "ftp://kernel.org/pub/$dir";
206 }
207 } elsif ($mirror =~ /^\@GNOME\/(.+)$/) {
208 push @mirrors, "http://ftp.gnome.org/pub/GNOME/sources/$1";
209 push @mirrors, "http://www.mirrorservice.org/sites/ftp.gnome.org/pub/GNOME/sources/$1";
210 push @mirrors, "http://fr2.rpmfind.net/linux/gnome.org/sources/$1";
211 push @mirrors, "http://ftp.acc.umu.se/pub/GNOME/sources/$1";
212 push @mirrors, "http://ftp.belnet.be/ftp.gnome.org/sources/$1";
213 push @mirrors, "ftp://ftp.cse.buffalo.edu/pub/Gnome/sources/$1";
214 push @mirrors, "ftp://ftp.nara.wide.ad.jp/pub/X11/GNOME/sources/$1";
215 }
216 else {
217 push @mirrors, $mirror;
218 }
219 }
220
221 #push @mirrors, 'http://mirror1.openwrt.org';
222 push @mirrors, 'http://mirror2.openwrt.org/sources';
223 push @mirrors, 'http://downloads.openwrt.org/sources';
224
225 while (!$ok) {
226 my $mirror = shift @mirrors;
227 $mirror or die "No more mirrors to try - giving up.\n";
228
229 download($mirror);
230 -f "$target/$filename" and $ok = 1;
231 }
232
233 $SIG{INT} = \&cleanup;
234