upgrade.cc 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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. bool pkgDistUpgrade(pkgDepCache &Cache)
  23. {
  24. std::string const solver = _config->Find("APT::Solver", "internal");
  25. if (solver != "internal") {
  26. OpTextProgress Prog(*_config);
  27. return EDSP::ResolveExternal(solver.c_str(), Cache, false, true, false, &Prog);
  28. }
  29. pkgDepCache::ActionGroup group(Cache);
  30. /* Upgrade all installed packages first without autoinst to help the resolver
  31. in versioned or-groups to upgrade the old solver instead of installing
  32. a new one (if the old solver is not the first one [anymore]) */
  33. for (pkgCache::PkgIterator I = Cache.PkgBegin(); I.end() == false; ++I)
  34. if (I->CurrentVer != 0)
  35. Cache.MarkInstall(I, false, 0, false);
  36. /* Auto upgrade all installed packages, this provides the basis
  37. for the installation */
  38. for (pkgCache::PkgIterator I = Cache.PkgBegin(); I.end() == false; ++I)
  39. if (I->CurrentVer != 0)
  40. Cache.MarkInstall(I, true, 0, false);
  41. /* Now, install each essential package which is not installed
  42. (and not provided by another package in the same name group) */
  43. std::string essential = _config->Find("pkgCacheGen::Essential", "all");
  44. if (essential == "all")
  45. {
  46. for (pkgCache::GrpIterator G = Cache.GrpBegin(); G.end() == false; ++G)
  47. {
  48. bool isEssential = false;
  49. bool instEssential = false;
  50. for (pkgCache::PkgIterator P = G.PackageList(); P.end() == false; P = G.NextPkg(P))
  51. {
  52. if ((P->Flags & pkgCache::Flag::Essential) != pkgCache::Flag::Essential)
  53. continue;
  54. isEssential = true;
  55. if (Cache[P].Install() == true)
  56. {
  57. instEssential = true;
  58. break;
  59. }
  60. }
  61. if (isEssential == false || instEssential == true)
  62. continue;
  63. pkgCache::PkgIterator P = G.FindPreferredPkg();
  64. Cache.MarkInstall(P, true, 0, false);
  65. }
  66. }
  67. else if (essential != "none")
  68. for (pkgCache::PkgIterator I = Cache.PkgBegin(); I.end() == false; ++I)
  69. if ((I->Flags & pkgCache::Flag::Essential) == pkgCache::Flag::Essential)
  70. Cache.MarkInstall(I, true, 0, false);
  71. /* We do it again over all previously installed packages to force
  72. conflict resolution on them all. */
  73. for (pkgCache::PkgIterator I = Cache.PkgBegin(); I.end() == false; ++I)
  74. if (I->CurrentVer != 0)
  75. Cache.MarkInstall(I, false, 0, false);
  76. pkgProblemResolver Fix(&Cache);
  77. // Hold back held packages.
  78. if (_config->FindB("APT::Ignore-Hold",false) == false)
  79. {
  80. for (pkgCache::PkgIterator I = Cache.PkgBegin(); I.end() == false; ++I)
  81. {
  82. if (I->SelectedState == pkgCache::State::Hold)
  83. {
  84. Fix.Protect(I);
  85. Cache.MarkKeep(I, false, false);
  86. }
  87. }
  88. }
  89. return Fix.Resolve();
  90. }
  91. /*}}}*/
  92. // AllUpgradeNoNewPackages - Upgrade but no removals or new pkgs /*{{{*/
  93. static bool pkgAllUpgradeNoNewPackages(pkgDepCache &Cache)
  94. {
  95. std::string const solver = _config->Find("APT::Solver", "internal");
  96. if (solver != "internal") {
  97. OpTextProgress Prog(*_config);
  98. return EDSP::ResolveExternal(solver.c_str(), Cache, true, false, false, &Prog);
  99. }
  100. pkgDepCache::ActionGroup group(Cache);
  101. pkgProblemResolver Fix(&Cache);
  102. if (Cache.BrokenCount() != 0)
  103. return false;
  104. // Upgrade all installed packages
  105. for (pkgCache::PkgIterator I = Cache.PkgBegin(); I.end() == false; ++I)
  106. {
  107. if (Cache[I].Install() == true)
  108. Fix.Protect(I);
  109. if (_config->FindB("APT::Ignore-Hold",false) == false)
  110. if (I->SelectedState == pkgCache::State::Hold)
  111. continue;
  112. if (I->CurrentVer != 0 && Cache[I].InstallVer != 0)
  113. Cache.MarkInstall(I, false, 0, false);
  114. }
  115. return Fix.ResolveByKeep();
  116. }
  117. /*}}}*/
  118. // AllUpgradeWithNewInstalls - Upgrade + install new packages as needed /*{{{*/
  119. // ---------------------------------------------------------------------
  120. /* Right now the system must be consistent before this can be called.
  121. * Upgrade as much as possible without deleting anything (useful for
  122. * stable systems)
  123. */
  124. static bool pkgAllUpgradeWithNewPackages(pkgDepCache &Cache)
  125. {
  126. pkgDepCache::ActionGroup group(Cache);
  127. pkgProblemResolver Fix(&Cache);
  128. if (Cache.BrokenCount() != 0)
  129. return false;
  130. // provide the initial set of stuff we want to upgrade by marking
  131. // all upgradable packages for upgrade
  132. for (pkgCache::PkgIterator I = Cache.PkgBegin(); I.end() == false; ++I)
  133. {
  134. if (I->CurrentVer != 0 && Cache[I].InstallVer != 0)
  135. {
  136. if (_config->FindB("APT::Ignore-Hold",false) == false)
  137. if (I->SelectedState == pkgCache::State::Hold)
  138. continue;
  139. Cache.MarkInstall(I, false, 0, false);
  140. }
  141. }
  142. // then let auto-install loose
  143. for (pkgCache::PkgIterator I = Cache.PkgBegin(); I.end() == false; ++I)
  144. if (Cache[I].Install())
  145. Cache.MarkInstall(I, true, 0, false);
  146. // ... but it may remove stuff, we we need to clean up afterwards again
  147. for (pkgCache::PkgIterator I = Cache.PkgBegin(); I.end() == false; ++I)
  148. if (Cache[I].Delete() == true)
  149. Cache.MarkKeep(I, false, false);
  150. // resolve remaining issues via keep
  151. return Fix.ResolveByKeep();
  152. }
  153. /*}}}*/
  154. // AllUpgrade - Upgrade as many packages as possible /*{{{*/
  155. // ---------------------------------------------------------------------
  156. /* Right now the system must be consistent before this can be called.
  157. It also will not change packages marked for install, it only tries
  158. to install packages not marked for install */
  159. bool pkgAllUpgrade(pkgDepCache &Cache)
  160. {
  161. return pkgAllUpgradeNoNewPackages(Cache);
  162. }
  163. /*}}}*/
  164. // MinimizeUpgrade - Minimizes the set of packages to be upgraded /*{{{*/
  165. // ---------------------------------------------------------------------
  166. /* This simply goes over the entire set of packages and tries to keep
  167. each package marked for upgrade. If a conflict is generated then
  168. the package is restored. */
  169. bool pkgMinimizeUpgrade(pkgDepCache &Cache)
  170. {
  171. pkgDepCache::ActionGroup group(Cache);
  172. if (Cache.BrokenCount() != 0)
  173. return false;
  174. // We loop for 10 tries to get the minimal set size.
  175. bool Change = false;
  176. unsigned int Count = 0;
  177. do
  178. {
  179. Change = false;
  180. for (pkgCache::PkgIterator I = Cache.PkgBegin(); I.end() == false; ++I)
  181. {
  182. // Not interesting
  183. if (Cache[I].Upgrade() == false || Cache[I].NewInstall() == true)
  184. continue;
  185. // Keep it and see if that is OK
  186. Cache.MarkKeep(I, false, false);
  187. if (Cache.BrokenCount() != 0)
  188. Cache.MarkInstall(I, false, 0, false);
  189. else
  190. {
  191. // If keep didn't actually do anything then there was no change..
  192. if (Cache[I].Upgrade() == false)
  193. Change = true;
  194. }
  195. }
  196. ++Count;
  197. }
  198. while (Change == true && Count < 10);
  199. if (Cache.BrokenCount() != 0)
  200. return _error->Error("Internal Error in pkgMinimizeUpgrade");
  201. return true;
  202. }
  203. /*}}}*/
  204. // APT::Upgrade::Upgrade - Upgrade using a specific strategy /*{{{*/
  205. bool APT::Upgrade::Upgrade(pkgDepCache &Cache, int mode)
  206. {
  207. if (mode == 0)
  208. {
  209. return pkgDistUpgrade(Cache);
  210. }
  211. else if ((mode & ~FORBID_REMOVE_PACKAGES) == 0)
  212. {
  213. return pkgAllUpgradeWithNewPackages(Cache);
  214. }
  215. else if ((mode & ~(FORBID_REMOVE_PACKAGES|FORBID_INSTALL_NEW_PACKAGES)) == 0)
  216. {
  217. return pkgAllUpgradeNoNewPackages(Cache);
  218. }
  219. else
  220. _error->Error("pkgAllUpgrade called with unsupported mode %i", mode);
  221. return false;
  222. }
  223. /*}}}*/