summaryrefslogtreecommitdiff
path: root/methods/mirror.cc
blob: 8f9b8ed343014396848e1cbd0ef92db23a2aa588 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
// -*- mode: cpp; mode: fold -*-
// Description								/*{{{*/
// $Id: mirror.cc,v 1.59 2004/05/08 19:42:35 mdz Exp $
/* ######################################################################

   Mirror Aquire Method - This is the Mirror aquire method for APT.
   
   ##################################################################### */
									/*}}}*/
// Include Files							/*{{{*/
#include <apt-pkg/fileutl.h>
#include <apt-pkg/acquire-method.h>
#include <apt-pkg/acquire-item.h>
#include <apt-pkg/acquire.h>
#include <apt-pkg/error.h>
#include <apt-pkg/hashes.h>
#include <apt-pkg/sourcelist.h>

#include <fstream>
#include <iostream>
#include <stdarg.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <dirent.h>

using namespace std;

#include "mirror.h"
#include "http.h"
#include "apti18n.h"
									/*}}}*/

/* Done:
 * - works with http only
 * - always picks the first mirror from the list
 * - call out to problem reporting script
 * - supports "deb mirror://host/path/to/mirror-list/// dist component"
 * 
 * TODO: 
 * what about gpgv  failures? this should call-out to the problem reporting
   script, but we need to know what mirror was used
 * better standard format for errors to send back 
 * - implement failure reporting  at the pkgAcquire::Item::Failed() level 
     but then we need to send back what uri exactly was failing 
     [mvo: the problem with this approach is ::Failed() is not really
           called for all failures :/ e.g. md5sum mismatch in a archive
           is not]
 * - deal with runing as non-root because we can't write to the lists 
     dir then -> use the cached mirror file
 * - better method to download than having a pkgAcquire interface here
 * - magicmarker is (a bit) evil, maybe just use a similar approach as in
     clean and read the sources.list and use the GetURI() method to find
     the prefix?
 * support more than http
 * - testing :)
 */

MirrorMethod::MirrorMethod()
   : HttpMethod(), HasMirrorFile(false)
{
#if 0
   HasMirrorFile=true;
   BaseUri="mirror://people.ubuntu.com/~mvo/mirror/mirrors";
   MirrorFile="/var/lib/apt/lists/people.ubuntu.com_%7emvo_apt_mirror_mirrors";
   Mirror="http://de.archive.ubuntu.com/ubuntu/";
#endif
};

// HttpMethod::Configuration - Handle a configuration message		/*{{{*/
// ---------------------------------------------------------------------
/* We stash the desired pipeline depth */
bool MirrorMethod::Configuration(string Message)
{
   if (pkgAcqMethod::Configuration(Message) == false)
      return false;
   Debug = _config->FindB("Debug::Acquire::mirror",false);
   
   return true;
}
									/*}}}*/

// clean the mirrors dir based on ttl information
bool MirrorMethod::Clean(string Dir)
{
   vector<metaIndex *>::const_iterator I;

   if(Debug)
      clog << "MirrorMethod::Clean(): " << Dir << endl;

   // read sources.list
   pkgSourceList list;
   list.ReadMainList();

   DIR *D = opendir(Dir.c_str());   
   if (D == 0)
      return _error->Errno("opendir",_("Unable to read %s"),Dir.c_str());
   
   string StartDir = SafeGetCWD();
   if (chdir(Dir.c_str()) != 0)
   {
      closedir(D);
      return _error->Errno("chdir",_("Unable to change to %s"),Dir.c_str());
   }
   
   for (struct dirent *Dir = readdir(D); Dir != 0; Dir = readdir(D))
   {
      // Skip some files..
      if (strcmp(Dir->d_name,"lock") == 0 ||
	  strcmp(Dir->d_name,"partial") == 0 ||
	  strcmp(Dir->d_name,".") == 0 ||
	  strcmp(Dir->d_name,"..") == 0)
	 continue;

      // see if we have that uri
      for(I=list.begin(); I != list.end(); I++)
      {
	 string uri = (*I)->GetURI();
	 if(uri.substr(0,strlen("mirror://")) != string("mirror://"))
	    continue;
	 string Marker = _config->Find("Acquire::Mirror::MagicMarker","///");
	 string BaseUri = uri.substr(0,uri.find(Marker));
	 if (URItoFileName(BaseUri) == Dir->d_name)
	    break;
      }
      // nothing found, nuke it
      if (I == list.end())
	 unlink(Dir->d_name);
   };
   
   chdir(StartDir.c_str());
   closedir(D);
   return true;   
}


bool MirrorMethod::GetMirrorFile(string uri)
{
   string Marker = _config->Find("Acquire::Mirror::MagicMarker","///");
   BaseUri = uri.substr(0,uri.find(Marker));

   string fetch = BaseUri;
   fetch.replace(0,strlen("mirror://"),"http://");

   // get new file
   MirrorFile = _config->FindDir("Dir::State::mirrors") + URItoFileName(BaseUri);

   if(Debug) 
   {
      cerr << "base-uri: " << BaseUri << endl;
      cerr << "mirror-file: " << MirrorFile << endl;
   }

   // check the file, if it is not older than RefreshInterval just use it
   // otherwise try to get a new one
   if(FileExists(MirrorFile)) 
   {
      struct stat buf;
      time_t t,now,refresh;
      if(stat(MirrorFile.c_str(), &buf) != 0)
	 return false;
      t = std::max(buf.st_mtime, buf.st_ctime);
      now = time(NULL);
      refresh = 60*_config->FindI("Acquire::Mirror::RefreshInterval",360);
      if(t + refresh > now)
      {
	 if(Debug)
	    clog << "Mirror file is in RefreshInterval" << endl;
	 HasMirrorFile = true;
	 return true;
      }
      if(Debug)
	 clog << "Mirror file " << MirrorFile << " older than " << refresh << "min, re-download it" << endl;
   }

   // not that great to use pkgAcquire here, but we do not have 
   // any other way right now
   pkgAcquire Fetcher;
   new pkgAcqFile(&Fetcher, fetch, "", 0, "", "", "", MirrorFile);
   bool res = (Fetcher.Run() == pkgAcquire::Continue);
   if(res)
      HasMirrorFile = true;
   Fetcher.Shutdown();
   return res;
}

bool MirrorMethod::SelectMirror()
{
   // FIXME: make the mirror selection more clever, do not 
   //        just use the first one!
   ifstream in(MirrorFile.c_str());
   getline(in, Mirror);
   if(Debug)
      cerr << "Using mirror: " << Mirror << endl;
   return true;
}

// MirrorMethod::Fetch - Fetch an item					/*{{{*/
// ---------------------------------------------------------------------
/* This adds an item to the pipeline. We keep the pipeline at a fixed
   depth. */
bool MirrorMethod::Fetch(FetchItem *Itm)
{
   // select mirror only once per session
   if(!HasMirrorFile)
   {
      Clean(_config->FindDir("Dir::State::mirrors"));
      GetMirrorFile(Itm->Uri);
      SelectMirror();
   }

   for (FetchItem *I = Queue; I != 0; I = I->Next)
   {
      if(I->Uri.find("mirror://") != string::npos)
	 I->Uri.replace(0,BaseUri.size(),Mirror);
   }

   // now run the real fetcher
   return HttpMethod::Fetch(Itm);
};

void MirrorMethod::Fail(string Err,bool Transient)
{
   // FIXME: queue next mirror?
   ReportMirrorFailure(Err);

   if(Queue->Uri.find("http://") != string::npos)
      Queue->Uri.replace(0,Mirror.size(), BaseUri);
   pkgAcqMethod::Fail(Err, Transient);
}

void MirrorMethod::URIStart(FetchResult &Res)
{
   if(Queue->Uri.find("http://") != string::npos)
      Queue->Uri.replace(0,Mirror.size(), BaseUri);
   pkgAcqMethod::URIStart(Res);
}

void MirrorMethod::URIDone(FetchResult &Res,FetchResult *Alt)
{
   // FIXME: queue next mirror?
   if(Queue->ExpectedMD5 != "" && Res.MD5Sum != Queue->ExpectedMD5)
      ReportMirrorFailure("499 Hash mismatch");

   if(Queue->Uri.find("http://") != string::npos)
      Queue->Uri.replace(0,Mirror.size(), BaseUri);
   pkgAcqMethod::URIDone(Res, Alt);
}

void MirrorMethod::ReportMirrorFailure(string FailCode)
{
   // report that Queue->Uri failed
#if 0
   std::cerr << "\nReportMirrorFailure: " 
	     << Queue->Uri
	     << " FailCode: " 
	     << FailCode << std::endl;
#endif
   const char *Args[40];
   unsigned int i = 0;
   string report = _config->Find("Methods::Mirror::ProblemReporting", 
				 "/usr/lib/apt/report-mirror-failure");
   Args[i++] = report.c_str();
   Args[i++] = Queue->Uri.c_str();
   Args[i++] = FailCode.c_str();
   pid_t pid = ExecFork();
   if(pid < 0) 
   {
      _error->Error("ReportMirrorFailure Fork failed");
      return;
   }
   else if(pid == 0) 
   {
      execvp(report.c_str(), (char**)Args);
   }
   if(!ExecWait(pid, "report-mirror-failure")) 
   {
      _error->Warning("Couldn't report problem to '%s'",
		      _config->Find("Acquire::Mirror::ReportFailures").c_str());
   }
}

int main()
{
   setlocale(LC_ALL, "");

   MirrorMethod Mth;

   return Mth.Loop();
}