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