download.pl 2.9 KB

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