]>
Commit | Line | Data |
---|---|---|
5f6b130d MV |
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> | |
0c312e0e | 17 | #include <apt-pkg/sourcelist.h> |
5f6b130d MV |
18 | |
19 | #include <fstream> | |
20 | #include <iostream> | |
14e097c1 | 21 | #include <stdarg.h> |
d731f9c5 | 22 | #include <sys/stat.h> |
70288656 MV |
23 | #include <sys/types.h> |
24 | #include <dirent.h> | |
14e097c1 | 25 | |
5f6b130d MV |
26 | using namespace std; |
27 | ||
28 | #include "mirror.h" | |
29 | #include "http.h" | |
70288656 | 30 | #include "apti18n.h" |
5f6b130d MV |
31 | /*}}}*/ |
32 | ||
362d2934 | 33 | /* Done: |
59271f62 | 34 | * - works with http (only!) |
362d2934 MV |
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" | |
59271f62 MV |
38 | * - use pkgAcqMethod::FailReason() to have a string representation |
39 | * of the failure that is also send to LP | |
362d2934 | 40 | * |
86c17f0a | 41 | * TODO: |
d731f9c5 MV |
42 | * - deal with runing as non-root because we can't write to the lists |
43 | dir then -> use the cached mirror file | |
86c17f0a | 44 | * - better method to download than having a pkgAcquire interface here |
066b53e9 | 45 | * - support more than http |
86c17f0a | 46 | * - testing :) |
86c17f0a MV |
47 | */ |
48 | ||
5f6b130d | 49 | MirrorMethod::MirrorMethod() |
14e097c1 | 50 | : HttpMethod(), HasMirrorFile(false) |
5f6b130d | 51 | { |
5f6b130d MV |
52 | }; |
53 | ||
14e097c1 MV |
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 | ||
d731f9c5 | 67 | // clean the mirrors dir based on ttl information |
70288656 | 68 | bool MirrorMethod::Clean(string Dir) |
d731f9c5 | 69 | { |
0c312e0e MV |
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(); | |
70288656 MV |
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; | |
0c312e0e MV |
98 | |
99 | // see if we have that uri | |
100 | for(I=list.begin(); I != list.end(); I++) | |
70288656 | 101 | { |
0c312e0e MV |
102 | string uri = (*I)->GetURI(); |
103 | if(uri.substr(0,strlen("mirror://")) != string("mirror://")) | |
104 | continue; | |
066b53e9 | 105 | string BaseUri = uri.substr(0,uri.size()-1); |
0c312e0e MV |
106 | if (URItoFileName(BaseUri) == Dir->d_name) |
107 | break; | |
70288656 | 108 | } |
0c312e0e MV |
109 | // nothing found, nuke it |
110 | if (I == list.end()) | |
70288656 | 111 | unlink(Dir->d_name); |
70288656 | 112 | }; |
d731f9c5 | 113 | |
70288656 MV |
114 | chdir(StartDir.c_str()); |
115 | closedir(D); | |
116 | return true; | |
d731f9c5 MV |
117 | } |
118 | ||
14e097c1 | 119 | |
066b53e9 | 120 | bool MirrorMethod::GetMirrorFile(string mirror_uri_str) |
5f6b130d | 121 | { |
066b53e9 MV |
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 | std::cerr << "GetMirrorFile: " << mirror_uri_str << std::endl; | |
147 | ||
148 | // read sources.list and find match | |
149 | vector<metaIndex *>::const_iterator I; | |
150 | pkgSourceList list; | |
151 | list.ReadMainList(); | |
152 | for(I=list.begin(); I != list.end(); I++) | |
153 | { | |
154 | string uristr = (*I)->GetURI(); | |
155 | std::cerr << "Checking: " << uristr << std::endl; | |
156 | if(uristr.substr(0,strlen("mirror://")) != string("mirror://")) | |
157 | continue; | |
158 | // find matching uri in sources.list | |
159 | if(mirror_uri_str.substr(0,uristr.size()) == uristr) | |
160 | { | |
161 | std::cerr << "found BaseURI: " << uristr << std::endl; | |
162 | BaseUri = uristr.substr(0,uristr.size()-1); | |
163 | } | |
164 | } | |
14e097c1 MV |
165 | string fetch = BaseUri; |
166 | fetch.replace(0,strlen("mirror://"),"http://"); | |
5f6b130d | 167 | |
70288656 | 168 | // get new file |
d731f9c5 | 169 | MirrorFile = _config->FindDir("Dir::State::mirrors") + URItoFileName(BaseUri); |
5f6b130d | 170 | |
14e097c1 MV |
171 | if(Debug) |
172 | { | |
173 | cerr << "base-uri: " << BaseUri << endl; | |
174 | cerr << "mirror-file: " << MirrorFile << endl; | |
175 | } | |
5f6b130d | 176 | |
d731f9c5 MV |
177 | // check the file, if it is not older than RefreshInterval just use it |
178 | // otherwise try to get a new one | |
179 | if(FileExists(MirrorFile)) | |
180 | { | |
181 | struct stat buf; | |
182 | time_t t,now,refresh; | |
183 | if(stat(MirrorFile.c_str(), &buf) != 0) | |
184 | return false; | |
185 | t = std::max(buf.st_mtime, buf.st_ctime); | |
186 | now = time(NULL); | |
187 | refresh = 60*_config->FindI("Acquire::Mirror::RefreshInterval",360); | |
188 | if(t + refresh > now) | |
189 | { | |
190 | if(Debug) | |
191 | clog << "Mirror file is in RefreshInterval" << endl; | |
192 | HasMirrorFile = true; | |
193 | return true; | |
194 | } | |
195 | if(Debug) | |
70288656 | 196 | clog << "Mirror file " << MirrorFile << " older than " << refresh << "min, re-download it" << endl; |
d731f9c5 MV |
197 | } |
198 | ||
199 | // not that great to use pkgAcquire here, but we do not have | |
200 | // any other way right now | |
5f6b130d | 201 | pkgAcquire Fetcher; |
14e097c1 | 202 | new pkgAcqFile(&Fetcher, fetch, "", 0, "", "", "", MirrorFile); |
5f6b130d | 203 | bool res = (Fetcher.Run() == pkgAcquire::Continue); |
d731f9c5 | 204 | if(res) |
5f6b130d MV |
205 | HasMirrorFile = true; |
206 | Fetcher.Shutdown(); | |
d731f9c5 | 207 | return res; |
5f6b130d MV |
208 | } |
209 | ||
210 | bool MirrorMethod::SelectMirror() | |
211 | { | |
d731f9c5 MV |
212 | // FIXME: make the mirror selection more clever, do not |
213 | // just use the first one! | |
5f6b130d MV |
214 | ifstream in(MirrorFile.c_str()); |
215 | getline(in, Mirror); | |
14e097c1 MV |
216 | if(Debug) |
217 | cerr << "Using mirror: " << Mirror << endl; | |
36280399 MV |
218 | |
219 | UsedMirror = Mirror; | |
14e097c1 | 220 | return true; |
5f6b130d MV |
221 | } |
222 | ||
223 | // MirrorMethod::Fetch - Fetch an item /*{{{*/ | |
224 | // --------------------------------------------------------------------- | |
225 | /* This adds an item to the pipeline. We keep the pipeline at a fixed | |
226 | depth. */ | |
227 | bool MirrorMethod::Fetch(FetchItem *Itm) | |
228 | { | |
d731f9c5 | 229 | // select mirror only once per session |
5f6b130d MV |
230 | if(!HasMirrorFile) |
231 | { | |
70288656 | 232 | Clean(_config->FindDir("Dir::State::mirrors")); |
5f6b130d MV |
233 | GetMirrorFile(Itm->Uri); |
234 | SelectMirror(); | |
235 | } | |
236 | ||
ef1e6d88 MV |
237 | for (FetchItem *I = Queue; I != 0; I = I->Next) |
238 | { | |
239 | if(I->Uri.find("mirror://") != string::npos) | |
240 | I->Uri.replace(0,BaseUri.size(),Mirror); | |
241 | } | |
5f6b130d | 242 | |
14e097c1 MV |
243 | // now run the real fetcher |
244 | return HttpMethod::Fetch(Itm); | |
5f6b130d MV |
245 | }; |
246 | ||
14e097c1 MV |
247 | void MirrorMethod::Fail(string Err,bool Transient) |
248 | { | |
249 | if(Queue->Uri.find("http://") != string::npos) | |
250 | Queue->Uri.replace(0,Mirror.size(), BaseUri); | |
251 | pkgAcqMethod::Fail(Err, Transient); | |
252 | } | |
253 | ||
254 | void MirrorMethod::URIStart(FetchResult &Res) | |
255 | { | |
256 | if(Queue->Uri.find("http://") != string::npos) | |
257 | Queue->Uri.replace(0,Mirror.size(), BaseUri); | |
258 | pkgAcqMethod::URIStart(Res); | |
259 | } | |
260 | ||
261 | void MirrorMethod::URIDone(FetchResult &Res,FetchResult *Alt) | |
262 | { | |
263 | if(Queue->Uri.find("http://") != string::npos) | |
264 | Queue->Uri.replace(0,Mirror.size(), BaseUri); | |
265 | pkgAcqMethod::URIDone(Res, Alt); | |
266 | } | |
267 | ||
268 | ||
5f6b130d MV |
269 | int main() |
270 | { | |
271 | setlocale(LC_ALL, ""); | |
272 | ||
273 | MirrorMethod Mth; | |
274 | ||
14e097c1 | 275 | return Mth.Loop(); |
5f6b130d MV |
276 | } |
277 | ||
278 |