mirror.cc 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  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 Acquire Method - This is the Mirror acquire method for APT.
  6. ##################################################################### */
  7. /*}}}*/
  8. // Include Files /*{{{*/
  9. #include <config.h>
  10. #include <apt-pkg/aptconfiguration.h>
  11. #include <apt-pkg/fileutl.h>
  12. #include <apt-pkg/acquire-method.h>
  13. #include <apt-pkg/acquire-item.h>
  14. #include <apt-pkg/acquire.h>
  15. #include <apt-pkg/error.h>
  16. #include <apt-pkg/hashes.h>
  17. #include <apt-pkg/sourcelist.h>
  18. #include <apt-pkg/configuration.h>
  19. #include <apt-pkg/metaindex.h>
  20. #include <algorithm>
  21. #include <fstream>
  22. #include <iostream>
  23. #include <stdarg.h>
  24. #include <sys/stat.h>
  25. #include <sys/types.h>
  26. #include <sys/utsname.h>
  27. #include <dirent.h>
  28. using namespace std;
  29. #include<sstream>
  30. #include "mirror.h"
  31. #include "http.h"
  32. #include <apti18n.h>
  33. /*}}}*/
  34. /* Done:
  35. * - works with http (only!)
  36. * - always picks the first mirror from the list
  37. * - call out to problem reporting script
  38. * - supports "deb mirror://host/path/to/mirror-list/// dist component"
  39. * - uses pkgAcqMethod::FailReason() to have a string representation
  40. * of the failure that is also send to LP
  41. *
  42. * TODO:
  43. * - deal with running as non-root because we can't write to the lists
  44. dir then -> use the cached mirror file
  45. * - better method to download than having a pkgAcquire interface here
  46. * and better error handling there!
  47. * - support more than http
  48. * - testing :)
  49. */
  50. MirrorMethod::MirrorMethod()
  51. : HttpMethod(), DownloadedMirrorFile(false), Debug(false)
  52. {
  53. };
  54. // HttpMethod::Configuration - Handle a configuration message /*{{{*/
  55. // ---------------------------------------------------------------------
  56. /* We stash the desired pipeline depth */
  57. bool MirrorMethod::Configuration(string Message)
  58. {
  59. if (pkgAcqMethod::Configuration(Message) == false)
  60. return false;
  61. Debug = _config->FindB("Debug::Acquire::mirror",false);
  62. return true;
  63. }
  64. /*}}}*/
  65. // clean the mirrors dir based on ttl information
  66. bool MirrorMethod::Clean(string Dir)
  67. {
  68. vector<metaIndex *>::const_iterator I;
  69. if(Debug)
  70. clog << "MirrorMethod::Clean(): " << Dir << endl;
  71. if(Dir == "/")
  72. return _error->Error("will not clean: '/'");
  73. // read sources.list
  74. pkgSourceList list;
  75. list.ReadMainList();
  76. DIR *D = opendir(Dir.c_str());
  77. if (D == 0)
  78. return _error->Errno("opendir",_("Unable to read %s"),Dir.c_str());
  79. string StartDir = SafeGetCWD();
  80. if (chdir(Dir.c_str()) != 0)
  81. {
  82. closedir(D);
  83. return _error->Errno("chdir",_("Unable to change to %s"),Dir.c_str());
  84. }
  85. for (struct dirent *Dir = readdir(D); Dir != 0; Dir = readdir(D))
  86. {
  87. // Skip some files..
  88. if (strcmp(Dir->d_name,"lock") == 0 ||
  89. strcmp(Dir->d_name,"partial") == 0 ||
  90. strcmp(Dir->d_name,".") == 0 ||
  91. strcmp(Dir->d_name,"..") == 0)
  92. continue;
  93. // see if we have that uri
  94. for(I=list.begin(); I != list.end(); ++I)
  95. {
  96. string uri = (*I)->GetURI();
  97. if(uri.compare(0, strlen("mirror://"), "mirror://") != 0)
  98. continue;
  99. string BaseUri = uri.substr(0,uri.size()-1);
  100. if (URItoFileName(BaseUri) == Dir->d_name)
  101. break;
  102. }
  103. // nothing found, nuke it
  104. if (I == list.end())
  105. unlink(Dir->d_name);
  106. };
  107. closedir(D);
  108. if (chdir(StartDir.c_str()) != 0)
  109. return _error->Errno("chdir",_("Unable to change to %s"),StartDir.c_str());
  110. return true;
  111. }
  112. bool MirrorMethod::DownloadMirrorFile(string mirror_uri_str)
  113. {
  114. // not that great to use pkgAcquire here, but we do not have
  115. // any other way right now
  116. string fetch = BaseUri;
  117. fetch.replace(0,strlen("mirror://"),"http://");
  118. #if 0 // no need for this, the getArchitectures() will also include the main
  119. // arch
  120. // append main architecture
  121. fetch += "?arch=" + _config->Find("Apt::Architecture");
  122. #endif
  123. // append all architectures
  124. std::vector<std::string> vec = APT::Configuration::getArchitectures();
  125. for (std::vector<std::string>::const_iterator I = vec.begin();
  126. I != vec.end(); ++I)
  127. if (I == vec.begin())
  128. fetch += "?arch=" + (*I);
  129. else
  130. fetch += "&arch=" + (*I);
  131. // append the dist as a query string
  132. if (Dist != "")
  133. fetch += "&dist=" + Dist;
  134. if(Debug)
  135. clog << "MirrorMethod::DownloadMirrorFile(): '" << fetch << "'"
  136. << " to " << MirrorFile << endl;
  137. pkgAcquire Fetcher;
  138. new pkgAcqFile(&Fetcher, fetch, "", 0, "", "", "", MirrorFile);
  139. bool res = (Fetcher.Run() == pkgAcquire::Continue);
  140. if(res) {
  141. DownloadedMirrorFile = true;
  142. chmod(MirrorFile.c_str(), 0644);
  143. }
  144. Fetcher.Shutdown();
  145. if(Debug)
  146. clog << "MirrorMethod::DownloadMirrorFile() success: " << res << endl;
  147. return res;
  148. }
  149. // Randomizes the lines in the mirror file, this is used so that
  150. // we spread the load on the mirrors evenly
  151. bool MirrorMethod::RandomizeMirrorFile(string mirror_file)
  152. {
  153. vector<string> content;
  154. string line;
  155. if (!FileExists(mirror_file))
  156. return false;
  157. // read
  158. ifstream in(mirror_file.c_str());
  159. while ( !in.eof() ) {
  160. getline(in, line);
  161. content.push_back(line);
  162. }
  163. // we want the file to be random for each different machine, but also
  164. // "stable" on the same machine. this is to avoid running into out-of-sync
  165. // issues (i.e. Release/Release.gpg different on each mirror)
  166. struct utsname buf;
  167. int seed=1;
  168. if(uname(&buf) == 0) {
  169. for(int i=0,seed=1; buf.nodename[i] != 0; ++i) {
  170. seed = seed * 31 + buf.nodename[i];
  171. }
  172. }
  173. srand( seed );
  174. random_shuffle(content.begin(), content.end());
  175. // write
  176. ofstream out(mirror_file.c_str());
  177. while ( !content.empty()) {
  178. line = content.back();
  179. content.pop_back();
  180. out << line << "\n";
  181. }
  182. return true;
  183. }
  184. /* convert a the Queue->Uri back to the mirror base uri and look
  185. * at all mirrors we have for this, this is needed as queue->uri
  186. * may point to different mirrors (if TryNextMirror() was run)
  187. */
  188. void MirrorMethod::CurrentQueueUriToMirror()
  189. {
  190. // already in mirror:// style so nothing to do
  191. if(Queue->Uri.find("mirror://") == 0)
  192. return;
  193. // find current mirror and select next one
  194. for (vector<string>::const_iterator mirror = AllMirrors.begin();
  195. mirror != AllMirrors.end(); ++mirror)
  196. {
  197. if (Queue->Uri.find(*mirror) == 0)
  198. {
  199. Queue->Uri.replace(0, mirror->length(), BaseUri);
  200. return;
  201. }
  202. }
  203. _error->Error("Internal error: Failed to convert %s back to %s",
  204. Queue->Uri.c_str(), BaseUri.c_str());
  205. }
  206. bool MirrorMethod::TryNextMirror()
  207. {
  208. // find current mirror and select next one
  209. for (vector<string>::const_iterator mirror = AllMirrors.begin();
  210. mirror != AllMirrors.end(); ++mirror)
  211. {
  212. if (Queue->Uri.find(*mirror) != 0)
  213. continue;
  214. vector<string>::const_iterator nextmirror = mirror + 1;
  215. if (nextmirror == AllMirrors.end())
  216. break;
  217. Queue->Uri.replace(0, mirror->length(), *nextmirror);
  218. if (Debug)
  219. clog << "TryNextMirror: " << Queue->Uri << endl;
  220. // inform parent
  221. UsedMirror = *nextmirror;
  222. Log("Switching mirror");
  223. return true;
  224. }
  225. if (Debug)
  226. clog << "TryNextMirror could not find another mirror to try" << endl;
  227. return false;
  228. }
  229. bool MirrorMethod::InitMirrors()
  230. {
  231. // if we do not have a MirrorFile, fallback
  232. if(!FileExists(MirrorFile))
  233. {
  234. // FIXME: fallback to a default mirror here instead
  235. // and provide a config option to define that default
  236. return _error->Error(_("No mirror file '%s' found "), MirrorFile.c_str());
  237. }
  238. if (access(MirrorFile.c_str(), R_OK) != 0)
  239. {
  240. // FIXME: fallback to a default mirror here instead
  241. // and provide a config option to define that default
  242. return _error->Error(_("Can not read mirror file '%s'"), MirrorFile.c_str());
  243. }
  244. // FIXME: make the mirror selection more clever, do not
  245. // just use the first one!
  246. // BUT: we can not make this random, the mirror has to be
  247. // stable across session, because otherwise we can
  248. // get into sync issues (got indexfiles from mirror A,
  249. // but packages from mirror B - one might be out of date etc)
  250. ifstream in(MirrorFile.c_str());
  251. string s;
  252. while (!in.eof())
  253. {
  254. getline(in, s);
  255. // ignore lines that start with #
  256. if (s.find("#") == 0)
  257. continue;
  258. // ignore empty lines
  259. if (s.size() == 0)
  260. continue;
  261. // ignore non http lines
  262. if (s.compare(0, strlen("http://"), "http://") != 0)
  263. continue;
  264. AllMirrors.push_back(s);
  265. }
  266. if (AllMirrors.empty()) {
  267. return _error->Error(_("No entry found in mirror file '%s'"), MirrorFile.c_str());
  268. }
  269. Mirror = AllMirrors[0];
  270. UsedMirror = Mirror;
  271. return true;
  272. }
  273. string MirrorMethod::GetMirrorFileName(string mirror_uri_str)
  274. {
  275. /*
  276. - a mirror_uri_str looks like this:
  277. mirror://people.ubuntu.com/~mvo/apt/mirror/mirrors/dists/feisty/Release.gpg
  278. - the matching source.list entry
  279. deb mirror://people.ubuntu.com/~mvo/apt/mirror/mirrors feisty main
  280. - we actually want to go after:
  281. http://people.ubuntu.com/~mvo/apt/mirror/mirrors
  282. And we need to save the BaseUri for later:
  283. - mirror://people.ubuntu.com/~mvo/apt/mirror/mirrors
  284. FIXME: what if we have two similar prefixes?
  285. mirror://people.ubuntu.com/~mvo/mirror
  286. mirror://people.ubuntu.com/~mvo/mirror2
  287. then mirror_uri_str looks like:
  288. mirror://people.ubuntu.com/~mvo/apt/mirror/dists/feisty/Release.gpg
  289. mirror://people.ubuntu.com/~mvo/apt/mirror2/dists/feisty/Release.gpg
  290. we search sources.list and find:
  291. mirror://people.ubuntu.com/~mvo/apt/mirror
  292. in both cases! So we need to apply some domain knowledge here :( and
  293. check for /dists/ or /Release.gpg as suffixes
  294. */
  295. string name;
  296. if(Debug)
  297. std::cerr << "GetMirrorFileName: " << mirror_uri_str << std::endl;
  298. // read sources.list and find match
  299. vector<metaIndex *>::const_iterator I;
  300. pkgSourceList list;
  301. list.ReadMainList();
  302. for(I=list.begin(); I != list.end(); ++I)
  303. {
  304. string uristr = (*I)->GetURI();
  305. if(Debug)
  306. std::cerr << "Checking: " << uristr << std::endl;
  307. if(uristr.substr(0,strlen("mirror://")) != string("mirror://"))
  308. continue;
  309. // find matching uri in sources.list
  310. if(mirror_uri_str.substr(0,uristr.size()) == uristr)
  311. {
  312. if(Debug)
  313. std::cerr << "found BaseURI: " << uristr << std::endl;
  314. BaseUri = uristr.substr(0,uristr.size()-1);
  315. Dist = (*I)->GetDist();
  316. }
  317. }
  318. // get new file
  319. name = _config->FindDir("Dir::State::mirrors") + URItoFileName(BaseUri);
  320. if(Debug)
  321. {
  322. cerr << "base-uri: " << BaseUri << endl;
  323. cerr << "mirror-file: " << name << endl;
  324. }
  325. return name;
  326. }
  327. // MirrorMethod::Fetch - Fetch an item /*{{{*/
  328. // ---------------------------------------------------------------------
  329. /* This adds an item to the pipeline. We keep the pipeline at a fixed
  330. depth. */
  331. bool MirrorMethod::Fetch(FetchItem *Itm)
  332. {
  333. if(Debug)
  334. clog << "MirrorMethod::Fetch()" << endl;
  335. // the http method uses Fetch(0) as a way to update the pipeline,
  336. // just let it do its work in this case - Fetch() with a valid
  337. // Itm will always run before the first Fetch(0)
  338. if(Itm == NULL)
  339. return HttpMethod::Fetch(Itm);
  340. // if we don't have the name of the mirror file on disk yet,
  341. // calculate it now (can be derived from the uri)
  342. if(MirrorFile.empty())
  343. MirrorFile = GetMirrorFileName(Itm->Uri);
  344. // download mirror file once (if we are after index files)
  345. if(Itm->IndexFile && !DownloadedMirrorFile)
  346. {
  347. Clean(_config->FindDir("Dir::State::mirrors"));
  348. if (DownloadMirrorFile(Itm->Uri))
  349. RandomizeMirrorFile(MirrorFile);
  350. }
  351. if(AllMirrors.empty()) {
  352. if(!InitMirrors()) {
  353. // no valid mirror selected, something went wrong downloading
  354. // from the master mirror site most likely and there is
  355. // no old mirror file availalbe
  356. return false;
  357. }
  358. }
  359. if(Itm->Uri.find("mirror://") != string::npos)
  360. Itm->Uri.replace(0,BaseUri.size(), Mirror);
  361. if(Debug)
  362. clog << "Fetch: " << Itm->Uri << endl << endl;
  363. // now run the real fetcher
  364. return HttpMethod::Fetch(Itm);
  365. };
  366. void MirrorMethod::Fail(string Err,bool Transient)
  367. {
  368. // FIXME: TryNextMirror is not ideal for indexfile as we may
  369. // run into auth issues
  370. if (Debug)
  371. clog << "Failure to get " << Queue->Uri << endl;
  372. // try the next mirror on fail (if its not a expected failure,
  373. // e.g. translations are ok to ignore)
  374. if (!Queue->FailIgnore && TryNextMirror())
  375. return;
  376. // all mirrors failed, so bail out
  377. string s;
  378. strprintf(s, _("[Mirror: %s]"), Mirror.c_str());
  379. SetIP(s);
  380. CurrentQueueUriToMirror();
  381. pkgAcqMethod::Fail(Err, Transient);
  382. }
  383. void MirrorMethod::URIStart(FetchResult &Res)
  384. {
  385. CurrentQueueUriToMirror();
  386. pkgAcqMethod::URIStart(Res);
  387. }
  388. void MirrorMethod::URIDone(FetchResult &Res,FetchResult *Alt)
  389. {
  390. CurrentQueueUriToMirror();
  391. pkgAcqMethod::URIDone(Res, Alt);
  392. }
  393. int main()
  394. {
  395. setlocale(LC_ALL, "");
  396. MirrorMethod Mth;
  397. return Mth.Loop();
  398. }