mirror.cc 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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 <fstream>
  16. #include <iostream>
  17. #include <stdarg.h>
  18. #include <sys/stat.h>
  19. #include <sys/types.h>
  20. #include <dirent.h>
  21. using namespace std;
  22. #include "mirror.h"
  23. #include "http.h"
  24. #include "apti18n.h"
  25. /*}}}*/
  26. /*
  27. * TODO:
  28. * - send expected checksum to the mirror method so that
  29. some checking/falling back can be done here already
  30. * - keep the mirror file around in /var/lib/apt/mirrors
  31. * can't be put into lists/ because of the listclearer
  32. * cleanup by time (mtime relative to the other mtimes)
  33. * - use a TTL time the mirror file is fetched again (6h?)
  34. * - deal with runing as non-root because we can't write to the lists
  35. dir then -> use the cached mirror file
  36. * - better method to download than having a pkgAcquire interface here
  37. * - magicmarker is (a bit) evil
  38. * - testing :)
  39. */
  40. MirrorMethod::MirrorMethod()
  41. : HttpMethod(), HasMirrorFile(false)
  42. {
  43. #if 0
  44. HasMirrorFile=true;
  45. BaseUri="mirror://people.ubuntu.com/~mvo/mirror/mirrors";
  46. MirrorFile="/var/lib/apt/lists/people.ubuntu.com_%7emvo_apt_mirror_mirrors";
  47. Mirror="http://de.archive.ubuntu.com/ubuntu/";
  48. #endif
  49. };
  50. // HttpMethod::Configuration - Handle a configuration message /*{{{*/
  51. // ---------------------------------------------------------------------
  52. /* We stash the desired pipeline depth */
  53. bool MirrorMethod::Configuration(string Message)
  54. {
  55. if (pkgAcqMethod::Configuration(Message) == false)
  56. return false;
  57. Debug = _config->FindB("Debug::Acquire::mirror",false);
  58. return true;
  59. }
  60. /*}}}*/
  61. // clean the mirrors dir based on ttl information
  62. bool MirrorMethod::Clean(string Dir)
  63. {
  64. // FIXME: it would better to have a global idea of the mirrors
  65. // in the sources.list and use this instead of this time
  66. // based approach. currently apt does not support this :/
  67. DIR *D = opendir(Dir.c_str());
  68. if (D == 0)
  69. return _error->Errno("opendir",_("Unable to read %s"),Dir.c_str());
  70. string StartDir = SafeGetCWD();
  71. if (chdir(Dir.c_str()) != 0)
  72. {
  73. closedir(D);
  74. return _error->Errno("chdir",_("Unable to change to %s"),Dir.c_str());
  75. }
  76. for (struct dirent *Dir = readdir(D); Dir != 0; Dir = readdir(D))
  77. {
  78. // Skip some files..
  79. if (strcmp(Dir->d_name,"lock") == 0 ||
  80. strcmp(Dir->d_name,"partial") == 0 ||
  81. strcmp(Dir->d_name,".") == 0 ||
  82. strcmp(Dir->d_name,"..") == 0)
  83. continue;
  84. // Del everything not touched for MaxAge days
  85. time_t t,now,max;
  86. struct stat buf;
  87. if(stat(Dir->d_name, &buf) != 0)
  88. {
  89. cerr << "Can't stat '" << Dir->d_name << "'" << endl;
  90. continue;
  91. }
  92. t = std::max(buf.st_mtime, buf.st_ctime);
  93. now = time(NULL);
  94. max = 24*60*60*_config->FindI("Acquire::Mirror::MaxAge",90);
  95. if(t + max < now)
  96. {
  97. if(Debug)
  98. clog << "Mirror file is older than MaxAge days, deleting" << endl;
  99. unlink(Dir->d_name);
  100. }
  101. };
  102. chdir(StartDir.c_str());
  103. closedir(D);
  104. return true;
  105. }
  106. bool MirrorMethod::GetMirrorFile(string uri)
  107. {
  108. string Marker = _config->Find("Acquire::Mirror::MagicMarker","///");
  109. BaseUri = uri.substr(0,uri.find(Marker));
  110. string fetch = BaseUri;
  111. fetch.replace(0,strlen("mirror://"),"http://");
  112. // get new file
  113. MirrorFile = _config->FindDir("Dir::State::mirrors") + URItoFileName(BaseUri);
  114. if(Debug)
  115. {
  116. cerr << "base-uri: " << BaseUri << endl;
  117. cerr << "mirror-file: " << MirrorFile << endl;
  118. }
  119. // check the file, if it is not older than RefreshInterval just use it
  120. // otherwise try to get a new one
  121. if(FileExists(MirrorFile))
  122. {
  123. struct stat buf;
  124. time_t t,now,refresh;
  125. if(stat(MirrorFile.c_str(), &buf) != 0)
  126. return false;
  127. t = std::max(buf.st_mtime, buf.st_ctime);
  128. now = time(NULL);
  129. refresh = 60*_config->FindI("Acquire::Mirror::RefreshInterval",360);
  130. if(t + refresh > now)
  131. {
  132. if(Debug)
  133. clog << "Mirror file is in RefreshInterval" << endl;
  134. HasMirrorFile = true;
  135. return true;
  136. }
  137. if(Debug)
  138. clog << "Mirror file " << MirrorFile << " older than " << refresh << "min, re-download it" << endl;
  139. }
  140. // not that great to use pkgAcquire here, but we do not have
  141. // any other way right now
  142. pkgAcquire Fetcher;
  143. new pkgAcqFile(&Fetcher, fetch, "", 0, "", "", "", MirrorFile);
  144. bool res = (Fetcher.Run() == pkgAcquire::Continue);
  145. if(res)
  146. HasMirrorFile = true;
  147. Fetcher.Shutdown();
  148. return res;
  149. }
  150. bool MirrorMethod::SelectMirror()
  151. {
  152. // FIXME: make the mirror selection more clever, do not
  153. // just use the first one!
  154. ifstream in(MirrorFile.c_str());
  155. getline(in, Mirror);
  156. if(Debug)
  157. cerr << "Using mirror: " << Mirror << endl;
  158. return true;
  159. }
  160. // MirrorMethod::Fetch - Fetch an item /*{{{*/
  161. // ---------------------------------------------------------------------
  162. /* This adds an item to the pipeline. We keep the pipeline at a fixed
  163. depth. */
  164. bool MirrorMethod::Fetch(FetchItem *Itm)
  165. {
  166. // select mirror only once per session
  167. if(!HasMirrorFile)
  168. {
  169. Clean(_config->FindDir("Dir::State::mirrors"));
  170. GetMirrorFile(Itm->Uri);
  171. SelectMirror();
  172. }
  173. for (FetchItem *I = Queue; I != 0; I = I->Next)
  174. {
  175. if(I->Uri.find("mirror://") != string::npos)
  176. I->Uri.replace(0,BaseUri.size(),Mirror);
  177. }
  178. // now run the real fetcher
  179. return HttpMethod::Fetch(Itm);
  180. };
  181. void MirrorMethod::Fail(string Err,bool Transient)
  182. {
  183. if(Queue->Uri.find("http://") != string::npos)
  184. Queue->Uri.replace(0,Mirror.size(), BaseUri);
  185. pkgAcqMethod::Fail(Err, Transient);
  186. }
  187. void MirrorMethod::URIStart(FetchResult &Res)
  188. {
  189. if(Queue->Uri.find("http://") != string::npos)
  190. Queue->Uri.replace(0,Mirror.size(), BaseUri);
  191. pkgAcqMethod::URIStart(Res);
  192. }
  193. void MirrorMethod::URIDone(FetchResult &Res,FetchResult *Alt)
  194. {
  195. if(Queue->Uri.find("http://") != string::npos)
  196. Queue->Uri.replace(0,Mirror.size(), BaseUri);
  197. pkgAcqMethod::URIDone(Res, Alt);
  198. }
  199. int main()
  200. {
  201. setlocale(LC_ALL, "");
  202. MirrorMethod Mth;
  203. return Mth.Loop();
  204. }