mirror.cc 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. // -*- mode: cpp; mode: fold -*-
  2. // Description /*{{{*/
  3. // $Id: mirror.cc,v 1.59 2004/05/08 19:42:35 mdz Exp $
  4. /* ######################################################################
  5. Mirror Aquire Method - This is the Mirror aquire method for APT.
  6. ##################################################################### */
  7. /*}}}*/
  8. // Include Files /*{{{*/
  9. #include <apt-pkg/fileutl.h>
  10. #include <apt-pkg/acquire-method.h>
  11. #include <apt-pkg/acquire-item.h>
  12. #include <apt-pkg/acquire.h>
  13. #include <apt-pkg/error.h>
  14. #include <apt-pkg/hashes.h>
  15. #include <apt-pkg/sourcelist.h>
  16. #include <fstream>
  17. #include <iostream>
  18. #include <stdarg.h>
  19. #include <sys/stat.h>
  20. #include <sys/types.h>
  21. #include <dirent.h>
  22. using namespace std;
  23. #include "mirror.h"
  24. #include "http.h"
  25. #include "apti18n.h"
  26. /*}}}*/
  27. /* Done:
  28. * - works with http only
  29. * - always picks the first mirror from the list
  30. * - call out to problem reporting script
  31. * - supports "deb mirror://host/path/to/mirror-list/// dist component"
  32. *
  33. * TODO:
  34. * what about gpgv failures? this should call-out to the problem reporting
  35. script, but we need to know what mirror was used
  36. * better standard format for errors to send back
  37. * - implement failure reporting at the pkgAcquire::Item::Failed() level
  38. but then we need to send back what uri exactly was failing
  39. [mvo: the problem with this approach is ::Failed() is not really
  40. called for all failures :/ e.g. md5sum mismatch in a archive
  41. is not]
  42. * - deal with runing as non-root because we can't write to the lists
  43. dir then -> use the cached mirror file
  44. * - better method to download than having a pkgAcquire interface here
  45. * - magicmarker is (a bit) evil, maybe just use a similar approach as in
  46. clean and read the sources.list and use the GetURI() method to find
  47. the prefix?
  48. * support more than http
  49. * - testing :)
  50. */
  51. MirrorMethod::MirrorMethod()
  52. : HttpMethod(), HasMirrorFile(false)
  53. {
  54. #if 0
  55. HasMirrorFile=true;
  56. BaseUri="mirror://people.ubuntu.com/~mvo/mirror/mirrors";
  57. MirrorFile="/var/lib/apt/lists/people.ubuntu.com_%7emvo_apt_mirror_mirrors";
  58. Mirror="http://de.archive.ubuntu.com/ubuntu/";
  59. #endif
  60. };
  61. // HttpMethod::Configuration - Handle a configuration message /*{{{*/
  62. // ---------------------------------------------------------------------
  63. /* We stash the desired pipeline depth */
  64. bool MirrorMethod::Configuration(string Message)
  65. {
  66. if (pkgAcqMethod::Configuration(Message) == false)
  67. return false;
  68. Debug = _config->FindB("Debug::Acquire::mirror",false);
  69. return true;
  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. if(Debug)
  77. clog << "MirrorMethod::Clean(): " << Dir << endl;
  78. // read sources.list
  79. pkgSourceList list;
  80. list.ReadMainList();
  81. DIR *D = opendir(Dir.c_str());
  82. if (D == 0)
  83. return _error->Errno("opendir",_("Unable to read %s"),Dir.c_str());
  84. string StartDir = SafeGetCWD();
  85. if (chdir(Dir.c_str()) != 0)
  86. {
  87. closedir(D);
  88. return _error->Errno("chdir",_("Unable to change to %s"),Dir.c_str());
  89. }
  90. for (struct dirent *Dir = readdir(D); Dir != 0; Dir = readdir(D))
  91. {
  92. // Skip some files..
  93. if (strcmp(Dir->d_name,"lock") == 0 ||
  94. strcmp(Dir->d_name,"partial") == 0 ||
  95. strcmp(Dir->d_name,".") == 0 ||
  96. strcmp(Dir->d_name,"..") == 0)
  97. continue;
  98. // see if we have that uri
  99. for(I=list.begin(); I != list.end(); I++)
  100. {
  101. string uri = (*I)->GetURI();
  102. if(uri.substr(0,strlen("mirror://")) != string("mirror://"))
  103. continue;
  104. string Marker = _config->Find("Acquire::Mirror::MagicMarker","///");
  105. string BaseUri = uri.substr(0,uri.find(Marker));
  106. if (URItoFileName(BaseUri) == Dir->d_name)
  107. break;
  108. }
  109. // nothing found, nuke it
  110. if (I == list.end())
  111. unlink(Dir->d_name);
  112. };
  113. chdir(StartDir.c_str());
  114. closedir(D);
  115. return true;
  116. }
  117. bool MirrorMethod::GetMirrorFile(string uri)
  118. {
  119. string Marker = _config->Find("Acquire::Mirror::MagicMarker","///");
  120. BaseUri = uri.substr(0,uri.find(Marker));
  121. string fetch = BaseUri;
  122. fetch.replace(0,strlen("mirror://"),"http://");
  123. // get new file
  124. MirrorFile = _config->FindDir("Dir::State::mirrors") + URItoFileName(BaseUri);
  125. if(Debug)
  126. {
  127. cerr << "base-uri: " << BaseUri << endl;
  128. cerr << "mirror-file: " << MirrorFile << endl;
  129. }
  130. // check the file, if it is not older than RefreshInterval just use it
  131. // otherwise try to get a new one
  132. if(FileExists(MirrorFile))
  133. {
  134. struct stat buf;
  135. time_t t,now,refresh;
  136. if(stat(MirrorFile.c_str(), &buf) != 0)
  137. return false;
  138. t = std::max(buf.st_mtime, buf.st_ctime);
  139. now = time(NULL);
  140. refresh = 60*_config->FindI("Acquire::Mirror::RefreshInterval",360);
  141. if(t + refresh > now)
  142. {
  143. if(Debug)
  144. clog << "Mirror file is in RefreshInterval" << endl;
  145. HasMirrorFile = true;
  146. return true;
  147. }
  148. if(Debug)
  149. clog << "Mirror file " << MirrorFile << " older than " << refresh << "min, re-download it" << endl;
  150. }
  151. // not that great to use pkgAcquire here, but we do not have
  152. // any other way right now
  153. pkgAcquire Fetcher;
  154. new pkgAcqFile(&Fetcher, fetch, "", 0, "", "", "", MirrorFile);
  155. bool res = (Fetcher.Run() == pkgAcquire::Continue);
  156. if(res)
  157. HasMirrorFile = true;
  158. Fetcher.Shutdown();
  159. return res;
  160. }
  161. bool MirrorMethod::SelectMirror()
  162. {
  163. // FIXME: make the mirror selection more clever, do not
  164. // just use the first one!
  165. ifstream in(MirrorFile.c_str());
  166. getline(in, Mirror);
  167. if(Debug)
  168. cerr << "Using mirror: " << Mirror << endl;
  169. return true;
  170. }
  171. // MirrorMethod::Fetch - Fetch an item /*{{{*/
  172. // ---------------------------------------------------------------------
  173. /* This adds an item to the pipeline. We keep the pipeline at a fixed
  174. depth. */
  175. bool MirrorMethod::Fetch(FetchItem *Itm)
  176. {
  177. // select mirror only once per session
  178. if(!HasMirrorFile)
  179. {
  180. Clean(_config->FindDir("Dir::State::mirrors"));
  181. GetMirrorFile(Itm->Uri);
  182. SelectMirror();
  183. }
  184. for (FetchItem *I = Queue; I != 0; I = I->Next)
  185. {
  186. if(I->Uri.find("mirror://") != string::npos)
  187. I->Uri.replace(0,BaseUri.size(),Mirror);
  188. }
  189. // now run the real fetcher
  190. return HttpMethod::Fetch(Itm);
  191. };
  192. void MirrorMethod::Fail(string Err,bool Transient)
  193. {
  194. // FIXME: queue next mirror?
  195. ReportMirrorFailure(Err);
  196. if(Queue->Uri.find("http://") != string::npos)
  197. Queue->Uri.replace(0,Mirror.size(), BaseUri);
  198. pkgAcqMethod::Fail(Err, Transient);
  199. }
  200. void MirrorMethod::URIStart(FetchResult &Res)
  201. {
  202. if(Queue->Uri.find("http://") != string::npos)
  203. Queue->Uri.replace(0,Mirror.size(), BaseUri);
  204. pkgAcqMethod::URIStart(Res);
  205. }
  206. void MirrorMethod::URIDone(FetchResult &Res,FetchResult *Alt)
  207. {
  208. // FIXME: queue next mirror?
  209. if(Queue->ExpectedMD5 != "" && Res.MD5Sum != Queue->ExpectedMD5)
  210. ReportMirrorFailure("499 Hash mismatch");
  211. if(Queue->Uri.find("http://") != string::npos)
  212. Queue->Uri.replace(0,Mirror.size(), BaseUri);
  213. pkgAcqMethod::URIDone(Res, Alt);
  214. }
  215. void MirrorMethod::ReportMirrorFailure(string FailCode)
  216. {
  217. // report that Queue->Uri failed
  218. #if 0
  219. std::cerr << "\nReportMirrorFailure: "
  220. << Queue->Uri
  221. << " FailCode: "
  222. << FailCode << std::endl;
  223. #endif
  224. const char *Args[40];
  225. unsigned int i = 0;
  226. string report = _config->Find("Methods::Mirror::ProblemReporting",
  227. "/usr/lib/apt/report-mirror-failure");
  228. Args[i++] = report.c_str();
  229. Args[i++] = Queue->Uri.c_str();
  230. Args[i++] = FailCode.c_str();
  231. pid_t pid = ExecFork();
  232. if(pid < 0)
  233. {
  234. _error->Error("ReportMirrorFailure Fork failed");
  235. return;
  236. }
  237. else if(pid == 0)
  238. {
  239. execvp(report.c_str(), (char**)Args);
  240. }
  241. if(!ExecWait(pid, "report-mirror-failure"))
  242. {
  243. _error->Warning("Couldn't report problem to '%s'",
  244. _config->Find("Acquire::Mirror::ReportFailures").c_str());
  245. }
  246. }
  247. int main()
  248. {
  249. setlocale(LC_ALL, "");
  250. MirrorMethod Mth;
  251. return Mth.Loop();
  252. }