243a0a60b999d8f8fe779648197b5b9d8c864baf
[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/Tr1hash.h"
25 #include "zypp/base/NonCopyable.h"
26 #include "zypp/base/SerialNumber.h"
27 #include "zypp/sat/detail/PoolMember.h"
28 #include "zypp/sat/Queue.h"
29 #include "zypp/RepoInfo.h"
30 #include "zypp/Locale.h"
31 #include "zypp/Capability.h"
32 #include "zypp/IdString.h"
33
34 ///////////////////////////////////////////////////////////////////
35 namespace zypp
36 { /////////////////////////////////////////////////////////////////
37   ///////////////////////////////////////////////////////////////////
38   namespace sat
39   { /////////////////////////////////////////////////////////////////
40     ///////////////////////////////////////////////////////////////////
41     namespace detail
42     { /////////////////////////////////////////////////////////////////
43
44       ///////////////////////////////////////////////////////////////////
45       //
46       //        CLASS NAME : PoolImpl
47       //
48       /** */
49       class PoolImpl : private base::NonCopyable
50       {
51         public:
52           /** Default ctor */
53           PoolImpl();
54
55           /** Dtor */
56           ~PoolImpl();
57
58           /** Pointer style access forwarded to sat-pool. */
59           CPool * operator->()
60           { return _pool; }
61
62         public:
63           /** Serial number changing whenever the content changes. */
64           const SerialNumber & serial() const
65           { return _serial; }
66
67           /** Update housekeeping data (e.g. whatprovides).
68            * \todo actually requires a watcher.
69            */
70           void prepare() const;
71           /** \ref prepare plus some expensive checks done before solving only. */
72           void prepareForSolving() const;
73
74         private:
75           /** Invalidate housekeeping data (e.g. whatprovides) if the
76            *  pools content changed.
77            */
78           void setDirty( const char * a1 = 0, const char * a2 = 0, const char * a3 = 0 );
79
80           /** Invalidate housekeeping data (e.g. whatprovides) if dependencies changed.
81            */
82           void depSetDirty( const char * a1 = 0, const char * a2 = 0, const char * a3 = 0 );
83
84           /** Callback to resolve namespace dependencies (language, modalias, filesystem, etc.). */
85           static detail::IdType nsCallback( CPool *, void * data, detail::IdType lhs, detail::IdType rhs );
86
87         public:
88           /** Reserved system repository alias \c @System. */
89           static const std::string & systemRepoAlias();
90
91           bool isSystemRepo( CRepo * repo_r ) const
92           { return repo_r && _pool->installed == repo_r; }
93
94           CRepo * systemRepo() const
95           { return _pool->installed; }
96
97           /** Get rootdir (for file conflicts check) */
98           Pathname rootDir() const
99           {
100             const char * rd = ::pool_get_rootdir( _pool );
101             return( rd ? rd : "/" );
102           }
103
104           /** Set rootdir (for file conflicts check) */
105           void rootDir( const Pathname & root_r )
106           {
107             if ( root_r.empty() || root_r == "/" )
108               ::pool_set_rootdir( _pool, nullptr );
109             else
110               ::pool_set_rootdir( _pool, root_r.c_str() );
111           }
112
113         public:
114           /** \name Actions invalidating housekeeping data.
115            *
116            * All methods expect valid arguments being passed.
117            */
118           //@{
119           /** Creating a new repo named \a name_r. */
120           CRepo * _createRepo( const std::string & name_r );
121
122           /** Creating a new repo named \a name_r. */
123           void _deleteRepo( CRepo * repo_r );
124
125           /** Adding solv file to a repo.
126            * Except for \c isSystemRepo_r, solvables of incompatible architecture
127            * are filtered out.
128           */
129           int _addSolv( CRepo * repo_r, FILE * file_r );
130
131           /** Adding helix file to a repo.
132            * Except for \c isSystemRepo_r, solvables of incompatible architecture
133            * are filtered out.
134           */
135           int _addHelix( CRepo * repo_r, FILE * file_r );
136
137           /** Adding Solvables to a repo. */
138           detail::SolvableIdType _addSolvables( CRepo * repo_r, unsigned count_r );
139           //@}
140
141           /** Helper postprocessing the repo after adding solv or helix files. */
142           void _postRepoAdd( CRepo * repo_r );
143
144         public:
145           /** a \c valid \ref Solvable has a non NULL repo pointer. */
146           bool validSolvable( const CSolvable & slv_r ) const
147           { return slv_r.repo; }
148           /** \overload Check also for id_r being in range of _pool->solvables. */
149           bool validSolvable( SolvableIdType id_r ) const
150           { return id_r < unsigned(_pool->nsolvables) && validSolvable( _pool->solvables[id_r] ); }
151           /** \overload Check also for slv_r being in range of _pool->solvables. */
152           bool validSolvable( const CSolvable * slv_r ) const
153           { return _pool->solvables <= slv_r && slv_r <= _pool->solvables+_pool->nsolvables && validSolvable( *slv_r ); }
154
155         public:
156           CPool * getPool() const
157           { return _pool; }
158
159           /** \todo a quick check whether the repo was meanwhile deleted. */
160           CRepo * getRepo( RepoIdType id_r ) const
161           { return id_r; }
162
163           /** Return pointer to the sat-solvable or NULL if it is not valid.
164            * \see \ref validSolvable.
165            */
166           CSolvable * getSolvable( SolvableIdType id_r ) const
167           {
168             if ( validSolvable( id_r ) )
169               return &_pool->solvables[id_r];
170             return 0;
171           }
172
173         public:
174           /** Get id of the first valid \ref Solvable.
175            * This is the next valid after the system solvable.
176            */
177           SolvableIdType getFirstId()  const
178           { return getNextId( 1 ); }
179
180           /** Get id of the next valid \ref Solvable.
181            * This goes round robbin. At the end it returns \ref noSolvableId.
182            * Passing \ref noSolvableId it returns the 1st valid  \ref Solvable.
183            * \see \ref validSolvable.
184            */
185           SolvableIdType getNextId( SolvableIdType id_r ) const
186           {
187             for( ++id_r; id_r < unsigned(_pool->nsolvables); ++id_r )
188             {
189               if ( validSolvable( _pool->solvables[id_r] ) )
190                 return id_r;
191             }
192             return noSolvableId;
193           }
194
195         public:
196           /** */
197           const RepoInfo & repoInfo( RepoIdType id_r )
198           { return _repoinfos[id_r]; }
199           /** Also adjust repo priority and subpriority accordingly. */
200           void setRepoInfo( RepoIdType id_r, const RepoInfo & info_r );
201           /** */
202           void eraseRepoInfo( RepoIdType id_r )
203           { _repoinfos.erase( id_r ); }
204
205         public:
206           /** Returns the id stored at \c offset_r in the internal
207            * whatprovidesdata array.
208           */
209           const sat::detail::IdType whatProvidesData( unsigned offset_r )
210           { return _pool->whatprovidesdata[offset_r]; }
211
212           /** Returns offset into the internal whatprovidesdata array.
213            * Use \ref whatProvidesData to get the stored Id.
214           */
215           unsigned whatProvides( Capability cap_r )
216           { prepare(); return ::pool_whatprovides( _pool, cap_r.id() ); }
217
218         public:
219           /** \name Requested locales. */
220           //@{
221           void setTextLocale( const Locale & locale_r );
222           void setRequestedLocales( const LocaleSet & locales_r );
223           bool addRequestedLocale( const Locale & locale_r );
224           bool eraseRequestedLocale( const Locale & locale_r );
225
226           const LocaleSet & getRequestedLocales() const
227           { return _requestedLocales; }
228
229           bool isRequestedLocale( const Locale & locale_r ) const
230           {
231             LocaleSet::const_iterator it( _requestedLocales.find( locale_r ) );
232             return it != _requestedLocales.end();
233           }
234
235           const LocaleSet & getAvailableLocales() const;
236
237           bool isAvailableLocale( const Locale & locale_r ) const
238           {
239             const LocaleSet & avl( getAvailableLocales() );
240             LocaleSet::const_iterator it( avl.find( locale_r ) );
241             return it != avl.end();
242           }
243           //@}
244
245         public:
246           /** \name Multiversion install. */
247           //@{
248           typedef IdStringSet MultiversionList;
249
250           const MultiversionList & multiversionList() const
251           {
252             if ( ! _multiversionListPtr )
253               multiversionListInit();
254             return *_multiversionListPtr;
255           }
256
257           bool isMultiversion( IdString ident_r ) const
258           {
259             const MultiversionList & l( multiversionList() );
260             return l.find( ident_r ) != l.end();
261           }
262           //@}
263
264         public:
265           /** \name Installed on behalf of a user request hint. */
266           //@{
267           /** Get ident list of all autoinstalled solvables. */
268           StringQueue autoInstalled() const
269           { return _autoinstalled; }
270
271           /** Set ident list of all autoinstalled solvables. */
272           void setAutoInstalled( const StringQueue & autoInstalled_r )
273           { _autoinstalled = autoInstalled_r; }
274
275           bool isOnSystemByUser( IdString ident_r ) const
276           { return !_autoinstalled.contains( ident_r.id() ); }
277           //@}
278
279         public:
280           /** accessor for etc/sysconfig/storage reading file on demand */
281           const std::set<std::string> & requiredFilesystems() const;
282
283         private:
284           /** sat-pool. */
285           CPool * _pool;
286           /** Serial number. */
287           SerialNumber _serial;
288           /** Watch serial number. */
289           SerialNumberWatcher _watcher;
290           /** Additional \ref RepoInfo. */
291           std::map<RepoIdType,RepoInfo> _repoinfos;
292
293           /**  */
294           LocaleSet _requestedLocales;
295           mutable scoped_ptr<LocaleSet> _availableLocalesPtr;
296           mutable std::tr1::unordered_set<IdString> _locale2Solver;
297
298           /**  */
299           void multiversionListInit() const;
300           mutable scoped_ptr<MultiversionList> _multiversionListPtr;
301
302           /**  */
303           sat::StringQueue _autoinstalled;
304
305           /** filesystems mentioned in /etc/sysconfig/storage */
306           mutable scoped_ptr<std::set<std::string> > _requiredFilesystemsPtr;
307       };
308       ///////////////////////////////////////////////////////////////////
309
310       /////////////////////////////////////////////////////////////////
311     } // namespace detail
312     ///////////////////////////////////////////////////////////////////
313     /////////////////////////////////////////////////////////////////
314   } // namespace sat
315   ///////////////////////////////////////////////////////////////////
316   /////////////////////////////////////////////////////////////////
317 } // namespace zypp
318 ///////////////////////////////////////////////////////////////////
319 #define POOL_SETDIRTY
320 #endif // ZYPP_SAT_DETAIL_POOLIMPL_H