]> git.saurik.com Git - apt.git/blob - apt-pkg/depcache.cc
* apt-pkg/depcache.cc:
[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::DpkgBreaks && 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 &&
307 (Dep->Type == Dep::Conflicts || Dep->Type == Dep::DpkgBreaks))
308 continue;
309
310 // Check if the provides is a hit
311 if (Type == NowVersion)
312 {
313 if (P.OwnerPkg().CurrentVer() != P.OwnerVer())
314 continue;
315 }
316
317 if (Type == InstallVersion)
318 {
319 StateCache &State = PkgState[P.OwnerPkg()->ID];
320 if (State.InstallVer != (Version *)P.OwnerVer())
321 continue;
322 }
323
324 if (Type == CandidateVersion)
325 {
326 StateCache &State = PkgState[P.OwnerPkg()->ID];
327 if (State.CandidateVer != (Version *)P.OwnerVer())
328 continue;
329 }
330
331 // Compare the versions.
332 if (VS().CheckDep(P.ProvideVersion(),Dep->CompareOp,Dep.TargetVer()) == true)
333 {
334 Res = P.OwnerPkg();
335 return true;
336 }
337 }
338
339 return false;
340 }
341 /*}}}*/
342 // DepCache::AddSizes - Add the packages sizes to the counters /*{{{*/
343 // ---------------------------------------------------------------------
344 /* Call with Mult = -1 to preform the inverse opration */
345 void pkgDepCache::AddSizes(const PkgIterator &Pkg,signed long Mult)
346 {
347 StateCache &P = PkgState[Pkg->ID];
348
349 if (Pkg->VersionList == 0)
350 return;
351
352 if (Pkg.State() == pkgCache::PkgIterator::NeedsConfigure &&
353 P.Keep() == true)
354 return;
355
356 // Compute the size data
357 if (P.NewInstall() == true)
358 {
359 iUsrSize += (signed)(Mult*P.InstVerIter(*this)->InstalledSize);
360 iDownloadSize += (signed)(Mult*P.InstVerIter(*this)->Size);
361 return;
362 }
363
364 // Upgrading
365 if (Pkg->CurrentVer != 0 &&
366 (P.InstallVer != (Version *)Pkg.CurrentVer() ||
367 (P.iFlags & ReInstall) == ReInstall) && P.InstallVer != 0)
368 {
369 iUsrSize += (signed)(Mult*((signed)P.InstVerIter(*this)->InstalledSize -
370 (signed)Pkg.CurrentVer()->InstalledSize));
371 iDownloadSize += (signed)(Mult*P.InstVerIter(*this)->Size);
372 return;
373 }
374
375 // Reinstall
376 if (Pkg.State() == pkgCache::PkgIterator::NeedsUnpack &&
377 P.Delete() == false)
378 {
379 iDownloadSize += (signed)(Mult*P.InstVerIter(*this)->Size);
380 return;
381 }
382
383 // Removing
384 if (Pkg->CurrentVer != 0 && P.InstallVer == 0)
385 {
386 iUsrSize -= (signed)(Mult*Pkg.CurrentVer()->InstalledSize);
387 return;
388 }
389 }
390 /*}}}*/
391 // DepCache::AddStates - Add the package to the state counter /*{{{*/
392 // ---------------------------------------------------------------------
393 /* This routine is tricky to use, you must make sure that it is never
394 called twice for the same package. This means the Remove/Add section
395 should be as short as possible and not encompass any code that will
396 calld Remove/Add itself. Remember, dependencies can be circular so
397 while processing a dep for Pkg it is possible that Add/Remove
398 will be called on Pkg */
399 void pkgDepCache::AddStates(const PkgIterator &Pkg,int Add)
400 {
401 StateCache &State = PkgState[Pkg->ID];
402
403 // The Package is broken (either minimal dep or policy dep)
404 if ((State.DepState & DepInstMin) != DepInstMin)
405 iBrokenCount += Add;
406 if ((State.DepState & DepInstPolicy) != DepInstPolicy)
407 iPolicyBrokenCount += Add;
408
409 // Bad state
410 if (Pkg.State() != PkgIterator::NeedsNothing)
411 iBadCount += Add;
412
413 // Not installed
414 if (Pkg->CurrentVer == 0)
415 {
416 if (State.Mode == ModeDelete &&
417 (State.iFlags | Purge) == Purge && Pkg.Purge() == false)
418 iDelCount += Add;
419
420 if (State.Mode == ModeInstall)
421 iInstCount += Add;
422 return;
423 }
424
425 // Installed, no upgrade
426 if (State.Status == 0)
427 {
428 if (State.Mode == ModeDelete)
429 iDelCount += Add;
430 else
431 if ((State.iFlags & ReInstall) == ReInstall)
432 iInstCount += Add;
433
434 return;
435 }
436
437 // Alll 3 are possible
438 if (State.Mode == ModeDelete)
439 iDelCount += Add;
440 if (State.Mode == ModeKeep)
441 iKeepCount += Add;
442 if (State.Mode == ModeInstall)
443 iInstCount += Add;
444 }
445 /*}}}*/
446 // DepCache::BuildGroupOrs - Generate the Or group dep data /*{{{*/
447 // ---------------------------------------------------------------------
448 /* The or group results are stored in the last item of the or group. This
449 allows easy detection of the state of a whole or'd group. */
450 void pkgDepCache::BuildGroupOrs(VerIterator const &V)
451 {
452 unsigned char Group = 0;
453
454 for (DepIterator D = V.DependsList(); D.end() != true; D++)
455 {
456 // Build the dependency state.
457 unsigned char &State = DepState[D->ID];
458
459 /* Invert for Conflicts. We have to do this twice to get the
460 right sense for a conflicts group */
461 if (D->Type == Dep::Conflicts ||
462 D->Type == Dep::DpkgBreaks ||
463 D->Type == Dep::Obsoletes)
464 State = ~State;
465
466 // Add to the group if we are within an or..
467 State &= 0x7;
468 Group |= State;
469 State |= Group << 3;
470 if ((D->CompareOp & Dep::Or) != Dep::Or)
471 Group = 0;
472
473 // Invert for Conflicts
474 if (D->Type == Dep::Conflicts ||
475 D->Type == Dep::DpkgBreaks ||
476 D->Type == Dep::Obsoletes)
477 State = ~State;
478 }
479 }
480 /*}}}*/
481 // DepCache::VersionState - Perform a pass over a dependency list /*{{{*/
482 // ---------------------------------------------------------------------
483 /* This is used to run over a dependency list and determine the dep
484 state of the list, filtering it through both a Min check and a Policy
485 check. The return result will have SetMin/SetPolicy low if a check
486 fails. It uses the DepState cache for it's computations. */
487 unsigned char pkgDepCache::VersionState(DepIterator D,unsigned char Check,
488 unsigned char SetMin,
489 unsigned char SetPolicy)
490 {
491 unsigned char Dep = 0xFF;
492
493 while (D.end() != true)
494 {
495 // Compute a single dependency element (glob or)
496 DepIterator Start = D;
497 unsigned char State = 0;
498 for (bool LastOR = true; D.end() == false && LastOR == true; D++)
499 {
500 State |= DepState[D->ID];
501 LastOR = (D->CompareOp & Dep::Or) == Dep::Or;
502 }
503
504 // Minimum deps that must be satisfied to have a working package
505 if (Start.IsCritical() == true)
506 if ((State & Check) != Check)
507 Dep &= ~SetMin;
508
509 // Policy deps that must be satisfied to install the package
510 if (IsImportantDep(Start) == true &&
511 (State & Check) != Check)
512 Dep &= ~SetPolicy;
513 }
514
515 return Dep;
516 }
517 /*}}}*/
518 // DepCache::DependencyState - Compute the 3 results for a dep /*{{{*/
519 // ---------------------------------------------------------------------
520 /* This is the main dependency computation bit. It computes the 3 main
521 results for a dependencys, Now, Install and Candidate. Callers must
522 invert the result if dealing with conflicts. */
523 unsigned char pkgDepCache::DependencyState(DepIterator &D)
524 {
525 unsigned char State = 0;
526
527 if (CheckDep(D,NowVersion) == true)
528 State |= DepNow;
529 if (CheckDep(D,InstallVersion) == true)
530 State |= DepInstall;
531 if (CheckDep(D,CandidateVersion) == true)
532 State |= DepCVer;
533
534 return State;
535 }
536 /*}}}*/
537 // DepCache::UpdateVerState - Compute the Dep member of the state /*{{{*/
538 // ---------------------------------------------------------------------
539 /* This determines the combined dependency representation of a package
540 for its two states now and install. This is done by using the pre-generated
541 dependency information. */
542 void pkgDepCache::UpdateVerState(PkgIterator Pkg)
543 {
544 // Empty deps are always true
545 StateCache &State = PkgState[Pkg->ID];
546 State.DepState = 0xFF;
547
548 // Check the Current state
549 if (Pkg->CurrentVer != 0)
550 {
551 DepIterator D = Pkg.CurrentVer().DependsList();
552 State.DepState &= VersionState(D,DepNow,DepNowMin,DepNowPolicy);
553 }
554
555 /* Check the candidate state. We do not compare against the whole as
556 a candidate state but check the candidate version against the
557 install states */
558 if (State.CandidateVer != 0)
559 {
560 DepIterator D = State.CandidateVerIter(*this).DependsList();
561 State.DepState &= VersionState(D,DepInstall,DepCandMin,DepCandPolicy);
562 }
563
564 // Check target state which can only be current or installed
565 if (State.InstallVer != 0)
566 {
567 DepIterator D = State.InstVerIter(*this).DependsList();
568 State.DepState &= VersionState(D,DepInstall,DepInstMin,DepInstPolicy);
569 }
570 }
571 /*}}}*/
572 // DepCache::Update - Figure out all the state information /*{{{*/
573 // ---------------------------------------------------------------------
574 /* This will figure out the state of all the packages and all the
575 dependencies based on the current policy. */
576 void pkgDepCache::Update(OpProgress *Prog)
577 {
578 iUsrSize = 0;
579 iDownloadSize = 0;
580 iDelCount = 0;
581 iInstCount = 0;
582 iKeepCount = 0;
583 iBrokenCount = 0;
584 iBadCount = 0;
585
586 // Perform the depends pass
587 int Done = 0;
588 for (PkgIterator I = PkgBegin(); I.end() != true; I++,Done++)
589 {
590 if (Prog != 0 && Done%20 == 0)
591 Prog->Progress(Done);
592 for (VerIterator V = I.VersionList(); V.end() != true; V++)
593 {
594 unsigned char Group = 0;
595
596 for (DepIterator D = V.DependsList(); D.end() != true; D++)
597 {
598 // Build the dependency state.
599 unsigned char &State = DepState[D->ID];
600 State = DependencyState(D);
601
602 // Add to the group if we are within an or..
603 Group |= State;
604 State |= Group << 3;
605 if ((D->CompareOp & Dep::Or) != Dep::Or)
606 Group = 0;
607
608 // Invert for Conflicts
609 if (D->Type == Dep::Conflicts ||
610 D->Type == Dep::DpkgBreaks ||
611 D->Type == Dep::Obsoletes)
612 State = ~State;
613 }
614 }
615
616 // Compute the pacakge dependency state and size additions
617 AddSizes(I);
618 UpdateVerState(I);
619 AddStates(I);
620 }
621
622 if (Prog != 0)
623 Prog->Progress(Done);
624
625 readStateFile(Prog);
626 }
627 /*}}}*/
628 // DepCache::Update - Update the deps list of a package /*{{{*/
629 // ---------------------------------------------------------------------
630 /* This is a helper for update that only does the dep portion of the scan.
631 It is mainly meant to scan reverse dependencies. */
632 void pkgDepCache::Update(DepIterator D)
633 {
634 // Update the reverse deps
635 for (;D.end() != true; D++)
636 {
637 unsigned char &State = DepState[D->ID];
638 State = DependencyState(D);
639
640 // Invert for Conflicts
641 if (D->Type == Dep::Conflicts ||
642 D->Type == Dep::DpkgBreaks ||
643 D->Type == Dep::Obsoletes)
644 State = ~State;
645
646 RemoveStates(D.ParentPkg());
647 BuildGroupOrs(D.ParentVer());
648 UpdateVerState(D.ParentPkg());
649 AddStates(D.ParentPkg());
650 }
651 }
652 /*}}}*/
653 // DepCache::Update - Update the related deps of a package /*{{{*/
654 // ---------------------------------------------------------------------
655 /* This is called whenever the state of a package changes. It updates
656 all cached dependencies related to this package. */
657 void pkgDepCache::Update(PkgIterator const &Pkg)
658 {
659 // Recompute the dep of the package
660 RemoveStates(Pkg);
661 UpdateVerState(Pkg);
662 AddStates(Pkg);
663
664 // Update the reverse deps
665 Update(Pkg.RevDependsList());
666
667 // Update the provides map for the current ver
668 if (Pkg->CurrentVer != 0)
669 for (PrvIterator P = Pkg.CurrentVer().ProvidesList();
670 P.end() != true; P++)
671 Update(P.ParentPkg().RevDependsList());
672
673 // Update the provides map for the candidate ver
674 if (PkgState[Pkg->ID].CandidateVer != 0)
675 for (PrvIterator P = PkgState[Pkg->ID].CandidateVerIter(*this).ProvidesList();
676 P.end() != true; P++)
677 Update(P.ParentPkg().RevDependsList());
678 }
679
680 /*}}}*/
681
682 // DepCache::MarkKeep - Put the package in the keep state /*{{{*/
683 // ---------------------------------------------------------------------
684 /* */
685 void pkgDepCache::MarkKeep(PkgIterator const &Pkg, bool Soft, bool FromUser)
686 {
687 // Simplifies other routines.
688 if (Pkg.end() == true)
689 return;
690
691 /* Reject an attempt to keep a non-source broken installed package, those
692 must be upgraded */
693 if (Pkg.State() == PkgIterator::NeedsUnpack &&
694 Pkg.CurrentVer().Downloadable() == false)
695 return;
696
697 /** \todo Can this be moved later in the method? */
698 ActionGroup group(*this);
699
700 /* We changed the soft state all the time so the UI is a bit nicer
701 to use */
702 StateCache &P = PkgState[Pkg->ID];
703 if (Soft == true)
704 P.iFlags |= AutoKept;
705 else
706 P.iFlags &= ~AutoKept;
707
708 // Check that it is not already kept
709 if (P.Mode == ModeKeep)
710 return;
711
712 // We dont even try to keep virtual packages..
713 if (Pkg->VersionList == 0)
714 return;
715
716 if(FromUser && !P.Marked)
717 P.Flags &= ~Flag::Auto;
718 RemoveSizes(Pkg);
719 RemoveStates(Pkg);
720
721 P.Mode = ModeKeep;
722 if (Pkg->CurrentVer == 0)
723 P.InstallVer = 0;
724 else
725 P.InstallVer = Pkg.CurrentVer();
726
727 AddStates(Pkg);
728
729 Update(Pkg);
730
731 AddSizes(Pkg);
732 }
733 /*}}}*/
734 // DepCache::MarkDelete - Put the package in the delete state /*{{{*/
735 // ---------------------------------------------------------------------
736 /* */
737 void pkgDepCache::MarkDelete(PkgIterator const &Pkg, bool rPurge)
738 {
739 // Simplifies other routines.
740 if (Pkg.end() == true)
741 return;
742
743 ActionGroup group(*this);
744
745 // Check that it is not already marked for delete
746 StateCache &P = PkgState[Pkg->ID];
747 P.iFlags &= ~(AutoKept | Purge);
748 if (rPurge == true)
749 P.iFlags |= Purge;
750
751 if ((P.Mode == ModeDelete || P.InstallVer == 0) &&
752 (Pkg.Purge() == true || rPurge == false))
753 return;
754
755 // We dont even try to delete virtual packages..
756 if (Pkg->VersionList == 0)
757 return;
758
759 RemoveSizes(Pkg);
760 RemoveStates(Pkg);
761
762 if (Pkg->CurrentVer == 0 && (Pkg.Purge() == true || rPurge == false))
763 P.Mode = ModeKeep;
764 else
765 P.Mode = ModeDelete;
766 P.InstallVer = 0;
767
768 AddStates(Pkg);
769 Update(Pkg);
770 AddSizes(Pkg);
771 }
772 /*}}}*/
773 // DepCache::MarkInstall - Put the package in the install state /*{{{*/
774 // ---------------------------------------------------------------------
775 /* */
776 void pkgDepCache::MarkInstall(PkgIterator const &Pkg,bool AutoInst,
777 unsigned long Depth, bool FromUser,
778 bool ForceImportantDeps)
779 {
780 if (Depth > 100)
781 return;
782
783 // Simplifies other routines.
784 if (Pkg.end() == true)
785 return;
786
787 ActionGroup group(*this);
788
789 /* Check that it is not already marked for install and that it can be
790 installed */
791 StateCache &P = PkgState[Pkg->ID];
792 P.iFlags &= ~AutoKept;
793 if ((P.InstPolicyBroken() == false && P.InstBroken() == false) &&
794 (P.Mode == ModeInstall ||
795 P.CandidateVer == (Version *)Pkg.CurrentVer()))
796 {
797 if (P.CandidateVer == (Version *)Pkg.CurrentVer() && P.InstallVer == 0)
798 MarkKeep(Pkg, false, FromUser);
799 return;
800 }
801
802 // See if there is even any possible instalation candidate
803 if (P.CandidateVer == 0)
804 return;
805 // We dont even try to install virtual packages..
806 if (Pkg->VersionList == 0)
807 return;
808 /* Target the candidate version and remove the autoflag. We reset the
809 autoflag below if this was called recursively. Otherwise the user
810 should have the ability to de-auto a package by changing its state */
811 RemoveSizes(Pkg);
812 RemoveStates(Pkg);
813
814 P.Mode = ModeInstall;
815 P.InstallVer = P.CandidateVer;
816
817 if(FromUser)
818 {
819 // Set it to manual if it's a new install or cancelling the
820 // removal of a garbage package.
821 if(P.Status == 2 || (!Pkg.CurrentVer().end() && !P.Marked))
822 P.Flags &= ~Flag::Auto;
823 }
824 else
825 {
826 // Set it to auto if this is a new install.
827 if(P.Status == 2)
828 P.Flags |= Flag::Auto;
829 }
830 if (P.CandidateVer == (Version *)Pkg.CurrentVer())
831 P.Mode = ModeKeep;
832
833 AddStates(Pkg);
834 Update(Pkg);
835 AddSizes(Pkg);
836
837 if (AutoInst == false)
838 return;
839
840 DepIterator Dep = P.InstVerIter(*this).DependsList();
841 for (; Dep.end() != true;)
842 {
843 // Grok or groups
844 DepIterator Start = Dep;
845 bool Result = true;
846 unsigned Ors = 0;
847 for (bool LastOR = true; Dep.end() == false && LastOR == true; Dep++,Ors++)
848 {
849 LastOR = (Dep->CompareOp & Dep::Or) == Dep::Or;
850
851 if ((DepState[Dep->ID] & DepInstall) == DepInstall)
852 Result = false;
853 }
854
855 // Dep is satisfied okay.
856 if (Result == false)
857 continue;
858
859 /* Check if this dep should be consider for install. If it is a user
860 defined important dep and we are installed a new package then
861 it will be installed. Otherwise we only check for important
862 deps that have changed from the installed version
863 */
864 if (IsImportantDep(Start) == false)
865 continue;
866
867 /* check if any ImportantDep() (but not Critial) where added
868 * since we installed the package
869 */
870 bool isNewImportantDep = false;
871 if(!ForceImportantDeps && !Start.IsCritical())
872 {
873 bool found=false;
874 VerIterator instVer = Pkg.CurrentVer();
875 if(!instVer.end())
876 {
877 for (DepIterator D = instVer.DependsList(); D.end() != true; D++)
878 {
879 //FIXME: deal better with or-groups(?)
880 DepIterator LocalStart = D;
881
882 if(IsImportantDep(D) && Start.TargetPkg() == D.TargetPkg())
883 found=true;
884 }
885 // this is a new dep if it was not found to be already
886 // a important dep of the installed pacakge
887 isNewImportantDep = !found;
888 }
889 }
890 if(isNewImportantDep)
891 if(_config->FindB("Debug::pkgDepCache::AutoInstall",false) == true)
892 std::clog << "new important dependency: "
893 << Start.TargetPkg().Name() << std::endl;
894
895 // skip important deps if the package is already installed
896 if (Pkg->CurrentVer != 0 && Start.IsCritical() == false
897 && !isNewImportantDep && !ForceImportantDeps)
898 continue;
899
900 /* If we are in an or group locate the first or that can
901 succeed. We have already cached this.. */
902 for (; Ors > 1 && (DepState[Start->ID] & DepCVer) != DepCVer; Ors--)
903 Start++;
904
905 /* This bit is for processing the possibilty of an install/upgrade
906 fixing the problem */
907 SPtrArray<Version *> List = Start.AllTargets();
908 if (Start->Type != Dep::DpkgBreaks &&
909 (DepState[Start->ID] & DepCVer) == DepCVer)
910 {
911 // Right, find the best version to install..
912 Version **Cur = List;
913 PkgIterator P = Start.TargetPkg();
914 PkgIterator InstPkg(*Cache,0);
915
916 // See if there are direct matches (at the start of the list)
917 for (; *Cur != 0 && (*Cur)->ParentPkg == P.Index(); Cur++)
918 {
919 PkgIterator Pkg(*Cache,Cache->PkgP + (*Cur)->ParentPkg);
920 if (PkgState[Pkg->ID].CandidateVer != *Cur)
921 continue;
922 InstPkg = Pkg;
923 break;
924 }
925
926 // Select the highest priority providing package
927 if (InstPkg.end() == true)
928 {
929 pkgPrioSortList(*Cache,Cur);
930 for (; *Cur != 0; Cur++)
931 {
932 PkgIterator Pkg(*Cache,Cache->PkgP + (*Cur)->ParentPkg);
933 if (PkgState[Pkg->ID].CandidateVer != *Cur)
934 continue;
935 InstPkg = Pkg;
936 break;
937 }
938 }
939
940 if (InstPkg.end() == false)
941 {
942 if(_config->FindB("Debug::pkgDepCache::AutoInstall",false) == true)
943 std::clog << "Installing " << InstPkg.Name()
944 << " as dep of " << Pkg.Name()
945 << std::endl;
946 MarkInstall(InstPkg,true,Depth + 1, false, ForceImportantDeps);
947
948 // Set the autoflag, after MarkInstall because MarkInstall unsets it
949 if (P->CurrentVer == 0)
950 PkgState[InstPkg->ID].Flags |= Flag::Auto;
951 }
952 continue;
953 }
954
955 /* For conflicts we just de-install the package and mark as auto,
956 Conflicts may not have or groups. For dpkg's Breaks we try to
957 upgrade the package. */
958 if (Start->Type == Dep::Conflicts || Start->Type == Dep::Obsoletes ||
959 Start->Type == Dep::DpkgBreaks)
960 {
961 for (Version **I = List; *I != 0; I++)
962 {
963 VerIterator Ver(*this,*I);
964 PkgIterator Pkg = Ver.ParentPkg();
965
966 if (Start->Type != Dep::DpkgBreaks)
967 MarkDelete(Pkg);
968 else
969 if (PkgState[Pkg->ID].CandidateVer != *I)
970 MarkInstall(Pkg,true,Depth + 1, false, ForceImportantDeps);
971 }
972 continue;
973 }
974 }
975 }
976 /*}}}*/
977 // DepCache::SetReInstall - Set the reinstallation flag /*{{{*/
978 // ---------------------------------------------------------------------
979 /* */
980 void pkgDepCache::SetReInstall(PkgIterator const &Pkg,bool To)
981 {
982 ActionGroup group(*this);
983
984 RemoveSizes(Pkg);
985 RemoveStates(Pkg);
986
987 StateCache &P = PkgState[Pkg->ID];
988 if (To == true)
989 P.iFlags |= ReInstall;
990 else
991 P.iFlags &= ~ReInstall;
992
993 AddStates(Pkg);
994 AddSizes(Pkg);
995 }
996 /*}}}*/
997 // DepCache::SetCandidateVersion - Change the candidate version /*{{{*/
998 // ---------------------------------------------------------------------
999 /* */
1000 void pkgDepCache::SetCandidateVersion(VerIterator TargetVer)
1001 {
1002 ActionGroup group(*this);
1003
1004 pkgCache::PkgIterator Pkg = TargetVer.ParentPkg();
1005 StateCache &P = PkgState[Pkg->ID];
1006
1007 RemoveSizes(Pkg);
1008 RemoveStates(Pkg);
1009
1010 if (P.CandidateVer == P.InstallVer)
1011 P.InstallVer = (Version *)TargetVer;
1012 P.CandidateVer = (Version *)TargetVer;
1013 P.Update(Pkg,*this);
1014
1015 AddStates(Pkg);
1016 Update(Pkg);
1017 AddSizes(Pkg);
1018 }
1019
1020 void pkgDepCache::MarkAuto(const PkgIterator &Pkg, bool Auto)
1021 {
1022 StateCache &state = PkgState[Pkg->ID];
1023
1024 ActionGroup group(*this);
1025
1026 if(Auto)
1027 state.Flags |= Flag::Auto;
1028 else
1029 state.Flags &= ~Flag::Auto;
1030 }
1031 /*}}}*/
1032 // StateCache::Update - Compute the various static display things /*{{{*/
1033 // ---------------------------------------------------------------------
1034 /* This is called whenever the Candidate version changes. */
1035 void pkgDepCache::StateCache::Update(PkgIterator Pkg,pkgCache &Cache)
1036 {
1037 // Some info
1038 VerIterator Ver = CandidateVerIter(Cache);
1039
1040 // Use a null string or the version string
1041 if (Ver.end() == true)
1042 CandVersion = "";
1043 else
1044 CandVersion = Ver.VerStr();
1045
1046 // Find the current version
1047 CurVersion = "";
1048 if (Pkg->CurrentVer != 0)
1049 CurVersion = Pkg.CurrentVer().VerStr();
1050
1051 // Strip off the epochs for display
1052 CurVersion = StripEpoch(CurVersion);
1053 CandVersion = StripEpoch(CandVersion);
1054
1055 // Figure out if its up or down or equal
1056 Status = Ver.CompareVer(Pkg.CurrentVer());
1057 if (Pkg->CurrentVer == 0 || Pkg->VersionList == 0 || CandidateVer == 0)
1058 Status = 2;
1059 }
1060 /*}}}*/
1061 // StateCache::StripEpoch - Remove the epoch specifier from the version /*{{{*/
1062 // ---------------------------------------------------------------------
1063 /* */
1064 const char *pkgDepCache::StateCache::StripEpoch(const char *Ver)
1065 {
1066 if (Ver == 0)
1067 return 0;
1068
1069 // Strip any epoch
1070 for (const char *I = Ver; *I != 0; I++)
1071 if (*I == ':')
1072 return I + 1;
1073 return Ver;
1074 }
1075 /*}}}*/
1076
1077 // Policy::GetCandidateVer - Returns the Candidate install version /*{{{*/
1078 // ---------------------------------------------------------------------
1079 /* The default just returns the highest available version that is not
1080 a source and automatic. */
1081 pkgCache::VerIterator pkgDepCache::Policy::GetCandidateVer(PkgIterator Pkg)
1082 {
1083 /* Not source/not automatic versions cannot be a candidate version
1084 unless they are already installed */
1085 VerIterator Last(*(pkgCache *)this,0);
1086
1087 for (VerIterator I = Pkg.VersionList(); I.end() == false; I++)
1088 {
1089 if (Pkg.CurrentVer() == I)
1090 return I;
1091
1092 for (VerFileIterator J = I.FileList(); J.end() == false; J++)
1093 {
1094 if ((J.File()->Flags & Flag::NotSource) != 0)
1095 continue;
1096
1097 /* Stash the highest version of a not-automatic source, we use it
1098 if there is nothing better */
1099 if ((J.File()->Flags & Flag::NotAutomatic) != 0)
1100 {
1101 if (Last.end() == true)
1102 Last = I;
1103 continue;
1104 }
1105
1106 return I;
1107 }
1108 }
1109
1110 return Last;
1111 }
1112 /*}}}*/
1113 /*}}}*/
1114
1115 pkgDepCache::DefaultRootSetFunc::DefaultRootSetFunc()
1116 : constructedSuccessfully(false)
1117 {
1118 Configuration::Item const *Opts;
1119 Opts = _config->Tree("APT::NeverAutoRemove");
1120 if (Opts != 0 && Opts->Child != 0)
1121 {
1122 Opts = Opts->Child;
1123 for (; Opts != 0; Opts = Opts->Next)
1124 {
1125 if (Opts->Value.empty() == true)
1126 continue;
1127
1128 regex_t *p = new regex_t;
1129 if(regcomp(p,Opts->Value.c_str(),
1130 REG_EXTENDED | REG_ICASE | REG_NOSUB) != 0)
1131 {
1132 regfree(p);
1133 delete p;
1134 _error->Error("Regex compilation error for APT::NeverAutoRemove");
1135 return;
1136 }
1137
1138 rootSetRegexp.push_back(p);
1139 }
1140 }
1141
1142 constructedSuccessfully = true;
1143 }
1144
1145 pkgDepCache::DefaultRootSetFunc::~DefaultRootSetFunc()
1146 {
1147 for(unsigned int i = 0; i < rootSetRegexp.size(); i++)
1148 {
1149 regfree(rootSetRegexp[i]);
1150 delete rootSetRegexp[i];
1151 }
1152 }
1153
1154
1155 bool pkgDepCache::DefaultRootSetFunc::InRootSet(const pkgCache::PkgIterator &pkg)
1156 {
1157 for(unsigned int i = 0; i < rootSetRegexp.size(); i++)
1158 if (regexec(rootSetRegexp[i], pkg.Name(), 0, 0, 0) == 0)
1159 return true;
1160
1161 return false;
1162 }
1163
1164 pkgDepCache::InRootSetFunc *pkgDepCache::GetRootSetFunc()
1165 {
1166 DefaultRootSetFunc *f = new DefaultRootSetFunc;
1167 if(f->wasConstructedSuccessfully())
1168 return f;
1169 else
1170 {
1171 delete f;
1172 return NULL;
1173 }
1174 }
1175
1176 bool pkgDepCache::MarkFollowsRecommends()
1177 {
1178 return _config->FindB("APT::AutoRemove::RecommendsImportant", true);
1179 }
1180
1181 bool pkgDepCache::MarkFollowsSuggests()
1182 {
1183 return _config->FindB("APT::AutoRemove::SuggestsImportant", false);
1184 }
1185
1186 // the main mark algorithm
1187 bool pkgDepCache::MarkRequired(InRootSetFunc &userFunc)
1188 {
1189 bool follow_recommends;
1190 bool follow_suggests;
1191
1192 // init the states
1193 for(PkgIterator p = PkgBegin(); !p.end(); ++p)
1194 {
1195 PkgState[p->ID].Marked = false;
1196 PkgState[p->ID].Garbage = false;
1197
1198 // debug output
1199 if(_config->FindB("Debug::pkgAutoRemove",false)
1200 && PkgState[p->ID].Flags & Flag::Auto)
1201 std::clog << "AutoDep: " << p.Name() << std::endl;
1202 }
1203
1204 // init vars
1205 follow_recommends = MarkFollowsRecommends();
1206 follow_suggests = MarkFollowsSuggests();
1207
1208
1209
1210 // do the mark part, this is the core bit of the algorithm
1211 for(PkgIterator p = PkgBegin(); !p.end(); ++p)
1212 {
1213 if(!(PkgState[p->ID].Flags & Flag::Auto) ||
1214 (p->Flags & Flag::Essential) ||
1215 userFunc.InRootSet(p))
1216
1217 {
1218 // the package is installed (and set to keep)
1219 if(PkgState[p->ID].Keep() && !p.CurrentVer().end())
1220 MarkPackage(p, p.CurrentVer(),
1221 follow_recommends, follow_suggests);
1222 // the package is to be installed
1223 else if(PkgState[p->ID].Install())
1224 MarkPackage(p, PkgState[p->ID].InstVerIter(*this),
1225 follow_recommends, follow_suggests);
1226 }
1227 }
1228
1229 return true;
1230 }
1231
1232 // mark a single package in Mark-and-Sweep
1233 void pkgDepCache::MarkPackage(const pkgCache::PkgIterator &pkg,
1234 const pkgCache::VerIterator &ver,
1235 bool follow_recommends,
1236 bool follow_suggests)
1237 {
1238 pkgDepCache::StateCache &state = PkgState[pkg->ID];
1239 VerIterator candver = state.CandidateVerIter(*this);
1240 VerIterator instver = state.InstVerIter(*this);
1241
1242 #if 0
1243 // If a package was garbage-collected but is now being marked, we
1244 // should re-select it
1245 // For cases when a pkg is set to upgrade and this trigger the
1246 // removal of a no-longer used dependency. if the pkg is set to
1247 // keep again later it will result in broken deps
1248 if(state.Delete() && state.RemoveReason = Unused)
1249 {
1250 if(ver==candver)
1251 mark_install(pkg, false, false, NULL);
1252 else if(ver==pkg.CurrentVer())
1253 MarkKeep(pkg, false, false);
1254
1255 instver=state.InstVerIter(*this);
1256 }
1257 #endif
1258
1259 // Ignore versions other than the InstVer, and ignore packages
1260 // that are already going to be removed or just left uninstalled.
1261 if(!(ver == instver && !instver.end()))
1262 return;
1263
1264 // if we are marked already we are done
1265 if(state.Marked)
1266 return;
1267
1268 //std::cout << "Setting Marked for: " << pkg.Name() << std::endl;
1269 state.Marked=true;
1270
1271 if(!ver.end())
1272 {
1273 for(DepIterator d = ver.DependsList(); !d.end(); ++d)
1274 {
1275 if(d->Type == Dep::Depends ||
1276 d->Type == Dep::PreDepends ||
1277 (follow_recommends &&
1278 d->Type == Dep::Recommends) ||
1279 (follow_suggests &&
1280 d->Type == Dep::Suggests))
1281 {
1282 // Try all versions of this package.
1283 for(VerIterator V = d.TargetPkg().VersionList();
1284 !V.end(); ++V)
1285 {
1286 if(_system->VS->CheckDep(V.VerStr(), d->CompareOp, d.TargetVer()))
1287 {
1288 MarkPackage(V.ParentPkg(), V,
1289 follow_recommends, follow_suggests);
1290 }
1291 }
1292 // Now try virtual packages
1293 for(PrvIterator prv=d.TargetPkg().ProvidesList();
1294 !prv.end(); ++prv)
1295 {
1296 if(_system->VS->CheckDep(prv.ProvideVersion(), d->CompareOp,
1297 d.TargetVer()))
1298 {
1299 MarkPackage(prv.OwnerPkg(), prv.OwnerVer(),
1300 follow_recommends, follow_suggests);
1301 }
1302 }
1303 }
1304 }
1305 }
1306 }
1307
1308 bool pkgDepCache::Sweep()
1309 {
1310 // do the sweep
1311 for(PkgIterator p=PkgBegin(); !p.end(); ++p)
1312 {
1313 StateCache &state=PkgState[p->ID];
1314
1315 // if it is not marked and it is installed, it's garbage
1316 if(!state.Marked && (!p.CurrentVer().end() || state.Install()) &&
1317 !state.Delete())
1318 {
1319 state.Garbage=true;
1320 if(_config->FindB("Debug::pkgAutoRemove",false))
1321 std::cout << "Garbage: " << p.Name() << std::endl;
1322 }
1323 }
1324
1325 return true;
1326 }
1327
1328 // Policy::IsImportantDep - True if the dependency is important /*{{{*/
1329 // ---------------------------------------------------------------------
1330 /* */
1331 bool pkgDepCache::Policy::IsImportantDep(DepIterator Dep)
1332 {
1333 if(Dep.IsCritical())
1334 return true;
1335 else if(Dep->Type == pkgCache::Dep::Recommends)
1336 {
1337 if ( _config->FindB("APT::Install-Recommends", false))
1338 return true;
1339 // we suport a special mode to only install-recommends for certain
1340 // sections
1341 // FIXME: this is a meant as a temporarly solution until the
1342 // recommends are cleaned up
1343 string s = _config->Find("APT::Install-Recommends-Section","");
1344 if(s.size() > 0)
1345 {
1346 const char *sec = Dep.TargetPkg().Section();
1347 if (sec && strcmp(sec, s.c_str()) == 0)
1348 return true;
1349 }
1350 }
1351 else if(Dep->Type == pkgCache::Dep::Suggests)
1352 return _config->FindB("APT::Install-Suggests", false);
1353
1354 return false;
1355 }
1356 /*}}}*/