]>
git.saurik.com Git - apt.git/blob - apt-pkg/packagemanager.cc
1 // -*- mode: cpp; mode: fold -*-
3 // $Id: packagemanager.cc,v 1.26 2001/02/20 07:03:17 jgg 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>
33 // PM::PackageManager - Constructor /*{{{*/
34 // ---------------------------------------------------------------------
36 pkgPackageManager::pkgPackageManager(pkgDepCache
*pCache
) : Cache(*pCache
)
38 FileNames
= new string
[Cache
.Head().PackageCount
];
40 Debug
= _config
->FindB("Debug::pkgPackageManager",false);
43 // PM::PackageManager - Destructor /*{{{*/
44 // ---------------------------------------------------------------------
46 pkgPackageManager::~pkgPackageManager()
52 // PM::GetArchives - Queue the archives for download /*{{{*/
53 // ---------------------------------------------------------------------
55 bool pkgPackageManager::GetArchives(pkgAcquire
*Owner
,pkgSourceList
*Sources
,
58 if (CreateOrderList() == false)
61 if (List
->OrderUnpack() == false)
62 return _error
->Error("Internal ordering error");
64 for (pkgOrderList::iterator I
= List
->begin(); I
!= List
->end(); I
++)
66 PkgIterator
Pkg(Cache
,*I
);
67 FileNames
[Pkg
->ID
] = string();
69 // Skip packages to erase
70 if (Cache
[Pkg
].Delete() == true)
73 // Skip Packages that need configure only.
74 if (Pkg
.State() == pkgCache::PkgIterator::NeedsConfigure
&&
75 Cache
[Pkg
].Keep() == true)
78 // Skip already processed packages
79 if (List
->IsNow(Pkg
) == false)
82 new pkgAcqArchive(Owner
,Sources
,Recs
,Cache
[Pkg
].InstVerIter(Cache
),
89 // PM::FixMissing - Keep all missing packages /*{{{*/
90 // ---------------------------------------------------------------------
91 /* This is called to correct the installation when packages could not
93 bool pkgPackageManager::FixMissing()
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
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;
121 // PM::CreateOrderList - Create the ordering class /*{{{*/
122 // ---------------------------------------------------------------------
123 /* This populates the ordering list with all the packages that are
125 bool pkgPackageManager::CreateOrderList()
131 List
= new pkgOrderList(&Cache
);
133 bool NoImmConfigure
= _config
->FindB("APT::Immediate-Configure",false);
135 // Generate the list of affected packages and sort it
136 for (PkgIterator I
= Cache
.PkgBegin(); I
.end() == false; I
++)
138 // Mark the package and its dependends for immediate configuration
139 if (((I
->Flags
& pkgCache::Flag::Essential
) == pkgCache::Flag::Essential
||
140 (I
->Flags
& pkgCache::Flag::Important
) == pkgCache::Flag::Important
) &&
141 NoImmConfigure
== false)
143 List
->Flag(I
,pkgOrderList::Immediate
);
145 // Look for other packages to make immediate configurea
146 if (Cache
[I
].InstallVer
!= 0)
147 for (DepIterator D
= Cache
[I
].InstVerIter(Cache
).DependsList();
148 D
.end() == false; D
++)
149 if (D
->Type
== pkgCache::Dep::Depends
|| D
->Type
== pkgCache::Dep::PreDepends
)
150 List
->Flag(D
.TargetPkg(),pkgOrderList::Immediate
);
152 // And again with the current version.
153 if (I
->CurrentVer
!= 0)
154 for (DepIterator D
= I
.CurrentVer().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
);
161 if ((Cache
[I
].Keep() == true ||
162 Cache
[I
].InstVerIter(Cache
) == I
.CurrentVer()) &&
163 I
.State() == pkgCache::PkgIterator::NeedsNothing
&&
164 (Cache
[I
].iFlags
& pkgDepCache::ReInstall
) != pkgDepCache::ReInstall
&&
165 (I
.Purge() != false || Cache
[I
].Mode
!= pkgDepCache::ModeDelete
||
166 (Cache
[I
].iFlags
& pkgDepCache::Purge
) != pkgDepCache::Purge
))
169 // Append it to the list
176 // PM::DepAlwaysTrue - Returns true if this dep is irrelevent /*{{{*/
177 // ---------------------------------------------------------------------
178 /* The restriction on provides is to eliminate the case when provides
179 are transitioning between valid states [ie exim to smail] */
180 bool pkgPackageManager::DepAlwaysTrue(DepIterator D
)
182 if (D
.TargetPkg()->ProvidesList
!= 0)
185 if ((Cache
[D
] & pkgDepCache::DepInstall
) != 0 &&
186 (Cache
[D
] & pkgDepCache::DepNow
) != 0)
191 // PM::CheckRConflicts - Look for reverse conflicts /*{{{*/
192 // ---------------------------------------------------------------------
193 /* This looks over the reverses for a conflicts line that needs early
195 bool pkgPackageManager::CheckRConflicts(PkgIterator Pkg
,DepIterator D
,
198 for (;D
.end() == false; D
++)
200 if (D
->Type
!= pkgCache::Dep::Conflicts
&&
201 D
->Type
!= pkgCache::Dep::Obsoletes
)
204 // The package hasnt been changed
205 if (List
->IsNow(Pkg
) == false)
208 // Ignore self conflicts, ignore conflicts from irrelevent versions
209 if (D
.ParentPkg() == Pkg
|| D
.ParentVer() != D
.ParentPkg().CurrentVer())
212 if (Cache
.VS().CheckDep(Ver
,D
->CompareOp
,D
.TargetVer()) == false)
215 if (EarlyRemove(D
.ParentPkg()) == false)
216 return _error
->Error("Reverse conflicts early remove for package '%s' failed",
222 // PM::ConfigureAll - Run the all out configuration /*{{{*/
223 // ---------------------------------------------------------------------
224 /* This configures every package. It is assumed they are all unpacked and
225 that the final configuration is valid. */
226 bool pkgPackageManager::ConfigureAll()
228 pkgOrderList
OList(&Cache
);
230 // Populate the order list
231 for (pkgOrderList::iterator I
= List
->begin(); I
!= List
->end(); I
++)
232 if (List
->IsFlag(pkgCache::PkgIterator(Cache
,*I
),
233 pkgOrderList::UnPacked
) == true)
236 if (OList
.OrderConfigure() == false)
239 // Perform the configuring
240 for (pkgOrderList::iterator I
= OList
.begin(); I
!= OList
.end(); I
++)
242 PkgIterator
Pkg(Cache
,*I
);
244 if (Configure(Pkg
) == false)
247 List
->Flag(Pkg
,pkgOrderList::Configured
,pkgOrderList::States
);
253 // PM::SmartConfigure - Perform immediate configuration of the pkg /*{{{*/
254 // ---------------------------------------------------------------------
255 /* This routine scheduals the configuration of the given package and all
256 of it's dependents. */
257 bool pkgPackageManager::SmartConfigure(PkgIterator Pkg
)
259 pkgOrderList
OList(&Cache
);
261 if (DepAdd(OList
,Pkg
) == false)
264 if (OList
.OrderConfigure() == false)
267 // Perform the configuring
268 for (pkgOrderList::iterator I
= OList
.begin(); I
!= OList
.end(); I
++)
270 PkgIterator
Pkg(Cache
,*I
);
272 if (Configure(Pkg
) == false)
275 List
->Flag(Pkg
,pkgOrderList::Configured
,pkgOrderList::States
);
279 if (List
->IsFlag(Pkg
,pkgOrderList::Configured
) == false)
280 return _error
->Error("Internal error, could not immediate configure %s",Pkg
.Name());
285 // PM::DepAdd - Add all dependents to the oder list /*{{{*/
286 // ---------------------------------------------------------------------
287 /* This recursively adds all dependents to the order list */
288 bool pkgPackageManager::DepAdd(pkgOrderList
&OList
,PkgIterator Pkg
,int Depth
)
290 if (OList
.IsFlag(Pkg
,pkgOrderList::Added
) == true)
292 if (List
->IsFlag(Pkg
,pkgOrderList::Configured
) == true)
294 if (List
->IsFlag(Pkg
,pkgOrderList::UnPacked
) == false)
297 // Put the package on the list
298 OList
.push_back(Pkg
);
299 OList
.Flag(Pkg
,pkgOrderList::Added
);
302 // Check the dependencies to see if they are all satisfied.
304 for (DepIterator D
= Cache
[Pkg
].InstVerIter(Cache
).DependsList(); D
.end() == false;)
306 if (D
->Type
!= pkgCache::Dep::Depends
&& D
->Type
!= pkgCache::Dep::PreDepends
)
314 for (bool LastOR
= true; D
.end() == false && LastOR
== true; D
++)
316 LastOR
= (D
->CompareOp
& pkgCache::Dep::Or
) == pkgCache::Dep::Or
;
321 SPtrArray
<Version
*> VList
= D
.AllTargets();
322 for (Version
**I
= VList
; *I
!= 0 && Bad
== true; I
++)
324 VerIterator
Ver(Cache
,*I
);
325 PkgIterator Pkg
= Ver
.ParentPkg();
327 // See if the current version is ok
328 if (Pkg
.CurrentVer() == Ver
&& List
->IsNow(Pkg
) == true &&
329 Pkg
.State() == PkgIterator::NeedsNothing
)
335 // Not the install version
336 if (Cache
[Pkg
].InstallVer
!= *I
||
337 (Cache
[Pkg
].Keep() == true && Pkg
.State() == PkgIterator::NeedsNothing
))
340 if (List
->IsFlag(Pkg
,pkgOrderList::UnPacked
) == true)
341 Bad
= !DepAdd(OList
,Pkg
,Depth
);
342 if (List
->IsFlag(Pkg
,pkgOrderList::Configured
) == true)
349 OList
.Flag(Pkg
,0,pkgOrderList::Added
);
360 // PM::EarlyRemove - Perform removal of packages before their time /*{{{*/
361 // ---------------------------------------------------------------------
362 /* This is called to deal with conflicts arising from unpacking */
363 bool pkgPackageManager::EarlyRemove(PkgIterator Pkg
)
365 if (List
->IsNow(Pkg
) == false)
368 // Already removed it
369 if (List
->IsFlag(Pkg
,pkgOrderList::Removed
) == true)
372 // Woops, it will not be re-installed!
373 if (List
->IsFlag(Pkg
,pkgOrderList::InList
) == false)
376 // Essential packages get special treatment
377 bool IsEssential
= false;
378 if ((Pkg
->Flags
& pkgCache::Flag::Essential
) != 0)
381 /* Check for packages that are the dependents of essential packages and
383 if (Pkg
->CurrentVer
!= 0)
385 for (DepIterator D
= Pkg
.RevDependsList(); D
.end() == false &&
386 IsEssential
== false; D
++)
387 if (D
->Type
== pkgCache::Dep::Depends
|| D
->Type
== pkgCache::Dep::PreDepends
)
388 if ((D
.ParentPkg()->Flags
& pkgCache::Flag::Essential
) != 0)
392 if (IsEssential
== true)
394 if (_config
->FindB("APT::Force-LoopBreak",false) == false)
395 return _error
->Error(_("This installation run will require temporarily "
396 "removing the essential package %s due to a "
397 "Conflicts/Pre-Depends loop. This is often bad, "
398 "but if you really want to do it, activate the "
399 "APT::Force-LoopBreak option."),Pkg
.Name());
402 bool Res
= SmartRemove(Pkg
);
403 if (Cache
[Pkg
].Delete() == false)
404 List
->Flag(Pkg
,pkgOrderList::Removed
,pkgOrderList::States
);
409 // PM::SmartRemove - Removal Helper /*{{{*/
410 // ---------------------------------------------------------------------
412 bool pkgPackageManager::SmartRemove(PkgIterator Pkg
)
414 if (List
->IsNow(Pkg
) == false)
417 List
->Flag(Pkg
,pkgOrderList::Configured
,pkgOrderList::States
);
418 return Remove(Pkg
,(Cache
[Pkg
].iFlags
& pkgDepCache::Purge
) == pkgDepCache::Purge
);
421 // PM::SmartUnPack - Install helper /*{{{*/
422 // ---------------------------------------------------------------------
423 /* This performs the task of handling pre-depends. */
424 bool pkgPackageManager::SmartUnPack(PkgIterator Pkg
)
426 // Check if it is already unpacked
427 if (Pkg
.State() == pkgCache::PkgIterator::NeedsConfigure
&&
428 Cache
[Pkg
].Keep() == true)
430 List
->Flag(Pkg
,pkgOrderList::UnPacked
,pkgOrderList::States
);
431 if (List
->IsFlag(Pkg
,pkgOrderList::Immediate
) == true)
432 if (SmartConfigure(Pkg
) == false)
433 return _error
->Error("Internal Error, Could not perform immediate configuration (1) on %s",Pkg
.Name());
437 /* See if this packages install version has any predependencies
438 that are not met by 'now' packages. */
439 for (DepIterator D
= Cache
[Pkg
].InstVerIter(Cache
).DependsList();
442 // Compute a single dependency element (glob or)
443 pkgCache::DepIterator Start
;
444 pkgCache::DepIterator End
;
447 while (End
->Type
== pkgCache::Dep::PreDepends
)
449 // Look for possible ok targets.
450 SPtrArray
<Version
*> VList
= Start
.AllTargets();
452 for (Version
**I
= VList
; *I
!= 0 && Bad
== true; I
++)
454 VerIterator
Ver(Cache
,*I
);
455 PkgIterator Pkg
= Ver
.ParentPkg();
457 // See if the current version is ok
458 if (Pkg
.CurrentVer() == Ver
&& List
->IsNow(Pkg
) == true &&
459 Pkg
.State() == PkgIterator::NeedsNothing
)
466 // Look for something that could be configured.
467 for (Version
**I
= VList
; *I
!= 0 && Bad
== true; I
++)
469 VerIterator
Ver(Cache
,*I
);
470 PkgIterator Pkg
= Ver
.ParentPkg();
472 // Not the install version
473 if (Cache
[Pkg
].InstallVer
!= *I
||
474 (Cache
[Pkg
].Keep() == true && Pkg
.State() == PkgIterator::NeedsNothing
))
477 Bad
= !SmartConfigure(Pkg
);
480 /* If this or element did not match then continue on to the
481 next or element until a matching element is found*/
485 return _error
->Error("Internal Error, Couldn't configure a pre-depend");
492 if (End
->Type
== pkgCache::Dep::Conflicts
||
493 End
->Type
== pkgCache::Dep::Obsoletes
)
495 /* Look for conflicts. Two packages that are both in the install
496 state cannot conflict so we don't check.. */
497 SPtrArray
<Version
*> VList
= End
.AllTargets();
498 for (Version
**I
= VList
; *I
!= 0; I
++)
500 VerIterator
Ver(Cache
,*I
);
501 PkgIterator Pkg
= Ver
.ParentPkg();
503 // See if the current version is conflicting
504 if (Pkg
.CurrentVer() == Ver
&& List
->IsNow(Pkg
) == true)
506 if (EarlyRemove(Pkg
) == false)
507 return _error
->Error("Internal Error, Could not early remove %s",Pkg
.Name());
513 // Check for reverse conflicts.
514 if (CheckRConflicts(Pkg
,Pkg
.RevDependsList(),
515 Cache
[Pkg
].InstVerIter(Cache
).VerStr()) == false)
518 for (PrvIterator P
= Cache
[Pkg
].InstVerIter(Cache
).ProvidesList();
519 P
.end() == false; P
++)
520 CheckRConflicts(Pkg
,P
.ParentPkg().RevDependsList(),P
.ProvideVersion());
522 if (Install(Pkg
,FileNames
[Pkg
->ID
]) == false)
525 List
->Flag(Pkg
,pkgOrderList::UnPacked
,pkgOrderList::States
);
527 // Perform immedate configuration of the package.
528 if (List
->IsFlag(Pkg
,pkgOrderList::Immediate
) == true)
529 if (SmartConfigure(Pkg
) == false)
530 return _error
->Error("Internal Error, Could not perform immediate configuration (2) on %s",Pkg
.Name());
535 // PM::OrderInstall - Installation ordering routine /*{{{*/
536 // ---------------------------------------------------------------------
538 pkgPackageManager::OrderResult
pkgPackageManager::OrderInstall()
540 if (CreateOrderList() == false)
546 clog
<< "Begining to order" << endl
;
548 if (List
->OrderUnpack(FileNames
) == false)
550 _error
->Error("Internal ordering error");
555 clog
<< "Done ordering" << endl
;
557 bool DoneSomething
= false;
558 for (pkgOrderList::iterator I
= List
->begin(); I
!= List
->end(); I
++)
560 PkgIterator
Pkg(Cache
,*I
);
562 if (List
->IsNow(Pkg
) == false)
565 clog
<< "Skipping already done " << Pkg
.Name() << endl
;
569 if (List
->IsMissing(Pkg
) == true)
572 clog
<< "Sequence completed at " << Pkg
.Name() << endl
;
573 if (DoneSomething
== false)
575 _error
->Error("Internal Error, ordering was unable to handle the media swap");
582 if (Cache
[Pkg
].Keep() == true &&
583 Pkg
.State() == pkgCache::PkgIterator::NeedsNothing
&&
584 (Cache
[Pkg
].iFlags
& pkgDepCache::ReInstall
) != pkgDepCache::ReInstall
)
586 _error
->Error("Internal Error, trying to manipulate a kept package");
590 // Perform a delete or an install
591 if (Cache
[Pkg
].Delete() == true)
593 if (SmartRemove(Pkg
) == false)
597 if (SmartUnPack(Pkg
) == false)
599 DoneSomething
= true;
602 // Final run through the configure phase
603 if (ConfigureAll() == false)
607 for (pkgOrderList::iterator I
= List
->begin(); I
!= List
->end(); I
++)
609 if (List
->IsFlag(*I
,pkgOrderList::Configured
) == false)
611 _error
->Error("Internal error, packages left unconfigured. %s",
612 PkgIterator(Cache
,*I
).Name());
620 // PM::DoInstall - Does the installation /*{{{*/
621 // ---------------------------------------------------------------------
622 /* This uses the filenames in FileNames and the information in the
623 DepCache to perform the installation of packages.*/
624 pkgPackageManager::OrderResult
pkgPackageManager::DoInstall()
626 OrderResult Res
= OrderInstall();