1
0
mirror of git://projects.qi-hardware.com/openwrt-xburst.git synced 2024-11-23 22:05:20 +02:00

[scripts] download.pl:

- cleanup cache handling code
	- use alternative m// and s/// delimiters to get rid of tedious slash escaping
	- use multi-argument form of system() where possible to avoid quoting & escaping issues
	- avoid a temporary variable

git-svn-id: svn://svn.openwrt.org/openwrt/trunk@31677 3c298f89-4303-0410-b956-a3cf2f4a3e73
This commit is contained in:
jow 2012-05-11 18:17:15 +00:00
parent 88a3d8e65f
commit afc8169ea7

View File

@ -52,56 +52,59 @@ sub which($) {
return $res; return $res;
} }
my $md5cmd = which("md5sum"); my $md5cmd = which("md5sum") || which("md5") || die 'no md5 checksum program found, please install md5 or md5sum';
$md5cmd or $md5cmd = which("md5");
$md5cmd or die 'no md5 checksum program found, please install md5 or md5sum';
chomp $md5cmd; chomp $md5cmd;
sub download sub download
{ {
my $mirror = shift; my $mirror = shift;
my $options = $ENV{WGET_OPTIONS}; my $options = $ENV{WGET_OPTIONS} || "";
$options or $options = "";
$mirror =~ s/\/$//; $mirror =~ s!/$!!;
if( $mirror =~ /^file:\/\// ) {
my $cache = $mirror; if ($mirror =~ s!^file://!!) {
$cache =~ s/file:\/\///g; if (! -d "$mirror") {
if(system("test -d $cache")) { print STDERR "Wrong local cache directory -$mirror-.\n";
print STDERR "Wrong local cache directory -$cache-.\n";
cleanup(); cleanup();
return; return;
} }
if(! -d $target) {
system("mkdir -p $target/"); if (! -d "$target") {
system("mkdir", "-p", "$target/");
} }
if (open TMPDLS, "find $cache -follow -name $filename 2>/dev/null |"){
my $i = 0; if (! open TMPDLS, "find $mirror -follow -name $filename 2>/dev/null |") {
my $link = ""; print("Failed to search for $filename in $mirror\n");
while (defined($link = readline TMPDLS)) { return;
chomp $link; }
$i++;
if ($i > 1) { my $link;
print("$i or more instances of $filename in $cache found . Only one instance allowed.\n");
return; while (defined(my $line = readline TMPDLS)) {
} chomp ($link = $line);
} if ($. > 1) {
close TMPDLS; print("$. or more instances of $filename in $mirror found . Only one instance allowed.\n");
if ($i < 1) { return;
print("No instances of $filename found in $cache.\n"); }
return; }
} elsif ($i == 1){
print("Copying $filename from $link\n"); close TMPDLS;
copy($link, "$target/$filename.dl");
} if (! $link) {
} else { print("No instances of $filename found in $mirror.\n");
print("Failed to search for $filename in $cache\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");
return; return;
} }
system("$md5cmd $target/$filename.dl > \"$target/$filename.md5sum\" ") == 0 or return;
} else { } else {
open WGET, "wget -t5 --timeout=20 --no-check-certificate $options -O- \"$mirror/$filename\" |" or die "Cannot launch wget.\n"; 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"; open MD5SUM, "| $md5cmd > '$target/$filename.md5sum'" or die "Cannot launch md5sum.\n";
open OUTPUT, "> $target/$filename.dl" or die "Cannot create file $target/$filename.dl: $!\n"; open OUTPUT, "> $target/$filename.dl" or die "Cannot create file $target/$filename.dl: $!\n";
my $buffer; my $buffer;
while (read WGET, $buffer, 1048576) { while (read WGET, $buffer, 1048576) {
@ -112,7 +115,7 @@ sub download
close WGET; close WGET;
close OUTPUT; close OUTPUT;
if (($? >> 8) != 0 ) { if ($? >> 8) {
print STDERR "Download failed.\n"; print STDERR "Download failed.\n";
cleanup(); cleanup();
return; return;
@ -130,7 +133,7 @@ sub download
} }
unlink "$target/$filename"; unlink "$target/$filename";
system("mv \"$target/$filename.dl\" \"$target/$filename\""); system("mv", "$target/$filename.dl", "$target/$filename");
cleanup(); cleanup();
} }