download.pl 5.7 KB

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