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