download.pl 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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. use strict;
  10. use warnings;
  11. use File::Basename;
  12. use File::Copy;
  13. use Text::ParseWords;
  14. @ARGV > 2 or die "Syntax: $0 <target dir> <filename> <hash> <url filename> [<mirror> ...]\n";
  15. my $url_filename;
  16. my $target = glob(shift @ARGV);
  17. my $filename = shift @ARGV;
  18. my $file_hash = shift @ARGV;
  19. $url_filename = shift @ARGV unless $ARGV[0] =~ /:\/\//;
  20. my $scriptdir = dirname($0);
  21. my @mirrors;
  22. my $ok;
  23. $url_filename or $url_filename = $filename;
  24. sub localmirrors {
  25. my @mlist;
  26. open LM, "$scriptdir/localmirrors" and do {
  27. while (<LM>) {
  28. chomp $_;
  29. push @mlist, $_ if $_;
  30. }
  31. close LM;
  32. };
  33. open CONFIG, "<".$ENV{'TOPDIR'}."/.config" and do {
  34. while (<CONFIG>) {
  35. /^CONFIG_LOCALMIRROR="(.+)"/ and do {
  36. chomp;
  37. my @local_mirrors = split(/;/, $1);
  38. push @mlist, @local_mirrors;
  39. };
  40. }
  41. close CONFIG;
  42. };
  43. my $mirror = $ENV{'DOWNLOAD_MIRROR'};
  44. $mirror and push @mlist, split(/;/, $mirror);
  45. return @mlist;
  46. }
  47. sub which($) {
  48. my $prog = shift;
  49. my $res = `command -v $prog`;
  50. $res or return undef;
  51. return $res;
  52. }
  53. sub hash_cmd() {
  54. my $len = length($file_hash);
  55. my $cmd;
  56. $len == 64 and return "$ENV{'MKHASH'} sha256";
  57. $len == 32 and return "$ENV{'MKHASH'} md5";
  58. return undef;
  59. }
  60. sub download_cmd($) {
  61. my $url = shift;
  62. my $have_curl = 0;
  63. if (open CURL, '-|', 'curl', '--version') {
  64. if (defined(my $line = readline CURL)) {
  65. $have_curl = 1 if $line =~ /^curl /;
  66. }
  67. close CURL;
  68. }
  69. return $have_curl
  70. ? (qw(curl -f --connect-timeout 20 --retry 5 --location --insecure), shellwords($ENV{CURL_OPTIONS} || ''), $url)
  71. : (qw(wget --tries=5 --timeout=20 --no-check-certificate --output-document=-), shellwords($ENV{WGET_OPTIONS} || ''), $url)
  72. ;
  73. }
  74. my $hash_cmd = hash_cmd();
  75. $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";
  76. sub download
  77. {
  78. my $mirror = shift;
  79. my $download_filename = shift;
  80. $mirror =~ s!/$!!;
  81. if ($mirror =~ s!^file://!!) {
  82. if (! -d "$mirror") {
  83. print STDERR "Wrong local cache directory -$mirror-.\n";
  84. cleanup();
  85. return;
  86. }
  87. if (! -d "$target") {
  88. system("mkdir", "-p", "$target/");
  89. }
  90. if (! open TMPDLS, "find $mirror -follow -name $filename 2>/dev/null |") {
  91. print("Failed to search for $filename in $mirror\n");
  92. return;
  93. }
  94. my $link;
  95. while (defined(my $line = readline TMPDLS)) {
  96. chomp ($link = $line);
  97. if ($. > 1) {
  98. print("$. or more instances of $filename in $mirror found . Only one instance allowed.\n");
  99. return;
  100. }
  101. }
  102. close TMPDLS;
  103. if (! $link) {
  104. print("No instances of $filename found in $mirror.\n");
  105. return;
  106. }
  107. print("Copying $filename from $link\n");
  108. copy($link, "$target/$filename.dl");
  109. $hash_cmd and do {
  110. if (system("cat '$target/$filename.dl' | $hash_cmd > '$target/$filename.hash'")) {
  111. print("Failed to generate hash for $filename\n");
  112. return;
  113. }
  114. };
  115. } else {
  116. my @cmd = download_cmd("$mirror/$download_filename");
  117. print STDERR "+ ".join(" ",@cmd)."\n";
  118. open(FETCH_FD, '-|', @cmd) or die "Cannot launch curl or wget.\n";
  119. $hash_cmd and do {
  120. open MD5SUM, "| $hash_cmd > '$target/$filename.hash'" or die "Cannot launch $hash_cmd.\n";
  121. };
  122. open OUTPUT, "> $target/$filename.dl" or die "Cannot create file $target/$filename.dl: $!\n";
  123. my $buffer;
  124. while (read FETCH_FD, $buffer, 1048576) {
  125. $hash_cmd and print MD5SUM $buffer;
  126. print OUTPUT $buffer;
  127. }
  128. $hash_cmd and close MD5SUM;
  129. close FETCH_FD;
  130. close OUTPUT;
  131. if ($? >> 8) {
  132. print STDERR "Download failed.\n";
  133. cleanup();
  134. return;
  135. }
  136. }
  137. $hash_cmd and do {
  138. my $sum = `cat "$target/$filename.hash"`;
  139. $sum =~ /^(\w+)\s*/ or die "Could not generate file hash\n";
  140. $sum = $1;
  141. if ($sum ne $file_hash) {
  142. print STDERR "Hash of the downloaded file does not match (file: $sum, requested: $file_hash) - deleting download.\n";
  143. cleanup();
  144. return;
  145. }
  146. };
  147. unlink "$target/$filename";
  148. system("mv", "$target/$filename.dl", "$target/$filename");
  149. cleanup();
  150. }
  151. sub cleanup
  152. {
  153. unlink "$target/$filename.dl";
  154. unlink "$target/$filename.hash";
  155. }
  156. @mirrors = localmirrors();
  157. foreach my $mirror (@ARGV) {
  158. if ($mirror =~ /^\@SF\/(.+)$/) {
  159. # give sourceforge a few more tries, because it redirects to different mirrors
  160. for (1 .. 5) {
  161. push @mirrors, "https://downloads.sourceforge.net/$1";
  162. }
  163. } elsif ($mirror =~ /^\@OPENWRT$/) {
  164. # use OpenWrt source server directly
  165. } elsif ($mirror =~ /^\@DEBIAN\/(.+)$/) {
  166. push @mirrors, "https://ftp.debian.org/debian/$1";
  167. push @mirrors, "https://mirror.leaseweb.com/debian/$1";
  168. push @mirrors, "https://mirror.netcologne.de/debian/$1";
  169. } elsif ($mirror =~ /^\@APACHE\/(.+)$/) {
  170. push @mirrors, "https://mirror.netcologne.de/apache.org/$1";
  171. push @mirrors, "https://mirror.aarnet.edu.au/pub/apache/$1";
  172. push @mirrors, "https://mirror.csclub.uwaterloo.ca/apache/$1";
  173. push @mirrors, "https://archive.apache.org/dist/$1";
  174. push @mirrors, "http://mirror.cogentco.com/pub/apache/$1";
  175. push @mirrors, "http://mirror.navercorp.com/apache/$1";
  176. push @mirrors, "http://ftp.jaist.ac.jp/pub/apache/$1";
  177. push @mirrors, "ftp://apache.cs.utah.edu/apache.org/$1";
  178. push @mirrors, "ftp://apache.mirrors.ovh.net/ftp.apache.org/dist/$1";
  179. } elsif ($mirror =~ /^\@GITHUB\/(.+)$/) {
  180. # give github a few more tries (different mirrors)
  181. for (1 .. 5) {
  182. push @mirrors, "https://raw.githubusercontent.com/$1";
  183. }
  184. } elsif ($mirror =~ /^\@GNU\/(.+)$/) {
  185. push @mirrors, "https://mirror.csclub.uwaterloo.ca/gnu/$1";
  186. push @mirrors, "https://mirror.netcologne.de/gnu/$1";
  187. push @mirrors, "http://ftp.kddilabs.jp/GNU/gnu/$1";
  188. push @mirrors, "http://www.nic.funet.fi/pub/gnu/gnu/$1";
  189. push @mirrors, "http://mirror.internode.on.net/pub/gnu/$1";
  190. push @mirrors, "http://mirror.navercorp.com/gnu/$1";
  191. push @mirrors, "ftp://mirrors.rit.edu/gnu/$1";
  192. push @mirrors, "ftp://download.xs4all.nl/pub/gnu/$1";
  193. push @mirrors, "https://ftp.gnu.org/gnu/$1";
  194. } elsif ($mirror =~ /^\@SAVANNAH\/(.+)$/) {
  195. push @mirrors, "https://mirror.netcologne.de/savannah/$1";
  196. push @mirrors, "https://mirror.csclub.uwaterloo.ca/nongnu/$1";
  197. push @mirrors, "http://ftp.acc.umu.se/mirror/gnu.org/savannah/$1";
  198. push @mirrors, "http://nongnu.uib.no/$1";
  199. push @mirrors, "http://ftp.igh.cnrs.fr/pub/nongnu/$1";
  200. push @mirrors, "ftp://cdimage.debian.org/mirror/gnu.org/savannah/$1";
  201. push @mirrors, "ftp://ftp.acc.umu.se/mirror/gnu.org/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://download.xs4all.nl/ftp.kernel.org/pub/$dir";
  212. push @mirrors, "https://mirrors.mit.edu/kernel/$dir";
  213. push @mirrors, "http://ftp.nara.wide.ad.jp/pub/kernel.org/$dir";
  214. push @mirrors, "http://www.ring.gr.jp/archives/linux/kernel.org/$dir";
  215. push @mirrors, "ftp://ftp.riken.jp/Linux/kernel.org/$dir";
  216. push @mirrors, "ftp://www.mirrorservice.org/sites/ftp.kernel.org/pub/$dir";
  217. }
  218. } elsif ($mirror =~ /^\@GNOME\/(.+)$/) {
  219. push @mirrors, "https://download.gnome.org/sources/$1";
  220. push @mirrors, "https://mirror.csclub.uwaterloo.ca/gnome/sources/$1";
  221. push @mirrors, "http://ftp.acc.umu.se/pub/GNOME/sources/$1";
  222. push @mirrors, "http://ftp.kaist.ac.kr/gnome/sources/$1";
  223. push @mirrors, "http://www.mirrorservice.org/sites/ftp.gnome.org/pub/GNOME/sources/$1";
  224. push @mirrors, "http://mirror.internode.on.net/pub/gnome/sources/$1";
  225. push @mirrors, "http://ftp.belnet.be/ftp.gnome.org/sources/$1";
  226. push @mirrors, "ftp://ftp.cse.buffalo.edu/pub/Gnome/sources/$1";
  227. push @mirrors, "ftp://ftp.nara.wide.ad.jp/pub/X11/GNOME/sources/$1";
  228. } else {
  229. push @mirrors, $mirror;
  230. }
  231. }
  232. push @mirrors, 'https://sources.cdn.openwrt.org';
  233. push @mirrors, 'https://sources.openwrt.org';
  234. push @mirrors, 'https://mirror2.openwrt.org/sources';
  235. if (-f "$target/$filename") {
  236. $hash_cmd and do {
  237. if (system("cat '$target/$filename' | $hash_cmd > '$target/$filename.hash'")) {
  238. die "Failed to generate hash for $filename\n";
  239. }
  240. my $sum = `cat "$target/$filename.hash"`;
  241. $sum =~ /^(\w+)\s*/ or die "Could not generate file hash\n";
  242. $sum = $1;
  243. cleanup();
  244. exit 0 if $sum eq $file_hash;
  245. die "Hash of the local file $filename does not match (file: $sum, requested: $file_hash) - deleting download.\n";
  246. unlink "$target/$filename";
  247. };
  248. }
  249. while (!-f "$target/$filename") {
  250. my $mirror = shift @mirrors;
  251. $mirror or die "No more mirrors to try - giving up.\n";
  252. download($mirror, $url_filename);
  253. if (!-f "$target/$filename" && $url_filename ne $filename) {
  254. download($mirror, $filename);
  255. }
  256. }
  257. $SIG{INT} = \&cleanup;