acquire-item.cc 9.2 KB

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