You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

637 lines
22 KiB

  1. // -*- mode: cpp; mode: fold -*-
  2. // Description /*{{{*/
  3. /* ######################################################################
  4. Set of methods to help writing and reading everything needed for EDSP
  5. ##################################################################### */
  6. /*}}}*/
  7. // Include Files /*{{{*/
  8. #include <config.h>
  9. #include <apt-pkg/edsp.h>
  10. #include <apt-pkg/error.h>
  11. #include <apt-pkg/cacheset.h>
  12. #include <apt-pkg/configuration.h>
  13. #include <apt-pkg/tagfile.h>
  14. #include <apt-pkg/fileutl.h>
  15. #include <apt-pkg/progress.h>
  16. #include <apt-pkg/depcache.h>
  17. #include <apt-pkg/pkgcache.h>
  18. #include <apt-pkg/cacheiterators.h>
  19. #include <apt-pkg/strutl.h>
  20. #include <apt-pkg/pkgrecords.h>
  21. #include <ctype.h>
  22. #include <stddef.h>
  23. #include <string.h>
  24. #include <time.h>
  25. #include <unistd.h>
  26. #include <stdio.h>
  27. #include <algorithm>
  28. #include <iostream>
  29. #include <vector>
  30. #include <limits>
  31. #include <string>
  32. #include <list>
  33. #include <apti18n.h>
  34. /*}}}*/
  35. using std::string;
  36. // we could use pkgCache::DepType and ::Priority, but these would be localized strings…
  37. const char * const EDSP::PrioMap[] = {0, "important", "required", "standard",
  38. "optional", "extra"};
  39. const char * const EDSP::DepMap[] = {"", "Depends", "Pre-Depends", "Suggests",
  40. "Recommends" , "Conflicts", "Replaces",
  41. "Obsoletes", "Breaks", "Enhances"};
  42. // EDSP::WriteScenario - to the given file descriptor /*{{{*/
  43. bool EDSP::WriteScenario(pkgDepCache &Cache, FILE* output, OpProgress *Progress)
  44. {
  45. if (Progress != NULL)
  46. Progress->SubProgress(Cache.Head().VersionCount, _("Send scenario to solver"));
  47. unsigned long p = 0;
  48. std::vector<std::string> archs = APT::Configuration::getArchitectures();
  49. for (pkgCache::PkgIterator Pkg = Cache.PkgBegin(); Pkg.end() == false; ++Pkg)
  50. {
  51. std::string const arch = Pkg.Arch();
  52. if (std::find(archs.begin(), archs.end(), arch) == archs.end())
  53. continue;
  54. for (pkgCache::VerIterator Ver = Pkg.VersionList(); Ver.end() == false; ++Ver, ++p)
  55. {
  56. WriteScenarioVersion(Cache, output, Pkg, Ver);
  57. WriteScenarioDependency(output, Ver);
  58. fprintf(output, "\n");
  59. if (Progress != NULL && p % 100 == 0)
  60. Progress->Progress(p);
  61. }
  62. }
  63. return true;
  64. }
  65. /*}}}*/
  66. // EDSP::WriteLimitedScenario - to the given file descriptor /*{{{*/
  67. bool EDSP::WriteLimitedScenario(pkgDepCache &Cache, FILE* output,
  68. APT::PackageSet const &pkgset,
  69. OpProgress *Progress)
  70. {
  71. if (Progress != NULL)
  72. Progress->SubProgress(Cache.Head().VersionCount, _("Send scenario to solver"));
  73. unsigned long p = 0;
  74. for (APT::PackageSet::const_iterator Pkg = pkgset.begin(); Pkg != pkgset.end(); ++Pkg, ++p)
  75. for (pkgCache::VerIterator Ver = Pkg.VersionList(); Ver.end() == false; ++Ver)
  76. {
  77. WriteScenarioVersion(Cache, output, Pkg, Ver);
  78. WriteScenarioLimitedDependency(output, Ver, pkgset);
  79. fprintf(output, "\n");
  80. if (Progress != NULL && p % 100 == 0)
  81. Progress->Progress(p);
  82. }
  83. if (Progress != NULL)
  84. Progress->Done();
  85. return true;
  86. }
  87. /*}}}*/
  88. // EDSP::WriteScenarioVersion /*{{{*/
  89. void EDSP::WriteScenarioVersion(pkgDepCache &Cache, FILE* output, pkgCache::PkgIterator const &Pkg,
  90. pkgCache::VerIterator const &Ver)
  91. {
  92. fprintf(output, "Package: %s\n", Pkg.Name());
  93. #if APT_PKG_ABI >= 413
  94. fprintf(output, "Source: %s\n", Ver.SourcePkgName());
  95. #else
  96. pkgRecords Recs(Cache);
  97. pkgRecords::Parser &rec = Recs.Lookup(Ver.FileList());
  98. string srcpkg = rec.SourcePkg().empty() ? Pkg.Name() : rec.SourcePkg();
  99. fprintf(output, "Source: %s\n", srcpkg.c_str());
  100. #endif
  101. fprintf(output, "Architecture: %s\n", Ver.Arch());
  102. fprintf(output, "Version: %s\n", Ver.VerStr());
  103. if (Pkg.CurrentVer() == Ver)
  104. fprintf(output, "Installed: yes\n");
  105. if (Pkg->SelectedState == pkgCache::State::Hold ||
  106. (Cache[Pkg].Keep() == true && Cache[Pkg].Protect() == true))
  107. fprintf(output, "Hold: yes\n");
  108. fprintf(output, "APT-ID: %d\n", Ver->ID);
  109. fprintf(output, "Priority: %s\n", PrioMap[Ver->Priority]);
  110. if ((Pkg->Flags & pkgCache::Flag::Essential) == pkgCache::Flag::Essential)
  111. fprintf(output, "Essential: yes\n");
  112. fprintf(output, "Section: %s\n", Ver.Section());
  113. if ((Ver->MultiArch & pkgCache::Version::Allowed) == pkgCache::Version::Allowed)
  114. fprintf(output, "Multi-Arch: allowed\n");
  115. else if ((Ver->MultiArch & pkgCache::Version::Foreign) == pkgCache::Version::Foreign)
  116. fprintf(output, "Multi-Arch: foreign\n");
  117. else if ((Ver->MultiArch & pkgCache::Version::Same) == pkgCache::Version::Same)
  118. fprintf(output, "Multi-Arch: same\n");
  119. signed short Pin = std::numeric_limits<signed short>::min();
  120. std::set<string> Releases;
  121. for (pkgCache::VerFileIterator I = Ver.FileList(); I.end() == false; ++I) {
  122. pkgCache::PkgFileIterator File = I.File();
  123. signed short const p = Cache.GetPolicy().GetPriority(File);
  124. if (Pin < p)
  125. Pin = p;
  126. if (File.Flagged(pkgCache::Flag::NotSource) == false) {
  127. string Release = File.RelStr();
  128. if (!Release.empty())
  129. Releases.insert(Release);
  130. }
  131. }
  132. if (!Releases.empty()) {
  133. fprintf(output, "APT-Release:\n");
  134. for (std::set<string>::iterator R = Releases.begin(); R != Releases.end(); ++R)
  135. fprintf(output, " %s\n", R->c_str());
  136. }
  137. fprintf(output, "APT-Pin: %d\n", Pin);
  138. if (Cache.GetCandidateVer(Pkg) == Ver)
  139. fprintf(output, "APT-Candidate: yes\n");
  140. if ((Cache[Pkg].Flags & pkgCache::Flag::Auto) == pkgCache::Flag::Auto)
  141. fprintf(output, "APT-Automatic: yes\n");
  142. }
  143. /*}}}*/
  144. // EDSP::WriteScenarioDependency /*{{{*/
  145. void EDSP::WriteScenarioDependency( FILE* output, pkgCache::VerIterator const &Ver)
  146. {
  147. std::string dependencies[pkgCache::Dep::Enhances + 1];
  148. bool orGroup = false;
  149. for (pkgCache::DepIterator Dep = Ver.DependsList(); Dep.end() == false; ++Dep)
  150. {
  151. if (Dep.IsMultiArchImplicit() == true)
  152. continue;
  153. if (orGroup == false)
  154. dependencies[Dep->Type].append(", ");
  155. dependencies[Dep->Type].append(Dep.TargetPkg().Name());
  156. if (Dep->Version != 0)
  157. dependencies[Dep->Type].append(" (").append(pkgCache::CompTypeDeb(Dep->CompareOp)).append(" ").append(Dep.TargetVer()).append(")");
  158. if ((Dep->CompareOp & pkgCache::Dep::Or) == pkgCache::Dep::Or)
  159. {
  160. dependencies[Dep->Type].append(" | ");
  161. orGroup = true;
  162. }
  163. else
  164. orGroup = false;
  165. }
  166. for (int i = 1; i < pkgCache::Dep::Enhances + 1; ++i)
  167. if (dependencies[i].empty() == false)
  168. fprintf(output, "%s: %s\n", DepMap[i], dependencies[i].c_str()+2);
  169. string provides;
  170. for (pkgCache::PrvIterator Prv = Ver.ProvidesList(); Prv.end() == false; ++Prv)
  171. {
  172. if (Prv.IsMultiArchImplicit() == true)
  173. continue;
  174. provides.append(", ").append(Prv.Name());
  175. }
  176. if (provides.empty() == false)
  177. fprintf(output, "Provides: %s\n", provides.c_str()+2);
  178. }
  179. /*}}}*/
  180. // EDSP::WriteScenarioLimitedDependency /*{{{*/
  181. void EDSP::WriteScenarioLimitedDependency(FILE* output,
  182. pkgCache::VerIterator const &Ver,
  183. APT::PackageSet const &pkgset)
  184. {
  185. std::string dependencies[pkgCache::Dep::Enhances + 1];
  186. bool orGroup = false;
  187. for (pkgCache::DepIterator Dep = Ver.DependsList(); Dep.end() == false; ++Dep)
  188. {
  189. if (Dep.IsMultiArchImplicit() == true)
  190. continue;
  191. if (orGroup == false)
  192. {
  193. if (pkgset.find(Dep.TargetPkg()) == pkgset.end())
  194. continue;
  195. dependencies[Dep->Type].append(", ");
  196. }
  197. else if (pkgset.find(Dep.TargetPkg()) == pkgset.end())
  198. {
  199. if ((Dep->CompareOp & pkgCache::Dep::Or) == pkgCache::Dep::Or)
  200. continue;
  201. dependencies[Dep->Type].erase(dependencies[Dep->Type].end()-3, dependencies[Dep->Type].end());
  202. orGroup = false;
  203. continue;
  204. }
  205. dependencies[Dep->Type].append(Dep.TargetPkg().Name());
  206. if (Dep->Version != 0)
  207. dependencies[Dep->Type].append(" (").append(pkgCache::CompTypeDeb(Dep->CompareOp)).append(" ").append(Dep.TargetVer()).append(")");
  208. if ((Dep->CompareOp & pkgCache::Dep::Or) == pkgCache::Dep::Or)
  209. {
  210. dependencies[Dep->Type].append(" | ");
  211. orGroup = true;
  212. }
  213. else
  214. orGroup = false;
  215. }
  216. for (int i = 1; i < pkgCache::Dep::Enhances + 1; ++i)
  217. if (dependencies[i].empty() == false)
  218. fprintf(output, "%s: %s\n", DepMap[i], dependencies[i].c_str()+2);
  219. string provides;
  220. for (pkgCache::PrvIterator Prv = Ver.ProvidesList(); Prv.end() == false; ++Prv)
  221. {
  222. if (Prv.IsMultiArchImplicit() == true)
  223. continue;
  224. if (pkgset.find(Prv.ParentPkg()) == pkgset.end())
  225. continue;
  226. provides.append(", ").append(Prv.Name());
  227. }
  228. if (provides.empty() == false)
  229. fprintf(output, "Provides: %s\n", provides.c_str()+2);
  230. }
  231. /*}}}*/
  232. // EDSP::WriteRequest - to the given file descriptor /*{{{*/
  233. bool EDSP::WriteRequest(pkgDepCache &Cache, FILE* output, bool const Upgrade,
  234. bool const DistUpgrade, bool const AutoRemove,
  235. OpProgress *Progress)
  236. {
  237. if (Progress != NULL)
  238. Progress->SubProgress(Cache.Head().PackageCount, _("Send request to solver"));
  239. unsigned long p = 0;
  240. string del, inst;
  241. for (pkgCache::PkgIterator Pkg = Cache.PkgBegin(); Pkg.end() == false; ++Pkg, ++p)
  242. {
  243. if (Progress != NULL && p % 100 == 0)
  244. Progress->Progress(p);
  245. string* req;
  246. pkgDepCache::StateCache &P = Cache[Pkg];
  247. if (P.Delete() == true)
  248. req = &del;
  249. else if (P.NewInstall() == true || P.Upgrade() == true || P.ReInstall() == true ||
  250. (P.Mode == pkgDepCache::ModeKeep && (P.iFlags & pkgDepCache::Protected) == pkgDepCache::Protected))
  251. req = &inst;
  252. else
  253. continue;
  254. req->append(" ").append(Pkg.FullName());
  255. }
  256. fprintf(output, "Request: EDSP 0.5\n");
  257. const char *arch = _config->Find("APT::Architecture").c_str();
  258. std::vector<string> archs = APT::Configuration::getArchitectures();
  259. fprintf(output, "Architecture: %s\n", arch);
  260. fprintf(output, "Architectures:");
  261. for (std::vector<string>::const_iterator a = archs.begin(); a != archs.end(); ++a)
  262. fprintf(output, " %s", a->c_str());
  263. fprintf(output, "\n");
  264. if (del.empty() == false)
  265. fprintf(output, "Remove: %s\n", del.c_str()+1);
  266. if (inst.empty() == false)
  267. fprintf(output, "Install: %s\n", inst.c_str()+1);
  268. if (Upgrade == true)
  269. fprintf(output, "Upgrade: yes\n");
  270. if (DistUpgrade == true)
  271. fprintf(output, "Dist-Upgrade: yes\n");
  272. if (AutoRemove == true)
  273. fprintf(output, "Autoremove: yes\n");
  274. if (_config->FindB("APT::Solver::Strict-Pinning", true) == false)
  275. fprintf(output, "Strict-Pinning: no\n");
  276. string solverpref("APT::Solver::");
  277. solverpref.append(_config->Find("APT::Solver", "internal")).append("::Preferences");
  278. if (_config->Exists(solverpref) == true)
  279. fprintf(output, "Preferences: %s\n", _config->Find(solverpref,"").c_str());
  280. fprintf(output, "\n");
  281. return true;
  282. }
  283. /*}}}*/
  284. // EDSP::ReadResponse - from the given file descriptor /*{{{*/
  285. bool EDSP::ReadResponse(int const input, pkgDepCache &Cache, OpProgress *Progress) {
  286. /* We build an map id to mmap offset here
  287. In theory we could use the offset as ID, but then VersionCount
  288. couldn't be used to create other versionmappings anymore and it
  289. would be too easy for a (buggy) solver to segfault APT… */
  290. unsigned long long const VersionCount = Cache.Head().VersionCount;
  291. unsigned long VerIdx[VersionCount];
  292. for (pkgCache::PkgIterator P = Cache.PkgBegin(); P.end() == false; ++P) {
  293. for (pkgCache::VerIterator V = P.VersionList(); V.end() == false; ++V)
  294. VerIdx[V->ID] = V.Index();
  295. Cache[P].Marked = true;
  296. Cache[P].Garbage = false;
  297. }
  298. FileFd in;
  299. in.OpenDescriptor(input, FileFd::ReadOnly);
  300. pkgTagFile response(&in, 100);
  301. pkgTagSection section;
  302. while (response.Step(section) == true) {
  303. std::string type;
  304. if (section.Exists("Install") == true)
  305. type = "Install";
  306. else if (section.Exists("Remove") == true)
  307. type = "Remove";
  308. else if (section.Exists("Progress") == true) {
  309. if (Progress != NULL) {
  310. string msg = section.FindS("Message");
  311. if (msg.empty() == true)
  312. msg = _("Prepare for receiving solution");
  313. Progress->SubProgress(100, msg, section.FindI("Percentage", 0));
  314. }
  315. continue;
  316. } else if (section.Exists("Error") == true) {
  317. std::string msg = SubstVar(SubstVar(section.FindS("Message"), "\n .\n", "\n\n"), "\n ", "\n");
  318. if (msg.empty() == true) {
  319. msg = _("External solver failed without a proper error message");
  320. _error->Error("%s", msg.c_str());
  321. } else
  322. _error->Error("External solver failed with: %s", msg.substr(0,msg.find('\n')).c_str());
  323. if (Progress != NULL)
  324. Progress->Done();
  325. std::cerr << "The solver encountered an error of type: " << section.FindS("Error") << std::endl;
  326. std::cerr << "The following information might help you to understand what is wrong:" << std::endl;
  327. std::cerr << msg << std::endl << std::endl;
  328. return false;
  329. } else if (section.Exists("Autoremove") == true)
  330. type = "Autoremove";
  331. else
  332. continue;
  333. size_t const id = section.FindULL(type.c_str(), VersionCount);
  334. if (id == VersionCount) {
  335. _error->Warning("Unable to parse %s request with id value '%s'!", type.c_str(), section.FindS(type.c_str()).c_str());
  336. continue;
  337. } else if (id > Cache.Head().VersionCount) {
  338. _error->Warning("ID value '%s' in %s request stanza is to high to refer to a known version!", section.FindS(type.c_str()).c_str(), type.c_str());
  339. continue;
  340. }
  341. pkgCache::VerIterator Ver(Cache.GetCache(), Cache.GetCache().VerP + VerIdx[id]);
  342. Cache.SetCandidateVersion(Ver);
  343. if (type == "Install")
  344. Cache.MarkInstall(Ver.ParentPkg(), false, 0, false);
  345. else if (type == "Remove")
  346. Cache.MarkDelete(Ver.ParentPkg(), false);
  347. else if (type == "Autoremove") {
  348. Cache[Ver.ParentPkg()].Marked = false;
  349. Cache[Ver.ParentPkg()].Garbage = true;
  350. }
  351. }
  352. return true;
  353. }
  354. /*}}}*/
  355. // EDSP::ReadLine - first line from the given file descriptor /*{{{*/
  356. // ---------------------------------------------------------------------
  357. /* Little helper method to read a complete line into a string. Similar to
  358. fgets but we need to use the low-level read() here as otherwise the
  359. listparser will be confused later on as mixing of fgets and read isn't
  360. a supported action according to the manpages and results are undefined */
  361. bool EDSP::ReadLine(int const input, std::string &line) {
  362. char one;
  363. ssize_t data = 0;
  364. line.erase();
  365. line.reserve(100);
  366. while ((data = read(input, &one, sizeof(one))) != -1) {
  367. if (data != 1)
  368. continue;
  369. if (one == '\n')
  370. return true;
  371. if (one == '\r')
  372. continue;
  373. if (line.empty() == true && isblank(one) != 0)
  374. continue;
  375. line += one;
  376. }
  377. return false;
  378. }
  379. /*}}}*/
  380. // EDSP::StringToBool - convert yes/no to bool /*{{{*/
  381. // ---------------------------------------------------------------------
  382. /* we are not as lazy as we are in the global StringToBool as we really
  383. only accept yes/no here - but we will ignore leading spaces */
  384. bool EDSP::StringToBool(char const *answer, bool const defValue) {
  385. for (; isspace(*answer) != 0; ++answer);
  386. if (strncasecmp(answer, "yes", 3) == 0)
  387. return true;
  388. else if (strncasecmp(answer, "no", 2) == 0)
  389. return false;
  390. else
  391. _error->Warning("Value '%s' is not a boolean 'yes' or 'no'!", answer);
  392. return defValue;
  393. }
  394. /*}}}*/
  395. // EDSP::ReadRequest - first stanza from the given file descriptor /*{{{*/
  396. bool EDSP::ReadRequest(int const input, std::list<std::string> &install,
  397. std::list<std::string> &remove, bool &upgrade,
  398. bool &distUpgrade, bool &autoRemove)
  399. {
  400. install.clear();
  401. remove.clear();
  402. upgrade = false;
  403. distUpgrade = false;
  404. autoRemove = false;
  405. std::string line;
  406. while (ReadLine(input, line) == true)
  407. {
  408. // Skip empty lines before request
  409. if (line.empty() == true)
  410. continue;
  411. // The first Tag must be a request, so search for it
  412. if (line.compare(0, 8, "Request:") != 0)
  413. continue;
  414. while (ReadLine(input, line) == true)
  415. {
  416. // empty lines are the end of the request
  417. if (line.empty() == true)
  418. return true;
  419. std::list<std::string> *request = NULL;
  420. if (line.compare(0, 8, "Install:") == 0)
  421. {
  422. line.erase(0, 8);
  423. request = &install;
  424. }
  425. else if (line.compare(0, 7, "Remove:") == 0)
  426. {
  427. line.erase(0, 7);
  428. request = &remove;
  429. }
  430. else if (line.compare(0, 8, "Upgrade:") == 0)
  431. upgrade = EDSP::StringToBool(line.c_str() + 9, false);
  432. else if (line.compare(0, 13, "Dist-Upgrade:") == 0)
  433. distUpgrade = EDSP::StringToBool(line.c_str() + 14, false);
  434. else if (line.compare(0, 11, "Autoremove:") == 0)
  435. autoRemove = EDSP::StringToBool(line.c_str() + 12, false);
  436. else if (line.compare(0, 13, "Architecture:") == 0)
  437. _config->Set("APT::Architecture", line.c_str() + 14);
  438. else if (line.compare(0, 14, "Architectures:") == 0)
  439. {
  440. std::string const archs = line.c_str() + 15;
  441. _config->Set("APT::Architectures", SubstVar(archs, " ", ","));
  442. }
  443. else
  444. _error->Warning("Unknown line in EDSP Request stanza: %s", line.c_str());
  445. if (request == NULL)
  446. continue;
  447. size_t end = line.length();
  448. do {
  449. size_t begin = line.rfind(' ');
  450. if (begin == std::string::npos)
  451. {
  452. request->push_back(line.substr(0, end));
  453. break;
  454. }
  455. else if (begin < end)
  456. request->push_back(line.substr(begin + 1, end));
  457. line.erase(begin);
  458. end = line.find_last_not_of(' ');
  459. } while (end != std::string::npos);
  460. }
  461. }
  462. return false;
  463. }
  464. /*}}}*/
  465. // EDSP::ApplyRequest - first stanza from the given file descriptor /*{{{*/
  466. bool EDSP::ApplyRequest(std::list<std::string> const &install,
  467. std::list<std::string> const &remove,
  468. pkgDepCache &Cache)
  469. {
  470. for (std::list<std::string>::const_iterator i = install.begin();
  471. i != install.end(); ++i) {
  472. pkgCache::PkgIterator P = Cache.FindPkg(*i);
  473. if (P.end() == true)
  474. _error->Warning("Package %s is not known, so can't be installed", i->c_str());
  475. else
  476. Cache.MarkInstall(P, false);
  477. }
  478. for (std::list<std::string>::const_iterator i = remove.begin();
  479. i != remove.end(); ++i) {
  480. pkgCache::PkgIterator P = Cache.FindPkg(*i);
  481. if (P.end() == true)
  482. _error->Warning("Package %s is not known, so can't be installed", i->c_str());
  483. else
  484. Cache.MarkDelete(P);
  485. }
  486. return true;
  487. }
  488. /*}}}*/
  489. // EDSP::WriteSolution - to the given file descriptor /*{{{*/
  490. bool EDSP::WriteSolution(pkgDepCache &Cache, FILE* output)
  491. {
  492. bool const Debug = _config->FindB("Debug::EDSP::WriteSolution", false);
  493. for (pkgCache::PkgIterator Pkg = Cache.PkgBegin(); Pkg.end() == false; ++Pkg)
  494. {
  495. if (Cache[Pkg].Delete() == true)
  496. {
  497. fprintf(output, "Remove: %d\n", Pkg.CurrentVer()->ID);
  498. if (Debug == true)
  499. fprintf(output, "Package: %s\nVersion: %s\n", Pkg.FullName().c_str(), Pkg.CurrentVer().VerStr());
  500. }
  501. else if (Cache[Pkg].NewInstall() == true || Cache[Pkg].Upgrade() == true)
  502. {
  503. fprintf(output, "Install: %d\n", Cache.GetCandidateVer(Pkg)->ID);
  504. if (Debug == true)
  505. fprintf(output, "Package: %s\nVersion: %s\n", Pkg.FullName().c_str(), Cache.GetCandidateVer(Pkg).VerStr());
  506. }
  507. else if (Cache[Pkg].Garbage == true)
  508. {
  509. fprintf(output, "Autoremove: %d\n", Pkg.CurrentVer()->ID);
  510. if (Debug == true)
  511. fprintf(output, "Package: %s\nVersion: %s\n", Pkg.FullName().c_str(), Pkg.CurrentVer().VerStr());
  512. }
  513. else
  514. continue;
  515. fprintf(output, "\n");
  516. }
  517. return true;
  518. }
  519. /*}}}*/
  520. // EDSP::WriteProgess - pulse to the given file descriptor /*{{{*/
  521. bool EDSP::WriteProgress(unsigned short const percent, const char* const message, FILE* output) {
  522. fprintf(output, "Progress: %s\n", TimeRFC1123(time(NULL)).c_str());
  523. fprintf(output, "Percentage: %d\n", percent);
  524. fprintf(output, "Message: %s\n\n", message);
  525. fflush(output);
  526. return true;
  527. }
  528. /*}}}*/
  529. // EDSP::WriteError - format an error message to be send to file descriptor /*{{{*/
  530. bool EDSP::WriteError(char const * const uuid, std::string const &message, FILE* output) {
  531. fprintf(output, "Error: %s\n", uuid);
  532. fprintf(output, "Message: %s\n\n", SubstVar(SubstVar(message, "\n\n", "\n.\n"), "\n", "\n ").c_str());
  533. return true;
  534. }
  535. /*}}}*/
  536. // EDSP::ExecuteSolver - fork requested solver and setup ipc pipes {{{*/
  537. pid_t EDSP::ExecuteSolver(const char* const solver, int * const solver_in, int * const solver_out, bool) {
  538. std::vector<std::string> const solverDirs = _config->FindVector("Dir::Bin::Solvers");
  539. std::string file;
  540. for (std::vector<std::string>::const_iterator dir = solverDirs.begin();
  541. dir != solverDirs.end(); ++dir) {
  542. file = flCombine(*dir, solver);
  543. if (RealFileExists(file.c_str()) == true)
  544. break;
  545. file.clear();
  546. }
  547. if (file.empty() == true)
  548. {
  549. _error->Error("Can't call external solver '%s' as it is not in a configured directory!", solver);
  550. return 0;
  551. }
  552. int external[4] = {-1, -1, -1, -1};
  553. if (pipe(external) != 0 || pipe(external + 2) != 0)
  554. {
  555. _error->Errno("Resolve", "Can't create needed IPC pipes for EDSP");
  556. return 0;
  557. }
  558. for (int i = 0; i < 4; ++i)
  559. SetCloseExec(external[i], true);
  560. pid_t Solver = ExecFork();
  561. if (Solver == 0) {
  562. dup2(external[0], STDIN_FILENO);
  563. dup2(external[3], STDOUT_FILENO);
  564. const char* calling[2] = { file.c_str(), 0 };
  565. execv(calling[0], (char**) calling);
  566. std::cerr << "Failed to execute solver '" << solver << "'!" << std::endl;
  567. _exit(100);
  568. }
  569. close(external[0]);
  570. close(external[3]);
  571. if (WaitFd(external[1], true, 5) == false)
  572. {
  573. _error->Errno("Resolve", "Timed out while Waiting on availability of solver stdin");
  574. return 0;
  575. }
  576. *solver_in = external[1];
  577. *solver_out = external[2];
  578. return Solver;
  579. }
  580. bool EDSP::ExecuteSolver(const char* const solver, int *solver_in, int *solver_out) {
  581. if (ExecuteSolver(solver, solver_in, solver_out, true) == 0)
  582. return false;
  583. return true;
  584. }
  585. /*}}}*/
  586. // EDSP::ResolveExternal - resolve problems by asking external for help {{{*/
  587. bool EDSP::ResolveExternal(const char* const solver, pkgDepCache &Cache,
  588. bool const upgrade, bool const distUpgrade,
  589. bool const autoRemove, OpProgress *Progress) {
  590. int solver_in, solver_out;
  591. pid_t const solver_pid = EDSP::ExecuteSolver(solver, &solver_in, &solver_out, true);
  592. if (solver_pid == 0)
  593. return false;
  594. FILE* output = fdopen(solver_in, "w");
  595. if (output == NULL)
  596. return _error->Errno("Resolve", "fdopen on solver stdin failed");
  597. if (Progress != NULL)
  598. Progress->OverallProgress(0, 100, 5, _("Execute external solver"));
  599. EDSP::WriteRequest(Cache, output, upgrade, distUpgrade, autoRemove, Progress);
  600. if (Progress != NULL)
  601. Progress->OverallProgress(5, 100, 20, _("Execute external solver"));
  602. EDSP::WriteScenario(Cache, output, Progress);
  603. fclose(output);
  604. if (Progress != NULL)
  605. Progress->OverallProgress(25, 100, 75, _("Execute external solver"));
  606. if (EDSP::ReadResponse(solver_out, Cache, Progress) == false)
  607. return false;
  608. return ExecWait(solver_pid, solver);
  609. }
  610. /*}}}*/