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>
23 #include <sys/types.h>
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
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
52 MirrorMethod::MirrorMethod()
53 : HttpMethod(), DownloadedMirrorFile(false)
57 // HttpMethod::Configuration - Handle a configuration message /*{{{*/
58 // ---------------------------------------------------------------------
59 /* We stash the desired pipeline depth */
60 bool MirrorMethod::Configuration(string Message
)
62 if (pkgAcqMethod::Configuration(Message
) == false)
64 Debug
= _config
->FindB("Debug::Acquire::mirror",false);
70 // clean the mirrors dir based on ttl information
71 bool MirrorMethod::Clean(string Dir
)
73 vector
<metaIndex
*>::const_iterator I
;
76 clog
<< "MirrorMethod::Clean(): " << Dir
<< endl
;
79 return _error
->Error("will not clean: '/'");
85 DIR *D
= opendir(Dir
.c_str());
87 return _error
->Errno("opendir",_("Unable to read %s"),Dir
.c_str());
89 string StartDir
= SafeGetCWD();
90 if (chdir(Dir
.c_str()) != 0)
93 return _error
->Errno("chdir",_("Unable to change to %s"),Dir
.c_str());
96 for (struct dirent
*Dir
= readdir(D
); Dir
!= 0; Dir
= readdir(D
))
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)
105 // see if we have that uri
106 for(I
=list
.begin(); I
!= list
.end(); I
++)
108 string uri
= (*I
)->GetURI();
109 if(uri
.find("mirror://") != 0)
111 string BaseUri
= uri
.substr(0,uri
.size()-1);
112 if (URItoFileName(BaseUri
) == Dir
->d_name
)
115 // nothing found, nuke it
120 chdir(StartDir
.c_str());
126 bool MirrorMethod::DownloadMirrorFile(string mirror_uri_str
)
128 // not that great to use pkgAcquire here, but we do not have
129 // any other way right now
130 string fetch
= BaseUri
;
131 fetch
.replace(0,strlen("mirror://"),"http://");
134 clog
<< "MirrorMethod::DownloadMirrorFile(): '" << fetch
<< "'"
135 << " to " << MirrorFile
<< endl
;
138 new pkgAcqFile(&Fetcher
, fetch
, "", 0, "", "", "", MirrorFile
);
139 bool res
= (Fetcher
.Run() == pkgAcquire::Continue
);
141 DownloadedMirrorFile
= true;
145 clog
<< "MirrorMethod::DownloadMirrorFile() success: " << res
<< endl
;
150 /* convert a the Queue->Uri back to the mirror base uri and look
151 * at all mirrors we have for this, this is needed as queue->uri
152 * may point to different mirrors (if TryNextMirror() was run)
154 void MirrorMethod::CurrentQueueUriToMirror()
156 // already in mirror:// style so nothing to do
157 if(Queue
->Uri
.find("mirror://") == 0)
160 // find current mirror and select next one
161 for (vector
<string
>::const_iterator mirror
= AllMirrors
.begin();
162 mirror
!= AllMirrors
.end(); ++mirror
)
164 if (Queue
->Uri
.find(*mirror
) == 0)
166 Queue
->Uri
.replace(0, mirror
->length(), BaseUri
);
170 _error
->Error("Internal error: Failed to convert %s back to %s",
171 Queue
->Uri
.c_str(), BaseUri
.c_str());
174 bool MirrorMethod::TryNextMirror()
176 // find current mirror and select next one
177 for (vector
<string
>::const_iterator mirror
= AllMirrors
.begin();
178 mirror
!= AllMirrors
.end(); ++mirror
)
180 if (Queue
->Uri
.find(*mirror
) != 0)
183 vector
<string
>::const_iterator nextmirror
= mirror
+ 1;
184 if (nextmirror
== AllMirrors
.end())
186 Queue
->Uri
.replace(0, mirror
->length(), *nextmirror
);
188 clog
<< "TryNextMirror: " << Queue
->Uri
<< endl
;
193 clog
<< "TryNextMirror could not find another mirror to try" << endl
;
198 bool MirrorMethod::InitMirrors()
200 // if we do not have a MirrorFile, fallback
201 if(!FileExists(MirrorFile
))
203 // FIXME: fallback to a default mirror here instead
204 // and provide a config option to define that default
205 return _error
->Error(_("No mirror file '%s' found "), MirrorFile
.c_str());
208 // FIXME: make the mirror selection more clever, do not
209 // just use the first one!
210 // BUT: we can not make this random, the mirror has to be
211 // stable accross session, because otherwise we can
212 // get into sync issues (got indexfiles from mirror A,
213 // but packages from mirror B - one might be out of date etc)
214 ifstream
in(MirrorFile
.c_str());
220 AllMirrors
.push_back(s
);
222 Mirror
= AllMirrors
[0];
227 string
MirrorMethod::GetMirrorFileName(string mirror_uri_str
)
230 - a mirror_uri_str looks like this:
231 mirror://people.ubuntu.com/~mvo/apt/mirror/mirrors/dists/feisty/Release.gpg
233 - the matching source.list entry
234 deb mirror://people.ubuntu.com/~mvo/apt/mirror/mirrors feisty main
236 - we actually want to go after:
237 http://people.ubuntu.com/~mvo/apt/mirror/mirrors
239 And we need to save the BaseUri for later:
240 - mirror://people.ubuntu.com/~mvo/apt/mirror/mirrors
242 FIXME: what if we have two similar prefixes?
243 mirror://people.ubuntu.com/~mvo/mirror
244 mirror://people.ubuntu.com/~mvo/mirror2
245 then mirror_uri_str looks like:
246 mirror://people.ubuntu.com/~mvo/apt/mirror/dists/feisty/Release.gpg
247 mirror://people.ubuntu.com/~mvo/apt/mirror2/dists/feisty/Release.gpg
248 we search sources.list and find:
249 mirror://people.ubuntu.com/~mvo/apt/mirror
250 in both cases! So we need to apply some domain knowledge here :( and
251 check for /dists/ or /Release.gpg as suffixes
255 std::cerr
<< "GetMirrorFileName: " << mirror_uri_str
<< std::endl
;
257 // read sources.list and find match
258 vector
<metaIndex
*>::const_iterator I
;
261 for(I
=list
.begin(); I
!= list
.end(); I
++)
263 string uristr
= (*I
)->GetURI();
265 std::cerr
<< "Checking: " << uristr
<< std::endl
;
266 if(uristr
.substr(0,strlen("mirror://")) != string("mirror://"))
268 // find matching uri in sources.list
269 if(mirror_uri_str
.substr(0,uristr
.size()) == uristr
)
272 std::cerr
<< "found BaseURI: " << uristr
<< std::endl
;
273 BaseUri
= uristr
.substr(0,uristr
.size()-1);
277 name
= _config
->FindDir("Dir::State::mirrors") + URItoFileName(BaseUri
);
281 cerr
<< "base-uri: " << BaseUri
<< endl
;
282 cerr
<< "mirror-file: " << name
<< endl
;
287 // MirrorMethod::Fetch - Fetch an item /*{{{*/
288 // ---------------------------------------------------------------------
289 /* This adds an item to the pipeline. We keep the pipeline at a fixed
291 bool MirrorMethod::Fetch(FetchItem
*Itm
)
294 clog
<< "MirrorMethod::Fetch()" << endl
;
296 // the http method uses Fetch(0) as a way to update the pipeline,
297 // just let it do its work in this case - Fetch() with a valid
298 // Itm will always run before the first Fetch(0)
300 return HttpMethod::Fetch(Itm
);
302 // if we don't have the name of the mirror file on disk yet,
303 // calculate it now (can be derived from the uri)
304 if(MirrorFile
.empty())
305 MirrorFile
= GetMirrorFileName(Itm
->Uri
);
307 // download mirror file once (if we are after index files)
308 if(Itm
->IndexFile
&& !DownloadedMirrorFile
)
310 Clean(_config
->FindDir("Dir::State::mirrors"));
311 DownloadMirrorFile(Itm
->Uri
);
314 if(AllMirrors
.empty()) {
316 // no valid mirror selected, something went wrong downloading
317 // from the master mirror site most likely and there is
318 // no old mirror file availalbe
323 if(Itm
->Uri
.find("mirror://") != string::npos
)
324 Itm
->Uri
.replace(0,BaseUri
.size(), Mirror
);
327 clog
<< "Fetch: " << Itm
->Uri
<< endl
<< endl
;
329 // now run the real fetcher
330 return HttpMethod::Fetch(Itm
);
333 void MirrorMethod::Fail(string Err
,bool Transient
)
335 // FIXME: TryNextMirror is not ideal for indexfile as we may
336 // run into auth issues
339 clog
<< "Failure to get " << Queue
->Uri
<< endl
;
341 // try the next mirror on fail (if its not a expected failure,
342 // e.g. translations are ok to ignore)
343 if (!Queue
->FailIgnore
&& TryNextMirror())
346 // all mirrors failed, so bail out
348 strprintf(s
, _("[Mirror: %s]"), Mirror
.c_str());
351 CurrentQueueUriToMirror();
352 pkgAcqMethod::Fail(Err
, Transient
);
355 void MirrorMethod::URIStart(FetchResult
&Res
)
357 CurrentQueueUriToMirror();
358 pkgAcqMethod::URIStart(Res
);
361 void MirrorMethod::URIDone(FetchResult
&Res
,FetchResult
*Alt
)
363 CurrentQueueUriToMirror();
364 pkgAcqMethod::URIDone(Res
, Alt
);
370 setlocale(LC_ALL
, "");