edsp.cc 20 KB

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