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 /*{{{*/
13 #include <apt-pkg/aptconfiguration.h>
14 #include <apt-pkg/fileutl.h>
15 #include <apt-pkg/acquire-method.h>
16 #include <apt-pkg/acquire-item.h>
17 #include <apt-pkg/acquire.h>
18 #include <apt-pkg/error.h>
19 #include <apt-pkg/hashes.h>
20 #include <apt-pkg/sourcelist.h>
29 #include <sys/types.h>
30 #include <sys/utsname.h>
43 * - works with http (only!)
44 * - always picks the first mirror from the list
45 * - call out to problem reporting script
46 * - supports "deb mirror://host/path/to/mirror-list/// dist component"
47 * - uses pkgAcqMethod::FailReason() to have a string representation
48 * of the failure that is also send to LP
51 * - deal with runing as non-root because we can't write to the lists
52 dir then -> use the cached mirror file
53 * - better method to download than having a pkgAcquire interface here
54 * and better error handling there!
55 * - support more than http
59 MirrorMethod::MirrorMethod()
60 : HttpMethod(), DownloadedMirrorFile(false), Debug(false)
64 // HttpMethod::Configuration - Handle a configuration message /*{{{*/
65 // ---------------------------------------------------------------------
66 /* We stash the desired pipeline depth */
67 bool MirrorMethod::Configuration(string Message
)
69 if (pkgAcqMethod::Configuration(Message
) == false)
71 Debug
= _config
->FindB("Debug::Acquire::mirror",false);
77 // clean the mirrors dir based on ttl information
78 bool MirrorMethod::Clean(string Dir
)
80 vector
<metaIndex
*>::const_iterator I
;
83 clog
<< "MirrorMethod::Clean(): " << Dir
<< endl
;
86 return _error
->Error("will not clean: '/'");
92 DIR *D
= opendir(Dir
.c_str());
94 return _error
->Errno("opendir",_("Unable to read %s"),Dir
.c_str());
96 string StartDir
= SafeGetCWD();
97 if (chdir(Dir
.c_str()) != 0)
100 return _error
->Errno("chdir",_("Unable to change to %s"),Dir
.c_str());
103 for (struct dirent
*Dir
= readdir(D
); Dir
!= 0; Dir
= readdir(D
))
106 if (strcmp(Dir
->d_name
,"lock") == 0 ||
107 strcmp(Dir
->d_name
,"partial") == 0 ||
108 strcmp(Dir
->d_name
,".") == 0 ||
109 strcmp(Dir
->d_name
,"..") == 0)
112 // see if we have that uri
113 for(I
=list
.begin(); I
!= list
.end(); ++I
)
115 string uri
= (*I
)->GetURI();
116 if(uri
.find("mirror://") != 0)
118 string BaseUri
= uri
.substr(0,uri
.size()-1);
119 if (URItoFileName(BaseUri
) == Dir
->d_name
)
122 // nothing found, nuke it
127 chdir(StartDir
.c_str());
133 bool MirrorMethod::DownloadMirrorFile(string mirror_uri_str
)
135 // not that great to use pkgAcquire here, but we do not have
136 // any other way right now
137 string fetch
= BaseUri
;
138 fetch
.replace(0,strlen("mirror://"),"http://");
140 #if 0 // no need for this, the getArchitectures() will also include the main
142 // append main architecture
143 fetch
+= "?arch=" + _config
->Find("Apt::Architecture");
146 // append all architectures
147 std::vector
<std::string
> vec
= APT::Configuration::getArchitectures();
148 for (std::vector
<std::string
>::const_iterator I
= vec
.begin();
150 if (I
== vec
.begin())
151 fetch
+= "?arch" + (*I
);
153 fetch
+= "&arch=" + (*I
);
155 // append the dist as a query string
157 fetch
+= "&dist=" + Dist
;
160 clog
<< "MirrorMethod::DownloadMirrorFile(): '" << fetch
<< "'"
161 << " to " << MirrorFile
<< endl
;
164 new pkgAcqFile(&Fetcher
, fetch
, "", 0, "", "", "", MirrorFile
);
165 bool res
= (Fetcher
.Run() == pkgAcquire::Continue
);
167 DownloadedMirrorFile
= true;
168 chmod(MirrorFile
.c_str(), 0644);
173 clog
<< "MirrorMethod::DownloadMirrorFile() success: " << res
<< endl
;
178 // Randomizes the lines in the mirror file, this is used so that
179 // we spread the load on the mirrors evenly
180 bool MirrorMethod::RandomizeMirrorFile(string mirror_file
)
182 vector
<string
> content
;
185 if (!FileExists(mirror_file
))
189 ifstream
in(mirror_file
.c_str());
190 while ( !in
.eof() ) {
192 content
.push_back(line
);
195 // we want the file to be random for each different machine, but also
196 // "stable" on the same machine. this is to avoid running into out-of-sync
197 // issues (i.e. Release/Release.gpg different on each mirror)
200 if(uname(&buf
) == 0) {
201 for(i
=0,seed
=1; buf
.nodename
[i
] != 0; i
++) {
202 seed
= seed
* 31 + buf
.nodename
[i
];
206 random_shuffle(content
.begin(), content
.end());
209 ofstream
out(mirror_file
.c_str());
210 while ( !content
.empty()) {
211 line
= content
.back();
219 /* convert a the Queue->Uri back to the mirror base uri and look
220 * at all mirrors we have for this, this is needed as queue->uri
221 * may point to different mirrors (if TryNextMirror() was run)
223 void MirrorMethod::CurrentQueueUriToMirror()
225 // already in mirror:// style so nothing to do
226 if(Queue
->Uri
.find("mirror://") == 0)
229 // find current mirror and select next one
230 for (vector
<string
>::const_iterator mirror
= AllMirrors
.begin();
231 mirror
!= AllMirrors
.end(); ++mirror
)
233 if (Queue
->Uri
.find(*mirror
) == 0)
235 Queue
->Uri
.replace(0, mirror
->length(), BaseUri
);
239 _error
->Error("Internal error: Failed to convert %s back to %s",
240 Queue
->Uri
.c_str(), BaseUri
.c_str());
243 bool MirrorMethod::TryNextMirror()
245 // find current mirror and select next one
246 for (vector
<string
>::const_iterator mirror
= AllMirrors
.begin();
247 mirror
!= AllMirrors
.end(); ++mirror
)
249 if (Queue
->Uri
.find(*mirror
) != 0)
252 vector
<string
>::const_iterator nextmirror
= mirror
+ 1;
253 if (nextmirror
== AllMirrors
.end())
255 Queue
->Uri
.replace(0, mirror
->length(), *nextmirror
);
257 clog
<< "TryNextMirror: " << Queue
->Uri
<< endl
;
260 UsedMirror
= *nextmirror
;
261 Log("Switching mirror");
266 clog
<< "TryNextMirror could not find another mirror to try" << endl
;
271 bool MirrorMethod::InitMirrors()
273 // if we do not have a MirrorFile, fallback
274 if(!FileExists(MirrorFile
))
276 // FIXME: fallback to a default mirror here instead
277 // and provide a config option to define that default
278 return _error
->Error(_("No mirror file '%s' found "), MirrorFile
.c_str());
281 if (access(MirrorFile
.c_str(), R_OK
) != 0)
283 // FIXME: fallback to a default mirror here instead
284 // and provide a config option to define that default
285 return _error
->Error(_("Can not read mirror file '%s'"), MirrorFile
.c_str());
288 // FIXME: make the mirror selection more clever, do not
289 // just use the first one!
290 // BUT: we can not make this random, the mirror has to be
291 // stable accross session, because otherwise we can
292 // get into sync issues (got indexfiles from mirror A,
293 // but packages from mirror B - one might be out of date etc)
294 ifstream
in(MirrorFile
.c_str());
300 // ignore lines that start with #
301 if (s
.find("#") == 0)
303 // ignore empty lines
306 // ignore non http lines
307 if (s
.find("http://") != 0)
310 AllMirrors
.push_back(s
);
312 Mirror
= AllMirrors
[0];
317 string
MirrorMethod::GetMirrorFileName(string mirror_uri_str
)
320 - a mirror_uri_str looks like this:
321 mirror://people.ubuntu.com/~mvo/apt/mirror/mirrors/dists/feisty/Release.gpg
323 - the matching source.list entry
324 deb mirror://people.ubuntu.com/~mvo/apt/mirror/mirrors feisty main
326 - we actually want to go after:
327 http://people.ubuntu.com/~mvo/apt/mirror/mirrors
329 And we need to save the BaseUri for later:
330 - mirror://people.ubuntu.com/~mvo/apt/mirror/mirrors
332 FIXME: what if we have two similar prefixes?
333 mirror://people.ubuntu.com/~mvo/mirror
334 mirror://people.ubuntu.com/~mvo/mirror2
335 then mirror_uri_str looks like:
336 mirror://people.ubuntu.com/~mvo/apt/mirror/dists/feisty/Release.gpg
337 mirror://people.ubuntu.com/~mvo/apt/mirror2/dists/feisty/Release.gpg
338 we search sources.list and find:
339 mirror://people.ubuntu.com/~mvo/apt/mirror
340 in both cases! So we need to apply some domain knowledge here :( and
341 check for /dists/ or /Release.gpg as suffixes
345 std::cerr
<< "GetMirrorFileName: " << mirror_uri_str
<< std::endl
;
347 // read sources.list and find match
348 vector
<metaIndex
*>::const_iterator I
;
351 for(I
=list
.begin(); I
!= list
.end(); ++I
)
353 string uristr
= (*I
)->GetURI();
355 std::cerr
<< "Checking: " << uristr
<< std::endl
;
356 if(uristr
.substr(0,strlen("mirror://")) != string("mirror://"))
358 // find matching uri in sources.list
359 if(mirror_uri_str
.substr(0,uristr
.size()) == uristr
)
362 std::cerr
<< "found BaseURI: " << uristr
<< std::endl
;
363 BaseUri
= uristr
.substr(0,uristr
.size()-1);
364 Dist
= (*I
)->GetDist();
368 name
= _config
->FindDir("Dir::State::mirrors") + URItoFileName(BaseUri
);
372 cerr
<< "base-uri: " << BaseUri
<< endl
;
373 cerr
<< "mirror-file: " << name
<< endl
;
378 // MirrorMethod::Fetch - Fetch an item /*{{{*/
379 // ---------------------------------------------------------------------
380 /* This adds an item to the pipeline. We keep the pipeline at a fixed
382 bool MirrorMethod::Fetch(FetchItem
*Itm
)
385 clog
<< "MirrorMethod::Fetch()" << endl
;
387 // the http method uses Fetch(0) as a way to update the pipeline,
388 // just let it do its work in this case - Fetch() with a valid
389 // Itm will always run before the first Fetch(0)
391 return HttpMethod::Fetch(Itm
);
393 // if we don't have the name of the mirror file on disk yet,
394 // calculate it now (can be derived from the uri)
395 if(MirrorFile
.empty())
396 MirrorFile
= GetMirrorFileName(Itm
->Uri
);
398 // download mirror file once (if we are after index files)
399 if(Itm
->IndexFile
&& !DownloadedMirrorFile
)
401 Clean(_config
->FindDir("Dir::State::mirrors"));
402 if (DownloadMirrorFile(Itm
->Uri
))
403 RandomizeMirrorFile(MirrorFile
);
406 if(AllMirrors
.empty()) {
408 // no valid mirror selected, something went wrong downloading
409 // from the master mirror site most likely and there is
410 // no old mirror file availalbe
415 if(Itm
->Uri
.find("mirror://") != string::npos
)
416 Itm
->Uri
.replace(0,BaseUri
.size(), Mirror
);
419 clog
<< "Fetch: " << Itm
->Uri
<< endl
<< endl
;
421 // now run the real fetcher
422 return HttpMethod::Fetch(Itm
);
425 void MirrorMethod::Fail(string Err
,bool Transient
)
427 // FIXME: TryNextMirror is not ideal for indexfile as we may
428 // run into auth issues
431 clog
<< "Failure to get " << Queue
->Uri
<< endl
;
433 // try the next mirror on fail (if its not a expected failure,
434 // e.g. translations are ok to ignore)
435 if (!Queue
->FailIgnore
&& TryNextMirror())
438 // all mirrors failed, so bail out
440 strprintf(s
, _("[Mirror: %s]"), Mirror
.c_str());
443 CurrentQueueUriToMirror();
444 pkgAcqMethod::Fail(Err
, Transient
);
447 void MirrorMethod::URIStart(FetchResult
&Res
)
449 CurrentQueueUriToMirror();
450 pkgAcqMethod::URIStart(Res
);
453 void MirrorMethod::URIDone(FetchResult
&Res
,FetchResult
*Alt
)
455 CurrentQueueUriToMirror();
456 pkgAcqMethod::URIDone(Res
, Alt
);
462 setlocale(LC_ALL
, "");