download.pl 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. #!/usr/bin/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. my $target = shift @ARGV;
  12. my $filename = shift @ARGV;
  13. my $md5sum = shift @ARGV;
  14. my $scriptdir = dirname($0);
  15. my @mirrors;
  16. my $ok;
  17. @ARGV > 0 or die "Syntax: $0 <target dir> <filename> <md5sum> <mirror> [<mirror> ...]\n";
  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. open WGET, "wget -t1 --timeout=20 $options -O- \"$mirror/$filename\" |" or die "Cannot launch wget.\n";
  57. open MD5SUM, "| $md5cmd > \"$target/$filename.md5sum\"" or die "Cannot launch md5sum.\n";
  58. open OUTPUT, "> $target/$filename.dl" or die "Cannot create file $target/$filename.dl: $!\n";
  59. my $buffer;
  60. while (read WGET, $buffer, 1048576) {
  61. print MD5SUM $buffer;
  62. print OUTPUT $buffer;
  63. }
  64. close MD5SUM;
  65. close WGET;
  66. close OUTPUT;
  67. if (($? >> 8) != 0 ) {
  68. print STDERR "Download failed.\n";
  69. cleanup();
  70. return;
  71. }
  72. my $sum = `cat "$target/$filename.md5sum"`;
  73. $sum =~ /^(\w+)\s*/ or die "Could not generate md5sum\n";
  74. $sum = $1;
  75. if (($md5sum =~ /\w{32}/) and ($sum ne $md5sum)) {
  76. print STDERR "MD5 sum of the downloaded file does not match - deleting download.\n";
  77. cleanup();
  78. return;
  79. }
  80. unlink "$target/$filename";
  81. system("mv \"$target/$filename.dl\" \"$target/$filename\"");
  82. cleanup();
  83. }
  84. sub cleanup
  85. {
  86. unlink "$target/$filename.dl";
  87. unlink "$target/$filename.md5sum";
  88. }
  89. @mirrors = localmirrors();
  90. foreach my $mirror (@ARGV) {
  91. if ($mirror =~ /^\@SF\/(.+)$/) {
  92. # give sourceforge a few more tries, because it redirects to different mirrors
  93. for (1 .. 5) {
  94. push @mirrors, "http://downloads.sourceforge.net/$1";
  95. }
  96. } elsif ($mirror =~ /^\@GNU\/(.+)$/) {
  97. my $gnupath = $1;
  98. push @mirrors, "ftp://ftp.gnu.org/gnu/$gnupath";
  99. push @mirrors, "ftp://ftp.belnet.be/mirror/ftp.gnu.org/gnu/$gnupath";
  100. push @mirrors, "ftp://ftp.mirror.nl/pub/mirror/gnu/$gnupath";
  101. push @mirrors, "http://mirror.switch.ch/ftp/mirror/gnu/$gnupath";
  102. push @mirrors, "ftp://ftp.uu.net/archive/systems/gnu/$gnupath";
  103. push @mirrors, "ftp://ftp.eu.uu.net/pub/gnu/$gnupath";
  104. push @mirrors, "ftp://ftp.leo.org/pub/comp/os/unix/gnu/$gnupath";
  105. push @mirrors, "ftp://ftp.digex.net/pub/gnu/$gnupath";
  106. } else {
  107. push @mirrors, $mirror;
  108. }
  109. }
  110. #push @mirrors, 'http://mirror1.openwrt.org';
  111. push @mirrors, 'http://mirror2.openwrt.org/sources';
  112. push @mirrors, 'http://downloads.openwrt.org/sources';
  113. while (!$ok) {
  114. my $mirror = shift @mirrors;
  115. $mirror or die "No more mirrors to try - giving up.\n";
  116. download($mirror);
  117. -f "$target/$filename" and $ok = 1;
  118. }
  119. $SIG{INT} = \&cleanup;