]>
git.saurik.com Git - apt.git/blob - apt-pkg/packagemanager.cc
1 // -*- mode: cpp; mode: fold -*-
3 // $Id: packagemanager.cc,v 1.7 1998/11/22 23:37:05 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 new pkgAcqArchive(Owner
,Sources
,Recs
,Cache
[Pkg
].InstVerIter(Cache
),
75 // PM::FixMissing - Keep all missing packages /*{{{*/
76 // ---------------------------------------------------------------------
77 /* This is called to correct the installation when packages could not
79 bool pkgPackageManager::FixMissing()
81 pkgProblemResolver
Resolve(Cache
);
83 for (PkgIterator I
= Cache
.PkgBegin(); I
.end() == false; I
++)
85 if (Cache
[I
].Keep() == true)
87 if (FileNames
[I
->ID
].empty() == false || Cache
[I
].Delete() == true)
92 // Now downgrade everything that is broken
93 return Resolve
.ResolveByKeep() == true && Cache
.BrokenCount() == 0;
97 // PM::CreateOrderList - Create the ordering class /*{{{*/
98 // ---------------------------------------------------------------------
99 /* This populates the ordering list with all the packages that are
101 bool pkgPackageManager::CreateOrderList()
104 List
= new pkgOrderList(Cache
);
106 // Generate the list of affected packages and sort it
107 for (PkgIterator I
= Cache
.PkgBegin(); I
.end() == false; I
++)
109 // Consider all depends
110 if ((I
->Flags
& pkgCache::Flag::Essential
) == pkgCache::Flag::Essential
)
112 List
->Flag(I
,pkgOrderList::Immediate
);
113 if (Cache
[I
].InstallVer
!= 0)
114 for (DepIterator D
= Cache
[I
].InstVerIter(Cache
).DependsList();
115 D
.end() == false; D
++)
116 if (D
->Type
== pkgCache::Dep::Depends
|| D
->Type
== pkgCache::Dep::PreDepends
)
117 List
->Flag(D
.TargetPkg(),pkgOrderList::Immediate
);
118 if (I
->CurrentVer
!= 0)
119 for (DepIterator D
= I
.CurrentVer().DependsList();
120 D
.end() == false; D
++)
121 if (D
->Type
== pkgCache::Dep::Depends
|| D
->Type
== pkgCache::Dep::PreDepends
)
122 List
->Flag(D
.TargetPkg(),pkgOrderList::Immediate
);
126 if ((Cache
[I
].Keep() == true ||
127 Cache
[I
].InstVerIter(Cache
) == I
.CurrentVer()) &&
128 I
.State() == pkgCache::PkgIterator::NeedsNothing
)
131 // Append it to the list
134 if ((I
->Flags
& pkgCache::Flag::ImmediateConf
) == pkgCache::Flag::ImmediateConf
)
135 List
->Flag(I
,pkgOrderList::Immediate
);
141 // PM::DepAlwaysTrue - Returns true if this dep is irrelevent /*{{{*/
142 // ---------------------------------------------------------------------
143 /* The restriction on provides is to eliminate the case when provides
144 are transitioning between valid states [ie exim to smail] */
145 bool pkgPackageManager::DepAlwaysTrue(DepIterator D
)
147 if (D
.TargetPkg()->ProvidesList
!= 0)
150 if ((Cache
[D
] & pkgDepCache::DepInstall
) != 0 &&
151 (Cache
[D
] & pkgDepCache::DepNow
) != 0)
156 // PM::CheckRConflicts - Look for reverse conflicts /*{{{*/
157 // ---------------------------------------------------------------------
158 /* This looks over the reverses for a conflicts line that needs early
160 bool pkgPackageManager::CheckRConflicts(PkgIterator Pkg
,DepIterator D
,
163 for (;D
.end() == false; D
++)
165 if (D
->Type
!= pkgCache::Dep::Conflicts
)
168 if (D
.ParentPkg() == Pkg
)
171 if (pkgCheckDep(D
.TargetVer(),Ver
,D
->CompareOp
) == false)
174 if (List
->IsNow(Pkg
) == false)
177 if (EarlyRemove(D
.ParentPkg()) == false)
183 // PM::ConfigureAll - Run the all out configuration /*{{{*/
184 // ---------------------------------------------------------------------
185 /* This configures every package. It is assumed they are all unpacked and
186 that the final configuration is valid. */
187 bool pkgPackageManager::ConfigureAll()
189 pkgOrderList
OList(Cache
);
191 // Populate the order list
192 for (pkgOrderList::iterator I
= List
->begin(); I
!= List
->end(); I
++)
193 if (List
->IsFlag(pkgCache::PkgIterator(Cache
,*I
),
194 pkgOrderList::UnPacked
) == true)
197 if (OList
.OrderConfigure() == false)
200 // Perform the configuring
201 for (pkgOrderList::iterator I
= OList
.begin(); I
!= OList
.end(); I
++)
203 PkgIterator
Pkg(Cache
,*I
);
205 if (Configure(Pkg
) == false)
208 List
->Flag(Pkg
,pkgOrderList::Configured
,pkgOrderList::States
);
214 // PM::SmartConfigure - Perform immediate configuration of the pkg /*{{{*/
215 // ---------------------------------------------------------------------
216 /* This routine scheduals the configuration of the given package and all
217 of it's dependents. */
218 bool pkgPackageManager::SmartConfigure(PkgIterator Pkg
)
220 pkgOrderList
OList(Cache
);
222 if (DepAdd(OList
,Pkg
) == false)
225 if (OList
.OrderConfigure() == false)
228 // Perform the configuring
229 for (pkgOrderList::iterator I
= OList
.begin(); I
!= OList
.end(); I
++)
231 PkgIterator
Pkg(Cache
,*I
);
233 if (Configure(Pkg
) == false)
236 List
->Flag(Pkg
,pkgOrderList::Configured
,pkgOrderList::States
);
240 if (List
->IsFlag(Pkg
,pkgOrderList::Configured
) == false)
241 return _error
->Error("Internal error, could not immediate configure %s",Pkg
.Name());
246 // PM::DepAdd - Add all dependents to the oder list /*{{{*/
247 // ---------------------------------------------------------------------
248 /* This recursively adds all dependents to the order list */
249 bool pkgPackageManager::DepAdd(pkgOrderList
&OList
,PkgIterator Pkg
,int Depth
)
251 if (OList
.IsFlag(Pkg
,pkgOrderList::Added
) == true)
253 if (List
->IsFlag(Pkg
,pkgOrderList::Configured
) == true)
255 if (List
->IsFlag(Pkg
,pkgOrderList::UnPacked
) == false)
259 // Put the package on the list
260 OList
.push_back(Pkg
);
261 OList
.Flag(Pkg
,pkgOrderList::Added
);
264 // Check the dependencies to see if they are all satisfied.
266 for (DepIterator D
= Cache
[Pkg
].InstVerIter(Cache
).DependsList(); D
.end() == false;)
268 if (D
->Type
!= pkgCache::Dep::Depends
&& D
->Type
!= pkgCache::Dep::PreDepends
)
276 for (bool LastOR
= true; D
.end() == false && LastOR
== true; D
++)
278 LastOR
= (D
->CompareOp
& pkgCache::Dep::Or
) == pkgCache::Dep::Or
;
283 Version
**VList
= D
.AllTargets();
284 for (Version
**I
= VList
; *I
!= 0 && Bad
== true; I
++)
286 VerIterator
Ver(Cache
,*I
);
287 PkgIterator Pkg
= Ver
.ParentPkg();
289 // See if the current version is ok
290 if (Pkg
.CurrentVer() == Ver
&& List
->IsNow(Pkg
) == true &&
291 Pkg
.State() == PkgIterator::NeedsNothing
)
297 // Not the install version
298 if (Cache
[Pkg
].InstallVer
!= *I
||
299 (Cache
[Pkg
].Keep() == true && Pkg
.State() == PkgIterator::NeedsNothing
))
301 if (List
->IsFlag(Pkg
,pkgOrderList::UnPacked
) == true)
302 Bad
= !DepAdd(OList
,Pkg
,Depth
);
303 if (List
->IsFlag(Pkg
,pkgOrderList::Configured
) == true)
311 OList
.Flag(Pkg
,0,pkgOrderList::Added
);
322 // PM::EarlyRemove - Perform removal of packages before their time /*{{{*/
323 // ---------------------------------------------------------------------
324 /* This is called to deal with conflicts arising from unpacking */
325 bool pkgPackageManager::EarlyRemove(PkgIterator Pkg
)
327 if (List
->IsNow(Pkg
) == false)
330 // Already removed it
331 if (List
->IsFlag(Pkg
,pkgOrderList::Removed
) == true)
334 // Woops, it will not be re-installed!
335 if (List
->IsFlag(Pkg
,pkgOrderList::InList
) == false)
338 bool Res
= SmartRemove(Pkg
);
339 if (Cache
[Pkg
].Delete() == false)
340 List
->Flag(Pkg
,pkgOrderList::Removed
,pkgOrderList::States
);
345 // PM::SmartRemove - Removal Helper /*{{{*/
346 // ---------------------------------------------------------------------
348 bool pkgPackageManager::SmartRemove(PkgIterator Pkg
)
350 if (List
->IsNow(Pkg
) == false)
353 List
->Flag(Pkg
,pkgOrderList::Configured
,pkgOrderList::States
);
357 // PM::SmartUnPack - Install helper /*{{{*/
358 // ---------------------------------------------------------------------
359 /* This performs the task of handling pre-depends. */
360 bool pkgPackageManager::SmartUnPack(PkgIterator Pkg
)
362 // Check if it is already unpacked
363 if (Pkg
.State() == pkgCache::PkgIterator::NeedsConfigure
&&
364 Cache
[Pkg
].Keep() == true)
366 List
->Flag(Pkg
,pkgOrderList::UnPacked
,pkgOrderList::States
);
367 if (List
->IsFlag(Pkg
,pkgOrderList::Immediate
) == true)
368 if (SmartConfigure(Pkg
) == false)
369 return _error
->Error("Internal Error, Could not perform immediate configuraton");
373 /* See if this packages install version has any predependencies
374 that are not met by 'now' packages. */
375 for (DepIterator D
= Cache
[Pkg
].InstVerIter(Cache
).DependsList();
376 D
.end() == false; D
++)
378 if (D
->Type
== pkgCache::Dep::PreDepends
)
380 // Look for possible ok targets.
381 Version
**VList
= D
.AllTargets();
383 for (Version
**I
= VList
; *I
!= 0 && Bad
== true; I
++)
385 VerIterator
Ver(Cache
,*I
);
386 PkgIterator Pkg
= Ver
.ParentPkg();
388 // See if the current version is ok
389 if (Pkg
.CurrentVer() == Ver
&& List
->IsNow(Pkg
) == true &&
390 Pkg
.State() == PkgIterator::NeedsNothing
)
397 // Look for something that could be configured.
398 for (Version
**I
= VList
; *I
!= 0 && Bad
== true; I
++)
400 VerIterator
Ver(Cache
,*I
);
401 PkgIterator Pkg
= Ver
.ParentPkg();
403 // Not the install version
404 if (Cache
[Pkg
].InstallVer
!= *I
||
405 (Cache
[Pkg
].Keep() == true && Pkg
.State() == PkgIterator::NeedsNothing
))
408 Bad
= !SmartConfigure(Pkg
);
414 return _error
->Error("Internal Error, Couldn't configure a pre-depend");
419 if (D
->Type
== pkgCache::Dep::Conflicts
)
421 /* Look for conflicts. Two packages that are both in the install
422 state cannot conflict so we don't check.. */
423 Version
**VList
= D
.AllTargets();
424 for (Version
**I
= VList
; *I
!= 0; I
++)
426 VerIterator
Ver(Cache
,*I
);
427 PkgIterator Pkg
= Ver
.ParentPkg();
429 // See if the current version is conflicting
430 if (Pkg
.CurrentVer() == Ver
&& List
->IsNow(Pkg
) == true)
432 if (EarlyRemove(Pkg
) == false)
433 return _error
->Error("Internal Error, Could not early remove %s",Pkg
.Name());
440 // Check for reverse conflicts.
441 CheckRConflicts(Pkg
,Pkg
.RevDependsList(),
442 Cache
[Pkg
].InstVerIter(Cache
).VerStr());
443 for (PrvIterator P
= Cache
[Pkg
].InstVerIter(Cache
).ProvidesList();
444 P
.end() == false; P
++)
445 CheckRConflicts(Pkg
,P
.ParentPkg().RevDependsList(),P
.ProvideVersion());
447 if (Install(Pkg
,FileNames
[Pkg
->ID
]) == false)
450 List
->Flag(Pkg
,pkgOrderList::UnPacked
,pkgOrderList::States
);
452 // Perform immedate configuration of the package.
453 if (List
->IsFlag(Pkg
,pkgOrderList::Immediate
) == true)
454 if (SmartConfigure(Pkg
) == false)
455 return _error
->Error("Internal Error, Could not perform immediate configuraton");
460 // PM::OrderInstall - Installation ordering routine /*{{{*/
461 // ---------------------------------------------------------------------
463 bool pkgPackageManager::OrderInstall()
465 if (CreateOrderList() == false)
469 clog
<< "Begining to order" << endl
;
471 if (List
->OrderUnpack() == false)
472 return _error
->Error("Internal ordering error");
475 clog
<< "Done ordering" << endl
;
477 for (pkgOrderList::iterator I
= List
->begin(); I
!= List
->end(); I
++)
479 PkgIterator
Pkg(Cache
,*I
);
482 if (Cache
[Pkg
].Keep() == true && Pkg
.State() == pkgCache::PkgIterator::NeedsNothing
)
483 return _error
->Error("Internal Error, trying to manipulate a kept package");
485 // Perform a delete or an install
486 if (Cache
[Pkg
].Delete() == true)
488 if (SmartRemove(Pkg
) == false)
492 if (SmartUnPack(Pkg
) == false)
496 // Final run through the configure phase
497 if (ConfigureAll() == false)
501 for (pkgOrderList::iterator I
= List
->begin(); I
!= List
->end(); I
++)
502 if (List
->IsFlag(*I
,pkgOrderList::Configured
) == false)
503 return _error
->Error("Internal error, packages left unconfigured. %s",
504 PkgIterator(Cache
,*I
).Name());
509 // PM::DoInstall - Does the installation /*{{{*/
510 // ---------------------------------------------------------------------
511 /* This uses the filenames in FileNames and the information in the
512 DepCache to perform the installation of packages.*/
513 bool pkgPackageManager::DoInstall()
515 return OrderInstall() && Go();