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