]>
git.ipfire.org Git - people/pmueller/ipfire-2.x.git/blob - config/updxlrator/download
3 # This code is distributed under the terms of the GPL
5 # (c) 2006-2008 marco.s - http://update-accelerator.advproxy.net
7 # Portions (c) 2008 by dotzball - http://www.blockouttraffic.de
9 # $Id: download,v 2.1 2008/07/23 00:00:00 marco.s Exp $
15 require '/var/ipfire/updatexlrator/updxlrator-lib.pl';
17 my $logfile="/var/log/updatexlrator/download.log";
19 my $repository='/var/updatecache';
23 my %xlratorsettings=();
33 my $wgetContinueFlag="";
35 my $vendorid = $ARGV[0]; if (!defined($vendorid) || $vendorid eq '') { exit; }
36 my $sourceurl = $ARGV[1]; if (!defined($sourceurl) || $sourceurl eq '') { exit; }
37 my $cfmirror = $ARGV[2]; if (!defined($cfmirror) || $cfmirror eq '') { exit; }
38 my $restartdl = defined($ARGV[3]) ?
$ARGV[3] : 0;
42 $sourceurl =~ s@\
%2b@
+@ig;
43 $sourceurl =~ s@\
%2f@
/@ig;
44 $sourceurl =~ s@\
%7e@
~@ig;
45 $updatefile = substr($sourceurl,rindex($sourceurl,"/")+1);
46 $updatefile =~ s@\
%20@
@ig;
47 $vendorid =~ tr/A-Z/a-z/;
49 unless (-d
"$repository/download/$vendorid")
51 system("mkdir -p $repository/download/$vendorid");
52 chmod 0775, "$repository/download/$vendorid";
57 # this is a new download
58 exit if (-e
"$repository/download/$vendorid/$updatefile");
60 # dotzball: Why is this necessary?
61 system("touch $repository/download/$vendorid/$updatefile");
62 $wgetContinueFlag = "-nc";
67 # this is a restart of a previous (unfinished) download
68 # -> continue download
69 $wgetContinueFlag = "-c";
70 &writelog
("Continue download: $updatefile");
76 $uuid = `echo $updatefile | md5sum`;
78 $uuid = `echo $sourceurl | md5sum`;
81 $uuid =~ s/[^0-9a-f]//g;
82 $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/;
84 if (-e
"$UPDXLT::swroot/updatexlrator/settings")
86 &UPDXLT
::readhash
("$UPDXLT::swroot/updatexlrator/settings", \
%xlratorsettings);
87 if ($xlratorsettings{'MAX_DOWNLOAD_RATE'} ne '') { $dlrate = "--limit-rate=" . int($xlratorsettings{'MAX_DOWNLOAD_RATE'} / 8) . "k" };
90 if (-e
"$UPDXLT::swroot/proxy/settings") { &UPDXLT
::readhash
("$UPDXLT::swroot/proxy/settings", \
%proxysettings); }
92 if (-e
"$UPDXLT::swroot/proxy/advanced/settings")
95 &UPDXLT
::readhash
("$UPDXLT::swroot/proxy/advanced/settings", \
%proxysettings);
98 if (($proxysettings{'UPSTREAM_PROXY'}) && ($proxysettings{'UPSTREAM_USER'}))
100 $login = "--proxy-user=\"$proxysettings{'UPSTREAM_USER'}\"";
101 if ($proxysettings{'UPSTREAM_PASSWORD'})
103 $login .= " --proxy-password=\"$proxysettings{'UPSTREAM_PASSWORD'}\"";
107 if ($xlratorsettings{'MAX_DOWNLOAD_RATE'} eq '')
109 &writelog
("Retrieving file for local cache: $updatefile");
111 &writelog
("Retrieving file for local cache at max. " . $xlratorsettings{'MAX_DOWNLOAD_RATE'} . " kBit/s: $updatefile");
114 $ENV{'http_proxy'} = $proxysettings{'UPSTREAM_PROXY'};
115 @http_header = `$UPDXLT::wget $login --user-agent="$UPDXLT::useragent" --spider -S $sourceurl 2>&1`;
116 $ENV{'http_proxy'} = '';
118 foreach (@http_header)
121 if (/^\s*Content-Length:\s/) { s/[^0-9]//g; $remote_size=$_; &writelog
("Remote file size: $_ bytes"); }
122 if (/^\s*Last-Modified:\s/)
124 s/^\s*Last-Modified:\s//;
125 $remote_mtime = HTTP
::Date
::str2time
($_);
126 &writelog
("Remote file date: $_");
130 $ENV{'http_proxy'} = $proxysettings{'UPSTREAM_PROXY'};
134 # this is a new download
135 # -> download from scratch
136 unlink "$repository/download/$vendorid/$updatefile";
137 unlink "$repository/download/$vendorid/$updatefile.info";
140 # save file informations while downloading
141 $dlinfo{'VENDORID'} = $vendorid;
142 $dlinfo{'SRCURL'} = $sourceurl;
143 $dlinfo{'FILENAME'} = $updatefile;
144 $dlinfo{'CFMIRROR'} = $cfmirror;
145 $dlinfo{'REMOTETIME'} = $remote_mtime;
146 $dlinfo{'REMOTESIZE'} = $remote_size;
147 $dlinfo{'STATUS'} = "1";
148 &UPDXLT
::writehash
("$repository/download/$vendorid/$updatefile.info", \
%dlinfo);
150 my $cmd = "$UPDXLT::wget $login $dlrate --user-agent=\"$UPDXLT::useragent\" -q -P $repository/download/$vendorid $wgetContinueFlag $sourceurl";
153 $ENV{'http_proxy'} = '';
157 &writelog
("Download finished with result code: OK");
159 unless (-d
"$repository/$vendorid")
161 system("mkdir -p $repository/$vendorid");
162 chmod 0775, "$repository/$vendorid";
165 unless (-d
"$repository/$vendorid/$uuid")
167 system("mkdir -p $repository/$vendorid/$uuid");
168 chmod 0775, "$repository/$vendorid/$uuid";
171 &writelog
("Moving file to the cache directory: $vendorid/$uuid");
172 $updatefile =~ s@ @
\\ @ig;
173 system("mv $repository/download/$vendorid/$updatefile $repository/$vendorid/$uuid");
174 # Workaround for IPCop's mv bug:
175 utime time,$remote_mtime,"$repository/$vendorid/$uuid/$updatefile";
176 $updatefile =~ s@
\\ @
@ig;
178 &UPDXLT
::setcachestatus
("$repository/$vendorid/$uuid/source.url",$sourceurl);
179 &UPDXLT
::setcachestatus
("$repository/$vendorid/$uuid/status",$UPDXLT::sfOk
);
180 &UPDXLT
::setcachestatus
("$repository/$vendorid/$uuid/checkup.log",time);
181 &UPDXLT
::setcachestatus
("$repository/$vendorid/$uuid/access.log",time);
183 # Update permissions of all files in the download directory.
185 "$repository/$vendorid/$uuid/source.url",
186 "$repository/$vendorid/$uuid/status",
187 "$repository/$vendorid/$uuid/checkup.log",
188 "$repository/$vendorid/$uuid/access.log",
189 "$repository/$vendorid/$uuid/$updatefile"
193 unlink ("$repository/download/$vendorid/$updatefile.info");
196 &writelog
("Download finished with result code: ERROR");
197 if (-e
"$repository/download/$vendorid/$updatefile") { unlink ("$repository/download/$vendorid/$updatefile"); }
201 # -------------------------------------------------------------------
207 open (LOGFILE
,">>$logfile");
208 my @now = localtime(time);
209 printf LOGFILE
"%04d-%02d-%02d %02d:%02d:%02d [%d] %s\n",$now[5]+1900,$now[4]+1,$now[3],$now[2],$now[1],$now[0],$$,$_[0];
214 # -------------------------------------------------------------------