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