]> git.saurik.com Git - apt.git/blob - methods/mirror.cc
* debian/apt-utils.install:
[apt.git] / methods / mirror.cc
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 <config.h>
12
13 #include <apt-pkg/aptconfiguration.h>
14 #include <apt-pkg/fileutl.h>
15 #include <apt-pkg/acquire-method.h>
16 #include <apt-pkg/acquire-item.h>
17 #include <apt-pkg/acquire.h>
18 #include <apt-pkg/error.h>
19 #include <apt-pkg/hashes.h>
20 #include <apt-pkg/sourcelist.h>
21 #include <apt-pkg/configuration.h>
22 #include <apt-pkg/metaindex.h>
23
24 #include <algorithm>
25 #include <fstream>
26 #include <iostream>
27
28 #include <stdarg.h>
29 #include <sys/stat.h>
30 #include <sys/types.h>
31 #include <sys/utsname.h>
32 #include <dirent.h>
33
34 using namespace std;
35
36 #include<sstream>
37
38 #include "mirror.h"
39 #include "http.h"
40 #include <apti18n.h>
41 /*}}}*/
42
43 /* Done:
44 * - works with http (only!)
45 * - always picks the first mirror from the list
46 * - call out to problem reporting script
47 * - supports "deb mirror://host/path/to/mirror-list/// dist component"
48 * - uses pkgAcqMethod::FailReason() to have a string representation
49 * of the failure that is also send to LP
50 *
51 * TODO:
52 * - deal with runing as non-root because we can't write to the lists
53 dir then -> use the cached mirror file
54 * - better method to download than having a pkgAcquire interface here
55 * and better error handling there!
56 * - support more than http
57 * - testing :)
58 */
59
60 MirrorMethod::MirrorMethod()
61 : HttpMethod(), DownloadedMirrorFile(false), Debug(false)
62 {
63 };
64
65 // HttpMethod::Configuration - Handle a configuration message /*{{{*/
66 // ---------------------------------------------------------------------
67 /* We stash the desired pipeline depth */
68 bool MirrorMethod::Configuration(string Message)
69 {
70 if (pkgAcqMethod::Configuration(Message) == false)
71 return false;
72 Debug = _config->FindB("Debug::Acquire::mirror",false);
73
74 return true;
75 }
76 /*}}}*/
77
78 // clean the mirrors dir based on ttl information
79 bool MirrorMethod::Clean(string Dir)
80 {
81 vector<metaIndex *>::const_iterator I;
82
83 if(Debug)
84 clog << "MirrorMethod::Clean(): " << Dir << endl;
85
86 if(Dir == "/")
87 return _error->Error("will not clean: '/'");
88
89 // read sources.list
90 pkgSourceList list;
91 list.ReadMainList();
92
93 DIR *D = opendir(Dir.c_str());
94 if (D == 0)
95 return _error->Errno("opendir",_("Unable to read %s"),Dir.c_str());
96
97 string StartDir = SafeGetCWD();
98 if (chdir(Dir.c_str()) != 0)
99 {
100 closedir(D);
101 return _error->Errno("chdir",_("Unable to change to %s"),Dir.c_str());
102 }
103
104 for (struct dirent *Dir = readdir(D); Dir != 0; Dir = readdir(D))
105 {
106 // Skip some files..
107 if (strcmp(Dir->d_name,"lock") == 0 ||
108 strcmp(Dir->d_name,"partial") == 0 ||
109 strcmp(Dir->d_name,".") == 0 ||
110 strcmp(Dir->d_name,"..") == 0)
111 continue;
112
113 // see if we have that uri
114 for(I=list.begin(); I != list.end(); ++I)
115 {
116 string uri = (*I)->GetURI();
117 if(uri.find("mirror://") != 0)
118 continue;
119 string BaseUri = uri.substr(0,uri.size()-1);
120 if (URItoFileName(BaseUri) == Dir->d_name)
121 break;
122 }
123 // nothing found, nuke it
124 if (I == list.end())
125 unlink(Dir->d_name);
126 };
127
128 closedir(D);
129 if (chdir(StartDir.c_str()) != 0)
130 return _error->Errno("chdir",_("Unable to change to %s"),StartDir.c_str());
131 return true;
132 }
133
134
135 bool MirrorMethod::DownloadMirrorFile(string mirror_uri_str)
136 {
137 // not that great to use pkgAcquire here, but we do not have
138 // any other way right now
139 string fetch = BaseUri;
140 fetch.replace(0,strlen("mirror://"),"http://");
141
142 #if 0 // no need for this, the getArchitectures() will also include the main
143 // arch
144 // append main architecture
145 fetch += "?arch=" + _config->Find("Apt::Architecture");
146 #endif
147
148 // append all architectures
149 std::vector<std::string> vec = APT::Configuration::getArchitectures();
150 for (std::vector<std::string>::const_iterator I = vec.begin();
151 I != vec.end(); ++I)
152 if (I == vec.begin())
153 fetch += "?arch" + (*I);
154 else
155 fetch += "&arch=" + (*I);
156
157 // append the dist as a query string
158 if (Dist != "")
159 fetch += "&dist=" + Dist;
160
161 if(Debug)
162 clog << "MirrorMethod::DownloadMirrorFile(): '" << fetch << "'"
163 << " to " << MirrorFile << endl;
164
165 pkgAcquire Fetcher;
166 new pkgAcqFile(&Fetcher, fetch, "", 0, "", "", "", MirrorFile);
167 bool res = (Fetcher.Run() == pkgAcquire::Continue);
168 if(res) {
169 DownloadedMirrorFile = true;
170 chmod(MirrorFile.c_str(), 0644);
171 }
172 Fetcher.Shutdown();
173
174 if(Debug)
175 clog << "MirrorMethod::DownloadMirrorFile() success: " << res << endl;
176
177 return res;
178 }
179
180 // Randomizes the lines in the mirror file, this is used so that
181 // we spread the load on the mirrors evenly
182 bool MirrorMethod::RandomizeMirrorFile(string mirror_file)
183 {
184 vector<string> content;
185 string line;
186
187 if (!FileExists(mirror_file))
188 return false;
189
190 // read
191 ifstream in(mirror_file.c_str());
192 while ( !in.eof() ) {
193 getline(in, line);
194 content.push_back(line);
195 }
196
197 // we want the file to be random for each different machine, but also
198 // "stable" on the same machine. this is to avoid running into out-of-sync
199 // issues (i.e. Release/Release.gpg different on each mirror)
200 struct utsname buf;
201 int seed=1, i;
202 if(uname(&buf) == 0) {
203 for(i=0,seed=1; buf.nodename[i] != 0; i++) {
204 seed = seed * 31 + buf.nodename[i];
205 }
206 }
207 srand( seed );
208 random_shuffle(content.begin(), content.end());
209
210 // write
211 ofstream out(mirror_file.c_str());
212 while ( !content.empty()) {
213 line = content.back();
214 content.pop_back();
215 out << line << "\n";
216 }
217
218 return true;
219 }
220
221 /* convert a the Queue->Uri back to the mirror base uri and look
222 * at all mirrors we have for this, this is needed as queue->uri
223 * may point to different mirrors (if TryNextMirror() was run)
224 */
225 void MirrorMethod::CurrentQueueUriToMirror()
226 {
227 // already in mirror:// style so nothing to do
228 if(Queue->Uri.find("mirror://") == 0)
229 return;
230
231 // find current mirror and select next one
232 for (vector<string>::const_iterator mirror = AllMirrors.begin();
233 mirror != AllMirrors.end(); ++mirror)
234 {
235 if (Queue->Uri.find(*mirror) == 0)
236 {
237 Queue->Uri.replace(0, mirror->length(), BaseUri);
238 return;
239 }
240 }
241 _error->Error("Internal error: Failed to convert %s back to %s",
242 Queue->Uri.c_str(), BaseUri.c_str());
243 }
244
245 bool MirrorMethod::TryNextMirror()
246 {
247 // find current mirror and select next one
248 for (vector<string>::const_iterator mirror = AllMirrors.begin();
249 mirror != AllMirrors.end(); ++mirror)
250 {
251 if (Queue->Uri.find(*mirror) != 0)
252 continue;
253
254 vector<string>::const_iterator nextmirror = mirror + 1;
255 if (nextmirror == AllMirrors.end())
256 break;
257 Queue->Uri.replace(0, mirror->length(), *nextmirror);
258 if (Debug)
259 clog << "TryNextMirror: " << Queue->Uri << endl;
260
261 // inform parent
262 UsedMirror = *nextmirror;
263 Log("Switching mirror");
264 return true;
265 }
266
267 if (Debug)
268 clog << "TryNextMirror could not find another mirror to try" << endl;
269
270 return false;
271 }
272
273 bool MirrorMethod::InitMirrors()
274 {
275 // if we do not have a MirrorFile, fallback
276 if(!FileExists(MirrorFile))
277 {
278 // FIXME: fallback to a default mirror here instead
279 // and provide a config option to define that default
280 return _error->Error(_("No mirror file '%s' found "), MirrorFile.c_str());
281 }
282
283 if (access(MirrorFile.c_str(), R_OK) != 0)
284 {
285 // FIXME: fallback to a default mirror here instead
286 // and provide a config option to define that default
287 return _error->Error(_("Can not read mirror file '%s'"), MirrorFile.c_str());
288 }
289
290 // FIXME: make the mirror selection more clever, do not
291 // just use the first one!
292 // BUT: we can not make this random, the mirror has to be
293 // stable accross session, because otherwise we can
294 // get into sync issues (got indexfiles from mirror A,
295 // but packages from mirror B - one might be out of date etc)
296 ifstream in(MirrorFile.c_str());
297 string s;
298 while (!in.eof())
299 {
300 getline(in, s);
301
302 // ignore lines that start with #
303 if (s.find("#") == 0)
304 continue;
305 // ignore empty lines
306 if (s.size() == 0)
307 continue;
308 // ignore non http lines
309 if (s.find("http://") != 0)
310 continue;
311
312 AllMirrors.push_back(s);
313 }
314 Mirror = AllMirrors[0];
315 UsedMirror = Mirror;
316 return true;
317 }
318
319 string MirrorMethod::GetMirrorFileName(string mirror_uri_str)
320 {
321 /*
322 - a mirror_uri_str looks like this:
323 mirror://people.ubuntu.com/~mvo/apt/mirror/mirrors/dists/feisty/Release.gpg
324
325 - the matching source.list entry
326 deb mirror://people.ubuntu.com/~mvo/apt/mirror/mirrors feisty main
327
328 - we actually want to go after:
329 http://people.ubuntu.com/~mvo/apt/mirror/mirrors
330
331 And we need to save the BaseUri for later:
332 - mirror://people.ubuntu.com/~mvo/apt/mirror/mirrors
333
334 FIXME: what if we have two similar prefixes?
335 mirror://people.ubuntu.com/~mvo/mirror
336 mirror://people.ubuntu.com/~mvo/mirror2
337 then mirror_uri_str looks like:
338 mirror://people.ubuntu.com/~mvo/apt/mirror/dists/feisty/Release.gpg
339 mirror://people.ubuntu.com/~mvo/apt/mirror2/dists/feisty/Release.gpg
340 we search sources.list and find:
341 mirror://people.ubuntu.com/~mvo/apt/mirror
342 in both cases! So we need to apply some domain knowledge here :( and
343 check for /dists/ or /Release.gpg as suffixes
344 */
345 string name;
346 if(Debug)
347 std::cerr << "GetMirrorFileName: " << mirror_uri_str << std::endl;
348
349 // read sources.list and find match
350 vector<metaIndex *>::const_iterator I;
351 pkgSourceList list;
352 list.ReadMainList();
353 for(I=list.begin(); I != list.end(); ++I)
354 {
355 string uristr = (*I)->GetURI();
356 if(Debug)
357 std::cerr << "Checking: " << uristr << std::endl;
358 if(uristr.substr(0,strlen("mirror://")) != string("mirror://"))
359 continue;
360 // find matching uri in sources.list
361 if(mirror_uri_str.substr(0,uristr.size()) == uristr)
362 {
363 if(Debug)
364 std::cerr << "found BaseURI: " << uristr << std::endl;
365 BaseUri = uristr.substr(0,uristr.size()-1);
366 Dist = (*I)->GetDist();
367 }
368 }
369 // get new file
370 name = _config->FindDir("Dir::State::mirrors") + URItoFileName(BaseUri);
371
372 if(Debug)
373 {
374 cerr << "base-uri: " << BaseUri << endl;
375 cerr << "mirror-file: " << name << endl;
376 }
377 return name;
378 }
379
380 // MirrorMethod::Fetch - Fetch an item /*{{{*/
381 // ---------------------------------------------------------------------
382 /* This adds an item to the pipeline. We keep the pipeline at a fixed
383 depth. */
384 bool MirrorMethod::Fetch(FetchItem *Itm)
385 {
386 if(Debug)
387 clog << "MirrorMethod::Fetch()" << endl;
388
389 // the http method uses Fetch(0) as a way to update the pipeline,
390 // just let it do its work in this case - Fetch() with a valid
391 // Itm will always run before the first Fetch(0)
392 if(Itm == NULL)
393 return HttpMethod::Fetch(Itm);
394
395 // if we don't have the name of the mirror file on disk yet,
396 // calculate it now (can be derived from the uri)
397 if(MirrorFile.empty())
398 MirrorFile = GetMirrorFileName(Itm->Uri);
399
400 // download mirror file once (if we are after index files)
401 if(Itm->IndexFile && !DownloadedMirrorFile)
402 {
403 Clean(_config->FindDir("Dir::State::mirrors"));
404 if (DownloadMirrorFile(Itm->Uri))
405 RandomizeMirrorFile(MirrorFile);
406 }
407
408 if(AllMirrors.empty()) {
409 if(!InitMirrors()) {
410 // no valid mirror selected, something went wrong downloading
411 // from the master mirror site most likely and there is
412 // no old mirror file availalbe
413 return false;
414 }
415 }
416
417 if(Itm->Uri.find("mirror://") != string::npos)
418 Itm->Uri.replace(0,BaseUri.size(), Mirror);
419
420 if(Debug)
421 clog << "Fetch: " << Itm->Uri << endl << endl;
422
423 // now run the real fetcher
424 return HttpMethod::Fetch(Itm);
425 };
426
427 void MirrorMethod::Fail(string Err,bool Transient)
428 {
429 // FIXME: TryNextMirror is not ideal for indexfile as we may
430 // run into auth issues
431
432 if (Debug)
433 clog << "Failure to get " << Queue->Uri << endl;
434
435 // try the next mirror on fail (if its not a expected failure,
436 // e.g. translations are ok to ignore)
437 if (!Queue->FailIgnore && TryNextMirror())
438 return;
439
440 // all mirrors failed, so bail out
441 string s;
442 strprintf(s, _("[Mirror: %s]"), Mirror.c_str());
443 SetIP(s);
444
445 CurrentQueueUriToMirror();
446 pkgAcqMethod::Fail(Err, Transient);
447 }
448
449 void MirrorMethod::URIStart(FetchResult &Res)
450 {
451 CurrentQueueUriToMirror();
452 pkgAcqMethod::URIStart(Res);
453 }
454
455 void MirrorMethod::URIDone(FetchResult &Res,FetchResult *Alt)
456 {
457 CurrentQueueUriToMirror();
458 pkgAcqMethod::URIDone(Res, Alt);
459 }
460
461
462 int main()
463 {
464 setlocale(LC_ALL, "");
465
466 MirrorMethod Mth;
467
468 return Mth.Loop();
469 }
470
471