#
# (c) 2006-2008 marco.s - http://update-accelerator.advproxy.net
#
-# $Id: download,v 2.0 2008/04/06 00:00:00 marco.s Exp $
+# Portions (c) 2008 by dotzball - http://www.blockouttraffic.de
+#
+# $Id: download,v 2.1 2008/07/23 00:00:00 marco.s Exp $
#
use strict;
use HTTP::Date;
-my $swroot='/var/ipfire';
-my $apphome="$swroot/updatexlrator";
+require '/var/ipfire/updatexlrator/updxlrator-lib.pl';
+
my $logfile="/var/log/updatexlrator/download.log";
my $logging=0;
my $repository='/var/updatecache';
my $login='';
my $dlrate='';
my $uuid='';
-my $useragent="Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)";
my %xlratorsettings=();
my %proxysettings=();
my @http_header=();
+my $remote_size=0;
my $remote_mtime=0;
my $updatefile='';
my $unique=0;
my $mirror=1;
-my $sfOk="1";
+my %dlinfo=();
+my $wgetContinueFlag="";
-my $vendorid = @ARGV[0]; if ($vendorid eq '') { exit; }
-my $sourceurl = @ARGV[1]; if ($sourceurl eq '') { exit; }
-my $cfmirror = @ARGV[2]; if ($cfmirror eq '') { exit; }
+my $vendorid = $ARGV[0]; if (!defined($vendorid) || $vendorid eq '') { exit; }
+my $sourceurl = $ARGV[1]; if (!defined($sourceurl) || $sourceurl eq '') { exit; }
+my $cfmirror = $ARGV[2]; if (!defined($cfmirror) || $cfmirror eq '') { exit; }
+my $restartdl = defined($ARGV[3]) ? $ARGV[3] : 0;
umask(0002);
+$sourceurl =~ s@\%2b@+@ig;
$sourceurl =~ s@\%2f@/@ig;
$sourceurl =~ s@\%7e@~@ig;
$updatefile = substr($sourceurl,rindex($sourceurl,"/")+1);
unless (-d "$repository/download/$vendorid")
{
system("mkdir -p $repository/download/$vendorid");
- system("chown -R nobody.squid $repository/download/$vendorid");
system("chmod 775 $repository/download/$vendorid");
}
-exit if (-e "$repository/download/$vendorid/$updatefile");
+if($restartdl == 0)
+{
+ # this is a new download
+ exit if (-e "$repository/download/$vendorid/$updatefile");
+
+ # dotzball: Why is this necessary?
+ system("touch $repository/download/$vendorid/$updatefile");
+ $wgetContinueFlag = "-nc";
+
+}
+else
+{
+ # this is a restart of a previous (unfinished) download
+ # -> continue download
+ $wgetContinueFlag = "-c";
+ &writelog("Continue download: $updatefile");
+}
-system("touch $repository/download/$vendorid/$updatefile");
if ($cfmirror)
{
$uuid =~ s/[^0-9a-f]//g;
$uuid =~ s/([a-f\d]{8})([a-f\d]{4})([a-f\d]{4})([a-f\d]{4})([a-f\d]{12})/$1-$2-$3-$4-$5/;
-if (-e "$swroot/updatexlrator/settings")
+if (-e "$UPDXLT::swroot/updatexlrator/settings")
{
- &readhash("$swroot/updatexlrator/settings", \%xlratorsettings);
- if ($xlratorsettings{'ENABLE_LOG'} eq 'on') { $logging=1; };
+ &UPDXLT::readhash("$UPDXLT::swroot/updatexlrator/settings", \%xlratorsettings);
if ($xlratorsettings{'MAX_DOWNLOAD_RATE'} ne '') { $dlrate = "--limit-rate=" . int($xlratorsettings{'MAX_DOWNLOAD_RATE'} / 8) . "k" };
}
-if (-e "$swroot/proxy/settings") { &readhash("$swroot/proxy/settings", \%proxysettings); }
+if (-e "$UPDXLT::swroot/proxy/settings") { &UPDXLT::readhash("$UPDXLT::swroot/proxy/settings", \%proxysettings); }
-if (-e "$swroot/proxy/advanced/settings")
+if (-e "$UPDXLT::swroot/proxy/advanced/settings")
{
%proxysettings=();
- &readhash("$swroot/proxy/advanced/settings", \%proxysettings);
+ &UPDXLT::readhash("$UPDXLT::swroot/proxy/advanced/settings", \%proxysettings);
}
if (($proxysettings{'UPSTREAM_PROXY'}) && ($proxysettings{'UPSTREAM_USER'}))
}
$ENV{'http_proxy'} = $proxysettings{'UPSTREAM_PROXY'};
-@http_header = `wget $login --user-agent="$useragent" --spider -S $sourceurl 2>&1`;
+@http_header = `$UPDXLT::wget $login --user-agent="$UPDXLT::useragent" --spider -S $sourceurl 2>&1`;
$ENV{'http_proxy'} = '';
foreach (@http_header)
{
chomp;
- if (/^\s*Content-Length:\s/) { s/[^0-9]//g; &writelog("Remote file size: $_ bytes"); }
- if (/^\s*Last-Modified:\s/) { s/^\s*Last-Modified:\s//; $remote_mtime = HTTP::Date::str2time($_); &writelog("Remote file date: $_"); }
+ if (/^\s*Content-Length:\s/) { s/[^0-9]//g; $remote_size=$_; &writelog("Remote file size: $_ bytes"); }
+ if (/^\s*Last-Modified:\s/)
+ {
+ s/^\s*Last-Modified:\s//;
+ $remote_mtime = HTTP::Date::str2time($_);
+ &writelog("Remote file date: $_");
+ }
}
$ENV{'http_proxy'} = $proxysettings{'UPSTREAM_PROXY'};
-unlink "$repository/download/$vendorid/$updatefile";
-$_ = system("wget $login $dlrate --user-agent=\"$useragent\" -q -nc -P $repository/download/$vendorid $sourceurl");
+
+unless($restartdl)
+{
+ # this is a new download
+ # -> download from scratch
+ unlink "$repository/download/$vendorid/$updatefile";
+ unlink "$repository/download/$vendorid/$updatefile.info";
+}
+
+# save file informations while downloading
+$dlinfo{'VENDORID'} = $vendorid;
+$dlinfo{'SRCURL'} = $sourceurl;
+$dlinfo{'FILENAME'} = $updatefile;
+$dlinfo{'CFMIRROR'} = $cfmirror;
+$dlinfo{'REMOTETIME'} = $remote_mtime;
+$dlinfo{'REMOTESIZE'} = $remote_size;
+$dlinfo{'STATUS'} = "1";
+&UPDXLT::writehash("$repository/download/$vendorid/$updatefile.info", \%dlinfo);
+
+my $cmd = "$UPDXLT::wget $login $dlrate --user-agent=\"$UPDXLT::useragent\" -q -P $repository/download/$vendorid $wgetContinueFlag $sourceurl";
+
+$_ = system("$cmd");
$ENV{'http_proxy'} = '';
if ($_ == 0)
unless (-d "$repository/$vendorid")
{
system("mkdir -p $repository/$vendorid");
- system("chown -R nobody.squid $repository/$vendorid");
system("chmod 775 $repository/$vendorid");
}
unless (-d "$repository/$vendorid/$uuid")
{
system("mkdir -p $repository/$vendorid/$uuid");
- system("chown -R nobody.squid $repository/$vendorid/$uuid");
system("chmod 775 $repository/$vendorid/$uuid");
}
utime time,$remote_mtime,"$repository/$vendorid/$uuid/$updatefile";
$updatefile =~ s@\\ @ @ig;
- &setcachestatus("$repository/$vendorid/$uuid/source.url",$sourceurl);
- &setcachestatus("$repository/$vendorid/$uuid/status",$sfOk);
- &setcachestatus("$repository/$vendorid/$uuid/checkup.log",time);
- &setcachestatus("$repository/$vendorid/$uuid/access.log",time);
+ &UPDXLT::setcachestatus("$repository/$vendorid/$uuid/source.url",$sourceurl);
+ &UPDXLT::setcachestatus("$repository/$vendorid/$uuid/status",$UPDXLT::sfOk);
+ &UPDXLT::setcachestatus("$repository/$vendorid/$uuid/checkup.log",time);
+ &UPDXLT::setcachestatus("$repository/$vendorid/$uuid/access.log",time);
- system("chown -R nobody.squid $repository/$vendorid/$uuid/*");
+ system("/usr/local/bin/updxsetperms");
system("chmod 775 $repository/$vendorid/$uuid/*");
+ unlink ("$repository/download/$vendorid/$updatefile.info");
+
} else {
&writelog("Download finished with result code: ERROR");
if (-e "$repository/download/$vendorid/$updatefile") { unlink ("$repository/download/$vendorid/$updatefile"); }
}
-# -------------------------------------------------------------------
-
-sub readhash
-{
- my $filename = $_[0];
- my $hash = $_[1];
- my ($var, $val);
-
- if (-e $filename)
- {
- open(FILE, $filename) or die "Unable to read file $filename";
- while (<FILE>)
- {
- chop;
- ($var, $val) = split /=/, $_, 2;
- if ($var)
- {
- $val =~ s/^\'//g;
- $val =~ s/\'$//g;
-
- # Untaint variables read from hash
- $var =~ /([A-Za-z0-9_-]*)/; $var = $1;
- $val =~ /([\w\W]*)/; $val = $1;
- $hash->{$var} = $val;
- }
- }
- close FILE;
- }
-}
-
# -------------------------------------------------------------------
sub writelog
}
# -------------------------------------------------------------------
-
-sub setcachestatus
-{
- open (FILE,">$_[0]");
- print FILE "$_[1]\n";
- close FILE;
-}
-
-# -------------------------------------------------------------------