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