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