mirror.cc 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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. /*
  28. * TODO:
  29. * - what about gpgv failures? better standard format for errors
  30. to send back to LP
  31. #OR#
  32. * - implement it at the pkgAcquire::Item::Failed() level but then
  33. we need to send back what uri exactly was failing
  34. [mvo: the problem with this approach is ::Failed() is not really
  35. called for all failures :/ e.g. md5sum mismatch in a archive
  36. is not]
  37. * - deal with runing as non-root because we can't write to the lists
  38. dir then -> use the cached mirror file
  39. * - better method to download than having a pkgAcquire interface here
  40. * - magicmarker is (a bit) evil, maybe just use a similar approach as in
  41. clean and read the sources.list and use the GetURI() method to find
  42. the prefix?
  43. * - testing :)
  44. */
  45. MirrorMethod::MirrorMethod()
  46. : HttpMethod(), HasMirrorFile(false)
  47. {
  48. #if 0
  49. HasMirrorFile=true;
  50. BaseUri="mirror://people.ubuntu.com/~mvo/mirror/mirrors";
  51. MirrorFile="/var/lib/apt/lists/people.ubuntu.com_%7emvo_apt_mirror_mirrors";
  52. Mirror="http://de.archive.ubuntu.com/ubuntu/";
  53. #endif
  54. };
  55. // HttpMethod::Configuration - Handle a configuration message /*{{{*/
  56. // ---------------------------------------------------------------------
  57. /* We stash the desired pipeline depth */
  58. bool MirrorMethod::Configuration(string Message)
  59. {
  60. if (pkgAcqMethod::Configuration(Message) == false)
  61. return false;
  62. Debug = _config->FindB("Debug::Acquire::mirror",false);
  63. return true;
  64. }
  65. /*}}}*/
  66. // clean the mirrors dir based on ttl information
  67. bool MirrorMethod::Clean(string Dir)
  68. {
  69. vector<metaIndex *>::const_iterator I;
  70. if(Debug)
  71. clog << "MirrorMethod::Clean(): " << Dir << endl;
  72. // read sources.list
  73. pkgSourceList list;
  74. list.ReadMainList();
  75. DIR *D = opendir(Dir.c_str());
  76. if (D == 0)
  77. return _error->Errno("opendir",_("Unable to read %s"),Dir.c_str());
  78. string StartDir = SafeGetCWD();
  79. if (chdir(Dir.c_str()) != 0)
  80. {
  81. closedir(D);
  82. return _error->Errno("chdir",_("Unable to change to %s"),Dir.c_str());
  83. }
  84. for (struct dirent *Dir = readdir(D); Dir != 0; Dir = readdir(D))
  85. {
  86. // Skip some files..
  87. if (strcmp(Dir->d_name,"lock") == 0 ||
  88. strcmp(Dir->d_name,"partial") == 0 ||
  89. strcmp(Dir->d_name,".") == 0 ||
  90. strcmp(Dir->d_name,"..") == 0)
  91. continue;
  92. // see if we have that uri
  93. for(I=list.begin(); I != list.end(); I++)
  94. {
  95. string uri = (*I)->GetURI();
  96. if(uri.substr(0,strlen("mirror://")) != string("mirror://"))
  97. continue;
  98. string Marker = _config->Find("Acquire::Mirror::MagicMarker","///");
  99. string BaseUri = uri.substr(0,uri.find(Marker));
  100. if (URItoFileName(BaseUri) == Dir->d_name)
  101. break;
  102. }
  103. // nothing found, nuke it
  104. if (I == list.end())
  105. unlink(Dir->d_name);
  106. };
  107. chdir(StartDir.c_str());
  108. closedir(D);
  109. return true;
  110. }
  111. bool MirrorMethod::GetMirrorFile(string uri)
  112. {
  113. string Marker = _config->Find("Acquire::Mirror::MagicMarker","///");
  114. BaseUri = uri.substr(0,uri.find(Marker));
  115. string fetch = BaseUri;
  116. fetch.replace(0,strlen("mirror://"),"http://");
  117. // get new file
  118. MirrorFile = _config->FindDir("Dir::State::mirrors") + URItoFileName(BaseUri);
  119. if(Debug)
  120. {
  121. cerr << "base-uri: " << BaseUri << endl;
  122. cerr << "mirror-file: " << MirrorFile << endl;
  123. }
  124. // check the file, if it is not older than RefreshInterval just use it
  125. // otherwise try to get a new one
  126. if(FileExists(MirrorFile))
  127. {
  128. struct stat buf;
  129. time_t t,now,refresh;
  130. if(stat(MirrorFile.c_str(), &buf) != 0)
  131. return false;
  132. t = std::max(buf.st_mtime, buf.st_ctime);
  133. now = time(NULL);
  134. refresh = 60*_config->FindI("Acquire::Mirror::RefreshInterval",360);
  135. if(t + refresh > now)
  136. {
  137. if(Debug)
  138. clog << "Mirror file is in RefreshInterval" << endl;
  139. HasMirrorFile = true;
  140. return true;
  141. }
  142. if(Debug)
  143. clog << "Mirror file " << MirrorFile << " older than " << refresh << "min, re-download it" << endl;
  144. }
  145. // not that great to use pkgAcquire here, but we do not have
  146. // any other way right now
  147. pkgAcquire Fetcher;
  148. new pkgAcqFile(&Fetcher, fetch, "", 0, "", "", "", MirrorFile);
  149. bool res = (Fetcher.Run() == pkgAcquire::Continue);
  150. if(res)
  151. HasMirrorFile = true;
  152. Fetcher.Shutdown();
  153. return res;
  154. }
  155. bool MirrorMethod::SelectMirror()
  156. {
  157. // FIXME: make the mirror selection more clever, do not
  158. // just use the first one!
  159. ifstream in(MirrorFile.c_str());
  160. getline(in, Mirror);
  161. if(Debug)
  162. cerr << "Using mirror: " << Mirror << endl;
  163. return true;
  164. }
  165. // MirrorMethod::Fetch - Fetch an item /*{{{*/
  166. // ---------------------------------------------------------------------
  167. /* This adds an item to the pipeline. We keep the pipeline at a fixed
  168. depth. */
  169. bool MirrorMethod::Fetch(FetchItem *Itm)
  170. {
  171. // select mirror only once per session
  172. if(!HasMirrorFile)
  173. {
  174. Clean(_config->FindDir("Dir::State::mirrors"));
  175. GetMirrorFile(Itm->Uri);
  176. SelectMirror();
  177. }
  178. for (FetchItem *I = Queue; I != 0; I = I->Next)
  179. {
  180. if(I->Uri.find("mirror://") != string::npos)
  181. I->Uri.replace(0,BaseUri.size(),Mirror);
  182. }
  183. // now run the real fetcher
  184. return HttpMethod::Fetch(Itm);
  185. };
  186. void MirrorMethod::Fail(string Err,bool Transient)
  187. {
  188. // FIXME: queue next mirror?
  189. ReportMirrorFailure(Err);
  190. if(Queue->Uri.find("http://") != string::npos)
  191. Queue->Uri.replace(0,Mirror.size(), BaseUri);
  192. pkgAcqMethod::Fail(Err, Transient);
  193. }
  194. void MirrorMethod::URIStart(FetchResult &Res)
  195. {
  196. if(Queue->Uri.find("http://") != string::npos)
  197. Queue->Uri.replace(0,Mirror.size(), BaseUri);
  198. pkgAcqMethod::URIStart(Res);
  199. }
  200. void MirrorMethod::URIDone(FetchResult &Res,FetchResult *Alt)
  201. {
  202. // FIXME: queue next mirror?
  203. if(Queue->ExpectedMD5 != "" && Res.MD5Sum != Queue->ExpectedMD5)
  204. ReportMirrorFailure("499 Hash mismatch");
  205. if(Queue->Uri.find("http://") != string::npos)
  206. Queue->Uri.replace(0,Mirror.size(), BaseUri);
  207. pkgAcqMethod::URIDone(Res, Alt);
  208. }
  209. void MirrorMethod::ReportMirrorFailure(string FailCode)
  210. {
  211. // report that Queue->Uri failed
  212. std::cerr << "\nReportMirrorFailure: "
  213. << Queue->Uri
  214. << " FailCode: "
  215. << FailCode << std::endl;
  216. #if 0 // FIXME: do not use system, make sure to properly encode
  217. // URI/FailCode, do not hardcode the submit url
  218. system("curl -d url=" + Queue->Uri +
  219. " -d FailureCode=" + FailCode +
  220. " http://localhost:8000/ &");
  221. #endif
  222. }
  223. int main()
  224. {
  225. setlocale(LC_ALL, "");
  226. MirrorMethod Mth;
  227. return Mth.Loop();
  228. }