]> git.saurik.com Git - apt.git/blob - methods/mirror.cc
* apt-pkg/deb/dpkgpm.cc:
[apt.git] / methods / mirror.cc
1 // -*- mode: cpp; mode: fold -*-
2 // Description /*{{{*/
3 // $Id: mirror.cc,v 1.59 2004/05/08 19:42:35 mdz Exp $
4 /* ######################################################################
5
6 Mirror Aquire Method - This is the Mirror aquire method for APT.
7
8 ##################################################################### */
9 /*}}}*/
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>
18
19 #include <fstream>
20 #include <iostream>
21 #include <stdarg.h>
22 #include <sys/stat.h>
23 #include <sys/types.h>
24 #include <dirent.h>
25
26 using namespace std;
27
28 #include "mirror.h"
29 #include "http.h"
30 #include "apti18n.h"
31 /*}}}*/
32
33 /* Done:
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
40 *
41 * TODO:
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
47 * - testing :)
48 */
49
50 MirrorMethod::MirrorMethod()
51 : HttpMethod(), HasMirrorFile(false)
52 {
53 };
54
55 // HttpMethod::Configuration - Handle a configuration message /*{{{*/
56 // ---------------------------------------------------------------------
57 /* We stash the desired pipeline depth */
58 bool MirrorMethod::Configuration(string Message)
59 {
60 if (pkgAcqMethod::Configuration(Message) == false)
61 return false;
62 Debug = _config->FindB("Debug::Acquire::mirror",false);
63
64 return true;
65 }
66 /*}}}*/
67
68 // clean the mirrors dir based on ttl information
69 bool MirrorMethod::Clean(string Dir)
70 {
71 vector<metaIndex *>::const_iterator I;
72
73 if(Debug)
74 clog << "MirrorMethod::Clean(): " << Dir << endl;
75
76 // read sources.list
77 pkgSourceList list;
78 list.ReadMainList();
79
80 DIR *D = opendir(Dir.c_str());
81 if (D == 0)
82 return _error->Errno("opendir",_("Unable to read %s"),Dir.c_str());
83
84 string StartDir = SafeGetCWD();
85 if (chdir(Dir.c_str()) != 0)
86 {
87 closedir(D);
88 return _error->Errno("chdir",_("Unable to change to %s"),Dir.c_str());
89 }
90
91 for (struct dirent *Dir = readdir(D); Dir != 0; Dir = readdir(D))
92 {
93 // Skip some files..
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)
98 continue;
99
100 // see if we have that uri
101 for(I=list.begin(); I != list.end(); I++)
102 {
103 string uri = (*I)->GetURI();
104 if(uri.substr(0,strlen("mirror://")) != string("mirror://"))
105 continue;
106 string BaseUri = uri.substr(0,uri.size()-1);
107 if (URItoFileName(BaseUri) == Dir->d_name)
108 break;
109 }
110 // nothing found, nuke it
111 if (I == list.end())
112 unlink(Dir->d_name);
113 };
114
115 chdir(StartDir.c_str());
116 closedir(D);
117 return true;
118 }
119
120
121 bool MirrorMethod::GetMirrorFile(string mirror_uri_str)
122 {
123 /*
124 - a mirror_uri_str looks like this:
125 mirror://people.ubuntu.com/~mvo/apt/mirror/mirrors/dists/feisty/Release.gpg
126
127 - the matching source.list entry
128 deb mirror://people.ubuntu.com/~mvo/apt/mirror/mirrors feisty main
129
130 - we actually want to go after:
131 http://people.ubuntu.com/~mvo/apt/mirror/mirrors
132
133 And we need to save the BaseUri for later:
134 - mirror://people.ubuntu.com/~mvo/apt/mirror/mirrors
135
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
146 */
147 if(Debug)
148 std::cerr << "GetMirrorFile: " << mirror_uri_str << std::endl;
149
150 // read sources.list and find match
151 vector<metaIndex *>::const_iterator I;
152 pkgSourceList list;
153 list.ReadMainList();
154 for(I=list.begin(); I != list.end(); I++)
155 {
156 string uristr = (*I)->GetURI();
157 if(Debug)
158 std::cerr << "Checking: " << uristr << std::endl;
159 if(uristr.substr(0,strlen("mirror://")) != string("mirror://"))
160 continue;
161 // find matching uri in sources.list
162 if(mirror_uri_str.substr(0,uristr.size()) == uristr)
163 {
164 if(Debug)
165 std::cerr << "found BaseURI: " << uristr << std::endl;
166 BaseUri = uristr.substr(0,uristr.size()-1);
167 }
168 }
169 string fetch = BaseUri;
170 fetch.replace(0,strlen("mirror://"),"http://");
171
172 // get new file
173 MirrorFile = _config->FindDir("Dir::State::mirrors") + URItoFileName(BaseUri);
174
175 if(Debug)
176 {
177 cerr << "base-uri: " << BaseUri << endl;
178 cerr << "mirror-file: " << MirrorFile << endl;
179 }
180
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))
184 {
185 struct stat buf;
186 time_t t,now,refresh;
187 if(stat(MirrorFile.c_str(), &buf) != 0)
188 return false;
189 t = std::max(buf.st_mtime, buf.st_ctime);
190 now = time(NULL);
191 refresh = 60*_config->FindI("Acquire::Mirror::RefreshInterval",360);
192 if(t + refresh > now)
193 {
194 if(Debug)
195 clog << "Mirror file is in RefreshInterval" << endl;
196 HasMirrorFile = true;
197 return true;
198 }
199 if(Debug)
200 clog << "Mirror file " << MirrorFile << " older than " << refresh << "min, re-download it" << endl;
201 }
202
203 // not that great to use pkgAcquire here, but we do not have
204 // any other way right now
205 pkgAcquire Fetcher;
206 new pkgAcqFile(&Fetcher, fetch, "", 0, "", "", "", MirrorFile);
207 bool res = (Fetcher.Run() == pkgAcquire::Continue);
208 if(res)
209 HasMirrorFile = true;
210 Fetcher.Shutdown();
211 return res;
212 }
213
214 bool MirrorMethod::SelectMirror()
215 {
216 // FIXME: make the mirror selection more clever, do not
217 // just use the first one!
218 ifstream in(MirrorFile.c_str());
219 getline(in, Mirror);
220 if(Debug)
221 cerr << "Using mirror: " << Mirror << endl;
222
223 UsedMirror = Mirror;
224 return true;
225 }
226
227 // MirrorMethod::Fetch - Fetch an item /*{{{*/
228 // ---------------------------------------------------------------------
229 /* This adds an item to the pipeline. We keep the pipeline at a fixed
230 depth. */
231 bool MirrorMethod::Fetch(FetchItem *Itm)
232 {
233 // select mirror only once per session
234 if(!HasMirrorFile)
235 {
236 Clean(_config->FindDir("Dir::State::mirrors"));
237 GetMirrorFile(Itm->Uri);
238 SelectMirror();
239 }
240
241 for (FetchItem *I = Queue; I != 0; I = I->Next)
242 {
243 if(I->Uri.find("mirror://") != string::npos)
244 I->Uri.replace(0,BaseUri.size(),Mirror);
245 }
246
247 // now run the real fetcher
248 return HttpMethod::Fetch(Itm);
249 };
250
251 void MirrorMethod::Fail(string Err,bool Transient)
252 {
253 if(Queue->Uri.find("http://") != string::npos)
254 Queue->Uri.replace(0,Mirror.size(), BaseUri);
255 pkgAcqMethod::Fail(Err, Transient);
256 }
257
258 void MirrorMethod::URIStart(FetchResult &Res)
259 {
260 if(Queue->Uri.find("http://") != string::npos)
261 Queue->Uri.replace(0,Mirror.size(), BaseUri);
262 pkgAcqMethod::URIStart(Res);
263 }
264
265 void MirrorMethod::URIDone(FetchResult &Res,FetchResult *Alt)
266 {
267 if(Queue->Uri.find("http://") != string::npos)
268 Queue->Uri.replace(0,Mirror.size(), BaseUri);
269 pkgAcqMethod::URIDone(Res, Alt);
270 }
271
272
273 int main()
274 {
275 setlocale(LC_ALL, "");
276
277 MirrorMethod Mth;
278
279 return Mth.Loop();
280 }
281
282