]>
git.saurik.com Git - apt.git/blob - apt-pkg/packagemanager.cc
1 // -*- mode: cpp; mode: fold -*-
3 // $Id: packagemanager.cc,v 1.12 1999/01/31 06:24:46 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 bool NoImmConfigure
= _config
->FindB("APT::Immediate-Configure",false);
112 // Generate the list of affected packages and sort it
113 for (PkgIterator I
= Cache
.PkgBegin(); I
.end() == false; I
++)
115 // Mark the package for immediate configuration
116 if ((I
->Flags
& pkgCache::Flag::Essential
) == pkgCache::Flag::Essential
&&
117 NoImmConfigure
== false)
119 List
->Flag(I
,pkgOrderList::Immediate
);
121 // Look for other packages to make immediate configurea
122 if (Cache
[I
].InstallVer
!= 0)
123 for (DepIterator D
= Cache
[I
].InstVerIter(Cache
).DependsList();
124 D
.end() == false; D
++)
125 if (D
->Type
== pkgCache::Dep::Depends
|| D
->Type
== pkgCache::Dep::PreDepends
)
126 List
->Flag(D
.TargetPkg(),pkgOrderList::Immediate
);
128 // And again with the current version.
129 if (I
->CurrentVer
!= 0)
130 for (DepIterator D
= I
.CurrentVer().DependsList();
131 D
.end() == false; D
++)
132 if (D
->Type
== pkgCache::Dep::Depends
|| D
->Type
== pkgCache::Dep::PreDepends
)
133 List
->Flag(D
.TargetPkg(),pkgOrderList::Immediate
);
137 if ((Cache
[I
].Keep() == true ||
138 Cache
[I
].InstVerIter(Cache
) == I
.CurrentVer()) &&
139 I
.State() == pkgCache::PkgIterator::NeedsNothing
)
142 // Append it to the list
145 if ((I
->Flags
& pkgCache::Flag::ImmediateConf
) == pkgCache::Flag::ImmediateConf
)
146 List
->Flag(I
,pkgOrderList::Immediate
);
152 // PM::DepAlwaysTrue - Returns true if this dep is irrelevent /*{{{*/
153 // ---------------------------------------------------------------------
154 /* The restriction on provides is to eliminate the case when provides
155 are transitioning between valid states [ie exim to smail] */
156 bool pkgPackageManager::DepAlwaysTrue(DepIterator D
)
158 if (D
.TargetPkg()->ProvidesList
!= 0)
161 if ((Cache
[D
] & pkgDepCache::DepInstall
) != 0 &&
162 (Cache
[D
] & pkgDepCache::DepNow
) != 0)
167 // PM::CheckRConflicts - Look for reverse conflicts /*{{{*/
168 // ---------------------------------------------------------------------
169 /* This looks over the reverses for a conflicts line that needs early
171 bool pkgPackageManager::CheckRConflicts(PkgIterator Pkg
,DepIterator D
,
174 for (;D
.end() == false; D
++)
176 if (D
->Type
!= pkgCache::Dep::Conflicts
)
179 if (D
.ParentPkg() == Pkg
)
182 if (pkgCheckDep(D
.TargetVer(),Ver
,D
->CompareOp
) == false)
185 if (List
->IsNow(Pkg
) == false)
188 if (EarlyRemove(D
.ParentPkg()) == false)
194 // PM::ConfigureAll - Run the all out configuration /*{{{*/
195 // ---------------------------------------------------------------------
196 /* This configures every package. It is assumed they are all unpacked and
197 that the final configuration is valid. */
198 bool pkgPackageManager::ConfigureAll()
200 pkgOrderList
OList(Cache
);
202 // Populate the order list
203 for (pkgOrderList::iterator I
= List
->begin(); I
!= List
->end(); I
++)
204 if (List
->IsFlag(pkgCache::PkgIterator(Cache
,*I
),
205 pkgOrderList::UnPacked
) == true)
208 if (OList
.OrderConfigure() == false)
211 // Perform the configuring
212 for (pkgOrderList::iterator I
= OList
.begin(); I
!= OList
.end(); I
++)
214 PkgIterator
Pkg(Cache
,*I
);
216 if (Configure(Pkg
) == false)
219 List
->Flag(Pkg
,pkgOrderList::Configured
,pkgOrderList::States
);
225 // PM::SmartConfigure - Perform immediate configuration of the pkg /*{{{*/
226 // ---------------------------------------------------------------------
227 /* This routine scheduals the configuration of the given package and all
228 of it's dependents. */
229 bool pkgPackageManager::SmartConfigure(PkgIterator Pkg
)
231 pkgOrderList
OList(Cache
);
233 if (DepAdd(OList
,Pkg
) == false)
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
);
251 if (List
->IsFlag(Pkg
,pkgOrderList::Configured
) == false)
252 return _error
->Error("Internal error, could not immediate configure %s",Pkg
.Name());
257 // PM::DepAdd - Add all dependents to the oder list /*{{{*/
258 // ---------------------------------------------------------------------
259 /* This recursively adds all dependents to the order list */
260 bool pkgPackageManager::DepAdd(pkgOrderList
&OList
,PkgIterator Pkg
,int Depth
)
262 if (OList
.IsFlag(Pkg
,pkgOrderList::Added
) == true)
264 if (List
->IsFlag(Pkg
,pkgOrderList::Configured
) == true)
266 if (List
->IsFlag(Pkg
,pkgOrderList::UnPacked
) == false)
270 // Put the package on the list
271 OList
.push_back(Pkg
);
272 OList
.Flag(Pkg
,pkgOrderList::Added
);
275 // Check the dependencies to see if they are all satisfied.
277 for (DepIterator D
= Cache
[Pkg
].InstVerIter(Cache
).DependsList(); D
.end() == false;)
279 if (D
->Type
!= pkgCache::Dep::Depends
&& D
->Type
!= pkgCache::Dep::PreDepends
)
287 for (bool LastOR
= true; D
.end() == false && LastOR
== true; D
++)
289 LastOR
= (D
->CompareOp
& pkgCache::Dep::Or
) == pkgCache::Dep::Or
;
294 Version
**VList
= D
.AllTargets();
295 for (Version
**I
= VList
; *I
!= 0 && Bad
== true; I
++)
297 VerIterator
Ver(Cache
,*I
);
298 PkgIterator Pkg
= Ver
.ParentPkg();
300 // See if the current version is ok
301 if (Pkg
.CurrentVer() == Ver
&& List
->IsNow(Pkg
) == true &&
302 Pkg
.State() == PkgIterator::NeedsNothing
)
308 // Not the install version
309 if (Cache
[Pkg
].InstallVer
!= *I
||
310 (Cache
[Pkg
].Keep() == true && Pkg
.State() == PkgIterator::NeedsNothing
))
312 if (List
->IsFlag(Pkg
,pkgOrderList::UnPacked
) == true)
313 Bad
= !DepAdd(OList
,Pkg
,Depth
);
314 if (List
->IsFlag(Pkg
,pkgOrderList::Configured
) == true)
322 OList
.Flag(Pkg
,0,pkgOrderList::Added
);
333 // PM::EarlyRemove - Perform removal of packages before their time /*{{{*/
334 // ---------------------------------------------------------------------
335 /* This is called to deal with conflicts arising from unpacking */
336 bool pkgPackageManager::EarlyRemove(PkgIterator Pkg
)
338 if (List
->IsNow(Pkg
) == false)
341 // Already removed it
342 if (List
->IsFlag(Pkg
,pkgOrderList::Removed
) == true)
345 // Woops, it will not be re-installed!
346 if (List
->IsFlag(Pkg
,pkgOrderList::InList
) == false)
349 bool Res
= SmartRemove(Pkg
);
350 if (Cache
[Pkg
].Delete() == false)
351 List
->Flag(Pkg
,pkgOrderList::Removed
,pkgOrderList::States
);
356 // PM::SmartRemove - Removal Helper /*{{{*/
357 // ---------------------------------------------------------------------
359 bool pkgPackageManager::SmartRemove(PkgIterator Pkg
)
361 if (List
->IsNow(Pkg
) == false)
364 List
->Flag(Pkg
,pkgOrderList::Configured
,pkgOrderList::States
);
368 // PM::SmartUnPack - Install helper /*{{{*/
369 // ---------------------------------------------------------------------
370 /* This performs the task of handling pre-depends. */
371 bool pkgPackageManager::SmartUnPack(PkgIterator Pkg
)
373 // Check if it is already unpacked
374 if (Pkg
.State() == pkgCache::PkgIterator::NeedsConfigure
&&
375 Cache
[Pkg
].Keep() == true)
377 List
->Flag(Pkg
,pkgOrderList::UnPacked
,pkgOrderList::States
);
378 if (List
->IsFlag(Pkg
,pkgOrderList::Immediate
) == true)
379 if (SmartConfigure(Pkg
) == false)
380 return _error
->Error("Internal Error, Could not perform immediate configuraton");
384 /* See if this packages install version has any predependencies
385 that are not met by 'now' packages. */
386 for (DepIterator D
= Cache
[Pkg
].InstVerIter(Cache
).DependsList();
387 D
.end() == false; D
++)
389 if (D
->Type
== pkgCache::Dep::PreDepends
)
391 // Look for possible ok targets.
392 Version
**VList
= D
.AllTargets();
394 for (Version
**I
= VList
; *I
!= 0 && Bad
== true; I
++)
396 VerIterator
Ver(Cache
,*I
);
397 PkgIterator Pkg
= Ver
.ParentPkg();
399 // See if the current version is ok
400 if (Pkg
.CurrentVer() == Ver
&& List
->IsNow(Pkg
) == true &&
401 Pkg
.State() == PkgIterator::NeedsNothing
)
408 // Look for something that could be configured.
409 for (Version
**I
= VList
; *I
!= 0 && Bad
== true; I
++)
411 VerIterator
Ver(Cache
,*I
);
412 PkgIterator Pkg
= Ver
.ParentPkg();
414 // Not the install version
415 if (Cache
[Pkg
].InstallVer
!= *I
||
416 (Cache
[Pkg
].Keep() == true && Pkg
.State() == PkgIterator::NeedsNothing
))
419 Bad
= !SmartConfigure(Pkg
);
425 return _error
->Error("Internal Error, Couldn't configure a pre-depend");
430 if (D
->Type
== pkgCache::Dep::Conflicts
)
432 /* Look for conflicts. Two packages that are both in the install
433 state cannot conflict so we don't check.. */
434 Version
**VList
= D
.AllTargets();
435 for (Version
**I
= VList
; *I
!= 0; I
++)
437 VerIterator
Ver(Cache
,*I
);
438 PkgIterator Pkg
= Ver
.ParentPkg();
440 // See if the current version is conflicting
441 if (Pkg
.CurrentVer() == Ver
&& List
->IsNow(Pkg
) == true)
443 if (EarlyRemove(Pkg
) == false)
444 return _error
->Error("Internal Error, Could not early remove %s",Pkg
.Name());
451 // Check for reverse conflicts.
452 CheckRConflicts(Pkg
,Pkg
.RevDependsList(),
453 Cache
[Pkg
].InstVerIter(Cache
).VerStr());
454 for (PrvIterator P
= Cache
[Pkg
].InstVerIter(Cache
).ProvidesList();
455 P
.end() == false; P
++)
456 CheckRConflicts(Pkg
,P
.ParentPkg().RevDependsList(),P
.ProvideVersion());
458 if (Install(Pkg
,FileNames
[Pkg
->ID
]) == false)
461 List
->Flag(Pkg
,pkgOrderList::UnPacked
,pkgOrderList::States
);
463 // Perform immedate configuration of the package.
464 if (List
->IsFlag(Pkg
,pkgOrderList::Immediate
) == true)
465 if (SmartConfigure(Pkg
) == false)
466 return _error
->Error("Internal Error, Could not perform immediate configuraton");
471 // PM::OrderInstall - Installation ordering routine /*{{{*/
472 // ---------------------------------------------------------------------
474 bool pkgPackageManager::OrderInstall()
476 if (CreateOrderList() == false)
480 clog
<< "Begining to order" << endl
;
482 if (List
->OrderUnpack() == false)
483 return _error
->Error("Internal ordering error");
486 clog
<< "Done ordering" << endl
;
488 for (pkgOrderList::iterator I
= List
->begin(); I
!= List
->end(); I
++)
490 PkgIterator
Pkg(Cache
,*I
);
493 if (Cache
[Pkg
].Keep() == true && Pkg
.State() == pkgCache::PkgIterator::NeedsNothing
)
494 return _error
->Error("Internal Error, trying to manipulate a kept package");
496 // Perform a delete or an install
497 if (Cache
[Pkg
].Delete() == true)
499 if (SmartRemove(Pkg
) == false)
503 if (SmartUnPack(Pkg
) == false)
507 // Final run through the configure phase
508 if (ConfigureAll() == false)
512 for (pkgOrderList::iterator I
= List
->begin(); I
!= List
->end(); I
++)
513 if (List
->IsFlag(*I
,pkgOrderList::Configured
) == false)
514 return _error
->Error("Internal error, packages left unconfigured. %s",
515 PkgIterator(Cache
,*I
).Name());
520 // PM::DoInstall - Does the installation /*{{{*/
521 // ---------------------------------------------------------------------
522 /* This uses the filenames in FileNames and the information in the
523 DepCache to perform the installation of packages.*/
524 bool pkgPackageManager::DoInstall()
526 return OrderInstall() && Go();