]>
Commit | Line | Data |
---|---|---|
4f4092e3 CS |
1 | #!/usr/bin/perl |
2 | # | |
3 | # This code is distributed under the terms of the GPL | |
4 | # | |
5 | # (c) 2006-2008 marco.s - http://update-accelerator.advproxy.net | |
6 | # | |
7 | # Portions (c) 2008 by dotzball - http://www.blockouttraffic.de | |
8 | # | |
9 | # $Id: checkdeaddl,v 1.1 2008/11/29 00:00:00 marco.s Exp $ | |
10 | # | |
11 | ||
12 | use strict; | |
13 | ||
14 | use Fcntl ':flock'; # import LOCK_* constants | |
15 | use HTTP::Date; | |
16 | ||
17 | require '/var/ipfire/updatexlrator/updxlrator-lib.pl'; | |
18 | ||
19 | my $repository='/var/updatecache'; | |
20 | my $logfile="/var/log/updatexlrator/download.log"; | |
21 | my $logging=0; | |
22 | my $debug=0; | |
23 | my %proxysettings=(); | |
24 | my %xlratorsettings=(); | |
25 | my $updatefile=''; | |
26 | my @downloads=(); | |
27 | my $passive_mode=0; | |
28 | my $maxusage=0; | |
29 | my $nice=''; | |
30 | my $lockfile = "/var/log/updatexlrator/checkdeaddl.lck"; | |
31 | ||
32 | &debuglog("Check if there is a lock "); | |
33 | open SEM, ">$lockfile" or die "Can't write-open $lockfile: $!"; | |
34 | flock SEM, LOCK_EX; | |
35 | &debuglog("No lock, proceed "); | |
36 | ||
37 | &writelog("Check for dead downloads "); | |
38 | ||
39 | if (-e "${UPDXLT::apphome}/settings") | |
40 | { | |
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'}; | |
46 | } | |
47 | else | |
48 | { | |
49 | &writelog("Updatexlrator not enabled, exit"); | |
50 | exit 0; | |
51 | } | |
52 | if (!$maxusage) { $maxusage=75; }; | |
53 | ||
54 | if($passive_mode || (&UPDXLT::diskusage($repository) > $maxusage)) | |
55 | { | |
56 | # nothing to do | |
57 | &writelog("Running in passive mode or maximum diskusage exceeded, exit"); | |
58 | exit 0; | |
59 | } | |
60 | ||
61 | ||
62 | if (-e "${UPDXLT::swroot}/proxy/settings") { &UPDXLT::readhash("${UPDXLT::swroot}/proxy/settings", \%proxysettings); } | |
63 | ||
64 | if (-e "${UPDXLT::swroot}/proxy/advanced/settings") | |
65 | { | |
66 | %proxysettings=(); | |
67 | &UPDXLT::readhash("${UPDXLT::swroot}/proxy/advanced/settings", \%proxysettings); | |
68 | } | |
69 | ||
70 | ||
71 | foreach my $dir (<$repository/download/*>) | |
72 | { | |
73 | if (-d $dir) | |
74 | { | |
75 | foreach my $updatefile (<$dir/*>) | |
76 | { | |
77 | unless ($updatefile =~ /.info$/) | |
78 | { | |
79 | push(@downloads, $updatefile); | |
80 | } | |
81 | } | |
82 | } | |
83 | } | |
84 | ||
85 | foreach my $updatefile (@downloads) | |
86 | { | |
87 | if(-e "$updatefile.info") | |
88 | { | |
89 | &checkdeaddl($updatefile); | |
90 | } | |
91 | } | |
92 | ||
93 | ||
94 | # clean lock file | |
95 | close(SEM); | |
96 | unlink "$lockfile"; | |
97 | ||
98 | # ------------------------------------------------------------------- | |
99 | ||
100 | sub checkdeaddl | |
101 | { | |
102 | my $updatefile = shift; | |
103 | ||
104 | my %dlinfo = (); | |
105 | &UPDXLT::readhash("$updatefile.info", \%dlinfo); | |
106 | ||
107 | my @http_header = (); | |
108 | my $http_result = '000 n/a'; | |
109 | my $returncode = 0; | |
110 | my $remote_size = 0; | |
111 | my $remote_mtime = 0; | |
112 | my $login = ''; | |
113 | ||
114 | my $cmd_ps = "/bin/ps aux | /bin/grep -E \"(download|wget).*$dlinfo{'SRCURL'}\" | /bin/grep -v \"/bin/grep\""; | |
115 | my @runningdl = `$cmd_ps`; | |
116 | ||
117 | if(@runningdl > 0) | |
118 | { | |
119 | # download is still running, no need to restart download | |
120 | return; | |
121 | } | |
122 | ||
123 | if (($proxysettings{'UPSTREAM_PROXY'}) && ($proxysettings{'UPSTREAM_USER'})) | |
124 | { | |
125 | $login = "--proxy-user=\"$proxysettings{'UPSTREAM_USER'}\""; | |
126 | if ($proxysettings{'UPSTREAM_PASSWORD'}) | |
127 | { | |
128 | $login .= " --proxy-password=\"$proxysettings{'UPSTREAM_PASSWORD'}\""; | |
129 | } | |
130 | } | |
131 | ||
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'} = ''; | |
135 | ||
136 | foreach (@http_header) | |
137 | { | |
138 | chomp; | |
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) } | |
142 | } | |
143 | ||
144 | &writelog($updatefile); | |
145 | &writelog("HTTP result: $http_result"); | |
146 | &writelog("Source time: $remote_mtime"); | |
147 | &writelog("Cached time: " . $dlinfo{'REMOTETIME'}); | |
148 | ||
149 | if ($http_result =~ /\d+\s+OK$/) | |
150 | { | |
151 | my $cmd = "$nice$UPDXLT::apphome/bin/download $dlinfo{'VENDORID'} $dlinfo{'SRCURL'} $dlinfo{'CFMIRROR'}"; | |
152 | ||
153 | if ($remote_size > &UPDXLT::diskfree($repository)) | |
154 | { | |
155 | &writelog("Can't download file, because remote filesize exceeds maximum diskusage"); | |
156 | } | |
157 | elsif ($remote_mtime == $dlinfo{'REMOTETIME'}) | |
158 | { | |
159 | # still the same file, continue download | |
160 | &writelog("Status: Ok, continue download"); | |
161 | $cmd .= " 1 &"; | |
162 | &debuglog("Running command $cmd"); | |
163 | system("$cmd"); | |
164 | } | |
165 | else | |
166 | { | |
167 | # File is changed on remote site, download from scratch | |
168 | &writelog("Status: Outdated, restart download from scratch"); | |
169 | unlink($updatefile); | |
170 | $cmd .= " 0 &"; | |
171 | &debuglog("Running command $cmd"); | |
172 | system("$cmd"); | |
173 | } | |
174 | } else { | |
175 | $_ = $http_result; | |
176 | s/\D+//; | |
177 | if ($_ eq '404') | |
178 | { | |
179 | &writelog("Status: No source"); | |
180 | $dlinfo{'STATUS'} = ${UPDXLT::sfNoSource}; | |
181 | } else { | |
182 | &writelog("Status: Error"); | |
183 | $dlinfo{'STATUS'} = ${UPDXLT::sfUnknown}; | |
184 | } | |
185 | &UPDXLT::writehash("$updatefile.info", \%dlinfo); | |
186 | } | |
187 | } | |
188 | ||
189 | # ------------------------------------------------------------------- | |
190 | ||
191 | sub writelog | |
192 | { | |
193 | if ($logging) | |
194 | { | |
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]; | |
198 | close LOGFILE; | |
199 | } | |
200 | } | |
201 | ||
202 | # ------------------------------------------------------------------- | |
203 | ||
204 | sub debuglog | |
205 | { | |
206 | if ($debug) | |
207 | { | |
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]; | |
211 | close(LOGFILE); | |
212 | } | |
213 | } | |
214 | ||
215 | # ------------------------------------------------------------------- |