upgrade.cc 8.2 KB

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