acquire-method.cc 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505
  1. // -*- mode: cpp; mode: fold -*-
  2. // Description /*{{{*/
  3. // $Id: acquire-method.cc,v 1.27.2.1 2003/12/24 23:09:17 mdz Exp $
  4. /* ######################################################################
  5. Acquire Method
  6. This is a skeleton class that implements most of the functionality
  7. of a method and some useful 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. #include <apt-pkg/acquire-method.h>
  16. #include <apt-pkg/error.h>
  17. #include <apt-pkg/configuration.h>
  18. #include <apt-pkg/strutl.h>
  19. #include <apt-pkg/fileutl.h>
  20. #include <apt-pkg/hashes.h>
  21. #include <iostream>
  22. #include <stdarg.h>
  23. #include <stdio.h>
  24. #include <unistd.h>
  25. #include <sys/signal.h>
  26. /*}}}*/
  27. using namespace std;
  28. // AcqMethod::pkgAcqMethod - Constructor /*{{{*/
  29. // ---------------------------------------------------------------------
  30. /* This constructs the initialization text */
  31. pkgAcqMethod::pkgAcqMethod(const char *Ver,unsigned long Flags)
  32. {
  33. char S[300] = "";
  34. char *End = S;
  35. strcat(End,"100 Capabilities\n");
  36. sprintf(End+strlen(End),"Version: %s\n",Ver);
  37. if ((Flags & SingleInstance) == SingleInstance)
  38. strcat(End,"Single-Instance: true\n");
  39. if ((Flags & Pipeline) == Pipeline)
  40. strcat(End,"Pipeline: true\n");
  41. if ((Flags & SendConfig) == SendConfig)
  42. strcat(End,"Send-Config: true\n");
  43. if ((Flags & LocalOnly) == LocalOnly)
  44. strcat(End,"Local-Only: true\n");
  45. if ((Flags & NeedsCleanup) == NeedsCleanup)
  46. strcat(End,"Needs-Cleanup: true\n");
  47. if ((Flags & Removable) == Removable)
  48. strcat(End,"Removable: true\n");
  49. strcat(End,"\n");
  50. if (write(STDOUT_FILENO,S,strlen(S)) != (signed)strlen(S))
  51. exit(100);
  52. SetNonBlock(STDIN_FILENO,true);
  53. Queue = 0;
  54. QueueBack = 0;
  55. }
  56. /*}}}*/
  57. // AcqMethod::Fail - A fetch has failed /*{{{*/
  58. // ---------------------------------------------------------------------
  59. /* */
  60. void pkgAcqMethod::Fail(bool Transient)
  61. {
  62. string Err = "Undetermined Error";
  63. if (_error->empty() == false)
  64. _error->PopMessage(Err);
  65. _error->Discard();
  66. Fail(Err,Transient);
  67. }
  68. /*}}}*/
  69. // AcqMethod::Fail - A fetch has failed /*{{{*/
  70. // ---------------------------------------------------------------------
  71. /* */
  72. void pkgAcqMethod::Fail(string Err,bool Transient)
  73. {
  74. // Strip out junk from the error messages
  75. for (string::iterator I = Err.begin(); I != Err.end(); I++)
  76. {
  77. if (*I == '\r')
  78. *I = ' ';
  79. if (*I == '\n')
  80. *I = ' ';
  81. }
  82. char S[1024];
  83. char *End = S;
  84. if (Queue != 0)
  85. {
  86. End += snprintf(S,sizeof(S)-50,"400 URI Failure\nURI: %s\n"
  87. "Message: %s %s\n",Queue->Uri.c_str(), Err.c_str(), IP.c_str());
  88. // Dequeue
  89. FetchItem *Tmp = Queue;
  90. Queue = Queue->Next;
  91. delete Tmp;
  92. if (Tmp == QueueBack)
  93. QueueBack = Queue;
  94. }
  95. else
  96. {
  97. End += snprintf(S,sizeof(S)-50,"400 URI Failure\nURI: <UNKNOWN>\n"
  98. "Message: %s\n",Err.c_str());
  99. }
  100. if(FailReason.empty() == false)
  101. End += snprintf(End,sizeof(S)-50 - (End - S),"FailReason: %s\n",FailReason.c_str());
  102. if (UsedMirror.empty() == false)
  103. End += snprintf(End,sizeof(S)-50 - (End - S),"UsedMirror: %s\n",UsedMirror.c_str());
  104. // Set the transient flag
  105. if (Transient == true)
  106. strcat(S,"Transient-Failure: true\n\n");
  107. else
  108. strcat(S,"\n");
  109. if (write(STDOUT_FILENO,S,strlen(S)) != (signed)strlen(S))
  110. exit(100);
  111. }
  112. /*}}}*/
  113. // AcqMethod::URIStart - Indicate a download is starting /*{{{*/
  114. // ---------------------------------------------------------------------
  115. /* */
  116. void pkgAcqMethod::URIStart(FetchResult &Res)
  117. {
  118. if (Queue == 0)
  119. abort();
  120. char S[1024] = "";
  121. char *End = S;
  122. End += snprintf(S,sizeof(S),"200 URI Start\nURI: %s\n",Queue->Uri.c_str());
  123. if (Res.Size != 0)
  124. End += snprintf(End,sizeof(S)-4 - (End - S),"Size: %lu\n",Res.Size);
  125. if (Res.LastModified != 0)
  126. End += snprintf(End,sizeof(S)-4 - (End - S),"Last-Modified: %s\n",
  127. TimeRFC1123(Res.LastModified).c_str());
  128. if (Res.ResumePoint != 0)
  129. End += snprintf(End,sizeof(S)-4 - (End - S),"Resume-Point: %lu\n",
  130. Res.ResumePoint);
  131. strcat(End,"\n");
  132. if (write(STDOUT_FILENO,S,strlen(S)) != (signed)strlen(S))
  133. exit(100);
  134. }
  135. /*}}}*/
  136. // AcqMethod::URIDone - A URI is finished /*{{{*/
  137. // ---------------------------------------------------------------------
  138. /* */
  139. void pkgAcqMethod::URIDone(FetchResult &Res, FetchResult *Alt)
  140. {
  141. if (Queue == 0)
  142. abort();
  143. char S[1024] = "";
  144. char *End = S;
  145. End += snprintf(S,sizeof(S),"201 URI Done\nURI: %s\n",Queue->Uri.c_str());
  146. if (Res.Filename.empty() == false)
  147. End += snprintf(End,sizeof(S)-50 - (End - S),"Filename: %s\n",Res.Filename.c_str());
  148. if (Res.Size != 0)
  149. End += snprintf(End,sizeof(S)-50 - (End - S),"Size: %lu\n",Res.Size);
  150. if (Res.LastModified != 0)
  151. End += snprintf(End,sizeof(S)-50 - (End - S),"Last-Modified: %s\n",
  152. TimeRFC1123(Res.LastModified).c_str());
  153. if (Res.MD5Sum.empty() == false)
  154. {
  155. End += snprintf(End,sizeof(S)-50 - (End - S),"MD5-Hash: %s\n",Res.MD5Sum.c_str());
  156. End += snprintf(End,sizeof(S)-50 - (End - S),"MD5Sum-Hash: %s\n",Res.MD5Sum.c_str());
  157. }
  158. if (Res.SHA1Sum.empty() == false)
  159. End += snprintf(End,sizeof(S)-50 - (End - S),"SHA1-Hash: %s\n",Res.SHA1Sum.c_str());
  160. if (Res.SHA256Sum.empty() == false)
  161. End += snprintf(End,sizeof(S)-50 - (End - S),"SHA256-Hash: %s\n",Res.SHA256Sum.c_str());
  162. if (UsedMirror.empty() == false)
  163. End += snprintf(End,sizeof(S)-50 - (End - S),"UsedMirror: %s\n",UsedMirror.c_str());
  164. if (Res.GPGVOutput.size() > 0)
  165. End += snprintf(End,sizeof(S)-50 - (End - S),"GPGVOutput:\n");
  166. for (vector<string>::iterator I = Res.GPGVOutput.begin();
  167. I != Res.GPGVOutput.end(); I++)
  168. End += snprintf(End,sizeof(S)-50 - (End - S), " %s\n", (*I).c_str());
  169. if (Res.ResumePoint != 0)
  170. End += snprintf(End,sizeof(S)-50 - (End - S),"Resume-Point: %lu\n",
  171. Res.ResumePoint);
  172. if (Res.IMSHit == true)
  173. strcat(End,"IMS-Hit: true\n");
  174. End = S + strlen(S);
  175. if (Alt != 0)
  176. {
  177. if (Alt->Filename.empty() == false)
  178. End += snprintf(End,sizeof(S)-50 - (End - S),"Alt-Filename: %s\n",Alt->Filename.c_str());
  179. if (Alt->Size != 0)
  180. End += snprintf(End,sizeof(S)-50 - (End - S),"Alt-Size: %lu\n",Alt->Size);
  181. if (Alt->LastModified != 0)
  182. End += snprintf(End,sizeof(S)-50 - (End - S),"Alt-Last-Modified: %s\n",
  183. TimeRFC1123(Alt->LastModified).c_str());
  184. if (Alt->MD5Sum.empty() == false)
  185. End += snprintf(End,sizeof(S)-50 - (End - S),"Alt-MD5-Hash: %s\n",
  186. Alt->MD5Sum.c_str());
  187. if (Alt->SHA1Sum.empty() == false)
  188. End += snprintf(End,sizeof(S)-50 - (End - S),"Alt-SHA1-Hash: %s\n",
  189. Alt->SHA1Sum.c_str());
  190. if (Alt->SHA256Sum.empty() == false)
  191. End += snprintf(End,sizeof(S)-50 - (End - S),"Alt-SHA256-Hash: %s\n",
  192. Alt->SHA256Sum.c_str());
  193. if (Alt->IMSHit == true)
  194. strcat(End,"Alt-IMS-Hit: true\n");
  195. }
  196. strcat(End,"\n");
  197. if (write(STDOUT_FILENO,S,strlen(S)) != (signed)strlen(S))
  198. exit(100);
  199. // Dequeue
  200. FetchItem *Tmp = Queue;
  201. Queue = Queue->Next;
  202. delete Tmp;
  203. if (Tmp == QueueBack)
  204. QueueBack = Queue;
  205. }
  206. /*}}}*/
  207. // AcqMethod::MediaFail - Syncronous request for new media /*{{{*/
  208. // ---------------------------------------------------------------------
  209. /* This sends a 403 Media Failure message to the APT and waits for it
  210. to be ackd */
  211. bool pkgAcqMethod::MediaFail(string Required,string Drive)
  212. {
  213. char S[1024];
  214. snprintf(S,sizeof(S),"403 Media Failure\nMedia: %s\nDrive: %s\n\n",
  215. Required.c_str(),Drive.c_str());
  216. if (write(STDOUT_FILENO,S,strlen(S)) != (signed)strlen(S))
  217. exit(100);
  218. vector<string> MyMessages;
  219. /* Here we read messages until we find a 603, each non 603 message is
  220. appended to the main message list for later processing */
  221. while (1)
  222. {
  223. if (WaitFd(STDIN_FILENO) == false)
  224. return false;
  225. if (ReadMessages(STDIN_FILENO,MyMessages) == false)
  226. return false;
  227. string Message = MyMessages.front();
  228. MyMessages.erase(MyMessages.begin());
  229. // Fetch the message number
  230. char *End;
  231. int Number = strtol(Message.c_str(),&End,10);
  232. if (End == Message.c_str())
  233. {
  234. cerr << "Malformed message!" << endl;
  235. exit(100);
  236. }
  237. // Change ack
  238. if (Number == 603)
  239. {
  240. while (MyMessages.empty() == false)
  241. {
  242. Messages.push_back(MyMessages.front());
  243. MyMessages.erase(MyMessages.begin());
  244. }
  245. return !StringToBool(LookupTag(Message,"Failed"),false);
  246. }
  247. Messages.push_back(Message);
  248. }
  249. }
  250. /*}}}*/
  251. // AcqMethod::Configuration - Handle the configuration message /*{{{*/
  252. // ---------------------------------------------------------------------
  253. /* This parses each configuration entry and puts it into the _config
  254. Configuration class. */
  255. bool pkgAcqMethod::Configuration(string Message)
  256. {
  257. ::Configuration &Cnf = *_config;
  258. const char *I = Message.c_str();
  259. const char *MsgEnd = I + Message.length();
  260. unsigned int Length = strlen("Config-Item");
  261. for (; I + Length < MsgEnd; I++)
  262. {
  263. // Not a config item
  264. if (I[Length] != ':' || stringcasecmp(I,I+Length,"Config-Item") != 0)
  265. continue;
  266. I += Length + 1;
  267. for (; I < MsgEnd && *I == ' '; I++);
  268. const char *Equals = I;
  269. for (; Equals < MsgEnd && *Equals != '='; Equals++);
  270. const char *End = Equals;
  271. for (; End < MsgEnd && *End != '\n'; End++);
  272. if (End == Equals)
  273. return false;
  274. Cnf.Set(DeQuoteString(string(I,Equals-I)),
  275. DeQuoteString(string(Equals+1,End-Equals-1)));
  276. I = End;
  277. }
  278. return true;
  279. }
  280. /*}}}*/
  281. // AcqMethod::Run - Run the message engine /*{{{*/
  282. // ---------------------------------------------------------------------
  283. /* Fetch any messages and execute them. In single mode it returns 1 if
  284. there are no more available messages - any other result is a
  285. fatal failure code! */
  286. int pkgAcqMethod::Run(bool Single)
  287. {
  288. while (1)
  289. {
  290. // Block if the message queue is empty
  291. if (Messages.empty() == true)
  292. {
  293. if (Single == false)
  294. if (WaitFd(STDIN_FILENO) == false)
  295. break;
  296. if (ReadMessages(STDIN_FILENO,Messages) == false)
  297. break;
  298. }
  299. // Single mode exits if the message queue is empty
  300. if (Single == true && Messages.empty() == true)
  301. return -1;
  302. string Message = Messages.front();
  303. Messages.erase(Messages.begin());
  304. // Fetch the message number
  305. char *End;
  306. int Number = strtol(Message.c_str(),&End,10);
  307. if (End == Message.c_str())
  308. {
  309. cerr << "Malformed message!" << endl;
  310. return 100;
  311. }
  312. switch (Number)
  313. {
  314. case 601:
  315. if (Configuration(Message) == false)
  316. return 100;
  317. break;
  318. case 600:
  319. {
  320. FetchItem *Tmp = new FetchItem;
  321. Tmp->Uri = LookupTag(Message,"URI");
  322. Tmp->DestFile = LookupTag(Message,"FileName");
  323. if (RFC1123StrToTime(LookupTag(Message,"Last-Modified").c_str(),Tmp->LastModified) == false)
  324. Tmp->LastModified = 0;
  325. Tmp->IndexFile = StringToBool(LookupTag(Message,"Index-File"),false);
  326. Tmp->FailIgnore = StringToBool(LookupTag(Message,"Fail-Ignore"),false);
  327. Tmp->Next = 0;
  328. // Append it to the list
  329. FetchItem **I = &Queue;
  330. for (; *I != 0; I = &(*I)->Next);
  331. *I = Tmp;
  332. if (QueueBack == 0)
  333. QueueBack = Tmp;
  334. // Notify that this item is to be fetched.
  335. if (Fetch(Tmp) == false)
  336. Fail();
  337. break;
  338. }
  339. }
  340. }
  341. Exit();
  342. return 0;
  343. }
  344. /*}}}*/
  345. // AcqMethod::Log - Send a log message /*{{{*/
  346. // ---------------------------------------------------------------------
  347. /* */
  348. void pkgAcqMethod::Log(const char *Format,...)
  349. {
  350. string CurrentURI = "<UNKNOWN>";
  351. if (Queue != 0)
  352. CurrentURI = Queue->Uri;
  353. va_list args;
  354. va_start(args,Format);
  355. // sprintf the description
  356. char S[1024];
  357. unsigned int Len = snprintf(S,sizeof(S)-4,"101 Log\nURI: %s\n"
  358. "Message: ",CurrentURI.c_str());
  359. vsnprintf(S+Len,sizeof(S)-4-Len,Format,args);
  360. strcat(S,"\n\n");
  361. if (write(STDOUT_FILENO,S,strlen(S)) != (signed)strlen(S))
  362. exit(100);
  363. }
  364. /*}}}*/
  365. // AcqMethod::Status - Send a status message /*{{{*/
  366. // ---------------------------------------------------------------------
  367. /* */
  368. void pkgAcqMethod::Status(const char *Format,...)
  369. {
  370. string CurrentURI = "<UNKNOWN>";
  371. if (Queue != 0)
  372. CurrentURI = Queue->Uri;
  373. va_list args;
  374. va_start(args,Format);
  375. // sprintf the description
  376. char S[1024];
  377. unsigned int Len = snprintf(S,sizeof(S)-4,"102 Status\nURI: %s\n"
  378. "Message: ",CurrentURI.c_str());
  379. vsnprintf(S+Len,sizeof(S)-4-Len,Format,args);
  380. strcat(S,"\n\n");
  381. if (write(STDOUT_FILENO,S,strlen(S)) != (signed)strlen(S))
  382. exit(100);
  383. }
  384. /*}}}*/
  385. // AcqMethod::Redirect - Send a redirect message /*{{{*/
  386. // ---------------------------------------------------------------------
  387. /* This method sends the redirect message and also manipulates the queue
  388. to keep the pipeline synchronized. */
  389. void pkgAcqMethod::Redirect(const string &NewURI)
  390. {
  391. string CurrentURI = "<UNKNOWN>";
  392. if (Queue != 0)
  393. CurrentURI = Queue->Uri;
  394. char S[1024];
  395. snprintf(S, sizeof(S)-50, "103 Redirect\nURI: %s\nNew-URI: %s\n\n",
  396. CurrentURI.c_str(), NewURI.c_str());
  397. if (write(STDOUT_FILENO,S,strlen(S)) != (ssize_t)strlen(S))
  398. exit(100);
  399. // Change the URI for the request.
  400. Queue->Uri = NewURI;
  401. /* To keep the pipeline synchronized, move the current request to
  402. the end of the queue, past the end of the current pipeline. */
  403. FetchItem *I;
  404. for (I = Queue; I->Next != 0; I = I->Next) ;
  405. I->Next = Queue;
  406. Queue = Queue->Next;
  407. I->Next->Next = 0;
  408. if (QueueBack == 0)
  409. QueueBack = I->Next;
  410. }
  411. /*}}}*/
  412. // AcqMethod::FetchResult::FetchResult - Constructor /*{{{*/
  413. // ---------------------------------------------------------------------
  414. /* */
  415. pkgAcqMethod::FetchResult::FetchResult() : LastModified(0),
  416. IMSHit(false), Size(0), ResumePoint(0)
  417. {
  418. }
  419. /*}}}*/
  420. // AcqMethod::FetchResult::TakeHashes - Load hashes /*{{{*/
  421. // ---------------------------------------------------------------------
  422. /* This hides the number of hashes we are supporting from the caller.
  423. It just deals with the hash class. */
  424. void pkgAcqMethod::FetchResult::TakeHashes(Hashes &Hash)
  425. {
  426. MD5Sum = Hash.MD5.Result();
  427. SHA1Sum = Hash.SHA1.Result();
  428. SHA256Sum = Hash.SHA256.Result();
  429. }
  430. /*}}}*/