]> git.saurik.com Git - apt.git/blame - methods/mirror.cc
* implemented proper mirror list cleanup
[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
86c17f0a
MV
33/*
34 * TODO:
d731f9c5
MV
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
86c17f0a 43 * - better method to download than having a pkgAcquire interface here
d731f9c5 44 * - magicmarker is (a bit) evil
86c17f0a 45 * - testing :)
86c17f0a
MV
46 */
47
5f6b130d 48MirrorMethod::MirrorMethod()
14e097c1 49 : HttpMethod(), HasMirrorFile(false)
5f6b130d
MV
50{
51#if 0
52 HasMirrorFile=true;
14e097c1
MV
53 BaseUri="mirror://people.ubuntu.com/~mvo/mirror/mirrors";
54 MirrorFile="/var/lib/apt/lists/people.ubuntu.com_%7emvo_apt_mirror_mirrors";
5f6b130d
MV
55 Mirror="http://de.archive.ubuntu.com/ubuntu/";
56#endif
57};
58
14e097c1
MV
59// HttpMethod::Configuration - Handle a configuration message /*{{{*/
60// ---------------------------------------------------------------------
61/* We stash the desired pipeline depth */
62bool 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
d731f9c5 72// clean the mirrors dir based on ttl information
70288656 73bool MirrorMethod::Clean(string Dir)
d731f9c5 74{
0c312e0e
MV
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();
70288656
MV
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;
0c312e0e
MV
103
104 // see if we have that uri
105 for(I=list.begin(); I != list.end(); I++)
70288656 106 {
0c312e0e
MV
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;
70288656 114 }
0c312e0e
MV
115 // nothing found, nuke it
116 if (I == list.end())
70288656 117 unlink(Dir->d_name);
70288656 118 };
d731f9c5 119
70288656
MV
120 chdir(StartDir.c_str());
121 closedir(D);
122 return true;
d731f9c5
MV
123}
124
14e097c1 125
5f6b130d
MV
126bool MirrorMethod::GetMirrorFile(string uri)
127{
128 string Marker = _config->Find("Acquire::Mirror::MagicMarker","///");
129 BaseUri = uri.substr(0,uri.find(Marker));
14e097c1
MV
130
131 string fetch = BaseUri;
132 fetch.replace(0,strlen("mirror://"),"http://");
5f6b130d 133
70288656 134 // get new file
d731f9c5 135 MirrorFile = _config->FindDir("Dir::State::mirrors") + URItoFileName(BaseUri);
5f6b130d 136
14e097c1
MV
137 if(Debug)
138 {
139 cerr << "base-uri: " << BaseUri << endl;
140 cerr << "mirror-file: " << MirrorFile << endl;
141 }
5f6b130d 142
d731f9c5
MV
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)
70288656 162 clog << "Mirror file " << MirrorFile << " older than " << refresh << "min, re-download it" << endl;
d731f9c5
MV
163 }
164
165 // not that great to use pkgAcquire here, but we do not have
166 // any other way right now
5f6b130d 167 pkgAcquire Fetcher;
14e097c1 168 new pkgAcqFile(&Fetcher, fetch, "", 0, "", "", "", MirrorFile);
5f6b130d 169 bool res = (Fetcher.Run() == pkgAcquire::Continue);
d731f9c5 170 if(res)
5f6b130d
MV
171 HasMirrorFile = true;
172 Fetcher.Shutdown();
d731f9c5 173 return res;
5f6b130d
MV
174}
175
176bool MirrorMethod::SelectMirror()
177{
d731f9c5
MV
178 // FIXME: make the mirror selection more clever, do not
179 // just use the first one!
5f6b130d
MV
180 ifstream in(MirrorFile.c_str());
181 getline(in, Mirror);
14e097c1
MV
182 if(Debug)
183 cerr << "Using mirror: " << Mirror << endl;
184 return true;
5f6b130d
MV
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. */
191bool MirrorMethod::Fetch(FetchItem *Itm)
192{
d731f9c5 193 // select mirror only once per session
5f6b130d
MV
194 if(!HasMirrorFile)
195 {
70288656 196 Clean(_config->FindDir("Dir::State::mirrors"));
5f6b130d
MV
197 GetMirrorFile(Itm->Uri);
198 SelectMirror();
199 }
200
ef1e6d88
MV
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 }
5f6b130d 206
14e097c1
MV
207 // now run the real fetcher
208 return HttpMethod::Fetch(Itm);
5f6b130d
MV
209};
210
14e097c1
MV
211void 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
218void 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
225void 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
5f6b130d
MV
233int main()
234{
235 setlocale(LC_ALL, "");
236
237 MirrorMethod Mth;
238
14e097c1 239 return Mth.Loop();
5f6b130d
MV
240}
241
242