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