edsp.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. // -*- mode: cpp; mode: fold -*-
  2. /** Description \file edsp.h {{{
  3. ######################################################################
  4. Set of methods to help writing and reading everything needed for EDSP
  5. with the notable exception of reading a scenario for conversion into
  6. a Cache as this is handled by edsp interface for listparser and friends
  7. ##################################################################### */
  8. /*}}}*/
  9. #ifndef PKGLIB_EDSP_H
  10. #define PKGLIB_EDSP_H
  11. #include <apt-pkg/cacheset.h>
  12. #include <apt-pkg/pkgcache.h>
  13. #include <apt-pkg/cacheiterators.h>
  14. #include <apt-pkg/macros.h>
  15. #include <stdio.h>
  16. #include <list>
  17. #include <string>
  18. #include <vector>
  19. #ifndef APT_8_CLEANER_HEADERS
  20. #include <apt-pkg/depcache.h>
  21. #include <apt-pkg/progress.h>
  22. #endif
  23. class pkgDepCache;
  24. class OpProgress;
  25. namespace EDSP /*{{{*/
  26. {
  27. namespace Request
  28. {
  29. enum Flags
  30. {
  31. AUTOREMOVE = (1 << 0), /*!< removal of unneeded packages should be performed */
  32. UPGRADE_ALL = (1 << 1), /*!< upgrade all installed packages, like 'apt-get full-upgrade' without forbid flags */
  33. FORBID_NEW_INSTALL = (1 << 2), /*!< forbid the resolver to install new packages */
  34. FORBID_REMOVE = (1 << 3), /*!< forbid the resolver to remove packages */
  35. };
  36. }
  37. /** \brief creates the EDSP request stanza
  38. *
  39. * In the EDSP protocol the first thing send to the resolver is a stanza
  40. * encoding the request. This method will write this stanza by looking at
  41. * the given Cache and requests the installation of all packages which were
  42. * marked for installation in it (equally for remove).
  43. *
  44. * \param Cache in which the request is encoded
  45. * \param output is written to this "file"
  46. * \param flags effecting the request documented in #EDSP::Request::Flags
  47. * \param Progress is an instance to report progress to
  48. *
  49. * \return true if request was composed successfully, otherwise false
  50. */
  51. bool WriteRequest(pkgDepCache &Cache, FileFd &output,
  52. unsigned int const flags = 0,
  53. OpProgress *Progress = NULL);
  54. bool WriteRequest(pkgDepCache &Cache, FILE* output,
  55. bool const upgrade = false,
  56. bool const distUpgrade = false,
  57. bool const autoRemove = false,
  58. OpProgress *Progress = NULL) APT_DEPRECATED_MSG("Use FileFd-based interface instead");
  59. /** \brief creates the scenario representing the package universe
  60. *
  61. * After the request all known information about a package are send
  62. * to the solver. The output looks similar to a Packages or status file
  63. *
  64. * All packages and version included in this Cache are send, even if
  65. * it doesn't make sense from an APT resolver point of view like versions
  66. * with a negative pin to enable the solver to propose even that as a
  67. * solution or at least to be able to give a hint what can be done to
  68. * satisfy a request.
  69. *
  70. * \param Cache is the known package universe
  71. * \param output is written to this "file"
  72. * \param Progress is an instance to report progress to
  73. *
  74. * \return true if universe was composed successfully, otherwise false
  75. */
  76. bool WriteScenario(pkgDepCache &Cache, FileFd &output, OpProgress *Progress = NULL);
  77. bool WriteScenario(pkgDepCache &Cache, FILE* output, OpProgress *Progress = NULL) APT_DEPRECATED_MSG("Use FileFd-based interface instead");
  78. /** \brief creates a limited scenario representing the package universe
  79. *
  80. * This method works similar to #WriteScenario as it works in the same
  81. * way but doesn't send the complete universe to the solver but only
  82. * packages included in the pkgset which will have only dependencies
  83. * on packages which are in the given set. All other dependencies will
  84. * be removed, so that this method can be used to create testcases
  85. *
  86. * \param Cache is the known package universe
  87. * \param output is written to this "file"
  88. * \param pkgset is a set of packages the universe should be limited to
  89. * \param Progress is an instance to report progress to
  90. *
  91. * \return true if universe was composed successfully, otherwise false
  92. */
  93. bool WriteLimitedScenario(pkgDepCache &Cache, FileFd &output,
  94. std::vector<bool> const &pkgset,
  95. OpProgress *Progress = NULL);
  96. bool WriteLimitedScenario(pkgDepCache &Cache, FILE* output,
  97. APT::PackageSet const &pkgset,
  98. OpProgress *Progress = NULL) APT_DEPRECATED_MSG("Use FileFd-based interface instead");
  99. /** \brief waits and acts on the information returned from the solver
  100. *
  101. * This method takes care of interpreting whatever the solver sends
  102. * through the standard output like a solution, progress or an error.
  103. * The main thread should hand his control over to this method to
  104. * wait for the solver to finish the given task. The file descriptor
  105. * used as input is completely consumed and closed by the method.
  106. *
  107. * \param input file descriptor with the response from the solver
  108. * \param Cache the solution should be applied on if any
  109. * \param Progress is an instance to report progress to
  110. *
  111. * \return true if a solution is found and applied correctly, otherwise false
  112. */
  113. bool ReadResponse(int const input, pkgDepCache &Cache, OpProgress *Progress = NULL);
  114. /** \brief search and read the request stanza for action later
  115. *
  116. * This method while ignore the input up to the point it finds the
  117. * Request: line as an indicator for the Request stanza.
  118. * The request is stored in the parameters install and remove then,
  119. * as the cache isn't build yet as the scenario follows the request.
  120. *
  121. * \param input file descriptor with the edsp input for the solver
  122. * \param[out] install is a list which gets populated with requested installs
  123. * \param[out] remove is a list which gets populated with requested removals
  124. * \param[out] upgrade is true if it is a request like apt-get upgrade
  125. * \param[out] distUpgrade is true if it is a request like apt-get dist-upgrade
  126. * \param[out] autoRemove is true if removal of uneeded packages should be performed
  127. *
  128. * \return true if the request could be found and worked on, otherwise false
  129. */
  130. bool ReadRequest(int const input, std::list<std::string> &install,
  131. std::list<std::string> &remove, unsigned int &flags);
  132. APT_DEPRECATED_MSG("use the flag-based version instead") bool ReadRequest(int const input, std::list<std::string> &install,
  133. std::list<std::string> &remove, bool &upgrade,
  134. bool &distUpgrade, bool &autoRemove);
  135. /** \brief takes the request lists and applies it on the cache
  136. *
  137. * The lists as created by #ReadRequest will be used to find the
  138. * packages in question and mark them for install/remove.
  139. * No solving is done and no auto-install/-remove.
  140. *
  141. * \param install is a list of packages to mark for installation
  142. * \param remove is a list of packages to mark for removal
  143. * \param Cache is there the markers should be set
  144. *
  145. * \return false if the request couldn't be applied, true otherwise
  146. */
  147. bool ApplyRequest(std::list<std::string> const &install,
  148. std::list<std::string> const &remove,
  149. pkgDepCache &Cache);
  150. /** \brief formats a solution stanza for the given version
  151. *
  152. * EDSP uses a simple format for reporting solutions:
  153. * A single required field name with an ID as value.
  154. * Additional fields might appear as debug aids.
  155. *
  156. * \param output to write the stanza forming the solution to
  157. * \param Type of the stanza, used as field name
  158. * \param Ver this stanza applies to
  159. *
  160. * \return true if stanza could be written, otherwise false
  161. */
  162. bool WriteSolutionStanza(FileFd &output, char const * const Type, pkgCache::VerIterator const &Ver);
  163. bool WriteSolution(pkgDepCache &Cache, FILE* output) APT_DEPRECATED_MSG("Use FileFd-based single-stanza interface instead");
  164. /** \brief sends a progress report
  165. *
  166. * \param percent of the solving completed
  167. * \param message the solver wants the user to see
  168. * \param output the front-end listens for progress report
  169. */
  170. bool WriteProgress(unsigned short const percent, const char* const message, FileFd &output);
  171. bool WriteProgress(unsigned short const percent, const char* const message, FILE* output) APT_DEPRECATED_MSG("Use FileFd-based interface instead");
  172. /** \brief sends an error report
  173. *
  174. * Solvers are expected to execute successfully even if
  175. * they were unable to calculate a solution for a given task.
  176. * Obviously they can't send a solution through, so this
  177. * methods deals with formatting an error message correctly
  178. * so that the front-ends can receive and display it.
  179. *
  180. * The first line of the message should be a short description
  181. * of the error so it can be used for dialog titles or alike
  182. *
  183. * \param uuid of this error message
  184. * \param message is free form text to describe the error
  185. * \param output the front-end listens for error messages
  186. */
  187. bool WriteError(char const * const uuid, std::string const &message, FileFd &output);
  188. bool WriteError(char const * const uuid, std::string const &message, FILE* output) APT_DEPRECATED_MSG("Use FileFd-based interface instead");
  189. /** \brief executes the given solver and returns the pipe ends
  190. *
  191. * The given solver is executed if it can be found in one of the
  192. * configured directories and setup for it is performed.
  193. *
  194. * \param solver to execute
  195. * \param[out] solver_in will be the stdin of the solver
  196. * \param[out] solver_out will be the stdout of the solver
  197. *
  198. * \return PID of the started solver or 0 if failure occurred
  199. */
  200. pid_t ExecuteSolver(const char* const solver, int * const solver_in, int * const solver_out, bool /*overload*/);
  201. APT_DEPRECATED_MSG("add a dummy bool parameter to use the overload returning a pid_t") bool ExecuteSolver(const char* const solver, int *solver_in, int *solver_out);
  202. /** \brief call an external resolver to handle the request
  203. *
  204. * This method wraps all the methods above to call an external solver
  205. *
  206. * \param solver to execute
  207. * \param Cache with the problem and as universe to work in
  208. * \param flags effecting the request documented in #EDSP::Request::Flags
  209. * \param Progress is an instance to report progress to
  210. *
  211. * \return true if the solver has successfully solved the problem,
  212. * otherwise false
  213. */
  214. bool ResolveExternal(const char* const solver, pkgDepCache &Cache,
  215. unsigned int const flags = 0,
  216. OpProgress *Progress = NULL);
  217. APT_DEPRECATED_MSG("use the flag-based version instead") bool ResolveExternal(const char* const solver, pkgDepCache &Cache,
  218. bool const upgrade, bool const distUpgrade,
  219. bool const autoRemove, OpProgress *Progress = NULL);
  220. }
  221. /*}}}*/
  222. class pkgPackageManager;
  223. namespace EIPP /*{{{*/
  224. {
  225. namespace Request
  226. {
  227. enum Flags
  228. {
  229. IMMEDIATE_CONFIGURATION_ALL = (1 << 0), /*!< try to keep the least amount of packages unconfigured as possible at all times */
  230. NO_IMMEDIATE_CONFIGURATION = (1 << 1), /*!< do not perform immediate configuration at all */
  231. ALLOW_TEMPORARY_REMOVE_OF_ESSENTIALS = (1 << 2), /*!< just as the name suggests, very special case and dangerous! */
  232. };
  233. }
  234. APT_HIDDEN bool WriteRequest(pkgDepCache &Cache, FileFd &output,
  235. unsigned int const flags, OpProgress * const Progress);
  236. APT_HIDDEN bool WriteScenario(pkgDepCache &Cache, FileFd &output,
  237. OpProgress * const Progress);
  238. APT_HIDDEN bool OrderInstall(char const * const planner, pkgPackageManager * const PM,
  239. unsigned int const version, OpProgress * const Progress);
  240. APT_HIDDEN bool ReadResponse(int const input, pkgPackageManager * const PM,
  241. OpProgress * const Progress);
  242. enum class PKG_ACTION
  243. {
  244. NOOP,
  245. INSTALL,
  246. REINSTALL,
  247. REMOVE
  248. };
  249. bool ReadRequest(int const input,
  250. std::list<std::pair<std::string,PKG_ACTION>> &actions,
  251. unsigned int &flags);
  252. bool ApplyRequest(std::list<std::pair<std::string,PKG_ACTION>> &actions,
  253. pkgDepCache &Cache);
  254. }
  255. /*}}}*/
  256. #endif