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