mirror.cc 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  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. if(Debug)
  107. clog << "MirrorMethod::DownloadMirrorFile(): " << endl;
  108. // check the file, if it is not older than RefreshInterval just use it
  109. // otherwise try to get a new one
  110. if(FileExists(MirrorFile))
  111. {
  112. struct stat buf;
  113. time_t t,now,refresh;
  114. if(stat(MirrorFile.c_str(), &buf) != 0)
  115. return false;
  116. t = std::max(buf.st_mtime, buf.st_ctime);
  117. now = time(NULL);
  118. refresh = 60*_config->FindI("Acquire::Mirror::RefreshInterval",360);
  119. if(t + refresh > now)
  120. {
  121. if(Debug)
  122. clog << "Mirror file is in RefreshInterval" << endl;
  123. DownloadedMirrorFile = true;
  124. return true;
  125. }
  126. if(Debug)
  127. clog << "Mirror file " << MirrorFile << " older than " << refresh << "min, re-download it" << endl;
  128. }
  129. // not that great to use pkgAcquire here, but we do not have
  130. // any other way right now
  131. string fetch = BaseUri;
  132. fetch.replace(0,strlen("mirror://"),"http://");
  133. pkgAcquire Fetcher;
  134. new pkgAcqFile(&Fetcher, fetch, "", 0, "", "", "", MirrorFile);
  135. bool res = (Fetcher.Run() == pkgAcquire::Continue);
  136. if(res)
  137. DownloadedMirrorFile = true;
  138. Fetcher.Shutdown();
  139. return res;
  140. }
  141. bool MirrorMethod::SelectNextMirror()
  142. {
  143. if (AllMirrors.size() < 1)
  144. return false;
  145. Mirror = AllMirrors[0];
  146. AllMirrors.erase(AllMirrors.begin());
  147. if(Debug)
  148. cerr << "using mirror: " << Mirror << endl;
  149. UsedMirror = Mirror;
  150. return true;
  151. }
  152. bool MirrorMethod::InitMirrors()
  153. {
  154. // if we do not have a MirrorFile, fallback
  155. if(!FileExists(MirrorFile))
  156. {
  157. // FIXME: fallback to a default mirror here instead
  158. // and provide a config option to define that default
  159. return _error->Error(_("No mirror file '%s' found "), MirrorFile.c_str());
  160. }
  161. // FIXME: make the mirror selection more clever, do not
  162. // just use the first one!
  163. // BUT: we can not make this random, the mirror has to be
  164. // stable accross session, because otherwise we can
  165. // get into sync issues (got indexfiles from mirror A,
  166. // but packages from mirror B - one might be out of date etc)
  167. ifstream in(MirrorFile.c_str());
  168. string s;
  169. while (!in.eof())
  170. {
  171. getline(in, s);
  172. AllMirrors.push_back(s);
  173. }
  174. SelectNextMirror();
  175. return true;
  176. }
  177. string MirrorMethod::GetMirrorFileName(string mirror_uri_str)
  178. {
  179. /*
  180. - a mirror_uri_str looks like this:
  181. mirror://people.ubuntu.com/~mvo/apt/mirror/mirrors/dists/feisty/Release.gpg
  182. - the matching source.list entry
  183. deb mirror://people.ubuntu.com/~mvo/apt/mirror/mirrors feisty main
  184. - we actually want to go after:
  185. http://people.ubuntu.com/~mvo/apt/mirror/mirrors
  186. And we need to save the BaseUri for later:
  187. - mirror://people.ubuntu.com/~mvo/apt/mirror/mirrors
  188. FIXME: what if we have two similar prefixes?
  189. mirror://people.ubuntu.com/~mvo/mirror
  190. mirror://people.ubuntu.com/~mvo/mirror2
  191. then mirror_uri_str looks like:
  192. mirror://people.ubuntu.com/~mvo/apt/mirror/dists/feisty/Release.gpg
  193. mirror://people.ubuntu.com/~mvo/apt/mirror2/dists/feisty/Release.gpg
  194. we search sources.list and find:
  195. mirror://people.ubuntu.com/~mvo/apt/mirror
  196. in both cases! So we need to apply some domain knowledge here :( and
  197. check for /dists/ or /Release.gpg as suffixes
  198. */
  199. string name;
  200. if(Debug)
  201. std::cerr << "GetMirrorFileName: " << mirror_uri_str << std::endl;
  202. // read sources.list and find match
  203. vector<metaIndex *>::const_iterator I;
  204. pkgSourceList list;
  205. list.ReadMainList();
  206. for(I=list.begin(); I != list.end(); I++)
  207. {
  208. string uristr = (*I)->GetURI();
  209. if(Debug)
  210. std::cerr << "Checking: " << uristr << std::endl;
  211. if(uristr.substr(0,strlen("mirror://")) != string("mirror://"))
  212. continue;
  213. // find matching uri in sources.list
  214. if(mirror_uri_str.substr(0,uristr.size()) == uristr)
  215. {
  216. if(Debug)
  217. std::cerr << "found BaseURI: " << uristr << std::endl;
  218. BaseUri = uristr.substr(0,uristr.size()-1);
  219. }
  220. }
  221. // get new file
  222. name = _config->FindDir("Dir::State::mirrors") + URItoFileName(BaseUri);
  223. if(Debug)
  224. {
  225. cerr << "base-uri: " << BaseUri << endl;
  226. cerr << "mirror-file: " << name << endl;
  227. }
  228. return name;
  229. }
  230. // MirrorMethod::Fetch - Fetch an item /*{{{*/
  231. // ---------------------------------------------------------------------
  232. /* This adds an item to the pipeline. We keep the pipeline at a fixed
  233. depth. */
  234. bool MirrorMethod::Fetch(FetchItem *Itm)
  235. {
  236. if(Debug)
  237. clog << "MirrorMethod::Fetch()" << endl;
  238. // the http method uses Fetch(0) as a way to update the pipeline,
  239. // just let it do its work in this case - Fetch() with a valid
  240. // Itm will always run before the first Fetch(0)
  241. if(Itm == NULL)
  242. return HttpMethod::Fetch(Itm);
  243. // if we don't have the name of the mirror file on disk yet,
  244. // calculate it now (can be derived from the uri)
  245. if(MirrorFile.empty())
  246. MirrorFile = GetMirrorFileName(Itm->Uri);
  247. // download mirror file once (if we are after index files)
  248. if(Itm->IndexFile && !DownloadedMirrorFile)
  249. {
  250. Clean(_config->FindDir("Dir::State::mirrors"));
  251. DownloadMirrorFile(Itm->Uri);
  252. }
  253. if(Mirror.empty()) {
  254. if(!InitMirrors()) {
  255. // no valid mirror selected, something went wrong downloading
  256. // from the master mirror site most likely and there is
  257. // no old mirror file availalbe
  258. return false;
  259. }
  260. }
  261. if(Debug)
  262. clog << "selected mirror: " << Mirror << endl;
  263. for (FetchItem *I = Queue; I != 0; I = I->Next)
  264. {
  265. if(I->Uri.find("mirror://") != string::npos)
  266. I->Uri.replace(0,BaseUri.size(), Mirror);
  267. }
  268. // now run the real fetcher
  269. return HttpMethod::Fetch(Itm);
  270. };
  271. void MirrorMethod::Fail(string Err,bool Transient)
  272. {
  273. if(Queue->Uri.find("http://") != string::npos)
  274. Queue->Uri.replace(0,Mirror.size(), BaseUri);
  275. pkgAcqMethod::Fail(Err, Transient);
  276. }
  277. void MirrorMethod::URIStart(FetchResult &Res)
  278. {
  279. if(Queue->Uri.find("http://") != string::npos)
  280. Queue->Uri.replace(0,Mirror.size(), BaseUri);
  281. pkgAcqMethod::URIStart(Res);
  282. }
  283. void MirrorMethod::URIDone(FetchResult &Res,FetchResult *Alt)
  284. {
  285. if(Queue->Uri.find("http://") != string::npos)
  286. Queue->Uri.replace(0,Mirror.size(), BaseUri);
  287. pkgAcqMethod::URIDone(Res, Alt);
  288. }
  289. int main()
  290. {
  291. setlocale(LC_ALL, "");
  292. MirrorMethod Mth;
  293. return Mth.Loop();
  294. }