libzypp 17.38.15
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
19#include <zypp-core/base/Xml.h>
20
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
30extern "C"
31{
32#include <solv/solv_xfopen.h>
33}
34
35using std::endl;
36
38namespace zypp
39{
40
42
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
97
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
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
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
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 {
239 NO_REPOSITORY_RETURN( make_filter_iterator( detail::ByRepository( *this ),
242 return make_filter_iterator( detail::ByRepository( *this ),
243 sat::detail::SolvableIterator(_repo->start),
244 sat::detail::SolvableIterator(_repo->end) );
245 }
246
248 {
249 NO_REPOSITORY_RETURN( make_filter_iterator( detail::ByRepository( *this ),
252 return make_filter_iterator(detail::ByRepository( *this ),
254 sat::detail::SolvableIterator(_repo->end) );
255 }
256
262
267
273
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
444
446} // namespace zypp
#define for_(IT, BEG, END)
Convenient for-loops using iterator.
Definition Easy.h:27
#define ZYPP_THROW(EXCPT)
Drops a logline and throws the Exception.
Definition Exception.h:459
#define MIL
Definition Logger.h:130
#define NO_REPOSITORY_THROW(VAL)
Definition Repository.cc:55
#define NO_REPOSITORY_RETURN(VAL)
Definition Repository.cc:51
Reference counted access to a Tp object calling a custom Dispose function when the last AutoDispose h...
Definition AutoDispose.h:95
void resetDispose()
Set no dispose function.
Common Platform Enumearation (2.3) See http://cpe.mitre.org/ for more information on the Common Platf...
Definition CpeId.h:33
static constexpr NoThrowType noThrow
Indicator argument for non-trowing ctor.
Definition CpeId.h:63
CpeId()
Default ctor: ANY-Cpeid, all attribute values are ANY.
Definition CpeId.cc:375
Store and operate on date (time_t).
Definition Date.h:33
Date()
Default ctor: 0.
Definition Date.h:57
static Date now()
Return the current time.
Definition Date.h:78
Exception()
Default ctor.
Definition Exception.cc:94
Product interface.
Definition Product.h:34
bool hasUpdateContentIdentifier(const Repository::ContentIdentifier &cident_r) const
Whether cident_r is listed as required update repository.
Definition Product.cc:248
TraitsType::constPtrType constPtr
Definition Product.h:39
What is known about a repository.
Definition RepoInfo.h:72
Query class for Repository related products.
Definition Repository.h:383
std::string label() const
Product label.
CpeId cpeId() const
The Common Platform Enumeration name for this product.
Repository nextInPool() const
Return next Repository in Pool (or noRepository).
int satInternalSubPriority() const
Definition Repository.cc:85
static const Repository noRepository
Represents no Repository.
Definition Repository.h:67
void addHelix(const Pathname &file_r)
Load Solvables from a helix-file.
Repository()
Default ctor creates noRepository.
Definition Repository.h:53
bool hasKeyword(const std::string &val_r) const
Whether val_r is present in keywords.
bool isUpdateRepo() const
Hint whether the Repo may provide updates for a product.
Keywords keywords() const
repository keywords (tags)
sat::detail::CRepo * get() const
Expert backdoor.
Definition Repository.cc:48
bool solvablesEmpty() const
Whether Repository contains solvables.
std::string label() const
Alias or name, according to ZConfig::repoLabelIsAlias.
Definition Repository.cc:76
Date suggestedExpirationTimestamp() const
Suggested expiration timestamp.
filter_iterator< detail::ByRepository, sat::detail::SolvableIterator > SolvableIterator
Definition Repository.h:42
SolvableIterator solvablesEnd() const
Iterator behind the last Solvable.
ProductInfoIterator compatibleWithProductEnd() const
Get an iterator to the end of the repository compatible distros.
int satInternalPriority() const
libsolv internal priorities.
Definition Repository.cc:79
void clearInfo()
Remove any RepoInfo set for this repository.
sat::detail::size_type size_type
Definition Repository.h:43
sat::Solvable::IdType addSolvables(unsigned count_r)
Add count_r new empty Solvable to this Repository.
bool providesUpdatesFor(const CpeId &cpeid_r) const
Hint whether the Repo may provide updates for a product identified by its CpeId.
SolvableIterator solvablesBegin() const
Iterator to the first Solvable.
std::string ContentRevision
Definition Repository.h:48
void addTesttags(const Pathname &file_r)
Load Solvables from a libsolv testtags-file.
ProductInfoIterator updatesProductEnd() const
Get an iterator to the end of distos the repository provides upadates for.
ContentIdentifier contentIdentifier() const
Unique string identifying a repositories content.
Definition Repository.cc:98
bool maybeOutdated() const
The suggested expiration date of this repository already passed.
ProductInfoIterator compatibleWithProductBegin() const
Get an iterator to the beginning of the repository compatible distros.
std::string alias() const
Short unique string to identify a repo.
Definition Repository.cc:65
bool hasContentIdentifier(const ContentIdentifier &id_r) const
Whether id_r matches this repos content identifier.
size_type solvablesSize() const
Number of solvables in Repository.
void setInfo(const RepoInfo &info_r)
Set RepoInfo for this repository.
std::string name() const
Label to display for this repo.
Definition Repository.cc:73
void addSolv(const Pathname &file_r)
Load Solvables from a solv-file.
ContentRevision contentRevision() const
Timestamp or arbitrary user supplied string.
Definition Repository.cc:91
sat::ArrayAttr< std::string, std::string > Keywords
Definition Repository.h:46
Date generatedTimestamp() const
Timestamp when this repository was generated.
ProductInfoIterator updatesProductBegin() const
Get an iterator to the beginning of distos the repository provides upadates for.
std::string ContentIdentifier
Definition Repository.h:49
static const std::string & systemRepoAlias()
Reserved system repository alias @System .
Definition Repository.cc:43
RepoInfo info() const
Return any associated RepoInfo.
void eraseFromPool()
Remove this Repository from its Pool.
bool isSystemRepo() const
Return whether this is the system repository.
Definition Repository.cc:59
Global ResObject pool.
Definition ResPool.h:62
static ResPool instance()
Singleton ctor.
Definition ResPool.cc:38
const char * c_str() const
String representation.
Definition Pathname.h:113
const std::string & asString() const
String representation.
Definition Pathname.h:94
std::string label() const
Label for use in messages for the user interface.
std::string name() const
Repository name.
std::string alias() const
unique identifier for this source.
unsigned asUnsigned() const
This is an overloaded member function, provided for convenience. It differs from the above function o...
std::string asString() const
This is an overloaded member function, provided for convenience. It differs from the above function o...
iterator subFind(const SolvAttr &attr_r) const
Iterator pointing to the first occurance of SolvAttr attr_r in sub-structure.
iterator end() const
Iterator behind the end of query results.
bool empty() const
Whether the query is empty.
@ REPO_ATTR
Search for repository attributes.
Definition LookupAttr.h:120
iterator begin() const
Iterator to the begin of query results.
Lightweight repository attribute value lookup.
Definition LookupAttr.h:265
static Pool instance()
Singleton ctor.
Definition Pool.h:55
detail::CPool * get() const
Expert backdoor.
Definition Pool.cc:49
Solvable attribute keys.
Definition SolvAttr.h:41
static const SolvAttr repositoryUpdates
array of repositoryProductLabel repositoryProductCpeid pairs
Definition SolvAttr.h:186
static const SolvAttr repositoryRepoid
Definition SolvAttr.h:190
static const SolvAttr repositoryRevision
Definition SolvAttr.h:192
static const SolvAttr repositoryProductLabel
Definition SolvAttr.h:188
static const SolvAttr repositoryTimestamp
Definition SolvAttr.h:184
static const SolvAttr repositoryExpire
Definition SolvAttr.h:185
static const SolvAttr repositoryProductCpeid
Definition SolvAttr.h:189
static const SolvAttr repositoryDistros
array of repositoryProductLabel repositoryProductCpeid pairs
Definition SolvAttr.h:187
static const SolvAttr repositoryKeywords
Definition SolvAttr.h:191
bool isSystemRepo(CRepo *repo_r) const
Definition PoolImpl.h:105
CRepo * getRepo(RepoIdType id_r) const
Definition PoolImpl.h:180
const RepoInfo & repoInfo(RepoIdType id_r)
Definition PoolImpl.h:223
void _deleteRepo(CRepo *repo_r)
Delete repo repo_r from pool.
Definition PoolImpl.cc:323
detail::SolvableIdType _addSolvables(CRepo *repo_r, unsigned count_r)
Adding Solvables to a repo.
Definition PoolImpl.cc:410
static const std::string & systemRepoAlias()
Reserved system repository alias @System .
Definition PoolImpl.cc:102
void setRepoInfo(RepoIdType id_r, const RepoInfo &info_r)
Also adjust repo priority and subpriority accordingly.
Definition PoolImpl.cc:416
Iterate over valid Solvables in the pool.
Definition Solvable.h:552
String related utilities and Regular expression matching.
unsigned int SolvableIdType
Id type to connect Solvable and sat-solvable.
Definition PoolDefines.h:65
::s_Repo CRepo
Wrapped libsolv C data type exposed as backdoor.
Definition PoolDefines.h:38
::s_Pool CPool
Wrapped libsolv C data type exposed as backdoor.
Definition PoolDefines.h:36
std::string form(const char *format,...) __attribute__((format(printf
Printf style construction of std::string.
Definition String.cc:39
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
Easy-to use interface to the ZYPP dependency resolver.
std::ostream & dumpAsXmlOn(std::ostream &str, const Repository &obj)
relates: Repository XML output
std::string asString(const Patch::Category &obj)
relates: Patch::Category string representation.
Definition Patch.cc:122
std::ostream & operator<<(std::ostream &str, const Capabilities &obj)
relates: Capabilities Stream output
Functor filtering Solvable by Repository.
Definition Repository.h:490
static PoolImpl & myPool()
Definition PoolMember.cc:41