download.pl 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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> [<mirror> ...]\n";
  13. my $target = shift @ARGV;
  14. my $filename = shift @ARGV;
  15. my $md5sum = shift @ARGV;
  16. my $scriptdir = dirname($0);
  17. my @mirrors;
  18. my $ok;
  19. sub localmirrors {
  20. my @mlist;
  21. open LM, "$scriptdir/localmirrors" and do {
  22. while (<LM>) {
  23. chomp $_;
  24. push @mlist, $_ if $_;
  25. }
  26. close LM;
  27. };
  28. open CONFIG, "<".$ENV{'TOPDIR'}."/.config" and do {
  29. while (<CONFIG>) {
  30. /^CONFIG_LOCALMIRROR="(.+)"/ and do {
  31. chomp;
  32. my @local_mirrors = split(/;/, $1);
  33. push @mlist, @local_mirrors;
  34. };
  35. }
  36. close CONFIG;
  37. };
  38. return @mlist;
  39. }
  40. sub which($) {
  41. my $prog = shift;
  42. my $res = `which $prog`;
  43. $res or return undef;
  44. $res =~ /^no / and return undef;
  45. $res =~ /not found/ and return undef;
  46. return $res;
  47. }
  48. my $md5cmd = which("md5sum");
  49. $md5cmd or $md5cmd = which("md5");
  50. $md5cmd or die 'no md5 checksum program found, please install md5 or md5sum';
  51. chomp $md5cmd;
  52. sub download
  53. {
  54. my $mirror = shift;
  55. my $options = $ENV{WGET_OPTIONS};
  56. $options or $options = "";
  57. $mirror =~ s/\/$//;
  58. if( $mirror =~ /^file:\/\// ) {
  59. my $cache = $mirror;
  60. $cache =~ s/file:\/\///g;
  61. if(system("test -d $cache")) {
  62. print STDERR "Wrong local cache directory -$cache-.\n";
  63. cleanup();
  64. return;
  65. }
  66. if(! -d $target) {
  67. system("mkdir -p $target/");
  68. }
  69. if (open TMPDLS, "find $cache -follow -name $filename 2>/dev/null |"){
  70. my $i = 0;
  71. my $link = "";
  72. while (defined($link = readline TMPDLS)) {
  73. chomp $link;
  74. $i++;
  75. if ($i > 1) {
  76. print("$i or more instances of $filename in $cache found . Only one instance allowed.\n");
  77. return;
  78. }
  79. }
  80. close TMPDLS;
  81. if ($i < 1) {
  82. print("No instances of $filename found in $cache.\n");
  83. return;
  84. } elsif ($i == 1){
  85. print("Copying $filename from $link\n");
  86. copy($link, "$target/$filename.dl");
  87. }
  88. } else {
  89. print("Failed to search for $filename in $cache\n");
  90. return;
  91. }
  92. system("$md5cmd $target/$filename.dl > \"$target/$filename.md5sum\" ") == 0 or return;
  93. } else {
  94. open WGET, "wget -t5 --timeout=20 --no-check-certificate $options -O- \"$mirror/$filename\" |" or die "Cannot launch wget.\n";
  95. open MD5SUM, "| $md5cmd > \"$target/$filename.md5sum\"" or die "Cannot launch md5sum.\n";
  96. open OUTPUT, "> $target/$filename.dl" or die "Cannot create file $target/$filename.dl: $!\n";
  97. my $buffer;
  98. while (read WGET, $buffer, 1048576) {
  99. print MD5SUM $buffer;
  100. print OUTPUT $buffer;
  101. }
  102. close MD5SUM;
  103. close WGET;
  104. close OUTPUT;
  105. if (($? >> 8) != 0 ) {
  106. print STDERR "Download failed.\n";
  107. cleanup();
  108. return;
  109. }
  110. }
  111. my $sum = `cat "$target/$filename.md5sum"`;
  112. $sum =~ /^(\w+)\s*/ or die "Could not generate md5sum\n";
  113. $sum = $1;
  114. if (($md5sum =~ /\w{32}/) and ($sum ne $md5sum)) {
  115. print STDERR "MD5 sum of the downloaded file does not match (file: $sum, requested: $md5sum) - deleting download.\n";
  116. cleanup();
  117. return;
  118. }
  119. unlink "$target/$filename";
  120. system("mv \"$target/$filename.dl\" \"$target/$filename\"");
  121. cleanup();
  122. }
  123. sub cleanup
  124. {
  125. unlink "$target/$filename.dl";
  126. unlink "$target/$filename.md5sum";
  127. }
  128. @mirrors = localmirrors();
  129. foreach my $mirror (@ARGV) {
  130. if ($mirror =~ /^\@SF\/(.+)$/) {
  131. # give sourceforge a few more tries, because it redirects to different mirrors
  132. for (1 .. 5) {
  133. push @mirrors, "http://downloads.sourceforge.net/$1";
  134. }
  135. } elsif ($mirror =~ /^\@GNU\/(.+)$/) {
  136. push @mirrors, "ftp://ftp.gnu.org/gnu/$1";
  137. push @mirrors, "ftp://ftp.belnet.be/mirror/ftp.gnu.org/gnu/$1";
  138. push @mirrors, "ftp://ftp.mirror.nl/pub/mirror/gnu/$1";
  139. push @mirrors, "http://mirror.switch.ch/ftp/mirror/gnu/$1";
  140. push @mirrors, "ftp://ftp.uu.net/archive/systems/gnu/$1";
  141. push @mirrors, "ftp://ftp.eu.uu.net/pub/gnu/$1";
  142. push @mirrors, "ftp://ftp.leo.org/pub/comp/os/unix/gnu/$1";
  143. push @mirrors, "ftp://ftp.digex.net/pub/gnu/$1";
  144. } elsif ($mirror =~ /^\@KERNEL\/(.+)$/) {
  145. my @extra = ( $1 );
  146. if ($filename =~ /linux-\d+\.\d+(?:\.\d+)?-rc/) {
  147. push @extra, "$extra[0]/testing";
  148. } elsif ($filename =~ /linux-(\d+\.\d+(?:\.\d+)?)/) {
  149. push @extra, "$extra[0]/longterm/v$1";
  150. }
  151. foreach my $dir (@extra) {
  152. push @mirrors, "ftp://ftp.all.kernel.org/pub/$dir";
  153. push @mirrors, "http://ftp.all.kernel.org/pub/$dir";
  154. push @mirrors, "ftp://ftp.de.kernel.org/pub/$dir";
  155. push @mirrors, "http://ftp.de.kernel.org/pub/$dir";
  156. push @mirrors, "ftp://ftp.fr.kernel.org/pub/$dir";
  157. push @mirrors, "http://ftp.fr.kernel.org/pub/$dir";
  158. }
  159. } elsif ($mirror =~ /^\@GNOME\/(.+)$/) {
  160. push @mirrors, "http://ftp.gnome.org/pub/GNOME/sources/$1";
  161. push @mirrors, "http://ftp.unina.it/pub/linux/GNOME/sources/$1";
  162. push @mirrors, "http://fr2.rpmfind.net/linux/gnome.org/sources/$1";
  163. push @mirrors, "ftp://ftp.dit.upm.es/pub/GNOME/sources/$1";
  164. push @mirrors, "ftp://ftp.no.gnome.org/pub/GNOME/sources/$1";
  165. push @mirrors, "http://ftp.acc.umu.se/pub/GNOME/sources/$1";
  166. push @mirrors, "http://ftp.belnet.be/mirror/ftp.gnome.org/sources/$1";
  167. push @mirrors, "http://linorg.usp.br/gnome/sources/$1";
  168. push @mirrors, "http://mirror.aarnet.edu.au/pub/GNOME/sources/$1";
  169. push @mirrors, "http://mirrors.ibiblio.org/pub/mirrors/gnome/sources/$1";
  170. push @mirrors, "ftp://ftp.cse.buffalo.edu/pub/Gnome/sources/$1";
  171. push @mirrors, "ftp://ftp.nara.wide.ad.jp/pub/X11/GNOME/sources/$1";
  172. }
  173. else {
  174. push @mirrors, $mirror;
  175. }
  176. }
  177. #push @mirrors, 'http://mirror1.openwrt.org';
  178. push @mirrors, 'http://mirror2.openwrt.org/sources';
  179. push @mirrors, 'http://downloads.openwrt.org/sources';
  180. while (!$ok) {
  181. my $mirror = shift @mirrors;
  182. $mirror or die "No more mirrors to try - giving up.\n";
  183. download($mirror);
  184. -f "$target/$filename" and $ok = 1;
  185. }
  186. $SIG{INT} = \&cleanup;