edsp.cc 23 KB

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