]> git.saurik.com Git - apt.git/blame - methods/mirror.cc
make the mirror failures actually produce a error message
[apt.git] / methods / mirror.cc
CommitLineData
5f6b130d
MV
1// -*- mode: cpp; mode: fold -*-
2// Description /*{{{*/
3// $Id: mirror.cc,v 1.59 2004/05/08 19:42:35 mdz Exp $
4/* ######################################################################
5
6 Mirror Aquire Method - This is the Mirror aquire method for APT.
7
8 ##################################################################### */
9 /*}}}*/
10// Include Files /*{{{*/
11#include <apt-pkg/fileutl.h>
12#include <apt-pkg/acquire-method.h>
13#include <apt-pkg/acquire-item.h>
14#include <apt-pkg/acquire.h>
15#include <apt-pkg/error.h>
16#include <apt-pkg/hashes.h>
0c312e0e 17#include <apt-pkg/sourcelist.h>
5f6b130d
MV
18
19#include <fstream>
20#include <iostream>
14e097c1 21#include <stdarg.h>
d731f9c5 22#include <sys/stat.h>
70288656
MV
23#include <sys/types.h>
24#include <dirent.h>
14e097c1 25
5f6b130d
MV
26using namespace std;
27
28#include "mirror.h"
29#include "http.h"
70288656 30#include "apti18n.h"
5f6b130d
MV
31 /*}}}*/
32
362d2934
MV
33/* Done:
34 * - works with http only
35 * - always picks the first mirror from the list
36 * - call out to problem reporting script
37 * - supports "deb mirror://host/path/to/mirror-list/// dist component"
38 *
86c17f0a 39 * TODO:
362d2934
MV
40 * what about gpgv failures? this should call-out to the problem reporting
41 script, but we need to know what mirror was used
42 * better standard format for errors to send back
43 * - implement failure reporting at the pkgAcquire::Item::Failed() level
44 but then we need to send back what uri exactly was failing
cae9cdce
MV
45 [mvo: the problem with this approach is ::Failed() is not really
46 called for all failures :/ e.g. md5sum mismatch in a archive
47 is not]
d731f9c5
MV
48 * - deal with runing as non-root because we can't write to the lists
49 dir then -> use the cached mirror file
86c17f0a 50 * - better method to download than having a pkgAcquire interface here
933833c5
MV
51 * - magicmarker is (a bit) evil, maybe just use a similar approach as in
52 clean and read the sources.list and use the GetURI() method to find
53 the prefix?
362d2934 54 * support more than http
86c17f0a 55 * - testing :)
86c17f0a
MV
56 */
57
5f6b130d 58MirrorMethod::MirrorMethod()
14e097c1 59 : HttpMethod(), HasMirrorFile(false)
5f6b130d
MV
60{
61#if 0
62 HasMirrorFile=true;
14e097c1
MV
63 BaseUri="mirror://people.ubuntu.com/~mvo/mirror/mirrors";
64 MirrorFile="/var/lib/apt/lists/people.ubuntu.com_%7emvo_apt_mirror_mirrors";
5f6b130d
MV
65 Mirror="http://de.archive.ubuntu.com/ubuntu/";
66#endif
67};
68
14e097c1
MV
69// HttpMethod::Configuration - Handle a configuration message /*{{{*/
70// ---------------------------------------------------------------------
71/* We stash the desired pipeline depth */
72bool MirrorMethod::Configuration(string Message)
73{
74 if (pkgAcqMethod::Configuration(Message) == false)
75 return false;
76 Debug = _config->FindB("Debug::Acquire::mirror",false);
77
78 return true;
79}
80 /*}}}*/
81
d731f9c5 82// clean the mirrors dir based on ttl information
70288656 83bool MirrorMethod::Clean(string Dir)
d731f9c5 84{
0c312e0e
MV
85 vector<metaIndex *>::const_iterator I;
86
87 if(Debug)
88 clog << "MirrorMethod::Clean(): " << Dir << endl;
89
90 // read sources.list
91 pkgSourceList list;
92 list.ReadMainList();
70288656
MV
93
94 DIR *D = opendir(Dir.c_str());
95 if (D == 0)
96 return _error->Errno("opendir",_("Unable to read %s"),Dir.c_str());
97
98 string StartDir = SafeGetCWD();
99 if (chdir(Dir.c_str()) != 0)
100 {
101 closedir(D);
102 return _error->Errno("chdir",_("Unable to change to %s"),Dir.c_str());
103 }
104
105 for (struct dirent *Dir = readdir(D); Dir != 0; Dir = readdir(D))
106 {
107 // Skip some files..
108 if (strcmp(Dir->d_name,"lock") == 0 ||
109 strcmp(Dir->d_name,"partial") == 0 ||
110 strcmp(Dir->d_name,".") == 0 ||
111 strcmp(Dir->d_name,"..") == 0)
112 continue;
0c312e0e
MV
113
114 // see if we have that uri
115 for(I=list.begin(); I != list.end(); I++)
70288656 116 {
0c312e0e
MV
117 string uri = (*I)->GetURI();
118 if(uri.substr(0,strlen("mirror://")) != string("mirror://"))
119 continue;
120 string Marker = _config->Find("Acquire::Mirror::MagicMarker","///");
121 string BaseUri = uri.substr(0,uri.find(Marker));
122 if (URItoFileName(BaseUri) == Dir->d_name)
123 break;
70288656 124 }
0c312e0e
MV
125 // nothing found, nuke it
126 if (I == list.end())
70288656 127 unlink(Dir->d_name);
70288656 128 };
d731f9c5 129
70288656
MV
130 chdir(StartDir.c_str());
131 closedir(D);
132 return true;
d731f9c5
MV
133}
134
14e097c1 135
5f6b130d
MV
136bool MirrorMethod::GetMirrorFile(string uri)
137{
138 string Marker = _config->Find("Acquire::Mirror::MagicMarker","///");
139 BaseUri = uri.substr(0,uri.find(Marker));
14e097c1
MV
140
141 string fetch = BaseUri;
142 fetch.replace(0,strlen("mirror://"),"http://");
5f6b130d 143
70288656 144 // get new file
d731f9c5 145 MirrorFile = _config->FindDir("Dir::State::mirrors") + URItoFileName(BaseUri);
5f6b130d 146
14e097c1
MV
147 if(Debug)
148 {
149 cerr << "base-uri: " << BaseUri << endl;
150 cerr << "mirror-file: " << MirrorFile << endl;
151 }
5f6b130d 152
d731f9c5
MV
153 // check the file, if it is not older than RefreshInterval just use it
154 // otherwise try to get a new one
155 if(FileExists(MirrorFile))
156 {
157 struct stat buf;
158 time_t t,now,refresh;
159 if(stat(MirrorFile.c_str(), &buf) != 0)
160 return false;
161 t = std::max(buf.st_mtime, buf.st_ctime);
162 now = time(NULL);
163 refresh = 60*_config->FindI("Acquire::Mirror::RefreshInterval",360);
164 if(t + refresh > now)
165 {
166 if(Debug)
167 clog << "Mirror file is in RefreshInterval" << endl;
168 HasMirrorFile = true;
169 return true;
170 }
171 if(Debug)
70288656 172 clog << "Mirror file " << MirrorFile << " older than " << refresh << "min, re-download it" << endl;
d731f9c5
MV
173 }
174
175 // not that great to use pkgAcquire here, but we do not have
176 // any other way right now
5f6b130d 177 pkgAcquire Fetcher;
14e097c1 178 new pkgAcqFile(&Fetcher, fetch, "", 0, "", "", "", MirrorFile);
5f6b130d 179 bool res = (Fetcher.Run() == pkgAcquire::Continue);
d731f9c5 180 if(res)
5f6b130d
MV
181 HasMirrorFile = true;
182 Fetcher.Shutdown();
d731f9c5 183 return res;
5f6b130d
MV
184}
185
186bool MirrorMethod::SelectMirror()
187{
d731f9c5
MV
188 // FIXME: make the mirror selection more clever, do not
189 // just use the first one!
5f6b130d
MV
190 ifstream in(MirrorFile.c_str());
191 getline(in, Mirror);
14e097c1
MV
192 if(Debug)
193 cerr << "Using mirror: " << Mirror << endl;
194 return true;
5f6b130d
MV
195}
196
197// MirrorMethod::Fetch - Fetch an item /*{{{*/
198// ---------------------------------------------------------------------
199/* This adds an item to the pipeline. We keep the pipeline at a fixed
200 depth. */
201bool MirrorMethod::Fetch(FetchItem *Itm)
202{
d731f9c5 203 // select mirror only once per session
5f6b130d
MV
204 if(!HasMirrorFile)
205 {
70288656 206 Clean(_config->FindDir("Dir::State::mirrors"));
5f6b130d
MV
207 GetMirrorFile(Itm->Uri);
208 SelectMirror();
209 }
210
ef1e6d88
MV
211 for (FetchItem *I = Queue; I != 0; I = I->Next)
212 {
213 if(I->Uri.find("mirror://") != string::npos)
214 I->Uri.replace(0,BaseUri.size(),Mirror);
215 }
5f6b130d 216
14e097c1
MV
217 // now run the real fetcher
218 return HttpMethod::Fetch(Itm);
5f6b130d
MV
219};
220
14e097c1
MV
221void MirrorMethod::Fail(string Err,bool Transient)
222{
cae9cdce
MV
223 // FIXME: queue next mirror?
224 ReportMirrorFailure(Err);
225
14e097c1
MV
226 if(Queue->Uri.find("http://") != string::npos)
227 Queue->Uri.replace(0,Mirror.size(), BaseUri);
228 pkgAcqMethod::Fail(Err, Transient);
229}
230
231void MirrorMethod::URIStart(FetchResult &Res)
232{
233 if(Queue->Uri.find("http://") != string::npos)
234 Queue->Uri.replace(0,Mirror.size(), BaseUri);
235 pkgAcqMethod::URIStart(Res);
236}
237
238void MirrorMethod::URIDone(FetchResult &Res,FetchResult *Alt)
239{
cae9cdce
MV
240 // FIXME: queue next mirror?
241 if(Queue->ExpectedMD5 != "" && Res.MD5Sum != Queue->ExpectedMD5)
242 ReportMirrorFailure("499 Hash mismatch");
243
14e097c1
MV
244 if(Queue->Uri.find("http://") != string::npos)
245 Queue->Uri.replace(0,Mirror.size(), BaseUri);
246 pkgAcqMethod::URIDone(Res, Alt);
247}
248
cae9cdce
MV
249void MirrorMethod::ReportMirrorFailure(string FailCode)
250{
251 // report that Queue->Uri failed
362d2934 252#if 0
cae9cdce
MV
253 std::cerr << "\nReportMirrorFailure: "
254 << Queue->Uri
255 << " FailCode: "
256 << FailCode << std::endl;
2769f0bc 257#endif
362d2934
MV
258 const char *Args[40];
259 unsigned int i = 0;
260 string report = _config->Find("Methods::Mirror::ProblemReporting",
261 "/usr/lib/apt/report-mirror-failure");
262 Args[i++] = report.c_str();
263 Args[i++] = Queue->Uri.c_str();
264 Args[i++] = FailCode.c_str();
265 pid_t pid = ExecFork();
266 if(pid < 0)
267 {
268 _error->Error("ReportMirrorFailure Fork failed");
269 return;
270 }
271 else if(pid == 0)
272 {
273 execvp(report.c_str(), (char**)Args);
274 }
275 if(!ExecWait(pid, "report-mirror-failure"))
276 {
277 _error->Warning("Couldn't report problem to '%s'",
278 _config->Find("Acquire::Mirror::ReportFailures").c_str());
279 }
cae9cdce 280}
14e097c1 281
5f6b130d
MV
282int main()
283{
284 setlocale(LC_ALL, "");
285
286 MirrorMethod Mth;
287
14e097c1 288 return Mth.Loop();
5f6b130d
MV
289}
290
291