libzypp  17.38.17
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 #include <utility>
15 
16 #include <zypp-core/base/Logger.h>
17 #include <zypp-core/base/Gettext.h>
19 #include <zypp-core/base/Xml.h>
20 
21 #include <zypp-core/AutoDispose.h>
22 #include <zypp-core/Pathname.h>
23 
25 #include <zypp/Repository.h>
26 #include <zypp/ResPool.h>
27 #include <zypp/Product.h>
28 #include <zypp/sat/Pool.h>
29 
30 extern "C"
31 {
32 #include <solv/solv_xfopen.h>
33 }
34 
35 using std::endl;
36 
38 namespace zypp
39 {
40 
41  const Repository Repository::noRepository;
42 
43  const std::string & Repository::systemRepoAlias()
45 
47 
49  { return myPool().getRepo( _id ); }
50 
51 #define NO_REPOSITORY_RETURN( VAL ) \
52  sat::detail::CRepo * _repo( get() ); \
53  if ( ! _repo ) return VAL
54 
55 #define NO_REPOSITORY_THROW( VAL ) \
56  sat::detail::CRepo * _repo( get() ); \
57  if ( ! _repo ) ZYPP_THROW( VAL )
58 
60  {
61  NO_REPOSITORY_RETURN( false );
62  return myPool().isSystemRepo( _repo );
63  }
64 
65  std::string Repository::alias() const
66  {
67  NO_REPOSITORY_RETURN( std::string() );
68  if ( ! _repo->name )
69  return std::string();
70  return _repo->name;
71  }
72 
73  std::string Repository::name() const
74  { return info().name(); }
75 
76  std::string Repository::label() const
77  { return info().label(); }
78 
80  {
81  NO_REPOSITORY_RETURN( INT_MIN );
82  return _repo->priority;
83  }
84 
86  {
87  NO_REPOSITORY_RETURN( INT_MIN );
88  return _repo->subpriority;
89  }
90 
92  {
95  return q.empty() ? std::string() : q.begin().asString();
96  }
97 
99  {
102  return q.empty() ? std::string() : q.begin().asString();
103  }
104 
106  {
107  NO_REPOSITORY_RETURN( false );
109  for_( it, q.begin(), q.end() )
110  if ( it.asString() == id_r )
111  return true;
112  return false;
113  }
114 
116  {
119  return( q.empty() ? 0 : q.begin().asUnsigned() );
120  }
121 
123  {
125  Date generated = generatedTimestamp();
126  if ( ! generated )
127  return 0; // do not calculate over a missing generated timestamp
128 
130  if ( q.empty() )
131  return 0;
132 
133  return generated + Date(q.begin().asUnsigned());
134  }
135 
137  {
140  }
141 
142  bool Repository::hasKeyword( const std::string & val_r ) const
143  {
144  for ( const auto & val : keywords() )
145  if ( val == val_r )
146  return true;
147  return false;
148  }
149 
151  {
152  NO_REPOSITORY_RETURN( false );
153  // system repo is not mirrored
154  if ( isSystemRepo() )
155  return false;
156 
157  Date suggested = suggestedExpirationTimestamp();
158 
159  // if no data, don't suggest
160  if ( ! suggested )
161  return false;
162 
164  }
165 
166  bool Repository::providesUpdatesFor( const CpeId & cpeid_r ) const
167  {
168  NO_REPOSITORY_RETURN( false );
169  if ( ! cpeid_r )
170  return false; // filter queries/products without CpeId, as an empty CpeId matches ANYthing.
171 
172  // check in repository metadata
174  {
175  if ( compare( cpeid_r, it.cpeId(), SetRelation::subset ) )
176  return true;
177  }
178 
179  // check whether known products refer to this as update repo
180  sat::LookupRepoAttr myIds( sat::SolvAttr::repositoryRepoid, *this ); // usually just one, but...
181  if ( ! myIds.empty() )
182  {
183  const ResPool & pool( ResPool::instance() );
184  for_( it, pool.byKindBegin<Product>(), pool.byKindEnd<Product>() )
185  {
186  Product::constPtr prod( (*it)->asKind<Product>() );
187  if ( compare( cpeid_r, prod->cpeId(), SetRelation::superset ) )
188  {
189  for_( myId, myIds.begin(), myIds.end() )
190  {
191  if ( prod->hasUpdateContentIdentifier( myId.asString() ) )
192  return true;
193  }
194  }
195  }
196  }
197  return false;
198  }
199 
201  {
202  NO_REPOSITORY_RETURN( false );
203 
204  // check in repository metadata
206  return true;
207 
208  // check whether known products refer to this as update repo
209  sat::LookupRepoAttr myIds( sat::SolvAttr::repositoryRepoid, *this ); // usually just one, but...
210  if ( ! myIds.empty() )
211  {
212  const ResPool & pool( ResPool::instance() );
213  for_( it, pool.byKindBegin<Product>(), pool.byKindEnd<Product>() )
214  {
215  for_( myId, myIds.begin(), myIds.end() )
216  {
217  if ( (*it)->asKind<Product>()->hasUpdateContentIdentifier( myId.asString() ) )
218  return true;
219  }
220  }
221  }
222  return false;
223  }
224 
226  {
227  NO_REPOSITORY_RETURN( true );
228  return !_repo->nsolvables;
229  }
230 
232  {
234  return _repo->nsolvables;
235  }
236 
238  {
243  sat::detail::SolvableIterator(_repo->start),
244  sat::detail::SolvableIterator(_repo->end) );
245  }
246 
248  {
253  sat::detail::SolvableIterator(_repo->end),
254  sat::detail::SolvableIterator(_repo->end) );
255  }
256 
258  {
261  }
262 
264  {
265  return ProductInfoIterator();
266  }
267 
269  {
272  }
273 
275  {
276  return ProductInfoIterator();
277  }
278 
280  {
282  return myPool().repoInfo( _repo );
283  }
284 
285  void Repository::setInfo( const RepoInfo & info_r )
286  {
287  NO_REPOSITORY_THROW( Exception( "Can't set RepoInfo for norepo." ) );
288  if ( info_r.alias() != alias() )
289  {
290  ZYPP_THROW( Exception( str::form( "RepoInfo alias (%s) does not match repository alias (%s)",
291  info_r.alias().c_str(), alias().c_str() ) ) );
292  }
293  myPool().setRepoInfo( _repo, info_r );
294  MIL << *this << endl;
295  }
296 
298  {
300  myPool().setRepoInfo( _repo, RepoInfo() );
301  }
302 
304  {
306  MIL << *this << " removed from pool" << endl;
307  myPool()._deleteRepo( _repo );
308  _id = sat::detail::noRepoId;
309  }
310 
312  {
314  for_( it, sat::Pool::instance().reposBegin(), sat::Pool::instance().reposEnd() )
315  {
316  if ( *it == *this )
317  {
318  if ( ++it != _for_end )
319  return *it;
320  break;
321  }
322  }
323  return noRepository;
324  }
325 
326  void Repository::addSolv( const Pathname & file_r )
327  {
328  NO_REPOSITORY_THROW( Exception( "Can't add solvables to norepo." ) );
329 
330  AutoDispose<FILE*> file( ::fopen( file_r.c_str(), "re" ), ::fclose );
331  if ( file == NULL )
332  {
333  file.resetDispose();
334  ZYPP_THROW( Exception( "Can't open solv-file: "+file_r.asString() ) );
335  }
336 
337  if ( myPool()._addSolv( _repo, file ) != 0 )
338  {
339  ZYPP_THROW( Exception( "Error reading solv-file: "+file_r.asString() ) );
340  }
341 
342  MIL << *this << " after adding " << file_r << endl;
343  }
344 
345  void Repository::addHelix( const Pathname & file_r )
346  {
347  NO_REPOSITORY_THROW( Exception( "Can't add solvables to norepo." ) );
348 
349  AutoDispose<FILE*> file( ::solv_xfopen( file_r.c_str(), "r" ), ::fclose );
350  if ( file == NULL )
351  {
352  file.resetDispose();
353  ZYPP_THROW( Exception( "Can't open helix-file: "+file_r.asString() ) );
354  }
355 
356  if ( myPool()._addHelix( _repo, file ) != 0 )
357  {
358  ZYPP_THROW( Exception( "Error reading helix-file: "+file_r.asString() ) );
359  }
360 
361  MIL << *this << " after adding " << file_r << endl;
362  }
363 
364  void Repository::addTesttags( const Pathname & file_r )
365  {
366  NO_REPOSITORY_THROW( Exception( "Can't add solvables to norepo." ) );
367 
368  AutoDispose<FILE*> file( ::solv_xfopen( file_r.c_str(), "r" ), ::fclose );
369  if ( file == NULL )
370  {
371  file.resetDispose();
372  ZYPP_THROW( Exception( "Can't open testtags-file: "+file_r.asString() ) );
373  }
374 
375  if ( myPool()._addTesttags( _repo, file ) != 0 )
376  {
377  ZYPP_THROW( Exception( "Error reading testtags-file: "+file_r.asString() ) );
378  }
379 
380  MIL << *this << " after adding " << file_r << endl;
381  }
382 
384  {
385  NO_REPOSITORY_THROW( Exception( "Can't add solvables to norepo.") );
386  return myPool()._addSolvables( _repo, count_r );
387  }
388 
389  /******************************************************************
390  **
391  ** FUNCTION NAME : operator<<
392  ** FUNCTION TYPE : std::ostream &
393  */
394  std::ostream & operator<<( std::ostream & str, const Repository & obj )
395  {
396  if ( ! obj )
397  return str << "noRepository";
398 
399  return str << "sat::repo(" << obj.alias() << ")"
400  << "{"
401  << "prio " << obj.get()->priority << '.' << obj.get()->subpriority
402  << ", size " << obj.solvablesSize()
403  << "}";
404  }
405 
406  std::ostream & dumpAsXmlOn( std::ostream & str, const Repository & obj )
407  {
408  return xmlout::node( str, "repository", {
409  { "name", obj.name() },
410  { "alias", obj.alias() }
411  } );
412  }
413 
415  namespace detail
416  {
418  {
419  if ( base() )
420  {
422  do {
423  ++base_reference();
424  } while ( base() < satpool->repos+satpool->nrepos && !*base() );
425  }
426  }
427  } // namespace detail
429 
431  //
432  // Repository::ProductInfoIterator
433  //
435 
437  { base_reference() = sat::LookupRepoAttr( std::move(attr_r), repo_r ).begin(); }
438 
440  { return base_reference().subFind( sat::SolvAttr::repositoryProductLabel ).asString(); }
441 
443  { return CpeId( base_reference().subFind( sat::SolvAttr::repositoryProductCpeid ).asString(), CpeId::noThrow ); }
444 
446 } // namespace zypp
#define NO_REPOSITORY_RETURN(VAL)
Definition: Repository.cc:51
int satInternalSubPriority() const
Definition: Repository.cc:85
ContentIdentifier contentIdentifier() const
Unique string identifying a repositories content.
Definition: Repository.cc:98
Product interface.
Definition: Product.h:33
#define MIL
Definition: Logger.h:130
Functor filtering Solvable by Repository.
Definition: Repository.h:489
bool isUpdateRepo() const
Hint whether the Repo may provide updates for a product.
Definition: Repository.cc:200
Date suggestedExpirationTimestamp() const
Suggested expiration timestamp.
Definition: Repository.cc:122
static const SolvAttr repositoryExpire
Definition: SolvAttr.h:185
static const SolvAttr repositoryProductLabel
Definition: SolvAttr.h:188
#define ZYPP_THROW(EXCPT)
Drops a logline and throws the Exception.
Definition: Exception.h:459
zypp::RepoInfo RepoInfo
Definition: repomanager.h:38
bool isSystemRepo() const
Return whether this is the system repository.
Definition: Repository.cc:59
void addSolv(const Pathname &file_r)
Load Solvables from a solv-file.
Definition: Repository.cc:326
std::ostream & node(std::ostream &out_r, const std::string &name_r, const std::initializer_list< Node::Attr > &attrs_r={})
relates: Node Write a leaf node without PCDATA
Definition: Xml.h:198
bool providesUpdatesFor(const CpeId &cpeid_r) const
Hint whether the Repo may provide updates for a product identified by its CpeId.
Definition: Repository.cc:166
std::string asString(const Patch::Category &obj)
relates: Patch::Category string representation.
Definition: Patch.cc:122
ProductInfoIterator compatibleWithProductBegin() const
Get an iterator to the beginning of the repository compatible distros.
Definition: Repository.cc:257
::s_Repo CRepo
Wrapped libsolv C data type exposed as backdoor.
Definition: PoolDefines.h:38
void _deleteRepo(CRepo *repo_r)
Delete repo repo_r from pool.
Definition: PoolImpl.cc:323
sat::Solvable::IdType addSolvables(unsigned count_r)
Add count_r new empty Solvable to this Repository.
Definition: Repository.cc:383
std::string name() const
Label to display for this repo.
Definition: Repository.cc:73
std::string ContentRevision
Definition: Repository.h:48
byKind_iterator byKindBegin(const ResKind &kind_r) const
Definition: ResPool.h:262
void clearInfo()
Remove any RepoInfo set for this repository.
Definition: Repository.cc:297
const char * c_str() const
String representation.
Definition: Pathname.h:113
static const SolvAttr repositoryTimestamp
Definition: SolvAttr.h:184
ProductInfoIterator compatibleWithProductEnd() const
Get an iterator to the end of the repository compatible distros.
Definition: Repository.cc:263
String related utilities and Regular expression matching.
What is known about a repository.
Definition: RepoInfo.h:71
Common Platform Enumearation (2.3) See http://cpe.mitre.org/ for more information on the Common Platf...
Definition: CpeId.h:32
#define for_(IT, BEG, END)
Convenient for-loops using iterator.
Definition: Easy.h:27
detail::SolvableIdType _addSolvables(CRepo *repo_r, unsigned count_r)
Adding Solvables to a repo.
Definition: PoolImpl.cc:410
LookupAttr::TransformIterator based container to retrieve list attributes.
Definition: LookupAttr.h:599
bool maybeOutdated() const
The suggested expiration date of this repository already passed.
Definition: Repository.cc:150
std::string label() const
Product label.
Definition: Repository.cc:439
filter_iterator< detail::ByRepository, sat::detail::SolvableIterator > SolvableIterator
Definition: Repository.h:42
Iterate over valid Solvables in the pool.
Definition: Solvable.h:545
std::string form(const char *format,...) __attribute__((format(printf
Printf style construction of std::string.
Definition: String.cc:39
CRepo * getRepo(RepoIdType id_r) const
Definition: PoolImpl.h:180
sat::detail::size_type size_type
Definition: Repository.h:43
void addTesttags(const Pathname &file_r)
Load Solvables from a libsolv testtags-file.
Definition: Repository.cc:364
TraitsType::constPtrType constPtr
Definition: Product.h:39
static const SolvAttr repositoryRevision
Definition: SolvAttr.h:192
bool isSystemRepo(CRepo *repo_r) const
Definition: PoolImpl.h:105
CpeId cpeId() const
The Common Platform Enumeration name for this product.
Definition: Repository.cc:442
iterator end() const
Iterator behind the end of query results.
Definition: LookupAttr.cc:240
Store and operate on date (time_t).
Definition: Date.h:32
SolvableIterator solvablesEnd() const
Iterator behind the last Solvable.
Definition: Repository.cc:247
static Pool instance()
Singleton ctor.
Definition: Pool.h:55
Solvable attribute keys.
Definition: SolvAttr.h:40
sat::detail::CRepo * get() const
Expert backdoor.
Definition: Repository.cc:48
void setRepoInfo(RepoIdType id_r, const RepoInfo &info_r)
Also adjust repo priority and subpriority accordingly.
Definition: PoolImpl.cc:416
Lightweight repository attribute value lookup.
Definition: LookupAttr.h:264
SetCompare compare(const SetRelationMixin< Derived > &src, const SetRelationMixin< Derived > &trg)
relates: SetRelationMixin Compare sets
const std::string & asString() const
String representation.
Definition: Pathname.h:94
static constexpr NoThrowType noThrow
Indicator argument for non-trowing ctor.
Definition: CpeId.h:63
std::string alias() const
unique identifier for this source.
void setInfo(const RepoInfo &info_r)
Set RepoInfo for this repository.
Definition: Repository.cc:285
RepoInfo info() const
Return any associated RepoInfo.
Definition: Repository.cc:279
SolvableIterator solvablesBegin() const
Iterator to the first Solvable.
Definition: Repository.cc:237
Keywords keywords() const
repository keywords (tags)
Definition: Repository.cc:136
bool hasKeyword(const std::string &val_r) const
Whether val_r is present in keywords.
Definition: Repository.cc:142
detail::CPool * get() const
Expert backdoor.
Definition: Pool.cc:49
Query class for Repository related products.
Definition: Repository.h:376
std::string ContentIdentifier
Definition: Repository.h:49
Date generatedTimestamp() const
Timestamp when this repository was generated.
Definition: Repository.cc:115
int satInternalPriority() const
libsolv internal priorities.
Definition: Repository.cc:79
static PoolImpl & myPool()
Definition: PoolMember.cc:41
ProductInfoIterator updatesProductBegin() const
Get an iterator to the beginning of distos the repository provides upadates for.
Definition: Repository.cc:268
#define NO_REPOSITORY_THROW(VAL)
Definition: Repository.cc:55
bool hasContentIdentifier(const ContentIdentifier &id_r) const
Whether id_r matches this repos content identifier.
Definition: Repository.cc:105
sat::ArrayAttr< std::string, std::string > Keywords
Definition: Repository.h:46
void resetDispose()
Set no dispose function.
Definition: AutoDispose.h:171
std::ostream & dumpAsXmlOn(std::ostream &str, const Repository &obj)
relates: Repository XML output
Definition: Repository.cc:406
byKind_iterator byKindEnd(const ResKind &kind_r) const
Definition: ResPool.h:269
Repository nextInPool() const
Return next Repository in Pool (or noRepository).
Definition: Repository.cc:311
void addHelix(const Pathname &file_r)
Load Solvables from a helix-file.
Definition: Repository.cc:345
Global ResObject pool.
Definition: ResPool.h:61
std::string alias() const
Short unique string to identify a repo.
Definition: Repository.cc:65
ContentRevision contentRevision() const
Timestamp or arbitrary user supplied string.
Definition: Repository.cc:91
Base class for Exception.
Definition: Exception.h:152
static const Repository noRepository
Represents no Repository.
Definition: Repository.h:67
FilterIterator< Pred, Base > make_filter_iterator(Pred p, Base it, Base end)
Helper function to deduce types and construct a FilterIterator.
Definition: iterators.h:116
ProductInfoIterator updatesProductEnd() const
Get an iterator to the end of distos the repository provides upadates for.
Definition: Repository.cc:274
static Date now()
Return the current time.
Definition: Date.h:78
static const SolvAttr repositoryRepoid
Definition: SolvAttr.h:190
std::string name() const
Repository name.
::s_Pool CPool
Wrapped libsolv C data type exposed as backdoor.
Definition: PoolDefines.h:36
static const std::string & systemRepoAlias()
Reserved system repository alias .
Definition: PoolImpl.cc:102
Reference counted access to a Tp object calling a custom Dispose function when the last AutoDispose h...
Definition: AutoDispose.h:94
static const SolvAttr repositoryKeywords
Definition: SolvAttr.h:191
void eraseFromPool()
Remove this Repository from its Pool.
Definition: Repository.cc:303
static const SolvAttr repositoryProductCpeid
Definition: SolvAttr.h:189
bool solvablesEmpty() const
Whether Repository contains solvables.
Definition: Repository.cc:225
unsigned int SolvableIdType
Id type to connect Solvable and sat-solvable.
Definition: PoolDefines.h:65
static const std::string & systemRepoAlias()
Reserved system repository alias .
Definition: Repository.cc:43
bool hasUpdateContentIdentifier(const Repository::ContentIdentifier &cident_r) const
Whether cident_r is listed as required update repository.
Definition: Product.cc:248
static const SolvAttr repositoryDistros
array of repositoryProductLabel repositoryProductCpeid pairs
Definition: SolvAttr.h:187
std::ostream & operator<<(std::ostream &str, const Capabilities &obj)
relates: Capabilities Stream output
Definition: Capabilities.cc:65
static const SolvAttr repositoryUpdates
array of repositoryProductLabel repositoryProductCpeid pairs
Definition: SolvAttr.h:186
std::string asString() const
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition: LookupAttr.cc:611
const RepoInfo & repoInfo(RepoIdType id_r)
Definition: PoolImpl.h:223
size_type solvablesSize() const
Number of solvables in Repository.
Definition: Repository.cc:231
Easy-to use interface to the ZYPP dependency resolver.
Definition: CodePitfalls.doc:1
std::string label() const
Label for use in messages for the user interface.
bool empty() const
Whether the query is empty.
Definition: LookupAttr.cc:243
iterator begin() const
Iterator to the begin of query results.
Definition: LookupAttr.cc:237
std::string label() const
Alias or name, according to ZConfig::repoLabelIsAlias.
Definition: Repository.cc:76
Search for repository attributes.
Definition: LookupAttr.h:120
static ResPool instance()
Singleton ctor.
Definition: ResPool.cc:38