statechanges.cc 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. #include <apt-pkg/pkgcache.h>
  2. #include <apt-pkg/cacheset.h>
  3. #include <apt-pkg/debsystem.h>
  4. #include <apt-pkg/fileutl.h>
  5. #include <apt-pkg/statechanges.h>
  6. #include <apt-pkg/prettyprinters.h>
  7. #include <algorithm>
  8. #include <memory>
  9. namespace APT
  10. {
  11. class StateChanges::Private
  12. {
  13. public:
  14. APT::VersionVector hold;
  15. APT::VersionVector unhold;
  16. APT::VersionVector install;
  17. APT::VersionVector deinstall;
  18. APT::VersionVector purge;
  19. APT::VersionVector error;
  20. };
  21. #define APT_GETTERSETTER(Name, Container) \
  22. void StateChanges::Name(pkgCache::VerIterator const &Ver) \
  23. { \
  24. Container.push_back(Ver); \
  25. }\
  26. APT::VersionVector& StateChanges::Name() \
  27. { \
  28. return Container; \
  29. }
  30. APT_GETTERSETTER(Hold, d->hold)
  31. APT_GETTERSETTER(Unhold, d->unhold)
  32. APT_GETTERSETTER(Install, d->install)
  33. APT_GETTERSETTER(Remove, d->deinstall)
  34. APT_GETTERSETTER(Purge, d->purge)
  35. #undef APT_GETTERSETTER
  36. APT::VersionVector& StateChanges::Error()
  37. {
  38. return d->error;
  39. }
  40. void StateChanges::clear()
  41. {
  42. d->hold.clear();
  43. d->unhold.clear();
  44. d->install.clear();
  45. d->deinstall.clear();
  46. d->purge.clear();
  47. d->error.clear();
  48. }
  49. bool StateChanges::empty() const
  50. {
  51. return d->hold.empty() &&
  52. d->unhold.empty() &&
  53. d->install.empty() &&
  54. d->deinstall.empty() &&
  55. d->purge.empty() &&
  56. d->error.empty();
  57. }
  58. bool StateChanges::Save(bool const DiscardOutput)
  59. {
  60. bool const Debug = _config->FindB("Debug::pkgDpkgPm", false);
  61. d->error.clear();
  62. if (d->hold.empty() && d->unhold.empty() && d->install.empty() && d->deinstall.empty() && d->purge.empty())
  63. return true;
  64. std::vector<std::string> Args = debSystem::GetDpkgBaseCommand();
  65. // ensure dpkg knows about the package so that it keeps the status we set
  66. if (d->hold.empty() == false || d->install.empty() == false)
  67. {
  68. APT::VersionVector makeDpkgAvailable;
  69. auto const notInstalled = [](pkgCache::VerIterator const &V) { return V.ParentPkg()->CurrentVer == 0; };
  70. std::copy_if(d->hold.begin(), d->hold.end(), std::back_inserter(makeDpkgAvailable), notInstalled);
  71. std::copy_if(d->install.begin(), d->install.end(), std::back_inserter(makeDpkgAvailable), notInstalled);
  72. if (makeDpkgAvailable.empty() == false)
  73. {
  74. auto const BaseArgs = Args.size();
  75. Args.push_back("--merge-avail");
  76. // FIXME: supported only since 1.17.7 in dpkg
  77. Args.push_back("-");
  78. int dummyAvail = -1;
  79. if (Debug)
  80. {
  81. for (auto const &V: makeDpkgAvailable)
  82. {
  83. std::clog << "echo 'Dummy record for " << V.ParentPkg().FullName(false) << "' | ";
  84. std::copy(Args.begin(), Args.end(), std::ostream_iterator<std::string>(std::clog, " "));
  85. std::clog << std::endl;
  86. }
  87. }
  88. else
  89. {
  90. pid_t const dpkgMergeAvail = debSystem::ExecDpkg(Args, &dummyAvail, nullptr, true);
  91. FILE* dpkg = fdopen(dummyAvail, "w");
  92. for (auto const &V: makeDpkgAvailable)
  93. fprintf(dpkg, "Package: %s\nVersion: 0~\nArchitecture: %s\nMaintainer: Dummy Example <dummy@example.org>\n"
  94. "Description: dummy package record\n A record is needed to put a package on hold, so here it is.\n\n", V.ParentPkg().Name(), V.Arch());
  95. fclose(dpkg);
  96. ExecWait(dpkgMergeAvail, "dpkg --merge-avail", true);
  97. }
  98. Args.erase(Args.begin() + BaseArgs, Args.end());
  99. }
  100. }
  101. bool const dpkgMultiArch = _system->MultiArchSupported();
  102. Args.push_back("--set-selections");
  103. if (Debug)
  104. {
  105. std::string state;
  106. auto const dpkgName = [&](pkgCache::VerIterator const &V) {
  107. pkgCache::PkgIterator P = V.ParentPkg();
  108. if (strcmp(V.Arch(), "none") == 0)
  109. ioprintf(std::clog, "echo '%s %s' | ", P.Name(), state.c_str());
  110. else if (dpkgMultiArch == false)
  111. ioprintf(std::clog, "echo '%s %s' | ", P.FullName(true).c_str(), state.c_str());
  112. else
  113. ioprintf(std::clog, "echo '%s:%s %s' | ", P.Name(), V.Arch(), state.c_str());
  114. std::copy(Args.begin(), Args.end(), std::ostream_iterator<std::string>(std::clog, " "));
  115. std::clog << std::endl;
  116. };
  117. for (auto const &V: d->unhold)
  118. {
  119. if (V.ParentPkg()->CurrentVer != 0)
  120. state = "install";
  121. else
  122. state = "deinstall";
  123. dpkgName(V);
  124. }
  125. if (d->purge.empty() == false)
  126. {
  127. state = "purge";
  128. std::for_each(d->purge.begin(), d->purge.end(), dpkgName);
  129. }
  130. if (d->deinstall.empty() == false)
  131. {
  132. state = "deinstall";
  133. std::for_each(d->deinstall.begin(), d->deinstall.end(), dpkgName);
  134. }
  135. if (d->hold.empty() == false)
  136. {
  137. state = "hold";
  138. std::for_each(d->hold.begin(), d->hold.end(), dpkgName);
  139. }
  140. if (d->install.empty() == false)
  141. {
  142. state = "install";
  143. std::for_each(d->install.begin(), d->install.end(), dpkgName);
  144. }
  145. }
  146. else
  147. {
  148. int selections = -1;
  149. pid_t const dpkgSelections = debSystem::ExecDpkg(Args, &selections, nullptr, DiscardOutput);
  150. FILE* dpkg = fdopen(selections, "w");
  151. std::string state;
  152. auto const dpkgName = [&](pkgCache::VerIterator const &V) {
  153. pkgCache::PkgIterator P = V.ParentPkg();
  154. if (strcmp(V.Arch(), "none") == 0)
  155. fprintf(dpkg, "%s %s\n", P.Name(), state.c_str());
  156. else if (dpkgMultiArch == false)
  157. fprintf(dpkg, "%s %s\n", P.FullName(true).c_str(), state.c_str());
  158. else
  159. fprintf(dpkg, "%s:%s %s\n", P.Name(), V.Arch(), state.c_str());
  160. };
  161. for (auto const &V: d->unhold)
  162. {
  163. if (V.ParentPkg()->CurrentVer != 0)
  164. state = "install";
  165. else
  166. state = "deinstall";
  167. dpkgName(V);
  168. }
  169. if (d->purge.empty() == false)
  170. {
  171. state = "purge";
  172. std::for_each(d->purge.begin(), d->purge.end(), dpkgName);
  173. }
  174. if (d->deinstall.empty() == false)
  175. {
  176. state = "deinstall";
  177. std::for_each(d->deinstall.begin(), d->deinstall.end(), dpkgName);
  178. }
  179. if (d->hold.empty() == false)
  180. {
  181. state = "hold";
  182. std::for_each(d->hold.begin(), d->hold.end(), dpkgName);
  183. }
  184. if (d->install.empty() == false)
  185. {
  186. state = "install";
  187. std::for_each(d->install.begin(), d->install.end(), dpkgName);
  188. }
  189. fclose(dpkg);
  190. if (ExecWait(dpkgSelections, "dpkg --set-selections") == false)
  191. {
  192. std::move(d->purge.begin(), d->purge.end(), std::back_inserter(d->error));
  193. d->purge.clear();
  194. std::move(d->deinstall.begin(), d->deinstall.end(), std::back_inserter(d->error));
  195. d->deinstall.clear();
  196. std::move(d->hold.begin(), d->hold.end(), std::back_inserter(d->error));
  197. d->hold.clear();
  198. std::move(d->unhold.begin(), d->unhold.end(), std::back_inserter(d->error));
  199. d->unhold.clear();
  200. std::move(d->install.begin(), d->install.end(), std::back_inserter(d->error));
  201. d->install.clear();
  202. }
  203. }
  204. return d->error.empty();
  205. }
  206. StateChanges::StateChanges() : d(new StateChanges::Private()) {}
  207. StateChanges::StateChanges(StateChanges&&) = default;
  208. StateChanges& StateChanges::operator=(StateChanges&&) = default;
  209. StateChanges::~StateChanges() = default;
  210. }