mirror.cc 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  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<sstream>
  24. #include "mirror.h"
  25. #include "http.h"
  26. #include "apti18n.h"
  27. /*}}}*/
  28. /* Done:
  29. * - works with http (only!)
  30. * - always picks the first mirror from the list
  31. * - call out to problem reporting script
  32. * - supports "deb mirror://host/path/to/mirror-list/// dist component"
  33. * - uses pkgAcqMethod::FailReason() to have a string representation
  34. * of the failure that is also send to LP
  35. *
  36. * TODO:
  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. * and better error handling there!
  41. * - support more than http
  42. * - testing :)
  43. */
  44. MirrorMethod::MirrorMethod()
  45. : HttpMethod(), DownloadedMirrorFile(false)
  46. {
  47. };
  48. // HttpMethod::Configuration - Handle a configuration message /*{{{*/
  49. // ---------------------------------------------------------------------
  50. /* We stash the desired pipeline depth */
  51. bool MirrorMethod::Configuration(string Message)
  52. {
  53. if (pkgAcqMethod::Configuration(Message) == false)
  54. return false;
  55. Debug = _config->FindB("Debug::Acquire::mirror",false);
  56. return true;
  57. }
  58. /*}}}*/
  59. // clean the mirrors dir based on ttl information
  60. bool MirrorMethod::Clean(string Dir)
  61. {
  62. vector<metaIndex *>::const_iterator I;
  63. if(Debug)
  64. clog << "MirrorMethod::Clean(): " << Dir << endl;
  65. if(Dir == "/")
  66. return _error->Error("will not clean: '/'");
  67. // read sources.list
  68. pkgSourceList list;
  69. list.ReadMainList();
  70. DIR *D = opendir(Dir.c_str());
  71. if (D == 0)
  72. return _error->Errno("opendir",_("Unable to read %s"),Dir.c_str());
  73. string StartDir = SafeGetCWD();
  74. if (chdir(Dir.c_str()) != 0)
  75. {
  76. closedir(D);
  77. return _error->Errno("chdir",_("Unable to change to %s"),Dir.c_str());
  78. }
  79. for (struct dirent *Dir = readdir(D); Dir != 0; Dir = readdir(D))
  80. {
  81. // Skip some files..
  82. if (strcmp(Dir->d_name,"lock") == 0 ||
  83. strcmp(Dir->d_name,"partial") == 0 ||
  84. strcmp(Dir->d_name,".") == 0 ||
  85. strcmp(Dir->d_name,"..") == 0)
  86. continue;
  87. // see if we have that uri
  88. for(I=list.begin(); I != list.end(); I++)
  89. {
  90. string uri = (*I)->GetURI();
  91. if(uri.substr(0,strlen("mirror://")) != string("mirror://"))
  92. continue;
  93. string BaseUri = uri.substr(0,uri.size()-1);
  94. if (URItoFileName(BaseUri) == Dir->d_name)
  95. break;
  96. }
  97. // nothing found, nuke it
  98. if (I == list.end())
  99. unlink(Dir->d_name);
  100. };
  101. chdir(StartDir.c_str());
  102. closedir(D);
  103. return true;
  104. }
  105. bool MirrorMethod::DownloadMirrorFile(string mirror_uri_str)
  106. {
  107. if(Debug)
  108. clog << "MirrorMethod::DownloadMirrorFile(): " << endl;
  109. // not that great to use pkgAcquire here, but we do not have
  110. // any other way right now
  111. string fetch = BaseUri;
  112. fetch.replace(0,strlen("mirror://"),"http://");
  113. pkgAcquire Fetcher;
  114. new pkgAcqFile(&Fetcher, fetch, "", 0, "", "", "", MirrorFile);
  115. bool res = (Fetcher.Run() == pkgAcquire::Continue);
  116. if(res)
  117. DownloadedMirrorFile = true;
  118. Fetcher.Shutdown();
  119. return res;
  120. }
  121. bool MirrorMethod::SelectNextMirror()
  122. {
  123. if(Debug)
  124. cerr << "using mirror: " << Mirror << endl;
  125. Mirror = AllMirrors[0];
  126. UsedMirror = Mirror;
  127. return false;
  128. }
  129. bool MirrorMethod::InitMirrors()
  130. {
  131. // if we do not have a MirrorFile, fallback
  132. if(!FileExists(MirrorFile))
  133. {
  134. // FIXME: fallback to a default mirror here instead
  135. // and provide a config option to define that default
  136. return _error->Error(_("No mirror file '%s' found "), MirrorFile.c_str());
  137. }
  138. // FIXME: make the mirror selection more clever, do not
  139. // just use the first one!
  140. // BUT: we can not make this random, the mirror has to be
  141. // stable accross session, because otherwise we can
  142. // get into sync issues (got indexfiles from mirror A,
  143. // but packages from mirror B - one might be out of date etc)
  144. ifstream in(MirrorFile.c_str());
  145. string s;
  146. while (!in.eof())
  147. {
  148. getline(in, s);
  149. if (s.size() > 0)
  150. AllMirrors.push_back(s);
  151. }
  152. SelectNextMirror();
  153. return true;
  154. }
  155. string MirrorMethod::GetMirrorFileName(string mirror_uri_str)
  156. {
  157. /*
  158. - a mirror_uri_str looks like this:
  159. mirror://people.ubuntu.com/~mvo/apt/mirror/mirrors/dists/feisty/Release.gpg
  160. - the matching source.list entry
  161. deb mirror://people.ubuntu.com/~mvo/apt/mirror/mirrors feisty main
  162. - we actually want to go after:
  163. http://people.ubuntu.com/~mvo/apt/mirror/mirrors
  164. And we need to save the BaseUri for later:
  165. - mirror://people.ubuntu.com/~mvo/apt/mirror/mirrors
  166. FIXME: what if we have two similar prefixes?
  167. mirror://people.ubuntu.com/~mvo/mirror
  168. mirror://people.ubuntu.com/~mvo/mirror2
  169. then mirror_uri_str looks like:
  170. mirror://people.ubuntu.com/~mvo/apt/mirror/dists/feisty/Release.gpg
  171. mirror://people.ubuntu.com/~mvo/apt/mirror2/dists/feisty/Release.gpg
  172. we search sources.list and find:
  173. mirror://people.ubuntu.com/~mvo/apt/mirror
  174. in both cases! So we need to apply some domain knowledge here :( and
  175. check for /dists/ or /Release.gpg as suffixes
  176. */
  177. string name;
  178. if(Debug)
  179. std::cerr << "GetMirrorFileName: " << mirror_uri_str << std::endl;
  180. // read sources.list and find match
  181. vector<metaIndex *>::const_iterator I;
  182. pkgSourceList list;
  183. list.ReadMainList();
  184. for(I=list.begin(); I != list.end(); I++)
  185. {
  186. string uristr = (*I)->GetURI();
  187. if(Debug)
  188. std::cerr << "Checking: " << uristr << std::endl;
  189. if(uristr.substr(0,strlen("mirror://")) != string("mirror://"))
  190. continue;
  191. // find matching uri in sources.list
  192. if(mirror_uri_str.substr(0,uristr.size()) == uristr)
  193. {
  194. if(Debug)
  195. std::cerr << "found BaseURI: " << uristr << std::endl;
  196. BaseUri = uristr.substr(0,uristr.size()-1);
  197. }
  198. }
  199. // get new file
  200. name = _config->FindDir("Dir::State::mirrors") + URItoFileName(BaseUri);
  201. if(Debug)
  202. {
  203. cerr << "base-uri: " << BaseUri << endl;
  204. cerr << "mirror-file: " << name << endl;
  205. }
  206. return name;
  207. }
  208. // MirrorMethod::Fetch - Fetch an item /*{{{*/
  209. // ---------------------------------------------------------------------
  210. /* This adds an item to the pipeline. We keep the pipeline at a fixed
  211. depth. */
  212. bool MirrorMethod::Fetch(FetchItem *Itm)
  213. {
  214. if(Debug)
  215. clog << "MirrorMethod::Fetch()" << endl;
  216. // the http method uses Fetch(0) as a way to update the pipeline,
  217. // just let it do its work in this case - Fetch() with a valid
  218. // Itm will always run before the first Fetch(0)
  219. if(Itm == NULL)
  220. return HttpMethod::Fetch(Itm);
  221. // if we don't have the name of the mirror file on disk yet,
  222. // calculate it now (can be derived from the uri)
  223. if(MirrorFile.empty())
  224. MirrorFile = GetMirrorFileName(Itm->Uri);
  225. // download mirror file once (if we are after index files)
  226. if(Itm->IndexFile && !DownloadedMirrorFile)
  227. {
  228. Clean(_config->FindDir("Dir::State::mirrors"));
  229. DownloadMirrorFile(Itm->Uri);
  230. }
  231. if(Mirror.empty()) {
  232. if(!InitMirrors()) {
  233. // no valid mirror selected, something went wrong downloading
  234. // from the master mirror site most likely and there is
  235. // no old mirror file availalbe
  236. return false;
  237. }
  238. }
  239. if(Debug)
  240. clog << "selected mirror: " << Mirror << endl;
  241. for (FetchItem *I = Queue; I != 0; I = I->Next)
  242. {
  243. if(I->Uri.find("mirror://") != string::npos)
  244. I->Uri.replace(0,BaseUri.size(), Mirror);
  245. }
  246. // now run the real fetcher
  247. return HttpMethod::Fetch(Itm);
  248. };
  249. void MirrorMethod::Fail(string Err,bool Transient)
  250. {
  251. // try the next mirror on fail
  252. string old_mirror = Mirror;
  253. if (SelectNextMirror())
  254. {
  255. Queue->Uri.replace(0, old_mirror.size(), Mirror);
  256. return;
  257. }
  258. // all mirrors failed, so bail out
  259. string s;
  260. strprintf(s, _("[Mirror: %s]"), Mirror.c_str());
  261. SetIP(s);
  262. if(Queue->Uri.find("http://") != string::npos)
  263. Queue->Uri.replace(0,Mirror.size(), BaseUri);
  264. pkgAcqMethod::Fail(Err, Transient);
  265. }
  266. void MirrorMethod::URIStart(FetchResult &Res)
  267. {
  268. if(Queue->Uri.find("http://") != string::npos)
  269. Queue->Uri.replace(0,Mirror.size(), BaseUri);
  270. pkgAcqMethod::URIStart(Res);
  271. }
  272. void MirrorMethod::URIDone(FetchResult &Res,FetchResult *Alt)
  273. {
  274. if(Queue->Uri.find("http://") != string::npos)
  275. Queue->Uri.replace(0,Mirror.size(), BaseUri);
  276. pkgAcqMethod::URIDone(Res, Alt);
  277. }
  278. int main()
  279. {
  280. setlocale(LC_ALL, "");
  281. MirrorMethod Mth;
  282. return Mth.Loop();
  283. }