edsp.cc 8.9 KB

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