edsp.cc 22 KB

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