2
0

download.pl 4.8 KB

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