acquire-method.cc 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  1. // -*- mode: cpp; mode: fold -*-
  2. // Description /*{{{*/
  3. // $Id: acquire-method.cc,v 1.24 2000/01/17 07:11:49 jgg Exp $
  4. /* ######################################################################
  5. Acquire Method
  6. This is a skeleton class that implements most of the functionality
  7. of a method and some usefull functions to make method implementation
  8. simpler. The methods all derive this and specialize it. The most
  9. complex implementation is the http method which needs to provide
  10. pipelining, it runs the message engine at the same time it is
  11. downloading files..
  12. ##################################################################### */
  13. /*}}}*/
  14. // Include Files /*{{{*/
  15. #ifdef __GNUG__
  16. #pragma implementation "apt-pkg/acquire-method.h"
  17. #endif
  18. #include <apt-pkg/acquire-method.h>
  19. #include <apt-pkg/error.h>
  20. #include <apt-pkg/configuration.h>
  21. #include <apt-pkg/strutl.h>
  22. #include <apt-pkg/fileutl.h>
  23. #include <stdarg.h>
  24. #include <stdio.h>
  25. #include <unistd.h>
  26. /*}}}*/
  27. // AcqMethod::pkgAcqMethod - Constructor /*{{{*/
  28. // ---------------------------------------------------------------------
  29. /* This constructs the initialization text */
  30. pkgAcqMethod::pkgAcqMethod(const char *Ver,unsigned long Flags)
  31. {
  32. char S[300] = "";
  33. char *End = S;
  34. strcat(End,"100 Capabilities\n");
  35. sprintf(End+strlen(End),"Version: %s\n",Ver);
  36. if ((Flags & SingleInstance) == SingleInstance)
  37. strcat(End,"Single-Instance: true\n");
  38. if ((Flags & Pipeline) == Pipeline)
  39. strcat(End,"Pipeline: true\n");
  40. if ((Flags & SendConfig) == SendConfig)
  41. strcat(End,"Send-Config: true\n");
  42. if ((Flags & LocalOnly) == LocalOnly)
  43. strcat(End,"Local-Only: true\n");
  44. if ((Flags & NeedsCleanup) == NeedsCleanup)
  45. strcat(End,"Needs-Cleanup: true\n");
  46. if ((Flags & Removable) == Removable)
  47. strcat(End,"Removable: true\n");
  48. strcat(End,"\n");
  49. if (write(STDOUT_FILENO,S,strlen(S)) != (signed)strlen(S))
  50. exit(100);
  51. SetNonBlock(STDIN_FILENO,true);
  52. Queue = 0;
  53. QueueBack = 0;
  54. }
  55. /*}}}*/
  56. // AcqMethod::Fail - A fetch has failed /*{{{*/
  57. // ---------------------------------------------------------------------
  58. /* */
  59. void pkgAcqMethod::Fail(bool Transient)
  60. {
  61. string Err = "Undetermined Error";
  62. if (_error->empty() == false)
  63. _error->PopMessage(Err);
  64. _error->Discard();
  65. Fail(Err,Transient);
  66. }
  67. /*}}}*/
  68. // AcqMethod::Fail - A fetch has failed /*{{{*/
  69. // ---------------------------------------------------------------------
  70. /* */
  71. void pkgAcqMethod::Fail(string Err,bool Transient)
  72. {
  73. // Strip out junk from the error messages
  74. for (char *I = Err.begin(); I != Err.end(); I++)
  75. {
  76. if (*I == '\r')
  77. *I = ' ';
  78. if (*I == '\n')
  79. *I = ' ';
  80. }
  81. char S[1024];
  82. if (Queue != 0)
  83. {
  84. snprintf(S,sizeof(S)-50,"400 URI Failure\nURI: %s\n"
  85. "Message: %s\n",Queue->Uri.c_str(),Err.c_str());
  86. // Dequeue
  87. FetchItem *Tmp = Queue;
  88. Queue = Queue->Next;
  89. delete Tmp;
  90. if (Tmp == QueueBack)
  91. QueueBack = Queue;
  92. }
  93. else
  94. snprintf(S,sizeof(S)-50,"400 URI Failure\nURI: <UNKNOWN>\n"
  95. "Message: %s\n",Err.c_str());
  96. // Set the transient flag
  97. if (Transient == true)
  98. strcat(S,"Transient-Failure: true\n\n");
  99. else
  100. strcat(S,"\n");
  101. if (write(STDOUT_FILENO,S,strlen(S)) != (signed)strlen(S))
  102. exit(100);
  103. }
  104. /*}}}*/
  105. // AcqMethod::URIStart - Indicate a download is starting /*{{{*/
  106. // ---------------------------------------------------------------------
  107. /* */
  108. void pkgAcqMethod::URIStart(FetchResult &Res)
  109. {
  110. if (Queue == 0)
  111. abort();
  112. char S[1024] = "";
  113. char *End = S;
  114. End += snprintf(S,sizeof(S),"200 URI Start\nURI: %s\n",Queue->Uri.c_str());
  115. if (Res.Size != 0)
  116. End += snprintf(End,sizeof(S)-4 - (End - S),"Size: %lu\n",Res.Size);
  117. if (Res.LastModified != 0)
  118. End += snprintf(End,sizeof(S)-4 - (End - S),"Last-Modified: %s\n",
  119. TimeRFC1123(Res.LastModified).c_str());
  120. if (Res.ResumePoint != 0)
  121. End += snprintf(End,sizeof(S)-4 - (End - S),"Resume-Point: %lu\n",
  122. Res.ResumePoint);
  123. strcat(End,"\n");
  124. if (write(STDOUT_FILENO,S,strlen(S)) != (signed)strlen(S))
  125. exit(100);
  126. }
  127. /*}}}*/
  128. // AcqMethod::URIDone - A URI is finished /*{{{*/
  129. // ---------------------------------------------------------------------
  130. /* */
  131. void pkgAcqMethod::URIDone(FetchResult &Res, FetchResult *Alt)
  132. {
  133. if (Queue == 0)
  134. abort();
  135. char S[1024] = "";
  136. char *End = S;
  137. End += snprintf(S,sizeof(S),"201 URI Done\nURI: %s\n",Queue->Uri.c_str());
  138. if (Res.Filename.empty() == false)
  139. End += snprintf(End,sizeof(S)-50 - (End - S),"Filename: %s\n",Res.Filename.c_str());
  140. if (Res.Size != 0)
  141. End += snprintf(End,sizeof(S)-50 - (End - S),"Size: %lu\n",Res.Size);
  142. if (Res.LastModified != 0)
  143. End += snprintf(End,sizeof(S)-50 - (End - S),"Last-Modified: %s\n",
  144. TimeRFC1123(Res.LastModified).c_str());
  145. if (Res.MD5Sum.empty() == false)
  146. End += snprintf(End,sizeof(S)-50 - (End - S),"MD5-Hash: %s\n",Res.MD5Sum.c_str());
  147. if (Res.ResumePoint != 0)
  148. End += snprintf(End,sizeof(S)-50 - (End - S),"Resume-Point: %lu\n",
  149. Res.ResumePoint);
  150. if (Res.IMSHit == true)
  151. strcat(End,"IMS-Hit: true\n");
  152. End = S + strlen(S);
  153. if (Alt != 0)
  154. {
  155. if (Alt->Filename.empty() == false)
  156. End += snprintf(End,sizeof(S)-50 - (End - S),"Alt-Filename: %s\n",Alt->Filename.c_str());
  157. if (Alt->Size != 0)
  158. End += snprintf(End,sizeof(S)-50 - (End - S),"Alt-Size: %lu\n",Alt->Size);
  159. if (Alt->LastModified != 0)
  160. End += snprintf(End,sizeof(S)-50 - (End - S),"Alt-Last-Modified: %s\n",
  161. TimeRFC1123(Alt->LastModified).c_str());
  162. if (Alt->MD5Sum.empty() == false)
  163. End += snprintf(End,sizeof(S)-50 - (End - S),"Alt-MD5-Hash: %s\n",
  164. Alt->MD5Sum.c_str());
  165. if (Alt->IMSHit == true)
  166. strcat(End,"Alt-IMS-Hit: true\n");
  167. }
  168. strcat(End,"\n");
  169. if (write(STDOUT_FILENO,S,strlen(S)) != (signed)strlen(S))
  170. exit(100);
  171. // Dequeue
  172. FetchItem *Tmp = Queue;
  173. Queue = Queue->Next;
  174. delete Tmp;
  175. if (Tmp == QueueBack)
  176. QueueBack = Queue;
  177. }
  178. /*}}}*/
  179. // AcqMethod::MediaFail - Syncronous request for new media /*{{{*/
  180. // ---------------------------------------------------------------------
  181. /* This sends a 403 Media Failure message to the APT and waits for it
  182. to be ackd */
  183. bool pkgAcqMethod::MediaFail(string Required,string Drive)
  184. {
  185. char S[1024];
  186. snprintf(S,sizeof(S),"403 Media Failure\nMedia: %s\nDrive: %s\n\n",
  187. Required.c_str(),Drive.c_str());
  188. if (write(STDOUT_FILENO,S,strlen(S)) != (signed)strlen(S))
  189. exit(100);
  190. vector<string> MyMessages;
  191. /* Here we read messages until we find a 603, each non 603 message is
  192. appended to the main message list for later processing */
  193. while (1)
  194. {
  195. if (WaitFd(STDIN_FILENO) == false)
  196. return false;
  197. if (ReadMessages(STDIN_FILENO,MyMessages) == false)
  198. return false;
  199. string Message = MyMessages.front();
  200. MyMessages.erase(MyMessages.begin());
  201. // Fetch the message number
  202. char *End;
  203. int Number = strtol(Message.c_str(),&End,10);
  204. if (End == Message.c_str())
  205. {
  206. cerr << "Malformed message!" << endl;
  207. exit(100);
  208. }
  209. // Change ack
  210. if (Number == 603)
  211. {
  212. while (MyMessages.empty() == false)
  213. {
  214. Messages.push_back(MyMessages.front());
  215. MyMessages.erase(MyMessages.begin());
  216. }
  217. return !StringToBool(LookupTag(Message,"Fail"),false);
  218. }
  219. Messages.push_back(Message);
  220. }
  221. }
  222. /*}}}*/
  223. // AcqMethod::Configuration - Handle the configuration message /*{{{*/
  224. // ---------------------------------------------------------------------
  225. /* This parses each configuration entry and puts it into the _config
  226. Configuration class. */
  227. bool pkgAcqMethod::Configuration(string Message)
  228. {
  229. ::Configuration &Cnf = *_config;
  230. const char *I = Message.begin();
  231. unsigned int Length = strlen("Config-Item");
  232. for (; I + Length < Message.end(); I++)
  233. {
  234. // Not a config item
  235. if (I[Length] != ':' || stringcasecmp(I,I+Length,"Config-Item") != 0)
  236. continue;
  237. I += Length + 1;
  238. for (; I < Message.end() && *I == ' '; I++);
  239. const char *Equals = I;
  240. for (; Equals < Message.end() && *Equals != '='; Equals++);
  241. const char *End = Equals;
  242. for (; End < Message.end() && *End != '\n'; End++);
  243. if (End == Equals)
  244. return false;
  245. Cnf.Set(DeQuoteString(string(I,Equals-I)),
  246. DeQuoteString(string(Equals+1,End-Equals-1)));
  247. I = End;
  248. }
  249. return true;
  250. }
  251. /*}}}*/
  252. // AcqMethod::Run - Run the message engine /*{{{*/
  253. // ---------------------------------------------------------------------
  254. /* Fetch any messages and execute them. In single mode it returns 1 if
  255. there are no more available messages - any other result is a
  256. fatal failure code! */
  257. int pkgAcqMethod::Run(bool Single)
  258. {
  259. while (1)
  260. {
  261. // Block if the message queue is empty
  262. if (Messages.empty() == true)
  263. {
  264. if (Single == false)
  265. if (WaitFd(STDIN_FILENO) == false)
  266. break;
  267. if (ReadMessages(STDIN_FILENO,Messages) == false)
  268. break;
  269. }
  270. // Single mode exits if the message queue is empty
  271. if (Single == true && Messages.empty() == true)
  272. return -1;
  273. string Message = Messages.front();
  274. Messages.erase(Messages.begin());
  275. // Fetch the message number
  276. char *End;
  277. int Number = strtol(Message.c_str(),&End,10);
  278. if (End == Message.c_str())
  279. {
  280. cerr << "Malformed message!" << endl;
  281. return 100;
  282. }
  283. switch (Number)
  284. {
  285. case 601:
  286. if (Configuration(Message) == false)
  287. return 100;
  288. break;
  289. case 600:
  290. {
  291. FetchItem *Tmp = new FetchItem;
  292. Tmp->Uri = LookupTag(Message,"URI");
  293. Tmp->DestFile = LookupTag(Message,"FileName");
  294. if (StrToTime(LookupTag(Message,"Last-Modified"),Tmp->LastModified) == false)
  295. Tmp->LastModified = 0;
  296. Tmp->IndexFile = StringToBool(LookupTag(Message,"Index-File"),false);
  297. Tmp->Next = 0;
  298. // Append it to the list
  299. FetchItem **I = &Queue;
  300. for (; *I != 0; I = &(*I)->Next);
  301. *I = Tmp;
  302. if (QueueBack == 0)
  303. QueueBack = Tmp;
  304. // Notify that this item is to be fetched.
  305. if (Fetch(Tmp) == false)
  306. Fail();
  307. break;
  308. }
  309. }
  310. }
  311. Exit();
  312. return 0;
  313. }
  314. /*}}}*/
  315. // AcqMethod::Log - Send a log message /*{{{*/
  316. // ---------------------------------------------------------------------
  317. /* */
  318. void pkgAcqMethod::Log(const char *Format,...)
  319. {
  320. string CurrentURI = "<UNKNOWN>";
  321. if (Queue != 0)
  322. CurrentURI = Queue->Uri;
  323. va_list args;
  324. va_start(args,Format);
  325. // sprintf the description
  326. char S[1024];
  327. unsigned int Len = snprintf(S,sizeof(S)-4,"101 Log\nURI: %s\n"
  328. "Message: ",CurrentURI.c_str());
  329. vsnprintf(S+Len,sizeof(S)-4-Len,Format,args);
  330. strcat(S,"\n\n");
  331. if (write(STDOUT_FILENO,S,strlen(S)) != (signed)strlen(S))
  332. exit(100);
  333. }
  334. /*}}}*/
  335. // AcqMethod::Status - Send a status message /*{{{*/
  336. // ---------------------------------------------------------------------
  337. /* */
  338. void pkgAcqMethod::Status(const char *Format,...)
  339. {
  340. string CurrentURI = "<UNKNOWN>";
  341. if (Queue != 0)
  342. CurrentURI = Queue->Uri;
  343. va_list args;
  344. va_start(args,Format);
  345. // sprintf the description
  346. char S[1024];
  347. unsigned int Len = snprintf(S,sizeof(S)-4,"102 Status\nURI: %s\n"
  348. "Message: ",CurrentURI.c_str());
  349. vsnprintf(S+Len,sizeof(S)-4-Len,Format,args);
  350. strcat(S,"\n\n");
  351. if (write(STDOUT_FILENO,S,strlen(S)) != (signed)strlen(S))
  352. exit(100);
  353. }
  354. /*}}}*/
  355. // AcqMethod::FetchResult::FetchResult - Constructor /*{{{*/
  356. // ---------------------------------------------------------------------
  357. /* */
  358. pkgAcqMethod::FetchResult::FetchResult() : LastModified(0),
  359. IMSHit(false), Size(0), ResumePoint(0)
  360. {
  361. }
  362. /*}}}*/