]>
git.saurik.com Git - apt.git/blob - apt-pkg/depcache.cc
2bae94026933167b32caf0ad5adfce4be3d89c31
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 /*{{{*/
12 #pragma implementation "apt-pkg/depcache.h"
14 #include <apt-pkg/depcache.h>
15 #include <apt-pkg/version.h>
16 #include <apt-pkg/error.h>
17 #include <apt-pkg/sptr.h>
18 #include <apt-pkg/algorithms.h>
20 #include <apt-pkg/fileutl.h>
21 #include <apt-pkg/configuration.h>
22 #include <apt-pkg/pkgsystem.h>
23 #include <apt-pkg/tagfile.h>
31 pkgDepCache::ActionGroup::ActionGroup(pkgDepCache
&cache
) :
32 cache(cache
), released(false)
37 void pkgDepCache::ActionGroup::release()
41 if(cache
.group_level
== 0)
42 std::cerr
<< "W: Unbalanced action groups, expect badness" << std::endl
;
47 if(cache
.group_level
== 0)
55 pkgDepCache::ActionGroup::~ActionGroup()
60 // DepCache::pkgDepCache - Constructors /*{{{*/
61 // ---------------------------------------------------------------------
63 pkgDepCache::pkgDepCache(pkgCache
*pCache
,Policy
*Plcy
) :
64 group_level(0), Cache(pCache
), PkgState(0), DepState(0)
69 delLocalPolicy
= LocalPolicy
= new Policy
;
72 // DepCache::~pkgDepCache - Destructor /*{{{*/
73 // ---------------------------------------------------------------------
75 pkgDepCache::~pkgDepCache()
79 delete delLocalPolicy
;
82 // DepCache::Init - Generate the initial extra structures. /*{{{*/
83 // ---------------------------------------------------------------------
84 /* This allocats the extension buffers and initializes them. */
85 bool pkgDepCache::Init(OpProgress
*Prog
)
87 // Suppress mark updates during this operation (just in case) and
88 // run a mark operation when Init terminates.
89 ActionGroup
actions(*this);
93 PkgState
= new StateCache
[Head().PackageCount
];
94 DepState
= new unsigned char[Head().DependsCount
];
95 memset(PkgState
,0,sizeof(*PkgState
)*Head().PackageCount
);
96 memset(DepState
,0,sizeof(*DepState
)*Head().DependsCount
);
100 Prog
->OverallProgress(0,2*Head().PackageCount
,Head().PackageCount
,
101 _("Building dependency tree"));
102 Prog
->SubProgress(Head().PackageCount
,_("Candidate versions"));
105 /* Set the current state of everything. In this state all of the
106 packages are kept exactly as is. See AllUpgrade */
108 for (PkgIterator I
= PkgBegin(); I
.end() != true; I
++,Done
++)
111 Prog
->Progress(Done
);
113 // Find the proper cache slot
114 StateCache
&State
= PkgState
[I
->ID
];
117 // Figure out the install version
118 State
.CandidateVer
= GetCandidateVer(I
);
119 State
.InstallVer
= I
.CurrentVer();
120 State
.Mode
= ModeKeep
;
122 State
.Update(I
,*this);
128 Prog
->OverallProgress(Head().PackageCount
,2*Head().PackageCount
,
130 _("Building dependency tree"));
131 Prog
->SubProgress(Head().PackageCount
,_("Dependency generation"));
143 bool pkgDepCache::readStateFile(OpProgress
*Prog
)
146 string state
= _config
->FindDir("Dir::State") + "extended_states";
147 if(FileExists(state
)) {
148 state_file
.Open(state
, FileFd::ReadOnly
);
149 int file_size
= state_file
.Size();
151 Prog
->OverallProgress(0, file_size
, 1,
152 _("Reading state information"));
154 pkgTagFile
tagfile(&state_file
);
155 pkgTagSection section
;
157 while(tagfile
.Step(section
)) {
158 string pkgname
= section
.FindS("Package");
159 pkgCache::PkgIterator pkg
=Cache
->FindPkg(pkgname
);
160 // Silently ignore unknown packages and packages with no actual
162 if(!pkg
.end() && !pkg
.VersionList().end()) {
163 short reason
= section
.FindI("Auto-Installed", 0);
165 PkgState
[pkg
->ID
].Flags
|= Flag::Auto
;
166 if(_config
->FindB("Debug::pkgAutoRemove",false))
167 std::cout
<< "Auto-Installed : " << pkgname
<< std::endl
;
170 Prog
->OverallProgress(amt
, file_size
, 1,
171 _("Reading state information"));
174 Prog
->OverallProgress(file_size
, file_size
, 1,
175 _("Reading state information"));
182 bool pkgDepCache::writeStateFile(OpProgress
*prog
)
184 if(_config
->FindB("Debug::pkgAutoRemove",false))
185 std::clog
<< "pkgDepCache::writeStateFile()" << std::endl
;
188 string state
= _config
->FindDir("Dir::State") + "extended_states";
190 // if it does not exist, create a empty one
191 if(!FileExists(state
))
193 StateFile
.Open(state
, FileFd::WriteEmpty
);
198 if(!StateFile
.Open(state
, FileFd::ReadOnly
))
199 return _error
->Error(_("Failed to open StateFile %s"),
203 string outfile
= state
+ ".tmp";
204 if((OutFile
= fopen(outfile
.c_str(),"w")) == NULL
)
205 return _error
->Error(_("Failed to write temporary StateFile %s"),
208 // first merge with the existing sections
209 pkgTagFile
tagfile(&StateFile
);
210 pkgTagSection section
;
211 std::set
<string
> pkgs_seen
;
212 const char *nullreorderlist
[] = {0};
213 while(tagfile
.Step(section
)) {
214 string pkgname
= section
.FindS("Package");
215 // Silently ignore unknown packages and packages with no actual
217 pkgCache::PkgIterator pkg
=Cache
->FindPkg(pkgname
);
218 if(pkg
.end() || pkg
.VersionList().end())
220 bool oldAuto
= section
.FindI("Auto-Installed");
221 bool newAuto
= (PkgState
[pkg
->ID
].Flags
& Flag::Auto
);
222 if(_config
->FindB("Debug::pkgAutoRemove",false))
223 std::clog
<< "Update exisiting AutoInstall info: "
224 << pkg
.Name() << std::endl
;
225 TFRewriteData rewrite
[2];
226 rewrite
[0].Tag
= "Auto-Installed";
227 rewrite
[0].Rewrite
= newAuto
? "1" : "0";
228 rewrite
[0].NewTag
= 0;
230 TFRewrite(OutFile
, section
, nullreorderlist
, rewrite
);
231 fprintf(OutFile
,"\n");
232 pkgs_seen
.insert(pkgname
);
235 // then write the ones we have not seen yet
236 std::ostringstream ostr
;
237 for(pkgCache::PkgIterator pkg
=Cache
->PkgBegin(); !pkg
.end(); pkg
++) {
238 if(PkgState
[pkg
->ID
].Flags
& Flag::Auto
) {
239 if (pkgs_seen
.find(pkg
.Name()) != pkgs_seen
.end()) {
240 if(_config
->FindB("Debug::pkgAutoRemove",false))
241 std::clog
<< "Skipping already written " << pkg
.Name() << std::endl
;
244 if(_config
->FindB("Debug::pkgAutoRemove",false))
245 std::clog
<< "Writing new AutoInstall: "
246 << pkg
.Name() << std::endl
;
247 ostr
.str(string(""));
248 ostr
<< "Package: " << pkg
.Name()
249 << "\nAuto-Installed: 1\n\n";
250 fprintf(OutFile
,ostr
.str().c_str());
251 fprintf(OutFile
,"\n");
256 // move the outfile over the real file
257 rename(outfile
.c_str(), state
.c_str());
262 // DepCache::CheckDep - Checks a single dependency /*{{{*/
263 // ---------------------------------------------------------------------
264 /* This first checks the dependency against the main target package and
265 then walks along the package provides list and checks if each provides
266 will be installed then checks the provides against the dep. Res will be
267 set to the package which was used to satisfy the dep. */
268 bool pkgDepCache::CheckDep(DepIterator Dep
,int Type
,PkgIterator
&Res
)
270 Res
= Dep
.TargetPkg();
272 /* Check simple depends. A depends -should- never self match but
273 we allow it anyhow because dpkg does. Technically it is a packaging
274 bug. Conflicts may never self match */
275 if (Dep
.TargetPkg() != Dep
.ParentPkg() ||
276 (Dep
->Type
!= Dep::Conflicts
&& Dep
->Type
!= Dep::Obsoletes
))
278 PkgIterator Pkg
= Dep
.TargetPkg();
279 // Check the base package
280 if (Type
== NowVersion
&& Pkg
->CurrentVer
!= 0)
281 if (VS().CheckDep(Pkg
.CurrentVer().VerStr(),Dep
->CompareOp
,
282 Dep
.TargetVer()) == true)
285 if (Type
== InstallVersion
&& PkgState
[Pkg
->ID
].InstallVer
!= 0)
286 if (VS().CheckDep(PkgState
[Pkg
->ID
].InstVerIter(*this).VerStr(),
287 Dep
->CompareOp
,Dep
.TargetVer()) == true)
290 if (Type
== CandidateVersion
&& PkgState
[Pkg
->ID
].CandidateVer
!= 0)
291 if (VS().CheckDep(PkgState
[Pkg
->ID
].CandidateVerIter(*this).VerStr(),
292 Dep
->CompareOp
,Dep
.TargetVer()) == true)
296 if (Dep
->Type
== Dep::Obsoletes
)
299 // Check the providing packages
300 PrvIterator P
= Dep
.TargetPkg().ProvidesList();
301 PkgIterator Pkg
= Dep
.ParentPkg();
302 for (; P
.end() != true; P
++)
304 /* Provides may never be applied against the same package if it is
305 a conflicts. See the comment above. */
306 if (P
.OwnerPkg() == Pkg
&& Dep
->Type
== Dep::Conflicts
)
309 // Check if the provides is a hit
310 if (Type
== NowVersion
)
312 if (P
.OwnerPkg().CurrentVer() != P
.OwnerVer())
316 if (Type
== InstallVersion
)
318 StateCache
&State
= PkgState
[P
.OwnerPkg()->ID
];
319 if (State
.InstallVer
!= (Version
*)P
.OwnerVer())
323 if (Type
== CandidateVersion
)
325 StateCache
&State
= PkgState
[P
.OwnerPkg()->ID
];
326 if (State
.CandidateVer
!= (Version
*)P
.OwnerVer())
330 // Compare the versions.
331 if (VS().CheckDep(P
.ProvideVersion(),Dep
->CompareOp
,Dep
.TargetVer()) == true)
341 // DepCache::AddSizes - Add the packages sizes to the counters /*{{{*/
342 // ---------------------------------------------------------------------
343 /* Call with Mult = -1 to preform the inverse opration */
344 void pkgDepCache::AddSizes(const PkgIterator
&Pkg
,signed long Mult
)
346 StateCache
&P
= PkgState
[Pkg
->ID
];
348 if (Pkg
->VersionList
== 0)
351 if (Pkg
.State() == pkgCache::PkgIterator::NeedsConfigure
&&
355 // Compute the size data
356 if (P
.NewInstall() == true)
358 iUsrSize
+= (signed)(Mult
*P
.InstVerIter(*this)->InstalledSize
);
359 iDownloadSize
+= (signed)(Mult
*P
.InstVerIter(*this)->Size
);
364 if (Pkg
->CurrentVer
!= 0 &&
365 (P
.InstallVer
!= (Version
*)Pkg
.CurrentVer() ||
366 (P
.iFlags
& ReInstall
) == ReInstall
) && P
.InstallVer
!= 0)
368 iUsrSize
+= (signed)(Mult
*((signed)P
.InstVerIter(*this)->InstalledSize
-
369 (signed)Pkg
.CurrentVer()->InstalledSize
));
370 iDownloadSize
+= (signed)(Mult
*P
.InstVerIter(*this)->Size
);
375 if (Pkg
.State() == pkgCache::PkgIterator::NeedsUnpack
&&
378 iDownloadSize
+= (signed)(Mult
*P
.InstVerIter(*this)->Size
);
383 if (Pkg
->CurrentVer
!= 0 && P
.InstallVer
== 0)
385 iUsrSize
-= (signed)(Mult
*Pkg
.CurrentVer()->InstalledSize
);
390 // DepCache::AddStates - Add the package to the state counter /*{{{*/
391 // ---------------------------------------------------------------------
392 /* This routine is tricky to use, you must make sure that it is never
393 called twice for the same package. This means the Remove/Add section
394 should be as short as possible and not encompass any code that will
395 calld Remove/Add itself. Remember, dependencies can be circular so
396 while processing a dep for Pkg it is possible that Add/Remove
397 will be called on Pkg */
398 void pkgDepCache::AddStates(const PkgIterator
&Pkg
,int Add
)
400 StateCache
&State
= PkgState
[Pkg
->ID
];
402 // The Package is broken
403 if ((State
.DepState
& DepInstMin
) != DepInstMin
)
407 if (Pkg
.State() != PkgIterator::NeedsNothing
)
411 if (Pkg
->CurrentVer
== 0)
413 if (State
.Mode
== ModeDelete
&&
414 (State
.iFlags
| Purge
) == Purge
&& Pkg
.Purge() == false)
417 if (State
.Mode
== ModeInstall
)
422 // Installed, no upgrade
423 if (State
.Status
== 0)
425 if (State
.Mode
== ModeDelete
)
428 if ((State
.iFlags
& ReInstall
) == ReInstall
)
434 // Alll 3 are possible
435 if (State
.Mode
== ModeDelete
)
437 if (State
.Mode
== ModeKeep
)
439 if (State
.Mode
== ModeInstall
)
443 // DepCache::BuildGroupOrs - Generate the Or group dep data /*{{{*/
444 // ---------------------------------------------------------------------
445 /* The or group results are stored in the last item of the or group. This
446 allows easy detection of the state of a whole or'd group. */
447 void pkgDepCache::BuildGroupOrs(VerIterator
const &V
)
449 unsigned char Group
= 0;
451 for (DepIterator D
= V
.DependsList(); D
.end() != true; D
++)
453 // Build the dependency state.
454 unsigned char &State
= DepState
[D
->ID
];
456 /* Invert for Conflicts. We have to do this twice to get the
457 right sense for a conflicts group */
458 if (D
->Type
== Dep::Conflicts
|| D
->Type
== Dep::Obsoletes
)
461 // Add to the group if we are within an or..
465 if ((D
->CompareOp
& Dep::Or
) != Dep::Or
)
468 // Invert for Conflicts
469 if (D
->Type
== Dep::Conflicts
|| D
->Type
== Dep::Obsoletes
)
474 // DepCache::VersionState - Perform a pass over a dependency list /*{{{*/
475 // ---------------------------------------------------------------------
476 /* This is used to run over a dependency list and determine the dep
477 state of the list, filtering it through both a Min check and a Policy
478 check. The return result will have SetMin/SetPolicy low if a check
479 fails. It uses the DepState cache for it's computations. */
480 unsigned char pkgDepCache::VersionState(DepIterator D
,unsigned char Check
,
481 unsigned char SetMin
,
482 unsigned char SetPolicy
)
484 unsigned char Dep
= 0xFF;
486 while (D
.end() != true)
488 // Compute a single dependency element (glob or)
489 DepIterator Start
= D
;
490 unsigned char State
= 0;
491 for (bool LastOR
= true; D
.end() == false && LastOR
== true; D
++)
493 State
|= DepState
[D
->ID
];
494 LastOR
= (D
->CompareOp
& Dep::Or
) == Dep::Or
;
497 // Minimum deps that must be satisfied to have a working package
498 if (Start
.IsCritical() == true)
499 if ((State
& Check
) != Check
)
502 // Policy deps that must be satisfied to install the package
503 if (IsImportantDep(Start
) == true &&
504 (State
& Check
) != Check
)
511 // DepCache::DependencyState - Compute the 3 results for a dep /*{{{*/
512 // ---------------------------------------------------------------------
513 /* This is the main dependency computation bit. It computes the 3 main
514 results for a dependencys, Now, Install and Candidate. Callers must
515 invert the result if dealing with conflicts. */
516 unsigned char pkgDepCache::DependencyState(DepIterator
&D
)
518 unsigned char State
= 0;
520 if (CheckDep(D
,NowVersion
) == true)
522 if (CheckDep(D
,InstallVersion
) == true)
524 if (CheckDep(D
,CandidateVersion
) == true)
530 // DepCache::UpdateVerState - Compute the Dep member of the state /*{{{*/
531 // ---------------------------------------------------------------------
532 /* This determines the combined dependency representation of a package
533 for its two states now and install. This is done by using the pre-generated
534 dependency information. */
535 void pkgDepCache::UpdateVerState(PkgIterator Pkg
)
537 // Empty deps are always true
538 StateCache
&State
= PkgState
[Pkg
->ID
];
539 State
.DepState
= 0xFF;
541 // Check the Current state
542 if (Pkg
->CurrentVer
!= 0)
544 DepIterator D
= Pkg
.CurrentVer().DependsList();
545 State
.DepState
&= VersionState(D
,DepNow
,DepNowMin
,DepNowPolicy
);
548 /* Check the candidate state. We do not compare against the whole as
549 a candidate state but check the candidate version against the
551 if (State
.CandidateVer
!= 0)
553 DepIterator D
= State
.CandidateVerIter(*this).DependsList();
554 State
.DepState
&= VersionState(D
,DepInstall
,DepCandMin
,DepCandPolicy
);
557 // Check target state which can only be current or installed
558 if (State
.InstallVer
!= 0)
560 DepIterator D
= State
.InstVerIter(*this).DependsList();
561 State
.DepState
&= VersionState(D
,DepInstall
,DepInstMin
,DepInstPolicy
);
565 // DepCache::Update - Figure out all the state information /*{{{*/
566 // ---------------------------------------------------------------------
567 /* This will figure out the state of all the packages and all the
568 dependencies based on the current policy. */
569 void pkgDepCache::Update(OpProgress
*Prog
)
579 // Perform the depends pass
581 for (PkgIterator I
= PkgBegin(); I
.end() != true; I
++,Done
++)
583 if (Prog
!= 0 && Done%20
== 0)
584 Prog
->Progress(Done
);
585 for (VerIterator V
= I
.VersionList(); V
.end() != true; V
++)
587 unsigned char Group
= 0;
589 for (DepIterator D
= V
.DependsList(); D
.end() != true; D
++)
591 // Build the dependency state.
592 unsigned char &State
= DepState
[D
->ID
];
593 State
= DependencyState(D
);
595 // Add to the group if we are within an or..
598 if ((D
->CompareOp
& Dep::Or
) != Dep::Or
)
601 // Invert for Conflicts
602 if (D
->Type
== Dep::Conflicts
|| D
->Type
== Dep::Obsoletes
)
607 // Compute the pacakge dependency state and size additions
614 Prog
->Progress(Done
);
619 // DepCache::Update - Update the deps list of a package /*{{{*/
620 // ---------------------------------------------------------------------
621 /* This is a helper for update that only does the dep portion of the scan.
622 It is mainly meant to scan reverse dependencies. */
623 void pkgDepCache::Update(DepIterator D
)
625 // Update the reverse deps
626 for (;D
.end() != true; D
++)
628 unsigned char &State
= DepState
[D
->ID
];
629 State
= DependencyState(D
);
631 // Invert for Conflicts
632 if (D
->Type
== Dep::Conflicts
|| D
->Type
== Dep::Obsoletes
)
635 RemoveStates(D
.ParentPkg());
636 BuildGroupOrs(D
.ParentVer());
637 UpdateVerState(D
.ParentPkg());
638 AddStates(D
.ParentPkg());
642 // DepCache::Update - Update the related deps of a package /*{{{*/
643 // ---------------------------------------------------------------------
644 /* This is called whenever the state of a package changes. It updates
645 all cached dependencies related to this package. */
646 void pkgDepCache::Update(PkgIterator
const &Pkg
)
648 // Recompute the dep of the package
653 // Update the reverse deps
654 Update(Pkg
.RevDependsList());
656 // Update the provides map for the current ver
657 if (Pkg
->CurrentVer
!= 0)
658 for (PrvIterator P
= Pkg
.CurrentVer().ProvidesList();
659 P
.end() != true; P
++)
660 Update(P
.ParentPkg().RevDependsList());
662 // Update the provides map for the candidate ver
663 if (PkgState
[Pkg
->ID
].CandidateVer
!= 0)
664 for (PrvIterator P
= PkgState
[Pkg
->ID
].CandidateVerIter(*this).ProvidesList();
665 P
.end() != true; P
++)
666 Update(P
.ParentPkg().RevDependsList());
671 // DepCache::MarkKeep - Put the package in the keep state /*{{{*/
672 // ---------------------------------------------------------------------
674 void pkgDepCache::MarkKeep(PkgIterator
const &Pkg
, bool Soft
, bool FromUser
)
676 // Simplifies other routines.
677 if (Pkg
.end() == true)
680 /* Reject an attempt to keep a non-source broken installed package, those
682 if (Pkg
.State() == PkgIterator::NeedsUnpack
&&
683 Pkg
.CurrentVer().Downloadable() == false)
686 /** \todo Can this be moved later in the method? */
687 ActionGroup
group(*this);
689 /* We changed the soft state all the time so the UI is a bit nicer
691 StateCache
&P
= PkgState
[Pkg
->ID
];
693 P
.iFlags
|= AutoKept
;
695 P
.iFlags
&= ~AutoKept
;
697 // Check that it is not already kept
698 if (P
.Mode
== ModeKeep
)
701 // We dont even try to keep virtual packages..
702 if (Pkg
->VersionList
== 0)
705 #if 0 // reseting the autoflag here means we lose the
706 // auto-mark information if a user selects a package for removal
707 // but changes his mind then and sets it for keep again
708 // - this makes sense as default when all Garbage dependencies
709 // are automatically marked for removal (as aptitude does).
710 // setting a package for keep then makes it no longer autoinstalled
711 // for all other use-case this action is rather suprising
712 if(FromUser
&& !P
.Marked
)
713 P
.Flags
&= ~Flag::Auto
;
720 if (Pkg
->CurrentVer
== 0)
723 P
.InstallVer
= Pkg
.CurrentVer();
732 // DepCache::MarkDelete - Put the package in the delete state /*{{{*/
733 // ---------------------------------------------------------------------
735 void pkgDepCache::MarkDelete(PkgIterator
const &Pkg
, bool rPurge
)
737 // Simplifies other routines.
738 if (Pkg
.end() == true)
741 ActionGroup
group(*this);
743 // Check that it is not already marked for delete
744 StateCache
&P
= PkgState
[Pkg
->ID
];
745 P
.iFlags
&= ~(AutoKept
| Purge
);
749 if ((P
.Mode
== ModeDelete
|| P
.InstallVer
== 0) &&
750 (Pkg
.Purge() == true || rPurge
== false))
753 // We dont even try to delete virtual packages..
754 if (Pkg
->VersionList
== 0)
760 if (Pkg
->CurrentVer
== 0 && (Pkg
.Purge() == true || rPurge
== false))
771 // DepCache::MarkInstall - Put the package in the install state /*{{{*/
772 // ---------------------------------------------------------------------
774 void pkgDepCache::MarkInstall(PkgIterator
const &Pkg
,bool AutoInst
,
775 unsigned long Depth
, bool FromUser
)
780 // Simplifies other routines.
781 if (Pkg
.end() == true)
784 ActionGroup
group(*this);
786 /* Check that it is not already marked for install and that it can be
788 StateCache
&P
= PkgState
[Pkg
->ID
];
789 P
.iFlags
&= ~AutoKept
;
790 if (P
.InstBroken() == false && (P
.Mode
== ModeInstall
||
791 P
.CandidateVer
== (Version
*)Pkg
.CurrentVer()))
793 if (P
.CandidateVer
== (Version
*)Pkg
.CurrentVer() && P
.InstallVer
== 0)
794 MarkKeep(Pkg
, false, FromUser
);
798 // See if there is even any possible instalation candidate
799 if (P
.CandidateVer
== 0)
802 // We dont even try to install virtual packages..
803 if (Pkg
->VersionList
== 0)
806 /* Target the candidate version and remove the autoflag. We reset the
807 autoflag below if this was called recursively. Otherwise the user
808 should have the ability to de-auto a package by changing its state */
812 P
.Mode
= ModeInstall
;
813 P
.InstallVer
= P
.CandidateVer
;
817 // Set it to manual if it's a new install or cancelling the
818 // removal of a garbage package.
819 if(P
.Status
== 2 || (!Pkg
.CurrentVer().end() && !P
.Marked
))
820 P
.Flags
&= ~Flag::Auto
;
824 // Set it to auto if this is a new install.
826 P
.Flags
|= Flag::Auto
;
828 if (P
.CandidateVer
== (Version
*)Pkg
.CurrentVer())
835 if (AutoInst
== false)
838 DepIterator Dep
= P
.InstVerIter(*this).DependsList();
839 for (; Dep
.end() != true;)
842 DepIterator Start
= Dep
;
845 for (bool LastOR
= true; Dep
.end() == false && LastOR
== true; Dep
++,Ors
++)
847 LastOR
= (Dep
->CompareOp
& Dep::Or
) == Dep::Or
;
849 if ((DepState
[Dep
->ID
] & DepInstall
) == DepInstall
)
853 // Dep is satisfied okay.
857 /* Check if this dep should be consider for install. If it is a user
858 defined important dep and we are installed a new package then
859 it will be installed. Otherwise we only worry about critical deps */
860 if (IsImportantDep(Start
) == false)
862 if (Pkg
->CurrentVer
!= 0 && Start
.IsCritical() == false)
865 /* If we are in an or group locate the first or that can
866 succeed. We have already cached this.. */
867 for (; Ors
> 1 && (DepState
[Start
->ID
] & DepCVer
) != DepCVer
; Ors
--)
870 /* This bit is for processing the possibilty of an install/upgrade
871 fixing the problem */
872 SPtrArray
<Version
*> List
= Start
.AllTargets();
873 if ((DepState
[Start
->ID
] & DepCVer
) == DepCVer
)
875 // Right, find the best version to install..
876 Version
**Cur
= List
;
877 PkgIterator P
= Start
.TargetPkg();
878 PkgIterator
InstPkg(*Cache
,0);
880 // See if there are direct matches (at the start of the list)
881 for (; *Cur
!= 0 && (*Cur
)->ParentPkg
== P
.Index(); Cur
++)
883 PkgIterator
Pkg(*Cache
,Cache
->PkgP
+ (*Cur
)->ParentPkg
);
884 if (PkgState
[Pkg
->ID
].CandidateVer
!= *Cur
)
890 // Select the highest priority providing package
891 if (InstPkg
.end() == true)
893 pkgPrioSortList(*Cache
,Cur
);
894 for (; *Cur
!= 0; Cur
++)
896 PkgIterator
Pkg(*Cache
,Cache
->PkgP
+ (*Cur
)->ParentPkg
);
897 if (PkgState
[Pkg
->ID
].CandidateVer
!= *Cur
)
904 if (InstPkg
.end() == false)
906 if(_config
->FindB("Debug::pkgDepCache::AutoInstall",false) == true)
907 std::clog
<< "Installing " << InstPkg
.Name()
908 << " as dep of " << Pkg
.Name()
910 MarkInstall(InstPkg
, true, Depth
+ 1, false);
915 /* For conflicts we just de-install the package and mark as auto,
916 Conflicts may not have or groups */
917 if (Start
->Type
== Dep::Conflicts
|| Start
->Type
== Dep::Obsoletes
)
919 for (Version
**I
= List
; *I
!= 0; I
++)
921 VerIterator
Ver(*this,*I
);
922 PkgIterator Pkg
= Ver
.ParentPkg();
931 // DepCache::SetReInstall - Set the reinstallation flag /*{{{*/
932 // ---------------------------------------------------------------------
934 void pkgDepCache::SetReInstall(PkgIterator
const &Pkg
,bool To
)
936 ActionGroup
group(*this);
941 StateCache
&P
= PkgState
[Pkg
->ID
];
943 P
.iFlags
|= ReInstall
;
945 P
.iFlags
&= ~ReInstall
;
951 // DepCache::SetCandidateVersion - Change the candidate version /*{{{*/
952 // ---------------------------------------------------------------------
954 void pkgDepCache::SetCandidateVersion(VerIterator TargetVer
)
956 ActionGroup
group(*this);
958 pkgCache::PkgIterator Pkg
= TargetVer
.ParentPkg();
959 StateCache
&P
= PkgState
[Pkg
->ID
];
964 if (P
.CandidateVer
== P
.InstallVer
)
965 P
.InstallVer
= (Version
*)TargetVer
;
966 P
.CandidateVer
= (Version
*)TargetVer
;
974 void pkgDepCache::MarkAuto(const PkgIterator
&Pkg
, bool Auto
)
976 StateCache
&state
= PkgState
[Pkg
->ID
];
978 ActionGroup
group(*this);
981 state
.Flags
|= Flag::Auto
;
983 state
.Flags
&= ~Flag::Auto
;
986 // StateCache::Update - Compute the various static display things /*{{{*/
987 // ---------------------------------------------------------------------
988 /* This is called whenever the Candidate version changes. */
989 void pkgDepCache::StateCache::Update(PkgIterator Pkg
,pkgCache
&Cache
)
992 VerIterator Ver
= CandidateVerIter(Cache
);
994 // Use a null string or the version string
995 if (Ver
.end() == true)
998 CandVersion
= Ver
.VerStr();
1000 // Find the current version
1002 if (Pkg
->CurrentVer
!= 0)
1003 CurVersion
= Pkg
.CurrentVer().VerStr();
1005 // Strip off the epochs for display
1006 CurVersion
= StripEpoch(CurVersion
);
1007 CandVersion
= StripEpoch(CandVersion
);
1009 // Figure out if its up or down or equal
1010 Status
= Ver
.CompareVer(Pkg
.CurrentVer());
1011 if (Pkg
->CurrentVer
== 0 || Pkg
->VersionList
== 0 || CandidateVer
== 0)
1015 // StateCache::StripEpoch - Remove the epoch specifier from the version /*{{{*/
1016 // ---------------------------------------------------------------------
1018 const char *pkgDepCache::StateCache::StripEpoch(const char *Ver
)
1024 for (const char *I
= Ver
; *I
!= 0; I
++)
1031 // Policy::GetCandidateVer - Returns the Candidate install version /*{{{*/
1032 // ---------------------------------------------------------------------
1033 /* The default just returns the highest available version that is not
1034 a source and automatic. */
1035 pkgCache::VerIterator
pkgDepCache::Policy::GetCandidateVer(PkgIterator Pkg
)
1037 /* Not source/not automatic versions cannot be a candidate version
1038 unless they are already installed */
1039 VerIterator
Last(*(pkgCache
*)this,0);
1041 for (VerIterator I
= Pkg
.VersionList(); I
.end() == false; I
++)
1043 if (Pkg
.CurrentVer() == I
)
1046 for (VerFileIterator J
= I
.FileList(); J
.end() == false; J
++)
1048 if ((J
.File()->Flags
& Flag::NotSource
) != 0)
1051 /* Stash the highest version of a not-automatic source, we use it
1052 if there is nothing better */
1053 if ((J
.File()->Flags
& Flag::NotAutomatic
) != 0)
1055 if (Last
.end() == true)
1067 // Policy::IsImportantDep - True if the dependency is important /*{{{*/
1068 // ---------------------------------------------------------------------
1070 bool pkgDepCache::Policy::IsImportantDep(DepIterator Dep
)
1072 return Dep
.IsCritical();
1076 pkgDepCache::DefaultRootSetFunc::DefaultRootSetFunc()
1077 : constructedSuccessfully(false)
1079 Configuration::Item
const *Opts
;
1080 Opts
= _config
->Tree("APT::NeverAutoRemove");
1081 if (Opts
!= 0 && Opts
->Child
!= 0)
1084 for (; Opts
!= 0; Opts
= Opts
->Next
)
1086 if (Opts
->Value
.empty() == true)
1089 regex_t
*p
= new regex_t
;
1090 if(regcomp(p
,Opts
->Value
.c_str(),
1091 REG_EXTENDED
| REG_ICASE
| REG_NOSUB
) != 0)
1095 _error
->Error("Regex compilation error for APT::NeverAutoRemove");
1099 rootSetRegexp
.push_back(p
);
1103 constructedSuccessfully
= true;
1106 pkgDepCache::DefaultRootSetFunc::~DefaultRootSetFunc()
1108 for(unsigned int i
= 0; i
< rootSetRegexp
.size(); i
++)
1110 regfree(rootSetRegexp
[i
]);
1111 delete rootSetRegexp
[i
];
1116 bool pkgDepCache::DefaultRootSetFunc::InRootSet(const pkgCache::PkgIterator
&pkg
)
1118 for(unsigned int i
= 0; i
< rootSetRegexp
.size(); i
++)
1119 if (regexec(rootSetRegexp
[i
], pkg
.Name(), 0, 0, 0) == 0)
1125 pkgDepCache::InRootSetFunc
*pkgDepCache::GetRootSetFunc()
1127 DefaultRootSetFunc
*f
= new DefaultRootSetFunc
;
1128 if(f
->wasConstructedSuccessfully())
1137 bool pkgDepCache::MarkFollowsRecommends()
1139 return _config
->FindB("APT::AutoRemove::RecommendsImportant", true);
1142 bool pkgDepCache::MarkFollowsSuggests()
1144 return _config
->FindB("APT::AutoRemove::SuggestsImportant", false);
1147 // the main mark algorithm
1148 bool pkgDepCache::MarkRequired(InRootSetFunc
&userFunc
)
1150 bool follow_recommends
;
1151 bool follow_suggests
;
1154 for(PkgIterator p
= PkgBegin(); !p
.end(); ++p
)
1156 PkgState
[p
->ID
].Marked
= false;
1157 PkgState
[p
->ID
].Garbage
= false;
1160 if(_config
->FindB("Debug::pkgAutoRemove",false)
1161 && PkgState
[p
->ID
].Flags
& Flag::Auto
)
1162 std::clog
<< "AutoDep: " << p
.Name() << std::endl
;
1166 follow_recommends
= MarkFollowsRecommends();
1167 follow_suggests
= MarkFollowsSuggests();
1171 // do the mark part, this is the core bit of the algorithm
1172 for(PkgIterator p
= PkgBegin(); !p
.end(); ++p
)
1174 if(!(PkgState
[p
->ID
].Flags
& Flag::Auto
) ||
1175 (p
->Flags
& Flag::Essential
) ||
1176 userFunc
.InRootSet(p
))
1179 // the package is installed (and set to keep)
1180 if(PkgState
[p
->ID
].Keep() && !p
.CurrentVer().end())
1181 MarkPackage(p
, p
.CurrentVer(),
1182 follow_recommends
, follow_suggests
);
1183 // the package is to be installed
1184 else if(PkgState
[p
->ID
].Install())
1185 MarkPackage(p
, PkgState
[p
->ID
].InstVerIter(*this),
1186 follow_recommends
, follow_suggests
);
1193 // mark a single package in Mark-and-Sweep
1194 void pkgDepCache::MarkPackage(const pkgCache::PkgIterator
&pkg
,
1195 const pkgCache::VerIterator
&ver
,
1196 bool follow_recommends
,
1197 bool follow_suggests
)
1199 pkgDepCache::StateCache
&state
= PkgState
[pkg
->ID
];
1200 VerIterator candver
= state
.CandidateVerIter(*this);
1201 VerIterator instver
= state
.InstVerIter(*this);
1204 // If a package was garbage-collected but is now being marked, we
1205 // should re-select it
1206 // For cases when a pkg is set to upgrade and this trigger the
1207 // removal of a no-longer used dependency. if the pkg is set to
1208 // keep again later it will result in broken deps
1209 if(state
.Delete() && state
.RemoveReason
= Unused
)
1212 mark_install(pkg
, false, false, NULL
);
1213 else if(ver
==pkg
.CurrentVer())
1214 MarkKeep(pkg
, false, false);
1216 instver
=state
.InstVerIter(*this);
1220 // Ignore versions other than the InstVer, and ignore packages
1221 // that are already going to be removed or just left uninstalled.
1222 if(!(ver
== instver
&& !instver
.end()))
1225 // if we are marked already we are done
1229 //std::cout << "Setting Marked for: " << pkg.Name() << std::endl;
1234 for(DepIterator d
= ver
.DependsList(); !d
.end(); ++d
)
1236 if(d
->Type
== Dep::Depends
||
1237 d
->Type
== Dep::PreDepends
||
1238 (follow_recommends
&&
1239 d
->Type
== Dep::Recommends
) ||
1241 d
->Type
== Dep::Suggests
))
1243 // Try all versions of this package.
1244 for(VerIterator V
= d
.TargetPkg().VersionList();
1247 if(_system
->VS
->CheckDep(V
.VerStr(), d
->CompareOp
, d
.TargetVer()))
1249 MarkPackage(V
.ParentPkg(), V
,
1250 follow_recommends
, follow_suggests
);
1253 // Now try virtual packages
1254 for(PrvIterator prv
=d
.TargetPkg().ProvidesList();
1257 if(_system
->VS
->CheckDep(prv
.ProvideVersion(), d
->CompareOp
,
1260 MarkPackage(prv
.OwnerPkg(), prv
.OwnerVer(),
1261 follow_recommends
, follow_suggests
);
1269 bool pkgDepCache::Sweep()
1272 for(PkgIterator p
=PkgBegin(); !p
.end(); ++p
)
1274 StateCache
&state
=PkgState
[p
->ID
];
1276 // if it is not marked and it is installed, it's garbage
1277 if(!state
.Marked
&& (!p
.CurrentVer().end() || state
.Install()))
1280 if(_config
->FindB("Debug::pkgAutoRemove",false))
1281 std::cout
<< "Garbage: " << p
.Name() << std::endl
;