upgrade.cc 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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. 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. bool pkgDistUpgrade(pkgDepCache &Cache)
  105. {
  106. return pkgDistUpgrade(Cache, NULL);
  107. }
  108. /*}}}*/
  109. // AllUpgradeNoNewPackages - Upgrade but no removals or new pkgs /*{{{*/
  110. static bool pkgAllUpgradeNoNewPackages(pkgDepCache &Cache, OpProgress * const Progress)
  111. {
  112. std::string const solver = _config->Find("APT::Solver", "internal");
  113. if (solver != "internal")
  114. return EDSP::ResolveExternal(solver.c_str(), Cache, true, false, false, Progress);
  115. if (Progress != NULL)
  116. Progress->OverallProgress(0, 100, 1, _("Calculating upgrade"));
  117. pkgDepCache::ActionGroup group(Cache);
  118. pkgProblemResolver Fix(&Cache);
  119. // Upgrade all installed packages
  120. for (pkgCache::PkgIterator I = Cache.PkgBegin(); I.end() == false; ++I)
  121. {
  122. if (Cache[I].Install() == true)
  123. Fix.Protect(I);
  124. if (_config->FindB("APT::Ignore-Hold",false) == false)
  125. if (I->SelectedState == pkgCache::State::Hold)
  126. continue;
  127. if (I->CurrentVer != 0 && Cache[I].InstallVer != 0)
  128. Cache.MarkInstall(I, false, 0, false);
  129. }
  130. if (Progress != NULL)
  131. Progress->Progress(50);
  132. // resolve remaining issues via keep
  133. bool const success = Fix.ResolveByKeep(Progress);
  134. if (Progress != NULL)
  135. Progress->Done();
  136. return success;
  137. }
  138. /*}}}*/
  139. // AllUpgradeWithNewInstalls - Upgrade + install new packages as needed /*{{{*/
  140. // ---------------------------------------------------------------------
  141. /* Right now the system must be consistent before this can be called.
  142. * Upgrade as much as possible without deleting anything (useful for
  143. * stable systems)
  144. */
  145. static bool pkgAllUpgradeWithNewPackages(pkgDepCache &Cache, OpProgress * const Progress)
  146. {
  147. std::string const solver = _config->Find("APT::Solver", "internal");
  148. if (solver != "internal")
  149. return EDSP::ResolveExternal(solver.c_str(), Cache, true, false, false, Progress);
  150. if (Progress != NULL)
  151. Progress->OverallProgress(0, 100, 1, _("Calculating upgrade"));
  152. pkgDepCache::ActionGroup group(Cache);
  153. pkgProblemResolver Fix(&Cache);
  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. static bool pkgAllUpgrade(pkgDepCache &Cache, OpProgress * const Progress)
  193. {
  194. return pkgAllUpgradeNoNewPackages(Cache, Progress);
  195. }
  196. bool pkgAllUpgrade(pkgDepCache &Cache)
  197. {
  198. return pkgAllUpgrade(Cache, NULL);
  199. }
  200. /*}}}*/
  201. // MinimizeUpgrade - Minimizes the set of packages to be upgraded /*{{{*/
  202. // ---------------------------------------------------------------------
  203. /* This simply goes over the entire set of packages and tries to keep
  204. each package marked for upgrade. If a conflict is generated then
  205. the package is restored. */
  206. bool pkgMinimizeUpgrade(pkgDepCache &Cache)
  207. {
  208. pkgDepCache::ActionGroup group(Cache);
  209. if (Cache.BrokenCount() != 0)
  210. return false;
  211. // We loop for 10 tries to get the minimal set size.
  212. bool Change = false;
  213. unsigned int Count = 0;
  214. do
  215. {
  216. Change = false;
  217. for (pkgCache::PkgIterator I = Cache.PkgBegin(); I.end() == false; ++I)
  218. {
  219. // Not interesting
  220. if (Cache[I].Upgrade() == false || Cache[I].NewInstall() == true)
  221. continue;
  222. // Keep it and see if that is OK
  223. Cache.MarkKeep(I, false, false);
  224. if (Cache.BrokenCount() != 0)
  225. Cache.MarkInstall(I, false, 0, false);
  226. else
  227. {
  228. // If keep didn't actually do anything then there was no change..
  229. if (Cache[I].Upgrade() == false)
  230. Change = true;
  231. }
  232. }
  233. ++Count;
  234. }
  235. while (Change == true && Count < 10);
  236. if (Cache.BrokenCount() != 0)
  237. return _error->Error("Internal Error in pkgMinimizeUpgrade");
  238. return true;
  239. }
  240. /*}}}*/
  241. // APT::Upgrade::Upgrade - Upgrade using a specific strategy /*{{{*/
  242. bool APT::Upgrade::Upgrade(pkgDepCache &Cache, int mode, OpProgress * const Progress)
  243. {
  244. APT_IGNORE_DEPRECATED_PUSH
  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. APT_IGNORE_DEPRECATED_POP
  254. return false;
  255. }
  256. /*}}}*/