]>
git.saurik.com Git - apt.git/blob - apt-pkg/packagemanager.cc
1 // -*- mode: cpp; mode: fold -*-
3 // $Id: packagemanager.cc,v 1.22 1999/10/22 05:58:54 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"
19 #include <apt-pkg/packagemanager.h>
20 #include <apt-pkg/orderlist.h>
21 #include <apt-pkg/depcache.h>
22 #include <apt-pkg/error.h>
23 #include <apt-pkg/version.h>
24 #include <apt-pkg/acquire-item.h>
25 #include <apt-pkg/algorithms.h>
26 #include <apt-pkg/configuration.h>
29 // PM::PackageManager - Constructor /*{{{*/
30 // ---------------------------------------------------------------------
32 pkgPackageManager::pkgPackageManager(pkgDepCache
&Cache
) : Cache(Cache
)
34 FileNames
= new string
[Cache
.Head().PackageCount
];
36 Debug
= _config
->FindB("Debug::pkgPackageManager",false);
39 // PM::PackageManager - Destructor /*{{{*/
40 // ---------------------------------------------------------------------
42 pkgPackageManager::~pkgPackageManager()
48 // PM::GetArchives - Queue the archives for download /*{{{*/
49 // ---------------------------------------------------------------------
51 bool pkgPackageManager::GetArchives(pkgAcquire
*Owner
,pkgSourceList
*Sources
,
54 if (CreateOrderList() == false)
57 if (List
->OrderUnpack() == false)
58 return _error
->Error("Internal ordering error");
60 for (pkgOrderList::iterator I
= List
->begin(); I
!= List
->end(); I
++)
62 PkgIterator
Pkg(Cache
,*I
);
63 FileNames
[Pkg
->ID
] = string();
65 // Skip packages to erase
66 if (Cache
[Pkg
].Delete() == true)
69 // Skip Packages that need configure only.
70 if (Pkg
.State() == pkgCache::PkgIterator::NeedsConfigure
&&
71 Cache
[Pkg
].Keep() == true)
74 // Skip already processed packages
75 if (List
->IsNow(Pkg
) == false)
78 new pkgAcqArchive(Owner
,Sources
,Recs
,Cache
[Pkg
].InstVerIter(Cache
),
85 // PM::FixMissing - Keep all missing packages /*{{{*/
86 // ---------------------------------------------------------------------
87 /* This is called to correct the installation when packages could not
89 bool pkgPackageManager::FixMissing()
91 pkgProblemResolver
Resolve(Cache
);
92 List
->SetFileList(FileNames
);
95 for (PkgIterator I
= Cache
.PkgBegin(); I
.end() == false; I
++)
97 if (List
->IsMissing(I
) == false)
100 // Okay, this file is missing and we need it. Mark it for keep
105 // We have to empty the list otherwise it will not have the new changes
112 // Now downgrade everything that is broken
113 return Resolve
.ResolveByKeep() == true && Cache
.BrokenCount() == 0;
117 // PM::CreateOrderList - Create the ordering class /*{{{*/
118 // ---------------------------------------------------------------------
119 /* This populates the ordering list with all the packages that are
121 bool pkgPackageManager::CreateOrderList()
127 List
= new pkgOrderList(Cache
);
129 bool NoImmConfigure
= _config
->FindB("APT::Immediate-Configure",false);
131 // Generate the list of affected packages and sort it
132 for (PkgIterator I
= Cache
.PkgBegin(); I
.end() == false; I
++)
134 // Mark the package and its dependends for immediate configuration
135 if (((I
->Flags
& pkgCache::Flag::Essential
) == pkgCache::Flag::Essential
||
136 (I
->Flags
& pkgCache::Flag::Important
) == pkgCache::Flag::Important
) &&
137 NoImmConfigure
== false)
139 List
->Flag(I
,pkgOrderList::Immediate
);
141 // Look for other packages to make immediate configurea
142 if (Cache
[I
].InstallVer
!= 0)
143 for (DepIterator D
= Cache
[I
].InstVerIter(Cache
).DependsList();
144 D
.end() == false; D
++)
145 if (D
->Type
== pkgCache::Dep::Depends
|| D
->Type
== pkgCache::Dep::PreDepends
)
146 List
->Flag(D
.TargetPkg(),pkgOrderList::Immediate
);
148 // And again with the current version.
149 if (I
->CurrentVer
!= 0)
150 for (DepIterator D
= I
.CurrentVer().DependsList();
151 D
.end() == false; D
++)
152 if (D
->Type
== pkgCache::Dep::Depends
|| D
->Type
== pkgCache::Dep::PreDepends
)
153 List
->Flag(D
.TargetPkg(),pkgOrderList::Immediate
);
157 if ((Cache
[I
].Keep() == true ||
158 Cache
[I
].InstVerIter(Cache
) == I
.CurrentVer()) &&
159 I
.State() == pkgCache::PkgIterator::NeedsNothing
&&
160 (Cache
[I
].iFlags
& pkgDepCache::ReInstall
) != pkgDepCache::ReInstall
&&
161 (I
.Purge() != false || Cache
[I
].Mode
!= pkgDepCache::ModeDelete
||
162 (Cache
[I
].iFlags
& pkgDepCache::Purge
) != pkgDepCache::Purge
))
165 // Append it to the list
172 // PM::DepAlwaysTrue - Returns true if this dep is irrelevent /*{{{*/
173 // ---------------------------------------------------------------------
174 /* The restriction on provides is to eliminate the case when provides
175 are transitioning between valid states [ie exim to smail] */
176 bool pkgPackageManager::DepAlwaysTrue(DepIterator D
)
178 if (D
.TargetPkg()->ProvidesList
!= 0)
181 if ((Cache
[D
] & pkgDepCache::DepInstall
) != 0 &&
182 (Cache
[D
] & pkgDepCache::DepNow
) != 0)
187 // PM::CheckRConflicts - Look for reverse conflicts /*{{{*/
188 // ---------------------------------------------------------------------
189 /* This looks over the reverses for a conflicts line that needs early
191 bool pkgPackageManager::CheckRConflicts(PkgIterator Pkg
,DepIterator D
,
194 for (;D
.end() == false; D
++)
196 if (D
->Type
!= pkgCache::Dep::Conflicts
)
199 if (D
.ParentPkg() == Pkg
)
202 if (pkgCheckDep(D
.TargetVer(),Ver
,D
->CompareOp
) == false)
205 if (List
->IsNow(Pkg
) == false)
208 if (EarlyRemove(D
.ParentPkg()) == false)
214 // PM::ConfigureAll - Run the all out configuration /*{{{*/
215 // ---------------------------------------------------------------------
216 /* This configures every package. It is assumed they are all unpacked and
217 that the final configuration is valid. */
218 bool pkgPackageManager::ConfigureAll()
220 pkgOrderList
OList(Cache
);
222 // Populate the order list
223 for (pkgOrderList::iterator I
= List
->begin(); I
!= List
->end(); I
++)
224 if (List
->IsFlag(pkgCache::PkgIterator(Cache
,*I
),
225 pkgOrderList::UnPacked
) == true)
228 if (OList
.OrderConfigure() == false)
231 // Perform the configuring
232 for (pkgOrderList::iterator I
= OList
.begin(); I
!= OList
.end(); I
++)
234 PkgIterator
Pkg(Cache
,*I
);
236 if (Configure(Pkg
) == false)
239 List
->Flag(Pkg
,pkgOrderList::Configured
,pkgOrderList::States
);
245 // PM::SmartConfigure - Perform immediate configuration of the pkg /*{{{*/
246 // ---------------------------------------------------------------------
247 /* This routine scheduals the configuration of the given package and all
248 of it's dependents. */
249 bool pkgPackageManager::SmartConfigure(PkgIterator Pkg
)
251 pkgOrderList
OList(Cache
);
253 if (DepAdd(OList
,Pkg
) == false)
256 if (OList
.OrderConfigure() == false)
259 // Perform the configuring
260 for (pkgOrderList::iterator I
= OList
.begin(); I
!= OList
.end(); I
++)
262 PkgIterator
Pkg(Cache
,*I
);
264 if (Configure(Pkg
) == false)
267 List
->Flag(Pkg
,pkgOrderList::Configured
,pkgOrderList::States
);
271 if (List
->IsFlag(Pkg
,pkgOrderList::Configured
) == false)
272 return _error
->Error("Internal error, could not immediate configure %s",Pkg
.Name());
277 // PM::DepAdd - Add all dependents to the oder list /*{{{*/
278 // ---------------------------------------------------------------------
279 /* This recursively adds all dependents to the order list */
280 bool pkgPackageManager::DepAdd(pkgOrderList
&OList
,PkgIterator Pkg
,int Depth
)
282 if (OList
.IsFlag(Pkg
,pkgOrderList::Added
) == true)
284 if (List
->IsFlag(Pkg
,pkgOrderList::Configured
) == true)
286 if (List
->IsFlag(Pkg
,pkgOrderList::UnPacked
) == false)
290 // Put the package on the list
291 OList
.push_back(Pkg
);
292 OList
.Flag(Pkg
,pkgOrderList::Added
);
295 // Check the dependencies to see if they are all satisfied.
297 for (DepIterator D
= Cache
[Pkg
].InstVerIter(Cache
).DependsList(); D
.end() == false;)
299 if (D
->Type
!= pkgCache::Dep::Depends
&& D
->Type
!= pkgCache::Dep::PreDepends
)
307 for (bool LastOR
= true; D
.end() == false && LastOR
== true; D
++)
309 LastOR
= (D
->CompareOp
& pkgCache::Dep::Or
) == pkgCache::Dep::Or
;
314 Version
**VList
= D
.AllTargets();
315 for (Version
**I
= VList
; *I
!= 0 && Bad
== true; I
++)
317 VerIterator
Ver(Cache
,*I
);
318 PkgIterator Pkg
= Ver
.ParentPkg();
320 // See if the current version is ok
321 if (Pkg
.CurrentVer() == Ver
&& List
->IsNow(Pkg
) == true &&
322 Pkg
.State() == PkgIterator::NeedsNothing
)
328 // Not the install version
329 if (Cache
[Pkg
].InstallVer
!= *I
||
330 (Cache
[Pkg
].Keep() == true && Pkg
.State() == PkgIterator::NeedsNothing
))
332 if (List
->IsFlag(Pkg
,pkgOrderList::UnPacked
) == true)
333 Bad
= !DepAdd(OList
,Pkg
,Depth
);
334 if (List
->IsFlag(Pkg
,pkgOrderList::Configured
) == true)
342 OList
.Flag(Pkg
,0,pkgOrderList::Added
);
353 // PM::EarlyRemove - Perform removal of packages before their time /*{{{*/
354 // ---------------------------------------------------------------------
355 /* This is called to deal with conflicts arising from unpacking */
356 bool pkgPackageManager::EarlyRemove(PkgIterator Pkg
)
358 if (List
->IsNow(Pkg
) == false)
361 // Already removed it
362 if (List
->IsFlag(Pkg
,pkgOrderList::Removed
) == true)
365 // Woops, it will not be re-installed!
366 if (List
->IsFlag(Pkg
,pkgOrderList::InList
) == false)
369 // Essential packages get special treatment
370 if ((Pkg
->Flags
& pkgCache::Flag::Essential
) != 0)
372 if (_config
->FindB("APT::Force-LoopBreak",false) == false)
373 return _error
->Error("This installation run will require temporarily "
374 "removing the essential package %s due to a "
375 "Conflicts/Pre-Depends loop. This is often bad, "
376 "but if you really want to do it, activate the "
377 "APT::Force-LoopBreak option.",Pkg
.Name());
380 bool Res
= SmartRemove(Pkg
);
381 if (Cache
[Pkg
].Delete() == false)
382 List
->Flag(Pkg
,pkgOrderList::Removed
,pkgOrderList::States
);
387 // PM::SmartRemove - Removal Helper /*{{{*/
388 // ---------------------------------------------------------------------
390 bool pkgPackageManager::SmartRemove(PkgIterator Pkg
)
392 if (List
->IsNow(Pkg
) == false)
395 List
->Flag(Pkg
,pkgOrderList::Configured
,pkgOrderList::States
);
396 return Remove(Pkg
,(Cache
[Pkg
].iFlags
& pkgDepCache::Purge
) == pkgDepCache::Purge
);
399 // PM::SmartUnPack - Install helper /*{{{*/
400 // ---------------------------------------------------------------------
401 /* This performs the task of handling pre-depends. */
402 bool pkgPackageManager::SmartUnPack(PkgIterator Pkg
)
404 // Check if it is already unpacked
405 if (Pkg
.State() == pkgCache::PkgIterator::NeedsConfigure
&&
406 Cache
[Pkg
].Keep() == true)
408 List
->Flag(Pkg
,pkgOrderList::UnPacked
,pkgOrderList::States
);
409 if (List
->IsFlag(Pkg
,pkgOrderList::Immediate
) == true)
410 if (SmartConfigure(Pkg
) == false)
411 return _error
->Error("Internal Error, Could not perform immediate configuraton");
415 /* See if this packages install version has any predependencies
416 that are not met by 'now' packages. */
417 for (DepIterator D
= Cache
[Pkg
].InstVerIter(Cache
).DependsList();
420 // Compute a single dependency element (glob or)
421 pkgCache::DepIterator Start
;
422 pkgCache::DepIterator End
;
425 while (End
->Type
== pkgCache::Dep::PreDepends
)
427 // Look for possible ok targets.
428 Version
**VList
= Start
.AllTargets();
430 for (Version
**I
= VList
; *I
!= 0 && Bad
== true; I
++)
432 VerIterator
Ver(Cache
,*I
);
433 PkgIterator Pkg
= Ver
.ParentPkg();
435 // See if the current version is ok
436 if (Pkg
.CurrentVer() == Ver
&& List
->IsNow(Pkg
) == true &&
437 Pkg
.State() == PkgIterator::NeedsNothing
)
444 // Look for something that could be configured.
445 for (Version
**I
= VList
; *I
!= 0 && Bad
== true; I
++)
447 VerIterator
Ver(Cache
,*I
);
448 PkgIterator Pkg
= Ver
.ParentPkg();
450 // Not the install version
451 if (Cache
[Pkg
].InstallVer
!= *I
||
452 (Cache
[Pkg
].Keep() == true && Pkg
.State() == PkgIterator::NeedsNothing
))
455 Bad
= !SmartConfigure(Pkg
);
460 /* If this or element did not match then continue on to the
461 next or element until a matching element is found*/
465 return _error
->Error("Internal Error, Couldn't configure a pre-depend");
472 if (End
->Type
== pkgCache::Dep::Conflicts
)
474 /* Look for conflicts. Two packages that are both in the install
475 state cannot conflict so we don't check.. */
476 Version
**VList
= End
.AllTargets();
477 for (Version
**I
= VList
; *I
!= 0; I
++)
479 VerIterator
Ver(Cache
,*I
);
480 PkgIterator Pkg
= Ver
.ParentPkg();
482 // See if the current version is conflicting
483 if (Pkg
.CurrentVer() == Ver
&& List
->IsNow(Pkg
) == true)
485 if (EarlyRemove(Pkg
) == false)
486 return _error
->Error("Internal Error, Could not early remove %s",Pkg
.Name());
493 // Check for reverse conflicts.
494 CheckRConflicts(Pkg
,Pkg
.RevDependsList(),
495 Cache
[Pkg
].InstVerIter(Cache
).VerStr());
496 for (PrvIterator P
= Cache
[Pkg
].InstVerIter(Cache
).ProvidesList();
497 P
.end() == false; P
++)
498 CheckRConflicts(Pkg
,P
.ParentPkg().RevDependsList(),P
.ProvideVersion());
500 if (Install(Pkg
,FileNames
[Pkg
->ID
]) == false)
503 List
->Flag(Pkg
,pkgOrderList::UnPacked
,pkgOrderList::States
);
505 // Perform immedate configuration of the package.
506 if (List
->IsFlag(Pkg
,pkgOrderList::Immediate
) == true)
507 if (SmartConfigure(Pkg
) == false)
508 return _error
->Error("Internal Error, Could not perform immediate configuraton");
513 // PM::OrderInstall - Installation ordering routine /*{{{*/
514 // ---------------------------------------------------------------------
516 pkgPackageManager::OrderResult
pkgPackageManager::OrderInstall()
518 if (CreateOrderList() == false)
524 clog
<< "Begining to order" << endl
;
526 if (List
->OrderUnpack(FileNames
) == false)
528 _error
->Error("Internal ordering error");
533 clog
<< "Done ordering" << endl
;
535 bool DoneSomething
= false;
536 for (pkgOrderList::iterator I
= List
->begin(); I
!= List
->end(); I
++)
538 PkgIterator
Pkg(Cache
,*I
);
540 if (List
->IsNow(Pkg
) == false)
543 clog
<< "Skipping already done " << Pkg
.Name() << endl
;
547 if (List
->IsMissing(Pkg
) == true)
550 clog
<< "Sequence completed at" << Pkg
.Name() << endl
;
551 if (DoneSomething
== false)
553 _error
->Error("Internal Error, ordering was unable to handle the media swap");
560 if (Cache
[Pkg
].Keep() == true &&
561 Pkg
.State() == pkgCache::PkgIterator::NeedsNothing
&&
562 (Cache
[Pkg
].iFlags
& pkgDepCache::ReInstall
) != pkgDepCache::ReInstall
)
564 _error
->Error("Internal Error, trying to manipulate a kept package");
568 // Perform a delete or an install
569 if (Cache
[Pkg
].Delete() == true)
571 if (SmartRemove(Pkg
) == false)
575 if (SmartUnPack(Pkg
) == false)
577 DoneSomething
= true;
580 // Final run through the configure phase
581 if (ConfigureAll() == false)
585 for (pkgOrderList::iterator I
= List
->begin(); I
!= List
->end(); I
++)
587 if (List
->IsFlag(*I
,pkgOrderList::Configured
) == false)
589 _error
->Error("Internal error, packages left unconfigured. %s",
590 PkgIterator(Cache
,*I
).Name());
598 // PM::DoInstall - Does the installation /*{{{*/
599 // ---------------------------------------------------------------------
600 /* This uses the filenames in FileNames and the information in the
601 DepCache to perform the installation of packages.*/
602 pkgPackageManager::OrderResult
pkgPackageManager::DoInstall()
604 OrderResult Res
= OrderInstall();