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