acquire-item.cc 15 KB

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