]>
Commit | Line | Data |
---|---|---|
74a05226 | 1 | // -*- mode: c++; mode: fold -*- |
6c139d6e | 2 | // Description /*{{{*/ |
b2e465d6 | 3 | // $Id: depcache.h,v 1.14 2001/02/20 07:03:17 jgg Exp $ |
6c139d6e AL |
4 | /* ###################################################################### |
5 | ||
6 | DepCache - Dependency Extension data for the cache | |
7 | ||
8 | This class stores the cache data and a set of extension structures for | |
9 | monitoring the current state of all the packages. It also generates and | |
10 | caches the 'install' state of many things. This refers to the state of the | |
11 | package after an install has been run. | |
12 | ||
13 | The StateCache::State field can be -1,0,1,2 which is <,=,>,no current. | |
14 | StateCache::Mode is which of the 3 fields is active. | |
15 | ||
16 | This structure is important to support the readonly status of the cache | |
17 | file. When the data is saved the cache will be refereshed from our | |
18 | internal rep and written to disk. Then the actual persistant data | |
19 | files will be put on the disk. | |
20 | ||
21 | Each dependency is compared against 3 target versions to produce to | |
22 | 3 dependency results. | |
23 | Now - Compared using the Currently install version | |
24 | Install - Compared using the install version (final state) | |
25 | CVer - (Candidate Verion) Compared using the Candidate Version | |
26 | The candidate and now results are used to decide wheather a package | |
27 | should be automatically installed or if it should be left alone. | |
28 | ||
29 | Remember, the Candidate Version is selected based on the distribution | |
30 | settings for the Package. The Install Version is selected based on the | |
31 | state (Delete, Keep, Install) field and can be either the Current Version | |
32 | or the Candidate version. | |
33 | ||
34 | The Candidate version is what is shown the 'Install Version' field. | |
35 | ||
36 | ##################################################################### */ | |
37 | /*}}}*/ | |
6c139d6e AL |
38 | #ifndef PKGLIB_DEPCACHE_H |
39 | #define PKGLIB_DEPCACHE_H | |
40 | ||
6c139d6e | 41 | |
094a497d | 42 | #include <apt-pkg/pkgcache.h> |
a246f2dc | 43 | #include <apt-pkg/progress.h> |
6c139d6e | 44 | |
74a05226 MV |
45 | #include <regex.h> |
46 | ||
47 | #include <vector> | |
48 | ||
b2e465d6 | 49 | class pkgDepCache : protected pkgCache::Namespace |
6c139d6e AL |
50 | { |
51 | public: | |
74a05226 MV |
52 | |
53 | /** \brief An arbitrary predicate on packages. */ | |
54 | class InRootSetFunc | |
55 | { | |
56 | public: | |
57 | virtual bool InRootSet(const pkgCache::PkgIterator &pkg) {return false;}; | |
58 | virtual ~InRootSetFunc() {}; | |
59 | }; | |
60 | ||
61 | private: | |
62 | /** \brief Mark a single package and all its unmarked important | |
63 | * dependencies during mark-and-sweep. | |
64 | * | |
65 | * Recursively invokes itself to mark all dependencies of the | |
66 | * package. | |
67 | * | |
68 | * \param pkg The package to mark. | |
69 | * | |
70 | * \param ver The version of the package that is to be marked. | |
71 | * | |
72 | * \param follow_recommends If \b true, recommendations of the | |
73 | * package will be recursively marked. | |
74 | * | |
75 | * \param follow_suggests If \b true, suggestions of the package | |
76 | * will be recursively marked. | |
77 | */ | |
78 | void MarkPackage(const pkgCache::PkgIterator &pkg, | |
79 | const pkgCache::VerIterator &ver, | |
80 | bool follow_recommends, | |
81 | bool follow_suggests); | |
82 | ||
83 | /** \brief Update the Marked field of all packages. | |
84 | * | |
85 | * Each package's StateCache::Marked field will be set to \b true | |
86 | * if and only if it can be reached from the root set. By | |
87 | * default, the root set consists of the set of manually installed | |
88 | * or essential packages, but it can be extended using the | |
89 | * parameter #rootFunc. | |
90 | * | |
91 | * \param rootFunc A callback that can be used to add extra | |
92 | * packages to the root set. | |
93 | * | |
94 | * \return \b false if an error occured. | |
95 | */ | |
96 | bool MarkRequired(InRootSetFunc &rootFunc); | |
97 | ||
98 | /** \brief Set the StateCache::Garbage flag on all packages that | |
99 | * should be removed. | |
100 | * | |
101 | * Packages that were not marked by the last call to #MarkRequired | |
102 | * are tested to see whether they are actually garbage. If so, | |
103 | * they are marked as such. | |
104 | * | |
105 | * \return \b false if an error occured. | |
106 | */ | |
107 | bool Sweep(); | |
108 | ||
109 | public: | |
6c139d6e AL |
110 | |
111 | // These flags are used in DepState | |
112 | enum DepFlags {DepNow = (1 << 0),DepInstall = (1 << 1),DepCVer = (1 << 2), | |
113 | DepGNow = (1 << 3),DepGInstall = (1 << 4),DepGCVer = (1 << 5)}; | |
114 | ||
115 | // These flags are used in StateCache::DepState | |
116 | enum DepStateFlags {DepNowPolicy = (1 << 0), DepNowMin = (1 << 1), | |
117 | DepInstPolicy = (1 << 2), DepInstMin = (1 << 3), | |
118 | DepCandPolicy = (1 << 4), DepCandMin = (1 << 5)}; | |
119 | ||
120 | // These flags are used in StateCache::iFlags | |
d0c59649 | 121 | enum InternalFlags {AutoKept = (1 << 0), Purge = (1 << 1), ReInstall = (1 << 2)}; |
6c139d6e AL |
122 | |
123 | enum VersionTypes {NowVersion, InstallVersion, CandidateVersion}; | |
124 | enum ModeList {ModeDelete = 0, ModeKeep = 1, ModeInstall = 2}; | |
0a57c0f0 | 125 | |
74a05226 MV |
126 | /** \brief Represents an active action group. |
127 | * | |
128 | * An action group is a group of actions that are currently being | |
129 | * performed. While an active group is active, certain routine | |
130 | * clean-up actions that would normally be performed after every | |
131 | * cache operation are delayed until the action group is | |
132 | * completed. This is necessary primarily to avoid inefficiencies | |
133 | * when modifying a large number of packages at once. | |
134 | * | |
135 | * This class represents an active action group. Creating an | |
136 | * instance will create an action group; destroying one will | |
137 | * destroy the corresponding action group. | |
138 | * | |
139 | * The following operations are suppressed by this class: | |
140 | * | |
141 | * - Keeping the Marked and Garbage flags up to date. | |
142 | * | |
143 | * \note This can be used in the future to easily accumulate | |
144 | * atomic actions for undo or to display "what apt did anyway"; | |
145 | * e.g., change the counter of how many action groups are active | |
146 | * to a std::set of pointers to them and use those to store | |
147 | * information about what happened in a group in the group. | |
148 | */ | |
149 | class ActionGroup | |
150 | { | |
151 | pkgDepCache &cache; | |
152 | ||
153 | bool released; | |
154 | ||
155 | /** Action groups are noncopyable. */ | |
156 | ActionGroup(const ActionGroup &other); | |
157 | public: | |
158 | /** \brief Create a new ActionGroup. | |
159 | * | |
160 | * \param cache The cache that this ActionGroup should | |
161 | * manipulate. | |
162 | * | |
163 | * As long as this object exists, no automatic cleanup | |
164 | * operations will be undertaken. | |
165 | */ | |
166 | ActionGroup(pkgDepCache &cache); | |
167 | ||
168 | /** \brief Clean up the action group before it is destroyed. | |
169 | * | |
170 | * If it is destroyed later, no second cleanup wil be run. | |
171 | */ | |
172 | void release(); | |
173 | ||
174 | /** \brief Destroy the action group. | |
175 | * | |
176 | * If this is the last action group, the automatic cache | |
177 | * cleanup operations will be undertaken. | |
178 | */ | |
179 | ~ActionGroup(); | |
180 | }; | |
181 | ||
182 | /** \brief Returns \b true for packages matching a regular | |
183 | * expression in APT::NeverAutoRemove. | |
184 | */ | |
185 | class DefaultRootSetFunc : public InRootSetFunc | |
186 | { | |
187 | std::vector<regex_t *> rootSetRegexp; | |
188 | bool constructedSuccessfully; | |
189 | ||
190 | public: | |
191 | DefaultRootSetFunc(); | |
192 | ~DefaultRootSetFunc(); | |
193 | ||
194 | /** \return \b true if the class initialized successfully, \b | |
195 | * false otherwise. Used to avoid throwing an exception, since | |
196 | * APT classes generally don't. | |
197 | */ | |
198 | bool wasConstructedSuccessfully() const { return constructedSuccessfully; } | |
199 | ||
200 | bool InRootSet(const pkgCache::PkgIterator &pkg); | |
201 | }; | |
202 | ||
6c139d6e AL |
203 | struct StateCache |
204 | { | |
205 | // Epoch stripped text versions of the two version fields | |
206 | const char *CandVersion; | |
207 | const char *CurVersion; | |
208 | ||
209 | // Pointer to the candidate install version. | |
210 | Version *CandidateVer; | |
211 | ||
212 | // Pointer to the install version. | |
213 | Version *InstallVer; | |
b2e465d6 AL |
214 | |
215 | // Copy of Package::Flags | |
216 | unsigned short Flags; | |
217 | unsigned short iFlags; // Internal flags | |
6c139d6e | 218 | |
74a05226 | 219 | /** \brief \b true if this package can be reached from the root set. */ |
0a57c0f0 | 220 | bool Marked; |
74a05226 MV |
221 | |
222 | /** \brief \b true if this package is unused and should be removed. | |
223 | * | |
224 | * This differs from !#Marked, because it is possible that some | |
225 | * unreachable packages will be protected from becoming | |
226 | * garbage. | |
227 | */ | |
0a57c0f0 | 228 | bool Garbage; |
afb1e2e3 | 229 | |
6c139d6e AL |
230 | // Various tree indicators |
231 | signed char Status; // -1,0,1,2 | |
232 | unsigned char Mode; // ModeList | |
233 | unsigned char DepState; // DepState Flags | |
234 | ||
6c139d6e AL |
235 | // Update of candidate version |
236 | const char *StripEpoch(const char *Ver); | |
237 | void Update(PkgIterator Pkg,pkgCache &Cache); | |
238 | ||
239 | // Various test members for the current status of the package | |
240 | inline bool NewInstall() const {return Status == 2 && Mode == ModeInstall;}; | |
241 | inline bool Delete() const {return Mode == ModeDelete;}; | |
242 | inline bool Keep() const {return Mode == ModeKeep;}; | |
243 | inline bool Upgrade() const {return Status > 0 && Mode == ModeInstall;}; | |
0a8e3465 | 244 | inline bool Upgradable() const {return Status >= 1;}; |
6321777b | 245 | inline bool Downgrade() const {return Status < 0 && Mode == ModeInstall;}; |
6c139d6e AL |
246 | inline bool Held() const {return Status != 0 && Keep();}; |
247 | inline bool NowBroken() const {return (DepState & DepNowMin) != DepNowMin;}; | |
4ef9a929 | 248 | inline bool NowPolicyBroken() const {return (DepState & DepNowPolicy) != DepNowPolicy;}; |
6c139d6e | 249 | inline bool InstBroken() const {return (DepState & DepInstMin) != DepInstMin;}; |
60681f93 | 250 | inline bool InstPolicyBroken() const {return (DepState & DepInstPolicy) != DepInstPolicy;}; |
6c139d6e AL |
251 | inline bool Install() const {return Mode == ModeInstall;}; |
252 | inline VerIterator InstVerIter(pkgCache &Cache) | |
253 | {return VerIterator(Cache,InstallVer);}; | |
254 | inline VerIterator CandidateVerIter(pkgCache &Cache) | |
255 | {return VerIterator(Cache,CandidateVer);}; | |
256 | }; | |
257 | ||
258 | // Helper functions | |
259 | void BuildGroupOrs(VerIterator const &V); | |
260 | void UpdateVerState(PkgIterator Pkg); | |
261 | ||
b2e465d6 AL |
262 | // User Policy control |
263 | class Policy | |
264 | { | |
265 | public: | |
266 | ||
267 | virtual VerIterator GetCandidateVer(PkgIterator Pkg); | |
268 | virtual bool IsImportantDep(DepIterator Dep); | |
269 | ||
270 | virtual ~Policy() {}; | |
271 | }; | |
74a05226 MV |
272 | |
273 | private: | |
274 | /** The number of open "action groups"; certain post-action | |
275 | * operations are suppressed if this number is > 0. | |
276 | */ | |
277 | int group_level; | |
278 | ||
279 | friend class ActionGroup; | |
b2e465d6 | 280 | |
6c139d6e AL |
281 | protected: |
282 | ||
283 | // State information | |
b2e465d6 | 284 | pkgCache *Cache; |
6c139d6e AL |
285 | StateCache *PkgState; |
286 | unsigned char *DepState; | |
287 | ||
b2e465d6 AL |
288 | double iUsrSize; |
289 | double iDownloadSize; | |
a6568219 AL |
290 | unsigned long iInstCount; |
291 | unsigned long iDelCount; | |
292 | unsigned long iKeepCount; | |
293 | unsigned long iBrokenCount; | |
4ef9a929 | 294 | unsigned long iPolicyBrokenCount; |
a6568219 | 295 | unsigned long iBadCount; |
b2e465d6 AL |
296 | |
297 | Policy *delLocalPolicy; // For memory clean up.. | |
298 | Policy *LocalPolicy; | |
299 | ||
6c139d6e AL |
300 | // Check for a matching provides |
301 | bool CheckDep(DepIterator Dep,int Type,PkgIterator &Res); | |
302 | inline bool CheckDep(DepIterator Dep,int Type) | |
303 | { | |
b2e465d6 | 304 | PkgIterator Res(*this,0); |
6c139d6e | 305 | return CheckDep(Dep,Type,Res); |
b2e465d6 | 306 | } |
6c139d6e AL |
307 | |
308 | // Computes state information for deps and versions (w/o storing) | |
309 | unsigned char DependencyState(DepIterator &D); | |
310 | unsigned char VersionState(DepIterator D,unsigned char Check, | |
311 | unsigned char SetMin, | |
312 | unsigned char SetPolicy); | |
313 | ||
314 | // Recalculates various portions of the cache, call after changing something | |
315 | void Update(DepIterator Dep); // Mostly internal | |
316 | void Update(PkgIterator const &P); | |
317 | ||
318 | // Count manipulators | |
b2e465d6 | 319 | void AddSizes(const PkgIterator &Pkg,signed long Mult = 1); |
6c139d6e AL |
320 | inline void RemoveSizes(const PkgIterator &Pkg) {AddSizes(Pkg,-1);}; |
321 | void AddStates(const PkgIterator &Pkg,int Add = 1); | |
322 | inline void RemoveStates(const PkgIterator &Pkg) {AddStates(Pkg,-1);}; | |
b2e465d6 | 323 | |
6c139d6e AL |
324 | public: |
325 | ||
b2e465d6 AL |
326 | // Legacy.. We look like a pkgCache |
327 | inline operator pkgCache &() {return *Cache;}; | |
328 | inline Header &Head() {return *Cache->HeaderP;}; | |
329 | inline PkgIterator PkgBegin() {return Cache->PkgBegin();}; | |
330 | inline PkgIterator FindPkg(string const &Name) {return Cache->FindPkg(Name);}; | |
331 | ||
332 | inline pkgCache &GetCache() {return *Cache;}; | |
333 | inline pkgVersioningSystem &VS() {return *Cache->VS;}; | |
334 | ||
6c139d6e | 335 | // Policy implementation |
b2e465d6 AL |
336 | inline VerIterator GetCandidateVer(PkgIterator Pkg) {return LocalPolicy->GetCandidateVer(Pkg);}; |
337 | inline bool IsImportantDep(DepIterator Dep) {return LocalPolicy->IsImportantDep(Dep);}; | |
338 | inline Policy &GetPolicy() {return *LocalPolicy;}; | |
339 | ||
6c139d6e AL |
340 | // Accessors |
341 | inline StateCache &operator [](PkgIterator const &I) {return PkgState[I->ID];}; | |
342 | inline unsigned char &operator [](DepIterator const &I) {return DepState[I->ID];}; | |
343 | ||
74a05226 MV |
344 | /** \return A function identifying packages in the root set other |
345 | * than manually installed packages and essential packages, or \b | |
346 | * NULL if an error occurs. | |
347 | * | |
348 | * \todo Is this the best place for this function? Perhaps the | |
349 | * settings for mark-and-sweep should be stored in a single | |
350 | * external class? | |
351 | */ | |
352 | virtual InRootSetFunc *GetRootSetFunc(); | |
353 | ||
354 | /** \return \b true if the garbage collector should follow recommendations. | |
355 | */ | |
356 | virtual bool MarkFollowsRecommends(); | |
357 | ||
358 | /** \return \b true if the garbage collector should follow suggestions. | |
359 | */ | |
360 | virtual bool MarkFollowsSuggests(); | |
361 | ||
362 | /** \brief Update the Marked and Garbage fields of all packages. | |
363 | * | |
364 | * This routine is implicitly invoked after all state manipulators | |
365 | * and when an ActionGroup is destroyed. It invokes #MarkRequired | |
366 | * and #Sweep to do its dirty work. | |
367 | * | |
368 | * \param rootFunc A predicate that returns \b true for packages | |
369 | * that should be added to the root set. | |
370 | */ | |
371 | bool MarkAndSweep(InRootSetFunc &rootFunc) | |
372 | { | |
373 | return MarkRequired(rootFunc) && Sweep(); | |
374 | } | |
375 | ||
376 | bool MarkAndSweep() | |
377 | { | |
378 | std::auto_ptr<InRootSetFunc> f(GetRootSetFunc()); | |
379 | if(f.get() != NULL) | |
380 | return MarkAndSweep(*f.get()); | |
381 | else | |
382 | return false; | |
383 | } | |
384 | ||
385 | /** \name State Manipulators | |
386 | */ | |
387 | // @{ | |
388 | void MarkKeep(PkgIterator const &Pkg, bool Soft = false, | |
389 | bool FromUser = true); | |
d556d1a1 | 390 | void MarkDelete(PkgIterator const &Pkg,bool Purge = false); |
b2e465d6 | 391 | void MarkInstall(PkgIterator const &Pkg,bool AutoInst = true, |
2ae2e04d MV |
392 | unsigned long Depth = 0, bool FromUser = true, |
393 | bool ForceImportantDeps = false); | |
d0c59649 | 394 | void SetReInstall(PkgIterator const &Pkg,bool To); |
6321777b | 395 | void SetCandidateVersion(VerIterator TargetVer); |
74a05226 MV |
396 | |
397 | /** Set the "is automatically installed" flag of Pkg. */ | |
398 | void MarkAuto(const PkgIterator &Pkg, bool Auto); | |
399 | // @} | |
6c139d6e AL |
400 | |
401 | // This is for debuging | |
a246f2dc | 402 | void Update(OpProgress *Prog = 0); |
a83d884d MV |
403 | |
404 | // read persistent states | |
405 | bool readStateFile(OpProgress *prog); | |
95689ede | 406 | bool writeStateFile(OpProgress *prog, bool InstalledOnly=false); |
e331f6ed | 407 | |
6c139d6e | 408 | // Size queries |
b2e465d6 AL |
409 | inline double UsrSize() {return iUsrSize;}; |
410 | inline double DebSize() {return iDownloadSize;}; | |
a6568219 AL |
411 | inline unsigned long DelCount() {return iDelCount;}; |
412 | inline unsigned long KeepCount() {return iKeepCount;}; | |
413 | inline unsigned long InstCount() {return iInstCount;}; | |
414 | inline unsigned long BrokenCount() {return iBrokenCount;}; | |
4ef9a929 | 415 | inline unsigned long PolicyBrokenCount() {return iPolicyBrokenCount;}; |
a6568219 | 416 | inline unsigned long BadCount() {return iBadCount;}; |
b2e465d6 AL |
417 | |
418 | bool Init(OpProgress *Prog); | |
6c139d6e | 419 | |
b2e465d6 | 420 | pkgDepCache(pkgCache *Cache,Policy *Plcy = 0); |
6c139d6e AL |
421 | virtual ~pkgDepCache(); |
422 | }; | |
423 | ||
424 | #endif |