]>
git.saurik.com Git - apt.git/blob - apt-pkg/packagemanager.cc
1 // -*- mode: cpp; mode: fold -*-
3 // $Id: packagemanager.cc,v 1.30 2003/04/27 03:04:15 doogie Exp $
4 /* ######################################################################
6 Package Manager - Abstacts the package manager
8 More work is needed in the area of transitioning provides, ie exim
9 replacing smail. This can cause interesing side effects.
11 Other cases involving conflicts+replaces should be tested.
13 ##################################################################### */
15 // Include Files /*{{{*/
16 #include <apt-pkg/packagemanager.h>
17 #include <apt-pkg/orderlist.h>
18 #include <apt-pkg/depcache.h>
19 #include <apt-pkg/error.h>
20 #include <apt-pkg/version.h>
21 #include <apt-pkg/acquire-item.h>
22 #include <apt-pkg/algorithms.h>
23 #include <apt-pkg/configuration.h>
24 #include <apt-pkg/sptr.h>
32 // PM::PackageManager - Constructor /*{{{*/
33 // ---------------------------------------------------------------------
35 pkgPackageManager::pkgPackageManager(pkgDepCache
*pCache
) : Cache(*pCache
)
37 FileNames
= new string
[Cache
.Head().PackageCount
];
39 Debug
= _config
->FindB("Debug::pkgPackageManager",false);
42 // PM::PackageManager - Destructor /*{{{*/
43 // ---------------------------------------------------------------------
45 pkgPackageManager::~pkgPackageManager()
51 // PM::GetArchives - Queue the archives for download /*{{{*/
52 // ---------------------------------------------------------------------
54 bool pkgPackageManager::GetArchives(pkgAcquire
*Owner
,pkgSourceList
*Sources
,
57 if (CreateOrderList() == false)
61 _config
->FindB("PackageManager::UnpackAll",true) ?
62 List
->OrderUnpack() : List
->OrderCritical();
63 if (ordering
== false)
64 return _error
->Error("Internal ordering error");
66 for (pkgOrderList::iterator I
= List
->begin(); I
!= List
->end(); I
++)
68 PkgIterator
Pkg(Cache
,*I
);
69 FileNames
[Pkg
->ID
] = string();
71 // Skip packages to erase
72 if (Cache
[Pkg
].Delete() == true)
75 // Skip Packages that need configure only.
76 if (Pkg
.State() == pkgCache::PkgIterator::NeedsConfigure
&&
77 Cache
[Pkg
].Keep() == true)
80 // Skip already processed packages
81 if (List
->IsNow(Pkg
) == false)
84 new pkgAcqArchive(Owner
,Sources
,Recs
,Cache
[Pkg
].InstVerIter(Cache
),
91 // PM::FixMissing - Keep all missing packages /*{{{*/
92 // ---------------------------------------------------------------------
93 /* This is called to correct the installation when packages could not
95 bool pkgPackageManager::FixMissing()
97 pkgDepCache::ActionGroup
group(Cache
);
98 pkgProblemResolver
Resolve(&Cache
);
99 List
->SetFileList(FileNames
);
102 for (PkgIterator I
= Cache
.PkgBegin(); I
.end() == false; I
++)
104 if (List
->IsMissing(I
) == false)
107 // Okay, this file is missing and we need it. Mark it for keep
109 Cache
.MarkKeep(I
, false, false);
112 // We have to empty the list otherwise it will not have the new changes
119 // Now downgrade everything that is broken
120 return Resolve
.ResolveByKeep() == true && Cache
.BrokenCount() == 0;
123 // PM::ImmediateAdd - Add the immediate flag recursivly /*{{{*/
124 // ---------------------------------------------------------------------
125 /* This adds the immediate flag to the pkg and recursively to the
128 void pkgPackageManager::ImmediateAdd(PkgIterator I
, bool UseInstallVer
, unsigned const int &Depth
)
134 if(Cache
[I
].InstallVer
== 0)
136 D
= Cache
[I
].InstVerIter(Cache
).DependsList();
138 if (I
->CurrentVer
== 0)
140 D
= I
.CurrentVer().DependsList();
143 for ( /* nothing */ ; D
.end() == false; D
++)
144 if (D
->Type
== pkgCache::Dep::Depends
|| D
->Type
== pkgCache::Dep::PreDepends
)
146 // ignore dependencies if no instal/upgrade/remove is going to happen
147 if (D
.TargetPkg() == 0 || Cache
[D
.TargetPkg()].Keep())
150 if(!List
->IsFlag(D
.TargetPkg(), pkgOrderList::Immediate
))
153 clog
<< OutputInDepth(Depth
) << "ImmediateAdd(): Adding Immediate flag to " << D
.TargetPkg() << " cause of " << D
.DepType() << " " << I
.Name() << endl
;
154 List
->Flag(D
.TargetPkg(),pkgOrderList::Immediate
);
155 ImmediateAdd(D
.TargetPkg(), UseInstallVer
, Depth
+ 1);
161 // PM::CreateOrderList - Create the ordering class /*{{{*/
162 // ---------------------------------------------------------------------
163 /* This populates the ordering list with all the packages that are
165 bool pkgPackageManager::CreateOrderList()
171 List
= new pkgOrderList(&Cache
);
173 static bool const NoImmConfigure
= !_config
->FindB("APT::Immediate-Configure",true);
175 // Generate the list of affected packages and sort it
176 for (PkgIterator I
= Cache
.PkgBegin(); I
.end() == false; I
++)
178 // Ignore no-version packages
179 if (I
->VersionList
== 0)
182 // Mark the package and its dependends for immediate configuration
183 if (((I
->Flags
& pkgCache::Flag::Essential
) == pkgCache::Flag::Essential
||
184 (I
->Flags
& pkgCache::Flag::Important
) == pkgCache::Flag::Important
) &&
185 NoImmConfigure
== false)
188 clog
<< "CreateOrderList(): Adding Immediate flag for " << I
.Name() << endl
;
189 List
->Flag(I
,pkgOrderList::Immediate
);
191 // Look for other install packages to make immediate configurea
192 ImmediateAdd(I
, true);
194 // And again with the current version.
195 ImmediateAdd(I
, false);
199 if ((Cache
[I
].Keep() == true ||
200 Cache
[I
].InstVerIter(Cache
) == I
.CurrentVer()) &&
201 I
.State() == pkgCache::PkgIterator::NeedsNothing
&&
202 (Cache
[I
].iFlags
& pkgDepCache::ReInstall
) != pkgDepCache::ReInstall
&&
203 (I
.Purge() != false || Cache
[I
].Mode
!= pkgDepCache::ModeDelete
||
204 (Cache
[I
].iFlags
& pkgDepCache::Purge
) != pkgDepCache::Purge
))
207 // Append it to the list
214 // PM::DepAlwaysTrue - Returns true if this dep is irrelevent /*{{{*/
215 // ---------------------------------------------------------------------
216 /* The restriction on provides is to eliminate the case when provides
217 are transitioning between valid states [ie exim to smail] */
218 bool pkgPackageManager::DepAlwaysTrue(DepIterator D
)
220 if (D
.TargetPkg()->ProvidesList
!= 0)
223 if ((Cache
[D
] & pkgDepCache::DepInstall
) != 0 &&
224 (Cache
[D
] & pkgDepCache::DepNow
) != 0)
229 // PM::CheckRConflicts - Look for reverse conflicts /*{{{*/
230 // ---------------------------------------------------------------------
231 /* This looks over the reverses for a conflicts line that needs early
233 bool pkgPackageManager::CheckRConflicts(PkgIterator Pkg
,DepIterator D
,
236 for (;D
.end() == false; D
++)
238 if (D
->Type
!= pkgCache::Dep::Conflicts
&&
239 D
->Type
!= pkgCache::Dep::Obsoletes
)
242 // The package hasnt been changed
243 if (List
->IsNow(Pkg
) == false)
246 // Ignore self conflicts, ignore conflicts from irrelevent versions
247 if (D
.ParentPkg() == Pkg
|| D
.ParentVer() != D
.ParentPkg().CurrentVer())
250 if (Cache
.VS().CheckDep(Ver
,D
->CompareOp
,D
.TargetVer()) == false)
253 if (EarlyRemove(D
.ParentPkg()) == false)
254 return _error
->Error("Reverse conflicts early remove for package '%s' failed",
260 // PM::ConfigureAll - Run the all out configuration /*{{{*/
261 // ---------------------------------------------------------------------
262 /* This configures every package. It is assumed they are all unpacked and
263 that the final configuration is valid. */
264 bool pkgPackageManager::ConfigureAll()
266 pkgOrderList
OList(&Cache
);
268 // Populate the order list
269 for (pkgOrderList::iterator I
= List
->begin(); I
!= List
->end(); I
++)
270 if (List
->IsFlag(pkgCache::PkgIterator(Cache
,*I
),
271 pkgOrderList::UnPacked
) == true)
274 if (OList
.OrderConfigure() == false)
277 std::string
const conf
= _config
->Find("PackageManager::Configure","all");
278 bool const ConfigurePkgs
= (conf
== "all");
280 // Perform the configuring
281 for (pkgOrderList::iterator I
= OList
.begin(); I
!= OList
.end(); I
++)
283 PkgIterator
Pkg(Cache
,*I
);
285 if (ConfigurePkgs
== true && Configure(Pkg
) == false)
288 List
->Flag(Pkg
,pkgOrderList::Configured
,pkgOrderList::States
);
294 // PM::SmartConfigure - Perform immediate configuration of the pkg /*{{{*/
295 // ---------------------------------------------------------------------
296 /* This routine scheduals the configuration of the given package and all
297 of it's dependents. */
298 bool pkgPackageManager::SmartConfigure(PkgIterator Pkg
)
300 pkgOrderList
OList(&Cache
);
302 if (DepAdd(OList
,Pkg
) == false)
305 static std::string
const conf
= _config
->Find("PackageManager::Configure","all");
306 static bool const ConfigurePkgs
= (conf
== "all" || conf
== "smart");
308 if (ConfigurePkgs
== true)
309 if (OList
.OrderConfigure() == false)
312 // Perform the configuring
313 for (pkgOrderList::iterator I
= OList
.begin(); I
!= OList
.end(); I
++)
315 PkgIterator
Pkg(Cache
,*I
);
317 if (ConfigurePkgs
== true && Configure(Pkg
) == false)
320 List
->Flag(Pkg
,pkgOrderList::Configured
,pkgOrderList::States
);
324 if (List
->IsFlag(Pkg
,pkgOrderList::Configured
) == false)
325 return _error
->Error("Internal error, could not immediate configure %s",Pkg
.Name());
330 // PM::DepAdd - Add all dependents to the oder list /*{{{*/
331 // ---------------------------------------------------------------------
332 /* This recursively adds all dependents to the order list */
333 bool pkgPackageManager::DepAdd(pkgOrderList
&OList
,PkgIterator Pkg
,int Depth
)
335 if (OList
.IsFlag(Pkg
,pkgOrderList::Added
) == true)
337 if (List
->IsFlag(Pkg
,pkgOrderList::Configured
) == true)
339 if (List
->IsFlag(Pkg
,pkgOrderList::UnPacked
) == false)
342 // Put the package on the list
343 OList
.push_back(Pkg
);
344 OList
.Flag(Pkg
,pkgOrderList::Added
);
347 // Check the dependencies to see if they are all satisfied.
349 for (DepIterator D
= Cache
[Pkg
].InstVerIter(Cache
).DependsList(); D
.end() == false;)
351 if (D
->Type
!= pkgCache::Dep::Depends
&& D
->Type
!= pkgCache::Dep::PreDepends
)
359 for (bool LastOR
= true; D
.end() == false && LastOR
== true; D
++)
361 LastOR
= (D
->CompareOp
& pkgCache::Dep::Or
) == pkgCache::Dep::Or
;
366 SPtrArray
<Version
*> VList
= D
.AllTargets();
367 for (Version
**I
= VList
; *I
!= 0 && Bad
== true; I
++)
369 VerIterator
Ver(Cache
,*I
);
370 PkgIterator Pkg
= Ver
.ParentPkg();
372 // See if the current version is ok
373 if (Pkg
.CurrentVer() == Ver
&& List
->IsNow(Pkg
) == true &&
374 Pkg
.State() == PkgIterator::NeedsNothing
)
380 // Not the install version
381 if (Cache
[Pkg
].InstallVer
!= *I
||
382 (Cache
[Pkg
].Keep() == true && Pkg
.State() == PkgIterator::NeedsNothing
))
385 if (List
->IsFlag(Pkg
,pkgOrderList::UnPacked
) == true)
386 Bad
= !DepAdd(OList
,Pkg
,Depth
);
387 if (List
->IsFlag(Pkg
,pkgOrderList::Configured
) == true)
394 OList
.Flag(Pkg
,0,pkgOrderList::Added
);
405 // PM::EarlyRemove - Perform removal of packages before their time /*{{{*/
406 // ---------------------------------------------------------------------
407 /* This is called to deal with conflicts arising from unpacking */
408 bool pkgPackageManager::EarlyRemove(PkgIterator Pkg
)
410 if (List
->IsNow(Pkg
) == false)
413 // Already removed it
414 if (List
->IsFlag(Pkg
,pkgOrderList::Removed
) == true)
417 // Woops, it will not be re-installed!
418 if (List
->IsFlag(Pkg
,pkgOrderList::InList
) == false)
421 // Essential packages get special treatment
422 bool IsEssential
= false;
423 if ((Pkg
->Flags
& pkgCache::Flag::Essential
) != 0)
426 /* Check for packages that are the dependents of essential packages and
428 if (Pkg
->CurrentVer
!= 0)
430 for (DepIterator D
= Pkg
.RevDependsList(); D
.end() == false &&
431 IsEssential
== false; D
++)
432 if (D
->Type
== pkgCache::Dep::Depends
|| D
->Type
== pkgCache::Dep::PreDepends
)
433 if ((D
.ParentPkg()->Flags
& pkgCache::Flag::Essential
) != 0)
437 if (IsEssential
== true)
439 if (_config
->FindB("APT::Force-LoopBreak",false) == false)
440 return _error
->Error(_("This installation run will require temporarily "
441 "removing the essential package %s due to a "
442 "Conflicts/Pre-Depends loop. This is often bad, "
443 "but if you really want to do it, activate the "
444 "APT::Force-LoopBreak option."),Pkg
.Name());
447 bool Res
= SmartRemove(Pkg
);
448 if (Cache
[Pkg
].Delete() == false)
449 List
->Flag(Pkg
,pkgOrderList::Removed
,pkgOrderList::States
);
454 // PM::SmartRemove - Removal Helper /*{{{*/
455 // ---------------------------------------------------------------------
457 bool pkgPackageManager::SmartRemove(PkgIterator Pkg
)
459 if (List
->IsNow(Pkg
) == false)
462 List
->Flag(Pkg
,pkgOrderList::Configured
,pkgOrderList::States
);
463 return Remove(Pkg
,(Cache
[Pkg
].iFlags
& pkgDepCache::Purge
) == pkgDepCache::Purge
);
466 // PM::SmartUnPack - Install helper /*{{{*/
467 // ---------------------------------------------------------------------
468 /* This performs the task of handling pre-depends. */
469 bool pkgPackageManager::SmartUnPack(PkgIterator Pkg
)
471 // Check if it is already unpacked
472 if (Pkg
.State() == pkgCache::PkgIterator::NeedsConfigure
&&
473 Cache
[Pkg
].Keep() == true)
475 List
->Flag(Pkg
,pkgOrderList::UnPacked
,pkgOrderList::States
);
476 if (List
->IsFlag(Pkg
,pkgOrderList::Immediate
) == true)
477 if (SmartConfigure(Pkg
) == false)
478 return _error
->Error("Internal Error, Could not perform immediate configuration (1) on %s",Pkg
.Name());
482 /* See if this packages install version has any predependencies
483 that are not met by 'now' packages. */
484 for (DepIterator D
= Cache
[Pkg
].InstVerIter(Cache
).DependsList();
487 // Compute a single dependency element (glob or)
488 pkgCache::DepIterator Start
;
489 pkgCache::DepIterator End
;
492 while (End
->Type
== pkgCache::Dep::PreDepends
)
494 // Look for possible ok targets.
495 SPtrArray
<Version
*> VList
= Start
.AllTargets();
497 for (Version
**I
= VList
; *I
!= 0 && Bad
== true; I
++)
499 VerIterator
Ver(Cache
,*I
);
500 PkgIterator Pkg
= Ver
.ParentPkg();
502 // See if the current version is ok
503 if (Pkg
.CurrentVer() == Ver
&& List
->IsNow(Pkg
) == true &&
504 Pkg
.State() == PkgIterator::NeedsNothing
)
511 // Look for something that could be configured.
512 for (Version
**I
= VList
; *I
!= 0 && Bad
== true; I
++)
514 VerIterator
Ver(Cache
,*I
);
515 PkgIterator Pkg
= Ver
.ParentPkg();
517 // Not the install version
518 if (Cache
[Pkg
].InstallVer
!= *I
||
519 (Cache
[Pkg
].Keep() == true && Pkg
.State() == PkgIterator::NeedsNothing
))
522 Bad
= !SmartConfigure(Pkg
);
525 /* If this or element did not match then continue on to the
526 next or element until a matching element is found */
529 // This triggers if someone make a pre-depends/depend loop.
531 return _error
->Error("Couldn't configure pre-depend %s for %s, "
532 "probably a dependency cycle.",
533 End
.TargetPkg().Name(),Pkg
.Name());
540 if (End
->Type
== pkgCache::Dep::Conflicts
||
541 End
->Type
== pkgCache::Dep::Obsoletes
)
543 /* Look for conflicts. Two packages that are both in the install
544 state cannot conflict so we don't check.. */
545 SPtrArray
<Version
*> VList
= End
.AllTargets();
546 for (Version
**I
= VList
; *I
!= 0; I
++)
548 VerIterator
Ver(Cache
,*I
);
549 PkgIterator Pkg
= Ver
.ParentPkg();
551 // See if the current version is conflicting
552 if (Pkg
.CurrentVer() == Ver
&& List
->IsNow(Pkg
) == true)
554 if (EarlyRemove(Pkg
) == false)
555 return _error
->Error("Internal Error, Could not early remove %s",Pkg
.Name());
561 // Check for reverse conflicts.
562 if (CheckRConflicts(Pkg
,Pkg
.RevDependsList(),
563 Cache
[Pkg
].InstVerIter(Cache
).VerStr()) == false)
566 for (PrvIterator P
= Cache
[Pkg
].InstVerIter(Cache
).ProvidesList();
567 P
.end() == false; P
++)
568 CheckRConflicts(Pkg
,P
.ParentPkg().RevDependsList(),P
.ProvideVersion());
570 if (Install(Pkg
,FileNames
[Pkg
->ID
]) == false)
573 List
->Flag(Pkg
,pkgOrderList::UnPacked
,pkgOrderList::States
);
575 // Perform immedate configuration of the package.
576 if (List
->IsFlag(Pkg
,pkgOrderList::Immediate
) == true)
577 if (SmartConfigure(Pkg
) == false)
578 return _error
->Error("Internal Error, Could not perform immediate configuration (2) on %s",Pkg
.Name());
583 // PM::OrderInstall - Installation ordering routine /*{{{*/
584 // ---------------------------------------------------------------------
586 pkgPackageManager::OrderResult
pkgPackageManager::OrderInstall()
588 if (CreateOrderList() == false)
594 clog
<< "Beginning to order" << endl
;
596 bool const ordering
=
597 _config
->FindB("PackageManager::UnpackAll",true) ?
598 List
->OrderUnpack(FileNames
) : List
->OrderCritical();
599 if (ordering
== false)
601 _error
->Error("Internal ordering error");
606 clog
<< "Done ordering" << endl
;
608 bool DoneSomething
= false;
609 for (pkgOrderList::iterator I
= List
->begin(); I
!= List
->end(); I
++)
611 PkgIterator
Pkg(Cache
,*I
);
613 if (List
->IsNow(Pkg
) == false)
616 clog
<< "Skipping already done " << Pkg
.Name() << endl
;
620 if (List
->IsMissing(Pkg
) == true)
623 clog
<< "Sequence completed at " << Pkg
.Name() << endl
;
624 if (DoneSomething
== false)
626 _error
->Error("Internal Error, ordering was unable to handle the media swap");
633 if (Cache
[Pkg
].Keep() == true &&
634 Pkg
.State() == pkgCache::PkgIterator::NeedsNothing
&&
635 (Cache
[Pkg
].iFlags
& pkgDepCache::ReInstall
) != pkgDepCache::ReInstall
)
637 _error
->Error("Internal Error, trying to manipulate a kept package (%s)",Pkg
.Name());
641 // Perform a delete or an install
642 if (Cache
[Pkg
].Delete() == true)
644 if (SmartRemove(Pkg
) == false)
648 if (SmartUnPack(Pkg
) == false)
650 DoneSomething
= true;
653 // Final run through the configure phase
654 if (ConfigureAll() == false)
658 for (pkgOrderList::iterator I
= List
->begin(); I
!= List
->end(); I
++)
660 if (List
->IsFlag(*I
,pkgOrderList::Configured
) == false)
662 _error
->Error("Internal error, packages left unconfigured. %s",
663 PkgIterator(Cache
,*I
).Name());
671 // PM::DoInstallPostFork - Does install part that happens after the fork /*{{{*/
672 // ---------------------------------------------------------------------
673 pkgPackageManager::OrderResult
674 pkgPackageManager::DoInstallPostFork(int statusFd
)
677 // FIXME: use SetCloseExec here once it taught about throwing
678 // exceptions instead of doing _exit(100) on failure
679 fcntl(statusFd
,F_SETFD
,FD_CLOEXEC
);
680 bool goResult
= Go(statusFd
);
681 if(goResult
== false)
687 // PM::DoInstall - Does the installation /*{{{*/
688 // ---------------------------------------------------------------------
689 /* This uses the filenames in FileNames and the information in the
690 DepCache to perform the installation of packages.*/
691 pkgPackageManager::OrderResult
pkgPackageManager::DoInstall(int statusFd
)
693 if(DoInstallPreFork() == Failed
)
696 return DoInstallPostFork(statusFd
);