acquire-method.cc 9.3 KB

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