libzypp  17.36.3
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/base/Logger.h>
17 #include <zypp/base/Gettext.h>
18 #include <zypp/base/Exception.h>
19 #include <zypp/base/Xml.h>
20 
21 #include <zypp/AutoDispose.h>
22 #include <zypp/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 using std::endl;
31 
33 namespace zypp
34 {
35 
36  const Repository Repository::noRepository;
37 
38  const std::string & Repository::systemRepoAlias()
40 
42 
44  { return myPool().getRepo( _id ); }
45 
46 #define NO_REPOSITORY_RETURN( VAL ) \
47  sat::detail::CRepo * _repo( get() ); \
48  if ( ! _repo ) return VAL
49 
50 #define NO_REPOSITORY_THROW( VAL ) \
51  sat::detail::CRepo * _repo( get() ); \
52  if ( ! _repo ) ZYPP_THROW( VAL )
53 
55  {
56  NO_REPOSITORY_RETURN( false );
57  return myPool().isSystemRepo( _repo );
58  }
59 
60  std::string Repository::alias() const
61  {
62  NO_REPOSITORY_RETURN( std::string() );
63  if ( ! _repo->name )
64  return std::string();
65  return _repo->name;
66  }
67 
68  std::string Repository::name() const
69  { return info().name(); }
70 
71  std::string Repository::label() const
72  { return info().label(); }
73 
75  {
76  NO_REPOSITORY_RETURN( INT_MIN );
77  return _repo->priority;
78  }
79 
81  {
82  NO_REPOSITORY_RETURN( INT_MIN );
83  return _repo->subpriority;
84  }
85 
87  {
90  return q.empty() ? std::string() : q.begin().asString();
91  }
92 
94  {
97  return q.empty() ? std::string() : q.begin().asString();
98  }
99 
101  {
102  NO_REPOSITORY_RETURN( false );
104  for_( it, q.begin(), q.end() )
105  if ( it.asString() == id_r )
106  return true;
107  return false;
108  }
109 
111  {
114  return( q.empty() ? 0 : q.begin().asUnsigned() );
115  }
116 
118  {
120  Date generated = generatedTimestamp();
121  if ( ! generated )
122  return 0; // do not calculate over a missing generated timestamp
123 
125  if ( q.empty() )
126  return 0;
127 
128  return generated + Date(q.begin().asUnsigned());
129  }
130 
132  {
135  }
136 
137  bool Repository::hasKeyword( const std::string & val_r ) const
138  {
139  for ( const auto & val : keywords() )
140  if ( val == val_r )
141  return true;
142  return false;
143  }
144 
146  {
147  NO_REPOSITORY_RETURN( false );
148  // system repo is not mirrored
149  if ( isSystemRepo() )
150  return false;
151 
152  Date suggested = suggestedExpirationTimestamp();
153 
154  // if no data, don't suggest
155  if ( ! suggested )
156  return false;
157 
159  }
160 
161  bool Repository::providesUpdatesFor( const CpeId & cpeid_r ) const
162  {
163  NO_REPOSITORY_RETURN( false );
164  if ( ! cpeid_r )
165  return false; // filter queries/products without CpeId, as an empty CpeId matches ANYthing.
166 
167  // check in repository metadata
169  {
170  if ( compare( cpeid_r, it.cpeId(), SetRelation::subset ) )
171  return true;
172  }
173 
174  // check whether known products refer to this as update repo
175  sat::LookupRepoAttr myIds( sat::SolvAttr::repositoryRepoid, *this ); // usually just one, but...
176  if ( ! myIds.empty() )
177  {
178  const ResPool & pool( ResPool::instance() );
179  for_( it, pool.byKindBegin<Product>(), pool.byKindEnd<Product>() )
180  {
181  Product::constPtr prod( (*it)->asKind<Product>() );
182  if ( compare( cpeid_r, prod->cpeId(), SetRelation::superset ) )
183  {
184  for_( myId, myIds.begin(), myIds.end() )
185  {
186  if ( prod->hasUpdateContentIdentifier( myId.asString() ) )
187  return true;
188  }
189  }
190  }
191  }
192  return false;
193  }
194 
196  {
197  NO_REPOSITORY_RETURN( false );
198 
199  // check in repository metadata
201  return true;
202 
203  // check whether known products refer to this as update repo
204  sat::LookupRepoAttr myIds( sat::SolvAttr::repositoryRepoid, *this ); // usually just one, but...
205  if ( ! myIds.empty() )
206  {
207  const ResPool & pool( ResPool::instance() );
208  for_( it, pool.byKindBegin<Product>(), pool.byKindEnd<Product>() )
209  {
210  for_( myId, myIds.begin(), myIds.end() )
211  {
212  if ( (*it)->asKind<Product>()->hasUpdateContentIdentifier( myId.asString() ) )
213  return true;
214  }
215  }
216  }
217  return false;
218  }
219 
221  {
222  NO_REPOSITORY_RETURN( true );
223  return !_repo->nsolvables;
224  }
225 
227  {
229  return _repo->nsolvables;
230  }
231 
233  {
234  NO_REPOSITORY_RETURN( make_filter_iterator( detail::ByRepository( *this ),
237  return make_filter_iterator( detail::ByRepository( *this ),
240  }
241 
243  {
244  NO_REPOSITORY_RETURN( make_filter_iterator( detail::ByRepository( *this ),
247  return make_filter_iterator(detail::ByRepository( *this ),
250  }
251 
253  {
256  }
257 
259  {
260  return ProductInfoIterator();
261  }
262 
264  {
267  }
268 
270  {
271  return ProductInfoIterator();
272  }
273 
275  {
277  return myPool().repoInfo( _repo );
278  }
279 
280  void Repository::setInfo( const RepoInfo & info_r )
281  {
282  NO_REPOSITORY_THROW( Exception( "Can't set RepoInfo for norepo." ) );
283  if ( info_r.alias() != alias() )
284  {
285  ZYPP_THROW( Exception( str::form( "RepoInfo alias (%s) does not match repository alias (%s)",
286  info_r.alias().c_str(), alias().c_str() ) ) );
287  }
288  myPool().setRepoInfo( _repo, info_r );
289  MIL << *this << endl;
290  }
291 
293  {
296  }
297 
299  {
301  MIL << *this << " removed from pool" << endl;
302  myPool()._deleteRepo( _repo );
304  }
305 
307  {
309  for_( it, sat::Pool::instance().reposBegin(), sat::Pool::instance().reposEnd() )
310  {
311  if ( *it == *this )
312  {
313  if ( ++it != _for_end )
314  return *it;
315  break;
316  }
317  }
318  return noRepository;
319  }
320 
321  void Repository::addSolv( const Pathname & file_r )
322  {
323  NO_REPOSITORY_THROW( Exception( "Can't add solvables to norepo." ) );
324 
325  AutoDispose<FILE*> file( ::fopen( file_r.c_str(), "re" ), ::fclose );
326  if ( file == NULL )
327  {
328  file.resetDispose();
329  ZYPP_THROW( Exception( "Can't open solv-file: "+file_r.asString() ) );
330  }
331 
332  if ( myPool()._addSolv( _repo, file ) != 0 )
333  {
334  ZYPP_THROW( Exception( "Error reading solv-file: "+file_r.asString() ) );
335  }
336 
337  MIL << *this << " after adding " << file_r << endl;
338  }
339 
340  void Repository::addHelix( const Pathname & file_r )
341  {
342  NO_REPOSITORY_THROW( Exception( "Can't add solvables to norepo." ) );
343 
344  std::string command( file_r.extension() == ".gz" ? "zcat " : "cat " );
345  command += file_r.asString();
346 
347  AutoDispose<FILE*> file( ::popen( command.c_str(), "re" ), ::pclose );
348  if ( file == NULL )
349  {
350  file.resetDispose();
351  ZYPP_THROW( Exception( "Can't open helix-file: "+file_r.asString() ) );
352  }
353 
354  if ( myPool()._addHelix( _repo, file ) != 0 )
355  {
356  ZYPP_THROW( Exception( "Error reading helix-file: "+file_r.asString() ) );
357  }
358 
359  MIL << *this << " after adding " << file_r << endl;
360  }
361 
362  void Repository::addTesttags( const Pathname & file_r )
363  {
364  NO_REPOSITORY_THROW( Exception( "Can't add solvables to norepo." ) );
365 
366  std::string command( file_r.extension() == ".gz" ? "zcat " : "cat " );
367  command += file_r.asString();
368 
369  AutoDispose<FILE*> file( ::popen( command.c_str(), "re" ), ::pclose );
370  if ( file == NULL )
371  {
372  file.resetDispose();
373  ZYPP_THROW( Exception( "Can't open testtags-file: "+file_r.asString() ) );
374  }
375 
376  if ( myPool()._addTesttags( _repo, file ) != 0 )
377  {
378  ZYPP_THROW( Exception( "Error reading testtags-file: "+file_r.asString() ) );
379  }
380 
381  MIL << *this << " after adding " << file_r << endl;
382  }
383 
385  {
386  NO_REPOSITORY_THROW( Exception( "Can't add solvables to norepo.") );
387  return myPool()._addSolvables( _repo, count_r );
388  }
389 
390  /******************************************************************
391  **
392  ** FUNCTION NAME : operator<<
393  ** FUNCTION TYPE : std::ostream &
394  */
395  std::ostream & operator<<( std::ostream & str, const Repository & obj )
396  {
397  if ( ! obj )
398  return str << "noRepository";
399 
400  return str << "sat::repo(" << obj.alias() << ")"
401  << "{"
402  << "prio " << obj.get()->priority << '.' << obj.get()->subpriority
403  << ", size " << obj.solvablesSize()
404  << "}";
405  }
406 
407  std::ostream & dumpAsXmlOn( std::ostream & str, const Repository & obj )
408  {
409  return xmlout::node( str, "repository", {
410  { "name", obj.name() },
411  { "alias", obj.alias() }
412  } );
413  }
414 
416  namespace detail
417  {
419  {
420  if ( base() )
421  {
423  do {
424  ++base_reference();
425  } while ( base() < satpool->repos+satpool->nrepos && !*base() );
426  }
427  }
428  } // namespace detail
430 
432  //
433  // Repository::ProductInfoIterator
434  //
436 
438  { base_reference() = sat::LookupRepoAttr( std::move(attr_r), repo_r ).begin(); }
439 
441  { return base_reference().subFind( sat::SolvAttr::repositoryProductLabel ).asString(); }
442 
444  { return CpeId( base_reference().subFind( sat::SolvAttr::repositoryProductCpeid ).asString(), CpeId::noThrow ); }
445 
447 } // namespace zypp
std::string asString(const Patch::Category &obj)
Definition: Patch.cc:122
#define NO_REPOSITORY_RETURN(VAL)
Definition: Repository.cc:46
int satInternalSubPriority() const
Definition: Repository.cc:80
std::ostream & node(std::ostream &out_r, const std::string &name_r, Node::Attr attr_r)
Definition: Xml.h:204
unsigned priority() const
Repository priority for solver.
Definition: RepoInfo.cc:398
Interface to gettext.
ContentIdentifier contentIdentifier() const
Unique string identifying a repositories content.
Definition: Repository.cc:93
Product interface.
Definition: Product.h:33
#define MIL
Definition: Logger.h:100
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:195
Date suggestedExpirationTimestamp() const
Suggested expiration timestamp.
Definition: Repository.cc:117
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:424
zypp::RepoInfo RepoInfo
Definition: repomanager.h:36
bool isSystemRepo() const
Return whether this is the system repository.
Definition: Repository.cc:54
void addSolv(const Pathname &file_r)
Load Solvables from a solv-file.
Definition: Repository.cc:321
bool providesUpdatesFor(const CpeId &cpeid_r) const
Hint whether the Repo may provide updates for a product identified by its CpeId.
Definition: Repository.cc:161
ProductInfoIterator compatibleWithProductBegin() const
Get an iterator to the beginning of the repository compatible distros.
Definition: Repository.cc:252
::s_Repo CRepo
Wrapped libsolv C data type exposed as backdoor.
Definition: PoolMember.h:63
void _deleteRepo(CRepo *repo_r)
Delete repo repo_r from pool.
Definition: PoolImpl.cc:337
sat::Solvable::IdType addSolvables(unsigned count_r)
Add count_r new empty Solvable to this Repository.
Definition: Repository.cc:384
std::string name() const
Label to display for this repo.
Definition: Repository.cc:68
static const RepoIdType noRepoId(0)
Id to denote Repo::noRepository.
std::string ContentRevision
Definition: Repository.h:48
#define for_(IT, BEG, END)
Convenient for-loops using iterator.
Definition: Easy.h:27
byKind_iterator byKindBegin(const ResKind &kind_r) const
Definition: ResPool.h:262
void clearInfo()
Remove any RepoInfo set for this repository.
Definition: Repository.cc:292
const char * c_str() const
String representation.
Definition: Pathname.h:112
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:258
String related utilities and Regular expression matching.
zypp::RepoInfo _repo
std::ostream & operator<<(std::ostream &str, const SerialNumber &obj)
Definition: SerialNumber.cc:52
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
detail::SolvableIdType _addSolvables(CRepo *repo_r, unsigned count_r)
Adding Solvables to a repo.
Definition: PoolImpl.cc:424
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:145
std::string label() const
Product label.
Definition: Repository.cc:440
filter_iterator< detail::ByRepository, sat::detail::SolvableIterator > SolvableIterator
Definition: Repository.h:42
Iterate over valid Solvables in the pool.
Definition: Solvable.h:525
std::string form(const char *format,...) __attribute__((format(printf
Printf style construction of std::string.
Definition: String.cc:37
CRepo * getRepo(RepoIdType id_r) const
Definition: PoolImpl.h:176
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:362
TraitsType::constPtrType constPtr
Definition: Product.h:39
static const SolvAttr repositoryRevision
Definition: SolvAttr.h:192
bool isSystemRepo(CRepo *repo_r) const
Definition: PoolImpl.h:101
CpeId cpeId() const
The Common Platform Enumeration name for this product.
Definition: Repository.cc:443
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:242
std::ostream & dumpAsXmlOn(std::ostream &str, const Repository &obj)
Definition: Repository.cc:407
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:43
void setRepoInfo(RepoIdType id_r, const RepoInfo &info_r)
Also adjust repo priority and subpriority accordingly.
Definition: PoolImpl.cc:430
Lightweight repository attribute value lookup.
Definition: LookupAttr.h:264
const std::string & asString() const
String representation.
Definition: Pathname.h:93
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:280
RepoInfo info() const
Return any associated RepoInfo.
Definition: Repository.cc:274
SolvableIterator solvablesBegin() const
Iterator to the first Solvable.
Definition: Repository.cc:232
Keywords keywords() const
repository keywords (tags)
Definition: Repository.cc:131
bool hasKeyword(const std::string &val_r) const
Whether val_r is present in keywords.
Definition: Repository.cc:137
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:110
int satInternalPriority() const
libsolv internal priorities.
Definition: Repository.cc:74
static PoolImpl & myPool()
Definition: PoolImpl.cc:184
ProductInfoIterator updatesProductBegin() const
Get an iterator to the beginning of distos the repository provides upadates for.
Definition: Repository.cc:263
#define NO_REPOSITORY_THROW(VAL)
Definition: Repository.cc:50
bool hasContentIdentifier(const ContentIdentifier &id_r) const
Whether id_r matches this repos content identifier.
Definition: Repository.cc:100
sat::ArrayAttr< std::string, std::string > Keywords
Definition: Repository.h:46
void resetDispose()
Set no dispose function.
Definition: AutoDispose.h:171
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:306
void addHelix(const Pathname &file_r)
Load Solvables from a helix-file.
Definition: Repository.cc:340
Global ResObject pool.
Definition: ResPool.h:61
std::string alias() const
Short unique string to identify a repo.
Definition: Repository.cc:60
ContentRevision contentRevision() const
Timestamp or arbitrary user supplied string.
Definition: Repository.cc:86
Base class for Exception.
Definition: Exception.h:146
static const Repository noRepository
Represents no Repository.
Definition: Repository.h:67
ProductInfoIterator updatesProductEnd() const
Get an iterator to the end of distos the repository provides upadates for.
Definition: Repository.cc:269
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: PoolMember.h:61
static const std::string & systemRepoAlias()
Reserved system repository alias .
Definition: PoolImpl.cc:100
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:298
static const SolvAttr repositoryProductCpeid
Definition: SolvAttr.h:189
bool solvablesEmpty() const
Whether Repository contains solvables.
Definition: Repository.cc:220
unsigned int SolvableIdType
Id type to connect Solvable and sat-solvable.
Definition: PoolMember.h:125
static const std::string & systemRepoAlias()
Reserved system repository alias .
Definition: Repository.cc:38
bool hasUpdateContentIdentifier(const Repository::ContentIdentifier &cident_r) const
Whether cident_r is listed as required update repository.
Definition: Product.cc:230
static const SolvAttr repositoryDistros
array of repositoryProductLabel repositoryProductCpeid pairs
Definition: SolvAttr.h:187
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:218
size_type solvablesSize() const
Number of solvables in Repository.
Definition: Repository.cc:226
Easy-to use interface to the ZYPP dependency resolver.
Definition: Application.cc:19
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:71
Search for repository attributes.
Definition: LookupAttr.h:120
static ResPool instance()
Singleton ctor.
Definition: ResPool.cc:38
std::string extension() const
Return all of the characters in name after and including the last dot in the last element of name...
Definition: Pathname.h:137