mirror.cc 7.7 KB

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