]>
git.saurik.com Git - apt.git/blob - apt-pkg/packagemanager.cc
1 // -*- mode: cpp; mode: fold -*-
3 // $Id: packagemanager.cc,v 1.8 1998/11/23 07:03:01 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
)
72 new pkgAcqArchive(Owner
,Sources
,Recs
,Cache
[Pkg
].InstVerIter(Cache
),
79 // PM::FixMissing - Keep all missing packages /*{{{*/
80 // ---------------------------------------------------------------------
81 /* This is called to correct the installation when packages could not
83 bool pkgPackageManager::FixMissing()
85 pkgProblemResolver
Resolve(Cache
);
87 for (PkgIterator I
= Cache
.PkgBegin(); I
.end() == false; I
++)
89 if (Cache
[I
].Keep() == true)
91 if (FileNames
[I
->ID
].empty() == false || Cache
[I
].Delete() == true)
96 // Now downgrade everything that is broken
97 return Resolve
.ResolveByKeep() == true && Cache
.BrokenCount() == 0;
101 // PM::CreateOrderList - Create the ordering class /*{{{*/
102 // ---------------------------------------------------------------------
103 /* This populates the ordering list with all the packages that are
105 bool pkgPackageManager::CreateOrderList()
108 List
= new pkgOrderList(Cache
);
110 // Generate the list of affected packages and sort it
111 for (PkgIterator I
= Cache
.PkgBegin(); I
.end() == false; I
++)
113 // Mark the package for immediate configuration
114 if ((I
->Flags
& pkgCache::Flag::Essential
) == pkgCache::Flag::Essential
)
116 List
->Flag(I
,pkgOrderList::Immediate
);
118 // Look for other packages to make immediate configurea
119 if (Cache
[I
].InstallVer
!= 0)
120 for (DepIterator D
= Cache
[I
].InstVerIter(Cache
).DependsList();
121 D
.end() == false; D
++)
122 if (D
->Type
== pkgCache::Dep::Depends
|| D
->Type
== pkgCache::Dep::PreDepends
)
123 List
->Flag(D
.TargetPkg(),pkgOrderList::Immediate
);
125 // And again with the current version.
126 if (I
->CurrentVer
!= 0)
127 for (DepIterator D
= I
.CurrentVer().DependsList();
128 D
.end() == false; D
++)
129 if (D
->Type
== pkgCache::Dep::Depends
|| D
->Type
== pkgCache::Dep::PreDepends
)
130 List
->Flag(D
.TargetPkg(),pkgOrderList::Immediate
);
134 if ((Cache
[I
].Keep() == true ||
135 Cache
[I
].InstVerIter(Cache
) == I
.CurrentVer()) &&
136 I
.State() == pkgCache::PkgIterator::NeedsNothing
)
139 // Append it to the list
142 if ((I
->Flags
& pkgCache::Flag::ImmediateConf
) == pkgCache::Flag::ImmediateConf
)
143 List
->Flag(I
,pkgOrderList::Immediate
);
149 // PM::DepAlwaysTrue - Returns true if this dep is irrelevent /*{{{*/
150 // ---------------------------------------------------------------------
151 /* The restriction on provides is to eliminate the case when provides
152 are transitioning between valid states [ie exim to smail] */
153 bool pkgPackageManager::DepAlwaysTrue(DepIterator D
)
155 if (D
.TargetPkg()->ProvidesList
!= 0)
158 if ((Cache
[D
] & pkgDepCache::DepInstall
) != 0 &&
159 (Cache
[D
] & pkgDepCache::DepNow
) != 0)
164 // PM::CheckRConflicts - Look for reverse conflicts /*{{{*/
165 // ---------------------------------------------------------------------
166 /* This looks over the reverses for a conflicts line that needs early
168 bool pkgPackageManager::CheckRConflicts(PkgIterator Pkg
,DepIterator D
,
171 for (;D
.end() == false; D
++)
173 if (D
->Type
!= pkgCache::Dep::Conflicts
)
176 if (D
.ParentPkg() == Pkg
)
179 if (pkgCheckDep(D
.TargetVer(),Ver
,D
->CompareOp
) == false)
182 if (List
->IsNow(Pkg
) == false)
185 if (EarlyRemove(D
.ParentPkg()) == false)
191 // PM::ConfigureAll - Run the all out configuration /*{{{*/
192 // ---------------------------------------------------------------------
193 /* This configures every package. It is assumed they are all unpacked and
194 that the final configuration is valid. */
195 bool pkgPackageManager::ConfigureAll()
197 pkgOrderList
OList(Cache
);
199 // Populate the order list
200 for (pkgOrderList::iterator I
= List
->begin(); I
!= List
->end(); I
++)
201 if (List
->IsFlag(pkgCache::PkgIterator(Cache
,*I
),
202 pkgOrderList::UnPacked
) == true)
205 if (OList
.OrderConfigure() == false)
208 // Perform the configuring
209 for (pkgOrderList::iterator I
= OList
.begin(); I
!= OList
.end(); I
++)
211 PkgIterator
Pkg(Cache
,*I
);
213 if (Configure(Pkg
) == false)
216 List
->Flag(Pkg
,pkgOrderList::Configured
,pkgOrderList::States
);
222 // PM::SmartConfigure - Perform immediate configuration of the pkg /*{{{*/
223 // ---------------------------------------------------------------------
224 /* This routine scheduals the configuration of the given package and all
225 of it's dependents. */
226 bool pkgPackageManager::SmartConfigure(PkgIterator Pkg
)
228 pkgOrderList
OList(Cache
);
230 if (DepAdd(OList
,Pkg
) == false)
233 if (OList
.OrderConfigure() == false)
236 // Perform the configuring
237 for (pkgOrderList::iterator I
= OList
.begin(); I
!= OList
.end(); I
++)
239 PkgIterator
Pkg(Cache
,*I
);
241 if (Configure(Pkg
) == false)
244 List
->Flag(Pkg
,pkgOrderList::Configured
,pkgOrderList::States
);
248 if (List
->IsFlag(Pkg
,pkgOrderList::Configured
) == false)
249 return _error
->Error("Internal error, could not immediate configure %s",Pkg
.Name());
254 // PM::DepAdd - Add all dependents to the oder list /*{{{*/
255 // ---------------------------------------------------------------------
256 /* This recursively adds all dependents to the order list */
257 bool pkgPackageManager::DepAdd(pkgOrderList
&OList
,PkgIterator Pkg
,int Depth
)
259 if (OList
.IsFlag(Pkg
,pkgOrderList::Added
) == true)
261 if (List
->IsFlag(Pkg
,pkgOrderList::Configured
) == true)
263 if (List
->IsFlag(Pkg
,pkgOrderList::UnPacked
) == false)
267 // Put the package on the list
268 OList
.push_back(Pkg
);
269 OList
.Flag(Pkg
,pkgOrderList::Added
);
272 // Check the dependencies to see if they are all satisfied.
274 for (DepIterator D
= Cache
[Pkg
].InstVerIter(Cache
).DependsList(); D
.end() == false;)
276 if (D
->Type
!= pkgCache::Dep::Depends
&& D
->Type
!= pkgCache::Dep::PreDepends
)
284 for (bool LastOR
= true; D
.end() == false && LastOR
== true; D
++)
286 LastOR
= (D
->CompareOp
& pkgCache::Dep::Or
) == pkgCache::Dep::Or
;
291 Version
**VList
= D
.AllTargets();
292 for (Version
**I
= VList
; *I
!= 0 && Bad
== true; I
++)
294 VerIterator
Ver(Cache
,*I
);
295 PkgIterator Pkg
= Ver
.ParentPkg();
297 // See if the current version is ok
298 if (Pkg
.CurrentVer() == Ver
&& List
->IsNow(Pkg
) == true &&
299 Pkg
.State() == PkgIterator::NeedsNothing
)
305 // Not the install version
306 if (Cache
[Pkg
].InstallVer
!= *I
||
307 (Cache
[Pkg
].Keep() == true && Pkg
.State() == PkgIterator::NeedsNothing
))
309 if (List
->IsFlag(Pkg
,pkgOrderList::UnPacked
) == true)
310 Bad
= !DepAdd(OList
,Pkg
,Depth
);
311 if (List
->IsFlag(Pkg
,pkgOrderList::Configured
) == true)
319 OList
.Flag(Pkg
,0,pkgOrderList::Added
);
330 // PM::EarlyRemove - Perform removal of packages before their time /*{{{*/
331 // ---------------------------------------------------------------------
332 /* This is called to deal with conflicts arising from unpacking */
333 bool pkgPackageManager::EarlyRemove(PkgIterator Pkg
)
335 if (List
->IsNow(Pkg
) == false)
338 // Already removed it
339 if (List
->IsFlag(Pkg
,pkgOrderList::Removed
) == true)
342 // Woops, it will not be re-installed!
343 if (List
->IsFlag(Pkg
,pkgOrderList::InList
) == false)
346 bool Res
= SmartRemove(Pkg
);
347 if (Cache
[Pkg
].Delete() == false)
348 List
->Flag(Pkg
,pkgOrderList::Removed
,pkgOrderList::States
);
353 // PM::SmartRemove - Removal Helper /*{{{*/
354 // ---------------------------------------------------------------------
356 bool pkgPackageManager::SmartRemove(PkgIterator Pkg
)
358 if (List
->IsNow(Pkg
) == false)
361 List
->Flag(Pkg
,pkgOrderList::Configured
,pkgOrderList::States
);
365 // PM::SmartUnPack - Install helper /*{{{*/
366 // ---------------------------------------------------------------------
367 /* This performs the task of handling pre-depends. */
368 bool pkgPackageManager::SmartUnPack(PkgIterator Pkg
)
370 // Check if it is already unpacked
371 if (Pkg
.State() == pkgCache::PkgIterator::NeedsConfigure
&&
372 Cache
[Pkg
].Keep() == true)
374 List
->Flag(Pkg
,pkgOrderList::UnPacked
,pkgOrderList::States
);
375 if (List
->IsFlag(Pkg
,pkgOrderList::Immediate
) == true)
376 if (SmartConfigure(Pkg
) == false)
377 return _error
->Error("Internal Error, Could not perform immediate configuraton");
381 /* See if this packages install version has any predependencies
382 that are not met by 'now' packages. */
383 for (DepIterator D
= Cache
[Pkg
].InstVerIter(Cache
).DependsList();
384 D
.end() == false; D
++)
386 if (D
->Type
== pkgCache::Dep::PreDepends
)
388 // Look for possible ok targets.
389 Version
**VList
= D
.AllTargets();
391 for (Version
**I
= VList
; *I
!= 0 && Bad
== true; I
++)
393 VerIterator
Ver(Cache
,*I
);
394 PkgIterator Pkg
= Ver
.ParentPkg();
396 // See if the current version is ok
397 if (Pkg
.CurrentVer() == Ver
&& List
->IsNow(Pkg
) == true &&
398 Pkg
.State() == PkgIterator::NeedsNothing
)
405 // Look for something that could be configured.
406 for (Version
**I
= VList
; *I
!= 0 && Bad
== true; I
++)
408 VerIterator
Ver(Cache
,*I
);
409 PkgIterator Pkg
= Ver
.ParentPkg();
411 // Not the install version
412 if (Cache
[Pkg
].InstallVer
!= *I
||
413 (Cache
[Pkg
].Keep() == true && Pkg
.State() == PkgIterator::NeedsNothing
))
416 Bad
= !SmartConfigure(Pkg
);
422 return _error
->Error("Internal Error, Couldn't configure a pre-depend");
427 if (D
->Type
== pkgCache::Dep::Conflicts
)
429 /* Look for conflicts. Two packages that are both in the install
430 state cannot conflict so we don't check.. */
431 Version
**VList
= D
.AllTargets();
432 for (Version
**I
= VList
; *I
!= 0; I
++)
434 VerIterator
Ver(Cache
,*I
);
435 PkgIterator Pkg
= Ver
.ParentPkg();
437 // See if the current version is conflicting
438 if (Pkg
.CurrentVer() == Ver
&& List
->IsNow(Pkg
) == true)
440 if (EarlyRemove(Pkg
) == false)
441 return _error
->Error("Internal Error, Could not early remove %s",Pkg
.Name());
448 // Check for reverse conflicts.
449 CheckRConflicts(Pkg
,Pkg
.RevDependsList(),
450 Cache
[Pkg
].InstVerIter(Cache
).VerStr());
451 for (PrvIterator P
= Cache
[Pkg
].InstVerIter(Cache
).ProvidesList();
452 P
.end() == false; P
++)
453 CheckRConflicts(Pkg
,P
.ParentPkg().RevDependsList(),P
.ProvideVersion());
455 if (Install(Pkg
,FileNames
[Pkg
->ID
]) == false)
458 List
->Flag(Pkg
,pkgOrderList::UnPacked
,pkgOrderList::States
);
460 // Perform immedate configuration of the package.
461 if (List
->IsFlag(Pkg
,pkgOrderList::Immediate
) == true)
462 if (SmartConfigure(Pkg
) == false)
463 return _error
->Error("Internal Error, Could not perform immediate configuraton");
468 // PM::OrderInstall - Installation ordering routine /*{{{*/
469 // ---------------------------------------------------------------------
471 bool pkgPackageManager::OrderInstall()
473 if (CreateOrderList() == false)
477 clog
<< "Begining to order" << endl
;
479 if (List
->OrderUnpack() == false)
480 return _error
->Error("Internal ordering error");
483 clog
<< "Done ordering" << endl
;
485 for (pkgOrderList::iterator I
= List
->begin(); I
!= List
->end(); I
++)
487 PkgIterator
Pkg(Cache
,*I
);
490 if (Cache
[Pkg
].Keep() == true && Pkg
.State() == pkgCache::PkgIterator::NeedsNothing
)
491 return _error
->Error("Internal Error, trying to manipulate a kept package");
493 // Perform a delete or an install
494 if (Cache
[Pkg
].Delete() == true)
496 if (SmartRemove(Pkg
) == false)
500 if (SmartUnPack(Pkg
) == false)
504 // Final run through the configure phase
505 if (ConfigureAll() == false)
509 for (pkgOrderList::iterator I
= List
->begin(); I
!= List
->end(); I
++)
510 if (List
->IsFlag(*I
,pkgOrderList::Configured
) == false)
511 return _error
->Error("Internal error, packages left unconfigured. %s",
512 PkgIterator(Cache
,*I
).Name());
517 // PM::DoInstall - Does the installation /*{{{*/
518 // ---------------------------------------------------------------------
519 /* This uses the filenames in FileNames and the information in the
520 DepCache to perform the installation of packages.*/
521 bool pkgPackageManager::DoInstall()
523 return OrderInstall() && Go();