merged trunk
[ntk/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>
0a57c0f0 22#include <apt-pkg/version.h>
b2e465d6 23#include <apt-pkg/sptr.h>
760d4968 24#include <apt-pkg/acquire-item.h>
c3b85126 25#include <apt-pkg/edsp.h>
472ff00e
DK
26#include <apt-pkg/sourcelist.h>
27#include <apt-pkg/fileutl.h>
28#include <apt-pkg/progress.h>
6d38011b 29
22dcc318 30#include <sys/types.h>
152ab79e
MV
31#include <cstdlib>
32#include <algorithm>
90f057fd 33#include <iostream>
6d38011b 34#include <stdio.h>
ea542140
DK
35
36#include <apti18n.h>
6c139d6e 37 /*}}}*/
584e4558 38using namespace std;
6c139d6e
AL
39
40pkgProblemResolver *pkgProblemResolver::This = 0;
41
42// Simulate::Simulate - Constructor /*{{{*/
43// ---------------------------------------------------------------------
b2e465d6
AL
44/* The legacy translations here of input Pkg iterators is obsolete,
45 this is not necessary since the pkgCaches are fully shared now. */
46pkgSimulate::pkgSimulate(pkgDepCache *Cache) : pkgPackageManager(Cache),
47 iPolicy(Cache),
496d5c70
MV
48 Sim(&Cache->GetCache(),&iPolicy),
49 group(Sim)
6c139d6e 50{
b2e465d6
AL
51 Sim.Init(0);
52 Flags = new unsigned char[Cache->Head().PackageCount];
53 memset(Flags,0,sizeof(*Flags)*Cache->Head().PackageCount);
281daf46
AL
54
55 // Fake a filename so as not to activate the media swapping
56 string Jnk = "SIMULATE";
b2e465d6 57 for (unsigned int I = 0; I != Cache->Head().PackageCount; I++)
281daf46 58 FileNames[I] = Jnk;
6c139d6e
AL
59}
60 /*}}}*/
b270388b
MV
61// Simulate::~Simulate - Destructor /*{{{*/
62pkgSimulate::~pkgSimulate()
63{
64 delete[] Flags;
65}
66 /*}}}*/
b2e465d6
AL
67// Simulate::Describe - Describe a package /*{{{*/
68// ---------------------------------------------------------------------
3826564e
MZ
69/* Parameter Current == true displays the current package version,
70 Parameter Candidate == true displays the candidate package version */
71void pkgSimulate::Describe(PkgIterator Pkg,ostream &out,bool Current,bool Candidate)
b2e465d6
AL
72{
73 VerIterator Ver(Sim);
e59458f7 74
47f6d1b7 75 out << Pkg.FullName(true);
e59458f7 76
3826564e 77 if (Current == true)
e59458f7 78 {
b2e465d6 79 Ver = Pkg.CurrentVer();
e59458f7
AL
80 if (Ver.end() == false)
81 out << " [" << Ver.VerStr() << ']';
82 }
b2e465d6 83
3826564e
MZ
84 if (Candidate == true)
85 {
86 Ver = Sim[Pkg].CandidateVerIter(Sim);
87 if (Ver.end() == true)
88 return;
b2e465d6 89
3826564e
MZ
90 out << " (" << Ver.VerStr() << ' ' << Ver.RelStr() << ')';
91 }
b2e465d6
AL
92}
93 /*}}}*/
6c139d6e
AL
94// Simulate::Install - Simulate unpacking of a package /*{{{*/
95// ---------------------------------------------------------------------
96/* */
97bool pkgSimulate::Install(PkgIterator iPkg,string /*File*/)
98{
99 // Adapt the iterator
803ea2a8 100 PkgIterator Pkg = Sim.FindPkg(iPkg.Name(), iPkg.Arch());
6c139d6e
AL
101 Flags[Pkg->ID] = 1;
102
b2e465d6 103 cout << "Inst ";
3826564e 104 Describe(Pkg,cout,true,true);
6c139d6e 105 Sim.MarkInstall(Pkg,false);
803ea2a8 106
6c139d6e 107 // Look for broken conflicts+predepends.
f7f0d6c7 108 for (PkgIterator I = Sim.PkgBegin(); I.end() == false; ++I)
6c139d6e
AL
109 {
110 if (Sim[I].InstallVer == 0)
111 continue;
112
b2e465d6
AL
113 for (DepIterator D = Sim[I].InstVerIter(Sim).DependsList(); D.end() == false;)
114 {
115 DepIterator Start;
116 DepIterator End;
117 D.GlobOr(Start,End);
359e46db 118 if (Start.IsNegative() == true ||
b2e465d6 119 End->Type == pkgCache::Dep::PreDepends)
6c139d6e 120 {
b2e465d6 121 if ((Sim[End] & pkgDepCache::DepGInstall) == 0)
6c139d6e 122 {
47f6d1b7 123 cout << " [" << I.FullName(false) << " on " << Start.TargetPkg().FullName(false) << ']';
b2e465d6 124 if (Start->Type == pkgCache::Dep::Conflicts)
47f6d1b7 125 _error->Error("Fatal, conflicts violated %s",I.FullName(false).c_str());
6c139d6e 126 }
b2e465d6
AL
127 }
128 }
6c139d6e
AL
129 }
130
131 if (Sim.BrokenCount() != 0)
132 ShortBreaks();
133 else
04aa15a8 134 cout << endl;
6c139d6e
AL
135 return true;
136}
137 /*}}}*/
138// Simulate::Configure - Simulate configuration of a Package /*{{{*/
139// ---------------------------------------------------------------------
140/* This is not an acurate simulation of relatity, we should really not
141 install the package.. For some investigations it may be necessary
142 however. */
143bool pkgSimulate::Configure(PkgIterator iPkg)
144{
145 // Adapt the iterator
803ea2a8 146 PkgIterator Pkg = Sim.FindPkg(iPkg.Name(), iPkg.Arch());
6c139d6e
AL
147
148 Flags[Pkg->ID] = 2;
803ea2a8 149
6c139d6e
AL
150 if (Sim[Pkg].InstBroken() == true)
151 {
47f6d1b7 152 cout << "Conf " << Pkg.FullName(false) << " broken" << endl;
6c139d6e
AL
153
154 Sim.Update();
155
156 // Print out each package and the failed dependencies
f7f0d6c7 157 for (pkgCache::DepIterator D = Sim[Pkg].InstVerIter(Sim).DependsList(); D.end() == false; ++D)
6c139d6e
AL
158 {
159 if (Sim.IsImportantDep(D) == false ||
160 (Sim[D] & pkgDepCache::DepInstall) != 0)
161 continue;
162
b2e465d6 163 if (D->Type == pkgCache::Dep::Obsoletes)
47f6d1b7 164 cout << " Obsoletes:" << D.TargetPkg().FullName(false);
b2e465d6 165 else if (D->Type == pkgCache::Dep::Conflicts)
47f6d1b7 166 cout << " Conflicts:" << D.TargetPkg().FullName(false);
308c7d30 167 else if (D->Type == pkgCache::Dep::DpkgBreaks)
47f6d1b7 168 cout << " Breaks:" << D.TargetPkg().FullName(false);
6c139d6e 169 else
47f6d1b7 170 cout << " Depends:" << D.TargetPkg().FullName(false);
6c139d6e 171 }
04aa15a8 172 cout << endl;
6c139d6e 173
09a10f9c 174 _error->Error("Conf Broken %s",Pkg.FullName(false).c_str());
6c139d6e
AL
175 }
176 else
b2e465d6
AL
177 {
178 cout << "Conf ";
3826564e 179 Describe(Pkg,cout,false,true);
b2e465d6 180 }
6c139d6e
AL
181
182 if (Sim.BrokenCount() != 0)
183 ShortBreaks();
184 else
04aa15a8 185 cout << endl;
6c139d6e
AL
186
187 return true;
188}
189 /*}}}*/
190// Simulate::Remove - Simulate the removal of a package /*{{{*/
191// ---------------------------------------------------------------------
192/* */
fc4b5c9f 193bool pkgSimulate::Remove(PkgIterator iPkg,bool Purge)
6c139d6e
AL
194{
195 // Adapt the iterator
803ea2a8 196 PkgIterator Pkg = Sim.FindPkg(iPkg.Name(), iPkg.Arch());
c919ad6e
DK
197 if (Pkg.end() == true)
198 {
199 std::cerr << (Purge ? "Purg" : "Remv") << " invalid package " << iPkg.FullName() << std::endl;
200 return false;
201 }
6c139d6e
AL
202
203 Flags[Pkg->ID] = 3;
204 Sim.MarkDelete(Pkg);
803ea2a8 205
fc4b5c9f 206 if (Purge == true)
b2e465d6 207 cout << "Purg ";
fc4b5c9f 208 else
b2e465d6 209 cout << "Remv ";
3826564e 210 Describe(Pkg,cout,true,false);
6c139d6e
AL
211
212 if (Sim.BrokenCount() != 0)
213 ShortBreaks();
214 else
04aa15a8 215 cout << endl;
6c139d6e
AL
216
217 return true;
218}
219 /*}}}*/
220// Simulate::ShortBreaks - Print out a short line describing all breaks /*{{{*/
221// ---------------------------------------------------------------------
222/* */
223void pkgSimulate::ShortBreaks()
224{
04aa15a8 225 cout << " [";
f7f0d6c7 226 for (PkgIterator I = Sim.PkgBegin(); I.end() == false; ++I)
6c139d6e
AL
227 {
228 if (Sim[I].InstBroken() == true)
229 {
230 if (Flags[I->ID] == 0)
47f6d1b7 231 cout << I.FullName(false) << ' ';
6c139d6e 232/* else
04aa15a8 233 cout << I.Name() << "! ";*/
6c139d6e
AL
234 }
235 }
04aa15a8 236 cout << ']' << endl;
6c139d6e
AL
237}
238 /*}}}*/
239// ApplyStatus - Adjust for non-ok packages /*{{{*/
240// ---------------------------------------------------------------------
241/* We attempt to change the state of the all packages that have failed
242 installation toward their real state. The ordering code will perform
243 the necessary calculations to deal with the problems. */
244bool pkgApplyStatus(pkgDepCache &Cache)
245{
74a05226
MV
246 pkgDepCache::ActionGroup group(Cache);
247
f7f0d6c7 248 for (pkgCache::PkgIterator I = Cache.PkgBegin(); I.end() == false; ++I)
6c139d6e 249 {
e481d5b0
AL
250 if (I->VersionList == 0)
251 continue;
252
d38b7b3d
AL
253 // Only choice for a ReInstReq package is to reinstall
254 if (I->InstState == pkgCache::State::ReInstReq ||
255 I->InstState == pkgCache::State::HoldReInstReq)
256 {
5871718b 257 if (I->CurrentVer != 0 && I.CurrentVer().Downloadable() == true)
74a05226 258 Cache.MarkKeep(I, false, false);
813c8eea
AL
259 else
260 {
261 // Is this right? Will dpkg choke on an upgrade?
2a3f3893
AL
262 if (Cache[I].CandidateVer != 0 &&
263 Cache[I].CandidateVerIter(Cache).Downloadable() == true)
74a05226 264 Cache.MarkInstall(I, false, 0, false);
813c8eea 265 else
b2e465d6 266 return _error->Error(_("The package %s needs to be reinstalled, "
09a10f9c 267 "but I can't find an archive for it."),I.FullName(true).c_str());
813c8eea
AL
268 }
269
d38b7b3d
AL
270 continue;
271 }
272
6c139d6e
AL
273 switch (I->CurrentState)
274 {
813c8eea
AL
275 /* This means installation failed somehow - it does not need to be
276 re-unpacked (probably) */
b518cca6
AL
277 case pkgCache::State::UnPacked:
278 case pkgCache::State::HalfConfigured:
9d06bc80
MV
279 case pkgCache::State::TriggersAwaited:
280 case pkgCache::State::TriggersPending:
5871718b 281 if ((I->CurrentVer != 0 && I.CurrentVer().Downloadable() == true) ||
813c8eea 282 I.State() != pkgCache::PkgIterator::NeedsUnpack)
74a05226 283 Cache.MarkKeep(I, false, false);
813c8eea
AL
284 else
285 {
2a3f3893
AL
286 if (Cache[I].CandidateVer != 0 &&
287 Cache[I].CandidateVerIter(Cache).Downloadable() == true)
74a05226 288 Cache.MarkInstall(I, true, 0, false);
813c8eea 289 else
b83cad32 290 Cache.MarkDelete(I, false, 0, false);
813c8eea 291 }
6c139d6e
AL
292 break;
293
294 // This means removal failed
b518cca6 295 case pkgCache::State::HalfInstalled:
b83cad32 296 Cache.MarkDelete(I, false, 0, false);
6c139d6e
AL
297 break;
298
299 default:
b518cca6 300 if (I->InstState != pkgCache::State::Ok)
6c139d6e 301 return _error->Error("The package %s is not ok and I "
09a10f9c 302 "don't know how to fix it!",I.FullName(false).c_str());
6c139d6e
AL
303 }
304 }
305 return true;
306}
307 /*}}}*/
308// FixBroken - Fix broken packages /*{{{*/
309// ---------------------------------------------------------------------
0a8e3465
AL
310/* This autoinstalls every broken package and then runs the problem resolver
311 on the result. */
6c139d6e
AL
312bool pkgFixBroken(pkgDepCache &Cache)
313{
74a05226
MV
314 pkgDepCache::ActionGroup group(Cache);
315
6c139d6e 316 // Auto upgrade all broken packages
f7f0d6c7 317 for (pkgCache::PkgIterator I = Cache.PkgBegin(); I.end() == false; ++I)
6c139d6e 318 if (Cache[I].NowBroken() == true)
74a05226 319 Cache.MarkInstall(I, true, 0, false);
7e798dd7 320
6c139d6e
AL
321 /* Fix packages that are in a NeedArchive state but don't have a
322 downloadable install version */
f7f0d6c7 323 for (pkgCache::PkgIterator I = Cache.PkgBegin(); I.end() == false; ++I)
6c139d6e
AL
324 {
325 if (I.State() != pkgCache::PkgIterator::NeedsUnpack ||
326 Cache[I].Delete() == true)
327 continue;
328
b518cca6 329 if (Cache[I].InstVerIter(Cache).Downloadable() == false)
6c139d6e
AL
330 continue;
331
74a05226 332 Cache.MarkInstall(I, true, 0, false);
6c139d6e
AL
333 }
334
b2e465d6 335 pkgProblemResolver Fix(&Cache);
6c139d6e
AL
336 return Fix.Resolve(true);
337}
338 /*}}}*/
339// DistUpgrade - Distribution upgrade /*{{{*/
340// ---------------------------------------------------------------------
341/* This autoinstalls every package and then force installs every
342 pre-existing package. This creates the initial set of conditions which
343 most likely contain problems because too many things were installed.
344
0a8e3465 345 The problem resolver is used to resolve the problems.
6c139d6e
AL
346 */
347bool pkgDistUpgrade(pkgDepCache &Cache)
348{
98278a81 349 std::string const solver = _config->Find("APT::Solver", "internal");
b57c0e35
DK
350 if (solver != "internal") {
351 OpTextProgress Prog(*_config);
352 return EDSP::ResolveExternal(solver.c_str(), Cache, false, true, false, &Prog);
353 }
741b7da9 354
74a05226
MV
355 pkgDepCache::ActionGroup group(Cache);
356
c427b1e2
DK
357 /* Upgrade all installed packages first without autoinst to help the resolver
358 in versioned or-groups to upgrade the old solver instead of installing
359 a new one (if the old solver is not the first one [anymore]) */
360 for (pkgCache::PkgIterator I = Cache.PkgBegin(); I.end() == false; ++I)
361 if (I->CurrentVer != 0)
362 Cache.MarkInstall(I, false, 0, false);
363
6c139d6e
AL
364 /* Auto upgrade all installed packages, this provides the basis
365 for the installation */
f7f0d6c7 366 for (pkgCache::PkgIterator I = Cache.PkgBegin(); I.end() == false; ++I)
6c139d6e 367 if (I->CurrentVer != 0)
74a05226 368 Cache.MarkInstall(I, true, 0, false);
6c139d6e 369
e5a91f7e
DK
370 /* Now, install each essential package which is not installed
371 (and not provided by another package in the same name group) */
372 std::string essential = _config->Find("pkgCacheGen::Essential", "all");
373 if (essential == "all")
374 {
375 for (pkgCache::GrpIterator G = Cache.GrpBegin(); G.end() == false; ++G)
376 {
377 bool isEssential = false;
378 bool instEssential = false;
379 for (pkgCache::PkgIterator P = G.PackageList(); P.end() == false; P = G.NextPkg(P))
380 {
381 if ((P->Flags & pkgCache::Flag::Essential) != pkgCache::Flag::Essential)
382 continue;
383 isEssential = true;
384 if (Cache[P].Install() == true)
385 {
386 instEssential = true;
387 break;
388 }
389 }
390 if (isEssential == false || instEssential == true)
391 continue;
392 pkgCache::PkgIterator P = G.FindPreferredPkg();
393 Cache.MarkInstall(P, true, 0, false);
394 }
395 }
396 else if (essential != "none")
397 for (pkgCache::PkgIterator I = Cache.PkgBegin(); I.end() == false; ++I)
398 if ((I->Flags & pkgCache::Flag::Essential) == pkgCache::Flag::Essential)
399 Cache.MarkInstall(I, true, 0, false);
6c139d6e
AL
400
401 /* We do it again over all previously installed packages to force
402 conflict resolution on them all. */
f7f0d6c7 403 for (pkgCache::PkgIterator I = Cache.PkgBegin(); I.end() == false; ++I)
6c139d6e 404 if (I->CurrentVer != 0)
74a05226 405 Cache.MarkInstall(I, false, 0, false);
6c139d6e 406
b2e465d6 407 pkgProblemResolver Fix(&Cache);
c88edf1d 408
6c139d6e 409 // Hold back held packages.
4490f2de 410 if (_config->FindB("APT::Ignore-Hold",false) == false)
6c139d6e 411 {
f7f0d6c7 412 for (pkgCache::PkgIterator I = Cache.PkgBegin(); I.end() == false; ++I)
6c139d6e 413 {
c88edf1d
AL
414 if (I->SelectedState == pkgCache::State::Hold)
415 {
416 Fix.Protect(I);
74a05226 417 Cache.MarkKeep(I, false, false);
c88edf1d 418 }
6c139d6e
AL
419 }
420 }
421
422 return Fix.Resolve();
423}
424 /*}}}*/
0a8e3465
AL
425// AllUpgrade - Upgrade as many packages as possible /*{{{*/
426// ---------------------------------------------------------------------
427/* Right now the system must be consistent before this can be called.
428 It also will not change packages marked for install, it only tries
429 to install packages not marked for install */
430bool pkgAllUpgrade(pkgDepCache &Cache)
431{
98278a81 432 std::string const solver = _config->Find("APT::Solver", "internal");
b57c0e35
DK
433 if (solver != "internal") {
434 OpTextProgress Prog(*_config);
435 return EDSP::ResolveExternal(solver.c_str(), Cache, true, false, false, &Prog);
436 }
741b7da9 437
74a05226
MV
438 pkgDepCache::ActionGroup group(Cache);
439
b2e465d6 440 pkgProblemResolver Fix(&Cache);
0a8e3465
AL
441
442 if (Cache.BrokenCount() != 0)
443 return false;
444
445 // Upgrade all installed packages
f7f0d6c7 446 for (pkgCache::PkgIterator I = Cache.PkgBegin(); I.end() == false; ++I)
0a8e3465
AL
447 {
448 if (Cache[I].Install() == true)
449 Fix.Protect(I);
450
b2e465d6 451 if (_config->FindB("APT::Ignore-Hold",false) == false)
c88edf1d
AL
452 if (I->SelectedState == pkgCache::State::Hold)
453 continue;
0a8e3465
AL
454
455 if (I->CurrentVer != 0 && Cache[I].InstallVer != 0)
74a05226 456 Cache.MarkInstall(I, false, 0, false);
0a8e3465
AL
457 }
458
459 return Fix.ResolveByKeep();
460}
461 /*}}}*/
7e798dd7
AL
462// MinimizeUpgrade - Minimizes the set of packages to be upgraded /*{{{*/
463// ---------------------------------------------------------------------
464/* This simply goes over the entire set of packages and tries to keep
465 each package marked for upgrade. If a conflict is generated then
466 the package is restored. */
467bool pkgMinimizeUpgrade(pkgDepCache &Cache)
468{
74a05226
MV
469 pkgDepCache::ActionGroup group(Cache);
470
7e798dd7
AL
471 if (Cache.BrokenCount() != 0)
472 return false;
473
abc8419e 474 // We loop for 10 tries to get the minimal set size.
7e798dd7 475 bool Change = false;
a005475e 476 unsigned int Count = 0;
7e798dd7
AL
477 do
478 {
479 Change = false;
f7f0d6c7 480 for (pkgCache::PkgIterator I = Cache.PkgBegin(); I.end() == false; ++I)
7e798dd7
AL
481 {
482 // Not interesting
483 if (Cache[I].Upgrade() == false || Cache[I].NewInstall() == true)
484 continue;
a005475e 485
7e798dd7 486 // Keep it and see if that is OK
74a05226 487 Cache.MarkKeep(I, false, false);
7e798dd7 488 if (Cache.BrokenCount() != 0)
74a05226 489 Cache.MarkInstall(I, false, 0, false);
7e798dd7 490 else
a005475e
AL
491 {
492 // If keep didnt actually do anything then there was no change..
493 if (Cache[I].Upgrade() == false)
494 Change = true;
495 }
7e798dd7 496 }
f7f0d6c7 497 ++Count;
7e798dd7 498 }
a005475e 499 while (Change == true && Count < 10);
7e798dd7
AL
500
501 if (Cache.BrokenCount() != 0)
502 return _error->Error("Internal Error in pkgMinimizeUpgrade");
503
504 return true;
505}
506 /*}}}*/
6c139d6e
AL
507// ProblemResolver::pkgProblemResolver - Constructor /*{{{*/
508// ---------------------------------------------------------------------
509/* */
dcaa1185 510pkgProblemResolver::pkgProblemResolver(pkgDepCache *pCache) : d(NULL), Cache(*pCache)
6c139d6e
AL
511{
512 // Allocate memory
b2e465d6 513 unsigned long Size = Cache.Head().PackageCount;
d0f2c87c 514 Scores = new int[Size];
6c139d6e
AL
515 Flags = new unsigned char[Size];
516 memset(Flags,0,sizeof(*Flags)*Size);
517
518 // Set debug to true to see its decision logic
0a8e3465 519 Debug = _config->FindB("Debug::pkgProblemResolver",false);
6c139d6e
AL
520}
521 /*}}}*/
b2e465d6
AL
522// ProblemResolver::~pkgProblemResolver - Destructor /*{{{*/
523// ---------------------------------------------------------------------
524/* */
525pkgProblemResolver::~pkgProblemResolver()
526{
527 delete [] Scores;
528 delete [] Flags;
529}
530 /*}}}*/
6c139d6e
AL
531// ProblemResolver::ScoreSort - Sort the list by score /*{{{*/
532// ---------------------------------------------------------------------
533/* */
534int pkgProblemResolver::ScoreSort(const void *a,const void *b)
535{
536 Package const **A = (Package const **)a;
537 Package const **B = (Package const **)b;
538 if (This->Scores[(*A)->ID] > This->Scores[(*B)->ID])
539 return -1;
540 if (This->Scores[(*A)->ID] < This->Scores[(*B)->ID])
541 return 1;
542 return 0;
543}
544 /*}}}*/
545// ProblemResolver::MakeScores - Make the score table /*{{{*/
546// ---------------------------------------------------------------------
547/* */
548void pkgProblemResolver::MakeScores()
549{
b2e465d6 550 unsigned long Size = Cache.Head().PackageCount;
6c139d6e
AL
551 memset(Scores,0,sizeof(*Scores)*Size);
552
c0751c06
MV
553 // Maps to pkgCache::State::VerPriority
554 // which is "Important Required Standard Optional Extra"
555 // (yes, that is confusing, the order of pkgCache::State::VerPriority
556 // needs to be adjusted but that requires a ABI break)
d0f2c87c 557 int PrioMap[] = {
8b4894fe 558 0,
84de0cea
MV
559 _config->FindI("pkgProblemResolver::Scores::Important",2),
560 _config->FindI("pkgProblemResolver::Scores::Required",3),
d0f2c87c
CW
561 _config->FindI("pkgProblemResolver::Scores::Standard",1),
562 _config->FindI("pkgProblemResolver::Scores::Optional",-1),
563 _config->FindI("pkgProblemResolver::Scores::Extra",-2)
8b4894fe 564 };
d0f2c87c
CW
565 int PrioEssentials = _config->FindI("pkgProblemResolver::Scores::Essentials",100);
566 int PrioInstalledAndNotObsolete = _config->FindI("pkgProblemResolver::Scores::NotObsolete",1);
567 int PrioDepends = _config->FindI("pkgProblemResolver::Scores::Depends",1);
568 int PrioRecommends = _config->FindI("pkgProblemResolver::Scores::Recommends",1);
569 int AddProtected = _config->FindI("pkgProblemResolver::Scores::AddProtected",10000);
570 int AddEssential = _config->FindI("pkgProblemResolver::Scores::AddEssential",5000);
8b4894fe
MV
571
572 if (_config->FindB("Debug::pkgProblemResolver::ShowScores",false) == true)
573 clog << "Settings used to calculate pkgProblemResolver::Scores::" << endl
84de0cea
MV
574 << " Required => " << PrioMap[pkgCache::State::Required] << endl
575 << " Important => " << PrioMap[pkgCache::State::Important] << endl
576 << " Standard => " << PrioMap[pkgCache::State::Standard] << endl
577 << " Optional => " << PrioMap[pkgCache::State::Optional] << endl
578 << " Extra => " << PrioMap[pkgCache::State::Extra] << endl
8b4894fe
MV
579 << " Essentials => " << PrioEssentials << endl
580 << " InstalledAndNotObsolete => " << PrioInstalledAndNotObsolete << endl
581 << " Depends => " << PrioDepends << endl
53391d0f 582 << " Recommends => " << PrioRecommends << endl
8b4894fe
MV
583 << " AddProtected => " << AddProtected << endl
584 << " AddEssential => " << AddEssential << endl;
585
6c139d6e 586 // Generate the base scores for a package based on its properties
f7f0d6c7 587 for (pkgCache::PkgIterator I = Cache.PkgBegin(); I.end() == false; ++I)
6c139d6e
AL
588 {
589 if (Cache[I].InstallVer == 0)
590 continue;
591
d0f2c87c 592 int &Score = Scores[I->ID];
6c139d6e 593
7365ff46 594 /* This is arbitrary, it should be high enough to elevate an
6c139d6e
AL
595 essantial package above most other packages but low enough
596 to allow an obsolete essential packages to be removed by
597 a conflicts on a powerfull normal package (ie libc6) */
c5200869
JAK
598 if ((I->Flags & pkgCache::Flag::Essential) == pkgCache::Flag::Essential
599 || (I->Flags & pkgCache::Flag::Important) == pkgCache::Flag::Important)
8b4894fe 600 Score += PrioEssentials;
6c139d6e
AL
601
602 // We transform the priority
6c139d6e
AL
603 if (Cache[I].InstVerIter(Cache)->Priority <= 5)
604 Score += PrioMap[Cache[I].InstVerIter(Cache)->Priority];
605
606 /* This helps to fix oddball problems with conflicting packages
4172c784
MV
607 on the same level. We enhance the score of installed packages
608 if those are not obsolete
609 */
020daa7b 610 if (I->CurrentVer != 0 && Cache[I].CandidateVer != 0 && Cache[I].CandidateVerIter(Cache).Downloadable())
8b4894fe 611 Score += PrioInstalledAndNotObsolete;
6c139d6e
AL
612 }
613
614 // Now that we have the base scores we go and propogate dependencies
f7f0d6c7 615 for (pkgCache::PkgIterator I = Cache.PkgBegin(); I.end() == false; ++I)
6c139d6e
AL
616 {
617 if (Cache[I].InstallVer == 0)
618 continue;
619
f7f0d6c7 620 for (pkgCache::DepIterator D = Cache[I].InstVerIter(Cache).DependsList(); D.end() == false; ++D)
6c139d6e 621 {
3a998f6a 622 if (D->Type == pkgCache::Dep::Depends ||
53391d0f
MV
623 D->Type == pkgCache::Dep::PreDepends)
624 Scores[D.TargetPkg()->ID] += PrioDepends;
625 else if (D->Type == pkgCache::Dep::Recommends)
626 Scores[D.TargetPkg()->ID] += PrioRecommends;
6c139d6e
AL
627 }
628 }
629
630 // Copy the scores to advoid additive looping
d0f2c87c 631 SPtrArray<int> OldScores = new int[Size];
6c139d6e
AL
632 memcpy(OldScores,Scores,sizeof(*Scores)*Size);
633
634 /* Now we cause 1 level of dependency inheritance, that is we add the
635 score of the packages that depend on the target Package. This
636 fortifies high scoring packages */
f7f0d6c7 637 for (pkgCache::PkgIterator I = Cache.PkgBegin(); I.end() == false; ++I)
6c139d6e
AL
638 {
639 if (Cache[I].InstallVer == 0)
640 continue;
641
f7f0d6c7 642 for (pkgCache::DepIterator D = I.RevDependsList(); D.end() == false; ++D)
6c139d6e
AL
643 {
644 // Only do it for the install version
645 if ((pkgCache::Version *)D.ParentVer() != Cache[D.ParentPkg()].InstallVer ||
3a998f6a
MV
646 (D->Type != pkgCache::Dep::Depends &&
647 D->Type != pkgCache::Dep::PreDepends &&
648 D->Type != pkgCache::Dep::Recommends))
6c139d6e
AL
649 continue;
650
dec5b117
MV
651 // Do not propagate negative scores otherwise
652 // an extra (-2) package might score better than an optional (-1)
653 if (OldScores[D.ParentPkg()->ID] > 0)
654 Scores[I->ID] += OldScores[D.ParentPkg()->ID];
6c139d6e
AL
655 }
656 }
657
658 /* Now we propogate along provides. This makes the packages that
659 provide important packages extremely important */
f7f0d6c7 660 for (pkgCache::PkgIterator I = Cache.PkgBegin(); I.end() == false; ++I)
6c139d6e 661 {
f7f0d6c7 662 for (pkgCache::PrvIterator P = I.ProvidesList(); P.end() == false; ++P)
6c139d6e
AL
663 {
664 // Only do it once per package
665 if ((pkgCache::Version *)P.OwnerVer() != Cache[P.OwnerPkg()].InstallVer)
666 continue;
667 Scores[P.OwnerPkg()->ID] += abs(Scores[I->ID] - OldScores[I->ID]);
668 }
669 }
670
671 /* Protected things are pushed really high up. This number should put them
672 ahead of everything */
f7f0d6c7 673 for (pkgCache::PkgIterator I = Cache.PkgBegin(); I.end() == false; ++I)
d2685fd6 674 {
6c139d6e 675 if ((Flags[I->ID] & Protected) != 0)
8b4894fe 676 Scores[I->ID] += AddProtected;
c5200869
JAK
677 if ((I->Flags & pkgCache::Flag::Essential) == pkgCache::Flag::Essential ||
678 (I->Flags & pkgCache::Flag::Important) == pkgCache::Flag::Important)
8b4894fe
MV
679 Scores[I->ID] += AddEssential;
680 }
6c139d6e
AL
681}
682 /*}}}*/
683// ProblemResolver::DoUpgrade - Attempt to upgrade this package /*{{{*/
684// ---------------------------------------------------------------------
685/* This goes through and tries to reinstall packages to make this package
686 installable */
687bool pkgProblemResolver::DoUpgrade(pkgCache::PkgIterator Pkg)
688{
74a05226
MV
689 pkgDepCache::ActionGroup group(Cache);
690
6c139d6e
AL
691 if ((Flags[Pkg->ID] & Upgradable) == 0 || Cache[Pkg].Upgradable() == false)
692 return false;
3a486305
AL
693 if ((Flags[Pkg->ID] & Protected) == Protected)
694 return false;
0a8e3465 695
6c139d6e
AL
696 Flags[Pkg->ID] &= ~Upgradable;
697
698 bool WasKept = Cache[Pkg].Keep();
74a05226 699 Cache.MarkInstall(Pkg, false, 0, false);
6c139d6e 700
0a8e3465
AL
701 // This must be a virtual package or something like that.
702 if (Cache[Pkg].InstVerIter(Cache).end() == true)
703 return false;
704
6c139d6e
AL
705 // Isolate the problem dependency
706 bool Fail = false;
707 for (pkgCache::DepIterator D = Cache[Pkg].InstVerIter(Cache).DependsList(); D.end() == false;)
708 {
709 // Compute a single dependency element (glob or)
710 pkgCache::DepIterator Start = D;
711 pkgCache::DepIterator End = D;
4b1b89c5 712 for (bool LastOR = true; D.end() == false && LastOR == true;)
6c139d6e 713 {
b518cca6 714 LastOR = (D->CompareOp & pkgCache::Dep::Or) == pkgCache::Dep::Or;
0284eee4 715 ++D;
6c139d6e
AL
716 if (LastOR == true)
717 End = D;
718 }
719
720 // We only worry about critical deps.
721 if (End.IsCritical() != true)
722 continue;
4b1b89c5
AL
723
724 // Iterate over all the members in the or group
725 while (1)
0a8e3465 726 {
4b1b89c5
AL
727 // Dep is ok now
728 if ((Cache[End] & pkgDepCache::DepGInstall) == pkgDepCache::DepGInstall)
729 break;
730
731 // Do not change protected packages
732 PkgIterator P = Start.SmartTargetPkg();
733 if ((Flags[P->ID] & Protected) == Protected)
734 {
735 if (Debug == true)
47f6d1b7 736 clog << " Reinst Failed because of protected " << P.FullName(false) << endl;
4b1b89c5 737 Fail = true;
4b1b89c5 738 }
648e3cb4 739 else
6c139d6e 740 {
648e3cb4
AL
741 // Upgrade the package if the candidate version will fix the problem.
742 if ((Cache[Start] & pkgDepCache::DepCVer) == pkgDepCache::DepCVer)
743 {
744 if (DoUpgrade(P) == false)
745 {
746 if (Debug == true)
47f6d1b7 747 clog << " Reinst Failed because of " << P.FullName(false) << endl;
648e3cb4
AL
748 Fail = true;
749 }
750 else
751 {
752 Fail = false;
753 break;
754 }
755 }
756 else
4b1b89c5 757 {
648e3cb4
AL
758 /* We let the algorithm deal with conflicts on its next iteration,
759 it is much smarter than us */
359e46db 760 if (Start.IsNegative() == true)
b2e465d6 761 break;
648e3cb4 762
4b1b89c5 763 if (Debug == true)
47f6d1b7 764 clog << " Reinst Failed early because of " << Start.TargetPkg().FullName(false) << endl;
4b1b89c5 765 Fail = true;
648e3cb4 766 }
4b1b89c5 767 }
6c139d6e 768
4b1b89c5
AL
769 if (Start == End)
770 break;
f7f0d6c7 771 ++Start;
4b1b89c5
AL
772 }
773 if (Fail == true)
6c139d6e 774 break;
6c139d6e
AL
775 }
776
777 // Undo our operations - it might be smart to undo everything this did..
778 if (Fail == true)
779 {
780 if (WasKept == true)
74a05226 781 Cache.MarkKeep(Pkg, false, false);
6c139d6e 782 else
b83cad32 783 Cache.MarkDelete(Pkg, false, 0, false);
6c139d6e
AL
784 return false;
785 }
786
787 if (Debug == true)
47f6d1b7 788 clog << " Re-Instated " << Pkg.FullName(false) << endl;
6c139d6e
AL
789 return true;
790}
791 /*}}}*/
6d38011b
DK
792// ProblemResolver::Resolve - calls a resolver to fix the situation /*{{{*/
793// ---------------------------------------------------------------------
794/* */
795bool pkgProblemResolver::Resolve(bool BrokenFix)
796{
98278a81 797 std::string const solver = _config->Find("APT::Solver", "internal");
b57c0e35
DK
798 if (solver != "internal") {
799 OpTextProgress Prog(*_config);
800 return EDSP::ResolveExternal(solver.c_str(), Cache, false, false, false, &Prog);
801 }
6d38011b
DK
802 return ResolveInternal(BrokenFix);
803}
804 /*}}}*/
805// ProblemResolver::ResolveInternal - Run the resolution pass /*{{{*/
6c139d6e
AL
806// ---------------------------------------------------------------------
807/* This routines works by calculating a score for each package. The score
808 is derived by considering the package's priority and all reverse
809 dependents giving an integer that reflects the amount of breakage that
810 adjusting the package will inflict.
811
812 It goes from highest score to lowest and corrects all of the breaks by
813 keeping or removing the dependant packages. If that fails then it removes
814 the package itself and goes on. The routine should be able to intelligently
815 go from any broken state to a fixed state.
816
817 The BrokenFix flag enables a mode where the algorithm tries to
818 upgrade packages to advoid problems. */
6d38011b 819bool pkgProblemResolver::ResolveInternal(bool const BrokenFix)
6c139d6e 820{
74a05226
MV
821 pkgDepCache::ActionGroup group(Cache);
822
6c139d6e
AL
823 // Record which packages are marked for install
824 bool Again = false;
825 do
826 {
827 Again = false;
f7f0d6c7 828 for (pkgCache::PkgIterator I = Cache.PkgBegin(); I.end() == false; ++I)
6c139d6e
AL
829 {
830 if (Cache[I].Install() == true)
831 Flags[I->ID] |= PreInstalled;
832 else
833 {
834 if (Cache[I].InstBroken() == true && BrokenFix == true)
835 {
74a05226 836 Cache.MarkInstall(I, false, 0, false);
6c139d6e
AL
837 if (Cache[I].Install() == true)
838 Again = true;
839 }
840
841 Flags[I->ID] &= ~PreInstalled;
842 }
843 Flags[I->ID] |= Upgradable;
844 }
845 }
846 while (Again == true);
847
848 if (Debug == true)
0a8e3465 849 clog << "Starting" << endl;
6c139d6e
AL
850
851 MakeScores();
6d38011b
DK
852
853 unsigned long const Size = Cache.Head().PackageCount;
854
6c139d6e
AL
855 /* We have to order the packages so that the broken fixing pass
856 operates from highest score to lowest. This prevents problems when
857 high score packages cause the removal of lower score packages that
858 would cause the removal of even lower score packages. */
b2e465d6 859 SPtrArray<pkgCache::Package *> PList = new pkgCache::Package *[Size];
6c139d6e 860 pkgCache::Package **PEnd = PList;
f7f0d6c7 861 for (pkgCache::PkgIterator I = Cache.PkgBegin(); I.end() == false; ++I)
6c139d6e
AL
862 *PEnd++ = I;
863 This = this;
864 qsort(PList,PEnd - PList,sizeof(*PList),&ScoreSort);
8b4894fe
MV
865
866 if (_config->FindB("Debug::pkgProblemResolver::ShowScores",false) == true)
867 {
868 clog << "Show Scores" << endl;
869 for (pkgCache::Package **K = PList; K != PEnd; K++)
870 if (Scores[(*K)->ID] != 0)
871 {
872 pkgCache::PkgIterator Pkg(Cache,*K);
873 clog << Scores[(*K)->ID] << ' ' << Pkg << std::endl;
874 }
875 }
6c139d6e
AL
876
877 if (Debug == true)
0a8e3465 878 clog << "Starting 2" << endl;
8b4894fe 879
6c139d6e
AL
880 /* Now consider all broken packages. For each broken package we either
881 remove the package or fix it's problem. We do this once, it should
882 not be possible for a loop to form (that is a < b < c and fixing b by
883 changing a breaks c) */
884 bool Change = true;
09a10f9c 885 bool const TryFixByInstall = _config->FindB("pkgProblemResolver::FixByInstall", true);
6c139d6e
AL
886 for (int Counter = 0; Counter != 10 && Change == true; Counter++)
887 {
888 Change = false;
889 for (pkgCache::Package **K = PList; K != PEnd; K++)
890 {
891 pkgCache::PkgIterator I(Cache,*K);
892
893 /* We attempt to install this and see if any breaks result,
894 this takes care of some strange cases */
895 if (Cache[I].CandidateVer != Cache[I].InstallVer &&
896 I->CurrentVer != 0 && Cache[I].InstallVer != 0 &&
897 (Flags[I->ID] & PreInstalled) != 0 &&
0a8e3465
AL
898 (Flags[I->ID] & Protected) == 0 &&
899 (Flags[I->ID] & ReInstateTried) == 0)
6c139d6e
AL
900 {
901 if (Debug == true)
09a10f9c 902 clog << " Try to Re-Instate (" << Counter << ") " << I.FullName(false) << endl;
a6568219 903 unsigned long OldBreaks = Cache.BrokenCount();
6c139d6e 904 pkgCache::Version *OldVer = Cache[I].InstallVer;
0a8e3465
AL
905 Flags[I->ID] &= ReInstateTried;
906
74a05226 907 Cache.MarkInstall(I, false, 0, false);
6c139d6e
AL
908 if (Cache[I].InstBroken() == true ||
909 OldBreaks < Cache.BrokenCount())
910 {
911 if (OldVer == 0)
b83cad32 912 Cache.MarkDelete(I, false, 0, false);
6c139d6e 913 else
74a05226 914 Cache.MarkKeep(I, false, false);
6c139d6e
AL
915 }
916 else
917 if (Debug == true)
47f6d1b7 918 clog << "Re-Instated " << I.FullName(false) << " (" << OldBreaks << " vs " << Cache.BrokenCount() << ')' << endl;
6c139d6e
AL
919 }
920
921 if (Cache[I].InstallVer == 0 || Cache[I].InstBroken() == false)
922 continue;
923
00b47c98 924 if (Debug == true)
09a10f9c 925 clog << "Investigating (" << Counter << ") " << I << endl;
00b47c98 926
6c139d6e
AL
927 // Isolate the problem dependency
928 PackageKill KillList[100];
929 PackageKill *LEnd = KillList;
421c8d10
AL
930 bool InOr = false;
931 pkgCache::DepIterator Start;
932 pkgCache::DepIterator End;
b2e465d6 933 PackageKill *OldEnd = LEnd;
648e3cb4
AL
934
935 enum {OrRemove,OrKeep} OrOp = OrRemove;
421c8d10
AL
936 for (pkgCache::DepIterator D = Cache[I].InstVerIter(Cache).DependsList();
937 D.end() == false || InOr == true;)
6c139d6e
AL
938 {
939 // Compute a single dependency element (glob or)
648e3cb4
AL
940 if (Start == End)
941 {
942 // Decide what to do
09a10f9c 943 if (InOr == true && OldEnd == LEnd)
648e3cb4 944 {
09a10f9c 945 if (OrOp == OrRemove)
70777d4b
AL
946 {
947 if ((Flags[I->ID] & Protected) != Protected)
00b47c98
AL
948 {
949 if (Debug == true)
47f6d1b7 950 clog << " Or group remove for " << I.FullName(false) << endl;
b83cad32 951 Cache.MarkDelete(I, false, 0, false);
cd14eaf2 952 Change = true;
09a10f9c
DK
953 }
954 }
955 else if (OrOp == OrKeep)
00b47c98
AL
956 {
957 if (Debug == true)
47f6d1b7 958 clog << " Or group keep for " << I.FullName(false) << endl;
74a05226 959 Cache.MarkKeep(I, false, false);
cd14eaf2 960 Change = true;
b2e465d6 961 }
648e3cb4
AL
962 }
963
b2e465d6
AL
964 /* We do an extra loop (as above) to finalize the or group
965 processing */
966 InOr = false;
648e3cb4 967 OrOp = OrRemove;
421c8d10 968 D.GlobOr(Start,End);
b2e465d6
AL
969 if (Start.end() == true)
970 break;
cd14eaf2 971
b2e465d6
AL
972 // We only worry about critical deps.
973 if (End.IsCritical() != true)
974 continue;
cd14eaf2 975
648e3cb4
AL
976 InOr = Start != End;
977 OldEnd = LEnd;
cd14eaf2 978 }
421c8d10 979 else
4cc152f9 980 {
f7f0d6c7 981 ++Start;
4cc152f9
MV
982 // We only worry about critical deps.
983 if (Start.IsCritical() != true)
984 continue;
985 }
cd14eaf2 986
6c139d6e
AL
987 // Dep is ok
988 if ((Cache[End] & pkgDepCache::DepGInstall) == pkgDepCache::DepGInstall)
cd14eaf2
AL
989 {
990 InOr = false;
6c139d6e 991 continue;
cd14eaf2
AL
992 }
993
6c139d6e 994 if (Debug == true)
47f6d1b7 995 clog << "Broken " << Start << endl;
fcf85120
AL
996
997 /* Look across the version list. If there are no possible
998 targets then we keep the package and bail. This is necessary
999 if a package has a dep on another package that cant be found */
b2e465d6 1000 SPtrArray<pkgCache::Version *> VList = Start.AllTargets();
fcf85120 1001 if (*VList == 0 && (Flags[I->ID] & Protected) != Protected &&
359e46db 1002 Start.IsNegative() == false &&
fcf85120 1003 Cache[I].NowBroken() == false)
648e3cb4
AL
1004 {
1005 if (InOr == true)
1006 {
1007 /* No keep choice because the keep being OK could be the
1008 result of another element in the OR group! */
1009 continue;
1010 }
1011
fcf85120 1012 Change = true;
74a05226 1013 Cache.MarkKeep(I, false, false);
fcf85120
AL
1014 break;
1015 }
1016
6c139d6e
AL
1017 bool Done = false;
1018 for (pkgCache::Version **V = VList; *V != 0; V++)
1019 {
1020 pkgCache::VerIterator Ver(Cache,*V);
1021 pkgCache::PkgIterator Pkg = Ver.ParentPkg();
a6bfe583 1022
2ba99db8
MV
1023 /* This is a conflicts, and the version we are looking
1024 at is not the currently selected version of the
1025 package, which means it is not necessary to
1026 remove/keep */
359e46db 1027 if (Cache[Pkg].InstallVer != Ver && Start.IsNegative() == true)
4429616b 1028 {
2ba99db8
MV
1029 if (Debug)
1030 clog << " Conflicts//Breaks against version "
1031 << Ver.VerStr() << " for " << Pkg.Name()
1032 << " but that is not InstVer, ignoring"
24e93662 1033 << endl;
2ba99db8 1034 continue;
4429616b
MV
1035 }
1036
6c139d6e 1037 if (Debug == true)
47f6d1b7
DK
1038 clog << " Considering " << Pkg.FullName(false) << ' ' << (int)Scores[Pkg->ID] <<
1039 " as a solution to " << I.FullName(false) << ' ' << (int)Scores[I->ID] << endl;
a6bfe583
AL
1040
1041 /* Try to fix the package under consideration rather than
1042 fiddle with the VList package */
6c139d6e 1043 if (Scores[I->ID] <= Scores[Pkg->ID] ||
421c8d10 1044 ((Cache[Start] & pkgDepCache::DepNow) == 0 &&
359e46db 1045 End.IsNegative() == false))
6c139d6e 1046 {
200f8c52 1047 // Try a little harder to fix protected packages..
3b5421b4 1048 if ((Flags[I->ID] & Protected) == Protected)
200f8c52
AL
1049 {
1050 if (DoUpgrade(Pkg) == true)
0296c633 1051 {
b2e465d6
AL
1052 if (Scores[Pkg->ID] > Scores[I->ID])
1053 Scores[Pkg->ID] = Scores[I->ID];
0296c633
AL
1054 break;
1055 }
1056
6c139d6e 1057 continue;
200f8c52
AL
1058 }
1059
1060 /* See if a keep will do, unless the package is protected,
648e3cb4
AL
1061 then installing it will be necessary */
1062 bool Installed = Cache[I].Install();
74a05226 1063 Cache.MarkKeep(I, false, false);
6c139d6e
AL
1064 if (Cache[I].InstBroken() == false)
1065 {
648e3cb4
AL
1066 // Unwind operation will be keep now
1067 if (OrOp == OrRemove)
1068 OrOp = OrKeep;
1069
1070 // Restore
1071 if (InOr == true && Installed == true)
74a05226 1072 Cache.MarkInstall(I, false, 0, false);
648e3cb4 1073
6c139d6e 1074 if (Debug == true)
47f6d1b7 1075 clog << " Holding Back " << I.FullName(false) << " rather than change " << Start.TargetPkg().FullName(false) << endl;
6c139d6e
AL
1076 }
1077 else
421c8d10 1078 {
6c139d6e
AL
1079 if (BrokenFix == false || DoUpgrade(I) == false)
1080 {
421c8d10 1081 // Consider other options
87da7451 1082 if (InOr == false || Cache[I].Garbage == true)
421c8d10 1083 {
6910a2ac 1084 if (Debug == true)
47f6d1b7 1085 clog << " Removing " << I.FullName(false) << " rather than change " << Start.TargetPkg().FullName(false) << endl;
b83cad32 1086 Cache.MarkDelete(I, false, 0, false);
6910a2ac
DK
1087 if (Counter > 1 && Scores[Pkg->ID] > Scores[I->ID])
1088 Scores[I->ID] = Scores[Pkg->ID];
d6ebeb21 1089 }
09a10f9c
DK
1090 else if (TryFixByInstall == true &&
1091 Start.TargetPkg()->CurrentVer == 0 &&
1092 Cache[Start.TargetPkg()].Delete() == false &&
a3f1a6cc 1093 (Flags[Start.TargetPkg()->ID] & ToRemove) != ToRemove &&
09a10f9c
DK
1094 Cache.GetCandidateVer(Start.TargetPkg()).end() == false)
1095 {
1096 /* Before removing or keeping the package with the broken dependency
1097 try instead to install the first not previously installed package
1098 solving this dependency. This helps every time a previous solver
1099 is removed by the resolver because of a conflict or alike but it is
1100 dangerous as it could trigger new breaks/conflicts… */
443266ef
DK
1101 if (Debug == true)
1102 clog << " Try Installing " << Start.TargetPkg() << " before changing " << I.FullName(false) << std::endl;
09a10f9c
DK
1103 unsigned long const OldBroken = Cache.BrokenCount();
1104 Cache.MarkInstall(Start.TargetPkg(), true, 1, false);
1105 // FIXME: we should undo the complete MarkInstall process here
1106 if (Cache[Start.TargetPkg()].InstBroken() == true || Cache.BrokenCount() > OldBroken)
1107 Cache.MarkDelete(Start.TargetPkg(), false, 1, false);
1108 }
0a8e3465 1109 }
6c139d6e 1110 }
b5dc9785 1111
6c139d6e
AL
1112 Change = true;
1113 Done = true;
1114 break;
1115 }
1116 else
1117 {
308c7d30
IJ
1118 if (Start->Type == pkgCache::Dep::DpkgBreaks)
1119 {
76264cb7
MV
1120 // first, try upgradring the package, if that
1121 // does not help, the breaks goes onto the
1122 // kill list
2ba99db8 1123 //
76264cb7 1124 // FIXME: use DoUpgrade(Pkg) instead?
2ba99db8 1125 if (Cache[End] & pkgDepCache::DepGCVer)
76264cb7 1126 {
308c7d30 1127 if (Debug)
47f6d1b7 1128 clog << " Upgrading " << Pkg.FullName(false) << " due to Breaks field in " << I.FullName(false) << endl;
308c7d30
IJ
1129 Cache.MarkInstall(Pkg, false, 0, false);
1130 continue;
1131 }
308c7d30
IJ
1132 }
1133
648e3cb4 1134 // Skip adding to the kill list if it is protected
6c139d6e
AL
1135 if ((Flags[Pkg->ID] & Protected) != 0)
1136 continue;
a6bfe583
AL
1137
1138 if (Debug == true)
47f6d1b7 1139 clog << " Added " << Pkg.FullName(false) << " to the remove list" << endl;
6c139d6e
AL
1140
1141 LEnd->Pkg = Pkg;
1142 LEnd->Dep = End;
1143 LEnd++;
0a8e3465 1144
b47053bd 1145 if (Start.IsNegative() == false)
6c139d6e
AL
1146 break;
1147 }
1148 }
1149
1150 // Hm, nothing can possibly satisify this dep. Nuke it.
359e46db
DK
1151 if (VList[0] == 0 &&
1152 Start.IsNegative() == false &&
648e3cb4 1153 (Flags[I->ID] & Protected) != Protected)
6c139d6e 1154 {
648e3cb4 1155 bool Installed = Cache[I].Install();
6c139d6e
AL
1156 Cache.MarkKeep(I);
1157 if (Cache[I].InstBroken() == false)
1158 {
648e3cb4
AL
1159 // Unwind operation will be keep now
1160 if (OrOp == OrRemove)
1161 OrOp = OrKeep;
1162
1163 // Restore
1164 if (InOr == true && Installed == true)
74a05226 1165 Cache.MarkInstall(I, false, 0, false);
648e3cb4 1166
6c139d6e 1167 if (Debug == true)
47f6d1b7 1168 clog << " Holding Back " << I.FullName(false) << " because I can't find " << Start.TargetPkg().FullName(false) << endl;
6c139d6e
AL
1169 }
1170 else
1171 {
1172 if (Debug == true)
47f6d1b7 1173 clog << " Removing " << I.FullName(false) << " because I can't find " << Start.TargetPkg().FullName(false) << endl;
648e3cb4 1174 if (InOr == false)
b83cad32 1175 Cache.MarkDelete(I, false, 0, false);
6c139d6e
AL
1176 }
1177
1178 Change = true;
1179 Done = true;
1180 }
1181
421c8d10
AL
1182 // Try some more
1183 if (InOr == true)
1184 continue;
1185
6c139d6e
AL
1186 if (Done == true)
1187 break;
1188 }
1189
1190 // Apply the kill list now
1191 if (Cache[I].InstallVer != 0)
648e3cb4 1192 {
6c139d6e 1193 for (PackageKill *J = KillList; J != LEnd; J++)
6c139d6e 1194 {
648e3cb4
AL
1195 Change = true;
1196 if ((Cache[J->Dep] & pkgDepCache::DepGNow) == 0)
1197 {
359e46db 1198 if (J->Dep.IsNegative() == true)
648e3cb4
AL
1199 {
1200 if (Debug == true)
47f6d1b7 1201 clog << " Fixing " << I.FullName(false) << " via remove of " << J->Pkg.FullName(false) << endl;
b83cad32 1202 Cache.MarkDelete(J->Pkg, false, 0, false);
648e3cb4
AL
1203 }
1204 }
1205 else
6c139d6e
AL
1206 {
1207 if (Debug == true)
47f6d1b7 1208 clog << " Fixing " << I.FullName(false) << " via keep of " << J->Pkg.FullName(false) << endl;
74a05226 1209 Cache.MarkKeep(J->Pkg, false, false);
6c139d6e 1210 }
b2e465d6 1211
648e3cb4 1212 if (Counter > 1)
b2e465d6
AL
1213 {
1214 if (Scores[I->ID] > Scores[J->Pkg->ID])
1215 Scores[J->Pkg->ID] = Scores[I->ID];
1216 }
648e3cb4
AL
1217 }
1218 }
1219 }
6c139d6e
AL
1220 }
1221
1222 if (Debug == true)
0a8e3465 1223 clog << "Done" << endl;
b2e465d6 1224
6c139d6e 1225 if (Cache.BrokenCount() != 0)
b5dc9785
AL
1226 {
1227 // See if this is the result of a hold
1228 pkgCache::PkgIterator I = Cache.PkgBegin();
f7f0d6c7 1229 for (;I.end() != true; ++I)
b5dc9785
AL
1230 {
1231 if (Cache[I].InstBroken() == false)
1232 continue;
1233 if ((Flags[I->ID] & Protected) != Protected)
b2e465d6 1234 return _error->Error(_("Error, pkgProblemResolver::Resolve generated breaks, this may be caused by held packages."));
b5dc9785 1235 }
b2e465d6 1236 return _error->Error(_("Unable to correct problems, you have held broken packages."));
b5dc9785
AL
1237 }
1238
fce72602 1239 // set the auto-flags (mvo: I'm not sure if we _really_ need this)
80fa0d8a 1240 pkgCache::PkgIterator I = Cache.PkgBegin();
f7f0d6c7 1241 for (;I.end() != true; ++I) {
80fa0d8a 1242 if (Cache[I].NewInstall() && !(Flags[I->ID] & PreInstalled)) {
120365ce 1243 if(_config->FindI("Debug::pkgAutoRemove",false)) {
47f6d1b7 1244 std::clog << "Resolve installed new pkg: " << I.FullName(false)
120365ce
MV
1245 << " (now marking it as auto)" << std::endl;
1246 }
80fa0d8a
MV
1247 Cache[I].Flags |= pkgCache::Flag::Auto;
1248 }
1249 }
1250
1251
0a8e3465
AL
1252 return true;
1253}
1254 /*}}}*/
953b348c
MV
1255// ProblemResolver::BreaksInstOrPolicy - Check if the given pkg is broken/*{{{*/
1256// ---------------------------------------------------------------------
1257/* This checks if the given package is broken either by a hard dependency
1258 (InstBroken()) or by introducing a new policy breakage e.g. new
1259 unsatisfied recommends for a package that was in "policy-good" state
1260
1261 Note that this is not perfect as it will ignore further breakage
1262 for already broken policy (recommends)
1263*/
1264bool pkgProblemResolver::InstOrNewPolicyBroken(pkgCache::PkgIterator I)
1265{
953b348c
MV
1266 // a broken install is always a problem
1267 if (Cache[I].InstBroken() == true)
deec6474
DK
1268 {
1269 if (Debug == true)
1270 std::clog << " Dependencies are not satisfied for " << I << std::endl;
953b348c 1271 return true;
deec6474 1272 }
953b348c
MV
1273
1274 // a newly broken policy (recommends/suggests) is a problem
1275 if (Cache[I].NowPolicyBroken() == false &&
1276 Cache[I].InstPolicyBroken() == true)
deec6474
DK
1277 {
1278 if (Debug == true)
1279 std::clog << " Policy breaks with upgrade of " << I << std::endl;
953b348c 1280 return true;
deec6474
DK
1281 }
1282
953b348c
MV
1283 return false;
1284}
36a171e1 1285 /*}}}*/
0a8e3465
AL
1286// ProblemResolver::ResolveByKeep - Resolve problems using keep /*{{{*/
1287// ---------------------------------------------------------------------
1288/* This is the work horse of the soft upgrade routine. It is very gental
1289 in that it does not install or remove any packages. It is assumed that the
1290 system was non-broken previously. */
1291bool pkgProblemResolver::ResolveByKeep()
741b7da9 1292{
98278a81 1293 std::string const solver = _config->Find("APT::Solver", "internal");
b57c0e35
DK
1294 if (solver != "internal") {
1295 OpTextProgress Prog(*_config);
1296 return EDSP::ResolveExternal(solver.c_str(), Cache, true, false, false, &Prog);
1297 }
741b7da9
DK
1298 return ResolveByKeepInternal();
1299}
1300 /*}}}*/
1301// ProblemResolver::ResolveByKeepInternal - Resolve problems using keep /*{{{*/
1302// ---------------------------------------------------------------------
1303/* This is the work horse of the soft upgrade routine. It is very gental
1304 in that it does not install or remove any packages. It is assumed that the
1305 system was non-broken previously. */
1306bool pkgProblemResolver::ResolveByKeepInternal()
0a8e3465 1307{
74a05226
MV
1308 pkgDepCache::ActionGroup group(Cache);
1309
b2e465d6 1310 unsigned long Size = Cache.Head().PackageCount;
0a8e3465 1311
0a8e3465
AL
1312 MakeScores();
1313
1314 /* We have to order the packages so that the broken fixing pass
1315 operates from highest score to lowest. This prevents problems when
1316 high score packages cause the removal of lower score packages that
1317 would cause the removal of even lower score packages. */
1318 pkgCache::Package **PList = new pkgCache::Package *[Size];
1319 pkgCache::Package **PEnd = PList;
f7f0d6c7 1320 for (pkgCache::PkgIterator I = Cache.PkgBegin(); I.end() == false; ++I)
0a8e3465
AL
1321 *PEnd++ = I;
1322 This = this;
1323 qsort(PList,PEnd - PList,sizeof(*PList),&ScoreSort);
8b4894fe
MV
1324
1325 if (_config->FindB("Debug::pkgProblemResolver::ShowScores",false) == true)
1326 {
1327 clog << "Show Scores" << endl;
1328 for (pkgCache::Package **K = PList; K != PEnd; K++)
1329 if (Scores[(*K)->ID] != 0)
1330 {
1331 pkgCache::PkgIterator Pkg(Cache,*K);
1332 clog << Scores[(*K)->ID] << ' ' << Pkg << std::endl;
1333 }
1334 }
1335
1336 if (Debug == true)
1337 clog << "Entering ResolveByKeep" << endl;
1338
0a8e3465
AL
1339 // Consider each broken package
1340 pkgCache::Package **LastStop = 0;
1341 for (pkgCache::Package **K = PList; K != PEnd; K++)
1342 {
1343 pkgCache::PkgIterator I(Cache,*K);
1344
953b348c 1345 if (Cache[I].InstallVer == 0)
0a8e3465
AL
1346 continue;
1347
953b348c
MV
1348 if (InstOrNewPolicyBroken(I) == false)
1349 continue;
1350
0a8e3465 1351 /* Keep the package. If this works then great, otherwise we have
b2e465d6 1352 to be significantly more agressive and manipulate its dependencies */
0a8e3465
AL
1353 if ((Flags[I->ID] & Protected) == 0)
1354 {
1355 if (Debug == true)
47f6d1b7 1356 clog << "Keeping package " << I.FullName(false) << endl;
74a05226 1357 Cache.MarkKeep(I, false, false);
953b348c 1358 if (InstOrNewPolicyBroken(I) == false)
0a8e3465 1359 {
b2e465d6 1360 K = PList - 1;
0a8e3465
AL
1361 continue;
1362 }
1363 }
1364
1365 // Isolate the problem dependencies
1366 for (pkgCache::DepIterator D = Cache[I].InstVerIter(Cache).DependsList(); D.end() == false;)
1367 {
c5532863
AL
1368 DepIterator Start;
1369 DepIterator End;
1370 D.GlobOr(Start,End);
1371
0a8e3465
AL
1372 // We only worry about critical deps.
1373 if (End.IsCritical() != true)
1374 continue;
1375
1376 // Dep is ok
1377 if ((Cache[End] & pkgDepCache::DepGInstall) == pkgDepCache::DepGInstall)
1378 continue;
c5532863
AL
1379
1380 /* Hm, the group is broken.. I suppose the best thing to do is to
1381 is to try every combination of keep/not-keep for the set, but thats
1382 slow, and this never happens, just be conservative and assume the
1383 list of ors is in preference and keep till it starts to work. */
1384 while (true)
0a8e3465 1385 {
c5532863 1386 if (Debug == true)
47f6d1b7 1387 clog << "Package " << I.FullName(false) << " " << Start << endl;
8b4894fe 1388
c5532863
AL
1389 // Look at all the possible provides on this package
1390 SPtrArray<pkgCache::Version *> VList = Start.AllTargets();
1391 for (pkgCache::Version **V = VList; *V != 0; V++)
0a8e3465 1392 {
c5532863
AL
1393 pkgCache::VerIterator Ver(Cache,*V);
1394 pkgCache::PkgIterator Pkg = Ver.ParentPkg();
1395
1396 // It is not keepable
1397 if (Cache[Pkg].InstallVer == 0 ||
1398 Pkg->CurrentVer == 0)
1399 continue;
1400
1401 if ((Flags[I->ID] & Protected) == 0)
1402 {
1403 if (Debug == true)
47f6d1b7 1404 clog << " Keeping Package " << Pkg.FullName(false) << " due to " << Start.DepType() << endl;
74a05226 1405 Cache.MarkKeep(Pkg, false, false);
c5532863
AL
1406 }
1407
953b348c 1408 if (InstOrNewPolicyBroken(I) == false)
c5532863 1409 break;
0a8e3465
AL
1410 }
1411
953b348c 1412 if (InstOrNewPolicyBroken(I) == false)
0a8e3465 1413 break;
0a8e3465 1414
c5532863
AL
1415 if (Start == End)
1416 break;
f7f0d6c7 1417 ++Start;
c5532863
AL
1418 }
1419
953b348c 1420 if (InstOrNewPolicyBroken(I) == false)
0a8e3465
AL
1421 break;
1422 }
1423
953b348c 1424 if (InstOrNewPolicyBroken(I) == true)
0a8e3465
AL
1425 continue;
1426
1427 // Restart again.
0291f645
JT
1428 if (K == LastStop) {
1429 // I is an iterator based off our temporary package list,
1430 // so copy the name we need before deleting the temporary list
1431 std::string const LoopingPackage = I.FullName(false);
1432 delete[] PList;
1433 return _error->Error("Internal Error, pkgProblemResolver::ResolveByKeep is looping on package %s.", LoopingPackage.c_str());
1434 }
0a8e3465 1435 LastStop = K;
b2e465d6 1436 K = PList - 1;
0291f645 1437 }
6c139d6e 1438
0291f645 1439 delete[] PList;
6c139d6e
AL
1440 return true;
1441}
1442 /*}}}*/
3b5421b4
AL
1443// ProblemResolver::InstallProtect - Install all protected packages /*{{{*/
1444// ---------------------------------------------------------------------
1445/* This is used to make sure protected packages are installed */
1446void pkgProblemResolver::InstallProtect()
1447{
74a05226
MV
1448 pkgDepCache::ActionGroup group(Cache);
1449
f7f0d6c7 1450 for (pkgCache::PkgIterator I = Cache.PkgBegin(); I.end() == false; ++I)
3b5421b4
AL
1451 {
1452 if ((Flags[I->ID] & Protected) == Protected)
1453 {
1454 if ((Flags[I->ID] & ToRemove) == ToRemove)
1455 Cache.MarkDelete(I);
c15f5690
MV
1456 else
1457 {
da543ed8
OS
1458 // preserve the information whether the package was auto
1459 // or manually installed
c15f5690
MV
1460 bool autoInst = (Cache[I].Flags & pkgCache::Flag::Auto);
1461 Cache.MarkInstall(I, false, 0, !autoInst);
1462 }
3b5421b4
AL
1463 }
1464 }
1465}
1466 /*}}}*/
b2e465d6
AL
1467// PrioSortList - Sort a list of versions by priority /*{{{*/
1468// ---------------------------------------------------------------------
1469/* This is ment to be used in conjunction with AllTargets to get a list
1470 of versions ordered by preference. */
1471static pkgCache *PrioCache;
1472static int PrioComp(const void *A,const void *B)
1473{
1474 pkgCache::VerIterator L(*PrioCache,*(pkgCache::Version **)A);
1475 pkgCache::VerIterator R(*PrioCache,*(pkgCache::Version **)B);
1476
1477 if ((L.ParentPkg()->Flags & pkgCache::Flag::Essential) == pkgCache::Flag::Essential &&
b8c0f9b7
AL
1478 (R.ParentPkg()->Flags & pkgCache::Flag::Essential) != pkgCache::Flag::Essential)
1479 return 1;
b2e465d6 1480 if ((L.ParentPkg()->Flags & pkgCache::Flag::Essential) != pkgCache::Flag::Essential &&
b8c0f9b7
AL
1481 (R.ParentPkg()->Flags & pkgCache::Flag::Essential) == pkgCache::Flag::Essential)
1482 return -1;
c5200869
JAK
1483
1484 if ((L.ParentPkg()->Flags & pkgCache::Flag::Important) == pkgCache::Flag::Important &&
1485 (R.ParentPkg()->Flags & pkgCache::Flag::Important) != pkgCache::Flag::Important)
1486 return 1;
1487 if ((L.ParentPkg()->Flags & pkgCache::Flag::Important) != pkgCache::Flag::Important &&
1488 (R.ParentPkg()->Flags & pkgCache::Flag::Important) == pkgCache::Flag::Important)
1489 return -1;
b2e465d6
AL
1490
1491 if (L->Priority != R->Priority)
b8c0f9b7 1492 return R->Priority - L->Priority;
b2e465d6
AL
1493 return strcmp(L.ParentPkg().Name(),R.ParentPkg().Name());
1494}
1495void pkgPrioSortList(pkgCache &Cache,pkgCache::Version **List)
1496{
1497 unsigned long Count = 0;
1498 PrioCache = &Cache;
1499 for (pkgCache::Version **I = List; *I != 0; I++)
1500 Count++;
1501 qsort(List,Count,sizeof(*List),PrioComp);
1502}
1503 /*}}}*/
db09a1c5 1504// ListUpdate - construct Fetcher and update the cache files /*{{{*/
760d4968
MV
1505// ---------------------------------------------------------------------
1506/* This is a simple wrapper to update the cache. it will fetch stuff
1507 * from the network (or any other sources defined in sources.list)
1508 */
1509bool ListUpdate(pkgAcquireStatus &Stat,
1510 pkgSourceList &List,
1511 int PulseInterval)
1512{
1cd1c398
DK
1513 pkgAcquire Fetcher;
1514 if (Fetcher.Setup(&Stat, _config->FindDir("Dir::State::Lists")) == false)
1515 return false;
760d4968
MV
1516
1517 // Populate it with the source selection
1518 if (List.GetIndexes(&Fetcher) == false)
1519 return false;
1520
db09a1c5
DK
1521 return AcquireUpdate(Fetcher, PulseInterval, true);
1522}
1523 /*}}}*/
1524// AcquireUpdate - take Fetcher and update the cache files /*{{{*/
1525// ---------------------------------------------------------------------
1526/* This is a simple wrapper to update the cache with a provided acquire
1527 * If you only need control over Status and the used SourcesList use
1528 * ListUpdate method instead.
1529 */
1530bool AcquireUpdate(pkgAcquire &Fetcher, int const PulseInterval,
1531 bool const RunUpdateScripts, bool const ListCleanup)
1532{
760d4968 1533 // Run scripts
db09a1c5
DK
1534 if (RunUpdateScripts == true)
1535 RunScripts("APT::Update::Pre-Invoke");
1536
1537 pkgAcquire::RunResult res;
1538 if(PulseInterval > 0)
760d4968
MV
1539 res = Fetcher.Run(PulseInterval);
1540 else
1541 res = Fetcher.Run();
1542
1543 if (res == pkgAcquire::Failed)
1544 return false;
1545
1546 bool Failed = false;
1547 bool TransientNetworkFailure = false;
1548 for (pkgAcquire::ItemIterator I = Fetcher.ItemsBegin();
f7f0d6c7 1549 I != Fetcher.ItemsEnd(); ++I)
760d4968
MV
1550 {
1551 if ((*I)->Status == pkgAcquire::Item::StatDone)
1552 continue;
1553
1554 (*I)->Finished();
1555
70b3d263
MV
1556 ::URI uri((*I)->DescURI());
1557 uri.User.clear();
1558 uri.Password.clear();
1559 string descUri = string(uri);
4805f1cf 1560 _error->Warning(_("Failed to fetch %s %s\n"), descUri.c_str(),
760d4968
MV
1561 (*I)->ErrorText.c_str());
1562
1563 if ((*I)->Status == pkgAcquire::Item::StatTransientNetworkError)
1564 {
1565 TransientNetworkFailure = true;
1566 continue;
1567 }
1568
1569 Failed = true;
1570 }
1571
1572 // Clean out any old list files
1573 // Keep "APT::Get::List-Cleanup" name for compatibility, but
1574 // this is really a global option for the APT library now
db09a1c5 1575 if (!TransientNetworkFailure && !Failed && ListCleanup == true &&
b7c5ca8c 1576 (_config->FindB("APT::Get::List-Cleanup",true) == true &&
760d4968
MV
1577 _config->FindB("APT::List-Cleanup",true) == true))
1578 {
1579 if (Fetcher.Clean(_config->FindDir("Dir::State::lists")) == false ||
1580 Fetcher.Clean(_config->FindDir("Dir::State::lists") + "partial/") == false)
1581 // something went wrong with the clean
1582 return false;
1583 }
1584
1585 if (TransientNetworkFailure == true)
196c511c 1586 _error->Warning(_("Some index files failed to download. They have been ignored, or old ones used instead."));
760d4968 1587 else if (Failed == true)
196c511c 1588 return _error->Error(_("Some index files failed to download. They have been ignored, or old ones used instead."));
760d4968
MV
1589
1590
e06c72cd 1591 // Run the success scripts if all was fine
db09a1c5
DK
1592 if (RunUpdateScripts == true)
1593 {
1594 if(!TransientNetworkFailure && !Failed)
1595 RunScripts("APT::Update::Post-Invoke-Success");
e06c72cd 1596
db09a1c5
DK
1597 // Run the other scripts
1598 RunScripts("APT::Update::Post-Invoke");
1599 }
760d4968
MV
1600 return true;
1601}
1602 /*}}}*/