]> git.saurik.com Git - apt.git/blame - apt-pkg/algorithms.cc
let DPKG_COLORS default to our APT::Color setting
[apt.git] / apt-pkg / algorithms.cc
CommitLineData
6c139d6e
AL
1// -*- mode: cpp; mode: fold -*-
2// Description /*{{{*/
b8c0f9b7 3// $Id: algorithms.cc,v 1.44 2002/11/28 18:49:16 jgg Exp $
6c139d6e
AL
4/* ######################################################################
5
6 Algorithms - A set of misc algorithms
7
0a8e3465
AL
8 The pkgProblemResolver class has become insanely complex and
9 very sophisticated, it handles every test case I have thrown at it
10 to my satisfaction. Understanding exactly why all the steps the class
11 does are required is difficult and changing though not very risky
12 may result in other cases not working.
13
6c139d6e
AL
14 ##################################################################### */
15 /*}}}*/
16// Include Files /*{{{*/
ea542140
DK
17#include <config.h>
18
094a497d
AL
19#include <apt-pkg/algorithms.h>
20#include <apt-pkg/error.h>
0a8e3465 21#include <apt-pkg/configuration.h>
c3b85126 22#include <apt-pkg/edsp.h>
453b82a3
DK
23#include <apt-pkg/depcache.h>
24#include <apt-pkg/packagemanager.h>
25#include <apt-pkg/pkgcache.h>
26#include <apt-pkg/cacheiterators.h>
84573326 27#include <apt-pkg/prettyprinters.h>
6d38011b 28
453b82a3
DK
29#include <string.h>
30#include <string>
152ab79e 31#include <cstdlib>
90f057fd 32#include <iostream>
ea542140
DK
33
34#include <apti18n.h>
6c139d6e 35 /*}}}*/
584e4558 36using namespace std;
6c139d6e 37
6c139d6e
AL
38// Simulate::Simulate - Constructor /*{{{*/
39// ---------------------------------------------------------------------
b2e465d6
AL
40/* The legacy translations here of input Pkg iterators is obsolete,
41 this is not necessary since the pkgCaches are fully shared now. */
42pkgSimulate::pkgSimulate(pkgDepCache *Cache) : pkgPackageManager(Cache),
6c55f07a 43 d(NULL), iPolicy(Cache),
496d5c70
MV
44 Sim(&Cache->GetCache(),&iPolicy),
45 group(Sim)
6c139d6e 46{
b2e465d6
AL
47 Sim.Init(0);
48 Flags = new unsigned char[Cache->Head().PackageCount];
49 memset(Flags,0,sizeof(*Flags)*Cache->Head().PackageCount);
281daf46
AL
50
51 // Fake a filename so as not to activate the media swapping
52 string Jnk = "SIMULATE";
b2e465d6 53 for (unsigned int I = 0; I != Cache->Head().PackageCount; I++)
281daf46 54 FileNames[I] = Jnk;
6c139d6e
AL
55}
56 /*}}}*/
b270388b
MV
57// Simulate::~Simulate - Destructor /*{{{*/
58pkgSimulate::~pkgSimulate()
59{
60 delete[] Flags;
61}
62 /*}}}*/
b2e465d6
AL
63// Simulate::Describe - Describe a package /*{{{*/
64// ---------------------------------------------------------------------
3826564e
MZ
65/* Parameter Current == true displays the current package version,
66 Parameter Candidate == true displays the candidate package version */
67void pkgSimulate::Describe(PkgIterator Pkg,ostream &out,bool Current,bool Candidate)
b2e465d6
AL
68{
69 VerIterator Ver(Sim);
e59458f7 70
47f6d1b7 71 out << Pkg.FullName(true);
e59458f7 72
3826564e 73 if (Current == true)
e59458f7 74 {
b2e465d6 75 Ver = Pkg.CurrentVer();
e59458f7
AL
76 if (Ver.end() == false)
77 out << " [" << Ver.VerStr() << ']';
78 }
b2e465d6 79
3826564e
MZ
80 if (Candidate == true)
81 {
82 Ver = Sim[Pkg].CandidateVerIter(Sim);
83 if (Ver.end() == true)
84 return;
b2e465d6 85
3826564e
MZ
86 out << " (" << Ver.VerStr() << ' ' << Ver.RelStr() << ')';
87 }
b2e465d6
AL
88}
89 /*}}}*/
6c139d6e
AL
90// Simulate::Install - Simulate unpacking of a package /*{{{*/
91// ---------------------------------------------------------------------
92/* */
93bool pkgSimulate::Install(PkgIterator iPkg,string /*File*/)
94{
95 // Adapt the iterator
803ea2a8 96 PkgIterator Pkg = Sim.FindPkg(iPkg.Name(), iPkg.Arch());
6c139d6e
AL
97 Flags[Pkg->ID] = 1;
98
b2e465d6 99 cout << "Inst ";
3826564e 100 Describe(Pkg,cout,true,true);
6c139d6e 101 Sim.MarkInstall(Pkg,false);
803ea2a8 102
6c139d6e 103 // Look for broken conflicts+predepends.
f7f0d6c7 104 for (PkgIterator I = Sim.PkgBegin(); I.end() == false; ++I)
6c139d6e
AL
105 {
106 if (Sim[I].InstallVer == 0)
107 continue;
108
b2e465d6
AL
109 for (DepIterator D = Sim[I].InstVerIter(Sim).DependsList(); D.end() == false;)
110 {
111 DepIterator Start;
112 DepIterator End;
113 D.GlobOr(Start,End);
359e46db 114 if (Start.IsNegative() == true ||
b2e465d6 115 End->Type == pkgCache::Dep::PreDepends)
6c139d6e 116 {
b2e465d6 117 if ((Sim[End] & pkgDepCache::DepGInstall) == 0)
6c139d6e 118 {
47f6d1b7 119 cout << " [" << I.FullName(false) << " on " << Start.TargetPkg().FullName(false) << ']';
b2e465d6 120 if (Start->Type == pkgCache::Dep::Conflicts)
47f6d1b7 121 _error->Error("Fatal, conflicts violated %s",I.FullName(false).c_str());
6c139d6e 122 }
b2e465d6
AL
123 }
124 }
6c139d6e
AL
125 }
126
127 if (Sim.BrokenCount() != 0)
128 ShortBreaks();
129 else
04aa15a8 130 cout << endl;
6c139d6e
AL
131 return true;
132}
133 /*}}}*/
134// Simulate::Configure - Simulate configuration of a Package /*{{{*/
135// ---------------------------------------------------------------------
136/* This is not an acurate simulation of relatity, we should really not
137 install the package.. For some investigations it may be necessary
138 however. */
139bool pkgSimulate::Configure(PkgIterator iPkg)
140{
141 // Adapt the iterator
803ea2a8 142 PkgIterator Pkg = Sim.FindPkg(iPkg.Name(), iPkg.Arch());
6c139d6e
AL
143
144 Flags[Pkg->ID] = 2;
803ea2a8 145
6c139d6e
AL
146 if (Sim[Pkg].InstBroken() == true)
147 {
47f6d1b7 148 cout << "Conf " << Pkg.FullName(false) << " broken" << endl;
6c139d6e
AL
149
150 Sim.Update();
151
152 // Print out each package and the failed dependencies
f7f0d6c7 153 for (pkgCache::DepIterator D = Sim[Pkg].InstVerIter(Sim).DependsList(); D.end() == false; ++D)
6c139d6e
AL
154 {
155 if (Sim.IsImportantDep(D) == false ||
156 (Sim[D] & pkgDepCache::DepInstall) != 0)
157 continue;
158
b2e465d6 159 if (D->Type == pkgCache::Dep::Obsoletes)
47f6d1b7 160 cout << " Obsoletes:" << D.TargetPkg().FullName(false);
b2e465d6 161 else if (D->Type == pkgCache::Dep::Conflicts)
47f6d1b7 162 cout << " Conflicts:" << D.TargetPkg().FullName(false);
308c7d30 163 else if (D->Type == pkgCache::Dep::DpkgBreaks)
47f6d1b7 164 cout << " Breaks:" << D.TargetPkg().FullName(false);
6c139d6e 165 else
47f6d1b7 166 cout << " Depends:" << D.TargetPkg().FullName(false);
6c139d6e 167 }
04aa15a8 168 cout << endl;
6c139d6e 169
09a10f9c 170 _error->Error("Conf Broken %s",Pkg.FullName(false).c_str());
6c139d6e
AL
171 }
172 else
b2e465d6
AL
173 {
174 cout << "Conf ";
3826564e 175 Describe(Pkg,cout,false,true);
b2e465d6 176 }
6c139d6e
AL
177
178 if (Sim.BrokenCount() != 0)
179 ShortBreaks();
180 else
04aa15a8 181 cout << endl;
6c139d6e
AL
182
183 return true;
184}
185 /*}}}*/
186// Simulate::Remove - Simulate the removal of a package /*{{{*/
187// ---------------------------------------------------------------------
188/* */
fc4b5c9f 189bool pkgSimulate::Remove(PkgIterator iPkg,bool Purge)
6c139d6e
AL
190{
191 // Adapt the iterator
803ea2a8 192 PkgIterator Pkg = Sim.FindPkg(iPkg.Name(), iPkg.Arch());
c919ad6e
DK
193 if (Pkg.end() == true)
194 {
195 std::cerr << (Purge ? "Purg" : "Remv") << " invalid package " << iPkg.FullName() << std::endl;
196 return false;
197 }
6c139d6e
AL
198
199 Flags[Pkg->ID] = 3;
200 Sim.MarkDelete(Pkg);
803ea2a8 201
fc4b5c9f 202 if (Purge == true)
b2e465d6 203 cout << "Purg ";
fc4b5c9f 204 else
b2e465d6 205 cout << "Remv ";
3826564e 206 Describe(Pkg,cout,true,false);
6c139d6e
AL
207
208 if (Sim.BrokenCount() != 0)
209 ShortBreaks();
210 else
04aa15a8 211 cout << endl;
6c139d6e
AL
212
213 return true;
214}
215 /*}}}*/
216// Simulate::ShortBreaks - Print out a short line describing all breaks /*{{{*/
217// ---------------------------------------------------------------------
218/* */
219void pkgSimulate::ShortBreaks()
220{
04aa15a8 221 cout << " [";
f7f0d6c7 222 for (PkgIterator I = Sim.PkgBegin(); I.end() == false; ++I)
6c139d6e
AL
223 {
224 if (Sim[I].InstBroken() == true)
225 {
226 if (Flags[I->ID] == 0)
47f6d1b7 227 cout << I.FullName(false) << ' ';
6c139d6e 228/* else
04aa15a8 229 cout << I.Name() << "! ";*/
6c139d6e
AL
230 }
231 }
04aa15a8 232 cout << ']' << endl;
6c139d6e
AL
233}
234 /*}}}*/
235// ApplyStatus - Adjust for non-ok packages /*{{{*/
236// ---------------------------------------------------------------------
237/* We attempt to change the state of the all packages that have failed
238 installation toward their real state. The ordering code will perform
239 the necessary calculations to deal with the problems. */
240bool pkgApplyStatus(pkgDepCache &Cache)
241{
74a05226
MV
242 pkgDepCache::ActionGroup group(Cache);
243
f7f0d6c7 244 for (pkgCache::PkgIterator I = Cache.PkgBegin(); I.end() == false; ++I)
6c139d6e 245 {
e481d5b0
AL
246 if (I->VersionList == 0)
247 continue;
248
d38b7b3d
AL
249 // Only choice for a ReInstReq package is to reinstall
250 if (I->InstState == pkgCache::State::ReInstReq ||
251 I->InstState == pkgCache::State::HoldReInstReq)
252 {
5871718b 253 if (I->CurrentVer != 0 && I.CurrentVer().Downloadable() == true)
74a05226 254 Cache.MarkKeep(I, false, false);
813c8eea
AL
255 else
256 {
257 // Is this right? Will dpkg choke on an upgrade?
2a3f3893
AL
258 if (Cache[I].CandidateVer != 0 &&
259 Cache[I].CandidateVerIter(Cache).Downloadable() == true)
74a05226 260 Cache.MarkInstall(I, false, 0, false);
813c8eea 261 else
b2e465d6 262 return _error->Error(_("The package %s needs to be reinstalled, "
09a10f9c 263 "but I can't find an archive for it."),I.FullName(true).c_str());
813c8eea
AL
264 }
265
d38b7b3d
AL
266 continue;
267 }
268
6c139d6e
AL
269 switch (I->CurrentState)
270 {
813c8eea
AL
271 /* This means installation failed somehow - it does not need to be
272 re-unpacked (probably) */
b518cca6
AL
273 case pkgCache::State::UnPacked:
274 case pkgCache::State::HalfConfigured:
9d06bc80
MV
275 case pkgCache::State::TriggersAwaited:
276 case pkgCache::State::TriggersPending:
5871718b 277 if ((I->CurrentVer != 0 && I.CurrentVer().Downloadable() == true) ||
813c8eea 278 I.State() != pkgCache::PkgIterator::NeedsUnpack)
74a05226 279 Cache.MarkKeep(I, false, false);
813c8eea
AL
280 else
281 {
2a3f3893
AL
282 if (Cache[I].CandidateVer != 0 &&
283 Cache[I].CandidateVerIter(Cache).Downloadable() == true)
74a05226 284 Cache.MarkInstall(I, true, 0, false);
813c8eea 285 else
b83cad32 286 Cache.MarkDelete(I, false, 0, false);
813c8eea 287 }
6c139d6e
AL
288 break;
289
290 // This means removal failed
b518cca6 291 case pkgCache::State::HalfInstalled:
b83cad32 292 Cache.MarkDelete(I, false, 0, false);
6c139d6e
AL
293 break;
294
295 default:
b518cca6 296 if (I->InstState != pkgCache::State::Ok)
6c139d6e 297 return _error->Error("The package %s is not ok and I "
09a10f9c 298 "don't know how to fix it!",I.FullName(false).c_str());
6c139d6e
AL
299 }
300 }
301 return true;
302}
303 /*}}}*/
304// FixBroken - Fix broken packages /*{{{*/
305// ---------------------------------------------------------------------
0a8e3465
AL
306/* This autoinstalls every broken package and then runs the problem resolver
307 on the result. */
6c139d6e
AL
308bool pkgFixBroken(pkgDepCache &Cache)
309{
74a05226
MV
310 pkgDepCache::ActionGroup group(Cache);
311
6c139d6e 312 // Auto upgrade all broken packages
f7f0d6c7 313 for (pkgCache::PkgIterator I = Cache.PkgBegin(); I.end() == false; ++I)
6c139d6e 314 if (Cache[I].NowBroken() == true)
74a05226 315 Cache.MarkInstall(I, true, 0, false);
7e798dd7 316
6c139d6e
AL
317 /* Fix packages that are in a NeedArchive state but don't have a
318 downloadable install version */
f7f0d6c7 319 for (pkgCache::PkgIterator I = Cache.PkgBegin(); I.end() == false; ++I)
6c139d6e
AL
320 {
321 if (I.State() != pkgCache::PkgIterator::NeedsUnpack ||
322 Cache[I].Delete() == true)
323 continue;
324
b518cca6 325 if (Cache[I].InstVerIter(Cache).Downloadable() == false)
6c139d6e
AL
326 continue;
327
74a05226 328 Cache.MarkInstall(I, true, 0, false);
6c139d6e
AL
329 }
330
b2e465d6 331 pkgProblemResolver Fix(&Cache);
6c139d6e
AL
332 return Fix.Resolve(true);
333}
334 /*}}}*/
6c139d6e
AL
335// ProblemResolver::pkgProblemResolver - Constructor /*{{{*/
336// ---------------------------------------------------------------------
337/* */
dcaa1185 338pkgProblemResolver::pkgProblemResolver(pkgDepCache *pCache) : d(NULL), Cache(*pCache)
6c139d6e
AL
339{
340 // Allocate memory
b2e465d6 341 unsigned long Size = Cache.Head().PackageCount;
d0f2c87c 342 Scores = new int[Size];
6c139d6e
AL
343 Flags = new unsigned char[Size];
344 memset(Flags,0,sizeof(*Flags)*Size);
345
346 // Set debug to true to see its decision logic
0a8e3465 347 Debug = _config->FindB("Debug::pkgProblemResolver",false);
6c139d6e
AL
348}
349 /*}}}*/
b2e465d6
AL
350// ProblemResolver::~pkgProblemResolver - Destructor /*{{{*/
351// ---------------------------------------------------------------------
352/* */
353pkgProblemResolver::~pkgProblemResolver()
354{
355 delete [] Scores;
356 delete [] Flags;
357}
358 /*}}}*/
6c139d6e
AL
359// ProblemResolver::ScoreSort - Sort the list by score /*{{{*/
360// ---------------------------------------------------------------------
361/* */
f1f9f9bf 362int pkgProblemResolver::ScoreSort(Package const *A,Package const *B)
6c139d6e 363{
f1f9f9bf 364 if (Scores[A->ID] > Scores[B->ID])
6c139d6e 365 return -1;
f1f9f9bf 366 if (Scores[A->ID] < Scores[B->ID])
6c139d6e
AL
367 return 1;
368 return 0;
369}
370 /*}}}*/
371// ProblemResolver::MakeScores - Make the score table /*{{{*/
372// ---------------------------------------------------------------------
373/* */
374void pkgProblemResolver::MakeScores()
375{
b2e465d6 376 unsigned long Size = Cache.Head().PackageCount;
6c139d6e
AL
377 memset(Scores,0,sizeof(*Scores)*Size);
378
b9179170
MV
379 // maps to pkgCache::State::VerPriority:
380 // Required Important Standard Optional Extra
d0f2c87c 381 int PrioMap[] = {
8b4894fe 382 0,
84de0cea 383 _config->FindI("pkgProblemResolver::Scores::Required",3),
b9179170 384 _config->FindI("pkgProblemResolver::Scores::Important",2),
d0f2c87c
CW
385 _config->FindI("pkgProblemResolver::Scores::Standard",1),
386 _config->FindI("pkgProblemResolver::Scores::Optional",-1),
387 _config->FindI("pkgProblemResolver::Scores::Extra",-2)
8b4894fe 388 };
d0f2c87c
CW
389 int PrioEssentials = _config->FindI("pkgProblemResolver::Scores::Essentials",100);
390 int PrioInstalledAndNotObsolete = _config->FindI("pkgProblemResolver::Scores::NotObsolete",1);
8daf68e3
DK
391 int DepMap[] = {
392 0,
393 _config->FindI("pkgProblemResolver::Scores::Depends",1),
394 _config->FindI("pkgProblemResolver::Scores::PreDepends",1),
395 _config->FindI("pkgProblemResolver::Scores::Suggests",0),
396 _config->FindI("pkgProblemResolver::Scores::Recommends",1),
397 _config->FindI("pkgProblemResolver::Scores::Conflicts",-1),
398 _config->FindI("pkgProblemResolver::Scores::Replaces",0),
399 _config->FindI("pkgProblemResolver::Scores::Obsoletes",0),
400 _config->FindI("pkgProblemResolver::Scores::Breaks",-1),
401 _config->FindI("pkgProblemResolver::Scores::Enhances",0)
402 };
d0f2c87c
CW
403 int AddProtected = _config->FindI("pkgProblemResolver::Scores::AddProtected",10000);
404 int AddEssential = _config->FindI("pkgProblemResolver::Scores::AddEssential",5000);
8b4894fe
MV
405
406 if (_config->FindB("Debug::pkgProblemResolver::ShowScores",false) == true)
407 clog << "Settings used to calculate pkgProblemResolver::Scores::" << endl
84de0cea
MV
408 << " Required => " << PrioMap[pkgCache::State::Required] << endl
409 << " Important => " << PrioMap[pkgCache::State::Important] << endl
410 << " Standard => " << PrioMap[pkgCache::State::Standard] << endl
411 << " Optional => " << PrioMap[pkgCache::State::Optional] << endl
412 << " Extra => " << PrioMap[pkgCache::State::Extra] << endl
8b4894fe
MV
413 << " Essentials => " << PrioEssentials << endl
414 << " InstalledAndNotObsolete => " << PrioInstalledAndNotObsolete << endl
8daf68e3
DK
415 << " Pre-Depends => " << DepMap[pkgCache::Dep::PreDepends] << endl
416 << " Depends => " << DepMap[pkgCache::Dep::Depends] << endl
417 << " Recommends => " << DepMap[pkgCache::Dep::Recommends] << endl
418 << " Suggests => " << DepMap[pkgCache::Dep::Suggests] << endl
419 << " Conflicts => " << DepMap[pkgCache::Dep::Conflicts] << endl
420 << " Breaks => " << DepMap[pkgCache::Dep::DpkgBreaks] << endl
421 << " Replaces => " << DepMap[pkgCache::Dep::Replaces] << endl
422 << " Obsoletes => " << DepMap[pkgCache::Dep::Obsoletes] << endl
423 << " Enhances => " << DepMap[pkgCache::Dep::Enhances] << endl
8b4894fe
MV
424 << " AddProtected => " << AddProtected << endl
425 << " AddEssential => " << AddEssential << endl;
426
6c139d6e 427 // Generate the base scores for a package based on its properties
f7f0d6c7 428 for (pkgCache::PkgIterator I = Cache.PkgBegin(); I.end() == false; ++I)
6c139d6e
AL
429 {
430 if (Cache[I].InstallVer == 0)
431 continue;
432
d0f2c87c 433 int &Score = Scores[I->ID];
6c139d6e 434
7365ff46 435 /* This is arbitrary, it should be high enough to elevate an
6c139d6e
AL
436 essantial package above most other packages but low enough
437 to allow an obsolete essential packages to be removed by
1e3f4083 438 a conflicts on a powerful normal package (ie libc6) */
c5200869
JAK
439 if ((I->Flags & pkgCache::Flag::Essential) == pkgCache::Flag::Essential
440 || (I->Flags & pkgCache::Flag::Important) == pkgCache::Flag::Important)
8b4894fe 441 Score += PrioEssentials;
6c139d6e 442
4cd4a2e7
DK
443 pkgCache::VerIterator const InstVer = Cache[I].InstVerIter(Cache);
444 // We apply priorities only to downloadable packages, all others are prio:extra
445 // as an obsolete prio:standard package can't be that standard anymore…
446 if (InstVer->Priority <= pkgCache::State::Extra && InstVer.Downloadable() == true)
447 Score += PrioMap[InstVer->Priority];
448 else
449 Score += PrioMap[pkgCache::State::Extra];
450
6c139d6e 451 /* This helps to fix oddball problems with conflicting packages
4cd4a2e7
DK
452 on the same level. We enhance the score of installed packages
453 if those are not obsolete */
020daa7b 454 if (I->CurrentVer != 0 && Cache[I].CandidateVer != 0 && Cache[I].CandidateVerIter(Cache).Downloadable())
8b4894fe 455 Score += PrioInstalledAndNotObsolete;
8daf68e3 456
cb4b85b0 457 // propagate score points along dependencies
4cd4a2e7 458 for (pkgCache::DepIterator D = InstVer.DependsList(); D.end() == false; ++D)
9ec748ff
DK
459 {
460 if (DepMap[D->Type] == 0)
461 continue;
462 pkgCache::PkgIterator const T = D.TargetPkg();
463 if (D->Version != 0)
464 {
465 pkgCache::VerIterator const IV = Cache[T].InstVerIter(Cache);
77b6f202 466 if (IV.end() == true || D.IsSatisfied(IV) == false)
9ec748ff
DK
467 continue;
468 }
469 Scores[T->ID] += DepMap[D->Type];
470 }
8daf68e3
DK
471 }
472
6c139d6e 473 // Copy the scores to advoid additive looping
98cc7fd2
JAK
474 std::unique_ptr<int[]> OldScores(new int[Size]);
475 memcpy(OldScores.get(),Scores,sizeof(*Scores)*Size);
6c139d6e
AL
476
477 /* Now we cause 1 level of dependency inheritance, that is we add the
478 score of the packages that depend on the target Package. This
479 fortifies high scoring packages */
f7f0d6c7 480 for (pkgCache::PkgIterator I = Cache.PkgBegin(); I.end() == false; ++I)
6c139d6e
AL
481 {
482 if (Cache[I].InstallVer == 0)
483 continue;
484
f7f0d6c7 485 for (pkgCache::DepIterator D = I.RevDependsList(); D.end() == false; ++D)
6c139d6e
AL
486 {
487 // Only do it for the install version
488 if ((pkgCache::Version *)D.ParentVer() != Cache[D.ParentPkg()].InstallVer ||
3a998f6a
MV
489 (D->Type != pkgCache::Dep::Depends &&
490 D->Type != pkgCache::Dep::PreDepends &&
491 D->Type != pkgCache::Dep::Recommends))
6c139d6e
AL
492 continue;
493
dec5b117
MV
494 // Do not propagate negative scores otherwise
495 // an extra (-2) package might score better than an optional (-1)
496 if (OldScores[D.ParentPkg()->ID] > 0)
497 Scores[I->ID] += OldScores[D.ParentPkg()->ID];
6c139d6e
AL
498 }
499 }
500
1e3f4083 501 /* Now we propagate along provides. This makes the packages that
6c139d6e 502 provide important packages extremely important */
f7f0d6c7 503 for (pkgCache::PkgIterator I = Cache.PkgBegin(); I.end() == false; ++I)
6c139d6e 504 {
f7f0d6c7 505 for (pkgCache::PrvIterator P = I.ProvidesList(); P.end() == false; ++P)
6c139d6e
AL
506 {
507 // Only do it once per package
508 if ((pkgCache::Version *)P.OwnerVer() != Cache[P.OwnerPkg()].InstallVer)
509 continue;
510 Scores[P.OwnerPkg()->ID] += abs(Scores[I->ID] - OldScores[I->ID]);
511 }
512 }
513
514 /* Protected things are pushed really high up. This number should put them
515 ahead of everything */
f7f0d6c7 516 for (pkgCache::PkgIterator I = Cache.PkgBegin(); I.end() == false; ++I)
d2685fd6 517 {
6c139d6e 518 if ((Flags[I->ID] & Protected) != 0)
8b4894fe 519 Scores[I->ID] += AddProtected;
c5200869
JAK
520 if ((I->Flags & pkgCache::Flag::Essential) == pkgCache::Flag::Essential ||
521 (I->Flags & pkgCache::Flag::Important) == pkgCache::Flag::Important)
8b4894fe
MV
522 Scores[I->ID] += AddEssential;
523 }
6c139d6e
AL
524}
525 /*}}}*/
526// ProblemResolver::DoUpgrade - Attempt to upgrade this package /*{{{*/
527// ---------------------------------------------------------------------
528/* This goes through and tries to reinstall packages to make this package
529 installable */
530bool pkgProblemResolver::DoUpgrade(pkgCache::PkgIterator Pkg)
531{
74a05226
MV
532 pkgDepCache::ActionGroup group(Cache);
533
6c139d6e
AL
534 if ((Flags[Pkg->ID] & Upgradable) == 0 || Cache[Pkg].Upgradable() == false)
535 return false;
3a486305
AL
536 if ((Flags[Pkg->ID] & Protected) == Protected)
537 return false;
0a8e3465 538
6c139d6e
AL
539 Flags[Pkg->ID] &= ~Upgradable;
540
541 bool WasKept = Cache[Pkg].Keep();
74a05226 542 Cache.MarkInstall(Pkg, false, 0, false);
6c139d6e 543
0a8e3465
AL
544 // This must be a virtual package or something like that.
545 if (Cache[Pkg].InstVerIter(Cache).end() == true)
546 return false;
547
6c139d6e
AL
548 // Isolate the problem dependency
549 bool Fail = false;
550 for (pkgCache::DepIterator D = Cache[Pkg].InstVerIter(Cache).DependsList(); D.end() == false;)
551 {
552 // Compute a single dependency element (glob or)
553 pkgCache::DepIterator Start = D;
554 pkgCache::DepIterator End = D;
4b1b89c5 555 for (bool LastOR = true; D.end() == false && LastOR == true;)
6c139d6e 556 {
b518cca6 557 LastOR = (D->CompareOp & pkgCache::Dep::Or) == pkgCache::Dep::Or;
0284eee4 558 ++D;
6c139d6e
AL
559 if (LastOR == true)
560 End = D;
561 }
562
563 // We only worry about critical deps.
564 if (End.IsCritical() != true)
565 continue;
4b1b89c5
AL
566
567 // Iterate over all the members in the or group
568 while (1)
0a8e3465 569 {
4b1b89c5
AL
570 // Dep is ok now
571 if ((Cache[End] & pkgDepCache::DepGInstall) == pkgDepCache::DepGInstall)
572 break;
573
574 // Do not change protected packages
575 PkgIterator P = Start.SmartTargetPkg();
576 if ((Flags[P->ID] & Protected) == Protected)
577 {
578 if (Debug == true)
47f6d1b7 579 clog << " Reinst Failed because of protected " << P.FullName(false) << endl;
4b1b89c5 580 Fail = true;
4b1b89c5 581 }
648e3cb4 582 else
6c139d6e 583 {
648e3cb4
AL
584 // Upgrade the package if the candidate version will fix the problem.
585 if ((Cache[Start] & pkgDepCache::DepCVer) == pkgDepCache::DepCVer)
586 {
587 if (DoUpgrade(P) == false)
588 {
589 if (Debug == true)
47f6d1b7 590 clog << " Reinst Failed because of " << P.FullName(false) << endl;
648e3cb4
AL
591 Fail = true;
592 }
593 else
594 {
595 Fail = false;
596 break;
597 }
598 }
599 else
4b1b89c5 600 {
648e3cb4
AL
601 /* We let the algorithm deal with conflicts on its next iteration,
602 it is much smarter than us */
359e46db 603 if (Start.IsNegative() == true)
b2e465d6 604 break;
648e3cb4 605
4b1b89c5 606 if (Debug == true)
47f6d1b7 607 clog << " Reinst Failed early because of " << Start.TargetPkg().FullName(false) << endl;
4b1b89c5 608 Fail = true;
648e3cb4 609 }
4b1b89c5 610 }
6c139d6e 611
4b1b89c5
AL
612 if (Start == End)
613 break;
f7f0d6c7 614 ++Start;
4b1b89c5
AL
615 }
616 if (Fail == true)
6c139d6e 617 break;
6c139d6e
AL
618 }
619
620 // Undo our operations - it might be smart to undo everything this did..
621 if (Fail == true)
622 {
623 if (WasKept == true)
74a05226 624 Cache.MarkKeep(Pkg, false, false);
6c139d6e 625 else
b83cad32 626 Cache.MarkDelete(Pkg, false, 0, false);
6c139d6e
AL
627 return false;
628 }
629
630 if (Debug == true)
47f6d1b7 631 clog << " Re-Instated " << Pkg.FullName(false) << endl;
6c139d6e
AL
632 return true;
633}
634 /*}}}*/
6d38011b 635// ProblemResolver::Resolve - calls a resolver to fix the situation /*{{{*/
2a884c61 636bool pkgProblemResolver::Resolve(bool BrokenFix, OpProgress * const Progress)
6d38011b 637{
98278a81 638 std::string const solver = _config->Find("APT::Solver", "internal");
2a884c61
DK
639 if (solver != "internal")
640 return EDSP::ResolveExternal(solver.c_str(), Cache, false, false, false, Progress);
6d38011b
DK
641 return ResolveInternal(BrokenFix);
642}
643 /*}}}*/
644// ProblemResolver::ResolveInternal - Run the resolution pass /*{{{*/
6c139d6e
AL
645// ---------------------------------------------------------------------
646/* This routines works by calculating a score for each package. The score
647 is derived by considering the package's priority and all reverse
648 dependents giving an integer that reflects the amount of breakage that
649 adjusting the package will inflict.
650
651 It goes from highest score to lowest and corrects all of the breaks by
1e3f4083 652 keeping or removing the dependent packages. If that fails then it removes
6c139d6e
AL
653 the package itself and goes on. The routine should be able to intelligently
654 go from any broken state to a fixed state.
655
656 The BrokenFix flag enables a mode where the algorithm tries to
657 upgrade packages to advoid problems. */
6d38011b 658bool pkgProblemResolver::ResolveInternal(bool const BrokenFix)
6c139d6e 659{
74a05226
MV
660 pkgDepCache::ActionGroup group(Cache);
661
6c139d6e
AL
662 // Record which packages are marked for install
663 bool Again = false;
664 do
665 {
666 Again = false;
f7f0d6c7 667 for (pkgCache::PkgIterator I = Cache.PkgBegin(); I.end() == false; ++I)
6c139d6e
AL
668 {
669 if (Cache[I].Install() == true)
670 Flags[I->ID] |= PreInstalled;
671 else
672 {
673 if (Cache[I].InstBroken() == true && BrokenFix == true)
674 {
74a05226 675 Cache.MarkInstall(I, false, 0, false);
6c139d6e
AL
676 if (Cache[I].Install() == true)
677 Again = true;
678 }
679
680 Flags[I->ID] &= ~PreInstalled;
681 }
682 Flags[I->ID] |= Upgradable;
683 }
684 }
685 while (Again == true);
686
32b5dd08 687 if (Debug == true) {
49b49018
MV
688 clog << "Starting pkgProblemResolver with broken count: "
689 << Cache.BrokenCount() << endl;
32b5dd08 690 }
6c139d6e
AL
691
692 MakeScores();
6d38011b
DK
693
694 unsigned long const Size = Cache.Head().PackageCount;
695
6c139d6e
AL
696 /* We have to order the packages so that the broken fixing pass
697 operates from highest score to lowest. This prevents problems when
698 high score packages cause the removal of lower score packages that
699 would cause the removal of even lower score packages. */
98cc7fd2
JAK
700 std::unique_ptr<pkgCache::Package *[]> PList(new pkgCache::Package *[Size]);
701 pkgCache::Package **PEnd = PList.get();
f7f0d6c7 702 for (pkgCache::PkgIterator I = Cache.PkgBegin(); I.end() == false; ++I)
6c139d6e 703 *PEnd++ = I;
f1f9f9bf
JAK
704
705 std::sort(PList.get(), PEnd, [this](Package *a, Package *b) { return ScoreSort(a, b) < 0; });
8b4894fe
MV
706
707 if (_config->FindB("Debug::pkgProblemResolver::ShowScores",false) == true)
708 {
709 clog << "Show Scores" << endl;
98cc7fd2 710 for (pkgCache::Package **K = PList.get(); K != PEnd; K++)
8b4894fe
MV
711 if (Scores[(*K)->ID] != 0)
712 {
713 pkgCache::PkgIterator Pkg(Cache,*K);
84573326 714 clog << Scores[(*K)->ID] << ' ' << APT::PrettyPkg(&Cache, Pkg) << std::endl;
8b4894fe
MV
715 }
716 }
6c139d6e 717
32b5dd08 718 if (Debug == true) {
49b49018
MV
719 clog << "Starting 2 pkgProblemResolver with broken count: "
720 << Cache.BrokenCount() << endl;
32b5dd08 721 }
8b4894fe 722
6c139d6e
AL
723 /* Now consider all broken packages. For each broken package we either
724 remove the package or fix it's problem. We do this once, it should
725 not be possible for a loop to form (that is a < b < c and fixing b by
726 changing a breaks c) */
727 bool Change = true;
09a10f9c 728 bool const TryFixByInstall = _config->FindB("pkgProblemResolver::FixByInstall", true);
f99b0621 729 std::vector<PackageKill> KillList;
6c139d6e
AL
730 for (int Counter = 0; Counter != 10 && Change == true; Counter++)
731 {
732 Change = false;
98cc7fd2 733 for (pkgCache::Package **K = PList.get(); K != PEnd; K++)
6c139d6e
AL
734 {
735 pkgCache::PkgIterator I(Cache,*K);
736
737 /* We attempt to install this and see if any breaks result,
738 this takes care of some strange cases */
739 if (Cache[I].CandidateVer != Cache[I].InstallVer &&
740 I->CurrentVer != 0 && Cache[I].InstallVer != 0 &&
741 (Flags[I->ID] & PreInstalled) != 0 &&
0a8e3465
AL
742 (Flags[I->ID] & Protected) == 0 &&
743 (Flags[I->ID] & ReInstateTried) == 0)
6c139d6e
AL
744 {
745 if (Debug == true)
09a10f9c 746 clog << " Try to Re-Instate (" << Counter << ") " << I.FullName(false) << endl;
a6568219 747 unsigned long OldBreaks = Cache.BrokenCount();
6c139d6e 748 pkgCache::Version *OldVer = Cache[I].InstallVer;
0a8e3465
AL
749 Flags[I->ID] &= ReInstateTried;
750
74a05226 751 Cache.MarkInstall(I, false, 0, false);
6c139d6e
AL
752 if (Cache[I].InstBroken() == true ||
753 OldBreaks < Cache.BrokenCount())
754 {
755 if (OldVer == 0)
b83cad32 756 Cache.MarkDelete(I, false, 0, false);
6c139d6e 757 else
74a05226 758 Cache.MarkKeep(I, false, false);
6c139d6e
AL
759 }
760 else
761 if (Debug == true)
47f6d1b7 762 clog << "Re-Instated " << I.FullName(false) << " (" << OldBreaks << " vs " << Cache.BrokenCount() << ')' << endl;
6c139d6e
AL
763 }
764
765 if (Cache[I].InstallVer == 0 || Cache[I].InstBroken() == false)
766 continue;
767
00b47c98 768 if (Debug == true)
84573326 769 clog << "Investigating (" << Counter << ") " << APT::PrettyPkg(&Cache, I) << endl;
00b47c98 770
6c139d6e 771 // Isolate the problem dependency
421c8d10
AL
772 bool InOr = false;
773 pkgCache::DepIterator Start;
774 pkgCache::DepIterator End;
f99b0621
JAK
775 size_t OldSize = 0;
776
777 KillList.resize(0);
648e3cb4
AL
778
779 enum {OrRemove,OrKeep} OrOp = OrRemove;
421c8d10
AL
780 for (pkgCache::DepIterator D = Cache[I].InstVerIter(Cache).DependsList();
781 D.end() == false || InOr == true;)
6c139d6e
AL
782 {
783 // Compute a single dependency element (glob or)
648e3cb4
AL
784 if (Start == End)
785 {
786 // Decide what to do
f99b0621 787 if (InOr == true && OldSize == KillList.size())
648e3cb4 788 {
09a10f9c 789 if (OrOp == OrRemove)
70777d4b
AL
790 {
791 if ((Flags[I->ID] & Protected) != Protected)
00b47c98
AL
792 {
793 if (Debug == true)
47f6d1b7 794 clog << " Or group remove for " << I.FullName(false) << endl;
b83cad32 795 Cache.MarkDelete(I, false, 0, false);
cd14eaf2 796 Change = true;
09a10f9c
DK
797 }
798 }
799 else if (OrOp == OrKeep)
00b47c98
AL
800 {
801 if (Debug == true)
47f6d1b7 802 clog << " Or group keep for " << I.FullName(false) << endl;
74a05226 803 Cache.MarkKeep(I, false, false);
cd14eaf2 804 Change = true;
b2e465d6 805 }
648e3cb4
AL
806 }
807
b2e465d6
AL
808 /* We do an extra loop (as above) to finalize the or group
809 processing */
810 InOr = false;
648e3cb4 811 OrOp = OrRemove;
421c8d10 812 D.GlobOr(Start,End);
b2e465d6
AL
813 if (Start.end() == true)
814 break;
cd14eaf2 815
b2e465d6
AL
816 // We only worry about critical deps.
817 if (End.IsCritical() != true)
818 continue;
cd14eaf2 819
648e3cb4 820 InOr = Start != End;
f99b0621 821 OldSize = KillList.size();
cd14eaf2 822 }
421c8d10 823 else
4cc152f9 824 {
f7f0d6c7 825 ++Start;
4cc152f9
MV
826 // We only worry about critical deps.
827 if (Start.IsCritical() != true)
828 continue;
829 }
cd14eaf2 830
6c139d6e
AL
831 // Dep is ok
832 if ((Cache[End] & pkgDepCache::DepGInstall) == pkgDepCache::DepGInstall)
cd14eaf2
AL
833 {
834 InOr = false;
6c139d6e 835 continue;
cd14eaf2
AL
836 }
837
6c139d6e 838 if (Debug == true)
84573326 839 clog << "Broken " << APT::PrettyDep(&Cache, Start) << endl;
fcf85120
AL
840
841 /* Look across the version list. If there are no possible
842 targets then we keep the package and bail. This is necessary
1e3f4083 843 if a package has a dep on another package that can't be found */
98cc7fd2
JAK
844 std::unique_ptr<pkgCache::Version *[]> VList(Start.AllTargets());
845 if (VList[0] == 0 && (Flags[I->ID] & Protected) != Protected &&
359e46db 846 Start.IsNegative() == false &&
fcf85120 847 Cache[I].NowBroken() == false)
648e3cb4
AL
848 {
849 if (InOr == true)
850 {
851 /* No keep choice because the keep being OK could be the
852 result of another element in the OR group! */
853 continue;
854 }
855
fcf85120 856 Change = true;
74a05226 857 Cache.MarkKeep(I, false, false);
fcf85120
AL
858 break;
859 }
860
6c139d6e 861 bool Done = false;
98cc7fd2 862 for (pkgCache::Version **V = VList.get(); *V != 0; V++)
6c139d6e
AL
863 {
864 pkgCache::VerIterator Ver(Cache,*V);
865 pkgCache::PkgIterator Pkg = Ver.ParentPkg();
a6bfe583 866
2ba99db8
MV
867 /* This is a conflicts, and the version we are looking
868 at is not the currently selected version of the
869 package, which means it is not necessary to
870 remove/keep */
359e46db 871 if (Cache[Pkg].InstallVer != Ver && Start.IsNegative() == true)
4429616b 872 {
2ba99db8
MV
873 if (Debug)
874 clog << " Conflicts//Breaks against version "
875 << Ver.VerStr() << " for " << Pkg.Name()
876 << " but that is not InstVer, ignoring"
24e93662 877 << endl;
2ba99db8 878 continue;
4429616b
MV
879 }
880
6c139d6e 881 if (Debug == true)
e788a834
DK
882 clog << " Considering " << Pkg.FullName(false) << ' ' << Scores[Pkg->ID] <<
883 " as a solution to " << I.FullName(false) << ' ' << Scores[I->ID] << endl;
a6bfe583
AL
884
885 /* Try to fix the package under consideration rather than
886 fiddle with the VList package */
6c139d6e 887 if (Scores[I->ID] <= Scores[Pkg->ID] ||
421c8d10 888 ((Cache[Start] & pkgDepCache::DepNow) == 0 &&
359e46db 889 End.IsNegative() == false))
6c139d6e 890 {
200f8c52 891 // Try a little harder to fix protected packages..
3b5421b4 892 if ((Flags[I->ID] & Protected) == Protected)
200f8c52
AL
893 {
894 if (DoUpgrade(Pkg) == true)
0296c633 895 {
b2e465d6
AL
896 if (Scores[Pkg->ID] > Scores[I->ID])
897 Scores[Pkg->ID] = Scores[I->ID];
0296c633
AL
898 break;
899 }
900
6c139d6e 901 continue;
200f8c52
AL
902 }
903
904 /* See if a keep will do, unless the package is protected,
648e3cb4
AL
905 then installing it will be necessary */
906 bool Installed = Cache[I].Install();
74a05226 907 Cache.MarkKeep(I, false, false);
6c139d6e
AL
908 if (Cache[I].InstBroken() == false)
909 {
648e3cb4
AL
910 // Unwind operation will be keep now
911 if (OrOp == OrRemove)
912 OrOp = OrKeep;
913
914 // Restore
915 if (InOr == true && Installed == true)
74a05226 916 Cache.MarkInstall(I, false, 0, false);
648e3cb4 917
6c139d6e 918 if (Debug == true)
47f6d1b7 919 clog << " Holding Back " << I.FullName(false) << " rather than change " << Start.TargetPkg().FullName(false) << endl;
6c139d6e
AL
920 }
921 else
421c8d10 922 {
6c139d6e
AL
923 if (BrokenFix == false || DoUpgrade(I) == false)
924 {
421c8d10 925 // Consider other options
87da7451 926 if (InOr == false || Cache[I].Garbage == true)
421c8d10 927 {
6910a2ac 928 if (Debug == true)
47f6d1b7 929 clog << " Removing " << I.FullName(false) << " rather than change " << Start.TargetPkg().FullName(false) << endl;
b83cad32 930 Cache.MarkDelete(I, false, 0, false);
6910a2ac
DK
931 if (Counter > 1 && Scores[Pkg->ID] > Scores[I->ID])
932 Scores[I->ID] = Scores[Pkg->ID];
d6ebeb21 933 }
09a10f9c
DK
934 else if (TryFixByInstall == true &&
935 Start.TargetPkg()->CurrentVer == 0 &&
936 Cache[Start.TargetPkg()].Delete() == false &&
a3f1a6cc 937 (Flags[Start.TargetPkg()->ID] & ToRemove) != ToRemove &&
294a8020 938 Cache.GetCandidateVersion(Start.TargetPkg()).end() == false)
09a10f9c
DK
939 {
940 /* Before removing or keeping the package with the broken dependency
941 try instead to install the first not previously installed package
942 solving this dependency. This helps every time a previous solver
943 is removed by the resolver because of a conflict or alike but it is
944 dangerous as it could trigger new breaks/conflicts… */
443266ef 945 if (Debug == true)
84573326 946 clog << " Try Installing " << APT::PrettyPkg(&Cache, Start.TargetPkg()) << " before changing " << I.FullName(false) << std::endl;
09a10f9c
DK
947 unsigned long const OldBroken = Cache.BrokenCount();
948 Cache.MarkInstall(Start.TargetPkg(), true, 1, false);
949 // FIXME: we should undo the complete MarkInstall process here
950 if (Cache[Start.TargetPkg()].InstBroken() == true || Cache.BrokenCount() > OldBroken)
951 Cache.MarkDelete(Start.TargetPkg(), false, 1, false);
952 }
0a8e3465 953 }
6c139d6e 954 }
b5dc9785 955
6c139d6e
AL
956 Change = true;
957 Done = true;
958 break;
959 }
960 else
961 {
308c7d30
IJ
962 if (Start->Type == pkgCache::Dep::DpkgBreaks)
963 {
76264cb7
MV
964 // first, try upgradring the package, if that
965 // does not help, the breaks goes onto the
966 // kill list
2ba99db8 967 //
76264cb7 968 // FIXME: use DoUpgrade(Pkg) instead?
2ba99db8 969 if (Cache[End] & pkgDepCache::DepGCVer)
76264cb7 970 {
308c7d30 971 if (Debug)
47f6d1b7 972 clog << " Upgrading " << Pkg.FullName(false) << " due to Breaks field in " << I.FullName(false) << endl;
308c7d30
IJ
973 Cache.MarkInstall(Pkg, false, 0, false);
974 continue;
975 }
308c7d30
IJ
976 }
977
648e3cb4 978 // Skip adding to the kill list if it is protected
6c139d6e
AL
979 if ((Flags[Pkg->ID] & Protected) != 0)
980 continue;
a6bfe583
AL
981
982 if (Debug == true)
47f6d1b7 983 clog << " Added " << Pkg.FullName(false) << " to the remove list" << endl;
f99b0621
JAK
984
985 KillList.push_back({Pkg, End});
0a8e3465 986
b47053bd 987 if (Start.IsNegative() == false)
6c139d6e
AL
988 break;
989 }
990 }
991
992 // Hm, nothing can possibly satisify this dep. Nuke it.
359e46db
DK
993 if (VList[0] == 0 &&
994 Start.IsNegative() == false &&
648e3cb4 995 (Flags[I->ID] & Protected) != Protected)
6c139d6e 996 {
648e3cb4 997 bool Installed = Cache[I].Install();
6c139d6e
AL
998 Cache.MarkKeep(I);
999 if (Cache[I].InstBroken() == false)
1000 {
648e3cb4
AL
1001 // Unwind operation will be keep now
1002 if (OrOp == OrRemove)
1003 OrOp = OrKeep;
1004
1005 // Restore
1006 if (InOr == true && Installed == true)
74a05226 1007 Cache.MarkInstall(I, false, 0, false);
648e3cb4 1008
6c139d6e 1009 if (Debug == true)
47f6d1b7 1010 clog << " Holding Back " << I.FullName(false) << " because I can't find " << Start.TargetPkg().FullName(false) << endl;
6c139d6e
AL
1011 }
1012 else
1013 {
1014 if (Debug == true)
47f6d1b7 1015 clog << " Removing " << I.FullName(false) << " because I can't find " << Start.TargetPkg().FullName(false) << endl;
648e3cb4 1016 if (InOr == false)
b83cad32 1017 Cache.MarkDelete(I, false, 0, false);
6c139d6e
AL
1018 }
1019
1020 Change = true;
1021 Done = true;
1022 }
1023
421c8d10
AL
1024 // Try some more
1025 if (InOr == true)
1026 continue;
1027
6c139d6e
AL
1028 if (Done == true)
1029 break;
1030 }
1031
1032 // Apply the kill list now
1033 if (Cache[I].InstallVer != 0)
648e3cb4 1034 {
f99b0621 1035 for (auto J = KillList.begin(); J != KillList.end(); J++)
6c139d6e 1036 {
648e3cb4
AL
1037 Change = true;
1038 if ((Cache[J->Dep] & pkgDepCache::DepGNow) == 0)
1039 {
359e46db 1040 if (J->Dep.IsNegative() == true)
648e3cb4
AL
1041 {
1042 if (Debug == true)
47f6d1b7 1043 clog << " Fixing " << I.FullName(false) << " via remove of " << J->Pkg.FullName(false) << endl;
b83cad32 1044 Cache.MarkDelete(J->Pkg, false, 0, false);
648e3cb4
AL
1045 }
1046 }
1047 else
6c139d6e
AL
1048 {
1049 if (Debug == true)
47f6d1b7 1050 clog << " Fixing " << I.FullName(false) << " via keep of " << J->Pkg.FullName(false) << endl;
74a05226 1051 Cache.MarkKeep(J->Pkg, false, false);
6c139d6e 1052 }
b2e465d6 1053
648e3cb4 1054 if (Counter > 1)
b2e465d6
AL
1055 {
1056 if (Scores[I->ID] > Scores[J->Pkg->ID])
1057 Scores[J->Pkg->ID] = Scores[I->ID];
1058 }
648e3cb4
AL
1059 }
1060 }
1061 }
6c139d6e
AL
1062 }
1063
1064 if (Debug == true)
0a8e3465 1065 clog << "Done" << endl;
b2e465d6 1066
6c139d6e 1067 if (Cache.BrokenCount() != 0)
b5dc9785
AL
1068 {
1069 // See if this is the result of a hold
1070 pkgCache::PkgIterator I = Cache.PkgBegin();
f7f0d6c7 1071 for (;I.end() != true; ++I)
b5dc9785
AL
1072 {
1073 if (Cache[I].InstBroken() == false)
1074 continue;
1075 if ((Flags[I->ID] & Protected) != Protected)
b2e465d6 1076 return _error->Error(_("Error, pkgProblemResolver::Resolve generated breaks, this may be caused by held packages."));
b5dc9785 1077 }
b2e465d6 1078 return _error->Error(_("Unable to correct problems, you have held broken packages."));
b5dc9785
AL
1079 }
1080
fce72602 1081 // set the auto-flags (mvo: I'm not sure if we _really_ need this)
80fa0d8a 1082 pkgCache::PkgIterator I = Cache.PkgBegin();
f7f0d6c7 1083 for (;I.end() != true; ++I) {
80fa0d8a 1084 if (Cache[I].NewInstall() && !(Flags[I->ID] & PreInstalled)) {
120365ce 1085 if(_config->FindI("Debug::pkgAutoRemove",false)) {
47f6d1b7 1086 std::clog << "Resolve installed new pkg: " << I.FullName(false)
120365ce
MV
1087 << " (now marking it as auto)" << std::endl;
1088 }
80fa0d8a
MV
1089 Cache[I].Flags |= pkgCache::Flag::Auto;
1090 }
1091 }
1092
1093
0a8e3465
AL
1094 return true;
1095}
1096 /*}}}*/
953b348c
MV
1097// ProblemResolver::BreaksInstOrPolicy - Check if the given pkg is broken/*{{{*/
1098// ---------------------------------------------------------------------
1099/* This checks if the given package is broken either by a hard dependency
1100 (InstBroken()) or by introducing a new policy breakage e.g. new
1101 unsatisfied recommends for a package that was in "policy-good" state
1102
1103 Note that this is not perfect as it will ignore further breakage
1104 for already broken policy (recommends)
1105*/
1106bool pkgProblemResolver::InstOrNewPolicyBroken(pkgCache::PkgIterator I)
1107{
953b348c
MV
1108 // a broken install is always a problem
1109 if (Cache[I].InstBroken() == true)
deec6474
DK
1110 {
1111 if (Debug == true)
84573326 1112 std::clog << " Dependencies are not satisfied for " << APT::PrettyPkg(&Cache, I) << std::endl;
953b348c 1113 return true;
deec6474 1114 }
953b348c
MV
1115
1116 // a newly broken policy (recommends/suggests) is a problem
1117 if (Cache[I].NowPolicyBroken() == false &&
1118 Cache[I].InstPolicyBroken() == true)
deec6474
DK
1119 {
1120 if (Debug == true)
84573326 1121 std::clog << " Policy breaks with upgrade of " << APT::PrettyPkg(&Cache, I) << std::endl;
953b348c 1122 return true;
deec6474
DK
1123 }
1124
953b348c
MV
1125 return false;
1126}
36a171e1 1127 /*}}}*/
0a8e3465
AL
1128// ProblemResolver::ResolveByKeep - Resolve problems using keep /*{{{*/
1129// ---------------------------------------------------------------------
1130/* This is the work horse of the soft upgrade routine. It is very gental
1131 in that it does not install or remove any packages. It is assumed that the
1132 system was non-broken previously. */
2a884c61 1133bool pkgProblemResolver::ResolveByKeep(OpProgress * const Progress)
741b7da9 1134{
98278a81 1135 std::string const solver = _config->Find("APT::Solver", "internal");
2a884c61
DK
1136 if (solver != "internal")
1137 return EDSP::ResolveExternal(solver.c_str(), Cache, true, false, false, Progress);
741b7da9
DK
1138 return ResolveByKeepInternal();
1139}
1140 /*}}}*/
1141// ProblemResolver::ResolveByKeepInternal - Resolve problems using keep /*{{{*/
1142// ---------------------------------------------------------------------
1143/* This is the work horse of the soft upgrade routine. It is very gental
1144 in that it does not install or remove any packages. It is assumed that the
1145 system was non-broken previously. */
1146bool pkgProblemResolver::ResolveByKeepInternal()
0a8e3465 1147{
74a05226
MV
1148 pkgDepCache::ActionGroup group(Cache);
1149
b2e465d6 1150 unsigned long Size = Cache.Head().PackageCount;
0a8e3465 1151
0a8e3465
AL
1152 MakeScores();
1153
1154 /* We have to order the packages so that the broken fixing pass
1155 operates from highest score to lowest. This prevents problems when
1156 high score packages cause the removal of lower score packages that
1157 would cause the removal of even lower score packages. */
1158 pkgCache::Package **PList = new pkgCache::Package *[Size];
1159 pkgCache::Package **PEnd = PList;
f7f0d6c7 1160 for (pkgCache::PkgIterator I = Cache.PkgBegin(); I.end() == false; ++I)
0a8e3465 1161 *PEnd++ = I;
f1f9f9bf
JAK
1162
1163 std::sort(PList,PEnd,[this](Package *a, Package *b) { return ScoreSort(a, b) < 0; });
1164
8b4894fe
MV
1165
1166 if (_config->FindB("Debug::pkgProblemResolver::ShowScores",false) == true)
1167 {
1168 clog << "Show Scores" << endl;
1169 for (pkgCache::Package **K = PList; K != PEnd; K++)
1170 if (Scores[(*K)->ID] != 0)
1171 {
1172 pkgCache::PkgIterator Pkg(Cache,*K);
84573326 1173 clog << Scores[(*K)->ID] << ' ' << APT::PrettyPkg(&Cache, Pkg) << std::endl;
8b4894fe
MV
1174 }
1175 }
1176
1177 if (Debug == true)
1178 clog << "Entering ResolveByKeep" << endl;
1179
0a8e3465
AL
1180 // Consider each broken package
1181 pkgCache::Package **LastStop = 0;
1182 for (pkgCache::Package **K = PList; K != PEnd; K++)
1183 {
1184 pkgCache::PkgIterator I(Cache,*K);
1185
953b348c 1186 if (Cache[I].InstallVer == 0)
0a8e3465
AL
1187 continue;
1188
953b348c
MV
1189 if (InstOrNewPolicyBroken(I) == false)
1190 continue;
1191
0a8e3465 1192 /* Keep the package. If this works then great, otherwise we have
1e3f4083 1193 to be significantly more aggressive and manipulate its dependencies */
0a8e3465
AL
1194 if ((Flags[I->ID] & Protected) == 0)
1195 {
1196 if (Debug == true)
47f6d1b7 1197 clog << "Keeping package " << I.FullName(false) << endl;
74a05226 1198 Cache.MarkKeep(I, false, false);
953b348c 1199 if (InstOrNewPolicyBroken(I) == false)
0a8e3465 1200 {
b2e465d6 1201 K = PList - 1;
0a8e3465
AL
1202 continue;
1203 }
1204 }
1205
1206 // Isolate the problem dependencies
1207 for (pkgCache::DepIterator D = Cache[I].InstVerIter(Cache).DependsList(); D.end() == false;)
1208 {
c5532863
AL
1209 DepIterator Start;
1210 DepIterator End;
1211 D.GlobOr(Start,End);
1212
0a8e3465
AL
1213 // We only worry about critical deps.
1214 if (End.IsCritical() != true)
1215 continue;
1216
1217 // Dep is ok
1218 if ((Cache[End] & pkgDepCache::DepGInstall) == pkgDepCache::DepGInstall)
1219 continue;
c5532863
AL
1220
1221 /* Hm, the group is broken.. I suppose the best thing to do is to
1222 is to try every combination of keep/not-keep for the set, but thats
1223 slow, and this never happens, just be conservative and assume the
1224 list of ors is in preference and keep till it starts to work. */
1225 while (true)
0a8e3465 1226 {
c5532863 1227 if (Debug == true)
84573326 1228 clog << "Package " << I.FullName(false) << " " << APT::PrettyDep(&Cache, Start) << endl;
8b4894fe 1229
c5532863 1230 // Look at all the possible provides on this package
98cc7fd2
JAK
1231 std::unique_ptr<pkgCache::Version *[]> VList(Start.AllTargets());
1232 for (pkgCache::Version **V = VList.get(); *V != 0; V++)
0a8e3465 1233 {
c5532863
AL
1234 pkgCache::VerIterator Ver(Cache,*V);
1235 pkgCache::PkgIterator Pkg = Ver.ParentPkg();
1236
1237 // It is not keepable
1238 if (Cache[Pkg].InstallVer == 0 ||
1239 Pkg->CurrentVer == 0)
1240 continue;
1241
1242 if ((Flags[I->ID] & Protected) == 0)
1243 {
1244 if (Debug == true)
47f6d1b7 1245 clog << " Keeping Package " << Pkg.FullName(false) << " due to " << Start.DepType() << endl;
74a05226 1246 Cache.MarkKeep(Pkg, false, false);
c5532863
AL
1247 }
1248
953b348c 1249 if (InstOrNewPolicyBroken(I) == false)
c5532863 1250 break;
0a8e3465
AL
1251 }
1252
953b348c 1253 if (InstOrNewPolicyBroken(I) == false)
0a8e3465 1254 break;
0a8e3465 1255
c5532863
AL
1256 if (Start == End)
1257 break;
f7f0d6c7 1258 ++Start;
c5532863
AL
1259 }
1260
953b348c 1261 if (InstOrNewPolicyBroken(I) == false)
0a8e3465
AL
1262 break;
1263 }
1264
953b348c 1265 if (InstOrNewPolicyBroken(I) == true)
0a8e3465
AL
1266 continue;
1267
1268 // Restart again.
0291f645
JT
1269 if (K == LastStop) {
1270 // I is an iterator based off our temporary package list,
1271 // so copy the name we need before deleting the temporary list
1272 std::string const LoopingPackage = I.FullName(false);
1273 delete[] PList;
1274 return _error->Error("Internal Error, pkgProblemResolver::ResolveByKeep is looping on package %s.", LoopingPackage.c_str());
1275 }
0a8e3465 1276 LastStop = K;
b2e465d6 1277 K = PList - 1;
0291f645 1278 }
6c139d6e 1279
0291f645 1280 delete[] PList;
6c139d6e
AL
1281 return true;
1282}
1283 /*}}}*/
f3c736f9 1284// ProblemResolver::InstallProtect - deprecated cpu-eating no-op /*{{{*/
3b5421b4 1285// ---------------------------------------------------------------------
f3c736f9
DK
1286/* Actions issued with FromUser bit set are protected from further
1287 modification (expect by other calls with FromUser set) nowadays , so we
1288 don't need to reissue actions here, they are already set in stone. */
3b5421b4
AL
1289void pkgProblemResolver::InstallProtect()
1290{
74a05226
MV
1291 pkgDepCache::ActionGroup group(Cache);
1292
f7f0d6c7 1293 for (pkgCache::PkgIterator I = Cache.PkgBegin(); I.end() == false; ++I)
3b5421b4
AL
1294 {
1295 if ((Flags[I->ID] & Protected) == Protected)
1296 {
1297 if ((Flags[I->ID] & ToRemove) == ToRemove)
1298 Cache.MarkDelete(I);
c15f5690
MV
1299 else
1300 {
da543ed8
OS
1301 // preserve the information whether the package was auto
1302 // or manually installed
c15f5690
MV
1303 bool autoInst = (Cache[I].Flags & pkgCache::Flag::Auto);
1304 Cache.MarkInstall(I, false, 0, !autoInst);
1305 }
3b5421b4
AL
1306 }
1307 }
1308}
1309 /*}}}*/
b2e465d6
AL
1310// PrioSortList - Sort a list of versions by priority /*{{{*/
1311// ---------------------------------------------------------------------
1312/* This is ment to be used in conjunction with AllTargets to get a list
1313 of versions ordered by preference. */
f1f9f9bf
JAK
1314
1315struct PrioComp {
1316 pkgCache &PrioCache;
1317
258b9e51 1318 explicit PrioComp(pkgCache &PrioCache) : PrioCache(PrioCache) {
f1f9f9bf
JAK
1319 }
1320
1321 bool operator() (pkgCache::Version * const &A, pkgCache::Version * const &B) {
1322 return compare(A, B) < 0;
1323 }
1324
1325 int compare(pkgCache::Version * const &A, pkgCache::Version * const &B) {
1326 pkgCache::VerIterator L(PrioCache,A);
1327 pkgCache::VerIterator R(PrioCache,B);
1328
1329 if ((L.ParentPkg()->Flags & pkgCache::Flag::Essential) == pkgCache::Flag::Essential &&
1330 (R.ParentPkg()->Flags & pkgCache::Flag::Essential) != pkgCache::Flag::Essential)
1331 return 1;
1332 if ((L.ParentPkg()->Flags & pkgCache::Flag::Essential) != pkgCache::Flag::Essential &&
1333 (R.ParentPkg()->Flags & pkgCache::Flag::Essential) == pkgCache::Flag::Essential)
1334 return -1;
1335
1336 if ((L.ParentPkg()->Flags & pkgCache::Flag::Important) == pkgCache::Flag::Important &&
1337 (R.ParentPkg()->Flags & pkgCache::Flag::Important) != pkgCache::Flag::Important)
1338 return 1;
1339 if ((L.ParentPkg()->Flags & pkgCache::Flag::Important) != pkgCache::Flag::Important &&
1340 (R.ParentPkg()->Flags & pkgCache::Flag::Important) == pkgCache::Flag::Important)
1341 return -1;
1342
1343 if (L->Priority != R->Priority)
1344 return R->Priority - L->Priority;
1345 return strcmp(L.ParentPkg().Name(),R.ParentPkg().Name());
1346 }
1347};
1348
b2e465d6
AL
1349void pkgPrioSortList(pkgCache &Cache,pkgCache::Version **List)
1350{
1351 unsigned long Count = 0;
b2e465d6
AL
1352 for (pkgCache::Version **I = List; *I != 0; I++)
1353 Count++;
f1f9f9bf 1354 std::sort(List,List+Count,PrioComp(Cache));
b2e465d6
AL
1355}
1356 /*}}}*/