2008-09-24 02:29:44 +08:00
|
|
|
#!/usr/bin/env perl
|
2006-06-27 08:35:46 +08:00
|
|
|
#
|
|
|
|
# Copyright (C) 2006 OpenWrt.org
|
|
|
|
#
|
|
|
|
# This is free software, licensed under the GNU General Public License v2.
|
|
|
|
# See /LICENSE for more information.
|
|
|
|
#
|
|
|
|
|
2005-03-20 06:51:51 +08:00
|
|
|
use strict;
|
|
|
|
use warnings;
|
2007-01-25 06:38:02 +08:00
|
|
|
use File::Basename;
|
2012-04-10 22:11:45 +08:00
|
|
|
use File::Copy;
|
2005-03-20 06:51:51 +08:00
|
|
|
|
2007-09-29 08:05:48 +08:00
|
|
|
@ARGV > 2 or die "Syntax: $0 <target dir> <filename> <md5sum> [<mirror> ...]\n";
|
|
|
|
|
2005-03-20 06:51:51 +08:00
|
|
|
my $target = shift @ARGV;
|
|
|
|
my $filename = shift @ARGV;
|
|
|
|
my $md5sum = shift @ARGV;
|
2007-01-25 06:38:02 +08:00
|
|
|
my $scriptdir = dirname($0);
|
2005-05-01 03:47:17 +08:00
|
|
|
my @mirrors;
|
2005-03-20 06:51:51 +08:00
|
|
|
my $ok;
|
|
|
|
|
2007-01-25 06:38:02 +08:00
|
|
|
sub localmirrors {
|
2008-03-19 05:07:22 +08:00
|
|
|
my @mlist;
|
|
|
|
open LM, "$scriptdir/localmirrors" and do {
|
2007-04-07 07:15:39 +08:00
|
|
|
while (<LM>) {
|
|
|
|
chomp $_;
|
2011-07-04 03:33:24 +08:00
|
|
|
push @mlist, $_ if $_;
|
2007-04-07 07:15:39 +08:00
|
|
|
}
|
|
|
|
close LM;
|
|
|
|
};
|
|
|
|
open CONFIG, "<".$ENV{'TOPDIR'}."/.config" and do {
|
|
|
|
while (<CONFIG>) {
|
|
|
|
/^CONFIG_LOCALMIRROR="(.+)"/ and do {
|
|
|
|
chomp;
|
2009-02-20 18:16:47 +08:00
|
|
|
my @local_mirrors = split(/;/, $1);
|
|
|
|
push @mlist, @local_mirrors;
|
2007-04-07 07:15:39 +08:00
|
|
|
};
|
|
|
|
}
|
|
|
|
close CONFIG;
|
|
|
|
};
|
2007-01-25 06:38:02 +08:00
|
|
|
|
2014-12-12 20:35:23 +08:00
|
|
|
my $mirror = $ENV{'DOWNLOAD_MIRROR'};
|
|
|
|
$mirror and push @mlist, split(/;/, $mirror);
|
|
|
|
|
2008-03-19 05:07:22 +08:00
|
|
|
return @mlist;
|
2007-01-25 06:38:02 +08:00
|
|
|
}
|
|
|
|
|
2006-10-14 08:07:19 +08:00
|
|
|
sub which($) {
|
|
|
|
my $prog = shift;
|
|
|
|
my $res = `which $prog`;
|
|
|
|
$res or return undef;
|
|
|
|
$res =~ /^no / and return undef;
|
|
|
|
$res =~ /not found/ and return undef;
|
|
|
|
return $res;
|
|
|
|
}
|
|
|
|
|
2012-05-12 02:17:15 +08:00
|
|
|
my $md5cmd = which("md5sum") || which("md5") || die 'no md5 checksum program found, please install md5 or md5sum';
|
2006-10-11 00:17:16 +08:00
|
|
|
chomp $md5cmd;
|
2006-10-11 00:01:49 +08:00
|
|
|
|
2005-03-20 06:51:51 +08:00
|
|
|
sub download
|
|
|
|
{
|
|
|
|
my $mirror = shift;
|
2012-05-12 02:17:15 +08:00
|
|
|
my $options = $ENV{WGET_OPTIONS} || "";
|
|
|
|
|
|
|
|
$mirror =~ s!/$!!;
|
|
|
|
|
|
|
|
if ($mirror =~ s!^file://!!) {
|
|
|
|
if (! -d "$mirror") {
|
|
|
|
print STDERR "Wrong local cache directory -$mirror-.\n";
|
2009-02-20 18:16:47 +08:00
|
|
|
cleanup();
|
|
|
|
return;
|
|
|
|
}
|
2012-05-12 02:17:15 +08:00
|
|
|
|
|
|
|
if (! -d "$target") {
|
|
|
|
system("mkdir", "-p", "$target/");
|
2009-02-20 18:16:47 +08:00
|
|
|
}
|
2012-05-12 02:17:15 +08:00
|
|
|
|
|
|
|
if (! open TMPDLS, "find $mirror -follow -name $filename 2>/dev/null |") {
|
|
|
|
print("Failed to search for $filename in $mirror\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
my $link;
|
|
|
|
|
|
|
|
while (defined(my $line = readline TMPDLS)) {
|
|
|
|
chomp ($link = $line);
|
|
|
|
if ($. > 1) {
|
|
|
|
print("$. or more instances of $filename in $mirror found . Only one instance allowed.\n");
|
2012-04-10 22:11:45 +08:00
|
|
|
return;
|
|
|
|
}
|
2012-05-12 02:17:15 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
close TMPDLS;
|
|
|
|
|
|
|
|
if (! $link) {
|
|
|
|
print("No instances of $filename found in $mirror.\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
print("Copying $filename from $link\n");
|
|
|
|
copy($link, "$target/$filename.dl");
|
|
|
|
|
|
|
|
if (system("$md5cmd '$target/$filename.dl' > '$target/$filename.md5sum'")) {
|
|
|
|
print("Failed to generate md5 sum for $filename\n");
|
2012-04-10 22:11:45 +08:00
|
|
|
return;
|
|
|
|
}
|
2008-03-19 05:07:22 +08:00
|
|
|
} else {
|
2012-05-12 02:17:15 +08:00
|
|
|
open WGET, "wget -t5 --timeout=20 --no-check-certificate $options -O- '$mirror/$filename' |" or die "Cannot launch wget.\n";
|
|
|
|
open MD5SUM, "| $md5cmd > '$target/$filename.md5sum'" or die "Cannot launch md5sum.\n";
|
2008-03-19 05:07:22 +08:00
|
|
|
open OUTPUT, "> $target/$filename.dl" or die "Cannot create file $target/$filename.dl: $!\n";
|
|
|
|
my $buffer;
|
|
|
|
while (read WGET, $buffer, 1048576) {
|
|
|
|
print MD5SUM $buffer;
|
|
|
|
print OUTPUT $buffer;
|
|
|
|
}
|
|
|
|
close MD5SUM;
|
|
|
|
close WGET;
|
|
|
|
close OUTPUT;
|
|
|
|
|
2012-05-12 02:17:15 +08:00
|
|
|
if ($? >> 8) {
|
2008-03-19 05:07:22 +08:00
|
|
|
print STDERR "Download failed.\n";
|
|
|
|
cleanup();
|
|
|
|
return;
|
|
|
|
}
|
2005-03-20 06:51:51 +08:00
|
|
|
}
|
2008-03-19 05:07:22 +08:00
|
|
|
|
2005-03-20 06:51:51 +08:00
|
|
|
my $sum = `cat "$target/$filename.md5sum"`;
|
2006-10-11 00:08:25 +08:00
|
|
|
$sum =~ /^(\w+)\s*/ or die "Could not generate md5sum\n";
|
2005-03-20 06:51:51 +08:00
|
|
|
$sum = $1;
|
2008-03-19 05:07:22 +08:00
|
|
|
|
2005-03-20 06:51:51 +08:00
|
|
|
if (($md5sum =~ /\w{32}/) and ($sum ne $md5sum)) {
|
2007-10-20 23:30:16 +08:00
|
|
|
print STDERR "MD5 sum of the downloaded file does not match (file: $sum, requested: $md5sum) - deleting download.\n";
|
2005-03-20 06:51:51 +08:00
|
|
|
cleanup();
|
|
|
|
return;
|
|
|
|
}
|
2008-03-19 05:07:22 +08:00
|
|
|
|
2005-03-20 06:51:51 +08:00
|
|
|
unlink "$target/$filename";
|
2012-05-12 02:17:15 +08:00
|
|
|
system("mv", "$target/$filename.dl", "$target/$filename");
|
2005-03-20 07:52:32 +08:00
|
|
|
cleanup();
|
2005-03-20 06:51:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
sub cleanup
|
|
|
|
{
|
|
|
|
unlink "$target/$filename.dl";
|
|
|
|
unlink "$target/$filename.md5sum";
|
|
|
|
}
|
|
|
|
|
2007-01-25 06:38:02 +08:00
|
|
|
@mirrors = localmirrors();
|
|
|
|
|
2005-05-01 03:47:17 +08:00
|
|
|
foreach my $mirror (@ARGV) {
|
2005-03-20 06:51:51 +08:00
|
|
|
if ($mirror =~ /^\@SF\/(.+)$/) {
|
2006-12-08 20:44:26 +08:00
|
|
|
# give sourceforge a few more tries, because it redirects to different mirrors
|
|
|
|
for (1 .. 5) {
|
2012-02-26 17:29:53 +08:00
|
|
|
push @mirrors, "http://downloads.sourceforge.net/$1";
|
2005-03-20 06:51:51 +08:00
|
|
|
}
|
2005-06-08 14:48:19 +08:00
|
|
|
} elsif ($mirror =~ /^\@GNU\/(.+)$/) {
|
2013-03-01 07:49:36 +08:00
|
|
|
push @mirrors, "http://ftpmirror.gnu.org/$1";
|
2013-12-14 00:43:07 +08:00
|
|
|
push @mirrors, "http://ftp.gnu.org/pub/gnu/$1";
|
2007-08-27 02:21:24 +08:00
|
|
|
push @mirrors, "ftp://ftp.belnet.be/mirror/ftp.gnu.org/gnu/$1";
|
|
|
|
push @mirrors, "ftp://ftp.mirror.nl/pub/mirror/gnu/$1";
|
|
|
|
push @mirrors, "http://mirror.switch.ch/ftp/mirror/gnu/$1";
|
2014-10-08 16:01:39 +08:00
|
|
|
} elsif ($mirror =~ /^\@SAVANNAH\/(.+)$/) {
|
|
|
|
push @mirrors, "http://download.savannah.gnu.org/releases/$1";
|
|
|
|
push @mirrors, "http://nongnu.uib.no/$1";
|
|
|
|
push @mirrors, "http://ftp.igh.cnrs.fr/pub/nongnu/$1";
|
|
|
|
push @mirrors, "http://download-mirror.savannah.gnu.org/releases/$1";
|
2007-08-27 02:21:24 +08:00
|
|
|
} elsif ($mirror =~ /^\@KERNEL\/(.+)$/) {
|
2011-02-19 23:41:00 +08:00
|
|
|
my @extra = ( $1 );
|
2011-07-03 23:00:24 +08:00
|
|
|
if ($filename =~ /linux-\d+\.\d+(?:\.\d+)?-rc/) {
|
2011-02-19 23:41:00 +08:00
|
|
|
push @extra, "$extra[0]/testing";
|
2011-07-03 23:00:24 +08:00
|
|
|
} elsif ($filename =~ /linux-(\d+\.\d+(?:\.\d+)?)/) {
|
2011-02-19 23:41:00 +08:00
|
|
|
push @extra, "$extra[0]/longterm/v$1";
|
|
|
|
}
|
|
|
|
foreach my $dir (@extra) {
|
|
|
|
push @mirrors, "ftp://ftp.all.kernel.org/pub/$dir";
|
|
|
|
push @mirrors, "http://ftp.all.kernel.org/pub/$dir";
|
|
|
|
}
|
2008-09-02 05:46:17 +08:00
|
|
|
} elsif ($mirror =~ /^\@GNOME\/(.+)$/) {
|
|
|
|
push @mirrors, "http://ftp.gnome.org/pub/GNOME/sources/$1";
|
|
|
|
push @mirrors, "http://ftp.unina.it/pub/linux/GNOME/sources/$1";
|
|
|
|
push @mirrors, "http://fr2.rpmfind.net/linux/gnome.org/sources/$1";
|
|
|
|
push @mirrors, "ftp://ftp.dit.upm.es/pub/GNOME/sources/$1";
|
|
|
|
push @mirrors, "ftp://ftp.no.gnome.org/pub/GNOME/sources/$1";
|
|
|
|
push @mirrors, "http://ftp.acc.umu.se/pub/GNOME/sources/$1";
|
|
|
|
push @mirrors, "http://ftp.belnet.be/mirror/ftp.gnome.org/sources/$1";
|
|
|
|
push @mirrors, "http://linorg.usp.br/gnome/sources/$1";
|
|
|
|
push @mirrors, "http://mirror.aarnet.edu.au/pub/GNOME/sources/$1";
|
|
|
|
push @mirrors, "http://mirrors.ibiblio.org/pub/mirrors/gnome/sources/$1";
|
|
|
|
push @mirrors, "ftp://ftp.cse.buffalo.edu/pub/Gnome/sources/$1";
|
|
|
|
push @mirrors, "ftp://ftp.nara.wide.ad.jp/pub/X11/GNOME/sources/$1";
|
|
|
|
}
|
|
|
|
else {
|
2005-05-01 03:47:17 +08:00
|
|
|
push @mirrors, $mirror;
|
2005-03-20 06:51:51 +08:00
|
|
|
}
|
2005-05-01 03:47:17 +08:00
|
|
|
}
|
|
|
|
|
2006-11-27 04:40:10 +08:00
|
|
|
#push @mirrors, 'http://mirror1.openwrt.org';
|
2006-09-06 08:26:15 +08:00
|
|
|
push @mirrors, 'http://mirror2.openwrt.org/sources';
|
2006-11-27 04:40:10 +08:00
|
|
|
push @mirrors, 'http://downloads.openwrt.org/sources';
|
2005-05-01 03:47:17 +08:00
|
|
|
|
|
|
|
while (!$ok) {
|
|
|
|
my $mirror = shift @mirrors;
|
|
|
|
$mirror or die "No more mirrors to try - giving up.\n";
|
2008-03-19 05:07:22 +08:00
|
|
|
|
2005-05-01 03:47:17 +08:00
|
|
|
download($mirror);
|
2005-03-20 06:51:51 +08:00
|
|
|
-f "$target/$filename" and $ok = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
$SIG{INT} = \&cleanup;
|
|
|
|
|