acquire-method.cc 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. // -*- mode: cpp; mode: fold -*-
  2. // Description /*{{{*/
  3. // $Id: acquire-method.cc,v 1.4 1998/11/05 07:21:38 jgg Exp $
  4. /* ######################################################################
  5. Acquire Method
  6. ##################################################################### */
  7. /*}}}*/
  8. // Include Files /*{{{*/
  9. #ifdef __GNUG__
  10. #pragma implementation "apt-pkg/acquire-method.h"
  11. #endif
  12. #include <apt-pkg/acquire-method.h>
  13. #include <apt-pkg/error.h>
  14. #include <apt-pkg/configuration.h>
  15. #include <strutl.h>
  16. #include <apt-pkg/fileutl.h>
  17. #include <stdio.h>
  18. /*}}}*/
  19. // AcqMethod::pkgAcqMethod - Constructor /*{{{*/
  20. // ---------------------------------------------------------------------
  21. /* This constructs the initialization text */
  22. pkgAcqMethod::pkgAcqMethod(const char *Ver,unsigned long Flags)
  23. {
  24. char S[300] = "";
  25. char *End = S;
  26. strcat(End,"100 Capabilities\n");
  27. sprintf(End+strlen(End),"Version: %s\n",Ver);
  28. if ((Flags & SingleInstance) == SingleInstance)
  29. strcat(End,"Single-Instance: true\n");
  30. if ((Flags & PreScan) == PreScan)
  31. strcat(End,"Pre-Scan: true\n");
  32. if ((Flags & Pipeline) == Pipeline)
  33. strcat(End,"Pipeline: true\n");
  34. if ((Flags & SendConfig) == SendConfig)
  35. strcat(End,"Send-Config: true\n");
  36. strcat(End,"\n");
  37. if (write(STDOUT_FILENO,S,strlen(S)) != (signed)strlen(S))
  38. exit(100);
  39. SetNonBlock(STDIN_FILENO,true);
  40. Queue = 0;
  41. }
  42. /*}}}*/
  43. // AcqMethod::Fail - A fetch has failed /*{{{*/
  44. // ---------------------------------------------------------------------
  45. /* */
  46. void pkgAcqMethod::Fail()
  47. {
  48. string Err = "Undetermined Error";
  49. if (_error->empty() == false)
  50. _error->PopMessage(Err);
  51. _error->Discard();
  52. Fail(Err);
  53. }
  54. /*}}}*/
  55. // AcqMethod::Fail - A fetch has failed /*{{{*/
  56. // ---------------------------------------------------------------------
  57. /* */
  58. void pkgAcqMethod::Fail(string Err)
  59. {
  60. char S[1024];
  61. if (Queue != 0)
  62. {
  63. snprintf(S,sizeof(S),"400 URI Failure\nURI: %s\n"
  64. "Message: %s\n\n",Queue->Uri.c_str(),Err.c_str());
  65. // Dequeue
  66. FetchItem *Tmp = Queue;
  67. Queue = Queue->Next;
  68. delete Tmp;
  69. }
  70. else
  71. snprintf(S,sizeof(S),"400 URI Failure\nURI: <UNKNOWN>\n"
  72. "Message: %s\n\n",Err.c_str());
  73. if (write(STDOUT_FILENO,S,strlen(S)) != (signed)strlen(S))
  74. exit(100);
  75. }
  76. /*}}}*/
  77. // AcqMethod::URIStart - Indicate a download is starting /*{{{*/
  78. // ---------------------------------------------------------------------
  79. /* */
  80. void pkgAcqMethod::URIStart(FetchResult &Res)
  81. {
  82. if (Queue == 0)
  83. abort();
  84. char S[1024] = "";
  85. char *End = S;
  86. End += snprintf(S,sizeof(S),"200 URI Start\nURI: %s\n",Queue->Uri.c_str());
  87. if (Res.Size != 0)
  88. End += snprintf(End,sizeof(S) - (End - S),"Size: %u\n",Res.Size);
  89. if (Res.LastModified != 0)
  90. End += snprintf(End,sizeof(S) - (End - S),"Last-Modified: %s\n",
  91. TimeRFC1123(Res.LastModified).c_str());
  92. if (Res.ResumePoint != 0)
  93. End += snprintf(End,sizeof(S) - (End - S),"Resume-Point: %u\n",
  94. Res.ResumePoint);
  95. strcat(End,"\n");
  96. if (write(STDOUT_FILENO,S,strlen(S)) != (signed)strlen(S))
  97. exit(100);
  98. }
  99. /*}}}*/
  100. // AcqMethod::URIDone - A URI is finished /*{{{*/
  101. // ---------------------------------------------------------------------
  102. /* */
  103. void pkgAcqMethod::URIDone(FetchResult &Res, FetchResult *Alt)
  104. {
  105. if (Queue == 0)
  106. abort();
  107. char S[1024] = "";
  108. char *End = S;
  109. End += snprintf(S,sizeof(S),"201 URI Done\nURI: %s\n",Queue->Uri.c_str());
  110. if (Res.Filename.empty() == false)
  111. End += snprintf(End,sizeof(S) - (End - S),"Filename: %s\n",Res.Filename.c_str());
  112. if (Res.Size != 0)
  113. End += snprintf(End,sizeof(S) - (End - S),"Size: %u\n",Res.Size);
  114. if (Res.LastModified != 0)
  115. End += snprintf(End,sizeof(S) - (End - S),"Last-Modified: %s\n",
  116. TimeRFC1123(Res.LastModified).c_str());
  117. if (Res.MD5Sum.empty() == false)
  118. End += snprintf(End,sizeof(S) - (End - S),"MD5Sum: %s\n",Res.MD5Sum.c_str());
  119. if (Res.IMSHit == true)
  120. strcat(End,"IMS-Hit: true\n");
  121. End = S + strlen(S);
  122. if (Alt != 0)
  123. {
  124. if (Alt->Filename.empty() == false)
  125. End += snprintf(End,sizeof(S) - (End - S),"Alt-Filename: %s\n",Alt->Filename.c_str());
  126. if (Alt->Size != 0)
  127. End += snprintf(End,sizeof(S) - (End - S),"Alt-Size: %u\n",Alt->Size);
  128. if (Alt->LastModified != 0)
  129. End += snprintf(End,sizeof(S) - (End - S),"Alt-Last-Modified: %s\n",
  130. TimeRFC1123(Alt->LastModified).c_str());
  131. if (Alt->MD5Sum.empty() == false)
  132. End += snprintf(End,sizeof(S) - (End - S),"Alt-MD5Sum: %s\n",
  133. Alt->MD5Sum.c_str());
  134. if (Alt->IMSHit == true)
  135. strcat(End,"Alt-IMS-Hit: true\n");
  136. }
  137. strcat(End,"\n");
  138. if (write(STDOUT_FILENO,S,strlen(S)) != (signed)strlen(S))
  139. exit(100);
  140. // Dequeue
  141. FetchItem *Tmp = Queue;
  142. Queue = Queue->Next;
  143. delete Tmp;
  144. }
  145. /*}}}*/
  146. // AcqMethod::Configuration - Handle the configuration message /*{{{*/
  147. // ---------------------------------------------------------------------
  148. /* This parses each configuration entry and puts it into the _config
  149. Configuration class. */
  150. bool pkgAcqMethod::Configuration(string Message)
  151. {
  152. ::Configuration &Cnf = *_config;
  153. const char *I = Message.begin();
  154. unsigned int Length = strlen("Config-Item");
  155. for (; I + Length < Message.end(); I++)
  156. {
  157. // Not a config item
  158. if (I[Length] != ':' || stringcasecmp(I,I+Length,"Config-Item") != 0)
  159. continue;
  160. I += Length + 1;
  161. for (; I < Message.end() && *I == ' '; I++);
  162. const char *Equals = I;
  163. for (; Equals < Message.end() && *Equals != '='; Equals++);
  164. const char *End = Equals;
  165. for (; End < Message.end() && *End != '\n'; End++);
  166. if (End == Equals)
  167. return false;
  168. Cnf.Set(string(I,Equals-I),string(Equals+1,End-Equals-1));
  169. I = End;
  170. }
  171. return true;
  172. }
  173. /*}}}*/
  174. // AcqMethod::Run - Run the message engine /*{{{*/
  175. // ---------------------------------------------------------------------
  176. /* */
  177. int pkgAcqMethod::Run(bool Single)
  178. {
  179. while (1)
  180. {
  181. // Block if the message queue is empty
  182. if (Messages.empty() == true)
  183. {
  184. if (Single == false)
  185. if (WaitFd(STDIN_FILENO) == false)
  186. return 0;
  187. if (ReadMessages(STDIN_FILENO,Messages) == false)
  188. return 0;
  189. }
  190. // Single mode exits if the message queue is empty
  191. if (Single == true && Messages.empty() == true)
  192. return 0;
  193. string Message = Messages.front();
  194. Messages.erase(Messages.begin());
  195. // Fetch the message number
  196. char *End;
  197. int Number = strtol(Message.c_str(),&End,10);
  198. if (End == Message.c_str())
  199. {
  200. cerr << "Malformed message!" << endl;
  201. return 100;
  202. }
  203. switch (Number)
  204. {
  205. case 601:
  206. if (Configuration(Message) == false)
  207. return 100;
  208. break;
  209. case 600:
  210. {
  211. FetchItem *Tmp = new FetchItem;
  212. Tmp->Uri = LookupTag(Message,"URI");
  213. Tmp->DestFile = LookupTag(Message,"FileName");
  214. StrToTime(LookupTag(Message,"Last-Modified"),Tmp->LastModified);
  215. Tmp->Next = 0;
  216. // Append it to the list
  217. FetchItem **I = &Queue;
  218. for (; *I != 0; I = &(*I)->Next);
  219. *I = Tmp;
  220. // Notify that this item is to be fetched.
  221. if (Fetch(Tmp) == false)
  222. Fail();
  223. break;
  224. }
  225. }
  226. }
  227. return 0;
  228. }
  229. /*}}}*/
  230. // AcqMethod::Log - Send a log message /*{{{*/
  231. // ---------------------------------------------------------------------
  232. /* */
  233. void pkgAcqMethod::Log(const char *Format,...)
  234. {
  235. string CurrentURI = "<UNKNOWN>";
  236. if (Queue != 0)
  237. CurrentURI = Queue->Uri;
  238. va_list args;
  239. va_start(args,Format);
  240. // sprintf the description
  241. char S[1024];
  242. unsigned int Len = snprintf(S,sizeof(S),"101 Log\nURI: %s\n"
  243. "Message: ",CurrentURI.c_str());
  244. vsnprintf(S+Len,sizeof(S)-Len,Format,args);
  245. strcat(S,"\n\n");
  246. if (write(STDOUT_FILENO,S,strlen(S)) != (signed)strlen(S))
  247. exit(100);
  248. }
  249. /*}}}*/
  250. // AcqMethod::Status - Send a status message /*{{{*/
  251. // ---------------------------------------------------------------------
  252. /* */
  253. void pkgAcqMethod::Status(const char *Format,...)
  254. {
  255. string CurrentURI = "<UNKNOWN>";
  256. if (Queue != 0)
  257. CurrentURI = Queue->Uri;
  258. va_list args;
  259. va_start(args,Format);
  260. // sprintf the description
  261. char S[1024];
  262. unsigned int Len = snprintf(S,sizeof(S),"101 Status\nURI: %s\n"
  263. "Message: ",CurrentURI.c_str());
  264. vsnprintf(S+Len,sizeof(S)-Len,Format,args);
  265. strcat(S,"\n\n");
  266. if (write(STDOUT_FILENO,S,strlen(S)) != (signed)strlen(S))
  267. exit(100);
  268. }
  269. /*}}}*/
  270. // AcqMethod::FetchResult::FetchResult - Constructor /*{{{*/
  271. // ---------------------------------------------------------------------
  272. /* */
  273. pkgAcqMethod::FetchResult::FetchResult() : LastModified(0),
  274. IMSHit(false), Size(0)
  275. {
  276. }
  277. /*}}}*/