* debian/apt.postinst:
[ntk/apt.git] / methods / mirror.cc
CommitLineData
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
26using 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"
a577a938 38 * - uses pkgAcqMethod::FailReason() to have a string representation
59271f62 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
3f599bb7 45 * and better error handling there!
066b53e9 46 * - support more than http
86c17f0a 47 * - testing :)
86c17f0a
MV
48 */
49
5f6b130d 50MirrorMethod::MirrorMethod()
38eedeb7 51 : HttpMethod(), DownloadedMirrorFile(false)
5f6b130d 52{
5f6b130d
MV
53};
54
14e097c1
MV
55// HttpMethod::Configuration - Handle a configuration message /*{{{*/
56// ---------------------------------------------------------------------
57/* We stash the desired pipeline depth */
58bool 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
d731f9c5 68// clean the mirrors dir based on ttl information
70288656 69bool MirrorMethod::Clean(string Dir)
d731f9c5 70{
0c312e0e
MV
71 vector<metaIndex *>::const_iterator I;
72
73 if(Debug)
74 clog << "MirrorMethod::Clean(): " << Dir << endl;
75
38eedeb7
MV
76 if(Dir == "/")
77 return _error->Error("will not clean: '/'");
78
0c312e0e
MV
79 // read sources.list
80 pkgSourceList list;
81 list.ReadMainList();
70288656
MV
82
83 DIR *D = opendir(Dir.c_str());
84 if (D == 0)
85 return _error->Errno("opendir",_("Unable to read %s"),Dir.c_str());
86
87 string StartDir = SafeGetCWD();
88 if (chdir(Dir.c_str()) != 0)
89 {
90 closedir(D);
91 return _error->Errno("chdir",_("Unable to change to %s"),Dir.c_str());
92 }
93
94 for (struct dirent *Dir = readdir(D); Dir != 0; Dir = readdir(D))
95 {
96 // Skip some files..
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)
101 continue;
0c312e0e
MV
102
103 // see if we have that uri
104 for(I=list.begin(); I != list.end(); I++)
70288656 105 {
0c312e0e
MV
106 string uri = (*I)->GetURI();
107 if(uri.substr(0,strlen("mirror://")) != string("mirror://"))
108 continue;
066b53e9 109 string BaseUri = uri.substr(0,uri.size()-1);
0c312e0e
MV
110 if (URItoFileName(BaseUri) == Dir->d_name)
111 break;
70288656 112 }
0c312e0e
MV
113 // nothing found, nuke it
114 if (I == list.end())
70288656 115 unlink(Dir->d_name);
70288656 116 };
d731f9c5 117
70288656
MV
118 chdir(StartDir.c_str());
119 closedir(D);
120 return true;
d731f9c5
MV
121}
122
14e097c1 123
38eedeb7
MV
124bool MirrorMethod::DownloadMirrorFile(string mirror_uri_str)
125{
5dad4134
MV
126 if(Debug)
127 clog << "MirrorMethod::DownloadMirrorFile(): " << endl;
38eedeb7
MV
128
129 // check the file, if it is not older than RefreshInterval just use it
130 // otherwise try to get a new one
131 if(FileExists(MirrorFile))
132 {
133 struct stat buf;
134 time_t t,now,refresh;
135 if(stat(MirrorFile.c_str(), &buf) != 0)
136 return false;
137 t = std::max(buf.st_mtime, buf.st_ctime);
138 now = time(NULL);
139 refresh = 60*_config->FindI("Acquire::Mirror::RefreshInterval",360);
140 if(t + refresh > now)
141 {
142 if(Debug)
143 clog << "Mirror file is in RefreshInterval" << endl;
144 DownloadedMirrorFile = true;
145 return true;
146 }
147 if(Debug)
148 clog << "Mirror file " << MirrorFile << " older than " << refresh << "min, re-download it" << endl;
149 }
150
151 // not that great to use pkgAcquire here, but we do not have
152 // any other way right now
153 string fetch = BaseUri;
154 fetch.replace(0,strlen("mirror://"),"http://");
155
156 pkgAcquire Fetcher;
157 new pkgAcqFile(&Fetcher, fetch, "", 0, "", "", "", MirrorFile);
158 bool res = (Fetcher.Run() == pkgAcquire::Continue);
159 if(res)
160 DownloadedMirrorFile = true;
161 Fetcher.Shutdown();
162 return res;
163}
164
165bool MirrorMethod::SelectMirror()
166{
167 // if we do not have a MirrorFile, fallback
168 if(!FileExists(MirrorFile))
169 {
170 // FIXME: fallback to a default mirror here instead
171 // and provide a config option to define that default
172 return _error->Error(_("No mirror file '%s' found "), MirrorFile.c_str());
173 }
174
175 // FIXME: make the mirror selection more clever, do not
176 // just use the first one!
177 // BUT: we can not make this random, the mirror has to be
178 // stable accross session, because otherwise we can
179 // get into sync issues (got indexfiles from mirror A,
180 // but packages from mirror B - one might be out of date etc)
181 ifstream in(MirrorFile.c_str());
182 getline(in, Mirror);
183 if(Debug)
184 cerr << "Using mirror: " << Mirror << endl;
185
186 UsedMirror = Mirror;
187 return true;
188}
189
190string MirrorMethod::GetMirrorFileName(string mirror_uri_str)
5f6b130d 191{
066b53e9
MV
192 /*
193 - a mirror_uri_str looks like this:
194 mirror://people.ubuntu.com/~mvo/apt/mirror/mirrors/dists/feisty/Release.gpg
195
196 - the matching source.list entry
197 deb mirror://people.ubuntu.com/~mvo/apt/mirror/mirrors feisty main
198
199 - we actually want to go after:
200 http://people.ubuntu.com/~mvo/apt/mirror/mirrors
201
202 And we need to save the BaseUri for later:
203 - mirror://people.ubuntu.com/~mvo/apt/mirror/mirrors
204
205 FIXME: what if we have two similar prefixes?
206 mirror://people.ubuntu.com/~mvo/mirror
207 mirror://people.ubuntu.com/~mvo/mirror2
208 then mirror_uri_str looks like:
209 mirror://people.ubuntu.com/~mvo/apt/mirror/dists/feisty/Release.gpg
210 mirror://people.ubuntu.com/~mvo/apt/mirror2/dists/feisty/Release.gpg
211 we search sources.list and find:
212 mirror://people.ubuntu.com/~mvo/apt/mirror
213 in both cases! So we need to apply some domain knowledge here :( and
214 check for /dists/ or /Release.gpg as suffixes
215 */
38eedeb7 216 string name;
f0b509cd 217 if(Debug)
38eedeb7 218 std::cerr << "GetMirrorFileName: " << mirror_uri_str << std::endl;
066b53e9
MV
219
220 // read sources.list and find match
221 vector<metaIndex *>::const_iterator I;
222 pkgSourceList list;
223 list.ReadMainList();
224 for(I=list.begin(); I != list.end(); I++)
225 {
226 string uristr = (*I)->GetURI();
f0b509cd
MV
227 if(Debug)
228 std::cerr << "Checking: " << uristr << std::endl;
066b53e9
MV
229 if(uristr.substr(0,strlen("mirror://")) != string("mirror://"))
230 continue;
231 // find matching uri in sources.list
232 if(mirror_uri_str.substr(0,uristr.size()) == uristr)
233 {
f0b509cd
MV
234 if(Debug)
235 std::cerr << "found BaseURI: " << uristr << std::endl;
066b53e9
MV
236 BaseUri = uristr.substr(0,uristr.size()-1);
237 }
238 }
70288656 239 // get new file
38eedeb7 240 name = _config->FindDir("Dir::State::mirrors") + URItoFileName(BaseUri);
5f6b130d 241
14e097c1
MV
242 if(Debug)
243 {
244 cerr << "base-uri: " << BaseUri << endl;
38eedeb7 245 cerr << "mirror-file: " << name << endl;
d731f9c5 246 }
38eedeb7 247 return name;
5f6b130d
MV
248}
249
250// MirrorMethod::Fetch - Fetch an item /*{{{*/
251// ---------------------------------------------------------------------
252/* This adds an item to the pipeline. We keep the pipeline at a fixed
253 depth. */
254bool MirrorMethod::Fetch(FetchItem *Itm)
255{
5dad4134
MV
256 if(Debug)
257 clog << "MirrorMethod::Fetch()" << endl;
258
38eedeb7
MV
259 // the http method uses Fetch(0) as a way to update the pipeline,
260 // just let it do its work in this case - Fetch() with a valid
261 // Itm will always run before the first Fetch(0)
262 if(Itm == NULL)
263 return HttpMethod::Fetch(Itm);
264
265 // if we don't have the name of the mirror file on disk yet,
266 // calculate it now (can be derived from the uri)
267 if(MirrorFile.empty())
268 MirrorFile = GetMirrorFileName(Itm->Uri);
269
270 // download mirror file once (if we are after index files)
271 if(Itm->IndexFile && !DownloadedMirrorFile)
5f6b130d 272 {
70288656 273 Clean(_config->FindDir("Dir::State::mirrors"));
38eedeb7 274 DownloadMirrorFile(Itm->Uri);
5f6b130d
MV
275 }
276
5dad4134
MV
277 if(Mirror.empty()) {
278 if(!SelectMirror()) {
279 // no valid mirror selected, something went wrong downloading
280 // from the master mirror site most likely and there is
281 // no old mirror file availalbe
282 return false;
283 }
284 }
285 if(Debug)
286 clog << "selected mirror: " << Mirror << endl;
287
38eedeb7 288
ef1e6d88
MV
289 for (FetchItem *I = Queue; I != 0; I = I->Next)
290 {
291 if(I->Uri.find("mirror://") != string::npos)
38eedeb7 292 I->Uri.replace(0,BaseUri.size(), Mirror);
ef1e6d88 293 }
38eedeb7 294
14e097c1
MV
295 // now run the real fetcher
296 return HttpMethod::Fetch(Itm);
5f6b130d
MV
297};
298
14e097c1
MV
299void MirrorMethod::Fail(string Err,bool Transient)
300{
301 if(Queue->Uri.find("http://") != string::npos)
302 Queue->Uri.replace(0,Mirror.size(), BaseUri);
303 pkgAcqMethod::Fail(Err, Transient);
304}
305
306void MirrorMethod::URIStart(FetchResult &Res)
307{
308 if(Queue->Uri.find("http://") != string::npos)
309 Queue->Uri.replace(0,Mirror.size(), BaseUri);
310 pkgAcqMethod::URIStart(Res);
311}
312
313void MirrorMethod::URIDone(FetchResult &Res,FetchResult *Alt)
314{
315 if(Queue->Uri.find("http://") != string::npos)
316 Queue->Uri.replace(0,Mirror.size(), BaseUri);
317 pkgAcqMethod::URIDone(Res, Alt);
318}
319
320
5f6b130d
MV
321int main()
322{
323 setlocale(LC_ALL, "");
324
325 MirrorMethod Mth;
326
14e097c1 327 return Mth.Loop();
5f6b130d
MV
328}
329
330