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