]> git.saurik.com Git - apt.git/blob - methods/mirror.cc
* get the mirror failure stuff really working
[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 *
39 * TODO:
40 * what about gpgv failures? this should call-out to the problem reporting
41 script, but we need to know what mirror was used -> just run pkgAcquire::Item::ReportMirrorFailure()
42 * better standard format for errors to send back
43 * - implement failure reporting at the pkgAcquire::Item::Failed() level
44 but then we need to send back what uri exactly was failing
45 [mvo: the problem with this approach is ::Failed() is not really
46 called for all failures :/ e.g. md5sum mismatch in a archive
47 is not]
48 * - deal with runing as non-root because we can't write to the lists
49 dir then -> use the cached mirror file
50 * - better method to download than having a pkgAcquire interface here
51 * - magicmarker is evil, maybe just use a similar approach as in
52 clean and read the sources.list and use the GetURI() method to find
53 the prefix?
54 * support more than http
55 * - testing :)
56 */
57
58 MirrorMethod::MirrorMethod()
59 : HttpMethod(), HasMirrorFile(false)
60 {
61 #if 0
62 HasMirrorFile=true;
63 BaseUri="mirror://people.ubuntu.com/~mvo/mirror/mirrors";
64 MirrorFile="/var/lib/apt/lists/people.ubuntu.com_%7emvo_apt_mirror_mirrors";
65 Mirror="http://de.archive.ubuntu.com/ubuntu/";
66 #endif
67 };
68
69 // HttpMethod::Configuration - Handle a configuration message /*{{{*/
70 // ---------------------------------------------------------------------
71 /* We stash the desired pipeline depth */
72 bool MirrorMethod::Configuration(string Message)
73 {
74 if (pkgAcqMethod::Configuration(Message) == false)
75 return false;
76 Debug = _config->FindB("Debug::Acquire::mirror",false);
77
78 return true;
79 }
80 /*}}}*/
81
82 // clean the mirrors dir based on ttl information
83 bool MirrorMethod::Clean(string Dir)
84 {
85 vector<metaIndex *>::const_iterator I;
86
87 if(Debug)
88 clog << "MirrorMethod::Clean(): " << Dir << endl;
89
90 // read sources.list
91 pkgSourceList list;
92 list.ReadMainList();
93
94 DIR *D = opendir(Dir.c_str());
95 if (D == 0)
96 return _error->Errno("opendir",_("Unable to read %s"),Dir.c_str());
97
98 string StartDir = SafeGetCWD();
99 if (chdir(Dir.c_str()) != 0)
100 {
101 closedir(D);
102 return _error->Errno("chdir",_("Unable to change to %s"),Dir.c_str());
103 }
104
105 for (struct dirent *Dir = readdir(D); Dir != 0; Dir = readdir(D))
106 {
107 // Skip some files..
108 if (strcmp(Dir->d_name,"lock") == 0 ||
109 strcmp(Dir->d_name,"partial") == 0 ||
110 strcmp(Dir->d_name,".") == 0 ||
111 strcmp(Dir->d_name,"..") == 0)
112 continue;
113
114 // see if we have that uri
115 for(I=list.begin(); I != list.end(); I++)
116 {
117 string uri = (*I)->GetURI();
118 if(uri.substr(0,strlen("mirror://")) != string("mirror://"))
119 continue;
120 string Marker = _config->Find("Acquire::Mirror::MagicMarker","///");
121 string BaseUri = uri.substr(0,uri.find(Marker));
122 if (URItoFileName(BaseUri) == Dir->d_name)
123 break;
124 }
125 // nothing found, nuke it
126 if (I == list.end())
127 unlink(Dir->d_name);
128 };
129
130 chdir(StartDir.c_str());
131 closedir(D);
132 return true;
133 }
134
135
136 bool MirrorMethod::GetMirrorFile(string uri)
137 {
138 string Marker = _config->Find("Acquire::Mirror::MagicMarker","///");
139 BaseUri = uri.substr(0,uri.find(Marker));
140
141 string fetch = BaseUri;
142 fetch.replace(0,strlen("mirror://"),"http://");
143
144 // get new file
145 MirrorFile = _config->FindDir("Dir::State::mirrors") + URItoFileName(BaseUri);
146
147 if(Debug)
148 {
149 cerr << "base-uri: " << BaseUri << endl;
150 cerr << "mirror-file: " << MirrorFile << endl;
151 }
152
153 // check the file, if it is not older than RefreshInterval just use it
154 // otherwise try to get a new one
155 if(FileExists(MirrorFile))
156 {
157 struct stat buf;
158 time_t t,now,refresh;
159 if(stat(MirrorFile.c_str(), &buf) != 0)
160 return false;
161 t = std::max(buf.st_mtime, buf.st_ctime);
162 now = time(NULL);
163 refresh = 60*_config->FindI("Acquire::Mirror::RefreshInterval",360);
164 if(t + refresh > now)
165 {
166 if(Debug)
167 clog << "Mirror file is in RefreshInterval" << endl;
168 HasMirrorFile = true;
169 return true;
170 }
171 if(Debug)
172 clog << "Mirror file " << MirrorFile << " older than " << refresh << "min, re-download it" << endl;
173 }
174
175 // not that great to use pkgAcquire here, but we do not have
176 // any other way right now
177 pkgAcquire Fetcher;
178 new pkgAcqFile(&Fetcher, fetch, "", 0, "", "", "", MirrorFile);
179 bool res = (Fetcher.Run() == pkgAcquire::Continue);
180 if(res)
181 HasMirrorFile = true;
182 Fetcher.Shutdown();
183 return res;
184 }
185
186 bool MirrorMethod::SelectMirror()
187 {
188 // FIXME: make the mirror selection more clever, do not
189 // just use the first one!
190 ifstream in(MirrorFile.c_str());
191 getline(in, Mirror);
192 if(Debug)
193 cerr << "Using mirror: " << Mirror << endl;
194
195 UsedMirror = Mirror;
196 return true;
197 }
198
199 // MirrorMethod::Fetch - Fetch an item /*{{{*/
200 // ---------------------------------------------------------------------
201 /* This adds an item to the pipeline. We keep the pipeline at a fixed
202 depth. */
203 bool MirrorMethod::Fetch(FetchItem *Itm)
204 {
205 // select mirror only once per session
206 if(!HasMirrorFile)
207 {
208 Clean(_config->FindDir("Dir::State::mirrors"));
209 GetMirrorFile(Itm->Uri);
210 SelectMirror();
211 }
212
213 for (FetchItem *I = Queue; I != 0; I = I->Next)
214 {
215 if(I->Uri.find("mirror://") != string::npos)
216 I->Uri.replace(0,BaseUri.size(),Mirror);
217 }
218
219 // now run the real fetcher
220 return HttpMethod::Fetch(Itm);
221 };
222
223 void MirrorMethod::Fail(string Err,bool Transient)
224 {
225 if(Queue->Uri.find("http://") != string::npos)
226 Queue->Uri.replace(0,Mirror.size(), BaseUri);
227 pkgAcqMethod::Fail(Err, Transient);
228 }
229
230 void MirrorMethod::URIStart(FetchResult &Res)
231 {
232 if(Queue->Uri.find("http://") != string::npos)
233 Queue->Uri.replace(0,Mirror.size(), BaseUri);
234 pkgAcqMethod::URIStart(Res);
235 }
236
237 void MirrorMethod::URIDone(FetchResult &Res,FetchResult *Alt)
238 {
239 if(Queue->Uri.find("http://") != string::npos)
240 Queue->Uri.replace(0,Mirror.size(), BaseUri);
241 pkgAcqMethod::URIDone(Res, Alt);
242 }
243
244
245 int main()
246 {
247 setlocale(LC_ALL, "");
248
249 MirrorMethod Mth;
250
251 return Mth.Loop();
252 }
253
254