download.pl 6.3 KB

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