upgrade.cc 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. // Include Files /*{{{*/
  2. #include <config.h>
  3. #include <apt-pkg/algorithms.h>
  4. #include <apt-pkg/configuration.h>
  5. #include <apt-pkg/edsp.h>
  6. #include <apt-pkg/error.h>
  7. #include <apt-pkg/progress.h>
  8. #include <apt-pkg/upgrade.h>
  9. #include <apt-pkg/depcache.h>
  10. #include <apt-pkg/pkgcache.h>
  11. #include <apt-pkg/cacheiterators.h>
  12. #include <string>
  13. #include <apti18n.h>
  14. /*}}}*/
  15. // DistUpgrade - Distribution upgrade /*{{{*/
  16. // ---------------------------------------------------------------------
  17. /* This autoinstalls every package and then force installs every
  18. pre-existing package. This creates the initial set of conditions which
  19. most likely contain problems because too many things were installed.
  20. The problem resolver is used to resolve the problems.
  21. */
  22. static bool pkgDistUpgrade(pkgDepCache &Cache, OpProgress * const Progress)
  23. {
  24. std::string const solver = _config->Find("APT::Solver", "internal");
  25. auto const ret = EDSP::ResolveExternal(solver.c_str(), Cache, EDSP::Request::UPGRADE_ALL, Progress);
  26. if (solver != "internal")
  27. return ret;
  28. if (Progress != NULL)
  29. Progress->OverallProgress(0, 100, 1, _("Calculating upgrade"));
  30. pkgDepCache::ActionGroup group(Cache);
  31. /* Upgrade all installed packages first without autoinst to help the resolver
  32. in versioned or-groups to upgrade the old solver instead of installing
  33. a new one (if the old solver is not the first one [anymore]) */
  34. for (pkgCache::PkgIterator I = Cache.PkgBegin(); I.end() == false; ++I)
  35. if (I->CurrentVer != 0)
  36. Cache.MarkInstall(I, false, 0, false);
  37. if (Progress != NULL)
  38. Progress->Progress(10);
  39. /* Auto upgrade all installed packages, this provides the basis
  40. for the installation */
  41. for (pkgCache::PkgIterator I = Cache.PkgBegin(); I.end() == false; ++I)
  42. if (I->CurrentVer != 0)
  43. Cache.MarkInstall(I, true, 0, false);
  44. if (Progress != NULL)
  45. Progress->Progress(50);
  46. /* Now, install each essential package which is not installed
  47. (and not provided by another package in the same name group) */
  48. std::string essential = _config->Find("pkgCacheGen::Essential", "all");
  49. if (essential == "all")
  50. {
  51. for (pkgCache::GrpIterator G = Cache.GrpBegin(); G.end() == false; ++G)
  52. {
  53. bool isEssential = false;
  54. bool instEssential = false;
  55. for (pkgCache::PkgIterator P = G.PackageList(); P.end() == false; P = G.NextPkg(P))
  56. {
  57. if ((P->Flags & pkgCache::Flag::Essential) != pkgCache::Flag::Essential)
  58. continue;
  59. isEssential = true;
  60. if (Cache[P].Install() == true)
  61. {
  62. instEssential = true;
  63. break;
  64. }
  65. }
  66. if (isEssential == false || instEssential == true)
  67. continue;
  68. pkgCache::PkgIterator P = G.FindPreferredPkg();
  69. Cache.MarkInstall(P, true, 0, false);
  70. }
  71. }
  72. else if (essential != "none")
  73. for (pkgCache::PkgIterator I = Cache.PkgBegin(); I.end() == false; ++I)
  74. if ((I->Flags & pkgCache::Flag::Essential) == pkgCache::Flag::Essential)
  75. Cache.MarkInstall(I, true, 0, false);
  76. if (Progress != NULL)
  77. Progress->Progress(55);
  78. /* We do it again over all previously installed packages to force
  79. conflict resolution on them all. */
  80. for (pkgCache::PkgIterator I = Cache.PkgBegin(); I.end() == false; ++I)
  81. if (I->CurrentVer != 0)
  82. Cache.MarkInstall(I, false, 0, false);
  83. if (Progress != NULL)
  84. Progress->Progress(65);
  85. pkgProblemResolver Fix(&Cache);
  86. if (Progress != NULL)
  87. Progress->Progress(95);
  88. // Hold back held packages.
  89. if (_config->FindB("APT::Ignore-Hold",false) == false)
  90. {
  91. for (pkgCache::PkgIterator I = Cache.PkgBegin(); I.end() == false; ++I)
  92. {
  93. if (I->SelectedState == pkgCache::State::Hold)
  94. {
  95. Fix.Protect(I);
  96. Cache.MarkKeep(I, false, false);
  97. }
  98. }
  99. }
  100. bool const success = Fix.ResolveInternal(false);
  101. if (Progress != NULL)
  102. Progress->Done();
  103. return success;
  104. }
  105. bool pkgDistUpgrade(pkgDepCache &Cache)
  106. {
  107. return pkgDistUpgrade(Cache, NULL);
  108. }
  109. /*}}}*/
  110. // AllUpgradeNoNewPackages - Upgrade but no removals or new pkgs /*{{{*/
  111. static bool pkgAllUpgradeNoNewPackages(pkgDepCache &Cache, OpProgress * const Progress)
  112. {
  113. std::string const solver = _config->Find("APT::Solver", "internal");
  114. constexpr auto flags = EDSP::Request::UPGRADE_ALL | EDSP::Request::FORBID_NEW_INSTALL | EDSP::Request::FORBID_REMOVE;
  115. auto const ret = EDSP::ResolveExternal(solver.c_str(), Cache, flags, Progress);
  116. if (solver != "internal")
  117. return ret;
  118. if (Progress != NULL)
  119. Progress->OverallProgress(0, 100, 1, _("Calculating upgrade"));
  120. pkgDepCache::ActionGroup group(Cache);
  121. pkgProblemResolver Fix(&Cache);
  122. // Upgrade all installed packages
  123. for (pkgCache::PkgIterator I = Cache.PkgBegin(); I.end() == false; ++I)
  124. {
  125. if (Cache[I].Install() == true)
  126. Fix.Protect(I);
  127. if (_config->FindB("APT::Ignore-Hold",false) == false)
  128. if (I->SelectedState == pkgCache::State::Hold)
  129. continue;
  130. if (I->CurrentVer != 0 && Cache[I].InstallVer != 0)
  131. Cache.MarkInstall(I, false, 0, false);
  132. }
  133. if (Progress != NULL)
  134. Progress->Progress(50);
  135. // resolve remaining issues via keep
  136. bool const success = Fix.ResolveByKeepInternal();
  137. if (Progress != NULL)
  138. Progress->Done();
  139. return success;
  140. }
  141. /*}}}*/
  142. // AllUpgradeWithNewInstalls - Upgrade + install new packages as needed /*{{{*/
  143. // ---------------------------------------------------------------------
  144. /* Right now the system must be consistent before this can be called.
  145. * Upgrade as much as possible without deleting anything (useful for
  146. * stable systems)
  147. */
  148. static bool pkgAllUpgradeWithNewPackages(pkgDepCache &Cache, OpProgress * const Progress)
  149. {
  150. std::string const solver = _config->Find("APT::Solver", "internal");
  151. constexpr auto flags = EDSP::Request::UPGRADE_ALL | EDSP::Request::FORBID_REMOVE;
  152. auto const ret = EDSP::ResolveExternal(solver.c_str(), Cache, flags, Progress);
  153. if (solver != "internal")
  154. return ret;
  155. if (Progress != NULL)
  156. Progress->OverallProgress(0, 100, 1, _("Calculating upgrade"));
  157. pkgDepCache::ActionGroup group(Cache);
  158. pkgProblemResolver Fix(&Cache);
  159. // provide the initial set of stuff we want to upgrade by marking
  160. // all upgradable packages for upgrade
  161. for (pkgCache::PkgIterator I = Cache.PkgBegin(); I.end() == false; ++I)
  162. {
  163. if (I->CurrentVer != 0 && Cache[I].InstallVer != 0)
  164. {
  165. if (_config->FindB("APT::Ignore-Hold",false) == false)
  166. if (I->SelectedState == pkgCache::State::Hold)
  167. continue;
  168. Cache.MarkInstall(I, false, 0, false);
  169. }
  170. }
  171. if (Progress != NULL)
  172. Progress->Progress(10);
  173. // then let auto-install loose
  174. for (pkgCache::PkgIterator I = Cache.PkgBegin(); I.end() == false; ++I)
  175. if (Cache[I].Install())
  176. Cache.MarkInstall(I, true, 0, false);
  177. if (Progress != NULL)
  178. Progress->Progress(50);
  179. // ... but it may remove stuff, we we need to clean up afterwards again
  180. for (pkgCache::PkgIterator I = Cache.PkgBegin(); I.end() == false; ++I)
  181. if (Cache[I].Delete() == true)
  182. Cache.MarkKeep(I, false, false);
  183. if (Progress != NULL)
  184. Progress->Progress(60);
  185. // resolve remaining issues via keep
  186. bool const success = Fix.ResolveByKeepInternal();
  187. if (Progress != NULL)
  188. Progress->Done();
  189. return success;
  190. }
  191. /*}}}*/
  192. // AllUpgrade - Upgrade as many packages as possible /*{{{*/
  193. // ---------------------------------------------------------------------
  194. /* Right now the system must be consistent before this can be called.
  195. It also will not change packages marked for install, it only tries
  196. to install packages not marked for install */
  197. static bool pkgAllUpgrade(pkgDepCache &Cache, OpProgress * const Progress)
  198. {
  199. return pkgAllUpgradeNoNewPackages(Cache, Progress);
  200. }
  201. bool pkgAllUpgrade(pkgDepCache &Cache)
  202. {
  203. return pkgAllUpgrade(Cache, NULL);
  204. }
  205. /*}}}*/
  206. // MinimizeUpgrade - Minimizes the set of packages to be upgraded /*{{{*/
  207. // ---------------------------------------------------------------------
  208. /* This simply goes over the entire set of packages and tries to keep
  209. each package marked for upgrade. If a conflict is generated then
  210. the package is restored. */
  211. bool pkgMinimizeUpgrade(pkgDepCache &Cache)
  212. {
  213. pkgDepCache::ActionGroup group(Cache);
  214. if (Cache.BrokenCount() != 0)
  215. return false;
  216. // We loop for 10 tries to get the minimal set size.
  217. bool Change = false;
  218. unsigned int Count = 0;
  219. do
  220. {
  221. Change = false;
  222. for (pkgCache::PkgIterator I = Cache.PkgBegin(); I.end() == false; ++I)
  223. {
  224. // Not interesting
  225. if (Cache[I].Upgrade() == false || Cache[I].NewInstall() == true)
  226. continue;
  227. // Keep it and see if that is OK
  228. Cache.MarkKeep(I, false, false);
  229. if (Cache.BrokenCount() != 0)
  230. Cache.MarkInstall(I, false, 0, false);
  231. else
  232. {
  233. // If keep didn't actually do anything then there was no change..
  234. if (Cache[I].Upgrade() == false)
  235. Change = true;
  236. }
  237. }
  238. ++Count;
  239. }
  240. while (Change == true && Count < 10);
  241. if (Cache.BrokenCount() != 0)
  242. return _error->Error("Internal Error in pkgMinimizeUpgrade");
  243. return true;
  244. }
  245. /*}}}*/
  246. // APT::Upgrade::Upgrade - Upgrade using a specific strategy /*{{{*/
  247. bool APT::Upgrade::Upgrade(pkgDepCache &Cache, int mode, OpProgress * const Progress)
  248. {
  249. APT_IGNORE_DEPRECATED_PUSH
  250. if (mode == ALLOW_EVERYTHING)
  251. return pkgDistUpgrade(Cache, Progress);
  252. else if ((mode & ~FORBID_REMOVE_PACKAGES) == 0)
  253. return pkgAllUpgradeWithNewPackages(Cache, Progress);
  254. else if ((mode & ~(FORBID_REMOVE_PACKAGES|FORBID_INSTALL_NEW_PACKAGES)) == 0)
  255. return pkgAllUpgradeNoNewPackages(Cache, Progress);
  256. else
  257. _error->Error("pkgAllUpgrade called with unsupported mode %i", mode);
  258. APT_IGNORE_DEPRECATED_POP
  259. return false;
  260. }
  261. /*}}}*/