mirror.cc 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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. * - uses 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(), DownloadedMirrorFile(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. if(Dir == "/")
  65. return _error->Error("will not clean: '/'");
  66. // read sources.list
  67. pkgSourceList list;
  68. list.ReadMainList();
  69. DIR *D = opendir(Dir.c_str());
  70. if (D == 0)
  71. return _error->Errno("opendir",_("Unable to read %s"),Dir.c_str());
  72. string StartDir = SafeGetCWD();
  73. if (chdir(Dir.c_str()) != 0)
  74. {
  75. closedir(D);
  76. return _error->Errno("chdir",_("Unable to change to %s"),Dir.c_str());
  77. }
  78. for (struct dirent *Dir = readdir(D); Dir != 0; Dir = readdir(D))
  79. {
  80. // Skip some files..
  81. if (strcmp(Dir->d_name,"lock") == 0 ||
  82. strcmp(Dir->d_name,"partial") == 0 ||
  83. strcmp(Dir->d_name,".") == 0 ||
  84. strcmp(Dir->d_name,"..") == 0)
  85. continue;
  86. // see if we have that uri
  87. for(I=list.begin(); I != list.end(); I++)
  88. {
  89. string uri = (*I)->GetURI();
  90. if(uri.substr(0,strlen("mirror://")) != string("mirror://"))
  91. continue;
  92. string BaseUri = uri.substr(0,uri.size()-1);
  93. if (URItoFileName(BaseUri) == Dir->d_name)
  94. break;
  95. }
  96. // nothing found, nuke it
  97. if (I == list.end())
  98. unlink(Dir->d_name);
  99. };
  100. chdir(StartDir.c_str());
  101. closedir(D);
  102. return true;
  103. }
  104. bool MirrorMethod::DownloadMirrorFile(string mirror_uri_str)
  105. {
  106. // check the file, if it is not older than RefreshInterval just use it
  107. // otherwise try to get a new one
  108. if(FileExists(MirrorFile))
  109. {
  110. struct stat buf;
  111. time_t t,now,refresh;
  112. if(stat(MirrorFile.c_str(), &buf) != 0)
  113. return false;
  114. t = std::max(buf.st_mtime, buf.st_ctime);
  115. now = time(NULL);
  116. refresh = 60*_config->FindI("Acquire::Mirror::RefreshInterval",360);
  117. if(t + refresh > now)
  118. {
  119. if(Debug)
  120. clog << "Mirror file is in RefreshInterval" << endl;
  121. DownloadedMirrorFile = true;
  122. return true;
  123. }
  124. if(Debug)
  125. clog << "Mirror file " << MirrorFile << " older than " << refresh << "min, re-download it" << endl;
  126. }
  127. // not that great to use pkgAcquire here, but we do not have
  128. // any other way right now
  129. string fetch = BaseUri;
  130. fetch.replace(0,strlen("mirror://"),"http://");
  131. pkgAcquire Fetcher;
  132. new pkgAcqFile(&Fetcher, fetch, "", 0, "", "", "", MirrorFile);
  133. bool res = (Fetcher.Run() == pkgAcquire::Continue);
  134. if(res)
  135. DownloadedMirrorFile = true;
  136. Fetcher.Shutdown();
  137. return res;
  138. }
  139. bool MirrorMethod::SelectMirror()
  140. {
  141. // if we do not have a MirrorFile, fallback
  142. if(!FileExists(MirrorFile))
  143. {
  144. // FIXME: fallback to a default mirror here instead
  145. // and provide a config option to define that default
  146. return _error->Error(_("No mirror file '%s' found "), MirrorFile.c_str());
  147. }
  148. // FIXME: make the mirror selection more clever, do not
  149. // just use the first one!
  150. // BUT: we can not make this random, the mirror has to be
  151. // stable accross session, because otherwise we can
  152. // get into sync issues (got indexfiles from mirror A,
  153. // but packages from mirror B - one might be out of date etc)
  154. ifstream in(MirrorFile.c_str());
  155. getline(in, Mirror);
  156. if(Debug)
  157. cerr << "Using mirror: " << Mirror << endl;
  158. UsedMirror = Mirror;
  159. return true;
  160. }
  161. string MirrorMethod::GetMirrorFileName(string mirror_uri_str)
  162. {
  163. /*
  164. - a mirror_uri_str looks like this:
  165. mirror://people.ubuntu.com/~mvo/apt/mirror/mirrors/dists/feisty/Release.gpg
  166. - the matching source.list entry
  167. deb mirror://people.ubuntu.com/~mvo/apt/mirror/mirrors feisty main
  168. - we actually want to go after:
  169. http://people.ubuntu.com/~mvo/apt/mirror/mirrors
  170. And we need to save the BaseUri for later:
  171. - mirror://people.ubuntu.com/~mvo/apt/mirror/mirrors
  172. FIXME: what if we have two similar prefixes?
  173. mirror://people.ubuntu.com/~mvo/mirror
  174. mirror://people.ubuntu.com/~mvo/mirror2
  175. then mirror_uri_str looks like:
  176. mirror://people.ubuntu.com/~mvo/apt/mirror/dists/feisty/Release.gpg
  177. mirror://people.ubuntu.com/~mvo/apt/mirror2/dists/feisty/Release.gpg
  178. we search sources.list and find:
  179. mirror://people.ubuntu.com/~mvo/apt/mirror
  180. in both cases! So we need to apply some domain knowledge here :( and
  181. check for /dists/ or /Release.gpg as suffixes
  182. */
  183. string name;
  184. if(Debug)
  185. std::cerr << "GetMirrorFileName: " << mirror_uri_str << std::endl;
  186. // read sources.list and find match
  187. vector<metaIndex *>::const_iterator I;
  188. pkgSourceList list;
  189. list.ReadMainList();
  190. for(I=list.begin(); I != list.end(); I++)
  191. {
  192. string uristr = (*I)->GetURI();
  193. if(Debug)
  194. std::cerr << "Checking: " << uristr << std::endl;
  195. if(uristr.substr(0,strlen("mirror://")) != string("mirror://"))
  196. continue;
  197. // find matching uri in sources.list
  198. if(mirror_uri_str.substr(0,uristr.size()) == uristr)
  199. {
  200. if(Debug)
  201. std::cerr << "found BaseURI: " << uristr << std::endl;
  202. BaseUri = uristr.substr(0,uristr.size()-1);
  203. }
  204. }
  205. // get new file
  206. name = _config->FindDir("Dir::State::mirrors") + URItoFileName(BaseUri);
  207. if(Debug)
  208. {
  209. cerr << "base-uri: " << BaseUri << endl;
  210. cerr << "mirror-file: " << name << endl;
  211. }
  212. return name;
  213. }
  214. // MirrorMethod::Fetch - Fetch an item /*{{{*/
  215. // ---------------------------------------------------------------------
  216. /* This adds an item to the pipeline. We keep the pipeline at a fixed
  217. depth. */
  218. bool MirrorMethod::Fetch(FetchItem *Itm)
  219. {
  220. // the http method uses Fetch(0) as a way to update the pipeline,
  221. // just let it do its work in this case - Fetch() with a valid
  222. // Itm will always run before the first Fetch(0)
  223. if(Itm == NULL)
  224. return HttpMethod::Fetch(Itm);
  225. // if we don't have the name of the mirror file on disk yet,
  226. // calculate it now (can be derived from the uri)
  227. if(MirrorFile.empty())
  228. MirrorFile = GetMirrorFileName(Itm->Uri);
  229. // download mirror file once (if we are after index files)
  230. if(Itm->IndexFile && !DownloadedMirrorFile)
  231. {
  232. Clean(_config->FindDir("Dir::State::mirrors"));
  233. DownloadMirrorFile(Itm->Uri);
  234. }
  235. if(Mirror.empty())
  236. SelectMirror();
  237. for (FetchItem *I = Queue; I != 0; I = I->Next)
  238. {
  239. if(I->Uri.find("mirror://") != string::npos)
  240. I->Uri.replace(0,BaseUri.size(), Mirror);
  241. }
  242. // now run the real fetcher
  243. return HttpMethod::Fetch(Itm);
  244. };
  245. void MirrorMethod::Fail(string Err,bool Transient)
  246. {
  247. if(Queue->Uri.find("http://") != string::npos)
  248. Queue->Uri.replace(0,Mirror.size(), BaseUri);
  249. pkgAcqMethod::Fail(Err, Transient);
  250. }
  251. void MirrorMethod::URIStart(FetchResult &Res)
  252. {
  253. if(Queue->Uri.find("http://") != string::npos)
  254. Queue->Uri.replace(0,Mirror.size(), BaseUri);
  255. pkgAcqMethod::URIStart(Res);
  256. }
  257. void MirrorMethod::URIDone(FetchResult &Res,FetchResult *Alt)
  258. {
  259. if(Queue->Uri.find("http://") != string::npos)
  260. Queue->Uri.replace(0,Mirror.size(), BaseUri);
  261. pkgAcqMethod::URIDone(Res, Alt);
  262. }
  263. int main()
  264. {
  265. setlocale(LC_ALL, "");
  266. MirrorMethod Mth;
  267. return Mth.Loop();
  268. }