download.pl 5.1 KB

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