acquire-item.cc 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. // -*- mode: cpp; mode: fold -*-
  2. // Description /*{{{*/
  3. // $Id: acquire-item.cc,v 1.6 1998/10/30 07:53:34 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. DestFile = _config->FindDir("Dir::State::lists") + "partial/";
  92. DestFile += URItoFileName(Location->PackagesURI());
  93. QueueURI(Location->PackagesURI() + ".gz",Location->PackagesInfo());
  94. // Create the Release fetch class
  95. new pkgAcqIndexRel(Owner,Location);
  96. }
  97. /*}}}*/
  98. // AcqIndex::Custom600Headers - Insert custom request headers /*{{{*/
  99. // ---------------------------------------------------------------------
  100. /* The only header we use is the last-modified header. */
  101. string pkgAcqIndex::Custom600Headers()
  102. {
  103. string Final = _config->FindDir("Dir::State::lists");
  104. Final += URItoFileName(Location->PackagesURI());
  105. struct stat Buf;
  106. if (stat(Final.c_str(),&Buf) != 0)
  107. return string();
  108. return "\nLast-Modified: " + TimeRFC1123(Buf.st_mtime);
  109. }
  110. /*}}}*/
  111. // AcqIndex::Done - Finished a fetch /*{{{*/
  112. // ---------------------------------------------------------------------
  113. /* This goes through a number of states.. On the initial fetch the
  114. method could possibly return an alternate filename which points
  115. to the uncompressed version of the file. If this is so the file
  116. is copied into the partial directory. In all other cases the file
  117. is decompressed with a gzip uri. */
  118. void pkgAcqIndex::Done(string Message,unsigned long Size,string MD5)
  119. {
  120. Item::Done(Message,Size,MD5);
  121. if (Decompression == true)
  122. {
  123. // Done, move it into position
  124. string FinalFile = _config->FindDir("Dir::State::lists");
  125. FinalFile += URItoFileName(Location->PackagesURI());
  126. Rename(DestFile,FinalFile);
  127. return;
  128. }
  129. // Handle the unzipd case
  130. string FileName = LookupTag(Message,"Alt-Filename");
  131. if (FileName.empty() == false)
  132. {
  133. // The files timestamp matches
  134. if (StringToBool(LookupTag(Message,"Alt-IMS-Hit"),false) == true)
  135. return;
  136. Decompression = true;
  137. DestFile += ".decomp";
  138. QueueURI("copy:" + FileName,string());
  139. return;
  140. }
  141. FileName = LookupTag(Message,"Filename");
  142. if (FileName.empty() == true)
  143. {
  144. Status = StatError;
  145. ErrorText = "Method gave a blank filename";
  146. }
  147. // The files timestamp matches
  148. if (StringToBool(LookupTag(Message,"IMS-Hit"),false) == true)
  149. return;
  150. Decompression = true;
  151. DestFile += ".decomp";
  152. QueueURI("gzip:" + FileName,string());
  153. }
  154. /*}}}*/
  155. // AcqIndexRel::pkgAcqIndexRel - Constructor /*{{{*/
  156. // ---------------------------------------------------------------------
  157. /* The Release file is added to the queue */
  158. pkgAcqIndexRel::pkgAcqIndexRel(pkgAcquire *Owner,
  159. const pkgSourceList::Item *Location) :
  160. Item(Owner), Location(Location)
  161. {
  162. DestFile = _config->FindDir("Dir::State::lists") + "partial/";
  163. DestFile += URItoFileName(Location->ReleaseURI());
  164. QueueURI(Location->ReleaseURI(),Location->ReleaseInfo());
  165. }
  166. /*}}}*/
  167. // AcqIndexRel::Custom600Headers - Insert custom request headers /*{{{*/
  168. // ---------------------------------------------------------------------
  169. /* The only header we use is the last-modified header. */
  170. string pkgAcqIndexRel::Custom600Headers()
  171. {
  172. string Final = _config->FindDir("Dir::State::lists");
  173. Final += URItoFileName(Location->ReleaseURI());
  174. struct stat Buf;
  175. if (stat(Final.c_str(),&Buf) != 0)
  176. return string();
  177. return "\nLast-Modified: " + TimeRFC1123(Buf.st_mtime);
  178. }
  179. /*}}}*/
  180. // AcqIndexRel::Done - Item downloaded OK /*{{{*/
  181. // ---------------------------------------------------------------------
  182. /* The release file was not placed into the download directory then
  183. a copy URI is generated and it is copied there otherwise the file
  184. in the partial directory is moved into .. and the URI is finished. */
  185. void pkgAcqIndexRel::Done(string Message,unsigned long Size,string MD5)
  186. {
  187. Item::Done(Message,Size,MD5);
  188. string FileName = LookupTag(Message,"Filename");
  189. if (FileName.empty() == true)
  190. {
  191. Status = StatError;
  192. ErrorText = "Method gave a blank filename";
  193. return;
  194. }
  195. // The files timestamp matches
  196. if (StringToBool(LookupTag(Message,"IMS-Hit"),false) == true)
  197. return;
  198. // We have to copy it into place
  199. if (FileName != DestFile)
  200. {
  201. QueueURI("copy:" + FileName,string());
  202. return;
  203. }
  204. // Done, move it into position
  205. string FinalFile = _config->FindDir("Dir::State::lists");
  206. FinalFile += URItoFileName(Location->ReleaseURI());
  207. Rename(DestFile,FinalFile);
  208. }
  209. /*}}}*/