]> git.saurik.com Git - apt.git/blame - apt-pkg/depcache.cc
remove duplicated test, move test for conflicts higher up, better debug output
[apt.git] / apt-pkg / depcache.cc
CommitLineData
6c139d6e
AL
1// -*- mode: cpp; mode: fold -*-
2// Description /*{{{*/
e7b470ee 3// $Id: depcache.cc,v 1.25 2001/05/27 05:36:04 jgg Exp $
6c139d6e
AL
4/* ######################################################################
5
6 Dependency Cache - Caches Dependency information.
7
8 ##################################################################### */
9 /*}}}*/
10// Include Files /*{{{*/
094a497d 11#include <apt-pkg/depcache.h>
094a497d
AL
12#include <apt-pkg/version.h>
13#include <apt-pkg/error.h>
b2e465d6
AL
14#include <apt-pkg/sptr.h>
15#include <apt-pkg/algorithms.h>
afb1e2e3
MV
16
17#include <apt-pkg/fileutl.h>
fa3b0945 18#include <apt-pkg/strutl.h>
afb1e2e3 19#include <apt-pkg/configuration.h>
8b32e920 20#include <apt-pkg/aptconfiguration.h>
74a05226 21#include <apt-pkg/pkgsystem.h>
afb1e2e3 22#include <apt-pkg/tagfile.h>
120365ce
MV
23
24#include <iostream>
a83d884d 25#include <sstream>
b1a8717a 26#include <set>
d4c5f11f 27
8a3a2e99
MV
28#include <sys/stat.h>
29
b2e465d6 30#include <apti18n.h>
92fcbfc1
DK
31 /*}}}*/
32// helper for Install-Recommends-Sections and Never-MarkAuto-Sections /*{{{*/
cb1933df
MV
33static bool
34ConfigValueInSubTree(const char* SubTree, const char *needle)
35{
36 Configuration::Item const *Opts;
37 Opts = _config->Tree(SubTree);
38 if (Opts != 0 && Opts->Child != 0)
39 {
40 Opts = Opts->Child;
41 for (; Opts != 0; Opts = Opts->Next)
42 {
43 if (Opts->Value.empty() == true)
44 continue;
45 if (strcmp(needle, Opts->Value.c_str()) == 0)
46 return true;
47 }
48 }
49 return false;
50}
92fcbfc1
DK
51 /*}}}*/
52pkgDepCache::ActionGroup::ActionGroup(pkgDepCache &cache) : /*{{{*/
74a05226
MV
53 cache(cache), released(false)
54{
55 ++cache.group_level;
56}
57
58void pkgDepCache::ActionGroup::release()
59{
60 if(!released)
61 {
62 if(cache.group_level == 0)
63 std::cerr << "W: Unbalanced action groups, expect badness" << std::endl;
64 else
65 {
66 --cache.group_level;
67
68 if(cache.group_level == 0)
69 cache.MarkAndSweep();
70 }
71
72 released = false;
73 }
74}
75
76pkgDepCache::ActionGroup::~ActionGroup()
77{
78 release();
79}
92fcbfc1 80 /*}}}*/
6c139d6e
AL
81// DepCache::pkgDepCache - Constructors /*{{{*/
82// ---------------------------------------------------------------------
83/* */
b2e465d6 84pkgDepCache::pkgDepCache(pkgCache *pCache,Policy *Plcy) :
74a05226 85 group_level(0), Cache(pCache), PkgState(0), DepState(0)
6c139d6e 86{
af29ffb4
MV
87 DebugMarker = _config->FindB("Debug::pkgDepCache::Marker", false);
88 DebugAutoInstall = _config->FindB("Debug::pkgDepCache::AutoInstall", false);
b2e465d6
AL
89 delLocalPolicy = 0;
90 LocalPolicy = Plcy;
91 if (LocalPolicy == 0)
92 delLocalPolicy = LocalPolicy = new Policy;
6c139d6e
AL
93}
94 /*}}}*/
95// DepCache::~pkgDepCache - Destructor /*{{{*/
96// ---------------------------------------------------------------------
97/* */
98pkgDepCache::~pkgDepCache()
99{
100 delete [] PkgState;
101 delete [] DepState;
b2e465d6 102 delete delLocalPolicy;
6c139d6e
AL
103}
104 /*}}}*/
6c139d6e
AL
105// DepCache::Init - Generate the initial extra structures. /*{{{*/
106// ---------------------------------------------------------------------
107/* This allocats the extension buffers and initializes them. */
a246f2dc 108bool pkgDepCache::Init(OpProgress *Prog)
6c139d6e 109{
74a05226
MV
110 // Suppress mark updates during this operation (just in case) and
111 // run a mark operation when Init terminates.
112 ActionGroup actions(*this);
113
6c139d6e
AL
114 delete [] PkgState;
115 delete [] DepState;
116 PkgState = new StateCache[Head().PackageCount];
117 DepState = new unsigned char[Head().DependsCount];
118 memset(PkgState,0,sizeof(*PkgState)*Head().PackageCount);
119 memset(DepState,0,sizeof(*DepState)*Head().DependsCount);
b2e465d6 120
a246f2dc
AL
121 if (Prog != 0)
122 {
123 Prog->OverallProgress(0,2*Head().PackageCount,Head().PackageCount,
db0db9fe
CP
124 _("Building dependency tree"));
125 Prog->SubProgress(Head().PackageCount,_("Candidate versions"));
a246f2dc
AL
126 }
127
6c139d6e
AL
128 /* Set the current state of everything. In this state all of the
129 packages are kept exactly as is. See AllUpgrade */
a246f2dc
AL
130 int Done = 0;
131 for (PkgIterator I = PkgBegin(); I.end() != true; I++,Done++)
6c139d6e 132 {
2edcefd5 133 if (Prog != 0 && Done%20 == 0)
a246f2dc
AL
134 Prog->Progress(Done);
135
6c139d6e
AL
136 // Find the proper cache slot
137 StateCache &State = PkgState[I->ID];
138 State.iFlags = 0;
afb1e2e3 139
6c139d6e
AL
140 // Figure out the install version
141 State.CandidateVer = GetCandidateVer(I);
142 State.InstallVer = I.CurrentVer();
143 State.Mode = ModeKeep;
144
145 State.Update(I,*this);
146 }
147
a246f2dc
AL
148 if (Prog != 0)
149 {
150
151 Prog->OverallProgress(Head().PackageCount,2*Head().PackageCount,
152 Head().PackageCount,
db0db9fe
CP
153 _("Building dependency tree"));
154 Prog->SubProgress(Head().PackageCount,_("Dependency generation"));
a246f2dc
AL
155 }
156
157 Update(Prog);
e004867d
MV
158
159 if(Prog != 0)
160 Prog->Done();
74a05226 161
6c139d6e
AL
162 return true;
163}
164 /*}}}*/
92fcbfc1 165bool pkgDepCache::readStateFile(OpProgress *Prog) /*{{{*/
a83d884d
MV
166{
167 FileFd state_file;
d34690e1 168 string const state = _config->FindFile("Dir::State::extended_states");
a83d884d
MV
169 if(FileExists(state)) {
170 state_file.Open(state, FileFd::ReadOnly);
e0b94b97 171 int const file_size = state_file.Size();
bc80031f
MV
172 if(Prog != NULL)
173 Prog->OverallProgress(0, file_size, 1,
174 _("Reading state information"));
a83d884d
MV
175
176 pkgTagFile tagfile(&state_file);
177 pkgTagSection section;
e0b94b97
DK
178 int amt = 0;
179 bool const debug_autoremove = _config->FindB("Debug::pkgAutoRemove",false);
a83d884d 180 while(tagfile.Step(section)) {
e0b94b97
DK
181 string const pkgname = section.FindS("Package");
182 string pkgarch = section.FindS("Architecture");
183 if (pkgarch.empty() == true)
184 pkgarch = "any";
185 pkgCache::PkgIterator pkg = Cache->FindPkg(pkgname, pkgarch);
186 // Silently ignore unknown packages and packages with no actual version.
187 if(pkg.end() == true || pkg->VersionList == 0)
188 continue;
189
190 short const reason = section.FindI("Auto-Installed", 0);
191 if(reason > 0)
192 {
193 PkgState[pkg->ID].Flags |= Flag::Auto;
194 if (unlikely(debug_autoremove))
6ee2b0f8 195 std::clog << "Auto-Installed : " << pkg.FullName() << std::endl;
e0b94b97
DK
196 if (pkgarch == "any")
197 {
198 pkgCache::GrpIterator G = pkg.Group();
199 for (pkg = G.NextPkg(pkg); pkg.end() != true; pkg = G.NextPkg(pkg))
200 if (pkg->VersionList != 0)
201 PkgState[pkg->ID].Flags |= Flag::Auto;
202 }
a83d884d 203 }
e0b94b97
DK
204 amt += section.size();
205 if(Prog != NULL)
206 Prog->OverallProgress(amt, file_size, 1,
207 _("Reading state information"));
a83d884d 208 }
eef21b9f
DK
209 if(Prog != NULL)
210 Prog->OverallProgress(file_size, file_size, 1,
211 _("Reading state information"));
a83d884d
MV
212 }
213
214 return true;
215}
92fcbfc1
DK
216 /*}}}*/
217bool pkgDepCache::writeStateFile(OpProgress *prog, bool InstalledOnly) /*{{{*/
a83d884d 218{
e0b94b97 219 bool const debug_autoremove = _config->FindB("Debug::pkgAutoRemove",false);
95afdfd0
OS
220
221 if(debug_autoremove)
e23e6733
MV
222 std::clog << "pkgDepCache::writeStateFile()" << std::endl;
223
b1a8717a 224 FileFd StateFile;
d34690e1 225 string const state = _config->FindFile("Dir::State::extended_states");
9a9665f9
MV
226
227 // if it does not exist, create a empty one
228 if(!FileExists(state))
229 {
22041bd2 230 StateFile.Open(state, FileFd::WriteAtomic);
9a9665f9
MV
231 StateFile.Close();
232 }
233
234 // open it
b1a8717a
MV
235 if(!StateFile.Open(state, FileFd::ReadOnly))
236 return _error->Error(_("Failed to open StateFile %s"),
a83d884d
MV
237 state.c_str());
238
b1a8717a 239 FILE *OutFile;
e0b94b97 240 string const outfile = state + ".tmp";
b1a8717a
MV
241 if((OutFile = fopen(outfile.c_str(),"w")) == NULL)
242 return _error->Error(_("Failed to write temporary StateFile %s"),
243 outfile.c_str());
80fa0d8a 244
b1a8717a
MV
245 // first merge with the existing sections
246 pkgTagFile tagfile(&StateFile);
247 pkgTagSection section;
248 std::set<string> pkgs_seen;
249 const char *nullreorderlist[] = {0};
250 while(tagfile.Step(section)) {
c176c4d0 251 string const pkgname = section.FindS("Package");
e0b94b97
DK
252 string pkgarch = section.FindS("Architecture");
253 if (pkgarch.empty() == true)
254 pkgarch = "native";
b1a8717a
MV
255 // Silently ignore unknown packages and packages with no actual
256 // version.
e0b94b97 257 pkgCache::PkgIterator pkg = Cache->FindPkg(pkgname, pkgarch);
b1a8717a
MV
258 if(pkg.end() || pkg.VersionList().end())
259 continue;
c176c4d0
DK
260 StateCache const &P = PkgState[pkg->ID];
261 bool newAuto = (P.Flags & Flag::Auto);
262 // skip not installed or now-removed ones if requested
263 if (InstalledOnly && (
264 (pkg->CurrentVer == 0 && P.Mode != ModeInstall) ||
265 (pkg->CurrentVer != 0 && P.Mode == ModeDelete)))
266 {
267 // The section is obsolete if it contains no other tag
268 unsigned int const count = section.Count();
269 if (count < 2 ||
c67dc114
DK
270 (count == 2 && section.Exists("Auto-Installed")) ||
271 (count == 3 && section.Exists("Auto-Installed") && section.Exists("Architecture")))
c176c4d0
DK
272 continue;
273 else
274 newAuto = false;
275 }
b1a8717a 276 if(_config->FindB("Debug::pkgAutoRemove",false))
a1e42d1f 277 std::clog << "Update existing AutoInstall info: "
e0b94b97
DK
278 << pkg.FullName() << std::endl;
279 TFRewriteData rewrite[3];
280 rewrite[0].Tag = "Architecture";
281 rewrite[0].Rewrite = pkg.Arch();
b1a8717a 282 rewrite[0].NewTag = 0;
e0b94b97
DK
283 rewrite[1].Tag = "Auto-Installed";
284 rewrite[1].Rewrite = newAuto ? "1" : "0";
285 rewrite[1].NewTag = 0;
286 rewrite[2].Tag = 0;
b1a8717a
MV
287 TFRewrite(OutFile, section, nullreorderlist, rewrite);
288 fprintf(OutFile,"\n");
e0b94b97 289 pkgs_seen.insert(pkg.FullName());
b1a8717a
MV
290 }
291
292 // then write the ones we have not seen yet
293 std::ostringstream ostr;
294 for(pkgCache::PkgIterator pkg=Cache->PkgBegin(); !pkg.end(); pkg++) {
c176c4d0
DK
295 StateCache const &P = PkgState[pkg->ID];
296 if(P.Flags & Flag::Auto) {
e0b94b97 297 if (pkgs_seen.find(pkg.FullName()) != pkgs_seen.end()) {
95afdfd0 298 if(debug_autoremove)
e0b94b97 299 std::clog << "Skipping already written " << pkg.FullName() << std::endl;
b1a8717a
MV
300 continue;
301 }
c176c4d0
DK
302 // skip not installed ones if requested
303 if (InstalledOnly && (
304 (pkg->CurrentVer == 0 && P.Mode != ModeInstall) ||
305 (pkg->CurrentVer != 0 && P.Mode == ModeDelete)))
306 continue;
e0b94b97
DK
307 const char* const pkgarch = pkg.Arch();
308 if (strcmp(pkgarch, "all") == 0)
309 continue;
95afdfd0 310 if(debug_autoremove)
e0b94b97 311 std::clog << "Writing new AutoInstall: " << pkg.FullName() << std::endl;
a83d884d 312 ostr.str(string(""));
e0b94b97
DK
313 ostr << "Package: " << pkg.Name()
314 << "\nArchitecture: " << pkgarch
fc5aece9 315 << "\nAuto-Installed: 1\n\n";
3c8cda8b 316 fprintf(OutFile,"%s",ostr.str().c_str());
a83d884d
MV
317 }
318 }
a4decc40 319 fclose(OutFile);
b1a8717a 320
8a3a2e99 321 // move the outfile over the real file and set permissions
b1a8717a 322 rename(outfile.c_str(), state.c_str());
8a3a2e99 323 chmod(state.c_str(), 0644);
b1a8717a 324
a83d884d
MV
325 return true;
326}
92fcbfc1 327 /*}}}*/
6c139d6e
AL
328// DepCache::CheckDep - Checks a single dependency /*{{{*/
329// ---------------------------------------------------------------------
330/* This first checks the dependency against the main target package and
331 then walks along the package provides list and checks if each provides
332 will be installed then checks the provides against the dep. Res will be
333 set to the package which was used to satisfy the dep. */
334bool pkgDepCache::CheckDep(DepIterator Dep,int Type,PkgIterator &Res)
335{
336 Res = Dep.TargetPkg();
337
338 /* Check simple depends. A depends -should- never self match but
339 we allow it anyhow because dpkg does. Technically it is a packaging
340 bug. Conflicts may never self match */
d3814b27 341 if (Dep.TargetPkg()->Group != Dep.ParentPkg()->Group ||
308c7d30 342 (Dep->Type != Dep::Conflicts && Dep->Type != Dep::DpkgBreaks && Dep->Type != Dep::Obsoletes))
6c139d6e
AL
343 {
344 PkgIterator Pkg = Dep.TargetPkg();
345 // Check the base package
346 if (Type == NowVersion && Pkg->CurrentVer != 0)
b2e465d6
AL
347 if (VS().CheckDep(Pkg.CurrentVer().VerStr(),Dep->CompareOp,
348 Dep.TargetVer()) == true)
6c139d6e
AL
349 return true;
350
351 if (Type == InstallVersion && PkgState[Pkg->ID].InstallVer != 0)
b2e465d6
AL
352 if (VS().CheckDep(PkgState[Pkg->ID].InstVerIter(*this).VerStr(),
353 Dep->CompareOp,Dep.TargetVer()) == true)
6c139d6e
AL
354 return true;
355
356 if (Type == CandidateVersion && PkgState[Pkg->ID].CandidateVer != 0)
b2e465d6
AL
357 if (VS().CheckDep(PkgState[Pkg->ID].CandidateVerIter(*this).VerStr(),
358 Dep->CompareOp,Dep.TargetVer()) == true)
6c139d6e
AL
359 return true;
360 }
361
b2e465d6
AL
362 if (Dep->Type == Dep::Obsoletes)
363 return false;
364
6c139d6e
AL
365 // Check the providing packages
366 PrvIterator P = Dep.TargetPkg().ProvidesList();
367 PkgIterator Pkg = Dep.ParentPkg();
368 for (; P.end() != true; P++)
369 {
d3814b27
DK
370 /* Provides may never be applied against the same package (or group)
371 if it is a conflicts. See the comment above. */
372 if (P.OwnerPkg()->Group == Pkg->Group &&
308c7d30 373 (Dep->Type == Dep::Conflicts || Dep->Type == Dep::DpkgBreaks))
6c139d6e
AL
374 continue;
375
376 // Check if the provides is a hit
377 if (Type == NowVersion)
378 {
379 if (P.OwnerPkg().CurrentVer() != P.OwnerVer())
380 continue;
381 }
382
383 if (Type == InstallVersion)
384 {
385 StateCache &State = PkgState[P.OwnerPkg()->ID];
386 if (State.InstallVer != (Version *)P.OwnerVer())
387 continue;
388 }
389
390 if (Type == CandidateVersion)
391 {
392 StateCache &State = PkgState[P.OwnerPkg()->ID];
393 if (State.CandidateVer != (Version *)P.OwnerVer())
394 continue;
395 }
396
397 // Compare the versions.
b2e465d6 398 if (VS().CheckDep(P.ProvideVersion(),Dep->CompareOp,Dep.TargetVer()) == true)
6c139d6e
AL
399 {
400 Res = P.OwnerPkg();
401 return true;
402 }
403 }
404
405 return false;
406}
407 /*}}}*/
408// DepCache::AddSizes - Add the packages sizes to the counters /*{{{*/
409// ---------------------------------------------------------------------
81305a0b
DK
410/* Call with Mult = -1 to preform the inverse opration
411 The Mult increases the complexity of the calulations here and is unused -
412 or do we really have a usecase for removing the size of a package two
413 times? So let us replace it with a simple bool and be done with it… */
414__deprecated void pkgDepCache::AddSizes(const PkgIterator &Pkg,signed long Mult)
6c139d6e
AL
415{
416 StateCache &P = PkgState[Pkg->ID];
417
e7b470ee
AL
418 if (Pkg->VersionList == 0)
419 return;
420
e5a1f2ff
AL
421 if (Pkg.State() == pkgCache::PkgIterator::NeedsConfigure &&
422 P.Keep() == true)
2cca3bd9 423 return;
2cca3bd9 424
6c139d6e
AL
425 // Compute the size data
426 if (P.NewInstall() == true)
427 {
e2c66de5
DK
428 iUsrSize += (signed long long)(Mult*P.InstVerIter(*this)->InstalledSize);
429 iDownloadSize += (signed long long)(Mult*P.InstVerIter(*this)->Size);
d38b7b3d 430 return;
6c139d6e
AL
431 }
432
433 // Upgrading
d0c59649
AL
434 if (Pkg->CurrentVer != 0 &&
435 (P.InstallVer != (Version *)Pkg.CurrentVer() ||
436 (P.iFlags & ReInstall) == ReInstall) && P.InstallVer != 0)
6c139d6e 437 {
e2c66de5
DK
438 iUsrSize += (signed long long)(Mult*((signed long long)P.InstVerIter(*this)->InstalledSize -
439 (signed long long)Pkg.CurrentVer()->InstalledSize));
440 iDownloadSize += (signed long long)(Mult*P.InstVerIter(*this)->Size);
d38b7b3d
AL
441 return;
442 }
443
444 // Reinstall
2cca3bd9
AL
445 if (Pkg.State() == pkgCache::PkgIterator::NeedsUnpack &&
446 P.Delete() == false)
d38b7b3d 447 {
e2c66de5 448 iDownloadSize += (signed long long)(Mult*P.InstVerIter(*this)->Size);
d38b7b3d 449 return;
6c139d6e
AL
450 }
451
452 // Removing
453 if (Pkg->CurrentVer != 0 && P.InstallVer == 0)
d38b7b3d 454 {
e2c66de5 455 iUsrSize -= (signed long long)(Mult*Pkg.CurrentVer()->InstalledSize);
d38b7b3d
AL
456 return;
457 }
6c139d6e
AL
458}
459 /*}}}*/
81305a0b
DK
460// DepCache::AddSizes - Add the packages sizes to the counters /*{{{*/
461// ---------------------------------------------------------------------
462/* Call with Inverse = true to preform the inverse opration */
463void pkgDepCache::AddSizes(const PkgIterator &Pkg, bool const &Inverse)
464{
465 StateCache &P = PkgState[Pkg->ID];
466
467 if (Pkg->VersionList == 0)
468 return;
469
470 if (Pkg.State() == pkgCache::PkgIterator::NeedsConfigure &&
471 P.Keep() == true)
472 return;
473
474 // Compute the size data
475 if (P.NewInstall() == true)
476 {
477 if (Inverse == false) {
478 iUsrSize += P.InstVerIter(*this)->InstalledSize;
479 iDownloadSize += P.InstVerIter(*this)->Size;
480 } else {
481 iUsrSize -= P.InstVerIter(*this)->InstalledSize;
482 iDownloadSize -= P.InstVerIter(*this)->Size;
483 }
484 return;
485 }
486
487 // Upgrading
488 if (Pkg->CurrentVer != 0 &&
489 (P.InstallVer != (Version *)Pkg.CurrentVer() ||
490 (P.iFlags & ReInstall) == ReInstall) && P.InstallVer != 0)
491 {
492 if (Inverse == false) {
493 iUsrSize -= Pkg.CurrentVer()->InstalledSize;
494 iUsrSize += P.InstVerIter(*this)->InstalledSize;
495 iDownloadSize += P.InstVerIter(*this)->Size;
496 } else {
497 iUsrSize -= P.InstVerIter(*this)->InstalledSize;
498 iUsrSize += Pkg.CurrentVer()->InstalledSize;
499 iDownloadSize -= P.InstVerIter(*this)->Size;
500 }
501 return;
502 }
503
504 // Reinstall
505 if (Pkg.State() == pkgCache::PkgIterator::NeedsUnpack &&
506 P.Delete() == false)
507 {
508 if (Inverse == false)
509 iDownloadSize += P.InstVerIter(*this)->Size;
510 else
511 iDownloadSize -= P.InstVerIter(*this)->Size;
512 return;
513 }
514
515 // Removing
516 if (Pkg->CurrentVer != 0 && P.InstallVer == 0)
517 {
518 if (Inverse == false)
519 iUsrSize -= Pkg.CurrentVer()->InstalledSize;
520 else
521 iUsrSize += Pkg.CurrentVer()->InstalledSize;
d38b7b3d
AL
522 return;
523 }
6c139d6e
AL
524}
525 /*}}}*/
526// DepCache::AddStates - Add the package to the state counter /*{{{*/
527// ---------------------------------------------------------------------
528/* This routine is tricky to use, you must make sure that it is never
529 called twice for the same package. This means the Remove/Add section
530 should be as short as possible and not encompass any code that will
531 calld Remove/Add itself. Remember, dependencies can be circular so
532 while processing a dep for Pkg it is possible that Add/Remove
533 will be called on Pkg */
534void pkgDepCache::AddStates(const PkgIterator &Pkg,int Add)
535{
536 StateCache &State = PkgState[Pkg->ID];
537
4ef9a929 538 // The Package is broken (either minimal dep or policy dep)
6c139d6e
AL
539 if ((State.DepState & DepInstMin) != DepInstMin)
540 iBrokenCount += Add;
4ef9a929
MV
541 if ((State.DepState & DepInstPolicy) != DepInstPolicy)
542 iPolicyBrokenCount += Add;
6c139d6e
AL
543
544 // Bad state
545 if (Pkg.State() != PkgIterator::NeedsNothing)
546 iBadCount += Add;
547
548 // Not installed
549 if (Pkg->CurrentVer == 0)
550 {
d556d1a1
AL
551 if (State.Mode == ModeDelete &&
552 (State.iFlags | Purge) == Purge && Pkg.Purge() == false)
553 iDelCount += Add;
554
6c139d6e
AL
555 if (State.Mode == ModeInstall)
556 iInstCount += Add;
557 return;
558 }
559
560 // Installed, no upgrade
6321777b 561 if (State.Status == 0)
d0c59649 562 {
6c139d6e
AL
563 if (State.Mode == ModeDelete)
564 iDelCount += Add;
d0c59649
AL
565 else
566 if ((State.iFlags & ReInstall) == ReInstall)
567 iInstCount += Add;
568
6c139d6e
AL
569 return;
570 }
571
572 // Alll 3 are possible
573 if (State.Mode == ModeDelete)
574 iDelCount += Add;
575 if (State.Mode == ModeKeep)
576 iKeepCount += Add;
577 if (State.Mode == ModeInstall)
578 iInstCount += Add;
579}
580 /*}}}*/
581// DepCache::BuildGroupOrs - Generate the Or group dep data /*{{{*/
582// ---------------------------------------------------------------------
583/* The or group results are stored in the last item of the or group. This
584 allows easy detection of the state of a whole or'd group. */
585void pkgDepCache::BuildGroupOrs(VerIterator const &V)
586{
587 unsigned char Group = 0;
588
589 for (DepIterator D = V.DependsList(); D.end() != true; D++)
590 {
591 // Build the dependency state.
592 unsigned char &State = DepState[D->ID];
593
594 /* Invert for Conflicts. We have to do this twice to get the
595 right sense for a conflicts group */
308c7d30
IJ
596 if (D->Type == Dep::Conflicts ||
597 D->Type == Dep::DpkgBreaks ||
598 D->Type == Dep::Obsoletes)
6c139d6e
AL
599 State = ~State;
600
601 // Add to the group if we are within an or..
d2685fd6 602 State &= 0x7;
6c139d6e
AL
603 Group |= State;
604 State |= Group << 3;
605 if ((D->CompareOp & Dep::Or) != Dep::Or)
606 Group = 0;
607
608 // Invert for Conflicts
308c7d30
IJ
609 if (D->Type == Dep::Conflicts ||
610 D->Type == Dep::DpkgBreaks ||
611 D->Type == Dep::Obsoletes)
6c139d6e
AL
612 State = ~State;
613 }
614}
615 /*}}}*/
616// DepCache::VersionState - Perform a pass over a dependency list /*{{{*/
617// ---------------------------------------------------------------------
618/* This is used to run over a dependency list and determine the dep
619 state of the list, filtering it through both a Min check and a Policy
620 check. The return result will have SetMin/SetPolicy low if a check
621 fails. It uses the DepState cache for it's computations. */
622unsigned char pkgDepCache::VersionState(DepIterator D,unsigned char Check,
623 unsigned char SetMin,
624 unsigned char SetPolicy)
625{
626 unsigned char Dep = 0xFF;
627
628 while (D.end() != true)
629 {
630 // Compute a single dependency element (glob or)
631 DepIterator Start = D;
632 unsigned char State = 0;
633 for (bool LastOR = true; D.end() == false && LastOR == true; D++)
634 {
635 State |= DepState[D->ID];
636 LastOR = (D->CompareOp & Dep::Or) == Dep::Or;
637 }
638
639 // Minimum deps that must be satisfied to have a working package
640 if (Start.IsCritical() == true)
641 if ((State & Check) != Check)
642 Dep &= ~SetMin;
643
644 // Policy deps that must be satisfied to install the package
645 if (IsImportantDep(Start) == true &&
646 (State & Check) != Check)
647 Dep &= ~SetPolicy;
648 }
649
650 return Dep;
651}
652 /*}}}*/
653// DepCache::DependencyState - Compute the 3 results for a dep /*{{{*/
654// ---------------------------------------------------------------------
655/* This is the main dependency computation bit. It computes the 3 main
656 results for a dependencys, Now, Install and Candidate. Callers must
657 invert the result if dealing with conflicts. */
658unsigned char pkgDepCache::DependencyState(DepIterator &D)
659{
660 unsigned char State = 0;
661
662 if (CheckDep(D,NowVersion) == true)
663 State |= DepNow;
664 if (CheckDep(D,InstallVersion) == true)
665 State |= DepInstall;
666 if (CheckDep(D,CandidateVersion) == true)
667 State |= DepCVer;
668
669 return State;
670}
671 /*}}}*/
672// DepCache::UpdateVerState - Compute the Dep member of the state /*{{{*/
673// ---------------------------------------------------------------------
674/* This determines the combined dependency representation of a package
675 for its two states now and install. This is done by using the pre-generated
676 dependency information. */
677void pkgDepCache::UpdateVerState(PkgIterator Pkg)
678{
679 // Empty deps are always true
680 StateCache &State = PkgState[Pkg->ID];
681 State.DepState = 0xFF;
682
683 // Check the Current state
684 if (Pkg->CurrentVer != 0)
685 {
686 DepIterator D = Pkg.CurrentVer().DependsList();
687 State.DepState &= VersionState(D,DepNow,DepNowMin,DepNowPolicy);
688 }
689
690 /* Check the candidate state. We do not compare against the whole as
691 a candidate state but check the candidate version against the
692 install states */
693 if (State.CandidateVer != 0)
694 {
695 DepIterator D = State.CandidateVerIter(*this).DependsList();
696 State.DepState &= VersionState(D,DepInstall,DepCandMin,DepCandPolicy);
697 }
698
699 // Check target state which can only be current or installed
700 if (State.InstallVer != 0)
701 {
702 DepIterator D = State.InstVerIter(*this).DependsList();
703 State.DepState &= VersionState(D,DepInstall,DepInstMin,DepInstPolicy);
704 }
705}
706 /*}}}*/
8b32e920
DK
707// DepCache::RemovePseudoInstalledPkg - MultiArch helper for Update() /*{{{*/
708// ---------------------------------------------------------------------
709/* We "install" arch all packages for all archs if it is installed. Many
710 of these will be broken. This method will look at these broken Pkg and
711 "remove" it. */
712bool pkgDepCache::RemovePseudoInstalledPkg(PkgIterator &Pkg, std::set<unsigned long> &recheck) {
713 if (unlikely(Pkg->CurrentVer == 0))
714 return false;
715
716 VerIterator V = Pkg.CurrentVer();
717 if (V->MultiArch != Version::All)
718 return false;
719
1ec1653c
DK
720 // Never ever kill an "all" package - they have no dependency so they can't be broken
721 if (strcmp(Pkg.Arch(),"all") == 0)
8b32e920
DK
722 return false;
723
1f530ccb
DK
724 unsigned char const CurDepState = VersionState(V.DependsList(),DepInstall,DepInstMin,DepInstPolicy);
725 if ((CurDepState & DepInstMin) == DepInstMin) {
1ec1653c
DK
726 // okay, the package isn't broken, but is the package also required?
727 // If it has no real dependencies, no installed rdepends and doesn't
728 // provide something of value, we will kill it as not required.
729 // These pseudopackages have otherwise interesting effects if they get
730 // a new dependency in a newer version…
731 for (pkgCache::DepIterator D = V.DependsList();
732 D.end() != true; ++D)
1f530ccb 733 if (D.IsCritical() == true && D.ParentPkg()->Group != Pkg->Group)
1ec1653c
DK
734 return false;
735 for (DepIterator D = Pkg.RevDependsList(); D.end() != true; ++D)
736 {
1f530ccb 737 if (D.IsCritical() == false)
1ec1653c
DK
738 continue;
739 PkgIterator const P = D.ParentPkg();
1f530ccb
DK
740 if (P->Group == Pkg->Group)
741 continue;
1ec1653c
DK
742 if (P->CurrentVer != 0)
743 return false;
744 }
745 for (PrvIterator Prv = V.ProvidesList(); Prv.end() != true; Prv++)
746 for (DepIterator d = Prv.ParentPkg().RevDependsList();
747 d.end() != true; ++d)
748 {
749 PkgIterator const P = d.ParentPkg();
750 if (P->CurrentVer != 0 &&
751 P->Group != Pkg->Group)
752 return false;
753 }
754 }
755
756 // Dependencies for this arch all package are not statisfied
8b32e920
DK
757 // so we installed it only for our convenience: get right of it now.
758 RemoveSizes(Pkg);
759 RemoveStates(Pkg);
760
761 Pkg->CurrentVer = 0;
762 PkgState[Pkg->ID].InstallVer = 0;
763
764 AddStates(Pkg);
765 Update(Pkg);
766 AddSizes(Pkg);
767
768 // After the remove previously satisfied pseudo pkg could be now
769 // no longer satisfied, so we need to recheck the reverse dependencies
770 for (DepIterator d = Pkg.RevDependsList(); d.end() != true; ++d)
771 {
772 PkgIterator const P = d.ParentPkg();
773 if (P->CurrentVer != 0)
774 recheck.insert(P.Index());
775 }
776
1ec1653c
DK
777 for (DepIterator d = V.DependsList(); d.end() != true; ++d)
778 {
779 PkgIterator const P = d.TargetPkg();
780 for (PrvIterator Prv = P.ProvidesList(); Prv.end() != true; ++Prv)
781 {
782 PkgIterator const O = Prv.OwnerPkg();
783 if (O->CurrentVer != 0)
784 recheck.insert(O.Index());
785 }
786
787 if (P->CurrentVer != 0)
788 recheck.insert(P.Index());
789 }
790
791 for (PrvIterator Prv = V.ProvidesList(); Prv.end() != true; Prv++)
792 {
793 for (DepIterator d = Prv.ParentPkg().RevDependsList();
794 d.end() != true; ++d)
795 {
796 PkgIterator const P = d.ParentPkg();
797 if (P->CurrentVer == 0)
798 continue;
799
800 recheck.insert(P.Index());
801 }
802 }
803
8b32e920
DK
804
805 return true;
806}
807 /*}}}*/
6c139d6e
AL
808// DepCache::Update - Figure out all the state information /*{{{*/
809// ---------------------------------------------------------------------
810/* This will figure out the state of all the packages and all the
811 dependencies based on the current policy. */
a246f2dc 812void pkgDepCache::Update(OpProgress *Prog)
6c139d6e
AL
813{
814 iUsrSize = 0;
815 iDownloadSize = 0;
816 iDelCount = 0;
817 iInstCount = 0;
818 iKeepCount = 0;
819 iBrokenCount = 0;
820 iBadCount = 0;
8b32e920
DK
821
822 std::set<unsigned long> recheck;
823
6c139d6e 824 // Perform the depends pass
a246f2dc 825 int Done = 0;
8b32e920
DK
826 bool const checkMultiArch = APT::Configuration::getArchitectures().size() > 1;
827 unsigned long killed = 0;
a246f2dc 828 for (PkgIterator I = PkgBegin(); I.end() != true; I++,Done++)
6c139d6e 829 {
2edcefd5 830 if (Prog != 0 && Done%20 == 0)
a246f2dc 831 Prog->Progress(Done);
6c139d6e
AL
832 for (VerIterator V = I.VersionList(); V.end() != true; V++)
833 {
834 unsigned char Group = 0;
8b32e920 835
6c139d6e
AL
836 for (DepIterator D = V.DependsList(); D.end() != true; D++)
837 {
838 // Build the dependency state.
839 unsigned char &State = DepState[D->ID];
b2e465d6 840 State = DependencyState(D);
6c139d6e
AL
841
842 // Add to the group if we are within an or..
843 Group |= State;
844 State |= Group << 3;
845 if ((D->CompareOp & Dep::Or) != Dep::Or)
846 Group = 0;
847
848 // Invert for Conflicts
308c7d30
IJ
849 if (D->Type == Dep::Conflicts ||
850 D->Type == Dep::DpkgBreaks ||
851 D->Type == Dep::Obsoletes)
6c139d6e 852 State = ~State;
8b32e920 853 }
6c139d6e
AL
854 }
855
8b32e920 856 // Compute the package dependency state and size additions
6c139d6e
AL
857 AddSizes(I);
858 UpdateVerState(I);
859 AddStates(I);
8b32e920
DK
860
861 if (checkMultiArch != true || I->CurrentVer == 0)
862 continue;
863
864 VerIterator const V = I.CurrentVer();
865 if (V->MultiArch != Version::All)
866 continue;
867
868 recheck.insert(I.Index());
869 --Done; // no progress if we need to recheck the package
6c139d6e 870 }
a246f2dc 871
8b32e920
DK
872 if (checkMultiArch == true) {
873 /* FIXME: recheck breaks proper progress reporting as we don't know
874 how many packages we need to recheck. To lower the effect
875 a bit we increase with a kill, but we should do something more clever… */
6248b28e
DK
876 while(recheck.empty() == false)
877 for (std::set<unsigned long>::const_iterator p = recheck.begin();
461e4a5e 878 p != recheck.end();) {
6248b28e
DK
879 if (Prog != 0 && Done%20 == 0)
880 Prog->Progress(Done);
881 PkgIterator P = PkgIterator(*Cache, Cache->PkgP + *p);
882 if (RemovePseudoInstalledPkg(P, recheck) == true) {
883 ++killed;
884 ++Done;
885 }
461e4a5e 886 recheck.erase(p++);
8b32e920 887 }
1f530ccb
DK
888
889 /* Okay, we have killed a great amount of pseudopackages -
890 we have killed so many that we have now arch "all" packages
891 without an installed pseudo package, but we NEED an installed
892 pseudo package, so we will search now for a pseudo package
893 we can install without breaking everything. */
894 for (GrpIterator G = Cache->GrpBegin(); G.end() != true; ++G)
895 {
896 PkgIterator P = G.FindPkg("all");
897 if (P.end() == true)
898 continue;
899 if (P->CurrentVer == 0)
900 continue;
901 bool installed = false;
902 for (P = G.FindPkg("any"); P.end() != true; P = G.NextPkg(P))
903 {
904 if (strcmp(P.Arch(), "all") == 0)
905 continue;
906 if (P->CurrentVer == 0)
907 continue;
908 installed = true;
909 break;
910 }
911 if (installed == false)
912 recheck.insert(G.Index());
913 }
1f530ccb 914
edde664d
DK
915 while (recheck.empty() != true)
916 {
917 std::set<unsigned long>::const_iterator g = recheck.begin();
918 unsigned long const G = *g;
919 recheck.erase(g);
920 if (unlikely(ReInstallPseudoForGroup(G, recheck) == false))
461e4a5e 921 _error->Warning(_("Internal error, group '%s' has no installable pseudo package"), GrpIterator(*Cache, Cache->GrpP + G).Name());
edde664d 922 }
6c139d6e 923 }
a246f2dc 924
8b32e920 925 if (Prog != 0)
a246f2dc 926 Prog->Progress(Done);
74a05226
MV
927
928 readStateFile(Prog);
6c139d6e
AL
929}
930 /*}}}*/
edde664d
DK
931// DepCache::ReInstallPseudoForGroup - MultiArch helper for Update() /*{{{*/
932// ---------------------------------------------------------------------
933/* RemovePseudoInstalledPkg() is very successful. It even kills packages
934 to an amount that no pseudo package is left, but we need a pseudo package
935 for upgrading senarios so we need to reinstall one pseudopackage which
936 doesn't break everything. Thankfully we can't have architecture depending
937 negative dependencies so this problem is already eliminated */
938bool pkgDepCache::ReInstallPseudoForGroup(pkgCache::PkgIterator const &P, std::set<unsigned long> &recheck)
939{
940 if (P->CurrentVer != 0)
941 return true;
942 // recursive call for packages which provide this package
943 for (pkgCache::PrvIterator Prv = P.ProvidesList(); Prv.end() != true; ++Prv)
944 ReInstallPseudoForGroup(Prv.OwnerPkg(), recheck);
945 // check if we actually need to look at this group
946 unsigned long const G = P->Group;
947 std::set<unsigned long>::const_iterator Pi = recheck.find(G);
948 if (Pi == recheck.end())
949 return true;
950 recheck.erase(Pi); // remove here, so we can't fall into an endless loop
951 if (unlikely(ReInstallPseudoForGroup(G, recheck) == false))
952 {
953 recheck.insert(G);
954 return false;
955 }
956 return true;
957}
958bool pkgDepCache::ReInstallPseudoForGroup(unsigned long const &G, std::set<unsigned long> &recheck)
959{
960 std::vector<std::string> static const Archs = APT::Configuration::getArchitectures();
961 pkgCache::GrpIterator Grp(*Cache, Cache->GrpP + G);
962 if (unlikely(Grp.end() == true))
963 return false;
964 for (std::vector<std::string>::const_iterator a = Archs.begin();
965 a != Archs.end(); ++a)
966 {
967 pkgCache::PkgIterator P = Grp.FindPkg(*a);
968 if (P.end() == true)
969 continue;
970 pkgCache::VerIterator allV = Grp.FindPkg("all").CurrentVer();
971 for (VerIterator V = P.VersionList(); V.end() != true; ++V)
972 {
973 // search for the same version as the all package
974 if (allV->Hash != V->Hash || strcmp(allV.VerStr(),V.VerStr()) != 0)
975 continue;
976 unsigned char const CurDepState = VersionState(V.DependsList(),DepInstall,DepInstMin,DepInstPolicy);
977 // If it is broken, try to install dependencies first before retry
978 if ((CurDepState & DepInstMin) != DepInstMin)
979 {
980 for (pkgCache::DepIterator D = V.DependsList(); D.end() != true; ++D)
981 {
982 if (D->Type != pkgCache::Dep::PreDepends && D->Type != pkgCache::Dep::Depends)
983 continue;
984 ReInstallPseudoForGroup(D.TargetPkg(), recheck);
985 }
986 unsigned char const CurDepState = VersionState(V.DependsList(),DepInstall,DepInstMin,DepInstPolicy);
987 // if package ist still broken… try another arch
988 if ((CurDepState & DepInstMin) != DepInstMin)
989 break;
990 }
991 // dependencies satisfied: reinstall the package
992 RemoveSizes(P);
993 RemoveStates(P);
994 P->CurrentVer = V.Index();
995 PkgState[P->ID].InstallVer = V;
996 AddStates(P);
997 Update(P);
998 AddSizes(P);
999 return true;
1000 }
1001 }
1002 return false;
1003}
1004 /*}}}*/
6c139d6e
AL
1005// DepCache::Update - Update the deps list of a package /*{{{*/
1006// ---------------------------------------------------------------------
1007/* This is a helper for update that only does the dep portion of the scan.
74a05226 1008 It is mainly meant to scan reverse dependencies. */
6c139d6e
AL
1009void pkgDepCache::Update(DepIterator D)
1010{
1011 // Update the reverse deps
1012 for (;D.end() != true; D++)
1013 {
1014 unsigned char &State = DepState[D->ID];
1015 State = DependencyState(D);
1016
1017 // Invert for Conflicts
308c7d30
IJ
1018 if (D->Type == Dep::Conflicts ||
1019 D->Type == Dep::DpkgBreaks ||
1020 D->Type == Dep::Obsoletes)
6c139d6e 1021 State = ~State;
b2e465d6 1022
6c139d6e
AL
1023 RemoveStates(D.ParentPkg());
1024 BuildGroupOrs(D.ParentVer());
1025 UpdateVerState(D.ParentPkg());
1026 AddStates(D.ParentPkg());
1027 }
1028}
1029 /*}}}*/
1030// DepCache::Update - Update the related deps of a package /*{{{*/
1031// ---------------------------------------------------------------------
1032/* This is called whenever the state of a package changes. It updates
1033 all cached dependencies related to this package. */
1034void pkgDepCache::Update(PkgIterator const &Pkg)
b2e465d6 1035{
6c139d6e
AL
1036 // Recompute the dep of the package
1037 RemoveStates(Pkg);
1038 UpdateVerState(Pkg);
1039 AddStates(Pkg);
1040
1041 // Update the reverse deps
1042 Update(Pkg.RevDependsList());
1043
1044 // Update the provides map for the current ver
1045 if (Pkg->CurrentVer != 0)
1046 for (PrvIterator P = Pkg.CurrentVer().ProvidesList();
1047 P.end() != true; P++)
1048 Update(P.ParentPkg().RevDependsList());
1049
1050 // Update the provides map for the candidate ver
9972233d
AL
1051 if (PkgState[Pkg->ID].CandidateVer != 0)
1052 for (PrvIterator P = PkgState[Pkg->ID].CandidateVerIter(*this).ProvidesList();
1053 P.end() != true; P++)
1054 Update(P.ParentPkg().RevDependsList());
6c139d6e 1055}
6c139d6e 1056 /*}}}*/
6c139d6e
AL
1057// DepCache::MarkKeep - Put the package in the keep state /*{{{*/
1058// ---------------------------------------------------------------------
1059/* */
af29ffb4
MV
1060void pkgDepCache::MarkKeep(PkgIterator const &Pkg, bool Soft, bool FromUser,
1061 unsigned long Depth)
6c139d6e
AL
1062{
1063 // Simplifies other routines.
1064 if (Pkg.end() == true)
1065 return;
813c8eea
AL
1066
1067 /* Reject an attempt to keep a non-source broken installed package, those
1068 must be upgraded */
1069 if (Pkg.State() == PkgIterator::NeedsUnpack &&
1070 Pkg.CurrentVer().Downloadable() == false)
1071 return;
6c139d6e 1072
74a05226
MV
1073 /** \todo Can this be moved later in the method? */
1074 ActionGroup group(*this);
1075
6c139d6e
AL
1076 /* We changed the soft state all the time so the UI is a bit nicer
1077 to use */
1078 StateCache &P = PkgState[Pkg->ID];
1079 if (Soft == true)
1080 P.iFlags |= AutoKept;
1081 else
1082 P.iFlags &= ~AutoKept;
1083
1084 // Check that it is not already kept
1085 if (P.Mode == ModeKeep)
1086 return;
1087
1088 // We dont even try to keep virtual packages..
1089 if (Pkg->VersionList == 0)
1090 return;
32085498
MV
1091#if 0 // reseting the autoflag here means we lose the
1092 // auto-mark information if a user selects a package for removal
1093 // but changes his mind then and sets it for keep again
1094 // - this makes sense as default when all Garbage dependencies
1095 // are automatically marked for removal (as aptitude does).
1096 // setting a package for keep then makes it no longer autoinstalled
1097 // for all other use-case this action is rather suprising
74a05226
MV
1098 if(FromUser && !P.Marked)
1099 P.Flags &= ~Flag::Auto;
32085498
MV
1100#endif
1101
af29ffb4 1102 if (DebugMarker == true)
c3a85f49 1103 std::clog << OutputInDepth(Depth) << "MarkKeep " << Pkg << " FU=" << FromUser << std::endl;
af29ffb4 1104
6c139d6e
AL
1105 RemoveSizes(Pkg);
1106 RemoveStates(Pkg);
1107
1108 P.Mode = ModeKeep;
1109 if (Pkg->CurrentVer == 0)
1110 P.InstallVer = 0;
1111 else
1112 P.InstallVer = Pkg.CurrentVer();
1113
1114 AddStates(Pkg);
1115
1116 Update(Pkg);
1117
1118 AddSizes(Pkg);
1119}
1120 /*}}}*/
1121// DepCache::MarkDelete - Put the package in the delete state /*{{{*/
1122// ---------------------------------------------------------------------
1123/* */
af29ffb4 1124void pkgDepCache::MarkDelete(PkgIterator const &Pkg, bool rPurge,
6910a2ac 1125 unsigned long Depth, bool FromUser)
6c139d6e
AL
1126{
1127 // Simplifies other routines.
1128 if (Pkg.end() == true)
1129 return;
1130
74a05226
MV
1131 ActionGroup group(*this);
1132
6c139d6e
AL
1133 // Check that it is not already marked for delete
1134 StateCache &P = PkgState[Pkg->ID];
d556d1a1
AL
1135 P.iFlags &= ~(AutoKept | Purge);
1136 if (rPurge == true)
1137 P.iFlags |= Purge;
1138
1139 if ((P.Mode == ModeDelete || P.InstallVer == 0) &&
1140 (Pkg.Purge() == true || rPurge == false))
6c139d6e 1141 return;
d556d1a1 1142
6c139d6e
AL
1143 // We dont even try to delete virtual packages..
1144 if (Pkg->VersionList == 0)
1145 return;
1146
6910a2ac
DK
1147 // check if we are allowed to install the package
1148 if (IsDeleteOk(Pkg,rPurge,Depth,FromUser) == false)
6910a2ac 1149 return;
6910a2ac 1150
af29ffb4 1151 if (DebugMarker == true)
35db2f35 1152 std::clog << OutputInDepth(Depth) << (rPurge ? "MarkPurge " : "MarkDelete ") << Pkg << " FU=" << FromUser << std::endl;
af29ffb4 1153
6c139d6e
AL
1154 RemoveSizes(Pkg);
1155 RemoveStates(Pkg);
1156
d556d1a1 1157 if (Pkg->CurrentVer == 0 && (Pkg.Purge() == true || rPurge == false))
3d615484
AL
1158 P.Mode = ModeKeep;
1159 else
1160 P.Mode = ModeDelete;
6c139d6e 1161 P.InstallVer = 0;
6c139d6e
AL
1162
1163 AddStates(Pkg);
1164 Update(Pkg);
1165 AddSizes(Pkg);
803ea2a8
DK
1166
1167 // if we remove the pseudo package, we also need to remove the "real"
1168 if (Pkg->CurrentVer != 0 && Pkg.CurrentVer().Pseudo() == true)
1169 MarkDelete(Pkg.Group().FindPkg("all"), rPurge, Depth+1, FromUser);
35db2f35
DK
1170 else if (rPurge == true && Pkg->CurrentVer == 0 &&
1171 Pkg->CurrentState != pkgCache::State::NotInstalled &&
1172 strcmp(Pkg.Arch(), "all") != 0)
1173 {
1174 PkgIterator const allPkg = Pkg.Group().FindPkg("all");
1175 if (allPkg.end() == false && allPkg->CurrentVer == 0 &&
1176 allPkg->CurrentState != pkgCache::State::NotInstalled)
1177 MarkDelete(allPkg, rPurge, Depth+1, FromUser);
1178 }
6c139d6e
AL
1179}
1180 /*}}}*/
6910a2ac
DK
1181// DepCache::IsDeleteOk - check if it is ok to remove this package /*{{{*/
1182// ---------------------------------------------------------------------
1183/* The default implementation just honors dpkg hold
1184 But an application using this library can override this method
1185 to control the MarkDelete behaviour */
1186bool pkgDepCache::IsDeleteOk(PkgIterator const &Pkg,bool rPurge,
1187 unsigned long Depth, bool FromUser)
1188{
83cb4069 1189 if (FromUser == false && Pkg->SelectedState == pkgCache::State::Hold && _config->FindB("APT::Ignore-Hold",false) == false)
6910a2ac
DK
1190 {
1191 if (DebugMarker == true)
c3a85f49 1192 std::clog << OutputInDepth(Depth) << "Hold prevents MarkDelete of " << Pkg << " FU=" << FromUser << std::endl;
6910a2ac
DK
1193 return false;
1194 }
1195 return true;
1196}
1197 /*}}}*/
6c139d6e
AL
1198// DepCache::MarkInstall - Put the package in the install state /*{{{*/
1199// ---------------------------------------------------------------------
1200/* */
b2e465d6 1201void pkgDepCache::MarkInstall(PkgIterator const &Pkg,bool AutoInst,
7610bb3d
MV
1202 unsigned long Depth, bool FromUser,
1203 bool ForceImportantDeps)
b2e465d6
AL
1204{
1205 if (Depth > 100)
1206 return;
1207
6c139d6e
AL
1208 // Simplifies other routines.
1209 if (Pkg.end() == true)
1210 return;
1211
74a05226
MV
1212 ActionGroup group(*this);
1213
6c139d6e
AL
1214 /* Check that it is not already marked for install and that it can be
1215 installed */
1216 StateCache &P = PkgState[Pkg->ID];
1217 P.iFlags &= ~AutoKept;
60681f93
MV
1218 if ((P.InstPolicyBroken() == false && P.InstBroken() == false) &&
1219 (P.Mode == ModeInstall ||
6c139d6e
AL
1220 P.CandidateVer == (Version *)Pkg.CurrentVer()))
1221 {
1222 if (P.CandidateVer == (Version *)Pkg.CurrentVer() && P.InstallVer == 0)
af29ffb4 1223 MarkKeep(Pkg, false, FromUser, Depth+1);
6c139d6e
AL
1224 return;
1225 }
b2e465d6
AL
1226
1227 // See if there is even any possible instalation candidate
1228 if (P.CandidateVer == 0)
1229 return;
6c139d6e
AL
1230 // We dont even try to install virtual packages..
1231 if (Pkg->VersionList == 0)
1232 return;
6910a2ac
DK
1233
1234 // check if we are allowed to install the package
1235 if (IsInstallOk(Pkg,AutoInst,Depth,FromUser) == false)
6910a2ac 1236 return;
6910a2ac 1237
6c139d6e
AL
1238 /* Target the candidate version and remove the autoflag. We reset the
1239 autoflag below if this was called recursively. Otherwise the user
1240 should have the ability to de-auto a package by changing its state */
1241 RemoveSizes(Pkg);
1242 RemoveStates(Pkg);
1243
1244 P.Mode = ModeInstall;
1245 P.InstallVer = P.CandidateVer;
74a05226
MV
1246
1247 if(FromUser)
1248 {
1249 // Set it to manual if it's a new install or cancelling the
1250 // removal of a garbage package.
1251 if(P.Status == 2 || (!Pkg.CurrentVer().end() && !P.Marked))
1252 P.Flags &= ~Flag::Auto;
1253 }
1254 else
1255 {
1256 // Set it to auto if this is a new install.
1257 if(P.Status == 2)
1258 P.Flags |= Flag::Auto;
1259 }
6c139d6e
AL
1260 if (P.CandidateVer == (Version *)Pkg.CurrentVer())
1261 P.Mode = ModeKeep;
1262
1263 AddStates(Pkg);
1264 Update(Pkg);
1265 AddSizes(Pkg);
6910a2ac 1266
5c640e86
DK
1267 // always trigger the install of the all package for a pseudo package
1268 if (P.CandidateVerIter(*Cache).Pseudo() == true)
1269 MarkInstall(Pkg.Group().FindPkg("all"), AutoInst, Depth, FromUser, ForceImportantDeps);
1270
6c139d6e
AL
1271 if (AutoInst == false)
1272 return;
1273
af29ffb4 1274 if (DebugMarker == true)
c3a85f49 1275 std::clog << OutputInDepth(Depth) << "MarkInstall " << Pkg << " FU=" << FromUser << std::endl;
af29ffb4 1276
6c139d6e
AL
1277 DepIterator Dep = P.InstVerIter(*this).DependsList();
1278 for (; Dep.end() != true;)
1279 {
1280 // Grok or groups
1281 DepIterator Start = Dep;
1282 bool Result = true;
b2e465d6
AL
1283 unsigned Ors = 0;
1284 for (bool LastOR = true; Dep.end() == false && LastOR == true; Dep++,Ors++)
6c139d6e
AL
1285 {
1286 LastOR = (Dep->CompareOp & Dep::Or) == Dep::Or;
1287
1288 if ((DepState[Dep->ID] & DepInstall) == DepInstall)
1289 Result = false;
1290 }
1291
1292 // Dep is satisfied okay.
1293 if (Result == false)
1294 continue;
1295
1296 /* Check if this dep should be consider for install. If it is a user
1297 defined important dep and we are installed a new package then
4ef9a929
MV
1298 it will be installed. Otherwise we only check for important
1299 deps that have changed from the installed version
1300 */
6c139d6e
AL
1301 if (IsImportantDep(Start) == false)
1302 continue;
4ef9a929 1303
0526822a
DB
1304 /* Check if any ImportantDep() (but not Critical) were added
1305 * since we installed the package. Also check for deps that
1306 * were satisfied in the past: for instance, if a version
1307 * restriction in a Recommends was tightened, upgrading the
1308 * package should follow that Recommends rather than causing the
1309 * dependency to be removed. (bug #470115)
1b1c2224
MV
1310 */
1311 bool isNewImportantDep = false;
0526822a 1312 bool isPreviouslySatisfiedImportantDep = false;
4ef9a929 1313 if(!ForceImportantDeps && !Start.IsCritical())
1b1c2224
MV
1314 {
1315 bool found=false;
1316 VerIterator instVer = Pkg.CurrentVer();
6ea08680 1317 if(!instVer.end())
1d722933 1318 {
0526822a
DB
1319 for (DepIterator D = instVer.DependsList(); D.end() != true; D++)
1320 {
6ea08680
MV
1321 //FIXME: deal better with or-groups(?)
1322 DepIterator LocalStart = D;
4faff53f
MV
1323
1324 if(IsImportantDep(D) && !D.IsCritical() &&
1325 Start.TargetPkg() == D.TargetPkg())
0526822a
DB
1326 {
1327 if(!isPreviouslySatisfiedImportantDep)
1328 {
1329 DepIterator D2 = D;
1330 while((D2->CompareOp & Dep::Or) != 0)
1331 ++D2;
1332
1333 isPreviouslySatisfiedImportantDep =
1334 (((*this)[D2] & DepGNow) != 0);
1335 }
1336
1337 found=true;
1338 }
1339 }
1d722933
MV
1340 // this is a new dep if it was not found to be already
1341 // a important dep of the installed pacakge
1342 isNewImportantDep = !found;
1343 }
1b1c2224
MV
1344 }
1345 if(isNewImportantDep)
af29ffb4
MV
1346 if(DebugAutoInstall == true)
1347 std::clog << OutputInDepth(Depth) << "new important dependency: "
1b1c2224 1348 << Start.TargetPkg().Name() << std::endl;
0526822a 1349 if(isPreviouslySatisfiedImportantDep)
af29ffb4
MV
1350 if(DebugAutoInstall == true)
1351 std::clog << OutputInDepth(Depth) << "previously satisfied important dependency on "
0526822a 1352 << Start.TargetPkg().Name() << std::endl;
1b1c2224 1353
4ef9a929
MV
1354 // skip important deps if the package is already installed
1355 if (Pkg->CurrentVer != 0 && Start.IsCritical() == false
0526822a
DB
1356 && !isNewImportantDep && !isPreviouslySatisfiedImportantDep
1357 && !ForceImportantDeps)
6c139d6e 1358 continue;
b2e465d6
AL
1359
1360 /* If we are in an or group locate the first or that can
1361 succeed. We have already cached this.. */
1362 for (; Ors > 1 && (DepState[Start->ID] & DepCVer) != DepCVer; Ors--)
1363 Start++;
2ed9b455 1364
b2e465d6
AL
1365 /* This bit is for processing the possibilty of an install/upgrade
1366 fixing the problem */
1367 SPtrArray<Version *> List = Start.AllTargets();
308c7d30
IJ
1368 if (Start->Type != Dep::DpkgBreaks &&
1369 (DepState[Start->ID] & DepCVer) == DepCVer)
6c139d6e 1370 {
b2e465d6
AL
1371 // Right, find the best version to install..
1372 Version **Cur = List;
1373 PkgIterator P = Start.TargetPkg();
1374 PkgIterator InstPkg(*Cache,0);
6c139d6e 1375
b2e465d6
AL
1376 // See if there are direct matches (at the start of the list)
1377 for (; *Cur != 0 && (*Cur)->ParentPkg == P.Index(); Cur++)
1378 {
1379 PkgIterator Pkg(*Cache,Cache->PkgP + (*Cur)->ParentPkg);
1380 if (PkgState[Pkg->ID].CandidateVer != *Cur)
1381 continue;
1382 InstPkg = Pkg;
1383 break;
1384 }
1385
1386 // Select the highest priority providing package
2ed9b455 1387 if (InstPkg.end() == true)
b2e465d6
AL
1388 {
1389 pkgPrioSortList(*Cache,Cur);
1390 for (; *Cur != 0; Cur++)
1391 {
1392 PkgIterator Pkg(*Cache,Cache->PkgP + (*Cur)->ParentPkg);
1393 if (PkgState[Pkg->ID].CandidateVer != *Cur)
1394 continue;
1395 InstPkg = Pkg;
1396 break;
1397 }
1398 }
1399
6910a2ac 1400 if (InstPkg.end() == false)
b2e465d6 1401 {
af29ffb4
MV
1402 if(DebugAutoInstall == true)
1403 std::clog << OutputInDepth(Depth) << "Installing " << InstPkg.Name()
1404 << " as " << Start.DepType() << " of " << Pkg.Name()
d4c5f11f 1405 << std::endl;
92b9551f 1406 // now check if we should consider it a automatic dependency or not
cb1933df 1407 if(Pkg.Section() && ConfigValueInSubTree("APT::Never-MarkAuto-Sections", Pkg.Section()))
92b9551f 1408 {
af29ffb4
MV
1409 if(DebugAutoInstall == true)
1410 std::clog << OutputInDepth(Depth) << "Setting NOT as auto-installed (direct "
1411 << Start.DepType() << " of pkg in APT::Never-MarkAuto-Sections)" << std::endl;
92b9551f
MV
1412 MarkInstall(InstPkg,true,Depth + 1, true);
1413 }
1414 else
1415 {
1416 // mark automatic dependency
b51ff02a 1417 MarkInstall(InstPkg,true,Depth + 1, false, ForceImportantDeps);
92b9551f
MV
1418 // Set the autoflag, after MarkInstall because MarkInstall unsets it
1419 if (P->CurrentVer == 0)
1420 PkgState[InstPkg->ID].Flags |= Flag::Auto;
1421 }
b2e465d6 1422 }
6c139d6e
AL
1423 continue;
1424 }
308c7d30 1425
b2e465d6 1426 /* For conflicts we just de-install the package and mark as auto,
308c7d30
IJ
1427 Conflicts may not have or groups. For dpkg's Breaks we try to
1428 upgrade the package. */
1429 if (Start->Type == Dep::Conflicts || Start->Type == Dep::Obsoletes ||
1430 Start->Type == Dep::DpkgBreaks)
6c139d6e 1431 {
6c139d6e
AL
1432 for (Version **I = List; *I != 0; I++)
1433 {
1434 VerIterator Ver(*this,*I);
1435 PkgIterator Pkg = Ver.ParentPkg();
308c7d30 1436
736f6751
JAK
1437
1438
1439 if (PkgState[Pkg->ID].CandidateVer != *I &&
1440 Start->Type == Dep::DpkgBreaks)
6910a2ac 1441 MarkInstall(Pkg,true,Depth + 1, false, ForceImportantDeps);
736f6751
JAK
1442 else
1443 MarkDelete(Pkg,false,Depth + 1, false);
6c139d6e 1444 }
6c139d6e
AL
1445 continue;
1446 }
1447 }
1448}
92fcbfc1 1449 /*}}}*/
6910a2ac 1450// DepCache::IsInstallOk - check if it is ok to install this package /*{{{*/
2d403b92 1451// ---------------------------------------------------------------------
d116d668 1452/* The default implementation just honors dpkg hold
6910a2ac 1453 But an application using this library can override this method
d116d668 1454 to control the MarkInstall behaviour */
6910a2ac
DK
1455bool pkgDepCache::IsInstallOk(PkgIterator const &Pkg,bool AutoInst,
1456 unsigned long Depth, bool FromUser)
2d403b92 1457{
83cb4069 1458 if (FromUser == false && Pkg->SelectedState == pkgCache::State::Hold && _config->FindB("APT::Ignore-Hold",false) == false)
6910a2ac
DK
1459 {
1460 if (DebugMarker == true)
c3a85f49 1461 std::clog << OutputInDepth(Depth) << "Hold prevents MarkInstall of " << Pkg << " FU=" << FromUser << std::endl;
6910a2ac
DK
1462 return false;
1463 }
1464 return true;
2d403b92
MV
1465}
1466 /*}}}*/
d0c59649
AL
1467// DepCache::SetReInstall - Set the reinstallation flag /*{{{*/
1468// ---------------------------------------------------------------------
1469/* */
1470void pkgDepCache::SetReInstall(PkgIterator const &Pkg,bool To)
1471{
1019948c
DK
1472 if (unlikely(Pkg.end() == true))
1473 return;
1474
74a05226
MV
1475 ActionGroup group(*this);
1476
d0c59649
AL
1477 RemoveSizes(Pkg);
1478 RemoveStates(Pkg);
1479
1480 StateCache &P = PkgState[Pkg->ID];
1481 if (To == true)
1482 P.iFlags |= ReInstall;
1483 else
1484 P.iFlags &= ~ReInstall;
1485
1486 AddStates(Pkg);
1487 AddSizes(Pkg);
1019948c
DK
1488
1489 if (unlikely(Pkg.CurrentVer().end() == true) || Pkg.CurrentVer().Pseudo() == false)
1490 return;
1491
1492 SetReInstall(Pkg.Group().FindPkg("all"), To);
d0c59649
AL
1493}
1494 /*}}}*/
b2e465d6
AL
1495// DepCache::SetCandidateVersion - Change the candidate version /*{{{*/
1496// ---------------------------------------------------------------------
1497/* */
1019948c 1498void pkgDepCache::SetCandidateVersion(VerIterator TargetVer, bool const &Pseudo)
b2e465d6 1499{
74a05226
MV
1500 ActionGroup group(*this);
1501
b2e465d6
AL
1502 pkgCache::PkgIterator Pkg = TargetVer.ParentPkg();
1503 StateCache &P = PkgState[Pkg->ID];
74a05226 1504
b2e465d6
AL
1505 RemoveSizes(Pkg);
1506 RemoveStates(Pkg);
1507
1508 if (P.CandidateVer == P.InstallVer)
1509 P.InstallVer = (Version *)TargetVer;
1510 P.CandidateVer = (Version *)TargetVer;
1511 P.Update(Pkg,*this);
1512
1513 AddStates(Pkg);
1514 Update(Pkg);
1515 AddSizes(Pkg);
1019948c
DK
1516
1517 if (TargetVer.Pseudo() == false || Pseudo == false)
1518 return;
1519
1520 // the version was pseudo: set all other pseudos also
1521 pkgCache::GrpIterator Grp = Pkg.Group();
1522 for (Pkg = Grp.FindPkg("any"); Pkg.end() == false; ++Pkg)
1523 {
1524 StateCache &P = PkgState[Pkg->ID];
1525 if (TargetVer.SimilarVer(P.CandidateVerIter(*this)) == true ||
1526 (P.CandidateVerIter(*this).Pseudo() == false &&
1527 strcmp(Pkg.Arch(), "all") != 0))
1528 continue;
1529
1530 for (pkgCache::VerIterator Ver = Pkg.VersionList(); Ver.end() == false; ++Ver)
1531 {
1532 if (TargetVer.SimilarVer(Ver) == false)
1533 continue;
1534 SetCandidateVersion(Ver, false);
1535 break;
1536 }
1537 }
b2e465d6 1538}
74a05226
MV
1539
1540void pkgDepCache::MarkAuto(const PkgIterator &Pkg, bool Auto)
1541{
1542 StateCache &state = PkgState[Pkg->ID];
1543
1544 ActionGroup group(*this);
1545
1546 if(Auto)
1547 state.Flags |= Flag::Auto;
1548 else
1549 state.Flags &= ~Flag::Auto;
1550}
b2e465d6 1551 /*}}}*/
6c139d6e
AL
1552// StateCache::Update - Compute the various static display things /*{{{*/
1553// ---------------------------------------------------------------------
1554/* This is called whenever the Candidate version changes. */
1555void pkgDepCache::StateCache::Update(PkgIterator Pkg,pkgCache &Cache)
1556{
1557 // Some info
1558 VerIterator Ver = CandidateVerIter(Cache);
1559
1560 // Use a null string or the version string
1561 if (Ver.end() == true)
1562 CandVersion = "";
1563 else
1564 CandVersion = Ver.VerStr();
1565
1566 // Find the current version
1567 CurVersion = "";
1568 if (Pkg->CurrentVer != 0)
1569 CurVersion = Pkg.CurrentVer().VerStr();
1570
1571 // Strip off the epochs for display
1572 CurVersion = StripEpoch(CurVersion);
1573 CandVersion = StripEpoch(CandVersion);
1574
1575 // Figure out if its up or down or equal
1576 Status = Ver.CompareVer(Pkg.CurrentVer());
1577 if (Pkg->CurrentVer == 0 || Pkg->VersionList == 0 || CandidateVer == 0)
1578 Status = 2;
1579}
1580 /*}}}*/
1581// StateCache::StripEpoch - Remove the epoch specifier from the version /*{{{*/
1582// ---------------------------------------------------------------------
1583/* */
1584const char *pkgDepCache::StateCache::StripEpoch(const char *Ver)
1585{
1586 if (Ver == 0)
1587 return 0;
1588
1589 // Strip any epoch
1590 for (const char *I = Ver; *I != 0; I++)
1591 if (*I == ':')
1592 return I + 1;
1593 return Ver;
1594}
1595 /*}}}*/
b2e465d6 1596// Policy::GetCandidateVer - Returns the Candidate install version /*{{{*/
6321777b 1597// ---------------------------------------------------------------------
b2e465d6
AL
1598/* The default just returns the highest available version that is not
1599 a source and automatic. */
e841200b 1600pkgCache::VerIterator pkgDepCache::Policy::GetCandidateVer(PkgIterator const &Pkg)
6321777b 1601{
b2e465d6
AL
1602 /* Not source/not automatic versions cannot be a candidate version
1603 unless they are already installed */
1604 VerIterator Last(*(pkgCache *)this,0);
6321777b 1605
b2e465d6
AL
1606 for (VerIterator I = Pkg.VersionList(); I.end() == false; I++)
1607 {
1608 if (Pkg.CurrentVer() == I)
1609 return I;
1610
1611 for (VerFileIterator J = I.FileList(); J.end() == false; J++)
1612 {
1613 if ((J.File()->Flags & Flag::NotSource) != 0)
1614 continue;
1615
1616 /* Stash the highest version of a not-automatic source, we use it
1617 if there is nothing better */
1618 if ((J.File()->Flags & Flag::NotAutomatic) != 0)
1619 {
1620 if (Last.end() == true)
1621 Last = I;
1622 continue;
1623 }
1624
1625 return I;
1626 }
1627 }
6321777b 1628
b2e465d6
AL
1629 return Last;
1630}
1631 /*}}}*/
1632// Policy::IsImportantDep - True if the dependency is important /*{{{*/
1633// ---------------------------------------------------------------------
1634/* */
e841200b 1635bool pkgDepCache::Policy::IsImportantDep(DepIterator const &Dep)
b2e465d6 1636{
60681f93
MV
1637 if(Dep.IsCritical())
1638 return true;
1d722933
MV
1639 else if(Dep->Type == pkgCache::Dep::Recommends)
1640 {
1641 if ( _config->FindB("APT::Install-Recommends", false))
1642 return true;
1643 // we suport a special mode to only install-recommends for certain
1644 // sections
1645 // FIXME: this is a meant as a temporarly solution until the
1646 // recommends are cleaned up
cb1933df
MV
1647 const char *sec = Dep.ParentVer().Section();
1648 if (sec && ConfigValueInSubTree("APT::Install-Recommends-Sections", sec))
1649 return true;
1d722933 1650 }
60681f93
MV
1651 else if(Dep->Type == pkgCache::Dep::Suggests)
1652 return _config->FindB("APT::Install-Suggests", false);
1653
74a05226
MV
1654 return false;
1655}
92fcbfc1
DK
1656 /*}}}*/
1657pkgDepCache::InRootSetFunc *pkgDepCache::GetRootSetFunc() /*{{{*/
74a05226
MV
1658{
1659 DefaultRootSetFunc *f = new DefaultRootSetFunc;
1660 if(f->wasConstructedSuccessfully())
1661 return f;
1662 else
1663 {
1664 delete f;
1665 return NULL;
1666 }
1667}
92fcbfc1 1668 /*}}}*/
74a05226
MV
1669bool pkgDepCache::MarkFollowsRecommends()
1670{
1671 return _config->FindB("APT::AutoRemove::RecommendsImportant", true);
1672}
1673
1674bool pkgDepCache::MarkFollowsSuggests()
1675{
1676 return _config->FindB("APT::AutoRemove::SuggestsImportant", false);
1677}
1678
92fcbfc1 1679// pkgDepCache::MarkRequired - the main mark algorithm /*{{{*/
74a05226
MV
1680bool pkgDepCache::MarkRequired(InRootSetFunc &userFunc)
1681{
1682 bool follow_recommends;
1683 bool follow_suggests;
95afdfd0 1684 bool debug_autoremove = _config->FindB("Debug::pkgAutoRemove",false);
74a05226
MV
1685
1686 // init the states
1687 for(PkgIterator p = PkgBegin(); !p.end(); ++p)
1688 {
1689 PkgState[p->ID].Marked = false;
1690 PkgState[p->ID].Garbage = false;
1691
1692 // debug output
95afdfd0 1693 if(debug_autoremove && PkgState[p->ID].Flags & Flag::Auto)
e0b94b97 1694 std::clog << "AutoDep: " << p.FullName() << std::endl;
74a05226
MV
1695 }
1696
1697 // init vars
1698 follow_recommends = MarkFollowsRecommends();
1699 follow_suggests = MarkFollowsSuggests();
1700
1701
1702
1703 // do the mark part, this is the core bit of the algorithm
1704 for(PkgIterator p = PkgBegin(); !p.end(); ++p)
1705 {
1706 if(!(PkgState[p->ID].Flags & Flag::Auto) ||
1707 (p->Flags & Flag::Essential) ||
83789130
DK
1708 userFunc.InRootSet(p) ||
1709 // be nice even then a required package violates the policy (#583517)
1710 // and do the full mark process also for required packages
1711 (p.CurrentVer().end() != true &&
1712 p.CurrentVer()->Priority == pkgCache::State::Required))
74a05226
MV
1713 {
1714 // the package is installed (and set to keep)
1715 if(PkgState[p->ID].Keep() && !p.CurrentVer().end())
83860e37 1716 MarkPackage(p, p.CurrentVer(),
74a05226
MV
1717 follow_recommends, follow_suggests);
1718 // the package is to be installed
1719 else if(PkgState[p->ID].Install())
1720 MarkPackage(p, PkgState[p->ID].InstVerIter(*this),
1721 follow_recommends, follow_suggests);
1722 }
1723 }
1724
1725 return true;
1726}
92fcbfc1
DK
1727 /*}}}*/
1728// MarkPackage - mark a single package in Mark-and-Sweep /*{{{*/
74a05226
MV
1729void pkgDepCache::MarkPackage(const pkgCache::PkgIterator &pkg,
1730 const pkgCache::VerIterator &ver,
e0b94b97
DK
1731 bool const &follow_recommends,
1732 bool const &follow_suggests)
74a05226
MV
1733{
1734 pkgDepCache::StateCache &state = PkgState[pkg->ID];
e0b94b97
DK
1735
1736 // if we are marked already we are done
1737 if(state.Marked)
1738 return;
1739
1740 VerIterator const currver = pkg.CurrentVer();
1741 VerIterator const candver = state.CandidateVerIter(*this);
1742 VerIterator const instver = state.InstVerIter(*this);
74a05226
MV
1743
1744#if 0
1745 // If a package was garbage-collected but is now being marked, we
1746 // should re-select it
1747 // For cases when a pkg is set to upgrade and this trigger the
1748 // removal of a no-longer used dependency. if the pkg is set to
1749 // keep again later it will result in broken deps
1750 if(state.Delete() && state.RemoveReason = Unused)
1751 {
1752 if(ver==candver)
1753 mark_install(pkg, false, false, NULL);
1754 else if(ver==pkg.CurrentVer())
1755 MarkKeep(pkg, false, false);
1756
1757 instver=state.InstVerIter(*this);
1758 }
1759#endif
1760
36baa77a
MV
1761 // For packages that are not going to be removed, ignore versions
1762 // other than the InstVer. For packages that are going to be
1763 // removed, ignore versions other than the current version.
1764 if(!(ver == instver && !instver.end()) &&
1765 !(ver == currver && instver.end() && !ver.end()))
74a05226
MV
1766 return;
1767
e0b94b97 1768 bool const debug_autoremove = _config->FindB("Debug::pkgAutoRemove", false);
74a05226 1769
95afdfd0 1770 if(debug_autoremove)
83860e37 1771 {
e0b94b97 1772 std::clog << "Marking: " << pkg.FullName();
83860e37
DB
1773 if(!ver.end())
1774 std::clog << " " << ver.VerStr();
1775 if(!currver.end())
1776 std::clog << ", Curr=" << currver.VerStr();
1777 if(!instver.end())
1778 std::clog << ", Inst=" << instver.VerStr();
1779 std::clog << std::endl;
1780 }
1781
74a05226
MV
1782 state.Marked=true;
1783
e0b94b97
DK
1784 if(ver.end() == true)
1785 return;
1786
1787 // If the version belongs to a Multi-Arch all package
1788 // we will mark all others in this Group with this version also
e0b94b97
DK
1789 if (ver->MultiArch == pkgCache::Version::All &&
1790 strcmp(ver.Arch(true), "all") == 0)
74a05226 1791 {
e0b94b97
DK
1792 GrpIterator G = pkg.Group();
1793 const char* const VerStr = ver.VerStr();
1794 for (PkgIterator P = G.FindPkg("any");
1795 P.end() != true; P = G.NextPkg(P))
1796 {
1797 for (VerIterator V = P.VersionList();
1798 V.end() != true; ++V)
1799 {
392a882a
DK
1800 if (ver->Hash != V->Hash ||
1801 strcmp(VerStr, V.VerStr()) != 0)
e0b94b97
DK
1802 continue;
1803 MarkPackage(P, V, follow_recommends, follow_suggests);
1804 break;
1805 }
1806 }
1807 }
1808
74a05226
MV
1809 for(DepIterator d = ver.DependsList(); !d.end(); ++d)
1810 {
1811 if(d->Type == Dep::Depends ||
1812 d->Type == Dep::PreDepends ||
1813 (follow_recommends &&
1814 d->Type == Dep::Recommends) ||
1815 (follow_suggests &&
1816 d->Type == Dep::Suggests))
1817 {
1818 // Try all versions of this package.
1819 for(VerIterator V = d.TargetPkg().VersionList();
1820 !V.end(); ++V)
1821 {
1822 if(_system->VS->CheckDep(V.VerStr(), d->CompareOp, d.TargetVer()))
1823 {
95afdfd0 1824 if(debug_autoremove)
83860e37 1825 {
e0b94b97 1826 std::clog << "Following dep: " << d.ParentPkg().FullName()
83860e37 1827 << " " << d.ParentVer().VerStr() << " "
e0b94b97 1828 << d.DepType() << " " << d.TargetPkg().FullName();
83860e37
DB
1829 if((d->CompareOp & ~pkgCache::Dep::Or) != pkgCache::Dep::NoOp)
1830 {
1831 std::clog << " (" << d.CompType() << " "
1832 << d.TargetVer() << ")";
1833 }
1834 std::clog << std::endl;
1835 }
e0b94b97 1836 MarkPackage(V.ParentPkg(), V,
74a05226
MV
1837 follow_recommends, follow_suggests);
1838 }
1839 }
1840 // Now try virtual packages
1841 for(PrvIterator prv=d.TargetPkg().ProvidesList();
1842 !prv.end(); ++prv)
1843 {
1844 if(_system->VS->CheckDep(prv.ProvideVersion(), d->CompareOp,
1845 d.TargetVer()))
1846 {
95afdfd0 1847 if(debug_autoremove)
83860e37 1848 {
e0b94b97
DK
1849 std::clog << "Following dep: " << d.ParentPkg().FullName() << " "
1850 << d.ParentVer().VerStr() << " "
1851 << d.DepType() << " " << d.TargetPkg().FullName() << " ";
83860e37
DB
1852 if((d->CompareOp & ~pkgCache::Dep::Or) != pkgCache::Dep::NoOp)
1853 {
1854 std::clog << " (" << d.CompType() << " "
1855 << d.TargetVer() << ")";
1856 }
1857 std::clog << ", provided by "
e0b94b97 1858 << prv.OwnerPkg().FullName() << " "
83860e37
DB
1859 << prv.OwnerVer().VerStr()
1860 << std::endl;
1861 }
1862
74a05226
MV
1863 MarkPackage(prv.OwnerPkg(), prv.OwnerVer(),
1864 follow_recommends, follow_suggests);
1865 }
1866 }
1867 }
1868 }
74a05226 1869}
92fcbfc1
DK
1870 /*}}}*/
1871bool pkgDepCache::Sweep() /*{{{*/
74a05226 1872{
95afdfd0
OS
1873 bool debug_autoremove = _config->FindB("Debug::pkgAutoRemove",false);
1874
74a05226
MV
1875 // do the sweep
1876 for(PkgIterator p=PkgBegin(); !p.end(); ++p)
95afdfd0 1877 {
74a05226
MV
1878 StateCache &state=PkgState[p->ID];
1879
c9b320e8
MV
1880 // skip required packages
1881 if (!p.CurrentVer().end() &&
1882 (p.CurrentVer()->Priority == pkgCache::State::Required))
1883 continue;
1884
74a05226 1885 // if it is not marked and it is installed, it's garbage
32085498 1886 if(!state.Marked && (!p.CurrentVer().end() || state.Install()))
74a05226
MV
1887 {
1888 state.Garbage=true;
95afdfd0 1889 if(debug_autoremove)
6ee2b0f8 1890 std::clog << "Garbage: " << p.FullName() << std::endl;
74a05226
MV
1891 }
1892 }
1893
1894 return true;
1895}
92fcbfc1 1896 /*}}}*/