acquire-item.cc 32 KB

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