]>
git.ipfire.org Git - people/teissler/ipfire-2.x.git/blob - config/updxlrator/checkdeaddl
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: checkdeaddl,v 1.1 2008/11/29 00:00:00 marco.s Exp $
14 use Fcntl
':flock'; # import LOCK_* constants
17 require '/var/ipfire/updatexlrator/updxlrator-lib.pl';
19 my $repository='/var/updatecache';
20 my $logfile="/var/log/updatexlrator/download.log";
24 my %xlratorsettings=();
30 my $lockfile = "/var/log/updatexlrator/checkdeaddl.lck";
32 &debuglog
("Check if there is a lock ");
33 open SEM
, ">$lockfile" or die "Can't write-open $lockfile: $!";
35 &debuglog
("No lock, proceed ");
37 &writelog
("Check for dead downloads ");
39 if (-e
"${UPDXLT::apphome}/settings")
41 &UPDXLT
::readhash
("${UPDXLT::apphome}/settings", \
%xlratorsettings);
42 if ($xlratorsettings{'ENABLE_LOG'} eq 'on') { $logging=1; };
43 if ($xlratorsettings{'PASSIVE_MODE'} eq 'on') { $passive_mode=1; };
44 if ($xlratorsettings{'LOW_DOWNLOAD_PRIORITY'} eq 'on') { $nice='/bin/nice --adjustment=15 '; };
45 $maxusage = $xlratorsettings{'MAX_DISK_USAGE'};
49 &writelog
("Updatexlrator not enabled, exit");
52 if (!$maxusage) { $maxusage=75; };
54 if($passive_mode || (&UPDXLT
::diskusage
($repository) > $maxusage))
57 &writelog
("Running in passive mode or maximum diskusage exceeded, exit");
62 if (-e
"${UPDXLT::swroot}/proxy/settings") { &UPDXLT
::readhash
("${UPDXLT::swroot}/proxy/settings", \
%proxysettings); }
64 if (-e
"${UPDXLT::swroot}/proxy/advanced/settings")
67 &UPDXLT
::readhash
("${UPDXLT::swroot}/proxy/advanced/settings", \
%proxysettings);
71 foreach my $dir (<$repository/download/*>)
75 foreach my $updatefile (<$dir/*>)
77 unless ($updatefile =~ /.info$/)
79 push(@downloads, $updatefile);
85 foreach my $updatefile (@downloads)
87 if(-e
"$updatefile.info")
89 &checkdeaddl
($updatefile);
98 # -------------------------------------------------------------------
102 my $updatefile = shift;
105 &UPDXLT
::readhash
("$updatefile.info", \
%dlinfo);
107 my @http_header = ();
108 my $http_result = '000 n/a';
111 my $remote_mtime = 0;
114 my $cmd_ps = "/bin/ps aux | /bin/grep -E \"(download|wget).*$dlinfo{'SRCURL'}\" | /bin/grep -v \"/bin/grep\"";
115 my @runningdl = `$cmd_ps`;
119 # download is still running, no need to restart download
123 if (($proxysettings{'UPSTREAM_PROXY'}) && ($proxysettings{'UPSTREAM_USER'}))
125 $login = "--proxy-user=\"$proxysettings{'UPSTREAM_USER'}\"";
126 if ($proxysettings{'UPSTREAM_PASSWORD'})
128 $login .= " --proxy-password=\"$proxysettings{'UPSTREAM_PASSWORD'}\"";
132 $ENV{'http_proxy'} = $proxysettings{'UPSTREAM_PROXY'};
133 @http_header = `${UPDXLT::wget} $login --user-agent="${UPDXLT::useragent}" --spider -S $dlinfo{'SRCURL'} 2>&1`;
134 $ENV{'http_proxy'} = '';
136 foreach (@http_header)
139 if (/^\s*HTTP\/\d
+\
.\d
+\s\d
+\s
+\w
+/) { $http_result = $_; $http_result =~ s/^\s
*HTTP\
/\d+\.\d+\s+//; }
140 if (/^\s*Content-Length:\s/) { $remote_size = $_; $remote_size =~ s/[^0-9]//g; }
141 if (/^\s*Last-Modified:\s/) { $remote_mtime = $_; $remote_mtime =~ s/^\s*Last-Modified:\s//; $remote_mtime = HTTP
::Date
::str2time
($remote_mtime) }
144 &writelog
($updatefile);
145 &writelog
("HTTP result: $http_result");
146 &writelog
("Source time: $remote_mtime");
147 &writelog
("Cached time: " . $dlinfo{'REMOTETIME'});
149 if ($http_result =~ /\d+\s+OK$/)
151 my $cmd = "$nice$UPDXLT::apphome/bin/download $dlinfo{'VENDORID'} $dlinfo{'SRCURL'} $dlinfo{'CFMIRROR'}";
153 if ($remote_size > &UPDXLT
::diskfree
($repository))
155 &writelog
("Can't download file, because remote filesize exceeds maximum diskusage");
157 elsif ($remote_mtime == $dlinfo{'REMOTETIME'})
159 # still the same file, continue download
160 &writelog
("Status: Ok, continue download");
162 &debuglog
("Running command $cmd");
167 # File is changed on remote site, download from scratch
168 &writelog
("Status: Outdated, restart download from scratch");
171 &debuglog
("Running command $cmd");
179 &writelog
("Status: No source");
180 $dlinfo{'STATUS'} = ${UPDXLT
::sfNoSource
};
182 &writelog
("Status: Error");
183 $dlinfo{'STATUS'} = ${UPDXLT
::sfUnknown
};
185 &UPDXLT
::writehash
("$updatefile.info", \
%dlinfo);
189 # -------------------------------------------------------------------
195 open (LOGFILE
,">>$logfile");
196 my @now = localtime(time);
197 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];
202 # -------------------------------------------------------------------
208 open(LOGFILE
,">>/var/log/updatexlrator/debug.log");
209 my @now = localtime(time);
210 printf LOGFILE
"%04d-%02d-%02d %02d:%02d:%02d [%d] [%s] %s\n",$now[5]+1900,$now[4]+1,$now[3],$now[2],$now[1],$now[0],$$,"updxlrator",$_[0];
215 # -------------------------------------------------------------------