]> git.saurik.com Git - apt.git/blob - methods/mirror.cc
b9fa55d8792c21249c5870543982ae1773a76de1
[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.substr(0,strlen("mirror://")) != string("mirror://"))
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 // check the file, if it is not older than RefreshInterval just use it
132 // otherwise try to get a new one
133 if(FileExists(MirrorFile))
134 {
135 struct stat buf;
136 time_t t,now,refresh;
137 if(stat(MirrorFile.c_str(), &buf) != 0)
138 return false;
139 t = std::max(buf.st_mtime, buf.st_ctime);
140 now = time(NULL);
141 refresh = 60*_config->FindI("Acquire::Mirror::RefreshInterval",360);
142 if(t + refresh > now)
143 {
144 if(Debug)
145 clog << "Mirror file is in RefreshInterval" << endl;
146 DownloadedMirrorFile = true;
147 return true;
148 }
149 if(Debug)
150 clog << "Mirror file " << MirrorFile << " older than " << refresh << "min, re-download it" << endl;
151 }
152
153 // not that great to use pkgAcquire here, but we do not have
154 // any other way right now
155 string fetch = BaseUri;
156 fetch.replace(0,strlen("mirror://"),"http://");
157
158 pkgAcquire Fetcher;
159 new pkgAcqFile(&Fetcher, fetch, "", 0, "", "", "", MirrorFile);
160 bool res = (Fetcher.Run() == pkgAcquire::Continue);
161 if(res)
162 DownloadedMirrorFile = true;
163 Fetcher.Shutdown();
164 return res;
165 }
166
167 bool MirrorMethod::SelectNextMirror()
168 {
169 if(Debug)
170 cerr << "using mirror: " << Mirror << endl;
171
172 Mirror = AllMirrors[0];
173 UsedMirror = Mirror;
174 return false;
175 }
176
177 bool MirrorMethod::InitMirrors()
178 {
179 // if we do not have a MirrorFile, fallback
180 if(!FileExists(MirrorFile))
181 {
182 // FIXME: fallback to a default mirror here instead
183 // and provide a config option to define that default
184 return _error->Error(_("No mirror file '%s' found "), MirrorFile.c_str());
185 }
186
187 // FIXME: make the mirror selection more clever, do not
188 // just use the first one!
189 // BUT: we can not make this random, the mirror has to be
190 // stable accross session, because otherwise we can
191 // get into sync issues (got indexfiles from mirror A,
192 // but packages from mirror B - one might be out of date etc)
193 ifstream in(MirrorFile.c_str());
194 string s;
195 while (!in.eof())
196 {
197 getline(in, s);
198 if (s.size() > 0)
199 AllMirrors.push_back(s);
200 }
201 SelectNextMirror();
202 return true;
203 }
204
205 string MirrorMethod::GetMirrorFileName(string mirror_uri_str)
206 {
207 /*
208 - a mirror_uri_str looks like this:
209 mirror://people.ubuntu.com/~mvo/apt/mirror/mirrors/dists/feisty/Release.gpg
210
211 - the matching source.list entry
212 deb mirror://people.ubuntu.com/~mvo/apt/mirror/mirrors feisty main
213
214 - we actually want to go after:
215 http://people.ubuntu.com/~mvo/apt/mirror/mirrors
216
217 And we need to save the BaseUri for later:
218 - mirror://people.ubuntu.com/~mvo/apt/mirror/mirrors
219
220 FIXME: what if we have two similar prefixes?
221 mirror://people.ubuntu.com/~mvo/mirror
222 mirror://people.ubuntu.com/~mvo/mirror2
223 then mirror_uri_str looks like:
224 mirror://people.ubuntu.com/~mvo/apt/mirror/dists/feisty/Release.gpg
225 mirror://people.ubuntu.com/~mvo/apt/mirror2/dists/feisty/Release.gpg
226 we search sources.list and find:
227 mirror://people.ubuntu.com/~mvo/apt/mirror
228 in both cases! So we need to apply some domain knowledge here :( and
229 check for /dists/ or /Release.gpg as suffixes
230 */
231 string name;
232 if(Debug)
233 std::cerr << "GetMirrorFileName: " << mirror_uri_str << std::endl;
234
235 // read sources.list and find match
236 vector<metaIndex *>::const_iterator I;
237 pkgSourceList list;
238 list.ReadMainList();
239 for(I=list.begin(); I != list.end(); I++)
240 {
241 string uristr = (*I)->GetURI();
242 if(Debug)
243 std::cerr << "Checking: " << uristr << std::endl;
244 if(uristr.substr(0,strlen("mirror://")) != string("mirror://"))
245 continue;
246 // find matching uri in sources.list
247 if(mirror_uri_str.substr(0,uristr.size()) == uristr)
248 {
249 if(Debug)
250 std::cerr << "found BaseURI: " << uristr << std::endl;
251 BaseUri = uristr.substr(0,uristr.size()-1);
252 }
253 }
254 // get new file
255 name = _config->FindDir("Dir::State::mirrors") + URItoFileName(BaseUri);
256
257 if(Debug)
258 {
259 cerr << "base-uri: " << BaseUri << endl;
260 cerr << "mirror-file: " << name << endl;
261 }
262 return name;
263 }
264
265 // MirrorMethod::Fetch - Fetch an item /*{{{*/
266 // ---------------------------------------------------------------------
267 /* This adds an item to the pipeline. We keep the pipeline at a fixed
268 depth. */
269 bool MirrorMethod::Fetch(FetchItem *Itm)
270 {
271 if(Debug)
272 clog << "MirrorMethod::Fetch()" << endl;
273
274 // the http method uses Fetch(0) as a way to update the pipeline,
275 // just let it do its work in this case - Fetch() with a valid
276 // Itm will always run before the first Fetch(0)
277 if(Itm == NULL)
278 return HttpMethod::Fetch(Itm);
279
280 // if we don't have the name of the mirror file on disk yet,
281 // calculate it now (can be derived from the uri)
282 if(MirrorFile.empty())
283 MirrorFile = GetMirrorFileName(Itm->Uri);
284
285 // download mirror file once (if we are after index files)
286 if(Itm->IndexFile && !DownloadedMirrorFile)
287 {
288 Clean(_config->FindDir("Dir::State::mirrors"));
289 DownloadMirrorFile(Itm->Uri);
290 }
291
292 if(Mirror.empty()) {
293 if(!InitMirrors()) {
294 // no valid mirror selected, something went wrong downloading
295 // from the master mirror site most likely and there is
296 // no old mirror file availalbe
297 return false;
298 }
299 }
300 if(Debug)
301 clog << "selected mirror: " << Mirror << endl;
302
303
304 for (FetchItem *I = Queue; I != 0; I = I->Next)
305 {
306 if(I->Uri.find("mirror://") != string::npos)
307 I->Uri.replace(0,BaseUri.size(), Mirror);
308 }
309
310 // now run the real fetcher
311 return HttpMethod::Fetch(Itm);
312 };
313
314 void MirrorMethod::Fail(string Err,bool Transient)
315 {
316 // try the next mirror on fail
317 string old_mirror = Mirror;
318 if (SelectNextMirror())
319 {
320 Queue->Uri.replace(0, old_mirror.size(), Mirror);
321 return;
322 }
323
324 // all mirrors failed, so bail out
325 string s;
326 strprintf(s, _("[Mirror: %s]"), Mirror.c_str());
327 SetIP(s);
328
329 if(Queue->Uri.find("http://") != string::npos)
330 Queue->Uri.replace(0,Mirror.size(), BaseUri);
331 pkgAcqMethod::Fail(Err, Transient);
332 }
333
334 void MirrorMethod::URIStart(FetchResult &Res)
335 {
336 if(Queue->Uri.find("http://") != string::npos)
337 Queue->Uri.replace(0,Mirror.size(), BaseUri);
338 pkgAcqMethod::URIStart(Res);
339 }
340
341 void MirrorMethod::URIDone(FetchResult &Res,FetchResult *Alt)
342 {
343 if(Queue->Uri.find("http://") != string::npos)
344 Queue->Uri.replace(0,Mirror.size(), BaseUri);
345 pkgAcqMethod::URIDone(Res, Alt);
346 }
347
348
349 int main()
350 {
351 setlocale(LC_ALL, "");
352
353 MirrorMethod Mth;
354
355 return Mth.Loop();
356 }
357
358