libzypp  16.1.1
Pool.cc
Go to the documentation of this file.
1 /*---------------------------------------------------------------------\
2 | ____ _ __ __ ___ |
3 | |__ / \ / / . \ . \ |
4 | / / \ V /| _/ _/ |
5 | / /__ | | | | | | |
6 | /_____||_| |_| |_| |
7 | |
8 \---------------------------------------------------------------------*/
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 
34 #include "zypp/sat/Pool.h"
35 #include "zypp/sat/LookupAttr.h"
36 
37 using std::endl;
38 
40 namespace zypp
41 {
42  namespace sat
44  {
45 
46  const std::string & Pool::systemRepoAlias()
48 
50  { return myPool().getPool(); }
51 
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 
71  { return myPool()->urepos; }
72 
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 
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 
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 
114  { return SolvableIterator( myPool().getFirstId() ); }
115 
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 
150  {
151  return Repository( myPool().systemRepo() );
152  }
153 
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.)
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 
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.)
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 
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 
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 ); }
230 
232  { return myPool().getAvailableLocales(); }
233 
234  bool Pool::isAvailableLocale( const Locale & locale_r ) const
235  { return myPool().isAvailableLocale( locale_r ); }
236 
238  { return myPool().multiversionList(); }
239 
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 
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 
316  } // namespace sat
319 } // namespace zypp
Repository reposInsert(const std::string &alias_r)
Return a Repository named alias_r.
Definition: Pool.cc:119
Interface to gettext.
Pathname rootDir() const
Get rootdir (for file conflicts check)
Definition: PoolImpl.h:102
std::string alias() const
unique identifier for this source.
bool eraseRequestedLocale(const Locale &locale_r)
User change (tracked).
Definition: PoolImpl.cc:459
void setRequestedLocales(const LocaleSet &locales_r)
Set the requested locales.
Definition: Pool.cc:212
void setAutorefresh(bool autorefresh)
enable or disable autorefresh
Definition: RepoInfoBase.cc:91
size_type reposSize() const
Number of repos in Pool.
Definition: Pool.cc:70
Convenience errno wrapper.
Definition: Errno.h:25
void setTextLocale(const Locale &locale_r)
Set the default language for retrieving translated texts.
Definition: Pool.cc:209
SolvableIterator solvablesEnd() const
Iterator behind the last Solvable.
Definition: Pool.cc:116
::_Repo CRepo
Wrapped libsolv C data type exposed as backdoor.
Definition: PoolMember.h:63
bool addRequestedLocale(const Locale &locale_r)
User change (tracked).
Definition: PoolImpl.cc:448
void setEnabled(bool enabled)
enable or disable the repository
Definition: RepoInfoBase.cc:88
CPool * getPool() const
Definition: PoolImpl.h:160
::_Pool CPool
Wrapped libsolv C data type exposed as backdoor.
Definition: PoolMember.h:61
void setAlias(const std::string &alias)
set the repository alias
Definition: RepoInfoBase.cc:94
String related utilities and Regular expression matching.
void setAutoInstalled(const Queue &autoInstalled_r)
Set ident list of all autoinstalled solvables.
Definition: Pool.cc:241
bool isRequestedLocale(const Locale &locale_r) const
Whether this Locale is in the set of requested locales.
Definition: Pool.cc:224
detail::CPool * get() const
Expert backdoor.
Definition: Pool.cc:49
What is known about a repository.
Definition: RepoInfo.h:71
bool isAvailableLocale(const Locale &locale_r) const
Definition: PoolImpl.h:264
#define for_(IT, BEG, END)
Convenient for-loops using iterator.
Definition: Easy.h:27
const MultiversionList & multiversion() const
Definition: Pool.cc:237
const LocaleSet & getAddedRequestedLocales() const
Added since last initRequestedLocales.
Definition: PoolImpl.h:240
Repository addRepoHelix(const Pathname &file_r, const std::string &name_r)
Load Solvables from a helix-file into a Repository named name_r.
Definition: Pool.cc:185
::_Solvable CSolvable
Wrapped libsolv C data type exposed as backdoor.
Definition: PoolMember.h:64
bool isSystemRepo() const
Return whether this is the system repository.
Definition: Repository.cc:53
const MultiversionList & multiversionList() const
Definition: PoolImpl.cc:588
const SerialNumber & serial() const
Housekeeping data serial number.
Definition: Pool.cc:55
const LocaleSet & getRequestedLocales() const
Current set of requested Locales.
Definition: PoolImpl.h:248
size_type solvablesSize() const
Number of solvables in Pool.
Definition: Pool.cc:100
#define ERR
Definition: Logger.h:66
const SerialNumber & serial() const
Serial number changing whenever the content changes.
Definition: PoolImpl.h:66
RepositoryIterator reposEnd() const
Iterator behind the last Repository.
Definition: Pool.cc:84
size_type capacity() const
Internal array size for stats only.
Definition: Pool.cc:52
const LocaleSet & getAvailableLocales() const
All Locales occurring in any repo.
Definition: PoolImpl.cc:542
Repository systemRepo()
Return the system repository, create it if missing.
Definition: Pool.cc:154
void initRequestedLocales(const LocaleSet &locales_r)
Start tracking changes based on this locales_r.
Definition: Pool.cc:227
Functor removing Repository from it&#39;s Pool.
Definition: Repository.h:430
const LocaleSet & getRemovedRequestedLocales() const
Removed since last initRequestedLocales.
Definition: Pool.cc:229
const LocaleSet & getRemovedRequestedLocales() const
Removed since last initRequestedLocales.
Definition: PoolImpl.h:244
int unlink(const Pathname &path)
Like &#39;unlink&#39;.
Definition: PathInfo.cc:653
static const std::string & systemRepoAlias()
Reserved system repository alias .
Definition: Pool.cc:46
void setInfo(const RepoInfo &info_r)
Set RepoInfo for this repository.
Definition: Repository.cc:279
zypp::detail::RepositoryIterator RepositoryIterator
Definition: Pool.h:48
void setRequestedLocales(const LocaleSet &locales_r)
User change (tracked).
Definition: PoolImpl.cc:439
Queue autoInstalled() const
Get ident list of all autoinstalled solvables.
Definition: Pool.cc:240
bool reposEmpty() const
Whether Pool contains repos.
Definition: Pool.cc:67
Repository reposFind(const std::string &alias_r) const
Find a Repository named alias_r.
Definition: Pool.cc:139
RepositoryIterator reposBegin() const
Iterator to the first Repository.
Definition: Pool.cc:73
Pathname rootDir() const
Get rootdir (for file conflicts check)
Definition: Pool.cc:61
void updateSolvFileIndex(const Pathname &solvfile_r)
Create solv file content digest for zypper bash completion.
Definition: Pool.cc:260
StringQueue autoInstalled() const
Get ident list of all autoinstalled solvables.
Definition: PoolImpl.h:293
static PoolImpl & myPool()
Definition: PoolImpl.cc:166
detail::SolvableIterator SolvableIterator
Definition: Pool.h:47
bool addRequestedLocale(const Locale &locale_r)
Add one Locale to the set of requested locales.
Definition: Pool.cc:215
void resetDispose()
Set no dispose function.
Definition: AutoDispose.h:162
Simple serial number provider.
Definition: SerialNumber.h:44
&#39;Language[_Country]&#39; codes.
Definition: Locale.h:49
Libsolv Id queue wrapper.
Definition: Queue.h:34
const LocaleSet & getAddedRequestedLocales() const
Added since last initRequestedLocales.
Definition: Pool.cc:228
std::ostream & operator<<(std::ostream &str, const Pool &obj)
Definition: Pool.cc:248
detail::size_type size_type
Definition: Pool.h:49
void setTextLocale(const Locale &locale_r)
Definition: PoolImpl.cc:413
const LocaleSet & getRequestedLocales() const
Return the requested locales.
Definition: Pool.cc:221
void prepare() const
Update housekeeping data (e.g.
Definition: PoolImpl.cc:260
bool isAvailableLocale(const Locale &locale_r) const
Whether this Locale is in the set of available locales.
Definition: Pool.cc:234
void initRequestedLocales(const LocaleSet &locales_r)
Start tracking changes based on this locales_r.
Definition: PoolImpl.cc:430
static const std::string & systemRepoAlias()
Reserved system repository alias .
Definition: PoolImpl.cc:94
Reference counted access to a Tp object calling a custom Dispose function when the last AutoDispose h...
Definition: AutoDispose.h:92
void prepare() const
Update housekeeping data if necessary (e.g.
Definition: Pool.cc:58
Global sat-pool.
Definition: Pool.h:44
SolvableIterator solvablesBegin() const
Iterator to the first Solvable.
Definition: Pool.cc:113
const LocaleSet & getAvailableLocales() const
Get the set of available locales.
Definition: Pool.cc:231
bool isRequestedLocale(const Locale &locale_r) const
Definition: PoolImpl.h:251
void setName(const std::string &name)
set the repository name
Definition: RepoInfoBase.cc:97
Repository addRepoSolv(const Pathname &file_r, const std::string &name_r)
Load Solvables from a solv-file into a Repository named name_r.
Definition: Pool.cc:161
bool solvablesEmpty() const
Whether Pool contains solvables.
Definition: Pool.cc:87
Easy-to use interface to the ZYPP dependency resolver.
Definition: CodePitfalls.doc:1
#define idstr(V)
Repository findSystemRepo() const
Return the system repository if it is on the pool.
Definition: Pool.cc:149
bool eraseRequestedLocale(const Locale &locale_r)
Erase one Locale from the set of requested locales.
Definition: Pool.cc:218
std::unordered_set< Locale > LocaleSet
Definition: Locale.h:27
void setAutoInstalled(const StringQueue &autoInstalled_r)
Set ident list of all autoinstalled solvables.
Definition: PoolImpl.h:297
Iterable< RepositoryIterator > repos() const
Iterate the repositories.
Definition: Pool.h:90
#define SEP
Solvable set wrapper to allow adding additional convenience iterators.
Definition: SolvableSet.h:35