Random fixes..
[ntk/apt.git] / apt-pkg / packagemanager.cc
CommitLineData
6c139d6e
AL
1// -*- mode: cpp; mode: fold -*-
2// Description /*{{{*/
bdae53f1 3// $Id: packagemanager.cc,v 1.19 1999/08/03 05:19:41 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
AL
159 I.State() == pkgCache::PkgIterator::NeedsNothing &&
160 (I.Purge() != false || Cache[I].Mode != pkgDepCache::ModeDelete ||
161 (Cache[I].iFlags & pkgDepCache::Purge) != pkgDepCache::Purge))
7a1b1f8b
AL
162 continue;
163
164 // Append it to the list
138d4b3d 165 List->push_back(I);
7a1b1f8b
AL
166 }
167
168 return true;
169}
170 /*}}}*/
6c139d6e
AL
171// PM::DepAlwaysTrue - Returns true if this dep is irrelevent /*{{{*/
172// ---------------------------------------------------------------------
173/* The restriction on provides is to eliminate the case when provides
174 are transitioning between valid states [ie exim to smail] */
175bool pkgPackageManager::DepAlwaysTrue(DepIterator D)
176{
177 if (D.TargetPkg()->ProvidesList != 0)
178 return false;
179
180 if ((Cache[D] & pkgDepCache::DepInstall) != 0 &&
181 (Cache[D] & pkgDepCache::DepNow) != 0)
182 return true;
183 return false;
184}
185 /*}}}*/
186// PM::CheckRConflicts - Look for reverse conflicts /*{{{*/
187// ---------------------------------------------------------------------
188/* This looks over the reverses for a conflicts line that needs early
189 removal. */
190bool pkgPackageManager::CheckRConflicts(PkgIterator Pkg,DepIterator D,
191 const char *Ver)
192{
193 for (;D.end() == false; D++)
194 {
b50b2c97 195 if (D->Type != pkgCache::Dep::Conflicts)
6c139d6e
AL
196 continue;
197
198 if (D.ParentPkg() == Pkg)
199 continue;
200
201 if (pkgCheckDep(D.TargetVer(),Ver,D->CompareOp) == false)
202 continue;
203
204 if (List->IsNow(Pkg) == false)
205 continue;
206
207 if (EarlyRemove(D.ParentPkg()) == false)
208 return false;
209 }
210 return true;
211}
212 /*}}}*/
213// PM::ConfigureAll - Run the all out configuration /*{{{*/
214// ---------------------------------------------------------------------
215/* This configures every package. It is assumed they are all unpacked and
216 that the final configuration is valid. */
217bool pkgPackageManager::ConfigureAll()
218{
219 pkgOrderList OList(Cache);
220
221 // Populate the order list
222 for (pkgOrderList::iterator I = List->begin(); I != List->end(); I++)
223 if (List->IsFlag(pkgCache::PkgIterator(Cache,*I),
224 pkgOrderList::UnPacked) == true)
225 OList.push_back(*I);
226
227 if (OList.OrderConfigure() == false)
228 return false;
229
230 // Perform the configuring
231 for (pkgOrderList::iterator I = OList.begin(); I != OList.end(); I++)
232 {
233 PkgIterator Pkg(Cache,*I);
234
235 if (Configure(Pkg) == false)
236 return false;
237
238 List->Flag(Pkg,pkgOrderList::Configured,pkgOrderList::States);
239 }
240
241 return true;
242}
243 /*}}}*/
244// PM::SmartConfigure - Perform immediate configuration of the pkg /*{{{*/
245// ---------------------------------------------------------------------
246/* This routine scheduals the configuration of the given package and all
247 of it's dependents. */
248bool pkgPackageManager::SmartConfigure(PkgIterator Pkg)
249{
250 pkgOrderList OList(Cache);
251
252 if (DepAdd(OList,Pkg) == false)
253 return false;
254
255 if (OList.OrderConfigure() == false)
256 return false;
257
258 // Perform the configuring
259 for (pkgOrderList::iterator I = OList.begin(); I != OList.end(); I++)
260 {
261 PkgIterator Pkg(Cache,*I);
262
263 if (Configure(Pkg) == false)
264 return false;
265
266 List->Flag(Pkg,pkgOrderList::Configured,pkgOrderList::States);
267 }
268
269 // Sanity Check
270 if (List->IsFlag(Pkg,pkgOrderList::Configured) == false)
271 return _error->Error("Internal error, could not immediate configure %s",Pkg.Name());
272
273 return true;
274}
275 /*}}}*/
276// PM::DepAdd - Add all dependents to the oder list /*{{{*/
277// ---------------------------------------------------------------------
278/* This recursively adds all dependents to the order list */
279bool pkgPackageManager::DepAdd(pkgOrderList &OList,PkgIterator Pkg,int Depth)
280{
281 if (OList.IsFlag(Pkg,pkgOrderList::Added) == true)
282 return true;
283 if (List->IsFlag(Pkg,pkgOrderList::Configured) == true)
284 return true;
285 if (List->IsFlag(Pkg,pkgOrderList::UnPacked) == false)
286 return false;
287
288
289 // Put the package on the list
290 OList.push_back(Pkg);
291 OList.Flag(Pkg,pkgOrderList::Added);
292 Depth++;
293
294 // Check the dependencies to see if they are all satisfied.
295 bool Bad = false;
296 for (DepIterator D = Cache[Pkg].InstVerIter(Cache).DependsList(); D.end() == false;)
297 {
b50b2c97 298 if (D->Type != pkgCache::Dep::Depends && D->Type != pkgCache::Dep::PreDepends)
6c139d6e
AL
299 {
300 D++;
301 continue;
302 }
303
304 // Grok or groups
305 Bad = true;
306 for (bool LastOR = true; D.end() == false && LastOR == true; D++)
307 {
b50b2c97 308 LastOR = (D->CompareOp & pkgCache::Dep::Or) == pkgCache::Dep::Or;
6c139d6e
AL
309
310 if (Bad == false)
311 continue;
312
313 Version **VList = D.AllTargets();
314 for (Version **I = VList; *I != 0 && Bad == true; I++)
315 {
316 VerIterator Ver(Cache,*I);
317 PkgIterator Pkg = Ver.ParentPkg();
318
319 // See if the current version is ok
320 if (Pkg.CurrentVer() == Ver && List->IsNow(Pkg) == true &&
321 Pkg.State() == PkgIterator::NeedsNothing)
322 {
323 Bad = false;
324 continue;
325 }
326
327 // Not the install version
328 if (Cache[Pkg].InstallVer != *I ||
329 (Cache[Pkg].Keep() == true && Pkg.State() == PkgIterator::NeedsNothing))
330 continue;
331 if (List->IsFlag(Pkg,pkgOrderList::UnPacked) == true)
332 Bad = !DepAdd(OList,Pkg,Depth);
333 if (List->IsFlag(Pkg,pkgOrderList::Configured) == true)
334 Bad = false;
335 }
336 delete [] VList;
337 }
338
339 if (Bad == true)
340 {
341 OList.Flag(Pkg,0,pkgOrderList::Added);
342 OList.pop_back();
343 Depth--;
344 return false;
345 }
346 }
347
348 Depth--;
349 return true;
350}
351 /*}}}*/
352// PM::EarlyRemove - Perform removal of packages before their time /*{{{*/
353// ---------------------------------------------------------------------
354/* This is called to deal with conflicts arising from unpacking */
355bool pkgPackageManager::EarlyRemove(PkgIterator Pkg)
356{
357 if (List->IsNow(Pkg) == false)
358 return true;
359
360 // Already removed it
361 if (List->IsFlag(Pkg,pkgOrderList::Removed) == true)
362 return true;
363
364 // Woops, it will not be re-installed!
365 if (List->IsFlag(Pkg,pkgOrderList::InList) == false)
366 return false;
367
368 bool Res = SmartRemove(Pkg);
369 if (Cache[Pkg].Delete() == false)
370 List->Flag(Pkg,pkgOrderList::Removed,pkgOrderList::States);
371
372 return Res;
373}
374 /*}}}*/
375// PM::SmartRemove - Removal Helper /*{{{*/
376// ---------------------------------------------------------------------
377/* */
378bool pkgPackageManager::SmartRemove(PkgIterator Pkg)
379{
380 if (List->IsNow(Pkg) == false)
381 return true;
382
383 List->Flag(Pkg,pkgOrderList::Configured,pkgOrderList::States);
fc4b5c9f 384 return Remove(Pkg,(Cache[Pkg].iFlags & pkgDepCache::Purge) == pkgDepCache::Purge);
6c139d6e
AL
385}
386 /*}}}*/
387// PM::SmartUnPack - Install helper /*{{{*/
388// ---------------------------------------------------------------------
389/* This performs the task of handling pre-depends. */
390bool pkgPackageManager::SmartUnPack(PkgIterator Pkg)
391{
392 // Check if it is already unpacked
393 if (Pkg.State() == pkgCache::PkgIterator::NeedsConfigure &&
394 Cache[Pkg].Keep() == true)
395 {
396 List->Flag(Pkg,pkgOrderList::UnPacked,pkgOrderList::States);
397 if (List->IsFlag(Pkg,pkgOrderList::Immediate) == true)
398 if (SmartConfigure(Pkg) == false)
399 return _error->Error("Internal Error, Could not perform immediate configuraton");
400 return true;
401 }
981d20eb 402
6c139d6e
AL
403 /* See if this packages install version has any predependencies
404 that are not met by 'now' packages. */
405 for (DepIterator D = Cache[Pkg].InstVerIter(Cache).DependsList();
406 D.end() == false; D++)
407 {
b50b2c97 408 if (D->Type == pkgCache::Dep::PreDepends)
6c139d6e
AL
409 {
410 // Look for possible ok targets.
411 Version **VList = D.AllTargets();
412 bool Bad = true;
413 for (Version **I = VList; *I != 0 && Bad == true; I++)
414 {
415 VerIterator Ver(Cache,*I);
416 PkgIterator Pkg = Ver.ParentPkg();
417
418 // See if the current version is ok
419 if (Pkg.CurrentVer() == Ver && List->IsNow(Pkg) == true &&
420 Pkg.State() == PkgIterator::NeedsNothing)
421 {
422 Bad = false;
423 continue;
424 }
425 }
426
427 // Look for something that could be configured.
428 for (Version **I = VList; *I != 0 && Bad == true; I++)
429 {
430 VerIterator Ver(Cache,*I);
431 PkgIterator Pkg = Ver.ParentPkg();
432
433 // Not the install version
434 if (Cache[Pkg].InstallVer != *I ||
435 (Cache[Pkg].Keep() == true && Pkg.State() == PkgIterator::NeedsNothing))
436 continue;
437
438 Bad = !SmartConfigure(Pkg);
439 }
440
441 delete [] VList;
442
443 if (Bad == true)
444 return _error->Error("Internal Error, Couldn't configure a pre-depend");
445
446 continue;
447 }
448
b50b2c97 449 if (D->Type == pkgCache::Dep::Conflicts)
6c139d6e
AL
450 {
451 /* Look for conflicts. Two packages that are both in the install
452 state cannot conflict so we don't check.. */
453 Version **VList = D.AllTargets();
454 for (Version **I = VList; *I != 0; I++)
455 {
456 VerIterator Ver(Cache,*I);
457 PkgIterator Pkg = Ver.ParentPkg();
458
459 // See if the current version is conflicting
460 if (Pkg.CurrentVer() == Ver && List->IsNow(Pkg) == true)
461 {
462 if (EarlyRemove(Pkg) == false)
463 return _error->Error("Internal Error, Could not early remove %s",Pkg.Name());
464 }
465 }
466 delete [] VList;
467 }
468 }
469
470 // Check for reverse conflicts.
471 CheckRConflicts(Pkg,Pkg.RevDependsList(),
472 Cache[Pkg].InstVerIter(Cache).VerStr());
473 for (PrvIterator P = Cache[Pkg].InstVerIter(Cache).ProvidesList();
474 P.end() == false; P++)
475 CheckRConflicts(Pkg,P.ParentPkg().RevDependsList(),P.ProvideVersion());
476
477 if (Install(Pkg,FileNames[Pkg->ID]) == false)
478 return false;
479
480 List->Flag(Pkg,pkgOrderList::UnPacked,pkgOrderList::States);
481
482 // Perform immedate configuration of the package.
483 if (List->IsFlag(Pkg,pkgOrderList::Immediate) == true)
484 if (SmartConfigure(Pkg) == false)
485 return _error->Error("Internal Error, Could not perform immediate configuraton");
486
487 return true;
488}
489 /*}}}*/
490// PM::OrderInstall - Installation ordering routine /*{{{*/
491// ---------------------------------------------------------------------
492/* */
281daf46 493pkgPackageManager::OrderResult pkgPackageManager::OrderInstall()
6c139d6e 494{
7a1b1f8b 495 if (CreateOrderList() == false)
281daf46
AL
496 return Failed;
497
498 Reset();
6c139d6e 499
30e1eab5
AL
500 if (Debug == true)
501 clog << "Begining to order" << endl;
6c139d6e 502
281daf46
AL
503 if (List->OrderUnpack(FileNames) == false)
504 {
505 _error->Error("Internal ordering error");
506 return Failed;
507 }
508
30e1eab5
AL
509 if (Debug == true)
510 clog << "Done ordering" << endl;
511
281daf46 512 bool DoneSomething = false;
6c139d6e
AL
513 for (pkgOrderList::iterator I = List->begin(); I != List->end(); I++)
514 {
515 PkgIterator Pkg(Cache,*I);
281daf46
AL
516
517 if (List->IsNow(Pkg) == false)
518 {
519 if (Debug == true)
520 clog << "Skipping already done " << Pkg.Name() << endl;
521 continue;
522 }
523
2fd65468 524 if (List->IsMissing(Pkg) == true)
281daf46
AL
525 {
526 if (Debug == true)
527 clog << "Sequence completed at" << Pkg.Name() << endl;
528 if (DoneSomething == false)
529 {
530 _error->Error("Internal Error, ordering was unable to handle the media swap");
531 return Failed;
532 }
533 return Incomplete;
534 }
6c139d6e
AL
535
536 // Sanity check
537 if (Cache[Pkg].Keep() == true && Pkg.State() == pkgCache::PkgIterator::NeedsNothing)
281daf46
AL
538 {
539 _error->Error("Internal Error, trying to manipulate a kept package");
540 return Failed;
541 }
6c139d6e
AL
542
543 // Perform a delete or an install
544 if (Cache[Pkg].Delete() == true)
545 {
546 if (SmartRemove(Pkg) == false)
281daf46 547 return Failed;
6c139d6e
AL
548 }
549 else
550 if (SmartUnPack(Pkg) == false)
281daf46
AL
551 return Failed;
552 DoneSomething = true;
6c139d6e
AL
553 }
554
555 // Final run through the configure phase
556 if (ConfigureAll() == false)
281daf46 557 return Failed;
6c139d6e
AL
558
559 // Sanity check
560 for (pkgOrderList::iterator I = List->begin(); I != List->end(); I++)
281daf46 561 {
6c139d6e 562 if (List->IsFlag(*I,pkgOrderList::Configured) == false)
281daf46
AL
563 {
564 _error->Error("Internal error, packages left unconfigured. %s",
565 PkgIterator(Cache,*I).Name());
566 return Failed;
567 }
568 }
569
570 return Completed;
6c139d6e
AL
571}
572 /*}}}*/
573// PM::DoInstall - Does the installation /*{{{*/
574// ---------------------------------------------------------------------
575/* This uses the filenames in FileNames and the information in the
576 DepCache to perform the installation of packages.*/
281daf46 577pkgPackageManager::OrderResult pkgPackageManager::DoInstall()
6c139d6e 578{
281daf46
AL
579 OrderResult Res = OrderInstall();
580 if (Res != Failed)
581 if (Go() == false)
582 return Failed;
583 return Res;
6c139d6e
AL
584}
585 /*}}}*/