Dselect support
[ntk/apt.git] / apt-pkg / packagemanager.cc
CommitLineData
6c139d6e
AL
1// -*- mode: cpp; mode: fold -*-
2// Description /*{{{*/
7a1b1f8b 3// $Id: packagemanager.cc,v 1.7 1998/11/22 23:37:05 jgg Exp $
6c139d6e
AL
4/* ######################################################################
5
6 Package Manager - Abstacts the package manager
7
8 More work is needed in the area of transitioning provides, ie exim
9 replacing smail. This can cause interesing side effects.
10
11 Other cases involving conflicts+replaces should be tested.
12
13 ##################################################################### */
14 /*}}}*/
15// Include Files /*{{{*/
16#ifdef __GNUG__
094a497d 17#pragma implementation "apt-pkg/packagemanager.h"
6c139d6e 18#endif
094a497d
AL
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>
03e39e59 24#include <apt-pkg/acquire-item.h>
30e1eab5
AL
25#include <apt-pkg/algorithms.h>
26#include <apt-pkg/configuration.h>
6c139d6e
AL
27 /*}}}*/
28
29// PM::PackageManager - Constructor /*{{{*/
30// ---------------------------------------------------------------------
31/* */
32pkgPackageManager::pkgPackageManager(pkgDepCache &Cache) : Cache(Cache)
33{
34 FileNames = new string[Cache.Head().PackageCount];
35 List = 0;
30e1eab5 36 Debug = _config->FindB("Debug::pkgPackageManager",false);
6c139d6e
AL
37}
38 /*}}}*/
39// PM::PackageManager - Destructor /*{{{*/
40// ---------------------------------------------------------------------
41/* */
42pkgPackageManager::~pkgPackageManager()
43{
44 delete List;
45 delete [] FileNames;
46}
47 /*}}}*/
03e39e59
AL
48// PM::GetArchives - Queue the archives for download /*{{{*/
49// ---------------------------------------------------------------------
50/* */
51bool pkgPackageManager::GetArchives(pkgAcquire *Owner,pkgSourceList *Sources,
52 pkgRecords *Recs)
53{
7a1b1f8b
AL
54 if (CreateOrderList() == false)
55 return false;
56
57 if (List->OrderUnpack() == false)
58 return _error->Error("Internal ordering error");
59
60 for (pkgOrderList::iterator I = List->begin(); I != List->end(); I++)
61 {
62 PkgIterator Pkg(Cache,*I);
63
64 // Skip packages to erase
65 if (Cache[Pkg].Delete() == true)
03e39e59
AL
66 continue;
67
7a1b1f8b
AL
68 new pkgAcqArchive(Owner,Sources,Recs,Cache[Pkg].InstVerIter(Cache),
69 FileNames[Pkg->ID]);
03e39e59 70 }
7a1b1f8b 71
03e39e59
AL
72 return true;
73}
74 /*}}}*/
6c139d6e
AL
75// PM::FixMissing - Keep all missing packages /*{{{*/
76// ---------------------------------------------------------------------
77/* This is called to correct the installation when packages could not
78 be downloaded. */
79bool pkgPackageManager::FixMissing()
80{
30e1eab5
AL
81 pkgProblemResolver Resolve(Cache);
82
6c139d6e
AL
83 for (PkgIterator I = Cache.PkgBegin(); I.end() == false; I++)
84 {
30e1eab5 85 if (Cache[I].Keep() == true)
6c139d6e
AL
86 continue;
87 if (FileNames[I->ID].empty() == false || Cache[I].Delete() == true)
88 continue;
89 Cache.MarkKeep(I);
90 }
91
92 // Now downgrade everything that is broken
30e1eab5 93 return Resolve.ResolveByKeep() == true && Cache.BrokenCount() == 0;
6c139d6e
AL
94}
95 /*}}}*/
96
7a1b1f8b
AL
97// PM::CreateOrderList - Create the ordering class /*{{{*/
98// ---------------------------------------------------------------------
99/* This populates the ordering list with all the packages that are
100 going to change. */
101bool pkgPackageManager::CreateOrderList()
102{
103 delete List;
104 List = new pkgOrderList(Cache);
105
106 // Generate the list of affected packages and sort it
107 for (PkgIterator I = Cache.PkgBegin(); I.end() == false; I++)
108 {
109 // Consider all depends
110 if ((I->Flags & pkgCache::Flag::Essential) == pkgCache::Flag::Essential)
111 {
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);
123 }
124
125 // Not interesting
126 if ((Cache[I].Keep() == true ||
127 Cache[I].InstVerIter(Cache) == I.CurrentVer()) &&
128 I.State() == pkgCache::PkgIterator::NeedsNothing)
129 continue;
130
131 // Append it to the list
132 List->push_back(I);
133
134 if ((I->Flags & pkgCache::Flag::ImmediateConf) == pkgCache::Flag::ImmediateConf)
135 List->Flag(I,pkgOrderList::Immediate);
136 }
137
138 return true;
139}
140 /*}}}*/
6c139d6e
AL
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] */
145bool pkgPackageManager::DepAlwaysTrue(DepIterator D)
146{
147 if (D.TargetPkg()->ProvidesList != 0)
148 return false;
149
150 if ((Cache[D] & pkgDepCache::DepInstall) != 0 &&
151 (Cache[D] & pkgDepCache::DepNow) != 0)
152 return true;
153 return false;
154}
155 /*}}}*/
156// PM::CheckRConflicts - Look for reverse conflicts /*{{{*/
157// ---------------------------------------------------------------------
158/* This looks over the reverses for a conflicts line that needs early
159 removal. */
160bool pkgPackageManager::CheckRConflicts(PkgIterator Pkg,DepIterator D,
161 const char *Ver)
162{
163 for (;D.end() == false; D++)
164 {
b50b2c97 165 if (D->Type != pkgCache::Dep::Conflicts)
6c139d6e
AL
166 continue;
167
168 if (D.ParentPkg() == Pkg)
169 continue;
170
171 if (pkgCheckDep(D.TargetVer(),Ver,D->CompareOp) == false)
172 continue;
173
174 if (List->IsNow(Pkg) == false)
175 continue;
176
177 if (EarlyRemove(D.ParentPkg()) == false)
178 return false;
179 }
180 return true;
181}
182 /*}}}*/
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. */
187bool pkgPackageManager::ConfigureAll()
188{
189 pkgOrderList OList(Cache);
190
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)
195 OList.push_back(*I);
196
197 if (OList.OrderConfigure() == false)
198 return false;
199
200 // Perform the configuring
201 for (pkgOrderList::iterator I = OList.begin(); I != OList.end(); I++)
202 {
203 PkgIterator Pkg(Cache,*I);
204
205 if (Configure(Pkg) == false)
206 return false;
207
208 List->Flag(Pkg,pkgOrderList::Configured,pkgOrderList::States);
209 }
210
211 return true;
212}
213 /*}}}*/
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. */
218bool pkgPackageManager::SmartConfigure(PkgIterator Pkg)
219{
220 pkgOrderList OList(Cache);
221
222 if (DepAdd(OList,Pkg) == false)
223 return false;
224
225 if (OList.OrderConfigure() == false)
226 return false;
227
228 // Perform the configuring
229 for (pkgOrderList::iterator I = OList.begin(); I != OList.end(); I++)
230 {
231 PkgIterator Pkg(Cache,*I);
232
233 if (Configure(Pkg) == false)
234 return false;
235
236 List->Flag(Pkg,pkgOrderList::Configured,pkgOrderList::States);
237 }
238
239 // Sanity Check
240 if (List->IsFlag(Pkg,pkgOrderList::Configured) == false)
241 return _error->Error("Internal error, could not immediate configure %s",Pkg.Name());
242
243 return true;
244}
245 /*}}}*/
246// PM::DepAdd - Add all dependents to the oder list /*{{{*/
247// ---------------------------------------------------------------------
248/* This recursively adds all dependents to the order list */
249bool pkgPackageManager::DepAdd(pkgOrderList &OList,PkgIterator Pkg,int Depth)
250{
251 if (OList.IsFlag(Pkg,pkgOrderList::Added) == true)
252 return true;
253 if (List->IsFlag(Pkg,pkgOrderList::Configured) == true)
254 return true;
255 if (List->IsFlag(Pkg,pkgOrderList::UnPacked) == false)
256 return false;
257
258
259 // Put the package on the list
260 OList.push_back(Pkg);
261 OList.Flag(Pkg,pkgOrderList::Added);
262 Depth++;
263
264 // Check the dependencies to see if they are all satisfied.
265 bool Bad = false;
266 for (DepIterator D = Cache[Pkg].InstVerIter(Cache).DependsList(); D.end() == false;)
267 {
b50b2c97 268 if (D->Type != pkgCache::Dep::Depends && D->Type != pkgCache::Dep::PreDepends)
6c139d6e
AL
269 {
270 D++;
271 continue;
272 }
273
274 // Grok or groups
275 Bad = true;
276 for (bool LastOR = true; D.end() == false && LastOR == true; D++)
277 {
b50b2c97 278 LastOR = (D->CompareOp & pkgCache::Dep::Or) == pkgCache::Dep::Or;
6c139d6e
AL
279
280 if (Bad == false)
281 continue;
282
283 Version **VList = D.AllTargets();
284 for (Version **I = VList; *I != 0 && Bad == true; I++)
285 {
286 VerIterator Ver(Cache,*I);
287 PkgIterator Pkg = Ver.ParentPkg();
288
289 // See if the current version is ok
290 if (Pkg.CurrentVer() == Ver && List->IsNow(Pkg) == true &&
291 Pkg.State() == PkgIterator::NeedsNothing)
292 {
293 Bad = false;
294 continue;
295 }
296
297 // Not the install version
298 if (Cache[Pkg].InstallVer != *I ||
299 (Cache[Pkg].Keep() == true && Pkg.State() == PkgIterator::NeedsNothing))
300 continue;
301 if (List->IsFlag(Pkg,pkgOrderList::UnPacked) == true)
302 Bad = !DepAdd(OList,Pkg,Depth);
303 if (List->IsFlag(Pkg,pkgOrderList::Configured) == true)
304 Bad = false;
305 }
306 delete [] VList;
307 }
308
309 if (Bad == true)
310 {
311 OList.Flag(Pkg,0,pkgOrderList::Added);
312 OList.pop_back();
313 Depth--;
314 return false;
315 }
316 }
317
318 Depth--;
319 return true;
320}
321 /*}}}*/
322// PM::EarlyRemove - Perform removal of packages before their time /*{{{*/
323// ---------------------------------------------------------------------
324/* This is called to deal with conflicts arising from unpacking */
325bool pkgPackageManager::EarlyRemove(PkgIterator Pkg)
326{
327 if (List->IsNow(Pkg) == false)
328 return true;
329
330 // Already removed it
331 if (List->IsFlag(Pkg,pkgOrderList::Removed) == true)
332 return true;
333
334 // Woops, it will not be re-installed!
335 if (List->IsFlag(Pkg,pkgOrderList::InList) == false)
336 return false;
337
338 bool Res = SmartRemove(Pkg);
339 if (Cache[Pkg].Delete() == false)
340 List->Flag(Pkg,pkgOrderList::Removed,pkgOrderList::States);
341
342 return Res;
343}
344 /*}}}*/
345// PM::SmartRemove - Removal Helper /*{{{*/
346// ---------------------------------------------------------------------
347/* */
348bool pkgPackageManager::SmartRemove(PkgIterator Pkg)
349{
350 if (List->IsNow(Pkg) == false)
351 return true;
352
353 List->Flag(Pkg,pkgOrderList::Configured,pkgOrderList::States);
354 return Remove(Pkg);
355}
356 /*}}}*/
357// PM::SmartUnPack - Install helper /*{{{*/
358// ---------------------------------------------------------------------
359/* This performs the task of handling pre-depends. */
360bool pkgPackageManager::SmartUnPack(PkgIterator Pkg)
361{
362 // Check if it is already unpacked
363 if (Pkg.State() == pkgCache::PkgIterator::NeedsConfigure &&
364 Cache[Pkg].Keep() == true)
365 {
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");
370 return true;
371 }
372
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++)
377 {
b50b2c97 378 if (D->Type == pkgCache::Dep::PreDepends)
6c139d6e
AL
379 {
380 // Look for possible ok targets.
381 Version **VList = D.AllTargets();
382 bool Bad = true;
383 for (Version **I = VList; *I != 0 && Bad == true; I++)
384 {
385 VerIterator Ver(Cache,*I);
386 PkgIterator Pkg = Ver.ParentPkg();
387
388 // See if the current version is ok
389 if (Pkg.CurrentVer() == Ver && List->IsNow(Pkg) == true &&
390 Pkg.State() == PkgIterator::NeedsNothing)
391 {
392 Bad = false;
393 continue;
394 }
395 }
396
397 // Look for something that could be configured.
398 for (Version **I = VList; *I != 0 && Bad == true; I++)
399 {
400 VerIterator Ver(Cache,*I);
401 PkgIterator Pkg = Ver.ParentPkg();
402
403 // Not the install version
404 if (Cache[Pkg].InstallVer != *I ||
405 (Cache[Pkg].Keep() == true && Pkg.State() == PkgIterator::NeedsNothing))
406 continue;
407
408 Bad = !SmartConfigure(Pkg);
409 }
410
411 delete [] VList;
412
413 if (Bad == true)
414 return _error->Error("Internal Error, Couldn't configure a pre-depend");
415
416 continue;
417 }
418
b50b2c97 419 if (D->Type == pkgCache::Dep::Conflicts)
6c139d6e
AL
420 {
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++)
425 {
426 VerIterator Ver(Cache,*I);
427 PkgIterator Pkg = Ver.ParentPkg();
428
429 // See if the current version is conflicting
430 if (Pkg.CurrentVer() == Ver && List->IsNow(Pkg) == true)
431 {
432 if (EarlyRemove(Pkg) == false)
433 return _error->Error("Internal Error, Could not early remove %s",Pkg.Name());
434 }
435 }
436 delete [] VList;
437 }
438 }
439
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());
446
447 if (Install(Pkg,FileNames[Pkg->ID]) == false)
448 return false;
449
450 List->Flag(Pkg,pkgOrderList::UnPacked,pkgOrderList::States);
451
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");
456
457 return true;
458}
459 /*}}}*/
460// PM::OrderInstall - Installation ordering routine /*{{{*/
461// ---------------------------------------------------------------------
462/* */
463bool pkgPackageManager::OrderInstall()
464{
7a1b1f8b
AL
465 if (CreateOrderList() == false)
466 return false;
6c139d6e 467
30e1eab5
AL
468 if (Debug == true)
469 clog << "Begining to order" << endl;
6c139d6e
AL
470
471 if (List->OrderUnpack() == false)
472 return _error->Error("Internal ordering error");
473
30e1eab5
AL
474 if (Debug == true)
475 clog << "Done ordering" << endl;
476
6c139d6e
AL
477 for (pkgOrderList::iterator I = List->begin(); I != List->end(); I++)
478 {
479 PkgIterator Pkg(Cache,*I);
480
481 // Sanity check
482 if (Cache[Pkg].Keep() == true && Pkg.State() == pkgCache::PkgIterator::NeedsNothing)
483 return _error->Error("Internal Error, trying to manipulate a kept package");
484
485 // Perform a delete or an install
486 if (Cache[Pkg].Delete() == true)
487 {
488 if (SmartRemove(Pkg) == false)
489 return false;
490 }
491 else
492 if (SmartUnPack(Pkg) == false)
493 return false;
494 }
495
496 // Final run through the configure phase
497 if (ConfigureAll() == false)
498 return false;
499
500 // Sanity check
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());
505
506 return true;
507}
508 /*}}}*/
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.*/
513bool pkgPackageManager::DoInstall()
514{
515 return OrderInstall() && Go();
516}
517 /*}}}*/