1 // -*- mode: cpp; mode: fold -*-
3 // $Id: mirror.cc,v 1.59 2004/05/08 19:42:35 mdz Exp $
4 /* ######################################################################
6 Mirror Acquire Method - This is the Mirror acquire method for APT.
8 ##################################################################### */
10 // Include Files /*{{{*/
13 #include <apt-pkg/aptconfiguration.h>
14 #include <apt-pkg/fileutl.h>
15 #include <apt-pkg/acquire-item.h>
16 #include <apt-pkg/acquire.h>
17 #include <apt-pkg/error.h>
18 #include <apt-pkg/sourcelist.h>
19 #include <apt-pkg/configuration.h>
20 #include <apt-pkg/metaindex.h>
21 #include <apt-pkg/strutl.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 running 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("mirror"), 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
= DebugEnabled();
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
,"lost+found") == 0 ||
109 strcmp(Dir
->d_name
,".") == 0 ||
110 strcmp(Dir
->d_name
,"..") == 0)
113 // see if we have that uri
114 for(I
=list
.begin(); I
!= list
.end(); ++I
)
116 string uri
= (*I
)->GetURI();
117 if(uri
.compare(0, strlen("mirror://"), "mirror://") != 0)
119 string BaseUri
= uri
.substr(0,uri
.size()-1);
120 if (URItoFileName(BaseUri
) == Dir
->d_name
)
123 // nothing found, nuke it
125 RemoveFile("mirror", Dir
->d_name
);
129 if (chdir(StartDir
.c_str()) != 0)
130 return _error
->Errno("chdir",_("Unable to change to %s"),StartDir
.c_str());
135 bool MirrorMethod::DownloadMirrorFile(string
/*mirror_uri_str*/)
137 // not that great to use pkgAcquire here, but we do not have
138 // any other way right now
139 string fetch
= BaseUri
;
140 fetch
.replace(0,strlen("mirror://"),"http://");
142 #if 0 // no need for this, the getArchitectures() will also include the main
144 // append main architecture
145 fetch
+= "?arch=" + _config
->Find("Apt::Architecture");
148 // append all architectures
149 std::vector
<std::string
> vec
= APT::Configuration::getArchitectures();
150 for (std::vector
<std::string
>::const_iterator I
= vec
.begin();
152 if (I
== vec
.begin())
153 fetch
+= "?arch=" + (*I
);
155 fetch
+= "&arch=" + (*I
);
157 // append the dist as a query string
159 fetch
+= "&dist=" + Dist
;
162 clog
<< "MirrorMethod::DownloadMirrorFile(): '" << fetch
<< "'"
163 << " to " << MirrorFile
<< endl
;
166 new pkgAcqFile(&Fetcher
, fetch
, "", 0, "", "", "", MirrorFile
);
167 bool res
= (Fetcher
.Run() == pkgAcquire::Continue
);
169 DownloadedMirrorFile
= true;
170 chmod(MirrorFile
.c_str(), 0644);
175 clog
<< "MirrorMethod::DownloadMirrorFile() success: " << res
<< endl
;
180 // Randomizes the lines in the mirror file, this is used so that
181 // we spread the load on the mirrors evenly
182 bool MirrorMethod::RandomizeMirrorFile(string mirror_file
)
184 vector
<string
> content
;
187 if (!FileExists(mirror_file
))
191 ifstream
in(mirror_file
.c_str());
192 while ( !in
.eof() ) {
194 content
.push_back(line
);
197 // we want the file to be random for each different machine, but also
198 // "stable" on the same machine. this is to avoid running into out-of-sync
199 // issues (i.e. Release/Release.gpg different on each mirror)
202 if(uname(&buf
) == 0) {
203 for(int i
=0,seed
=1; buf
.nodename
[i
] != 0; ++i
) {
204 seed
= seed
* 31 + buf
.nodename
[i
];
208 random_shuffle(content
.begin(), content
.end());
211 ofstream
out(mirror_file
.c_str());
212 while ( !content
.empty()) {
213 line
= content
.back();
221 /* convert a the Queue->Uri back to the mirror base uri and look
222 * at all mirrors we have for this, this is needed as queue->uri
223 * may point to different mirrors (if TryNextMirror() was run)
225 void MirrorMethod::CurrentQueueUriToMirror()
227 // already in mirror:// style so nothing to do
228 if(Queue
->Uri
.find("mirror://") == 0)
231 // find current mirror and select next one
232 for (vector
<string
>::const_iterator mirror
= AllMirrors
.begin();
233 mirror
!= AllMirrors
.end(); ++mirror
)
235 if (Queue
->Uri
.find(*mirror
) == 0)
237 Queue
->Uri
.replace(0, mirror
->length(), BaseUri
);
241 _error
->Error("Internal error: Failed to convert %s back to %s",
242 Queue
->Uri
.c_str(), BaseUri
.c_str());
245 bool MirrorMethod::TryNextMirror()
247 // find current mirror and select next one
248 for (vector
<string
>::const_iterator mirror
= AllMirrors
.begin();
249 mirror
!= AllMirrors
.end(); ++mirror
)
251 if (Queue
->Uri
.find(*mirror
) != 0)
254 vector
<string
>::const_iterator nextmirror
= mirror
+ 1;
255 if (nextmirror
== AllMirrors
.end())
257 Queue
->Uri
.replace(0, mirror
->length(), *nextmirror
);
259 clog
<< "TryNextMirror: " << Queue
->Uri
<< endl
;
262 UsedMirror
= *nextmirror
;
263 Log("Switching mirror");
268 clog
<< "TryNextMirror could not find another mirror to try" << endl
;
273 bool MirrorMethod::InitMirrors()
275 // if we do not have a MirrorFile, fallback
276 if(!FileExists(MirrorFile
))
278 // FIXME: fallback to a default mirror here instead
279 // and provide a config option to define that default
280 return _error
->Error(_("No mirror file '%s' found "), MirrorFile
.c_str());
283 if (access(MirrorFile
.c_str(), R_OK
) != 0)
285 // FIXME: fallback to a default mirror here instead
286 // and provide a config option to define that default
287 return _error
->Error(_("Can not read mirror file '%s'"), MirrorFile
.c_str());
290 // FIXME: make the mirror selection more clever, do not
291 // just use the first one!
292 // BUT: we can not make this random, the mirror has to be
293 // stable across session, because otherwise we can
294 // get into sync issues (got indexfiles from mirror A,
295 // but packages from mirror B - one might be out of date etc)
296 ifstream
in(MirrorFile
.c_str());
302 // ignore lines that start with #
303 if (s
.find("#") == 0)
305 // ignore empty lines
308 // ignore non http lines
309 if (s
.compare(0, strlen("http://"), "http://") != 0)
312 AllMirrors
.push_back(s
);
314 if (AllMirrors
.empty()) {
315 return _error
->Error(_("No entry found in mirror file '%s'"), MirrorFile
.c_str());
317 Mirror
= AllMirrors
[0];
322 string
MirrorMethod::GetMirrorFileName(string mirror_uri_str
)
325 - a mirror_uri_str looks like this:
326 mirror://people.ubuntu.com/~mvo/apt/mirror/mirrors/dists/feisty/Release.gpg
328 - the matching source.list entry
329 deb mirror://people.ubuntu.com/~mvo/apt/mirror/mirrors feisty main
331 - we actually want to go after:
332 http://people.ubuntu.com/~mvo/apt/mirror/mirrors
334 And we need to save the BaseUri for later:
335 - mirror://people.ubuntu.com/~mvo/apt/mirror/mirrors
337 FIXME: what if we have two similar prefixes?
338 mirror://people.ubuntu.com/~mvo/mirror
339 mirror://people.ubuntu.com/~mvo/mirror2
340 then mirror_uri_str looks like:
341 mirror://people.ubuntu.com/~mvo/apt/mirror/dists/feisty/Release.gpg
342 mirror://people.ubuntu.com/~mvo/apt/mirror2/dists/feisty/Release.gpg
343 we search sources.list and find:
344 mirror://people.ubuntu.com/~mvo/apt/mirror
345 in both cases! So we need to apply some domain knowledge here :( and
346 check for /dists/ or /Release.gpg as suffixes
350 std::cerr
<< "GetMirrorFileName: " << mirror_uri_str
<< std::endl
;
352 // read sources.list and find match
353 vector
<metaIndex
*>::const_iterator I
;
356 for(I
=list
.begin(); I
!= list
.end(); ++I
)
358 string uristr
= (*I
)->GetURI();
360 std::cerr
<< "Checking: " << uristr
<< std::endl
;
361 if(uristr
.substr(0,strlen("mirror://")) != string("mirror://"))
363 // find matching uri in sources.list
364 if(mirror_uri_str
.substr(0,uristr
.size()) == uristr
)
367 std::cerr
<< "found BaseURI: " << uristr
<< std::endl
;
368 BaseUri
= uristr
.substr(0,uristr
.size()-1);
369 Dist
= (*I
)->GetDist();
373 name
= _config
->FindDir("Dir::State::mirrors") + URItoFileName(BaseUri
);
377 cerr
<< "base-uri: " << BaseUri
<< endl
;
378 cerr
<< "mirror-file: " << name
<< endl
;
383 // MirrorMethod::Fetch - Fetch an item /*{{{*/
384 // ---------------------------------------------------------------------
385 /* This adds an item to the pipeline. We keep the pipeline at a fixed
387 bool MirrorMethod::Fetch(FetchItem
*Itm
)
390 clog
<< "MirrorMethod::Fetch()" << endl
;
392 // the http method uses Fetch(0) as a way to update the pipeline,
393 // just let it do its work in this case - Fetch() with a valid
394 // Itm will always run before the first Fetch(0)
396 return HttpMethod::Fetch(Itm
);
398 // if we don't have the name of the mirror file on disk yet,
399 // calculate it now (can be derived from the uri)
400 if(MirrorFile
.empty())
401 MirrorFile
= GetMirrorFileName(Itm
->Uri
);
403 // download mirror file once (if we are after index files)
404 if(Itm
->IndexFile
&& !DownloadedMirrorFile
)
406 Clean(_config
->FindDir("Dir::State::mirrors"));
407 if (DownloadMirrorFile(Itm
->Uri
))
408 RandomizeMirrorFile(MirrorFile
);
411 if(AllMirrors
.empty()) {
413 // no valid mirror selected, something went wrong downloading
414 // from the master mirror site most likely and there is
415 // no old mirror file availalbe
420 if(Itm
->Uri
.find("mirror://") != string::npos
)
421 Itm
->Uri
.replace(0,BaseUri
.size(), Mirror
);
424 clog
<< "Fetch: " << Itm
->Uri
<< endl
<< endl
;
426 // now run the real fetcher
427 return HttpMethod::Fetch(Itm
);
430 void MirrorMethod::Fail(string Err
,bool Transient
)
432 // FIXME: TryNextMirror is not ideal for indexfile as we may
433 // run into auth issues
436 clog
<< "Failure to get " << Queue
->Uri
<< endl
;
438 // try the next mirror on fail (if its not a expected failure,
439 // e.g. translations are ok to ignore)
440 if (!Queue
->FailIgnore
&& TryNextMirror())
443 // all mirrors failed, so bail out
445 strprintf(s
, _("[Mirror: %s]"), Mirror
.c_str());
448 CurrentQueueUriToMirror();
449 pkgAcqMethod::Fail(Err
, Transient
);
452 void MirrorMethod::URIStart(FetchResult
&Res
)
454 CurrentQueueUriToMirror();
455 pkgAcqMethod::URIStart(Res
);
458 void MirrorMethod::URIDone(FetchResult
&Res
,FetchResult
*Alt
)
460 CurrentQueueUriToMirror();
461 pkgAcqMethod::URIDone(Res
, Alt
);
467 return MirrorMethod().Loop();