acquire-method.cc 11 KB

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