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 // 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
)))
282 // The section is obsolete if it contains no other tag
283 unsigned int const count
= section
.Count();
285 (count
== 2 && section
.Exists("Auto-Installed")) ||
286 (count
== 3 && section
.Exists("Auto-Installed") && section
.Exists("Architecture")))
291 if(_config
->FindB("Debug::pkgAutoRemove",false))
292 std::clog
<< "Update existing AutoInstall info: "
293 << pkg
.FullName() << std::endl
;
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)
301 pkgs_seen
.insert(pkg
.FullName());
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()) {
310 std::clog
<< "Skipping already written " << pkg
.FullName() << std::endl
;
313 // skip not installed ones if requested
314 if (InstalledOnly
&& (
315 (pkg
->CurrentVer
== 0 && P
.Mode
!= ModeInstall
) ||
316 (pkg
->CurrentVer
!= 0 && P
.Mode
== ModeDelete
)))
318 const char* const pkgarch
= pkg
.Arch();
319 if (strcmp(pkgarch
, "all") == 0)
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)
331 if (StateFile
.Failed())
336 if (OutFile
.Close() == false)
338 chmod(state
.c_str(), 0644);
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
)
350 Res
= Dep
.TargetPkg();
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)
357 // Check the base package
358 if (Type
== NowVersion
)
360 if (Res
->CurrentVer
!= 0 && Dep
.IsSatisfied(Res
.CurrentVer()) == true)
363 else if (Type
== InstallVersion
)
365 if (PkgState
[Res
->ID
].InstallVer
!= 0 &&
366 Dep
.IsSatisfied(PkgState
[Res
->ID
].InstVerIter(*this)) == true)
369 else if (Type
== CandidateVersion
)
370 if (PkgState
[Res
->ID
].CandidateVer
!= 0 &&
371 Dep
.IsSatisfied(PkgState
[Res
->ID
].CandidateVerIter(*this)) == true)
375 if (Dep
->Type
== Dep::Obsoletes
)
378 // Check the providing packages
379 PrvIterator P
= Dep
.TargetPkg().ProvidesList();
380 for (; P
.end() != true; ++P
)
382 if (Dep
.IsIgnorable(P
) == true)
385 // Check if the provides is a hit
386 if (Type
== NowVersion
)
388 if (P
.OwnerPkg().CurrentVer() != P
.OwnerVer())
391 else if (Type
== InstallVersion
)
393 StateCache
&State
= PkgState
[P
.OwnerPkg()->ID
];
394 if (State
.InstallVer
!= (Version
*)P
.OwnerVer())
397 else if (Type
== CandidateVersion
)
399 StateCache
&State
= PkgState
[P
.OwnerPkg()->ID
];
400 if (State
.CandidateVer
!= (Version
*)P
.OwnerVer())
404 // Compare the versions.
405 if (Dep
.IsSatisfied(P
) == true)
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
)
420 StateCache
&P
= PkgState
[Pkg
->ID
];
422 if (Pkg
->VersionList
== 0)
425 if (Pkg
.State() == pkgCache::PkgIterator::NeedsConfigure
&&
429 // Compute the size data
430 if (P
.NewInstall() == true)
432 if (Inverse
== false) {
433 iUsrSize
+= P
.InstVerIter(*this)->InstalledSize
;
434 iDownloadSize
+= P
.InstVerIter(*this)->Size
;
436 iUsrSize
-= P
.InstVerIter(*this)->InstalledSize
;
437 iDownloadSize
-= P
.InstVerIter(*this)->Size
;
443 if (Pkg
->CurrentVer
!= 0 &&
444 (P
.InstallVer
!= (Version
*)Pkg
.CurrentVer() ||
445 (P
.iFlags
& ReInstall
) == ReInstall
) && P
.InstallVer
!= 0)
447 if (Inverse
== false) {
448 iUsrSize
-= Pkg
.CurrentVer()->InstalledSize
;
449 iUsrSize
+= P
.InstVerIter(*this)->InstalledSize
;
450 iDownloadSize
+= P
.InstVerIter(*this)->Size
;
452 iUsrSize
-= P
.InstVerIter(*this)->InstalledSize
;
453 iUsrSize
+= Pkg
.CurrentVer()->InstalledSize
;
454 iDownloadSize
-= P
.InstVerIter(*this)->Size
;
460 if (Pkg
.State() == pkgCache::PkgIterator::NeedsUnpack
&&
463 if (Inverse
== false)
464 iDownloadSize
+= P
.InstVerIter(*this)->Size
;
466 iDownloadSize
-= P
.InstVerIter(*this)->Size
;
471 if (Pkg
->CurrentVer
!= 0 && P
.InstallVer
== 0)
473 if (Inverse
== false)
474 iUsrSize
-= Pkg
.CurrentVer()->InstalledSize
;
476 iUsrSize
+= Pkg
.CurrentVer()->InstalledSize
;
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
)
491 signed char const Add
= (Invert
== false) ? 1 : -1;
492 StateCache
&State
= PkgState
[Pkg
->ID
];
494 // The Package is broken (either minimal dep or policy dep)
495 if ((State
.DepState
& DepInstMin
) != DepInstMin
)
497 if ((State
.DepState
& DepInstPolicy
) != DepInstPolicy
)
498 iPolicyBrokenCount
+= Add
;
501 if (Pkg
.State() != PkgIterator::NeedsNothing
)
505 if (Pkg
->CurrentVer
== 0)
507 if (State
.Mode
== ModeDelete
&&
508 (State
.iFlags
& Purge
) == Purge
&& Pkg
.Purge() == false)
511 if (State
.Mode
== ModeInstall
)
516 // Installed, no upgrade
517 if (State
.Status
== 0)
519 if (State
.Mode
== ModeDelete
)
522 if ((State
.iFlags
& ReInstall
) == ReInstall
)
527 // Alll 3 are possible
528 if (State
.Mode
== ModeDelete
)
530 else if (State
.Mode
== ModeKeep
)
532 else if (State
.Mode
== ModeInstall
)
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
)
542 unsigned char Group
= 0;
543 for (DepIterator D
= V
.DependsList(); D
.end() != true; ++D
)
545 // Build the dependency state.
546 unsigned char &State
= DepState
[D
->ID
];
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)
553 // Add to the group if we are within an or..
557 if ((D
->CompareOp
& Dep::Or
) != Dep::Or
)
560 // Invert for Conflicts
561 if (D
.IsNegative() == true)
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
576 unsigned char Dep
= 0xFF;
577 while (D
.end() != true)
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);
586 // Minimum deps that must be satisfied to have a working package
587 if (Start
.IsCritical() == true)
589 if ((State
& Check
) != Check
)
590 return Dep
&= ~(SetMin
| SetPolicy
);
592 // Policy deps that must be satisfied to install the package
593 else if (IsImportantDep(Start
) == true &&
594 (State
& Check
) != Check
)
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
)
607 unsigned char State
= 0;
609 if (CheckDep(D
,NowVersion
) == true)
611 if (CheckDep(D
,InstallVersion
) == true)
613 if (CheckDep(D
,CandidateVersion
) == true)
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
)
626 // Empty deps are always true
627 StateCache
&State
= PkgState
[Pkg
->ID
];
628 State
.DepState
= 0xFF;
630 // Check the Current state
631 if (Pkg
->CurrentVer
!= 0)
633 DepIterator D
= Pkg
.CurrentVer().DependsList();
634 State
.DepState
&= VersionState(D
,DepNow
,DepNowMin
,DepNowPolicy
);
637 /* Check the candidate state. We do not compare against the whole as
638 a candidate state but check the candidate version against the
640 if (State
.CandidateVer
!= 0)
642 DepIterator D
= State
.CandidateVerIter(*this).DependsList();
643 State
.DepState
&= VersionState(D
,DepInstall
,DepCandMin
,DepCandPolicy
);
646 // Check target state which can only be current or installed
647 if (State
.InstallVer
!= 0)
649 DepIterator D
= State
.InstVerIter(*this).DependsList();
650 State
.DepState
&= VersionState(D
,DepInstall
,DepInstMin
,DepInstPolicy
);
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
)
666 iPolicyBrokenCount
= 0;
669 // Perform the depends pass
671 for (PkgIterator I
= PkgBegin(); I
.end() != true; ++I
, ++Done
)
673 if (Prog
!= 0 && Done%20
== 0)
674 Prog
->Progress(Done
);
675 for (VerIterator V
= I
.VersionList(); V
.end() != true; ++V
)
677 unsigned char Group
= 0;
679 for (DepIterator D
= V
.DependsList(); D
.end() != true; ++D
)
681 // Build the dependency state.
682 unsigned char &State
= DepState
[D
->ID
];
683 State
= DependencyState(D
);
685 // Add to the group if we are within an or..
688 if ((D
->CompareOp
& Dep::Or
) != Dep::Or
)
691 // Invert for Conflicts
692 if (D
.IsNegative() == true)
697 // Compute the package dependency state and size additions
704 Prog
->Progress(Done
);
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
)
715 // Update the reverse deps
716 for (;D
.end() != true; ++D
)
718 unsigned char &State
= DepState
[D
->ID
];
719 State
= DependencyState(D
);
721 // Invert for Conflicts
722 if (D
.IsNegative() == true)
725 RemoveStates(D
.ParentPkg());
726 BuildGroupOrs(D
.ParentVer());
727 UpdateVerState(D
.ParentPkg());
728 AddStates(D
.ParentPkg());
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
)
738 // Recompute the dep of the package
743 // Update the reverse deps
744 Update(Pkg
.RevDependsList());
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());
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());
759 // DepCache::MarkKeep - Put the package in the keep state /*{{{*/
760 // ---------------------------------------------------------------------
762 bool pkgDepCache::MarkKeep(PkgIterator
const &Pkg
, bool Soft
, bool FromUser
,
765 if (IsModeChangeOk(ModeKeep
, Pkg
, Depth
, FromUser
) == false)
768 /* Reject an attempt to keep a non-source broken installed package, those
770 if (Pkg
.State() == PkgIterator::NeedsUnpack
&&
771 Pkg
.CurrentVer().Downloadable() == false)
774 /* We changed the soft state all the time so the UI is a bit nicer
776 StateCache
&P
= PkgState
[Pkg
->ID
];
778 // Check that it is not already kept
779 if (P
.Mode
== ModeKeep
)
783 P
.iFlags
|= AutoKept
;
785 P
.iFlags
&= ~AutoKept
;
787 ActionGroup
group(*this);
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
;
800 if (DebugMarker
== true)
801 std::clog
<< OutputInDepth(Depth
) << "MarkKeep " << APT::PrettyPkg(this, Pkg
) << " FU=" << FromUser
<< std::endl
;
807 if (Pkg
->CurrentVer
== 0)
810 P
.InstallVer
= Pkg
.CurrentVer();
819 // DepCache::MarkDelete - Put the package in the delete state /*{{{*/
820 // ---------------------------------------------------------------------
822 bool pkgDepCache::MarkDelete(PkgIterator
const &Pkg
, bool rPurge
,
823 unsigned long Depth
, bool FromUser
)
825 if (IsModeChangeOk(ModeDelete
, Pkg
, Depth
, FromUser
) == false)
828 StateCache
&P
= PkgState
[Pkg
->ID
];
830 // Check that it is not already marked for delete
831 if ((P
.Mode
== ModeDelete
|| P
.InstallVer
== 0) &&
832 (Pkg
.Purge() == true || rPurge
== false))
835 // check if we are allowed to remove the package
836 if (IsDeleteOk(Pkg
,rPurge
,Depth
,FromUser
) == false)
839 P
.iFlags
&= ~(AutoKept
| Purge
);
843 ActionGroup
group(*this);
845 if (FromUser
== false)
847 VerIterator
const PV
= P
.InstVerIter(*this);
848 if (PV
.end() == false)
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
)
859 for (DepIterator D
= PV
.DependsList(); D
.end() != true; ++D
)
861 if (D
.IsMultiArchImplicit() == true || D
.IsNegative() == true || IsImportantDep(D
) == false)
864 pkgCacheFile
CacheFile(this);
865 APT::VersionList verlist
= APT::VersionList::FromDependency(CacheFile
, D
, APT::CacheSetHelper::INSTALLED
);
866 for (auto const &V
: verlist
)
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
;
880 if (DebugMarker
== true)
881 std::clog
<< OutputInDepth(Depth
) << (rPurge
? "MarkPurge " : "MarkDelete ") << APT::PrettyPkg(this, Pkg
) << " FU=" << FromUser
<< std::endl
;
886 if (Pkg
->CurrentVer
== 0 && (Pkg
.Purge() == true || rPurge
== false))
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
)
906 return IsDeleteOkProtectInstallRequests(Pkg
, rPurge
, Depth
, FromUser
);
908 bool pkgDepCache::IsDeleteOkProtectInstallRequests(PkgIterator
const &Pkg
,
909 bool const /*rPurge*/, unsigned long const Depth
, bool const FromUser
)
911 if (FromUser
== false && Pkg
->CurrentVer
== 0)
913 StateCache
&P
= PkgState
[Pkg
->ID
];
914 if (P
.InstallVer
!= 0 && P
.Status
== 2 && (P
.Flags
& Flag::Auto
) != Flag::Auto
)
916 if (DebugMarker
== true)
917 std::clog
<< OutputInDepth(Depth
) << "Manual install request prevents MarkDelete of " << APT::PrettyPkg(this, Pkg
) << std::endl
;
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
)
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";
941 bool pkgDepCache::IsModeChangeOk(ModeList
const mode
, PkgIterator
const &Pkg
,
942 unsigned long const Depth
, bool const FromUser
)
944 // we are not trying to hard…
945 if (unlikely(Depth
> 100))
949 if (unlikely(Pkg
.end() == true || Pkg
->VersionList
== 0))
952 // the user is always right
953 if (FromUser
== true)
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
962 // if previous state was set by user only user can reset it
963 if ((P
.iFlags
& Protected
) == Protected
)
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
;
971 // enforce dpkg holds
972 else if (mode
!= ModeKeep
&& Pkg
->SelectedState
== pkgCache::State::Hold
&&
973 _config
->FindB("APT::Ignore-Hold",false) == false)
975 if (unlikely(DebugMarker
== true))
976 std::clog
<< OutputInDepth(Depth
) << "Hold prevents Mark" << PrintMode(mode
)
977 << " of " << APT::PrettyPkg(this, Pkg
) << std::endl
;
984 // DepCache::MarkInstall - Put the package in the install state /*{{{*/
985 // ---------------------------------------------------------------------
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
)
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
)
1000 if ((AV
->MultiArch
& pkgCache::Version::Same
) == pkgCache::Version::Same
)
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)
1011 if ((BV
->MultiArch
& pkgCache::Version::Same
) == pkgCache::Version::Same
)
1013 pkgCache::GrpIterator Grp
= B
.Group();
1014 for (pkgCache::PkgIterator P
= Grp
.PackageList(); P
.end() == false; P
= Grp
.NextPkg(P
))
1016 if (P
->CurrentVer
!= 0)
1024 return instA
== false;
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
)
1031 if (A
->Group
== Pkg
->Group
&& B
->Group
!= Pkg
->Group
)
1033 else if (B
->Group
== Pkg
->Group
&& A
->Group
!= Pkg
->Group
)
1036 // we like essentials
1037 if ((A
->Flags
& pkgCache::Flag::Essential
) != (B
->Flags
& pkgCache::Flag::Essential
))
1039 if ((A
->Flags
& pkgCache::Flag::Essential
) == pkgCache::Flag::Essential
)
1041 else if ((B
->Flags
& pkgCache::Flag::Essential
) == pkgCache::Flag::Essential
)
1044 if ((A
->Flags
& pkgCache::Flag::Important
) != (B
->Flags
& pkgCache::Flag::Important
))
1046 if ((A
->Flags
& pkgCache::Flag::Important
) == pkgCache::Flag::Important
)
1048 else if ((B
->Flags
& pkgCache::Flag::Important
) == pkgCache::Flag::Important
)
1051 // prefer native architecture
1052 if (strcmp(A
.Arch(), B
.Arch()) != 0)
1054 if (strcmp(A
.Arch(), A
.Cache()->NativeArch()) == 0)
1056 else if (strcmp(B
.Arch(), B
.Cache()->NativeArch()) == 0)
1058 std::vector
<std::string
> archs
= APT::Configuration::getArchitectures();
1059 for (std::vector
<std::string
>::const_iterator a
= archs
.begin(); a
!= archs
.end(); ++a
)
1062 else if (*a
== B
.Arch())
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
;
1072 bool pkgDepCache::MarkInstall(PkgIterator
const &Pkg
,bool AutoInst
,
1073 unsigned long Depth
, bool FromUser
,
1074 bool ForceImportantDeps
)
1076 if (IsModeChangeOk(ModeInstall
, Pkg
, Depth
, FromUser
) == false)
1079 StateCache
&P
= PkgState
[Pkg
->ID
];
1081 // See if there is even any possible instalation candidate
1082 if (P
.CandidateVer
== 0)
1085 /* Check that it is not already marked for install and that it can be
1087 if ((P
.InstPolicyBroken() == false && P
.InstBroken() == false) &&
1088 (P
.Mode
== ModeInstall
||
1089 P
.CandidateVer
== (Version
*)Pkg
.CurrentVer()))
1091 if (P
.CandidateVer
== (Version
*)Pkg
.CurrentVer() && P
.InstallVer
== 0)
1092 return MarkKeep(Pkg
, false, FromUser
, Depth
+1);
1096 // check if we are allowed to install the package
1097 if (IsInstallOk(Pkg
,AutoInst
,Depth
,FromUser
) == false)
1100 ActionGroup
group(*this);
1101 P
.iFlags
&= ~AutoKept
;
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 */
1109 P
.Mode
= ModeInstall
;
1110 P
.InstallVer
= P
.CandidateVer
;
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
;
1122 // Set it to auto if this is a new install.
1124 P
.Flags
|= Flag::Auto
;
1126 if (P
.CandidateVer
== (Version
*)Pkg
.CurrentVer())
1133 if (AutoInst
== false || _config
->Find("APT::Solver", "internal") != "internal")
1136 if (DebugMarker
== true)
1137 std::clog
<< OutputInDepth(Depth
) << "MarkInstall " << APT::PrettyPkg(this, Pkg
) << " FU=" << FromUser
<< std::endl
;
1139 bool MoveAutoBitToDependencies
= false;
1140 VerIterator
const PV
= P
.InstVerIter(*this);
1141 if (unlikely(PV
.end() == true))
1143 else if (PV
->Section
!= 0 && (P
.Flags
& Flag::Auto
) != Flag::Auto
)
1145 VerIterator
const CurVer
= Pkg
.CurrentVer();
1146 if (CurVer
.end() == false && CurVer
->Section
!= 0 && strcmp(CurVer
.Section(), PV
.Section()) != 0)
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)
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);
1160 DepIterator Dep
= PV
.DependsList();
1161 for (; Dep
.end() != true;)
1164 DepIterator Start
= Dep
;
1167 for (bool LastOR
= true; Dep
.end() == false && LastOR
== true; ++Dep
, ++Ors
)
1169 LastOR
= (Dep
->CompareOp
& Dep::Or
) == Dep::Or
;
1171 if ((DepState
[Dep
->ID
] & DepInstall
) == DepInstall
)
1175 // Dep is satisfied okay.
1176 if (Result
== false)
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)
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
)
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)
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)
1204 if (Pkg
->CurrentVer
!= 0 && ForceImportantDeps
== false && Start
.IsCritical() == false)
1206 bool isNewImportantDep
= true;
1207 bool isPreviouslySatisfiedImportantDep
= false;
1208 for (DepIterator D
= Pkg
.CurrentVer().DependsList(); D
.end() != true; ++D
)
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())
1217 isNewImportantDep
= false;
1219 while ((D
->CompareOp
& Dep::Or
) != 0)
1222 isPreviouslySatisfiedImportantDep
= (((*this)[D
] & DepGNow
) != 0);
1223 if (isPreviouslySatisfiedImportantDep
== true)
1227 if(isNewImportantDep
== true)
1229 if (DebugAutoInstall
== true)
1230 std::clog
<< OutputInDepth(Depth
) << "new important dependency: "
1231 << Start
.TargetPkg().FullName() << std::endl
;
1233 else if(isPreviouslySatisfiedImportantDep
== true)
1235 if (DebugAutoInstall
== true)
1236 std::clog
<< OutputInDepth(Depth
) << "previously satisfied important dependency on "
1237 << Start
.TargetPkg().FullName() << std::endl
;
1241 if (DebugAutoInstall
== true)
1242 std::clog
<< OutputInDepth(Depth
) << "ignore old unsatisfied important dependency on "
1243 << Start
.TargetPkg().FullName() << std::endl
;
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
)
1252 pkgCacheFile
CacheFile(this);
1253 APT::VersionList verlist
= APT::VersionList::FromDependency(CacheFile
, Start
, APT::CacheSetHelper::CANDIDATE
);
1254 CompareProviders
comp(Start
);
1257 APT::VersionList::iterator InstVer
= std::max_element(verlist
.begin(), verlist
.end(), comp
);
1259 if (InstVer
== verlist
.end())
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()
1267 if (MarkInstall(InstPkg
, true, Depth
+ 1, false, ForceImportantDeps
) == false)
1269 verlist
.erase(InstVer
);
1273 // now check if we should consider it a automatic dependency or not
1274 if(InstPkg
->CurrentVer
== 0 && MoveAutoBitToDependencies
)
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);
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
)
1292 std::unique_ptr
<Version
*[]> List(Start
.AllTargets());
1293 pkgCache::PkgIterator TrgPkg
= Start
.TargetPkg();
1294 for (Version
**I
= List
.get(); *I
!= 0; I
++)
1296 VerIterator
Ver(*this,*I
);
1297 PkgIterator Pkg
= Ver
.ParentPkg();
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)
1304 /* Ignore negative dependencies that we are not going to
1306 if (PkgState
[Pkg
->ID
].InstallVer
!= *I
)
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)
1314 else if (Start
->Type
== pkgCache::Dep::Conflicts
||
1315 Start
->Type
== pkgCache::Dep::DpkgBreaks
)
1317 if(DebugAutoInstall
== true)
1318 std::clog
<< OutputInDepth(Depth
)
1319 << " Removing: " << Pkg
.Name()
1321 if (MarkDelete(Pkg
,false,Depth
+ 1, false) == false)
1329 return Dep
.end() == true;
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
)
1340 return IsInstallOkMultiArchSameVersionSynced(Pkg
,AutoInst
, Depth
, FromUser
) &&
1341 IsInstallOkDependenciesSatisfiableByCandidates(Pkg
,AutoInst
, Depth
, FromUser
);
1343 bool pkgDepCache::IsInstallOkMultiArchSameVersionSynced(PkgIterator
const &Pkg
,
1344 bool const /*AutoInst*/, unsigned long const Depth
, bool const FromUser
)
1346 if (FromUser
== true) // as always: user is always right
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
)
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
)
1360 GrpIterator
const Grp
= Pkg
.Group();
1361 for (PkgIterator P
= Grp
.PackageList(); P
.end() == false; P
= Grp
.NextPkg(P
))
1363 // not installed or self-check: fine by definition
1364 if (P
->CurrentVer
== 0 || P
== Pkg
)
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)
1373 // packages losing M-A:same can be out-of-sync
1374 if ((CV
->MultiArch
& pkgCache::Version::Same
) != pkgCache::Version::Same
)
1377 // not downloadable means the package is obsolete, so allow out-of-sync
1378 if (CV
.Downloadable() == false)
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
;
1391 bool pkgDepCache::IsInstallOkDependenciesSatisfiableByCandidates(PkgIterator
const &Pkg
,
1392 bool const AutoInst
, unsigned long const Depth
, bool const /*FromUser*/)
1394 if (AutoInst
== false)
1397 VerIterator
const CandVer
= PkgState
[Pkg
->ID
].CandidateVerIter(*this);
1398 if (unlikely(CandVer
.end() == true) || CandVer
== Pkg
.CurrentVer())
1401 for (DepIterator Dep
= CandVer
.DependsList(); Dep
.end() != true;)
1404 DepIterator Start
= Dep
;
1407 for (bool LastOR
= true; Dep
.end() == false && LastOR
== true; ++Dep
, ++Ors
)
1409 LastOR
= (Dep
->CompareOp
& Dep::Or
) == Dep::Or
;
1411 if ((DepState
[Dep
->ID
] & DepInstall
) == DepInstall
)
1415 if (Start
.IsCritical() == false || Start
.IsNegative() == true || Result
== false)
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
)
1423 if (Ors
== 1 && (DepState
[Start
->ID
] &DepCVer
) != DepCVer
)
1425 if (DebugAutoInstall
== true)
1426 std::clog
<< OutputInDepth(Depth
) << APT::PrettyDep(this, Start
) << " can't be satisfied!" << std::endl
;
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
)
1432 SetCandidateVersion(Pkg
.CurrentVer());
1433 StateCache
&State
= PkgState
[Pkg
->ID
];
1434 if (State
.Mode
!= ModeDelete
)
1436 State
.Mode
= ModeKeep
;
1437 State
.Update(Pkg
, *this);
1447 // DepCache::SetReInstall - Set the reinstallation flag /*{{{*/
1448 // ---------------------------------------------------------------------
1450 void pkgDepCache::SetReInstall(PkgIterator
const &Pkg
,bool To
)
1452 if (unlikely(Pkg
.end() == true))
1455 APT::PackageList pkglist
;
1456 if (Pkg
->CurrentVer
!= 0 &&
1457 (Pkg
.CurrentVer()-> MultiArch
& pkgCache::Version::Same
) == pkgCache::Version::Same
)
1459 pkgCache::GrpIterator Grp
= Pkg
.Group();
1460 for (pkgCache::PkgIterator P
= Grp
.PackageList(); P
.end() == false; P
= Grp
.NextPkg(P
))
1462 if (P
->CurrentVer
!= 0)
1467 pkglist
.insert(Pkg
);
1469 ActionGroup
group(*this);
1471 for (APT::PackageList::const_iterator Pkg
= pkglist
.begin(); Pkg
!= pkglist
.end(); ++Pkg
)
1476 StateCache
&P
= PkgState
[Pkg
->ID
];
1478 P
.iFlags
|= ReInstall
;
1480 P
.iFlags
&= ~ReInstall
;
1487 pkgCache::VerIterator
pkgDepCache::GetCandidateVersion(PkgIterator
const &Pkg
)/*{{{*/
1489 return PkgState
[Pkg
->ID
].CandidateVerIter(*this);
1492 // DepCache::SetCandidateVersion - Change the candidate version /*{{{*/
1493 // ---------------------------------------------------------------------
1495 void pkgDepCache::SetCandidateVersion(VerIterator TargetVer
)
1497 pkgCache::PkgIterator Pkg
= TargetVer
.ParentPkg();
1498 StateCache
&P
= PkgState
[Pkg
->ID
];
1500 if (P
.CandidateVer
== TargetVer
)
1503 ActionGroup
group(*this);
1508 if (P
.CandidateVer
== P
.InstallVer
&& P
.Install() == true)
1509 P
.InstallVer
= (Version
*)TargetVer
;
1510 P
.CandidateVer
= (Version
*)TargetVer
;
1511 P
.Update(Pkg
,*this);
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
)
1527 std::list
<std::pair
<pkgCache::VerIterator
, pkgCache::VerIterator
> > Changed
;
1528 return SetCandidateRelease(TargetVer
, TargetRel
, Changed
);
1530 bool pkgDepCache::SetCandidateRelease(pkgCache::VerIterator TargetVer
,
1531 std::string
const &TargetRel
,
1532 std::list
<std::pair
<pkgCache::VerIterator
, pkgCache::VerIterator
> > &Changed
)
1534 ActionGroup
group(*this);
1535 SetCandidateVersion(TargetVer
);
1537 if (TargetRel
== "installed" || TargetRel
== "candidate") // both doesn't make sense in this context
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());
1544 for (pkgCache::DepIterator D
= TargetVer
.DependsList(); D
.end() == false; ++D
)
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))
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
)
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))
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')
1566 // Check if one of the providers is installable
1567 if (P
->ProvidesList
!= 0)
1569 pkgCache::PrvIterator Prv
= P
.ProvidesList();
1570 for (; Prv
.end() == false; ++Prv
)
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
)
1578 if (Prv
.end() == true)
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
)
1588 if (Cand
.end() == true)
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
)
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
);
1605 // walk again over the or-group and check each if a candidate switch would help
1607 for (bool stillOr
= true; stillOr
== true; ++D
)
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')
1613 if (stillOr
== true)
1618 pkgCache::VerIterator V
;
1619 if (TargetRel
== "newest")
1620 V
= D
.TargetPkg().VersionList();
1622 V
= Match
.Find(D
.TargetPkg());
1624 // check if the version from this release could satisfy the dependency
1625 if (V
.end() == true || D
.IsSatisfied(V
) == false)
1627 if (stillOr
== true)
1632 pkgCache::VerIterator oldCand
= PkgState
[D
.TargetPkg()->ID
].CandidateVerIter(*this);
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
)
1639 if (c
->first
->ParentPkg
!= V
->ParentPkg
)
1646 if (itsFine
== false)
1648 // change the candidate
1649 Changed
.push_back(make_pair(V
, TargetVer
));
1650 if (SetCandidateRelease(V
, TargetRel
, Changed
) == false)
1652 if (stillOr
== false)
1654 // undo the candidate changing
1655 SetCandidateVersion(oldCand
);
1662 // something in the or-group was fine, skip all other members
1663 for (; (D
->CompareOp
& Dep::Or
) == Dep::Or
; ++D
);
1667 if (itsFine
== false && (D
->Type
== pkgCache::Dep::PreDepends
|| D
->Type
== pkgCache::Dep::Depends
))
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());
1680 // DepCache::MarkAuto - set the Auto flag for a package /*{{{*/
1681 // ---------------------------------------------------------------------
1683 void pkgDepCache::MarkAuto(const PkgIterator
&Pkg
, bool Auto
)
1685 StateCache
&state
= PkgState
[Pkg
->ID
];
1687 ActionGroup
group(*this);
1690 state
.Flags
|= Flag::Auto
;
1692 state
.Flags
&= ~Flag::Auto
;
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
)
1701 VerIterator Ver
= CandidateVerIter(Cache
);
1703 // Use a null string or the version string
1704 if (Ver
.end() == true)
1707 CandVersion
= Ver
.VerStr();
1709 // Find the current version
1711 if (Pkg
->CurrentVer
!= 0)
1712 CurVersion
= Pkg
.CurrentVer().VerStr();
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)
1720 // StateCache::StripEpoch - Remove the epoch specifier from the version /*{{{*/
1721 // ---------------------------------------------------------------------
1723 const char *pkgDepCache::StateCache::StripEpoch(const char *Ver
)
1729 char const * const I
= strchr(Ver
, ':');
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
)
1741 /* Not source/not automatic versions cannot be a candidate version
1742 unless they are already installed */
1745 for (VerIterator I
= Pkg
.VersionList(); I
.end() == false; ++I
)
1747 if (Pkg
.CurrentVer() == I
)
1750 for (VerFileIterator J
= I
.FileList(); J
.end() == false; ++J
)
1752 if (J
.File().Flagged(Flag::NotSource
))
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
))
1760 if (Last
.end() == true)
1772 // Policy::IsImportantDep - True if the dependency is important /*{{{*/
1773 // ---------------------------------------------------------------------
1775 bool pkgDepCache::Policy::IsImportantDep(DepIterator
const &Dep
) const
1777 if(Dep
.IsCritical())
1779 else if(Dep
->Type
== pkgCache::Dep::Recommends
)
1781 if (InstallRecommends
)
1783 // we suport a special mode to only install-recommends for certain
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
))
1791 else if(Dep
->Type
== pkgCache::Dep::Suggests
)
1792 return InstallSuggests
;
1797 // Policy::GetPriority - Get the priority of the package pin /*{{{*/
1798 APT_CONST
signed short pkgDepCache::Policy::GetPriority(pkgCache::PkgIterator
const &/*Pkg*/)
1800 APT_CONST
signed short pkgDepCache::Policy::GetPriority(pkgCache::VerIterator
const &/*Ver*/, bool /*ConsiderFiles*/)
1802 APT_CONST
signed short pkgDepCache::Policy::GetPriority(pkgCache::PkgFileIterator
const &/*File*/)
1805 pkgDepCache::InRootSetFunc
*pkgDepCache::GetRootSetFunc() /*{{{*/
1807 DefaultRootSetFunc
*f
= new DefaultRootSetFunc
;
1808 if(f
->wasConstructedSuccessfully())
1817 bool pkgDepCache::MarkFollowsRecommends()
1819 return _config
->FindB("APT::AutoRemove::RecommendsImportant", true);
1822 bool pkgDepCache::MarkFollowsSuggests()
1824 return _config
->FindB("APT::AutoRemove::SuggestsImportant", true);
1827 // pkgDepCache::MarkRequired - the main mark algorithm /*{{{*/
1828 static bool IsPkgInBoringState(pkgCache::PkgIterator
const &Pkg
, pkgDepCache::StateCache
const * const PkgState
)
1830 if (Pkg
->CurrentVer
== 0)
1832 if (PkgState
[Pkg
->ID
].Keep())
1837 if (PkgState
[Pkg
->ID
].Delete())
1842 bool pkgDepCache::MarkRequired(InRootSetFunc
&userFunc
)
1844 if (_config
->Find("APT::Solver", "internal") != "internal")
1847 bool const debug_autoremove
= _config
->FindB("Debug::pkgAutoRemove",false);
1850 auto const PackagesCount
= Head().PackageCount
;
1851 for(auto i
= decltype(PackagesCount
){0}; i
< PackagesCount
; ++i
)
1853 PkgState
[i
].Marked
= false;
1854 PkgState
[i
].Garbage
= false;
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
;
1861 bool const follow_recommends
= MarkFollowsRecommends();
1862 bool const follow_suggests
= MarkFollowsSuggests();
1864 // do the mark part, this is the core bit of the algorithm
1865 for (PkgIterator P
= PkgBegin(); !P
.end(); ++P
)
1867 if (PkgState
[P
->ID
].Marked
|| IsPkgInBoringState(P
, PkgState
))
1870 if ((PkgState
[P
->ID
].Flags
& Flag::Auto
) == 0)
1872 else if ((P
->Flags
& Flag::Essential
) || (P
->Flags
& Flag::Important
))
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
)
1878 else if (userFunc
.InRootSet(P
))
1880 // packages which can't be changed (like holds) can't be garbage
1881 else if (IsModeChangeOk(ModeGarbage
, P
, 0, false) == false)
1886 if (PkgState
[P
->ID
].Install())
1887 MarkPackage(P
, PkgState
[P
->ID
].InstVerIter(*this),
1888 follow_recommends
, follow_suggests
);
1890 MarkPackage(P
, P
.CurrentVer(),
1891 follow_recommends
, follow_suggests
);
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
)
1904 pkgDepCache::StateCache
&state
= PkgState
[Pkg
->ID
];
1905 // if we are marked already we are done
1906 if(state
.Marked
|| unlikely(Ver
.end()))
1911 if (IsPkgInBoringState(Pkg
, PkgState
))
1914 bool const debug_autoremove
= _config
->FindB("Debug::pkgAutoRemove", false);
1915 if(debug_autoremove
)
1916 std::clog
<< "Marking: " << Pkg
.FullName() << " " << Ver
.VerStr() << std::endl
;
1918 for (auto D
= Ver
.DependsList(); D
.end() == false; ++D
)
1920 auto const T
= D
.TargetPkg();
1921 if (PkgState
[T
->ID
].Marked
)
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
))
1930 // handle the virtual part first
1931 APT::VersionVector providers
;
1932 for(auto Prv
= T
.ProvidesList(); Prv
.end() == false; ++Prv
)
1934 auto PP
= Prv
.OwnerPkg();
1935 if (IsPkgInBoringState(PP
, PkgState
))
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)
1944 providers
.emplace_back(PV
);
1946 if (providers
.empty() == false)
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());
1955 auto const verret
= A
.Cache()->VS
->CmpVersion(A
.SourceVerStr(), B
.SourceVerStr());
1958 return strcmp(A
.ParentPkg().Name(), B
.ParentPkg().Name()) < 0;
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
)
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
);
1977 // now deal with the real part of the package
1978 if (IsPkgInBoringState(T
, PkgState
))
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)
1986 if (debug_autoremove
)
1987 std::clog
<< "Following dep: " << APT::PrettyDep(this, D
) << std::endl
;
1988 MarkPackage(T
, TV
, follow_recommends
, follow_suggests
);
1992 bool pkgDepCache::Sweep() /*{{{*/
1994 bool debug_autoremove
= _config
->FindB("Debug::pkgAutoRemove",false);
1997 for(PkgIterator p
=PkgBegin(); !p
.end(); ++p
)
1999 StateCache
&state
=PkgState
[p
->ID
];
2001 // skip required packages
2002 if (!p
.CurrentVer().end() &&
2003 (p
.CurrentVer()->Priority
== pkgCache::State::Required
))
2006 // if it is not marked and it is installed, it's garbage
2007 if(!state
.Marked
&& (!p
.CurrentVer().end() || state
.Install()))
2010 if(debug_autoremove
)
2011 std::clog
<< "Garbage: " << p
.FullName() << std::endl
;
2018 // DepCache::MarkAndSweep /*{{{*/
2019 bool pkgDepCache::MarkAndSweep(InRootSetFunc
&rootFunc
)
2021 return MarkRequired(rootFunc
) && Sweep();
2023 bool pkgDepCache::MarkAndSweep()
2025 std::unique_ptr
<InRootSetFunc
> f(GetRootSetFunc());
2027 return MarkAndSweep(*f
.get());