]>
git.saurik.com Git - apt.git/blob - methods/mirror.cc
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>
34 * - works with http (only!)
35 * - always picks the first mirror from the list
36 * - call out to problem reporting script
37 * - supports "deb mirror://host/path/to/mirror-list/// dist component"
38 * - uses pkgAcqMethod::FailReason() to have a string representation
39 * of the failure that is also send to LP
42 * - deal with runing as non-root because we can't write to the lists
43 dir then -> use the cached mirror file
44 * - better method to download than having a pkgAcquire interface here
45 * and better error handling there!
46 * - support more than http
50 MirrorMethod::MirrorMethod()
51 : HttpMethod(), DownloadedMirrorFile(false)
55 // HttpMethod::Configuration - Handle a configuration message /*{{{*/
56 // ---------------------------------------------------------------------
57 /* We stash the desired pipeline depth */
58 bool MirrorMethod::Configuration(string Message
)
60 if (pkgAcqMethod::Configuration(Message
) == false)
62 Debug
= _config
->FindB("Debug::Acquire::mirror",false);
68 // clean the mirrors dir based on ttl information
69 bool MirrorMethod::Clean(string Dir
)
71 vector
<metaIndex
*>::const_iterator I
;
74 clog
<< "MirrorMethod::Clean(): " << Dir
<< endl
;
77 return _error
->Error("will not clean: '/'");
83 DIR *D
= opendir(Dir
.c_str());
85 return _error
->Errno("opendir",_("Unable to read %s"),Dir
.c_str());
87 string StartDir
= SafeGetCWD();
88 if (chdir(Dir
.c_str()) != 0)
91 return _error
->Errno("chdir",_("Unable to change to %s"),Dir
.c_str());
94 for (struct dirent
*Dir
= readdir(D
); Dir
!= 0; Dir
= readdir(D
))
97 if (strcmp(Dir
->d_name
,"lock") == 0 ||
98 strcmp(Dir
->d_name
,"partial") == 0 ||
99 strcmp(Dir
->d_name
,".") == 0 ||
100 strcmp(Dir
->d_name
,"..") == 0)
103 // see if we have that uri
104 for(I
=list
.begin(); I
!= list
.end(); I
++)
106 string uri
= (*I
)->GetURI();
107 if(uri
.substr(0,strlen("mirror://")) != string("mirror://"))
109 string BaseUri
= uri
.substr(0,uri
.size()-1);
110 if (URItoFileName(BaseUri
) == Dir
->d_name
)
113 // nothing found, nuke it
118 chdir(StartDir
.c_str());
124 bool MirrorMethod::DownloadMirrorFile(string mirror_uri_str
)
127 // check the file, if it is not older than RefreshInterval just use it
128 // otherwise try to get a new one
129 if(FileExists(MirrorFile
))
132 time_t t
,now
,refresh
;
133 if(stat(MirrorFile
.c_str(), &buf
) != 0)
135 t
= std::max(buf
.st_mtime
, buf
.st_ctime
);
137 refresh
= 60*_config
->FindI("Acquire::Mirror::RefreshInterval",360);
138 if(t
+ refresh
> now
)
141 clog
<< "Mirror file is in RefreshInterval" << endl
;
142 DownloadedMirrorFile
= true;
146 clog
<< "Mirror file " << MirrorFile
<< " older than " << refresh
<< "min, re-download it" << endl
;
149 // not that great to use pkgAcquire here, but we do not have
150 // any other way right now
151 string fetch
= BaseUri
;
152 fetch
.replace(0,strlen("mirror://"),"http://");
155 new pkgAcqFile(&Fetcher
, fetch
, "", 0, "", "", "", MirrorFile
);
156 bool res
= (Fetcher
.Run() == pkgAcquire::Continue
);
158 DownloadedMirrorFile
= true;
163 bool MirrorMethod::SelectMirror()
165 // if we do not have a MirrorFile, fallback
166 if(!FileExists(MirrorFile
))
168 // FIXME: fallback to a default mirror here instead
169 // and provide a config option to define that default
170 return _error
->Error(_("No mirror file '%s' found "), MirrorFile
.c_str());
173 // FIXME: make the mirror selection more clever, do not
174 // just use the first one!
175 // BUT: we can not make this random, the mirror has to be
176 // stable accross session, because otherwise we can
177 // get into sync issues (got indexfiles from mirror A,
178 // but packages from mirror B - one might be out of date etc)
179 ifstream
in(MirrorFile
.c_str());
182 cerr
<< "Using mirror: " << Mirror
<< endl
;
188 string
MirrorMethod::GetMirrorFileName(string mirror_uri_str
)
191 - a mirror_uri_str looks like this:
192 mirror://people.ubuntu.com/~mvo/apt/mirror/mirrors/dists/feisty/Release.gpg
194 - the matching source.list entry
195 deb mirror://people.ubuntu.com/~mvo/apt/mirror/mirrors feisty main
197 - we actually want to go after:
198 http://people.ubuntu.com/~mvo/apt/mirror/mirrors
200 And we need to save the BaseUri for later:
201 - mirror://people.ubuntu.com/~mvo/apt/mirror/mirrors
203 FIXME: what if we have two similar prefixes?
204 mirror://people.ubuntu.com/~mvo/mirror
205 mirror://people.ubuntu.com/~mvo/mirror2
206 then mirror_uri_str looks like:
207 mirror://people.ubuntu.com/~mvo/apt/mirror/dists/feisty/Release.gpg
208 mirror://people.ubuntu.com/~mvo/apt/mirror2/dists/feisty/Release.gpg
209 we search sources.list and find:
210 mirror://people.ubuntu.com/~mvo/apt/mirror
211 in both cases! So we need to apply some domain knowledge here :( and
212 check for /dists/ or /Release.gpg as suffixes
216 std::cerr
<< "GetMirrorFileName: " << mirror_uri_str
<< std::endl
;
218 // read sources.list and find match
219 vector
<metaIndex
*>::const_iterator I
;
222 for(I
=list
.begin(); I
!= list
.end(); I
++)
224 string uristr
= (*I
)->GetURI();
226 std::cerr
<< "Checking: " << uristr
<< std::endl
;
227 if(uristr
.substr(0,strlen("mirror://")) != string("mirror://"))
229 // find matching uri in sources.list
230 if(mirror_uri_str
.substr(0,uristr
.size()) == uristr
)
233 std::cerr
<< "found BaseURI: " << uristr
<< std::endl
;
234 BaseUri
= uristr
.substr(0,uristr
.size()-1);
238 name
= _config
->FindDir("Dir::State::mirrors") + URItoFileName(BaseUri
);
242 cerr
<< "base-uri: " << BaseUri
<< endl
;
243 cerr
<< "mirror-file: " << name
<< endl
;
248 // MirrorMethod::Fetch - Fetch an item /*{{{*/
249 // ---------------------------------------------------------------------
250 /* This adds an item to the pipeline. We keep the pipeline at a fixed
252 bool MirrorMethod::Fetch(FetchItem
*Itm
)
254 // the http method uses Fetch(0) as a way to update the pipeline,
255 // just let it do its work in this case - Fetch() with a valid
256 // Itm will always run before the first Fetch(0)
258 return HttpMethod::Fetch(Itm
);
260 // if we don't have the name of the mirror file on disk yet,
261 // calculate it now (can be derived from the uri)
262 if(MirrorFile
.empty())
263 MirrorFile
= GetMirrorFileName(Itm
->Uri
);
265 // download mirror file once (if we are after index files)
266 if(Itm
->IndexFile
&& !DownloadedMirrorFile
)
268 Clean(_config
->FindDir("Dir::State::mirrors"));
269 DownloadMirrorFile(Itm
->Uri
);
275 for (FetchItem
*I
= Queue
; I
!= 0; I
= I
->Next
)
277 if(I
->Uri
.find("mirror://") != string::npos
)
278 I
->Uri
.replace(0,BaseUri
.size(), Mirror
);
281 // now run the real fetcher
282 return HttpMethod::Fetch(Itm
);
285 void MirrorMethod::Fail(string Err
,bool Transient
)
287 if(Queue
->Uri
.find("http://") != string::npos
)
288 Queue
->Uri
.replace(0,Mirror
.size(), BaseUri
);
289 pkgAcqMethod::Fail(Err
, Transient
);
292 void MirrorMethod::URIStart(FetchResult
&Res
)
294 if(Queue
->Uri
.find("http://") != string::npos
)
295 Queue
->Uri
.replace(0,Mirror
.size(), BaseUri
);
296 pkgAcqMethod::URIStart(Res
);
299 void MirrorMethod::URIDone(FetchResult
&Res
,FetchResult
*Alt
)
301 if(Queue
->Uri
.find("http://") != string::npos
)
302 Queue
->Uri
.replace(0,Mirror
.size(), BaseUri
);
303 pkgAcqMethod::URIDone(Res
, Alt
);
309 setlocale(LC_ALL
, "");