mirror.cc 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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. * - use pkgAcqMethod::FailReason() to have a string representation
  33. * of the failure that is also send to LP
  34. *
  35. * TODO:
  36. * - deal with runing as non-root because we can't write to the lists
  37. dir then -> use the cached mirror file
  38. * - better method to download than having a pkgAcquire interface here
  39. * - support more than http
  40. * - testing :)
  41. */
  42. MirrorMethod::MirrorMethod()
  43. : HttpMethod(), HasMirrorFile(false)
  44. {
  45. };
  46. // HttpMethod::Configuration - Handle a configuration message /*{{{*/
  47. // ---------------------------------------------------------------------
  48. /* We stash the desired pipeline depth */
  49. bool MirrorMethod::Configuration(string Message)
  50. {
  51. if (pkgAcqMethod::Configuration(Message) == false)
  52. return false;
  53. Debug = _config->FindB("Debug::Acquire::mirror",false);
  54. return true;
  55. }
  56. /*}}}*/
  57. // clean the mirrors dir based on ttl information
  58. bool MirrorMethod::Clean(string Dir)
  59. {
  60. vector<metaIndex *>::const_iterator I;
  61. if(Debug)
  62. clog << "MirrorMethod::Clean(): " << Dir << endl;
  63. // read sources.list
  64. pkgSourceList list;
  65. list.ReadMainList();
  66. DIR *D = opendir(Dir.c_str());
  67. if (D == 0)
  68. return _error->Errno("opendir",_("Unable to read %s"),Dir.c_str());
  69. string StartDir = SafeGetCWD();
  70. if (chdir(Dir.c_str()) != 0)
  71. {
  72. closedir(D);
  73. return _error->Errno("chdir",_("Unable to change to %s"),Dir.c_str());
  74. }
  75. for (struct dirent *Dir = readdir(D); Dir != 0; Dir = readdir(D))
  76. {
  77. // Skip some files..
  78. if (strcmp(Dir->d_name,"lock") == 0 ||
  79. strcmp(Dir->d_name,"partial") == 0 ||
  80. strcmp(Dir->d_name,".") == 0 ||
  81. strcmp(Dir->d_name,"..") == 0)
  82. continue;
  83. // see if we have that uri
  84. for(I=list.begin(); I != list.end(); I++)
  85. {
  86. string uri = (*I)->GetURI();
  87. if(uri.substr(0,strlen("mirror://")) != string("mirror://"))
  88. continue;
  89. string BaseUri = uri.substr(0,uri.size()-1);
  90. if (URItoFileName(BaseUri) == Dir->d_name)
  91. break;
  92. }
  93. // nothing found, nuke it
  94. if (I == list.end())
  95. unlink(Dir->d_name);
  96. };
  97. chdir(StartDir.c_str());
  98. closedir(D);
  99. return true;
  100. }
  101. bool MirrorMethod::GetMirrorFile(string mirror_uri_str)
  102. {
  103. /*
  104. - a mirror_uri_str looks like this:
  105. mirror://people.ubuntu.com/~mvo/apt/mirror/mirrors/dists/feisty/Release.gpg
  106. - the matching source.list entry
  107. deb mirror://people.ubuntu.com/~mvo/apt/mirror/mirrors feisty main
  108. - we actually want to go after:
  109. http://people.ubuntu.com/~mvo/apt/mirror/mirrors
  110. And we need to save the BaseUri for later:
  111. - mirror://people.ubuntu.com/~mvo/apt/mirror/mirrors
  112. FIXME: what if we have two similar prefixes?
  113. mirror://people.ubuntu.com/~mvo/mirror
  114. mirror://people.ubuntu.com/~mvo/mirror2
  115. then mirror_uri_str looks like:
  116. mirror://people.ubuntu.com/~mvo/apt/mirror/dists/feisty/Release.gpg
  117. mirror://people.ubuntu.com/~mvo/apt/mirror2/dists/feisty/Release.gpg
  118. we search sources.list and find:
  119. mirror://people.ubuntu.com/~mvo/apt/mirror
  120. in both cases! So we need to apply some domain knowledge here :( and
  121. check for /dists/ or /Release.gpg as suffixes
  122. */
  123. if(Debug)
  124. std::cerr << "GetMirrorFile: " << mirror_uri_str << std::endl;
  125. // read sources.list and find match
  126. vector<metaIndex *>::const_iterator I;
  127. pkgSourceList list;
  128. list.ReadMainList();
  129. for(I=list.begin(); I != list.end(); I++)
  130. {
  131. string uristr = (*I)->GetURI();
  132. if(Debug)
  133. std::cerr << "Checking: " << uristr << std::endl;
  134. if(uristr.substr(0,strlen("mirror://")) != string("mirror://"))
  135. continue;
  136. // find matching uri in sources.list
  137. if(mirror_uri_str.substr(0,uristr.size()) == uristr)
  138. {
  139. if(Debug)
  140. std::cerr << "found BaseURI: " << uristr << std::endl;
  141. BaseUri = uristr.substr(0,uristr.size()-1);
  142. }
  143. }
  144. string fetch = BaseUri;
  145. fetch.replace(0,strlen("mirror://"),"http://");
  146. // get new file
  147. MirrorFile = _config->FindDir("Dir::State::mirrors") + URItoFileName(BaseUri);
  148. if(Debug)
  149. {
  150. cerr << "base-uri: " << BaseUri << endl;
  151. cerr << "mirror-file: " << MirrorFile << endl;
  152. }
  153. // check the file, if it is not older than RefreshInterval just use it
  154. // otherwise try to get a new one
  155. if(FileExists(MirrorFile))
  156. {
  157. struct stat buf;
  158. time_t t,now,refresh;
  159. if(stat(MirrorFile.c_str(), &buf) != 0)
  160. return false;
  161. t = std::max(buf.st_mtime, buf.st_ctime);
  162. now = time(NULL);
  163. refresh = 60*_config->FindI("Acquire::Mirror::RefreshInterval",360);
  164. if(t + refresh > now)
  165. {
  166. if(Debug)
  167. clog << "Mirror file is in RefreshInterval" << endl;
  168. HasMirrorFile = true;
  169. return true;
  170. }
  171. if(Debug)
  172. clog << "Mirror file " << MirrorFile << " older than " << refresh << "min, re-download it" << endl;
  173. }
  174. // not that great to use pkgAcquire here, but we do not have
  175. // any other way right now
  176. pkgAcquire Fetcher;
  177. new pkgAcqFile(&Fetcher, fetch, "", 0, "", "", "", MirrorFile);
  178. bool res = (Fetcher.Run() == pkgAcquire::Continue);
  179. if(res)
  180. HasMirrorFile = true;
  181. Fetcher.Shutdown();
  182. return res;
  183. }
  184. bool MirrorMethod::SelectMirror()
  185. {
  186. // FIXME: make the mirror selection more clever, do not
  187. // just use the first one!
  188. ifstream in(MirrorFile.c_str());
  189. getline(in, Mirror);
  190. if(Debug)
  191. cerr << "Using mirror: " << Mirror << endl;
  192. UsedMirror = Mirror;
  193. return true;
  194. }
  195. // MirrorMethod::Fetch - Fetch an item /*{{{*/
  196. // ---------------------------------------------------------------------
  197. /* This adds an item to the pipeline. We keep the pipeline at a fixed
  198. depth. */
  199. bool MirrorMethod::Fetch(FetchItem *Itm)
  200. {
  201. // select mirror only once per session
  202. if(!HasMirrorFile)
  203. {
  204. Clean(_config->FindDir("Dir::State::mirrors"));
  205. GetMirrorFile(Itm->Uri);
  206. SelectMirror();
  207. }
  208. for (FetchItem *I = Queue; I != 0; I = I->Next)
  209. {
  210. if(I->Uri.find("mirror://") != string::npos)
  211. I->Uri.replace(0,BaseUri.size(),Mirror);
  212. }
  213. // now run the real fetcher
  214. return HttpMethod::Fetch(Itm);
  215. };
  216. void MirrorMethod::Fail(string Err,bool Transient)
  217. {
  218. if(Queue->Uri.find("http://") != string::npos)
  219. Queue->Uri.replace(0,Mirror.size(), BaseUri);
  220. pkgAcqMethod::Fail(Err, Transient);
  221. }
  222. void MirrorMethod::URIStart(FetchResult &Res)
  223. {
  224. if(Queue->Uri.find("http://") != string::npos)
  225. Queue->Uri.replace(0,Mirror.size(), BaseUri);
  226. pkgAcqMethod::URIStart(Res);
  227. }
  228. void MirrorMethod::URIDone(FetchResult &Res,FetchResult *Alt)
  229. {
  230. if(Queue->Uri.find("http://") != string::npos)
  231. Queue->Uri.replace(0,Mirror.size(), BaseUri);
  232. pkgAcqMethod::URIDone(Res, Alt);
  233. }
  234. int main()
  235. {
  236. setlocale(LC_ALL, "");
  237. MirrorMethod Mth;
  238. return Mth.Loop();
  239. }