libzypp 17.35.14
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
36 const Repository Repository::noRepository;
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
241
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
297
299 {
301 MIL << *this << " removed from pool" << endl;
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 {
418 void RepositoryIterator::increment()
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
445
447} // 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:147
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
unsigned priority() const
Repository priority for solver.
Definition RepoInfo.cc:398
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.
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
byKind_iterator byKindEnd(const ResKind &kind_r) const
Definition ResPool.h:269
byKind_iterator byKindBegin(const ResKind &kind_r) const
Definition ResPool.h:262
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.
LookupAttr::TransformIterator based container to retrieve list attributes.
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:174
static const SolvAttr repositoryRepoid
Definition SolvAttr.h:178
static const SolvAttr repositoryRevision
Definition SolvAttr.h:180
static const SolvAttr repositoryProductLabel
Definition SolvAttr.h:176
static const SolvAttr repositoryTimestamp
Definition SolvAttr.h:172
static const SolvAttr repositoryExpire
Definition SolvAttr.h:173
static const SolvAttr repositoryProductCpeid
Definition SolvAttr.h:177
static const SolvAttr repositoryDistros
array of repositoryProductLabel repositoryProductCpeid pairs
Definition SolvAttr.h:175
static const SolvAttr repositoryKeywords
Definition SolvAttr.h:179
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:337
detail::SolvableIdType _addSolvables(CRepo *repo_r, unsigned count_r)
Adding Solvables to a repo.
Definition PoolImpl.cc:424
static const std::string & systemRepoAlias()
Reserved system repository alias @System .
Definition PoolImpl.cc:100
void setRepoInfo(RepoIdType id_r, const RepoInfo &info_r)
Also adjust repo priority and subpriority accordingly.
Definition PoolImpl.cc:430
Iterate over valid Solvables in the pool.
Definition Solvable.h:521
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:37
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
zypp::RepoInfo _repo
Functor filtering Solvable by Repository.
Definition Repository.h:490
static PoolImpl & myPool()
Definition PoolImpl.cc:184
#define for_(IT, BEG, END)
Convenient for-loops using iterator.
Definition Easy.h:28
#define ZYPP_THROW(EXCPT)
Drops a logline and throws the Exception.
Definition Exception.h:424
#define MIL
Definition Logger.h:100
Interface to gettext.