]>
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)
60 if (List
->OrderUnpack() == false)
61 return _error
->Error("Internal ordering error");
63 for (pkgOrderList::iterator I
= List
->begin(); I
!= List
->end(); I
++)
65 PkgIterator
Pkg(Cache
,*I
);
66 FileNames
[Pkg
->ID
] = string();
68 // Skip packages to erase
69 if (Cache
[Pkg
].Delete() == true)
72 // Skip Packages that need configure only.
73 if (Pkg
.State() == pkgCache::PkgIterator::NeedsConfigure
&&
74 Cache
[Pkg
].Keep() == true)
77 // Skip already processed packages
78 if (List
->IsNow(Pkg
) == false)
81 new pkgAcqArchive(Owner
,Sources
,Recs
,Cache
[Pkg
].InstVerIter(Cache
),
88 // PM::FixMissing - Keep all missing packages /*{{{*/
89 // ---------------------------------------------------------------------
90 /* This is called to correct the installation when packages could not
92 bool pkgPackageManager::FixMissing()
94 pkgDepCache::ActionGroup
group(Cache
);
95 pkgProblemResolver
Resolve(&Cache
);
96 List
->SetFileList(FileNames
);
99 for (PkgIterator I
= Cache
.PkgBegin(); I
.end() == false; I
++)
101 if (List
->IsMissing(I
) == false)
104 // Okay, this file is missing and we need it. Mark it for keep
106 Cache
.MarkKeep(I
, false, false);
109 // We have to empty the list otherwise it will not have the new changes
116 // Now downgrade everything that is broken
117 return Resolve
.ResolveByKeep() == true && Cache
.BrokenCount() == 0;
120 // PM::ImmediateAdd - Add the immediate flag recursivly /*{{{*/
121 // ---------------------------------------------------------------------
122 /* This adds the immediate flag to the pkg and recursively to the
125 void pkgPackageManager::ImmediateAdd(PkgIterator I
, bool UseInstallVer
)
131 if(Cache
[I
].InstallVer
== 0)
133 D
= Cache
[I
].InstVerIter(Cache
).DependsList();
135 if (I
->CurrentVer
== 0)
137 D
= I
.CurrentVer().DependsList();
140 for ( /* nothing */ ; D
.end() == false; D
++)
141 if (D
->Type
== pkgCache::Dep::Depends
|| D
->Type
== pkgCache::Dep::PreDepends
)
143 if(!List
->IsFlag(D
.TargetPkg(), pkgOrderList::Immediate
))
146 clog
<< "ImmediateAdd(): Adding Immediate flag to " << I
.Name() << endl
;
147 List
->Flag(D
.TargetPkg(),pkgOrderList::Immediate
);
148 ImmediateAdd(D
.TargetPkg(), UseInstallVer
);
154 // PM::CreateOrderList - Create the ordering class /*{{{*/
155 // ---------------------------------------------------------------------
156 /* This populates the ordering list with all the packages that are
158 bool pkgPackageManager::CreateOrderList()
164 List
= new pkgOrderList(&Cache
);
166 bool NoImmConfigure
= !_config
->FindB("APT::Immediate-Configure",true);
168 // Generate the list of affected packages and sort it
169 for (PkgIterator I
= Cache
.PkgBegin(); I
.end() == false; I
++)
171 // Ignore no-version packages
172 if (I
->VersionList
== 0)
175 // Mark the package and its dependends for immediate configuration
176 if (((I
->Flags
& pkgCache::Flag::Essential
) == pkgCache::Flag::Essential
||
177 (I
->Flags
& pkgCache::Flag::Important
) == pkgCache::Flag::Important
) &&
178 NoImmConfigure
== false)
181 clog
<< "CreateOrderList(): Adding Immediate flag for " << I
.Name() << endl
;
182 List
->Flag(I
,pkgOrderList::Immediate
);
184 // Look for other install packages to make immediate configurea
185 ImmediateAdd(I
, true);
187 // And again with the current version.
188 ImmediateAdd(I
, false);
192 if ((Cache
[I
].Keep() == true ||
193 Cache
[I
].InstVerIter(Cache
) == I
.CurrentVer()) &&
194 I
.State() == pkgCache::PkgIterator::NeedsNothing
&&
195 (Cache
[I
].iFlags
& pkgDepCache::ReInstall
) != pkgDepCache::ReInstall
&&
196 (I
.Purge() != false || Cache
[I
].Mode
!= pkgDepCache::ModeDelete
||
197 (Cache
[I
].iFlags
& pkgDepCache::Purge
) != pkgDepCache::Purge
))
200 // Append it to the list
207 // PM::DepAlwaysTrue - Returns true if this dep is irrelevent /*{{{*/
208 // ---------------------------------------------------------------------
209 /* The restriction on provides is to eliminate the case when provides
210 are transitioning between valid states [ie exim to smail] */
211 bool pkgPackageManager::DepAlwaysTrue(DepIterator D
)
213 if (D
.TargetPkg()->ProvidesList
!= 0)
216 if ((Cache
[D
] & pkgDepCache::DepInstall
) != 0 &&
217 (Cache
[D
] & pkgDepCache::DepNow
) != 0)
222 // PM::CheckRConflicts - Look for reverse conflicts /*{{{*/
223 // ---------------------------------------------------------------------
224 /* This looks over the reverses for a conflicts line that needs early
226 bool pkgPackageManager::CheckRConflicts(PkgIterator Pkg
,DepIterator D
,
229 for (;D
.end() == false; D
++)
231 if (D
->Type
!= pkgCache::Dep::Conflicts
&&
232 D
->Type
!= pkgCache::Dep::Obsoletes
)
235 // The package hasnt been changed
236 if (List
->IsNow(Pkg
) == false)
239 // Ignore self conflicts, ignore conflicts from irrelevent versions
240 if (D
.ParentPkg() == Pkg
|| D
.ParentVer() != D
.ParentPkg().CurrentVer())
243 if (Cache
.VS().CheckDep(Ver
,D
->CompareOp
,D
.TargetVer()) == false)
246 if (EarlyRemove(D
.ParentPkg()) == false)
247 return _error
->Error("Reverse conflicts early remove for package '%s' failed",
253 // PM::ConfigureAll - Run the all out configuration /*{{{*/
254 // ---------------------------------------------------------------------
255 /* This configures every package. It is assumed they are all unpacked and
256 that the final configuration is valid. */
257 bool pkgPackageManager::ConfigureAll()
259 pkgOrderList
OList(&Cache
);
261 // Populate the order list
262 for (pkgOrderList::iterator I
= List
->begin(); I
!= List
->end(); I
++)
263 if (List
->IsFlag(pkgCache::PkgIterator(Cache
,*I
),
264 pkgOrderList::UnPacked
) == true)
267 if (OList
.OrderConfigure() == false)
270 // Perform the configuring
271 for (pkgOrderList::iterator I
= OList
.begin(); I
!= OList
.end(); I
++)
273 PkgIterator
Pkg(Cache
,*I
);
275 if (Configure(Pkg
) == false)
278 List
->Flag(Pkg
,pkgOrderList::Configured
,pkgOrderList::States
);
284 // PM::SmartConfigure - Perform immediate configuration of the pkg /*{{{*/
285 // ---------------------------------------------------------------------
286 /* This routine scheduals the configuration of the given package and all
287 of it's dependents. */
288 bool pkgPackageManager::SmartConfigure(PkgIterator Pkg
)
290 pkgOrderList
OList(&Cache
);
292 if (DepAdd(OList
,Pkg
) == false)
295 if (OList
.OrderConfigure() == false)
298 // Perform the configuring
299 for (pkgOrderList::iterator I
= OList
.begin(); I
!= OList
.end(); I
++)
301 PkgIterator
Pkg(Cache
,*I
);
303 if (Configure(Pkg
) == false)
306 List
->Flag(Pkg
,pkgOrderList::Configured
,pkgOrderList::States
);
310 if (List
->IsFlag(Pkg
,pkgOrderList::Configured
) == false)
311 return _error
->Error("Internal error, could not immediate configure %s",Pkg
.Name());
316 // PM::DepAdd - Add all dependents to the oder list /*{{{*/
317 // ---------------------------------------------------------------------
318 /* This recursively adds all dependents to the order list */
319 bool pkgPackageManager::DepAdd(pkgOrderList
&OList
,PkgIterator Pkg
,int Depth
)
321 if (OList
.IsFlag(Pkg
,pkgOrderList::Added
) == true)
323 if (List
->IsFlag(Pkg
,pkgOrderList::Configured
) == true)
325 if (List
->IsFlag(Pkg
,pkgOrderList::UnPacked
) == false)
328 // Put the package on the list
329 OList
.push_back(Pkg
);
330 OList
.Flag(Pkg
,pkgOrderList::Added
);
333 // Check the dependencies to see if they are all satisfied.
335 for (DepIterator D
= Cache
[Pkg
].InstVerIter(Cache
).DependsList(); D
.end() == false;)
337 if (D
->Type
!= pkgCache::Dep::Depends
&& D
->Type
!= pkgCache::Dep::PreDepends
)
345 for (bool LastOR
= true; D
.end() == false && LastOR
== true; D
++)
347 LastOR
= (D
->CompareOp
& pkgCache::Dep::Or
) == pkgCache::Dep::Or
;
352 SPtrArray
<Version
*> VList
= D
.AllTargets();
353 for (Version
**I
= VList
; *I
!= 0 && Bad
== true; I
++)
355 VerIterator
Ver(Cache
,*I
);
356 PkgIterator Pkg
= Ver
.ParentPkg();
358 // See if the current version is ok
359 if (Pkg
.CurrentVer() == Ver
&& List
->IsNow(Pkg
) == true &&
360 Pkg
.State() == PkgIterator::NeedsNothing
)
366 // Not the install version
367 if (Cache
[Pkg
].InstallVer
!= *I
||
368 (Cache
[Pkg
].Keep() == true && Pkg
.State() == PkgIterator::NeedsNothing
))
371 if (List
->IsFlag(Pkg
,pkgOrderList::UnPacked
) == true)
372 Bad
= !DepAdd(OList
,Pkg
,Depth
);
373 if (List
->IsFlag(Pkg
,pkgOrderList::Configured
) == true)
380 OList
.Flag(Pkg
,0,pkgOrderList::Added
);
391 // PM::EarlyRemove - Perform removal of packages before their time /*{{{*/
392 // ---------------------------------------------------------------------
393 /* This is called to deal with conflicts arising from unpacking */
394 bool pkgPackageManager::EarlyRemove(PkgIterator Pkg
)
396 if (List
->IsNow(Pkg
) == false)
399 // Already removed it
400 if (List
->IsFlag(Pkg
,pkgOrderList::Removed
) == true)
403 // Woops, it will not be re-installed!
404 if (List
->IsFlag(Pkg
,pkgOrderList::InList
) == false)
407 // Essential packages get special treatment
408 bool IsEssential
= false;
409 if ((Pkg
->Flags
& pkgCache::Flag::Essential
) != 0)
412 /* Check for packages that are the dependents of essential packages and
414 if (Pkg
->CurrentVer
!= 0)
416 for (DepIterator D
= Pkg
.RevDependsList(); D
.end() == false &&
417 IsEssential
== false; D
++)
418 if (D
->Type
== pkgCache::Dep::Depends
|| D
->Type
== pkgCache::Dep::PreDepends
)
419 if ((D
.ParentPkg()->Flags
& pkgCache::Flag::Essential
) != 0)
423 if (IsEssential
== true)
425 if (_config
->FindB("APT::Force-LoopBreak",false) == false)
426 return _error
->Error(_("This installation run will require temporarily "
427 "removing the essential package %s due to a "
428 "Conflicts/Pre-Depends loop. This is often bad, "
429 "but if you really want to do it, activate the "
430 "APT::Force-LoopBreak option."),Pkg
.Name());
433 bool Res
= SmartRemove(Pkg
);
434 if (Cache
[Pkg
].Delete() == false)
435 List
->Flag(Pkg
,pkgOrderList::Removed
,pkgOrderList::States
);
440 // PM::SmartRemove - Removal Helper /*{{{*/
441 // ---------------------------------------------------------------------
443 bool pkgPackageManager::SmartRemove(PkgIterator Pkg
)
445 if (List
->IsNow(Pkg
) == false)
448 List
->Flag(Pkg
,pkgOrderList::Configured
,pkgOrderList::States
);
449 return Remove(Pkg
,(Cache
[Pkg
].iFlags
& pkgDepCache::Purge
) == pkgDepCache::Purge
);
452 // PM::SmartUnPack - Install helper /*{{{*/
453 // ---------------------------------------------------------------------
454 /* This performs the task of handling pre-depends. */
455 bool pkgPackageManager::SmartUnPack(PkgIterator Pkg
)
457 // Check if it is already unpacked
458 if (Pkg
.State() == pkgCache::PkgIterator::NeedsConfigure
&&
459 Cache
[Pkg
].Keep() == true)
461 List
->Flag(Pkg
,pkgOrderList::UnPacked
,pkgOrderList::States
);
462 if (List
->IsFlag(Pkg
,pkgOrderList::Immediate
) == true)
463 if (SmartConfigure(Pkg
) == false)
464 return _error
->Error("Internal Error, Could not perform immediate configuration (1) on %s",Pkg
.Name());
468 /* See if this packages install version has any predependencies
469 that are not met by 'now' packages. */
470 for (DepIterator D
= Cache
[Pkg
].InstVerIter(Cache
).DependsList();
473 // Compute a single dependency element (glob or)
474 pkgCache::DepIterator Start
;
475 pkgCache::DepIterator End
;
478 while (End
->Type
== pkgCache::Dep::PreDepends
)
480 // Look for possible ok targets.
481 SPtrArray
<Version
*> VList
= Start
.AllTargets();
483 for (Version
**I
= VList
; *I
!= 0 && Bad
== true; I
++)
485 VerIterator
Ver(Cache
,*I
);
486 PkgIterator Pkg
= Ver
.ParentPkg();
488 // See if the current version is ok
489 if (Pkg
.CurrentVer() == Ver
&& List
->IsNow(Pkg
) == true &&
490 Pkg
.State() == PkgIterator::NeedsNothing
)
497 // Look for something that could be configured.
498 for (Version
**I
= VList
; *I
!= 0 && Bad
== true; I
++)
500 VerIterator
Ver(Cache
,*I
);
501 PkgIterator Pkg
= Ver
.ParentPkg();
503 // Not the install version
504 if (Cache
[Pkg
].InstallVer
!= *I
||
505 (Cache
[Pkg
].Keep() == true && Pkg
.State() == PkgIterator::NeedsNothing
))
508 Bad
= !SmartConfigure(Pkg
);
511 /* If this or element did not match then continue on to the
512 next or element until a matching element is found */
515 // This triggers if someone make a pre-depends/depend loop.
517 return _error
->Error("Couldn't configure pre-depend %s for %s, "
518 "probably a dependency cycle.",
519 End
.TargetPkg().Name(),Pkg
.Name());
526 if (End
->Type
== pkgCache::Dep::Conflicts
||
527 End
->Type
== pkgCache::Dep::Obsoletes
)
529 /* Look for conflicts. Two packages that are both in the install
530 state cannot conflict so we don't check.. */
531 SPtrArray
<Version
*> VList
= End
.AllTargets();
532 for (Version
**I
= VList
; *I
!= 0; I
++)
534 VerIterator
Ver(Cache
,*I
);
535 PkgIterator Pkg
= Ver
.ParentPkg();
537 // See if the current version is conflicting
538 if (Pkg
.CurrentVer() == Ver
&& List
->IsNow(Pkg
) == true)
540 if (EarlyRemove(Pkg
) == false)
541 return _error
->Error("Internal Error, Could not early remove %s",Pkg
.Name());
547 // Check for reverse conflicts.
548 if (CheckRConflicts(Pkg
,Pkg
.RevDependsList(),
549 Cache
[Pkg
].InstVerIter(Cache
).VerStr()) == false)
552 for (PrvIterator P
= Cache
[Pkg
].InstVerIter(Cache
).ProvidesList();
553 P
.end() == false; P
++)
554 CheckRConflicts(Pkg
,P
.ParentPkg().RevDependsList(),P
.ProvideVersion());
556 if (Install(Pkg
,FileNames
[Pkg
->ID
]) == false)
559 List
->Flag(Pkg
,pkgOrderList::UnPacked
,pkgOrderList::States
);
561 // Perform immedate configuration of the package.
562 if (List
->IsFlag(Pkg
,pkgOrderList::Immediate
) == true)
563 if (SmartConfigure(Pkg
) == false)
564 return _error
->Error("Internal Error, Could not perform immediate configuration (2) on %s",Pkg
.Name());
569 // PM::OrderInstall - Installation ordering routine /*{{{*/
570 // ---------------------------------------------------------------------
572 pkgPackageManager::OrderResult
pkgPackageManager::OrderInstall()
574 if (CreateOrderList() == false)
580 clog
<< "Begining to order" << endl
;
582 if (List
->OrderUnpack(FileNames
) == false)
584 _error
->Error("Internal ordering error");
589 clog
<< "Done ordering" << endl
;
591 bool DoneSomething
= false;
592 for (pkgOrderList::iterator I
= List
->begin(); I
!= List
->end(); I
++)
594 PkgIterator
Pkg(Cache
,*I
);
596 if (List
->IsNow(Pkg
) == false)
599 clog
<< "Skipping already done " << Pkg
.Name() << endl
;
603 if (List
->IsMissing(Pkg
) == true)
606 clog
<< "Sequence completed at " << Pkg
.Name() << endl
;
607 if (DoneSomething
== false)
609 _error
->Error("Internal Error, ordering was unable to handle the media swap");
616 if (Cache
[Pkg
].Keep() == true &&
617 Pkg
.State() == pkgCache::PkgIterator::NeedsNothing
&&
618 (Cache
[Pkg
].iFlags
& pkgDepCache::ReInstall
) != pkgDepCache::ReInstall
)
620 _error
->Error("Internal Error, trying to manipulate a kept package (%s)",Pkg
.Name());
624 // Perform a delete or an install
625 if (Cache
[Pkg
].Delete() == true)
627 if (SmartRemove(Pkg
) == false)
631 if (SmartUnPack(Pkg
) == false)
633 DoneSomething
= true;
636 // Final run through the configure phase
637 if (ConfigureAll() == false)
641 for (pkgOrderList::iterator I
= List
->begin(); I
!= List
->end(); I
++)
643 if (List
->IsFlag(*I
,pkgOrderList::Configured
) == false)
645 _error
->Error("Internal error, packages left unconfigured. %s",
646 PkgIterator(Cache
,*I
).Name());
654 // PM::DoInstallPostFork - Does install part that happens after the fork /*{{{*/
655 // ---------------------------------------------------------------------
656 pkgPackageManager::OrderResult
657 pkgPackageManager::DoInstallPostFork(int statusFd
)
660 // FIXME: use SetCloseExec here once it taught about throwing
661 // exceptions instead of doing _exit(100) on failure
662 fcntl(statusFd
,F_SETFD
,FD_CLOEXEC
);
663 bool goResult
= Go(statusFd
);
664 if(goResult
== false)
670 // PM::DoInstall - Does the installation /*{{{*/
671 // ---------------------------------------------------------------------
672 /* This uses the filenames in FileNames and the information in the
673 DepCache to perform the installation of packages.*/
674 pkgPackageManager::OrderResult
pkgPackageManager::DoInstall(int statusFd
)
676 if(DoInstallPreFork() == Failed
)
679 return DoInstallPostFork(statusFd
);