upgrade.cc 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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, EDSP::Request::UPGRADE_ALL, 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,
  115. EDSP::Request::UPGRADE_ALL | EDSP::Request::FORBID_NEW_INSTALL | EDSP::Request::FORBID_REMOVE,
  116. Progress);
  117. if (Progress != NULL)
  118. Progress->OverallProgress(0, 100, 1, _("Calculating upgrade"));
  119. pkgDepCache::ActionGroup group(Cache);
  120. pkgProblemResolver Fix(&Cache);
  121. // Upgrade all installed packages
  122. for (pkgCache::PkgIterator I = Cache.PkgBegin(); I.end() == false; ++I)
  123. {
  124. if (Cache[I].Install() == true)
  125. Fix.Protect(I);
  126. if (_config->FindB("APT::Ignore-Hold",false) == false)
  127. if (I->SelectedState == pkgCache::State::Hold)
  128. continue;
  129. if (I->CurrentVer != 0 && Cache[I].InstallVer != 0)
  130. Cache.MarkInstall(I, false, 0, false);
  131. }
  132. if (Progress != NULL)
  133. Progress->Progress(50);
  134. // resolve remaining issues via keep
  135. bool const success = Fix.ResolveByKeep(Progress);
  136. if (Progress != NULL)
  137. Progress->Done();
  138. return success;
  139. }
  140. /*}}}*/
  141. // AllUpgradeWithNewInstalls - Upgrade + install new packages as needed /*{{{*/
  142. // ---------------------------------------------------------------------
  143. /* Right now the system must be consistent before this can be called.
  144. * Upgrade as much as possible without deleting anything (useful for
  145. * stable systems)
  146. */
  147. static bool pkgAllUpgradeWithNewPackages(pkgDepCache &Cache, OpProgress * const Progress)
  148. {
  149. std::string const solver = _config->Find("APT::Solver", "internal");
  150. if (solver != "internal")
  151. return EDSP::ResolveExternal(solver.c_str(), Cache,
  152. EDSP::Request::UPGRADE_ALL | EDSP::Request::FORBID_REMOVE,
  153. Progress);
  154. if (Progress != NULL)
  155. Progress->OverallProgress(0, 100, 1, _("Calculating upgrade"));
  156. pkgDepCache::ActionGroup group(Cache);
  157. pkgProblemResolver Fix(&Cache);
  158. // provide the initial set of stuff we want to upgrade by marking
  159. // all upgradable packages for upgrade
  160. for (pkgCache::PkgIterator I = Cache.PkgBegin(); I.end() == false; ++I)
  161. {
  162. if (I->CurrentVer != 0 && Cache[I].InstallVer != 0)
  163. {
  164. if (_config->FindB("APT::Ignore-Hold",false) == false)
  165. if (I->SelectedState == pkgCache::State::Hold)
  166. continue;
  167. Cache.MarkInstall(I, false, 0, false);
  168. }
  169. }
  170. if (Progress != NULL)
  171. Progress->Progress(10);
  172. // then let auto-install loose
  173. for (pkgCache::PkgIterator I = Cache.PkgBegin(); I.end() == false; ++I)
  174. if (Cache[I].Install())
  175. Cache.MarkInstall(I, true, 0, false);
  176. if (Progress != NULL)
  177. Progress->Progress(50);
  178. // ... but it may remove stuff, we we need to clean up afterwards again
  179. for (pkgCache::PkgIterator I = Cache.PkgBegin(); I.end() == false; ++I)
  180. if (Cache[I].Delete() == true)
  181. Cache.MarkKeep(I, false, false);
  182. if (Progress != NULL)
  183. Progress->Progress(60);
  184. // resolve remaining issues via keep
  185. bool const success = Fix.ResolveByKeep(Progress);
  186. if (Progress != NULL)
  187. Progress->Done();
  188. return success;
  189. }
  190. /*}}}*/
  191. // AllUpgrade - Upgrade as many packages as possible /*{{{*/
  192. // ---------------------------------------------------------------------
  193. /* Right now the system must be consistent before this can be called.
  194. It also will not change packages marked for install, it only tries
  195. to install packages not marked for install */
  196. static bool pkgAllUpgrade(pkgDepCache &Cache, OpProgress * const Progress)
  197. {
  198. return pkgAllUpgradeNoNewPackages(Cache, Progress);
  199. }
  200. bool pkgAllUpgrade(pkgDepCache &Cache)
  201. {
  202. return pkgAllUpgrade(Cache, NULL);
  203. }
  204. /*}}}*/
  205. // MinimizeUpgrade - Minimizes the set of packages to be upgraded /*{{{*/
  206. // ---------------------------------------------------------------------
  207. /* This simply goes over the entire set of packages and tries to keep
  208. each package marked for upgrade. If a conflict is generated then
  209. the package is restored. */
  210. bool pkgMinimizeUpgrade(pkgDepCache &Cache)
  211. {
  212. pkgDepCache::ActionGroup group(Cache);
  213. if (Cache.BrokenCount() != 0)
  214. return false;
  215. // We loop for 10 tries to get the minimal set size.
  216. bool Change = false;
  217. unsigned int Count = 0;
  218. do
  219. {
  220. Change = false;
  221. for (pkgCache::PkgIterator I = Cache.PkgBegin(); I.end() == false; ++I)
  222. {
  223. // Not interesting
  224. if (Cache[I].Upgrade() == false || Cache[I].NewInstall() == true)
  225. continue;
  226. // Keep it and see if that is OK
  227. Cache.MarkKeep(I, false, false);
  228. if (Cache.BrokenCount() != 0)
  229. Cache.MarkInstall(I, false, 0, false);
  230. else
  231. {
  232. // If keep didn't actually do anything then there was no change..
  233. if (Cache[I].Upgrade() == false)
  234. Change = true;
  235. }
  236. }
  237. ++Count;
  238. }
  239. while (Change == true && Count < 10);
  240. if (Cache.BrokenCount() != 0)
  241. return _error->Error("Internal Error in pkgMinimizeUpgrade");
  242. return true;
  243. }
  244. /*}}}*/
  245. // APT::Upgrade::Upgrade - Upgrade using a specific strategy /*{{{*/
  246. bool APT::Upgrade::Upgrade(pkgDepCache &Cache, int mode, OpProgress * const Progress)
  247. {
  248. APT_IGNORE_DEPRECATED_PUSH
  249. if (mode == ALLOW_EVERYTHING)
  250. return pkgDistUpgrade(Cache, Progress);
  251. else if ((mode & ~FORBID_REMOVE_PACKAGES) == 0)
  252. return pkgAllUpgradeWithNewPackages(Cache, Progress);
  253. else if ((mode & ~(FORBID_REMOVE_PACKAGES|FORBID_INSTALL_NEW_PACKAGES)) == 0)
  254. return pkgAllUpgradeNoNewPackages(Cache, Progress);
  255. else
  256. _error->Error("pkgAllUpgrade called with unsupported mode %i", mode);
  257. APT_IGNORE_DEPRECATED_POP
  258. return false;
  259. }
  260. /*}}}*/