acquire-item.cc 18 KB

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