]>
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 /*{{{*/ | |
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 |
26 | using namespace std; |
27 | ||
0ded3ad3 MV |
28 | #include<sstream> |
29 | ||
5f6b130d MV |
30 | #include "mirror.h" |
31 | #include "http.h" | |
70288656 | 32 | #include "apti18n.h" |
5f6b130d MV |
33 | /*}}}*/ |
34 | ||
362d2934 | 35 | /* Done: |
59271f62 | 36 | * - works with http (only!) |
362d2934 MV |
37 | * - always picks the first mirror from the list |
38 | * - call out to problem reporting script | |
39 | * - supports "deb mirror://host/path/to/mirror-list/// dist component" | |
a577a938 | 40 | * - uses pkgAcqMethod::FailReason() to have a string representation |
59271f62 | 41 | * of the failure that is also send to LP |
362d2934 | 42 | * |
86c17f0a | 43 | * TODO: |
d731f9c5 MV |
44 | * - deal with runing as non-root because we can't write to the lists |
45 | dir then -> use the cached mirror file | |
86c17f0a | 46 | * - better method to download than having a pkgAcquire interface here |
3f599bb7 | 47 | * and better error handling there! |
066b53e9 | 48 | * - support more than http |
86c17f0a | 49 | * - testing :) |
86c17f0a MV |
50 | */ |
51 | ||
5f6b130d | 52 | MirrorMethod::MirrorMethod() |
38eedeb7 | 53 | : HttpMethod(), DownloadedMirrorFile(false) |
5f6b130d | 54 | { |
5f6b130d MV |
55 | }; |
56 | ||
14e097c1 MV |
57 | // HttpMethod::Configuration - Handle a configuration message /*{{{*/ |
58 | // --------------------------------------------------------------------- | |
59 | /* We stash the desired pipeline depth */ | |
60 | bool MirrorMethod::Configuration(string Message) | |
61 | { | |
62 | if (pkgAcqMethod::Configuration(Message) == false) | |
63 | return false; | |
64 | Debug = _config->FindB("Debug::Acquire::mirror",false); | |
65 | ||
66 | return true; | |
67 | } | |
68 | /*}}}*/ | |
69 | ||
d731f9c5 | 70 | // clean the mirrors dir based on ttl information |
70288656 | 71 | bool MirrorMethod::Clean(string Dir) |
d731f9c5 | 72 | { |
0c312e0e MV |
73 | vector<metaIndex *>::const_iterator I; |
74 | ||
75 | if(Debug) | |
76 | clog << "MirrorMethod::Clean(): " << Dir << endl; | |
77 | ||
38eedeb7 MV |
78 | if(Dir == "/") |
79 | return _error->Error("will not clean: '/'"); | |
80 | ||
0c312e0e MV |
81 | // read sources.list |
82 | pkgSourceList list; | |
83 | list.ReadMainList(); | |
70288656 MV |
84 | |
85 | DIR *D = opendir(Dir.c_str()); | |
86 | if (D == 0) | |
87 | return _error->Errno("opendir",_("Unable to read %s"),Dir.c_str()); | |
88 | ||
89 | string StartDir = SafeGetCWD(); | |
90 | if (chdir(Dir.c_str()) != 0) | |
91 | { | |
92 | closedir(D); | |
93 | return _error->Errno("chdir",_("Unable to change to %s"),Dir.c_str()); | |
94 | } | |
95 | ||
96 | for (struct dirent *Dir = readdir(D); Dir != 0; Dir = readdir(D)) | |
97 | { | |
98 | // Skip some files.. | |
99 | if (strcmp(Dir->d_name,"lock") == 0 || | |
100 | strcmp(Dir->d_name,"partial") == 0 || | |
101 | strcmp(Dir->d_name,".") == 0 || | |
102 | strcmp(Dir->d_name,"..") == 0) | |
103 | continue; | |
0c312e0e MV |
104 | |
105 | // see if we have that uri | |
106 | for(I=list.begin(); I != list.end(); I++) | |
70288656 | 107 | { |
0c312e0e | 108 | string uri = (*I)->GetURI(); |
b86f6421 | 109 | if(uri.find("mirror://") != 0) |
0c312e0e | 110 | continue; |
066b53e9 | 111 | string BaseUri = uri.substr(0,uri.size()-1); |
0c312e0e MV |
112 | if (URItoFileName(BaseUri) == Dir->d_name) |
113 | break; | |
70288656 | 114 | } |
0c312e0e MV |
115 | // nothing found, nuke it |
116 | if (I == list.end()) | |
70288656 | 117 | unlink(Dir->d_name); |
70288656 | 118 | }; |
d731f9c5 | 119 | |
70288656 MV |
120 | chdir(StartDir.c_str()); |
121 | closedir(D); | |
122 | return true; | |
d731f9c5 MV |
123 | } |
124 | ||
14e097c1 | 125 | |
38eedeb7 MV |
126 | bool MirrorMethod::DownloadMirrorFile(string mirror_uri_str) |
127 | { | |
5dad4134 MV |
128 | if(Debug) |
129 | clog << "MirrorMethod::DownloadMirrorFile(): " << endl; | |
38eedeb7 | 130 | |
38eedeb7 MV |
131 | // not that great to use pkgAcquire here, but we do not have |
132 | // any other way right now | |
133 | string fetch = BaseUri; | |
134 | fetch.replace(0,strlen("mirror://"),"http://"); | |
135 | ||
136 | pkgAcquire Fetcher; | |
137 | new pkgAcqFile(&Fetcher, fetch, "", 0, "", "", "", MirrorFile); | |
138 | bool res = (Fetcher.Run() == pkgAcquire::Continue); | |
139 | if(res) | |
140 | DownloadedMirrorFile = true; | |
141 | Fetcher.Shutdown(); | |
142 | return res; | |
143 | } | |
144 | ||
03915427 MV |
145 | /* convert a the Queue->Uri back to the mirror base uri and look |
146 | * at all mirrors we have for this, this is needed as queue->uri | |
147 | * may point to different mirrors (if TryNextMirror() was run) | |
148 | */ | |
149 | void MirrorMethod::CurrentQueueUriToMirror() | |
150 | { | |
151 | // already in mirror:// style so nothing to do | |
152 | if(Queue->Uri.find("mirror://") == 0) | |
153 | return; | |
154 | ||
155 | // find current mirror and select next one | |
51561c4d DK |
156 | for (vector<string>::const_iterator mirror = AllMirrors.begin(); |
157 | mirror != AllMirrors.end(); ++mirror) | |
03915427 | 158 | { |
51561c4d | 159 | if (Queue->Uri.find(*mirror) == 0) |
03915427 | 160 | { |
51561c4d | 161 | Queue->Uri.replace(0, mirror->length(), BaseUri); |
03915427 MV |
162 | return; |
163 | } | |
164 | } | |
165 | _error->Error("Internal error: Failed to convert %s back to %s", | |
963b16dc | 166 | Queue->Uri.c_str(), BaseUri.c_str()); |
03915427 MV |
167 | } |
168 | ||
169 | bool MirrorMethod::TryNextMirror() | |
96db74ce | 170 | { |
03915427 | 171 | // find current mirror and select next one |
51561c4d DK |
172 | for (vector<string>::const_iterator mirror = AllMirrors.begin(); |
173 | mirror != AllMirrors.end(); ++mirror) | |
03915427 | 174 | { |
51561c4d DK |
175 | if (Queue->Uri.find(*mirror) != 0) |
176 | continue; | |
177 | ||
178 | vector<string>::const_iterator nextmirror = mirror + 1; | |
179 | if (nextmirror != AllMirrors.end()) | |
180 | break; | |
181 | Queue->Uri.replace(0, mirror->length(), *nextmirror); | |
182 | if (Debug) | |
183 | clog << "TryNextMirror: " << Queue->Uri << endl; | |
184 | return true; | |
03915427 MV |
185 | } |
186 | ||
963b16dc MV |
187 | if (Debug) |
188 | clog << "TryNextMirror could not find another mirror to try" << endl; | |
189 | ||
0ded3ad3 | 190 | return false; |
96db74ce MV |
191 | } |
192 | ||
193 | bool MirrorMethod::InitMirrors() | |
38eedeb7 MV |
194 | { |
195 | // if we do not have a MirrorFile, fallback | |
196 | if(!FileExists(MirrorFile)) | |
197 | { | |
198 | // FIXME: fallback to a default mirror here instead | |
199 | // and provide a config option to define that default | |
200 | return _error->Error(_("No mirror file '%s' found "), MirrorFile.c_str()); | |
201 | } | |
202 | ||
203 | // FIXME: make the mirror selection more clever, do not | |
204 | // just use the first one! | |
205 | // BUT: we can not make this random, the mirror has to be | |
206 | // stable accross session, because otherwise we can | |
207 | // get into sync issues (got indexfiles from mirror A, | |
208 | // but packages from mirror B - one might be out of date etc) | |
209 | ifstream in(MirrorFile.c_str()); | |
96db74ce MV |
210 | string s; |
211 | while (!in.eof()) | |
212 | { | |
213 | getline(in, s); | |
483dfdd8 MV |
214 | if (s.size() > 0) |
215 | AllMirrors.push_back(s); | |
96db74ce | 216 | } |
03915427 MV |
217 | Mirror = AllMirrors[0]; |
218 | UsedMirror = Mirror; | |
38eedeb7 MV |
219 | return true; |
220 | } | |
221 | ||
222 | string MirrorMethod::GetMirrorFileName(string mirror_uri_str) | |
5f6b130d | 223 | { |
066b53e9 MV |
224 | /* |
225 | - a mirror_uri_str looks like this: | |
226 | mirror://people.ubuntu.com/~mvo/apt/mirror/mirrors/dists/feisty/Release.gpg | |
227 | ||
228 | - the matching source.list entry | |
229 | deb mirror://people.ubuntu.com/~mvo/apt/mirror/mirrors feisty main | |
230 | ||
231 | - we actually want to go after: | |
232 | http://people.ubuntu.com/~mvo/apt/mirror/mirrors | |
233 | ||
234 | And we need to save the BaseUri for later: | |
235 | - mirror://people.ubuntu.com/~mvo/apt/mirror/mirrors | |
236 | ||
237 | FIXME: what if we have two similar prefixes? | |
238 | mirror://people.ubuntu.com/~mvo/mirror | |
239 | mirror://people.ubuntu.com/~mvo/mirror2 | |
240 | then mirror_uri_str looks like: | |
241 | mirror://people.ubuntu.com/~mvo/apt/mirror/dists/feisty/Release.gpg | |
242 | mirror://people.ubuntu.com/~mvo/apt/mirror2/dists/feisty/Release.gpg | |
243 | we search sources.list and find: | |
244 | mirror://people.ubuntu.com/~mvo/apt/mirror | |
245 | in both cases! So we need to apply some domain knowledge here :( and | |
246 | check for /dists/ or /Release.gpg as suffixes | |
247 | */ | |
38eedeb7 | 248 | string name; |
f0b509cd | 249 | if(Debug) |
38eedeb7 | 250 | std::cerr << "GetMirrorFileName: " << mirror_uri_str << std::endl; |
066b53e9 MV |
251 | |
252 | // read sources.list and find match | |
253 | vector<metaIndex *>::const_iterator I; | |
254 | pkgSourceList list; | |
255 | list.ReadMainList(); | |
256 | for(I=list.begin(); I != list.end(); I++) | |
257 | { | |
258 | string uristr = (*I)->GetURI(); | |
f0b509cd MV |
259 | if(Debug) |
260 | std::cerr << "Checking: " << uristr << std::endl; | |
066b53e9 MV |
261 | if(uristr.substr(0,strlen("mirror://")) != string("mirror://")) |
262 | continue; | |
263 | // find matching uri in sources.list | |
264 | if(mirror_uri_str.substr(0,uristr.size()) == uristr) | |
265 | { | |
f0b509cd MV |
266 | if(Debug) |
267 | std::cerr << "found BaseURI: " << uristr << std::endl; | |
066b53e9 MV |
268 | BaseUri = uristr.substr(0,uristr.size()-1); |
269 | } | |
270 | } | |
70288656 | 271 | // get new file |
38eedeb7 | 272 | name = _config->FindDir("Dir::State::mirrors") + URItoFileName(BaseUri); |
5f6b130d | 273 | |
14e097c1 MV |
274 | if(Debug) |
275 | { | |
276 | cerr << "base-uri: " << BaseUri << endl; | |
38eedeb7 | 277 | cerr << "mirror-file: " << name << endl; |
d731f9c5 | 278 | } |
38eedeb7 | 279 | return name; |
5f6b130d MV |
280 | } |
281 | ||
282 | // MirrorMethod::Fetch - Fetch an item /*{{{*/ | |
283 | // --------------------------------------------------------------------- | |
284 | /* This adds an item to the pipeline. We keep the pipeline at a fixed | |
285 | depth. */ | |
286 | bool MirrorMethod::Fetch(FetchItem *Itm) | |
287 | { | |
5dad4134 MV |
288 | if(Debug) |
289 | clog << "MirrorMethod::Fetch()" << endl; | |
290 | ||
38eedeb7 MV |
291 | // the http method uses Fetch(0) as a way to update the pipeline, |
292 | // just let it do its work in this case - Fetch() with a valid | |
293 | // Itm will always run before the first Fetch(0) | |
294 | if(Itm == NULL) | |
295 | return HttpMethod::Fetch(Itm); | |
296 | ||
297 | // if we don't have the name of the mirror file on disk yet, | |
298 | // calculate it now (can be derived from the uri) | |
299 | if(MirrorFile.empty()) | |
300 | MirrorFile = GetMirrorFileName(Itm->Uri); | |
301 | ||
302 | // download mirror file once (if we are after index files) | |
303 | if(Itm->IndexFile && !DownloadedMirrorFile) | |
5f6b130d | 304 | { |
70288656 | 305 | Clean(_config->FindDir("Dir::State::mirrors")); |
38eedeb7 | 306 | DownloadMirrorFile(Itm->Uri); |
5f6b130d MV |
307 | } |
308 | ||
2ac9b90b | 309 | if(AllMirrors.empty()) { |
96db74ce | 310 | if(!InitMirrors()) { |
5dad4134 MV |
311 | // no valid mirror selected, something went wrong downloading |
312 | // from the master mirror site most likely and there is | |
313 | // no old mirror file availalbe | |
314 | return false; | |
315 | } | |
316 | } | |
5dad4134 | 317 | |
963b16dc MV |
318 | if(Itm->Uri.find("mirror://") != string::npos) |
319 | Itm->Uri.replace(0,BaseUri.size(), Mirror); | |
38eedeb7 | 320 | |
963b16dc MV |
321 | if(Debug) |
322 | clog << "Fetch: " << Itm->Uri << endl << endl; | |
38eedeb7 | 323 | |
14e097c1 MV |
324 | // now run the real fetcher |
325 | return HttpMethod::Fetch(Itm); | |
5f6b130d MV |
326 | }; |
327 | ||
14e097c1 MV |
328 | void MirrorMethod::Fail(string Err,bool Transient) |
329 | { | |
2ac9b90b MV |
330 | // FIXME: TryNextMirror is not ideal for indexfile as we may |
331 | // run into auth issues | |
332 | ||
333 | if (Debug) | |
334 | clog << "Failure to get " << Queue->Uri << endl; | |
335 | ||
336 | // try the next mirror on fail (if its not a expected failure, | |
337 | // e.g. translations are ok to ignore) | |
963b16dc | 338 | if (!Queue->FailIgnore && TryNextMirror()) |
483dfdd8 | 339 | return; |
483dfdd8 MV |
340 | |
341 | // all mirrors failed, so bail out | |
0ded3ad3 MV |
342 | string s; |
343 | strprintf(s, _("[Mirror: %s]"), Mirror.c_str()); | |
344 | SetIP(s); | |
345 | ||
03915427 | 346 | CurrentQueueUriToMirror(); |
14e097c1 MV |
347 | pkgAcqMethod::Fail(Err, Transient); |
348 | } | |
349 | ||
350 | void MirrorMethod::URIStart(FetchResult &Res) | |
351 | { | |
03915427 | 352 | CurrentQueueUriToMirror(); |
14e097c1 MV |
353 | pkgAcqMethod::URIStart(Res); |
354 | } | |
355 | ||
356 | void MirrorMethod::URIDone(FetchResult &Res,FetchResult *Alt) | |
357 | { | |
03915427 | 358 | CurrentQueueUriToMirror(); |
14e097c1 MV |
359 | pkgAcqMethod::URIDone(Res, Alt); |
360 | } | |
361 | ||
362 | ||
5f6b130d MV |
363 | int main() |
364 | { | |
365 | setlocale(LC_ALL, ""); | |
366 | ||
367 | MirrorMethod Mth; | |
368 | ||
14e097c1 | 369 | return Mth.Loop(); |
5f6b130d MV |
370 | } |
371 | ||
372 |