]>
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(), HasMirrorFile(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
;
80 DIR *D
= opendir(Dir
.c_str());
82 return _error
->Errno("opendir",_("Unable to read %s"),Dir
.c_str());
84 string StartDir
= SafeGetCWD();
85 if (chdir(Dir
.c_str()) != 0)
88 return _error
->Errno("chdir",_("Unable to change to %s"),Dir
.c_str());
91 for (struct dirent
*Dir
= readdir(D
); Dir
!= 0; Dir
= readdir(D
))
94 if (strcmp(Dir
->d_name
,"lock") == 0 ||
95 strcmp(Dir
->d_name
,"partial") == 0 ||
96 strcmp(Dir
->d_name
,".") == 0 ||
97 strcmp(Dir
->d_name
,"..") == 0)
100 // see if we have that uri
101 for(I
=list
.begin(); I
!= list
.end(); I
++)
103 string uri
= (*I
)->GetURI();
104 if(uri
.substr(0,strlen("mirror://")) != string("mirror://"))
106 string BaseUri
= uri
.substr(0,uri
.size()-1);
107 if (URItoFileName(BaseUri
) == Dir
->d_name
)
110 // nothing found, nuke it
115 chdir(StartDir
.c_str());
121 bool MirrorMethod::GetMirrorFile(string mirror_uri_str
)
124 - a mirror_uri_str looks like this:
125 mirror://people.ubuntu.com/~mvo/apt/mirror/mirrors/dists/feisty/Release.gpg
127 - the matching source.list entry
128 deb mirror://people.ubuntu.com/~mvo/apt/mirror/mirrors feisty main
130 - we actually want to go after:
131 http://people.ubuntu.com/~mvo/apt/mirror/mirrors
133 And we need to save the BaseUri for later:
134 - mirror://people.ubuntu.com/~mvo/apt/mirror/mirrors
136 FIXME: what if we have two similar prefixes?
137 mirror://people.ubuntu.com/~mvo/mirror
138 mirror://people.ubuntu.com/~mvo/mirror2
139 then mirror_uri_str looks like:
140 mirror://people.ubuntu.com/~mvo/apt/mirror/dists/feisty/Release.gpg
141 mirror://people.ubuntu.com/~mvo/apt/mirror2/dists/feisty/Release.gpg
142 we search sources.list and find:
143 mirror://people.ubuntu.com/~mvo/apt/mirror
144 in both cases! So we need to apply some domain knowledge here :( and
145 check for /dists/ or /Release.gpg as suffixes
148 std::cerr
<< "GetMirrorFile: " << mirror_uri_str
<< std::endl
;
150 // read sources.list and find match
151 vector
<metaIndex
*>::const_iterator I
;
154 for(I
=list
.begin(); I
!= list
.end(); I
++)
156 string uristr
= (*I
)->GetURI();
158 std::cerr
<< "Checking: " << uristr
<< std::endl
;
159 if(uristr
.substr(0,strlen("mirror://")) != string("mirror://"))
161 // find matching uri in sources.list
162 if(mirror_uri_str
.substr(0,uristr
.size()) == uristr
)
165 std::cerr
<< "found BaseURI: " << uristr
<< std::endl
;
166 BaseUri
= uristr
.substr(0,uristr
.size()-1);
169 string fetch
= BaseUri
;
170 fetch
.replace(0,strlen("mirror://"),"http://");
173 MirrorFile
= _config
->FindDir("Dir::State::mirrors") + URItoFileName(BaseUri
);
177 cerr
<< "base-uri: " << BaseUri
<< endl
;
178 cerr
<< "mirror-file: " << MirrorFile
<< endl
;
181 // check the file, if it is not older than RefreshInterval just use it
182 // otherwise try to get a new one
183 if(FileExists(MirrorFile
))
186 time_t t
,now
,refresh
;
187 if(stat(MirrorFile
.c_str(), &buf
) != 0)
189 t
= std::max(buf
.st_mtime
, buf
.st_ctime
);
191 refresh
= 60*_config
->FindI("Acquire::Mirror::RefreshInterval",360);
192 if(t
+ refresh
> now
)
195 clog
<< "Mirror file is in RefreshInterval" << endl
;
196 HasMirrorFile
= true;
200 clog
<< "Mirror file " << MirrorFile
<< " older than " << refresh
<< "min, re-download it" << endl
;
203 // not that great to use pkgAcquire here, but we do not have
204 // any other way right now
206 new pkgAcqFile(&Fetcher
, fetch
, "", 0, "", "", "", MirrorFile
);
207 bool res
= (Fetcher
.Run() == pkgAcquire::Continue
);
209 HasMirrorFile
= true;
214 bool MirrorMethod::SelectMirror()
216 // FIXME: make the mirror selection more clever, do not
217 // just use the first one!
218 ifstream
in(MirrorFile
.c_str());
221 cerr
<< "Using mirror: " << Mirror
<< endl
;
227 // MirrorMethod::Fetch - Fetch an item /*{{{*/
228 // ---------------------------------------------------------------------
229 /* This adds an item to the pipeline. We keep the pipeline at a fixed
231 bool MirrorMethod::Fetch(FetchItem
*Itm
)
233 // select mirror only once per session
236 Clean(_config
->FindDir("Dir::State::mirrors"));
237 GetMirrorFile(Itm
->Uri
);
241 for (FetchItem
*I
= Queue
; I
!= 0; I
= I
->Next
)
243 if(I
->Uri
.find("mirror://") != string::npos
)
244 I
->Uri
.replace(0,BaseUri
.size(),Mirror
);
247 // now run the real fetcher
248 return HttpMethod::Fetch(Itm
);
251 void MirrorMethod::Fail(string Err
,bool Transient
)
253 if(Queue
->Uri
.find("http://") != string::npos
)
254 Queue
->Uri
.replace(0,Mirror
.size(), BaseUri
);
255 pkgAcqMethod::Fail(Err
, Transient
);
258 void MirrorMethod::URIStart(FetchResult
&Res
)
260 if(Queue
->Uri
.find("http://") != string::npos
)
261 Queue
->Uri
.replace(0,Mirror
.size(), BaseUri
);
262 pkgAcqMethod::URIStart(Res
);
265 void MirrorMethod::URIDone(FetchResult
&Res
,FetchResult
*Alt
)
267 if(Queue
->Uri
.find("http://") != string::npos
)
268 Queue
->Uri
.replace(0,Mirror
.size(), BaseUri
);
269 pkgAcqMethod::URIDone(Res
, Alt
);
275 setlocale(LC_ALL
, "");