download.pl 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  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 File::Path;
  14. use Text::ParseWords;
  15. use JSON::PP;
  16. @ARGV > 2 or die "Syntax: $0 <target dir> <filename> <hash> <url filename> [<mirror> ...]\n";
  17. my $url_filename;
  18. my $target = glob(shift @ARGV);
  19. my $filename = shift @ARGV;
  20. my $file_hash = shift @ARGV;
  21. $url_filename = shift @ARGV unless $ARGV[0] =~ /:\/\//;
  22. my $scriptdir = dirname($0);
  23. my @mirrors;
  24. my $ok;
  25. my $check_certificate = $ENV{DOWNLOAD_CHECK_CERTIFICATE} eq "y";
  26. my $custom_tool = $ENV{DOWNLOAD_TOOL_CUSTOM};
  27. my $download_tool;
  28. $url_filename or $url_filename = $filename;
  29. sub localmirrors {
  30. my @mlist;
  31. open LM, "$scriptdir/localmirrors" and do {
  32. while (<LM>) {
  33. chomp $_;
  34. push @mlist, $_ if $_;
  35. }
  36. close LM;
  37. };
  38. open CONFIG, "<".$ENV{'TOPDIR'}."/.config" and do {
  39. while (<CONFIG>) {
  40. /^CONFIG_LOCALMIRROR="(.+)"/ and do {
  41. chomp;
  42. my @local_mirrors = split(/;/, $1);
  43. push @mlist, @local_mirrors;
  44. };
  45. }
  46. close CONFIG;
  47. };
  48. my $mirror = $ENV{'DOWNLOAD_MIRROR'};
  49. $mirror and push @mlist, split(/;/, $mirror);
  50. return @mlist;
  51. }
  52. sub projectsmirrors {
  53. my $project = shift;
  54. my $append = shift;
  55. open (PM, "$scriptdir/projectsmirrors.json") ||
  56. die "Can´t open $scriptdir/projectsmirrors.json: $!\n";
  57. local $/;
  58. my $mirror_json = <PM>;
  59. my $mirror = decode_json $mirror_json;
  60. foreach (@{$mirror->{$project}}) {
  61. push @mirrors, $_ . "/" . ($append or "");
  62. }
  63. }
  64. sub which($) {
  65. my $prog = shift;
  66. my $res = `command -v $prog`;
  67. $res or return undef;
  68. return $res;
  69. }
  70. sub hash_cmd() {
  71. my $len = length($file_hash);
  72. my $cmd;
  73. $len == 64 and return "$ENV{'MKHASH'} sha256";
  74. $len == 32 and return "$ENV{'MKHASH'} md5";
  75. return undef;
  76. }
  77. sub tool_present {
  78. my $tool_name = shift;
  79. my $compare_line = shift;
  80. my $present = 0;
  81. if (open TOOL, "$tool_name --version 2>/dev/null |") {
  82. if (defined(my $line = readline TOOL)) {
  83. $present = 1 if $line =~ /^$compare_line /;
  84. }
  85. close TOOL;
  86. }
  87. return $present
  88. }
  89. sub select_tool {
  90. $custom_tool =~ tr/"//d;
  91. if ($custom_tool) {
  92. return $custom_tool;
  93. }
  94. # Try to use curl if available
  95. if (tool_present("curl", "curl")) {
  96. return "curl";
  97. }
  98. # No tool found, fallback to wget
  99. return "wget";
  100. }
  101. sub download_cmd {
  102. my $url = shift;
  103. my $filename = shift;
  104. if ($download_tool eq "curl") {
  105. return (qw(curl -f --connect-timeout 20 --retry 5 --location),
  106. $check_certificate ? () : '--insecure',
  107. shellwords($ENV{CURL_OPTIONS} || ''),
  108. $url);
  109. } elsif ($download_tool eq "wget") {
  110. return (qw(wget --tries=5 --timeout=20 --output-document=-),
  111. $check_certificate ? () : '--no-check-certificate',
  112. shellwords($ENV{WGET_OPTIONS} || ''),
  113. $url);
  114. } elsif ($download_tool eq "aria2c") {
  115. my $additional_mirrors = join(" ", map "$_/$filename", @_);
  116. my @chArray = ('a'..'z', 'A'..'Z', 0..9);
  117. my $rfn = join '', "${filename}_", map{ $chArray[int rand @chArray] } 0..9;
  118. @mirrors=();
  119. return join(" ", "[ -d $ENV{'TMPDIR'}/aria2c ] || mkdir $ENV{'TMPDIR'}/aria2c;",
  120. "touch $ENV{'TMPDIR'}/aria2c/${rfn}_spp;",
  121. qw(aria2c --stderr -c -x2 -s10 -j10 -k1M), $url, $additional_mirrors,
  122. $check_certificate ? () : '--check-certificate=false',
  123. "--server-stat-of=$ENV{'TMPDIR'}/aria2c/${rfn}_spp",
  124. "--server-stat-if=$ENV{'TMPDIR'}/aria2c/${rfn}_spp",
  125. "--daemon=false --no-conf", shellwords($ENV{ARIA2C_OPTIONS} || ''),
  126. "-d $ENV{'TMPDIR'}/aria2c -o $rfn;",
  127. "cat $ENV{'TMPDIR'}/aria2c/$rfn;",
  128. "rm $ENV{'TMPDIR'}/aria2c/$rfn $ENV{'TMPDIR'}/aria2c/${rfn}_spp");
  129. } else {
  130. return join(" ", $download_tool, $url);
  131. }
  132. }
  133. my $hash_cmd = hash_cmd();
  134. $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";
  135. sub download
  136. {
  137. my $mirror = shift;
  138. my $download_filename = shift;
  139. my @additional_mirrors = @_;
  140. $mirror =~ s!/$!!;
  141. if ($mirror =~ s!^file://!!) {
  142. if (! -d "$mirror") {
  143. print STDERR "Wrong local cache directory -$mirror-.\n";
  144. cleanup();
  145. return;
  146. }
  147. if (! -d "$target") {
  148. make_path($target);
  149. }
  150. if (! open TMPDLS, "find $mirror -follow -name $filename 2>/dev/null |") {
  151. print("Failed to search for $filename in $mirror\n");
  152. return;
  153. }
  154. my $link;
  155. while (defined(my $line = readline TMPDLS)) {
  156. chomp ($link = $line);
  157. if ($. > 1) {
  158. print("$. or more instances of $filename in $mirror found . Only one instance allowed.\n");
  159. return;
  160. }
  161. }
  162. close TMPDLS;
  163. if (! $link) {
  164. print("No instances of $filename found in $mirror.\n");
  165. return;
  166. }
  167. print("Copying $filename from $link\n");
  168. copy($link, "$target/$filename.dl");
  169. $hash_cmd and do {
  170. if (system("cat '$target/$filename.dl' | $hash_cmd > '$target/$filename.hash'")) {
  171. print("Failed to generate hash for $filename\n");
  172. return;
  173. }
  174. };
  175. } else {
  176. my @cmd = download_cmd("$mirror/$download_filename", $download_filename, @additional_mirrors);
  177. print STDERR "+ ".join(" ",@cmd)."\n";
  178. open(FETCH_FD, '-|', @cmd) or die "Cannot launch aria2c, curl or wget.\n";
  179. $hash_cmd and do {
  180. open MD5SUM, "| $hash_cmd > '$target/$filename.hash'" or die "Cannot launch $hash_cmd.\n";
  181. };
  182. open OUTPUT, "> $target/$filename.dl" or die "Cannot create file $target/$filename.dl: $!\n";
  183. my $buffer;
  184. while (read FETCH_FD, $buffer, 1048576) {
  185. $hash_cmd and print MD5SUM $buffer;
  186. print OUTPUT $buffer;
  187. }
  188. $hash_cmd and close MD5SUM;
  189. close FETCH_FD;
  190. close OUTPUT;
  191. if ($? >> 8) {
  192. print STDERR "Download failed.\n";
  193. cleanup();
  194. return;
  195. }
  196. }
  197. $hash_cmd and do {
  198. my $sum = `cat "$target/$filename.hash"`;
  199. $sum =~ /^(\w+)\s*/ or die "Could not generate file hash\n";
  200. $sum = $1;
  201. if ($sum ne $file_hash) {
  202. print STDERR "Hash of the downloaded file does not match (file: $sum, requested: $file_hash) - deleting download.\n";
  203. cleanup();
  204. return;
  205. }
  206. };
  207. unlink "$target/$filename";
  208. move("$target/$filename.dl", "$target/$filename");
  209. cleanup();
  210. }
  211. sub cleanup
  212. {
  213. unlink "$target/$filename.dl";
  214. unlink "$target/$filename.hash";
  215. }
  216. @mirrors = localmirrors();
  217. foreach my $mirror (@ARGV) {
  218. if ($mirror =~ /^\@SF\/(.+)$/) {
  219. # give sourceforge a few more tries, because it redirects to different mirrors
  220. for (1 .. 5) {
  221. projectsmirrors '@SF', $1;
  222. }
  223. } elsif ($mirror =~ /^\@OPENWRT$/) {
  224. # use OpenWrt source server directly
  225. } elsif ($mirror =~ /^\@DEBIAN\/(.+)$/) {
  226. projectsmirrors '@DEBIAN', $1;
  227. } elsif ($mirror =~ /^\@APACHE\/(.+)$/) {
  228. projectsmirrors '@APACHE', $1;
  229. } elsif ($mirror =~ /^\@GITHUB\/(.+)$/) {
  230. # give github a few more tries (different mirrors)
  231. for (1 .. 5) {
  232. projectsmirrors '@GITHUB', $1;
  233. }
  234. } elsif ($mirror =~ /^\@GNU\/(.+)$/) {
  235. projectsmirrors '@GNU', $1;
  236. } elsif ($mirror =~ /^\@SAVANNAH\/(.+)$/) {
  237. projectsmirrors '@SAVANNAH', $1;
  238. } elsif ($mirror =~ /^\@KERNEL\/(.+)$/) {
  239. my @extra = ( $1 );
  240. if ($filename =~ /linux-\d+\.\d+(?:\.\d+)?-rc/) {
  241. push @extra, "$extra[0]/testing";
  242. } elsif ($filename =~ /linux-(\d+\.\d+(?:\.\d+)?)/) {
  243. push @extra, "$extra[0]/longterm/v$1";
  244. }
  245. foreach my $dir (@extra) {
  246. projectsmirrors '@KERNEL', $dir;
  247. }
  248. } elsif ($mirror =~ /^\@GNOME\/(.+)$/) {
  249. projectsmirrors '@GNOME', $1;
  250. } else {
  251. push @mirrors, $mirror;
  252. }
  253. }
  254. projectsmirrors '@OPENWRT';
  255. if (-f "$target/$filename") {
  256. $hash_cmd and do {
  257. if (system("cat '$target/$filename' | $hash_cmd > '$target/$filename.hash'")) {
  258. die "Failed to generate hash for $filename\n";
  259. }
  260. my $sum = `cat "$target/$filename.hash"`;
  261. $sum =~ /^(\w+)\s*/ or die "Could not generate file hash\n";
  262. $sum = $1;
  263. cleanup();
  264. exit 0 if $sum eq $file_hash;
  265. die "Hash of the local file $filename does not match (file: $sum, requested: $file_hash) - deleting download.\n";
  266. unlink "$target/$filename";
  267. };
  268. }
  269. $download_tool = select_tool();
  270. while (!-f "$target/$filename") {
  271. my $mirror = shift @mirrors;
  272. $mirror or die "No more mirrors to try - giving up.\n";
  273. download($mirror, $url_filename, @mirrors);
  274. if (!-f "$target/$filename" && $url_filename ne $filename) {
  275. download($mirror, $filename, @mirrors);
  276. }
  277. }
  278. $SIG{INT} = \&cleanup;