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/aptconfiguration.h>
12 #include <apt-pkg/fileutl.h>
13 #include <apt-pkg/acquire-method.h>
14 #include <apt-pkg/acquire-item.h>
15 #include <apt-pkg/acquire.h>
16 #include <apt-pkg/error.h>
17 #include <apt-pkg/hashes.h>
18 #include <apt-pkg/sourcelist.h>
27 #include <sys/types.h>
28 #include <sys/utsname.h>
41 * - works with http (only!)
42 * - always picks the first mirror from the list
43 * - call out to problem reporting script
44 * - supports "deb mirror://host/path/to/mirror-list/// dist component"
45 * - uses pkgAcqMethod::FailReason() to have a string representation
46 * of the failure that is also send to LP
49 * - deal with runing as non-root because we can't write to the lists
50 dir then -> use the cached mirror file
51 * - better method to download than having a pkgAcquire interface here
52 * and better error handling there!
53 * - support more than http
57 MirrorMethod::MirrorMethod()
58 : HttpMethod(), DownloadedMirrorFile(false)
62 // HttpMethod::Configuration - Handle a configuration message /*{{{*/
63 // ---------------------------------------------------------------------
64 /* We stash the desired pipeline depth */
65 bool MirrorMethod::Configuration(string Message
)
67 if (pkgAcqMethod::Configuration(Message
) == false)
69 Debug
= _config
->FindB("Debug::Acquire::mirror",false);
75 // clean the mirrors dir based on ttl information
76 bool MirrorMethod::Clean(string Dir
)
78 vector
<metaIndex
*>::const_iterator I
;
81 clog
<< "MirrorMethod::Clean(): " << Dir
<< endl
;
84 return _error
->Error("will not clean: '/'");
90 DIR *D
= opendir(Dir
.c_str());
92 return _error
->Errno("opendir",_("Unable to read %s"),Dir
.c_str());
94 string StartDir
= SafeGetCWD();
95 if (chdir(Dir
.c_str()) != 0)
98 return _error
->Errno("chdir",_("Unable to change to %s"),Dir
.c_str());
101 for (struct dirent
*Dir
= readdir(D
); Dir
!= 0; Dir
= readdir(D
))
104 if (strcmp(Dir
->d_name
,"lock") == 0 ||
105 strcmp(Dir
->d_name
,"partial") == 0 ||
106 strcmp(Dir
->d_name
,".") == 0 ||
107 strcmp(Dir
->d_name
,"..") == 0)
110 // see if we have that uri
111 for(I
=list
.begin(); I
!= list
.end(); I
++)
113 string uri
= (*I
)->GetURI();
114 if(uri
.find("mirror://") != 0)
116 string BaseUri
= uri
.substr(0,uri
.size()-1);
117 if (URItoFileName(BaseUri
) == Dir
->d_name
)
120 // nothing found, nuke it
125 chdir(StartDir
.c_str());
131 bool MirrorMethod::DownloadMirrorFile(string mirror_uri_str
)
133 // not that great to use pkgAcquire here, but we do not have
134 // any other way right now
135 string fetch
= BaseUri
;
136 fetch
.replace(0,strlen("mirror://"),"http://");
138 #if 0 // no need for this, the getArchitectures() will also include the main
140 // append main architecture
141 fetch
+= "?arch=" + _config
->Find("Apt::Architecture");
144 // append all architectures
145 std::vector
<std::string
> vec
= APT::Configuration::getArchitectures();
146 for (std::vector
<std::string
>::const_iterator I
= vec
.begin();
148 if (I
== vec
.begin())
149 fetch
+= "?arch" + (*I
);
151 fetch
+= "&arch=" + (*I
);
153 // append the dist as a query string
155 fetch
+= "&dist=" + Dist
;
158 clog
<< "MirrorMethod::DownloadMirrorFile(): '" << fetch
<< "'"
159 << " to " << MirrorFile
<< endl
;
162 new pkgAcqFile(&Fetcher
, fetch
, "", 0, "", "", "", MirrorFile
);
163 bool res
= (Fetcher
.Run() == pkgAcquire::Continue
);
165 DownloadedMirrorFile
= true;
166 chmod(MirrorFile
.c_str(), 0644);
171 clog
<< "MirrorMethod::DownloadMirrorFile() success: " << res
<< endl
;
176 // Randomizes the lines in the mirror file, this is used so that
177 // we spread the load on the mirrors evenly
178 bool MirrorMethod::RandomizeMirrorFile(string mirror_file
)
180 vector
<string
> content
;
183 if (!FileExists(mirror_file
))
187 ifstream
in(mirror_file
.c_str());
188 while ( !in
.eof() ) {
190 content
.push_back(line
);
193 // we want the file to be random for each different machine, but also
194 // "stable" on the same machine. this is to avoid running into out-of-sync
195 // issues (i.e. Release/Release.gpg different on each mirror)
198 if(uname(&buf
) == 0) {
199 for(i
=0,seed
=1; buf
.nodename
[i
] != 0; i
++) {
200 seed
= seed
* 31 + buf
.nodename
[i
];
204 random_shuffle(content
.begin(), content
.end());
207 ofstream
out(mirror_file
.c_str());
208 while ( !content
.empty()) {
209 line
= content
.back();
217 /* convert a the Queue->Uri back to the mirror base uri and look
218 * at all mirrors we have for this, this is needed as queue->uri
219 * may point to different mirrors (if TryNextMirror() was run)
221 void MirrorMethod::CurrentQueueUriToMirror()
223 // already in mirror:// style so nothing to do
224 if(Queue
->Uri
.find("mirror://") == 0)
227 // find current mirror and select next one
228 for (vector
<string
>::const_iterator mirror
= AllMirrors
.begin();
229 mirror
!= AllMirrors
.end(); ++mirror
)
231 if (Queue
->Uri
.find(*mirror
) == 0)
233 Queue
->Uri
.replace(0, mirror
->length(), BaseUri
);
237 _error
->Error("Internal error: Failed to convert %s back to %s",
238 Queue
->Uri
.c_str(), BaseUri
.c_str());
241 bool MirrorMethod::TryNextMirror()
243 // find current mirror and select next one
244 for (vector
<string
>::const_iterator mirror
= AllMirrors
.begin();
245 mirror
!= AllMirrors
.end(); ++mirror
)
247 if (Queue
->Uri
.find(*mirror
) != 0)
250 vector
<string
>::const_iterator nextmirror
= mirror
+ 1;
251 if (nextmirror
== AllMirrors
.end())
253 Queue
->Uri
.replace(0, mirror
->length(), *nextmirror
);
255 clog
<< "TryNextMirror: " << Queue
->Uri
<< endl
;
258 UsedMirror
= *nextmirror
;
259 Log("Switching mirror");
264 clog
<< "TryNextMirror could not find another mirror to try" << endl
;
269 bool MirrorMethod::InitMirrors()
271 // if we do not have a MirrorFile, fallback
272 if(!FileExists(MirrorFile
))
274 // FIXME: fallback to a default mirror here instead
275 // and provide a config option to define that default
276 return _error
->Error(_("No mirror file '%s' found "), MirrorFile
.c_str());
279 if (access(MirrorFile
.c_str(), R_OK
) != 0)
281 // FIXME: fallback to a default mirror here instead
282 // and provide a config option to define that default
283 return _error
->Error(_("Can not read mirror file '%s'"), MirrorFile
.c_str());
286 // FIXME: make the mirror selection more clever, do not
287 // just use the first one!
288 // BUT: we can not make this random, the mirror has to be
289 // stable accross session, because otherwise we can
290 // get into sync issues (got indexfiles from mirror A,
291 // but packages from mirror B - one might be out of date etc)
292 ifstream
in(MirrorFile
.c_str());
298 // ignore lines that start with #
299 if (s
.find("#") == 0)
301 // ignore empty lines
304 // ignore non http lines
305 if (s
.find("http://") != 0)
308 AllMirrors
.push_back(s
);
310 Mirror
= AllMirrors
[0];
315 string
MirrorMethod::GetMirrorFileName(string mirror_uri_str
)
318 - a mirror_uri_str looks like this:
319 mirror://people.ubuntu.com/~mvo/apt/mirror/mirrors/dists/feisty/Release.gpg
321 - the matching source.list entry
322 deb mirror://people.ubuntu.com/~mvo/apt/mirror/mirrors feisty main
324 - we actually want to go after:
325 http://people.ubuntu.com/~mvo/apt/mirror/mirrors
327 And we need to save the BaseUri for later:
328 - mirror://people.ubuntu.com/~mvo/apt/mirror/mirrors
330 FIXME: what if we have two similar prefixes?
331 mirror://people.ubuntu.com/~mvo/mirror
332 mirror://people.ubuntu.com/~mvo/mirror2
333 then mirror_uri_str looks like:
334 mirror://people.ubuntu.com/~mvo/apt/mirror/dists/feisty/Release.gpg
335 mirror://people.ubuntu.com/~mvo/apt/mirror2/dists/feisty/Release.gpg
336 we search sources.list and find:
337 mirror://people.ubuntu.com/~mvo/apt/mirror
338 in both cases! So we need to apply some domain knowledge here :( and
339 check for /dists/ or /Release.gpg as suffixes
343 std::cerr
<< "GetMirrorFileName: " << mirror_uri_str
<< std::endl
;
345 // read sources.list and find match
346 vector
<metaIndex
*>::const_iterator I
;
349 for(I
=list
.begin(); I
!= list
.end(); I
++)
351 string uristr
= (*I
)->GetURI();
353 std::cerr
<< "Checking: " << uristr
<< std::endl
;
354 if(uristr
.substr(0,strlen("mirror://")) != string("mirror://"))
356 // find matching uri in sources.list
357 if(mirror_uri_str
.substr(0,uristr
.size()) == uristr
)
360 std::cerr
<< "found BaseURI: " << uristr
<< std::endl
;
361 BaseUri
= uristr
.substr(0,uristr
.size()-1);
362 Dist
= (*I
)->GetDist();
366 name
= _config
->FindDir("Dir::State::mirrors") + URItoFileName(BaseUri
);
370 cerr
<< "base-uri: " << BaseUri
<< endl
;
371 cerr
<< "mirror-file: " << name
<< endl
;
376 // MirrorMethod::Fetch - Fetch an item /*{{{*/
377 // ---------------------------------------------------------------------
378 /* This adds an item to the pipeline. We keep the pipeline at a fixed
380 bool MirrorMethod::Fetch(FetchItem
*Itm
)
383 clog
<< "MirrorMethod::Fetch()" << endl
;
385 // the http method uses Fetch(0) as a way to update the pipeline,
386 // just let it do its work in this case - Fetch() with a valid
387 // Itm will always run before the first Fetch(0)
389 return HttpMethod::Fetch(Itm
);
391 // if we don't have the name of the mirror file on disk yet,
392 // calculate it now (can be derived from the uri)
393 if(MirrorFile
.empty())
394 MirrorFile
= GetMirrorFileName(Itm
->Uri
);
396 // download mirror file once (if we are after index files)
397 if(Itm
->IndexFile
&& !DownloadedMirrorFile
)
399 Clean(_config
->FindDir("Dir::State::mirrors"));
400 if (DownloadMirrorFile(Itm
->Uri
))
401 RandomizeMirrorFile(MirrorFile
);
404 if(AllMirrors
.empty()) {
406 // no valid mirror selected, something went wrong downloading
407 // from the master mirror site most likely and there is
408 // no old mirror file availalbe
413 if(Itm
->Uri
.find("mirror://") != string::npos
)
414 Itm
->Uri
.replace(0,BaseUri
.size(), Mirror
);
417 clog
<< "Fetch: " << Itm
->Uri
<< endl
<< endl
;
419 // now run the real fetcher
420 return HttpMethod::Fetch(Itm
);
423 void MirrorMethod::Fail(string Err
,bool Transient
)
425 // FIXME: TryNextMirror is not ideal for indexfile as we may
426 // run into auth issues
429 clog
<< "Failure to get " << Queue
->Uri
<< endl
;
431 // try the next mirror on fail (if its not a expected failure,
432 // e.g. translations are ok to ignore)
433 if (!Queue
->FailIgnore
&& TryNextMirror())
436 // all mirrors failed, so bail out
438 strprintf(s
, _("[Mirror: %s]"), Mirror
.c_str());
441 CurrentQueueUriToMirror();
442 pkgAcqMethod::Fail(Err
, Transient
);
445 void MirrorMethod::URIStart(FetchResult
&Res
)
447 CurrentQueueUriToMirror();
448 pkgAcqMethod::URIStart(Res
);
451 void MirrorMethod::URIDone(FetchResult
&Res
,FetchResult
*Alt
)
453 CurrentQueueUriToMirror();
454 pkgAcqMethod::URIDone(Res
, Alt
);
460 setlocale(LC_ALL
, "");