2552f056ca45146e39519aac154838781a3c7dd3
[platform/upstream/libzypp.git] / zypp / sat / detail / PoolImpl.h
1 /*---------------------------------------------------------------------\
2 |                          ____ _   __ __ ___                          |
3 |                         |__  / \ / / . \ . \                         |
4 |                           / / \ V /|  _/  _/                         |
5 |                          / /__ | | | | | |                           |
6 |                         /_____||_| |_| |_|                           |
7 |                                                                      |
8 \---------------------------------------------------------------------*/
9 /** \file       zypp/sat/detail/PoolImpl.h
10  *
11 */
12 #ifndef ZYPP_SAT_DETAIL_POOLIMPL_H
13 #define ZYPP_SAT_DETAIL_POOLIMPL_H
14 extern "C"
15 {
16 #include <solv/pool.h>
17 #include <solv/repo.h>
18 #include <solv/solvable.h>
19 #include <solv/poolarch.h>
20 #include <solv/repo_solv.h>
21 }
22 #include <iosfwd>
23
24 #include "zypp/base/Hash.h"
25 #include "zypp/base/NonCopyable.h"
26 #include "zypp/base/SerialNumber.h"
27 #include "zypp/base/SetTracker.h"
28 #include "zypp/sat/detail/PoolMember.h"
29 #include "zypp/sat/Queue.h"
30 #include "zypp/RepoInfo.h"
31 #include "zypp/Locale.h"
32 #include "zypp/Capability.h"
33 #include "zypp/IdString.h"
34
35 ///////////////////////////////////////////////////////////////////
36 namespace zypp
37 { /////////////////////////////////////////////////////////////////
38   ///////////////////////////////////////////////////////////////////
39   namespace sat
40   { /////////////////////////////////////////////////////////////////
41     class SolvableSet;
42     ///////////////////////////////////////////////////////////////////
43     namespace detail
44     { /////////////////////////////////////////////////////////////////
45
46       ///////////////////////////////////////////////////////////////////
47       //
48       //        CLASS NAME : PoolImpl
49       //
50       /** */
51       class PoolImpl : private base::NonCopyable
52       {
53         public:
54           /** Default ctor */
55           PoolImpl();
56
57           /** Dtor */
58           ~PoolImpl();
59
60           /** Pointer style access forwarded to sat-pool. */
61           CPool * operator->()
62           { return _pool; }
63
64         public:
65           /** Serial number changing whenever the content changes. */
66           const SerialNumber & serial() const
67           { return _serial; }
68
69           /** Serial number changing whenever resusePoolIDs==true was used. ResPool must also invalidate it's PoolItems! */
70           const SerialNumber & serialIDs() const
71           { return _serialIDs; }
72
73           /** Update housekeeping data (e.g. whatprovides).
74            * \todo actually requires a watcher.
75            */
76           void prepare() const;
77
78         private:
79           /** Invalidate housekeeping data (e.g. whatprovides) if the
80            *  pools content changed.
81            */
82           void setDirty( const char * a1 = 0, const char * a2 = 0, const char * a3 = 0 );
83
84           /** Invalidate locale related housekeeping data.
85            */
86           void localeSetDirty( const char * a1 = 0, const char * a2 = 0, const char * a3 = 0 );
87
88           /** Invalidate housekeeping data (e.g. whatprovides) if dependencies changed.
89            */
90           void depSetDirty( const char * a1 = 0, const char * a2 = 0, const char * a3 = 0 );
91
92           /** Callback to resolve namespace dependencies (language, modalias, filesystem, etc.). */
93           static detail::IdType nsCallback( CPool *, void * data, detail::IdType lhs, detail::IdType rhs );
94
95         public:
96           /** Reserved system repository alias \c @System. */
97           static const std::string & systemRepoAlias();
98
99           bool isSystemRepo( CRepo * repo_r ) const
100           { return repo_r && _pool->installed == repo_r; }
101
102           CRepo * systemRepo() const
103           { return _pool->installed; }
104
105           /** Get rootdir (for file conflicts check) */
106           Pathname rootDir() const
107           {
108             const char * rd = ::pool_get_rootdir( _pool );
109             return( rd ? rd : "/" );
110           }
111
112           /** Set rootdir (for file conflicts check) */
113           void rootDir( const Pathname & root_r )
114           {
115             if ( root_r.empty() || root_r == "/" )
116               ::pool_set_rootdir( _pool, nullptr );
117             else
118               ::pool_set_rootdir( _pool, root_r.c_str() );
119           }
120
121         public:
122           /** \name Actions invalidating housekeeping data.
123            *
124            * All methods expect valid arguments being passed.
125            */
126           //@{
127           /** Creating a new repo named \a name_r. */
128           CRepo * _createRepo( const std::string & name_r );
129
130           /** Delete repo \a repo_r from pool. */
131           void _deleteRepo( CRepo * repo_r );
132
133           /** Adding solv file to a repo.
134            * Except for \c isSystemRepo_r, solvables of incompatible architecture
135            * are filtered out.
136           */
137           int _addSolv( CRepo * repo_r, FILE * file_r );
138
139           /** Adding helix file to a repo.
140            * Except for \c isSystemRepo_r, solvables of incompatible architecture
141            * are filtered out.
142           */
143           int _addHelix( CRepo * repo_r, FILE * file_r );
144
145           /** Adding Solvables to a repo. */
146           detail::SolvableIdType _addSolvables( CRepo * repo_r, unsigned count_r );
147           //@}
148
149           /** Helper postprocessing the repo after adding solv or helix files. */
150           void _postRepoAdd( CRepo * repo_r );
151
152         public:
153           /** a \c valid \ref Solvable has a non NULL repo pointer. */
154           bool validSolvable( const CSolvable & slv_r ) const
155           { return slv_r.repo; }
156           /** \overload Check also for id_r being in range of _pool->solvables. */
157           bool validSolvable( SolvableIdType id_r ) const
158           { return id_r < unsigned(_pool->nsolvables) && validSolvable( _pool->solvables[id_r] ); }
159           /** \overload Check also for slv_r being in range of _pool->solvables. */
160           bool validSolvable( const CSolvable * slv_r ) const
161           { return _pool->solvables <= slv_r && slv_r <= _pool->solvables+_pool->nsolvables && validSolvable( *slv_r ); }
162
163         public:
164           CPool * getPool() const
165           { return _pool; }
166
167           /** \todo a quick check whether the repo was meanwhile deleted. */
168           CRepo * getRepo( RepoIdType id_r ) const
169           { return id_r; }
170
171           /** Return pointer to the sat-solvable or NULL if it is not valid.
172            * \see \ref validSolvable.
173            */
174           CSolvable * getSolvable( SolvableIdType id_r ) const
175           {
176             if ( validSolvable( id_r ) )
177               return &_pool->solvables[id_r];
178             return 0;
179           }
180
181         public:
182           /** Get id of the first valid \ref Solvable.
183            * This is the next valid after the system solvable.
184            */
185           SolvableIdType getFirstId()  const
186           { return getNextId( 1 ); }
187
188           /** Get id of the next valid \ref Solvable.
189            * This goes round robbin. At the end it returns \ref noSolvableId.
190            * Passing \ref noSolvableId it returns the 1st valid  \ref Solvable.
191            * \see \ref validSolvable.
192            */
193           SolvableIdType getNextId( SolvableIdType id_r ) const
194           {
195             for( ++id_r; id_r < unsigned(_pool->nsolvables); ++id_r )
196             {
197               if ( validSolvable( _pool->solvables[id_r] ) )
198                 return id_r;
199             }
200             return noSolvableId;
201           }
202
203         public:
204           /** */
205           const RepoInfo & repoInfo( RepoIdType id_r )
206           { return _repoinfos[id_r]; }
207           /** Also adjust repo priority and subpriority accordingly. */
208           void setRepoInfo( RepoIdType id_r, const RepoInfo & info_r );
209           /** */
210           void eraseRepoInfo( RepoIdType id_r )
211           { _repoinfos.erase( id_r ); }
212
213         public:
214           /** Returns the id stored at \c offset_r in the internal
215            * whatprovidesdata array.
216           */
217           const sat::detail::IdType whatProvidesData( unsigned offset_r )
218           { return _pool->whatprovidesdata[offset_r]; }
219
220           /** Returns offset into the internal whatprovidesdata array.
221            * Use \ref whatProvidesData to get the stored Id.
222           */
223           unsigned whatProvides( Capability cap_r )
224           { prepare(); return ::pool_whatprovides( _pool, cap_r.id() ); }
225
226         public:
227           /// \name Requested locales.
228           /// The requested LocaleSets managed in _requestedLocalesTracker
229           /// are unexpanded; i.e. they contain just the pure user selection.
230           /// The resolver however uses expanded sets ('de_DE' will also
231           /// include its fallback locales 'de', (en); here in the namespace:
232           /// callback and in the Resolver itself).
233           //@{
234           /** */
235           void setTextLocale( const Locale & locale_r );
236
237
238           /** Start tracking changes based on this \a locales_r.
239            * Usually called on TargetInit.
240            */
241           void initRequestedLocales( const LocaleSet & locales_r );
242
243           /** Added since last initRequestedLocales. */
244           const LocaleSet & getAddedRequestedLocales() const
245           { return _requestedLocalesTracker.added(); }
246
247           /** Removed since last initRequestedLocales. */
248           const LocaleSet & getRemovedRequestedLocales() const
249           { return _requestedLocalesTracker.removed(); }
250
251           /** Current set of requested Locales. */
252           const LocaleSet & getRequestedLocales() const
253           { return _requestedLocalesTracker.current(); }
254
255           bool isRequestedLocale( const Locale & locale_r ) const
256           { return _requestedLocalesTracker.contains( locale_r ); }
257
258           /** User change (tracked). */
259           void setRequestedLocales( const LocaleSet & locales_r );
260           /** User change (tracked). */
261           bool addRequestedLocale( const Locale & locale_r );
262           /** User change (tracked). */
263           bool eraseRequestedLocale( const Locale & locale_r );
264
265           /** All Locales occurring in any repo. */
266           const LocaleSet & getAvailableLocales() const;
267
268           bool isAvailableLocale( const Locale & locale_r ) const
269           {
270             const LocaleSet & avl( getAvailableLocales() );
271             LocaleSet::const_iterator it( avl.find( locale_r ) );
272             return it != avl.end();
273           }
274
275           typedef base::SetTracker<IdStringSet> TrackedLocaleIds;
276
277           /** Expanded _requestedLocalesTracker for solver.*/
278           const TrackedLocaleIds & trackedLocaleIds() const;
279           //@}
280
281         public:
282           /** \name Multiversion install. */
283           //@{
284           typedef SolvableSet MultiversionList;
285
286           const MultiversionList & multiversionList() const;
287
288           bool isMultiversion( const Solvable & solv_r ) const;
289
290           void multiversionSpecChanged();
291           //@}
292
293         public:
294           /** \name Installed on behalf of a user request hint. */
295           //@{
296           /** Get ident list of all autoinstalled solvables. */
297           StringQueue autoInstalled() const
298           { return _autoinstalled; }
299
300           /** Set ident list of all autoinstalled solvables. */
301           void setAutoInstalled( const StringQueue & autoInstalled_r )
302           { _autoinstalled = autoInstalled_r; }
303
304           bool isOnSystemByUser( IdString ident_r ) const
305           { return !_autoinstalled.contains( ident_r.id() ); }
306
307           bool isOnSystemByAuto( IdString ident_r ) const
308           { return _autoinstalled.contains( ident_r.id() ); }
309           //@}
310
311         public:
312           /** accessor for etc/sysconfig/storage reading file on demand */
313           const std::set<std::string> & requiredFilesystems() const;
314
315         private:
316           /** sat-pool. */
317           CPool * _pool;
318           /** Serial number - changes with each Pool content change. */
319           SerialNumber _serial;
320           /** Serial number of IDs - changes whenever resusePoolIDs==true - ResPool must also invalidate it's PoolItems! */
321           SerialNumber _serialIDs;
322           /** Watch serial number. */
323           SerialNumberWatcher _watcher;
324           /** Additional \ref RepoInfo. */
325           std::map<RepoIdType,RepoInfo> _repoinfos;
326
327           /**  */
328           base::SetTracker<LocaleSet> _requestedLocalesTracker;
329           mutable scoped_ptr<TrackedLocaleIds> _trackedLocaleIdsPtr;
330
331           mutable scoped_ptr<LocaleSet> _availableLocalesPtr;
332
333           /**  */
334           void multiversionListInit() const;
335           mutable scoped_ptr<MultiversionList> _multiversionListPtr;
336
337           /**  */
338           sat::StringQueue _autoinstalled;
339
340           /** filesystems mentioned in /etc/sysconfig/storage */
341           mutable scoped_ptr<std::set<std::string> > _requiredFilesystemsPtr;
342       };
343       ///////////////////////////////////////////////////////////////////
344
345       /////////////////////////////////////////////////////////////////
346     } // namespace detail
347     ///////////////////////////////////////////////////////////////////
348     /////////////////////////////////////////////////////////////////
349   } // namespace sat
350   ///////////////////////////////////////////////////////////////////
351   /////////////////////////////////////////////////////////////////
352 } // namespace zypp
353 ///////////////////////////////////////////////////////////////////
354 #define POOL_SETDIRTY
355 #endif // ZYPP_SAT_DETAIL_POOLIMPL_H