upgrade.cc 9.6 KB

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