acquire-item.cc 17 KB

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