download.pl 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. #!/usr/bin/env perl
  2. #
  3. # Copyright (C) 2006 OpenWrt.org
  4. # Copyright (C) 2016 LEDE project
  5. #
  6. # This is free software, licensed under the GNU General Public License v2.
  7. # See /LICENSE for more information.
  8. #
  9. use strict;
  10. use warnings;
  11. use File::Basename;
  12. use File::Copy;
  13. use Text::ParseWords;
  14. @ARGV > 2 or die "Syntax: $0 <target dir> <filename> <hash> <url filename> [<mirror> ...]\n";
  15. my $url_filename;
  16. my $target = glob(shift @ARGV);
  17. my $filename = shift @ARGV;
  18. my $file_hash = shift @ARGV;
  19. $url_filename = shift @ARGV unless $ARGV[0] =~ /:\/\//;
  20. my $scriptdir = dirname($0);
  21. my @mirrors;
  22. my $ok;
  23. $url_filename or $url_filename = $filename;
  24. sub localmirrors {
  25. my @mlist;
  26. open LM, "$scriptdir/localmirrors" and do {
  27. while (<LM>) {
  28. chomp $_;
  29. push @mlist, $_ if $_;
  30. }
  31. close LM;
  32. };
  33. open CONFIG, "<".$ENV{'TOPDIR'}."/.config" and do {
  34. while (<CONFIG>) {
  35. /^CONFIG_LOCALMIRROR="(.+)"/ and do {
  36. chomp;
  37. my @local_mirrors = split(/;/, $1);
  38. push @mlist, @local_mirrors;
  39. };
  40. }
  41. close CONFIG;
  42. };
  43. my $mirror = $ENV{'DOWNLOAD_MIRROR'};
  44. $mirror and push @mlist, split(/;/, $mirror);
  45. return @mlist;
  46. }
  47. sub which($) {
  48. my $prog = shift;
  49. my $res = `which $prog`;
  50. $res or return undef;
  51. $res =~ /^no / and return undef;
  52. $res =~ /not found/ and return undef;
  53. return $res;
  54. }
  55. sub hash_cmd() {
  56. my $len = length($file_hash);
  57. my $cmd;
  58. $len == 64 and return "mkhash sha256";
  59. $len == 32 and return "mkhash md5";
  60. return undef;
  61. }
  62. sub download_cmd($) {
  63. my $url = shift;
  64. my $have_curl = 0;
  65. if (open CURL, '-|', 'curl', '--version') {
  66. if (defined(my $line = readline CURL)) {
  67. $have_curl = 1 if $line =~ /^curl /;
  68. }
  69. close CURL;
  70. }
  71. return $have_curl
  72. ? (qw(curl -f --connect-timeout 20 --retry 5 --location --insecure), shellwords($ENV{CURL_OPTIONS} || ''), $url)
  73. : (qw(wget --tries=5 --timeout=20 --no-check-certificate --output-document=-), shellwords($ENV{WGET_OPTIONS} || ''), $url)
  74. ;
  75. }
  76. my $hash_cmd = hash_cmd();
  77. $hash_cmd or ($file_hash eq "skip") or die "Cannot find appropriate hash command, ensure the provided hash is either a MD5 or SHA256 checksum.\n";
  78. sub download
  79. {
  80. my $mirror = shift;
  81. $mirror =~ s!/$!!;
  82. if ($mirror =~ s!^file://!!) {
  83. if (! -d "$mirror") {
  84. print STDERR "Wrong local cache directory -$mirror-.\n";
  85. cleanup();
  86. return;
  87. }
  88. if (! -d "$target") {
  89. system("mkdir", "-p", "$target/");
  90. }
  91. if (! open TMPDLS, "find $mirror -follow -name $filename 2>/dev/null |") {
  92. print("Failed to search for $filename in $mirror\n");
  93. return;
  94. }
  95. my $link;
  96. while (defined(my $line = readline TMPDLS)) {
  97. chomp ($link = $line);
  98. if ($. > 1) {
  99. print("$. or more instances of $filename in $mirror found . Only one instance allowed.\n");
  100. return;
  101. }
  102. }
  103. close TMPDLS;
  104. if (! $link) {
  105. print("No instances of $filename found in $mirror.\n");
  106. return;
  107. }
  108. print("Copying $filename from $link\n");
  109. copy($link, "$target/$filename.dl");
  110. $hash_cmd and do {
  111. if (system("cat '$target/$filename.dl' | $hash_cmd > '$target/$filename.hash'")) {
  112. print("Failed to generate hash for $filename\n");
  113. return;
  114. }
  115. };
  116. } else {
  117. my @cmd = download_cmd("$mirror/$url_filename");
  118. print STDERR "+ ".join(" ",@cmd)."\n";
  119. open(FETCH_FD, '-|', @cmd) or die "Cannot launch curl or wget.\n";
  120. $hash_cmd and do {
  121. open MD5SUM, "| $hash_cmd > '$target/$filename.hash'" or die "Cannot launch $hash_cmd.\n";
  122. };
  123. open OUTPUT, "> $target/$filename.dl" or die "Cannot create file $target/$filename.dl: $!\n";
  124. my $buffer;
  125. while (read FETCH_FD, $buffer, 1048576) {
  126. $hash_cmd and print MD5SUM $buffer;
  127. print OUTPUT $buffer;
  128. }
  129. $hash_cmd and close MD5SUM;
  130. close FETCH_FD;
  131. close OUTPUT;
  132. if ($? >> 8) {
  133. print STDERR "Download failed.\n";
  134. cleanup();
  135. return;
  136. }
  137. }
  138. $hash_cmd and do {
  139. my $sum = `cat "$target/$filename.hash"`;
  140. $sum =~ /^(\w+)\s*/ or die "Could not generate file hash\n";
  141. $sum = $1;
  142. if ($sum ne $file_hash) {
  143. print STDERR "Hash of the downloaded file does not match (file: $sum, requested: $file_hash) - deleting download.\n";
  144. cleanup();
  145. return;
  146. }
  147. };
  148. unlink "$target/$filename";
  149. system("mv", "$target/$filename.dl", "$target/$filename");
  150. cleanup();
  151. }
  152. sub cleanup
  153. {
  154. unlink "$target/$filename.dl";
  155. unlink "$target/$filename.hash";
  156. }
  157. @mirrors = localmirrors();
  158. foreach my $mirror (@ARGV) {
  159. if ($mirror =~ /^\@SF\/(.+)$/) {
  160. # give sourceforge a few more tries, because it redirects to different mirrors
  161. for (1 .. 5) {
  162. push @mirrors, "https://downloads.sourceforge.net/$1";
  163. }
  164. } elsif ($mirror =~ /^\@APACHE\/(.+)$/) {
  165. push @mirrors, "https://mirror.netcologne.de/apache.org/$1";
  166. push @mirrors, "https://mirror.aarnet.edu.au/pub/apache/$1";
  167. push @mirrors, "https://mirror.csclub.uwaterloo.ca/apache/$1";
  168. } elsif ($mirror =~ /^\@GITHUB\/(.+)$/) {
  169. # give github a few more tries (different mirrors)
  170. for (1 .. 5) {
  171. push @mirrors, "https://raw.githubusercontent.com/$1";
  172. }
  173. } elsif ($mirror =~ /^\@GNU\/(.+)$/) {
  174. push @mirrors, "https://mirror.csclub.uwaterloo.ca/gnu/$1";
  175. push @mirrors, "https://mirror.netcologne.de/gnu/$1";
  176. } elsif ($mirror =~ /^\@SAVANNAH\/(.+)$/) {
  177. push @mirrors, "https://mirror.netcologne.de/savannah/$1";
  178. push @mirrors, "https://mirror.csclub.uwaterloo.ca/nongnu/$1";
  179. } elsif ($mirror =~ /^\@KERNEL\/(.+)$/) {
  180. my @extra = ( $1 );
  181. if ($filename =~ /linux-\d+\.\d+(?:\.\d+)?-rc/) {
  182. push @extra, "$extra[0]/testing";
  183. } elsif ($filename =~ /linux-(\d+\.\d+(?:\.\d+)?)/) {
  184. push @extra, "$extra[0]/longterm/v$1";
  185. }
  186. foreach my $dir (@extra) {
  187. push @mirrors, "https://cdn.kernel.org/pub/$dir";
  188. push @mirrors, "https://mirror.rackspace.com/kernel.org/$dir";
  189. }
  190. } elsif ($mirror =~ /^\@KERNEL_LIBRE\/(.+)$/) {
  191. my @extra = ( $1 );
  192. if ($filename =~ /linux-libre-\d+\.\d+(?:\.\d+)?-rc-gnu/) {
  193. push @extra, "$extra[0]/testing";
  194. } elsif ($filename =~ /linux-libre-(\d+\.\d+(?:\.\d+)?)-gnu/) {
  195. push @extra, "$extra[0]/v$1";
  196. }
  197. foreach my $dir (@extra) {
  198. push @mirrors, "https://linux-libre.fsfla.org/pub/linux-libre/releases/$dir";
  199. }
  200. } elsif ($mirror =~ /^\@GNOME\/(.+)$/) {
  201. push @mirrors, "https://mirror.csclub.uwaterloo.ca/gnome/sources/$1";
  202. }
  203. else {
  204. push @mirrors, $mirror;
  205. }
  206. }
  207. push @mirrors, 'https://librecmc.org/librecmc/downloads/sources/v1.5';
  208. while (!-f "$target/$filename") {
  209. my $mirror = shift @mirrors;
  210. $mirror or die "No more mirrors to try - giving up.\n";
  211. download($mirror);
  212. }
  213. $SIG{INT} = \&cleanup;