libzypp 17.37.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/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
30using std::endl;
31
33namespace zypp
34{
35
37
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
92
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
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
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
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 ),
238 sat::detail::SolvableIterator(_repo->start),
239 sat::detail::SolvableIterator(_repo->end) );
240 }
241
243 {
244 NO_REPOSITORY_RETURN( make_filter_iterator( detail::ByRepository( *this ),
247 return make_filter_iterator(detail::ByRepository( *this ),
249 sat::detail::SolvableIterator(_repo->end) );
250 }
251
257
262
268
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 {
295 myPool().setRepoInfo( _repo, RepoInfo() );
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 += "'";
368 command += file_r.asString();
369 command += "'";
370
371 AutoDispose<FILE*> file( ::popen( command.c_str(), "re" ), ::pclose );
372 if ( file == NULL )
373 {
374 file.resetDispose();
375 ZYPP_THROW( Exception( "Can't open testtags-file: "+file_r.asString() ) );
376 }
377
378 if ( myPool()._addTesttags( _repo, file ) != 0 )
379 {
380 ZYPP_THROW( Exception( "Error reading testtags-file: "+file_r.asString() ) );
381 }
382
383 MIL << *this << " after adding " << file_r << endl;
384 }
385
387 {
388 NO_REPOSITORY_THROW( Exception( "Can't add solvables to norepo.") );
389 return myPool()._addSolvables( _repo, count_r );
390 }
391
392 /******************************************************************
393 **
394 ** FUNCTION NAME : operator<<
395 ** FUNCTION TYPE : std::ostream &
396 */
397 std::ostream & operator<<( std::ostream & str, const Repository & obj )
398 {
399 if ( ! obj )
400 return str << "noRepository";
401
402 return str << "sat::repo(" << obj.alias() << ")"
403 << "{"
404 << "prio " << obj.get()->priority << '.' << obj.get()->subpriority
405 << ", size " << obj.solvablesSize()
406 << "}";
407 }
408
409 std::ostream & dumpAsXmlOn( std::ostream & str, const Repository & obj )
410 {
411 return xmlout::node( str, "repository", {
412 { "name", obj.name() },
413 { "alias", obj.alias() }
414 } );
415 }
416
418 namespace detail
419 {
421 {
422 if ( base() )
423 {
425 do {
426 ++base_reference();
427 } while ( base() < satpool->repos+satpool->nrepos && !*base() );
428 }
429 }
430 } // namespace detail
432
434 //
435 // Repository::ProductInfoIterator
436 //
438
440 { base_reference() = sat::LookupRepoAttr( std::move(attr_r), repo_r ).begin(); }
441
443 { return base_reference().subFind( sat::SolvAttr::repositoryProductLabel ).asString(); }
444
447
449} // namespace zypp
#define NO_REPOSITORY_THROW(VAL)
Definition Repository.cc:50
#define NO_REPOSITORY_RETURN(VAL)
Definition Repository.cc:46
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
Store and operate on date (time_t).
Definition Date.h:33
static Date now()
Return the current time.
Definition Date.h:78
Base class for Exception.
Definition Exception.h:153
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:230
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:80
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:43
bool solvablesEmpty() const
Whether Repository contains solvables.
std::string label() const
Alias or name, according to ZConfig::repoLabelIsAlias.
Definition Repository.cc:71
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:74
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:93
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:60
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:68
void addSolv(const Pathname &file_r)
Load Solvables from a solv-file.
ContentRevision contentRevision() const
Timestamp or arbitrary user supplied string.
Definition Repository.cc:86
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:38
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:54
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:112
const std::string & asString() const
String representation.
Definition Pathname.h:93
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
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:101
CRepo * getRepo(RepoIdType id_r) const
Definition PoolImpl.h:176
const RepoInfo & repoInfo(RepoIdType id_r)
Definition PoolImpl.h:218
void _deleteRepo(CRepo *repo_r)
Delete repo repo_r from pool.
Definition PoolImpl.cc:338
detail::SolvableIdType _addSolvables(CRepo *repo_r, unsigned count_r)
Adding Solvables to a repo.
Definition PoolImpl.cc:425
static const std::string & systemRepoAlias()
Reserved system repository alias @System .
Definition PoolImpl.cc:101
void setRepoInfo(RepoIdType id_r, const RepoInfo &info_r)
Also adjust repo priority and subpriority accordingly.
Definition PoolImpl.cc:431
Iterate over valid Solvables in the pool.
Definition Solvable.h:533
String related utilities and Regular expression matching.
unsigned int SolvableIdType
Id type to connect Solvable and sat-solvable.
Definition PoolMember.h:125
::s_Repo CRepo
Wrapped libsolv C data type exposed as backdoor.
Definition PoolMember.h:63
static const RepoIdType noRepoId(0)
Id to denote Repo::noRepository.
::s_Pool CPool
Wrapped libsolv C data type exposed as backdoor.
Definition PoolMember.h:61
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, Node::Attr attr_r)
Definition Xml.h:204
Easy-to use interface to the ZYPP dependency resolver.
std::ostream & operator<<(std::ostream &str, const SerialNumber &obj)
std::ostream & dumpAsXmlOn(std::ostream &str, const Repository &obj)
std::string asString(const Patch::Category &obj)
Definition Patch.cc:122
Functor filtering Solvable by Repository.
Definition Repository.h:490
static PoolImpl & myPool()
Definition PoolImpl.cc:185
#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:100
Interface to gettext.