edsp.cc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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. // EDSP::WriteScenario - to the given file descriptor /*{{{*/
  19. bool EDSP::WriteScenario(pkgDepCache &Cache, FILE* output)
  20. {
  21. // we could use pkgCache::DepType and ::Priority, but these would be lokalized strings…
  22. const char * const PrioMap[] = {0, "important", "required", "standard",
  23. "optional", "extra"};
  24. const char * const DepMap[] = {"", "Depends", "PreDepends", "Suggests",
  25. "Recommends" , "Conflicts", "Replaces",
  26. "Obsoletes", "Breaks", "Enhances"};
  27. for (pkgCache::PkgIterator Pkg = Cache.PkgBegin(); Pkg.end() == false; ++Pkg)
  28. {
  29. for (pkgCache::VerIterator Ver = Pkg.VersionList(); Ver.end() == false; ++Ver)
  30. {
  31. fprintf(output, "Package: %s\n", Pkg.Name());
  32. fprintf(output, "Architecture: %s\n", Ver.Arch());
  33. fprintf(output, "Version: %s\n", Ver.VerStr());
  34. if (Pkg.CurrentVer() == Ver)
  35. fprintf(output, "Installed: yes\n");
  36. if (Pkg->SelectedState == pkgCache::State::Hold)
  37. fprintf(output, "Hold: yes\n");
  38. fprintf(output, "APT-ID: %u\n", Ver->ID);
  39. fprintf(output, "Priority: %s\n", PrioMap[Ver->Priority]);
  40. if ((Pkg->Flags & pkgCache::Flag::Essential) == pkgCache::Flag::Essential)
  41. fprintf(output, "Essential: yes\n");
  42. fprintf(output, "Section: %s\n", Ver.Section());
  43. if (Ver->MultiArch == pkgCache::Version::Allowed || Ver->MultiArch == pkgCache::Version::AllAllowed)
  44. fprintf(output, "Multi-Arch: allowed\n");
  45. else if (Ver->MultiArch == pkgCache::Version::Foreign || Ver->MultiArch == pkgCache::Version::AllForeign)
  46. fprintf(output, "Multi-Arch: foreign\n");
  47. else if (Ver->MultiArch == pkgCache::Version::Same)
  48. fprintf(output, "Multi-Arch: same\n");
  49. signed short Pin = std::numeric_limits<signed short>::min();
  50. for (pkgCache::VerFileIterator File = Ver.FileList(); File.end() == false; ++File) {
  51. signed short const p = Cache.GetPolicy().GetPriority(File.File());
  52. if (Pin < p)
  53. Pin = p;
  54. }
  55. fprintf(output, "APT-Pin: %d\n", Pin);
  56. if (Cache.GetCandidateVer(Pkg) == Ver)
  57. fprintf(output, "APT-Candidate: yes\n");
  58. if ((Cache[Pkg].Flags & pkgCache::Flag::Auto) == pkgCache::Flag::Auto)
  59. fprintf(output, "APT-Automatic: yes\n");
  60. std::string dependencies[pkgCache::Dep::Enhances + 1];
  61. bool orGroup = false;
  62. for (pkgCache::DepIterator Dep = Ver.DependsList(); Dep.end() == false; ++Dep)
  63. {
  64. // Ignore implicit dependencies for multiarch here
  65. if (strcmp(Pkg.Arch(), Dep.TargetPkg().Arch()) != 0)
  66. continue;
  67. if (orGroup == false)
  68. dependencies[Dep->Type].append(", ");
  69. dependencies[Dep->Type].append(Dep.TargetPkg().Name());
  70. if (Dep->Version != 0)
  71. dependencies[Dep->Type].append(" (").append(pkgCache::CompTypeDeb(Dep->CompareOp)).append(" ").append(Dep.TargetVer()).append(")");
  72. if ((Dep->CompareOp & pkgCache::Dep::Or) == pkgCache::Dep::Or)
  73. {
  74. dependencies[Dep->Type].append(" | ");
  75. orGroup = true;
  76. }
  77. else
  78. orGroup = false;
  79. }
  80. for (int i = 1; i < pkgCache::Dep::Enhances + 1; ++i)
  81. if (dependencies[i].empty() == false)
  82. fprintf(output, "%s: %s\n", DepMap[i], dependencies[i].c_str()+2);
  83. string provides;
  84. for (pkgCache::PrvIterator Prv = Ver.ProvidesList(); Prv.end() == false; ++Prv)
  85. {
  86. // Ignore implicit provides for multiarch here
  87. if (strcmp(Pkg.Arch(), Prv.ParentPkg().Arch()) != 0 || strcmp(Pkg.Name(),Prv.Name()) == 0)
  88. continue;
  89. provides.append(", ").append(Prv.Name());
  90. }
  91. if (provides.empty() == false)
  92. fprintf(output, "Provides: %s\n", provides.c_str()+2);
  93. fprintf(output, "\n");
  94. }
  95. }
  96. return true;
  97. }
  98. /*}}}*/
  99. // EDSP::WriteRequest - to the given file descriptor /*{{{*/
  100. bool EDSP::WriteRequest(pkgDepCache &Cache, FILE* output, bool const Upgrade,
  101. bool const DistUpgrade, bool const AutoRemove)
  102. {
  103. string del, inst;
  104. for (pkgCache::PkgIterator Pkg = Cache.PkgBegin(); Pkg.end() == false; ++Pkg)
  105. {
  106. string* req;
  107. if (Cache[Pkg].Delete() == true)
  108. req = &del;
  109. else if (Cache[Pkg].NewInstall() == true || Cache[Pkg].Upgrade() == true)
  110. req = &inst;
  111. else
  112. continue;
  113. req->append(" ").append(Pkg.FullName());
  114. }
  115. fprintf(output, "Request: EDSP 0.2\n");
  116. if (del.empty() == false)
  117. fprintf(output, "Remove: %s\n", del.c_str()+1);
  118. if (inst.empty() == false)
  119. fprintf(output, "Install: %s\n", inst.c_str()+1);
  120. if (Upgrade == true)
  121. fprintf(output, "Upgrade: yes\n");
  122. if (DistUpgrade == true)
  123. fprintf(output, "Dist-Upgrade: yes\n");
  124. if (AutoRemove == true)
  125. fprintf(output, "Autoremove: yes\n");
  126. if (_config->FindB("APT::Solver::Strict-Pinning", true) == false)
  127. fprintf(output, "Strict-Pinning: no\n");
  128. string solverpref("APT::Solver::");
  129. solverpref.append(_config->Find("APT::Solver::Name", "internal")).append("::Preferences");
  130. if (_config->Exists(solverpref) == true)
  131. fprintf(output, "Preferences: %s\n", _config->Find(solverpref,"").c_str());
  132. fprintf(output, "\n");
  133. return true;
  134. }
  135. /*}}}*/
  136. // EDSP::ReadResponse - from the given file descriptor /*{{{*/
  137. bool EDSP::ReadResponse(int const input, pkgDepCache &Cache) {
  138. FileFd in;
  139. in.OpenDescriptor(input, FileFd::ReadOnly);
  140. pkgTagFile response(&in);
  141. pkgTagSection section;
  142. while (response.Step(section) == true) {
  143. std::string type;
  144. if (section.Exists("Install") == true)
  145. type = "Install";
  146. else if (section.Exists("Remove") == true)
  147. type = "Remove";
  148. //FIXME: handle progress
  149. else
  150. continue;
  151. int const id = section.FindI(type.c_str(), -1);
  152. if (id == -1)
  153. return _error->Error("Unable to parse %s request!", type.c_str());
  154. //FIXME: find version by id and mark it correctly
  155. }
  156. return true;
  157. }
  158. /*}}}*/
  159. // EDSP::ReadLine - first line from the given file descriptor /*{{{*/
  160. // ---------------------------------------------------------------------
  161. /* Little helper method to read a complete line into a string. Similar to
  162. fgets but we need to use the low-level read() here as otherwise the
  163. listparser will be confused later on as mixing of fgets and read isn't
  164. a supported action according to the manpages and results are undefined */
  165. bool EDSP::ReadLine(int const input, std::string &line) {
  166. char one;
  167. ssize_t data = 0;
  168. line.erase();
  169. line.reserve(100);
  170. while ((data = read(input, &one, sizeof(one))) != -1) {
  171. if (data != 1)
  172. continue;
  173. if (one == '\n')
  174. return true;
  175. if (one == '\r')
  176. continue;
  177. if (line.empty() == true && isblank(one) != 0)
  178. continue;
  179. line += one;
  180. }
  181. return false;
  182. }
  183. /*}}}*/
  184. // EDSP::StringToBool - convert yes/no to bool /*{{{*/
  185. // ---------------------------------------------------------------------
  186. /* we are not as lazy as we are in the global StringToBool as we really
  187. only accept yes/no here - but we will ignore leading spaces */
  188. bool EDSP::StringToBool(char const *answer, bool const defValue) {
  189. for (; isspace(*answer) != 0; ++answer);
  190. if (strncasecmp(answer, "yes", 3) == 0)
  191. return true;
  192. else if (strncasecmp(answer, "no", 2) == 0)
  193. return false;
  194. else
  195. _error->Warning("Value '%s' is not a boolean 'yes' or 'no'!", answer);
  196. return defValue;
  197. }
  198. /*}}}*/
  199. // EDSP::ReadRequest - first stanza from the given file descriptor /*{{{*/
  200. bool EDSP::ReadRequest(int const input, std::list<std::string> &install,
  201. std::list<std::string> &remove, bool &upgrade,
  202. bool &distUpgrade, bool &autoRemove)
  203. {
  204. install.clear();
  205. remove.clear();
  206. upgrade = false;
  207. distUpgrade = false;
  208. autoRemove = false;
  209. std::string line;
  210. while (ReadLine(input, line) == true)
  211. {
  212. // Skip empty lines before request
  213. if (line.empty() == true)
  214. continue;
  215. // The first Tag must be a request, so search for it
  216. if (line.compare(0, 8, "Request:") != 0)
  217. continue;
  218. while (ReadLine(input, line) == true)
  219. {
  220. // empty lines are the end of the request
  221. if (line.empty() == true)
  222. return true;
  223. std::list<std::string> *request = NULL;
  224. if (line.compare(0, 8, "Install:") == 0)
  225. {
  226. line.erase(0, 8);
  227. request = &install;
  228. }
  229. else if (line.compare(0, 7, "Remove:") == 0)
  230. {
  231. line.erase(0, 7);
  232. request = &remove;
  233. }
  234. else if (line.compare(0, 8, "Upgrade:") == 0)
  235. upgrade = EDSP::StringToBool(line.c_str() + 9, false);
  236. else if (line.compare(0, 13, "Dist-Upgrade:") == 0)
  237. distUpgrade = EDSP::StringToBool(line.c_str() + 14, false);
  238. else if (line.compare(0, 11, "Autoremove:") == 0)
  239. autoRemove = EDSP::StringToBool(line.c_str() + 12, false);
  240. else
  241. _error->Warning("Unknown line in EDSP Request stanza: %s", line.c_str());
  242. if (request == NULL)
  243. continue;
  244. size_t end = line.length();
  245. do {
  246. size_t begin = line.rfind(' ');
  247. if (begin == std::string::npos)
  248. {
  249. request->push_back(line.substr(0, end));
  250. break;
  251. }
  252. else if (begin < end)
  253. request->push_back(line.substr(begin + 1, end));
  254. line.erase(begin);
  255. end = line.find_last_not_of(' ');
  256. } while (end != std::string::npos);
  257. }
  258. }
  259. return false;
  260. }
  261. /*}}}*/
  262. // EDSP::ApplyRequest - first stanza from the given file descriptor /*{{{*/
  263. bool EDSP::ApplyRequest(std::list<std::string> const &install,
  264. std::list<std::string> const &remove,
  265. pkgDepCache &Cache)
  266. {
  267. for (std::list<std::string>::const_iterator i = install.begin();
  268. i != install.end(); ++i)
  269. Cache.MarkInstall(Cache.FindPkg(*i), false);
  270. for (std::list<std::string>::const_iterator i = remove.begin();
  271. i != remove.end(); ++i)
  272. Cache.MarkDelete(Cache.FindPkg(*i));
  273. return true;
  274. }
  275. /*}}}*/
  276. // EDSP::WriteSolution - to the given file descriptor /*{{{*/
  277. bool EDSP::WriteSolution(pkgDepCache &Cache, FILE* output)
  278. {
  279. bool const Debug = _config->FindB("Debug::EDSP::WriteSolution", false);
  280. for (pkgCache::PkgIterator Pkg = Cache.PkgBegin(); Pkg.end() == false; ++Pkg)
  281. {
  282. if (Cache[Pkg].Delete() == true)
  283. fprintf(output, "Remove: %d\n", Cache.GetCandidateVer(Pkg)->ID);
  284. else if (Cache[Pkg].NewInstall() == true || Cache[Pkg].Upgrade() == true)
  285. fprintf(output, "Install: %d\n", Cache.GetCandidateVer(Pkg)->ID);
  286. else
  287. continue;
  288. if (Debug == true)
  289. fprintf(output, "Package: %s\nVersion: %s\n", Pkg.FullName().c_str(), Cache.GetCandidateVer(Pkg).VerStr());
  290. fprintf(output, "\n");
  291. }
  292. return true;
  293. }
  294. /*}}}*/
  295. bool EDSP::WriteError(std::string const &message, FILE* output) { return false; }