]>
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 if(!List
->IsFlag(D
.TargetPkg(), pkgOrderList::Immediate
))
149 clog
<< OutputInDepth(Depth
) << "ImmediateAdd(): Adding Immediate flag to " << D
.TargetPkg() << " cause of " << D
.DepType() << " " << I
.Name() << endl
;
150 List
->Flag(D
.TargetPkg(),pkgOrderList::Immediate
);
151 ImmediateAdd(D
.TargetPkg(), UseInstallVer
, Depth
+ 1);
157 // PM::CreateOrderList - Create the ordering class /*{{{*/
158 // ---------------------------------------------------------------------
159 /* This populates the ordering list with all the packages that are
161 bool pkgPackageManager::CreateOrderList()
167 List
= new pkgOrderList(&Cache
);
169 static bool const NoImmConfigure
= !_config
->FindB("APT::Immediate-Configure",true);
171 // Generate the list of affected packages and sort it
172 for (PkgIterator I
= Cache
.PkgBegin(); I
.end() == false; I
++)
174 // Ignore no-version packages
175 if (I
->VersionList
== 0)
178 // Mark the package and its dependends for immediate configuration
179 if (((I
->Flags
& pkgCache::Flag::Essential
) == pkgCache::Flag::Essential
||
180 (I
->Flags
& pkgCache::Flag::Important
) == pkgCache::Flag::Important
) &&
181 NoImmConfigure
== false)
184 clog
<< "CreateOrderList(): Adding Immediate flag for " << I
.Name() << endl
;
185 List
->Flag(I
,pkgOrderList::Immediate
);
187 // Look for other install packages to make immediate configurea
188 ImmediateAdd(I
, true);
190 // And again with the current version.
191 ImmediateAdd(I
, false);
195 if ((Cache
[I
].Keep() == true ||
196 Cache
[I
].InstVerIter(Cache
) == I
.CurrentVer()) &&
197 I
.State() == pkgCache::PkgIterator::NeedsNothing
&&
198 (Cache
[I
].iFlags
& pkgDepCache::ReInstall
) != pkgDepCache::ReInstall
&&
199 (I
.Purge() != false || Cache
[I
].Mode
!= pkgDepCache::ModeDelete
||
200 (Cache
[I
].iFlags
& pkgDepCache::Purge
) != pkgDepCache::Purge
))
203 // Append it to the list
210 // PM::DepAlwaysTrue - Returns true if this dep is irrelevent /*{{{*/
211 // ---------------------------------------------------------------------
212 /* The restriction on provides is to eliminate the case when provides
213 are transitioning between valid states [ie exim to smail] */
214 bool pkgPackageManager::DepAlwaysTrue(DepIterator D
)
216 if (D
.TargetPkg()->ProvidesList
!= 0)
219 if ((Cache
[D
] & pkgDepCache::DepInstall
) != 0 &&
220 (Cache
[D
] & pkgDepCache::DepNow
) != 0)
225 // PM::CheckRConflicts - Look for reverse conflicts /*{{{*/
226 // ---------------------------------------------------------------------
227 /* This looks over the reverses for a conflicts line that needs early
229 bool pkgPackageManager::CheckRConflicts(PkgIterator Pkg
,DepIterator D
,
232 for (;D
.end() == false; D
++)
234 if (D
->Type
!= pkgCache::Dep::Conflicts
&&
235 D
->Type
!= pkgCache::Dep::Obsoletes
)
238 // The package hasnt been changed
239 if (List
->IsNow(Pkg
) == false)
242 // Ignore self conflicts, ignore conflicts from irrelevent versions
243 if (D
.ParentPkg() == Pkg
|| D
.ParentVer() != D
.ParentPkg().CurrentVer())
246 if (Cache
.VS().CheckDep(Ver
,D
->CompareOp
,D
.TargetVer()) == false)
249 if (EarlyRemove(D
.ParentPkg()) == false)
250 return _error
->Error("Reverse conflicts early remove for package '%s' failed",
256 // PM::ConfigureAll - Run the all out configuration /*{{{*/
257 // ---------------------------------------------------------------------
258 /* This configures every package. It is assumed they are all unpacked and
259 that the final configuration is valid. */
260 bool pkgPackageManager::ConfigureAll()
262 pkgOrderList
OList(&Cache
);
264 // Populate the order list
265 for (pkgOrderList::iterator I
= List
->begin(); I
!= List
->end(); I
++)
266 if (List
->IsFlag(pkgCache::PkgIterator(Cache
,*I
),
267 pkgOrderList::UnPacked
) == true)
270 if (OList
.OrderConfigure() == false)
273 std::string
const conf
= _config
->Find("PackageManager::Configure","all");
274 bool const ConfigurePkgs
= (conf
== "all");
276 // Perform the configuring
277 for (pkgOrderList::iterator I
= OList
.begin(); I
!= OList
.end(); I
++)
279 PkgIterator
Pkg(Cache
,*I
);
281 if (ConfigurePkgs
== true && Configure(Pkg
) == false)
284 List
->Flag(Pkg
,pkgOrderList::Configured
,pkgOrderList::States
);
290 // PM::SmartConfigure - Perform immediate configuration of the pkg /*{{{*/
291 // ---------------------------------------------------------------------
292 /* This routine scheduals the configuration of the given package and all
293 of it's dependents. */
294 bool pkgPackageManager::SmartConfigure(PkgIterator Pkg
)
296 pkgOrderList
OList(&Cache
);
298 if (DepAdd(OList
,Pkg
) == false)
301 static std::string
const conf
= _config
->Find("PackageManager::Configure","all");
302 static bool const ConfigurePkgs
= (conf
== "all" || conf
== "smart");
304 if (ConfigurePkgs
== true)
305 if (OList
.OrderConfigure() == false)
308 // Perform the configuring
309 for (pkgOrderList::iterator I
= OList
.begin(); I
!= OList
.end(); I
++)
311 PkgIterator
Pkg(Cache
,*I
);
313 if (ConfigurePkgs
== true && Configure(Pkg
) == false)
316 List
->Flag(Pkg
,pkgOrderList::Configured
,pkgOrderList::States
);
320 if (List
->IsFlag(Pkg
,pkgOrderList::Configured
) == false)
321 return _error
->Error(_("Could not perform immediate configuration on '%s'."
322 "Please see man 5 apt.conf under APT::Immediate-Configure for details. (%d)"),Pkg
.Name(),1);
327 // PM::DepAdd - Add all dependents to the oder list /*{{{*/
328 // ---------------------------------------------------------------------
329 /* This recursively adds all dependents to the order list */
330 bool pkgPackageManager::DepAdd(pkgOrderList
&OList
,PkgIterator Pkg
,int Depth
)
332 if (OList
.IsFlag(Pkg
,pkgOrderList::Added
) == true)
334 if (List
->IsFlag(Pkg
,pkgOrderList::Configured
) == true)
336 if (List
->IsFlag(Pkg
,pkgOrderList::UnPacked
) == false)
339 // Put the package on the list
340 OList
.push_back(Pkg
);
341 OList
.Flag(Pkg
,pkgOrderList::Added
);
344 // Check the dependencies to see if they are all satisfied.
346 for (DepIterator D
= Cache
[Pkg
].InstVerIter(Cache
).DependsList(); D
.end() == false;)
348 if (D
->Type
!= pkgCache::Dep::Depends
&& D
->Type
!= pkgCache::Dep::PreDepends
)
356 for (bool LastOR
= true; D
.end() == false && LastOR
== true; D
++)
358 LastOR
= (D
->CompareOp
& pkgCache::Dep::Or
) == pkgCache::Dep::Or
;
363 SPtrArray
<Version
*> VList
= D
.AllTargets();
364 for (Version
**I
= VList
; *I
!= 0 && Bad
== true; I
++)
366 VerIterator
Ver(Cache
,*I
);
367 PkgIterator Pkg
= Ver
.ParentPkg();
369 // See if the current version is ok
370 if (Pkg
.CurrentVer() == Ver
&& List
->IsNow(Pkg
) == true &&
371 Pkg
.State() == PkgIterator::NeedsNothing
)
377 // Not the install version
378 if (Cache
[Pkg
].InstallVer
!= *I
||
379 (Cache
[Pkg
].Keep() == true && Pkg
.State() == PkgIterator::NeedsNothing
))
382 if (List
->IsFlag(Pkg
,pkgOrderList::UnPacked
) == true)
383 Bad
= !DepAdd(OList
,Pkg
,Depth
);
384 if (List
->IsFlag(Pkg
,pkgOrderList::Configured
) == true)
391 OList
.Flag(Pkg
,0,pkgOrderList::Added
);
402 // PM::EarlyRemove - Perform removal of packages before their time /*{{{*/
403 // ---------------------------------------------------------------------
404 /* This is called to deal with conflicts arising from unpacking */
405 bool pkgPackageManager::EarlyRemove(PkgIterator Pkg
)
407 if (List
->IsNow(Pkg
) == false)
410 // Already removed it
411 if (List
->IsFlag(Pkg
,pkgOrderList::Removed
) == true)
414 // Woops, it will not be re-installed!
415 if (List
->IsFlag(Pkg
,pkgOrderList::InList
) == false)
418 // Essential packages get special treatment
419 bool IsEssential
= false;
420 if ((Pkg
->Flags
& pkgCache::Flag::Essential
) != 0)
423 /* Check for packages that are the dependents of essential packages and
425 if (Pkg
->CurrentVer
!= 0)
427 for (DepIterator D
= Pkg
.RevDependsList(); D
.end() == false &&
428 IsEssential
== false; D
++)
429 if (D
->Type
== pkgCache::Dep::Depends
|| D
->Type
== pkgCache::Dep::PreDepends
)
430 if ((D
.ParentPkg()->Flags
& pkgCache::Flag::Essential
) != 0)
434 if (IsEssential
== true)
436 if (_config
->FindB("APT::Force-LoopBreak",false) == false)
437 return _error
->Error(_("This installation run will require temporarily "
438 "removing the essential package %s due to a "
439 "Conflicts/Pre-Depends loop. This is often bad, "
440 "but if you really want to do it, activate the "
441 "APT::Force-LoopBreak option."),Pkg
.Name());
444 bool Res
= SmartRemove(Pkg
);
445 if (Cache
[Pkg
].Delete() == false)
446 List
->Flag(Pkg
,pkgOrderList::Removed
,pkgOrderList::States
);
451 // PM::SmartRemove - Removal Helper /*{{{*/
452 // ---------------------------------------------------------------------
454 bool pkgPackageManager::SmartRemove(PkgIterator Pkg
)
456 if (List
->IsNow(Pkg
) == false)
459 List
->Flag(Pkg
,pkgOrderList::Configured
,pkgOrderList::States
);
460 return Remove(Pkg
,(Cache
[Pkg
].iFlags
& pkgDepCache::Purge
) == pkgDepCache::Purge
);
463 // PM::SmartUnPack - Install helper /*{{{*/
464 // ---------------------------------------------------------------------
465 /* This performs the task of handling pre-depends. */
466 bool pkgPackageManager::SmartUnPack(PkgIterator Pkg
)
468 // Check if it is already unpacked
469 if (Pkg
.State() == pkgCache::PkgIterator::NeedsConfigure
&&
470 Cache
[Pkg
].Keep() == true)
472 List
->Flag(Pkg
,pkgOrderList::UnPacked
,pkgOrderList::States
);
473 if (List
->IsFlag(Pkg
,pkgOrderList::Immediate
) == true)
474 if (SmartConfigure(Pkg
) == false)
475 return _error
->Error(_("Could not perform immediate configuration on already unpacked '%s'."
476 "Please see man 5 apt.conf under APT::Immediate-Configure for details."),Pkg
.Name());
480 /* See if this packages install version has any predependencies
481 that are not met by 'now' packages. */
482 for (DepIterator D
= Cache
[Pkg
].InstVerIter(Cache
).DependsList();
485 // Compute a single dependency element (glob or)
486 pkgCache::DepIterator Start
;
487 pkgCache::DepIterator End
;
490 while (End
->Type
== pkgCache::Dep::PreDepends
)
492 // Look for possible ok targets.
493 SPtrArray
<Version
*> VList
= Start
.AllTargets();
495 for (Version
**I
= VList
; *I
!= 0 && Bad
== true; I
++)
497 VerIterator
Ver(Cache
,*I
);
498 PkgIterator Pkg
= Ver
.ParentPkg();
500 // See if the current version is ok
501 if (Pkg
.CurrentVer() == Ver
&& List
->IsNow(Pkg
) == true &&
502 Pkg
.State() == PkgIterator::NeedsNothing
)
509 // Look for something that could be configured.
510 for (Version
**I
= VList
; *I
!= 0 && Bad
== true; I
++)
512 VerIterator
Ver(Cache
,*I
);
513 PkgIterator Pkg
= Ver
.ParentPkg();
515 // Not the install version
516 if (Cache
[Pkg
].InstallVer
!= *I
||
517 (Cache
[Pkg
].Keep() == true && Pkg
.State() == PkgIterator::NeedsNothing
))
520 Bad
= !SmartConfigure(Pkg
);
523 /* If this or element did not match then continue on to the
524 next or element until a matching element is found */
527 // This triggers if someone make a pre-depends/depend loop.
529 return _error
->Error("Couldn't configure pre-depend %s for %s, "
530 "probably a dependency cycle.",
531 End
.TargetPkg().Name(),Pkg
.Name());
538 if (End
->Type
== pkgCache::Dep::Conflicts
||
539 End
->Type
== pkgCache::Dep::Obsoletes
)
541 /* Look for conflicts. Two packages that are both in the install
542 state cannot conflict so we don't check.. */
543 SPtrArray
<Version
*> VList
= End
.AllTargets();
544 for (Version
**I
= VList
; *I
!= 0; I
++)
546 VerIterator
Ver(Cache
,*I
);
547 PkgIterator Pkg
= Ver
.ParentPkg();
549 // See if the current version is conflicting
550 if (Pkg
.CurrentVer() == Ver
&& List
->IsNow(Pkg
) == true)
552 if (EarlyRemove(Pkg
) == false)
553 return _error
->Error("Internal Error, Could not early remove %s",Pkg
.Name());
559 // Check for reverse conflicts.
560 if (CheckRConflicts(Pkg
,Pkg
.RevDependsList(),
561 Cache
[Pkg
].InstVerIter(Cache
).VerStr()) == false)
564 for (PrvIterator P
= Cache
[Pkg
].InstVerIter(Cache
).ProvidesList();
565 P
.end() == false; P
++)
566 CheckRConflicts(Pkg
,P
.ParentPkg().RevDependsList(),P
.ProvideVersion());
568 if (Install(Pkg
,FileNames
[Pkg
->ID
]) == false)
571 List
->Flag(Pkg
,pkgOrderList::UnPacked
,pkgOrderList::States
);
573 // Perform immedate configuration of the package.
574 if (List
->IsFlag(Pkg
,pkgOrderList::Immediate
) == true)
575 if (SmartConfigure(Pkg
) == false)
576 return _error
->Error(_("Could not perform immediate configuration on '%s'."
577 "Please see man 5 apt.conf under APT::Immediate-Configure for details. (%d)"),Pkg
.Name(),2);
582 // PM::OrderInstall - Installation ordering routine /*{{{*/
583 // ---------------------------------------------------------------------
585 pkgPackageManager::OrderResult
pkgPackageManager::OrderInstall()
587 if (CreateOrderList() == false)
593 clog
<< "Beginning to order" << endl
;
595 bool const ordering
=
596 _config
->FindB("PackageManager::UnpackAll",true) ?
597 List
->OrderUnpack(FileNames
) : List
->OrderCritical();
598 if (ordering
== false)
600 _error
->Error("Internal ordering error");
605 clog
<< "Done ordering" << endl
;
607 bool DoneSomething
= false;
608 for (pkgOrderList::iterator I
= List
->begin(); I
!= List
->end(); I
++)
610 PkgIterator
Pkg(Cache
,*I
);
612 if (List
->IsNow(Pkg
) == false)
615 clog
<< "Skipping already done " << Pkg
.Name() << endl
;
619 if (List
->IsMissing(Pkg
) == true)
622 clog
<< "Sequence completed at " << Pkg
.Name() << endl
;
623 if (DoneSomething
== false)
625 _error
->Error("Internal Error, ordering was unable to handle the media swap");
632 if (Cache
[Pkg
].Keep() == true &&
633 Pkg
.State() == pkgCache::PkgIterator::NeedsNothing
&&
634 (Cache
[Pkg
].iFlags
& pkgDepCache::ReInstall
) != pkgDepCache::ReInstall
)
636 _error
->Error("Internal Error, trying to manipulate a kept package (%s)",Pkg
.Name());
640 // Perform a delete or an install
641 if (Cache
[Pkg
].Delete() == true)
643 if (SmartRemove(Pkg
) == false)
647 if (SmartUnPack(Pkg
) == false)
649 DoneSomething
= true;
652 // Final run through the configure phase
653 if (ConfigureAll() == false)
657 for (pkgOrderList::iterator I
= List
->begin(); I
!= List
->end(); I
++)
659 if (List
->IsFlag(*I
,pkgOrderList::Configured
) == false)
661 _error
->Error("Internal error, packages left unconfigured. %s",
662 PkgIterator(Cache
,*I
).Name());
670 // PM::DoInstallPostFork - Does install part that happens after the fork /*{{{*/
671 // ---------------------------------------------------------------------
672 pkgPackageManager::OrderResult
673 pkgPackageManager::DoInstallPostFork(int statusFd
)
676 // FIXME: use SetCloseExec here once it taught about throwing
677 // exceptions instead of doing _exit(100) on failure
678 fcntl(statusFd
,F_SETFD
,FD_CLOEXEC
);
679 bool goResult
= Go(statusFd
);
680 if(goResult
== false)
686 // PM::DoInstall - Does the installation /*{{{*/
687 // ---------------------------------------------------------------------
688 /* This uses the filenames in FileNames and the information in the
689 DepCache to perform the installation of packages.*/
690 pkgPackageManager::OrderResult
pkgPackageManager::DoInstall(int statusFd
)
692 if(DoInstallPreFork() == Failed
)
695 return DoInstallPostFork(statusFd
);