1 // -*- mode: cpp; mode: fold -*-
3 // $Id: packagemanager.cc,v 1.13 1999/02/19 07:56:07 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
);
64 // Skip packages to erase
65 if (Cache
[Pkg
].Delete() == true)
68 // Skip Packages that need configure only.
69 if (Pkg
.State() == pkgCache::PkgIterator::NeedsConfigure
&&
70 Cache
[Pkg
].Keep() == true)
73 new pkgAcqArchive(Owner
,Sources
,Recs
,Cache
[Pkg
].InstVerIter(Cache
),
80 // PM::FixMissing - Keep all missing packages /*{{{*/
81 // ---------------------------------------------------------------------
82 /* This is called to correct the installation when packages could not
84 bool pkgPackageManager::FixMissing()
86 pkgProblemResolver
Resolve(Cache
);
89 for (PkgIterator I
= Cache
.PkgBegin(); I
.end() == false; I
++)
91 // These don't need files
92 if (Cache
[I
].Keep() == true)
94 if (Cache
[I
].Delete() == true)
98 if (FileNames
[I
->ID
].empty() == false)
101 // Skip Packages that need configure only.
102 if (I
.State() == pkgCache::PkgIterator::NeedsConfigure
&&
103 Cache
[I
].Keep() == true)
106 // Okay, this file is missing and we need it. Mark it for keep
114 // Now downgrade everything that is broken
115 return Resolve
.ResolveByKeep() == true && Cache
.BrokenCount() == 0;
119 // PM::CreateOrderList - Create the ordering class /*{{{*/
120 // ---------------------------------------------------------------------
121 /* This populates the ordering list with all the packages that are
123 bool pkgPackageManager::CreateOrderList()
126 List
= new pkgOrderList(Cache
);
128 bool NoImmConfigure
= _config
->FindB("APT::Immediate-Configure",false);
130 // Generate the list of affected packages and sort it
131 for (PkgIterator I
= Cache
.PkgBegin(); I
.end() == false; I
++)
133 // Mark the package for immediate configuration
134 if ((I
->Flags
& pkgCache::Flag::Essential
) == pkgCache::Flag::Essential
&&
135 NoImmConfigure
== false)
137 List
->Flag(I
,pkgOrderList::Immediate
);
139 // Look for other packages to make immediate configurea
140 if (Cache
[I
].InstallVer
!= 0)
141 for (DepIterator D
= Cache
[I
].InstVerIter(Cache
).DependsList();
142 D
.end() == false; D
++)
143 if (D
->Type
== pkgCache::Dep::Depends
|| D
->Type
== pkgCache::Dep::PreDepends
)
144 List
->Flag(D
.TargetPkg(),pkgOrderList::Immediate
);
146 // And again with the current version.
147 if (I
->CurrentVer
!= 0)
148 for (DepIterator D
= I
.CurrentVer().DependsList();
149 D
.end() == false; D
++)
150 if (D
->Type
== pkgCache::Dep::Depends
|| D
->Type
== pkgCache::Dep::PreDepends
)
151 List
->Flag(D
.TargetPkg(),pkgOrderList::Immediate
);
155 if ((Cache
[I
].Keep() == true ||
156 Cache
[I
].InstVerIter(Cache
) == I
.CurrentVer()) &&
157 I
.State() == pkgCache::PkgIterator::NeedsNothing
)
160 // Append it to the list
163 if ((I
->Flags
& pkgCache::Flag::ImmediateConf
) == pkgCache::Flag::ImmediateConf
)
164 List
->Flag(I
,pkgOrderList::Immediate
);
170 // PM::DepAlwaysTrue - Returns true if this dep is irrelevent /*{{{*/
171 // ---------------------------------------------------------------------
172 /* The restriction on provides is to eliminate the case when provides
173 are transitioning between valid states [ie exim to smail] */
174 bool pkgPackageManager::DepAlwaysTrue(DepIterator D
)
176 if (D
.TargetPkg()->ProvidesList
!= 0)
179 if ((Cache
[D
] & pkgDepCache::DepInstall
) != 0 &&
180 (Cache
[D
] & pkgDepCache::DepNow
) != 0)
185 // PM::CheckRConflicts - Look for reverse conflicts /*{{{*/
186 // ---------------------------------------------------------------------
187 /* This looks over the reverses for a conflicts line that needs early
189 bool pkgPackageManager::CheckRConflicts(PkgIterator Pkg
,DepIterator D
,
192 for (;D
.end() == false; D
++)
194 if (D
->Type
!= pkgCache::Dep::Conflicts
)
197 if (D
.ParentPkg() == Pkg
)
200 if (pkgCheckDep(D
.TargetVer(),Ver
,D
->CompareOp
) == false)
203 if (List
->IsNow(Pkg
) == false)
206 if (EarlyRemove(D
.ParentPkg()) == false)
212 // PM::ConfigureAll - Run the all out configuration /*{{{*/
213 // ---------------------------------------------------------------------
214 /* This configures every package. It is assumed they are all unpacked and
215 that the final configuration is valid. */
216 bool pkgPackageManager::ConfigureAll()
218 pkgOrderList
OList(Cache
);
220 // Populate the order list
221 for (pkgOrderList::iterator I
= List
->begin(); I
!= List
->end(); I
++)
222 if (List
->IsFlag(pkgCache::PkgIterator(Cache
,*I
),
223 pkgOrderList::UnPacked
) == true)
226 if (OList
.OrderConfigure() == false)
229 // Perform the configuring
230 for (pkgOrderList::iterator I
= OList
.begin(); I
!= OList
.end(); I
++)
232 PkgIterator
Pkg(Cache
,*I
);
234 if (Configure(Pkg
) == false)
237 List
->Flag(Pkg
,pkgOrderList::Configured
,pkgOrderList::States
);
243 // PM::SmartConfigure - Perform immediate configuration of the pkg /*{{{*/
244 // ---------------------------------------------------------------------
245 /* This routine scheduals the configuration of the given package and all
246 of it's dependents. */
247 bool pkgPackageManager::SmartConfigure(PkgIterator Pkg
)
249 pkgOrderList
OList(Cache
);
251 if (DepAdd(OList
,Pkg
) == false)
254 if (OList
.OrderConfigure() == false)
257 // Perform the configuring
258 for (pkgOrderList::iterator I
= OList
.begin(); I
!= OList
.end(); I
++)
260 PkgIterator
Pkg(Cache
,*I
);
262 if (Configure(Pkg
) == false)
265 List
->Flag(Pkg
,pkgOrderList::Configured
,pkgOrderList::States
);
269 if (List
->IsFlag(Pkg
,pkgOrderList::Configured
) == false)
270 return _error
->Error("Internal error, could not immediate configure %s",Pkg
.Name());
275 // PM::DepAdd - Add all dependents to the oder list /*{{{*/
276 // ---------------------------------------------------------------------
277 /* This recursively adds all dependents to the order list */
278 bool pkgPackageManager::DepAdd(pkgOrderList
&OList
,PkgIterator Pkg
,int Depth
)
280 if (OList
.IsFlag(Pkg
,pkgOrderList::Added
) == true)
282 if (List
->IsFlag(Pkg
,pkgOrderList::Configured
) == true)
284 if (List
->IsFlag(Pkg
,pkgOrderList::UnPacked
) == false)
288 // Put the package on the list
289 OList
.push_back(Pkg
);
290 OList
.Flag(Pkg
,pkgOrderList::Added
);
293 // Check the dependencies to see if they are all satisfied.
295 for (DepIterator D
= Cache
[Pkg
].InstVerIter(Cache
).DependsList(); D
.end() == false;)
297 if (D
->Type
!= pkgCache::Dep::Depends
&& D
->Type
!= pkgCache::Dep::PreDepends
)
305 for (bool LastOR
= true; D
.end() == false && LastOR
== true; D
++)
307 LastOR
= (D
->CompareOp
& pkgCache::Dep::Or
) == pkgCache::Dep::Or
;
312 Version
**VList
= D
.AllTargets();
313 for (Version
**I
= VList
; *I
!= 0 && Bad
== true; I
++)
315 VerIterator
Ver(Cache
,*I
);
316 PkgIterator Pkg
= Ver
.ParentPkg();
318 // See if the current version is ok
319 if (Pkg
.CurrentVer() == Ver
&& List
->IsNow(Pkg
) == true &&
320 Pkg
.State() == PkgIterator::NeedsNothing
)
326 // Not the install version
327 if (Cache
[Pkg
].InstallVer
!= *I
||
328 (Cache
[Pkg
].Keep() == true && Pkg
.State() == PkgIterator::NeedsNothing
))
330 if (List
->IsFlag(Pkg
,pkgOrderList::UnPacked
) == true)
331 Bad
= !DepAdd(OList
,Pkg
,Depth
);
332 if (List
->IsFlag(Pkg
,pkgOrderList::Configured
) == true)
340 OList
.Flag(Pkg
,0,pkgOrderList::Added
);
351 // PM::EarlyRemove - Perform removal of packages before their time /*{{{*/
352 // ---------------------------------------------------------------------
353 /* This is called to deal with conflicts arising from unpacking */
354 bool pkgPackageManager::EarlyRemove(PkgIterator Pkg
)
356 if (List
->IsNow(Pkg
) == false)
359 // Already removed it
360 if (List
->IsFlag(Pkg
,pkgOrderList::Removed
) == true)
363 // Woops, it will not be re-installed!
364 if (List
->IsFlag(Pkg
,pkgOrderList::InList
) == false)
367 bool Res
= SmartRemove(Pkg
);
368 if (Cache
[Pkg
].Delete() == false)
369 List
->Flag(Pkg
,pkgOrderList::Removed
,pkgOrderList::States
);
374 // PM::SmartRemove - Removal Helper /*{{{*/
375 // ---------------------------------------------------------------------
377 bool pkgPackageManager::SmartRemove(PkgIterator Pkg
)
379 if (List
->IsNow(Pkg
) == false)
382 List
->Flag(Pkg
,pkgOrderList::Configured
,pkgOrderList::States
);
386 // PM::SmartUnPack - Install helper /*{{{*/
387 // ---------------------------------------------------------------------
388 /* This performs the task of handling pre-depends. */
389 bool pkgPackageManager::SmartUnPack(PkgIterator Pkg
)
391 // Check if it is already unpacked
392 if (Pkg
.State() == pkgCache::PkgIterator::NeedsConfigure
&&
393 Cache
[Pkg
].Keep() == true)
395 List
->Flag(Pkg
,pkgOrderList::UnPacked
,pkgOrderList::States
);
396 if (List
->IsFlag(Pkg
,pkgOrderList::Immediate
) == true)
397 if (SmartConfigure(Pkg
) == false)
398 return _error
->Error("Internal Error, Could not perform immediate configuraton");
402 /* See if this packages install version has any predependencies
403 that are not met by 'now' packages. */
404 for (DepIterator D
= Cache
[Pkg
].InstVerIter(Cache
).DependsList();
405 D
.end() == false; D
++)
407 if (D
->Type
== pkgCache::Dep::PreDepends
)
409 // Look for possible ok targets.
410 Version
**VList
= D
.AllTargets();
412 for (Version
**I
= VList
; *I
!= 0 && Bad
== true; I
++)
414 VerIterator
Ver(Cache
,*I
);
415 PkgIterator Pkg
= Ver
.ParentPkg();
417 // See if the current version is ok
418 if (Pkg
.CurrentVer() == Ver
&& List
->IsNow(Pkg
) == true &&
419 Pkg
.State() == PkgIterator::NeedsNothing
)
426 // Look for something that could be configured.
427 for (Version
**I
= VList
; *I
!= 0 && Bad
== true; I
++)
429 VerIterator
Ver(Cache
,*I
);
430 PkgIterator Pkg
= Ver
.ParentPkg();
432 // Not the install version
433 if (Cache
[Pkg
].InstallVer
!= *I
||
434 (Cache
[Pkg
].Keep() == true && Pkg
.State() == PkgIterator::NeedsNothing
))
437 Bad
= !SmartConfigure(Pkg
);
443 return _error
->Error("Internal Error, Couldn't configure a pre-depend");
448 if (D
->Type
== pkgCache::Dep::Conflicts
)
450 /* Look for conflicts. Two packages that are both in the install
451 state cannot conflict so we don't check.. */
452 Version
**VList
= D
.AllTargets();
453 for (Version
**I
= VList
; *I
!= 0; I
++)
455 VerIterator
Ver(Cache
,*I
);
456 PkgIterator Pkg
= Ver
.ParentPkg();
458 // See if the current version is conflicting
459 if (Pkg
.CurrentVer() == Ver
&& List
->IsNow(Pkg
) == true)
461 if (EarlyRemove(Pkg
) == false)
462 return _error
->Error("Internal Error, Could not early remove %s",Pkg
.Name());
469 // Check for reverse conflicts.
470 CheckRConflicts(Pkg
,Pkg
.RevDependsList(),
471 Cache
[Pkg
].InstVerIter(Cache
).VerStr());
472 for (PrvIterator P
= Cache
[Pkg
].InstVerIter(Cache
).ProvidesList();
473 P
.end() == false; P
++)
474 CheckRConflicts(Pkg
,P
.ParentPkg().RevDependsList(),P
.ProvideVersion());
476 if (Install(Pkg
,FileNames
[Pkg
->ID
]) == false)
479 List
->Flag(Pkg
,pkgOrderList::UnPacked
,pkgOrderList::States
);
481 // Perform immedate configuration of the package.
482 if (List
->IsFlag(Pkg
,pkgOrderList::Immediate
) == true)
483 if (SmartConfigure(Pkg
) == false)
484 return _error
->Error("Internal Error, Could not perform immediate configuraton");
489 // PM::OrderInstall - Installation ordering routine /*{{{*/
490 // ---------------------------------------------------------------------
492 bool pkgPackageManager::OrderInstall()
494 if (CreateOrderList() == false)
498 clog
<< "Begining to order" << endl
;
500 if (List
->OrderUnpack() == false)
501 return _error
->Error("Internal ordering error");
504 clog
<< "Done ordering" << endl
;
506 for (pkgOrderList::iterator I
= List
->begin(); I
!= List
->end(); I
++)
508 PkgIterator
Pkg(Cache
,*I
);
511 if (Cache
[Pkg
].Keep() == true && Pkg
.State() == pkgCache::PkgIterator::NeedsNothing
)
512 return _error
->Error("Internal Error, trying to manipulate a kept package");
514 // Perform a delete or an install
515 if (Cache
[Pkg
].Delete() == true)
517 if (SmartRemove(Pkg
) == false)
521 if (SmartUnPack(Pkg
) == false)
525 // Final run through the configure phase
526 if (ConfigureAll() == false)
530 for (pkgOrderList::iterator I
= List
->begin(); I
!= List
->end(); I
++)
531 if (List
->IsFlag(*I
,pkgOrderList::Configured
) == false)
532 return _error
->Error("Internal error, packages left unconfigured. %s",
533 PkgIterator(Cache
,*I
).Name());
538 // PM::DoInstall - Does the installation /*{{{*/
539 // ---------------------------------------------------------------------
540 /* This uses the filenames in FileNames and the information in the
541 DepCache to perform the installation of packages.*/
542 bool pkgPackageManager::DoInstall()
544 return OrderInstall() && Go();