avoid 416 response teardown binding to null pointer
[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 /*{{{*/
ea542140
DK
11#include<config.h>
12
094a497d 13#include <apt-pkg/depcache.h>
2c085486 14#include <apt-pkg/versionmatch.h>
a0ed43f7 15#include <apt-pkg/version.h>
094a497d 16#include <apt-pkg/error.h>
afb1e2e3 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>
afb1e2e3 21#include <apt-pkg/tagfile.h>
472ff00e 22#include <apt-pkg/progress.h>
2b5c35c7 23#include <apt-pkg/cacheset.h>
453b82a3
DK
24#include <apt-pkg/pkgcache.h>
25#include <apt-pkg/cacheiterators.h>
84573326 26#include <apt-pkg/prettyprinters.h>
9112f777 27#include <apt-pkg/cachefile.h>
453b82a3 28#include <apt-pkg/macros.h>
120365ce 29
453b82a3
DK
30#include <stdio.h>
31#include <string.h>
32#include <list>
33#include <string>
34#include <utility>
35#include <vector>
2b5c35c7 36#include <algorithm>
120365ce 37#include <iostream>
b1a8717a 38#include <set>
d4c5f11f 39
8a3a2e99
MV
40#include <sys/stat.h>
41
ea542140 42#include <apti18n.h>
92fcbfc1 43 /*}}}*/
8f3ba4e8
DK
44
45using std::string;
46
92fcbfc1 47// helper for Install-Recommends-Sections and Never-MarkAuto-Sections /*{{{*/
cb1933df
MV
48static bool
49ConfigValueInSubTree(const char* SubTree, const char *needle)
50{
51 Configuration::Item const *Opts;
52 Opts = _config->Tree(SubTree);
53 if (Opts != 0 && Opts->Child != 0)
54 {
55 Opts = Opts->Child;
56 for (; Opts != 0; Opts = Opts->Next)
57 {
58 if (Opts->Value.empty() == true)
59 continue;
60 if (strcmp(needle, Opts->Value.c_str()) == 0)
61 return true;
62 }
63 }
64 return false;
65}
92fcbfc1
DK
66 /*}}}*/
67pkgDepCache::ActionGroup::ActionGroup(pkgDepCache &cache) : /*{{{*/
6c55f07a 68 d(NULL), cache(cache), released(false)
74a05226
MV
69{
70 ++cache.group_level;
71}
72
73void pkgDepCache::ActionGroup::release()
74{
75 if(!released)
76 {
77 if(cache.group_level == 0)
78 std::cerr << "W: Unbalanced action groups, expect badness" << std::endl;
79 else
80 {
81 --cache.group_level;
82
83 if(cache.group_level == 0)
84 cache.MarkAndSweep();
85 }
86
ab60fb67 87 released = true;
74a05226
MV
88 }
89}
90
91pkgDepCache::ActionGroup::~ActionGroup()
92{
93 release();
94}
92fcbfc1 95 /*}}}*/
6c139d6e
AL
96// DepCache::pkgDepCache - Constructors /*{{{*/
97// ---------------------------------------------------------------------
98/* */
fd23676e 99pkgDepCache::pkgDepCache(pkgCache * const pCache,Policy * const Plcy) :
e8afd168
DK
100 group_level(0), Cache(pCache), PkgState(0), DepState(0),
101 iUsrSize(0), iDownloadSize(0), iInstCount(0), iDelCount(0), iKeepCount(0),
6c55f07a 102 iBrokenCount(0), iPolicyBrokenCount(0), iBadCount(0), d(NULL)
6c139d6e 103{
af29ffb4
MV
104 DebugMarker = _config->FindB("Debug::pkgDepCache::Marker", false);
105 DebugAutoInstall = _config->FindB("Debug::pkgDepCache::AutoInstall", false);
b2e465d6
AL
106 delLocalPolicy = 0;
107 LocalPolicy = Plcy;
108 if (LocalPolicy == 0)
109 delLocalPolicy = LocalPolicy = new Policy;
6c139d6e
AL
110}
111 /*}}}*/
112// DepCache::~pkgDepCache - Destructor /*{{{*/
113// ---------------------------------------------------------------------
114/* */
115pkgDepCache::~pkgDepCache()
116{
117 delete [] PkgState;
118 delete [] DepState;
b2e465d6 119 delete delLocalPolicy;
6c139d6e
AL
120}
121 /*}}}*/
6c139d6e
AL
122// DepCache::Init - Generate the initial extra structures. /*{{{*/
123// ---------------------------------------------------------------------
124/* This allocats the extension buffers and initializes them. */
fd23676e 125bool pkgDepCache::Init(OpProgress * const Prog)
6c139d6e 126{
74a05226
MV
127 // Suppress mark updates during this operation (just in case) and
128 // run a mark operation when Init terminates.
129 ActionGroup actions(*this);
130
6c139d6e
AL
131 delete [] PkgState;
132 delete [] DepState;
133 PkgState = new StateCache[Head().PackageCount];
134 DepState = new unsigned char[Head().DependsCount];
135 memset(PkgState,0,sizeof(*PkgState)*Head().PackageCount);
fd23676e 136 memset(DepState,0,sizeof(*DepState)*Head().DependsCount);
b2e465d6 137
a246f2dc
AL
138 if (Prog != 0)
139 {
140 Prog->OverallProgress(0,2*Head().PackageCount,Head().PackageCount,
db0db9fe
CP
141 _("Building dependency tree"));
142 Prog->SubProgress(Head().PackageCount,_("Candidate versions"));
a246f2dc 143 }
fd23676e 144
6c139d6e
AL
145 /* Set the current state of everything. In this state all of the
146 packages are kept exactly as is. See AllUpgrade */
a246f2dc 147 int Done = 0;
f7f0d6c7 148 for (PkgIterator I = PkgBegin(); I.end() != true; ++I, ++Done)
6c139d6e 149 {
2edcefd5 150 if (Prog != 0 && Done%20 == 0)
a246f2dc 151 Prog->Progress(Done);
fd23676e 152
6c139d6e
AL
153 // Find the proper cache slot
154 StateCache &State = PkgState[I->ID];
155 State.iFlags = 0;
afb1e2e3 156
6c139d6e 157 // Figure out the install version
294a8020 158 State.CandidateVer = LocalPolicy->GetCandidateVer(I);
6c139d6e
AL
159 State.InstallVer = I.CurrentVer();
160 State.Mode = ModeKeep;
fd23676e 161
6c139d6e 162 State.Update(I,*this);
fd23676e
DK
163 }
164
a246f2dc
AL
165 if (Prog != 0)
166 {
a246f2dc
AL
167 Prog->OverallProgress(Head().PackageCount,2*Head().PackageCount,
168 Head().PackageCount,
db0db9fe
CP
169 _("Building dependency tree"));
170 Prog->SubProgress(Head().PackageCount,_("Dependency generation"));
a246f2dc 171 }
fd23676e 172
a246f2dc 173 Update(Prog);
e004867d
MV
174
175 if(Prog != 0)
176 Prog->Done();
74a05226 177
6c139d6e 178 return true;
fd23676e 179}
6c139d6e 180 /*}}}*/
fd23676e 181bool pkgDepCache::readStateFile(OpProgress * const Prog) /*{{{*/
a83d884d
MV
182{
183 FileFd state_file;
d34690e1 184 string const state = _config->FindFile("Dir::State::extended_states");
36f1098a 185 if(RealFileExists(state)) {
a83d884d 186 state_file.Open(state, FileFd::ReadOnly);
650faab0 187 off_t const file_size = state_file.Size();
bc80031f 188 if(Prog != NULL)
fd23676e 189 Prog->OverallProgress(0, file_size, 1,
bc80031f 190 _("Reading state information"));
a83d884d
MV
191
192 pkgTagFile tagfile(&state_file);
193 pkgTagSection section;
650faab0 194 off_t amt = 0;
e0b94b97 195 bool const debug_autoremove = _config->FindB("Debug::pkgAutoRemove",false);
a83d884d 196 while(tagfile.Step(section)) {
e0b94b97
DK
197 string const pkgname = section.FindS("Package");
198 string pkgarch = section.FindS("Architecture");
199 if (pkgarch.empty() == true)
200 pkgarch = "any";
201 pkgCache::PkgIterator pkg = Cache->FindPkg(pkgname, pkgarch);
202 // Silently ignore unknown packages and packages with no actual version.
203 if(pkg.end() == true || pkg->VersionList == 0)
204 continue;
205
206 short const reason = section.FindI("Auto-Installed", 0);
207 if(reason > 0)
208 {
209 PkgState[pkg->ID].Flags |= Flag::Auto;
210 if (unlikely(debug_autoremove))
6ee2b0f8 211 std::clog << "Auto-Installed : " << pkg.FullName() << std::endl;
e0b94b97
DK
212 if (pkgarch == "any")
213 {
214 pkgCache::GrpIterator G = pkg.Group();
215 for (pkg = G.NextPkg(pkg); pkg.end() != true; pkg = G.NextPkg(pkg))
216 if (pkg->VersionList != 0)
217 PkgState[pkg->ID].Flags |= Flag::Auto;
218 }
a83d884d 219 }
e0b94b97
DK
220 amt += section.size();
221 if(Prog != NULL)
222 Prog->OverallProgress(amt, file_size, 1,
223 _("Reading state information"));
a83d884d 224 }
eef21b9f
DK
225 if(Prog != NULL)
226 Prog->OverallProgress(file_size, file_size, 1,
227 _("Reading state information"));
a83d884d
MV
228 }
229
230 return true;
231}
92fcbfc1 232 /*}}}*/
fd23676e 233bool pkgDepCache::writeStateFile(OpProgress * const /*prog*/, bool const InstalledOnly) /*{{{*/
a83d884d 234{
e0b94b97 235 bool const debug_autoremove = _config->FindB("Debug::pkgAutoRemove",false);
95afdfd0
OS
236
237 if(debug_autoremove)
e23e6733
MV
238 std::clog << "pkgDepCache::writeStateFile()" << std::endl;
239
b1a8717a 240 FileFd StateFile;
d34690e1 241 string const state = _config->FindFile("Dir::State::extended_states");
1b671a9b
DK
242 if (CreateAPTDirectoryIfNeeded(_config->FindDir("Dir::State"), flNotFile(state)) == false)
243 return false;
9a9665f9
MV
244
245 // if it does not exist, create a empty one
1b671a9b 246 if(!RealFileExists(state))
9a9665f9 247 {
22041bd2 248 StateFile.Open(state, FileFd::WriteAtomic);
9a9665f9
MV
249 StateFile.Close();
250 }
251
252 // open it
b1a8717a
MV
253 if(!StateFile.Open(state, FileFd::ReadOnly))
254 return _error->Error(_("Failed to open StateFile %s"),
a83d884d
MV
255 state.c_str());
256
88593886
DK
257 FileFd OutFile(state, FileFd::ReadWrite | FileFd::Atomic);
258 if (OutFile.IsOpen() == false || OutFile.Failed() == true)
259 return _error->Error(_("Failed to write temporary StateFile %s"), state.c_str());
80fa0d8a 260
b1a8717a
MV
261 // first merge with the existing sections
262 pkgTagFile tagfile(&StateFile);
263 pkgTagSection section;
264 std::set<string> pkgs_seen;
b1a8717a 265 while(tagfile.Step(section)) {
c176c4d0 266 string const pkgname = section.FindS("Package");
e0b94b97
DK
267 string pkgarch = section.FindS("Architecture");
268 if (pkgarch.empty() == true)
269 pkgarch = "native";
b1a8717a
MV
270 // Silently ignore unknown packages and packages with no actual
271 // version.
e0b94b97 272 pkgCache::PkgIterator pkg = Cache->FindPkg(pkgname, pkgarch);
88593886 273 if(pkg.end() || pkg.VersionList().end())
b1a8717a 274 continue;
c176c4d0
DK
275 StateCache const &P = PkgState[pkg->ID];
276 bool newAuto = (P.Flags & Flag::Auto);
277 // skip not installed or now-removed ones if requested
278 if (InstalledOnly && (
279 (pkg->CurrentVer == 0 && P.Mode != ModeInstall) ||
280 (pkg->CurrentVer != 0 && P.Mode == ModeDelete)))
281 {
282 // The section is obsolete if it contains no other tag
283 unsigned int const count = section.Count();
284 if (count < 2 ||
c67dc114
DK
285 (count == 2 && section.Exists("Auto-Installed")) ||
286 (count == 3 && section.Exists("Auto-Installed") && section.Exists("Architecture")))
c176c4d0
DK
287 continue;
288 else
289 newAuto = false;
290 }
b1a8717a 291 if(_config->FindB("Debug::pkgAutoRemove",false))
a1e42d1f 292 std::clog << "Update existing AutoInstall info: "
e0b94b97 293 << pkg.FullName() << std::endl;
88593886
DK
294
295 std::vector<pkgTagSection::Tag> rewrite;
296 rewrite.push_back(pkgTagSection::Tag::Rewrite("Architecture", pkg.Arch()));
297 rewrite.push_back(pkgTagSection::Tag::Rewrite("Auto-Installed", newAuto ? "1" : "0"));
298 section.Write(OutFile, NULL, rewrite);
299 if (OutFile.Write("\n", 1) == false)
300 return false;
e0b94b97 301 pkgs_seen.insert(pkg.FullName());
b1a8717a 302 }
88593886 303
b1a8717a 304 // then write the ones we have not seen yet
f7f0d6c7 305 for(pkgCache::PkgIterator pkg=Cache->PkgBegin(); !pkg.end(); ++pkg) {
c176c4d0
DK
306 StateCache const &P = PkgState[pkg->ID];
307 if(P.Flags & Flag::Auto) {
e0b94b97 308 if (pkgs_seen.find(pkg.FullName()) != pkgs_seen.end()) {
95afdfd0 309 if(debug_autoremove)
e0b94b97 310 std::clog << "Skipping already written " << pkg.FullName() << std::endl;
b1a8717a
MV
311 continue;
312 }
c176c4d0
DK
313 // skip not installed ones if requested
314 if (InstalledOnly && (
315 (pkg->CurrentVer == 0 && P.Mode != ModeInstall) ||
316 (pkg->CurrentVer != 0 && P.Mode == ModeDelete)))
317 continue;
e0b94b97
DK
318 const char* const pkgarch = pkg.Arch();
319 if (strcmp(pkgarch, "all") == 0)
320 continue;
95afdfd0 321 if(debug_autoremove)
e0b94b97 322 std::clog << "Writing new AutoInstall: " << pkg.FullName() << std::endl;
88593886
DK
323 std::string stanza = "Package: ";
324 stanza.append(pkg.Name())
325 .append("\nArchitecture: ").append(pkgarch)
326 .append("\nAuto-Installed: 1\n\n");
327 if (OutFile.Write(stanza.c_str(), stanza.length()) == false)
328 return false;
a83d884d
MV
329 }
330 }
52093186
DK
331 if (StateFile.Failed())
332 {
333 OutFile.OpFail();
334 return false;
335 }
88593886
DK
336 if (OutFile.Close() == false)
337 return false;
8a3a2e99 338 chmod(state.c_str(), 0644);
a83d884d
MV
339 return true;
340}
92fcbfc1 341 /*}}}*/
6c139d6e
AL
342// DepCache::CheckDep - Checks a single dependency /*{{{*/
343// ---------------------------------------------------------------------
344/* This first checks the dependency against the main target package and
345 then walks along the package provides list and checks if each provides
346 will be installed then checks the provides against the dep. Res will be
347 set to the package which was used to satisfy the dep. */
fd23676e 348bool pkgDepCache::CheckDep(DepIterator const &Dep,int const Type,PkgIterator &Res)
6c139d6e
AL
349{
350 Res = Dep.TargetPkg();
351
352 /* Check simple depends. A depends -should- never self match but
353 we allow it anyhow because dpkg does. Technically it is a packaging
354 bug. Conflicts may never self match */
021626db 355 if (Dep.IsIgnorable(Res) == false)
6c139d6e 356 {
6c139d6e 357 // Check the base package
fd23676e
DK
358 if (Type == NowVersion)
359 {
71c9e95b 360 if (Res->CurrentVer != 0 && Dep.IsSatisfied(Res.CurrentVer()) == true)
6c139d6e 361 return true;
fd23676e
DK
362 }
363 else if (Type == InstallVersion)
364 {
71c9e95b
DK
365 if (PkgState[Res->ID].InstallVer != 0 &&
366 Dep.IsSatisfied(PkgState[Res->ID].InstVerIter(*this)) == true)
6c139d6e 367 return true;
fd23676e
DK
368 }
369 else if (Type == CandidateVersion)
71c9e95b
DK
370 if (PkgState[Res->ID].CandidateVer != 0 &&
371 Dep.IsSatisfied(PkgState[Res->ID].CandidateVerIter(*this)) == true)
6c139d6e
AL
372 return true;
373 }
fd23676e 374
b2e465d6
AL
375 if (Dep->Type == Dep::Obsoletes)
376 return false;
fd23676e 377
6c139d6e
AL
378 // Check the providing packages
379 PrvIterator P = Dep.TargetPkg().ProvidesList();
f7f0d6c7 380 for (; P.end() != true; ++P)
6c139d6e 381 {
85434114
DK
382 if (Dep.IsIgnorable(P) == true)
383 continue;
88a52816 384
6c139d6e
AL
385 // Check if the provides is a hit
386 if (Type == NowVersion)
387 {
388 if (P.OwnerPkg().CurrentVer() != P.OwnerVer())
389 continue;
390 }
fd23676e 391 else if (Type == InstallVersion)
6c139d6e
AL
392 {
393 StateCache &State = PkgState[P.OwnerPkg()->ID];
394 if (State.InstallVer != (Version *)P.OwnerVer())
395 continue;
396 }
fd23676e 397 else if (Type == CandidateVersion)
6c139d6e
AL
398 {
399 StateCache &State = PkgState[P.OwnerPkg()->ID];
400 if (State.CandidateVer != (Version *)P.OwnerVer())
401 continue;
402 }
fd23676e 403
6c139d6e 404 // Compare the versions.
887c6940 405 if (Dep.IsSatisfied(P) == true)
6c139d6e
AL
406 {
407 Res = P.OwnerPkg();
408 return true;
409 }
410 }
fd23676e 411
6c139d6e
AL
412 return false;
413}
414 /*}}}*/
415// DepCache::AddSizes - Add the packages sizes to the counters /*{{{*/
416// ---------------------------------------------------------------------
81305a0b 417/* Call with Inverse = true to preform the inverse opration */
6935cd05 418void pkgDepCache::AddSizes(const PkgIterator &Pkg, bool const Inverse)
81305a0b
DK
419{
420 StateCache &P = PkgState[Pkg->ID];
421
422 if (Pkg->VersionList == 0)
423 return;
424
425 if (Pkg.State() == pkgCache::PkgIterator::NeedsConfigure &&
426 P.Keep() == true)
427 return;
428
429 // Compute the size data
430 if (P.NewInstall() == true)
431 {
432 if (Inverse == false) {
433 iUsrSize += P.InstVerIter(*this)->InstalledSize;
434 iDownloadSize += P.InstVerIter(*this)->Size;
435 } else {
436 iUsrSize -= P.InstVerIter(*this)->InstalledSize;
437 iDownloadSize -= P.InstVerIter(*this)->Size;
438 }
439 return;
440 }
441
442 // Upgrading
443 if (Pkg->CurrentVer != 0 &&
444 (P.InstallVer != (Version *)Pkg.CurrentVer() ||
445 (P.iFlags & ReInstall) == ReInstall) && P.InstallVer != 0)
446 {
447 if (Inverse == false) {
448 iUsrSize -= Pkg.CurrentVer()->InstalledSize;
449 iUsrSize += P.InstVerIter(*this)->InstalledSize;
450 iDownloadSize += P.InstVerIter(*this)->Size;
451 } else {
452 iUsrSize -= P.InstVerIter(*this)->InstalledSize;
453 iUsrSize += Pkg.CurrentVer()->InstalledSize;
454 iDownloadSize -= P.InstVerIter(*this)->Size;
455 }
456 return;
457 }
458
459 // Reinstall
460 if (Pkg.State() == pkgCache::PkgIterator::NeedsUnpack &&
461 P.Delete() == false)
462 {
463 if (Inverse == false)
464 iDownloadSize += P.InstVerIter(*this)->Size;
465 else
466 iDownloadSize -= P.InstVerIter(*this)->Size;
467 return;
468 }
469
470 // Removing
471 if (Pkg->CurrentVer != 0 && P.InstallVer == 0)
472 {
473 if (Inverse == false)
474 iUsrSize -= Pkg.CurrentVer()->InstalledSize;
475 else
476 iUsrSize += Pkg.CurrentVer()->InstalledSize;
d38b7b3d
AL
477 return;
478 }
6c139d6e
AL
479}
480 /*}}}*/
481// DepCache::AddStates - Add the package to the state counter /*{{{*/
482// ---------------------------------------------------------------------
483/* This routine is tricky to use, you must make sure that it is never
484 called twice for the same package. This means the Remove/Add section
485 should be as short as possible and not encompass any code that will
486 calld Remove/Add itself. Remember, dependencies can be circular so
487 while processing a dep for Pkg it is possible that Add/Remove
488 will be called on Pkg */
6935cd05 489void pkgDepCache::AddStates(const PkgIterator &Pkg, bool const Invert)
6c139d6e 490{
6935cd05 491 signed char const Add = (Invert == false) ? 1 : -1;
6c139d6e 492 StateCache &State = PkgState[Pkg->ID];
fd23676e 493
4ef9a929 494 // The Package is broken (either minimal dep or policy dep)
6c139d6e
AL
495 if ((State.DepState & DepInstMin) != DepInstMin)
496 iBrokenCount += Add;
4ef9a929
MV
497 if ((State.DepState & DepInstPolicy) != DepInstPolicy)
498 iPolicyBrokenCount += Add;
fd23676e 499
6c139d6e
AL
500 // Bad state
501 if (Pkg.State() != PkgIterator::NeedsNothing)
502 iBadCount += Add;
fd23676e 503
6c139d6e
AL
504 // Not installed
505 if (Pkg->CurrentVer == 0)
506 {
949e033c
DK
507 if (State.Mode == ModeDelete &&
508 (State.iFlags & Purge) == Purge && Pkg.Purge() == false)
d556d1a1 509 iDelCount += Add;
fd23676e 510
6c139d6e
AL
511 if (State.Mode == ModeInstall)
512 iInstCount += Add;
513 return;
514 }
fd23676e 515
6c139d6e 516 // Installed, no upgrade
6321777b 517 if (State.Status == 0)
fd23676e 518 {
6c139d6e
AL
519 if (State.Mode == ModeDelete)
520 iDelCount += Add;
d0c59649
AL
521 else
522 if ((State.iFlags & ReInstall) == ReInstall)
523 iInstCount += Add;
6c139d6e
AL
524 return;
525 }
fd23676e 526
6c139d6e
AL
527 // Alll 3 are possible
528 if (State.Mode == ModeDelete)
fd23676e
DK
529 iDelCount += Add;
530 else if (State.Mode == ModeKeep)
6c139d6e 531 iKeepCount += Add;
fd23676e 532 else if (State.Mode == ModeInstall)
6c139d6e
AL
533 iInstCount += Add;
534}
535 /*}}}*/
536// DepCache::BuildGroupOrs - Generate the Or group dep data /*{{{*/
537// ---------------------------------------------------------------------
538/* The or group results are stored in the last item of the or group. This
539 allows easy detection of the state of a whole or'd group. */
540void pkgDepCache::BuildGroupOrs(VerIterator const &V)
541{
542 unsigned char Group = 0;
f7f0d6c7 543 for (DepIterator D = V.DependsList(); D.end() != true; ++D)
6c139d6e
AL
544 {
545 // Build the dependency state.
546 unsigned char &State = DepState[D->ID];
547
548 /* Invert for Conflicts. We have to do this twice to get the
549 right sense for a conflicts group */
359e46db 550 if (D.IsNegative() == true)
6c139d6e 551 State = ~State;
fd23676e 552
6c139d6e 553 // Add to the group if we are within an or..
d2685fd6 554 State &= 0x7;
6c139d6e
AL
555 Group |= State;
556 State |= Group << 3;
557 if ((D->CompareOp & Dep::Or) != Dep::Or)
558 Group = 0;
fd23676e 559
6c139d6e 560 // Invert for Conflicts
359e46db 561 if (D.IsNegative() == true)
6c139d6e 562 State = ~State;
fd23676e 563 }
6c139d6e
AL
564}
565 /*}}}*/
566// DepCache::VersionState - Perform a pass over a dependency list /*{{{*/
567// ---------------------------------------------------------------------
568/* This is used to run over a dependency list and determine the dep
569 state of the list, filtering it through both a Min check and a Policy
570 check. The return result will have SetMin/SetPolicy low if a check
571 fails. It uses the DepState cache for it's computations. */
fd23676e
DK
572unsigned char pkgDepCache::VersionState(DepIterator D, unsigned char const Check,
573 unsigned char const SetMin,
574 unsigned char const SetPolicy) const
6c139d6e
AL
575{
576 unsigned char Dep = 0xFF;
6c139d6e
AL
577 while (D.end() != true)
578 {
fd23676e
DK
579 // the last or-dependency has the state of all previous or'ed
580 DepIterator Start, End;
581 D.GlobOr(Start, End);
582 // ignore if we are called with Dep{Install,…} or DepG{Install,…}
583 // the later would be more correct, but the first is what we get
584 unsigned char const State = DepState[End->ID] | (DepState[End->ID] >> 3);
585
6c139d6e
AL
586 // Minimum deps that must be satisfied to have a working package
587 if (Start.IsCritical() == true)
fd23676e 588 {
6c139d6e 589 if ((State & Check) != Check)
fd23676e
DK
590 return Dep &= ~(SetMin | SetPolicy);
591 }
6c139d6e 592 // Policy deps that must be satisfied to install the package
fd23676e 593 else if (IsImportantDep(Start) == true &&
6c139d6e
AL
594 (State & Check) != Check)
595 Dep &= ~SetPolicy;
596 }
6c139d6e
AL
597 return Dep;
598}
599 /*}}}*/
600// DepCache::DependencyState - Compute the 3 results for a dep /*{{{*/
601// ---------------------------------------------------------------------
602/* This is the main dependency computation bit. It computes the 3 main
603 results for a dependencys, Now, Install and Candidate. Callers must
604 invert the result if dealing with conflicts. */
fd23676e 605unsigned char pkgDepCache::DependencyState(DepIterator const &D)
6c139d6e
AL
606{
607 unsigned char State = 0;
fd23676e 608
6c139d6e
AL
609 if (CheckDep(D,NowVersion) == true)
610 State |= DepNow;
611 if (CheckDep(D,InstallVersion) == true)
612 State |= DepInstall;
613 if (CheckDep(D,CandidateVersion) == true)
614 State |= DepCVer;
fd23676e 615
6c139d6e
AL
616 return State;
617}
618 /*}}}*/
619// DepCache::UpdateVerState - Compute the Dep member of the state /*{{{*/
620// ---------------------------------------------------------------------
621/* This determines the combined dependency representation of a package
622 for its two states now and install. This is done by using the pre-generated
623 dependency information. */
fd23676e 624void pkgDepCache::UpdateVerState(PkgIterator const &Pkg)
6c139d6e
AL
625{
626 // Empty deps are always true
627 StateCache &State = PkgState[Pkg->ID];
628 State.DepState = 0xFF;
629
630 // Check the Current state
631 if (Pkg->CurrentVer != 0)
632 {
633 DepIterator D = Pkg.CurrentVer().DependsList();
634 State.DepState &= VersionState(D,DepNow,DepNowMin,DepNowPolicy);
635 }
636
637 /* Check the candidate state. We do not compare against the whole as
638 a candidate state but check the candidate version against the
639 install states */
640 if (State.CandidateVer != 0)
641 {
642 DepIterator D = State.CandidateVerIter(*this).DependsList();
643 State.DepState &= VersionState(D,DepInstall,DepCandMin,DepCandPolicy);
644 }
645
646 // Check target state which can only be current or installed
647 if (State.InstallVer != 0)
648 {
649 DepIterator D = State.InstVerIter(*this).DependsList();
650 State.DepState &= VersionState(D,DepInstall,DepInstMin,DepInstPolicy);
651 }
652}
653 /*}}}*/
654// DepCache::Update - Figure out all the state information /*{{{*/
655// ---------------------------------------------------------------------
656/* This will figure out the state of all the packages and all the
657 dependencies based on the current policy. */
fd23676e 658void pkgDepCache::Update(OpProgress * const Prog)
6c139d6e
AL
659{
660 iUsrSize = 0;
661 iDownloadSize = 0;
6c139d6e 662 iInstCount = 0;
fb4c7643 663 iDelCount = 0;
6c139d6e
AL
664 iKeepCount = 0;
665 iBrokenCount = 0;
fb4c7643 666 iPolicyBrokenCount = 0;
6c139d6e 667 iBadCount = 0;
8b32e920 668
6c139d6e 669 // Perform the depends pass
a246f2dc 670 int Done = 0;
f7f0d6c7 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);
f7f0d6c7 675 for (VerIterator V = I.VersionList(); V.end() != true; ++V)
6c139d6e
AL
676 {
677 unsigned char Group = 0;
8b32e920 678
f7f0d6c7 679 for (DepIterator D = V.DependsList(); D.end() != true; ++D)
6c139d6e
AL
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
359e46db 692 if (D.IsNegative() == true)
6c139d6e 693 State = ~State;
8b32e920 694 }
6c139d6e
AL
695 }
696
8b32e920 697 // Compute the package dependency state and size additions
6c139d6e
AL
698 AddSizes(I);
699 UpdateVerState(I);
700 AddStates(I);
701 }
a246f2dc 702
8b32e920 703 if (Prog != 0)
a246f2dc 704 Prog->Progress(Done);
74a05226
MV
705
706 readStateFile(Prog);
6c139d6e
AL
707}
708 /*}}}*/
709// DepCache::Update - Update the deps list of a package /*{{{*/
710// ---------------------------------------------------------------------
711/* This is a helper for update that only does the dep portion of the scan.
74a05226 712 It is mainly meant to scan reverse dependencies. */
6c139d6e
AL
713void pkgDepCache::Update(DepIterator D)
714{
715 // Update the reverse deps
f7f0d6c7 716 for (;D.end() != true; ++D)
6c139d6e
AL
717 {
718 unsigned char &State = DepState[D->ID];
719 State = DependencyState(D);
720
721 // Invert for Conflicts
359e46db 722 if (D.IsNegative() == true)
6c139d6e 723 State = ~State;
b2e465d6 724
6c139d6e
AL
725 RemoveStates(D.ParentPkg());
726 BuildGroupOrs(D.ParentVer());
727 UpdateVerState(D.ParentPkg());
728 AddStates(D.ParentPkg());
729 }
730}
731 /*}}}*/
732// DepCache::Update - Update the related deps of a package /*{{{*/
733// ---------------------------------------------------------------------
734/* This is called whenever the state of a package changes. It updates
735 all cached dependencies related to this package. */
736void pkgDepCache::Update(PkgIterator const &Pkg)
b2e465d6 737{
6c139d6e
AL
738 // Recompute the dep of the package
739 RemoveStates(Pkg);
740 UpdateVerState(Pkg);
741 AddStates(Pkg);
742
743 // Update the reverse deps
744 Update(Pkg.RevDependsList());
745
746 // Update the provides map for the current ver
747 if (Pkg->CurrentVer != 0)
748 for (PrvIterator P = Pkg.CurrentVer().ProvidesList();
f7f0d6c7 749 P.end() != true; ++P)
6c139d6e
AL
750 Update(P.ParentPkg().RevDependsList());
751
752 // Update the provides map for the candidate ver
9972233d
AL
753 if (PkgState[Pkg->ID].CandidateVer != 0)
754 for (PrvIterator P = PkgState[Pkg->ID].CandidateVerIter(*this).ProvidesList();
f7f0d6c7 755 P.end() != true; ++P)
9972233d 756 Update(P.ParentPkg().RevDependsList());
6c139d6e 757}
6c139d6e 758 /*}}}*/
6c139d6e
AL
759// DepCache::MarkKeep - Put the package in the keep state /*{{{*/
760// ---------------------------------------------------------------------
761/* */
3d619a20 762bool pkgDepCache::MarkKeep(PkgIterator const &Pkg, bool Soft, bool FromUser,
af29ffb4 763 unsigned long Depth)
6c139d6e 764{
dde0c674 765 if (IsModeChangeOk(ModeKeep, Pkg, Depth, FromUser) == false)
3d619a20 766 return false;
813c8eea
AL
767
768 /* Reject an attempt to keep a non-source broken installed package, those
769 must be upgraded */
770 if (Pkg.State() == PkgIterator::NeedsUnpack &&
771 Pkg.CurrentVer().Downloadable() == false)
3d619a20 772 return false;
74a05226 773
6c139d6e
AL
774 /* We changed the soft state all the time so the UI is a bit nicer
775 to use */
776 StateCache &P = PkgState[Pkg->ID];
dde0c674
DK
777
778 // Check that it is not already kept
779 if (P.Mode == ModeKeep)
3d619a20 780 return true;
dde0c674 781
6c139d6e
AL
782 if (Soft == true)
783 P.iFlags |= AutoKept;
784 else
785 P.iFlags &= ~AutoKept;
6c139d6e 786
dde0c674
DK
787 ActionGroup group(*this);
788
32085498
MV
789#if 0 // reseting the autoflag here means we lose the
790 // auto-mark information if a user selects a package for removal
791 // but changes his mind then and sets it for keep again
792 // - this makes sense as default when all Garbage dependencies
793 // are automatically marked for removal (as aptitude does).
794 // setting a package for keep then makes it no longer autoinstalled
1e3f4083 795 // for all other use-case this action is rather surprising
74a05226
MV
796 if(FromUser && !P.Marked)
797 P.Flags &= ~Flag::Auto;
32085498
MV
798#endif
799
af29ffb4 800 if (DebugMarker == true)
84573326 801 std::clog << OutputInDepth(Depth) << "MarkKeep " << APT::PrettyPkg(this, Pkg) << " FU=" << FromUser << std::endl;
af29ffb4 802
6c139d6e
AL
803 RemoveSizes(Pkg);
804 RemoveStates(Pkg);
805
806 P.Mode = ModeKeep;
807 if (Pkg->CurrentVer == 0)
808 P.InstallVer = 0;
809 else
810 P.InstallVer = Pkg.CurrentVer();
811
812 AddStates(Pkg);
6c139d6e 813 Update(Pkg);
6c139d6e 814 AddSizes(Pkg);
3d619a20
DK
815
816 return true;
6c139d6e
AL
817}
818 /*}}}*/
819// DepCache::MarkDelete - Put the package in the delete state /*{{{*/
820// ---------------------------------------------------------------------
821/* */
3d619a20 822bool pkgDepCache::MarkDelete(PkgIterator const &Pkg, bool rPurge,
6910a2ac 823 unsigned long Depth, bool FromUser)
6c139d6e 824{
dde0c674 825 if (IsModeChangeOk(ModeDelete, Pkg, Depth, FromUser) == false)
3d619a20 826 return false;
6c139d6e 827
dde0c674 828 StateCache &P = PkgState[Pkg->ID];
74a05226 829
6c139d6e 830 // Check that it is not already marked for delete
d556d1a1
AL
831 if ((P.Mode == ModeDelete || P.InstallVer == 0) &&
832 (Pkg.Purge() == true || rPurge == false))
3d619a20 833 return true;
6c139d6e 834
dde0c674 835 // check if we are allowed to remove the package
6910a2ac 836 if (IsDeleteOk(Pkg,rPurge,Depth,FromUser) == false)
3d619a20 837 return false;
6910a2ac 838
dde0c674
DK
839 P.iFlags &= ~(AutoKept | Purge);
840 if (rPurge == true)
841 P.iFlags |= Purge;
842
843 ActionGroup group(*this);
844
7c2cc4a7
DK
845 if (FromUser == false)
846 {
847 VerIterator const PV = P.InstVerIter(*this);
848 if (PV.end() == false)
849 {
850 // removed metapackages mark their dependencies as manual to prevent in "desktop depends browser, texteditor"
851 // the removal of browser to suggest the removal of desktop and texteditor.
852 // We ignore the auto-bit here as we can't deal with metapackage cascardes otherwise.
853 // We do not check for or-groups here as we don't know which package takes care of
854 // providing the feature the user likes e.g.: browser1 | browser2 | browser3
855 // Temporary removals are effected by this as well, which is bad, but unlikely in practice
856 bool const PinNeverMarkAutoSection = (PV->Section != 0 && ConfigValueInSubTree("APT::Never-MarkAuto-Sections", PV.Section()));
857 if (PinNeverMarkAutoSection)
858 {
859 for (DepIterator D = PV.DependsList(); D.end() != true; ++D)
860 {
861 if (D.IsMultiArchImplicit() == true || D.IsNegative() == true || IsImportantDep(D) == false)
862 continue;
863
864 pkgCacheFile CacheFile(this);
865 APT::VersionList verlist = APT::VersionList::FromDependency(CacheFile, D, APT::CacheSetHelper::INSTALLED);
866 for (auto const &V : verlist)
867 {
868 PkgIterator const DP = V.ParentPkg();
869 if(DebugAutoInstall == true)
870 std::clog << OutputInDepth(Depth) << "Setting " << DP.FullName(false) << " NOT as auto-installed (direct "
871 << D.DepType() << " of " << Pkg.FullName(false) << " which is in APT::Never-MarkAuto-Sections)" << std::endl;
872
873 MarkAuto(DP, false);
874 }
875 }
876 }
877 }
878 }
879
af29ffb4 880 if (DebugMarker == true)
84573326 881 std::clog << OutputInDepth(Depth) << (rPurge ? "MarkPurge " : "MarkDelete ") << APT::PrettyPkg(this, Pkg) << " FU=" << FromUser << std::endl;
af29ffb4 882
6c139d6e
AL
883 RemoveSizes(Pkg);
884 RemoveStates(Pkg);
885
d556d1a1 886 if (Pkg->CurrentVer == 0 && (Pkg.Purge() == true || rPurge == false))
3d615484
AL
887 P.Mode = ModeKeep;
888 else
889 P.Mode = ModeDelete;
6c139d6e 890 P.InstallVer = 0;
6c139d6e
AL
891
892 AddStates(Pkg);
893 Update(Pkg);
894 AddSizes(Pkg);
803ea2a8 895
3d619a20 896 return true;
6c139d6e
AL
897}
898 /*}}}*/
6910a2ac
DK
899// DepCache::IsDeleteOk - check if it is ok to remove this package /*{{{*/
900// ---------------------------------------------------------------------
dde0c674
DK
901/* The default implementation tries to prevent deletion of install requests.
902 dpkg holds are enforced by the private IsModeChangeOk */
6910a2ac
DK
903bool pkgDepCache::IsDeleteOk(PkgIterator const &Pkg,bool rPurge,
904 unsigned long Depth, bool FromUser)
486d190d
DK
905{
906 return IsDeleteOkProtectInstallRequests(Pkg, rPurge, Depth, FromUser);
907}
908bool pkgDepCache::IsDeleteOkProtectInstallRequests(PkgIterator const &Pkg,
65512241 909 bool const /*rPurge*/, unsigned long const Depth, bool const FromUser)
6910a2ac 910{
dde0c674 911 if (FromUser == false && Pkg->CurrentVer == 0)
0c6aa02e
DK
912 {
913 StateCache &P = PkgState[Pkg->ID];
914 if (P.InstallVer != 0 && P.Status == 2 && (P.Flags & Flag::Auto) != Flag::Auto)
915 {
916 if (DebugMarker == true)
84573326 917 std::clog << OutputInDepth(Depth) << "Manual install request prevents MarkDelete of " << APT::PrettyPkg(this, Pkg) << std::endl;
0c6aa02e
DK
918 return false;
919 }
920 }
dde0c674
DK
921 return true;
922}
923 /*}}}*/
924// DepCache::IsModeChangeOk - check if it is ok to change the mode /*{{{*/
925// ---------------------------------------------------------------------
926/* this is used by all Mark methods on the very first line to check sanity
927 and prevents mode changes for packages on hold for example.
928 If you want to check Mode specific stuff you can use the virtual public
929 Is<Mode>Ok methods instead */
c3ccac92 930static char const* PrintMode(char const mode)
dde0c674
DK
931{
932 switch (mode)
933 {
934 case pkgDepCache::ModeInstall: return "Install";
935 case pkgDepCache::ModeKeep: return "Keep";
936 case pkgDepCache::ModeDelete: return "Delete";
8fa042ca 937 case pkgDepCache::ModeGarbage: return "Garbage";
dde0c674
DK
938 default: return "UNKNOWN";
939 }
940}
941bool pkgDepCache::IsModeChangeOk(ModeList const mode, PkgIterator const &Pkg,
942 unsigned long const Depth, bool const FromUser)
943{
944 // we are not trying to hard…
945 if (unlikely(Depth > 100))
946 return false;
947
948 // general sanity
949 if (unlikely(Pkg.end() == true || Pkg->VersionList == 0))
950 return false;
951
cc26da01
DK
952 // the user is always right
953 if (FromUser == true)
954 return true;
955
956 StateCache &P = PkgState[Pkg->ID];
35f6b9ea
DK
957 // not changing the mode is obviously also fine as we might want to call
958 // e.g. MarkInstall multiple times with different arguments for the same package
959 if (P.Mode == mode)
960 return true;
cc26da01
DK
961
962 // if previous state was set by user only user can reset it
963 if ((P.iFlags & Protected) == Protected)
964 {
35f6b9ea 965 if (unlikely(DebugMarker == true))
cc26da01 966 std::clog << OutputInDepth(Depth) << "Ignore Mark" << PrintMode(mode)
84573326 967 << " of " << APT::PrettyPkg(this, Pkg) << " as its mode (" << PrintMode(P.Mode)
cc26da01
DK
968 << ") is protected" << std::endl;
969 return false;
970 }
dde0c674 971 // enforce dpkg holds
cc26da01 972 else if (mode != ModeKeep && Pkg->SelectedState == pkgCache::State::Hold &&
dde0c674
DK
973 _config->FindB("APT::Ignore-Hold",false) == false)
974 {
35f6b9ea 975 if (unlikely(DebugMarker == true))
dde0c674 976 std::clog << OutputInDepth(Depth) << "Hold prevents Mark" << PrintMode(mode)
84573326 977 << " of " << APT::PrettyPkg(this, Pkg) << std::endl;
dde0c674
DK
978 return false;
979 }
980
6910a2ac
DK
981 return true;
982}
983 /*}}}*/
6c139d6e
AL
984// DepCache::MarkInstall - Put the package in the install state /*{{{*/
985// ---------------------------------------------------------------------
986/* */
2b5c35c7
DK
987struct CompareProviders {
988 pkgCache::PkgIterator const Pkg;
e8afd168 989 explicit CompareProviders(pkgCache::DepIterator const &Dep) : Pkg(Dep.TargetPkg()) {};
2b5c35c7
DK
990 //bool operator() (APT::VersionList::iterator const &AV, APT::VersionList::iterator const &BV)
991 bool operator() (pkgCache::VerIterator const &AV, pkgCache::VerIterator const &BV)
992 {
993 pkgCache::PkgIterator const A = AV.ParentPkg();
994 pkgCache::PkgIterator const B = BV.ParentPkg();
9bfd7b57
DK
995 // Prefer MA:same packages if other architectures for it are installed
996 if ((AV->MultiArch & pkgCache::Version::Same) == pkgCache::Version::Same ||
997 (BV->MultiArch & pkgCache::Version::Same) == pkgCache::Version::Same)
998 {
999 bool instA = false;
1000 if ((AV->MultiArch & pkgCache::Version::Same) == pkgCache::Version::Same)
1001 {
1002 pkgCache::GrpIterator Grp = A.Group();
1003 for (pkgCache::PkgIterator P = Grp.PackageList(); P.end() == false; P = Grp.NextPkg(P))
1004 if (P->CurrentVer != 0)
1005 {
1006 instA = true;
1007 break;
1008 }
1009 }
1010 bool instB = false;
1011 if ((BV->MultiArch & pkgCache::Version::Same) == pkgCache::Version::Same)
1012 {
1013 pkgCache::GrpIterator Grp = B.Group();
1014 for (pkgCache::PkgIterator P = Grp.PackageList(); P.end() == false; P = Grp.NextPkg(P))
1015 {
1016 if (P->CurrentVer != 0)
1017 {
1018 instB = true;
1019 break;
1020 }
1021 }
1022 }
1023 if (instA != instB)
1024 return instA == false;
1025 }
529bf9b0
DK
1026 if ((A->CurrentVer == 0 || B->CurrentVer == 0) && A->CurrentVer != B->CurrentVer)
1027 return A->CurrentVer == 0;
2b5c35c7
DK
1028 // Prefer packages in the same group as the target; e.g. foo:i386, foo:amd64
1029 if (A->Group != B->Group)
1030 {
1031 if (A->Group == Pkg->Group && B->Group != Pkg->Group)
1032 return false;
1033 else if (B->Group == Pkg->Group && A->Group != Pkg->Group)
1034 return true;
1035 }
1036 // we like essentials
1037 if ((A->Flags & pkgCache::Flag::Essential) != (B->Flags & pkgCache::Flag::Essential))
1038 {
1039 if ((A->Flags & pkgCache::Flag::Essential) == pkgCache::Flag::Essential)
1040 return false;
1041 else if ((B->Flags & pkgCache::Flag::Essential) == pkgCache::Flag::Essential)
1042 return true;
1043 }
c5200869
JAK
1044 if ((A->Flags & pkgCache::Flag::Important) != (B->Flags & pkgCache::Flag::Important))
1045 {
1046 if ((A->Flags & pkgCache::Flag::Important) == pkgCache::Flag::Important)
1047 return false;
1048 else if ((B->Flags & pkgCache::Flag::Important) == pkgCache::Flag::Important)
1049 return true;
1050 }
2b5c35c7
DK
1051 // prefer native architecture
1052 if (strcmp(A.Arch(), B.Arch()) != 0)
1053 {
1054 if (strcmp(A.Arch(), A.Cache()->NativeArch()) == 0)
1055 return false;
1056 else if (strcmp(B.Arch(), B.Cache()->NativeArch()) == 0)
1057 return true;
1058 std::vector<std::string> archs = APT::Configuration::getArchitectures();
1059 for (std::vector<std::string>::const_iterator a = archs.begin(); a != archs.end(); ++a)
1060 if (*a == A.Arch())
1061 return false;
1062 else if (*a == B.Arch())
1063 return true;
1064 }
096bd9f5
CW
1065 // higher priority seems like a good idea
1066 if (AV->Priority != BV->Priority)
1067 return AV->Priority > BV->Priority;
2b5c35c7
DK
1068 // unable to decide…
1069 return A->ID < B->ID;
1070 }
1071};
3d619a20 1072bool pkgDepCache::MarkInstall(PkgIterator const &Pkg,bool AutoInst,
7610bb3d
MV
1073 unsigned long Depth, bool FromUser,
1074 bool ForceImportantDeps)
b2e465d6 1075{
dde0c674 1076 if (IsModeChangeOk(ModeInstall, Pkg, Depth, FromUser) == false)
3d619a20 1077 return false;
dde0c674
DK
1078
1079 StateCache &P = PkgState[Pkg->ID];
1080
1081 // See if there is even any possible instalation candidate
1082 if (P.CandidateVer == 0)
3d619a20 1083 return false;
74a05226 1084
6c139d6e
AL
1085 /* Check that it is not already marked for install and that it can be
1086 installed */
60681f93
MV
1087 if ((P.InstPolicyBroken() == false && P.InstBroken() == false) &&
1088 (P.Mode == ModeInstall ||
6c139d6e
AL
1089 P.CandidateVer == (Version *)Pkg.CurrentVer()))
1090 {
1091 if (P.CandidateVer == (Version *)Pkg.CurrentVer() && P.InstallVer == 0)
3d619a20
DK
1092 return MarkKeep(Pkg, false, FromUser, Depth+1);
1093 return true;
6c139d6e 1094 }
b2e465d6 1095
be7ce6f1
DK
1096 // check if we are allowed to install the package
1097 if (IsInstallOk(Pkg,AutoInst,Depth,FromUser) == false)
1098 return false;
6910a2ac 1099
dde0c674
DK
1100 ActionGroup group(*this);
1101 P.iFlags &= ~AutoKept;
1102
6c139d6e
AL
1103 /* Target the candidate version and remove the autoflag. We reset the
1104 autoflag below if this was called recursively. Otherwise the user
1105 should have the ability to de-auto a package by changing its state */
1106 RemoveSizes(Pkg);
1107 RemoveStates(Pkg);
1108
1109 P.Mode = ModeInstall;
1110 P.InstallVer = P.CandidateVer;
74a05226
MV
1111
1112 if(FromUser)
1113 {
e26a777c
DK
1114 // Set it to manual if it's a new install or already installed,
1115 // but only if its not marked by the autoremover (aptitude depend on this behavior)
1116 // or if we do automatic installation (aptitude never does it)
1117 if(P.Status == 2 || (Pkg->CurrentVer != 0 && (AutoInst == true || P.Marked == false)))
74a05226
MV
1118 P.Flags &= ~Flag::Auto;
1119 }
1120 else
1121 {
1122 // Set it to auto if this is a new install.
1123 if(P.Status == 2)
1124 P.Flags |= Flag::Auto;
1125 }
6c139d6e
AL
1126 if (P.CandidateVer == (Version *)Pkg.CurrentVer())
1127 P.Mode = ModeKeep;
1128
1129 AddStates(Pkg);
1130 Update(Pkg);
1131 AddSizes(Pkg);
6910a2ac 1132
98278a81 1133 if (AutoInst == false || _config->Find("APT::Solver", "internal") != "internal")
3d619a20 1134 return true;
6c139d6e 1135
af29ffb4 1136 if (DebugMarker == true)
84573326 1137 std::clog << OutputInDepth(Depth) << "MarkInstall " << APT::PrettyPkg(this, Pkg) << " FU=" << FromUser << std::endl;
af29ffb4 1138
5f4495e3 1139 bool MoveAutoBitToDependencies = false;
3cbeed98
DK
1140 VerIterator const PV = P.InstVerIter(*this);
1141 if (unlikely(PV.end() == true))
1142 return false;
5f4495e3
DK
1143 else if (PV->Section != 0 && (P.Flags & Flag::Auto) != Flag::Auto)
1144 {
1145 VerIterator const CurVer = Pkg.CurrentVer();
1146 if (CurVer.end() == false && CurVer->Section != 0 && strcmp(CurVer.Section(), PV.Section()) != 0)
1147 {
1148 bool const CurVerInMoveSection = ConfigValueInSubTree("APT::Move-Autobit-Sections", CurVer.Section());
1149 bool const InstVerInMoveSection = ConfigValueInSubTree("APT::Move-Autobit-Sections", PV.Section());
1150 MoveAutoBitToDependencies = (CurVerInMoveSection == false && InstVerInMoveSection == true);
1151 if (MoveAutoBitToDependencies == true)
1152 {
1153 if(DebugAutoInstall == true)
1154 std::clog << OutputInDepth(Depth) << "Setting " << Pkg.FullName(false) << " as auto-installed, moving manual to its dependencies" << std::endl;
1155 MarkAuto(Pkg, true);
1156 }
1157 }
1158 }
3cbeed98
DK
1159
1160 DepIterator Dep = PV.DependsList();
6c139d6e
AL
1161 for (; Dep.end() != true;)
1162 {
1163 // Grok or groups
1164 DepIterator Start = Dep;
1165 bool Result = true;
b2e465d6 1166 unsigned Ors = 0;
f7f0d6c7 1167 for (bool LastOR = true; Dep.end() == false && LastOR == true; ++Dep, ++Ors)
6c139d6e
AL
1168 {
1169 LastOR = (Dep->CompareOp & Dep::Or) == Dep::Or;
1170
1171 if ((DepState[Dep->ID] & DepInstall) == DepInstall)
1172 Result = false;
1173 }
1174
1175 // Dep is satisfied okay.
1176 if (Result == false)
1177 continue;
1178
1179 /* Check if this dep should be consider for install. If it is a user
21b3eac8 1180 defined important dep and we are installed a new package then
4ef9a929 1181 it will be installed. Otherwise we only check for important
21b3eac8 1182 deps that have changed from the installed version */
6c139d6e
AL
1183 if (IsImportantDep(Start) == false)
1184 continue;
e92f6a18 1185
21b3eac8
DK
1186 /* If we are in an or group locate the first or that can
1187 succeed. We have already cached this… */
e92f6a18
DK
1188 for (; Ors > 1 && (DepState[Start->ID] & DepCVer) != DepCVer; --Ors)
1189 ++Start;
21b3eac8
DK
1190
1191 /* unsatisfiable dependency: IsInstallOkDependenciesSatisfiableByCandidates
1192 would have prevented us to get here if not overridden, so just skip
add81166 1193 over the problem here as the front-end will know what it is doing */
e92f6a18 1194 if (Ors == 1 && (DepState[Start->ID] &DepCVer) != DepCVer && Start.IsNegative() == false)
21b3eac8 1195 continue;
e92f6a18 1196
0526822a
DB
1197 /* Check if any ImportantDep() (but not Critical) were added
1198 * since we installed the package. Also check for deps that
1199 * were satisfied in the past: for instance, if a version
1200 * restriction in a Recommends was tightened, upgrading the
1201 * package should follow that Recommends rather than causing the
1202 * dependency to be removed. (bug #470115)
1b1c2224 1203 */
6ed08516 1204 if (Pkg->CurrentVer != 0 && ForceImportantDeps == false && Start.IsCritical() == false)
1b1c2224 1205 {
6ed08516
DK
1206 bool isNewImportantDep = true;
1207 bool isPreviouslySatisfiedImportantDep = false;
1208 for (DepIterator D = Pkg.CurrentVer().DependsList(); D.end() != true; ++D)
1d722933 1209 {
6ed08516
DK
1210 //FIXME: Should we handle or-group better here?
1211 // We do not check if the package we look for is part of the same or-group
1212 // we might find while searching, but could that really be a problem?
1213 if (D.IsCritical() == true || IsImportantDep(D) == false ||
1214 Start.TargetPkg() != D.TargetPkg())
1215 continue;
1216
1217 isNewImportantDep = false;
1218
1219 while ((D->CompareOp & Dep::Or) != 0)
1220 ++D;
1221
1222 isPreviouslySatisfiedImportantDep = (((*this)[D] & DepGNow) != 0);
1223 if (isPreviouslySatisfiedImportantDep == true)
1224 break;
1225 }
1226
1227 if(isNewImportantDep == true)
1228 {
1229 if (DebugAutoInstall == true)
1230 std::clog << OutputInDepth(Depth) << "new important dependency: "
1231 << Start.TargetPkg().FullName() << std::endl;
1232 }
1233 else if(isPreviouslySatisfiedImportantDep == true)
1234 {
1235 if (DebugAutoInstall == true)
1236 std::clog << OutputInDepth(Depth) << "previously satisfied important dependency on "
1237 << Start.TargetPkg().FullName() << std::endl;
1238 }
1239 else
1240 {
1241 if (DebugAutoInstall == true)
1242 std::clog << OutputInDepth(Depth) << "ignore old unsatisfied important dependency on "
1243 << Start.TargetPkg().FullName() << std::endl;
1244 continue;
1d722933 1245 }
1b1c2224 1246 }
6ed08516 1247
1e3f4083 1248 /* This bit is for processing the possibility of an install/upgrade
074da097
DK
1249 fixing the problem for "positive" dependencies */
1250 if (Start.IsNegative() == false && (DepState[Start->ID] & DepCVer) == DepCVer)
6c139d6e 1251 {
9112f777
DK
1252 pkgCacheFile CacheFile(this);
1253 APT::VersionList verlist = APT::VersionList::FromDependency(CacheFile, Start, APT::CacheSetHelper::CANDIDATE);
2b5c35c7 1254 CompareProviders comp(Start);
b2e465d6 1255
66706285
DK
1256 do {
1257 APT::VersionList::iterator InstVer = std::max_element(verlist.begin(), verlist.end(), comp);
1258
1259 if (InstVer == verlist.end())
1260 break;
1261
2b5c35c7 1262 pkgCache::PkgIterator InstPkg = InstVer.ParentPkg();
af29ffb4
MV
1263 if(DebugAutoInstall == true)
1264 std::clog << OutputInDepth(Depth) << "Installing " << InstPkg.Name()
1265 << " as " << Start.DepType() << " of " << Pkg.Name()
d4c5f11f 1266 << std::endl;
66706285
DK
1267 if (MarkInstall(InstPkg, true, Depth + 1, false, ForceImportantDeps) == false)
1268 {
1269 verlist.erase(InstVer);
1270 continue;
1271 }
5f4495e3
DK
1272
1273 // now check if we should consider it a automatic dependency or not
1274 if(InstPkg->CurrentVer == 0 && MoveAutoBitToDependencies)
1275 {
1276 if(DebugAutoInstall == true)
1277 std::clog << OutputInDepth(Depth) << "Setting " << InstPkg.FullName(false) << " NOT as auto-installed (direct "
1278 << Start.DepType() << " of " << Pkg.FullName(false) << " which is manual and in APT::Move-Autobit-Sections)" << std::endl;
1279 MarkAuto(InstPkg, false);
1280 }
1281
1282
66706285
DK
1283 break;
1284 } while(true);
6c139d6e
AL
1285 continue;
1286 }
074da097
DK
1287 /* Negative dependencies have no or-group
1288 If the dependency isn't versioned, we try if an upgrade might solve the problem.
1289 Otherwise we remove the offender if needed */
1290 else if (Start.IsNegative() == true && Start->Type != pkgCache::Dep::Obsoletes)
6c139d6e 1291 {
98cc7fd2 1292 std::unique_ptr<Version *[]> List(Start.AllTargets());
074da097 1293 pkgCache::PkgIterator TrgPkg = Start.TargetPkg();
98cc7fd2 1294 for (Version **I = List.get(); *I != 0; I++)
6c139d6e
AL
1295 {
1296 VerIterator Ver(*this,*I);
1297 PkgIterator Pkg = Ver.ParentPkg();
308c7d30 1298
edbda33b
DK
1299 /* The List includes all packages providing this dependency,
1300 even providers which are not installed, so skip them. */
1301 if (PkgState[Pkg->ID].InstallVer == 0)
1302 continue;
1303
0dfc7eef
MV
1304 /* Ignore negative dependencies that we are not going to
1305 get installed */
1306 if (PkgState[Pkg->ID].InstallVer != *I)
1307 continue;
1308
074da097
DK
1309 if ((Start->Version != 0 || TrgPkg != Pkg) &&
1310 PkgState[Pkg->ID].CandidateVer != PkgState[Pkg->ID].InstallVer &&
1311 PkgState[Pkg->ID].CandidateVer != *I &&
a16dec4d
DK
1312 MarkInstall(Pkg,true,Depth + 1, false, ForceImportantDeps) == true)
1313 continue;
06f88117
MV
1314 else if (Start->Type == pkgCache::Dep::Conflicts ||
1315 Start->Type == pkgCache::Dep::DpkgBreaks)
1316 {
1317 if(DebugAutoInstall == true)
1318 std::clog << OutputInDepth(Depth)
1319 << " Removing: " << Pkg.Name()
1320 << std::endl;
1321 if (MarkDelete(Pkg,false,Depth + 1, false) == false)
1322 break;
1323 }
6c139d6e 1324 }
6c139d6e 1325 continue;
074da097 1326 }
6c139d6e 1327 }
3d619a20 1328
a16dec4d 1329 return Dep.end() == true;
6c139d6e 1330}
92fcbfc1 1331 /*}}}*/
6910a2ac 1332// DepCache::IsInstallOk - check if it is ok to install this package /*{{{*/
2d403b92 1333// ---------------------------------------------------------------------
486d190d
DK
1334/* The default implementation checks if the installation of an M-A:same
1335 package would lead us into a version-screw and if so forbids it.
dde0c674 1336 dpkg holds are enforced by the private IsModeChangeOk */
6910a2ac
DK
1337bool pkgDepCache::IsInstallOk(PkgIterator const &Pkg,bool AutoInst,
1338 unsigned long Depth, bool FromUser)
2d403b92 1339{
21b3eac8
DK
1340 return IsInstallOkMultiArchSameVersionSynced(Pkg,AutoInst, Depth, FromUser) &&
1341 IsInstallOkDependenciesSatisfiableByCandidates(Pkg,AutoInst, Depth, FromUser);
486d190d
DK
1342}
1343bool pkgDepCache::IsInstallOkMultiArchSameVersionSynced(PkgIterator const &Pkg,
65512241 1344 bool const /*AutoInst*/, unsigned long const Depth, bool const FromUser)
486d190d
DK
1345{
1346 if (FromUser == true) // as always: user is always right
1347 return true;
1348
be7ce6f1
DK
1349 // if we have checked before and it was okay, it will still be okay
1350 if (PkgState[Pkg->ID].Mode == ModeInstall &&
1351 PkgState[Pkg->ID].InstallVer == PkgState[Pkg->ID].CandidateVer)
1352 return true;
1353
486d190d
DK
1354 // ignore packages with none-M-A:same candidates
1355 VerIterator const CandVer = PkgState[Pkg->ID].CandidateVerIter(*this);
1356 if (unlikely(CandVer.end() == true) || CandVer == Pkg.CurrentVer() ||
1357 (CandVer->MultiArch & pkgCache::Version::Same) != pkgCache::Version::Same)
1358 return true;
1359
1360 GrpIterator const Grp = Pkg.Group();
1361 for (PkgIterator P = Grp.PackageList(); P.end() == false; P = Grp.NextPkg(P))
1362 {
98c9c26c
DK
1363 // not installed or self-check: fine by definition
1364 if (P->CurrentVer == 0 || P == Pkg)
1365 continue;
1366
1367 // not having a candidate or being in sync
486d190d 1368 // (simple string-compare as stuff like '1' == '0:1-0' can't happen here)
98c9c26c 1369 VerIterator CV = PkgState[P->ID].CandidateVerIter(*this);
91414dd7 1370 if (CV.end() == true || strcmp(CandVer.VerStr(), CV.VerStr()) == 0)
486d190d 1371 continue;
98c9c26c 1372
1e3f4083 1373 // packages losing M-A:same can be out-of-sync
98c9c26c 1374 if ((CV->MultiArch & pkgCache::Version::Same) != pkgCache::Version::Same)
486d190d
DK
1375 continue;
1376
1377 // not downloadable means the package is obsolete, so allow out-of-sync
1378 if (CV.Downloadable() == false)
1379 continue;
1380
1381 PkgState[Pkg->ID].iFlags |= AutoKept;
1382 if (unlikely(DebugMarker == true))
84573326
DK
1383 std::clog << OutputInDepth(Depth) << "Ignore MarkInstall of " << APT::PrettyPkg(this, Pkg)
1384 << " as it is not in sync with its M-A:same sibling " << APT::PrettyPkg(this, P)
91414dd7 1385 << " (" << CandVer.VerStr() << " != " << CV.VerStr() << ")" << std::endl;
486d190d
DK
1386 return false;
1387 }
1388
21b3eac8
DK
1389 return true;
1390}
1391bool pkgDepCache::IsInstallOkDependenciesSatisfiableByCandidates(PkgIterator const &Pkg,
1392 bool const AutoInst, unsigned long const Depth, bool const /*FromUser*/)
1393{
1394 if (AutoInst == false)
1395 return true;
1396
1397 VerIterator const CandVer = PkgState[Pkg->ID].CandidateVerIter(*this);
1398 if (unlikely(CandVer.end() == true) || CandVer == Pkg.CurrentVer())
1399 return true;
1400
1401 for (DepIterator Dep = CandVer.DependsList(); Dep.end() != true;)
1402 {
1403 // Grok or groups
1404 DepIterator Start = Dep;
1405 bool Result = true;
1406 unsigned Ors = 0;
1407 for (bool LastOR = true; Dep.end() == false && LastOR == true; ++Dep, ++Ors)
1408 {
1409 LastOR = (Dep->CompareOp & Dep::Or) == Dep::Or;
1410
1411 if ((DepState[Dep->ID] & DepInstall) == DepInstall)
1412 Result = false;
1413 }
1414
1415 if (Start.IsCritical() == false || Start.IsNegative() == true || Result == false)
1416 continue;
1417
1418 /* If we are in an or group locate the first or that can succeed.
1419 We have already cached this… */
1420 for (; Ors > 1 && (DepState[Start->ID] & DepCVer) != DepCVer; --Ors)
1421 ++Start;
1422
1423 if (Ors == 1 && (DepState[Start->ID] &DepCVer) != DepCVer)
1424 {
1425 if (DebugAutoInstall == true)
84573326 1426 std::clog << OutputInDepth(Depth) << APT::PrettyDep(this, Start) << " can't be satisfied!" << std::endl;
21b3eac8
DK
1427
1428 // the dependency is critical, but can't be installed, so discard the candidate
1429 // as the problemresolver will trip over it otherwise trying to install it (#735967)
e41d3d7e 1430 if (Pkg->CurrentVer != 0 && (PkgState[Pkg->ID].iFlags & Protected) != Protected)
0390edd5 1431 {
21b3eac8 1432 SetCandidateVersion(Pkg.CurrentVer());
0390edd5 1433 StateCache &State = PkgState[Pkg->ID];
6df56323
JAK
1434 if (State.Mode != ModeDelete)
1435 {
1436 State.Mode = ModeKeep;
1437 State.Update(Pkg, *this);
1438 }
0390edd5 1439 }
21b3eac8
DK
1440 return false;
1441 }
1442 }
1443
6910a2ac 1444 return true;
2d403b92
MV
1445}
1446 /*}}}*/
d0c59649
AL
1447// DepCache::SetReInstall - Set the reinstallation flag /*{{{*/
1448// ---------------------------------------------------------------------
1449/* */
1450void pkgDepCache::SetReInstall(PkgIterator const &Pkg,bool To)
1451{
1019948c
DK
1452 if (unlikely(Pkg.end() == true))
1453 return;
1454
9535a4db
DK
1455 APT::PackageList pkglist;
1456 if (Pkg->CurrentVer != 0 &&
1457 (Pkg.CurrentVer()-> MultiArch & pkgCache::Version::Same) == pkgCache::Version::Same)
1458 {
1459 pkgCache::GrpIterator Grp = Pkg.Group();
1460 for (pkgCache::PkgIterator P = Grp.PackageList(); P.end() == false; P = Grp.NextPkg(P))
1461 {
1462 if (P->CurrentVer != 0)
1463 pkglist.insert(P);
1464 }
1465 }
1466 else
1467 pkglist.insert(Pkg);
1468
74a05226
MV
1469 ActionGroup group(*this);
1470
9535a4db
DK
1471 for (APT::PackageList::const_iterator Pkg = pkglist.begin(); Pkg != pkglist.end(); ++Pkg)
1472 {
1473 RemoveSizes(Pkg);
1474 RemoveStates(Pkg);
1475
1476 StateCache &P = PkgState[Pkg->ID];
1477 if (To == true)
1478 P.iFlags |= ReInstall;
1479 else
1480 P.iFlags &= ~ReInstall;
1481
1482 AddStates(Pkg);
1483 AddSizes(Pkg);
1484 }
d0c59649
AL
1485}
1486 /*}}}*/
294a8020
DK
1487pkgCache::VerIterator pkgDepCache::GetCandidateVersion(PkgIterator const &Pkg)/*{{{*/
1488{
1489 return PkgState[Pkg->ID].CandidateVerIter(*this);
1490}
1491 /*}}}*/
b2e465d6
AL
1492// DepCache::SetCandidateVersion - Change the candidate version /*{{{*/
1493// ---------------------------------------------------------------------
1494/* */
27e8981a 1495void pkgDepCache::SetCandidateVersion(VerIterator TargetVer)
b2e465d6
AL
1496{
1497 pkgCache::PkgIterator Pkg = TargetVer.ParentPkg();
1498 StateCache &P = PkgState[Pkg->ID];
74a05226 1499
08fa1cab
DK
1500 if (P.CandidateVer == TargetVer)
1501 return;
1502
1503 ActionGroup group(*this);
1504
b2e465d6
AL
1505 RemoveSizes(Pkg);
1506 RemoveStates(Pkg);
1507
758729c8 1508 if (P.CandidateVer == P.InstallVer && P.Install() == true)
b2e465d6
AL
1509 P.InstallVer = (Version *)TargetVer;
1510 P.CandidateVer = (Version *)TargetVer;
1511 P.Update(Pkg,*this);
1512
1513 AddStates(Pkg);
1514 Update(Pkg);
1515 AddSizes(Pkg);
1019948c 1516
b2e465d6 1517}
08fa1cab 1518 /*}}}*/
2c085486
DK
1519// DepCache::SetCandidateRelease - Change the candidate version /*{{{*/
1520// ---------------------------------------------------------------------
1521/* changes the candidate of a package and walks over all its dependencies
1522 to check if it needs to change the candidate of the dependency, too,
1523 to reach a installable versionstate */
1524bool pkgDepCache::SetCandidateRelease(pkgCache::VerIterator TargetVer,
1525 std::string const &TargetRel)
1526{
1527 std::list<std::pair<pkgCache::VerIterator, pkgCache::VerIterator> > Changed;
1528 return SetCandidateRelease(TargetVer, TargetRel, Changed);
1529}
1530bool pkgDepCache::SetCandidateRelease(pkgCache::VerIterator TargetVer,
1531 std::string const &TargetRel,
1532 std::list<std::pair<pkgCache::VerIterator, pkgCache::VerIterator> > &Changed)
1533{
758729c8 1534 ActionGroup group(*this);
2c085486
DK
1535 SetCandidateVersion(TargetVer);
1536
1537 if (TargetRel == "installed" || TargetRel == "candidate") // both doesn't make sense in this context
1538 return true;
1539
1540 pkgVersionMatch Match(TargetRel, pkgVersionMatch::Release);
1541 // save the position of the last element we will not undo - if we have to
1542 std::list<std::pair<pkgCache::VerIterator, pkgCache::VerIterator> >::iterator newChanged = --(Changed.end());
1543
1544 for (pkgCache::DepIterator D = TargetVer.DependsList(); D.end() == false; ++D)
1545 {
1546 if (D->Type != pkgCache::Dep::PreDepends && D->Type != pkgCache::Dep::Depends &&
1547 ((D->Type != pkgCache::Dep::Recommends && D->Type != pkgCache::Dep::Suggests) ||
1548 IsImportantDep(D) == false))
1549 continue;
1550
1551 // walk over an or-group and check if we need to do anything
1552 // for simpilicity no or-group is handled as a or-group including one dependency
1553 pkgCache::DepIterator Start = D;
1554 bool itsFine = false;
1555 for (bool stillOr = true; stillOr == true; ++Start)
1556 {
1557 stillOr = (Start->CompareOp & Dep::Or) == Dep::Or;
1558 pkgCache::PkgIterator const P = Start.TargetPkg();
1559 // virtual packages can't be a solution
1560 if (P.end() == true || (P->ProvidesList == 0 && P->VersionList == 0))
1561 continue;
1562 pkgCache::VerIterator const Cand = PkgState[P->ID].CandidateVerIter(*this);
1563 // no versioned dependency - but is it installable?
1564 if (Start.TargetVer() == 0 || Start.TargetVer()[0] == '\0')
1565 {
1566 // Check if one of the providers is installable
1567 if (P->ProvidesList != 0)
1568 {
1569 pkgCache::PrvIterator Prv = P.ProvidesList();
1570 for (; Prv.end() == false; ++Prv)
1571 {
1572 pkgCache::VerIterator const C = PkgState[Prv.OwnerPkg()->ID].CandidateVerIter(*this);
1573 if (C.end() == true || C != Prv.OwnerVer() ||
1574 (VersionState(C.DependsList(), DepInstall, DepCandMin, DepCandPolicy) & DepCandMin) != DepCandMin)
1575 continue;
1576 break;
1577 }
1578 if (Prv.end() == true)
1579 continue;
1580 }
1581 // no providers, so check if we have an installable candidate version
1582 else if (Cand.end() == true ||
1583 (VersionState(Cand.DependsList(), DepInstall, DepCandMin, DepCandPolicy) & DepCandMin) != DepCandMin)
1584 continue;
1585 itsFine = true;
1586 break;
1587 }
1588 if (Cand.end() == true)
1589 continue;
1590 // check if the current candidate is enough for the versioned dependency - and installable?
887c6940 1591 if (Start.IsSatisfied(Cand) == true &&
2c085486
DK
1592 (VersionState(Cand.DependsList(), DepInstall, DepCandMin, DepCandPolicy) & DepCandMin) == DepCandMin)
1593 {
1594 itsFine = true;
1595 break;
1596 }
1597 }
1598
1599 if (itsFine == true) {
1600 // something in the or-group was fine, skip all other members
1601 for (; (D->CompareOp & Dep::Or) == Dep::Or; ++D);
1602 continue;
1603 }
1604
1605 // walk again over the or-group and check each if a candidate switch would help
1606 itsFine = false;
1607 for (bool stillOr = true; stillOr == true; ++D)
1608 {
1609 stillOr = (D->CompareOp & Dep::Or) == Dep::Or;
1610 // changing candidate will not help if the dependency is not versioned
1611 if (D.TargetVer() == 0 || D.TargetVer()[0] == '\0')
1612 {
1613 if (stillOr == true)
1614 continue;
1615 break;
1616 }
1617
1618 pkgCache::VerIterator V;
1619 if (TargetRel == "newest")
1620 V = D.TargetPkg().VersionList();
1621 else
1622 V = Match.Find(D.TargetPkg());
1623
1624 // check if the version from this release could satisfy the dependency
887c6940 1625 if (V.end() == true || D.IsSatisfied(V) == false)
2c085486
DK
1626 {
1627 if (stillOr == true)
1628 continue;
1629 break;
1630 }
1631
1632 pkgCache::VerIterator oldCand = PkgState[D.TargetPkg()->ID].CandidateVerIter(*this);
1633 if (V == oldCand)
1634 {
1635 // Do we already touched this Version? If so, their versioned dependencies are okay, no need to check again
1636 for (std::list<std::pair<pkgCache::VerIterator, pkgCache::VerIterator> >::const_iterator c = Changed.begin();
1637 c != Changed.end(); ++c)
1638 {
1639 if (c->first->ParentPkg != V->ParentPkg)
1640 continue;
1641 itsFine = true;
1642 break;
1643 }
1644 }
1645
1646 if (itsFine == false)
1647 {
1648 // change the candidate
2f5ed336 1649 Changed.push_back(make_pair(V, TargetVer));
2c085486
DK
1650 if (SetCandidateRelease(V, TargetRel, Changed) == false)
1651 {
1652 if (stillOr == false)
1653 break;
1654 // undo the candidate changing
1655 SetCandidateVersion(oldCand);
1656 Changed.pop_back();
1657 continue;
1658 }
1659 itsFine = true;
1660 }
1661
1662 // something in the or-group was fine, skip all other members
1663 for (; (D->CompareOp & Dep::Or) == Dep::Or; ++D);
1664 break;
1665 }
1666
1667 if (itsFine == false && (D->Type == pkgCache::Dep::PreDepends || D->Type == pkgCache::Dep::Depends))
1668 {
1669 // undo all changes which aren't lead to a solution
1670 for (std::list<std::pair<pkgCache::VerIterator, pkgCache::VerIterator> >::const_iterator c = ++newChanged;
1671 c != Changed.end(); ++c)
1672 SetCandidateVersion(c->first);
1673 Changed.erase(newChanged, Changed.end());
1674 return false;
1675 }
1676 }
1677 return true;
1678}
1679 /*}}}*/
08fa1cab
DK
1680// DepCache::MarkAuto - set the Auto flag for a package /*{{{*/
1681// ---------------------------------------------------------------------
1682/* */
74a05226
MV
1683void pkgDepCache::MarkAuto(const PkgIterator &Pkg, bool Auto)
1684{
1685 StateCache &state = PkgState[Pkg->ID];
1686
1687 ActionGroup group(*this);
1688
1689 if(Auto)
1690 state.Flags |= Flag::Auto;
1691 else
1692 state.Flags &= ~Flag::Auto;
1693}
b2e465d6 1694 /*}}}*/
6c139d6e
AL
1695// StateCache::Update - Compute the various static display things /*{{{*/
1696// ---------------------------------------------------------------------
1697/* This is called whenever the Candidate version changes. */
1698void pkgDepCache::StateCache::Update(PkgIterator Pkg,pkgCache &Cache)
1699{
1700 // Some info
1701 VerIterator Ver = CandidateVerIter(Cache);
1702
1703 // Use a null string or the version string
1704 if (Ver.end() == true)
1705 CandVersion = "";
1706 else
1707 CandVersion = Ver.VerStr();
1708
1709 // Find the current version
1710 CurVersion = "";
1711 if (Pkg->CurrentVer != 0)
1712 CurVersion = Pkg.CurrentVer().VerStr();
a38cec81 1713
6c139d6e
AL
1714 // Figure out if its up or down or equal
1715 Status = Ver.CompareVer(Pkg.CurrentVer());
1716 if (Pkg->CurrentVer == 0 || Pkg->VersionList == 0 || CandidateVer == 0)
1717 Status = 2;
1718}
1719 /*}}}*/
1720// StateCache::StripEpoch - Remove the epoch specifier from the version /*{{{*/
1721// ---------------------------------------------------------------------
1722/* */
1723const char *pkgDepCache::StateCache::StripEpoch(const char *Ver)
1724{
1725 if (Ver == 0)
1726 return 0;
1727
1728 // Strip any epoch
fd23676e
DK
1729 char const * const I = strchr(Ver, ':');
1730 if (I == nullptr)
1731 return Ver;
1732 return I + 1;
6c139d6e
AL
1733}
1734 /*}}}*/
b2e465d6 1735// Policy::GetCandidateVer - Returns the Candidate install version /*{{{*/
6321777b 1736// ---------------------------------------------------------------------
b2e465d6
AL
1737/* The default just returns the highest available version that is not
1738 a source and automatic. */
e841200b 1739pkgCache::VerIterator pkgDepCache::Policy::GetCandidateVer(PkgIterator const &Pkg)
6321777b 1740{
b2e465d6
AL
1741 /* Not source/not automatic versions cannot be a candidate version
1742 unless they are already installed */
de6221c9 1743 VerIterator Last;
6321777b 1744
f7f0d6c7 1745 for (VerIterator I = Pkg.VersionList(); I.end() == false; ++I)
b2e465d6
AL
1746 {
1747 if (Pkg.CurrentVer() == I)
1748 return I;
1749
f7f0d6c7 1750 for (VerFileIterator J = I.FileList(); J.end() == false; ++J)
b2e465d6 1751 {
b07aeb1a 1752 if (J.File().Flagged(Flag::NotSource))
b2e465d6
AL
1753 continue;
1754
1755 /* Stash the highest version of a not-automatic source, we use it
1756 if there is nothing better */
b07aeb1a
DK
1757 if (J.File().Flagged(Flag::NotAutomatic) ||
1758 J.File().Flagged(Flag::ButAutomaticUpgrades))
b2e465d6
AL
1759 {
1760 if (Last.end() == true)
1761 Last = I;
1762 continue;
1763 }
1764
1765 return I;
1766 }
1767 }
6321777b 1768
b2e465d6
AL
1769 return Last;
1770}
1771 /*}}}*/
1772// Policy::IsImportantDep - True if the dependency is important /*{{{*/
1773// ---------------------------------------------------------------------
1774/* */
fd23676e 1775bool pkgDepCache::Policy::IsImportantDep(DepIterator const &Dep) const
b2e465d6 1776{
60681f93
MV
1777 if(Dep.IsCritical())
1778 return true;
fd23676e 1779 else if(Dep->Type == pkgCache::Dep::Recommends)
1d722933 1780 {
b20c1683 1781 if (InstallRecommends)
1d722933
MV
1782 return true;
1783 // we suport a special mode to only install-recommends for certain
1784 // sections
fd23676e 1785 // FIXME: this is a meant as a temporarly solution until the
1d722933 1786 // recommends are cleaned up
cb1933df
MV
1787 const char *sec = Dep.ParentVer().Section();
1788 if (sec && ConfigValueInSubTree("APT::Install-Recommends-Sections", sec))
1789 return true;
1d722933 1790 }
60681f93 1791 else if(Dep->Type == pkgCache::Dep::Suggests)
b20c1683 1792 return InstallSuggests;
60681f93 1793
74a05226
MV
1794 return false;
1795}
92fcbfc1 1796 /*}}}*/
6d38011b 1797// Policy::GetPriority - Get the priority of the package pin /*{{{*/
a02db58f 1798APT_CONST signed short pkgDepCache::Policy::GetPriority(pkgCache::PkgIterator const &/*Pkg*/)
d3e8fbb3 1799{ return 0; }
a0a66955
JAK
1800APT_CONST signed short pkgDepCache::Policy::GetPriority(pkgCache::VerIterator const &/*Ver*/, bool /*ConsiderFiles*/)
1801{ return 0; }
a02db58f 1802APT_CONST signed short pkgDepCache::Policy::GetPriority(pkgCache::PkgFileIterator const &/*File*/)
d3e8fbb3 1803{ return 0; }
6d38011b 1804 /*}}}*/
92fcbfc1 1805pkgDepCache::InRootSetFunc *pkgDepCache::GetRootSetFunc() /*{{{*/
74a05226
MV
1806{
1807 DefaultRootSetFunc *f = new DefaultRootSetFunc;
1808 if(f->wasConstructedSuccessfully())
1809 return f;
1810 else
1811 {
1812 delete f;
1813 return NULL;
1814 }
1815}
92fcbfc1 1816 /*}}}*/
74a05226
MV
1817bool pkgDepCache::MarkFollowsRecommends()
1818{
1819 return _config->FindB("APT::AutoRemove::RecommendsImportant", true);
1820}
1821
1822bool pkgDepCache::MarkFollowsSuggests()
1823{
db4351be 1824 return _config->FindB("APT::AutoRemove::SuggestsImportant", true);
74a05226
MV
1825}
1826
92fcbfc1 1827// pkgDepCache::MarkRequired - the main mark algorithm /*{{{*/
5a3339db
DK
1828static bool IsPkgInBoringState(pkgCache::PkgIterator const &Pkg, pkgDepCache::StateCache const * const PkgState)
1829{
1830 if (Pkg->CurrentVer == 0)
1831 {
1832 if (PkgState[Pkg->ID].Keep())
1833 return true;
1834 }
1835 else
1836 {
1837 if (PkgState[Pkg->ID].Delete())
1838 return true;
1839 }
1840 return false;
1841}
74a05226
MV
1842bool pkgDepCache::MarkRequired(InRootSetFunc &userFunc)
1843{
98278a81 1844 if (_config->Find("APT::Solver", "internal") != "internal")
76d4aab0
DK
1845 return true;
1846
fd23676e 1847 bool const debug_autoremove = _config->FindB("Debug::pkgAutoRemove",false);
74a05226
MV
1848
1849 // init the states
5a3339db
DK
1850 auto const PackagesCount = Head().PackageCount;
1851 for(auto i = decltype(PackagesCount){0}; i < PackagesCount; ++i)
74a05226 1852 {
fd23676e
DK
1853 PkgState[i].Marked = false;
1854 PkgState[i].Garbage = false;
74a05226 1855 }
fd23676e
DK
1856 if (debug_autoremove)
1857 for(PkgIterator p = PkgBegin(); !p.end(); ++p)
1858 if(PkgState[p->ID].Flags & Flag::Auto)
1859 std::clog << "AutoDep: " << p.FullName() << std::endl;
74a05226 1860
fd23676e
DK
1861 bool const follow_recommends = MarkFollowsRecommends();
1862 bool const follow_suggests = MarkFollowsSuggests();
74a05226 1863
74a05226 1864 // do the mark part, this is the core bit of the algorithm
769e9f3e 1865 for (PkgIterator P = PkgBegin(); !P.end(); ++P)
74a05226 1866 {
5a3339db
DK
1867 if (PkgState[P->ID].Marked || IsPkgInBoringState(P, PkgState))
1868 continue;
769e9f3e
DK
1869
1870 if ((PkgState[P->ID].Flags & Flag::Auto) == 0)
1871 ;
1872 else if ((P->Flags & Flag::Essential) || (P->Flags & Flag::Important))
1873 ;
1874 // be nice even then a required package violates the policy (#583517)
1875 // and do the full mark process also for required packages
1876 else if (P->CurrentVer != 0 && P.CurrentVer()->Priority == pkgCache::State::Required)
1877 ;
1878 else if (userFunc.InRootSet(P))
1879 ;
1880 // packages which can't be changed (like holds) can't be garbage
1881 else if (IsModeChangeOk(ModeGarbage, P, 0, false) == false)
1882 ;
1883 else
1884 continue;
1885
1886 if (PkgState[P->ID].Install())
1887 MarkPackage(P, PkgState[P->ID].InstVerIter(*this),
1888 follow_recommends, follow_suggests);
1889 else
1890 MarkPackage(P, P.CurrentVer(),
1891 follow_recommends, follow_suggests);
74a05226
MV
1892 }
1893
1894 return true;
1895}
92fcbfc1
DK
1896 /*}}}*/
1897// MarkPackage - mark a single package in Mark-and-Sweep /*{{{*/
5a3339db
DK
1898void pkgDepCache::MarkPackage(const pkgCache::PkgIterator &Pkg,
1899 const pkgCache::VerIterator &Ver,
e0b94b97
DK
1900 bool const &follow_recommends,
1901 bool const &follow_suggests)
74a05226 1902{
5a3339db
DK
1903 {
1904 pkgDepCache::StateCache &state = PkgState[Pkg->ID];
1905 // if we are marked already we are done
1906 if(state.Marked || unlikely(Ver.end()))
1907 return;
1908 state.Marked=true;
1909 }
e0b94b97 1910
5a3339db 1911 if (IsPkgInBoringState(Pkg, PkgState))
e0b94b97
DK
1912 return;
1913
5a3339db
DK
1914 bool const debug_autoremove = _config->FindB("Debug::pkgAutoRemove", false);
1915 if(debug_autoremove)
1916 std::clog << "Marking: " << Pkg.FullName() << " " << Ver.VerStr() << std::endl;
cda456d7 1917
5a3339db 1918 for (auto D = Ver.DependsList(); D.end() == false; ++D)
74a05226 1919 {
5a3339db
DK
1920 auto const T = D.TargetPkg();
1921 if (PkgState[T->ID].Marked)
1922 continue;
74a05226 1923
5a3339db
DK
1924 if (D->Type != Dep::Depends &&
1925 D->Type != Dep::PreDepends &&
1926 (follow_recommends == false || D->Type != Dep::Recommends) &&
1927 (follow_suggests == false || D->Type != Dep::Suggests))
1928 continue;
74a05226 1929
5a3339db 1930 // handle the virtual part first
a0ed43f7 1931 APT::VersionVector providers;
5a3339db
DK
1932 for(auto Prv = T.ProvidesList(); Prv.end() == false; ++Prv)
1933 {
1934 auto PP = Prv.OwnerPkg();
a0ed43f7 1935 if (IsPkgInBoringState(PP, PkgState))
5a3339db 1936 continue;
74a05226 1937
5a3339db
DK
1938 // we want to ignore provides from uninteresting versions
1939 auto const PV = (PkgState[PP->ID].Install()) ?
1940 PkgState[PP->ID].InstVerIter(*this) : PP.CurrentVer();
1941 if (unlikely(PV.end()) || PV != Prv.OwnerVer() || D.IsSatisfied(Prv) == false)
1942 continue;
83860e37 1943
a0ed43f7
DK
1944 providers.emplace_back(PV);
1945 }
1946 if (providers.empty() == false)
1947 {
1948 // sort providers by source version so that only the latest versioned
1949 // binary package of a source package is marked instead of all
1950 std::sort(providers.begin(), providers.end(),
1951 [](pkgCache::VerIterator const &A, pkgCache::VerIterator const &B) {
1952 auto const nameret = strcmp(A.SourcePkgName(), B.SourcePkgName());
1953 if (nameret != 0)
1954 return nameret < 0;
1955 auto const verret = A.Cache()->VS->CmpVersion(A.SourceVerStr(), B.SourceVerStr());
1956 if (verret != 0)
1957 return verret > 0;
1958 return strcmp(A.ParentPkg().Name(), B.ParentPkg().Name()) < 0;
1959 });
1960 auto const prvsize = providers.size();
1961 providers.erase(std::unique(providers.begin(), providers.end(),
1962 [](pkgCache::VerIterator const &A, pkgCache::VerIterator const &B) {
1963 return strcmp(A.SourcePkgName(), B.SourcePkgName()) == 0 &&
1964 strcmp(A.SourceVerStr(), B.SourceVerStr()) != 0;
1965 }), providers.end());
1966 for (auto && PV: providers)
1967 {
1968 auto const PP = PV.ParentPkg();
1969 if (debug_autoremove)
1970 std::clog << "Following dep: " << APT::PrettyDep(this, D)
1971 << ", provided by " << PP.FullName() << " " << PV.VerStr()
1972 << " (" << providers.size() << "/" << prvsize << ")"<< std::endl;
1973 MarkPackage(PP, PV, follow_recommends, follow_suggests);
1974 }
5a3339db 1975 }
74a05226 1976
5a3339db
DK
1977 // now deal with the real part of the package
1978 if (IsPkgInBoringState(T, PkgState))
1979 continue;
e0b94b97 1980
5a3339db
DK
1981 auto const TV = (PkgState[T->ID].Install()) ?
1982 PkgState[T->ID].InstVerIter(*this) : T.CurrentVer();
1983 if (unlikely(TV.end()) || D.IsSatisfied(TV) == false)
1984 continue;
1985
1986 if (debug_autoremove)
1987 std::clog << "Following dep: " << APT::PrettyDep(this, D) << std::endl;
1988 MarkPackage(T, TV, follow_recommends, follow_suggests);
1989 }
74a05226 1990}
92fcbfc1
DK
1991 /*}}}*/
1992bool pkgDepCache::Sweep() /*{{{*/
74a05226 1993{
95afdfd0
OS
1994 bool debug_autoremove = _config->FindB("Debug::pkgAutoRemove",false);
1995
74a05226
MV
1996 // do the sweep
1997 for(PkgIterator p=PkgBegin(); !p.end(); ++p)
95afdfd0 1998 {
74a05226
MV
1999 StateCache &state=PkgState[p->ID];
2000
c9b320e8
MV
2001 // skip required packages
2002 if (!p.CurrentVer().end() &&
2003 (p.CurrentVer()->Priority == pkgCache::State::Required))
2004 continue;
2005
74a05226 2006 // if it is not marked and it is installed, it's garbage
32085498 2007 if(!state.Marked && (!p.CurrentVer().end() || state.Install()))
74a05226
MV
2008 {
2009 state.Garbage=true;
95afdfd0 2010 if(debug_autoremove)
6ee2b0f8 2011 std::clog << "Garbage: " << p.FullName() << std::endl;
74a05226
MV
2012 }
2013 }
2014
2015 return true;
2016}
92fcbfc1 2017 /*}}}*/
107d054b
DK
2018// DepCache::MarkAndSweep /*{{{*/
2019bool pkgDepCache::MarkAndSweep(InRootSetFunc &rootFunc)
2020{
2021 return MarkRequired(rootFunc) && Sweep();
2022}
2023bool pkgDepCache::MarkAndSweep()
2024{
47c37a1b 2025 std::unique_ptr<InRootSetFunc> f(GetRootSetFunc());
107d054b
DK
2026 if(f.get() != NULL)
2027 return MarkAndSweep(*f.get());
2028 else
2029 return false;
2030}
2031 /*}}}*/