acquire-method.cc 14 KB

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