libzypp  17.7.0
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  const SerialNumber & Pool::serialIDs() const
59  { return myPool().serialIDs(); }
60 
61  void Pool::prepare() const
62  { return myPool().prepare(); }
63 
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 
74  { return myPool()->urepos; }
75 
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 
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 
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 
117  { return SolvableIterator( myPool().getFirstId() ); }
118 
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 
153  {
154  return Repository( myPool().systemRepo() );
155  }
156 
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.)
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 
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 
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.)
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 
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 
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 
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 ); }
233 
235  { return myPool().getAvailableLocales(); }
236 
237  bool Pool::isAvailableLocale( const Locale & locale_r ) const
238  { return myPool().isAvailableLocale( locale_r ); }
239 
241  { return myPool().multiversionList(); }
242 
244  void Pool::setAutoInstalled( const Queue & autoInstalled_r ){ myPool().setAutoInstalled( autoInstalled_r ); }
245 
246  /******************************************************************
247  **
248  ** FUNCTION NAME : operator<<
249  ** FUNCTION TYPE : std::ostream &
250  */
251  std::ostream & operator<<( std::ostream & str, const Pool & obj )
252  {
253  return str << "sat::pool(" << obj.serial() << ")["
254  << obj.capacity() << "]{"
255  << obj.reposSize() << "repos|"
256  << obj.solvablesSize() << "solv}";
257  }
258 
260  #undef ZYPP_BASE_LOGGER_LOGGROUP
261  #define ZYPP_BASE_LOGGER_LOGGROUP "solvidx"
262 
263  void updateSolvFileIndex( const Pathname & solvfile_r )
264  {
265  AutoDispose<FILE*> solv( ::fopen( solvfile_r.c_str(), "re" ), ::fclose );
266  if ( solv == NULL )
267  {
268  solv.resetDispose();
269  ERR << "Can't open solv-file: " << solv << endl;
270  return;
271  }
272 
273  std::string solvidxfile( solvfile_r.extend(".idx").asString() );
274  if ( ::unlink( solvidxfile.c_str() ) == -1 && errno != ENOENT )
275  {
276  ERR << "Can't unlink solv-idx: " << Errno() << endl;
277  return;
278  }
279  {
280  int fd = ::open( solvidxfile.c_str(), O_CREAT|O_EXCL|O_WRONLY|O_TRUNC, 0644 );
281  if ( fd == -1 )
282  {
283  ERR << "Can't create solv-idx: " << Errno() << endl;
284  return;
285  }
286  ::close( fd );
287  }
288  std::ofstream idx( solvidxfile.c_str() );
289 
290 
291  detail::CPool * _pool = ::pool_create();
292  detail::CRepo * _repo = ::repo_create( _pool, "" );
293  if ( ::repo_add_solv( _repo, solv, 0 ) == 0 )
294  {
295  int _id = 0;
296  detail::CSolvable * _solv = nullptr;
297  FOR_REPO_SOLVABLES( _repo, _id, _solv )
298  {
299  if ( _solv )
300  {
301 #define SEP '\t'
302 #define idstr(V) pool_id2str( _pool, _solv->V )
303  if ( _solv->arch == ARCH_SRC || _solv->arch == ARCH_NOSRC )
304  idx << "srcpackage:" << idstr(name) << SEP << idstr(evr) << SEP << "noarch" << endl;
305  else
306  idx << idstr(name) << SEP << idstr(evr) << SEP << idstr(arch) << endl;
307  }
308  }
309  }
310  else
311  {
312  ERR << "Can't read solv-file: " << ::pool_errstr( _pool ) << endl;
313  }
314  ::repo_free( _repo, 0 );
315  ::pool_free( _pool );
316  }
317 
319  } // namespace sat
322 } // namespace zypp
Repository reposInsert(const std::string &alias_r)
Return a Repository named alias_r.
Definition: Pool.cc:122
Interface to gettext.
const SerialNumber & serial() const
Serial number changing whenever the content changes.
Definition: PoolImpl.h:66
StringQueue autoInstalled() const
Get ident list of all autoinstalled solvables.
Definition: PoolImpl.h:297
const LocaleSet & getAddedRequestedLocales() const
Added since last initRequestedLocales.
Definition: Pool.cc:231
bool eraseRequestedLocale(const Locale &locale_r)
User change (tracked).
Definition: PoolImpl.cc:468
void setRequestedLocales(const LocaleSet &locales_r)
Set the requested locales.
Definition: Pool.cc:215
bool isRequestedLocale(const Locale &locale_r) const
Definition: PoolImpl.h:255
bool reposEmpty() const
Whether Pool contains repos.
Definition: Pool.cc:70
void setAutorefresh(bool autorefresh)
enable or disable autorefresh
Definition: RepoInfoBase.cc:91
bool isSystemRepo() const
Return whether this is the system repository.
Definition: Repository.cc:53
Convenience errno wrapper.
Definition: Errno.h:25
void setTextLocale(const Locale &locale_r)
Set the default language for retrieving translated texts.
Definition: Pool.cc:212
::_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:457
SolvableIterator solvablesBegin() const
Iterator to the first Solvable.
Definition: Pool.cc:116
bool isAvailableLocale(const Locale &locale_r) const
Whether this Locale is in the set of available locales.
Definition: Pool.cc:237
const MultiversionList & multiversionList() const
Definition: PoolImpl.cc:597
const LocaleSet & getAddedRequestedLocales() const
Added since last initRequestedLocales.
Definition: PoolImpl.h:244
Pathname extend(const std::string &r) const
Append string r to the last component of the path.
Definition: Pathname.h:169
std::ostream & operator<<(std::ostream &str, const FileConflicts &obj)
void setEnabled(bool enabled)
enable or disable the repository
Definition: RepoInfoBase.cc:88
::_Pool CPool
Wrapped libsolv C data type exposed as backdoor.
Definition: PoolMember.h:61
const char * c_str() const
String representation.
Definition: Pathname.h:109
RepositoryIterator reposEnd() const
Iterator behind the last Repository.
Definition: Pool.cc:87
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:244
What is known about a repository.
Definition: RepoInfo.h:71
#define for_(IT, BEG, END)
Convenient for-loops using iterator.
Definition: Easy.h:27
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:188
::_Solvable CSolvable
Wrapped libsolv C data type exposed as backdoor.
Definition: PoolMember.h:64
std::string basename() const
Return the last component of this path.
Definition: Pathname.h:127
size_type capacity() const
Internal array size for stats only.
Definition: Pool.cc:52
size_type reposSize() const
Number of repos in Pool.
Definition: Pool.cc:73
#define ERR
Definition: Logger.h:66
const LocaleSet & getAvailableLocales() const
Get the set of available locales.
Definition: Pool.cc:234
Repository systemRepo()
Return the system repository, create it if missing.
Definition: Pool.cc:157
void prepare() const
Update housekeeping data if necessary (e.g.
Definition: Pool.cc:61
void initRequestedLocales(const LocaleSet &locales_r)
Start tracking changes based on this locales_r.
Definition: Pool.cc:230
Functor removing Repository from it&#39;s Pool.
Definition: Repository.h:430
const SerialNumber & serialIDs() const
Serial number changing whenever resusePoolIDs==true was used.
Definition: PoolImpl.h:70
const LocaleSet & getRequestedLocales() const
Current set of requested Locales.
Definition: PoolImpl.h:252
CPool * getPool() const
Definition: PoolImpl.h:164
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
const std::string & asString() const
String representation.
Definition: Pathname.h:90
std::string alias() const
unique identifier for this source.
void setInfo(const RepoInfo &info_r)
Set RepoInfo for this repository.
Definition: Repository.cc:279
zypp::detail::RepositoryIterator RepositoryIterator
Definition: Pool.h:48
Pathname rootDir() const
Get rootdir (for file conflicts check)
Definition: Pool.cc:64
void setRequestedLocales(const LocaleSet &locales_r)
User change (tracked).
Definition: PoolImpl.cc:448
detail::CPool * get() const
Expert backdoor.
Definition: Pool.cc:49
const LocaleSet & getRemovedRequestedLocales() const
Removed since last initRequestedLocales.
Definition: PoolImpl.h:248
void updateSolvFileIndex(const Pathname &solvfile_r)
Create solv file content digest for zypper bash completion.
Definition: Pool.cc:263
const SerialNumber & serial() const
Housekeeping data serial number.
Definition: Pool.cc:55
size_type solvablesSize() const
Number of solvables in Pool.
Definition: Pool.cc:103
static PoolImpl & myPool()
Definition: PoolImpl.cc:172
detail::SolvableIterator SolvableIterator
Definition: Pool.h:47
const LocaleSet & getRequestedLocales() const
Return the requested locales.
Definition: Pool.cc:224
bool addRequestedLocale(const Locale &locale_r)
Add one Locale to the set of requested locales.
Definition: Pool.cc:218
RepositoryIterator reposBegin() const
Iterator to the first Repository.
Definition: Pool.cc:76
bool solvablesEmpty() const
Whether Pool contains solvables.
Definition: Pool.cc:90
void prepare() const
Update housekeeping data (e.g.
Definition: PoolImpl.cc:266
void resetDispose()
Set no dispose function.
Definition: AutoDispose.h:162
Simple serial number provider.
Definition: SerialNumber.h:44
const MultiversionList & multiversion() const
Definition: Pool.cc:240
&#39;Language[_Country]&#39; codes.
Definition: Locale.h:49
Pathname rootDir() const
Get rootdir (for file conflicts check)
Definition: PoolImpl.h:106
Libsolv Id queue wrapper.
Definition: Queue.h:34
Queue autoInstalled() const
Get ident list of all autoinstalled solvables.
Definition: Pool.cc:243
Repository reposFind(const std::string &alias_r) const
Find a Repository named alias_r.
Definition: Pool.cc:142
const LocaleSet & getRemovedRequestedLocales() const
Removed since last initRequestedLocales.
Definition: Pool.cc:232
SolvableIterator solvablesEnd() const
Iterator behind the last Solvable.
Definition: Pool.cc:119
detail::size_type size_type
Definition: Pool.h:49
void setTextLocale(const Locale &locale_r)
Definition: PoolImpl.cc:422
Repository findSystemRepo() const
Return the system repository if it is on the pool.
Definition: Pool.cc:152
void initRequestedLocales(const LocaleSet &locales_r)
Start tracking changes based on this locales_r.
Definition: PoolImpl.cc:439
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
Global sat-pool.
Definition: Pool.h:44
const LocaleSet & getAvailableLocales() const
All Locales occurring in any repo.
Definition: PoolImpl.cc:551
const SerialNumber & serialIDs() const
Serial number changing whenever resusePoolIDs==true was used.
Definition: Pool.cc:58
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:164
Easy-to use interface to the ZYPP dependency resolver.
Definition: CodePitfalls.doc:1
Iterable< RepositoryIterator > repos() const
Iterate the repositories.
Definition: Pool.h:93
bool isAvailableLocale(const Locale &locale_r) const
Definition: PoolImpl.h:268
bool isRequestedLocale(const Locale &locale_r) const
Whether this Locale is in the set of requested locales.
Definition: Pool.cc:227
#define idstr(V)
bool eraseRequestedLocale(const Locale &locale_r)
Erase one Locale from the set of requested locales.
Definition: Pool.cc:221
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:301
#define SEP
Solvable set wrapper to allow adding additional convenience iterators.
Definition: SolvableSet.h:35