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