2
0

download.pl 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  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 = `which $prog`;
  50. $res or return undef;
  51. $res =~ /^no / and return undef;
  52. $res =~ /not found/ and return undef;
  53. return $res;
  54. }
  55. sub hash_cmd() {
  56. my $len = length($file_hash);
  57. my $cmd;
  58. $len == 64 and return "mkhash sha256";
  59. $len == 32 and return "mkhash md5";
  60. return undef;
  61. }
  62. sub download_cmd($) {
  63. my $url = shift;
  64. my $have_curl = 0;
  65. if (open CURL, '-|', 'curl', '--version') {
  66. if (defined(my $line = readline CURL)) {
  67. $have_curl = 1 if $line =~ /^curl /;
  68. }
  69. close CURL;
  70. }
  71. return $have_curl
  72. ? (qw(curl -f --connect-timeout 20 --retry 5 --location --insecure), shellwords($ENV{CURL_OPTIONS} || ''), $url)
  73. : (qw(wget --tries=5 --timeout=20 --no-check-certificate --output-document=-), shellwords($ENV{WGET_OPTIONS} || ''), $url)
  74. ;
  75. }
  76. my $hash_cmd = hash_cmd();
  77. $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";
  78. sub download
  79. {
  80. my $mirror = shift;
  81. my $download_filename = shift;
  82. $mirror =~ s!/$!!;
  83. if ($mirror =~ s!^file://!!) {
  84. if (! -d "$mirror") {
  85. print STDERR "Wrong local cache directory -$mirror-.\n";
  86. cleanup();
  87. return;
  88. }
  89. if (! -d "$target") {
  90. system("mkdir", "-p", "$target/");
  91. }
  92. if (! open TMPDLS, "find $mirror -follow -name $filename 2>/dev/null |") {
  93. print("Failed to search for $filename in $mirror\n");
  94. return;
  95. }
  96. my $link;
  97. while (defined(my $line = readline TMPDLS)) {
  98. chomp ($link = $line);
  99. if ($. > 1) {
  100. print("$. or more instances of $filename in $mirror found . Only one instance allowed.\n");
  101. return;
  102. }
  103. }
  104. close TMPDLS;
  105. if (! $link) {
  106. print("No instances of $filename found in $mirror.\n");
  107. return;
  108. }
  109. print("Copying $filename from $link\n");
  110. copy($link, "$target/$filename.dl");
  111. $hash_cmd and do {
  112. if (system("cat '$target/$filename.dl' | $hash_cmd > '$target/$filename.hash'")) {
  113. print("Failed to generate hash for $filename\n");
  114. return;
  115. }
  116. };
  117. } else {
  118. my @cmd = download_cmd("$mirror/$download_filename");
  119. print STDERR "+ ".join(" ",@cmd)."\n";
  120. open(FETCH_FD, '-|', @cmd) or die "Cannot launch curl or wget.\n";
  121. $hash_cmd and do {
  122. open MD5SUM, "| $hash_cmd > '$target/$filename.hash'" or die "Cannot launch $hash_cmd.\n";
  123. };
  124. open OUTPUT, "> $target/$filename.dl" or die "Cannot create file $target/$filename.dl: $!\n";
  125. my $buffer;
  126. while (read FETCH_FD, $buffer, 1048576) {
  127. $hash_cmd and print MD5SUM $buffer;
  128. print OUTPUT $buffer;
  129. }
  130. $hash_cmd and close MD5SUM;
  131. close FETCH_FD;
  132. close OUTPUT;
  133. if ($? >> 8) {
  134. print STDERR "Download failed.\n";
  135. cleanup();
  136. return;
  137. }
  138. }
  139. $hash_cmd and do {
  140. my $sum = `cat "$target/$filename.hash"`;
  141. $sum =~ /^(\w+)\s*/ or die "Could not generate file hash\n";
  142. $sum = $1;
  143. if ($sum ne $file_hash) {
  144. print STDERR "Hash of the downloaded file does not match (file: $sum, requested: $file_hash) - deleting download.\n";
  145. cleanup();
  146. return;
  147. }
  148. };
  149. unlink "$target/$filename";
  150. system("mv", "$target/$filename.dl", "$target/$filename");
  151. cleanup();
  152. }
  153. sub cleanup
  154. {
  155. unlink "$target/$filename.dl";
  156. unlink "$target/$filename.hash";
  157. }
  158. @mirrors = localmirrors();
  159. foreach my $mirror (@ARGV) {
  160. if ($mirror =~ /^\@SF\/(.+)$/) {
  161. # give sourceforge a few more tries, because it redirects to different mirrors
  162. for (1 .. 5) {
  163. push @mirrors, "https://downloads.sourceforge.net/$1";
  164. }
  165. } elsif ($mirror =~ /^\@APACHE\/(.+)$/) {
  166. push @mirrors, "https://mirror.netcologne.de/apache.org/$1";
  167. push @mirrors, "https://mirror.aarnet.edu.au/pub/apache/$1";
  168. push @mirrors, "https://mirror.csclub.uwaterloo.ca/apache/$1";
  169. push @mirrors, "http://mirror.cogentco.com/pub/apache/$1";
  170. push @mirrors, "http://mirror.navercorp.com/apache/$1";
  171. push @mirrors, "http://ftp.jaist.ac.jp/pub/apache/$1";
  172. push @mirrors, "ftp://apache.cs.utah.edu/apache.org/$1";
  173. push @mirrors, "ftp://apache.mirrors.ovh.net/ftp.apache.org/dist/$1";
  174. } elsif ($mirror =~ /^\@DEBIAN\/(.+)$/) {
  175. push @mirrors, "https://ftp.debian.org/debian/$1";
  176. push @mirrors, "https://mirror.leaseweb.com/debian/$1";
  177. push @mirrors, "https://mirror.netcologne.de/debian/$1";
  178. } elsif ($mirror =~ /^\@GITHUB\/(.+)$/) {
  179. # give github a few more tries (different mirrors)
  180. for (1 .. 5) {
  181. push @mirrors, "https://raw.githubusercontent.com/$1";
  182. }
  183. } elsif ($mirror =~ /^\@GNU\/(.+)$/) {
  184. push @mirrors, "https://mirror.csclub.uwaterloo.ca/gnu/$1";
  185. push @mirrors, "https://mirror.netcologne.de/gnu/$1";
  186. push @mirrors, "http://ftp.kddilabs.jp/GNU/gnu/$1";
  187. push @mirrors, "http://www.nic.funet.fi/pub/gnu/gnu/$1";
  188. push @mirrors, "http://mirror.internode.on.net/pub/gnu/$1";
  189. push @mirrors, "http://mirror.navercorp.com/gnu/$1";
  190. push @mirrors, "ftp://mirrors.rit.edu/gnu/$1";
  191. push @mirrors, "ftp://download.xs4all.nl/pub/gnu/";
  192. } elsif ($mirror =~ /^\@SAVANNAH\/(.+)$/) {
  193. push @mirrors, "https://mirror.netcologne.de/savannah/$1";
  194. push @mirrors, "https://mirror.csclub.uwaterloo.ca/nongnu/$1";
  195. push @mirrors, "http://ftp.acc.umu.se/mirror/gnu.org/savannah/$1";
  196. push @mirrors, "http://nongnu.uib.no/$1";
  197. push @mirrors, "http://ftp.igh.cnrs.fr/pub/nongnu/$1";
  198. push @mirrors, "http://public.p-knowledge.co.jp/Savannah-nongnu-mirror/$1";
  199. push @mirrors, "ftp://cdimage.debian.org/mirror/gnu.org/savannah/$1";
  200. push @mirrors, "ftp://ftp.acc.umu.se/mirror/gnu.org/savannah/$1";
  201. } elsif ($mirror =~ /^\@KERNEL\/(.+)$/) {
  202. my @extra = ( $1 );
  203. if ($filename =~ /linux-\d+\.\d+(?:\.\d+)?-rc/) {
  204. push @extra, "$extra[0]/testing";
  205. } elsif ($filename =~ /linux-(\d+\.\d+(?:\.\d+)?)/) {
  206. push @extra, "$extra[0]/longterm/v$1";
  207. }
  208. foreach my $dir (@extra) {
  209. push @mirrors, "https://cdn.kernel.org/pub/$dir";
  210. push @mirrors, "https://mirror.rackspace.com/kernel.org/$dir";
  211. push @mirrors, "http://download.xs4all.nl/ftp.kernel.org/pub/$dir";
  212. push @mirrors, "http://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 =~ /^\@KERNEL_LIBRE\/(.+)$/) {
  219. my @extra = ( $1 );
  220. if ($filename =~ /linux-libre-\d+\.\d+(?:\.\d+)?-rc-gnu/) {
  221. push @extra, "$extra[0]/testing";
  222. } elsif ($filename =~ /linux-libre-(\d+\.\d+(?:\.\d+)?)-gnu/) {
  223. push @extra, "$extra[0]/v$1";
  224. }
  225. foreach my $dir (@extra) {
  226. push @mirrors, "https://linux-libre.fsfla.org/pub/linux-libre/releases/$dir";
  227. push @mirrors, "https://librecmc.org/pub/linux-libre/releases/$dir";
  228. }
  229. } elsif ($mirror =~ /^\@GNOME\/(.+)$/) {
  230. push @mirrors, "https://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. push @mirrors, 'https://librecmc.org/librecmc/downloads/sources/v1.5';
  244. if (-f "$target/$filename") {
  245. $hash_cmd and do {
  246. if (system("cat '$target/$filename' | $hash_cmd > '$target/$filename.hash'")) {
  247. die "Failed to generate hash for $filename\n";
  248. }
  249. my $sum = `cat "$target/$filename.hash"`;
  250. $sum =~ /^(\w+)\s*/ or die "Could not generate file hash\n";
  251. $sum = $1;
  252. cleanup();
  253. exit 0 if $sum eq $file_hash;
  254. die "Hash of the local file $filename does not match (file: $sum, requested: $file_hash) - deleting download.\n";
  255. unlink "$target/$filename";
  256. };
  257. }
  258. while (!-f "$target/$filename") {
  259. my $mirror = shift @mirrors;
  260. $mirror or die "No more mirrors to try - giving up.\n";
  261. download($mirror, $url_filename);
  262. if (!-f "$target/$filename" && $url_filename ne $filename) {
  263. download($mirror, $filename);
  264. }
  265. }
  266. $SIG{INT} = \&cleanup;