1 // -*- mode: cpp; mode: fold -*-
3 // $Id: depcache.cc,v 1.25 2001/05/27 05:36:04 jgg Exp $
4 /* ######################################################################
6 Dependency Cache - Caches Dependency information.
8 ##################################################################### */
10 // Include Files /*{{{*/
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>
47 // helper for Install-Recommends-Sections and Never-MarkAuto-Sections /*{{{*/
49 ConfigValueInSubTree(const char* SubTree
, const char *needle
)
51 Configuration::Item
const *Opts
;
52 Opts
= _config
->Tree(SubTree
);
53 if (Opts
!= 0 && Opts
->Child
!= 0)
56 for (; Opts
!= 0; Opts
= Opts
->Next
)
58 if (Opts
->Value
.empty() == true)
60 if (strcmp(needle
, Opts
->Value
.c_str()) == 0)
67 pkgDepCache::ActionGroup::ActionGroup(pkgDepCache
&cache
) : /*{{{*/
68 d(NULL
), cache(cache
), released(false)
73 void pkgDepCache::ActionGroup::release()
77 if(cache
.group_level
== 0)
78 std::cerr
<< "W: Unbalanced action groups, expect badness" << std::endl
;
83 if(cache
.group_level
== 0)
91 pkgDepCache::ActionGroup::~ActionGroup()
96 // DepCache::pkgDepCache - Constructors /*{{{*/
97 // ---------------------------------------------------------------------
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
)
104 DebugMarker
= _config
->FindB("Debug::pkgDepCache::Marker", false);
105 DebugAutoInstall
= _config
->FindB("Debug::pkgDepCache::AutoInstall", false);
108 if (LocalPolicy
== 0)
109 delLocalPolicy
= LocalPolicy
= new Policy
;
112 // DepCache::~pkgDepCache - Destructor /*{{{*/
113 // ---------------------------------------------------------------------
115 pkgDepCache::~pkgDepCache()
119 delete delLocalPolicy
;
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
)
127 // Suppress mark updates during this operation (just in case) and
128 // run a mark operation when Init terminates.
129 ActionGroup
actions(*this);
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
);
140 Prog
->OverallProgress(0,2*Head().PackageCount
,Head().PackageCount
,
141 _("Building dependency tree"));
142 Prog
->SubProgress(Head().PackageCount
,_("Candidate versions"));
145 /* Set the current state of everything. In this state all of the
146 packages are kept exactly as is. See AllUpgrade */
148 for (PkgIterator I
= PkgBegin(); I
.end() != true; ++I
, ++Done
)
150 if (Prog
!= 0 && Done%20
== 0)
151 Prog
->Progress(Done
);
153 // Find the proper cache slot
154 StateCache
&State
= PkgState
[I
->ID
];
157 // Figure out the install version
158 State
.CandidateVer
= LocalPolicy
->GetCandidateVer(I
);
159 State
.InstallVer
= I
.CurrentVer();
160 State
.Mode
= ModeKeep
;
162 State
.Update(I
,*this);
167 Prog
->OverallProgress(Head().PackageCount
,2*Head().PackageCount
,
169 _("Building dependency tree"));
170 Prog
->SubProgress(Head().PackageCount
,_("Dependency generation"));
181 bool pkgDepCache::readStateFile(OpProgress
* const Prog
) /*{{{*/
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();
189 Prog
->OverallProgress(0, file_size
, 1,
190 _("Reading state information"));
192 pkgTagFile
tagfile(&state_file
);
193 pkgTagSection section
;
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)
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)
206 short const reason
= section
.FindI("Auto-Installed", 0);
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")
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
;
220 amt
+= section
.size();
222 Prog
->OverallProgress(amt
, file_size
, 1,
223 _("Reading state information"));
226 Prog
->OverallProgress(file_size
, file_size
, 1,
227 _("Reading state information"));
233 bool pkgDepCache::writeStateFile(OpProgress
* const /*prog*/, bool const InstalledOnly
) /*{{{*/
235 bool const debug_autoremove
= _config
->FindB("Debug::pkgAutoRemove",false);
238 std::clog
<< "pkgDepCache::writeStateFile()" << std::endl
;
241 string
const state
= _config
->FindFile("Dir::State::extended_states");
242 if (CreateAPTDirectoryIfNeeded(_config
->FindDir("Dir::State"), flNotFile(state
)) == false)
245 // if it does not exist, create a empty one
246 if(!RealFileExists(state
))
248 StateFile
.Open(state
, FileFd::WriteAtomic
);
253 if(!StateFile
.Open(state
, FileFd::ReadOnly
))
254 return _error
->Error(_("Failed to open StateFile %s"),
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());
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)
270 // Silently ignore unknown packages and packages with no actual
272 pkgCache::PkgIterator pkg
= Cache
->FindPkg(pkgname
, pkgarch
);
273 if(pkg
.end() || pkg
.VersionList().end())
275 StateCache
const &P
= PkgState
[pkg
->ID
];
276 bool newAuto
= (P
.Flags
& Flag::Auto
);
277 // reset to default (=manual) 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
)))
282 if (newAuto
== false)
284 // The section is obsolete if it contains no other tag
285 auto const count
= section
.Count();
287 (count
== 2 && section
.Exists("Auto-Installed")) ||
288 (count
== 3 && section
.Exists("Auto-Installed") && section
.Exists("Architecture")))
291 std::clog
<< "Drop obsolete section with " << count
<< " fields for " << APT::PrettyPkg(this, pkg
) << std::endl
;
297 std::clog
<< "Update existing AutoInstall to " << newAuto
<< " for " << APT::PrettyPkg(this, pkg
) << std::endl
;
299 std::vector
<pkgTagSection::Tag
> rewrite
;
300 rewrite
.push_back(pkgTagSection::Tag::Rewrite("Architecture", pkg
.Arch()));
301 rewrite
.push_back(pkgTagSection::Tag::Rewrite("Auto-Installed", newAuto
? "1" : "0"));
302 section
.Write(OutFile
, NULL
, rewrite
);
303 if (OutFile
.Write("\n", 1) == false)
305 pkgs_seen
.insert(pkg
.FullName());
308 // then write the ones we have not seen yet
309 for(pkgCache::PkgIterator pkg
=Cache
->PkgBegin(); !pkg
.end(); ++pkg
) {
310 StateCache
const &P
= PkgState
[pkg
->ID
];
311 if(P
.Flags
& Flag::Auto
) {
312 if (pkgs_seen
.find(pkg
.FullName()) != pkgs_seen
.end()) {
314 std::clog
<< "Skipping already written " << APT::PrettyPkg(this, pkg
) << std::endl
;
317 // skip not installed ones if requested
318 if (InstalledOnly
&& (
319 (pkg
->CurrentVer
== 0 && P
.Mode
!= ModeInstall
) ||
320 (pkg
->CurrentVer
!= 0 && P
.Mode
== ModeDelete
)))
323 std::clog
<< "Writing new AutoInstall: " << APT::PrettyPkg(this, pkg
) << std::endl
;
324 std::string stanza
= "Package: ";
325 stanza
.append(pkg
.Name())
326 .append("\nArchitecture: ").append(pkg
.Arch())
327 .append("\nAuto-Installed: 1\n\n");
328 if (OutFile
.Write(stanza
.c_str(), stanza
.length()) == false)
332 if (StateFile
.Failed())
337 if (OutFile
.Close() == false)
339 chmod(state
.c_str(), 0644);
343 // DepCache::CheckDep - Checks a single dependency /*{{{*/
344 // ---------------------------------------------------------------------
345 /* This first checks the dependency against the main target package and
346 then walks along the package provides list and checks if each provides
347 will be installed then checks the provides against the dep. Res will be
348 set to the package which was used to satisfy the dep. */
349 bool pkgDepCache::CheckDep(DepIterator
const &Dep
,int const Type
,PkgIterator
&Res
)
351 Res
= Dep
.TargetPkg();
353 /* Check simple depends. A depends -should- never self match but
354 we allow it anyhow because dpkg does. Technically it is a packaging
355 bug. Conflicts may never self match */
356 if (Dep
.IsIgnorable(Res
) == false)
358 // Check the base package
359 if (Type
== NowVersion
)
361 if (Res
->CurrentVer
!= 0 && Dep
.IsSatisfied(Res
.CurrentVer()) == true)
364 else if (Type
== InstallVersion
)
366 if (PkgState
[Res
->ID
].InstallVer
!= 0 &&
367 Dep
.IsSatisfied(PkgState
[Res
->ID
].InstVerIter(*this)) == true)
370 else if (Type
== CandidateVersion
)
371 if (PkgState
[Res
->ID
].CandidateVer
!= 0 &&
372 Dep
.IsSatisfied(PkgState
[Res
->ID
].CandidateVerIter(*this)) == true)
376 if (Dep
->Type
== Dep::Obsoletes
)
379 // Check the providing packages
380 PrvIterator P
= Dep
.TargetPkg().ProvidesList();
381 for (; P
.end() != true; ++P
)
383 if (Dep
.IsIgnorable(P
) == true)
386 // Check if the provides is a hit
387 if (Type
== NowVersion
)
389 if (P
.OwnerPkg().CurrentVer() != P
.OwnerVer())
392 else if (Type
== InstallVersion
)
394 StateCache
&State
= PkgState
[P
.OwnerPkg()->ID
];
395 if (State
.InstallVer
!= (Version
*)P
.OwnerVer())
398 else if (Type
== CandidateVersion
)
400 StateCache
&State
= PkgState
[P
.OwnerPkg()->ID
];
401 if (State
.CandidateVer
!= (Version
*)P
.OwnerVer())
405 // Compare the versions.
406 if (Dep
.IsSatisfied(P
) == true)
416 // DepCache::AddSizes - Add the packages sizes to the counters /*{{{*/
417 // ---------------------------------------------------------------------
418 /* Call with Inverse = true to preform the inverse opration */
419 void pkgDepCache::AddSizes(const PkgIterator
&Pkg
, bool const Inverse
)
421 StateCache
&P
= PkgState
[Pkg
->ID
];
423 if (Pkg
->VersionList
== 0)
426 if (Pkg
.State() == pkgCache::PkgIterator::NeedsConfigure
&&
430 // Compute the size data
431 if (P
.NewInstall() == true)
433 if (Inverse
== false) {
434 iUsrSize
+= P
.InstVerIter(*this)->InstalledSize
;
435 iDownloadSize
+= P
.InstVerIter(*this)->Size
;
437 iUsrSize
-= P
.InstVerIter(*this)->InstalledSize
;
438 iDownloadSize
-= P
.InstVerIter(*this)->Size
;
444 if (Pkg
->CurrentVer
!= 0 &&
445 (P
.InstallVer
!= (Version
*)Pkg
.CurrentVer() ||
446 (P
.iFlags
& ReInstall
) == ReInstall
) && P
.InstallVer
!= 0)
448 if (Inverse
== false) {
449 iUsrSize
-= Pkg
.CurrentVer()->InstalledSize
;
450 iUsrSize
+= P
.InstVerIter(*this)->InstalledSize
;
451 iDownloadSize
+= P
.InstVerIter(*this)->Size
;
453 iUsrSize
-= P
.InstVerIter(*this)->InstalledSize
;
454 iUsrSize
+= Pkg
.CurrentVer()->InstalledSize
;
455 iDownloadSize
-= P
.InstVerIter(*this)->Size
;
461 if (Pkg
.State() == pkgCache::PkgIterator::NeedsUnpack
&&
464 if (Inverse
== false)
465 iDownloadSize
+= P
.InstVerIter(*this)->Size
;
467 iDownloadSize
-= P
.InstVerIter(*this)->Size
;
472 if (Pkg
->CurrentVer
!= 0 && P
.InstallVer
== 0)
474 if (Inverse
== false)
475 iUsrSize
-= Pkg
.CurrentVer()->InstalledSize
;
477 iUsrSize
+= Pkg
.CurrentVer()->InstalledSize
;
482 // DepCache::AddStates - Add the package to the state counter /*{{{*/
483 // ---------------------------------------------------------------------
484 /* This routine is tricky to use, you must make sure that it is never
485 called twice for the same package. This means the Remove/Add section
486 should be as short as possible and not encompass any code that will
487 calld Remove/Add itself. Remember, dependencies can be circular so
488 while processing a dep for Pkg it is possible that Add/Remove
489 will be called on Pkg */
490 void pkgDepCache::AddStates(const PkgIterator
&Pkg
, bool const Invert
)
492 signed char const Add
= (Invert
== false) ? 1 : -1;
493 StateCache
&State
= PkgState
[Pkg
->ID
];
495 // The Package is broken (either minimal dep or policy dep)
496 if ((State
.DepState
& DepInstMin
) != DepInstMin
)
498 if ((State
.DepState
& DepInstPolicy
) != DepInstPolicy
)
499 iPolicyBrokenCount
+= Add
;
502 if (Pkg
.State() != PkgIterator::NeedsNothing
)
506 if (Pkg
->CurrentVer
== 0)
508 if (State
.Mode
== ModeDelete
&&
509 (State
.iFlags
& Purge
) == Purge
&& Pkg
.Purge() == false)
512 if (State
.Mode
== ModeInstall
)
517 // Installed, no upgrade
518 if (State
.Status
== 0)
520 if (State
.Mode
== ModeDelete
)
523 if ((State
.iFlags
& ReInstall
) == ReInstall
)
528 // Alll 3 are possible
529 if (State
.Mode
== ModeDelete
)
531 else if (State
.Mode
== ModeKeep
)
533 else if (State
.Mode
== ModeInstall
)
537 // DepCache::BuildGroupOrs - Generate the Or group dep data /*{{{*/
538 // ---------------------------------------------------------------------
539 /* The or group results are stored in the last item of the or group. This
540 allows easy detection of the state of a whole or'd group. */
541 void pkgDepCache::BuildGroupOrs(VerIterator
const &V
)
543 unsigned char Group
= 0;
544 for (DepIterator D
= V
.DependsList(); D
.end() != true; ++D
)
546 // Build the dependency state.
547 unsigned char &State
= DepState
[D
->ID
];
549 /* Invert for Conflicts. We have to do this twice to get the
550 right sense for a conflicts group */
551 if (D
.IsNegative() == true)
554 // Add to the group if we are within an or..
558 if ((D
->CompareOp
& Dep::Or
) != Dep::Or
)
561 // Invert for Conflicts
562 if (D
.IsNegative() == true)
567 // DepCache::VersionState - Perform a pass over a dependency list /*{{{*/
568 // ---------------------------------------------------------------------
569 /* This is used to run over a dependency list and determine the dep
570 state of the list, filtering it through both a Min check and a Policy
571 check. The return result will have SetMin/SetPolicy low if a check
572 fails. It uses the DepState cache for it's computations. */
573 unsigned char pkgDepCache::VersionState(DepIterator D
, unsigned char const Check
,
574 unsigned char const SetMin
,
575 unsigned char const SetPolicy
) const
577 unsigned char Dep
= 0xFF;
578 while (D
.end() != true)
580 // the last or-dependency has the state of all previous or'ed
581 DepIterator Start
, End
;
582 D
.GlobOr(Start
, End
);
583 // ignore if we are called with Dep{Install,…} or DepG{Install,…}
584 // the later would be more correct, but the first is what we get
585 unsigned char const State
= DepState
[End
->ID
] | (DepState
[End
->ID
] >> 3);
587 // Minimum deps that must be satisfied to have a working package
588 if (Start
.IsCritical() == true)
590 if ((State
& Check
) != Check
)
591 return Dep
&= ~(SetMin
| SetPolicy
);
593 // Policy deps that must be satisfied to install the package
594 else if (IsImportantDep(Start
) == true &&
595 (State
& Check
) != Check
)
601 // DepCache::DependencyState - Compute the 3 results for a dep /*{{{*/
602 // ---------------------------------------------------------------------
603 /* This is the main dependency computation bit. It computes the 3 main
604 results for a dependencys, Now, Install and Candidate. Callers must
605 invert the result if dealing with conflicts. */
606 unsigned char pkgDepCache::DependencyState(DepIterator
const &D
)
608 unsigned char State
= 0;
610 if (CheckDep(D
,NowVersion
) == true)
612 if (CheckDep(D
,InstallVersion
) == true)
614 if (CheckDep(D
,CandidateVersion
) == true)
620 // DepCache::UpdateVerState - Compute the Dep member of the state /*{{{*/
621 // ---------------------------------------------------------------------
622 /* This determines the combined dependency representation of a package
623 for its two states now and install. This is done by using the pre-generated
624 dependency information. */
625 void pkgDepCache::UpdateVerState(PkgIterator
const &Pkg
)
627 // Empty deps are always true
628 StateCache
&State
= PkgState
[Pkg
->ID
];
629 State
.DepState
= 0xFF;
631 // Check the Current state
632 if (Pkg
->CurrentVer
!= 0)
634 DepIterator D
= Pkg
.CurrentVer().DependsList();
635 State
.DepState
&= VersionState(D
,DepNow
,DepNowMin
,DepNowPolicy
);
638 /* Check the candidate state. We do not compare against the whole as
639 a candidate state but check the candidate version against the
641 if (State
.CandidateVer
!= 0)
643 DepIterator D
= State
.CandidateVerIter(*this).DependsList();
644 State
.DepState
&= VersionState(D
,DepInstall
,DepCandMin
,DepCandPolicy
);
647 // Check target state which can only be current or installed
648 if (State
.InstallVer
!= 0)
650 DepIterator D
= State
.InstVerIter(*this).DependsList();
651 State
.DepState
&= VersionState(D
,DepInstall
,DepInstMin
,DepInstPolicy
);
655 // DepCache::Update - Figure out all the state information /*{{{*/
656 // ---------------------------------------------------------------------
657 /* This will figure out the state of all the packages and all the
658 dependencies based on the current policy. */
659 void pkgDepCache::Update(OpProgress
* const Prog
)
667 iPolicyBrokenCount
= 0;
670 // Perform the depends pass
672 for (PkgIterator I
= PkgBegin(); I
.end() != true; ++I
, ++Done
)
674 if (Prog
!= 0 && Done%20
== 0)
675 Prog
->Progress(Done
);
676 for (VerIterator V
= I
.VersionList(); V
.end() != true; ++V
)
678 unsigned char Group
= 0;
680 for (DepIterator D
= V
.DependsList(); D
.end() != true; ++D
)
682 // Build the dependency state.
683 unsigned char &State
= DepState
[D
->ID
];
684 State
= DependencyState(D
);
686 // Add to the group if we are within an or..
689 if ((D
->CompareOp
& Dep::Or
) != Dep::Or
)
692 // Invert for Conflicts
693 if (D
.IsNegative() == true)
698 // Compute the package dependency state and size additions
705 Prog
->Progress(Done
);
710 // DepCache::Update - Update the deps list of a package /*{{{*/
711 // ---------------------------------------------------------------------
712 /* This is a helper for update that only does the dep portion of the scan.
713 It is mainly meant to scan reverse dependencies. */
714 void pkgDepCache::Update(DepIterator D
)
716 // Update the reverse deps
717 for (;D
.end() != true; ++D
)
719 unsigned char &State
= DepState
[D
->ID
];
720 State
= DependencyState(D
);
722 // Invert for Conflicts
723 if (D
.IsNegative() == true)
726 RemoveStates(D
.ParentPkg());
727 BuildGroupOrs(D
.ParentVer());
728 UpdateVerState(D
.ParentPkg());
729 AddStates(D
.ParentPkg());
733 // DepCache::Update - Update the related deps of a package /*{{{*/
734 // ---------------------------------------------------------------------
735 /* This is called whenever the state of a package changes. It updates
736 all cached dependencies related to this package. */
737 void pkgDepCache::Update(PkgIterator
const &Pkg
)
739 // Recompute the dep of the package
744 // Update the reverse deps
745 Update(Pkg
.RevDependsList());
747 // Update the provides map for the current ver
748 if (Pkg
->CurrentVer
!= 0)
749 for (PrvIterator P
= Pkg
.CurrentVer().ProvidesList();
750 P
.end() != true; ++P
)
751 Update(P
.ParentPkg().RevDependsList());
753 // Update the provides map for the candidate ver
754 if (PkgState
[Pkg
->ID
].CandidateVer
!= 0)
755 for (PrvIterator P
= PkgState
[Pkg
->ID
].CandidateVerIter(*this).ProvidesList();
756 P
.end() != true; ++P
)
757 Update(P
.ParentPkg().RevDependsList());
760 // DepCache::MarkKeep - Put the package in the keep state /*{{{*/
761 // ---------------------------------------------------------------------
763 bool pkgDepCache::MarkKeep(PkgIterator
const &Pkg
, bool Soft
, bool FromUser
,
766 if (IsModeChangeOk(ModeKeep
, Pkg
, Depth
, FromUser
) == false)
769 /* Reject an attempt to keep a non-source broken installed package, those
771 if (Pkg
.State() == PkgIterator::NeedsUnpack
&&
772 Pkg
.CurrentVer().Downloadable() == false)
775 /* We changed the soft state all the time so the UI is a bit nicer
777 StateCache
&P
= PkgState
[Pkg
->ID
];
779 // Check that it is not already kept
780 if (P
.Mode
== ModeKeep
)
784 P
.iFlags
|= AutoKept
;
786 P
.iFlags
&= ~AutoKept
;
788 ActionGroup
group(*this);
790 #if 0 // reseting the autoflag here means we lose the
791 // auto-mark information if a user selects a package for removal
792 // but changes his mind then and sets it for keep again
793 // - this makes sense as default when all Garbage dependencies
794 // are automatically marked for removal (as aptitude does).
795 // setting a package for keep then makes it no longer autoinstalled
796 // for all other use-case this action is rather surprising
797 if(FromUser
&& !P
.Marked
)
798 P
.Flags
&= ~Flag::Auto
;
801 if (DebugMarker
== true)
802 std::clog
<< OutputInDepth(Depth
) << "MarkKeep " << APT::PrettyPkg(this, Pkg
) << " FU=" << FromUser
<< std::endl
;
808 if (Pkg
->CurrentVer
== 0)
811 P
.InstallVer
= Pkg
.CurrentVer();
820 // DepCache::MarkDelete - Put the package in the delete state /*{{{*/
821 // ---------------------------------------------------------------------
823 bool pkgDepCache::MarkDelete(PkgIterator
const &Pkg
, bool rPurge
,
824 unsigned long Depth
, bool FromUser
)
826 if (IsModeChangeOk(ModeDelete
, Pkg
, Depth
, FromUser
) == false)
829 StateCache
&P
= PkgState
[Pkg
->ID
];
831 // Check that it is not already marked for delete
832 if ((P
.Mode
== ModeDelete
|| P
.InstallVer
== 0) &&
833 (Pkg
.Purge() == true || rPurge
== false))
836 // check if we are allowed to remove the package
837 if (IsDeleteOk(Pkg
,rPurge
,Depth
,FromUser
) == false)
840 P
.iFlags
&= ~(AutoKept
| Purge
);
844 ActionGroup
group(*this);
846 if (FromUser
== false)
848 VerIterator
const PV
= P
.InstVerIter(*this);
849 if (PV
.end() == false)
851 // removed metapackages mark their dependencies as manual to prevent in "desktop depends browser, texteditor"
852 // the removal of browser to suggest the removal of desktop and texteditor.
853 // We ignore the auto-bit here as we can't deal with metapackage cascardes otherwise.
854 // We do not check for or-groups here as we don't know which package takes care of
855 // providing the feature the user likes e.g.: browser1 | browser2 | browser3
856 // Temporary removals are effected by this as well, which is bad, but unlikely in practice
857 bool const PinNeverMarkAutoSection
= (PV
->Section
!= 0 && ConfigValueInSubTree("APT::Never-MarkAuto-Sections", PV
.Section()));
858 if (PinNeverMarkAutoSection
)
860 for (DepIterator D
= PV
.DependsList(); D
.end() != true; ++D
)
862 if (D
.IsMultiArchImplicit() == true || D
.IsNegative() == true || IsImportantDep(D
) == false)
865 pkgCacheFile
CacheFile(this);
866 APT::VersionList verlist
= APT::VersionList::FromDependency(CacheFile
, D
, APT::CacheSetHelper::INSTALLED
);
867 for (auto const &V
: verlist
)
869 PkgIterator
const DP
= V
.ParentPkg();
870 if(DebugAutoInstall
== true)
871 std::clog
<< OutputInDepth(Depth
) << "Setting " << DP
.FullName(false) << " NOT as auto-installed (direct "
872 << D
.DepType() << " of " << Pkg
.FullName(false) << " which is in APT::Never-MarkAuto-Sections)" << std::endl
;
881 if (DebugMarker
== true)
882 std::clog
<< OutputInDepth(Depth
) << (rPurge
? "MarkPurge " : "MarkDelete ") << APT::PrettyPkg(this, Pkg
) << " FU=" << FromUser
<< std::endl
;
887 if (Pkg
->CurrentVer
== 0 && (Pkg
.Purge() == true || rPurge
== false))
900 // DepCache::IsDeleteOk - check if it is ok to remove this package /*{{{*/
901 // ---------------------------------------------------------------------
902 /* The default implementation tries to prevent deletion of install requests.
903 dpkg holds are enforced by the private IsModeChangeOk */
904 bool pkgDepCache::IsDeleteOk(PkgIterator
const &Pkg
,bool rPurge
,
905 unsigned long Depth
, bool FromUser
)
907 return IsDeleteOkProtectInstallRequests(Pkg
, rPurge
, Depth
, FromUser
);
909 bool pkgDepCache::IsDeleteOkProtectInstallRequests(PkgIterator
const &Pkg
,
910 bool const /*rPurge*/, unsigned long const Depth
, bool const FromUser
)
912 if (FromUser
== false && Pkg
->CurrentVer
== 0)
914 StateCache
&P
= PkgState
[Pkg
->ID
];
915 if (P
.InstallVer
!= 0 && P
.Status
== 2 && (P
.Flags
& Flag::Auto
) != Flag::Auto
)
917 if (DebugMarker
== true)
918 std::clog
<< OutputInDepth(Depth
) << "Manual install request prevents MarkDelete of " << APT::PrettyPkg(this, Pkg
) << std::endl
;
925 // DepCache::IsModeChangeOk - check if it is ok to change the mode /*{{{*/
926 // ---------------------------------------------------------------------
927 /* this is used by all Mark methods on the very first line to check sanity
928 and prevents mode changes for packages on hold for example.
929 If you want to check Mode specific stuff you can use the virtual public
930 Is<Mode>Ok methods instead */
931 static char const* PrintMode(char const mode
)
935 case pkgDepCache::ModeInstall
: return "Install";
936 case pkgDepCache::ModeKeep
: return "Keep";
937 case pkgDepCache::ModeDelete
: return "Delete";
938 case pkgDepCache::ModeGarbage
: return "Garbage";
939 default: return "UNKNOWN";
942 bool pkgDepCache::IsModeChangeOk(ModeList
const mode
, PkgIterator
const &Pkg
,
943 unsigned long const Depth
, bool const FromUser
)
945 // we are not trying to hard…
946 if (unlikely(Depth
> 100))
950 if (unlikely(Pkg
.end() == true || Pkg
->VersionList
== 0))
953 // the user is always right
954 if (FromUser
== true)
957 StateCache
&P
= PkgState
[Pkg
->ID
];
958 // not changing the mode is obviously also fine as we might want to call
959 // e.g. MarkInstall multiple times with different arguments for the same package
963 // if previous state was set by user only user can reset it
964 if ((P
.iFlags
& Protected
) == Protected
)
966 if (unlikely(DebugMarker
== true))
967 std::clog
<< OutputInDepth(Depth
) << "Ignore Mark" << PrintMode(mode
)
968 << " of " << APT::PrettyPkg(this, Pkg
) << " as its mode (" << PrintMode(P
.Mode
)
969 << ") is protected" << std::endl
;
972 // enforce dpkg holds
973 else if (mode
!= ModeKeep
&& Pkg
->SelectedState
== pkgCache::State::Hold
&&
974 _config
->FindB("APT::Ignore-Hold",false) == false)
976 if (unlikely(DebugMarker
== true))
977 std::clog
<< OutputInDepth(Depth
) << "Hold prevents Mark" << PrintMode(mode
)
978 << " of " << APT::PrettyPkg(this, Pkg
) << std::endl
;
985 // DepCache::MarkInstall - Put the package in the install state /*{{{*/
986 // ---------------------------------------------------------------------
988 struct CompareProviders
{
989 pkgCache::PkgIterator
const Pkg
;
990 explicit CompareProviders(pkgCache::DepIterator
const &Dep
) : Pkg(Dep
.TargetPkg()) {};
991 //bool operator() (APT::VersionList::iterator const &AV, APT::VersionList::iterator const &BV)
992 bool operator() (pkgCache::VerIterator
const &AV
, pkgCache::VerIterator
const &BV
)
994 pkgCache::PkgIterator
const A
= AV
.ParentPkg();
995 pkgCache::PkgIterator
const B
= BV
.ParentPkg();
996 // Prefer MA:same packages if other architectures for it are installed
997 if ((AV
->MultiArch
& pkgCache::Version::Same
) == pkgCache::Version::Same
||
998 (BV
->MultiArch
& pkgCache::Version::Same
) == pkgCache::Version::Same
)
1001 if ((AV
->MultiArch
& pkgCache::Version::Same
) == pkgCache::Version::Same
)
1003 pkgCache::GrpIterator Grp
= A
.Group();
1004 for (pkgCache::PkgIterator P
= Grp
.PackageList(); P
.end() == false; P
= Grp
.NextPkg(P
))
1005 if (P
->CurrentVer
!= 0)
1012 if ((BV
->MultiArch
& pkgCache::Version::Same
) == pkgCache::Version::Same
)
1014 pkgCache::GrpIterator Grp
= B
.Group();
1015 for (pkgCache::PkgIterator P
= Grp
.PackageList(); P
.end() == false; P
= Grp
.NextPkg(P
))
1017 if (P
->CurrentVer
!= 0)
1025 return instA
== false;
1027 if ((A
->CurrentVer
== 0 || B
->CurrentVer
== 0) && A
->CurrentVer
!= B
->CurrentVer
)
1028 return A
->CurrentVer
== 0;
1029 // Prefer packages in the same group as the target; e.g. foo:i386, foo:amd64
1030 if (A
->Group
!= B
->Group
)
1032 if (A
->Group
== Pkg
->Group
&& B
->Group
!= Pkg
->Group
)
1034 else if (B
->Group
== Pkg
->Group
&& A
->Group
!= Pkg
->Group
)
1037 // we like essentials
1038 if ((A
->Flags
& pkgCache::Flag::Essential
) != (B
->Flags
& pkgCache::Flag::Essential
))
1040 if ((A
->Flags
& pkgCache::Flag::Essential
) == pkgCache::Flag::Essential
)
1042 else if ((B
->Flags
& pkgCache::Flag::Essential
) == pkgCache::Flag::Essential
)
1045 if ((A
->Flags
& pkgCache::Flag::Important
) != (B
->Flags
& pkgCache::Flag::Important
))
1047 if ((A
->Flags
& pkgCache::Flag::Important
) == pkgCache::Flag::Important
)
1049 else if ((B
->Flags
& pkgCache::Flag::Important
) == pkgCache::Flag::Important
)
1052 // prefer native architecture
1053 if (strcmp(A
.Arch(), B
.Arch()) != 0)
1055 if (strcmp(A
.Arch(), A
.Cache()->NativeArch()) == 0)
1057 else if (strcmp(B
.Arch(), B
.Cache()->NativeArch()) == 0)
1059 std::vector
<std::string
> archs
= APT::Configuration::getArchitectures();
1060 for (std::vector
<std::string
>::const_iterator a
= archs
.begin(); a
!= archs
.end(); ++a
)
1063 else if (*a
== B
.Arch())
1066 // higher priority seems like a good idea
1067 if (AV
->Priority
!= BV
->Priority
)
1068 return AV
->Priority
> BV
->Priority
;
1069 // unable to decide…
1070 return A
->ID
< B
->ID
;
1073 bool pkgDepCache::MarkInstall(PkgIterator
const &Pkg
,bool AutoInst
,
1074 unsigned long Depth
, bool FromUser
,
1075 bool ForceImportantDeps
)
1077 if (IsModeChangeOk(ModeInstall
, Pkg
, Depth
, FromUser
) == false)
1080 StateCache
&P
= PkgState
[Pkg
->ID
];
1082 // See if there is even any possible instalation candidate
1083 if (P
.CandidateVer
== 0)
1086 /* Check that it is not already marked for install and that it can be
1088 if ((P
.InstPolicyBroken() == false && P
.InstBroken() == false) &&
1089 (P
.Mode
== ModeInstall
||
1090 P
.CandidateVer
== (Version
*)Pkg
.CurrentVer()))
1092 if (P
.CandidateVer
== (Version
*)Pkg
.CurrentVer() && P
.InstallVer
== 0)
1093 return MarkKeep(Pkg
, false, FromUser
, Depth
+1);
1097 // check if we are allowed to install the package
1098 if (IsInstallOk(Pkg
,AutoInst
,Depth
,FromUser
) == false)
1101 ActionGroup
group(*this);
1102 P
.iFlags
&= ~AutoKept
;
1104 /* Target the candidate version and remove the autoflag. We reset the
1105 autoflag below if this was called recursively. Otherwise the user
1106 should have the ability to de-auto a package by changing its state */
1110 P
.Mode
= ModeInstall
;
1111 P
.InstallVer
= P
.CandidateVer
;
1115 // Set it to manual if it's a new install or already installed,
1116 // but only if its not marked by the autoremover (aptitude depend on this behavior)
1117 // or if we do automatic installation (aptitude never does it)
1118 if(P
.Status
== 2 || (Pkg
->CurrentVer
!= 0 && (AutoInst
== true || P
.Marked
== false)))
1119 P
.Flags
&= ~Flag::Auto
;
1123 // Set it to auto if this is a new install.
1125 P
.Flags
|= Flag::Auto
;
1127 if (P
.CandidateVer
== (Version
*)Pkg
.CurrentVer())
1134 if (AutoInst
== false || _config
->Find("APT::Solver", "internal") != "internal")
1137 if (DebugMarker
== true)
1138 std::clog
<< OutputInDepth(Depth
) << "MarkInstall " << APT::PrettyPkg(this, Pkg
) << " FU=" << FromUser
<< std::endl
;
1140 bool MoveAutoBitToDependencies
= false;
1141 VerIterator
const PV
= P
.InstVerIter(*this);
1142 if (unlikely(PV
.end() == true))
1144 else if (PV
->Section
!= 0 && (P
.Flags
& Flag::Auto
) != Flag::Auto
)
1146 VerIterator
const CurVer
= Pkg
.CurrentVer();
1147 if (CurVer
.end() == false && CurVer
->Section
!= 0 && strcmp(CurVer
.Section(), PV
.Section()) != 0)
1149 bool const CurVerInMoveSection
= ConfigValueInSubTree("APT::Move-Autobit-Sections", CurVer
.Section());
1150 bool const InstVerInMoveSection
= ConfigValueInSubTree("APT::Move-Autobit-Sections", PV
.Section());
1151 MoveAutoBitToDependencies
= (CurVerInMoveSection
== false && InstVerInMoveSection
== true);
1152 if (MoveAutoBitToDependencies
== true)
1154 if(DebugAutoInstall
== true)
1155 std::clog
<< OutputInDepth(Depth
) << "Setting " << Pkg
.FullName(false) << " as auto-installed, moving manual to its dependencies" << std::endl
;
1156 MarkAuto(Pkg
, true);
1161 DepIterator Dep
= PV
.DependsList();
1162 for (; Dep
.end() != true;)
1165 DepIterator Start
= Dep
;
1168 for (bool LastOR
= true; Dep
.end() == false && LastOR
== true; ++Dep
, ++Ors
)
1170 LastOR
= (Dep
->CompareOp
& Dep::Or
) == Dep::Or
;
1172 if ((DepState
[Dep
->ID
] & DepInstall
) == DepInstall
)
1176 // Dep is satisfied okay.
1177 if (Result
== false)
1180 /* Check if this dep should be consider for install. If it is a user
1181 defined important dep and we are installed a new package then
1182 it will be installed. Otherwise we only check for important
1183 deps that have changed from the installed version */
1184 if (IsImportantDep(Start
) == false)
1187 /* If we are in an or group locate the first or that can
1188 succeed. We have already cached this… */
1189 for (; Ors
> 1 && (DepState
[Start
->ID
] & DepCVer
) != DepCVer
; --Ors
)
1192 /* unsatisfiable dependency: IsInstallOkDependenciesSatisfiableByCandidates
1193 would have prevented us to get here if not overridden, so just skip
1194 over the problem here as the front-end will know what it is doing */
1195 if (Ors
== 1 && (DepState
[Start
->ID
] &DepCVer
) != DepCVer
&& Start
.IsNegative() == false)
1198 /* Check if any ImportantDep() (but not Critical) were added
1199 * since we installed the package. Also check for deps that
1200 * were satisfied in the past: for instance, if a version
1201 * restriction in a Recommends was tightened, upgrading the
1202 * package should follow that Recommends rather than causing the
1203 * dependency to be removed. (bug #470115)
1205 if (Pkg
->CurrentVer
!= 0 && ForceImportantDeps
== false && Start
.IsCritical() == false)
1207 bool isNewImportantDep
= true;
1208 bool isPreviouslySatisfiedImportantDep
= false;
1209 for (DepIterator D
= Pkg
.CurrentVer().DependsList(); D
.end() != true; ++D
)
1211 //FIXME: Should we handle or-group better here?
1212 // We do not check if the package we look for is part of the same or-group
1213 // we might find while searching, but could that really be a problem?
1214 if (D
.IsCritical() == true || IsImportantDep(D
) == false ||
1215 Start
.TargetPkg() != D
.TargetPkg())
1218 isNewImportantDep
= false;
1220 while ((D
->CompareOp
& Dep::Or
) != 0)
1223 isPreviouslySatisfiedImportantDep
= (((*this)[D
] & DepGNow
) != 0);
1224 if (isPreviouslySatisfiedImportantDep
== true)
1228 if(isNewImportantDep
== true)
1230 if (DebugAutoInstall
== true)
1231 std::clog
<< OutputInDepth(Depth
) << "new important dependency: "
1232 << Start
.TargetPkg().FullName() << std::endl
;
1234 else if(isPreviouslySatisfiedImportantDep
== true)
1236 if (DebugAutoInstall
== true)
1237 std::clog
<< OutputInDepth(Depth
) << "previously satisfied important dependency on "
1238 << Start
.TargetPkg().FullName() << std::endl
;
1242 if (DebugAutoInstall
== true)
1243 std::clog
<< OutputInDepth(Depth
) << "ignore old unsatisfied important dependency on "
1244 << Start
.TargetPkg().FullName() << std::endl
;
1249 /* This bit is for processing the possibility of an install/upgrade
1250 fixing the problem for "positive" dependencies */
1251 if (Start
.IsNegative() == false && (DepState
[Start
->ID
] & DepCVer
) == DepCVer
)
1253 pkgCacheFile
CacheFile(this);
1254 APT::VersionList verlist
= APT::VersionList::FromDependency(CacheFile
, Start
, APT::CacheSetHelper::CANDIDATE
);
1255 CompareProviders
comp(Start
);
1258 APT::VersionList::iterator InstVer
= std::max_element(verlist
.begin(), verlist
.end(), comp
);
1260 if (InstVer
== verlist
.end())
1263 pkgCache::PkgIterator InstPkg
= InstVer
.ParentPkg();
1264 if(DebugAutoInstall
== true)
1265 std::clog
<< OutputInDepth(Depth
) << "Installing " << InstPkg
.Name()
1266 << " as " << Start
.DepType() << " of " << Pkg
.Name()
1268 if (MarkInstall(InstPkg
, true, Depth
+ 1, false, ForceImportantDeps
) == false)
1270 verlist
.erase(InstVer
);
1274 // now check if we should consider it a automatic dependency or not
1275 if(InstPkg
->CurrentVer
== 0 && MoveAutoBitToDependencies
)
1277 if(DebugAutoInstall
== true)
1278 std::clog
<< OutputInDepth(Depth
) << "Setting " << InstPkg
.FullName(false) << " NOT as auto-installed (direct "
1279 << Start
.DepType() << " of " << Pkg
.FullName(false) << " which is manual and in APT::Move-Autobit-Sections)" << std::endl
;
1280 MarkAuto(InstPkg
, false);
1288 /* Negative dependencies have no or-group
1289 If the dependency isn't versioned, we try if an upgrade might solve the problem.
1290 Otherwise we remove the offender if needed */
1291 else if (Start
.IsNegative() == true && Start
->Type
!= pkgCache::Dep::Obsoletes
)
1293 std::unique_ptr
<Version
*[]> List(Start
.AllTargets());
1294 pkgCache::PkgIterator TrgPkg
= Start
.TargetPkg();
1295 for (Version
**I
= List
.get(); *I
!= 0; I
++)
1297 VerIterator
Ver(*this,*I
);
1298 PkgIterator Pkg
= Ver
.ParentPkg();
1300 /* The List includes all packages providing this dependency,
1301 even providers which are not installed, so skip them. */
1302 if (PkgState
[Pkg
->ID
].InstallVer
== 0)
1305 /* Ignore negative dependencies that we are not going to
1307 if (PkgState
[Pkg
->ID
].InstallVer
!= *I
)
1310 if ((Start
->Version
!= 0 || TrgPkg
!= Pkg
) &&
1311 PkgState
[Pkg
->ID
].CandidateVer
!= PkgState
[Pkg
->ID
].InstallVer
&&
1312 PkgState
[Pkg
->ID
].CandidateVer
!= *I
&&
1313 MarkInstall(Pkg
,true,Depth
+ 1, false, ForceImportantDeps
) == true)
1315 else if (Start
->Type
== pkgCache::Dep::Conflicts
||
1316 Start
->Type
== pkgCache::Dep::DpkgBreaks
)
1318 if(DebugAutoInstall
== true)
1319 std::clog
<< OutputInDepth(Depth
)
1320 << " Removing: " << Pkg
.Name()
1322 if (MarkDelete(Pkg
,false,Depth
+ 1, false) == false)
1330 return Dep
.end() == true;
1333 // DepCache::IsInstallOk - check if it is ok to install this package /*{{{*/
1334 // ---------------------------------------------------------------------
1335 /* The default implementation checks if the installation of an M-A:same
1336 package would lead us into a version-screw and if so forbids it.
1337 dpkg holds are enforced by the private IsModeChangeOk */
1338 bool pkgDepCache::IsInstallOk(PkgIterator
const &Pkg
,bool AutoInst
,
1339 unsigned long Depth
, bool FromUser
)
1341 return IsInstallOkMultiArchSameVersionSynced(Pkg
,AutoInst
, Depth
, FromUser
) &&
1342 IsInstallOkDependenciesSatisfiableByCandidates(Pkg
,AutoInst
, Depth
, FromUser
);
1344 bool pkgDepCache::IsInstallOkMultiArchSameVersionSynced(PkgIterator
const &Pkg
,
1345 bool const /*AutoInst*/, unsigned long const Depth
, bool const FromUser
)
1347 if (FromUser
== true) // as always: user is always right
1350 // if we have checked before and it was okay, it will still be okay
1351 if (PkgState
[Pkg
->ID
].Mode
== ModeInstall
&&
1352 PkgState
[Pkg
->ID
].InstallVer
== PkgState
[Pkg
->ID
].CandidateVer
)
1355 // ignore packages with none-M-A:same candidates
1356 VerIterator
const CandVer
= PkgState
[Pkg
->ID
].CandidateVerIter(*this);
1357 if (unlikely(CandVer
.end() == true) || CandVer
== Pkg
.CurrentVer() ||
1358 (CandVer
->MultiArch
& pkgCache::Version::Same
) != pkgCache::Version::Same
)
1361 GrpIterator
const Grp
= Pkg
.Group();
1362 for (PkgIterator P
= Grp
.PackageList(); P
.end() == false; P
= Grp
.NextPkg(P
))
1364 // not installed or self-check: fine by definition
1365 if (P
->CurrentVer
== 0 || P
== Pkg
)
1368 // not having a candidate or being in sync
1369 // (simple string-compare as stuff like '1' == '0:1-0' can't happen here)
1370 VerIterator CV
= PkgState
[P
->ID
].CandidateVerIter(*this);
1371 if (CV
.end() == true || strcmp(CandVer
.VerStr(), CV
.VerStr()) == 0)
1374 // packages losing M-A:same can be out-of-sync
1375 if ((CV
->MultiArch
& pkgCache::Version::Same
) != pkgCache::Version::Same
)
1378 // not downloadable means the package is obsolete, so allow out-of-sync
1379 if (CV
.Downloadable() == false)
1382 PkgState
[Pkg
->ID
].iFlags
|= AutoKept
;
1383 if (unlikely(DebugMarker
== true))
1384 std::clog
<< OutputInDepth(Depth
) << "Ignore MarkInstall of " << APT::PrettyPkg(this, Pkg
)
1385 << " as it is not in sync with its M-A:same sibling " << APT::PrettyPkg(this, P
)
1386 << " (" << CandVer
.VerStr() << " != " << CV
.VerStr() << ")" << std::endl
;
1392 bool pkgDepCache::IsInstallOkDependenciesSatisfiableByCandidates(PkgIterator
const &Pkg
,
1393 bool const AutoInst
, unsigned long const Depth
, bool const /*FromUser*/)
1395 if (AutoInst
== false)
1398 VerIterator
const CandVer
= PkgState
[Pkg
->ID
].CandidateVerIter(*this);
1399 if (unlikely(CandVer
.end() == true) || CandVer
== Pkg
.CurrentVer())
1402 for (DepIterator Dep
= CandVer
.DependsList(); Dep
.end() != true;)
1405 DepIterator Start
= Dep
;
1408 for (bool LastOR
= true; Dep
.end() == false && LastOR
== true; ++Dep
, ++Ors
)
1410 LastOR
= (Dep
->CompareOp
& Dep::Or
) == Dep::Or
;
1412 if ((DepState
[Dep
->ID
] & DepInstall
) == DepInstall
)
1416 if (Start
.IsCritical() == false || Start
.IsNegative() == true || Result
== false)
1419 /* If we are in an or group locate the first or that can succeed.
1420 We have already cached this… */
1421 for (; Ors
> 1 && (DepState
[Start
->ID
] & DepCVer
) != DepCVer
; --Ors
)
1424 if (Ors
== 1 && (DepState
[Start
->ID
] &DepCVer
) != DepCVer
)
1426 if (DebugAutoInstall
== true)
1427 std::clog
<< OutputInDepth(Depth
) << APT::PrettyDep(this, Start
) << " can't be satisfied!" << std::endl
;
1429 // the dependency is critical, but can't be installed, so discard the candidate
1430 // as the problemresolver will trip over it otherwise trying to install it (#735967)
1431 if (Pkg
->CurrentVer
!= 0 && (PkgState
[Pkg
->ID
].iFlags
& Protected
) != Protected
)
1433 SetCandidateVersion(Pkg
.CurrentVer());
1434 StateCache
&State
= PkgState
[Pkg
->ID
];
1435 if (State
.Mode
!= ModeDelete
)
1437 State
.Mode
= ModeKeep
;
1438 State
.Update(Pkg
, *this);
1448 // DepCache::SetReInstall - Set the reinstallation flag /*{{{*/
1449 // ---------------------------------------------------------------------
1451 void pkgDepCache::SetReInstall(PkgIterator
const &Pkg
,bool To
)
1453 if (unlikely(Pkg
.end() == true))
1456 APT::PackageList pkglist
;
1457 if (Pkg
->CurrentVer
!= 0 &&
1458 (Pkg
.CurrentVer()-> MultiArch
& pkgCache::Version::Same
) == pkgCache::Version::Same
)
1460 pkgCache::GrpIterator Grp
= Pkg
.Group();
1461 for (pkgCache::PkgIterator P
= Grp
.PackageList(); P
.end() == false; P
= Grp
.NextPkg(P
))
1463 if (P
->CurrentVer
!= 0)
1468 pkglist
.insert(Pkg
);
1470 ActionGroup
group(*this);
1472 for (APT::PackageList::const_iterator Pkg
= pkglist
.begin(); Pkg
!= pkglist
.end(); ++Pkg
)
1477 StateCache
&P
= PkgState
[Pkg
->ID
];
1479 P
.iFlags
|= ReInstall
;
1481 P
.iFlags
&= ~ReInstall
;
1488 pkgCache::VerIterator
pkgDepCache::GetCandidateVersion(PkgIterator
const &Pkg
)/*{{{*/
1490 return PkgState
[Pkg
->ID
].CandidateVerIter(*this);
1493 // DepCache::SetCandidateVersion - Change the candidate version /*{{{*/
1494 // ---------------------------------------------------------------------
1496 void pkgDepCache::SetCandidateVersion(VerIterator TargetVer
)
1498 pkgCache::PkgIterator Pkg
= TargetVer
.ParentPkg();
1499 StateCache
&P
= PkgState
[Pkg
->ID
];
1501 if (P
.CandidateVer
== TargetVer
)
1504 ActionGroup
group(*this);
1509 if (P
.CandidateVer
== P
.InstallVer
&& P
.Install() == true)
1510 P
.InstallVer
= (Version
*)TargetVer
;
1511 P
.CandidateVer
= (Version
*)TargetVer
;
1512 P
.Update(Pkg
,*this);
1520 // DepCache::SetCandidateRelease - Change the candidate version /*{{{*/
1521 // ---------------------------------------------------------------------
1522 /* changes the candidate of a package and walks over all its dependencies
1523 to check if it needs to change the candidate of the dependency, too,
1524 to reach a installable versionstate */
1525 bool pkgDepCache::SetCandidateRelease(pkgCache::VerIterator TargetVer
,
1526 std::string
const &TargetRel
)
1528 std::list
<std::pair
<pkgCache::VerIterator
, pkgCache::VerIterator
> > Changed
;
1529 return SetCandidateRelease(TargetVer
, TargetRel
, Changed
);
1531 bool pkgDepCache::SetCandidateRelease(pkgCache::VerIterator TargetVer
,
1532 std::string
const &TargetRel
,
1533 std::list
<std::pair
<pkgCache::VerIterator
, pkgCache::VerIterator
> > &Changed
)
1535 ActionGroup
group(*this);
1536 SetCandidateVersion(TargetVer
);
1538 if (TargetRel
== "installed" || TargetRel
== "candidate") // both doesn't make sense in this context
1541 pkgVersionMatch
Match(TargetRel
, pkgVersionMatch::Release
);
1542 // save the position of the last element we will not undo - if we have to
1543 std::list
<std::pair
<pkgCache::VerIterator
, pkgCache::VerIterator
> >::iterator newChanged
= --(Changed
.end());
1545 for (pkgCache::DepIterator D
= TargetVer
.DependsList(); D
.end() == false; ++D
)
1547 if (D
->Type
!= pkgCache::Dep::PreDepends
&& D
->Type
!= pkgCache::Dep::Depends
&&
1548 ((D
->Type
!= pkgCache::Dep::Recommends
&& D
->Type
!= pkgCache::Dep::Suggests
) ||
1549 IsImportantDep(D
) == false))
1552 // walk over an or-group and check if we need to do anything
1553 // for simpilicity no or-group is handled as a or-group including one dependency
1554 pkgCache::DepIterator Start
= D
;
1555 bool itsFine
= false;
1556 for (bool stillOr
= true; stillOr
== true; ++Start
)
1558 stillOr
= (Start
->CompareOp
& Dep::Or
) == Dep::Or
;
1559 pkgCache::PkgIterator
const P
= Start
.TargetPkg();
1560 // virtual packages can't be a solution
1561 if (P
.end() == true || (P
->ProvidesList
== 0 && P
->VersionList
== 0))
1563 pkgCache::VerIterator
const Cand
= PkgState
[P
->ID
].CandidateVerIter(*this);
1564 // no versioned dependency - but is it installable?
1565 if (Start
.TargetVer() == 0 || Start
.TargetVer()[0] == '\0')
1567 // Check if one of the providers is installable
1568 if (P
->ProvidesList
!= 0)
1570 pkgCache::PrvIterator Prv
= P
.ProvidesList();
1571 for (; Prv
.end() == false; ++Prv
)
1573 pkgCache::VerIterator
const C
= PkgState
[Prv
.OwnerPkg()->ID
].CandidateVerIter(*this);
1574 if (C
.end() == true || C
!= Prv
.OwnerVer() ||
1575 (VersionState(C
.DependsList(), DepInstall
, DepCandMin
, DepCandPolicy
) & DepCandMin
) != DepCandMin
)
1579 if (Prv
.end() == true)
1582 // no providers, so check if we have an installable candidate version
1583 else if (Cand
.end() == true ||
1584 (VersionState(Cand
.DependsList(), DepInstall
, DepCandMin
, DepCandPolicy
) & DepCandMin
) != DepCandMin
)
1589 if (Cand
.end() == true)
1591 // check if the current candidate is enough for the versioned dependency - and installable?
1592 if (Start
.IsSatisfied(Cand
) == true &&
1593 (VersionState(Cand
.DependsList(), DepInstall
, DepCandMin
, DepCandPolicy
) & DepCandMin
) == DepCandMin
)
1600 if (itsFine
== true) {
1601 // something in the or-group was fine, skip all other members
1602 for (; (D
->CompareOp
& Dep::Or
) == Dep::Or
; ++D
);
1606 // walk again over the or-group and check each if a candidate switch would help
1608 for (bool stillOr
= true; stillOr
== true; ++D
)
1610 stillOr
= (D
->CompareOp
& Dep::Or
) == Dep::Or
;
1611 // changing candidate will not help if the dependency is not versioned
1612 if (D
.TargetVer() == 0 || D
.TargetVer()[0] == '\0')
1614 if (stillOr
== true)
1619 pkgCache::VerIterator V
;
1620 if (TargetRel
== "newest")
1621 V
= D
.TargetPkg().VersionList();
1623 V
= Match
.Find(D
.TargetPkg());
1625 // check if the version from this release could satisfy the dependency
1626 if (V
.end() == true || D
.IsSatisfied(V
) == false)
1628 if (stillOr
== true)
1633 pkgCache::VerIterator oldCand
= PkgState
[D
.TargetPkg()->ID
].CandidateVerIter(*this);
1636 // Do we already touched this Version? If so, their versioned dependencies are okay, no need to check again
1637 for (std::list
<std::pair
<pkgCache::VerIterator
, pkgCache::VerIterator
> >::const_iterator c
= Changed
.begin();
1638 c
!= Changed
.end(); ++c
)
1640 if (c
->first
->ParentPkg
!= V
->ParentPkg
)
1647 if (itsFine
== false)
1649 // change the candidate
1650 Changed
.push_back(make_pair(V
, TargetVer
));
1651 if (SetCandidateRelease(V
, TargetRel
, Changed
) == false)
1653 if (stillOr
== false)
1655 // undo the candidate changing
1656 SetCandidateVersion(oldCand
);
1663 // something in the or-group was fine, skip all other members
1664 for (; (D
->CompareOp
& Dep::Or
) == Dep::Or
; ++D
);
1668 if (itsFine
== false && (D
->Type
== pkgCache::Dep::PreDepends
|| D
->Type
== pkgCache::Dep::Depends
))
1670 // undo all changes which aren't lead to a solution
1671 for (std::list
<std::pair
<pkgCache::VerIterator
, pkgCache::VerIterator
> >::const_iterator c
= ++newChanged
;
1672 c
!= Changed
.end(); ++c
)
1673 SetCandidateVersion(c
->first
);
1674 Changed
.erase(newChanged
, Changed
.end());
1681 // DepCache::MarkAuto - set the Auto flag for a package /*{{{*/
1682 // ---------------------------------------------------------------------
1684 void pkgDepCache::MarkAuto(const PkgIterator
&Pkg
, bool Auto
)
1686 StateCache
&state
= PkgState
[Pkg
->ID
];
1688 ActionGroup
group(*this);
1691 state
.Flags
|= Flag::Auto
;
1693 state
.Flags
&= ~Flag::Auto
;
1696 // StateCache::Update - Compute the various static display things /*{{{*/
1697 // ---------------------------------------------------------------------
1698 /* This is called whenever the Candidate version changes. */
1699 void pkgDepCache::StateCache::Update(PkgIterator Pkg
,pkgCache
&Cache
)
1702 VerIterator Ver
= CandidateVerIter(Cache
);
1704 // Use a null string or the version string
1705 if (Ver
.end() == true)
1708 CandVersion
= Ver
.VerStr();
1710 // Find the current version
1712 if (Pkg
->CurrentVer
!= 0)
1713 CurVersion
= Pkg
.CurrentVer().VerStr();
1715 // Figure out if its up or down or equal
1716 Status
= Ver
.CompareVer(Pkg
.CurrentVer());
1717 if (Pkg
->CurrentVer
== 0 || Pkg
->VersionList
== 0 || CandidateVer
== 0)
1721 // StateCache::StripEpoch - Remove the epoch specifier from the version /*{{{*/
1722 // ---------------------------------------------------------------------
1724 const char *pkgDepCache::StateCache::StripEpoch(const char *Ver
)
1730 char const * const I
= strchr(Ver
, ':');
1736 // Policy::GetCandidateVer - Returns the Candidate install version /*{{{*/
1737 // ---------------------------------------------------------------------
1738 /* The default just returns the highest available version that is not
1739 a source and automatic. */
1740 pkgCache::VerIterator
pkgDepCache::Policy::GetCandidateVer(PkgIterator
const &Pkg
)
1742 /* Not source/not automatic versions cannot be a candidate version
1743 unless they are already installed */
1746 for (VerIterator I
= Pkg
.VersionList(); I
.end() == false; ++I
)
1748 if (Pkg
.CurrentVer() == I
)
1751 for (VerFileIterator J
= I
.FileList(); J
.end() == false; ++J
)
1753 if (J
.File().Flagged(Flag::NotSource
))
1756 /* Stash the highest version of a not-automatic source, we use it
1757 if there is nothing better */
1758 if (J
.File().Flagged(Flag::NotAutomatic
) ||
1759 J
.File().Flagged(Flag::ButAutomaticUpgrades
))
1761 if (Last
.end() == true)
1773 // Policy::IsImportantDep - True if the dependency is important /*{{{*/
1774 // ---------------------------------------------------------------------
1776 bool pkgDepCache::Policy::IsImportantDep(DepIterator
const &Dep
) const
1778 if(Dep
.IsCritical())
1780 else if(Dep
->Type
== pkgCache::Dep::Recommends
)
1782 if (InstallRecommends
)
1784 // we suport a special mode to only install-recommends for certain
1786 // FIXME: this is a meant as a temporarly solution until the
1787 // recommends are cleaned up
1788 const char *sec
= Dep
.ParentVer().Section();
1789 if (sec
&& ConfigValueInSubTree("APT::Install-Recommends-Sections", sec
))
1792 else if(Dep
->Type
== pkgCache::Dep::Suggests
)
1793 return InstallSuggests
;
1798 // Policy::GetPriority - Get the priority of the package pin /*{{{*/
1799 APT_CONST
signed short pkgDepCache::Policy::GetPriority(pkgCache::PkgIterator
const &/*Pkg*/)
1801 APT_CONST
signed short pkgDepCache::Policy::GetPriority(pkgCache::VerIterator
const &/*Ver*/, bool /*ConsiderFiles*/)
1803 APT_CONST
signed short pkgDepCache::Policy::GetPriority(pkgCache::PkgFileIterator
const &/*File*/)
1806 pkgDepCache::InRootSetFunc
*pkgDepCache::GetRootSetFunc() /*{{{*/
1808 DefaultRootSetFunc
*f
= new DefaultRootSetFunc
;
1809 if(f
->wasConstructedSuccessfully())
1818 bool pkgDepCache::MarkFollowsRecommends()
1820 return _config
->FindB("APT::AutoRemove::RecommendsImportant", true);
1823 bool pkgDepCache::MarkFollowsSuggests()
1825 return _config
->FindB("APT::AutoRemove::SuggestsImportant", true);
1828 // pkgDepCache::MarkRequired - the main mark algorithm /*{{{*/
1829 static bool IsPkgInBoringState(pkgCache::PkgIterator
const &Pkg
, pkgDepCache::StateCache
const * const PkgState
)
1831 if (Pkg
->CurrentVer
== 0)
1833 if (PkgState
[Pkg
->ID
].Keep())
1838 if (PkgState
[Pkg
->ID
].Delete())
1843 bool pkgDepCache::MarkRequired(InRootSetFunc
&userFunc
)
1845 if (_config
->Find("APT::Solver", "internal") != "internal")
1848 bool const debug_autoremove
= _config
->FindB("Debug::pkgAutoRemove",false);
1851 auto const PackagesCount
= Head().PackageCount
;
1852 for(auto i
= decltype(PackagesCount
){0}; i
< PackagesCount
; ++i
)
1854 PkgState
[i
].Marked
= false;
1855 PkgState
[i
].Garbage
= false;
1857 if (debug_autoremove
)
1858 for(PkgIterator p
= PkgBegin(); !p
.end(); ++p
)
1859 if(PkgState
[p
->ID
].Flags
& Flag::Auto
)
1860 std::clog
<< "AutoDep: " << p
.FullName() << std::endl
;
1862 bool const follow_recommends
= MarkFollowsRecommends();
1863 bool const follow_suggests
= MarkFollowsSuggests();
1865 // do the mark part, this is the core bit of the algorithm
1866 for (PkgIterator P
= PkgBegin(); !P
.end(); ++P
)
1868 if (PkgState
[P
->ID
].Marked
|| IsPkgInBoringState(P
, PkgState
))
1871 if ((PkgState
[P
->ID
].Flags
& Flag::Auto
) == 0)
1873 else if ((P
->Flags
& Flag::Essential
) || (P
->Flags
& Flag::Important
))
1875 // be nice even then a required package violates the policy (#583517)
1876 // and do the full mark process also for required packages
1877 else if (P
->CurrentVer
!= 0 && P
.CurrentVer()->Priority
== pkgCache::State::Required
)
1879 else if (userFunc
.InRootSet(P
))
1881 // packages which can't be changed (like holds) can't be garbage
1882 else if (IsModeChangeOk(ModeGarbage
, P
, 0, false) == false)
1887 if (PkgState
[P
->ID
].Install())
1888 MarkPackage(P
, PkgState
[P
->ID
].InstVerIter(*this),
1889 follow_recommends
, follow_suggests
);
1891 MarkPackage(P
, P
.CurrentVer(),
1892 follow_recommends
, follow_suggests
);
1898 // MarkPackage - mark a single package in Mark-and-Sweep /*{{{*/
1899 void pkgDepCache::MarkPackage(const pkgCache::PkgIterator
&Pkg
,
1900 const pkgCache::VerIterator
&Ver
,
1901 bool const &follow_recommends
,
1902 bool const &follow_suggests
)
1905 pkgDepCache::StateCache
&state
= PkgState
[Pkg
->ID
];
1906 // if we are marked already we are done
1907 if(state
.Marked
|| unlikely(Ver
.end()))
1912 if (IsPkgInBoringState(Pkg
, PkgState
))
1915 bool const debug_autoremove
= _config
->FindB("Debug::pkgAutoRemove", false);
1916 if(debug_autoremove
)
1917 std::clog
<< "Marking: " << Pkg
.FullName() << " " << Ver
.VerStr() << std::endl
;
1919 for (auto D
= Ver
.DependsList(); D
.end() == false; ++D
)
1921 auto const T
= D
.TargetPkg();
1922 if (PkgState
[T
->ID
].Marked
)
1925 if (D
->Type
!= Dep::Depends
&&
1926 D
->Type
!= Dep::PreDepends
&&
1927 (follow_recommends
== false || D
->Type
!= Dep::Recommends
) &&
1928 (follow_suggests
== false || D
->Type
!= Dep::Suggests
))
1931 // handle the virtual part first
1932 APT::VersionVector providers
;
1933 for(auto Prv
= T
.ProvidesList(); Prv
.end() == false; ++Prv
)
1935 auto PP
= Prv
.OwnerPkg();
1936 if (IsPkgInBoringState(PP
, PkgState
))
1939 // we want to ignore provides from uninteresting versions
1940 auto const PV
= (PkgState
[PP
->ID
].Install()) ?
1941 PkgState
[PP
->ID
].InstVerIter(*this) : PP
.CurrentVer();
1942 if (unlikely(PV
.end()) || PV
!= Prv
.OwnerVer() || D
.IsSatisfied(Prv
) == false)
1945 providers
.emplace_back(PV
);
1947 if (providers
.empty() == false)
1949 // sort providers by source version so that only the latest versioned
1950 // binary package of a source package is marked instead of all
1951 std::sort(providers
.begin(), providers
.end(),
1952 [](pkgCache::VerIterator
const &A
, pkgCache::VerIterator
const &B
) {
1953 auto const nameret
= strcmp(A
.SourcePkgName(), B
.SourcePkgName());
1956 auto const verret
= A
.Cache()->VS
->CmpVersion(A
.SourceVerStr(), B
.SourceVerStr());
1959 return strcmp(A
.ParentPkg().Name(), B
.ParentPkg().Name()) < 0;
1961 auto const prvsize
= providers
.size();
1962 providers
.erase(std::unique(providers
.begin(), providers
.end(),
1963 [](pkgCache::VerIterator
const &A
, pkgCache::VerIterator
const &B
) {
1964 return strcmp(A
.SourcePkgName(), B
.SourcePkgName()) == 0 &&
1965 strcmp(A
.SourceVerStr(), B
.SourceVerStr()) != 0;
1966 }), providers
.end());
1967 for (auto && PV
: providers
)
1969 auto const PP
= PV
.ParentPkg();
1970 if (debug_autoremove
)
1971 std::clog
<< "Following dep: " << APT::PrettyDep(this, D
)
1972 << ", provided by " << PP
.FullName() << " " << PV
.VerStr()
1973 << " (" << providers
.size() << "/" << prvsize
<< ")"<< std::endl
;
1974 MarkPackage(PP
, PV
, follow_recommends
, follow_suggests
);
1978 // now deal with the real part of the package
1979 if (IsPkgInBoringState(T
, PkgState
))
1982 auto const TV
= (PkgState
[T
->ID
].Install()) ?
1983 PkgState
[T
->ID
].InstVerIter(*this) : T
.CurrentVer();
1984 if (unlikely(TV
.end()) || D
.IsSatisfied(TV
) == false)
1987 if (debug_autoremove
)
1988 std::clog
<< "Following dep: " << APT::PrettyDep(this, D
) << std::endl
;
1989 MarkPackage(T
, TV
, follow_recommends
, follow_suggests
);
1993 bool pkgDepCache::Sweep() /*{{{*/
1995 bool debug_autoremove
= _config
->FindB("Debug::pkgAutoRemove",false);
1998 for(PkgIterator p
=PkgBegin(); !p
.end(); ++p
)
2000 StateCache
&state
=PkgState
[p
->ID
];
2002 // skip required packages
2003 if (!p
.CurrentVer().end() &&
2004 (p
.CurrentVer()->Priority
== pkgCache::State::Required
))
2007 // if it is not marked and it is installed, it's garbage
2008 if(!state
.Marked
&& (!p
.CurrentVer().end() || state
.Install()))
2011 if(debug_autoremove
)
2012 std::clog
<< "Garbage: " << p
.FullName() << std::endl
;
2019 // DepCache::MarkAndSweep /*{{{*/
2020 bool pkgDepCache::MarkAndSweep(InRootSetFunc
&rootFunc
)
2022 return MarkRequired(rootFunc
) && Sweep();
2024 bool pkgDepCache::MarkAndSweep()
2026 std::unique_ptr
<InRootSetFunc
> f(GetRootSetFunc());
2028 return MarkAndSweep(*f
.get());