acquire-item.cc 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739
  1. // -*- mode: cpp; mode: fold -*-
  2. // Description /*{{{*/
  3. // $Id: acquire-item.cc,v 1.46 2003/02/02 22:19:17 jgg 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/error.h>
  20. #include <apt-pkg/strutl.h>
  21. #include <apt-pkg/fileutl.h>
  22. #include <apti18n.h>
  23. #include <sys/stat.h>
  24. #include <unistd.h>
  25. #include <errno.h>
  26. #include <string>
  27. #include <stdio.h>
  28. /*}}}*/
  29. using std::string;
  30. // Acquire::Item::Item - Constructor /*{{{*/
  31. // ---------------------------------------------------------------------
  32. /* */
  33. pkgAcquire::Item::Item(pkgAcquire *Owner) : Owner(Owner), FileSize(0),
  34. PartialSize(0), Mode(0), ID(0), Complete(false),
  35. Local(false), QueueCounter(0)
  36. {
  37. Owner->Add(this);
  38. Status = StatIdle;
  39. }
  40. /*}}}*/
  41. // Acquire::Item::~Item - Destructor /*{{{*/
  42. // ---------------------------------------------------------------------
  43. /* */
  44. pkgAcquire::Item::~Item()
  45. {
  46. Owner->Remove(this);
  47. }
  48. /*}}}*/
  49. // Acquire::Item::Failed - Item failed to download /*{{{*/
  50. // ---------------------------------------------------------------------
  51. /* We return to an idle state if there are still other queues that could
  52. fetch this object */
  53. void pkgAcquire::Item::Failed(string Message,pkgAcquire::MethodConfig *Cnf)
  54. {
  55. Status = StatIdle;
  56. ErrorText = LookupTag(Message,"Message");
  57. if (QueueCounter <= 1)
  58. {
  59. /* This indicates that the file is not available right now but might
  60. be sometime later. If we do a retry cycle then this should be
  61. retried [CDROMs] */
  62. if (Cnf->LocalOnly == true &&
  63. StringToBool(LookupTag(Message,"Transient-Failure"),false) == true)
  64. {
  65. Status = StatIdle;
  66. Dequeue();
  67. return;
  68. }
  69. Status = StatError;
  70. Dequeue();
  71. }
  72. }
  73. /*}}}*/
  74. // Acquire::Item::Start - Item has begun to download /*{{{*/
  75. // ---------------------------------------------------------------------
  76. /* Stash status and the file size. Note that setting Complete means
  77. sub-phases of the acquire process such as decompresion are operating */
  78. void pkgAcquire::Item::Start(string /*Message*/,unsigned long Size)
  79. {
  80. Status = StatFetching;
  81. if (FileSize == 0 && Complete == false)
  82. FileSize = Size;
  83. }
  84. /*}}}*/
  85. // Acquire::Item::Done - Item downloaded OK /*{{{*/
  86. // ---------------------------------------------------------------------
  87. /* */
  88. void pkgAcquire::Item::Done(string Message,unsigned long Size,string,
  89. pkgAcquire::MethodConfig *Cnf)
  90. {
  91. // We just downloaded something..
  92. string FileName = LookupTag(Message,"Filename");
  93. if (Complete == false && FileName == DestFile)
  94. {
  95. if (Owner->Log != 0)
  96. Owner->Log->Fetched(Size,atoi(LookupTag(Message,"Resume-Point","0").c_str()));
  97. }
  98. if (FileSize == 0)
  99. FileSize= Size;
  100. Status = StatDone;
  101. ErrorText = string();
  102. Owner->Dequeue(this);
  103. }
  104. /*}}}*/
  105. // Acquire::Item::Rename - Rename a file /*{{{*/
  106. // ---------------------------------------------------------------------
  107. /* This helper function is used by alot of item methods as thier final
  108. step */
  109. void pkgAcquire::Item::Rename(string From,string To)
  110. {
  111. if (rename(From.c_str(),To.c_str()) != 0)
  112. {
  113. char S[300];
  114. snprintf(S,sizeof(S),_("rename failed, %s (%s -> %s)."),strerror(errno),
  115. From.c_str(),To.c_str());
  116. Status = StatError;
  117. ErrorText = S;
  118. }
  119. }
  120. /*}}}*/
  121. // AcqIndex::AcqIndex - Constructor /*{{{*/
  122. // ---------------------------------------------------------------------
  123. /* The package file is added to the queue and a second class is
  124. instantiated to fetch the revision file */
  125. pkgAcqIndex::pkgAcqIndex(pkgAcquire *Owner,
  126. string URI,string URIDesc,string ShortDesc) :
  127. Item(Owner), RealURI(URI)
  128. {
  129. Decompression = false;
  130. Erase = false;
  131. DestFile = _config->FindDir("Dir::State::lists") + "partial/";
  132. DestFile += URItoFileName(URI);
  133. // Create the item
  134. Desc.URI = URI + ".bz2";
  135. Desc.Description = URIDesc;
  136. Desc.Owner = this;
  137. Desc.ShortDesc = ShortDesc;
  138. QueueURI(Desc);
  139. }
  140. /*}}}*/
  141. // AcqIndex::Custom600Headers - Insert custom request headers /*{{{*/
  142. // ---------------------------------------------------------------------
  143. /* The only header we use is the last-modified header. */
  144. string pkgAcqIndex::Custom600Headers()
  145. {
  146. string Final = _config->FindDir("Dir::State::lists");
  147. Final += URItoFileName(RealURI);
  148. struct stat Buf;
  149. if (stat(Final.c_str(),&Buf) != 0)
  150. return "\nIndex-File: true";
  151. return "\nIndex-File: true\nLast-Modified: " + TimeRFC1123(Buf.st_mtime);
  152. }
  153. /*}}}*/
  154. void pkgAcqIndex::Failed(string Message,pkgAcquire::MethodConfig *Cnf)
  155. {
  156. // no .bz2 found, retry with .gz
  157. if(Desc.URI.substr(Desc.URI.size()-3,Desc.URI.size()-1) == "bz2") {
  158. Desc.URI = Desc.URI.substr(0,Desc.URI.size()-3) + "gz";
  159. QueueURI(Desc);
  160. return;
  161. }
  162. Item::Failed(Message,Cnf);
  163. }
  164. // AcqIndex::Done - Finished a fetch /*{{{*/
  165. // ---------------------------------------------------------------------
  166. /* This goes through a number of states.. On the initial fetch the
  167. method could possibly return an alternate filename which points
  168. to the uncompressed version of the file. If this is so the file
  169. is copied into the partial directory. In all other cases the file
  170. is decompressed with a gzip uri. */
  171. void pkgAcqIndex::Done(string Message,unsigned long Size,string MD5,
  172. pkgAcquire::MethodConfig *Cfg)
  173. {
  174. Item::Done(Message,Size,MD5,Cfg);
  175. if (Decompression == true)
  176. {
  177. // Done, move it into position
  178. string FinalFile = _config->FindDir("Dir::State::lists");
  179. FinalFile += URItoFileName(RealURI);
  180. Rename(DestFile,FinalFile);
  181. chmod(FinalFile.c_str(),0644);
  182. /* We restore the original name to DestFile so that the clean operation
  183. will work OK */
  184. DestFile = _config->FindDir("Dir::State::lists") + "partial/";
  185. DestFile += URItoFileName(RealURI);
  186. // Remove the compressed version.
  187. if (Erase == true)
  188. unlink(DestFile.c_str());
  189. return;
  190. }
  191. Erase = false;
  192. Complete = true;
  193. // Handle the unzipd case
  194. string FileName = LookupTag(Message,"Alt-Filename");
  195. if (FileName.empty() == false)
  196. {
  197. // The files timestamp matches
  198. if (StringToBool(LookupTag(Message,"Alt-IMS-Hit"),false) == true)
  199. return;
  200. Decompression = true;
  201. Local = true;
  202. DestFile += ".decomp";
  203. Desc.URI = "copy:" + FileName;
  204. QueueURI(Desc);
  205. Mode = "copy";
  206. return;
  207. }
  208. FileName = LookupTag(Message,"Filename");
  209. if (FileName.empty() == true)
  210. {
  211. Status = StatError;
  212. ErrorText = "Method gave a blank filename";
  213. }
  214. // The files timestamp matches
  215. if (StringToBool(LookupTag(Message,"IMS-Hit"),false) == true)
  216. return;
  217. if (FileName == DestFile)
  218. Erase = true;
  219. else
  220. Local = true;
  221. string compExt = Desc.URI.substr(Desc.URI.size()-3,Desc.URI.size()-1);
  222. char *decompProg;
  223. if(compExt == "bz2")
  224. decompProg = "bzip2";
  225. else if(compExt == ".gz")
  226. decompProg = "gzip";
  227. else {
  228. _error->Error("Unsupported extension: %s", compExt.c_str());
  229. return;
  230. }
  231. Decompression = true;
  232. DestFile += ".decomp";
  233. Desc.URI = string(decompProg) + ":" + FileName;
  234. QueueURI(Desc);
  235. Mode = decompProg;
  236. }
  237. /*}}}*/
  238. // AcqIndexRel::pkgAcqIndexRel - Constructor /*{{{*/
  239. // ---------------------------------------------------------------------
  240. /* The Release file is added to the queue */
  241. pkgAcqIndexRel::pkgAcqIndexRel(pkgAcquire *Owner,
  242. string URI,string URIDesc,string ShortDesc) :
  243. Item(Owner), RealURI(URI)
  244. {
  245. DestFile = _config->FindDir("Dir::State::lists") + "partial/";
  246. DestFile += URItoFileName(URI);
  247. // Create the item
  248. Desc.URI = URI;
  249. Desc.Description = URIDesc;
  250. Desc.ShortDesc = ShortDesc;
  251. Desc.Owner = this;
  252. QueueURI(Desc);
  253. }
  254. /*}}}*/
  255. // AcqIndexRel::Custom600Headers - Insert custom request headers /*{{{*/
  256. // ---------------------------------------------------------------------
  257. /* The only header we use is the last-modified header. */
  258. string pkgAcqIndexRel::Custom600Headers()
  259. {
  260. string Final = _config->FindDir("Dir::State::lists");
  261. Final += URItoFileName(RealURI);
  262. struct stat Buf;
  263. if (stat(Final.c_str(),&Buf) != 0)
  264. return "\nIndex-File: true";
  265. return "\nIndex-File: true\nLast-Modified: " + TimeRFC1123(Buf.st_mtime);
  266. }
  267. /*}}}*/
  268. // AcqIndexRel::Done - Item downloaded OK /*{{{*/
  269. // ---------------------------------------------------------------------
  270. /* The release file was not placed into the download directory then
  271. a copy URI is generated and it is copied there otherwise the file
  272. in the partial directory is moved into .. and the URI is finished. */
  273. void pkgAcqIndexRel::Done(string Message,unsigned long Size,string MD5,
  274. pkgAcquire::MethodConfig *Cfg)
  275. {
  276. Item::Done(Message,Size,MD5,Cfg);
  277. string FileName = LookupTag(Message,"Filename");
  278. if (FileName.empty() == true)
  279. {
  280. Status = StatError;
  281. ErrorText = "Method gave a blank filename";
  282. return;
  283. }
  284. Complete = true;
  285. // The files timestamp matches
  286. if (StringToBool(LookupTag(Message,"IMS-Hit"),false) == true)
  287. return;
  288. // We have to copy it into place
  289. if (FileName != DestFile)
  290. {
  291. Local = true;
  292. Desc.URI = "copy:" + FileName;
  293. QueueURI(Desc);
  294. return;
  295. }
  296. // Done, move it into position
  297. string FinalFile = _config->FindDir("Dir::State::lists");
  298. FinalFile += URItoFileName(RealURI);
  299. Rename(DestFile,FinalFile);
  300. chmod(FinalFile.c_str(),0644);
  301. }
  302. /*}}}*/
  303. // AcqIndexRel::Failed - Silence failure messages for missing rel files /*{{{*/
  304. // ---------------------------------------------------------------------
  305. /* */
  306. void pkgAcqIndexRel::Failed(string Message,pkgAcquire::MethodConfig *Cnf)
  307. {
  308. if (Cnf->LocalOnly == true ||
  309. StringToBool(LookupTag(Message,"Transient-Failure"),false) == false)
  310. {
  311. // Ignore this
  312. Status = StatDone;
  313. Complete = false;
  314. Dequeue();
  315. return;
  316. }
  317. Item::Failed(Message,Cnf);
  318. }
  319. /*}}}*/
  320. // AcqArchive::AcqArchive - Constructor /*{{{*/
  321. // ---------------------------------------------------------------------
  322. /* This just sets up the initial fetch environment and queues the first
  323. possibilitiy */
  324. pkgAcqArchive::pkgAcqArchive(pkgAcquire *Owner,pkgSourceList *Sources,
  325. pkgRecords *Recs,pkgCache::VerIterator const &Version,
  326. string &StoreFilename) :
  327. Item(Owner), Version(Version), Sources(Sources), Recs(Recs),
  328. StoreFilename(StoreFilename), Vf(Version.FileList())
  329. {
  330. Retries = _config->FindI("Acquire::Retries",0);
  331. if (Version.Arch() == 0)
  332. {
  333. _error->Error(_("I wasn't able to locate a file for the %s package. "
  334. "This might mean you need to manually fix this package. "
  335. "(due to missing arch)"),
  336. Version.ParentPkg().Name());
  337. return;
  338. }
  339. /* We need to find a filename to determine the extension. We make the
  340. assumption here that all the available sources for this version share
  341. the same extension.. */
  342. // Skip not source sources, they do not have file fields.
  343. for (; Vf.end() == false; Vf++)
  344. {
  345. if ((Vf.File()->Flags & pkgCache::Flag::NotSource) != 0)
  346. continue;
  347. break;
  348. }
  349. // Does not really matter here.. we are going to fail out below
  350. if (Vf.end() != true)
  351. {
  352. // If this fails to get a file name we will bomb out below.
  353. pkgRecords::Parser &Parse = Recs->Lookup(Vf);
  354. if (_error->PendingError() == true)
  355. return;
  356. // Generate the final file name as: package_version_arch.foo
  357. StoreFilename = QuoteString(Version.ParentPkg().Name(),"_:") + '_' +
  358. QuoteString(Version.VerStr(),"_:") + '_' +
  359. QuoteString(Version.Arch(),"_:.") +
  360. "." + flExtension(Parse.FileName());
  361. }
  362. // Select a source
  363. if (QueueNext() == false && _error->PendingError() == false)
  364. _error->Error(_("I wasn't able to locate file for the %s package. "
  365. "This might mean you need to manually fix this package."),
  366. Version.ParentPkg().Name());
  367. }
  368. /*}}}*/
  369. // AcqArchive::QueueNext - Queue the next file source /*{{{*/
  370. // ---------------------------------------------------------------------
  371. /* This queues the next available file version for download. It checks if
  372. the archive is already available in the cache and stashs the MD5 for
  373. checking later. */
  374. bool pkgAcqArchive::QueueNext()
  375. {
  376. for (; Vf.end() == false; Vf++)
  377. {
  378. // Ignore not source sources
  379. if ((Vf.File()->Flags & pkgCache::Flag::NotSource) != 0)
  380. continue;
  381. // Try to cross match against the source list
  382. pkgIndexFile *Index;
  383. if (Sources->FindIndex(Vf.File(),Index) == false)
  384. continue;
  385. // Grab the text package record
  386. pkgRecords::Parser &Parse = Recs->Lookup(Vf);
  387. if (_error->PendingError() == true)
  388. return false;
  389. string PkgFile = Parse.FileName();
  390. MD5 = Parse.MD5Hash();
  391. if (PkgFile.empty() == true)
  392. return _error->Error(_("The package index files are corrupted. No Filename: "
  393. "field for package %s."),
  394. Version.ParentPkg().Name());
  395. // See if we already have the file. (Legacy filenames)
  396. FileSize = Version->Size;
  397. string FinalFile = _config->FindDir("Dir::Cache::Archives") + flNotDir(PkgFile);
  398. struct stat Buf;
  399. if (stat(FinalFile.c_str(),&Buf) == 0)
  400. {
  401. // Make sure the size matches
  402. if ((unsigned)Buf.st_size == Version->Size)
  403. {
  404. Complete = true;
  405. Local = true;
  406. Status = StatDone;
  407. StoreFilename = DestFile = FinalFile;
  408. return true;
  409. }
  410. /* Hmm, we have a file and its size does not match, this means it is
  411. an old style mismatched arch */
  412. unlink(FinalFile.c_str());
  413. }
  414. // Check it again using the new style output filenames
  415. FinalFile = _config->FindDir("Dir::Cache::Archives") + flNotDir(StoreFilename);
  416. if (stat(FinalFile.c_str(),&Buf) == 0)
  417. {
  418. // Make sure the size matches
  419. if ((unsigned)Buf.st_size == Version->Size)
  420. {
  421. Complete = true;
  422. Local = true;
  423. Status = StatDone;
  424. StoreFilename = DestFile = FinalFile;
  425. return true;
  426. }
  427. /* Hmm, we have a file and its size does not match, this shouldnt
  428. happen.. */
  429. unlink(FinalFile.c_str());
  430. }
  431. DestFile = _config->FindDir("Dir::Cache::Archives") + "partial/" + flNotDir(StoreFilename);
  432. // Check the destination file
  433. if (stat(DestFile.c_str(),&Buf) == 0)
  434. {
  435. // Hmm, the partial file is too big, erase it
  436. if ((unsigned)Buf.st_size > Version->Size)
  437. unlink(DestFile.c_str());
  438. else
  439. PartialSize = Buf.st_size;
  440. }
  441. // Create the item
  442. Local = false;
  443. Desc.URI = Index->ArchiveURI(PkgFile);
  444. Desc.Description = Index->ArchiveInfo(Version);
  445. Desc.Owner = this;
  446. Desc.ShortDesc = Version.ParentPkg().Name();
  447. QueueURI(Desc);
  448. Vf++;
  449. return true;
  450. }
  451. return false;
  452. }
  453. /*}}}*/
  454. // AcqArchive::Done - Finished fetching /*{{{*/
  455. // ---------------------------------------------------------------------
  456. /* */
  457. void pkgAcqArchive::Done(string Message,unsigned long Size,string Md5Hash,
  458. pkgAcquire::MethodConfig *Cfg)
  459. {
  460. Item::Done(Message,Size,Md5Hash,Cfg);
  461. // Check the size
  462. if (Size != Version->Size)
  463. {
  464. Status = StatError;
  465. ErrorText = _("Size mismatch");
  466. return;
  467. }
  468. // Check the md5
  469. if (Md5Hash.empty() == false && MD5.empty() == false)
  470. {
  471. if (Md5Hash != MD5)
  472. {
  473. Status = StatError;
  474. ErrorText = _("MD5Sum mismatch");
  475. Rename(DestFile,DestFile + ".FAILED");
  476. return;
  477. }
  478. }
  479. // Grab the output filename
  480. string FileName = LookupTag(Message,"Filename");
  481. if (FileName.empty() == true)
  482. {
  483. Status = StatError;
  484. ErrorText = "Method gave a blank filename";
  485. return;
  486. }
  487. Complete = true;
  488. // Reference filename
  489. if (FileName != DestFile)
  490. {
  491. StoreFilename = DestFile = FileName;
  492. Local = true;
  493. return;
  494. }
  495. // Done, move it into position
  496. string FinalFile = _config->FindDir("Dir::Cache::Archives");
  497. FinalFile += flNotDir(StoreFilename);
  498. Rename(DestFile,FinalFile);
  499. StoreFilename = DestFile = FinalFile;
  500. Complete = true;
  501. }
  502. /*}}}*/
  503. // AcqArchive::Failed - Failure handler /*{{{*/
  504. // ---------------------------------------------------------------------
  505. /* Here we try other sources */
  506. void pkgAcqArchive::Failed(string Message,pkgAcquire::MethodConfig *Cnf)
  507. {
  508. ErrorText = LookupTag(Message,"Message");
  509. /* We don't really want to retry on failed media swaps, this prevents
  510. that. An interesting observation is that permanent failures are not
  511. recorded. */
  512. if (Cnf->Removable == true &&
  513. StringToBool(LookupTag(Message,"Transient-Failure"),false) == true)
  514. {
  515. // Vf = Version.FileList();
  516. while (Vf.end() == false) Vf++;
  517. StoreFilename = string();
  518. Item::Failed(Message,Cnf);
  519. return;
  520. }
  521. if (QueueNext() == false)
  522. {
  523. // This is the retry counter
  524. if (Retries != 0 &&
  525. Cnf->LocalOnly == false &&
  526. StringToBool(LookupTag(Message,"Transient-Failure"),false) == true)
  527. {
  528. Retries--;
  529. Vf = Version.FileList();
  530. if (QueueNext() == true)
  531. return;
  532. }
  533. StoreFilename = string();
  534. Item::Failed(Message,Cnf);
  535. }
  536. }
  537. /*}}}*/
  538. // AcqArchive::Finished - Fetching has finished, tidy up /*{{{*/
  539. // ---------------------------------------------------------------------
  540. /* */
  541. void pkgAcqArchive::Finished()
  542. {
  543. if (Status == pkgAcquire::Item::StatDone &&
  544. Complete == true)
  545. return;
  546. StoreFilename = string();
  547. }
  548. /*}}}*/
  549. // AcqFile::pkgAcqFile - Constructor /*{{{*/
  550. // ---------------------------------------------------------------------
  551. /* The file is added to the queue */
  552. pkgAcqFile::pkgAcqFile(pkgAcquire *Owner,string URI,string MD5,
  553. unsigned long Size,string Dsc,string ShortDesc) :
  554. Item(Owner), Md5Hash(MD5)
  555. {
  556. Retries = _config->FindI("Acquire::Retries",0);
  557. DestFile = flNotDir(URI);
  558. // Create the item
  559. Desc.URI = URI;
  560. Desc.Description = Dsc;
  561. Desc.Owner = this;
  562. // Set the short description to the archive component
  563. Desc.ShortDesc = ShortDesc;
  564. // Get the transfer sizes
  565. FileSize = Size;
  566. struct stat Buf;
  567. if (stat(DestFile.c_str(),&Buf) == 0)
  568. {
  569. // Hmm, the partial file is too big, erase it
  570. if ((unsigned)Buf.st_size > Size)
  571. unlink(DestFile.c_str());
  572. else
  573. PartialSize = Buf.st_size;
  574. }
  575. QueueURI(Desc);
  576. }
  577. /*}}}*/
  578. // AcqFile::Done - Item downloaded OK /*{{{*/
  579. // ---------------------------------------------------------------------
  580. /* */
  581. void pkgAcqFile::Done(string Message,unsigned long Size,string MD5,
  582. pkgAcquire::MethodConfig *Cnf)
  583. {
  584. // Check the md5
  585. if (Md5Hash.empty() == false && MD5.empty() == false)
  586. {
  587. if (Md5Hash != MD5)
  588. {
  589. Status = StatError;
  590. ErrorText = "MD5Sum mismatch";
  591. Rename(DestFile,DestFile + ".FAILED");
  592. return;
  593. }
  594. }
  595. Item::Done(Message,Size,MD5,Cnf);
  596. string FileName = LookupTag(Message,"Filename");
  597. if (FileName.empty() == true)
  598. {
  599. Status = StatError;
  600. ErrorText = "Method gave a blank filename";
  601. return;
  602. }
  603. Complete = true;
  604. // The files timestamp matches
  605. if (StringToBool(LookupTag(Message,"IMS-Hit"),false) == true)
  606. return;
  607. // We have to copy it into place
  608. if (FileName != DestFile)
  609. {
  610. Local = true;
  611. if (_config->FindB("Acquire::Source-Symlinks",true) == false ||
  612. Cnf->Removable == true)
  613. {
  614. Desc.URI = "copy:" + FileName;
  615. QueueURI(Desc);
  616. return;
  617. }
  618. // Erase the file if it is a symlink so we can overwrite it
  619. struct stat St;
  620. if (lstat(DestFile.c_str(),&St) == 0)
  621. {
  622. if (S_ISLNK(St.st_mode) != 0)
  623. unlink(DestFile.c_str());
  624. }
  625. // Symlink the file
  626. if (symlink(FileName.c_str(),DestFile.c_str()) != 0)
  627. {
  628. ErrorText = "Link to " + DestFile + " failure ";
  629. Status = StatError;
  630. Complete = false;
  631. }
  632. }
  633. }
  634. /*}}}*/
  635. // AcqFile::Failed - Failure handler /*{{{*/
  636. // ---------------------------------------------------------------------
  637. /* Here we try other sources */
  638. void pkgAcqFile::Failed(string Message,pkgAcquire::MethodConfig *Cnf)
  639. {
  640. ErrorText = LookupTag(Message,"Message");
  641. // This is the retry counter
  642. if (Retries != 0 &&
  643. Cnf->LocalOnly == false &&
  644. StringToBool(LookupTag(Message,"Transient-Failure"),false) == true)
  645. {
  646. Retries--;
  647. QueueURI(Desc);
  648. return;
  649. }
  650. Item::Failed(Message,Cnf);
  651. }
  652. /*}}}*/