download.pl 8.0 KB

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