a94c90a2ec9d593843d90d64866122769060fa7e
[platform/upstream/libzypp.git] / zypp / sat / Pool.cc
1 /*---------------------------------------------------------------------\
2 |                          ____ _   __ __ ___                          |
3 |                         |__  / \ / / . \ . \                         |
4 |                           / / \ V /|  _/  _/                         |
5 |                          / /__ | | | | | |                           |
6 |                         /_____||_| |_| |_|                           |
7 |                                                                      |
8 \---------------------------------------------------------------------*/
9 /** \file       zypp/sat/Pool.cc
10  *
11 */
12 #include <sys/types.h>
13 #include <sys/stat.h>
14 #include <fcntl.h>
15
16 extern "C"
17 {
18 #include <solv/pool.h>
19 #include <solv/repo.h>
20 #include <solv/solvable.h>
21 }
22
23 #include <iostream>
24 #include <fstream>
25
26 #include "zypp/base/Easy.h"
27 #include "zypp/base/Logger.h"
28 #include "zypp/base/Gettext.h"
29 #include "zypp/base/Exception.h"
30
31 #include "zypp/AutoDispose.h"
32
33 #include "zypp/sat/detail/PoolImpl.h"
34 #include "zypp/sat/Pool.h"
35 #include "zypp/sat/LookupAttr.h"
36
37 using std::endl;
38
39 ///////////////////////////////////////////////////////////////////
40 namespace zypp
41 { /////////////////////////////////////////////////////////////////
42   ///////////////////////////////////////////////////////////////////
43   namespace sat
44   { /////////////////////////////////////////////////////////////////
45
46     const std::string & Pool::systemRepoAlias()
47     { return detail::PoolImpl::systemRepoAlias(); }
48
49     detail::CPool * Pool::get() const
50     { return myPool().getPool(); }
51
52     Pool::size_type Pool::capacity() const
53     { return myPool()->nsolvables; }
54
55     const SerialNumber & Pool::serial() const
56     { return myPool().serial(); }
57
58     const SerialNumber & Pool::serialIDs() const
59     { return myPool().serialIDs(); }
60
61     void Pool::prepare() const
62     { return myPool().prepare(); }
63
64     Pathname Pool::rootDir() const
65     { return myPool().rootDir(); }
66
67     void Pool::rootDir( const Pathname & root_r )
68     { return myPool().rootDir( root_r ); }
69
70     bool Pool::reposEmpty() const
71     { return ! myPool()->urepos; }
72
73     Pool::size_type Pool::reposSize() const
74     { return myPool()->urepos; }
75
76     Pool::RepositoryIterator Pool::reposBegin() const
77     {
78       if ( myPool()->urepos )
79       { // repos[0] == NULL
80         for_( it, myPool()->repos+1, myPool()->repos+myPool()->nrepos )
81           if ( *it )
82             return RepositoryIterator( it );
83       }
84       return reposEnd();
85     }
86
87     Pool::RepositoryIterator Pool::reposEnd() const
88     { return RepositoryIterator( myPool()->repos+myPool()->nrepos ); }
89
90     bool Pool::solvablesEmpty() const
91     {
92       // return myPool()->nsolvables;
93       // nsolvables is the array size including
94       // invalid Solvables.
95       for_( it, reposBegin(), reposEnd() )
96       {
97         if ( ! it->solvablesEmpty() )
98           return false;
99       }
100       return true;
101     }
102
103     Pool::size_type Pool::solvablesSize() const
104     {
105       // Do not return myPool()->nsolvables;
106       // nsolvables is the array size including
107       // invalid Solvables.
108       size_type ret = 0;
109       for_( it, reposBegin(), reposEnd() )
110       {
111         ret += it->solvablesSize();
112       }
113       return ret;
114     }
115
116     Pool::SolvableIterator Pool::solvablesBegin() const
117     { return SolvableIterator( myPool().getFirstId() ); }
118
119     Pool::SolvableIterator Pool::solvablesEnd() const
120     { return SolvableIterator(); }
121
122     Repository Pool::reposInsert( const std::string & alias_r )
123     {
124       Repository ret( reposFind( alias_r ) );
125       if ( ret )
126         return ret;
127
128       ret = Repository( myPool()._createRepo( alias_r ) );
129       if ( ret.isSystemRepo() )
130       {
131         // autoprovide (dummy) RepoInfo
132         RepoInfo info;
133         info.setAlias( alias_r );
134         info.setName( alias_r );
135         info.setAutorefresh( true );
136         info.setEnabled( true );
137         ret.setInfo( info );
138       }
139       return ret;
140     }
141
142     Repository Pool::reposFind( const std::string & alias_r ) const
143     {
144       for_( it, reposBegin(), reposEnd() )
145       {
146         if ( alias_r == it->alias() )
147           return *it;
148       }
149       return Repository();
150     }
151
152     Repository Pool::findSystemRepo() const
153     {
154       return Repository( myPool().systemRepo() );
155     }
156
157     Repository Pool::systemRepo()
158     {
159       if ( myPool().systemRepo() )
160         return Repository( myPool().systemRepo() );
161       return reposInsert( systemRepoAlias() );
162     }
163
164     Repository Pool::addRepoSolv( const Pathname & file_r, const std::string & alias_r )
165     {
166       // Using a temporay repo! (The additional parenthesis are required.)
167       AutoDispose<Repository> tmprepo( (Repository::EraseFromPool()) );
168       *tmprepo = reposInsert( alias_r );
169       tmprepo->addSolv( file_r );
170
171       // no exceptions so we keep it:
172       tmprepo.resetDispose();
173       return tmprepo;
174     }
175
176     Repository Pool::addRepoSolv( const Pathname & file_r )
177     { return addRepoSolv( file_r, file_r.basename() ); }
178
179     Repository Pool::addRepoSolv( const Pathname & file_r, const RepoInfo & info_r )
180     {
181       Repository ret( addRepoSolv( file_r, info_r.alias() ) );
182       ret.setInfo( info_r );
183       return ret;
184     }
185
186     /////////////////////////////////////////////////////////////////
187
188     Repository Pool::addRepoHelix( const Pathname & file_r, const std::string & alias_r )
189     {
190       // Using a temporay repo! (The additional parenthesis are required.)
191       AutoDispose<Repository> tmprepo( (Repository::EraseFromPool()) );
192       *tmprepo = reposInsert( alias_r );
193       tmprepo->addHelix( file_r );
194
195       // no exceptions so we keep it:
196       tmprepo.resetDispose();
197       return tmprepo;
198     }
199
200     Repository Pool::addRepoHelix( const Pathname & file_r )
201     { return addRepoHelix( file_r, file_r.basename() ); }
202
203     Repository Pool::addRepoHelix( const Pathname & file_r, const RepoInfo & info_r )
204     {
205       Repository ret( addRepoHelix( file_r, info_r.alias() ) );
206       ret.setInfo( info_r );
207       return ret;
208     }
209
210    /////////////////////////////////////////////////////////////////
211
212     void Pool::setTextLocale( const Locale & locale_r )
213     { myPool().setTextLocale( locale_r ); }
214
215     void Pool::setRequestedLocales( const LocaleSet & locales_r )
216     { myPool().setRequestedLocales( locales_r ); }
217
218     bool Pool::addRequestedLocale( const Locale & locale_r )
219     { return myPool().addRequestedLocale( locale_r ); }
220
221     bool Pool::eraseRequestedLocale( const Locale & locale_r )
222     { return myPool().eraseRequestedLocale( locale_r ); }
223
224     const LocaleSet & Pool::getRequestedLocales() const
225     { return myPool().getRequestedLocales(); }
226
227     bool Pool::isRequestedLocale( const Locale & locale_r ) const
228     { return myPool().isRequestedLocale( locale_r ); }
229
230     void Pool::initRequestedLocales( const LocaleSet & locales_r )      { myPool().initRequestedLocales( locales_r ); }
231     const LocaleSet & Pool::getAddedRequestedLocales() const            { return myPool().getAddedRequestedLocales(); }
232     const LocaleSet & Pool::getRemovedRequestedLocales() const          { return myPool().getRemovedRequestedLocales(); }
233
234     const LocaleSet & Pool::getAvailableLocales() const
235     {  return myPool().getAvailableLocales(); }
236
237     bool Pool::isAvailableLocale( const Locale & locale_r ) const
238     { return myPool().isAvailableLocale( locale_r ); }
239
240     const  Pool::MultiversionList &  Pool::multiversion() const
241     { return myPool().multiversionList(); }
242
243     Queue Pool::autoInstalled() const                           { return myPool().autoInstalled(); }
244     void Pool::setAutoInstalled( const Queue & autoInstalled_r ){ myPool().setAutoInstalled( autoInstalled_r ); }
245
246     void Pool::setNeedrebootSpec( sat::SolvableSpec needrebootSpec_r )  { myPool().setNeedrebootSpec( std::move(needrebootSpec_r) ); }
247
248    /******************************************************************
249     **
250     **  FUNCTION NAME : operator<<
251     **  FUNCTION TYPE : std::ostream &
252     */
253     std::ostream & operator<<( std::ostream & str, const Pool & obj )
254     {
255       return str << "sat::pool(" << obj.serial() << ")["
256           << obj.capacity() << "]{"
257           << obj.reposSize() << "repos|"
258           << obj.solvablesSize() << "solv}";
259     }
260
261     /////////////////////////////////////////////////////////////////
262     #undef ZYPP_BASE_LOGGER_LOGGROUP
263     #define ZYPP_BASE_LOGGER_LOGGROUP "solvidx"
264
265     void updateSolvFileIndex( const Pathname & solvfile_r )
266     {
267       AutoDispose<FILE*> solv( ::fopen( solvfile_r.c_str(), "re" ), ::fclose );
268       if ( solv == NULL )
269       {
270         solv.resetDispose();
271         ERR << "Can't open solv-file: " << solv << endl;
272         return;
273       }
274
275       std::string solvidxfile( solvfile_r.extend(".idx").asString() );
276       if ( ::unlink( solvidxfile.c_str() ) == -1 && errno != ENOENT )
277       {
278         ERR << "Can't unlink solv-idx: " << Errno() << endl;
279         return;
280       }
281       {
282         int fd = ::open( solvidxfile.c_str(), O_CREAT|O_EXCL|O_WRONLY|O_TRUNC, 0644 );
283         if ( fd == -1 )
284         {
285           ERR << "Can't create solv-idx: " << Errno() << endl;
286           return;
287         }
288         ::close( fd );
289       }
290       std::ofstream idx( solvidxfile.c_str() );
291
292
293       detail::CPool * _pool = ::pool_create();
294       detail::CRepo * _repo = ::repo_create( _pool, "" );
295       if ( ::repo_add_solv( _repo, solv, 0 ) == 0 )
296       {
297         int _id = 0;
298         detail::CSolvable * _solv = nullptr;
299         FOR_REPO_SOLVABLES( _repo, _id, _solv )
300         {
301           if ( _solv )
302           {
303 #define SEP '\t'
304 #define idstr(V) pool_id2str( _pool, _solv->V )
305             if ( _solv->arch == ARCH_SRC || _solv->arch == ARCH_NOSRC )
306               idx << "srcpackage:" << idstr(name) << SEP << idstr(evr) << SEP << "noarch" << endl;
307             else
308               idx << idstr(name) << SEP << idstr(evr) << SEP << idstr(arch) << endl;
309           }
310         }
311       }
312       else
313       {
314         ERR << "Can't read solv-file: " << ::pool_errstr( _pool ) << endl;
315       }
316       ::repo_free( _repo, 0 );
317       ::pool_free( _pool );
318     }
319
320     /////////////////////////////////////////////////////////////////
321   } // namespace sat
322   ///////////////////////////////////////////////////////////////////
323   /////////////////////////////////////////////////////////////////
324 } // namespace zypp
325 ///////////////////////////////////////////////////////////////////