download.pl 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. unshift @mirrors, 'http://downloads.librecmc.com/sources';
  124. while (!$ok) {
  125. my $mirror = shift @mirrors;
  126. $mirror or die "No more mirrors to try - giving up.\n";
  127. download($mirror);
  128. -f "$target/$filename" and $ok = 1;
  129. }
  130. $SIG{INT} = \&cleanup;