acquire-item.cc 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. // -*- mode: cpp; mode: fold -*-
  2. // Description /*{{{*/
  3. // $Id: acquire-item.cc,v 1.7 1998/11/05 07:21:35 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 <strutl.h>
  19. #include <sys/stat.h>
  20. #include <unistd.h>
  21. #include <errno.h>
  22. #include <string.h>
  23. #include <stdio.h>
  24. /*}}}*/
  25. // Acquire::Item::Item - Constructor /*{{{*/
  26. // ---------------------------------------------------------------------
  27. /* */
  28. pkgAcquire::Item::Item(pkgAcquire *Owner) : Owner(Owner), QueueCounter(0)
  29. {
  30. Owner->Add(this);
  31. Status = StatIdle;
  32. }
  33. /*}}}*/
  34. // Acquire::Item::~Item - Destructor /*{{{*/
  35. // ---------------------------------------------------------------------
  36. /* */
  37. pkgAcquire::Item::~Item()
  38. {
  39. Owner->Remove(this);
  40. }
  41. /*}}}*/
  42. // Acquire::Item::Failed - Item failed to download /*{{{*/
  43. // ---------------------------------------------------------------------
  44. /* We return to an idle state if there are still other queues that could
  45. fetch this object */
  46. void pkgAcquire::Item::Failed(string Message)
  47. {
  48. Status = StatIdle;
  49. if (QueueCounter <= 1)
  50. {
  51. ErrorText = LookupTag(Message,"Message");
  52. Status = StatError;
  53. Owner->Dequeue(this);
  54. }
  55. }
  56. /*}}}*/
  57. // Acquire::Item::Done - Item downloaded OK /*{{{*/
  58. // ---------------------------------------------------------------------
  59. /* */
  60. void pkgAcquire::Item::Done(string,unsigned long,string)
  61. {
  62. Status = StatDone;
  63. ErrorText = string();
  64. Owner->Dequeue(this);
  65. }
  66. /*}}}*/
  67. // Acquire::Item::Rename - Rename a file /*{{{*/
  68. // ---------------------------------------------------------------------
  69. /* This helper function is used by alot of item methods as thier final
  70. step */
  71. void pkgAcquire::Item::Rename(string From,string To)
  72. {
  73. if (rename(From.c_str(),To.c_str()) != 0)
  74. {
  75. char S[300];
  76. sprintf(S,"rename failed, %s (%s -> %s).",strerror(errno),
  77. From.c_str(),To.c_str());
  78. Status = StatError;
  79. ErrorText = S;
  80. }
  81. }
  82. /*}}}*/
  83. // AcqIndex::AcqIndex - Constructor /*{{{*/
  84. // ---------------------------------------------------------------------
  85. /* The package file is added to the queue and a second class is
  86. instantiated to fetch the revision file */
  87. pkgAcqIndex::pkgAcqIndex(pkgAcquire *Owner,const pkgSourceList::Item *Location) :
  88. Item(Owner), Location(Location)
  89. {
  90. Decompression = false;
  91. Erase = false;
  92. DestFile = _config->FindDir("Dir::State::lists") + "partial/";
  93. DestFile += URItoFileName(Location->PackagesURI());
  94. QueueURI(Location->PackagesURI() + ".gz",Location->PackagesInfo());
  95. // Create the Release fetch class
  96. new pkgAcqIndexRel(Owner,Location);
  97. }
  98. /*}}}*/
  99. // AcqIndex::Custom600Headers - Insert custom request headers /*{{{*/
  100. // ---------------------------------------------------------------------
  101. /* The only header we use is the last-modified header. */
  102. string pkgAcqIndex::Custom600Headers()
  103. {
  104. string Final = _config->FindDir("Dir::State::lists");
  105. Final += URItoFileName(Location->PackagesURI());
  106. struct stat Buf;
  107. if (stat(Final.c_str(),&Buf) != 0)
  108. return string();
  109. return "\nLast-Modified: " + TimeRFC1123(Buf.st_mtime);
  110. }
  111. /*}}}*/
  112. // AcqIndex::Done - Finished a fetch /*{{{*/
  113. // ---------------------------------------------------------------------
  114. /* This goes through a number of states.. On the initial fetch the
  115. method could possibly return an alternate filename which points
  116. to the uncompressed version of the file. If this is so the file
  117. is copied into the partial directory. In all other cases the file
  118. is decompressed with a gzip uri. */
  119. void pkgAcqIndex::Done(string Message,unsigned long Size,string MD5)
  120. {
  121. Item::Done(Message,Size,MD5);
  122. if (Decompression == true)
  123. {
  124. // Done, move it into position
  125. string FinalFile = _config->FindDir("Dir::State::lists");
  126. FinalFile += URItoFileName(Location->PackagesURI());
  127. Rename(DestFile,FinalFile);
  128. // Remove the compressed version.
  129. if (Erase == true)
  130. {
  131. DestFile = _config->FindDir("Dir::State::lists") + "partial/";
  132. DestFile += URItoFileName(Location->PackagesURI());
  133. unlink(DestFile.c_str());
  134. }
  135. return;
  136. }
  137. Erase = false;
  138. // Handle the unzipd case
  139. string FileName = LookupTag(Message,"Alt-Filename");
  140. if (FileName.empty() == false)
  141. {
  142. // The files timestamp matches
  143. if (StringToBool(LookupTag(Message,"Alt-IMS-Hit"),false) == true)
  144. return;
  145. Decompression = true;
  146. DestFile += ".decomp";
  147. QueueURI("copy:" + FileName,string());
  148. return;
  149. }
  150. FileName = LookupTag(Message,"Filename");
  151. if (FileName.empty() == true)
  152. {
  153. Status = StatError;
  154. ErrorText = "Method gave a blank filename";
  155. }
  156. // The files timestamp matches
  157. if (StringToBool(LookupTag(Message,"IMS-Hit"),false) == true)
  158. return;
  159. if (FileName == DestFile)
  160. Erase = true;
  161. Decompression = true;
  162. DestFile += ".decomp";
  163. QueueURI("gzip:" + FileName,string());
  164. }
  165. /*}}}*/
  166. // AcqIndexRel::pkgAcqIndexRel - Constructor /*{{{*/
  167. // ---------------------------------------------------------------------
  168. /* The Release file is added to the queue */
  169. pkgAcqIndexRel::pkgAcqIndexRel(pkgAcquire *Owner,
  170. const pkgSourceList::Item *Location) :
  171. Item(Owner), Location(Location)
  172. {
  173. DestFile = _config->FindDir("Dir::State::lists") + "partial/";
  174. DestFile += URItoFileName(Location->ReleaseURI());
  175. QueueURI(Location->ReleaseURI(),Location->ReleaseInfo());
  176. }
  177. /*}}}*/
  178. // AcqIndexRel::Custom600Headers - Insert custom request headers /*{{{*/
  179. // ---------------------------------------------------------------------
  180. /* The only header we use is the last-modified header. */
  181. string pkgAcqIndexRel::Custom600Headers()
  182. {
  183. string Final = _config->FindDir("Dir::State::lists");
  184. Final += URItoFileName(Location->ReleaseURI());
  185. struct stat Buf;
  186. if (stat(Final.c_str(),&Buf) != 0)
  187. return string();
  188. return "\nLast-Modified: " + TimeRFC1123(Buf.st_mtime);
  189. }
  190. /*}}}*/
  191. // AcqIndexRel::Done - Item downloaded OK /*{{{*/
  192. // ---------------------------------------------------------------------
  193. /* The release file was not placed into the download directory then
  194. a copy URI is generated and it is copied there otherwise the file
  195. in the partial directory is moved into .. and the URI is finished. */
  196. void pkgAcqIndexRel::Done(string Message,unsigned long Size,string MD5)
  197. {
  198. Item::Done(Message,Size,MD5);
  199. string FileName = LookupTag(Message,"Filename");
  200. if (FileName.empty() == true)
  201. {
  202. Status = StatError;
  203. ErrorText = "Method gave a blank filename";
  204. return;
  205. }
  206. // The files timestamp matches
  207. if (StringToBool(LookupTag(Message,"IMS-Hit"),false) == true)
  208. return;
  209. // We have to copy it into place
  210. if (FileName != DestFile)
  211. {
  212. QueueURI("copy:" + FileName,string());
  213. return;
  214. }
  215. // Done, move it into position
  216. string FinalFile = _config->FindDir("Dir::State::lists");
  217. FinalFile += URItoFileName(Location->ReleaseURI());
  218. Rename(DestFile,FinalFile);
  219. }
  220. /*}}}*/