download.pl 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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. use File::Copy;
  12. @ARGV > 2 or die "Syntax: $0 <target dir> <filename> <md5sum> [<mirror> ...]\n";
  13. my $target = shift @ARGV;
  14. my $filename = shift @ARGV;
  15. my $md5sum = shift @ARGV;
  16. my $scriptdir = dirname($0);
  17. my @mirrors;
  18. my $ok;
  19. sub localmirrors {
  20. my @mlist;
  21. open LM, "$scriptdir/localmirrors" and do {
  22. while (<LM>) {
  23. chomp $_;
  24. push @mlist, $_ if $_;
  25. }
  26. close LM;
  27. };
  28. open CONFIG, "<".$ENV{'TOPDIR'}."/.config" and do {
  29. while (<CONFIG>) {
  30. /^CONFIG_LOCALMIRROR="(.+)"/ and do {
  31. chomp;
  32. my @local_mirrors = split(/;/, $1);
  33. push @mlist, @local_mirrors;
  34. };
  35. }
  36. close CONFIG;
  37. };
  38. return @mlist;
  39. }
  40. sub which($) {
  41. my $prog = shift;
  42. my $res = `which $prog`;
  43. $res or return undef;
  44. $res =~ /^no / and return undef;
  45. $res =~ /not found/ and return undef;
  46. return $res;
  47. }
  48. my $md5cmd = which("md5sum") || which("md5") || 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. $mirror =~ s!/$!!;
  55. if ($mirror =~ s!^file://!!) {
  56. if (! -d "$mirror") {
  57. print STDERR "Wrong local cache directory -$mirror-.\n";
  58. cleanup();
  59. return;
  60. }
  61. if (! -d "$target") {
  62. system("mkdir", "-p", "$target/");
  63. }
  64. if (! open TMPDLS, "find $mirror -follow -name $filename 2>/dev/null |") {
  65. print("Failed to search for $filename in $mirror\n");
  66. return;
  67. }
  68. my $link;
  69. while (defined(my $line = readline TMPDLS)) {
  70. chomp ($link = $line);
  71. if ($. > 1) {
  72. print("$. or more instances of $filename in $mirror found . Only one instance allowed.\n");
  73. return;
  74. }
  75. }
  76. close TMPDLS;
  77. if (! $link) {
  78. print("No instances of $filename found in $mirror.\n");
  79. return;
  80. }
  81. print("Copying $filename from $link\n");
  82. copy($link, "$target/$filename.dl");
  83. if (system("$md5cmd '$target/$filename.dl' > '$target/$filename.md5sum'")) {
  84. print("Failed to generate md5 sum for $filename\n");
  85. return;
  86. }
  87. } else {
  88. open WGET, "wget -t5 --timeout=20 --no-check-certificate $options -O- '$mirror/$filename' |" or die "Cannot launch wget.\n";
  89. open MD5SUM, "| $md5cmd > '$target/$filename.md5sum'" or die "Cannot launch md5sum.\n";
  90. open OUTPUT, "> $target/$filename.dl" or die "Cannot create file $target/$filename.dl: $!\n";
  91. my $buffer;
  92. while (read WGET, $buffer, 1048576) {
  93. print MD5SUM $buffer;
  94. print OUTPUT $buffer;
  95. }
  96. close MD5SUM;
  97. close WGET;
  98. close OUTPUT;
  99. if ($? >> 8) {
  100. print STDERR "Download failed.\n";
  101. cleanup();
  102. return;
  103. }
  104. }
  105. my $sum = `cat "$target/$filename.md5sum"`;
  106. $sum =~ /^(\w+)\s*/ or die "Could not generate md5sum\n";
  107. $sum = $1;
  108. if (($md5sum =~ /\w{32}/) and ($sum ne $md5sum)) {
  109. print STDERR "MD5 sum of the downloaded file does not match (file: $sum, requested: $md5sum) - deleting download.\n";
  110. cleanup();
  111. return;
  112. }
  113. unlink "$target/$filename";
  114. system("mv", "$target/$filename.dl", "$target/$filename");
  115. cleanup();
  116. }
  117. sub cleanup
  118. {
  119. unlink "$target/$filename.dl";
  120. unlink "$target/$filename.md5sum";
  121. }
  122. @mirrors = localmirrors();
  123. foreach my $mirror (@ARGV) {
  124. if ($mirror =~ /^\@SF\/(.+)$/) {
  125. # give sourceforge a few more tries, because it redirects to different mirrors
  126. for (1 .. 5) {
  127. push @mirrors, "http://downloads.sourceforge.net/$1";
  128. }
  129. } elsif ($mirror =~ /^\@GNU\/(.+)$/) {
  130. push @mirrors, "http://ftpmirror.gnu.org/$1";
  131. push @mirrors, "http://ftp.gnu.org/pub/gnu/$1";
  132. push @mirrors, "ftp://ftp.belnet.be/mirror/ftp.gnu.org/gnu/$1";
  133. push @mirrors, "ftp://ftp.mirror.nl/pub/mirror/gnu/$1";
  134. push @mirrors, "http://mirror.switch.ch/ftp/mirror/gnu/$1";
  135. } elsif ($mirror =~ /^\@SAVANNAH\/(.+)$/) {
  136. push @mirrors, "http://download.savannah.gnu.org/releases/$1";
  137. push @mirrors, "http://nongnu.uib.no/$1";
  138. push @mirrors, "http://ftp.igh.cnrs.fr/pub/nongnu/$1";
  139. push @mirrors, "http://download-mirror.savannah.gnu.org/releases/$1";
  140. } elsif ($mirror =~ /^\@GNOME\/(.+)$/) {
  141. push @mirrors, "http://ftp.gnome.org/pub/GNOME/sources/$1";
  142. push @mirrors, "http://ftp.unina.it/pub/linux/GNOME/sources/$1";
  143. push @mirrors, "http://fr2.rpmfind.net/linux/gnome.org/sources/$1";
  144. push @mirrors, "ftp://ftp.dit.upm.es/pub/GNOME/sources/$1";
  145. push @mirrors, "ftp://ftp.no.gnome.org/pub/GNOME/sources/$1";
  146. push @mirrors, "http://ftp.acc.umu.se/pub/GNOME/sources/$1";
  147. push @mirrors, "http://ftp.belnet.be/mirror/ftp.gnome.org/sources/$1";
  148. push @mirrors, "http://linorg.usp.br/gnome/sources/$1";
  149. push @mirrors, "http://mirror.aarnet.edu.au/pub/GNOME/sources/$1";
  150. push @mirrors, "http://mirrors.ibiblio.org/pub/mirrors/gnome/sources/$1";
  151. push @mirrors, "ftp://ftp.cse.buffalo.edu/pub/Gnome/sources/$1";
  152. push @mirrors, "ftp://ftp.nara.wide.ad.jp/pub/X11/GNOME/sources/$1";
  153. }
  154. else {
  155. push @mirrors, $mirror;
  156. }
  157. }
  158. push @mirrors, 'http://downloads.librecmc.org/sources/v1.3';
  159. while (!$ok) {
  160. my $mirror = shift @mirrors;
  161. $mirror or die "No more mirrors to try - giving up.\n";
  162. download($mirror);
  163. -f "$target/$filename" and $ok = 1;
  164. }
  165. $SIG{INT} = \&cleanup;