]> git.saurik.com Git - apt.git/blob - methods/mirror.cc
merged from lp:~donkult/apt/sid
[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 #include <fstream>
20 #include <iostream>
21 #include <stdarg.h>
22 #include <sys/stat.h>
23 #include <sys/types.h>
24 #include <dirent.h>
25
26 using namespace std;
27
28 #include<sstream>
29
30 #include "mirror.h"
31 #include "http.h"
32 #include "apti18n.h"
33 /*}}}*/
34
35 /* Done:
36 * - works with http (only!)
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"
40 * - uses pkgAcqMethod::FailReason() to have a string representation
41 * of the failure that is also send to LP
42 *
43 * TODO:
44 * - deal with runing as non-root because we can't write to the lists
45 dir then -> use the cached mirror file
46 * - better method to download than having a pkgAcquire interface here
47 * and better error handling there!
48 * - support more than http
49 * - testing :)
50 */
51
52 MirrorMethod::MirrorMethod()
53 : HttpMethod(), DownloadedMirrorFile(false)
54 {
55 };
56
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
70 // clean the mirrors dir based on ttl information
71 bool MirrorMethod::Clean(string Dir)
72 {
73 vector<metaIndex *>::const_iterator I;
74
75 if(Debug)
76 clog << "MirrorMethod::Clean(): " << Dir << endl;
77
78 if(Dir == "/")
79 return _error->Error("will not clean: '/'");
80
81 // read sources.list
82 pkgSourceList list;
83 list.ReadMainList();
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;
104
105 // see if we have that uri
106 for(I=list.begin(); I != list.end(); I++)
107 {
108 string uri = (*I)->GetURI();
109 if(uri.find("mirror://") != 0)
110 continue;
111 string BaseUri = uri.substr(0,uri.size()-1);
112 if (URItoFileName(BaseUri) == Dir->d_name)
113 break;
114 }
115 // nothing found, nuke it
116 if (I == list.end())
117 unlink(Dir->d_name);
118 };
119
120 chdir(StartDir.c_str());
121 closedir(D);
122 return true;
123 }
124
125
126 bool MirrorMethod::DownloadMirrorFile(string mirror_uri_str)
127 {
128 if(Debug)
129 clog << "MirrorMethod::DownloadMirrorFile(): " << endl;
130
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
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
156 for (int i=0; i < AllMirrors.size(); i++)
157 {
158 if (Queue->Uri.find(AllMirrors[i]) == 0)
159 {
160 Queue->Uri.replace(0, AllMirrors[i].size(), BaseUri);
161 return;
162 }
163 }
164 _error->Error("Internal error: Failed to convert %s back to %s",
165 Queue->Uri.c_str(), BaseUri.c_str());
166 }
167
168 bool MirrorMethod::TryNextMirror()
169 {
170 // find current mirror and select next one
171 for (int i=0; i < AllMirrors.size()-1; i++)
172 {
173 if (Queue->Uri.find(AllMirrors[i]) == 0)
174 {
175 Queue->Uri.replace(0, AllMirrors[i].size(), AllMirrors[i+1]);
176 if (Debug)
177 clog << "TryNextMirror: " << Queue->Uri << endl;
178 return true;
179 }
180 }
181
182 if (Debug)
183 clog << "TryNextMirror could not find another mirror to try" << endl;
184
185 return false;
186 }
187
188 bool MirrorMethod::InitMirrors()
189 {
190 // if we do not have a MirrorFile, fallback
191 if(!FileExists(MirrorFile))
192 {
193 // FIXME: fallback to a default mirror here instead
194 // and provide a config option to define that default
195 return _error->Error(_("No mirror file '%s' found "), MirrorFile.c_str());
196 }
197
198 // FIXME: make the mirror selection more clever, do not
199 // just use the first one!
200 // BUT: we can not make this random, the mirror has to be
201 // stable accross session, because otherwise we can
202 // get into sync issues (got indexfiles from mirror A,
203 // but packages from mirror B - one might be out of date etc)
204 ifstream in(MirrorFile.c_str());
205 string s;
206 while (!in.eof())
207 {
208 getline(in, s);
209 if (s.size() > 0)
210 AllMirrors.push_back(s);
211 }
212 Mirror = AllMirrors[0];
213 UsedMirror = Mirror;
214 return true;
215 }
216
217 string MirrorMethod::GetMirrorFileName(string mirror_uri_str)
218 {
219 /*
220 - a mirror_uri_str looks like this:
221 mirror://people.ubuntu.com/~mvo/apt/mirror/mirrors/dists/feisty/Release.gpg
222
223 - the matching source.list entry
224 deb mirror://people.ubuntu.com/~mvo/apt/mirror/mirrors feisty main
225
226 - we actually want to go after:
227 http://people.ubuntu.com/~mvo/apt/mirror/mirrors
228
229 And we need to save the BaseUri for later:
230 - mirror://people.ubuntu.com/~mvo/apt/mirror/mirrors
231
232 FIXME: what if we have two similar prefixes?
233 mirror://people.ubuntu.com/~mvo/mirror
234 mirror://people.ubuntu.com/~mvo/mirror2
235 then mirror_uri_str looks like:
236 mirror://people.ubuntu.com/~mvo/apt/mirror/dists/feisty/Release.gpg
237 mirror://people.ubuntu.com/~mvo/apt/mirror2/dists/feisty/Release.gpg
238 we search sources.list and find:
239 mirror://people.ubuntu.com/~mvo/apt/mirror
240 in both cases! So we need to apply some domain knowledge here :( and
241 check for /dists/ or /Release.gpg as suffixes
242 */
243 string name;
244 if(Debug)
245 std::cerr << "GetMirrorFileName: " << mirror_uri_str << std::endl;
246
247 // read sources.list and find match
248 vector<metaIndex *>::const_iterator I;
249 pkgSourceList list;
250 list.ReadMainList();
251 for(I=list.begin(); I != list.end(); I++)
252 {
253 string uristr = (*I)->GetURI();
254 if(Debug)
255 std::cerr << "Checking: " << uristr << std::endl;
256 if(uristr.substr(0,strlen("mirror://")) != string("mirror://"))
257 continue;
258 // find matching uri in sources.list
259 if(mirror_uri_str.substr(0,uristr.size()) == uristr)
260 {
261 if(Debug)
262 std::cerr << "found BaseURI: " << uristr << std::endl;
263 BaseUri = uristr.substr(0,uristr.size()-1);
264 }
265 }
266 // get new file
267 name = _config->FindDir("Dir::State::mirrors") + URItoFileName(BaseUri);
268
269 if(Debug)
270 {
271 cerr << "base-uri: " << BaseUri << endl;
272 cerr << "mirror-file: " << name << endl;
273 }
274 return name;
275 }
276
277 // MirrorMethod::Fetch - Fetch an item /*{{{*/
278 // ---------------------------------------------------------------------
279 /* This adds an item to the pipeline. We keep the pipeline at a fixed
280 depth. */
281 bool MirrorMethod::Fetch(FetchItem *Itm)
282 {
283 if(Debug)
284 clog << "MirrorMethod::Fetch()" << endl;
285
286 // the http method uses Fetch(0) as a way to update the pipeline,
287 // just let it do its work in this case - Fetch() with a valid
288 // Itm will always run before the first Fetch(0)
289 if(Itm == NULL)
290 return HttpMethod::Fetch(Itm);
291
292 // if we don't have the name of the mirror file on disk yet,
293 // calculate it now (can be derived from the uri)
294 if(MirrorFile.empty())
295 MirrorFile = GetMirrorFileName(Itm->Uri);
296
297 // download mirror file once (if we are after index files)
298 if(Itm->IndexFile && !DownloadedMirrorFile)
299 {
300 Clean(_config->FindDir("Dir::State::mirrors"));
301 DownloadMirrorFile(Itm->Uri);
302 }
303
304 if(AllMirrors.empty()) {
305 if(!InitMirrors()) {
306 // no valid mirror selected, something went wrong downloading
307 // from the master mirror site most likely and there is
308 // no old mirror file availalbe
309 return false;
310 }
311 }
312
313 if(Itm->Uri.find("mirror://") != string::npos)
314 Itm->Uri.replace(0,BaseUri.size(), Mirror);
315
316 if(Debug)
317 clog << "Fetch: " << Itm->Uri << endl << endl;
318
319 // now run the real fetcher
320 return HttpMethod::Fetch(Itm);
321 };
322
323 void MirrorMethod::Fail(string Err,bool Transient)
324 {
325 // FIXME: TryNextMirror is not ideal for indexfile as we may
326 // run into auth issues
327
328 if (Debug)
329 clog << "Failure to get " << Queue->Uri << endl;
330
331 // try the next mirror on fail (if its not a expected failure,
332 // e.g. translations are ok to ignore)
333 if (!Queue->FailIgnore && TryNextMirror())
334 return;
335
336 // all mirrors failed, so bail out
337 string s;
338 strprintf(s, _("[Mirror: %s]"), Mirror.c_str());
339 SetIP(s);
340
341 CurrentQueueUriToMirror();
342 pkgAcqMethod::Fail(Err, Transient);
343 }
344
345 void MirrorMethod::URIStart(FetchResult &Res)
346 {
347 CurrentQueueUriToMirror();
348 pkgAcqMethod::URIStart(Res);
349 }
350
351 void MirrorMethod::URIDone(FetchResult &Res,FetchResult *Alt)
352 {
353 CurrentQueueUriToMirror();
354 pkgAcqMethod::URIDone(Res, Alt);
355 }
356
357
358 int main()
359 {
360 setlocale(LC_ALL, "");
361
362 MirrorMethod Mth;
363
364 return Mth.Loop();
365 }
366
367