acquire-item.cc 14 KB

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