acquire-item.cc 7.2 KB

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