download.pl 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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" or return ();
  21. while (<LM>) {
  22. chomp $_;
  23. push @mlist, $_;
  24. }
  25. return @mlist;
  26. }
  27. sub which($) {
  28. my $prog = shift;
  29. my $res = `which $prog`;
  30. $res or return undef;
  31. $res =~ /^no / and return undef;
  32. $res =~ /not found/ and return undef;
  33. return $res;
  34. }
  35. my $md5cmd = which("md5sum");
  36. $md5cmd or $md5cmd = which("md5");
  37. $md5cmd or die 'no md5 checksum program found, please install md5 or md5sum';
  38. chomp $md5cmd;
  39. sub download
  40. {
  41. my $mirror = shift;
  42. my $options = $ENV{WGET_OPTIONS};
  43. $options or $options = "";
  44. $mirror =~ s/\/$//;
  45. open WGET, "wget -t1 --timeout=20 $options -O- \"$mirror/$filename\" |" or die "Cannot launch wget.\n";
  46. open MD5SUM, "| $md5cmd > \"$target/$filename.md5sum\"" or die "Cannot launch md5sum.\n";
  47. open OUTPUT, "> $target/$filename.dl" or die "Cannot create file $target/$filename.dl: $!\n";
  48. my $buffer;
  49. while (read WGET, $buffer, 1048576) {
  50. print MD5SUM $buffer;
  51. print OUTPUT $buffer;
  52. }
  53. close MD5SUM;
  54. close WGET;
  55. close OUTPUT;
  56. if (($? >> 8) != 0 ) {
  57. print STDERR "Download failed.\n";
  58. cleanup();
  59. return;
  60. }
  61. my $sum = `cat "$target/$filename.md5sum"`;
  62. $sum =~ /^(\w+)\s*/ or die "Could not generate md5sum\n";
  63. $sum = $1;
  64. if (($md5sum =~ /\w{32}/) and ($sum ne $md5sum)) {
  65. print STDERR "MD5 sum of the downloaded file does not match - deleting download.\n";
  66. cleanup();
  67. return;
  68. }
  69. unlink "$target/$filename";
  70. system("mv \"$target/$filename.dl\" \"$target/$filename\"");
  71. cleanup();
  72. }
  73. sub cleanup
  74. {
  75. unlink "$target/$filename.dl";
  76. unlink "$target/$filename.md5sum";
  77. }
  78. @mirrors = localmirrors();
  79. foreach my $mirror (@ARGV) {
  80. if ($mirror =~ /^\@SF\/(.+)$/) {
  81. # give sourceforge a few more tries, because it redirects to different mirrors
  82. for (1 .. 5) {
  83. push @mirrors, "http://downloads.sourceforge.net/$1";
  84. }
  85. } elsif ($mirror =~ /^\@GNU\/(.+)$/) {
  86. my $gnupath = $1;
  87. push @mirrors, "ftp://ftp.gnu.org/gnu/$gnupath";
  88. push @mirrors, "ftp://ftp.belnet.be/mirror/ftp.gnu.org/gnu/$gnupath";
  89. push @mirrors, "ftp://ftp.mirror.nl/pub/mirror/gnu/$gnupath";
  90. push @mirrors, "http://mirror.switch.ch/ftp/mirror/gnu/$gnupath";
  91. push @mirrors, "ftp://ftp.uu.net/archive/systems/gnu/$gnupath";
  92. push @mirrors, "ftp://ftp.eu.uu.net/pub/gnu/$gnupath";
  93. push @mirrors, "ftp://ftp.leo.org/pub/comp/os/unix/gnu/$gnupath";
  94. push @mirrors, "ftp://ftp.digex.net/pub/gnu/$gnupath";
  95. } else {
  96. push @mirrors, $mirror;
  97. }
  98. }
  99. #push @mirrors, 'http://mirror1.openwrt.org';
  100. push @mirrors, 'http://mirror2.openwrt.org/sources';
  101. push @mirrors, 'http://downloads.openwrt.org/sources';
  102. while (!$ok) {
  103. my $mirror = shift @mirrors;
  104. $mirror or die "No more mirrors to try - giving up.\n";
  105. download($mirror);
  106. -f "$target/$filename" and $ok = 1;
  107. }
  108. $SIG{INT} = \&cleanup;