* merged with apt--debian-sid
[ntk/apt.git] / apt-pkg / depcache.cc
1 // -*- mode: cpp; mode: fold -*-
2 // Description /*{{{*/
3 // $Id: depcache.cc,v 1.25 2001/05/27 05:36:04 jgg Exp $
4 /* ######################################################################
5
6 Dependency Cache - Caches Dependency information.
7
8 ##################################################################### */
9 /*}}}*/
10 // Include Files /*{{{*/
11 #ifdef __GNUG__
12 #pragma implementation "apt-pkg/depcache.h"
13 #endif
14 #include <apt-pkg/depcache.h>
15 #include <apt-pkg/version.h>
16 #include <apt-pkg/error.h>
17 #include <apt-pkg/sptr.h>
18 #include <apt-pkg/algorithms.h>
19
20 #include <apt-pkg/fileutl.h>
21 #include <apt-pkg/configuration.h>
22 #include <apt-pkg/pkgsystem.h>
23 #include <apt-pkg/tagfile.h>
24
25 #include <iostream>
26 #include <sstream>
27 #include <set>
28
29 #include <apti18n.h>
30
31 pkgDepCache::ActionGroup::ActionGroup(pkgDepCache &cache) :
32 cache(cache), released(false)
33 {
34 ++cache.group_level;
35 }
36
37 void pkgDepCache::ActionGroup::release()
38 {
39 if(!released)
40 {
41 if(cache.group_level == 0)
42 std::cerr << "W: Unbalanced action groups, expect badness" << std::endl;
43 else
44 {
45 --cache.group_level;
46
47 if(cache.group_level == 0)
48 cache.MarkAndSweep();
49 }
50
51 released = false;
52 }
53 }
54
55 pkgDepCache::ActionGroup::~ActionGroup()
56 {
57 release();
58 }
59
60 // DepCache::pkgDepCache - Constructors /*{{{*/
61 // ---------------------------------------------------------------------
62 /* */
63 pkgDepCache::pkgDepCache(pkgCache *pCache,Policy *Plcy) :
64 group_level(0), Cache(pCache), PkgState(0), DepState(0)
65 {
66 delLocalPolicy = 0;
67 LocalPolicy = Plcy;
68 if (LocalPolicy == 0)
69 delLocalPolicy = LocalPolicy = new Policy;
70 }
71 /*}}}*/
72 // DepCache::~pkgDepCache - Destructor /*{{{*/
73 // ---------------------------------------------------------------------
74 /* */
75 pkgDepCache::~pkgDepCache()
76 {
77 delete [] PkgState;
78 delete [] DepState;
79 delete delLocalPolicy;
80 }
81 /*}}}*/
82 // DepCache::Init - Generate the initial extra structures. /*{{{*/
83 // ---------------------------------------------------------------------
84 /* This allocats the extension buffers and initializes them. */
85 bool pkgDepCache::Init(OpProgress *Prog)
86 {
87 // Suppress mark updates during this operation (just in case) and
88 // run a mark operation when Init terminates.
89 ActionGroup actions(*this);
90
91 delete [] PkgState;
92 delete [] DepState;
93 PkgState = new StateCache[Head().PackageCount];
94 DepState = new unsigned char[Head().DependsCount];
95 memset(PkgState,0,sizeof(*PkgState)*Head().PackageCount);
96 memset(DepState,0,sizeof(*DepState)*Head().DependsCount);
97
98 if (Prog != 0)
99 {
100 Prog->OverallProgress(0,2*Head().PackageCount,Head().PackageCount,
101 _("Building dependency tree"));
102 Prog->SubProgress(Head().PackageCount,_("Candidate versions"));
103 }
104
105 /* Set the current state of everything. In this state all of the
106 packages are kept exactly as is. See AllUpgrade */
107 int Done = 0;
108 for (PkgIterator I = PkgBegin(); I.end() != true; I++,Done++)
109 {
110 if (Prog != 0)
111 Prog->Progress(Done);
112
113 // Find the proper cache slot
114 StateCache &State = PkgState[I->ID];
115 State.iFlags = 0;
116
117 // Figure out the install version
118 State.CandidateVer = GetCandidateVer(I);
119 State.InstallVer = I.CurrentVer();
120 State.Mode = ModeKeep;
121
122 State.Update(I,*this);
123 }
124
125 if (Prog != 0)
126 {
127
128 Prog->OverallProgress(Head().PackageCount,2*Head().PackageCount,
129 Head().PackageCount,
130 _("Building dependency tree"));
131 Prog->SubProgress(Head().PackageCount,_("Dependency generation"));
132 }
133
134 Update(Prog);
135
136 if(Prog != 0)
137 Prog->Done();
138
139 return true;
140 }
141 /*}}}*/
142
143 bool pkgDepCache::readStateFile(OpProgress *Prog)
144 {
145 FileFd state_file;
146 string state = _config->FindDir("Dir::State") + "extended_states";
147 if(FileExists(state)) {
148 state_file.Open(state, FileFd::ReadOnly);
149 int file_size = state_file.Size();
150 if(Prog != NULL)
151 Prog->OverallProgress(0, file_size, 1,
152 _("Reading state information"));
153
154 pkgTagFile tagfile(&state_file);
155 pkgTagSection section;
156 int amt=0;
157 while(tagfile.Step(section)) {
158 string pkgname = section.FindS("Package");
159 pkgCache::PkgIterator pkg=Cache->FindPkg(pkgname);
160 // Silently ignore unknown packages and packages with no actual
161 // version.
162 if(!pkg.end() && !pkg.VersionList().end()) {
163 short reason = section.FindI("Auto-Installed", 0);
164 if(reason > 0)
165 PkgState[pkg->ID].Flags |= Flag::Auto;
166 if(_config->FindB("Debug::pkgAutoRemove",false))
167 std::cout << "Auto-Installed : " << pkgname << std::endl;
168 amt+=section.size();
169 if(Prog != NULL)
170 Prog->OverallProgress(amt, file_size, 1,
171 _("Reading state information"));
172 }
173 if(Prog != NULL)
174 Prog->OverallProgress(file_size, file_size, 1,
175 _("Reading state information"));
176 }
177 }
178
179 return true;
180 }
181
182 bool pkgDepCache::writeStateFile(OpProgress *prog)
183 {
184 if(_config->FindB("Debug::pkgAutoRemove",false))
185 std::clog << "pkgDepCache::writeStateFile()" << std::endl;
186
187 FileFd StateFile;
188 string state = _config->FindDir("Dir::State") + "extended_states";
189
190 // if it does not exist, create a empty one
191 if(!FileExists(state))
192 {
193 StateFile.Open(state, FileFd::WriteEmpty);
194 StateFile.Close();
195 }
196
197 // open it
198 if(!StateFile.Open(state, FileFd::ReadOnly))
199 return _error->Error(_("Failed to open StateFile %s"),
200 state.c_str());
201
202 FILE *OutFile;
203 string outfile = state + ".tmp";
204 if((OutFile = fopen(outfile.c_str(),"w")) == NULL)
205 return _error->Error(_("Failed to write temporary StateFile %s"),
206 outfile.c_str());
207
208 // first merge with the existing sections
209 pkgTagFile tagfile(&StateFile);
210 pkgTagSection section;
211 std::set<string> pkgs_seen;
212 const char *nullreorderlist[] = {0};
213 while(tagfile.Step(section)) {
214 string pkgname = section.FindS("Package");
215 // Silently ignore unknown packages and packages with no actual
216 // version.
217 pkgCache::PkgIterator pkg=Cache->FindPkg(pkgname);
218 if(pkg.end() || pkg.VersionList().end())
219 continue;
220 bool oldAuto = section.FindI("Auto-Installed");
221 bool newAuto = (PkgState[pkg->ID].Flags & Flag::Auto);
222 if(_config->FindB("Debug::pkgAutoRemove",false))
223 std::clog << "Update exisiting AutoInstall info: "
224 << pkg.Name() << std::endl;
225 TFRewriteData rewrite[2];
226 rewrite[0].Tag = "Auto-Installed";
227 rewrite[0].Rewrite = newAuto ? "1" : "0";
228 rewrite[0].NewTag = 0;
229 rewrite[1].Tag = 0;
230 TFRewrite(OutFile, section, nullreorderlist, rewrite);
231 fprintf(OutFile,"\n");
232 pkgs_seen.insert(pkgname);
233 }
234
235 // then write the ones we have not seen yet
236 std::ostringstream ostr;
237 for(pkgCache::PkgIterator pkg=Cache->PkgBegin(); !pkg.end(); pkg++) {
238 if(PkgState[pkg->ID].Flags & Flag::Auto) {
239 if (pkgs_seen.find(pkg.Name()) != pkgs_seen.end()) {
240 if(_config->FindB("Debug::pkgAutoRemove",false))
241 std::clog << "Skipping already written " << pkg.Name() << std::endl;
242 continue;
243 }
244 if(_config->FindB("Debug::pkgAutoRemove",false))
245 std::clog << "Writing new AutoInstall: "
246 << pkg.Name() << std::endl;
247 ostr.str(string(""));
248 ostr << "Package: " << pkg.Name()
249 << "\nAuto-Installed: 1\n\n";
250 fprintf(OutFile,ostr.str().c_str());
251 fprintf(OutFile,"\n");
252 }
253 }
254 fclose(OutFile);
255
256 // move the outfile over the real file
257 rename(outfile.c_str(), state.c_str());
258
259 return true;
260 }
261
262 // DepCache::CheckDep - Checks a single dependency /*{{{*/
263 // ---------------------------------------------------------------------
264 /* This first checks the dependency against the main target package and
265 then walks along the package provides list and checks if each provides
266 will be installed then checks the provides against the dep. Res will be
267 set to the package which was used to satisfy the dep. */
268 bool pkgDepCache::CheckDep(DepIterator Dep,int Type,PkgIterator &Res)
269 {
270 Res = Dep.TargetPkg();
271
272 /* Check simple depends. A depends -should- never self match but
273 we allow it anyhow because dpkg does. Technically it is a packaging
274 bug. Conflicts may never self match */
275 if (Dep.TargetPkg() != Dep.ParentPkg() ||
276 (Dep->Type != Dep::Conflicts && Dep->Type != Dep::Obsoletes))
277 {
278 PkgIterator Pkg = Dep.TargetPkg();
279 // Check the base package
280 if (Type == NowVersion && Pkg->CurrentVer != 0)
281 if (VS().CheckDep(Pkg.CurrentVer().VerStr(),Dep->CompareOp,
282 Dep.TargetVer()) == true)
283 return true;
284
285 if (Type == InstallVersion && PkgState[Pkg->ID].InstallVer != 0)
286 if (VS().CheckDep(PkgState[Pkg->ID].InstVerIter(*this).VerStr(),
287 Dep->CompareOp,Dep.TargetVer()) == true)
288 return true;
289
290 if (Type == CandidateVersion && PkgState[Pkg->ID].CandidateVer != 0)
291 if (VS().CheckDep(PkgState[Pkg->ID].CandidateVerIter(*this).VerStr(),
292 Dep->CompareOp,Dep.TargetVer()) == true)
293 return true;
294 }
295
296 if (Dep->Type == Dep::Obsoletes)
297 return false;
298
299 // Check the providing packages
300 PrvIterator P = Dep.TargetPkg().ProvidesList();
301 PkgIterator Pkg = Dep.ParentPkg();
302 for (; P.end() != true; P++)
303 {
304 /* Provides may never be applied against the same package if it is
305 a conflicts. See the comment above. */
306 if (P.OwnerPkg() == Pkg && Dep->Type == Dep::Conflicts)
307 continue;
308
309 // Check if the provides is a hit
310 if (Type == NowVersion)
311 {
312 if (P.OwnerPkg().CurrentVer() != P.OwnerVer())
313 continue;
314 }
315
316 if (Type == InstallVersion)
317 {
318 StateCache &State = PkgState[P.OwnerPkg()->ID];
319 if (State.InstallVer != (Version *)P.OwnerVer())
320 continue;
321 }
322
323 if (Type == CandidateVersion)
324 {
325 StateCache &State = PkgState[P.OwnerPkg()->ID];
326 if (State.CandidateVer != (Version *)P.OwnerVer())
327 continue;
328 }
329
330 // Compare the versions.
331 if (VS().CheckDep(P.ProvideVersion(),Dep->CompareOp,Dep.TargetVer()) == true)
332 {
333 Res = P.OwnerPkg();
334 return true;
335 }
336 }
337
338 return false;
339 }
340 /*}}}*/
341 // DepCache::AddSizes - Add the packages sizes to the counters /*{{{*/
342 // ---------------------------------------------------------------------
343 /* Call with Mult = -1 to preform the inverse opration */
344 void pkgDepCache::AddSizes(const PkgIterator &Pkg,signed long Mult)
345 {
346 StateCache &P = PkgState[Pkg->ID];
347
348 if (Pkg->VersionList == 0)
349 return;
350
351 if (Pkg.State() == pkgCache::PkgIterator::NeedsConfigure &&
352 P.Keep() == true)
353 return;
354
355 // Compute the size data
356 if (P.NewInstall() == true)
357 {
358 iUsrSize += (signed)(Mult*P.InstVerIter(*this)->InstalledSize);
359 iDownloadSize += (signed)(Mult*P.InstVerIter(*this)->Size);
360 return;
361 }
362
363 // Upgrading
364 if (Pkg->CurrentVer != 0 &&
365 (P.InstallVer != (Version *)Pkg.CurrentVer() ||
366 (P.iFlags & ReInstall) == ReInstall) && P.InstallVer != 0)
367 {
368 iUsrSize += (signed)(Mult*((signed)P.InstVerIter(*this)->InstalledSize -
369 (signed)Pkg.CurrentVer()->InstalledSize));
370 iDownloadSize += (signed)(Mult*P.InstVerIter(*this)->Size);
371 return;
372 }
373
374 // Reinstall
375 if (Pkg.State() == pkgCache::PkgIterator::NeedsUnpack &&
376 P.Delete() == false)
377 {
378 iDownloadSize += (signed)(Mult*P.InstVerIter(*this)->Size);
379 return;
380 }
381
382 // Removing
383 if (Pkg->CurrentVer != 0 && P.InstallVer == 0)
384 {
385 iUsrSize -= (signed)(Mult*Pkg.CurrentVer()->InstalledSize);
386 return;
387 }
388 }
389 /*}}}*/
390 // DepCache::AddStates - Add the package to the state counter /*{{{*/
391 // ---------------------------------------------------------------------
392 /* This routine is tricky to use, you must make sure that it is never
393 called twice for the same package. This means the Remove/Add section
394 should be as short as possible and not encompass any code that will
395 calld Remove/Add itself. Remember, dependencies can be circular so
396 while processing a dep for Pkg it is possible that Add/Remove
397 will be called on Pkg */
398 void pkgDepCache::AddStates(const PkgIterator &Pkg,int Add)
399 {
400 StateCache &State = PkgState[Pkg->ID];
401
402 // The Package is broken (either minimal dep or policy dep)
403 if ((State.DepState & DepInstMin) != DepInstMin)
404 iBrokenCount += Add;
405 if ((State.DepState & DepInstPolicy) != DepInstPolicy)
406 iPolicyBrokenCount += Add;
407
408 // Bad state
409 if (Pkg.State() != PkgIterator::NeedsNothing)
410 iBadCount += Add;
411
412 // Not installed
413 if (Pkg->CurrentVer == 0)
414 {
415 if (State.Mode == ModeDelete &&
416 (State.iFlags | Purge) == Purge && Pkg.Purge() == false)
417 iDelCount += Add;
418
419 if (State.Mode == ModeInstall)
420 iInstCount += Add;
421 return;
422 }
423
424 // Installed, no upgrade
425 if (State.Status == 0)
426 {
427 if (State.Mode == ModeDelete)
428 iDelCount += Add;
429 else
430 if ((State.iFlags & ReInstall) == ReInstall)
431 iInstCount += Add;
432
433 return;
434 }
435
436 // Alll 3 are possible
437 if (State.Mode == ModeDelete)
438 iDelCount += Add;
439 if (State.Mode == ModeKeep)
440 iKeepCount += Add;
441 if (State.Mode == ModeInstall)
442 iInstCount += Add;
443 }
444 /*}}}*/
445 // DepCache::BuildGroupOrs - Generate the Or group dep data /*{{{*/
446 // ---------------------------------------------------------------------
447 /* The or group results are stored in the last item of the or group. This
448 allows easy detection of the state of a whole or'd group. */
449 void pkgDepCache::BuildGroupOrs(VerIterator const &V)
450 {
451 unsigned char Group = 0;
452
453 for (DepIterator D = V.DependsList(); D.end() != true; D++)
454 {
455 // Build the dependency state.
456 unsigned char &State = DepState[D->ID];
457
458 /* Invert for Conflicts. We have to do this twice to get the
459 right sense for a conflicts group */
460 if (D->Type == Dep::Conflicts || D->Type == Dep::Obsoletes)
461 State = ~State;
462
463 // Add to the group if we are within an or..
464 State &= 0x7;
465 Group |= State;
466 State |= Group << 3;
467 if ((D->CompareOp & Dep::Or) != Dep::Or)
468 Group = 0;
469
470 // Invert for Conflicts
471 if (D->Type == Dep::Conflicts || D->Type == Dep::Obsoletes)
472 State = ~State;
473 }
474 }
475 /*}}}*/
476 // DepCache::VersionState - Perform a pass over a dependency list /*{{{*/
477 // ---------------------------------------------------------------------
478 /* This is used to run over a dependency list and determine the dep
479 state of the list, filtering it through both a Min check and a Policy
480 check. The return result will have SetMin/SetPolicy low if a check
481 fails. It uses the DepState cache for it's computations. */
482 unsigned char pkgDepCache::VersionState(DepIterator D,unsigned char Check,
483 unsigned char SetMin,
484 unsigned char SetPolicy)
485 {
486 unsigned char Dep = 0xFF;
487
488 while (D.end() != true)
489 {
490 // Compute a single dependency element (glob or)
491 DepIterator Start = D;
492 unsigned char State = 0;
493 for (bool LastOR = true; D.end() == false && LastOR == true; D++)
494 {
495 State |= DepState[D->ID];
496 LastOR = (D->CompareOp & Dep::Or) == Dep::Or;
497 }
498
499 // Minimum deps that must be satisfied to have a working package
500 if (Start.IsCritical() == true)
501 if ((State & Check) != Check)
502 Dep &= ~SetMin;
503
504 // Policy deps that must be satisfied to install the package
505 if (IsImportantDep(Start) == true &&
506 (State & Check) != Check)
507 Dep &= ~SetPolicy;
508 }
509
510 return Dep;
511 }
512 /*}}}*/
513 // DepCache::DependencyState - Compute the 3 results for a dep /*{{{*/
514 // ---------------------------------------------------------------------
515 /* This is the main dependency computation bit. It computes the 3 main
516 results for a dependencys, Now, Install and Candidate. Callers must
517 invert the result if dealing with conflicts. */
518 unsigned char pkgDepCache::DependencyState(DepIterator &D)
519 {
520 unsigned char State = 0;
521
522 if (CheckDep(D,NowVersion) == true)
523 State |= DepNow;
524 if (CheckDep(D,InstallVersion) == true)
525 State |= DepInstall;
526 if (CheckDep(D,CandidateVersion) == true)
527 State |= DepCVer;
528
529 return State;
530 }
531 /*}}}*/
532 // DepCache::UpdateVerState - Compute the Dep member of the state /*{{{*/
533 // ---------------------------------------------------------------------
534 /* This determines the combined dependency representation of a package
535 for its two states now and install. This is done by using the pre-generated
536 dependency information. */
537 void pkgDepCache::UpdateVerState(PkgIterator Pkg)
538 {
539 // Empty deps are always true
540 StateCache &State = PkgState[Pkg->ID];
541 State.DepState = 0xFF;
542
543 // Check the Current state
544 if (Pkg->CurrentVer != 0)
545 {
546 DepIterator D = Pkg.CurrentVer().DependsList();
547 State.DepState &= VersionState(D,DepNow,DepNowMin,DepNowPolicy);
548 }
549
550 /* Check the candidate state. We do not compare against the whole as
551 a candidate state but check the candidate version against the
552 install states */
553 if (State.CandidateVer != 0)
554 {
555 DepIterator D = State.CandidateVerIter(*this).DependsList();
556 State.DepState &= VersionState(D,DepInstall,DepCandMin,DepCandPolicy);
557 }
558
559 // Check target state which can only be current or installed
560 if (State.InstallVer != 0)
561 {
562 DepIterator D = State.InstVerIter(*this).DependsList();
563 State.DepState &= VersionState(D,DepInstall,DepInstMin,DepInstPolicy);
564 }
565 }
566 /*}}}*/
567 // DepCache::Update - Figure out all the state information /*{{{*/
568 // ---------------------------------------------------------------------
569 /* This will figure out the state of all the packages and all the
570 dependencies based on the current policy. */
571 void pkgDepCache::Update(OpProgress *Prog)
572 {
573 iUsrSize = 0;
574 iDownloadSize = 0;
575 iDelCount = 0;
576 iInstCount = 0;
577 iKeepCount = 0;
578 iBrokenCount = 0;
579 iBadCount = 0;
580
581 // Perform the depends pass
582 int Done = 0;
583 for (PkgIterator I = PkgBegin(); I.end() != true; I++,Done++)
584 {
585 if (Prog != 0 && Done%20 == 0)
586 Prog->Progress(Done);
587 for (VerIterator V = I.VersionList(); V.end() != true; V++)
588 {
589 unsigned char Group = 0;
590
591 for (DepIterator D = V.DependsList(); D.end() != true; D++)
592 {
593 // Build the dependency state.
594 unsigned char &State = DepState[D->ID];
595 State = DependencyState(D);
596
597 // Add to the group if we are within an or..
598 Group |= State;
599 State |= Group << 3;
600 if ((D->CompareOp & Dep::Or) != Dep::Or)
601 Group = 0;
602
603 // Invert for Conflicts
604 if (D->Type == Dep::Conflicts || D->Type == Dep::Obsoletes)
605 State = ~State;
606 }
607 }
608
609 // Compute the pacakge dependency state and size additions
610 AddSizes(I);
611 UpdateVerState(I);
612 AddStates(I);
613 }
614
615 if (Prog != 0)
616 Prog->Progress(Done);
617
618 readStateFile(Prog);
619 }
620 /*}}}*/
621 // DepCache::Update - Update the deps list of a package /*{{{*/
622 // ---------------------------------------------------------------------
623 /* This is a helper for update that only does the dep portion of the scan.
624 It is mainly meant to scan reverse dependencies. */
625 void pkgDepCache::Update(DepIterator D)
626 {
627 // Update the reverse deps
628 for (;D.end() != true; D++)
629 {
630 unsigned char &State = DepState[D->ID];
631 State = DependencyState(D);
632
633 // Invert for Conflicts
634 if (D->Type == Dep::Conflicts || D->Type == Dep::Obsoletes)
635 State = ~State;
636
637 RemoveStates(D.ParentPkg());
638 BuildGroupOrs(D.ParentVer());
639 UpdateVerState(D.ParentPkg());
640 AddStates(D.ParentPkg());
641 }
642 }
643 /*}}}*/
644 // DepCache::Update - Update the related deps of a package /*{{{*/
645 // ---------------------------------------------------------------------
646 /* This is called whenever the state of a package changes. It updates
647 all cached dependencies related to this package. */
648 void pkgDepCache::Update(PkgIterator const &Pkg)
649 {
650 // Recompute the dep of the package
651 RemoveStates(Pkg);
652 UpdateVerState(Pkg);
653 AddStates(Pkg);
654
655 // Update the reverse deps
656 Update(Pkg.RevDependsList());
657
658 // Update the provides map for the current ver
659 if (Pkg->CurrentVer != 0)
660 for (PrvIterator P = Pkg.CurrentVer().ProvidesList();
661 P.end() != true; P++)
662 Update(P.ParentPkg().RevDependsList());
663
664 // Update the provides map for the candidate ver
665 if (PkgState[Pkg->ID].CandidateVer != 0)
666 for (PrvIterator P = PkgState[Pkg->ID].CandidateVerIter(*this).ProvidesList();
667 P.end() != true; P++)
668 Update(P.ParentPkg().RevDependsList());
669 }
670
671 /*}}}*/
672
673 // DepCache::MarkKeep - Put the package in the keep state /*{{{*/
674 // ---------------------------------------------------------------------
675 /* */
676 void pkgDepCache::MarkKeep(PkgIterator const &Pkg, bool Soft, bool FromUser)
677 {
678 // Simplifies other routines.
679 if (Pkg.end() == true)
680 return;
681
682 /* Reject an attempt to keep a non-source broken installed package, those
683 must be upgraded */
684 if (Pkg.State() == PkgIterator::NeedsUnpack &&
685 Pkg.CurrentVer().Downloadable() == false)
686 return;
687
688 /** \todo Can this be moved later in the method? */
689 ActionGroup group(*this);
690
691 /* We changed the soft state all the time so the UI is a bit nicer
692 to use */
693 StateCache &P = PkgState[Pkg->ID];
694 if (Soft == true)
695 P.iFlags |= AutoKept;
696 else
697 P.iFlags &= ~AutoKept;
698
699 // Check that it is not already kept
700 if (P.Mode == ModeKeep)
701 return;
702
703 // We dont even try to keep virtual packages..
704 if (Pkg->VersionList == 0)
705 return;
706 #if 0 // reseting the autoflag here means we lose the
707 // auto-mark information if a user selects a package for removal
708 // but changes his mind then and sets it for keep again
709 // - this makes sense as default when all Garbage dependencies
710 // are automatically marked for removal (as aptitude does).
711 // setting a package for keep then makes it no longer autoinstalled
712 // for all other use-case this action is rather suprising
713 if(FromUser && !P.Marked)
714 P.Flags &= ~Flag::Auto;
715 #endif
716
717 RemoveSizes(Pkg);
718 RemoveStates(Pkg);
719
720 P.Mode = ModeKeep;
721 if (Pkg->CurrentVer == 0)
722 P.InstallVer = 0;
723 else
724 P.InstallVer = Pkg.CurrentVer();
725
726 AddStates(Pkg);
727
728 Update(Pkg);
729
730 AddSizes(Pkg);
731 }
732 /*}}}*/
733 // DepCache::MarkDelete - Put the package in the delete state /*{{{*/
734 // ---------------------------------------------------------------------
735 /* */
736 void pkgDepCache::MarkDelete(PkgIterator const &Pkg, bool rPurge)
737 {
738 // Simplifies other routines.
739 if (Pkg.end() == true)
740 return;
741
742 ActionGroup group(*this);
743
744 // Check that it is not already marked for delete
745 StateCache &P = PkgState[Pkg->ID];
746 P.iFlags &= ~(AutoKept | Purge);
747 if (rPurge == true)
748 P.iFlags |= Purge;
749
750 if ((P.Mode == ModeDelete || P.InstallVer == 0) &&
751 (Pkg.Purge() == true || rPurge == false))
752 return;
753
754 // We dont even try to delete virtual packages..
755 if (Pkg->VersionList == 0)
756 return;
757
758 RemoveSizes(Pkg);
759 RemoveStates(Pkg);
760
761 if (Pkg->CurrentVer == 0 && (Pkg.Purge() == true || rPurge == false))
762 P.Mode = ModeKeep;
763 else
764 P.Mode = ModeDelete;
765 P.InstallVer = 0;
766
767 AddStates(Pkg);
768 Update(Pkg);
769 AddSizes(Pkg);
770 }
771 /*}}}*/
772 // DepCache::MarkInstall - Put the package in the install state /*{{{*/
773 // ---------------------------------------------------------------------
774 /* */
775 void pkgDepCache::MarkInstall(PkgIterator const &Pkg,bool AutoInst,
776 unsigned long Depth, bool FromUser,
777 bool ForceImportantDeps)
778 {
779 if (Depth > 100)
780 return;
781
782 // Simplifies other routines.
783 if (Pkg.end() == true)
784 return;
785
786 ActionGroup group(*this);
787
788 /* Check that it is not already marked for install and that it can be
789 installed */
790 StateCache &P = PkgState[Pkg->ID];
791 P.iFlags &= ~AutoKept;
792 if ((P.InstPolicyBroken() == false && P.InstBroken() == false) &&
793 (P.Mode == ModeInstall ||
794 P.CandidateVer == (Version *)Pkg.CurrentVer()))
795 {
796 if (P.CandidateVer == (Version *)Pkg.CurrentVer() && P.InstallVer == 0)
797 MarkKeep(Pkg, false, FromUser);
798 return;
799 }
800
801 // See if there is even any possible instalation candidate
802 if (P.CandidateVer == 0)
803 return;
804 // We dont even try to install virtual packages..
805 if (Pkg->VersionList == 0)
806 return;
807 /* Target the candidate version and remove the autoflag. We reset the
808 autoflag below if this was called recursively. Otherwise the user
809 should have the ability to de-auto a package by changing its state */
810 RemoveSizes(Pkg);
811 RemoveStates(Pkg);
812
813 P.Mode = ModeInstall;
814 P.InstallVer = P.CandidateVer;
815
816 if(FromUser)
817 {
818 // Set it to manual if it's a new install or cancelling the
819 // removal of a garbage package.
820 if(P.Status == 2 || (!Pkg.CurrentVer().end() && !P.Marked))
821 P.Flags &= ~Flag::Auto;
822 }
823 else
824 {
825 // Set it to auto if this is a new install.
826 if(P.Status == 2)
827 P.Flags |= Flag::Auto;
828 }
829 if (P.CandidateVer == (Version *)Pkg.CurrentVer())
830 P.Mode = ModeKeep;
831
832 AddStates(Pkg);
833 Update(Pkg);
834 AddSizes(Pkg);
835
836 if (AutoInst == false)
837 return;
838
839 DepIterator Dep = P.InstVerIter(*this).DependsList();
840 for (; Dep.end() != true;)
841 {
842 // Grok or groups
843 DepIterator Start = Dep;
844 bool Result = true;
845 unsigned Ors = 0;
846 for (bool LastOR = true; Dep.end() == false && LastOR == true; Dep++,Ors++)
847 {
848 LastOR = (Dep->CompareOp & Dep::Or) == Dep::Or;
849
850 if ((DepState[Dep->ID] & DepInstall) == DepInstall)
851 Result = false;
852 }
853
854 // Dep is satisfied okay.
855 if (Result == false)
856 continue;
857
858 /* Check if this dep should be consider for install. If it is a user
859 defined important dep and we are installed a new package then
860 it will be installed. Otherwise we only check for important
861 deps that have changed from the installed version
862 */
863 if (IsImportantDep(Start) == false)
864 continue;
865
866 /* check if any ImportantDep() (but not Critial) where added
867 * since we installed the package
868 */
869 bool isNewImportantDep = false;
870 if(!ForceImportantDeps && !Start.IsCritical())
871 {
872 bool found=false;
873 VerIterator instVer = Pkg.CurrentVer();
874 if(!instVer.end())
875 {
876 for (DepIterator D = instVer.DependsList(); D.end() != true; D++)
877 {
878 //FIXME: deal better with or-groups(?)
879 DepIterator LocalStart = D;
880
881 if(IsImportantDep(D) && Start.TargetPkg() == D.TargetPkg())
882 found=true;
883 }
884 // this is a new dep if it was not found to be already
885 // a important dep of the installed pacakge
886 isNewImportantDep = !found;
887 }
888 }
889 if(isNewImportantDep)
890 if(_config->FindB("Debug::pkgDepCache::AutoInstall",false) == true)
891 std::clog << "new important dependency: "
892 << Start.TargetPkg().Name() << std::endl;
893
894 // skip important deps if the package is already installed
895 if (Pkg->CurrentVer != 0 && Start.IsCritical() == false
896 && !isNewImportantDep && !ForceImportantDeps)
897 continue;
898
899 /* If we are in an or group locate the first or that can
900 succeed. We have already cached this.. */
901 for (; Ors > 1 && (DepState[Start->ID] & DepCVer) != DepCVer; Ors--)
902 Start++;
903
904 /* This bit is for processing the possibilty of an install/upgrade
905 fixing the problem */
906 SPtrArray<Version *> List = Start.AllTargets();
907 if ((DepState[Start->ID] & DepCVer) == DepCVer)
908 {
909 // Right, find the best version to install..
910 Version **Cur = List;
911 PkgIterator P = Start.TargetPkg();
912 PkgIterator InstPkg(*Cache,0);
913
914 // See if there are direct matches (at the start of the list)
915 for (; *Cur != 0 && (*Cur)->ParentPkg == P.Index(); Cur++)
916 {
917 PkgIterator Pkg(*Cache,Cache->PkgP + (*Cur)->ParentPkg);
918 if (PkgState[Pkg->ID].CandidateVer != *Cur)
919 continue;
920 InstPkg = Pkg;
921 break;
922 }
923
924 // Select the highest priority providing package
925 if (InstPkg.end() == true)
926 {
927 pkgPrioSortList(*Cache,Cur);
928 for (; *Cur != 0; Cur++)
929 {
930 PkgIterator Pkg(*Cache,Cache->PkgP + (*Cur)->ParentPkg);
931 if (PkgState[Pkg->ID].CandidateVer != *Cur)
932 continue;
933 InstPkg = Pkg;
934 break;
935 }
936 }
937
938 if (InstPkg.end() == false)
939 {
940 if(_config->FindB("Debug::pkgDepCache::AutoInstall",false) == true)
941 std::clog << "Installing " << InstPkg.Name()
942 << " as dep of " << Pkg.Name()
943 << std::endl;
944 MarkInstall(InstPkg, true, Depth + 1, false, ForceImportantDeps);
945 }
946 continue;
947 }
948
949 /* For conflicts we just de-install the package and mark as auto,
950 Conflicts may not have or groups */
951 if (Start->Type == Dep::Conflicts || Start->Type == Dep::Obsoletes)
952 {
953 for (Version **I = List; *I != 0; I++)
954 {
955 VerIterator Ver(*this,*I);
956 PkgIterator Pkg = Ver.ParentPkg();
957
958 MarkDelete(Pkg);
959 }
960 continue;
961 }
962 }
963 }
964 /*}}}*/
965 // DepCache::SetReInstall - Set the reinstallation flag /*{{{*/
966 // ---------------------------------------------------------------------
967 /* */
968 void pkgDepCache::SetReInstall(PkgIterator const &Pkg,bool To)
969 {
970 ActionGroup group(*this);
971
972 RemoveSizes(Pkg);
973 RemoveStates(Pkg);
974
975 StateCache &P = PkgState[Pkg->ID];
976 if (To == true)
977 P.iFlags |= ReInstall;
978 else
979 P.iFlags &= ~ReInstall;
980
981 AddStates(Pkg);
982 AddSizes(Pkg);
983 }
984 /*}}}*/
985 // DepCache::SetCandidateVersion - Change the candidate version /*{{{*/
986 // ---------------------------------------------------------------------
987 /* */
988 void pkgDepCache::SetCandidateVersion(VerIterator TargetVer)
989 {
990 ActionGroup group(*this);
991
992 pkgCache::PkgIterator Pkg = TargetVer.ParentPkg();
993 StateCache &P = PkgState[Pkg->ID];
994
995 RemoveSizes(Pkg);
996 RemoveStates(Pkg);
997
998 if (P.CandidateVer == P.InstallVer)
999 P.InstallVer = (Version *)TargetVer;
1000 P.CandidateVer = (Version *)TargetVer;
1001 P.Update(Pkg,*this);
1002
1003 AddStates(Pkg);
1004 Update(Pkg);
1005 AddSizes(Pkg);
1006 }
1007
1008 void pkgDepCache::MarkAuto(const PkgIterator &Pkg, bool Auto)
1009 {
1010 StateCache &state = PkgState[Pkg->ID];
1011
1012 ActionGroup group(*this);
1013
1014 if(Auto)
1015 state.Flags |= Flag::Auto;
1016 else
1017 state.Flags &= ~Flag::Auto;
1018 }
1019 /*}}}*/
1020 // StateCache::Update - Compute the various static display things /*{{{*/
1021 // ---------------------------------------------------------------------
1022 /* This is called whenever the Candidate version changes. */
1023 void pkgDepCache::StateCache::Update(PkgIterator Pkg,pkgCache &Cache)
1024 {
1025 // Some info
1026 VerIterator Ver = CandidateVerIter(Cache);
1027
1028 // Use a null string or the version string
1029 if (Ver.end() == true)
1030 CandVersion = "";
1031 else
1032 CandVersion = Ver.VerStr();
1033
1034 // Find the current version
1035 CurVersion = "";
1036 if (Pkg->CurrentVer != 0)
1037 CurVersion = Pkg.CurrentVer().VerStr();
1038
1039 // Strip off the epochs for display
1040 CurVersion = StripEpoch(CurVersion);
1041 CandVersion = StripEpoch(CandVersion);
1042
1043 // Figure out if its up or down or equal
1044 Status = Ver.CompareVer(Pkg.CurrentVer());
1045 if (Pkg->CurrentVer == 0 || Pkg->VersionList == 0 || CandidateVer == 0)
1046 Status = 2;
1047 }
1048 /*}}}*/
1049 // StateCache::StripEpoch - Remove the epoch specifier from the version /*{{{*/
1050 // ---------------------------------------------------------------------
1051 /* */
1052 const char *pkgDepCache::StateCache::StripEpoch(const char *Ver)
1053 {
1054 if (Ver == 0)
1055 return 0;
1056
1057 // Strip any epoch
1058 for (const char *I = Ver; *I != 0; I++)
1059 if (*I == ':')
1060 return I + 1;
1061 return Ver;
1062 }
1063 /*}}}*/
1064
1065 // Policy::GetCandidateVer - Returns the Candidate install version /*{{{*/
1066 // ---------------------------------------------------------------------
1067 /* The default just returns the highest available version that is not
1068 a source and automatic. */
1069 pkgCache::VerIterator pkgDepCache::Policy::GetCandidateVer(PkgIterator Pkg)
1070 {
1071 /* Not source/not automatic versions cannot be a candidate version
1072 unless they are already installed */
1073 VerIterator Last(*(pkgCache *)this,0);
1074
1075 for (VerIterator I = Pkg.VersionList(); I.end() == false; I++)
1076 {
1077 if (Pkg.CurrentVer() == I)
1078 return I;
1079
1080 for (VerFileIterator J = I.FileList(); J.end() == false; J++)
1081 {
1082 if ((J.File()->Flags & Flag::NotSource) != 0)
1083 continue;
1084
1085 /* Stash the highest version of a not-automatic source, we use it
1086 if there is nothing better */
1087 if ((J.File()->Flags & Flag::NotAutomatic) != 0)
1088 {
1089 if (Last.end() == true)
1090 Last = I;
1091 continue;
1092 }
1093
1094 return I;
1095 }
1096 }
1097
1098 return Last;
1099 }
1100 /*}}}*/
1101 // Policy::IsImportantDep - True if the dependency is important /*{{{*/
1102 // ---------------------------------------------------------------------
1103 /* */
1104 bool pkgDepCache::Policy::IsImportantDep(DepIterator Dep)
1105 {
1106 if(Dep.IsCritical())
1107 return true;
1108 else if(Dep->Type == pkgCache::Dep::Recommends)
1109 {
1110 if ( _config->FindB("APT::Install-Recommends", false))
1111 return true;
1112 // we suport a special mode to only install-recommends for certain
1113 // sections
1114 // FIXME: this is a meant as a temporarly solution until the
1115 // recommends are cleaned up
1116 string s = _config->Find("APT::Install-Recommends-Section","");
1117 if(s.size() > 0)
1118 {
1119 const char *sec = Dep.TargetPkg().Section();
1120 if (sec && strcmp(sec, s.c_str()) == 0)
1121 return true;
1122 }
1123 }
1124 else if(Dep->Type == pkgCache::Dep::Suggests)
1125 return _config->FindB("APT::Install-Suggests", false);
1126
1127 return false;
1128 }
1129 /*}}}*/
1130
1131 pkgDepCache::DefaultRootSetFunc::DefaultRootSetFunc()
1132 : constructedSuccessfully(false)
1133 {
1134 Configuration::Item const *Opts;
1135 Opts = _config->Tree("APT::NeverAutoRemove");
1136 if (Opts != 0 && Opts->Child != 0)
1137 {
1138 Opts = Opts->Child;
1139 for (; Opts != 0; Opts = Opts->Next)
1140 {
1141 if (Opts->Value.empty() == true)
1142 continue;
1143
1144 regex_t *p = new regex_t;
1145 if(regcomp(p,Opts->Value.c_str(),
1146 REG_EXTENDED | REG_ICASE | REG_NOSUB) != 0)
1147 {
1148 regfree(p);
1149 delete p;
1150 _error->Error("Regex compilation error for APT::NeverAutoRemove");
1151 return;
1152 }
1153
1154 rootSetRegexp.push_back(p);
1155 }
1156 }
1157
1158 constructedSuccessfully = true;
1159 }
1160
1161 pkgDepCache::DefaultRootSetFunc::~DefaultRootSetFunc()
1162 {
1163 for(unsigned int i = 0; i < rootSetRegexp.size(); i++)
1164 {
1165 regfree(rootSetRegexp[i]);
1166 delete rootSetRegexp[i];
1167 }
1168 }
1169
1170
1171 bool pkgDepCache::DefaultRootSetFunc::InRootSet(const pkgCache::PkgIterator &pkg)
1172 {
1173 for(unsigned int i = 0; i < rootSetRegexp.size(); i++)
1174 if (regexec(rootSetRegexp[i], pkg.Name(), 0, 0, 0) == 0)
1175 return true;
1176
1177 return false;
1178 }
1179
1180 pkgDepCache::InRootSetFunc *pkgDepCache::GetRootSetFunc()
1181 {
1182 DefaultRootSetFunc *f = new DefaultRootSetFunc;
1183 if(f->wasConstructedSuccessfully())
1184 return f;
1185 else
1186 {
1187 delete f;
1188 return NULL;
1189 }
1190 }
1191
1192 bool pkgDepCache::MarkFollowsRecommends()
1193 {
1194 return _config->FindB("APT::AutoRemove::RecommendsImportant", true);
1195 }
1196
1197 bool pkgDepCache::MarkFollowsSuggests()
1198 {
1199 return _config->FindB("APT::AutoRemove::SuggestsImportant", false);
1200 }
1201
1202 // the main mark algorithm
1203 bool pkgDepCache::MarkRequired(InRootSetFunc &userFunc)
1204 {
1205 bool follow_recommends;
1206 bool follow_suggests;
1207
1208 // init the states
1209 for(PkgIterator p = PkgBegin(); !p.end(); ++p)
1210 {
1211 PkgState[p->ID].Marked = false;
1212 PkgState[p->ID].Garbage = false;
1213
1214 // debug output
1215 if(_config->FindB("Debug::pkgAutoRemove",false)
1216 && PkgState[p->ID].Flags & Flag::Auto)
1217 std::clog << "AutoDep: " << p.Name() << std::endl;
1218 }
1219
1220 // init vars
1221 follow_recommends = MarkFollowsRecommends();
1222 follow_suggests = MarkFollowsSuggests();
1223
1224
1225
1226 // do the mark part, this is the core bit of the algorithm
1227 for(PkgIterator p = PkgBegin(); !p.end(); ++p)
1228 {
1229 if(!(PkgState[p->ID].Flags & Flag::Auto) ||
1230 (p->Flags & Flag::Essential) ||
1231 userFunc.InRootSet(p))
1232
1233 {
1234 // the package is installed (and set to keep)
1235 if(PkgState[p->ID].Keep() && !p.CurrentVer().end())
1236 MarkPackage(p, p.CurrentVer(),
1237 follow_recommends, follow_suggests);
1238 // the package is to be installed
1239 else if(PkgState[p->ID].Install())
1240 MarkPackage(p, PkgState[p->ID].InstVerIter(*this),
1241 follow_recommends, follow_suggests);
1242 }
1243 }
1244
1245 return true;
1246 }
1247
1248 // mark a single package in Mark-and-Sweep
1249 void pkgDepCache::MarkPackage(const pkgCache::PkgIterator &pkg,
1250 const pkgCache::VerIterator &ver,
1251 bool follow_recommends,
1252 bool follow_suggests)
1253 {
1254 pkgDepCache::StateCache &state = PkgState[pkg->ID];
1255 VerIterator candver = state.CandidateVerIter(*this);
1256 VerIterator instver = state.InstVerIter(*this);
1257
1258 #if 0
1259 // If a package was garbage-collected but is now being marked, we
1260 // should re-select it
1261 // For cases when a pkg is set to upgrade and this trigger the
1262 // removal of a no-longer used dependency. if the pkg is set to
1263 // keep again later it will result in broken deps
1264 if(state.Delete() && state.RemoveReason = Unused)
1265 {
1266 if(ver==candver)
1267 mark_install(pkg, false, false, NULL);
1268 else if(ver==pkg.CurrentVer())
1269 MarkKeep(pkg, false, false);
1270
1271 instver=state.InstVerIter(*this);
1272 }
1273 #endif
1274
1275 // Ignore versions other than the InstVer, and ignore packages
1276 // that are already going to be removed or just left uninstalled.
1277 if(!(ver == instver && !instver.end()))
1278 return;
1279
1280 // if we are marked already we are done
1281 if(state.Marked)
1282 return;
1283
1284 //std::cout << "Setting Marked for: " << pkg.Name() << std::endl;
1285 state.Marked=true;
1286
1287 if(!ver.end())
1288 {
1289 for(DepIterator d = ver.DependsList(); !d.end(); ++d)
1290 {
1291 if(d->Type == Dep::Depends ||
1292 d->Type == Dep::PreDepends ||
1293 (follow_recommends &&
1294 d->Type == Dep::Recommends) ||
1295 (follow_suggests &&
1296 d->Type == Dep::Suggests))
1297 {
1298 // Try all versions of this package.
1299 for(VerIterator V = d.TargetPkg().VersionList();
1300 !V.end(); ++V)
1301 {
1302 if(_system->VS->CheckDep(V.VerStr(), d->CompareOp, d.TargetVer()))
1303 {
1304 MarkPackage(V.ParentPkg(), V,
1305 follow_recommends, follow_suggests);
1306 }
1307 }
1308 // Now try virtual packages
1309 for(PrvIterator prv=d.TargetPkg().ProvidesList();
1310 !prv.end(); ++prv)
1311 {
1312 if(_system->VS->CheckDep(prv.ProvideVersion(), d->CompareOp,
1313 d.TargetVer()))
1314 {
1315 MarkPackage(prv.OwnerPkg(), prv.OwnerVer(),
1316 follow_recommends, follow_suggests);
1317 }
1318 }
1319 }
1320 }
1321 }
1322 }
1323
1324 bool pkgDepCache::Sweep()
1325 {
1326 // do the sweep
1327 for(PkgIterator p=PkgBegin(); !p.end(); ++p)
1328 {
1329 StateCache &state=PkgState[p->ID];
1330
1331 // if it is not marked and it is installed, it's garbage
1332 if(!state.Marked && (!p.CurrentVer().end() || state.Install()))
1333 {
1334 state.Garbage=true;
1335 if(_config->FindB("Debug::pkgAutoRemove",false))
1336 std::cout << "Garbage: " << p.Name() << std::endl;
1337 }
1338 }
1339
1340 return true;
1341 }