]> git.saurik.com Git - apt.git/blob - apt-pkg/depcache.cc
avoid 416 response teardown binding to null pointer
[apt.git] / apt-pkg / depcache.cc
1 // -*- mode: cpp; mode: fold -*-
2 // Description /*{{{*/
3 // $Id: depcache.cc,v 1.25 2001/05/27 05:36:04 jgg Exp $
4 /* ######################################################################
5
6 Dependency Cache - Caches Dependency information.
7
8 ##################################################################### */
9 /*}}}*/
10 // Include Files /*{{{*/
11 #include<config.h>
12
13 #include <apt-pkg/depcache.h>
14 #include <apt-pkg/versionmatch.h>
15 #include <apt-pkg/version.h>
16 #include <apt-pkg/error.h>
17 #include <apt-pkg/fileutl.h>
18 #include <apt-pkg/strutl.h>
19 #include <apt-pkg/configuration.h>
20 #include <apt-pkg/aptconfiguration.h>
21 #include <apt-pkg/tagfile.h>
22 #include <apt-pkg/progress.h>
23 #include <apt-pkg/cacheset.h>
24 #include <apt-pkg/pkgcache.h>
25 #include <apt-pkg/cacheiterators.h>
26 #include <apt-pkg/prettyprinters.h>
27 #include <apt-pkg/cachefile.h>
28 #include <apt-pkg/macros.h>
29
30 #include <stdio.h>
31 #include <string.h>
32 #include <list>
33 #include <string>
34 #include <utility>
35 #include <vector>
36 #include <algorithm>
37 #include <iostream>
38 #include <set>
39
40 #include <sys/stat.h>
41
42 #include <apti18n.h>
43 /*}}}*/
44
45 using std::string;
46
47 // helper for Install-Recommends-Sections and Never-MarkAuto-Sections /*{{{*/
48 static bool
49 ConfigValueInSubTree(const char* SubTree, const char *needle)
50 {
51 Configuration::Item const *Opts;
52 Opts = _config->Tree(SubTree);
53 if (Opts != 0 && Opts->Child != 0)
54 {
55 Opts = Opts->Child;
56 for (; Opts != 0; Opts = Opts->Next)
57 {
58 if (Opts->Value.empty() == true)
59 continue;
60 if (strcmp(needle, Opts->Value.c_str()) == 0)
61 return true;
62 }
63 }
64 return false;
65 }
66 /*}}}*/
67 pkgDepCache::ActionGroup::ActionGroup(pkgDepCache &cache) : /*{{{*/
68 d(NULL), cache(cache), released(false)
69 {
70 ++cache.group_level;
71 }
72
73 void pkgDepCache::ActionGroup::release()
74 {
75 if(!released)
76 {
77 if(cache.group_level == 0)
78 std::cerr << "W: Unbalanced action groups, expect badness" << std::endl;
79 else
80 {
81 --cache.group_level;
82
83 if(cache.group_level == 0)
84 cache.MarkAndSweep();
85 }
86
87 released = true;
88 }
89 }
90
91 pkgDepCache::ActionGroup::~ActionGroup()
92 {
93 release();
94 }
95 /*}}}*/
96 // DepCache::pkgDepCache - Constructors /*{{{*/
97 // ---------------------------------------------------------------------
98 /* */
99 pkgDepCache::pkgDepCache(pkgCache * const pCache,Policy * const Plcy) :
100 group_level(0), Cache(pCache), PkgState(0), DepState(0),
101 iUsrSize(0), iDownloadSize(0), iInstCount(0), iDelCount(0), iKeepCount(0),
102 iBrokenCount(0), iPolicyBrokenCount(0), iBadCount(0), d(NULL)
103 {
104 DebugMarker = _config->FindB("Debug::pkgDepCache::Marker", false);
105 DebugAutoInstall = _config->FindB("Debug::pkgDepCache::AutoInstall", false);
106 delLocalPolicy = 0;
107 LocalPolicy = Plcy;
108 if (LocalPolicy == 0)
109 delLocalPolicy = LocalPolicy = new Policy;
110 }
111 /*}}}*/
112 // DepCache::~pkgDepCache - Destructor /*{{{*/
113 // ---------------------------------------------------------------------
114 /* */
115 pkgDepCache::~pkgDepCache()
116 {
117 delete [] PkgState;
118 delete [] DepState;
119 delete delLocalPolicy;
120 }
121 /*}}}*/
122 // DepCache::Init - Generate the initial extra structures. /*{{{*/
123 // ---------------------------------------------------------------------
124 /* This allocats the extension buffers and initializes them. */
125 bool pkgDepCache::Init(OpProgress * const Prog)
126 {
127 // Suppress mark updates during this operation (just in case) and
128 // run a mark operation when Init terminates.
129 ActionGroup actions(*this);
130
131 delete [] PkgState;
132 delete [] DepState;
133 PkgState = new StateCache[Head().PackageCount];
134 DepState = new unsigned char[Head().DependsCount];
135 memset(PkgState,0,sizeof(*PkgState)*Head().PackageCount);
136 memset(DepState,0,sizeof(*DepState)*Head().DependsCount);
137
138 if (Prog != 0)
139 {
140 Prog->OverallProgress(0,2*Head().PackageCount,Head().PackageCount,
141 _("Building dependency tree"));
142 Prog->SubProgress(Head().PackageCount,_("Candidate versions"));
143 }
144
145 /* Set the current state of everything. In this state all of the
146 packages are kept exactly as is. See AllUpgrade */
147 int Done = 0;
148 for (PkgIterator I = PkgBegin(); I.end() != true; ++I, ++Done)
149 {
150 if (Prog != 0 && Done%20 == 0)
151 Prog->Progress(Done);
152
153 // Find the proper cache slot
154 StateCache &State = PkgState[I->ID];
155 State.iFlags = 0;
156
157 // Figure out the install version
158 State.CandidateVer = LocalPolicy->GetCandidateVer(I);
159 State.InstallVer = I.CurrentVer();
160 State.Mode = ModeKeep;
161
162 State.Update(I,*this);
163 }
164
165 if (Prog != 0)
166 {
167 Prog->OverallProgress(Head().PackageCount,2*Head().PackageCount,
168 Head().PackageCount,
169 _("Building dependency tree"));
170 Prog->SubProgress(Head().PackageCount,_("Dependency generation"));
171 }
172
173 Update(Prog);
174
175 if(Prog != 0)
176 Prog->Done();
177
178 return true;
179 }
180 /*}}}*/
181 bool pkgDepCache::readStateFile(OpProgress * const Prog) /*{{{*/
182 {
183 FileFd state_file;
184 string const state = _config->FindFile("Dir::State::extended_states");
185 if(RealFileExists(state)) {
186 state_file.Open(state, FileFd::ReadOnly);
187 off_t const file_size = state_file.Size();
188 if(Prog != NULL)
189 Prog->OverallProgress(0, file_size, 1,
190 _("Reading state information"));
191
192 pkgTagFile tagfile(&state_file);
193 pkgTagSection section;
194 off_t amt = 0;
195 bool const debug_autoremove = _config->FindB("Debug::pkgAutoRemove",false);
196 while(tagfile.Step(section)) {
197 string const pkgname = section.FindS("Package");
198 string pkgarch = section.FindS("Architecture");
199 if (pkgarch.empty() == true)
200 pkgarch = "any";
201 pkgCache::PkgIterator pkg = Cache->FindPkg(pkgname, pkgarch);
202 // Silently ignore unknown packages and packages with no actual version.
203 if(pkg.end() == true || pkg->VersionList == 0)
204 continue;
205
206 short const reason = section.FindI("Auto-Installed", 0);
207 if(reason > 0)
208 {
209 PkgState[pkg->ID].Flags |= Flag::Auto;
210 if (unlikely(debug_autoremove))
211 std::clog << "Auto-Installed : " << pkg.FullName() << std::endl;
212 if (pkgarch == "any")
213 {
214 pkgCache::GrpIterator G = pkg.Group();
215 for (pkg = G.NextPkg(pkg); pkg.end() != true; pkg = G.NextPkg(pkg))
216 if (pkg->VersionList != 0)
217 PkgState[pkg->ID].Flags |= Flag::Auto;
218 }
219 }
220 amt += section.size();
221 if(Prog != NULL)
222 Prog->OverallProgress(amt, file_size, 1,
223 _("Reading state information"));
224 }
225 if(Prog != NULL)
226 Prog->OverallProgress(file_size, file_size, 1,
227 _("Reading state information"));
228 }
229
230 return true;
231 }
232 /*}}}*/
233 bool pkgDepCache::writeStateFile(OpProgress * const /*prog*/, bool const InstalledOnly) /*{{{*/
234 {
235 bool const debug_autoremove = _config->FindB("Debug::pkgAutoRemove",false);
236
237 if(debug_autoremove)
238 std::clog << "pkgDepCache::writeStateFile()" << std::endl;
239
240 FileFd StateFile;
241 string const state = _config->FindFile("Dir::State::extended_states");
242 if (CreateAPTDirectoryIfNeeded(_config->FindDir("Dir::State"), flNotFile(state)) == false)
243 return false;
244
245 // if it does not exist, create a empty one
246 if(!RealFileExists(state))
247 {
248 StateFile.Open(state, FileFd::WriteAtomic);
249 StateFile.Close();
250 }
251
252 // open it
253 if(!StateFile.Open(state, FileFd::ReadOnly))
254 return _error->Error(_("Failed to open StateFile %s"),
255 state.c_str());
256
257 FileFd OutFile(state, FileFd::ReadWrite | FileFd::Atomic);
258 if (OutFile.IsOpen() == false || OutFile.Failed() == true)
259 return _error->Error(_("Failed to write temporary StateFile %s"), state.c_str());
260
261 // first merge with the existing sections
262 pkgTagFile tagfile(&StateFile);
263 pkgTagSection section;
264 std::set<string> pkgs_seen;
265 while(tagfile.Step(section)) {
266 string const pkgname = section.FindS("Package");
267 string pkgarch = section.FindS("Architecture");
268 if (pkgarch.empty() == true)
269 pkgarch = "native";
270 // Silently ignore unknown packages and packages with no actual
271 // version.
272 pkgCache::PkgIterator pkg = Cache->FindPkg(pkgname, pkgarch);
273 if(pkg.end() || pkg.VersionList().end())
274 continue;
275 StateCache const &P = PkgState[pkg->ID];
276 bool newAuto = (P.Flags & Flag::Auto);
277 // skip not installed or now-removed ones if requested
278 if (InstalledOnly && (
279 (pkg->CurrentVer == 0 && P.Mode != ModeInstall) ||
280 (pkg->CurrentVer != 0 && P.Mode == ModeDelete)))
281 {
282 // The section is obsolete if it contains no other tag
283 unsigned int const count = section.Count();
284 if (count < 2 ||
285 (count == 2 && section.Exists("Auto-Installed")) ||
286 (count == 3 && section.Exists("Auto-Installed") && section.Exists("Architecture")))
287 continue;
288 else
289 newAuto = false;
290 }
291 if(_config->FindB("Debug::pkgAutoRemove",false))
292 std::clog << "Update existing AutoInstall info: "
293 << pkg.FullName() << std::endl;
294
295 std::vector<pkgTagSection::Tag> rewrite;
296 rewrite.push_back(pkgTagSection::Tag::Rewrite("Architecture", pkg.Arch()));
297 rewrite.push_back(pkgTagSection::Tag::Rewrite("Auto-Installed", newAuto ? "1" : "0"));
298 section.Write(OutFile, NULL, rewrite);
299 if (OutFile.Write("\n", 1) == false)
300 return false;
301 pkgs_seen.insert(pkg.FullName());
302 }
303
304 // then write the ones we have not seen yet
305 for(pkgCache::PkgIterator pkg=Cache->PkgBegin(); !pkg.end(); ++pkg) {
306 StateCache const &P = PkgState[pkg->ID];
307 if(P.Flags & Flag::Auto) {
308 if (pkgs_seen.find(pkg.FullName()) != pkgs_seen.end()) {
309 if(debug_autoremove)
310 std::clog << "Skipping already written " << pkg.FullName() << std::endl;
311 continue;
312 }
313 // skip not installed ones if requested
314 if (InstalledOnly && (
315 (pkg->CurrentVer == 0 && P.Mode != ModeInstall) ||
316 (pkg->CurrentVer != 0 && P.Mode == ModeDelete)))
317 continue;
318 const char* const pkgarch = pkg.Arch();
319 if (strcmp(pkgarch, "all") == 0)
320 continue;
321 if(debug_autoremove)
322 std::clog << "Writing new AutoInstall: " << pkg.FullName() << std::endl;
323 std::string stanza = "Package: ";
324 stanza.append(pkg.Name())
325 .append("\nArchitecture: ").append(pkgarch)
326 .append("\nAuto-Installed: 1\n\n");
327 if (OutFile.Write(stanza.c_str(), stanza.length()) == false)
328 return false;
329 }
330 }
331 if (StateFile.Failed())
332 {
333 OutFile.OpFail();
334 return false;
335 }
336 if (OutFile.Close() == false)
337 return false;
338 chmod(state.c_str(), 0644);
339 return true;
340 }
341 /*}}}*/
342 // DepCache::CheckDep - Checks a single dependency /*{{{*/
343 // ---------------------------------------------------------------------
344 /* This first checks the dependency against the main target package and
345 then walks along the package provides list and checks if each provides
346 will be installed then checks the provides against the dep. Res will be
347 set to the package which was used to satisfy the dep. */
348 bool pkgDepCache::CheckDep(DepIterator const &Dep,int const Type,PkgIterator &Res)
349 {
350 Res = Dep.TargetPkg();
351
352 /* Check simple depends. A depends -should- never self match but
353 we allow it anyhow because dpkg does. Technically it is a packaging
354 bug. Conflicts may never self match */
355 if (Dep.IsIgnorable(Res) == false)
356 {
357 // Check the base package
358 if (Type == NowVersion)
359 {
360 if (Res->CurrentVer != 0 && Dep.IsSatisfied(Res.CurrentVer()) == true)
361 return true;
362 }
363 else if (Type == InstallVersion)
364 {
365 if (PkgState[Res->ID].InstallVer != 0 &&
366 Dep.IsSatisfied(PkgState[Res->ID].InstVerIter(*this)) == true)
367 return true;
368 }
369 else if (Type == CandidateVersion)
370 if (PkgState[Res->ID].CandidateVer != 0 &&
371 Dep.IsSatisfied(PkgState[Res->ID].CandidateVerIter(*this)) == true)
372 return true;
373 }
374
375 if (Dep->Type == Dep::Obsoletes)
376 return false;
377
378 // Check the providing packages
379 PrvIterator P = Dep.TargetPkg().ProvidesList();
380 for (; P.end() != true; ++P)
381 {
382 if (Dep.IsIgnorable(P) == true)
383 continue;
384
385 // Check if the provides is a hit
386 if (Type == NowVersion)
387 {
388 if (P.OwnerPkg().CurrentVer() != P.OwnerVer())
389 continue;
390 }
391 else if (Type == InstallVersion)
392 {
393 StateCache &State = PkgState[P.OwnerPkg()->ID];
394 if (State.InstallVer != (Version *)P.OwnerVer())
395 continue;
396 }
397 else if (Type == CandidateVersion)
398 {
399 StateCache &State = PkgState[P.OwnerPkg()->ID];
400 if (State.CandidateVer != (Version *)P.OwnerVer())
401 continue;
402 }
403
404 // Compare the versions.
405 if (Dep.IsSatisfied(P) == true)
406 {
407 Res = P.OwnerPkg();
408 return true;
409 }
410 }
411
412 return false;
413 }
414 /*}}}*/
415 // DepCache::AddSizes - Add the packages sizes to the counters /*{{{*/
416 // ---------------------------------------------------------------------
417 /* Call with Inverse = true to preform the inverse opration */
418 void pkgDepCache::AddSizes(const PkgIterator &Pkg, bool const Inverse)
419 {
420 StateCache &P = PkgState[Pkg->ID];
421
422 if (Pkg->VersionList == 0)
423 return;
424
425 if (Pkg.State() == pkgCache::PkgIterator::NeedsConfigure &&
426 P.Keep() == true)
427 return;
428
429 // Compute the size data
430 if (P.NewInstall() == true)
431 {
432 if (Inverse == false) {
433 iUsrSize += P.InstVerIter(*this)->InstalledSize;
434 iDownloadSize += P.InstVerIter(*this)->Size;
435 } else {
436 iUsrSize -= P.InstVerIter(*this)->InstalledSize;
437 iDownloadSize -= P.InstVerIter(*this)->Size;
438 }
439 return;
440 }
441
442 // Upgrading
443 if (Pkg->CurrentVer != 0 &&
444 (P.InstallVer != (Version *)Pkg.CurrentVer() ||
445 (P.iFlags & ReInstall) == ReInstall) && P.InstallVer != 0)
446 {
447 if (Inverse == false) {
448 iUsrSize -= Pkg.CurrentVer()->InstalledSize;
449 iUsrSize += P.InstVerIter(*this)->InstalledSize;
450 iDownloadSize += P.InstVerIter(*this)->Size;
451 } else {
452 iUsrSize -= P.InstVerIter(*this)->InstalledSize;
453 iUsrSize += Pkg.CurrentVer()->InstalledSize;
454 iDownloadSize -= P.InstVerIter(*this)->Size;
455 }
456 return;
457 }
458
459 // Reinstall
460 if (Pkg.State() == pkgCache::PkgIterator::NeedsUnpack &&
461 P.Delete() == false)
462 {
463 if (Inverse == false)
464 iDownloadSize += P.InstVerIter(*this)->Size;
465 else
466 iDownloadSize -= P.InstVerIter(*this)->Size;
467 return;
468 }
469
470 // Removing
471 if (Pkg->CurrentVer != 0 && P.InstallVer == 0)
472 {
473 if (Inverse == false)
474 iUsrSize -= Pkg.CurrentVer()->InstalledSize;
475 else
476 iUsrSize += Pkg.CurrentVer()->InstalledSize;
477 return;
478 }
479 }
480 /*}}}*/
481 // DepCache::AddStates - Add the package to the state counter /*{{{*/
482 // ---------------------------------------------------------------------
483 /* This routine is tricky to use, you must make sure that it is never
484 called twice for the same package. This means the Remove/Add section
485 should be as short as possible and not encompass any code that will
486 calld Remove/Add itself. Remember, dependencies can be circular so
487 while processing a dep for Pkg it is possible that Add/Remove
488 will be called on Pkg */
489 void pkgDepCache::AddStates(const PkgIterator &Pkg, bool const Invert)
490 {
491 signed char const Add = (Invert == false) ? 1 : -1;
492 StateCache &State = PkgState[Pkg->ID];
493
494 // The Package is broken (either minimal dep or policy dep)
495 if ((State.DepState & DepInstMin) != DepInstMin)
496 iBrokenCount += Add;
497 if ((State.DepState & DepInstPolicy) != DepInstPolicy)
498 iPolicyBrokenCount += Add;
499
500 // Bad state
501 if (Pkg.State() != PkgIterator::NeedsNothing)
502 iBadCount += Add;
503
504 // Not installed
505 if (Pkg->CurrentVer == 0)
506 {
507 if (State.Mode == ModeDelete &&
508 (State.iFlags & Purge) == Purge && Pkg.Purge() == false)
509 iDelCount += Add;
510
511 if (State.Mode == ModeInstall)
512 iInstCount += Add;
513 return;
514 }
515
516 // Installed, no upgrade
517 if (State.Status == 0)
518 {
519 if (State.Mode == ModeDelete)
520 iDelCount += Add;
521 else
522 if ((State.iFlags & ReInstall) == ReInstall)
523 iInstCount += Add;
524 return;
525 }
526
527 // Alll 3 are possible
528 if (State.Mode == ModeDelete)
529 iDelCount += Add;
530 else if (State.Mode == ModeKeep)
531 iKeepCount += Add;
532 else if (State.Mode == ModeInstall)
533 iInstCount += Add;
534 }
535 /*}}}*/
536 // DepCache::BuildGroupOrs - Generate the Or group dep data /*{{{*/
537 // ---------------------------------------------------------------------
538 /* The or group results are stored in the last item of the or group. This
539 allows easy detection of the state of a whole or'd group. */
540 void pkgDepCache::BuildGroupOrs(VerIterator const &V)
541 {
542 unsigned char Group = 0;
543 for (DepIterator D = V.DependsList(); D.end() != true; ++D)
544 {
545 // Build the dependency state.
546 unsigned char &State = DepState[D->ID];
547
548 /* Invert for Conflicts. We have to do this twice to get the
549 right sense for a conflicts group */
550 if (D.IsNegative() == true)
551 State = ~State;
552
553 // Add to the group if we are within an or..
554 State &= 0x7;
555 Group |= State;
556 State |= Group << 3;
557 if ((D->CompareOp & Dep::Or) != Dep::Or)
558 Group = 0;
559
560 // Invert for Conflicts
561 if (D.IsNegative() == true)
562 State = ~State;
563 }
564 }
565 /*}}}*/
566 // DepCache::VersionState - Perform a pass over a dependency list /*{{{*/
567 // ---------------------------------------------------------------------
568 /* This is used to run over a dependency list and determine the dep
569 state of the list, filtering it through both a Min check and a Policy
570 check. The return result will have SetMin/SetPolicy low if a check
571 fails. It uses the DepState cache for it's computations. */
572 unsigned char pkgDepCache::VersionState(DepIterator D, unsigned char const Check,
573 unsigned char const SetMin,
574 unsigned char const SetPolicy) const
575 {
576 unsigned char Dep = 0xFF;
577 while (D.end() != true)
578 {
579 // the last or-dependency has the state of all previous or'ed
580 DepIterator Start, End;
581 D.GlobOr(Start, End);
582 // ignore if we are called with Dep{Install,…} or DepG{Install,…}
583 // the later would be more correct, but the first is what we get
584 unsigned char const State = DepState[End->ID] | (DepState[End->ID] >> 3);
585
586 // Minimum deps that must be satisfied to have a working package
587 if (Start.IsCritical() == true)
588 {
589 if ((State & Check) != Check)
590 return Dep &= ~(SetMin | SetPolicy);
591 }
592 // Policy deps that must be satisfied to install the package
593 else if (IsImportantDep(Start) == true &&
594 (State & Check) != Check)
595 Dep &= ~SetPolicy;
596 }
597 return Dep;
598 }
599 /*}}}*/
600 // DepCache::DependencyState - Compute the 3 results for a dep /*{{{*/
601 // ---------------------------------------------------------------------
602 /* This is the main dependency computation bit. It computes the 3 main
603 results for a dependencys, Now, Install and Candidate. Callers must
604 invert the result if dealing with conflicts. */
605 unsigned char pkgDepCache::DependencyState(DepIterator const &D)
606 {
607 unsigned char State = 0;
608
609 if (CheckDep(D,NowVersion) == true)
610 State |= DepNow;
611 if (CheckDep(D,InstallVersion) == true)
612 State |= DepInstall;
613 if (CheckDep(D,CandidateVersion) == true)
614 State |= DepCVer;
615
616 return State;
617 }
618 /*}}}*/
619 // DepCache::UpdateVerState - Compute the Dep member of the state /*{{{*/
620 // ---------------------------------------------------------------------
621 /* This determines the combined dependency representation of a package
622 for its two states now and install. This is done by using the pre-generated
623 dependency information. */
624 void pkgDepCache::UpdateVerState(PkgIterator const &Pkg)
625 {
626 // Empty deps are always true
627 StateCache &State = PkgState[Pkg->ID];
628 State.DepState = 0xFF;
629
630 // Check the Current state
631 if (Pkg->CurrentVer != 0)
632 {
633 DepIterator D = Pkg.CurrentVer().DependsList();
634 State.DepState &= VersionState(D,DepNow,DepNowMin,DepNowPolicy);
635 }
636
637 /* Check the candidate state. We do not compare against the whole as
638 a candidate state but check the candidate version against the
639 install states */
640 if (State.CandidateVer != 0)
641 {
642 DepIterator D = State.CandidateVerIter(*this).DependsList();
643 State.DepState &= VersionState(D,DepInstall,DepCandMin,DepCandPolicy);
644 }
645
646 // Check target state which can only be current or installed
647 if (State.InstallVer != 0)
648 {
649 DepIterator D = State.InstVerIter(*this).DependsList();
650 State.DepState &= VersionState(D,DepInstall,DepInstMin,DepInstPolicy);
651 }
652 }
653 /*}}}*/
654 // DepCache::Update - Figure out all the state information /*{{{*/
655 // ---------------------------------------------------------------------
656 /* This will figure out the state of all the packages and all the
657 dependencies based on the current policy. */
658 void pkgDepCache::Update(OpProgress * const Prog)
659 {
660 iUsrSize = 0;
661 iDownloadSize = 0;
662 iInstCount = 0;
663 iDelCount = 0;
664 iKeepCount = 0;
665 iBrokenCount = 0;
666 iPolicyBrokenCount = 0;
667 iBadCount = 0;
668
669 // Perform the depends pass
670 int Done = 0;
671 for (PkgIterator I = PkgBegin(); I.end() != true; ++I, ++Done)
672 {
673 if (Prog != 0 && Done%20 == 0)
674 Prog->Progress(Done);
675 for (VerIterator V = I.VersionList(); V.end() != true; ++V)
676 {
677 unsigned char Group = 0;
678
679 for (DepIterator D = V.DependsList(); D.end() != true; ++D)
680 {
681 // Build the dependency state.
682 unsigned char &State = DepState[D->ID];
683 State = DependencyState(D);
684
685 // Add to the group if we are within an or..
686 Group |= State;
687 State |= Group << 3;
688 if ((D->CompareOp & Dep::Or) != Dep::Or)
689 Group = 0;
690
691 // Invert for Conflicts
692 if (D.IsNegative() == true)
693 State = ~State;
694 }
695 }
696
697 // Compute the package dependency state and size additions
698 AddSizes(I);
699 UpdateVerState(I);
700 AddStates(I);
701 }
702
703 if (Prog != 0)
704 Prog->Progress(Done);
705
706 readStateFile(Prog);
707 }
708 /*}}}*/
709 // DepCache::Update - Update the deps list of a package /*{{{*/
710 // ---------------------------------------------------------------------
711 /* This is a helper for update that only does the dep portion of the scan.
712 It is mainly meant to scan reverse dependencies. */
713 void pkgDepCache::Update(DepIterator D)
714 {
715 // Update the reverse deps
716 for (;D.end() != true; ++D)
717 {
718 unsigned char &State = DepState[D->ID];
719 State = DependencyState(D);
720
721 // Invert for Conflicts
722 if (D.IsNegative() == true)
723 State = ~State;
724
725 RemoveStates(D.ParentPkg());
726 BuildGroupOrs(D.ParentVer());
727 UpdateVerState(D.ParentPkg());
728 AddStates(D.ParentPkg());
729 }
730 }
731 /*}}}*/
732 // DepCache::Update - Update the related deps of a package /*{{{*/
733 // ---------------------------------------------------------------------
734 /* This is called whenever the state of a package changes. It updates
735 all cached dependencies related to this package. */
736 void pkgDepCache::Update(PkgIterator const &Pkg)
737 {
738 // Recompute the dep of the package
739 RemoveStates(Pkg);
740 UpdateVerState(Pkg);
741 AddStates(Pkg);
742
743 // Update the reverse deps
744 Update(Pkg.RevDependsList());
745
746 // Update the provides map for the current ver
747 if (Pkg->CurrentVer != 0)
748 for (PrvIterator P = Pkg.CurrentVer().ProvidesList();
749 P.end() != true; ++P)
750 Update(P.ParentPkg().RevDependsList());
751
752 // Update the provides map for the candidate ver
753 if (PkgState[Pkg->ID].CandidateVer != 0)
754 for (PrvIterator P = PkgState[Pkg->ID].CandidateVerIter(*this).ProvidesList();
755 P.end() != true; ++P)
756 Update(P.ParentPkg().RevDependsList());
757 }
758 /*}}}*/
759 // DepCache::MarkKeep - Put the package in the keep state /*{{{*/
760 // ---------------------------------------------------------------------
761 /* */
762 bool pkgDepCache::MarkKeep(PkgIterator const &Pkg, bool Soft, bool FromUser,
763 unsigned long Depth)
764 {
765 if (IsModeChangeOk(ModeKeep, Pkg, Depth, FromUser) == false)
766 return false;
767
768 /* Reject an attempt to keep a non-source broken installed package, those
769 must be upgraded */
770 if (Pkg.State() == PkgIterator::NeedsUnpack &&
771 Pkg.CurrentVer().Downloadable() == false)
772 return false;
773
774 /* We changed the soft state all the time so the UI is a bit nicer
775 to use */
776 StateCache &P = PkgState[Pkg->ID];
777
778 // Check that it is not already kept
779 if (P.Mode == ModeKeep)
780 return true;
781
782 if (Soft == true)
783 P.iFlags |= AutoKept;
784 else
785 P.iFlags &= ~AutoKept;
786
787 ActionGroup group(*this);
788
789 #if 0 // reseting the autoflag here means we lose the
790 // auto-mark information if a user selects a package for removal
791 // but changes his mind then and sets it for keep again
792 // - this makes sense as default when all Garbage dependencies
793 // are automatically marked for removal (as aptitude does).
794 // setting a package for keep then makes it no longer autoinstalled
795 // for all other use-case this action is rather surprising
796 if(FromUser && !P.Marked)
797 P.Flags &= ~Flag::Auto;
798 #endif
799
800 if (DebugMarker == true)
801 std::clog << OutputInDepth(Depth) << "MarkKeep " << APT::PrettyPkg(this, Pkg) << " FU=" << FromUser << std::endl;
802
803 RemoveSizes(Pkg);
804 RemoveStates(Pkg);
805
806 P.Mode = ModeKeep;
807 if (Pkg->CurrentVer == 0)
808 P.InstallVer = 0;
809 else
810 P.InstallVer = Pkg.CurrentVer();
811
812 AddStates(Pkg);
813 Update(Pkg);
814 AddSizes(Pkg);
815
816 return true;
817 }
818 /*}}}*/
819 // DepCache::MarkDelete - Put the package in the delete state /*{{{*/
820 // ---------------------------------------------------------------------
821 /* */
822 bool pkgDepCache::MarkDelete(PkgIterator const &Pkg, bool rPurge,
823 unsigned long Depth, bool FromUser)
824 {
825 if (IsModeChangeOk(ModeDelete, Pkg, Depth, FromUser) == false)
826 return false;
827
828 StateCache &P = PkgState[Pkg->ID];
829
830 // Check that it is not already marked for delete
831 if ((P.Mode == ModeDelete || P.InstallVer == 0) &&
832 (Pkg.Purge() == true || rPurge == false))
833 return true;
834
835 // check if we are allowed to remove the package
836 if (IsDeleteOk(Pkg,rPurge,Depth,FromUser) == false)
837 return false;
838
839 P.iFlags &= ~(AutoKept | Purge);
840 if (rPurge == true)
841 P.iFlags |= Purge;
842
843 ActionGroup group(*this);
844
845 if (FromUser == false)
846 {
847 VerIterator const PV = P.InstVerIter(*this);
848 if (PV.end() == false)
849 {
850 // removed metapackages mark their dependencies as manual to prevent in "desktop depends browser, texteditor"
851 // the removal of browser to suggest the removal of desktop and texteditor.
852 // We ignore the auto-bit here as we can't deal with metapackage cascardes otherwise.
853 // We do not check for or-groups here as we don't know which package takes care of
854 // providing the feature the user likes e.g.: browser1 | browser2 | browser3
855 // Temporary removals are effected by this as well, which is bad, but unlikely in practice
856 bool const PinNeverMarkAutoSection = (PV->Section != 0 && ConfigValueInSubTree("APT::Never-MarkAuto-Sections", PV.Section()));
857 if (PinNeverMarkAutoSection)
858 {
859 for (DepIterator D = PV.DependsList(); D.end() != true; ++D)
860 {
861 if (D.IsMultiArchImplicit() == true || D.IsNegative() == true || IsImportantDep(D) == false)
862 continue;
863
864 pkgCacheFile CacheFile(this);
865 APT::VersionList verlist = APT::VersionList::FromDependency(CacheFile, D, APT::CacheSetHelper::INSTALLED);
866 for (auto const &V : verlist)
867 {
868 PkgIterator const DP = V.ParentPkg();
869 if(DebugAutoInstall == true)
870 std::clog << OutputInDepth(Depth) << "Setting " << DP.FullName(false) << " NOT as auto-installed (direct "
871 << D.DepType() << " of " << Pkg.FullName(false) << " which is in APT::Never-MarkAuto-Sections)" << std::endl;
872
873 MarkAuto(DP, false);
874 }
875 }
876 }
877 }
878 }
879
880 if (DebugMarker == true)
881 std::clog << OutputInDepth(Depth) << (rPurge ? "MarkPurge " : "MarkDelete ") << APT::PrettyPkg(this, Pkg) << " FU=" << FromUser << std::endl;
882
883 RemoveSizes(Pkg);
884 RemoveStates(Pkg);
885
886 if (Pkg->CurrentVer == 0 && (Pkg.Purge() == true || rPurge == false))
887 P.Mode = ModeKeep;
888 else
889 P.Mode = ModeDelete;
890 P.InstallVer = 0;
891
892 AddStates(Pkg);
893 Update(Pkg);
894 AddSizes(Pkg);
895
896 return true;
897 }
898 /*}}}*/
899 // DepCache::IsDeleteOk - check if it is ok to remove this package /*{{{*/
900 // ---------------------------------------------------------------------
901 /* The default implementation tries to prevent deletion of install requests.
902 dpkg holds are enforced by the private IsModeChangeOk */
903 bool pkgDepCache::IsDeleteOk(PkgIterator const &Pkg,bool rPurge,
904 unsigned long Depth, bool FromUser)
905 {
906 return IsDeleteOkProtectInstallRequests(Pkg, rPurge, Depth, FromUser);
907 }
908 bool pkgDepCache::IsDeleteOkProtectInstallRequests(PkgIterator const &Pkg,
909 bool const /*rPurge*/, unsigned long const Depth, bool const FromUser)
910 {
911 if (FromUser == false && Pkg->CurrentVer == 0)
912 {
913 StateCache &P = PkgState[Pkg->ID];
914 if (P.InstallVer != 0 && P.Status == 2 && (P.Flags & Flag::Auto) != Flag::Auto)
915 {
916 if (DebugMarker == true)
917 std::clog << OutputInDepth(Depth) << "Manual install request prevents MarkDelete of " << APT::PrettyPkg(this, Pkg) << std::endl;
918 return false;
919 }
920 }
921 return true;
922 }
923 /*}}}*/
924 // DepCache::IsModeChangeOk - check if it is ok to change the mode /*{{{*/
925 // ---------------------------------------------------------------------
926 /* this is used by all Mark methods on the very first line to check sanity
927 and prevents mode changes for packages on hold for example.
928 If you want to check Mode specific stuff you can use the virtual public
929 Is<Mode>Ok methods instead */
930 static char const* PrintMode(char const mode)
931 {
932 switch (mode)
933 {
934 case pkgDepCache::ModeInstall: return "Install";
935 case pkgDepCache::ModeKeep: return "Keep";
936 case pkgDepCache::ModeDelete: return "Delete";
937 case pkgDepCache::ModeGarbage: return "Garbage";
938 default: return "UNKNOWN";
939 }
940 }
941 bool pkgDepCache::IsModeChangeOk(ModeList const mode, PkgIterator const &Pkg,
942 unsigned long const Depth, bool const FromUser)
943 {
944 // we are not trying to hard…
945 if (unlikely(Depth > 100))
946 return false;
947
948 // general sanity
949 if (unlikely(Pkg.end() == true || Pkg->VersionList == 0))
950 return false;
951
952 // the user is always right
953 if (FromUser == true)
954 return true;
955
956 StateCache &P = PkgState[Pkg->ID];
957 // not changing the mode is obviously also fine as we might want to call
958 // e.g. MarkInstall multiple times with different arguments for the same package
959 if (P.Mode == mode)
960 return true;
961
962 // if previous state was set by user only user can reset it
963 if ((P.iFlags & Protected) == Protected)
964 {
965 if (unlikely(DebugMarker == true))
966 std::clog << OutputInDepth(Depth) << "Ignore Mark" << PrintMode(mode)
967 << " of " << APT::PrettyPkg(this, Pkg) << " as its mode (" << PrintMode(P.Mode)
968 << ") is protected" << std::endl;
969 return false;
970 }
971 // enforce dpkg holds
972 else if (mode != ModeKeep && Pkg->SelectedState == pkgCache::State::Hold &&
973 _config->FindB("APT::Ignore-Hold",false) == false)
974 {
975 if (unlikely(DebugMarker == true))
976 std::clog << OutputInDepth(Depth) << "Hold prevents Mark" << PrintMode(mode)
977 << " of " << APT::PrettyPkg(this, Pkg) << std::endl;
978 return false;
979 }
980
981 return true;
982 }
983 /*}}}*/
984 // DepCache::MarkInstall - Put the package in the install state /*{{{*/
985 // ---------------------------------------------------------------------
986 /* */
987 struct CompareProviders {
988 pkgCache::PkgIterator const Pkg;
989 explicit CompareProviders(pkgCache::DepIterator const &Dep) : Pkg(Dep.TargetPkg()) {};
990 //bool operator() (APT::VersionList::iterator const &AV, APT::VersionList::iterator const &BV)
991 bool operator() (pkgCache::VerIterator const &AV, pkgCache::VerIterator const &BV)
992 {
993 pkgCache::PkgIterator const A = AV.ParentPkg();
994 pkgCache::PkgIterator const B = BV.ParentPkg();
995 // Prefer MA:same packages if other architectures for it are installed
996 if ((AV->MultiArch & pkgCache::Version::Same) == pkgCache::Version::Same ||
997 (BV->MultiArch & pkgCache::Version::Same) == pkgCache::Version::Same)
998 {
999 bool instA = false;
1000 if ((AV->MultiArch & pkgCache::Version::Same) == pkgCache::Version::Same)
1001 {
1002 pkgCache::GrpIterator Grp = A.Group();
1003 for (pkgCache::PkgIterator P = Grp.PackageList(); P.end() == false; P = Grp.NextPkg(P))
1004 if (P->CurrentVer != 0)
1005 {
1006 instA = true;
1007 break;
1008 }
1009 }
1010 bool instB = false;
1011 if ((BV->MultiArch & pkgCache::Version::Same) == pkgCache::Version::Same)
1012 {
1013 pkgCache::GrpIterator Grp = B.Group();
1014 for (pkgCache::PkgIterator P = Grp.PackageList(); P.end() == false; P = Grp.NextPkg(P))
1015 {
1016 if (P->CurrentVer != 0)
1017 {
1018 instB = true;
1019 break;
1020 }
1021 }
1022 }
1023 if (instA != instB)
1024 return instA == false;
1025 }
1026 if ((A->CurrentVer == 0 || B->CurrentVer == 0) && A->CurrentVer != B->CurrentVer)
1027 return A->CurrentVer == 0;
1028 // Prefer packages in the same group as the target; e.g. foo:i386, foo:amd64
1029 if (A->Group != B->Group)
1030 {
1031 if (A->Group == Pkg->Group && B->Group != Pkg->Group)
1032 return false;
1033 else if (B->Group == Pkg->Group && A->Group != Pkg->Group)
1034 return true;
1035 }
1036 // we like essentials
1037 if ((A->Flags & pkgCache::Flag::Essential) != (B->Flags & pkgCache::Flag::Essential))
1038 {
1039 if ((A->Flags & pkgCache::Flag::Essential) == pkgCache::Flag::Essential)
1040 return false;
1041 else if ((B->Flags & pkgCache::Flag::Essential) == pkgCache::Flag::Essential)
1042 return true;
1043 }
1044 if ((A->Flags & pkgCache::Flag::Important) != (B->Flags & pkgCache::Flag::Important))
1045 {
1046 if ((A->Flags & pkgCache::Flag::Important) == pkgCache::Flag::Important)
1047 return false;
1048 else if ((B->Flags & pkgCache::Flag::Important) == pkgCache::Flag::Important)
1049 return true;
1050 }
1051 // prefer native architecture
1052 if (strcmp(A.Arch(), B.Arch()) != 0)
1053 {
1054 if (strcmp(A.Arch(), A.Cache()->NativeArch()) == 0)
1055 return false;
1056 else if (strcmp(B.Arch(), B.Cache()->NativeArch()) == 0)
1057 return true;
1058 std::vector<std::string> archs = APT::Configuration::getArchitectures();
1059 for (std::vector<std::string>::const_iterator a = archs.begin(); a != archs.end(); ++a)
1060 if (*a == A.Arch())
1061 return false;
1062 else if (*a == B.Arch())
1063 return true;
1064 }
1065 // higher priority seems like a good idea
1066 if (AV->Priority != BV->Priority)
1067 return AV->Priority > BV->Priority;
1068 // unable to decide…
1069 return A->ID < B->ID;
1070 }
1071 };
1072 bool pkgDepCache::MarkInstall(PkgIterator const &Pkg,bool AutoInst,
1073 unsigned long Depth, bool FromUser,
1074 bool ForceImportantDeps)
1075 {
1076 if (IsModeChangeOk(ModeInstall, Pkg, Depth, FromUser) == false)
1077 return false;
1078
1079 StateCache &P = PkgState[Pkg->ID];
1080
1081 // See if there is even any possible instalation candidate
1082 if (P.CandidateVer == 0)
1083 return false;
1084
1085 /* Check that it is not already marked for install and that it can be
1086 installed */
1087 if ((P.InstPolicyBroken() == false && P.InstBroken() == false) &&
1088 (P.Mode == ModeInstall ||
1089 P.CandidateVer == (Version *)Pkg.CurrentVer()))
1090 {
1091 if (P.CandidateVer == (Version *)Pkg.CurrentVer() && P.InstallVer == 0)
1092 return MarkKeep(Pkg, false, FromUser, Depth+1);
1093 return true;
1094 }
1095
1096 // check if we are allowed to install the package
1097 if (IsInstallOk(Pkg,AutoInst,Depth,FromUser) == false)
1098 return false;
1099
1100 ActionGroup group(*this);
1101 P.iFlags &= ~AutoKept;
1102
1103 /* Target the candidate version and remove the autoflag. We reset the
1104 autoflag below if this was called recursively. Otherwise the user
1105 should have the ability to de-auto a package by changing its state */
1106 RemoveSizes(Pkg);
1107 RemoveStates(Pkg);
1108
1109 P.Mode = ModeInstall;
1110 P.InstallVer = P.CandidateVer;
1111
1112 if(FromUser)
1113 {
1114 // Set it to manual if it's a new install or already installed,
1115 // but only if its not marked by the autoremover (aptitude depend on this behavior)
1116 // or if we do automatic installation (aptitude never does it)
1117 if(P.Status == 2 || (Pkg->CurrentVer != 0 && (AutoInst == true || P.Marked == false)))
1118 P.Flags &= ~Flag::Auto;
1119 }
1120 else
1121 {
1122 // Set it to auto if this is a new install.
1123 if(P.Status == 2)
1124 P.Flags |= Flag::Auto;
1125 }
1126 if (P.CandidateVer == (Version *)Pkg.CurrentVer())
1127 P.Mode = ModeKeep;
1128
1129 AddStates(Pkg);
1130 Update(Pkg);
1131 AddSizes(Pkg);
1132
1133 if (AutoInst == false || _config->Find("APT::Solver", "internal") != "internal")
1134 return true;
1135
1136 if (DebugMarker == true)
1137 std::clog << OutputInDepth(Depth) << "MarkInstall " << APT::PrettyPkg(this, Pkg) << " FU=" << FromUser << std::endl;
1138
1139 bool MoveAutoBitToDependencies = false;
1140 VerIterator const PV = P.InstVerIter(*this);
1141 if (unlikely(PV.end() == true))
1142 return false;
1143 else if (PV->Section != 0 && (P.Flags & Flag::Auto) != Flag::Auto)
1144 {
1145 VerIterator const CurVer = Pkg.CurrentVer();
1146 if (CurVer.end() == false && CurVer->Section != 0 && strcmp(CurVer.Section(), PV.Section()) != 0)
1147 {
1148 bool const CurVerInMoveSection = ConfigValueInSubTree("APT::Move-Autobit-Sections", CurVer.Section());
1149 bool const InstVerInMoveSection = ConfigValueInSubTree("APT::Move-Autobit-Sections", PV.Section());
1150 MoveAutoBitToDependencies = (CurVerInMoveSection == false && InstVerInMoveSection == true);
1151 if (MoveAutoBitToDependencies == true)
1152 {
1153 if(DebugAutoInstall == true)
1154 std::clog << OutputInDepth(Depth) << "Setting " << Pkg.FullName(false) << " as auto-installed, moving manual to its dependencies" << std::endl;
1155 MarkAuto(Pkg, true);
1156 }
1157 }
1158 }
1159
1160 DepIterator Dep = PV.DependsList();
1161 for (; Dep.end() != true;)
1162 {
1163 // Grok or groups
1164 DepIterator Start = Dep;
1165 bool Result = true;
1166 unsigned Ors = 0;
1167 for (bool LastOR = true; Dep.end() == false && LastOR == true; ++Dep, ++Ors)
1168 {
1169 LastOR = (Dep->CompareOp & Dep::Or) == Dep::Or;
1170
1171 if ((DepState[Dep->ID] & DepInstall) == DepInstall)
1172 Result = false;
1173 }
1174
1175 // Dep is satisfied okay.
1176 if (Result == false)
1177 continue;
1178
1179 /* Check if this dep should be consider for install. If it is a user
1180 defined important dep and we are installed a new package then
1181 it will be installed. Otherwise we only check for important
1182 deps that have changed from the installed version */
1183 if (IsImportantDep(Start) == false)
1184 continue;
1185
1186 /* If we are in an or group locate the first or that can
1187 succeed. We have already cached this… */
1188 for (; Ors > 1 && (DepState[Start->ID] & DepCVer) != DepCVer; --Ors)
1189 ++Start;
1190
1191 /* unsatisfiable dependency: IsInstallOkDependenciesSatisfiableByCandidates
1192 would have prevented us to get here if not overridden, so just skip
1193 over the problem here as the front-end will know what it is doing */
1194 if (Ors == 1 && (DepState[Start->ID] &DepCVer) != DepCVer && Start.IsNegative() == false)
1195 continue;
1196
1197 /* Check if any ImportantDep() (but not Critical) were added
1198 * since we installed the package. Also check for deps that
1199 * were satisfied in the past: for instance, if a version
1200 * restriction in a Recommends was tightened, upgrading the
1201 * package should follow that Recommends rather than causing the
1202 * dependency to be removed. (bug #470115)
1203 */
1204 if (Pkg->CurrentVer != 0 && ForceImportantDeps == false && Start.IsCritical() == false)
1205 {
1206 bool isNewImportantDep = true;
1207 bool isPreviouslySatisfiedImportantDep = false;
1208 for (DepIterator D = Pkg.CurrentVer().DependsList(); D.end() != true; ++D)
1209 {
1210 //FIXME: Should we handle or-group better here?
1211 // We do not check if the package we look for is part of the same or-group
1212 // we might find while searching, but could that really be a problem?
1213 if (D.IsCritical() == true || IsImportantDep(D) == false ||
1214 Start.TargetPkg() != D.TargetPkg())
1215 continue;
1216
1217 isNewImportantDep = false;
1218
1219 while ((D->CompareOp & Dep::Or) != 0)
1220 ++D;
1221
1222 isPreviouslySatisfiedImportantDep = (((*this)[D] & DepGNow) != 0);
1223 if (isPreviouslySatisfiedImportantDep == true)
1224 break;
1225 }
1226
1227 if(isNewImportantDep == true)
1228 {
1229 if (DebugAutoInstall == true)
1230 std::clog << OutputInDepth(Depth) << "new important dependency: "
1231 << Start.TargetPkg().FullName() << std::endl;
1232 }
1233 else if(isPreviouslySatisfiedImportantDep == true)
1234 {
1235 if (DebugAutoInstall == true)
1236 std::clog << OutputInDepth(Depth) << "previously satisfied important dependency on "
1237 << Start.TargetPkg().FullName() << std::endl;
1238 }
1239 else
1240 {
1241 if (DebugAutoInstall == true)
1242 std::clog << OutputInDepth(Depth) << "ignore old unsatisfied important dependency on "
1243 << Start.TargetPkg().FullName() << std::endl;
1244 continue;
1245 }
1246 }
1247
1248 /* This bit is for processing the possibility of an install/upgrade
1249 fixing the problem for "positive" dependencies */
1250 if (Start.IsNegative() == false && (DepState[Start->ID] & DepCVer) == DepCVer)
1251 {
1252 pkgCacheFile CacheFile(this);
1253 APT::VersionList verlist = APT::VersionList::FromDependency(CacheFile, Start, APT::CacheSetHelper::CANDIDATE);
1254 CompareProviders comp(Start);
1255
1256 do {
1257 APT::VersionList::iterator InstVer = std::max_element(verlist.begin(), verlist.end(), comp);
1258
1259 if (InstVer == verlist.end())
1260 break;
1261
1262 pkgCache::PkgIterator InstPkg = InstVer.ParentPkg();
1263 if(DebugAutoInstall == true)
1264 std::clog << OutputInDepth(Depth) << "Installing " << InstPkg.Name()
1265 << " as " << Start.DepType() << " of " << Pkg.Name()
1266 << std::endl;
1267 if (MarkInstall(InstPkg, true, Depth + 1, false, ForceImportantDeps) == false)
1268 {
1269 verlist.erase(InstVer);
1270 continue;
1271 }
1272
1273 // now check if we should consider it a automatic dependency or not
1274 if(InstPkg->CurrentVer == 0 && MoveAutoBitToDependencies)
1275 {
1276 if(DebugAutoInstall == true)
1277 std::clog << OutputInDepth(Depth) << "Setting " << InstPkg.FullName(false) << " NOT as auto-installed (direct "
1278 << Start.DepType() << " of " << Pkg.FullName(false) << " which is manual and in APT::Move-Autobit-Sections)" << std::endl;
1279 MarkAuto(InstPkg, false);
1280 }
1281
1282
1283 break;
1284 } while(true);
1285 continue;
1286 }
1287 /* Negative dependencies have no or-group
1288 If the dependency isn't versioned, we try if an upgrade might solve the problem.
1289 Otherwise we remove the offender if needed */
1290 else if (Start.IsNegative() == true && Start->Type != pkgCache::Dep::Obsoletes)
1291 {
1292 std::unique_ptr<Version *[]> List(Start.AllTargets());
1293 pkgCache::PkgIterator TrgPkg = Start.TargetPkg();
1294 for (Version **I = List.get(); *I != 0; I++)
1295 {
1296 VerIterator Ver(*this,*I);
1297 PkgIterator Pkg = Ver.ParentPkg();
1298
1299 /* The List includes all packages providing this dependency,
1300 even providers which are not installed, so skip them. */
1301 if (PkgState[Pkg->ID].InstallVer == 0)
1302 continue;
1303
1304 /* Ignore negative dependencies that we are not going to
1305 get installed */
1306 if (PkgState[Pkg->ID].InstallVer != *I)
1307 continue;
1308
1309 if ((Start->Version != 0 || TrgPkg != Pkg) &&
1310 PkgState[Pkg->ID].CandidateVer != PkgState[Pkg->ID].InstallVer &&
1311 PkgState[Pkg->ID].CandidateVer != *I &&
1312 MarkInstall(Pkg,true,Depth + 1, false, ForceImportantDeps) == true)
1313 continue;
1314 else if (Start->Type == pkgCache::Dep::Conflicts ||
1315 Start->Type == pkgCache::Dep::DpkgBreaks)
1316 {
1317 if(DebugAutoInstall == true)
1318 std::clog << OutputInDepth(Depth)
1319 << " Removing: " << Pkg.Name()
1320 << std::endl;
1321 if (MarkDelete(Pkg,false,Depth + 1, false) == false)
1322 break;
1323 }
1324 }
1325 continue;
1326 }
1327 }
1328
1329 return Dep.end() == true;
1330 }
1331 /*}}}*/
1332 // DepCache::IsInstallOk - check if it is ok to install this package /*{{{*/
1333 // ---------------------------------------------------------------------
1334 /* The default implementation checks if the installation of an M-A:same
1335 package would lead us into a version-screw and if so forbids it.
1336 dpkg holds are enforced by the private IsModeChangeOk */
1337 bool pkgDepCache::IsInstallOk(PkgIterator const &Pkg,bool AutoInst,
1338 unsigned long Depth, bool FromUser)
1339 {
1340 return IsInstallOkMultiArchSameVersionSynced(Pkg,AutoInst, Depth, FromUser) &&
1341 IsInstallOkDependenciesSatisfiableByCandidates(Pkg,AutoInst, Depth, FromUser);
1342 }
1343 bool pkgDepCache::IsInstallOkMultiArchSameVersionSynced(PkgIterator const &Pkg,
1344 bool const /*AutoInst*/, unsigned long const Depth, bool const FromUser)
1345 {
1346 if (FromUser == true) // as always: user is always right
1347 return true;
1348
1349 // if we have checked before and it was okay, it will still be okay
1350 if (PkgState[Pkg->ID].Mode == ModeInstall &&
1351 PkgState[Pkg->ID].InstallVer == PkgState[Pkg->ID].CandidateVer)
1352 return true;
1353
1354 // ignore packages with none-M-A:same candidates
1355 VerIterator const CandVer = PkgState[Pkg->ID].CandidateVerIter(*this);
1356 if (unlikely(CandVer.end() == true) || CandVer == Pkg.CurrentVer() ||
1357 (CandVer->MultiArch & pkgCache::Version::Same) != pkgCache::Version::Same)
1358 return true;
1359
1360 GrpIterator const Grp = Pkg.Group();
1361 for (PkgIterator P = Grp.PackageList(); P.end() == false; P = Grp.NextPkg(P))
1362 {
1363 // not installed or self-check: fine by definition
1364 if (P->CurrentVer == 0 || P == Pkg)
1365 continue;
1366
1367 // not having a candidate or being in sync
1368 // (simple string-compare as stuff like '1' == '0:1-0' can't happen here)
1369 VerIterator CV = PkgState[P->ID].CandidateVerIter(*this);
1370 if (CV.end() == true || strcmp(CandVer.VerStr(), CV.VerStr()) == 0)
1371 continue;
1372
1373 // packages losing M-A:same can be out-of-sync
1374 if ((CV->MultiArch & pkgCache::Version::Same) != pkgCache::Version::Same)
1375 continue;
1376
1377 // not downloadable means the package is obsolete, so allow out-of-sync
1378 if (CV.Downloadable() == false)
1379 continue;
1380
1381 PkgState[Pkg->ID].iFlags |= AutoKept;
1382 if (unlikely(DebugMarker == true))
1383 std::clog << OutputInDepth(Depth) << "Ignore MarkInstall of " << APT::PrettyPkg(this, Pkg)
1384 << " as it is not in sync with its M-A:same sibling " << APT::PrettyPkg(this, P)
1385 << " (" << CandVer.VerStr() << " != " << CV.VerStr() << ")" << std::endl;
1386 return false;
1387 }
1388
1389 return true;
1390 }
1391 bool pkgDepCache::IsInstallOkDependenciesSatisfiableByCandidates(PkgIterator const &Pkg,
1392 bool const AutoInst, unsigned long const Depth, bool const /*FromUser*/)
1393 {
1394 if (AutoInst == false)
1395 return true;
1396
1397 VerIterator const CandVer = PkgState[Pkg->ID].CandidateVerIter(*this);
1398 if (unlikely(CandVer.end() == true) || CandVer == Pkg.CurrentVer())
1399 return true;
1400
1401 for (DepIterator Dep = CandVer.DependsList(); Dep.end() != true;)
1402 {
1403 // Grok or groups
1404 DepIterator Start = Dep;
1405 bool Result = true;
1406 unsigned Ors = 0;
1407 for (bool LastOR = true; Dep.end() == false && LastOR == true; ++Dep, ++Ors)
1408 {
1409 LastOR = (Dep->CompareOp & Dep::Or) == Dep::Or;
1410
1411 if ((DepState[Dep->ID] & DepInstall) == DepInstall)
1412 Result = false;
1413 }
1414
1415 if (Start.IsCritical() == false || Start.IsNegative() == true || Result == false)
1416 continue;
1417
1418 /* If we are in an or group locate the first or that can succeed.
1419 We have already cached this… */
1420 for (; Ors > 1 && (DepState[Start->ID] & DepCVer) != DepCVer; --Ors)
1421 ++Start;
1422
1423 if (Ors == 1 && (DepState[Start->ID] &DepCVer) != DepCVer)
1424 {
1425 if (DebugAutoInstall == true)
1426 std::clog << OutputInDepth(Depth) << APT::PrettyDep(this, Start) << " can't be satisfied!" << std::endl;
1427
1428 // the dependency is critical, but can't be installed, so discard the candidate
1429 // as the problemresolver will trip over it otherwise trying to install it (#735967)
1430 if (Pkg->CurrentVer != 0 && (PkgState[Pkg->ID].iFlags & Protected) != Protected)
1431 {
1432 SetCandidateVersion(Pkg.CurrentVer());
1433 StateCache &State = PkgState[Pkg->ID];
1434 if (State.Mode != ModeDelete)
1435 {
1436 State.Mode = ModeKeep;
1437 State.Update(Pkg, *this);
1438 }
1439 }
1440 return false;
1441 }
1442 }
1443
1444 return true;
1445 }
1446 /*}}}*/
1447 // DepCache::SetReInstall - Set the reinstallation flag /*{{{*/
1448 // ---------------------------------------------------------------------
1449 /* */
1450 void pkgDepCache::SetReInstall(PkgIterator const &Pkg,bool To)
1451 {
1452 if (unlikely(Pkg.end() == true))
1453 return;
1454
1455 APT::PackageList pkglist;
1456 if (Pkg->CurrentVer != 0 &&
1457 (Pkg.CurrentVer()-> MultiArch & pkgCache::Version::Same) == pkgCache::Version::Same)
1458 {
1459 pkgCache::GrpIterator Grp = Pkg.Group();
1460 for (pkgCache::PkgIterator P = Grp.PackageList(); P.end() == false; P = Grp.NextPkg(P))
1461 {
1462 if (P->CurrentVer != 0)
1463 pkglist.insert(P);
1464 }
1465 }
1466 else
1467 pkglist.insert(Pkg);
1468
1469 ActionGroup group(*this);
1470
1471 for (APT::PackageList::const_iterator Pkg = pkglist.begin(); Pkg != pkglist.end(); ++Pkg)
1472 {
1473 RemoveSizes(Pkg);
1474 RemoveStates(Pkg);
1475
1476 StateCache &P = PkgState[Pkg->ID];
1477 if (To == true)
1478 P.iFlags |= ReInstall;
1479 else
1480 P.iFlags &= ~ReInstall;
1481
1482 AddStates(Pkg);
1483 AddSizes(Pkg);
1484 }
1485 }
1486 /*}}}*/
1487 pkgCache::VerIterator pkgDepCache::GetCandidateVersion(PkgIterator const &Pkg)/*{{{*/
1488 {
1489 return PkgState[Pkg->ID].CandidateVerIter(*this);
1490 }
1491 /*}}}*/
1492 // DepCache::SetCandidateVersion - Change the candidate version /*{{{*/
1493 // ---------------------------------------------------------------------
1494 /* */
1495 void pkgDepCache::SetCandidateVersion(VerIterator TargetVer)
1496 {
1497 pkgCache::PkgIterator Pkg = TargetVer.ParentPkg();
1498 StateCache &P = PkgState[Pkg->ID];
1499
1500 if (P.CandidateVer == TargetVer)
1501 return;
1502
1503 ActionGroup group(*this);
1504
1505 RemoveSizes(Pkg);
1506 RemoveStates(Pkg);
1507
1508 if (P.CandidateVer == P.InstallVer && P.Install() == true)
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);
1516
1517 }
1518 /*}}}*/
1519 // DepCache::SetCandidateRelease - Change the candidate version /*{{{*/
1520 // ---------------------------------------------------------------------
1521 /* changes the candidate of a package and walks over all its dependencies
1522 to check if it needs to change the candidate of the dependency, too,
1523 to reach a installable versionstate */
1524 bool pkgDepCache::SetCandidateRelease(pkgCache::VerIterator TargetVer,
1525 std::string const &TargetRel)
1526 {
1527 std::list<std::pair<pkgCache::VerIterator, pkgCache::VerIterator> > Changed;
1528 return SetCandidateRelease(TargetVer, TargetRel, Changed);
1529 }
1530 bool pkgDepCache::SetCandidateRelease(pkgCache::VerIterator TargetVer,
1531 std::string const &TargetRel,
1532 std::list<std::pair<pkgCache::VerIterator, pkgCache::VerIterator> > &Changed)
1533 {
1534 ActionGroup group(*this);
1535 SetCandidateVersion(TargetVer);
1536
1537 if (TargetRel == "installed" || TargetRel == "candidate") // both doesn't make sense in this context
1538 return true;
1539
1540 pkgVersionMatch Match(TargetRel, pkgVersionMatch::Release);
1541 // save the position of the last element we will not undo - if we have to
1542 std::list<std::pair<pkgCache::VerIterator, pkgCache::VerIterator> >::iterator newChanged = --(Changed.end());
1543
1544 for (pkgCache::DepIterator D = TargetVer.DependsList(); D.end() == false; ++D)
1545 {
1546 if (D->Type != pkgCache::Dep::PreDepends && D->Type != pkgCache::Dep::Depends &&
1547 ((D->Type != pkgCache::Dep::Recommends && D->Type != pkgCache::Dep::Suggests) ||
1548 IsImportantDep(D) == false))
1549 continue;
1550
1551 // walk over an or-group and check if we need to do anything
1552 // for simpilicity no or-group is handled as a or-group including one dependency
1553 pkgCache::DepIterator Start = D;
1554 bool itsFine = false;
1555 for (bool stillOr = true; stillOr == true; ++Start)
1556 {
1557 stillOr = (Start->CompareOp & Dep::Or) == Dep::Or;
1558 pkgCache::PkgIterator const P = Start.TargetPkg();
1559 // virtual packages can't be a solution
1560 if (P.end() == true || (P->ProvidesList == 0 && P->VersionList == 0))
1561 continue;
1562 pkgCache::VerIterator const Cand = PkgState[P->ID].CandidateVerIter(*this);
1563 // no versioned dependency - but is it installable?
1564 if (Start.TargetVer() == 0 || Start.TargetVer()[0] == '\0')
1565 {
1566 // Check if one of the providers is installable
1567 if (P->ProvidesList != 0)
1568 {
1569 pkgCache::PrvIterator Prv = P.ProvidesList();
1570 for (; Prv.end() == false; ++Prv)
1571 {
1572 pkgCache::VerIterator const C = PkgState[Prv.OwnerPkg()->ID].CandidateVerIter(*this);
1573 if (C.end() == true || C != Prv.OwnerVer() ||
1574 (VersionState(C.DependsList(), DepInstall, DepCandMin, DepCandPolicy) & DepCandMin) != DepCandMin)
1575 continue;
1576 break;
1577 }
1578 if (Prv.end() == true)
1579 continue;
1580 }
1581 // no providers, so check if we have an installable candidate version
1582 else if (Cand.end() == true ||
1583 (VersionState(Cand.DependsList(), DepInstall, DepCandMin, DepCandPolicy) & DepCandMin) != DepCandMin)
1584 continue;
1585 itsFine = true;
1586 break;
1587 }
1588 if (Cand.end() == true)
1589 continue;
1590 // check if the current candidate is enough for the versioned dependency - and installable?
1591 if (Start.IsSatisfied(Cand) == true &&
1592 (VersionState(Cand.DependsList(), DepInstall, DepCandMin, DepCandPolicy) & DepCandMin) == DepCandMin)
1593 {
1594 itsFine = true;
1595 break;
1596 }
1597 }
1598
1599 if (itsFine == true) {
1600 // something in the or-group was fine, skip all other members
1601 for (; (D->CompareOp & Dep::Or) == Dep::Or; ++D);
1602 continue;
1603 }
1604
1605 // walk again over the or-group and check each if a candidate switch would help
1606 itsFine = false;
1607 for (bool stillOr = true; stillOr == true; ++D)
1608 {
1609 stillOr = (D->CompareOp & Dep::Or) == Dep::Or;
1610 // changing candidate will not help if the dependency is not versioned
1611 if (D.TargetVer() == 0 || D.TargetVer()[0] == '\0')
1612 {
1613 if (stillOr == true)
1614 continue;
1615 break;
1616 }
1617
1618 pkgCache::VerIterator V;
1619 if (TargetRel == "newest")
1620 V = D.TargetPkg().VersionList();
1621 else
1622 V = Match.Find(D.TargetPkg());
1623
1624 // check if the version from this release could satisfy the dependency
1625 if (V.end() == true || D.IsSatisfied(V) == false)
1626 {
1627 if (stillOr == true)
1628 continue;
1629 break;
1630 }
1631
1632 pkgCache::VerIterator oldCand = PkgState[D.TargetPkg()->ID].CandidateVerIter(*this);
1633 if (V == oldCand)
1634 {
1635 // Do we already touched this Version? If so, their versioned dependencies are okay, no need to check again
1636 for (std::list<std::pair<pkgCache::VerIterator, pkgCache::VerIterator> >::const_iterator c = Changed.begin();
1637 c != Changed.end(); ++c)
1638 {
1639 if (c->first->ParentPkg != V->ParentPkg)
1640 continue;
1641 itsFine = true;
1642 break;
1643 }
1644 }
1645
1646 if (itsFine == false)
1647 {
1648 // change the candidate
1649 Changed.push_back(make_pair(V, TargetVer));
1650 if (SetCandidateRelease(V, TargetRel, Changed) == false)
1651 {
1652 if (stillOr == false)
1653 break;
1654 // undo the candidate changing
1655 SetCandidateVersion(oldCand);
1656 Changed.pop_back();
1657 continue;
1658 }
1659 itsFine = true;
1660 }
1661
1662 // something in the or-group was fine, skip all other members
1663 for (; (D->CompareOp & Dep::Or) == Dep::Or; ++D);
1664 break;
1665 }
1666
1667 if (itsFine == false && (D->Type == pkgCache::Dep::PreDepends || D->Type == pkgCache::Dep::Depends))
1668 {
1669 // undo all changes which aren't lead to a solution
1670 for (std::list<std::pair<pkgCache::VerIterator, pkgCache::VerIterator> >::const_iterator c = ++newChanged;
1671 c != Changed.end(); ++c)
1672 SetCandidateVersion(c->first);
1673 Changed.erase(newChanged, Changed.end());
1674 return false;
1675 }
1676 }
1677 return true;
1678 }
1679 /*}}}*/
1680 // DepCache::MarkAuto - set the Auto flag for a package /*{{{*/
1681 // ---------------------------------------------------------------------
1682 /* */
1683 void pkgDepCache::MarkAuto(const PkgIterator &Pkg, bool Auto)
1684 {
1685 StateCache &state = PkgState[Pkg->ID];
1686
1687 ActionGroup group(*this);
1688
1689 if(Auto)
1690 state.Flags |= Flag::Auto;
1691 else
1692 state.Flags &= ~Flag::Auto;
1693 }
1694 /*}}}*/
1695 // StateCache::Update - Compute the various static display things /*{{{*/
1696 // ---------------------------------------------------------------------
1697 /* This is called whenever the Candidate version changes. */
1698 void pkgDepCache::StateCache::Update(PkgIterator Pkg,pkgCache &Cache)
1699 {
1700 // Some info
1701 VerIterator Ver = CandidateVerIter(Cache);
1702
1703 // Use a null string or the version string
1704 if (Ver.end() == true)
1705 CandVersion = "";
1706 else
1707 CandVersion = Ver.VerStr();
1708
1709 // Find the current version
1710 CurVersion = "";
1711 if (Pkg->CurrentVer != 0)
1712 CurVersion = Pkg.CurrentVer().VerStr();
1713
1714 // Figure out if its up or down or equal
1715 Status = Ver.CompareVer(Pkg.CurrentVer());
1716 if (Pkg->CurrentVer == 0 || Pkg->VersionList == 0 || CandidateVer == 0)
1717 Status = 2;
1718 }
1719 /*}}}*/
1720 // StateCache::StripEpoch - Remove the epoch specifier from the version /*{{{*/
1721 // ---------------------------------------------------------------------
1722 /* */
1723 const char *pkgDepCache::StateCache::StripEpoch(const char *Ver)
1724 {
1725 if (Ver == 0)
1726 return 0;
1727
1728 // Strip any epoch
1729 char const * const I = strchr(Ver, ':');
1730 if (I == nullptr)
1731 return Ver;
1732 return I + 1;
1733 }
1734 /*}}}*/
1735 // Policy::GetCandidateVer - Returns the Candidate install version /*{{{*/
1736 // ---------------------------------------------------------------------
1737 /* The default just returns the highest available version that is not
1738 a source and automatic. */
1739 pkgCache::VerIterator pkgDepCache::Policy::GetCandidateVer(PkgIterator const &Pkg)
1740 {
1741 /* Not source/not automatic versions cannot be a candidate version
1742 unless they are already installed */
1743 VerIterator Last;
1744
1745 for (VerIterator I = Pkg.VersionList(); I.end() == false; ++I)
1746 {
1747 if (Pkg.CurrentVer() == I)
1748 return I;
1749
1750 for (VerFileIterator J = I.FileList(); J.end() == false; ++J)
1751 {
1752 if (J.File().Flagged(Flag::NotSource))
1753 continue;
1754
1755 /* Stash the highest version of a not-automatic source, we use it
1756 if there is nothing better */
1757 if (J.File().Flagged(Flag::NotAutomatic) ||
1758 J.File().Flagged(Flag::ButAutomaticUpgrades))
1759 {
1760 if (Last.end() == true)
1761 Last = I;
1762 continue;
1763 }
1764
1765 return I;
1766 }
1767 }
1768
1769 return Last;
1770 }
1771 /*}}}*/
1772 // Policy::IsImportantDep - True if the dependency is important /*{{{*/
1773 // ---------------------------------------------------------------------
1774 /* */
1775 bool pkgDepCache::Policy::IsImportantDep(DepIterator const &Dep) const
1776 {
1777 if(Dep.IsCritical())
1778 return true;
1779 else if(Dep->Type == pkgCache::Dep::Recommends)
1780 {
1781 if (InstallRecommends)
1782 return true;
1783 // we suport a special mode to only install-recommends for certain
1784 // sections
1785 // FIXME: this is a meant as a temporarly solution until the
1786 // recommends are cleaned up
1787 const char *sec = Dep.ParentVer().Section();
1788 if (sec && ConfigValueInSubTree("APT::Install-Recommends-Sections", sec))
1789 return true;
1790 }
1791 else if(Dep->Type == pkgCache::Dep::Suggests)
1792 return InstallSuggests;
1793
1794 return false;
1795 }
1796 /*}}}*/
1797 // Policy::GetPriority - Get the priority of the package pin /*{{{*/
1798 APT_CONST signed short pkgDepCache::Policy::GetPriority(pkgCache::PkgIterator const &/*Pkg*/)
1799 { return 0; }
1800 APT_CONST signed short pkgDepCache::Policy::GetPriority(pkgCache::VerIterator const &/*Ver*/, bool /*ConsiderFiles*/)
1801 { return 0; }
1802 APT_CONST signed short pkgDepCache::Policy::GetPriority(pkgCache::PkgFileIterator const &/*File*/)
1803 { return 0; }
1804 /*}}}*/
1805 pkgDepCache::InRootSetFunc *pkgDepCache::GetRootSetFunc() /*{{{*/
1806 {
1807 DefaultRootSetFunc *f = new DefaultRootSetFunc;
1808 if(f->wasConstructedSuccessfully())
1809 return f;
1810 else
1811 {
1812 delete f;
1813 return NULL;
1814 }
1815 }
1816 /*}}}*/
1817 bool pkgDepCache::MarkFollowsRecommends()
1818 {
1819 return _config->FindB("APT::AutoRemove::RecommendsImportant", true);
1820 }
1821
1822 bool pkgDepCache::MarkFollowsSuggests()
1823 {
1824 return _config->FindB("APT::AutoRemove::SuggestsImportant", true);
1825 }
1826
1827 // pkgDepCache::MarkRequired - the main mark algorithm /*{{{*/
1828 static bool IsPkgInBoringState(pkgCache::PkgIterator const &Pkg, pkgDepCache::StateCache const * const PkgState)
1829 {
1830 if (Pkg->CurrentVer == 0)
1831 {
1832 if (PkgState[Pkg->ID].Keep())
1833 return true;
1834 }
1835 else
1836 {
1837 if (PkgState[Pkg->ID].Delete())
1838 return true;
1839 }
1840 return false;
1841 }
1842 bool pkgDepCache::MarkRequired(InRootSetFunc &userFunc)
1843 {
1844 if (_config->Find("APT::Solver", "internal") != "internal")
1845 return true;
1846
1847 bool const debug_autoremove = _config->FindB("Debug::pkgAutoRemove",false);
1848
1849 // init the states
1850 auto const PackagesCount = Head().PackageCount;
1851 for(auto i = decltype(PackagesCount){0}; i < PackagesCount; ++i)
1852 {
1853 PkgState[i].Marked = false;
1854 PkgState[i].Garbage = false;
1855 }
1856 if (debug_autoremove)
1857 for(PkgIterator p = PkgBegin(); !p.end(); ++p)
1858 if(PkgState[p->ID].Flags & Flag::Auto)
1859 std::clog << "AutoDep: " << p.FullName() << std::endl;
1860
1861 bool const follow_recommends = MarkFollowsRecommends();
1862 bool const follow_suggests = MarkFollowsSuggests();
1863
1864 // do the mark part, this is the core bit of the algorithm
1865 for (PkgIterator P = PkgBegin(); !P.end(); ++P)
1866 {
1867 if (PkgState[P->ID].Marked || IsPkgInBoringState(P, PkgState))
1868 continue;
1869
1870 if ((PkgState[P->ID].Flags & Flag::Auto) == 0)
1871 ;
1872 else if ((P->Flags & Flag::Essential) || (P->Flags & Flag::Important))
1873 ;
1874 // be nice even then a required package violates the policy (#583517)
1875 // and do the full mark process also for required packages
1876 else if (P->CurrentVer != 0 && P.CurrentVer()->Priority == pkgCache::State::Required)
1877 ;
1878 else if (userFunc.InRootSet(P))
1879 ;
1880 // packages which can't be changed (like holds) can't be garbage
1881 else if (IsModeChangeOk(ModeGarbage, P, 0, false) == false)
1882 ;
1883 else
1884 continue;
1885
1886 if (PkgState[P->ID].Install())
1887 MarkPackage(P, PkgState[P->ID].InstVerIter(*this),
1888 follow_recommends, follow_suggests);
1889 else
1890 MarkPackage(P, P.CurrentVer(),
1891 follow_recommends, follow_suggests);
1892 }
1893
1894 return true;
1895 }
1896 /*}}}*/
1897 // MarkPackage - mark a single package in Mark-and-Sweep /*{{{*/
1898 void pkgDepCache::MarkPackage(const pkgCache::PkgIterator &Pkg,
1899 const pkgCache::VerIterator &Ver,
1900 bool const &follow_recommends,
1901 bool const &follow_suggests)
1902 {
1903 {
1904 pkgDepCache::StateCache &state = PkgState[Pkg->ID];
1905 // if we are marked already we are done
1906 if(state.Marked || unlikely(Ver.end()))
1907 return;
1908 state.Marked=true;
1909 }
1910
1911 if (IsPkgInBoringState(Pkg, PkgState))
1912 return;
1913
1914 bool const debug_autoremove = _config->FindB("Debug::pkgAutoRemove", false);
1915 if(debug_autoremove)
1916 std::clog << "Marking: " << Pkg.FullName() << " " << Ver.VerStr() << std::endl;
1917
1918 for (auto D = Ver.DependsList(); D.end() == false; ++D)
1919 {
1920 auto const T = D.TargetPkg();
1921 if (PkgState[T->ID].Marked)
1922 continue;
1923
1924 if (D->Type != Dep::Depends &&
1925 D->Type != Dep::PreDepends &&
1926 (follow_recommends == false || D->Type != Dep::Recommends) &&
1927 (follow_suggests == false || D->Type != Dep::Suggests))
1928 continue;
1929
1930 // handle the virtual part first
1931 APT::VersionVector providers;
1932 for(auto Prv = T.ProvidesList(); Prv.end() == false; ++Prv)
1933 {
1934 auto PP = Prv.OwnerPkg();
1935 if (IsPkgInBoringState(PP, PkgState))
1936 continue;
1937
1938 // we want to ignore provides from uninteresting versions
1939 auto const PV = (PkgState[PP->ID].Install()) ?
1940 PkgState[PP->ID].InstVerIter(*this) : PP.CurrentVer();
1941 if (unlikely(PV.end()) || PV != Prv.OwnerVer() || D.IsSatisfied(Prv) == false)
1942 continue;
1943
1944 providers.emplace_back(PV);
1945 }
1946 if (providers.empty() == false)
1947 {
1948 // sort providers by source version so that only the latest versioned
1949 // binary package of a source package is marked instead of all
1950 std::sort(providers.begin(), providers.end(),
1951 [](pkgCache::VerIterator const &A, pkgCache::VerIterator const &B) {
1952 auto const nameret = strcmp(A.SourcePkgName(), B.SourcePkgName());
1953 if (nameret != 0)
1954 return nameret < 0;
1955 auto const verret = A.Cache()->VS->CmpVersion(A.SourceVerStr(), B.SourceVerStr());
1956 if (verret != 0)
1957 return verret > 0;
1958 return strcmp(A.ParentPkg().Name(), B.ParentPkg().Name()) < 0;
1959 });
1960 auto const prvsize = providers.size();
1961 providers.erase(std::unique(providers.begin(), providers.end(),
1962 [](pkgCache::VerIterator const &A, pkgCache::VerIterator const &B) {
1963 return strcmp(A.SourcePkgName(), B.SourcePkgName()) == 0 &&
1964 strcmp(A.SourceVerStr(), B.SourceVerStr()) != 0;
1965 }), providers.end());
1966 for (auto && PV: providers)
1967 {
1968 auto const PP = PV.ParentPkg();
1969 if (debug_autoremove)
1970 std::clog << "Following dep: " << APT::PrettyDep(this, D)
1971 << ", provided by " << PP.FullName() << " " << PV.VerStr()
1972 << " (" << providers.size() << "/" << prvsize << ")"<< std::endl;
1973 MarkPackage(PP, PV, follow_recommends, follow_suggests);
1974 }
1975 }
1976
1977 // now deal with the real part of the package
1978 if (IsPkgInBoringState(T, PkgState))
1979 continue;
1980
1981 auto const TV = (PkgState[T->ID].Install()) ?
1982 PkgState[T->ID].InstVerIter(*this) : T.CurrentVer();
1983 if (unlikely(TV.end()) || D.IsSatisfied(TV) == false)
1984 continue;
1985
1986 if (debug_autoremove)
1987 std::clog << "Following dep: " << APT::PrettyDep(this, D) << std::endl;
1988 MarkPackage(T, TV, follow_recommends, follow_suggests);
1989 }
1990 }
1991 /*}}}*/
1992 bool pkgDepCache::Sweep() /*{{{*/
1993 {
1994 bool debug_autoremove = _config->FindB("Debug::pkgAutoRemove",false);
1995
1996 // do the sweep
1997 for(PkgIterator p=PkgBegin(); !p.end(); ++p)
1998 {
1999 StateCache &state=PkgState[p->ID];
2000
2001 // skip required packages
2002 if (!p.CurrentVer().end() &&
2003 (p.CurrentVer()->Priority == pkgCache::State::Required))
2004 continue;
2005
2006 // if it is not marked and it is installed, it's garbage
2007 if(!state.Marked && (!p.CurrentVer().end() || state.Install()))
2008 {
2009 state.Garbage=true;
2010 if(debug_autoremove)
2011 std::clog << "Garbage: " << p.FullName() << std::endl;
2012 }
2013 }
2014
2015 return true;
2016 }
2017 /*}}}*/
2018 // DepCache::MarkAndSweep /*{{{*/
2019 bool pkgDepCache::MarkAndSweep(InRootSetFunc &rootFunc)
2020 {
2021 return MarkRequired(rootFunc) && Sweep();
2022 }
2023 bool pkgDepCache::MarkAndSweep()
2024 {
2025 std::unique_ptr<InRootSetFunc> f(GetRootSetFunc());
2026 if(f.get() != NULL)
2027 return MarkAndSweep(*f.get());
2028 else
2029 return false;
2030 }
2031 /*}}}*/