]>
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 if (pkgCache::VerIterator(Cache
, Cache
[Pkg
].CandidateVer
).Pseudo() == true)
87 new pkgAcqArchive(Owner
,Sources
,Recs
,Cache
[Pkg
].InstVerIter(Cache
),
94 // PM::FixMissing - Keep all missing packages /*{{{*/
95 // ---------------------------------------------------------------------
96 /* This is called to correct the installation when packages could not
98 bool pkgPackageManager::FixMissing()
100 pkgDepCache::ActionGroup
group(Cache
);
101 pkgProblemResolver
Resolve(&Cache
);
102 List
->SetFileList(FileNames
);
105 for (PkgIterator I
= Cache
.PkgBegin(); I
.end() == false; I
++)
107 if (List
->IsMissing(I
) == false)
110 // Okay, this file is missing and we need it. Mark it for keep
112 Cache
.MarkKeep(I
, false, false);
115 // We have to empty the list otherwise it will not have the new changes
122 // Now downgrade everything that is broken
123 return Resolve
.ResolveByKeep() == true && Cache
.BrokenCount() == 0;
126 // PM::ImmediateAdd - Add the immediate flag recursivly /*{{{*/
127 // ---------------------------------------------------------------------
128 /* This adds the immediate flag to the pkg and recursively to the
131 void pkgPackageManager::ImmediateAdd(PkgIterator I
, bool UseInstallVer
, unsigned const int &Depth
)
137 if(Cache
[I
].InstallVer
== 0)
139 D
= Cache
[I
].InstVerIter(Cache
).DependsList();
141 if (I
->CurrentVer
== 0)
143 D
= I
.CurrentVer().DependsList();
146 for ( /* nothing */ ; D
.end() == false; D
++)
147 if (D
->Type
== pkgCache::Dep::Depends
|| D
->Type
== pkgCache::Dep::PreDepends
)
149 if(!List
->IsFlag(D
.TargetPkg(), pkgOrderList::Immediate
))
152 clog
<< OutputInDepth(Depth
) << "ImmediateAdd(): Adding Immediate flag to " << D
.TargetPkg() << " cause of " << D
.DepType() << " " << I
.Name() << endl
;
153 List
->Flag(D
.TargetPkg(),pkgOrderList::Immediate
);
154 ImmediateAdd(D
.TargetPkg(), UseInstallVer
, Depth
+ 1);
160 // PM::CreateOrderList - Create the ordering class /*{{{*/
161 // ---------------------------------------------------------------------
162 /* This populates the ordering list with all the packages that are
164 bool pkgPackageManager::CreateOrderList()
170 List
= new pkgOrderList(&Cache
);
172 static bool const NoImmConfigure
= !_config
->FindB("APT::Immediate-Configure",true);
174 // Generate the list of affected packages and sort it
175 for (PkgIterator I
= Cache
.PkgBegin(); I
.end() == false; I
++)
177 // Ignore no-version packages
178 if (I
->VersionList
== 0)
181 // Mark the package and its dependends for immediate configuration
182 if (((I
->Flags
& pkgCache::Flag::Essential
) == pkgCache::Flag::Essential
||
183 (I
->Flags
& pkgCache::Flag::Important
) == pkgCache::Flag::Important
) &&
184 NoImmConfigure
== false)
187 clog
<< "CreateOrderList(): Adding Immediate flag for " << I
.Name() << endl
;
188 List
->Flag(I
,pkgOrderList::Immediate
);
190 // Look for other install packages to make immediate configurea
191 ImmediateAdd(I
, true);
193 // And again with the current version.
194 ImmediateAdd(I
, false);
198 if ((Cache
[I
].Keep() == true ||
199 Cache
[I
].InstVerIter(Cache
) == I
.CurrentVer()) &&
200 I
.State() == pkgCache::PkgIterator::NeedsNothing
&&
201 (Cache
[I
].iFlags
& pkgDepCache::ReInstall
) != pkgDepCache::ReInstall
&&
202 (I
.Purge() != false || Cache
[I
].Mode
!= pkgDepCache::ModeDelete
||
203 (Cache
[I
].iFlags
& pkgDepCache::Purge
) != pkgDepCache::Purge
))
206 // Append it to the list
213 // PM::DepAlwaysTrue - Returns true if this dep is irrelevent /*{{{*/
214 // ---------------------------------------------------------------------
215 /* The restriction on provides is to eliminate the case when provides
216 are transitioning between valid states [ie exim to smail] */
217 bool pkgPackageManager::DepAlwaysTrue(DepIterator D
)
219 if (D
.TargetPkg()->ProvidesList
!= 0)
222 if ((Cache
[D
] & pkgDepCache::DepInstall
) != 0 &&
223 (Cache
[D
] & pkgDepCache::DepNow
) != 0)
228 // PM::CheckRConflicts - Look for reverse conflicts /*{{{*/
229 // ---------------------------------------------------------------------
230 /* This looks over the reverses for a conflicts line that needs early
232 bool pkgPackageManager::CheckRConflicts(PkgIterator Pkg
,DepIterator D
,
235 for (;D
.end() == false; D
++)
237 if (D
->Type
!= pkgCache::Dep::Conflicts
&&
238 D
->Type
!= pkgCache::Dep::Obsoletes
)
241 // The package hasnt been changed
242 if (List
->IsNow(Pkg
) == false)
245 // Ignore self conflicts, ignore conflicts from irrelevent versions
246 if (D
.ParentPkg() == Pkg
|| D
.ParentVer() != D
.ParentPkg().CurrentVer())
249 if (Cache
.VS().CheckDep(Ver
,D
->CompareOp
,D
.TargetVer()) == false)
252 if (EarlyRemove(D
.ParentPkg()) == false)
253 return _error
->Error("Reverse conflicts early remove for package '%s' failed",
259 // PM::ConfigureAll - Run the all out configuration /*{{{*/
260 // ---------------------------------------------------------------------
261 /* This configures every package. It is assumed they are all unpacked and
262 that the final configuration is valid. */
263 bool pkgPackageManager::ConfigureAll()
265 pkgOrderList
OList(&Cache
);
267 // Populate the order list
268 for (pkgOrderList::iterator I
= List
->begin(); I
!= List
->end(); I
++)
269 if (List
->IsFlag(pkgCache::PkgIterator(Cache
,*I
),
270 pkgOrderList::UnPacked
) == true)
273 if (OList
.OrderConfigure() == false)
276 std::string
const conf
= _config
->Find("PackageManager::Configure","all");
277 bool const ConfigurePkgs
= (conf
== "all");
279 // Perform the configuring
280 for (pkgOrderList::iterator I
= OList
.begin(); I
!= OList
.end(); I
++)
282 PkgIterator
Pkg(Cache
,*I
);
284 if (ConfigurePkgs
== true &&
285 pkgCache::VerIterator(Cache
, Cache
[Pkg
].CandidateVer
).Pseudo() == false &&
286 Configure(Pkg
) == false)
289 List
->Flag(Pkg
,pkgOrderList::Configured
,pkgOrderList::States
);
295 // PM::SmartConfigure - Perform immediate configuration of the pkg /*{{{*/
296 // ---------------------------------------------------------------------
297 /* This routine scheduals the configuration of the given package and all
298 of it's dependents. */
299 bool pkgPackageManager::SmartConfigure(PkgIterator Pkg
)
302 clog
<< "SmartConfigure " << Pkg
.Name() << endl
;
304 pkgOrderList
OList(&Cache
);
306 if (DepAdd(OList
,Pkg
) == false)
309 static std::string
const conf
= _config
->Find("PackageManager::Configure","all");
310 static bool const ConfigurePkgs
= (conf
== "all" || conf
== "smart");
312 if (ConfigurePkgs
== true)
313 if (OList
.OrderConfigure() == false)
316 // Perform the configuring
317 for (pkgOrderList::iterator I
= OList
.begin(); I
!= OList
.end(); I
++)
319 PkgIterator
Pkg(Cache
,*I
);
321 if (ConfigurePkgs
== true &&
322 pkgCache::VerIterator(Cache
, Cache
[Pkg
].CandidateVer
).Pseudo() == false &&
323 Configure(Pkg
) == false)
326 List
->Flag(Pkg
,pkgOrderList::Configured
,pkgOrderList::States
);
330 if (List
->IsFlag(Pkg
,pkgOrderList::Configured
) == false)
331 return _error
->Error(_("Could not perform immediate configuration on '%s'."
332 "Please see man 5 apt.conf under APT::Immediate-Configure for details. (%d)"),Pkg
.Name(),1);
337 // PM::DepAdd - Add all dependents to the oder list /*{{{*/
338 // ---------------------------------------------------------------------
339 /* This recursively adds all dependents to the order list */
340 bool pkgPackageManager::DepAdd(pkgOrderList
&OList
,PkgIterator Pkg
,int Depth
)
342 if (OList
.IsFlag(Pkg
,pkgOrderList::Added
) == true)
344 if (List
->IsFlag(Pkg
,pkgOrderList::Configured
) == true)
346 if (List
->IsFlag(Pkg
,pkgOrderList::UnPacked
) == false)
349 // Put the package on the list
350 OList
.push_back(Pkg
);
351 OList
.Flag(Pkg
,pkgOrderList::Added
);
354 // Check the dependencies to see if they are all satisfied.
356 for (DepIterator D
= Cache
[Pkg
].InstVerIter(Cache
).DependsList(); D
.end() == false;)
358 if (D
->Type
!= pkgCache::Dep::Depends
&& D
->Type
!= pkgCache::Dep::PreDepends
)
366 for (bool LastOR
= true; D
.end() == false && LastOR
== true; D
++)
368 LastOR
= (D
->CompareOp
& pkgCache::Dep::Or
) == pkgCache::Dep::Or
;
373 SPtrArray
<Version
*> VList
= D
.AllTargets();
374 for (Version
**I
= VList
; *I
!= 0 && Bad
== true; I
++)
376 VerIterator
Ver(Cache
,*I
);
377 PkgIterator Pkg
= Ver
.ParentPkg();
379 // See if the current version is ok
380 if (Pkg
.CurrentVer() == Ver
&& List
->IsNow(Pkg
) == true &&
381 Pkg
.State() == PkgIterator::NeedsNothing
)
387 // Not the install version
388 if (Cache
[Pkg
].InstallVer
!= *I
||
389 (Cache
[Pkg
].Keep() == true && Pkg
.State() == PkgIterator::NeedsNothing
))
392 if (List
->IsFlag(Pkg
,pkgOrderList::UnPacked
) == true)
393 Bad
= !DepAdd(OList
,Pkg
,Depth
);
394 if (List
->IsFlag(Pkg
,pkgOrderList::Configured
) == true)
401 OList
.Flag(Pkg
,0,pkgOrderList::Added
);
412 // PM::EarlyRemove - Perform removal of packages before their time /*{{{*/
413 // ---------------------------------------------------------------------
414 /* This is called to deal with conflicts arising from unpacking */
415 bool pkgPackageManager::EarlyRemove(PkgIterator Pkg
)
417 if (List
->IsNow(Pkg
) == false)
420 // Already removed it
421 if (List
->IsFlag(Pkg
,pkgOrderList::Removed
) == true)
424 // Woops, it will not be re-installed!
425 if (List
->IsFlag(Pkg
,pkgOrderList::InList
) == false)
428 // Essential packages get special treatment
429 bool IsEssential
= false;
430 if ((Pkg
->Flags
& pkgCache::Flag::Essential
) != 0)
433 /* Check for packages that are the dependents of essential packages and
435 if (Pkg
->CurrentVer
!= 0)
437 for (DepIterator D
= Pkg
.RevDependsList(); D
.end() == false &&
438 IsEssential
== false; D
++)
439 if (D
->Type
== pkgCache::Dep::Depends
|| D
->Type
== pkgCache::Dep::PreDepends
)
440 if ((D
.ParentPkg()->Flags
& pkgCache::Flag::Essential
) != 0)
444 if (IsEssential
== true)
446 if (_config
->FindB("APT::Force-LoopBreak",false) == false)
447 return _error
->Error(_("This installation run will require temporarily "
448 "removing the essential package %s due to a "
449 "Conflicts/Pre-Depends loop. This is often bad, "
450 "but if you really want to do it, activate the "
451 "APT::Force-LoopBreak option."),Pkg
.Name());
454 bool Res
= SmartRemove(Pkg
);
455 if (Cache
[Pkg
].Delete() == false)
456 List
->Flag(Pkg
,pkgOrderList::Removed
,pkgOrderList::States
);
461 // PM::SmartRemove - Removal Helper /*{{{*/
462 // ---------------------------------------------------------------------
464 bool pkgPackageManager::SmartRemove(PkgIterator Pkg
)
466 if (List
->IsNow(Pkg
) == false)
469 List
->Flag(Pkg
,pkgOrderList::Configured
,pkgOrderList::States
);
471 if (pkgCache::VerIterator(Cache
, Cache
[Pkg
].CandidateVer
).Pseudo() == false)
472 return Remove(Pkg
,(Cache
[Pkg
].iFlags
& pkgDepCache::Purge
) == pkgDepCache::Purge
);
476 // PM::SmartUnPack - Install helper /*{{{*/
477 // ---------------------------------------------------------------------
478 /* This performs the task of handling pre-depends. */
479 bool pkgPackageManager::SmartUnPack(PkgIterator Pkg
)
481 // Check if it is already unpacked
482 if (Pkg
.State() == pkgCache::PkgIterator::NeedsConfigure
&&
483 Cache
[Pkg
].Keep() == true)
485 List
->Flag(Pkg
,pkgOrderList::UnPacked
,pkgOrderList::States
);
486 if (List
->IsFlag(Pkg
,pkgOrderList::Immediate
) == true)
487 if (SmartConfigure(Pkg
) == false)
488 return _error
->Error(_("Could not perform immediate configuration on already unpacked '%s'."
489 "Please see man 5 apt.conf under APT::Immediate-Configure for details."),Pkg
.Name());
493 /* See if this packages install version has any predependencies
494 that are not met by 'now' packages. */
495 for (DepIterator D
= Cache
[Pkg
].InstVerIter(Cache
).DependsList();
498 // Compute a single dependency element (glob or)
499 pkgCache::DepIterator Start
;
500 pkgCache::DepIterator End
;
503 while (End
->Type
== pkgCache::Dep::PreDepends
)
506 clog
<< "PreDepends order for " << Pkg
.Name() << std::endl
;
508 // Look for possible ok targets.
509 SPtrArray
<Version
*> VList
= Start
.AllTargets();
511 for (Version
**I
= VList
; *I
!= 0 && Bad
== true; I
++)
513 VerIterator
Ver(Cache
,*I
);
514 PkgIterator Pkg
= Ver
.ParentPkg();
516 // See if the current version is ok
517 if (Pkg
.CurrentVer() == Ver
&& List
->IsNow(Pkg
) == true &&
518 Pkg
.State() == PkgIterator::NeedsNothing
)
522 clog
<< "Found ok package " << Pkg
.Name() << endl
;
527 // Look for something that could be configured.
528 for (Version
**I
= VList
; *I
!= 0 && Bad
== true; I
++)
530 VerIterator
Ver(Cache
,*I
);
531 PkgIterator Pkg
= Ver
.ParentPkg();
533 // Not the install version
534 if (Cache
[Pkg
].InstallVer
!= *I
||
535 (Cache
[Pkg
].Keep() == true && Pkg
.State() == PkgIterator::NeedsNothing
))
539 clog
<< "Trying to SmartConfigure " << Pkg
.Name() << endl
;
540 Bad
= !SmartConfigure(Pkg
);
543 /* If this or element did not match then continue on to the
544 next or element until a matching element is found */
547 // This triggers if someone make a pre-depends/depend loop.
549 return _error
->Error("Couldn't configure pre-depend %s for %s, "
550 "probably a dependency cycle.",
551 End
.TargetPkg().Name(),Pkg
.Name());
558 if (End
->Type
== pkgCache::Dep::Conflicts
||
559 End
->Type
== pkgCache::Dep::Obsoletes
)
561 /* Look for conflicts. Two packages that are both in the install
562 state cannot conflict so we don't check.. */
563 SPtrArray
<Version
*> VList
= End
.AllTargets();
564 for (Version
**I
= VList
; *I
!= 0; I
++)
566 VerIterator
Ver(Cache
,*I
);
567 PkgIterator Pkg
= Ver
.ParentPkg();
569 // See if the current version is conflicting
570 if (Pkg
.CurrentVer() == Ver
&& List
->IsNow(Pkg
) == true)
572 if (EarlyRemove(Pkg
) == false)
573 return _error
->Error("Internal Error, Could not early remove %s",Pkg
.Name());
579 // Check for reverse conflicts.
580 if (CheckRConflicts(Pkg
,Pkg
.RevDependsList(),
581 Cache
[Pkg
].InstVerIter(Cache
).VerStr()) == false)
584 for (PrvIterator P
= Cache
[Pkg
].InstVerIter(Cache
).ProvidesList();
585 P
.end() == false; P
++)
586 CheckRConflicts(Pkg
,P
.ParentPkg().RevDependsList(),P
.ProvideVersion());
588 if (pkgCache::VerIterator(Cache
, Cache
[Pkg
].CandidateVer
).Pseudo() == false &&
589 Install(Pkg
,FileNames
[Pkg
->ID
]) == false)
592 List
->Flag(Pkg
,pkgOrderList::UnPacked
,pkgOrderList::States
);
594 // Perform immedate configuration of the package.
595 if (List
->IsFlag(Pkg
,pkgOrderList::Immediate
) == true)
596 if (SmartConfigure(Pkg
) == false)
597 return _error
->Error(_("Could not perform immediate configuration on '%s'."
598 "Please see man 5 apt.conf under APT::Immediate-Configure for details. (%d)"),Pkg
.Name(),2);
603 // PM::OrderInstall - Installation ordering routine /*{{{*/
604 // ---------------------------------------------------------------------
606 pkgPackageManager::OrderResult
pkgPackageManager::OrderInstall()
608 if (CreateOrderList() == false)
614 clog
<< "Beginning to order" << endl
;
616 bool const ordering
=
617 _config
->FindB("PackageManager::UnpackAll",true) ?
618 List
->OrderUnpack(FileNames
) : List
->OrderCritical();
619 if (ordering
== false)
621 _error
->Error("Internal ordering error");
626 clog
<< "Done ordering" << endl
;
628 bool DoneSomething
= false;
629 for (pkgOrderList::iterator I
= List
->begin(); I
!= List
->end(); I
++)
631 PkgIterator
Pkg(Cache
,*I
);
633 if (List
->IsNow(Pkg
) == false)
636 clog
<< "Skipping already done " << Pkg
.Name() << endl
;
640 if (List
->IsMissing(Pkg
) == true)
643 clog
<< "Sequence completed at " << Pkg
.Name() << endl
;
644 if (DoneSomething
== false)
646 _error
->Error("Internal Error, ordering was unable to handle the media swap");
653 if (Cache
[Pkg
].Keep() == true &&
654 Pkg
.State() == pkgCache::PkgIterator::NeedsNothing
&&
655 (Cache
[Pkg
].iFlags
& pkgDepCache::ReInstall
) != pkgDepCache::ReInstall
)
657 _error
->Error("Internal Error, trying to manipulate a kept package (%s)",Pkg
.Name());
661 // Perform a delete or an install
662 if (Cache
[Pkg
].Delete() == true)
664 if (SmartRemove(Pkg
) == false)
668 if (SmartUnPack(Pkg
) == false)
670 DoneSomething
= true;
673 // Final run through the configure phase
674 if (ConfigureAll() == false)
678 for (pkgOrderList::iterator I
= List
->begin(); I
!= List
->end(); I
++)
680 if (List
->IsFlag(*I
,pkgOrderList::Configured
) == false)
682 _error
->Error("Internal error, packages left unconfigured. %s",
683 PkgIterator(Cache
,*I
).Name());
691 // PM::DoInstallPostFork - Does install part that happens after the fork /*{{{*/
692 // ---------------------------------------------------------------------
693 pkgPackageManager::OrderResult
694 pkgPackageManager::DoInstallPostFork(int statusFd
)
697 // FIXME: use SetCloseExec here once it taught about throwing
698 // exceptions instead of doing _exit(100) on failure
699 fcntl(statusFd
,F_SETFD
,FD_CLOEXEC
);
700 bool goResult
= Go(statusFd
);
701 if(goResult
== false)
707 // PM::DoInstall - Does the installation /*{{{*/
708 // ---------------------------------------------------------------------
709 /* This uses the filenames in FileNames and the information in the
710 DepCache to perform the installation of packages.*/
711 pkgPackageManager::OrderResult
pkgPackageManager::DoInstall(int statusFd
)
713 if(DoInstallPreFork() == Failed
)
716 return DoInstallPostFork(statusFd
);