acquire-item.cc 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119
  1. // -*- mode: cpp; mode: fold -*-
  2. // Description /*{{{*/
  3. // $Id: acquire-item.cc,v 1.46.2.9 2004/01/16 18:51:11 mdz Exp $
  4. /* ######################################################################
  5. Acquire Item - Item to acquire
  6. Each item can download to exactly one file at a time. This means you
  7. cannot create an item that fetches two uri's to two files at the same
  8. time. The pkgAcqIndex class creates a second class upon instantiation
  9. to fetch the other index files because of this.
  10. ##################################################################### */
  11. /*}}}*/
  12. // Include Files /*{{{*/
  13. #ifdef __GNUG__
  14. #pragma implementation "apt-pkg/acquire-item.h"
  15. #endif
  16. #include <apt-pkg/acquire-item.h>
  17. #include <apt-pkg/configuration.h>
  18. #include <apt-pkg/sourcelist.h>
  19. #include <apt-pkg/vendorlist.h>
  20. #include <apt-pkg/error.h>
  21. #include <apt-pkg/strutl.h>
  22. #include <apt-pkg/fileutl.h>
  23. #include <apt-pkg/md5.h>
  24. #include <apti18n.h>
  25. #include <sys/stat.h>
  26. #include <unistd.h>
  27. #include <errno.h>
  28. #include <string>
  29. #include <stdio.h>
  30. /*}}}*/
  31. using namespace std;
  32. // Acquire::Item::Item - Constructor /*{{{*/
  33. // ---------------------------------------------------------------------
  34. /* */
  35. pkgAcquire::Item::Item(pkgAcquire *Owner) : Owner(Owner), FileSize(0),
  36. PartialSize(0), Mode(0), ID(0), Complete(false),
  37. Local(false), QueueCounter(0)
  38. {
  39. Owner->Add(this);
  40. Status = StatIdle;
  41. }
  42. /*}}}*/
  43. // Acquire::Item::~Item - Destructor /*{{{*/
  44. // ---------------------------------------------------------------------
  45. /* */
  46. pkgAcquire::Item::~Item()
  47. {
  48. Owner->Remove(this);
  49. }
  50. /*}}}*/
  51. // Acquire::Item::Failed - Item failed to download /*{{{*/
  52. // ---------------------------------------------------------------------
  53. /* We return to an idle state if there are still other queues that could
  54. fetch this object */
  55. void pkgAcquire::Item::Failed(string Message,pkgAcquire::MethodConfig *Cnf)
  56. {
  57. Status = StatIdle;
  58. ErrorText = LookupTag(Message,"Message");
  59. if (QueueCounter <= 1)
  60. {
  61. /* This indicates that the file is not available right now but might
  62. be sometime later. If we do a retry cycle then this should be
  63. retried [CDROMs] */
  64. if (Cnf->LocalOnly == true &&
  65. StringToBool(LookupTag(Message,"Transient-Failure"),false) == true)
  66. {
  67. Status = StatIdle;
  68. Dequeue();
  69. return;
  70. }
  71. Status = StatError;
  72. Dequeue();
  73. }
  74. }
  75. /*}}}*/
  76. // Acquire::Item::Start - Item has begun to download /*{{{*/
  77. // ---------------------------------------------------------------------
  78. /* Stash status and the file size. Note that setting Complete means
  79. sub-phases of the acquire process such as decompresion are operating */
  80. void pkgAcquire::Item::Start(string /*Message*/,unsigned long Size)
  81. {
  82. Status = StatFetching;
  83. if (FileSize == 0 && Complete == false)
  84. FileSize = Size;
  85. }
  86. /*}}}*/
  87. // Acquire::Item::Done - Item downloaded OK /*{{{*/
  88. // ---------------------------------------------------------------------
  89. /* */
  90. void pkgAcquire::Item::Done(string Message,unsigned long Size,string,
  91. pkgAcquire::MethodConfig *Cnf)
  92. {
  93. // We just downloaded something..
  94. string FileName = LookupTag(Message,"Filename");
  95. if (Complete == false && FileName == DestFile)
  96. {
  97. if (Owner->Log != 0)
  98. Owner->Log->Fetched(Size,atoi(LookupTag(Message,"Resume-Point","0").c_str()));
  99. }
  100. if (FileSize == 0)
  101. FileSize= Size;
  102. Status = StatDone;
  103. ErrorText = string();
  104. Owner->Dequeue(this);
  105. }
  106. /*}}}*/
  107. // Acquire::Item::Rename - Rename a file /*{{{*/
  108. // ---------------------------------------------------------------------
  109. /* This helper function is used by alot of item methods as thier final
  110. step */
  111. void pkgAcquire::Item::Rename(string From,string To)
  112. {
  113. if (rename(From.c_str(),To.c_str()) != 0)
  114. {
  115. char S[300];
  116. snprintf(S,sizeof(S),_("rename failed, %s (%s -> %s)."),strerror(errno),
  117. From.c_str(),To.c_str());
  118. Status = StatError;
  119. ErrorText = S;
  120. }
  121. }
  122. /*}}}*/
  123. // AcqIndex::AcqIndex - Constructor /*{{{*/
  124. // ---------------------------------------------------------------------
  125. /* The package file is added to the queue and a second class is
  126. instantiated to fetch the revision file */
  127. pkgAcqIndex::pkgAcqIndex(pkgAcquire *Owner,
  128. string URI,string URIDesc,string ShortDesc,
  129. string ExpectedMD5, string comprExt) :
  130. Item(Owner), RealURI(URI), ExpectedMD5(ExpectedMD5)
  131. {
  132. Decompression = false;
  133. Erase = false;
  134. DestFile = _config->FindDir("Dir::State::lists") + "partial/";
  135. DestFile += URItoFileName(URI);
  136. if(comprExt.empty())
  137. {
  138. // autoselect the compression method
  139. if(FileExists("/usr/bin/bzip2"))
  140. CompressionExtension = ".bz2";
  141. else
  142. CompressionExtension = ".gz";
  143. } else {
  144. CompressionExtension = comprExt;
  145. }
  146. Desc.URI = URI + CompressionExtension;
  147. Desc.Description = URIDesc;
  148. Desc.Owner = this;
  149. Desc.ShortDesc = ShortDesc;
  150. QueueURI(Desc);
  151. }
  152. /*}}}*/
  153. // AcqIndex::Custom600Headers - Insert custom request headers /*{{{*/
  154. // ---------------------------------------------------------------------
  155. /* The only header we use is the last-modified header. */
  156. string pkgAcqIndex::Custom600Headers()
  157. {
  158. string Final = _config->FindDir("Dir::State::lists");
  159. Final += URItoFileName(RealURI);
  160. struct stat Buf;
  161. if (stat(Final.c_str(),&Buf) != 0)
  162. return "\nIndex-File: true";
  163. return "\nIndex-File: true\nLast-Modified: " + TimeRFC1123(Buf.st_mtime);
  164. }
  165. /*}}}*/
  166. void pkgAcqIndex::Failed(string Message,pkgAcquire::MethodConfig *Cnf)
  167. {
  168. // no .bz2 found, retry with .gz
  169. if(Desc.URI.substr(Desc.URI.size()-3,Desc.URI.size()-1) == "bz2") {
  170. Desc.URI = Desc.URI.substr(0,Desc.URI.size()-3) + "gz";
  171. // retry with a gzip one
  172. new pkgAcqIndex(Owner, RealURI, Desc.Description,Desc.ShortDesc,
  173. ExpectedMD5, string(".gz"));
  174. Status = StatDone;
  175. Complete = false;
  176. Dequeue();
  177. return;
  178. }
  179. Item::Failed(Message,Cnf);
  180. }
  181. // AcqIndex::Done - Finished a fetch /*{{{*/
  182. // ---------------------------------------------------------------------
  183. /* This goes through a number of states.. On the initial fetch the
  184. method could possibly return an alternate filename which points
  185. to the uncompressed version of the file. If this is so the file
  186. is copied into the partial directory. In all other cases the file
  187. is decompressed with a gzip uri. */
  188. void pkgAcqIndex::Done(string Message,unsigned long Size,string MD5,
  189. pkgAcquire::MethodConfig *Cfg)
  190. {
  191. Item::Done(Message,Size,MD5,Cfg);
  192. if (Decompression == true)
  193. {
  194. if (_config->FindB("Debug::pkgAcquire::Auth", false))
  195. {
  196. std::cerr << std::endl << RealURI << ": Computed MD5: " << MD5;
  197. std::cerr << " Expected MD5: " << ExpectedMD5 << std::endl;
  198. }
  199. if (MD5.empty())
  200. {
  201. MD5Summation sum;
  202. FileFd Fd(DestFile, FileFd::ReadOnly);
  203. sum.AddFD(Fd.Fd(), Fd.Size());
  204. Fd.Close();
  205. MD5 = (string)sum.Result();
  206. }
  207. if (!ExpectedMD5.empty() && MD5 != ExpectedMD5)
  208. {
  209. Status = StatAuthError;
  210. ErrorText = _("MD5Sum mismatch");
  211. Rename(DestFile,DestFile + ".FAILED");
  212. return;
  213. }
  214. // Done, move it into position
  215. string FinalFile = _config->FindDir("Dir::State::lists");
  216. FinalFile += URItoFileName(RealURI);
  217. Rename(DestFile,FinalFile);
  218. chmod(FinalFile.c_str(),0644);
  219. /* We restore the original name to DestFile so that the clean operation
  220. will work OK */
  221. DestFile = _config->FindDir("Dir::State::lists") + "partial/";
  222. DestFile += URItoFileName(RealURI);
  223. // Remove the compressed version.
  224. if (Erase == true)
  225. unlink(DestFile.c_str());
  226. return;
  227. }
  228. Erase = false;
  229. Complete = true;
  230. // Handle the unzipd case
  231. string FileName = LookupTag(Message,"Alt-Filename");
  232. if (FileName.empty() == false)
  233. {
  234. // The files timestamp matches
  235. if (StringToBool(LookupTag(Message,"Alt-IMS-Hit"),false) == true)
  236. return;
  237. Decompression = true;
  238. Local = true;
  239. DestFile += ".decomp";
  240. Desc.URI = "copy:" + FileName;
  241. QueueURI(Desc);
  242. Mode = "copy";
  243. return;
  244. }
  245. FileName = LookupTag(Message,"Filename");
  246. if (FileName.empty() == true)
  247. {
  248. Status = StatError;
  249. ErrorText = "Method gave a blank filename";
  250. }
  251. // The files timestamp matches
  252. if (StringToBool(LookupTag(Message,"IMS-Hit"),false) == true)
  253. return;
  254. if (FileName == DestFile)
  255. Erase = true;
  256. else
  257. Local = true;
  258. string compExt = Desc.URI.substr(Desc.URI.size()-3,Desc.URI.size()-1);
  259. char *decompProg;
  260. if(compExt == "bz2")
  261. decompProg = "bzip2";
  262. else if(compExt == ".gz")
  263. decompProg = "gzip";
  264. else {
  265. _error->Error("Unsupported extension: %s", compExt.c_str());
  266. return;
  267. }
  268. Decompression = true;
  269. DestFile += ".decomp";
  270. Desc.URI = string(decompProg) + ":" + FileName;
  271. QueueURI(Desc);
  272. Mode = decompProg;
  273. }
  274. pkgAcqMetaSig::pkgAcqMetaSig(pkgAcquire *Owner,
  275. string URI,string URIDesc,string ShortDesc,
  276. string MetaIndexURI, string MetaIndexURIDesc,
  277. string MetaIndexShortDesc,
  278. const vector<IndexTarget*>* IndexTargets,
  279. indexRecords* MetaIndexParser) :
  280. Item(Owner), RealURI(URI), MetaIndexURI(MetaIndexURI),
  281. MetaIndexURIDesc(MetaIndexURIDesc), MetaIndexShortDesc(MetaIndexShortDesc)
  282. {
  283. this->MetaIndexParser = MetaIndexParser;
  284. this->IndexTargets = IndexTargets;
  285. DestFile = _config->FindDir("Dir::State::lists") + "partial/";
  286. DestFile += URItoFileName(URI);
  287. // remove any partial downloaded sig-file. it may confuse proxies
  288. // and is too small to warrant a partial download anyway
  289. unlink(DestFile.c_str());
  290. // Create the item
  291. Desc.Description = URIDesc;
  292. Desc.Owner = this;
  293. Desc.ShortDesc = ShortDesc;
  294. Desc.URI = URI;
  295. string Final = _config->FindDir("Dir::State::lists");
  296. Final += URItoFileName(RealURI);
  297. struct stat Buf;
  298. if (stat(Final.c_str(),&Buf) == 0)
  299. {
  300. // File was already in place. It needs to be re-verified
  301. // because Release might have changed, so Move it into partial
  302. Rename(Final,DestFile);
  303. // unlink the file and do not try to use I-M-S and Last-Modified
  304. // if the users proxy is broken
  305. if(_config->FindB("Acquire::BrokenProxy", false) == true) {
  306. std::cerr << "forcing re-get of the signature file as requested" << std::endl;
  307. unlink(DestFile.c_str());
  308. }
  309. }
  310. QueueURI(Desc);
  311. }
  312. /*}}}*/
  313. // pkgAcqMetaSig::Custom600Headers - Insert custom request headers /*{{{*/
  314. // ---------------------------------------------------------------------
  315. /* The only header we use is the last-modified header. */
  316. string pkgAcqMetaSig::Custom600Headers()
  317. {
  318. struct stat Buf;
  319. if (stat(DestFile.c_str(),&Buf) != 0)
  320. return "\nIndex-File: true";
  321. return "\nIndex-File: true\nLast-Modified: " + TimeRFC1123(Buf.st_mtime);
  322. }
  323. void pkgAcqMetaSig::Done(string Message,unsigned long Size,string MD5,
  324. pkgAcquire::MethodConfig *Cfg)
  325. {
  326. Item::Done(Message,Size,MD5,Cfg);
  327. string FileName = LookupTag(Message,"Filename");
  328. if (FileName.empty() == true)
  329. {
  330. Status = StatError;
  331. ErrorText = "Method gave a blank filename";
  332. return;
  333. }
  334. if (FileName != DestFile)
  335. {
  336. // We have to copy it into place
  337. Local = true;
  338. Desc.URI = "copy:" + FileName;
  339. QueueURI(Desc);
  340. return;
  341. }
  342. Complete = true;
  343. // queue a pkgAcqMetaIndex to be verified against the sig we just retrieved
  344. new pkgAcqMetaIndex(Owner, MetaIndexURI, MetaIndexURIDesc, MetaIndexShortDesc,
  345. DestFile, IndexTargets, MetaIndexParser);
  346. }
  347. /*}}}*/
  348. void pkgAcqMetaSig::Failed(string Message,pkgAcquire::MethodConfig *Cnf)
  349. {
  350. // Delete any existing sigfile, so that this source isn't
  351. // mistakenly trusted
  352. string Final = _config->FindDir("Dir::State::lists") + URItoFileName(RealURI);
  353. unlink(Final.c_str());
  354. // if we get a timeout if fail
  355. if(LookupTag(Message,"FailReason") == "Timeout" ||
  356. LookupTag(Message,"FailReason") == "TmpResolveFailure") {
  357. Item::Failed(Message,Cnf);
  358. return;
  359. }
  360. // queue a pkgAcqMetaIndex with no sigfile
  361. new pkgAcqMetaIndex(Owner, MetaIndexURI, MetaIndexURIDesc, MetaIndexShortDesc,
  362. "", IndexTargets, MetaIndexParser);
  363. if (Cnf->LocalOnly == true ||
  364. StringToBool(LookupTag(Message,"Transient-Failure"),false) == false)
  365. {
  366. // Ignore this
  367. Status = StatDone;
  368. Complete = false;
  369. Dequeue();
  370. return;
  371. }
  372. Item::Failed(Message,Cnf);
  373. }
  374. pkgAcqMetaIndex::pkgAcqMetaIndex(pkgAcquire *Owner,
  375. string URI,string URIDesc,string ShortDesc,
  376. string SigFile,
  377. const vector<struct IndexTarget*>* IndexTargets,
  378. indexRecords* MetaIndexParser) :
  379. Item(Owner), RealURI(URI), SigFile(SigFile)
  380. {
  381. this->AuthPass = false;
  382. this->MetaIndexParser = MetaIndexParser;
  383. this->IndexTargets = IndexTargets;
  384. DestFile = _config->FindDir("Dir::State::lists") + "partial/";
  385. DestFile += URItoFileName(URI);
  386. // Create the item
  387. Desc.Description = URIDesc;
  388. Desc.Owner = this;
  389. Desc.ShortDesc = ShortDesc;
  390. Desc.URI = URI;
  391. QueueURI(Desc);
  392. }
  393. /*}}}*/
  394. // pkgAcqMetaIndex::Custom600Headers - Insert custom request headers /*{{{*/
  395. // ---------------------------------------------------------------------
  396. /* The only header we use is the last-modified header. */
  397. string pkgAcqMetaIndex::Custom600Headers()
  398. {
  399. string Final = _config->FindDir("Dir::State::lists");
  400. Final += URItoFileName(RealURI);
  401. struct stat Buf;
  402. if (stat(Final.c_str(),&Buf) != 0)
  403. return "\nIndex-File: true";
  404. return "\nIndex-File: true\nLast-Modified: " + TimeRFC1123(Buf.st_mtime);
  405. }
  406. void pkgAcqMetaIndex::Done(string Message,unsigned long Size,string MD5,
  407. pkgAcquire::MethodConfig *Cfg)
  408. {
  409. Item::Done(Message,Size,MD5,Cfg);
  410. // MetaIndexes are done in two passes: one to download the
  411. // metaindex with an appropriate method, and a second to verify it
  412. // with the gpgv method
  413. if (AuthPass == true)
  414. {
  415. AuthDone(Message);
  416. }
  417. else
  418. {
  419. RetrievalDone(Message);
  420. if (!Complete)
  421. // Still more retrieving to do
  422. return;
  423. if (SigFile == "")
  424. {
  425. // There was no signature file, so we are finished. Download
  426. // the indexes without verification.
  427. QueueIndexes(false);
  428. }
  429. else
  430. {
  431. // There was a signature file, so pass it to gpgv for
  432. // verification
  433. if (_config->FindB("Debug::pkgAcquire::Auth", false))
  434. std::cerr << "Metaindex acquired, queueing gpg verification ("
  435. << SigFile << "," << DestFile << ")\n";
  436. AuthPass = true;
  437. Desc.URI = "gpgv:" + SigFile;
  438. QueueURI(Desc);
  439. Mode = "gpgv";
  440. }
  441. }
  442. }
  443. void pkgAcqMetaIndex::RetrievalDone(string Message)
  444. {
  445. // We have just finished downloading a Release file (it is not
  446. // verified yet)
  447. string FileName = LookupTag(Message,"Filename");
  448. if (FileName.empty() == true)
  449. {
  450. Status = StatError;
  451. ErrorText = "Method gave a blank filename";
  452. return;
  453. }
  454. if (FileName != DestFile)
  455. {
  456. Local = true;
  457. Desc.URI = "copy:" + FileName;
  458. QueueURI(Desc);
  459. return;
  460. }
  461. Complete = true;
  462. string FinalFile = _config->FindDir("Dir::State::lists");
  463. FinalFile += URItoFileName(RealURI);
  464. // The files timestamp matches
  465. if (StringToBool(LookupTag(Message,"IMS-Hit"),false) == false)
  466. {
  467. // Move it into position
  468. Rename(DestFile,FinalFile);
  469. }
  470. DestFile = FinalFile;
  471. }
  472. void pkgAcqMetaIndex::AuthDone(string Message)
  473. {
  474. // At this point, the gpgv method has succeeded, so there is a
  475. // valid signature from a key in the trusted keyring. We
  476. // perform additional verification of its contents, and use them
  477. // to verify the indexes we are about to download
  478. if (!MetaIndexParser->Load(DestFile))
  479. {
  480. Status = StatAuthError;
  481. ErrorText = MetaIndexParser->ErrorText;
  482. return;
  483. }
  484. if (!VerifyVendor())
  485. {
  486. return;
  487. }
  488. if (_config->FindB("Debug::pkgAcquire::Auth", false))
  489. std::cerr << "Signature verification succeeded: "
  490. << DestFile << std::endl;
  491. // Download further indexes with verification
  492. QueueIndexes(true);
  493. // Done, move signature file into position
  494. string VerifiedSigFile = _config->FindDir("Dir::State::lists") +
  495. URItoFileName(RealURI) + ".gpg";
  496. Rename(SigFile,VerifiedSigFile);
  497. chmod(VerifiedSigFile.c_str(),0644);
  498. }
  499. void pkgAcqMetaIndex::QueueIndexes(bool verify)
  500. {
  501. for (vector <struct IndexTarget*>::const_iterator Target = IndexTargets->begin();
  502. Target != IndexTargets->end();
  503. Target++)
  504. {
  505. string ExpectedIndexMD5;
  506. if (verify)
  507. {
  508. const indexRecords::checkSum *Record = MetaIndexParser->Lookup((*Target)->MetaKey);
  509. if (!Record)
  510. {
  511. Status = StatAuthError;
  512. ErrorText = "Unable to find expected entry "
  513. + (*Target)->MetaKey + " in Meta-index file (malformed Release file?)";
  514. return;
  515. }
  516. ExpectedIndexMD5 = Record->MD5Hash;
  517. if (_config->FindB("Debug::pkgAcquire::Auth", false))
  518. {
  519. std::cerr << "Queueing: " << (*Target)->URI << std::endl;
  520. std::cerr << "Expected MD5: " << ExpectedIndexMD5 << std::endl;
  521. }
  522. if (ExpectedIndexMD5.empty())
  523. {
  524. Status = StatAuthError;
  525. ErrorText = "Unable to find MD5 sum for "
  526. + (*Target)->MetaKey + " in Meta-index file";
  527. return;
  528. }
  529. }
  530. // Queue Packages file
  531. new pkgAcqIndex(Owner, (*Target)->URI, (*Target)->Description,
  532. (*Target)->ShortDesc, ExpectedIndexMD5);
  533. }
  534. }
  535. bool pkgAcqMetaIndex::VerifyVendor()
  536. {
  537. // // Maybe this should be made available from above so we don't have
  538. // // to read and parse it every time?
  539. // pkgVendorList List;
  540. // List.ReadMainList();
  541. // const Vendor* Vndr = NULL;
  542. // for (std::vector<string>::const_iterator I = GPGVOutput.begin(); I != GPGVOutput.end(); I++)
  543. // {
  544. // string::size_type pos = (*I).find("VALIDSIG ");
  545. // if (_config->FindB("Debug::Vendor", false))
  546. // std::cerr << "Looking for VALIDSIG in \"" << (*I) << "\": pos " << pos
  547. // << std::endl;
  548. // if (pos != std::string::npos)
  549. // {
  550. // string Fingerprint = (*I).substr(pos+sizeof("VALIDSIG"));
  551. // if (_config->FindB("Debug::Vendor", false))
  552. // std::cerr << "Looking for \"" << Fingerprint << "\" in vendor..." <<
  553. // std::endl;
  554. // Vndr = List.FindVendor(Fingerprint) != "";
  555. // if (Vndr != NULL);
  556. // break;
  557. // }
  558. // }
  559. string Transformed = MetaIndexParser->GetExpectedDist();
  560. if (Transformed == "../project/experimental")
  561. {
  562. Transformed = "experimental";
  563. }
  564. string::size_type pos = Transformed.rfind('/');
  565. if (pos != string::npos)
  566. {
  567. Transformed = Transformed.substr(0, pos);
  568. }
  569. if (Transformed == ".")
  570. {
  571. Transformed = "";
  572. }
  573. if (_config->FindB("Debug::pkgAcquire::Auth", false))
  574. {
  575. std::cerr << "Got Codename: " << MetaIndexParser->GetDist() << std::endl;
  576. std::cerr << "Expecting Dist: " << MetaIndexParser->GetExpectedDist() << std::endl;
  577. std::cerr << "Transformed Dist: " << Transformed << std::endl;
  578. }
  579. if (MetaIndexParser->CheckDist(Transformed) == false)
  580. {
  581. // This might become fatal one day
  582. // Status = StatAuthError;
  583. // ErrorText = "Conflicting distribution; expected "
  584. // + MetaIndexParser->GetExpectedDist() + " but got "
  585. // + MetaIndexParser->GetDist();
  586. // return false;
  587. if (!Transformed.empty())
  588. {
  589. _error->Warning("Conflicting distribution: %s (expected %s but got %s)",
  590. Desc.Description.c_str(),
  591. Transformed.c_str(),
  592. MetaIndexParser->GetDist().c_str());
  593. }
  594. }
  595. return true;
  596. }
  597. /*}}}*/
  598. // pkgAcqMetaIndex::Failed - no Release file present or no signature
  599. // file present /*{{{*/
  600. // ---------------------------------------------------------------------
  601. /* */
  602. void pkgAcqMetaIndex::Failed(string Message,pkgAcquire::MethodConfig *Cnf)
  603. {
  604. if (AuthPass == true)
  605. {
  606. // gpgv method failed
  607. _error->Warning("GPG error: %s: %s",
  608. Desc.Description.c_str(),
  609. LookupTag(Message,"Message").c_str());
  610. }
  611. // No Release file was present, or verification failed, so fall
  612. // back to queueing Packages files without verification
  613. QueueIndexes(false);
  614. }
  615. /*}}}*/
  616. // AcqArchive::AcqArchive - Constructor /*{{{*/
  617. // ---------------------------------------------------------------------
  618. /* This just sets up the initial fetch environment and queues the first
  619. possibilitiy */
  620. pkgAcqArchive::pkgAcqArchive(pkgAcquire *Owner,pkgSourceList *Sources,
  621. pkgRecords *Recs,pkgCache::VerIterator const &Version,
  622. string &StoreFilename) :
  623. Item(Owner), Version(Version), Sources(Sources), Recs(Recs),
  624. StoreFilename(StoreFilename), Vf(Version.FileList()),
  625. Trusted(false)
  626. {
  627. Retries = _config->FindI("Acquire::Retries",0);
  628. if (Version.Arch() == 0)
  629. {
  630. _error->Error(_("I wasn't able to locate a file for the %s package. "
  631. "This might mean you need to manually fix this package. "
  632. "(due to missing arch)"),
  633. Version.ParentPkg().Name());
  634. return;
  635. }
  636. /* We need to find a filename to determine the extension. We make the
  637. assumption here that all the available sources for this version share
  638. the same extension.. */
  639. // Skip not source sources, they do not have file fields.
  640. for (; Vf.end() == false; Vf++)
  641. {
  642. if ((Vf.File()->Flags & pkgCache::Flag::NotSource) != 0)
  643. continue;
  644. break;
  645. }
  646. // Does not really matter here.. we are going to fail out below
  647. if (Vf.end() != true)
  648. {
  649. // If this fails to get a file name we will bomb out below.
  650. pkgRecords::Parser &Parse = Recs->Lookup(Vf);
  651. if (_error->PendingError() == true)
  652. return;
  653. // Generate the final file name as: package_version_arch.foo
  654. StoreFilename = QuoteString(Version.ParentPkg().Name(),"_:") + '_' +
  655. QuoteString(Version.VerStr(),"_:") + '_' +
  656. QuoteString(Version.Arch(),"_:.") +
  657. "." + flExtension(Parse.FileName());
  658. }
  659. // check if we have one trusted source for the package. if so, switch
  660. // to "TrustedOnly" mode
  661. for (pkgCache::VerFileIterator i = Version.FileList(); i.end() == false; i++)
  662. {
  663. pkgIndexFile *Index;
  664. if (Sources->FindIndex(i.File(),Index) == false)
  665. continue;
  666. if (_config->FindB("Debug::pkgAcquire::Auth", false))
  667. {
  668. std::cerr << "Checking index: " << Index->Describe()
  669. << "(Trusted=" << Index->IsTrusted() << ")\n";
  670. }
  671. if (Index->IsTrusted()) {
  672. Trusted = true;
  673. break;
  674. }
  675. }
  676. // Select a source
  677. if (QueueNext() == false && _error->PendingError() == false)
  678. _error->Error(_("I wasn't able to locate file for the %s package. "
  679. "This might mean you need to manually fix this package."),
  680. Version.ParentPkg().Name());
  681. }
  682. /*}}}*/
  683. // AcqArchive::QueueNext - Queue the next file source /*{{{*/
  684. // ---------------------------------------------------------------------
  685. /* This queues the next available file version for download. It checks if
  686. the archive is already available in the cache and stashs the MD5 for
  687. checking later. */
  688. bool pkgAcqArchive::QueueNext()
  689. {
  690. for (; Vf.end() == false; Vf++)
  691. {
  692. // Ignore not source sources
  693. if ((Vf.File()->Flags & pkgCache::Flag::NotSource) != 0)
  694. continue;
  695. // Try to cross match against the source list
  696. pkgIndexFile *Index;
  697. if (Sources->FindIndex(Vf.File(),Index) == false)
  698. continue;
  699. // only try to get a trusted package from another source if that source
  700. // is also trusted
  701. if(Trusted && !Index->IsTrusted())
  702. continue;
  703. // Grab the text package record
  704. pkgRecords::Parser &Parse = Recs->Lookup(Vf);
  705. if (_error->PendingError() == true)
  706. return false;
  707. string PkgFile = Parse.FileName();
  708. MD5 = Parse.MD5Hash();
  709. if (PkgFile.empty() == true)
  710. return _error->Error(_("The package index files are corrupted. No Filename: "
  711. "field for package %s."),
  712. Version.ParentPkg().Name());
  713. Desc.URI = Index->ArchiveURI(PkgFile);
  714. Desc.Description = Index->ArchiveInfo(Version);
  715. Desc.Owner = this;
  716. Desc.ShortDesc = Version.ParentPkg().Name();
  717. // See if we already have the file. (Legacy filenames)
  718. FileSize = Version->Size;
  719. string FinalFile = _config->FindDir("Dir::Cache::Archives") + flNotDir(PkgFile);
  720. struct stat Buf;
  721. if (stat(FinalFile.c_str(),&Buf) == 0)
  722. {
  723. // Make sure the size matches
  724. if ((unsigned)Buf.st_size == Version->Size)
  725. {
  726. Complete = true;
  727. Local = true;
  728. Status = StatDone;
  729. StoreFilename = DestFile = FinalFile;
  730. return true;
  731. }
  732. /* Hmm, we have a file and its size does not match, this means it is
  733. an old style mismatched arch */
  734. unlink(FinalFile.c_str());
  735. }
  736. // Check it again using the new style output filenames
  737. FinalFile = _config->FindDir("Dir::Cache::Archives") + flNotDir(StoreFilename);
  738. if (stat(FinalFile.c_str(),&Buf) == 0)
  739. {
  740. // Make sure the size matches
  741. if ((unsigned)Buf.st_size == Version->Size)
  742. {
  743. Complete = true;
  744. Local = true;
  745. Status = StatDone;
  746. StoreFilename = DestFile = FinalFile;
  747. return true;
  748. }
  749. /* Hmm, we have a file and its size does not match, this shouldnt
  750. happen.. */
  751. unlink(FinalFile.c_str());
  752. }
  753. DestFile = _config->FindDir("Dir::Cache::Archives") + "partial/" + flNotDir(StoreFilename);
  754. // Check the destination file
  755. if (stat(DestFile.c_str(),&Buf) == 0)
  756. {
  757. // Hmm, the partial file is too big, erase it
  758. if ((unsigned)Buf.st_size > Version->Size)
  759. unlink(DestFile.c_str());
  760. else
  761. PartialSize = Buf.st_size;
  762. }
  763. // Create the item
  764. Local = false;
  765. Desc.URI = Index->ArchiveURI(PkgFile);
  766. Desc.Description = Index->ArchiveInfo(Version);
  767. Desc.Owner = this;
  768. Desc.ShortDesc = Version.ParentPkg().Name();
  769. QueueURI(Desc);
  770. Vf++;
  771. return true;
  772. }
  773. return false;
  774. }
  775. /*}}}*/
  776. // AcqArchive::Done - Finished fetching /*{{{*/
  777. // ---------------------------------------------------------------------
  778. /* */
  779. void pkgAcqArchive::Done(string Message,unsigned long Size,string Md5Hash,
  780. pkgAcquire::MethodConfig *Cfg)
  781. {
  782. Item::Done(Message,Size,Md5Hash,Cfg);
  783. // Check the size
  784. if (Size != Version->Size)
  785. {
  786. Status = StatError;
  787. ErrorText = _("Size mismatch");
  788. return;
  789. }
  790. // Check the md5
  791. if (Md5Hash.empty() == false && MD5.empty() == false)
  792. {
  793. if (Md5Hash != MD5)
  794. {
  795. Status = StatError;
  796. ErrorText = _("MD5Sum mismatch");
  797. if(FileExists(DestFile))
  798. Rename(DestFile,DestFile + ".FAILED");
  799. return;
  800. }
  801. }
  802. // Grab the output filename
  803. string FileName = LookupTag(Message,"Filename");
  804. if (FileName.empty() == true)
  805. {
  806. Status = StatError;
  807. ErrorText = "Method gave a blank filename";
  808. return;
  809. }
  810. Complete = true;
  811. // Reference filename
  812. if (FileName != DestFile)
  813. {
  814. StoreFilename = DestFile = FileName;
  815. Local = true;
  816. return;
  817. }
  818. // Done, move it into position
  819. string FinalFile = _config->FindDir("Dir::Cache::Archives");
  820. FinalFile += flNotDir(StoreFilename);
  821. Rename(DestFile,FinalFile);
  822. StoreFilename = DestFile = FinalFile;
  823. Complete = true;
  824. }
  825. /*}}}*/
  826. // AcqArchive::Failed - Failure handler /*{{{*/
  827. // ---------------------------------------------------------------------
  828. /* Here we try other sources */
  829. void pkgAcqArchive::Failed(string Message,pkgAcquire::MethodConfig *Cnf)
  830. {
  831. ErrorText = LookupTag(Message,"Message");
  832. /* We don't really want to retry on failed media swaps, this prevents
  833. that. An interesting observation is that permanent failures are not
  834. recorded. */
  835. if (Cnf->Removable == true &&
  836. StringToBool(LookupTag(Message,"Transient-Failure"),false) == true)
  837. {
  838. // Vf = Version.FileList();
  839. while (Vf.end() == false) Vf++;
  840. StoreFilename = string();
  841. Item::Failed(Message,Cnf);
  842. return;
  843. }
  844. if (QueueNext() == false)
  845. {
  846. // This is the retry counter
  847. if (Retries != 0 &&
  848. Cnf->LocalOnly == false &&
  849. StringToBool(LookupTag(Message,"Transient-Failure"),false) == true)
  850. {
  851. Retries--;
  852. Vf = Version.FileList();
  853. if (QueueNext() == true)
  854. return;
  855. }
  856. StoreFilename = string();
  857. Item::Failed(Message,Cnf);
  858. }
  859. }
  860. /*}}}*/
  861. // AcqArchive::IsTrusted - Determine whether this archive comes from a
  862. // trusted source /*{{{*/
  863. // ---------------------------------------------------------------------
  864. bool pkgAcqArchive::IsTrusted()
  865. {
  866. return Trusted;
  867. }
  868. // AcqArchive::Finished - Fetching has finished, tidy up /*{{{*/
  869. // ---------------------------------------------------------------------
  870. /* */
  871. void pkgAcqArchive::Finished()
  872. {
  873. if (Status == pkgAcquire::Item::StatDone &&
  874. Complete == true)
  875. return;
  876. StoreFilename = string();
  877. }
  878. /*}}}*/
  879. // AcqFile::pkgAcqFile - Constructor /*{{{*/
  880. // ---------------------------------------------------------------------
  881. /* The file is added to the queue */
  882. pkgAcqFile::pkgAcqFile(pkgAcquire *Owner,string URI,string MD5,
  883. unsigned long Size,string Dsc,string ShortDesc) :
  884. Item(Owner), Md5Hash(MD5)
  885. {
  886. Retries = _config->FindI("Acquire::Retries",0);
  887. DestFile = flNotDir(URI);
  888. // Create the item
  889. Desc.URI = URI;
  890. Desc.Description = Dsc;
  891. Desc.Owner = this;
  892. // Set the short description to the archive component
  893. Desc.ShortDesc = ShortDesc;
  894. // Get the transfer sizes
  895. FileSize = Size;
  896. struct stat Buf;
  897. if (stat(DestFile.c_str(),&Buf) == 0)
  898. {
  899. // Hmm, the partial file is too big, erase it
  900. if ((unsigned)Buf.st_size > Size)
  901. unlink(DestFile.c_str());
  902. else
  903. PartialSize = Buf.st_size;
  904. }
  905. QueueURI(Desc);
  906. }
  907. /*}}}*/
  908. // AcqFile::Done - Item downloaded OK /*{{{*/
  909. // ---------------------------------------------------------------------
  910. /* */
  911. void pkgAcqFile::Done(string Message,unsigned long Size,string MD5,
  912. pkgAcquire::MethodConfig *Cnf)
  913. {
  914. // Check the md5
  915. if (Md5Hash.empty() == false && MD5.empty() == false)
  916. {
  917. if (Md5Hash != MD5)
  918. {
  919. Status = StatError;
  920. ErrorText = "MD5Sum mismatch";
  921. Rename(DestFile,DestFile + ".FAILED");
  922. return;
  923. }
  924. }
  925. Item::Done(Message,Size,MD5,Cnf);
  926. string FileName = LookupTag(Message,"Filename");
  927. if (FileName.empty() == true)
  928. {
  929. Status = StatError;
  930. ErrorText = "Method gave a blank filename";
  931. return;
  932. }
  933. Complete = true;
  934. // The files timestamp matches
  935. if (StringToBool(LookupTag(Message,"IMS-Hit"),false) == true)
  936. return;
  937. // We have to copy it into place
  938. if (FileName != DestFile)
  939. {
  940. Local = true;
  941. if (_config->FindB("Acquire::Source-Symlinks",true) == false ||
  942. Cnf->Removable == true)
  943. {
  944. Desc.URI = "copy:" + FileName;
  945. QueueURI(Desc);
  946. return;
  947. }
  948. // Erase the file if it is a symlink so we can overwrite it
  949. struct stat St;
  950. if (lstat(DestFile.c_str(),&St) == 0)
  951. {
  952. if (S_ISLNK(St.st_mode) != 0)
  953. unlink(DestFile.c_str());
  954. }
  955. // Symlink the file
  956. if (symlink(FileName.c_str(),DestFile.c_str()) != 0)
  957. {
  958. ErrorText = "Link to " + DestFile + " failure ";
  959. Status = StatError;
  960. Complete = false;
  961. }
  962. }
  963. }
  964. /*}}}*/
  965. // AcqFile::Failed - Failure handler /*{{{*/
  966. // ---------------------------------------------------------------------
  967. /* Here we try other sources */
  968. void pkgAcqFile::Failed(string Message,pkgAcquire::MethodConfig *Cnf)
  969. {
  970. ErrorText = LookupTag(Message,"Message");
  971. // This is the retry counter
  972. if (Retries != 0 &&
  973. Cnf->LocalOnly == false &&
  974. StringToBool(LookupTag(Message,"Transient-Failure"),false) == true)
  975. {
  976. Retries--;
  977. QueueURI(Desc);
  978. return;
  979. }
  980. Item::Failed(Message,Cnf);
  981. }
  982. /*}}}*/