1 // -*- mode: cpp; mode: fold -*-
3 // $Id: mirror.cc,v 1.59 2004/05/08 19:42:35 mdz Exp $
4 /* ######################################################################
6 Mirror Aquire Method - This is the Mirror aquire method for APT.
8 ##################################################################### */
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>
26 #include <sys/types.h>
27 #include <sys/utsname.h>
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
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
56 MirrorMethod::MirrorMethod()
57 : HttpMethod(), DownloadedMirrorFile(false)
61 // HttpMethod::Configuration - Handle a configuration message /*{{{*/
62 // ---------------------------------------------------------------------
63 /* We stash the desired pipeline depth */
64 bool MirrorMethod::Configuration(string Message
)
66 if (pkgAcqMethod::Configuration(Message
) == false)
68 Debug
= _config
->FindB("Debug::Acquire::mirror",false);
74 // clean the mirrors dir based on ttl information
75 bool MirrorMethod::Clean(string Dir
)
77 vector
<metaIndex
*>::const_iterator I
;
80 clog
<< "MirrorMethod::Clean(): " << Dir
<< endl
;
83 return _error
->Error("will not clean: '/'");
89 DIR *D
= opendir(Dir
.c_str());
91 return _error
->Errno("opendir",_("Unable to read %s"),Dir
.c_str());
93 string StartDir
= SafeGetCWD();
94 if (chdir(Dir
.c_str()) != 0)
97 return _error
->Errno("chdir",_("Unable to change to %s"),Dir
.c_str());
100 for (struct dirent
*Dir
= readdir(D
); Dir
!= 0; Dir
= readdir(D
))
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)
109 // see if we have that uri
110 for(I
=list
.begin(); I
!= list
.end(); I
++)
112 string uri
= (*I
)->GetURI();
113 if(uri
.find("mirror://") != 0)
115 string BaseUri
= uri
.substr(0,uri
.size()-1);
116 if (URItoFileName(BaseUri
) == Dir
->d_name
)
119 // nothing found, nuke it
124 chdir(StartDir
.c_str());
130 bool MirrorMethod::DownloadMirrorFile(string mirror_uri_str
)
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://");
138 clog
<< "MirrorMethod::DownloadMirrorFile(): '" << fetch
<< "'"
139 << " to " << MirrorFile
<< endl
;
142 new pkgAcqFile(&Fetcher
, fetch
, "", 0, "", "", "", MirrorFile
);
143 bool res
= (Fetcher
.Run() == pkgAcquire::Continue
);
145 DownloadedMirrorFile
= true;
149 clog
<< "MirrorMethod::DownloadMirrorFile() success: " << res
<< endl
;
154 // Randomizes the lines in the mirror file, this is used so that
155 // we spread the load on the mirrors evenly
156 bool MirrorMethod::RandomizeMirrorFile(string mirror_file
)
158 vector
<string
> content
;
162 ifstream
in(mirror_file
.c_str());
163 while ( !in
.eof() ) {
165 content
.push_back(line
);
168 // we want the file to be random for each different machine, but also
169 // "stable" on the same machine. this is to avoid running into out-of-sync
170 // issues (i.e. Release/Release.gpg different on each mirror)
173 if(uname(&buf
) == 0) {
174 for(i
=0,seed
=1; buf
.nodename
[i
] != 0; i
++) {
175 seed
= seed
* 31 + buf
.nodename
[i
];
179 random_shuffle(content
.begin(), content
.end());
182 ofstream
out(mirror_file
.c_str());
183 while ( !content
.empty()) {
184 line
= content
.back();
192 /* convert a the Queue->Uri back to the mirror base uri and look
193 * at all mirrors we have for this, this is needed as queue->uri
194 * may point to different mirrors (if TryNextMirror() was run)
196 void MirrorMethod::CurrentQueueUriToMirror()
198 // already in mirror:// style so nothing to do
199 if(Queue
->Uri
.find("mirror://") == 0)
202 // find current mirror and select next one
203 for (vector
<string
>::const_iterator mirror
= AllMirrors
.begin();
204 mirror
!= AllMirrors
.end(); ++mirror
)
206 if (Queue
->Uri
.find(*mirror
) == 0)
208 Queue
->Uri
.replace(0, mirror
->length(), BaseUri
);
212 _error
->Error("Internal error: Failed to convert %s back to %s",
213 Queue
->Uri
.c_str(), BaseUri
.c_str());
216 bool MirrorMethod::TryNextMirror()
218 // find current mirror and select next one
219 for (vector
<string
>::const_iterator mirror
= AllMirrors
.begin();
220 mirror
!= AllMirrors
.end(); ++mirror
)
222 if (Queue
->Uri
.find(*mirror
) != 0)
225 vector
<string
>::const_iterator nextmirror
= mirror
+ 1;
226 if (nextmirror
== AllMirrors
.end())
228 Queue
->Uri
.replace(0, mirror
->length(), *nextmirror
);
230 clog
<< "TryNextMirror: " << Queue
->Uri
<< endl
;
233 UsedMirror
= *nextmirror
;
234 Log("Switching mirror");
239 clog
<< "TryNextMirror could not find another mirror to try" << endl
;
244 bool MirrorMethod::InitMirrors()
246 // if we do not have a MirrorFile, fallback
247 if(!FileExists(MirrorFile
))
249 // FIXME: fallback to a default mirror here instead
250 // and provide a config option to define that default
251 return _error
->Error(_("No mirror file '%s' found "), MirrorFile
.c_str());
254 // FIXME: make the mirror selection more clever, do not
255 // just use the first one!
256 // BUT: we can not make this random, the mirror has to be
257 // stable accross session, because otherwise we can
258 // get into sync issues (got indexfiles from mirror A,
259 // but packages from mirror B - one might be out of date etc)
260 ifstream
in(MirrorFile
.c_str());
266 AllMirrors
.push_back(s
);
268 Mirror
= AllMirrors
[0];
273 string
MirrorMethod::GetMirrorFileName(string mirror_uri_str
)
276 - a mirror_uri_str looks like this:
277 mirror://people.ubuntu.com/~mvo/apt/mirror/mirrors/dists/feisty/Release.gpg
279 - the matching source.list entry
280 deb mirror://people.ubuntu.com/~mvo/apt/mirror/mirrors feisty main
282 - we actually want to go after:
283 http://people.ubuntu.com/~mvo/apt/mirror/mirrors
285 And we need to save the BaseUri for later:
286 - mirror://people.ubuntu.com/~mvo/apt/mirror/mirrors
288 FIXME: what if we have two similar prefixes?
289 mirror://people.ubuntu.com/~mvo/mirror
290 mirror://people.ubuntu.com/~mvo/mirror2
291 then mirror_uri_str looks like:
292 mirror://people.ubuntu.com/~mvo/apt/mirror/dists/feisty/Release.gpg
293 mirror://people.ubuntu.com/~mvo/apt/mirror2/dists/feisty/Release.gpg
294 we search sources.list and find:
295 mirror://people.ubuntu.com/~mvo/apt/mirror
296 in both cases! So we need to apply some domain knowledge here :( and
297 check for /dists/ or /Release.gpg as suffixes
301 std::cerr
<< "GetMirrorFileName: " << mirror_uri_str
<< std::endl
;
303 // read sources.list and find match
304 vector
<metaIndex
*>::const_iterator I
;
307 for(I
=list
.begin(); I
!= list
.end(); I
++)
309 string uristr
= (*I
)->GetURI();
311 std::cerr
<< "Checking: " << uristr
<< std::endl
;
312 if(uristr
.substr(0,strlen("mirror://")) != string("mirror://"))
314 // find matching uri in sources.list
315 if(mirror_uri_str
.substr(0,uristr
.size()) == uristr
)
318 std::cerr
<< "found BaseURI: " << uristr
<< std::endl
;
319 BaseUri
= uristr
.substr(0,uristr
.size()-1);
323 name
= _config
->FindDir("Dir::State::mirrors") + URItoFileName(BaseUri
);
327 cerr
<< "base-uri: " << BaseUri
<< endl
;
328 cerr
<< "mirror-file: " << name
<< endl
;
333 // MirrorMethod::Fetch - Fetch an item /*{{{*/
334 // ---------------------------------------------------------------------
335 /* This adds an item to the pipeline. We keep the pipeline at a fixed
337 bool MirrorMethod::Fetch(FetchItem
*Itm
)
340 clog
<< "MirrorMethod::Fetch()" << endl
;
342 // the http method uses Fetch(0) as a way to update the pipeline,
343 // just let it do its work in this case - Fetch() with a valid
344 // Itm will always run before the first Fetch(0)
346 return HttpMethod::Fetch(Itm
);
348 // if we don't have the name of the mirror file on disk yet,
349 // calculate it now (can be derived from the uri)
350 if(MirrorFile
.empty())
351 MirrorFile
= GetMirrorFileName(Itm
->Uri
);
353 // download mirror file once (if we are after index files)
354 if(Itm
->IndexFile
&& !DownloadedMirrorFile
)
356 Clean(_config
->FindDir("Dir::State::mirrors"));
357 DownloadMirrorFile(Itm
->Uri
);
358 RandomizeMirrorFile(MirrorFile
);
361 if(AllMirrors
.empty()) {
363 // no valid mirror selected, something went wrong downloading
364 // from the master mirror site most likely and there is
365 // no old mirror file availalbe
370 if(Itm
->Uri
.find("mirror://") != string::npos
)
371 Itm
->Uri
.replace(0,BaseUri
.size(), Mirror
);
374 clog
<< "Fetch: " << Itm
->Uri
<< endl
<< endl
;
376 // now run the real fetcher
377 return HttpMethod::Fetch(Itm
);
380 void MirrorMethod::Fail(string Err
,bool Transient
)
382 // FIXME: TryNextMirror is not ideal for indexfile as we may
383 // run into auth issues
386 clog
<< "Failure to get " << Queue
->Uri
<< endl
;
388 // try the next mirror on fail (if its not a expected failure,
389 // e.g. translations are ok to ignore)
390 if (!Queue
->FailIgnore
&& TryNextMirror())
393 // all mirrors failed, so bail out
395 strprintf(s
, _("[Mirror: %s]"), Mirror
.c_str());
398 CurrentQueueUriToMirror();
399 pkgAcqMethod::Fail(Err
, Transient
);
402 void MirrorMethod::URIStart(FetchResult
&Res
)
404 CurrentQueueUriToMirror();
405 pkgAcqMethod::URIStart(Res
);
408 void MirrorMethod::URIDone(FetchResult
&Res
,FetchResult
*Alt
)
410 CurrentQueueUriToMirror();
411 pkgAcqMethod::URIDone(Res
, Alt
);
417 setlocale(LC_ALL
, "");