libzypp  13.10.6
Repository.cc
Go to the documentation of this file.
1 /*---------------------------------------------------------------------\
2 | ____ _ __ __ ___ |
3 | |__ / \ / / . \ . \ |
4 | / / \ V /| _/ _/ |
5 | / /__ | | | | | | |
6 | /_____||_| |_| |_| |
7 | |
8 \---------------------------------------------------------------------*/
12 #include <climits>
13 #include <iostream>
14 
15 #include "zypp/base/Logger.h"
16 #include "zypp/base/Gettext.h"
17 #include "zypp/base/Exception.h"
18 #include "zypp/base/Xml.h"
19 
20 #include "zypp/AutoDispose.h"
21 #include "zypp/Pathname.h"
22 
24 #include "zypp/Repository.h"
25 #include "zypp/sat/Pool.h"
26 
27 using std::endl;
28 
30 namespace zypp
31 {
32 
33  const Repository Repository::noRepository;
34 
35  const std::string & Repository::systemRepoAlias()
37 
39 
40  ::_Repo * Repository::get() const
41  { return myPool().getRepo( _id ); }
42 
43 #define NO_REPOSITORY_RETURN( VAL ) \
44  ::_Repo * _repo( get() ); \
45  if ( ! _repo ) return VAL
46 
47 #define NO_REPOSITORY_THROW( VAL ) \
48  ::_Repo * _repo( get() ); \
49  if ( ! _repo ) ZYPP_THROW( VAL )
50 
52  {
53  NO_REPOSITORY_RETURN( false );
54  return myPool().isSystemRepo( _repo );
55  }
56 
57  std::string Repository::alias() const
58  {
59  NO_REPOSITORY_RETURN( std::string() );
60  if ( ! _repo->name )
61  return std::string();
62  return _repo->name;
63  }
64 
65  std::string Repository::name() const
66  { return info().name(); }
67 
69  {
70  NO_REPOSITORY_RETURN( INT_MIN );
71  return _repo->priority;
72  }
73 
75  {
76  NO_REPOSITORY_RETURN( INT_MIN );
77  return _repo->subpriority;
78  }
79 
80 
82  {
85  return( q.empty() ? 0 : q.begin().asUnsigned() );
86  }
87 
89  {
91  Date generated = generatedTimestamp();
92  if ( ! generated )
93  return 0; // do not calculate over a missing generated timestamp
94 
96  if ( q.empty() )
97  return 0;
98 
99  return generated + q.begin().asUnsigned();
100  }
101 
103  {
106  }
107 
109  {
110  NO_REPOSITORY_RETURN( false );
111  // system repo is not mirrored
112  if ( isSystemRepo() )
113  return false;
114 
115  Date suggested = suggestedExpirationTimestamp();
116 
117  // if no data, don't suggest
118  if ( ! suggested )
119  return false;
120 
122  }
123 
124  bool Repository::providesUpdatesFor( const std::string &key ) const
125  {
126  NO_REPOSITORY_RETURN( false );
127 
128  for_( it,
131  {
132  // FIXME implement real CPE matching here
133  // someday
134  if ( key == it.cpeId() )
135  return true;
136  }
137 
138  return false;
139  }
140 
142  {
143  NO_REPOSITORY_RETURN( false );
144  return ( updatesProductBegin() != updatesProductEnd() );
145  }
146 
148  {
149  NO_REPOSITORY_RETURN( true );
150  return !_repo->nsolvables;
151  }
152 
154  {
156  return _repo->nsolvables;
157  }
158 
160  {
161  NO_REPOSITORY_RETURN( make_filter_iterator( detail::ByRepository( *this ),
164  return make_filter_iterator( detail::ByRepository( *this ),
165  sat::detail::SolvableIterator(_repo->start),
166  sat::detail::SolvableIterator(_repo->end) );
167  }
168 
170  {
171  NO_REPOSITORY_RETURN( make_filter_iterator( detail::ByRepository( *this ),
174  return make_filter_iterator(detail::ByRepository( *this ),
175  sat::detail::SolvableIterator(_repo->end),
176  sat::detail::SolvableIterator(_repo->end) );
177  }
178 
180  {
183  }
184 
186  {
187  return ProductInfoIterator();
188  }
189 
191  {
194  }
195 
197  {
198  return ProductInfoIterator();
199  }
200 
202  {
204  return myPool().repoInfo( _repo );
205  }
206 
207  void Repository::setInfo( const RepoInfo & info_r )
208  {
209  NO_REPOSITORY_THROW( Exception( "Can't set RepoInfo for norepo." ) );
210  if ( info_r.alias() != alias() )
211  {
212  ZYPP_THROW( Exception( str::form( "RepoInfo alias (%s) does not match repository alias (%s)",
213  info_r.alias().c_str(), alias().c_str() ) ) );
214  }
215  myPool().setRepoInfo( _repo, info_r );
216  MIL << *this << endl;
217  }
218 
220  {
222  myPool().setRepoInfo( _repo, RepoInfo() );
223  }
224 
226  {
228  MIL << *this << " removed from pool" << endl;
229  myPool()._deleteRepo( _repo );
231  }
232 
234  {
236  for_( it, sat::Pool::instance().reposBegin(), sat::Pool::instance().reposEnd() )
237  {
238  if ( *it == *this )
239  {
240  if ( ++it != _for_end )
241  return *it;
242  break;
243  }
244  }
245  return noRepository;
246  }
247 
248  void Repository::addSolv( const Pathname & file_r )
249  {
250  NO_REPOSITORY_THROW( Exception( "Can't add solvables to norepo." ) );
251 
252  AutoDispose<FILE*> file( ::fopen( file_r.c_str(), "re" ), ::fclose );
253  if ( file == NULL )
254  {
255  file.resetDispose();
256  ZYPP_THROW( Exception( "Can't open solv-file: "+file_r.asString() ) );
257  }
258 
259  if ( myPool()._addSolv( _repo, file ) != 0 )
260  {
261  ZYPP_THROW( Exception( "Error reading solv-file: "+file_r.asString() ) );
262  }
263 
264  MIL << *this << " after adding " << file_r << endl;
265  }
266 
267  void Repository::addHelix( const Pathname & file_r )
268  {
269  NO_REPOSITORY_THROW( Exception( "Can't add solvables to norepo." ) );
270 
271  std::string command( file_r.extension() == ".gz" ? "zcat " : "cat " );
272  command += file_r.asString();
273 
274  AutoDispose<FILE*> file( ::popen( command.c_str(), "re" ), ::pclose );
275  if ( file == NULL )
276  {
277  file.resetDispose();
278  ZYPP_THROW( Exception( "Can't open helix-file: "+file_r.asString() ) );
279  }
280 
281  if ( myPool()._addHelix( _repo, file ) != 0 )
282  {
283  ZYPP_THROW( Exception( "Error reading helix-file: "+file_r.asString() ) );
284  }
285 
286  MIL << *this << " after adding " << file_r << endl;
287  }
288 
290  {
291  NO_REPOSITORY_THROW( Exception( "Can't add solvables to norepo.") );
292  return myPool()._addSolvables( _repo, count_r );
293  }
294 
295  /******************************************************************
296  **
297  ** FUNCTION NAME : operator<<
298  ** FUNCTION TYPE : std::ostream &
299  */
300  std::ostream & operator<<( std::ostream & str, const Repository & obj )
301  {
302  if ( ! obj )
303  return str << "noRepository";
304 
305  return str << "sat::repo(" << obj.alias() << ")"
306  << "{"
307  << "prio " << obj.get()->priority << '.' << obj.get()->subpriority
308  << ", size " << obj.solvablesSize()
309  << "}";
310  }
311 
312  std::ostream & dumpAsXmlOn( std::ostream & str, const Repository & obj )
313  {
314  return xmlout::node( str, "repository", {
315  { "name", obj.name() },
316  { "alias", obj.alias() }
317  } );
318  }
319 
321  namespace detail
322  {
324  {
325  if ( base() )
326  {
327  ::_Pool * satpool = sat::Pool::instance().get();
328  do {
329  ++base_reference();
330  } while ( base() < satpool->repos+satpool->nrepos && !*base() );
331  }
332  }
333  } // namespace detail
335 
337  //
338  // Repository::ProductInfoIterator
339  //
341 
343  { base_reference() = sat::LookupRepoAttr( attr_r, repo_r ).begin(); }
344 
346  { return base_reference().subFind( sat::SolvAttr::repositoryProductLabel ).asString(); }
347 
349  { return base_reference().subFind( sat::SolvAttr::repositoryProductCpeid ).asString(); }
350 
352 } // namespace zypp
::_Pool * get() const
Expert backdoor.
Definition: Pool.cc:36
std::string name() const
Repository short label.
#define NO_REPOSITORY_RETURN(VAL)
Definition: Repository.cc:43
RepoInfo info() const
Return any associated RepoInfo.
Definition: Repository.cc:201
std::ostream & node(std::ostream &out_r, const std::string &name_r, Node::Attr attr_r)
Definition: Xml.h:164
Interface to gettext.
#define MIL
Definition: Logger.h:47
Functor filtering Solvable by Repository.
Definition: Repository.h:431
bool isSystemRepo(::_Repo *repo_r) const
Definition: PoolImpl.h:90
std::string alias() const
unique identifier for this source.
static const SolvAttr repositoryExpire
Definition: SolvAttr.h:159
static const SolvAttr repositoryProductLabel
Definition: SolvAttr.h:163
sat::ArrayAttr< std::string, std::string > Keywords
Definition: Repository.h:44
std::string alias() const
Short unique string to identify a repo.
Definition: Repository.cc:57
#define ZYPP_THROW(EXCPT)
Drops a logline and throws the Exception.
Definition: Exception.h:320
std::string name() const
Label to display for this repo.
Definition: Repository.cc:65
void addSolv(const Pathname &file_r)
Load Solvables from a solv-file.
Definition: Repository.cc:248
void resetDispose()
Set no dispose function.
Definition: AutoDispose.h:162
std::string label() const
Product label.
Definition: Repository.cc:345
std::ostream & dumpAsXmlOn(std::ostream &str, const CheckSum &obj)
Definition: CheckSum.cc:157
sat::Solvable::IdType addSolvables(unsigned count_r)
Add count_r new empty Solvable to this Repository.
Definition: Repository.cc:289
static const RepoIdType noRepoId(0)
Id to denote Repo::noRepository.
void clearInfo()
Remove any RepoInfo set for this repository.
Definition: Repository.cc:219
static const SolvAttr repositoryTimestamp
Definition: SolvAttr.h:158
unsigned SolvableIdType
Id type to connect Solvable and sat-solvable.
Definition: PoolMember.h:98
SolvableIterator solvablesEnd() const
Iterator behind the last Solvable.
Definition: Repository.cc:169
What is known about a repository.
Definition: RepoInfo.h:66
Date generatedTimestamp() const
Timestamp when this repository was generated.
Definition: Repository.cc:81
unsigned asUnsigned() const
Definition: LookupAttr.cc:560
#define for_(IT, BEG, END)
Convenient for-loops using iterator.
Definition: Easy.h:27
LookupAttr::transformIterator based container to retrieve list attributes.
Definition: LookupAttr.h:595
int satInternalSubPriority() const
Definition: Repository.cc:74
bool isSystemRepo() const
Return whether this is the system repository.
Definition: Repository.cc:51
bool providesUpdatesFor(const std::string &cpeid) const
whether the repository claims to update something prod with key cpeid
Definition: Repository.cc:124
void _deleteRepo(::_Repo *repo_r)
Creating a new repo named name_r.
Definition: PoolImpl.cc:283
filter_iterator< detail::ByRepository, sat::detail::SolvableIterator > SolvableIterator
Definition: Repository.h:40
bool isUpdateRepo() const
if the repository claims to update something then it is an update repository
Definition: Repository.cc:141
sat::detail::size_type size_type
Definition: Repository.h:41
Repository nextInPool() const
Return next Repository in Pool (or noRepository).
Definition: Repository.cc:233
detail::SolvableIdType _addSolvables(::_Repo *repo_r, unsigned count_r)
Adding Solvables to a repo.
Definition: PoolImpl.cc:362
Store and operate on date (time_t).
Definition: Date.h:31
static Pool instance()
Singleton ctor.
Definition: Pool.h:51
SolvableIterator solvablesBegin() const
Iterator to the first Solvable.
Definition: Repository.cc:159
Solvable attribute keys.
Definition: SolvAttr.h:40
void setRepoInfo(RepoIdType id_r, const RepoInfo &info_r)
Also adjust repo priority and subpriority accordingly.
Definition: PoolImpl.cc:368
Lightweight repository attribute value lookup.
Definition: LookupAttr.h:260
std::ostream & operator<<(std::ostream &str, const Exception &obj)
Definition: Exception.cc:120
void setInfo(const RepoInfo &info_r)
Set RepoInfo for this repository.
Definition: Repository.cc:207
Date suggestedExpirationTimestamp() const
Suggested expiration timestamp.
Definition: Repository.cc:88
Query class for Repository related products.
Definition: Repository.h:319
Keywords keywords() const
repository keywords (tags)
Definition: Repository.cc:102
static PoolImpl & myPool()
Definition: PoolImpl.cc:164
::_Repo * get() const
Expert backdoor.
Definition: Repository.cc:40
bool solvablesEmpty() const
Whether Repository contains solvables.
Definition: Repository.cc:147
#define NO_REPOSITORY_THROW(VAL)
Definition: Repository.cc:47
std::string cpeId() const
The Common Platform Enumeration name for this product.
Definition: Repository.cc:348
bool empty() const
Whether the query is empty.
Definition: LookupAttr.cc:242
size_type solvablesSize() const
Number of solvables in Repository.
Definition: Repository.cc:153
::_Repo * getRepo(RepoIdType id_r) const
Definition: PoolImpl.h:143
std::string form(const char *format,...)
Printf style construction of std::string.
Definition: String.cc:34
void addHelix(const Pathname &file_r)
Load Solvables from a helix-file.
Definition: Repository.cc:267
Base class for Exception.
Definition: Exception.h:143
static const Repository noRepository
Represents no Repository.
Definition: Repository.h:57
int satInternalPriority() const
libsolv internal priorities.
Definition: Repository.cc:68
static Date now()
Return the current time.
Definition: Date.h:76
static const std::string & systemRepoAlias()
Reserved system repository alias .
Definition: PoolImpl.cc:79
Reference counted access to a _Tp object calling a custom Dispose function when the last AutoDispose ...
Definition: AutoDispose.h:92
static const SolvAttr repositoryKeywords
Definition: SolvAttr.h:160
void eraseFromPool()
Remove this Repository from it&#39;s Pool.
Definition: Repository.cc:225
static const SolvAttr repositoryProductCpeid
Definition: SolvAttr.h:164
ProductInfoIterator updatesProductEnd() const
Get an iterator to the end of the repository compatible distros.
Definition: Repository.cc:196
static const std::string & systemRepoAlias()
Reserved system repository alias .
Definition: Repository.cc:35
static const SolvAttr repositoryDistros
array of repositoryProductLabel repositoryProductCpeid pairs
Definition: SolvAttr.h:162
static const SolvAttr repositoryUpdates
array of repositoryProductLabel repositoryProductCpeid pairs
Definition: SolvAttr.h:161
const RepoInfo & repoInfo(RepoIdType id_r)
Definition: PoolImpl.h:180
ProductInfoIterator compatibleWithProductEnd() const
Get an iterator to the end of the repository compatible distros.
Definition: Repository.cc:185
ProductInfoIterator updatesProductBegin() const
Get an iterator to the beginning of the repository compatible distros.
Definition: Repository.cc:190
bool maybeOutdated() const
The suggested expiration date of this repository already passed.
Definition: Repository.cc:108
ProductInfoIterator compatibleWithProductBegin() const
Get an iterator to the beginning of the repository compatible distros.
Definition: Repository.cc:179
iterator begin() const
Iterator to the begin of query results.
Definition: LookupAttr.cc:236
Search for repository attributes.
Definition: LookupAttr.h:122