acquire-item.cc 21 KB

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