]>
Commit | Line | Data |
---|---|---|
6c139d6e AL |
1 | // -*- mode: cpp; mode: fold -*- |
2 | // Description /*{{{*/ | |
cd14eaf2 | 3 | // $Id: algorithms.cc,v 1.40 2002/04/25 05:45:18 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 /*{{{*/ | |
17 | #ifdef __GNUG__ | |
094a497d | 18 | #pragma implementation "apt-pkg/algorithms.h" |
6c139d6e | 19 | #endif |
094a497d AL |
20 | #include <apt-pkg/algorithms.h> |
21 | #include <apt-pkg/error.h> | |
0a8e3465 | 22 | #include <apt-pkg/configuration.h> |
b2e465d6 AL |
23 | #include <apt-pkg/sptr.h> |
24 | ||
25 | #include <apti18n.h> | |
26 | ||
90f057fd | 27 | #include <iostream> |
6c139d6e AL |
28 | /*}}}*/ |
29 | ||
30 | pkgProblemResolver *pkgProblemResolver::This = 0; | |
31 | ||
32 | // Simulate::Simulate - Constructor /*{{{*/ | |
33 | // --------------------------------------------------------------------- | |
b2e465d6 AL |
34 | /* The legacy translations here of input Pkg iterators is obsolete, |
35 | this is not necessary since the pkgCaches are fully shared now. */ | |
36 | pkgSimulate::pkgSimulate(pkgDepCache *Cache) : pkgPackageManager(Cache), | |
37 | iPolicy(Cache), | |
38 | Sim(&Cache->GetCache(),&iPolicy) | |
6c139d6e | 39 | { |
b2e465d6 AL |
40 | Sim.Init(0); |
41 | Flags = new unsigned char[Cache->Head().PackageCount]; | |
42 | memset(Flags,0,sizeof(*Flags)*Cache->Head().PackageCount); | |
281daf46 AL |
43 | |
44 | // Fake a filename so as not to activate the media swapping | |
45 | string Jnk = "SIMULATE"; | |
b2e465d6 | 46 | for (unsigned int I = 0; I != Cache->Head().PackageCount; I++) |
281daf46 | 47 | FileNames[I] = Jnk; |
6c139d6e AL |
48 | } |
49 | /*}}}*/ | |
b2e465d6 AL |
50 | // Simulate::Describe - Describe a package /*{{{*/ |
51 | // --------------------------------------------------------------------- | |
52 | /* */ | |
53 | void pkgSimulate::Describe(PkgIterator Pkg,ostream &out,bool Now) | |
54 | { | |
55 | VerIterator Ver(Sim); | |
56 | if (Now == true) | |
57 | Ver = Pkg.CurrentVer(); | |
58 | else | |
59 | Ver = Sim[Pkg].CandidateVerIter(Sim); | |
60 | ||
61 | out << Pkg.Name(); | |
62 | ||
63 | if (Ver.end() == true) | |
64 | return; | |
65 | ||
66 | out << " (" << Ver.VerStr() << ' ' << Ver.RelStr() << ')'; | |
67 | } | |
68 | /*}}}*/ | |
6c139d6e AL |
69 | // Simulate::Install - Simulate unpacking of a package /*{{{*/ |
70 | // --------------------------------------------------------------------- | |
71 | /* */ | |
72 | bool pkgSimulate::Install(PkgIterator iPkg,string /*File*/) | |
73 | { | |
74 | // Adapt the iterator | |
75 | PkgIterator Pkg = Sim.FindPkg(iPkg.Name()); | |
76 | Flags[Pkg->ID] = 1; | |
77 | ||
b2e465d6 AL |
78 | cout << "Inst "; |
79 | Describe(Pkg,cout,false); | |
6c139d6e AL |
80 | Sim.MarkInstall(Pkg,false); |
81 | ||
82 | // Look for broken conflicts+predepends. | |
83 | for (PkgIterator I = Sim.PkgBegin(); I.end() == false; I++) | |
84 | { | |
85 | if (Sim[I].InstallVer == 0) | |
86 | continue; | |
87 | ||
b2e465d6 AL |
88 | for (DepIterator D = Sim[I].InstVerIter(Sim).DependsList(); D.end() == false;) |
89 | { | |
90 | DepIterator Start; | |
91 | DepIterator End; | |
92 | D.GlobOr(Start,End); | |
93 | if (Start->Type == pkgCache::Dep::Conflicts || | |
94 | Start->Type == pkgCache::Dep::Obsoletes || | |
95 | End->Type == pkgCache::Dep::PreDepends) | |
6c139d6e | 96 | { |
b2e465d6 | 97 | if ((Sim[End] & pkgDepCache::DepGInstall) == 0) |
6c139d6e | 98 | { |
b2e465d6 AL |
99 | cout << " [" << I.Name() << " on " << Start.TargetPkg().Name() << ']'; |
100 | if (Start->Type == pkgCache::Dep::Conflicts) | |
6c139d6e AL |
101 | _error->Error("Fatal, conflicts violated %s",I.Name()); |
102 | } | |
b2e465d6 AL |
103 | } |
104 | } | |
6c139d6e AL |
105 | } |
106 | ||
107 | if (Sim.BrokenCount() != 0) | |
108 | ShortBreaks(); | |
109 | else | |
04aa15a8 | 110 | cout << endl; |
6c139d6e AL |
111 | return true; |
112 | } | |
113 | /*}}}*/ | |
114 | // Simulate::Configure - Simulate configuration of a Package /*{{{*/ | |
115 | // --------------------------------------------------------------------- | |
116 | /* This is not an acurate simulation of relatity, we should really not | |
117 | install the package.. For some investigations it may be necessary | |
118 | however. */ | |
119 | bool pkgSimulate::Configure(PkgIterator iPkg) | |
120 | { | |
121 | // Adapt the iterator | |
122 | PkgIterator Pkg = Sim.FindPkg(iPkg.Name()); | |
123 | ||
124 | Flags[Pkg->ID] = 2; | |
125 | // Sim.MarkInstall(Pkg,false); | |
126 | if (Sim[Pkg].InstBroken() == true) | |
127 | { | |
04aa15a8 | 128 | cout << "Conf " << Pkg.Name() << " broken" << endl; |
6c139d6e AL |
129 | |
130 | Sim.Update(); | |
131 | ||
132 | // Print out each package and the failed dependencies | |
133 | for (pkgCache::DepIterator D = Sim[Pkg].InstVerIter(Sim).DependsList(); D.end() == false; D++) | |
134 | { | |
135 | if (Sim.IsImportantDep(D) == false || | |
136 | (Sim[D] & pkgDepCache::DepInstall) != 0) | |
137 | continue; | |
138 | ||
b2e465d6 AL |
139 | if (D->Type == pkgCache::Dep::Obsoletes) |
140 | cout << " Obsoletes:" << D.TargetPkg().Name(); | |
141 | else if (D->Type == pkgCache::Dep::Conflicts) | |
04aa15a8 | 142 | cout << " Conflicts:" << D.TargetPkg().Name(); |
6c139d6e | 143 | else |
04aa15a8 | 144 | cout << " Depends:" << D.TargetPkg().Name(); |
6c139d6e | 145 | } |
04aa15a8 | 146 | cout << endl; |
6c139d6e AL |
147 | |
148 | _error->Error("Conf Broken %s",Pkg.Name()); | |
149 | } | |
150 | else | |
b2e465d6 AL |
151 | { |
152 | cout << "Conf "; | |
153 | Describe(Pkg,cout,false); | |
154 | } | |
6c139d6e AL |
155 | |
156 | if (Sim.BrokenCount() != 0) | |
157 | ShortBreaks(); | |
158 | else | |
04aa15a8 | 159 | cout << endl; |
6c139d6e AL |
160 | |
161 | return true; | |
162 | } | |
163 | /*}}}*/ | |
164 | // Simulate::Remove - Simulate the removal of a package /*{{{*/ | |
165 | // --------------------------------------------------------------------- | |
166 | /* */ | |
fc4b5c9f | 167 | bool pkgSimulate::Remove(PkgIterator iPkg,bool Purge) |
6c139d6e AL |
168 | { |
169 | // Adapt the iterator | |
170 | PkgIterator Pkg = Sim.FindPkg(iPkg.Name()); | |
171 | ||
172 | Flags[Pkg->ID] = 3; | |
173 | Sim.MarkDelete(Pkg); | |
fc4b5c9f | 174 | if (Purge == true) |
b2e465d6 | 175 | cout << "Purg "; |
fc4b5c9f | 176 | else |
b2e465d6 AL |
177 | cout << "Remv "; |
178 | Describe(Pkg,cout,false); | |
6c139d6e AL |
179 | |
180 | if (Sim.BrokenCount() != 0) | |
181 | ShortBreaks(); | |
182 | else | |
04aa15a8 | 183 | cout << endl; |
6c139d6e AL |
184 | |
185 | return true; | |
186 | } | |
187 | /*}}}*/ | |
188 | // Simulate::ShortBreaks - Print out a short line describing all breaks /*{{{*/ | |
189 | // --------------------------------------------------------------------- | |
190 | /* */ | |
191 | void pkgSimulate::ShortBreaks() | |
192 | { | |
04aa15a8 | 193 | cout << " ["; |
6c139d6e AL |
194 | for (PkgIterator I = Sim.PkgBegin(); I.end() == false; I++) |
195 | { | |
196 | if (Sim[I].InstBroken() == true) | |
197 | { | |
198 | if (Flags[I->ID] == 0) | |
04aa15a8 | 199 | cout << I.Name() << ' '; |
6c139d6e | 200 | /* else |
04aa15a8 | 201 | cout << I.Name() << "! ";*/ |
6c139d6e AL |
202 | } |
203 | } | |
04aa15a8 | 204 | cout << ']' << endl; |
6c139d6e AL |
205 | } |
206 | /*}}}*/ | |
207 | // ApplyStatus - Adjust for non-ok packages /*{{{*/ | |
208 | // --------------------------------------------------------------------- | |
209 | /* We attempt to change the state of the all packages that have failed | |
210 | installation toward their real state. The ordering code will perform | |
211 | the necessary calculations to deal with the problems. */ | |
212 | bool pkgApplyStatus(pkgDepCache &Cache) | |
213 | { | |
214 | for (pkgCache::PkgIterator I = Cache.PkgBegin(); I.end() == false; I++) | |
215 | { | |
e481d5b0 AL |
216 | if (I->VersionList == 0) |
217 | continue; | |
218 | ||
d38b7b3d AL |
219 | // Only choice for a ReInstReq package is to reinstall |
220 | if (I->InstState == pkgCache::State::ReInstReq || | |
221 | I->InstState == pkgCache::State::HoldReInstReq) | |
222 | { | |
5871718b | 223 | if (I->CurrentVer != 0 && I.CurrentVer().Downloadable() == true) |
813c8eea AL |
224 | Cache.MarkKeep(I); |
225 | else | |
226 | { | |
227 | // Is this right? Will dpkg choke on an upgrade? | |
2a3f3893 AL |
228 | if (Cache[I].CandidateVer != 0 && |
229 | Cache[I].CandidateVerIter(Cache).Downloadable() == true) | |
813c8eea AL |
230 | Cache.MarkInstall(I); |
231 | else | |
b2e465d6 AL |
232 | return _error->Error(_("The package %s needs to be reinstalled, " |
233 | "but I can't find an archive for it."),I.Name()); | |
813c8eea AL |
234 | } |
235 | ||
d38b7b3d AL |
236 | continue; |
237 | } | |
238 | ||
6c139d6e AL |
239 | switch (I->CurrentState) |
240 | { | |
813c8eea AL |
241 | /* This means installation failed somehow - it does not need to be |
242 | re-unpacked (probably) */ | |
b518cca6 AL |
243 | case pkgCache::State::UnPacked: |
244 | case pkgCache::State::HalfConfigured: | |
5871718b | 245 | if ((I->CurrentVer != 0 && I.CurrentVer().Downloadable() == true) || |
813c8eea AL |
246 | I.State() != pkgCache::PkgIterator::NeedsUnpack) |
247 | Cache.MarkKeep(I); | |
248 | else | |
249 | { | |
2a3f3893 AL |
250 | if (Cache[I].CandidateVer != 0 && |
251 | Cache[I].CandidateVerIter(Cache).Downloadable() == true) | |
813c8eea AL |
252 | Cache.MarkInstall(I); |
253 | else | |
254 | Cache.MarkDelete(I); | |
255 | } | |
6c139d6e AL |
256 | break; |
257 | ||
258 | // This means removal failed | |
b518cca6 | 259 | case pkgCache::State::HalfInstalled: |
6c139d6e AL |
260 | Cache.MarkDelete(I); |
261 | break; | |
262 | ||
263 | default: | |
b518cca6 | 264 | if (I->InstState != pkgCache::State::Ok) |
6c139d6e AL |
265 | return _error->Error("The package %s is not ok and I " |
266 | "don't know how to fix it!",I.Name()); | |
267 | } | |
268 | } | |
269 | return true; | |
270 | } | |
271 | /*}}}*/ | |
272 | // FixBroken - Fix broken packages /*{{{*/ | |
273 | // --------------------------------------------------------------------- | |
0a8e3465 AL |
274 | /* This autoinstalls every broken package and then runs the problem resolver |
275 | on the result. */ | |
6c139d6e AL |
276 | bool pkgFixBroken(pkgDepCache &Cache) |
277 | { | |
278 | // Auto upgrade all broken packages | |
279 | for (pkgCache::PkgIterator I = Cache.PkgBegin(); I.end() == false; I++) | |
280 | if (Cache[I].NowBroken() == true) | |
281 | Cache.MarkInstall(I,true); | |
7e798dd7 | 282 | |
6c139d6e AL |
283 | /* Fix packages that are in a NeedArchive state but don't have a |
284 | downloadable install version */ | |
285 | for (pkgCache::PkgIterator I = Cache.PkgBegin(); I.end() == false; I++) | |
286 | { | |
287 | if (I.State() != pkgCache::PkgIterator::NeedsUnpack || | |
288 | Cache[I].Delete() == true) | |
289 | continue; | |
290 | ||
b518cca6 | 291 | if (Cache[I].InstVerIter(Cache).Downloadable() == false) |
6c139d6e AL |
292 | continue; |
293 | ||
7e798dd7 | 294 | Cache.MarkInstall(I,true); |
6c139d6e AL |
295 | } |
296 | ||
b2e465d6 | 297 | pkgProblemResolver Fix(&Cache); |
6c139d6e AL |
298 | return Fix.Resolve(true); |
299 | } | |
300 | /*}}}*/ | |
301 | // DistUpgrade - Distribution upgrade /*{{{*/ | |
302 | // --------------------------------------------------------------------- | |
303 | /* This autoinstalls every package and then force installs every | |
304 | pre-existing package. This creates the initial set of conditions which | |
305 | most likely contain problems because too many things were installed. | |
306 | ||
0a8e3465 | 307 | The problem resolver is used to resolve the problems. |
6c139d6e AL |
308 | */ |
309 | bool pkgDistUpgrade(pkgDepCache &Cache) | |
310 | { | |
311 | /* Auto upgrade all installed packages, this provides the basis | |
312 | for the installation */ | |
313 | for (pkgCache::PkgIterator I = Cache.PkgBegin(); I.end() == false; I++) | |
314 | if (I->CurrentVer != 0) | |
315 | Cache.MarkInstall(I,true); | |
316 | ||
317 | /* Now, auto upgrade all essential packages - this ensures that | |
318 | the essential packages are present and working */ | |
319 | for (pkgCache::PkgIterator I = Cache.PkgBegin(); I.end() == false; I++) | |
b518cca6 | 320 | if ((I->Flags & pkgCache::Flag::Essential) == pkgCache::Flag::Essential) |
6c139d6e AL |
321 | Cache.MarkInstall(I,true); |
322 | ||
323 | /* We do it again over all previously installed packages to force | |
324 | conflict resolution on them all. */ | |
325 | for (pkgCache::PkgIterator I = Cache.PkgBegin(); I.end() == false; I++) | |
326 | if (I->CurrentVer != 0) | |
327 | Cache.MarkInstall(I,false); | |
328 | ||
b2e465d6 | 329 | pkgProblemResolver Fix(&Cache); |
c88edf1d | 330 | |
6c139d6e | 331 | // Hold back held packages. |
4490f2de | 332 | if (_config->FindB("APT::Ignore-Hold",false) == false) |
6c139d6e | 333 | { |
c88edf1d | 334 | for (pkgCache::PkgIterator I = Cache.PkgBegin(); I.end() == false; I++) |
6c139d6e | 335 | { |
c88edf1d AL |
336 | if (I->SelectedState == pkgCache::State::Hold) |
337 | { | |
338 | Fix.Protect(I); | |
339 | Cache.MarkKeep(I); | |
340 | } | |
6c139d6e AL |
341 | } |
342 | } | |
343 | ||
344 | return Fix.Resolve(); | |
345 | } | |
346 | /*}}}*/ | |
0a8e3465 AL |
347 | // AllUpgrade - Upgrade as many packages as possible /*{{{*/ |
348 | // --------------------------------------------------------------------- | |
349 | /* Right now the system must be consistent before this can be called. | |
350 | It also will not change packages marked for install, it only tries | |
351 | to install packages not marked for install */ | |
352 | bool pkgAllUpgrade(pkgDepCache &Cache) | |
353 | { | |
b2e465d6 | 354 | pkgProblemResolver Fix(&Cache); |
0a8e3465 AL |
355 | |
356 | if (Cache.BrokenCount() != 0) | |
357 | return false; | |
358 | ||
359 | // Upgrade all installed packages | |
360 | for (pkgCache::PkgIterator I = Cache.PkgBegin(); I.end() == false; I++) | |
361 | { | |
362 | if (Cache[I].Install() == true) | |
363 | Fix.Protect(I); | |
364 | ||
b2e465d6 | 365 | if (_config->FindB("APT::Ignore-Hold",false) == false) |
c88edf1d AL |
366 | if (I->SelectedState == pkgCache::State::Hold) |
367 | continue; | |
0a8e3465 AL |
368 | |
369 | if (I->CurrentVer != 0 && Cache[I].InstallVer != 0) | |
370 | Cache.MarkInstall(I,false); | |
371 | } | |
372 | ||
373 | return Fix.ResolveByKeep(); | |
374 | } | |
375 | /*}}}*/ | |
7e798dd7 AL |
376 | // MinimizeUpgrade - Minimizes the set of packages to be upgraded /*{{{*/ |
377 | // --------------------------------------------------------------------- | |
378 | /* This simply goes over the entire set of packages and tries to keep | |
379 | each package marked for upgrade. If a conflict is generated then | |
380 | the package is restored. */ | |
381 | bool pkgMinimizeUpgrade(pkgDepCache &Cache) | |
382 | { | |
383 | if (Cache.BrokenCount() != 0) | |
384 | return false; | |
385 | ||
abc8419e | 386 | // We loop for 10 tries to get the minimal set size. |
7e798dd7 | 387 | bool Change = false; |
a005475e | 388 | unsigned int Count = 0; |
7e798dd7 AL |
389 | do |
390 | { | |
391 | Change = false; | |
392 | for (pkgCache::PkgIterator I = Cache.PkgBegin(); I.end() == false; I++) | |
393 | { | |
394 | // Not interesting | |
395 | if (Cache[I].Upgrade() == false || Cache[I].NewInstall() == true) | |
396 | continue; | |
a005475e | 397 | |
7e798dd7 AL |
398 | // Keep it and see if that is OK |
399 | Cache.MarkKeep(I); | |
400 | if (Cache.BrokenCount() != 0) | |
401 | Cache.MarkInstall(I,false); | |
402 | else | |
a005475e AL |
403 | { |
404 | // If keep didnt actually do anything then there was no change.. | |
405 | if (Cache[I].Upgrade() == false) | |
406 | Change = true; | |
407 | } | |
7e798dd7 | 408 | } |
a005475e | 409 | Count++; |
7e798dd7 | 410 | } |
a005475e | 411 | while (Change == true && Count < 10); |
7e798dd7 AL |
412 | |
413 | if (Cache.BrokenCount() != 0) | |
414 | return _error->Error("Internal Error in pkgMinimizeUpgrade"); | |
415 | ||
416 | return true; | |
417 | } | |
418 | /*}}}*/ | |
6c139d6e AL |
419 | |
420 | // ProblemResolver::pkgProblemResolver - Constructor /*{{{*/ | |
421 | // --------------------------------------------------------------------- | |
422 | /* */ | |
b2e465d6 | 423 | pkgProblemResolver::pkgProblemResolver(pkgDepCache *pCache) : Cache(*pCache) |
6c139d6e AL |
424 | { |
425 | // Allocate memory | |
b2e465d6 | 426 | unsigned long Size = Cache.Head().PackageCount; |
6c139d6e AL |
427 | Scores = new signed short[Size]; |
428 | Flags = new unsigned char[Size]; | |
429 | memset(Flags,0,sizeof(*Flags)*Size); | |
430 | ||
431 | // Set debug to true to see its decision logic | |
0a8e3465 | 432 | Debug = _config->FindB("Debug::pkgProblemResolver",false); |
6c139d6e AL |
433 | } |
434 | /*}}}*/ | |
b2e465d6 AL |
435 | // ProblemResolver::~pkgProblemResolver - Destructor /*{{{*/ |
436 | // --------------------------------------------------------------------- | |
437 | /* */ | |
438 | pkgProblemResolver::~pkgProblemResolver() | |
439 | { | |
440 | delete [] Scores; | |
441 | delete [] Flags; | |
442 | } | |
443 | /*}}}*/ | |
6c139d6e AL |
444 | // ProblemResolver::ScoreSort - Sort the list by score /*{{{*/ |
445 | // --------------------------------------------------------------------- | |
446 | /* */ | |
447 | int pkgProblemResolver::ScoreSort(const void *a,const void *b) | |
448 | { | |
449 | Package const **A = (Package const **)a; | |
450 | Package const **B = (Package const **)b; | |
451 | if (This->Scores[(*A)->ID] > This->Scores[(*B)->ID]) | |
452 | return -1; | |
453 | if (This->Scores[(*A)->ID] < This->Scores[(*B)->ID]) | |
454 | return 1; | |
455 | return 0; | |
456 | } | |
457 | /*}}}*/ | |
458 | // ProblemResolver::MakeScores - Make the score table /*{{{*/ | |
459 | // --------------------------------------------------------------------- | |
460 | /* */ | |
461 | void pkgProblemResolver::MakeScores() | |
462 | { | |
b2e465d6 | 463 | unsigned long Size = Cache.Head().PackageCount; |
6c139d6e AL |
464 | memset(Scores,0,sizeof(*Scores)*Size); |
465 | ||
466 | // Generate the base scores for a package based on its properties | |
467 | for (pkgCache::PkgIterator I = Cache.PkgBegin(); I.end() == false; I++) | |
468 | { | |
469 | if (Cache[I].InstallVer == 0) | |
470 | continue; | |
471 | ||
472 | signed short &Score = Scores[I->ID]; | |
473 | ||
474 | /* This is arbitary, it should be high enough to elevate an | |
475 | essantial package above most other packages but low enough | |
476 | to allow an obsolete essential packages to be removed by | |
477 | a conflicts on a powerfull normal package (ie libc6) */ | |
b518cca6 | 478 | if ((I->Flags & pkgCache::Flag::Essential) == pkgCache::Flag::Essential) |
6c139d6e AL |
479 | Score += 100; |
480 | ||
481 | // We transform the priority | |
482 | // Important Required Standard Optional Extra | |
483 | signed short PrioMap[] = {0,3,2,1,-1,-2}; | |
484 | if (Cache[I].InstVerIter(Cache)->Priority <= 5) | |
485 | Score += PrioMap[Cache[I].InstVerIter(Cache)->Priority]; | |
486 | ||
487 | /* This helps to fix oddball problems with conflicting packages | |
488 | on the same level. We enhance the score of installed packages */ | |
489 | if (I->CurrentVer != 0) | |
490 | Score += 1; | |
491 | } | |
492 | ||
493 | // Now that we have the base scores we go and propogate dependencies | |
494 | for (pkgCache::PkgIterator I = Cache.PkgBegin(); I.end() == false; I++) | |
495 | { | |
496 | if (Cache[I].InstallVer == 0) | |
497 | continue; | |
498 | ||
499 | for (pkgCache::DepIterator D = Cache[I].InstVerIter(Cache).DependsList(); D.end() == false; D++) | |
500 | { | |
b518cca6 | 501 | if (D->Type == pkgCache::Dep::Depends || D->Type == pkgCache::Dep::PreDepends) |
6c139d6e AL |
502 | Scores[D.TargetPkg()->ID]++; |
503 | } | |
504 | } | |
505 | ||
506 | // Copy the scores to advoid additive looping | |
b2e465d6 | 507 | SPtrArray<signed short> OldScores = new signed short[Size]; |
6c139d6e AL |
508 | memcpy(OldScores,Scores,sizeof(*Scores)*Size); |
509 | ||
510 | /* Now we cause 1 level of dependency inheritance, that is we add the | |
511 | score of the packages that depend on the target Package. This | |
512 | fortifies high scoring packages */ | |
513 | for (pkgCache::PkgIterator I = Cache.PkgBegin(); I.end() == false; I++) | |
514 | { | |
515 | if (Cache[I].InstallVer == 0) | |
516 | continue; | |
517 | ||
518 | for (pkgCache::DepIterator D = I.RevDependsList(); D.end() == false; D++) | |
519 | { | |
520 | // Only do it for the install version | |
521 | if ((pkgCache::Version *)D.ParentVer() != Cache[D.ParentPkg()].InstallVer || | |
b518cca6 | 522 | (D->Type != pkgCache::Dep::Depends && D->Type != pkgCache::Dep::PreDepends)) |
6c139d6e AL |
523 | continue; |
524 | ||
525 | Scores[I->ID] += abs(OldScores[D.ParentPkg()->ID]); | |
526 | } | |
527 | } | |
528 | ||
529 | /* Now we propogate along provides. This makes the packages that | |
530 | provide important packages extremely important */ | |
531 | for (pkgCache::PkgIterator I = Cache.PkgBegin(); I.end() == false; I++) | |
532 | { | |
533 | for (pkgCache::PrvIterator P = I.ProvidesList(); P.end() == false; P++) | |
534 | { | |
535 | // Only do it once per package | |
536 | if ((pkgCache::Version *)P.OwnerVer() != Cache[P.OwnerPkg()].InstallVer) | |
537 | continue; | |
538 | Scores[P.OwnerPkg()->ID] += abs(Scores[I->ID] - OldScores[I->ID]); | |
539 | } | |
540 | } | |
541 | ||
542 | /* Protected things are pushed really high up. This number should put them | |
543 | ahead of everything */ | |
544 | for (pkgCache::PkgIterator I = Cache.PkgBegin(); I.end() == false; I++) | |
d2685fd6 | 545 | { |
6c139d6e AL |
546 | if ((Flags[I->ID] & Protected) != 0) |
547 | Scores[I->ID] += 10000; | |
d2685fd6 AL |
548 | if ((I->Flags & pkgCache::Flag::Essential) == pkgCache::Flag::Essential) |
549 | Scores[I->ID] += 5000; | |
b2e465d6 | 550 | } |
6c139d6e AL |
551 | } |
552 | /*}}}*/ | |
553 | // ProblemResolver::DoUpgrade - Attempt to upgrade this package /*{{{*/ | |
554 | // --------------------------------------------------------------------- | |
555 | /* This goes through and tries to reinstall packages to make this package | |
556 | installable */ | |
557 | bool pkgProblemResolver::DoUpgrade(pkgCache::PkgIterator Pkg) | |
558 | { | |
559 | if ((Flags[Pkg->ID] & Upgradable) == 0 || Cache[Pkg].Upgradable() == false) | |
560 | return false; | |
0a8e3465 | 561 | |
6c139d6e AL |
562 | Flags[Pkg->ID] &= ~Upgradable; |
563 | ||
564 | bool WasKept = Cache[Pkg].Keep(); | |
565 | Cache.MarkInstall(Pkg,false); | |
566 | ||
0a8e3465 AL |
567 | // This must be a virtual package or something like that. |
568 | if (Cache[Pkg].InstVerIter(Cache).end() == true) | |
569 | return false; | |
570 | ||
6c139d6e AL |
571 | // Isolate the problem dependency |
572 | bool Fail = false; | |
573 | for (pkgCache::DepIterator D = Cache[Pkg].InstVerIter(Cache).DependsList(); D.end() == false;) | |
574 | { | |
575 | // Compute a single dependency element (glob or) | |
576 | pkgCache::DepIterator Start = D; | |
577 | pkgCache::DepIterator End = D; | |
578 | unsigned char State = 0; | |
4b1b89c5 | 579 | for (bool LastOR = true; D.end() == false && LastOR == true;) |
6c139d6e AL |
580 | { |
581 | State |= Cache[D]; | |
b518cca6 | 582 | LastOR = (D->CompareOp & pkgCache::Dep::Or) == pkgCache::Dep::Or; |
4b1b89c5 | 583 | D++; |
6c139d6e AL |
584 | if (LastOR == true) |
585 | End = D; | |
586 | } | |
587 | ||
588 | // We only worry about critical deps. | |
589 | if (End.IsCritical() != true) | |
590 | continue; | |
4b1b89c5 AL |
591 | |
592 | // Iterate over all the members in the or group | |
593 | while (1) | |
0a8e3465 | 594 | { |
4b1b89c5 AL |
595 | // Dep is ok now |
596 | if ((Cache[End] & pkgDepCache::DepGInstall) == pkgDepCache::DepGInstall) | |
597 | break; | |
598 | ||
599 | // Do not change protected packages | |
600 | PkgIterator P = Start.SmartTargetPkg(); | |
601 | if ((Flags[P->ID] & Protected) == Protected) | |
602 | { | |
603 | if (Debug == true) | |
648e3cb4 | 604 | clog << " Reinst Failed because of protected " << P.Name() << endl; |
4b1b89c5 | 605 | Fail = true; |
4b1b89c5 | 606 | } |
648e3cb4 | 607 | else |
6c139d6e | 608 | { |
648e3cb4 AL |
609 | // Upgrade the package if the candidate version will fix the problem. |
610 | if ((Cache[Start] & pkgDepCache::DepCVer) == pkgDepCache::DepCVer) | |
611 | { | |
612 | if (DoUpgrade(P) == false) | |
613 | { | |
614 | if (Debug == true) | |
615 | clog << " Reinst Failed because of " << P.Name() << endl; | |
616 | Fail = true; | |
617 | } | |
618 | else | |
619 | { | |
620 | Fail = false; | |
621 | break; | |
622 | } | |
623 | } | |
624 | else | |
4b1b89c5 | 625 | { |
648e3cb4 AL |
626 | /* We let the algorithm deal with conflicts on its next iteration, |
627 | it is much smarter than us */ | |
b2e465d6 AL |
628 | if (Start->Type == pkgCache::Dep::Conflicts || |
629 | Start->Type == pkgCache::Dep::Obsoletes) | |
630 | break; | |
648e3cb4 | 631 | |
4b1b89c5 | 632 | if (Debug == true) |
648e3cb4 | 633 | clog << " Reinst Failed early because of " << Start.TargetPkg().Name() << endl; |
4b1b89c5 | 634 | Fail = true; |
648e3cb4 | 635 | } |
4b1b89c5 | 636 | } |
6c139d6e | 637 | |
4b1b89c5 AL |
638 | if (Start == End) |
639 | break; | |
640 | Start++; | |
641 | } | |
642 | if (Fail == true) | |
6c139d6e | 643 | break; |
6c139d6e AL |
644 | } |
645 | ||
646 | // Undo our operations - it might be smart to undo everything this did.. | |
647 | if (Fail == true) | |
648 | { | |
649 | if (WasKept == true) | |
650 | Cache.MarkKeep(Pkg); | |
651 | else | |
652 | Cache.MarkDelete(Pkg); | |
653 | return false; | |
654 | } | |
655 | ||
656 | if (Debug == true) | |
0a8e3465 | 657 | clog << " Re-Instated " << Pkg.Name() << endl; |
6c139d6e AL |
658 | return true; |
659 | } | |
660 | /*}}}*/ | |
661 | // ProblemResolver::Resolve - Run the resolution pass /*{{{*/ | |
662 | // --------------------------------------------------------------------- | |
663 | /* This routines works by calculating a score for each package. The score | |
664 | is derived by considering the package's priority and all reverse | |
665 | dependents giving an integer that reflects the amount of breakage that | |
666 | adjusting the package will inflict. | |
667 | ||
668 | It goes from highest score to lowest and corrects all of the breaks by | |
669 | keeping or removing the dependant packages. If that fails then it removes | |
670 | the package itself and goes on. The routine should be able to intelligently | |
671 | go from any broken state to a fixed state. | |
672 | ||
673 | The BrokenFix flag enables a mode where the algorithm tries to | |
674 | upgrade packages to advoid problems. */ | |
675 | bool pkgProblemResolver::Resolve(bool BrokenFix) | |
676 | { | |
b2e465d6 | 677 | unsigned long Size = Cache.Head().PackageCount; |
6c139d6e AL |
678 | |
679 | // Record which packages are marked for install | |
680 | bool Again = false; | |
681 | do | |
682 | { | |
683 | Again = false; | |
684 | for (pkgCache::PkgIterator I = Cache.PkgBegin(); I.end() == false; I++) | |
685 | { | |
686 | if (Cache[I].Install() == true) | |
687 | Flags[I->ID] |= PreInstalled; | |
688 | else | |
689 | { | |
690 | if (Cache[I].InstBroken() == true && BrokenFix == true) | |
691 | { | |
692 | Cache.MarkInstall(I,false); | |
693 | if (Cache[I].Install() == true) | |
694 | Again = true; | |
695 | } | |
696 | ||
697 | Flags[I->ID] &= ~PreInstalled; | |
698 | } | |
699 | Flags[I->ID] |= Upgradable; | |
700 | } | |
701 | } | |
702 | while (Again == true); | |
703 | ||
704 | if (Debug == true) | |
0a8e3465 | 705 | clog << "Starting" << endl; |
6c139d6e AL |
706 | |
707 | MakeScores(); | |
708 | ||
709 | /* We have to order the packages so that the broken fixing pass | |
710 | operates from highest score to lowest. This prevents problems when | |
711 | high score packages cause the removal of lower score packages that | |
712 | would cause the removal of even lower score packages. */ | |
b2e465d6 | 713 | SPtrArray<pkgCache::Package *> PList = new pkgCache::Package *[Size]; |
6c139d6e AL |
714 | pkgCache::Package **PEnd = PList; |
715 | for (pkgCache::PkgIterator I = Cache.PkgBegin(); I.end() == false; I++) | |
716 | *PEnd++ = I; | |
717 | This = this; | |
718 | qsort(PList,PEnd - PList,sizeof(*PList),&ScoreSort); | |
719 | ||
720 | /* for (pkgCache::Package **K = PList; K != PEnd; K++) | |
721 | if (Scores[(*K)->ID] != 0) | |
722 | { | |
723 | pkgCache::PkgIterator Pkg(Cache,*K); | |
0a8e3465 | 724 | clog << Scores[(*K)->ID] << ' ' << Pkg.Name() << |
6c139d6e AL |
725 | ' ' << (pkgCache::Version *)Pkg.CurrentVer() << ' ' << |
726 | Cache[Pkg].InstallVer << ' ' << Cache[Pkg].CandidateVer << endl; | |
727 | } */ | |
728 | ||
729 | if (Debug == true) | |
0a8e3465 | 730 | clog << "Starting 2" << endl; |
6c139d6e AL |
731 | |
732 | /* Now consider all broken packages. For each broken package we either | |
733 | remove the package or fix it's problem. We do this once, it should | |
734 | not be possible for a loop to form (that is a < b < c and fixing b by | |
735 | changing a breaks c) */ | |
736 | bool Change = true; | |
737 | for (int Counter = 0; Counter != 10 && Change == true; Counter++) | |
738 | { | |
739 | Change = false; | |
740 | for (pkgCache::Package **K = PList; K != PEnd; K++) | |
741 | { | |
742 | pkgCache::PkgIterator I(Cache,*K); | |
743 | ||
744 | /* We attempt to install this and see if any breaks result, | |
745 | this takes care of some strange cases */ | |
746 | if (Cache[I].CandidateVer != Cache[I].InstallVer && | |
747 | I->CurrentVer != 0 && Cache[I].InstallVer != 0 && | |
748 | (Flags[I->ID] & PreInstalled) != 0 && | |
0a8e3465 AL |
749 | (Flags[I->ID] & Protected) == 0 && |
750 | (Flags[I->ID] & ReInstateTried) == 0) | |
6c139d6e AL |
751 | { |
752 | if (Debug == true) | |
0a8e3465 | 753 | clog << " Try to Re-Instate " << I.Name() << endl; |
a6568219 | 754 | unsigned long OldBreaks = Cache.BrokenCount(); |
6c139d6e | 755 | pkgCache::Version *OldVer = Cache[I].InstallVer; |
0a8e3465 AL |
756 | Flags[I->ID] &= ReInstateTried; |
757 | ||
6c139d6e AL |
758 | Cache.MarkInstall(I,false); |
759 | if (Cache[I].InstBroken() == true || | |
760 | OldBreaks < Cache.BrokenCount()) | |
761 | { | |
762 | if (OldVer == 0) | |
763 | Cache.MarkDelete(I); | |
764 | else | |
765 | Cache.MarkKeep(I); | |
766 | } | |
767 | else | |
768 | if (Debug == true) | |
0a8e3465 | 769 | clog << "Re-Instated " << I.Name() << " (" << OldBreaks << " vs " << Cache.BrokenCount() << ')' << endl; |
6c139d6e AL |
770 | } |
771 | ||
772 | if (Cache[I].InstallVer == 0 || Cache[I].InstBroken() == false) | |
773 | continue; | |
774 | ||
00b47c98 AL |
775 | if (Debug == true) |
776 | cout << "Investigating " << I.Name() << endl; | |
777 | ||
6c139d6e AL |
778 | // Isolate the problem dependency |
779 | PackageKill KillList[100]; | |
780 | PackageKill *LEnd = KillList; | |
421c8d10 AL |
781 | bool InOr = false; |
782 | pkgCache::DepIterator Start; | |
783 | pkgCache::DepIterator End; | |
b2e465d6 | 784 | PackageKill *OldEnd = LEnd; |
648e3cb4 AL |
785 | |
786 | enum {OrRemove,OrKeep} OrOp = OrRemove; | |
421c8d10 AL |
787 | for (pkgCache::DepIterator D = Cache[I].InstVerIter(Cache).DependsList(); |
788 | D.end() == false || InOr == true;) | |
6c139d6e AL |
789 | { |
790 | // Compute a single dependency element (glob or) | |
648e3cb4 AL |
791 | if (Start == End) |
792 | { | |
793 | // Decide what to do | |
794 | if (InOr == true) | |
795 | { | |
796 | if (OldEnd == LEnd && OrOp == OrRemove) | |
70777d4b AL |
797 | { |
798 | if ((Flags[I->ID] & Protected) != Protected) | |
00b47c98 AL |
799 | { |
800 | if (Debug == true) | |
801 | clog << " Or group remove for " << I.Name() << endl; | |
70777d4b | 802 | Cache.MarkDelete(I); |
cd14eaf2 | 803 | Change = true; |
00b47c98 | 804 | } |
70777d4b | 805 | } |
648e3cb4 | 806 | if (OldEnd == LEnd && OrOp == OrKeep) |
00b47c98 AL |
807 | { |
808 | if (Debug == true) | |
809 | clog << " Or group keep for " << I.Name() << endl; | |
648e3cb4 | 810 | Cache.MarkKeep(I); |
cd14eaf2 | 811 | Change = true; |
b2e465d6 | 812 | } |
648e3cb4 AL |
813 | } |
814 | ||
b2e465d6 AL |
815 | /* We do an extra loop (as above) to finalize the or group |
816 | processing */ | |
817 | InOr = false; | |
648e3cb4 | 818 | OrOp = OrRemove; |
421c8d10 | 819 | D.GlobOr(Start,End); |
b2e465d6 AL |
820 | if (Start.end() == true) |
821 | break; | |
cd14eaf2 | 822 | |
b2e465d6 AL |
823 | // We only worry about critical deps. |
824 | if (End.IsCritical() != true) | |
825 | continue; | |
cd14eaf2 | 826 | |
648e3cb4 AL |
827 | InOr = Start != End; |
828 | OldEnd = LEnd; | |
cd14eaf2 | 829 | } |
421c8d10 AL |
830 | else |
831 | Start++; | |
cd14eaf2 | 832 | |
6c139d6e AL |
833 | // Dep is ok |
834 | if ((Cache[End] & pkgDepCache::DepGInstall) == pkgDepCache::DepGInstall) | |
cd14eaf2 AL |
835 | { |
836 | InOr = false; | |
6c139d6e | 837 | continue; |
cd14eaf2 AL |
838 | } |
839 | ||
6c139d6e | 840 | if (Debug == true) |
421c8d10 | 841 | clog << "Package " << I.Name() << " has broken dep on " << Start.TargetPkg().Name() << endl; |
fcf85120 AL |
842 | |
843 | /* Look across the version list. If there are no possible | |
844 | targets then we keep the package and bail. This is necessary | |
845 | if a package has a dep on another package that cant be found */ | |
b2e465d6 | 846 | SPtrArray<pkgCache::Version *> VList = Start.AllTargets(); |
fcf85120 | 847 | if (*VList == 0 && (Flags[I->ID] & Protected) != Protected && |
648e3cb4 | 848 | Start->Type != pkgCache::Dep::Conflicts && |
b2e465d6 | 849 | Start->Type != pkgCache::Dep::Obsoletes && |
fcf85120 | 850 | Cache[I].NowBroken() == false) |
648e3cb4 AL |
851 | { |
852 | if (InOr == true) | |
853 | { | |
854 | /* No keep choice because the keep being OK could be the | |
855 | result of another element in the OR group! */ | |
856 | continue; | |
857 | } | |
858 | ||
fcf85120 | 859 | Change = true; |
648e3cb4 | 860 | Cache.MarkKeep(I); |
fcf85120 AL |
861 | break; |
862 | } | |
863 | ||
6c139d6e AL |
864 | bool Done = false; |
865 | for (pkgCache::Version **V = VList; *V != 0; V++) | |
866 | { | |
867 | pkgCache::VerIterator Ver(Cache,*V); | |
868 | pkgCache::PkgIterator Pkg = Ver.ParentPkg(); | |
a6bfe583 | 869 | |
6c139d6e | 870 | if (Debug == true) |
421c8d10 | 871 | clog << " Considering " << Pkg.Name() << ' ' << (int)Scores[Pkg->ID] << |
6c139d6e | 872 | " as a solution to " << I.Name() << ' ' << (int)Scores[I->ID] << endl; |
a6bfe583 AL |
873 | |
874 | /* Try to fix the package under consideration rather than | |
875 | fiddle with the VList package */ | |
6c139d6e | 876 | if (Scores[I->ID] <= Scores[Pkg->ID] || |
421c8d10 | 877 | ((Cache[Start] & pkgDepCache::DepNow) == 0 && |
b2e465d6 AL |
878 | End->Type != pkgCache::Dep::Conflicts && |
879 | End->Type != pkgCache::Dep::Obsoletes)) | |
6c139d6e | 880 | { |
200f8c52 | 881 | // Try a little harder to fix protected packages.. |
3b5421b4 | 882 | if ((Flags[I->ID] & Protected) == Protected) |
200f8c52 AL |
883 | { |
884 | if (DoUpgrade(Pkg) == true) | |
0296c633 | 885 | { |
b2e465d6 AL |
886 | if (Scores[Pkg->ID] > Scores[I->ID]) |
887 | Scores[Pkg->ID] = Scores[I->ID]; | |
0296c633 AL |
888 | break; |
889 | } | |
890 | ||
6c139d6e | 891 | continue; |
200f8c52 AL |
892 | } |
893 | ||
894 | /* See if a keep will do, unless the package is protected, | |
648e3cb4 AL |
895 | then installing it will be necessary */ |
896 | bool Installed = Cache[I].Install(); | |
6c139d6e AL |
897 | Cache.MarkKeep(I); |
898 | if (Cache[I].InstBroken() == false) | |
899 | { | |
648e3cb4 AL |
900 | // Unwind operation will be keep now |
901 | if (OrOp == OrRemove) | |
902 | OrOp = OrKeep; | |
903 | ||
904 | // Restore | |
905 | if (InOr == true && Installed == true) | |
906 | Cache.MarkInstall(I,false); | |
907 | ||
6c139d6e | 908 | if (Debug == true) |
421c8d10 | 909 | clog << " Holding Back " << I.Name() << " rather than change " << Start.TargetPkg().Name() << endl; |
6c139d6e AL |
910 | } |
911 | else | |
421c8d10 | 912 | { |
6c139d6e AL |
913 | if (BrokenFix == false || DoUpgrade(I) == false) |
914 | { | |
421c8d10 AL |
915 | // Consider other options |
916 | if (InOr == false) | |
917 | { | |
918 | if (Debug == true) | |
919 | clog << " Removing " << I.Name() << " rather than change " << Start.TargetPkg().Name() << endl; | |
920 | Cache.MarkDelete(I); | |
921 | if (Counter > 1) | |
b2e465d6 AL |
922 | { |
923 | if (Scores[Pkg->ID] > Scores[I->ID]) | |
924 | Scores[I->ID] = Scores[Pkg->ID]; | |
925 | } | |
421c8d10 | 926 | } |
0a8e3465 | 927 | } |
6c139d6e | 928 | } |
b5dc9785 | 929 | |
6c139d6e AL |
930 | Change = true; |
931 | Done = true; | |
932 | break; | |
933 | } | |
934 | else | |
935 | { | |
a6bfe583 AL |
936 | /* This is a conflicts, and the version we are looking |
937 | at is not the currently selected version of the | |
938 | package, which means it is not necessary to | |
939 | remove/keep */ | |
940 | if (Cache[Pkg].InstallVer != Ver && | |
941 | (Start->Type == pkgCache::Dep::Conflicts || | |
942 | Start->Type == pkgCache::Dep::Obsoletes)) | |
943 | continue; | |
944 | ||
648e3cb4 | 945 | // Skip adding to the kill list if it is protected |
6c139d6e AL |
946 | if ((Flags[Pkg->ID] & Protected) != 0) |
947 | continue; | |
a6bfe583 AL |
948 | |
949 | if (Debug == true) | |
950 | clog << " Added " << Pkg.Name() << " to the remove list" << endl; | |
6c139d6e AL |
951 | |
952 | LEnd->Pkg = Pkg; | |
953 | LEnd->Dep = End; | |
954 | LEnd++; | |
0a8e3465 | 955 | |
b2e465d6 AL |
956 | if (Start->Type != pkgCache::Dep::Conflicts && |
957 | Start->Type != pkgCache::Dep::Obsoletes) | |
6c139d6e AL |
958 | break; |
959 | } | |
960 | } | |
961 | ||
962 | // Hm, nothing can possibly satisify this dep. Nuke it. | |
b2e465d6 AL |
963 | if (VList[0] == 0 && |
964 | Start->Type != pkgCache::Dep::Conflicts && | |
965 | Start->Type != pkgCache::Dep::Obsoletes && | |
648e3cb4 | 966 | (Flags[I->ID] & Protected) != Protected) |
6c139d6e | 967 | { |
648e3cb4 | 968 | bool Installed = Cache[I].Install(); |
6c139d6e AL |
969 | Cache.MarkKeep(I); |
970 | if (Cache[I].InstBroken() == false) | |
971 | { | |
648e3cb4 AL |
972 | // Unwind operation will be keep now |
973 | if (OrOp == OrRemove) | |
974 | OrOp = OrKeep; | |
975 | ||
976 | // Restore | |
977 | if (InOr == true && Installed == true) | |
978 | Cache.MarkInstall(I,false); | |
979 | ||
6c139d6e | 980 | if (Debug == true) |
421c8d10 | 981 | clog << " Holding Back " << I.Name() << " because I can't find " << Start.TargetPkg().Name() << endl; |
6c139d6e AL |
982 | } |
983 | else | |
984 | { | |
985 | if (Debug == true) | |
421c8d10 | 986 | clog << " Removing " << I.Name() << " because I can't find " << Start.TargetPkg().Name() << endl; |
648e3cb4 AL |
987 | if (InOr == false) |
988 | Cache.MarkDelete(I); | |
6c139d6e AL |
989 | } |
990 | ||
991 | Change = true; | |
992 | Done = true; | |
993 | } | |
994 | ||
421c8d10 AL |
995 | // Try some more |
996 | if (InOr == true) | |
997 | continue; | |
998 | ||
6c139d6e AL |
999 | if (Done == true) |
1000 | break; | |
1001 | } | |
1002 | ||
1003 | // Apply the kill list now | |
1004 | if (Cache[I].InstallVer != 0) | |
648e3cb4 | 1005 | { |
6c139d6e | 1006 | for (PackageKill *J = KillList; J != LEnd; J++) |
6c139d6e | 1007 | { |
648e3cb4 AL |
1008 | Change = true; |
1009 | if ((Cache[J->Dep] & pkgDepCache::DepGNow) == 0) | |
1010 | { | |
b2e465d6 AL |
1011 | if (J->Dep->Type == pkgCache::Dep::Conflicts || |
1012 | J->Dep->Type == pkgCache::Dep::Obsoletes) | |
648e3cb4 AL |
1013 | { |
1014 | if (Debug == true) | |
1015 | clog << " Fixing " << I.Name() << " via remove of " << J->Pkg.Name() << endl; | |
1016 | Cache.MarkDelete(J->Pkg); | |
1017 | } | |
1018 | } | |
1019 | else | |
6c139d6e AL |
1020 | { |
1021 | if (Debug == true) | |
648e3cb4 AL |
1022 | clog << " Fixing " << I.Name() << " via keep of " << J->Pkg.Name() << endl; |
1023 | Cache.MarkKeep(J->Pkg); | |
6c139d6e | 1024 | } |
b2e465d6 | 1025 | |
648e3cb4 | 1026 | if (Counter > 1) |
b2e465d6 AL |
1027 | { |
1028 | if (Scores[I->ID] > Scores[J->Pkg->ID]) | |
1029 | Scores[J->Pkg->ID] = Scores[I->ID]; | |
1030 | } | |
648e3cb4 AL |
1031 | } |
1032 | } | |
1033 | } | |
6c139d6e AL |
1034 | } |
1035 | ||
1036 | if (Debug == true) | |
0a8e3465 | 1037 | clog << "Done" << endl; |
b2e465d6 | 1038 | |
6c139d6e | 1039 | if (Cache.BrokenCount() != 0) |
b5dc9785 AL |
1040 | { |
1041 | // See if this is the result of a hold | |
1042 | pkgCache::PkgIterator I = Cache.PkgBegin(); | |
1043 | for (;I.end() != true; I++) | |
1044 | { | |
1045 | if (Cache[I].InstBroken() == false) | |
1046 | continue; | |
1047 | if ((Flags[I->ID] & Protected) != Protected) | |
b2e465d6 | 1048 | return _error->Error(_("Error, pkgProblemResolver::Resolve generated breaks, this may be caused by held packages.")); |
b5dc9785 | 1049 | } |
b2e465d6 | 1050 | return _error->Error(_("Unable to correct problems, you have held broken packages.")); |
b5dc9785 AL |
1051 | } |
1052 | ||
0a8e3465 AL |
1053 | return true; |
1054 | } | |
1055 | /*}}}*/ | |
1056 | // ProblemResolver::ResolveByKeep - Resolve problems using keep /*{{{*/ | |
1057 | // --------------------------------------------------------------------- | |
1058 | /* This is the work horse of the soft upgrade routine. It is very gental | |
1059 | in that it does not install or remove any packages. It is assumed that the | |
1060 | system was non-broken previously. */ | |
1061 | bool pkgProblemResolver::ResolveByKeep() | |
1062 | { | |
b2e465d6 | 1063 | unsigned long Size = Cache.Head().PackageCount; |
0a8e3465 AL |
1064 | |
1065 | if (Debug == true) | |
1066 | clog << "Entering ResolveByKeep" << endl; | |
1067 | ||
1068 | MakeScores(); | |
1069 | ||
1070 | /* We have to order the packages so that the broken fixing pass | |
1071 | operates from highest score to lowest. This prevents problems when | |
1072 | high score packages cause the removal of lower score packages that | |
1073 | would cause the removal of even lower score packages. */ | |
1074 | pkgCache::Package **PList = new pkgCache::Package *[Size]; | |
1075 | pkgCache::Package **PEnd = PList; | |
1076 | for (pkgCache::PkgIterator I = Cache.PkgBegin(); I.end() == false; I++) | |
1077 | *PEnd++ = I; | |
1078 | This = this; | |
1079 | qsort(PList,PEnd - PList,sizeof(*PList),&ScoreSort); | |
1080 | ||
1081 | // Consider each broken package | |
1082 | pkgCache::Package **LastStop = 0; | |
1083 | for (pkgCache::Package **K = PList; K != PEnd; K++) | |
1084 | { | |
1085 | pkgCache::PkgIterator I(Cache,*K); | |
1086 | ||
1087 | if (Cache[I].InstallVer == 0 || Cache[I].InstBroken() == false) | |
1088 | continue; | |
1089 | ||
1090 | /* Keep the package. If this works then great, otherwise we have | |
b2e465d6 | 1091 | to be significantly more agressive and manipulate its dependencies */ |
0a8e3465 AL |
1092 | if ((Flags[I->ID] & Protected) == 0) |
1093 | { | |
1094 | if (Debug == true) | |
1095 | clog << "Keeping package " << I.Name() << endl; | |
1096 | Cache.MarkKeep(I); | |
1097 | if (Cache[I].InstBroken() == false) | |
1098 | { | |
b2e465d6 | 1099 | K = PList - 1; |
0a8e3465 AL |
1100 | continue; |
1101 | } | |
1102 | } | |
1103 | ||
1104 | // Isolate the problem dependencies | |
1105 | for (pkgCache::DepIterator D = Cache[I].InstVerIter(Cache).DependsList(); D.end() == false;) | |
1106 | { | |
c5532863 AL |
1107 | DepIterator Start; |
1108 | DepIterator End; | |
1109 | D.GlobOr(Start,End); | |
1110 | ||
0a8e3465 AL |
1111 | // We only worry about critical deps. |
1112 | if (End.IsCritical() != true) | |
1113 | continue; | |
1114 | ||
1115 | // Dep is ok | |
1116 | if ((Cache[End] & pkgDepCache::DepGInstall) == pkgDepCache::DepGInstall) | |
1117 | continue; | |
c5532863 AL |
1118 | |
1119 | /* Hm, the group is broken.. I suppose the best thing to do is to | |
1120 | is to try every combination of keep/not-keep for the set, but thats | |
1121 | slow, and this never happens, just be conservative and assume the | |
1122 | list of ors is in preference and keep till it starts to work. */ | |
1123 | while (true) | |
0a8e3465 | 1124 | { |
c5532863 AL |
1125 | if (Debug == true) |
1126 | clog << "Package " << I.Name() << " has broken dep on " << Start.TargetPkg().Name() << endl; | |
0a8e3465 | 1127 | |
c5532863 AL |
1128 | // Look at all the possible provides on this package |
1129 | SPtrArray<pkgCache::Version *> VList = Start.AllTargets(); | |
1130 | for (pkgCache::Version **V = VList; *V != 0; V++) | |
0a8e3465 | 1131 | { |
c5532863 AL |
1132 | pkgCache::VerIterator Ver(Cache,*V); |
1133 | pkgCache::PkgIterator Pkg = Ver.ParentPkg(); | |
1134 | ||
1135 | // It is not keepable | |
1136 | if (Cache[Pkg].InstallVer == 0 || | |
1137 | Pkg->CurrentVer == 0) | |
1138 | continue; | |
1139 | ||
1140 | if ((Flags[I->ID] & Protected) == 0) | |
1141 | { | |
1142 | if (Debug == true) | |
1143 | clog << " Keeping Package " << Pkg.Name() << " due to dep" << endl; | |
1144 | Cache.MarkKeep(Pkg); | |
1145 | } | |
1146 | ||
1147 | if (Cache[I].InstBroken() == false) | |
1148 | break; | |
0a8e3465 AL |
1149 | } |
1150 | ||
1151 | if (Cache[I].InstBroken() == false) | |
1152 | break; | |
0a8e3465 | 1153 | |
c5532863 AL |
1154 | if (Start == End) |
1155 | break; | |
1156 | Start++; | |
1157 | } | |
1158 | ||
0a8e3465 AL |
1159 | if (Cache[I].InstBroken() == false) |
1160 | break; | |
1161 | } | |
1162 | ||
1163 | if (Cache[I].InstBroken() == true) | |
1164 | continue; | |
1165 | ||
1166 | // Restart again. | |
1167 | if (K == LastStop) | |
1168 | return _error->Error("Internal Error, pkgProblemResolver::ResolveByKeep is looping on package %s.",I.Name()); | |
1169 | LastStop = K; | |
b2e465d6 | 1170 | K = PList - 1; |
0a8e3465 | 1171 | } |
6c139d6e AL |
1172 | |
1173 | return true; | |
1174 | } | |
1175 | /*}}}*/ | |
3b5421b4 AL |
1176 | // ProblemResolver::InstallProtect - Install all protected packages /*{{{*/ |
1177 | // --------------------------------------------------------------------- | |
1178 | /* This is used to make sure protected packages are installed */ | |
1179 | void pkgProblemResolver::InstallProtect() | |
1180 | { | |
1181 | for (pkgCache::PkgIterator I = Cache.PkgBegin(); I.end() == false; I++) | |
1182 | { | |
1183 | if ((Flags[I->ID] & Protected) == Protected) | |
1184 | { | |
1185 | if ((Flags[I->ID] & ToRemove) == ToRemove) | |
1186 | Cache.MarkDelete(I); | |
1187 | else | |
1188 | Cache.MarkInstall(I,false); | |
1189 | } | |
1190 | } | |
1191 | } | |
1192 | /*}}}*/ | |
b2e465d6 AL |
1193 | |
1194 | // PrioSortList - Sort a list of versions by priority /*{{{*/ | |
1195 | // --------------------------------------------------------------------- | |
1196 | /* This is ment to be used in conjunction with AllTargets to get a list | |
1197 | of versions ordered by preference. */ | |
1198 | static pkgCache *PrioCache; | |
1199 | static int PrioComp(const void *A,const void *B) | |
1200 | { | |
1201 | pkgCache::VerIterator L(*PrioCache,*(pkgCache::Version **)A); | |
1202 | pkgCache::VerIterator R(*PrioCache,*(pkgCache::Version **)B); | |
1203 | ||
1204 | if ((L.ParentPkg()->Flags & pkgCache::Flag::Essential) == pkgCache::Flag::Essential && | |
1205 | (L.ParentPkg()->Flags & pkgCache::Flag::Essential) != pkgCache::Flag::Essential) | |
1206 | return 1; | |
1207 | if ((L.ParentPkg()->Flags & pkgCache::Flag::Essential) != pkgCache::Flag::Essential && | |
1208 | (L.ParentPkg()->Flags & pkgCache::Flag::Essential) == pkgCache::Flag::Essential) | |
1209 | return -1; | |
1210 | ||
1211 | if (L->Priority != R->Priority) | |
1212 | return L->Priority - R->Priority; | |
1213 | return strcmp(L.ParentPkg().Name(),R.ParentPkg().Name()); | |
1214 | } | |
1215 | void pkgPrioSortList(pkgCache &Cache,pkgCache::Version **List) | |
1216 | { | |
1217 | unsigned long Count = 0; | |
1218 | PrioCache = &Cache; | |
1219 | for (pkgCache::Version **I = List; *I != 0; I++) | |
1220 | Count++; | |
1221 | qsort(List,Count,sizeof(*List),PrioComp); | |
1222 | } | |
1223 | /*}}}*/ |