acquire-item.cc 31 KB

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