]> git.saurik.com Git - apt.git/blame - apt-pkg/packagemanager.cc
More portability thingies
[apt.git] / apt-pkg / packagemanager.cc
CommitLineData
6c139d6e
AL
1// -*- mode: cpp; mode: fold -*-
2// Description /*{{{*/
a3eaf954 3// $Id: packagemanager.cc,v 1.23 1999/12/09 05:22:33 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);
281daf46
AL
63 FileNames[Pkg->ID] = string();
64
7a1b1f8b
AL
65 // Skip packages to erase
66 if (Cache[Pkg].Delete() == true)
03e39e59 67 continue;
d38b7b3d
AL
68
69 // Skip Packages that need configure only.
9dbb421f
AL
70 if (Pkg.State() == pkgCache::PkgIterator::NeedsConfigure &&
71 Cache[Pkg].Keep() == true)
d38b7b3d 72 continue;
281daf46
AL
73
74 // Skip already processed packages
75 if (List->IsNow(Pkg) == false)
76 continue;
77
7a1b1f8b
AL
78 new pkgAcqArchive(Owner,Sources,Recs,Cache[Pkg].InstVerIter(Cache),
79 FileNames[Pkg->ID]);
03e39e59 80 }
7a1b1f8b 81
03e39e59
AL
82 return true;
83}
84 /*}}}*/
6c139d6e
AL
85// PM::FixMissing - Keep all missing packages /*{{{*/
86// ---------------------------------------------------------------------
87/* This is called to correct the installation when packages could not
88 be downloaded. */
89bool pkgPackageManager::FixMissing()
bdae53f1 90{
30e1eab5 91 pkgProblemResolver Resolve(Cache);
2fd65468
AL
92 List->SetFileList(FileNames);
93
9dbb421f 94 bool Bad = false;
6c139d6e
AL
95 for (PkgIterator I = Cache.PkgBegin(); I.end() == false; I++)
96 {
2fd65468 97 if (List->IsMissing(I) == false)
9dbb421f 98 continue;
2fd65468 99
9dbb421f
AL
100 // Okay, this file is missing and we need it. Mark it for keep
101 Bad = true;
6c139d6e
AL
102 Cache.MarkKeep(I);
103 }
bdae53f1
AL
104
105 // We have to empty the list otherwise it will not have the new changes
106 delete List;
107 List = 0;
6c139d6e 108
9dbb421f
AL
109 if (Bad == false)
110 return true;
111
6c139d6e 112 // Now downgrade everything that is broken
30e1eab5 113 return Resolve.ResolveByKeep() == true && Cache.BrokenCount() == 0;
6c139d6e
AL
114}
115 /*}}}*/
116
7a1b1f8b
AL
117// PM::CreateOrderList - Create the ordering class /*{{{*/
118// ---------------------------------------------------------------------
119/* This populates the ordering list with all the packages that are
120 going to change. */
121bool pkgPackageManager::CreateOrderList()
122{
281daf46
AL
123 if (List != 0)
124 return true;
125
7a1b1f8b
AL
126 delete List;
127 List = new pkgOrderList(Cache);
128
b780bc1b 129 bool NoImmConfigure = _config->FindB("APT::Immediate-Configure",false);
079cc404 130
7a1b1f8b
AL
131 // Generate the list of affected packages and sort it
132 for (PkgIterator I = Cache.PkgBegin(); I.end() == false; I++)
133 {
138d4b3d
AL
134 // Mark the package and its dependends for immediate configuration
135 if (((I->Flags & pkgCache::Flag::Essential) == pkgCache::Flag::Essential ||
136 (I->Flags & pkgCache::Flag::Important) == pkgCache::Flag::Important) &&
079cc404 137 NoImmConfigure == false)
7a1b1f8b
AL
138 {
139 List->Flag(I,pkgOrderList::Immediate);
d38b7b3d
AL
140
141 // Look for other packages to make immediate configurea
7a1b1f8b
AL
142 if (Cache[I].InstallVer != 0)
143 for (DepIterator D = Cache[I].InstVerIter(Cache).DependsList();
144 D.end() == false; D++)
145 if (D->Type == pkgCache::Dep::Depends || D->Type == pkgCache::Dep::PreDepends)
146 List->Flag(D.TargetPkg(),pkgOrderList::Immediate);
d38b7b3d
AL
147
148 // And again with the current version.
7a1b1f8b
AL
149 if (I->CurrentVer != 0)
150 for (DepIterator D = I.CurrentVer().DependsList();
151 D.end() == false; D++)
152 if (D->Type == pkgCache::Dep::Depends || D->Type == pkgCache::Dep::PreDepends)
153 List->Flag(D.TargetPkg(),pkgOrderList::Immediate);
154 }
155
156 // Not interesting
157 if ((Cache[I].Keep() == true ||
158 Cache[I].InstVerIter(Cache) == I.CurrentVer()) &&
d556d1a1 159 I.State() == pkgCache::PkgIterator::NeedsNothing &&
d0c59649 160 (Cache[I].iFlags & pkgDepCache::ReInstall) != pkgDepCache::ReInstall &&
d556d1a1
AL
161 (I.Purge() != false || Cache[I].Mode != pkgDepCache::ModeDelete ||
162 (Cache[I].iFlags & pkgDepCache::Purge) != pkgDepCache::Purge))
7a1b1f8b
AL
163 continue;
164
165 // Append it to the list
138d4b3d 166 List->push_back(I);
7a1b1f8b
AL
167 }
168
169 return true;
170}
171 /*}}}*/
6c139d6e
AL
172// PM::DepAlwaysTrue - Returns true if this dep is irrelevent /*{{{*/
173// ---------------------------------------------------------------------
174/* The restriction on provides is to eliminate the case when provides
175 are transitioning between valid states [ie exim to smail] */
176bool pkgPackageManager::DepAlwaysTrue(DepIterator D)
177{
178 if (D.TargetPkg()->ProvidesList != 0)
179 return false;
180
181 if ((Cache[D] & pkgDepCache::DepInstall) != 0 &&
182 (Cache[D] & pkgDepCache::DepNow) != 0)
183 return true;
184 return false;
185}
186 /*}}}*/
187// PM::CheckRConflicts - Look for reverse conflicts /*{{{*/
188// ---------------------------------------------------------------------
189/* This looks over the reverses for a conflicts line that needs early
190 removal. */
191bool pkgPackageManager::CheckRConflicts(PkgIterator Pkg,DepIterator D,
192 const char *Ver)
193{
194 for (;D.end() == false; D++)
195 {
b50b2c97 196 if (D->Type != pkgCache::Dep::Conflicts)
6c139d6e
AL
197 continue;
198
199 if (D.ParentPkg() == Pkg)
200 continue;
201
202 if (pkgCheckDep(D.TargetVer(),Ver,D->CompareOp) == false)
203 continue;
204
205 if (List->IsNow(Pkg) == false)
206 continue;
207
208 if (EarlyRemove(D.ParentPkg()) == false)
209 return false;
210 }
211 return true;
212}
213 /*}}}*/
214// PM::ConfigureAll - Run the all out configuration /*{{{*/
215// ---------------------------------------------------------------------
216/* This configures every package. It is assumed they are all unpacked and
217 that the final configuration is valid. */
218bool pkgPackageManager::ConfigureAll()
219{
220 pkgOrderList OList(Cache);
221
222 // Populate the order list
223 for (pkgOrderList::iterator I = List->begin(); I != List->end(); I++)
224 if (List->IsFlag(pkgCache::PkgIterator(Cache,*I),
225 pkgOrderList::UnPacked) == true)
226 OList.push_back(*I);
227
228 if (OList.OrderConfigure() == false)
229 return false;
230
231 // Perform the configuring
232 for (pkgOrderList::iterator I = OList.begin(); I != OList.end(); I++)
233 {
234 PkgIterator Pkg(Cache,*I);
235
236 if (Configure(Pkg) == false)
237 return false;
238
239 List->Flag(Pkg,pkgOrderList::Configured,pkgOrderList::States);
240 }
241
242 return true;
243}
244 /*}}}*/
245// PM::SmartConfigure - Perform immediate configuration of the pkg /*{{{*/
246// ---------------------------------------------------------------------
247/* This routine scheduals the configuration of the given package and all
248 of it's dependents. */
249bool pkgPackageManager::SmartConfigure(PkgIterator Pkg)
250{
251 pkgOrderList OList(Cache);
252
253 if (DepAdd(OList,Pkg) == false)
254 return false;
255
256 if (OList.OrderConfigure() == false)
257 return false;
258
259 // Perform the configuring
260 for (pkgOrderList::iterator I = OList.begin(); I != OList.end(); I++)
261 {
262 PkgIterator Pkg(Cache,*I);
263
264 if (Configure(Pkg) == false)
265 return false;
266
267 List->Flag(Pkg,pkgOrderList::Configured,pkgOrderList::States);
268 }
269
270 // Sanity Check
271 if (List->IsFlag(Pkg,pkgOrderList::Configured) == false)
272 return _error->Error("Internal error, could not immediate configure %s",Pkg.Name());
273
274 return true;
275}
276 /*}}}*/
277// PM::DepAdd - Add all dependents to the oder list /*{{{*/
278// ---------------------------------------------------------------------
279/* This recursively adds all dependents to the order list */
280bool pkgPackageManager::DepAdd(pkgOrderList &OList,PkgIterator Pkg,int Depth)
281{
282 if (OList.IsFlag(Pkg,pkgOrderList::Added) == true)
283 return true;
284 if (List->IsFlag(Pkg,pkgOrderList::Configured) == true)
285 return true;
286 if (List->IsFlag(Pkg,pkgOrderList::UnPacked) == false)
287 return false;
288
289
290 // Put the package on the list
291 OList.push_back(Pkg);
292 OList.Flag(Pkg,pkgOrderList::Added);
293 Depth++;
294
295 // Check the dependencies to see if they are all satisfied.
296 bool Bad = false;
297 for (DepIterator D = Cache[Pkg].InstVerIter(Cache).DependsList(); D.end() == false;)
298 {
b50b2c97 299 if (D->Type != pkgCache::Dep::Depends && D->Type != pkgCache::Dep::PreDepends)
6c139d6e
AL
300 {
301 D++;
302 continue;
303 }
304
305 // Grok or groups
306 Bad = true;
307 for (bool LastOR = true; D.end() == false && LastOR == true; D++)
308 {
b50b2c97 309 LastOR = (D->CompareOp & pkgCache::Dep::Or) == pkgCache::Dep::Or;
6c139d6e
AL
310
311 if (Bad == false)
312 continue;
313
314 Version **VList = D.AllTargets();
315 for (Version **I = VList; *I != 0 && Bad == true; I++)
316 {
317 VerIterator Ver(Cache,*I);
318 PkgIterator Pkg = Ver.ParentPkg();
319
320 // See if the current version is ok
321 if (Pkg.CurrentVer() == Ver && List->IsNow(Pkg) == true &&
322 Pkg.State() == PkgIterator::NeedsNothing)
323 {
324 Bad = false;
325 continue;
326 }
327
328 // Not the install version
329 if (Cache[Pkg].InstallVer != *I ||
330 (Cache[Pkg].Keep() == true && Pkg.State() == PkgIterator::NeedsNothing))
331 continue;
332 if (List->IsFlag(Pkg,pkgOrderList::UnPacked) == true)
333 Bad = !DepAdd(OList,Pkg,Depth);
334 if (List->IsFlag(Pkg,pkgOrderList::Configured) == true)
335 Bad = false;
336 }
337 delete [] VList;
338 }
339
340 if (Bad == true)
341 {
342 OList.Flag(Pkg,0,pkgOrderList::Added);
343 OList.pop_back();
344 Depth--;
345 return false;
346 }
347 }
348
349 Depth--;
350 return true;
351}
352 /*}}}*/
353// PM::EarlyRemove - Perform removal of packages before their time /*{{{*/
354// ---------------------------------------------------------------------
355/* This is called to deal with conflicts arising from unpacking */
356bool pkgPackageManager::EarlyRemove(PkgIterator Pkg)
357{
358 if (List->IsNow(Pkg) == false)
359 return true;
360
361 // Already removed it
362 if (List->IsFlag(Pkg,pkgOrderList::Removed) == true)
363 return true;
364
365 // Woops, it will not be re-installed!
366 if (List->IsFlag(Pkg,pkgOrderList::InList) == false)
367 return false;
9d4c8f67
AL
368
369 // Essential packages get special treatment
370 if ((Pkg->Flags & pkgCache::Flag::Essential) != 0)
371 {
372 if (_config->FindB("APT::Force-LoopBreak",false) == false)
373 return _error->Error("This installation run will require temporarily "
374 "removing the essential package %s due to a "
375 "Conflicts/Pre-Depends loop. This is often bad, "
376 "but if you really want to do it, activate the "
377 "APT::Force-LoopBreak option.",Pkg.Name());
378 }
6c139d6e
AL
379
380 bool Res = SmartRemove(Pkg);
381 if (Cache[Pkg].Delete() == false)
382 List->Flag(Pkg,pkgOrderList::Removed,pkgOrderList::States);
383
384 return Res;
385}
386 /*}}}*/
387// PM::SmartRemove - Removal Helper /*{{{*/
388// ---------------------------------------------------------------------
389/* */
390bool pkgPackageManager::SmartRemove(PkgIterator Pkg)
391{
392 if (List->IsNow(Pkg) == false)
393 return true;
394
395 List->Flag(Pkg,pkgOrderList::Configured,pkgOrderList::States);
fc4b5c9f 396 return Remove(Pkg,(Cache[Pkg].iFlags & pkgDepCache::Purge) == pkgDepCache::Purge);
6c139d6e
AL
397}
398 /*}}}*/
399// PM::SmartUnPack - Install helper /*{{{*/
400// ---------------------------------------------------------------------
401/* This performs the task of handling pre-depends. */
402bool pkgPackageManager::SmartUnPack(PkgIterator Pkg)
403{
404 // Check if it is already unpacked
405 if (Pkg.State() == pkgCache::PkgIterator::NeedsConfigure &&
406 Cache[Pkg].Keep() == true)
407 {
408 List->Flag(Pkg,pkgOrderList::UnPacked,pkgOrderList::States);
409 if (List->IsFlag(Pkg,pkgOrderList::Immediate) == true)
410 if (SmartConfigure(Pkg) == false)
411 return _error->Error("Internal Error, Could not perform immediate configuraton");
412 return true;
413 }
981d20eb 414
6c139d6e
AL
415 /* See if this packages install version has any predependencies
416 that are not met by 'now' packages. */
417 for (DepIterator D = Cache[Pkg].InstVerIter(Cache).DependsList();
421c8d10 418 D.end() == false; )
6c139d6e 419 {
421c8d10
AL
420 // Compute a single dependency element (glob or)
421 pkgCache::DepIterator Start;
422 pkgCache::DepIterator End;
423 D.GlobOr(Start,End);
424
425 while (End->Type == pkgCache::Dep::PreDepends)
6c139d6e
AL
426 {
427 // Look for possible ok targets.
421c8d10 428 Version **VList = Start.AllTargets();
6c139d6e
AL
429 bool Bad = true;
430 for (Version **I = VList; *I != 0 && Bad == true; I++)
431 {
432 VerIterator Ver(Cache,*I);
433 PkgIterator Pkg = Ver.ParentPkg();
434
435 // See if the current version is ok
436 if (Pkg.CurrentVer() == Ver && List->IsNow(Pkg) == true &&
437 Pkg.State() == PkgIterator::NeedsNothing)
438 {
439 Bad = false;
440 continue;
441 }
442 }
443
444 // Look for something that could be configured.
445 for (Version **I = VList; *I != 0 && Bad == true; I++)
446 {
447 VerIterator Ver(Cache,*I);
448 PkgIterator Pkg = Ver.ParentPkg();
449
450 // Not the install version
451 if (Cache[Pkg].InstallVer != *I ||
452 (Cache[Pkg].Keep() == true && Pkg.State() == PkgIterator::NeedsNothing))
453 continue;
454
455 Bad = !SmartConfigure(Pkg);
456 }
457
458 delete [] VList;
6c139d6e 459
421c8d10
AL
460 /* If this or element did not match then continue on to the
461 next or element until a matching element is found*/
462 if (Bad == true)
463 {
464 if (Start == End)
465 return _error->Error("Internal Error, Couldn't configure a pre-depend");
466 Start++;
467 }
468 else
469 break;
6c139d6e
AL
470 }
471
421c8d10 472 if (End->Type == pkgCache::Dep::Conflicts)
6c139d6e
AL
473 {
474 /* Look for conflicts. Two packages that are both in the install
475 state cannot conflict so we don't check.. */
421c8d10 476 Version **VList = End.AllTargets();
6c139d6e
AL
477 for (Version **I = VList; *I != 0; I++)
478 {
479 VerIterator Ver(Cache,*I);
480 PkgIterator Pkg = Ver.ParentPkg();
481
482 // See if the current version is conflicting
483 if (Pkg.CurrentVer() == Ver && List->IsNow(Pkg) == true)
484 {
485 if (EarlyRemove(Pkg) == false)
486 return _error->Error("Internal Error, Could not early remove %s",Pkg.Name());
487 }
488 }
489 delete [] VList;
490 }
491 }
492
493 // Check for reverse conflicts.
494 CheckRConflicts(Pkg,Pkg.RevDependsList(),
495 Cache[Pkg].InstVerIter(Cache).VerStr());
496 for (PrvIterator P = Cache[Pkg].InstVerIter(Cache).ProvidesList();
497 P.end() == false; P++)
498 CheckRConflicts(Pkg,P.ParentPkg().RevDependsList(),P.ProvideVersion());
499
500 if (Install(Pkg,FileNames[Pkg->ID]) == false)
501 return false;
502
503 List->Flag(Pkg,pkgOrderList::UnPacked,pkgOrderList::States);
504
505 // Perform immedate configuration of the package.
506 if (List->IsFlag(Pkg,pkgOrderList::Immediate) == true)
507 if (SmartConfigure(Pkg) == false)
508 return _error->Error("Internal Error, Could not perform immediate configuraton");
509
510 return true;
511}
512 /*}}}*/
513// PM::OrderInstall - Installation ordering routine /*{{{*/
514// ---------------------------------------------------------------------
515/* */
281daf46 516pkgPackageManager::OrderResult pkgPackageManager::OrderInstall()
6c139d6e 517{
7a1b1f8b 518 if (CreateOrderList() == false)
281daf46
AL
519 return Failed;
520
521 Reset();
6c139d6e 522
30e1eab5
AL
523 if (Debug == true)
524 clog << "Begining to order" << endl;
6c139d6e 525
281daf46
AL
526 if (List->OrderUnpack(FileNames) == false)
527 {
528 _error->Error("Internal ordering error");
529 return Failed;
530 }
531
30e1eab5
AL
532 if (Debug == true)
533 clog << "Done ordering" << endl;
534
281daf46 535 bool DoneSomething = false;
6c139d6e
AL
536 for (pkgOrderList::iterator I = List->begin(); I != List->end(); I++)
537 {
538 PkgIterator Pkg(Cache,*I);
281daf46
AL
539
540 if (List->IsNow(Pkg) == false)
541 {
542 if (Debug == true)
543 clog << "Skipping already done " << Pkg.Name() << endl;
544 continue;
545 }
546
2fd65468 547 if (List->IsMissing(Pkg) == true)
281daf46
AL
548 {
549 if (Debug == true)
a3eaf954 550 clog << "Sequence completed at " << Pkg.Name() << endl;
281daf46
AL
551 if (DoneSomething == false)
552 {
553 _error->Error("Internal Error, ordering was unable to handle the media swap");
554 return Failed;
555 }
556 return Incomplete;
557 }
6c139d6e
AL
558
559 // Sanity check
d0c59649
AL
560 if (Cache[Pkg].Keep() == true &&
561 Pkg.State() == pkgCache::PkgIterator::NeedsNothing &&
562 (Cache[Pkg].iFlags & pkgDepCache::ReInstall) != pkgDepCache::ReInstall)
281daf46
AL
563 {
564 _error->Error("Internal Error, trying to manipulate a kept package");
565 return Failed;
566 }
6c139d6e
AL
567
568 // Perform a delete or an install
569 if (Cache[Pkg].Delete() == true)
570 {
571 if (SmartRemove(Pkg) == false)
281daf46 572 return Failed;
6c139d6e
AL
573 }
574 else
575 if (SmartUnPack(Pkg) == false)
281daf46
AL
576 return Failed;
577 DoneSomething = true;
6c139d6e
AL
578 }
579
580 // Final run through the configure phase
581 if (ConfigureAll() == false)
281daf46 582 return Failed;
6c139d6e
AL
583
584 // Sanity check
585 for (pkgOrderList::iterator I = List->begin(); I != List->end(); I++)
281daf46 586 {
6c139d6e 587 if (List->IsFlag(*I,pkgOrderList::Configured) == false)
281daf46
AL
588 {
589 _error->Error("Internal error, packages left unconfigured. %s",
590 PkgIterator(Cache,*I).Name());
591 return Failed;
592 }
593 }
594
595 return Completed;
6c139d6e
AL
596}
597 /*}}}*/
598// PM::DoInstall - Does the installation /*{{{*/
599// ---------------------------------------------------------------------
600/* This uses the filenames in FileNames and the information in the
601 DepCache to perform the installation of packages.*/
281daf46 602pkgPackageManager::OrderResult pkgPackageManager::DoInstall()
6c139d6e 603{
281daf46
AL
604 OrderResult Res = OrderInstall();
605 if (Res != Failed)
606 if (Go() == false)
607 return Failed;
608 return Res;
6c139d6e
AL
609}
610 /*}}}*/