download.pl 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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> <md5sum> <url filename> [<mirror> ...]\n";
  13. my $url_filename;
  14. my $target = shift @ARGV;
  15. my $filename = shift @ARGV;
  16. my $md5sum = 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. my $md5cmd = which("md5sum") || which("md5") || die 'no md5 checksum program found, please install md5 or md5sum';
  54. chomp $md5cmd;
  55. sub download
  56. {
  57. my $mirror = shift;
  58. my $options = $ENV{WGET_OPTIONS} || "";
  59. $mirror =~ s!/$!!;
  60. if ($mirror =~ s!^file://!!) {
  61. if (! -d "$mirror") {
  62. print STDERR "Wrong local cache directory -$mirror-.\n";
  63. cleanup();
  64. return;
  65. }
  66. if (! -d "$target") {
  67. system("mkdir", "-p", "$target/");
  68. }
  69. if (! open TMPDLS, "find $mirror -follow -name $filename 2>/dev/null |") {
  70. print("Failed to search for $filename in $mirror\n");
  71. return;
  72. }
  73. my $link;
  74. while (defined(my $line = readline TMPDLS)) {
  75. chomp ($link = $line);
  76. if ($. > 1) {
  77. print("$. or more instances of $filename in $mirror found . Only one instance allowed.\n");
  78. return;
  79. }
  80. }
  81. close TMPDLS;
  82. if (! $link) {
  83. print("No instances of $filename found in $mirror.\n");
  84. return;
  85. }
  86. print("Copying $filename from $link\n");
  87. copy($link, "$target/$filename.dl");
  88. if (system("$md5cmd '$target/$filename.dl' > '$target/$filename.md5sum'")) {
  89. print("Failed to generate md5 sum for $filename\n");
  90. return;
  91. }
  92. } else {
  93. open WGET, "wget -t5 --timeout=20 --no-check-certificate $options -O- '$mirror/$url_filename' |" or die "Cannot launch wget.\n";
  94. open MD5SUM, "| $md5cmd > '$target/$filename.md5sum'" or die "Cannot launch md5sum.\n";
  95. open OUTPUT, "> $target/$filename.dl" or die "Cannot create file $target/$filename.dl: $!\n";
  96. my $buffer;
  97. while (read WGET, $buffer, 1048576) {
  98. print MD5SUM $buffer;
  99. print OUTPUT $buffer;
  100. }
  101. close MD5SUM;
  102. close WGET;
  103. close OUTPUT;
  104. if ($? >> 8) {
  105. print STDERR "Download failed.\n";
  106. cleanup();
  107. return;
  108. }
  109. }
  110. my $sum = `cat "$target/$filename.md5sum"`;
  111. $sum =~ /^(\w+)\s*/ or die "Could not generate md5sum\n";
  112. $sum = $1;
  113. if (($md5sum =~ /\w{32}/) and ($sum ne $md5sum)) {
  114. print STDERR "MD5 sum of the downloaded file does not match (file: $sum, requested: $md5sum) - deleting download.\n";
  115. cleanup();
  116. return;
  117. }
  118. unlink "$target/$filename";
  119. system("mv", "$target/$filename.dl", "$target/$filename");
  120. cleanup();
  121. }
  122. sub cleanup
  123. {
  124. unlink "$target/$filename.dl";
  125. unlink "$target/$filename.md5sum";
  126. }
  127. @mirrors = localmirrors();
  128. foreach my $mirror (@ARGV) {
  129. if ($mirror =~ /^\@SF\/(.+)$/) {
  130. # give sourceforge a few more tries, because it redirects to different mirrors
  131. for (1 .. 5) {
  132. push @mirrors, "http://downloads.sourceforge.net/$1";
  133. }
  134. } elsif ($mirror =~ /^\@APACHE\/(.+)$/) {
  135. push @mirrors, "http://ftp.tudelft.nl/apache/$1";
  136. push @mirrors, "http://apache.openmirror.de/$1";
  137. push @mirrors, "http://mirrors.ocf.berkeley.edu/apache/$1";
  138. push @mirrors, "http://mirror.cc.columbia.edu/pub/software/apache/$1";
  139. push @mirrors, "http://ftp.jaist.ac.jp/pub/apache/$1";
  140. } elsif ($mirror =~ /^\@GITHUB\/(.+)$/) {
  141. # give github a few more tries (different mirrors)
  142. for (1 .. 5) {
  143. push @mirrors, "https://raw.githubusercontent.com/$1";
  144. }
  145. } elsif ($mirror =~ /^\@GNU\/(.+)$/) {
  146. push @mirrors, "http://ftpmirror.gnu.org/$1";
  147. push @mirrors, "http://ftp.gnu.org/pub/gnu/$1";
  148. push @mirrors, "ftp://ftp.belnet.be/mirror/ftp.gnu.org/gnu/$1";
  149. push @mirrors, "ftp://ftp.mirror.nl/pub/mirror/gnu/$1";
  150. push @mirrors, "http://mirror.switch.ch/ftp/mirror/gnu/$1";
  151. } elsif ($mirror =~ /^\@SAVANNAH\/(.+)$/) {
  152. push @mirrors, "http://download.savannah.gnu.org/releases/$1";
  153. push @mirrors, "http://nongnu.uib.no/$1";
  154. push @mirrors, "http://ftp.igh.cnrs.fr/pub/nongnu/$1";
  155. push @mirrors, "http://download-mirror.savannah.gnu.org/releases/$1";
  156. } elsif ($mirror =~ /^\@KERNEL\/(.+)$/) {
  157. my @extra = ( $1 );
  158. if ($filename =~ /linux-\d+\.\d+(?:\.\d+)?-rc/) {
  159. push @extra, "$extra[0]/testing";
  160. } elsif ($filename =~ /linux-(\d+\.\d+(?:\.\d+)?)/) {
  161. push @extra, "$extra[0]/longterm/v$1";
  162. }
  163. foreach my $dir (@extra) {
  164. push @mirrors, "https://cdn.kernel.org/pub/$dir";
  165. push @mirrors, "https://www.kernel.org/pub/$dir";
  166. }
  167. } elsif ($mirror =~ /^\@GNOME\/(.+)$/) {
  168. push @mirrors, "http://ftp.gnome.org/pub/GNOME/sources/$1";
  169. push @mirrors, "http://ftp.unina.it/pub/linux/GNOME/sources/$1";
  170. push @mirrors, "http://fr2.rpmfind.net/linux/gnome.org/sources/$1";
  171. push @mirrors, "ftp://ftp.dit.upm.es/pub/GNOME/sources/$1";
  172. push @mirrors, "ftp://ftp.no.gnome.org/pub/GNOME/sources/$1";
  173. push @mirrors, "http://ftp.acc.umu.se/pub/GNOME/sources/$1";
  174. push @mirrors, "http://ftp.belnet.be/mirror/ftp.gnome.org/sources/$1";
  175. push @mirrors, "http://linorg.usp.br/gnome/sources/$1";
  176. push @mirrors, "http://mirror.aarnet.edu.au/pub/GNOME/sources/$1";
  177. push @mirrors, "http://mirrors.ibiblio.org/pub/mirrors/gnome/sources/$1";
  178. push @mirrors, "ftp://ftp.cse.buffalo.edu/pub/Gnome/sources/$1";
  179. push @mirrors, "ftp://ftp.nara.wide.ad.jp/pub/X11/GNOME/sources/$1";
  180. }
  181. else {
  182. push @mirrors, $mirror;
  183. }
  184. }
  185. #push @mirrors, 'http://mirror1.openwrt.org';
  186. push @mirrors, 'http://mirror2.openwrt.org/sources';
  187. push @mirrors, 'http://downloads.openwrt.org/sources';
  188. while (!$ok) {
  189. my $mirror = shift @mirrors;
  190. $mirror or die "No more mirrors to try - giving up.\n";
  191. download($mirror);
  192. -f "$target/$filename" and $ok = 1;
  193. }
  194. $SIG{INT} = \&cleanup;