download.pl 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. open WGET, "wget -t1 --timeout=20 -O- \"$mirror/$filename\" |" or die "Cannot launch wget.\n";
  32. open MD5SUM, "| $md5cmd > \"$target/$filename.md5sum\"" or die "Cannot launch md5sum.\n";
  33. open OUTPUT, "> $target/$filename.dl" or die "Cannot create file $target/$filename.dl: $!\n";
  34. my $buffer;
  35. while (read WGET, $buffer, 1048576) {
  36. print MD5SUM $buffer;
  37. print OUTPUT $buffer;
  38. }
  39. close MD5SUM;
  40. close WGET;
  41. close OUTPUT;
  42. if (($? >> 8) != 0 ) {
  43. print STDERR "Download failed.\n";
  44. cleanup();
  45. return;
  46. }
  47. my $sum = `cat "$target/$filename.md5sum"`;
  48. $sum =~ /^(\w+)\s*/ or die "Could not generate md5sum\n";
  49. $sum = $1;
  50. if (($md5sum =~ /\w{32}/) and ($sum ne $md5sum)) {
  51. print STDERR "MD5 sum of the downloaded file does not match - deleting download.\n";
  52. cleanup();
  53. return;
  54. }
  55. unlink "$target/$filename";
  56. system("mv \"$target/$filename.dl\" \"$target/$filename\"");
  57. cleanup();
  58. }
  59. sub cleanup
  60. {
  61. unlink "$target/$filename.dl";
  62. unlink "$target/$filename.md5sum";
  63. }
  64. foreach my $mirror (@ARGV) {
  65. if ($mirror =~ /^\@SF\/(.+)$/) {
  66. my $sfpath = $1;
  67. open SF, "wget -t1 -q -O- 'http://prdownloads.sourceforge.net/$sfpath/$filename' |";
  68. while (<SF>) {
  69. /RADIO NAME=use_default VALUE=(\w+) OnClick="form\.submit\(\)">/ or
  70. /type="radio" name="use_default" value="(\w+)" onclick="form\.submit\(\)"\/>/ and do {
  71. push @mirrors, "http://$1.dl.sourceforge.net/sourceforge/$sfpath";
  72. };
  73. /<a href="\/.+\?use_mirror=(\w+)"><b>Download/ and do {
  74. push @mirrors, "http://$1.dl.sourceforge.net/sourceforge/$sfpath";
  75. };
  76. }
  77. push @mirrors, "http://dl.sourceforge.net/sourceforge/$sfpath";
  78. close SF;
  79. } elsif ($mirror =~ /^\@GNU\/(.+)$/) {
  80. my $gnupath = $1;
  81. push @mirrors, "ftp://ftp.gnu.org/gnu/$gnupath";
  82. push @mirrors, "ftp://ftp.belnet.be/mirror/ftp.gnu.org/gnu/$gnupath";
  83. push @mirrors, "ftp://ftp.mirror.nl/pub/mirror/gnu/$gnupath";
  84. push @mirrors, "http://mirror.switch.ch/ftp/mirror/gnu/$gnupath";
  85. push @mirrors, "ftp://ftp.uu.net/archive/systems/gnu/$gnupath";
  86. push @mirrors, "ftp://ftp.eu.uu.net/pub/gnu/$gnupath";
  87. push @mirrors, "ftp://ftp.leo.org/pub/comp/os/unix/gnu/$gnupath";
  88. push @mirrors, "ftp://ftp.digex.net/pub/gnu/$gnupath";
  89. } else {
  90. push @mirrors, $mirror;
  91. }
  92. }
  93. #push @mirrors, 'http://mirror1.openwrt.org/';
  94. push @mirrors, 'http://mirror2.openwrt.org/sources';
  95. push @mirrors, 'http://downloads.openwrt.org/sources/';
  96. while (!$ok) {
  97. my $mirror = shift @mirrors;
  98. $mirror or die "No more mirrors to try - giving up.\n";
  99. download($mirror);
  100. -f "$target/$filename" and $ok = 1;
  101. }
  102. $SIG{INT} = \&cleanup;