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