acquire-item.cc 14 KB

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