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