acquire-method.cc 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  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 <stdio.h>
  23. #include <sys/signal.h>
  24. /*}}}*/
  25. using namespace std;
  26. // AcqMethod::pkgAcqMethod - Constructor /*{{{*/
  27. // ---------------------------------------------------------------------
  28. /* This constructs the initialization text */
  29. pkgAcqMethod::pkgAcqMethod(const char *Ver,unsigned long Flags)
  30. {
  31. std::cout << "100 Capabilities\n"
  32. << "Version: " << Ver << "\n";
  33. if ((Flags & SingleInstance) == SingleInstance)
  34. std::cout << "Single-Instance: true\n";
  35. if ((Flags & Pipeline) == Pipeline)
  36. std::cout << "Pipeline: true\n";
  37. if ((Flags & SendConfig) == SendConfig)
  38. std::cout << "Send-Config: true\n";
  39. if ((Flags & LocalOnly) == LocalOnly)
  40. std::cout <<"Local-Only: true\n";
  41. if ((Flags & NeedsCleanup) == NeedsCleanup)
  42. std::cout << "Needs-Cleanup: true\n";
  43. if ((Flags & Removable) == Removable)
  44. std::cout << "Removable: true\n";
  45. std::cout << "\n" << std::flush;
  46. SetNonBlock(STDIN_FILENO,true);
  47. Queue = 0;
  48. QueueBack = 0;
  49. }
  50. /*}}}*/
  51. // AcqMethod::Fail - A fetch has failed /*{{{*/
  52. // ---------------------------------------------------------------------
  53. /* */
  54. void pkgAcqMethod::Fail(bool Transient)
  55. {
  56. string Err = "Undetermined Error";
  57. if (_error->empty() == false)
  58. _error->PopMessage(Err);
  59. _error->Discard();
  60. Fail(Err,Transient);
  61. }
  62. /*}}}*/
  63. // AcqMethod::Fail - A fetch has failed /*{{{*/
  64. // ---------------------------------------------------------------------
  65. /* */
  66. void pkgAcqMethod::Fail(string Err,bool Transient)
  67. {
  68. // Strip out junk from the error messages
  69. for (string::iterator I = Err.begin(); I != Err.end(); ++I)
  70. {
  71. if (*I == '\r')
  72. *I = ' ';
  73. if (*I == '\n')
  74. *I = ' ';
  75. }
  76. if (Queue != 0)
  77. {
  78. std::cout << "400 URI Failure\nURI: " << Queue->Uri << "\n"
  79. << "Message: " << Err << " " << IP << "\n";
  80. // Dequeue
  81. FetchItem *Tmp = Queue;
  82. Queue = Queue->Next;
  83. delete Tmp;
  84. if (Tmp == QueueBack)
  85. QueueBack = Queue;
  86. }
  87. else
  88. std::cout << "400 URI Failure\nURI: <UNKNOWN>\nMessage: " << Err << "\n";
  89. if(FailReason.empty() == false)
  90. std::cout << "FailReason: " << FailReason << "\n";
  91. if (UsedMirror.empty() == false)
  92. std::cout << "UsedMirror: " << UsedMirror << "\n";
  93. // Set the transient flag
  94. if (Transient == true)
  95. std::cout << "Transient-Failure: true\n";
  96. std::cout << "\n" << std::flush;
  97. }
  98. /*}}}*/
  99. // AcqMethod::URIStart - Indicate a download is starting /*{{{*/
  100. // ---------------------------------------------------------------------
  101. /* */
  102. void pkgAcqMethod::URIStart(FetchResult &Res)
  103. {
  104. if (Queue == 0)
  105. abort();
  106. std::cout << "200 URI Start\n"
  107. << "URI: " << Queue->Uri << "\n";
  108. if (Res.Size != 0)
  109. std::cout << "Size: " << Res.Size << "\n";
  110. if (Res.LastModified != 0)
  111. std::cout << "Last-Modified: " << TimeRFC1123(Res.LastModified) << "\n";
  112. if (Res.ResumePoint != 0)
  113. std::cout << "Resume-Point: " << Res.ResumePoint << "\n";
  114. if (UsedMirror.empty() == false)
  115. std::cout << "UsedMirror: " << UsedMirror << "\n";
  116. std::cout << "\n" << std::flush;
  117. }
  118. /*}}}*/
  119. // AcqMethod::URIDone - A URI is finished /*{{{*/
  120. // ---------------------------------------------------------------------
  121. /* */
  122. void pkgAcqMethod::URIDone(FetchResult &Res, FetchResult *Alt)
  123. {
  124. if (Queue == 0)
  125. abort();
  126. std::cout << "201 URI Done\n"
  127. << "URI: " << Queue->Uri << "\n";
  128. if (Res.Filename.empty() == false)
  129. std::cout << "Filename: " << Res.Filename << "\n";
  130. if (Res.Size != 0)
  131. std::cout << "Size: " << Res.Size << "\n";
  132. if (Res.LastModified != 0)
  133. std::cout << "Last-Modified: " << TimeRFC1123(Res.LastModified) << "\n";
  134. if (Res.MD5Sum.empty() == false)
  135. std::cout << "MD5-Hash: " << Res.MD5Sum << "\n"
  136. << "MD5Sum-Hash: " << Res.MD5Sum << "\n";
  137. if (Res.SHA1Sum.empty() == false)
  138. std::cout << "SHA1-Hash: " << Res.SHA1Sum << "\n";
  139. if (Res.SHA256Sum.empty() == false)
  140. std::cout << "SHA256-Hash: " << Res.SHA256Sum << "\n";
  141. if (UsedMirror.empty() == false)
  142. std::cout << "UsedMirror: " << UsedMirror << "\n";
  143. if (Res.GPGVOutput.empty() == false)
  144. {
  145. std::cout << "GPGVOutput:\n";
  146. for (vector<string>::const_iterator I = Res.GPGVOutput.begin();
  147. I != Res.GPGVOutput.end(); ++I)
  148. std::cout << " " << *I << "\n";
  149. }
  150. if (Res.ResumePoint != 0)
  151. std::cout << "Resume-Point: " << Res.ResumePoint << "\n";
  152. if (Res.IMSHit == true)
  153. std::cout << "IMS-Hit: true\n";
  154. if (Alt != 0)
  155. {
  156. if (Alt->Filename.empty() == false)
  157. std::cout << "Alt-Filename: " << Alt->Filename << "\n";
  158. if (Alt->Size != 0)
  159. std::cout << "Alt-Size: " << Alt->Size << "\n";
  160. if (Alt->LastModified != 0)
  161. std::cout << "Alt-Last-Modified: " << TimeRFC1123(Alt->LastModified) << "\n";
  162. if (Alt->MD5Sum.empty() == false)
  163. std::cout << "Alt-MD5-Hash: " << Alt->MD5Sum << "\n";
  164. if (Alt->SHA1Sum.empty() == false)
  165. std::cout << "Alt-SHA1-Hash: " << Alt->SHA1Sum << "\n";
  166. if (Alt->SHA256Sum.empty() == false)
  167. std::cout << "Alt-SHA256-Hash: " << Alt->SHA256Sum << "\n";
  168. if (Alt->IMSHit == true)
  169. std::cout << "Alt-IMS-Hit: true\n";
  170. }
  171. std::cout << "\n" << std::flush;
  172. // Dequeue
  173. FetchItem *Tmp = Queue;
  174. Queue = Queue->Next;
  175. delete Tmp;
  176. if (Tmp == QueueBack)
  177. QueueBack = Queue;
  178. }
  179. /*}}}*/
  180. // AcqMethod::MediaFail - Syncronous request for new media /*{{{*/
  181. // ---------------------------------------------------------------------
  182. /* This sends a 403 Media Failure message to the APT and waits for it
  183. to be ackd */
  184. bool pkgAcqMethod::MediaFail(string Required,string Drive)
  185. {
  186. fprintf(stdout, "403 Media Failure\nMedia: %s\nDrive: %s\n",
  187. Required.c_str(),Drive.c_str());
  188. std::cout << "\n" << std::flush;
  189. vector<string> MyMessages;
  190. /* Here we read messages until we find a 603, each non 603 message is
  191. appended to the main message list for later processing */
  192. while (1)
  193. {
  194. if (WaitFd(STDIN_FILENO) == false)
  195. return false;
  196. if (ReadMessages(STDIN_FILENO,MyMessages) == false)
  197. return false;
  198. string Message = MyMessages.front();
  199. MyMessages.erase(MyMessages.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. exit(100);
  207. }
  208. // Change ack
  209. if (Number == 603)
  210. {
  211. while (MyMessages.empty() == false)
  212. {
  213. Messages.push_back(MyMessages.front());
  214. MyMessages.erase(MyMessages.begin());
  215. }
  216. return !StringToBool(LookupTag(Message,"Failed"),false);
  217. }
  218. Messages.push_back(Message);
  219. }
  220. }
  221. /*}}}*/
  222. // AcqMethod::Configuration - Handle the configuration message /*{{{*/
  223. // ---------------------------------------------------------------------
  224. /* This parses each configuration entry and puts it into the _config
  225. Configuration class. */
  226. bool pkgAcqMethod::Configuration(string Message)
  227. {
  228. ::Configuration &Cnf = *_config;
  229. const char *I = Message.c_str();
  230. const char *MsgEnd = I + Message.length();
  231. unsigned int Length = strlen("Config-Item");
  232. for (; I + Length < MsgEnd; 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 < MsgEnd && *I == ' '; I++);
  239. const char *Equals = I;
  240. for (; Equals < MsgEnd && *Equals != '='; Equals++);
  241. const char *End = Equals;
  242. for (; End < MsgEnd && *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 (RFC1123StrToTime(LookupTag(Message,"Last-Modified").c_str(),Tmp->LastModified) == false)
  295. Tmp->LastModified = 0;
  296. Tmp->IndexFile = StringToBool(LookupTag(Message,"Index-File"),false);
  297. Tmp->FailIgnore = StringToBool(LookupTag(Message,"Fail-Ignore"),false);
  298. Tmp->Next = 0;
  299. // Append it to the list
  300. FetchItem **I = &Queue;
  301. for (; *I != 0; I = &(*I)->Next);
  302. *I = Tmp;
  303. if (QueueBack == 0)
  304. QueueBack = Tmp;
  305. // Notify that this item is to be fetched.
  306. if (Fetch(Tmp) == false)
  307. Fail();
  308. break;
  309. }
  310. }
  311. }
  312. Exit();
  313. return 0;
  314. }
  315. /*}}}*/
  316. // AcqMethod::PrintStatus - privately really send a log/status message /*{{{*/
  317. // ---------------------------------------------------------------------
  318. /* */
  319. void pkgAcqMethod::PrintStatus(char const * const header, const char* Format,
  320. va_list &args) const
  321. {
  322. string CurrentURI = "<UNKNOWN>";
  323. if (Queue != 0)
  324. CurrentURI = Queue->Uri;
  325. if (UsedMirror.empty() == true)
  326. fprintf(stdout, "%s\nURI: %s\nMessage: ",
  327. header, CurrentURI.c_str());
  328. else
  329. fprintf(stdout, "%s\nURI: %s\nUsedMirror: %s\nMessage: ",
  330. header, CurrentURI.c_str(), UsedMirror.c_str());
  331. vfprintf(stdout,Format,args);
  332. std::cout << "\n\n" << std::flush;
  333. }
  334. /*}}}*/
  335. // AcqMethod::Log - Send a log message /*{{{*/
  336. // ---------------------------------------------------------------------
  337. /* */
  338. void pkgAcqMethod::Log(const char *Format,...)
  339. {
  340. va_list args;
  341. va_start(args,Format);
  342. PrintStatus("101 Log", Format, args);
  343. va_end(args);
  344. }
  345. /*}}}*/
  346. // AcqMethod::Status - Send a status message /*{{{*/
  347. // ---------------------------------------------------------------------
  348. /* */
  349. void pkgAcqMethod::Status(const char *Format,...)
  350. {
  351. va_list args;
  352. va_start(args,Format);
  353. PrintStatus("102 Status", Format, args);
  354. va_end(args);
  355. }
  356. /*}}}*/
  357. // AcqMethod::Redirect - Send a redirect message /*{{{*/
  358. // ---------------------------------------------------------------------
  359. /* This method sends the redirect message and also manipulates the queue
  360. to keep the pipeline synchronized. */
  361. void pkgAcqMethod::Redirect(const string &NewURI)
  362. {
  363. std::cout << "103 Redirect\nURI: " << Queue->Uri << "\n"
  364. << "New-URI: " << NewURI << "\n"
  365. << "\n" << std::flush;
  366. // Change the URI for the request.
  367. Queue->Uri = NewURI;
  368. /* To keep the pipeline synchronized, move the current request to
  369. the end of the queue, past the end of the current pipeline. */
  370. FetchItem *I;
  371. for (I = Queue; I->Next != 0; I = I->Next) ;
  372. I->Next = Queue;
  373. Queue = Queue->Next;
  374. I->Next->Next = 0;
  375. if (QueueBack == 0)
  376. QueueBack = I->Next;
  377. }
  378. /*}}}*/
  379. // AcqMethod::FetchResult::FetchResult - Constructor /*{{{*/
  380. // ---------------------------------------------------------------------
  381. /* */
  382. pkgAcqMethod::FetchResult::FetchResult() : LastModified(0),
  383. IMSHit(false), Size(0), ResumePoint(0)
  384. {
  385. }
  386. /*}}}*/
  387. // AcqMethod::FetchResult::TakeHashes - Load hashes /*{{{*/
  388. // ---------------------------------------------------------------------
  389. /* This hides the number of hashes we are supporting from the caller.
  390. It just deals with the hash class. */
  391. void pkgAcqMethod::FetchResult::TakeHashes(Hashes &Hash)
  392. {
  393. MD5Sum = Hash.MD5.Result();
  394. SHA1Sum = Hash.SHA1.Result();
  395. SHA256Sum = Hash.SHA256.Result();
  396. }
  397. /*}}}*/