]>
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 /*{{{*/
17 #pragma implementation "apt-pkg/packagemanager.h"
20 #include <apt-pkg/packagemanager.h>
21 #include <apt-pkg/orderlist.h>
22 #include <apt-pkg/depcache.h>
23 #include <apt-pkg/error.h>
24 #include <apt-pkg/version.h>
25 #include <apt-pkg/acquire-item.h>
26 #include <apt-pkg/algorithms.h>
27 #include <apt-pkg/configuration.h>
28 #include <apt-pkg/sptr.h>
36 // PM::PackageManager - Constructor /*{{{*/
37 // ---------------------------------------------------------------------
39 pkgPackageManager::pkgPackageManager(pkgDepCache
*pCache
) : Cache(*pCache
)
41 FileNames
= new string
[Cache
.Head().PackageCount
];
43 Debug
= _config
->FindB("Debug::pkgPackageManager",false);
46 // PM::PackageManager - Destructor /*{{{*/
47 // ---------------------------------------------------------------------
49 pkgPackageManager::~pkgPackageManager()
55 // PM::GetArchives - Queue the archives for download /*{{{*/
56 // ---------------------------------------------------------------------
58 bool pkgPackageManager::GetArchives(pkgAcquire
*Owner
,pkgSourceList
*Sources
,
61 if (CreateOrderList() == false)
64 if (List
->OrderUnpack() == false)
65 return _error
->Error("Internal ordering error");
67 for (pkgOrderList::iterator I
= List
->begin(); I
!= List
->end(); I
++)
69 PkgIterator
Pkg(Cache
,*I
);
70 FileNames
[Pkg
->ID
] = string();
72 // Skip packages to erase
73 if (Cache
[Pkg
].Delete() == true)
76 // Skip Packages that need configure only.
77 if (Pkg
.State() == pkgCache::PkgIterator::NeedsConfigure
&&
78 Cache
[Pkg
].Keep() == true)
81 // Skip already processed packages
82 if (List
->IsNow(Pkg
) == false)
85 new pkgAcqArchive(Owner
,Sources
,Recs
,Cache
[Pkg
].InstVerIter(Cache
),
92 // PM::FixMissing - Keep all missing packages /*{{{*/
93 // ---------------------------------------------------------------------
94 /* This is called to correct the installation when packages could not
96 bool pkgPackageManager::FixMissing()
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
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;
124 // PM::CreateOrderList - Create the ordering class /*{{{*/
125 // ---------------------------------------------------------------------
126 /* This populates the ordering list with all the packages that are
128 bool pkgPackageManager::CreateOrderList()
134 List
= new pkgOrderList(&Cache
);
136 bool NoImmConfigure
= !_config
->FindB("APT::Immediate-Configure",true);
138 // Generate the list of affected packages and sort it
139 for (PkgIterator I
= Cache
.PkgBegin(); I
.end() == false; I
++)
141 // Ignore no-version packages
142 if (I
->VersionList
== 0)
145 // Mark the package and its dependends for immediate configuration
146 if (((I
->Flags
& pkgCache::Flag::Essential
) == pkgCache::Flag::Essential
||
147 (I
->Flags
& pkgCache::Flag::Important
) == pkgCache::Flag::Important
) &&
148 NoImmConfigure
== false)
150 List
->Flag(I
,pkgOrderList::Immediate
);
152 // Look for other packages to make immediate configurea
153 if (Cache
[I
].InstallVer
!= 0)
154 for (DepIterator D
= Cache
[I
].InstVerIter(Cache
).DependsList();
155 D
.end() == false; D
++)
156 if (D
->Type
== pkgCache::Dep::Depends
|| D
->Type
== pkgCache::Dep::PreDepends
)
157 List
->Flag(D
.TargetPkg(),pkgOrderList::Immediate
);
159 // And again with the current version.
160 if (I
->CurrentVer
!= 0)
161 for (DepIterator D
= I
.CurrentVer().DependsList();
162 D
.end() == false; D
++)
163 if (D
->Type
== pkgCache::Dep::Depends
|| D
->Type
== pkgCache::Dep::PreDepends
)
164 List
->Flag(D
.TargetPkg(),pkgOrderList::Immediate
);
168 if ((Cache
[I
].Keep() == true ||
169 Cache
[I
].InstVerIter(Cache
) == I
.CurrentVer()) &&
170 I
.State() == pkgCache::PkgIterator::NeedsNothing
&&
171 (Cache
[I
].iFlags
& pkgDepCache::ReInstall
) != pkgDepCache::ReInstall
&&
172 (I
.Purge() != false || Cache
[I
].Mode
!= pkgDepCache::ModeDelete
||
173 (Cache
[I
].iFlags
& pkgDepCache::Purge
) != pkgDepCache::Purge
))
176 // Append it to the list
183 // PM::DepAlwaysTrue - Returns true if this dep is irrelevent /*{{{*/
184 // ---------------------------------------------------------------------
185 /* The restriction on provides is to eliminate the case when provides
186 are transitioning between valid states [ie exim to smail] */
187 bool pkgPackageManager::DepAlwaysTrue(DepIterator D
)
189 if (D
.TargetPkg()->ProvidesList
!= 0)
192 if ((Cache
[D
] & pkgDepCache::DepInstall
) != 0 &&
193 (Cache
[D
] & pkgDepCache::DepNow
) != 0)
198 // PM::CheckRConflicts - Look for reverse conflicts /*{{{*/
199 // ---------------------------------------------------------------------
200 /* This looks over the reverses for a conflicts line that needs early
202 bool pkgPackageManager::CheckRConflicts(PkgIterator Pkg
,DepIterator D
,
205 for (;D
.end() == false; D
++)
207 if (D
->Type
!= pkgCache::Dep::Conflicts
&&
208 D
->Type
!= pkgCache::Dep::Obsoletes
)
211 // The package hasnt been changed
212 if (List
->IsNow(Pkg
) == false)
215 // Ignore self conflicts, ignore conflicts from irrelevent versions
216 if (D
.ParentPkg() == Pkg
|| D
.ParentVer() != D
.ParentPkg().CurrentVer())
219 if (Cache
.VS().CheckDep(Ver
,D
->CompareOp
,D
.TargetVer()) == false)
222 if (EarlyRemove(D
.ParentPkg()) == false)
223 return _error
->Error("Reverse conflicts early remove for package '%s' failed",
229 // PM::ConfigureAll - Run the all out configuration /*{{{*/
230 // ---------------------------------------------------------------------
231 /* This configures every package. It is assumed they are all unpacked and
232 that the final configuration is valid. */
233 bool pkgPackageManager::ConfigureAll()
235 pkgOrderList
OList(&Cache
);
237 // Populate the order list
238 for (pkgOrderList::iterator I
= List
->begin(); I
!= List
->end(); I
++)
239 if (List
->IsFlag(pkgCache::PkgIterator(Cache
,*I
),
240 pkgOrderList::UnPacked
) == true)
243 if (OList
.OrderConfigure() == false)
246 // Perform the configuring
247 for (pkgOrderList::iterator I
= OList
.begin(); I
!= OList
.end(); I
++)
249 PkgIterator
Pkg(Cache
,*I
);
251 if (Configure(Pkg
) == false)
254 List
->Flag(Pkg
,pkgOrderList::Configured
,pkgOrderList::States
);
260 // PM::SmartConfigure - Perform immediate configuration of the pkg /*{{{*/
261 // ---------------------------------------------------------------------
262 /* This routine scheduals the configuration of the given package and all
263 of it's dependents. */
264 bool pkgPackageManager::SmartConfigure(PkgIterator Pkg
)
266 pkgOrderList
OList(&Cache
);
268 if (DepAdd(OList
,Pkg
) == false)
271 if (OList
.OrderConfigure() == false)
274 // Perform the configuring
275 for (pkgOrderList::iterator I
= OList
.begin(); I
!= OList
.end(); I
++)
277 PkgIterator
Pkg(Cache
,*I
);
279 if (Configure(Pkg
) == false)
282 List
->Flag(Pkg
,pkgOrderList::Configured
,pkgOrderList::States
);
286 if (List
->IsFlag(Pkg
,pkgOrderList::Configured
) == false)
287 return _error
->Error("Internal error, could not immediate configure %s",Pkg
.Name());
292 // PM::DepAdd - Add all dependents to the oder list /*{{{*/
293 // ---------------------------------------------------------------------
294 /* This recursively adds all dependents to the order list */
295 bool pkgPackageManager::DepAdd(pkgOrderList
&OList
,PkgIterator Pkg
,int Depth
)
297 if (OList
.IsFlag(Pkg
,pkgOrderList::Added
) == true)
299 if (List
->IsFlag(Pkg
,pkgOrderList::Configured
) == true)
301 if (List
->IsFlag(Pkg
,pkgOrderList::UnPacked
) == false)
304 // Put the package on the list
305 OList
.push_back(Pkg
);
306 OList
.Flag(Pkg
,pkgOrderList::Added
);
309 // Check the dependencies to see if they are all satisfied.
311 for (DepIterator D
= Cache
[Pkg
].InstVerIter(Cache
).DependsList(); D
.end() == false;)
313 if (D
->Type
!= pkgCache::Dep::Depends
&& D
->Type
!= pkgCache::Dep::PreDepends
)
321 for (bool LastOR
= true; D
.end() == false && LastOR
== true; D
++)
323 LastOR
= (D
->CompareOp
& pkgCache::Dep::Or
) == pkgCache::Dep::Or
;
328 SPtrArray
<Version
*> VList
= D
.AllTargets();
329 for (Version
**I
= VList
; *I
!= 0 && Bad
== true; I
++)
331 VerIterator
Ver(Cache
,*I
);
332 PkgIterator Pkg
= Ver
.ParentPkg();
334 // See if the current version is ok
335 if (Pkg
.CurrentVer() == Ver
&& List
->IsNow(Pkg
) == true &&
336 Pkg
.State() == PkgIterator::NeedsNothing
)
342 // Not the install version
343 if (Cache
[Pkg
].InstallVer
!= *I
||
344 (Cache
[Pkg
].Keep() == true && Pkg
.State() == PkgIterator::NeedsNothing
))
347 if (List
->IsFlag(Pkg
,pkgOrderList::UnPacked
) == true)
348 Bad
= !DepAdd(OList
,Pkg
,Depth
);
349 if (List
->IsFlag(Pkg
,pkgOrderList::Configured
) == true)
356 OList
.Flag(Pkg
,0,pkgOrderList::Added
);
367 // PM::EarlyRemove - Perform removal of packages before their time /*{{{*/
368 // ---------------------------------------------------------------------
369 /* This is called to deal with conflicts arising from unpacking */
370 bool pkgPackageManager::EarlyRemove(PkgIterator Pkg
)
372 if (List
->IsNow(Pkg
) == false)
375 // Already removed it
376 if (List
->IsFlag(Pkg
,pkgOrderList::Removed
) == true)
379 // Woops, it will not be re-installed!
380 if (List
->IsFlag(Pkg
,pkgOrderList::InList
) == false)
383 // Essential packages get special treatment
384 bool IsEssential
= false;
385 if ((Pkg
->Flags
& pkgCache::Flag::Essential
) != 0)
388 /* Check for packages that are the dependents of essential packages and
390 if (Pkg
->CurrentVer
!= 0)
392 for (DepIterator D
= Pkg
.RevDependsList(); D
.end() == false &&
393 IsEssential
== false; D
++)
394 if (D
->Type
== pkgCache::Dep::Depends
|| D
->Type
== pkgCache::Dep::PreDepends
)
395 if ((D
.ParentPkg()->Flags
& pkgCache::Flag::Essential
) != 0)
399 if (IsEssential
== true)
401 if (_config
->FindB("APT::Force-LoopBreak",false) == false)
402 return _error
->Error(_("This installation run will require temporarily "
403 "removing the essential package %s due to a "
404 "Conflicts/Pre-Depends loop. This is often bad, "
405 "but if you really want to do it, activate the "
406 "APT::Force-LoopBreak option."),Pkg
.Name());
409 bool Res
= SmartRemove(Pkg
);
410 if (Cache
[Pkg
].Delete() == false)
411 List
->Flag(Pkg
,pkgOrderList::Removed
,pkgOrderList::States
);
416 // PM::SmartRemove - Removal Helper /*{{{*/
417 // ---------------------------------------------------------------------
419 bool pkgPackageManager::SmartRemove(PkgIterator Pkg
)
421 if (List
->IsNow(Pkg
) == false)
424 List
->Flag(Pkg
,pkgOrderList::Configured
,pkgOrderList::States
);
425 return Remove(Pkg
,(Cache
[Pkg
].iFlags
& pkgDepCache::Purge
) == pkgDepCache::Purge
);
428 // PM::SmartUnPack - Install helper /*{{{*/
429 // ---------------------------------------------------------------------
430 /* This performs the task of handling pre-depends. */
431 bool pkgPackageManager::SmartUnPack(PkgIterator Pkg
)
433 // Check if it is already unpacked
434 if (Pkg
.State() == pkgCache::PkgIterator::NeedsConfigure
&&
435 Cache
[Pkg
].Keep() == true)
437 List
->Flag(Pkg
,pkgOrderList::UnPacked
,pkgOrderList::States
);
438 if (List
->IsFlag(Pkg
,pkgOrderList::Immediate
) == true)
439 if (SmartConfigure(Pkg
) == false)
440 return _error
->Error("Internal Error, Could not perform immediate configuration (1) on %s",Pkg
.Name());
444 /* See if this packages install version has any predependencies
445 that are not met by 'now' packages. */
446 for (DepIterator D
= Cache
[Pkg
].InstVerIter(Cache
).DependsList();
449 // Compute a single dependency element (glob or)
450 pkgCache::DepIterator Start
;
451 pkgCache::DepIterator End
;
454 while (End
->Type
== pkgCache::Dep::PreDepends
)
456 // Look for possible ok targets.
457 SPtrArray
<Version
*> VList
= Start
.AllTargets();
459 for (Version
**I
= VList
; *I
!= 0 && Bad
== true; I
++)
461 VerIterator
Ver(Cache
,*I
);
462 PkgIterator Pkg
= Ver
.ParentPkg();
464 // See if the current version is ok
465 if (Pkg
.CurrentVer() == Ver
&& List
->IsNow(Pkg
) == true &&
466 Pkg
.State() == PkgIterator::NeedsNothing
)
473 // Look for something that could be configured.
474 for (Version
**I
= VList
; *I
!= 0 && Bad
== true; I
++)
476 VerIterator
Ver(Cache
,*I
);
477 PkgIterator Pkg
= Ver
.ParentPkg();
479 // Not the install version
480 if (Cache
[Pkg
].InstallVer
!= *I
||
481 (Cache
[Pkg
].Keep() == true && Pkg
.State() == PkgIterator::NeedsNothing
))
484 Bad
= !SmartConfigure(Pkg
);
487 /* If this or element did not match then continue on to the
488 next or element until a matching element is found */
491 // This triggers if someone make a pre-depends/depend loop.
493 return _error
->Error("Couldn't configure pre-depend %s for %s, "
494 "probably a dependency cycle.",
495 End
.TargetPkg().Name(),Pkg
.Name());
502 if (End
->Type
== pkgCache::Dep::Conflicts
||
503 End
->Type
== pkgCache::Dep::Obsoletes
)
505 /* Look for conflicts. Two packages that are both in the install
506 state cannot conflict so we don't check.. */
507 SPtrArray
<Version
*> VList
= End
.AllTargets();
508 for (Version
**I
= VList
; *I
!= 0; I
++)
510 VerIterator
Ver(Cache
,*I
);
511 PkgIterator Pkg
= Ver
.ParentPkg();
513 // See if the current version is conflicting
514 if (Pkg
.CurrentVer() == Ver
&& List
->IsNow(Pkg
) == true)
516 if (EarlyRemove(Pkg
) == false)
517 return _error
->Error("Internal Error, Could not early remove %s",Pkg
.Name());
523 // Check for reverse conflicts.
524 if (CheckRConflicts(Pkg
,Pkg
.RevDependsList(),
525 Cache
[Pkg
].InstVerIter(Cache
).VerStr()) == false)
528 for (PrvIterator P
= Cache
[Pkg
].InstVerIter(Cache
).ProvidesList();
529 P
.end() == false; P
++)
530 CheckRConflicts(Pkg
,P
.ParentPkg().RevDependsList(),P
.ProvideVersion());
532 if (Install(Pkg
,FileNames
[Pkg
->ID
]) == false)
535 List
->Flag(Pkg
,pkgOrderList::UnPacked
,pkgOrderList::States
);
537 // Perform immedate configuration of the package.
538 if (List
->IsFlag(Pkg
,pkgOrderList::Immediate
) == true)
539 if (SmartConfigure(Pkg
) == false)
540 return _error
->Error("Internal Error, Could not perform immediate configuration (2) on %s",Pkg
.Name());
545 // PM::OrderInstall - Installation ordering routine /*{{{*/
546 // ---------------------------------------------------------------------
548 pkgPackageManager::OrderResult
pkgPackageManager::OrderInstall()
550 if (CreateOrderList() == false)
556 clog
<< "Begining to order" << endl
;
558 if (List
->OrderUnpack(FileNames
) == false)
560 _error
->Error("Internal ordering error");
565 clog
<< "Done ordering" << endl
;
567 bool DoneSomething
= false;
568 for (pkgOrderList::iterator I
= List
->begin(); I
!= List
->end(); I
++)
570 PkgIterator
Pkg(Cache
,*I
);
572 if (List
->IsNow(Pkg
) == false)
575 clog
<< "Skipping already done " << Pkg
.Name() << endl
;
579 if (List
->IsMissing(Pkg
) == true)
582 clog
<< "Sequence completed at " << Pkg
.Name() << endl
;
583 if (DoneSomething
== false)
585 _error
->Error("Internal Error, ordering was unable to handle the media swap");
592 if (Cache
[Pkg
].Keep() == true &&
593 Pkg
.State() == pkgCache::PkgIterator::NeedsNothing
&&
594 (Cache
[Pkg
].iFlags
& pkgDepCache::ReInstall
) != pkgDepCache::ReInstall
)
596 _error
->Error("Internal Error, trying to manipulate a kept package");
600 // Perform a delete or an install
601 if (Cache
[Pkg
].Delete() == true)
603 if (SmartRemove(Pkg
) == false)
607 if (SmartUnPack(Pkg
) == false)
609 DoneSomething
= true;
612 // Final run through the configure phase
613 if (ConfigureAll() == false)
617 for (pkgOrderList::iterator I
= List
->begin(); I
!= List
->end(); I
++)
619 if (List
->IsFlag(*I
,pkgOrderList::Configured
) == false)
621 _error
->Error("Internal error, packages left unconfigured. %s",
622 PkgIterator(Cache
,*I
).Name());
630 // PM::DoInstall - Does the installation /*{{{*/
631 // ---------------------------------------------------------------------
632 /* This uses the filenames in FileNames and the information in the
633 DepCache to perform the installation of packages.*/
634 pkgPackageManager::OrderResult
pkgPackageManager::DoInstall()
636 OrderResult Res
= OrderInstall();