libzypp: zypp/ng/sat/repository.cc Source File

libzypp 17.38.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-core/base/LogTools.h>
17#include <zypp-core/base/Gettext.h>
18#include <zypp-core/base/Exception.h>
19
20#include <zypp-core/AutoDispose.h>
21#include <zypp-core/Pathname.h>
22
23#include <zypp-core/ng/base/precondition.h>
24#include <zypp-core/ng/base/ranges.h>
25#include <zypp/ng/sat/pool.h>
26#include <zypp/ng/sat/repository.h>
27#include <zypp/ng/sat/lookupattr.h>
28
29using std::endl;
30
32namespace zyppng
33{
34 namespace sat
35 {
36 namespace detail {
37 template<> Pool & poolFromType( Repository & r )
38 {
39 detail::CRepo * repo = r.get();
40 ZYPP_PRECONDITION( repo && repo->pool && repo->pool->appdata );
41 return *static_cast<Pool *>( repo->pool->appdata );
42 }
43 template<> const Pool & poolFromType( const Repository & r )
44 {
45 const detail::CRepo * repo = r.get();
46 ZYPP_PRECONDITION( repo && repo->pool && repo->pool->appdata );
47 return *static_cast<const Pool *>( repo->pool->appdata );
48 }
49 }
50
51 const Repository Repository::noRepository;
52
53 const std::string & Repository::systemRepoAlias()
54 { return Pool::systemRepoAlias(); }
55
57
58 detail::CRepo * Repository::get() const
59 { return _id; }
60
61#define NO_REPOSITORY_RETURN( VAL ) \
62 detail::CRepo * _repo( get() ); \
63 if ( ! _repo ) return VAL
64
65#define NO_REPOSITORY_THROW( VAL ) \
66 detail::CRepo * _repo( get() ); \
67 if ( ! _repo ) ZYPP_THROW( VAL )
68
69 bool Repository::isSystemRepo() const
70 {
71 NO_REPOSITORY_RETURN( false );
72 return pool().isSystemRepo( _repo );
73 }
74
75 std::string Repository::alias() const
76 {
77 NO_REPOSITORY_RETURN( std::string() );
78 if ( ! _repo->name )
79 return std::string();
80 return _repo->name;
81 }
82
83 int Repository::satInternalPriority() const
84 {
85 NO_REPOSITORY_RETURN( INT_MIN );
86 return _repo->priority;
87 }
88
89 int Repository::satInternalSubPriority() const
90 {
91 NO_REPOSITORY_RETURN( INT_MIN );
92 return _repo->subpriority;
93 }
94
95 Repository::ContentRevision Repository::contentRevision() const
96 {
97 NO_REPOSITORY_RETURN( ContentRevision() );
98 sat::LookupRepoAttr q( sat::SolvAttr::repositoryRevision, *this );
99 return q.empty() ? std::string() : (*q.begin()).asString();
100 }
101
102 Repository::ContentIdentifier Repository::contentIdentifier() const
103 {
104 NO_REPOSITORY_RETURN( ContentIdentifier() );
105 sat::LookupRepoAttr q( sat::SolvAttr::repositoryRepoid, *this );
106 return q.empty() ? std::string() : (*q.begin()).asString();
107 }
108
109 bool Repository::hasContentIdentifier( const ContentIdentifier & id_r ) const
110 {
111 NO_REPOSITORY_RETURN( false );
112
113 sat::LookupRepoAttr q( sat::SolvAttr::repositoryRepoid, *this );
114 return ranges::any_of( q, [&id_r]( const sat::LookupAttrValue v ){
115 return v.asString() == id_r;
116 });
117 }
118
119 zypp::Date Repository::generatedTimestamp() const
120 {
121 NO_REPOSITORY_RETURN( zypp::Date() );
122 sat::LookupRepoAttr q( sat::SolvAttr::repositoryTimestamp, *this );
123 return( q.empty() ? 0 : (*q.begin()).asUnsigned() );
124
125 }
126
127 zypp::Date Repository::suggestedExpirationTimestamp() const
128 {
129 NO_REPOSITORY_RETURN( zypp::Date() );
130 zypp::Date generated = generatedTimestamp();
131 if ( ! generated )
132 return 0; // do not calculate over a missing generated timestamp
133
134 sat::LookupRepoAttr q( sat::SolvAttr::repositoryExpire, *this );
135 if ( q.empty() )
136 return 0;
137
138 return generated + zypp::Date((*q.begin()).asUnsigned());
139
140 }
141
142 bool Repository::maybeOutdated() const
143 {
144 NO_REPOSITORY_RETURN( false );
145 // system repo is not mirrored
146 if ( isSystemRepo() )
147 return false;
148
149 zypp::Date suggested = suggestedExpirationTimestamp();
150
151 // if no data, don't suggest
152 if ( ! suggested )
153 return false;
154
155 return suggested < zypp::Date::now();
156 }
157
158 bool Repository::solvablesEmpty() const
159 {
160 NO_REPOSITORY_RETURN( true );
161 return !_repo->nsolvables;
162 }
163
164 Repository::size_type Repository::solvablesSize() const
165 {
166 NO_REPOSITORY_RETURN( 0 );
167 return _repo->nsolvables;
168 }
169
170 Repository::SolvableIterator Repository::solvablesBegin() const
171 {
172 NO_REPOSITORY_RETURN( zyppng::make_filter_iterator( detail::ByRepository( *this ),
173 sat::detail::SolvableIterator(),
174 sat::detail::SolvableIterator() ) );
175 return zyppng::make_filter_iterator( detail::ByRepository( *this ),
176 sat::detail::SolvableIterator(_repo->start),
177 sat::detail::SolvableIterator(_repo->end) );
178 }
179
180 Repository::SolvableIterator Repository::solvablesEnd() const
181 {
182 NO_REPOSITORY_RETURN( zyppng::make_filter_iterator( detail::ByRepository( *this ),
183 sat::detail::SolvableIterator(),
184 sat::detail::SolvableIterator() ) );
185 return zyppng::make_filter_iterator(detail::ByRepository( *this ),
186 sat::detail::SolvableIterator(_repo->end),
187 sat::detail::SolvableIterator(_repo->end) );
188 }
189
190 void Repository::eraseFromPool()
191 {
192 NO_REPOSITORY_RETURN();
193 //MIL << *this << " removed from pool" << endl;
194 pool()._deleteRepo( _repo );
195 _id = detail::noRepoId;
196 }
197
198 Repository Repository::nextInPool() const
199 {
200 NO_REPOSITORY_RETURN( noRepository );
201 auto iterable = pool().repos();
202 for( auto it = iterable.begin(); it != iterable.end(); ++it )
203 {
204 if ( *it == *this )
205 {
206 if ( ++it != iterable.end() )
207 return *it;
208 break;
209 }
210 }
211 return noRepository;
212 }
213
214 void Repository::addSolv( const zypp::Pathname & file_r )
215 {
216 NO_REPOSITORY_THROW( zypp::Exception( "Can't add solvables to norepo." ) );
217
218 zypp::AutoDispose<FILE*> file( ::fopen( file_r.c_str(), "re" ), ::fclose );
219 if ( file == NULL )
220 {
221 file.resetDispose();
222 ZYPP_THROW( zypp::Exception( "Can't open solv-file: "+file_r.asString() ) );
223 }
224
225 if ( pool()._addSolv( _repo, file ) != 0 )
226 {
227 ZYPP_THROW( zypp::Exception( "Error reading solv-file: "+file_r.asString() ) );
228 }
229
230 //MIL << *this << " after adding " << file_r << endl;
231 }
232
233 void Repository::addHelix( const zypp::Pathname & file_r )
234 {
235 NO_REPOSITORY_THROW( zypp::Exception( "Can't add solvables to norepo." ) );
236
237 std::string command( file_r.extension() == ".gz" ? "zcat " : "cat " );
238 command += "'";
239 command += file_r.asString();
240 command += "'";
241
242 zypp::AutoDispose<FILE*> file( ::popen( command.c_str(), "re" ), ::pclose );
243 if ( file == NULL )
244 {
245 file.resetDispose();
246 ZYPP_THROW( zypp::Exception( "Can't open helix-file: "+file_r.asString() ) );
247 }
248
249 if ( pool()._addHelix( _repo, file ) != 0 )
250 {
251 ZYPP_THROW( zypp::Exception( "Error reading helix-file: "+file_r.asString() ) );
252 }
253
254 //MIL << *this << " after adding " << file_r << endl;
255 }
256
257 void Repository::addTesttags( const zypp::Pathname & file_r )
258 {
259 NO_REPOSITORY_THROW( zypp::Exception( "Can't add solvables to norepo." ) );
260
261 std::string command( file_r.extension() == ".gz" ? "zcat " : "cat " );
262 command += "'";
263 command += file_r.asString();
264 command += "'";
265
266 zypp::AutoDispose<FILE*> file( ::popen( command.c_str(), "re" ), ::pclose );
267 if ( file == NULL )
268 {
269 file.resetDispose();
270 ZYPP_THROW( zypp::Exception( "Can't open testtags-file: "+file_r.asString() ) );
271 }
272
273 if ( pool()._addTesttags( _repo, file ) != 0 )
274 {
275 ZYPP_THROW( zypp::Exception( "Error reading testtags-file: "+file_r.asString() ) );
276 }
277
278 //MIL << *this << " after adding " << file_r << endl;
279 }
280
281 sat::Solvable::IdType Repository::addSolvables( unsigned count_r )
282 {
283 NO_REPOSITORY_THROW( zypp::Exception( "Can't add solvables to norepo.") );
284 return pool()._addSolvables( _repo, count_r );
285 }
286
287 namespace detail
288 {
289 void RepositoryIterator::increment()
290 {
291 if ( _base )
292 {
293 sat::detail::CPool * satpool = (*_base)->pool;
294 do {
295 ++_base;
296 } while ( _base < satpool->repos+satpool->nrepos && !*_base );
297 }
298 }
299 }
300
301 } // namespace sat
302} // namespace zyppng
AutoDispose.h
Exception.h
ZYPP_THROW
#define ZYPP_THROW(EXCPT)
Drops a logline and throws the Exception.
Definition Exception.h:459
Gettext.h
LogTools.h
Pathname.h
NO_REPOSITORY_THROW
#define NO_REPOSITORY_THROW(VAL)
Definition Repository.cc:50
NO_REPOSITORY_RETURN
#define NO_REPOSITORY_RETURN(VAL)
Definition Repository.cc:46
zypp::AutoDispose
Reference counted access to a Tp object calling a custom Dispose function when the last AutoDispose h...
Definition AutoDispose.h:95
zypp::AutoDispose::resetDispose
void resetDispose()
Set no dispose function.
Definition AutoDispose.h:171
zypp::Date
Store and operate on date (time_t).
Definition Date.h:33
zypp::Date::now
static Date now()
Return the current time.
Definition Date.h:78
zypp::Exception
Base class for Exception.
Definition Exception.h:153
zypp::Repository
Definition Repository.h:40
zypp::Repository::nextInPool
Repository nextInPool() const
Return next Repository in Pool (or noRepository).
Definition Repository.cc:306
zypp::Repository::satInternalSubPriority
int satInternalSubPriority() const
Definition Repository.cc:80
zypp::Repository::noRepository
static const Repository noRepository
Represents no Repository.
Definition Repository.h:67
zypp::Repository::addHelix
void addHelix(const Pathname &file_r)
Load Solvables from a helix-file.
Definition Repository.cc:340
zypp::Repository::get
sat::detail::CRepo * get() const
Expert backdoor.
Definition Repository.cc:43
zypp::Repository::solvablesEmpty
bool solvablesEmpty() const
Whether Repository contains solvables.
Definition Repository.cc:220
zypp::Repository::suggestedExpirationTimestamp
Date suggestedExpirationTimestamp() const
Suggested expiration timestamp.
Definition Repository.cc:117
zypp::Repository::solvablesEnd
SolvableIterator solvablesEnd() const
Iterator behind the last Solvable.
Definition Repository.cc:242
zypp::Repository::satInternalPriority
int satInternalPriority() const
libsolv internal priorities.
Definition Repository.cc:74
zypp::Repository::addSolvables
sat::Solvable::IdType addSolvables(unsigned count_r)
Add count_r new empty Solvable to this Repository.
Definition Repository.cc:388
zypp::Repository::solvablesBegin
SolvableIterator solvablesBegin() const
Iterator to the first Solvable.
Definition Repository.cc:232
zypp::Repository::addTesttags
void addTesttags(const Pathname &file_r)
Load Solvables from a libsolv testtags-file.
Definition Repository.cc:364
zypp::Repository::contentIdentifier
ContentIdentifier contentIdentifier() const
Unique string identifying a repositories content.
Definition Repository.cc:93
zypp::Repository::maybeOutdated
bool maybeOutdated() const
The suggested expiration date of this repository already passed.
Definition Repository.cc:145
zypp::Repository::alias
std::string alias() const
Short unique string to identify a repo.
Definition Repository.cc:60
zypp::Repository::hasContentIdentifier
bool hasContentIdentifier(const ContentIdentifier &id_r) const
Whether id_r matches this repos content identifier.
Definition Repository.cc:100
zypp::Repository::solvablesSize
size_type solvablesSize() const
Number of solvables in Repository.
Definition Repository.cc:226
zypp::Repository::addSolv
void addSolv(const Pathname &file_r)
Load Solvables from a solv-file.
Definition Repository.cc:321
zypp::Repository::contentRevision
ContentRevision contentRevision() const
Timestamp or arbitrary user supplied string.
Definition Repository.cc:86
zypp::Repository::generatedTimestamp
Date generatedTimestamp() const
Timestamp when this repository was generated.
Definition Repository.cc:110
zypp::Repository::systemRepoAlias
static const std::string & systemRepoAlias()
Reserved system repository alias @System .
Definition Repository.cc:38
zypp::Repository::eraseFromPool
void eraseFromPool()
Remove this Repository from its Pool.
Definition Repository.cc:298
zypp::Repository::isSystemRepo
bool isSystemRepo() const
Return whether this is the system repository.
Definition Repository.cc:54
zypp::filesystem::Pathname
Pathname.
Definition Pathname.h:48
zypp::filesystem::Pathname::c_str
const char * c_str() const
String representation.
Definition Pathname.h:113
zypp::filesystem::Pathname::asString
const std::string & asString() const
String representation.
Definition Pathname.h:94
zypp::filesystem::Pathname::extension
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:144
zyppng::sat::LookupAttrValue
Value proxy returned by LookupAttr::iterator::operator*().
Definition lookupattr.h:454
zyppng::sat::LookupAttrValue::asString
std::string asString() const
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition lookupattr.cc:575
zyppng::sat::LookupAttr::begin
iterator begin() const
Iterator to the begin of query results.
Definition lookupattr.cc:240
zyppng::sat::LookupAttr::empty
bool empty() const
Whether the query is empty.
Definition lookupattr.cc:246
zyppng::sat::LookupRepoAttr
Lightweight repository attribute value lookup.
Definition lookupattr.h:259
zyppng::sat::PoolMember< Repository >::pool
Pool & pool()
Definition poolmember.h:30
zyppng::sat::Pool
Orchestrator for a libsolv pool instance.
Definition pool.h:37
zyppng::sat::Pool::_deleteRepo
void _deleteRepo(detail::CRepo *repo_r)
Delete repo repo_r from pool.
Definition pool.cc:277
zyppng::sat::Pool::isSystemRepo
bool isSystemRepo(detail::CRepo *repo_r) const
Definition pool.h:132
zyppng::sat::Pool::repos
RepositoryIterable repos() const
Iteratable to the repositories.
Definition pool.cc:208
zyppng::sat::Pool::systemRepoAlias
static const std::string & systemRepoAlias()
Reserved system repository alias @System .
Definition pool.cc:178
zyppng::sat::Pool::_addSolvables
detail::SolvableIdType _addSolvables(detail::CRepo *repo_r, unsigned count_r)
Adding Solvables to a repo.
Definition pool.cc:318
zyppng::sat::Repository
Definition repository.h:51
zyppng::sat::Repository::Repository
Repository()
Default ctor creates noRepository.
Definition repository.h:65
zyppng::sat::Repository::isSystemRepo
bool isSystemRepo() const
Return whether this is the system repository.
Definition repository.cc:69
zyppng::sat::Repository::noRepository
static const Repository noRepository
Represents no Repository.
Definition repository.h:79
zyppng::sat::Repository::_id
IdType _id
Definition repository.h:348
zyppng::sat::Repository::generatedTimestamp
zypp::Date generatedTimestamp() const
Timestamp when this repository was generated.
Definition repository.cc:119
zyppng::sat::Repository::ContentRevision
std::string ContentRevision
Definition repository.h:60
zyppng::sat::Repository::SolvableIterator
zyppng::FilterIterator< detail::ByRepository, sat::detail::SolvableIterator > SolvableIterator
Definition repository.h:53
zyppng::sat::Repository::get
detail::CRepo * get() const
Expert backdoor.
Definition repository.cc:58
zyppng::sat::Repository::ContentIdentifier
std::string ContentIdentifier
Definition repository.h:61
zyppng::sat::Repository::size_type
unsigned int size_type
Definition repository.h:55
zyppng::sat::Repository::suggestedExpirationTimestamp
zypp::Date suggestedExpirationTimestamp() const
Suggested expiration timestamp.
Definition repository.cc:127
zyppng::sat::SolvAttr::repositoryRepoid
static const SolvAttr repositoryRepoid
Definition SolvAttr.h:190
zyppng::sat::SolvAttr::repositoryRevision
static const SolvAttr repositoryRevision
Definition SolvAttr.h:192
zyppng::sat::SolvAttr::repositoryTimestamp
static const SolvAttr repositoryTimestamp
Definition SolvAttr.h:184
zyppng::sat::SolvAttr::repositoryExpire
static const SolvAttr repositoryExpire
Definition SolvAttr.h:185
zyppng::sat::Solvable::IdType
detail::SolvableIdType IdType
Definition solvable.h:68
zyppng::sat::detail::RepositoryIterator::_base
sat::detail::CRepo ** _base
Definition repository.h:513
zyppng::sat::detail::RepositoryIterator::increment
void increment()
Definition repository.cc:289
zyppng::sat::detail::SolvableIterator
Iterate over valid Solvables in the pool.
Definition solvable.h:415
zyppng::repo
Definition downloader.cc:22
zyppng::sat::detail
CLASS NAME : detail::DIWrap.
Definition cap2str.cc:14
zyppng::sat::detail::CPool
zypp::sat::detail::CPool CPool
Definition poolconstants.h:36
zyppng::sat::detail::poolFromType
Pool & poolFromType(T &)
zyppng::sat::detail::noRepoId
static const RepoIdType noRepoId(0)
Id to denote Repo::noRepository.
zyppng::sat::detail::CRepo
zypp::sat::detail::CRepo CRepo
Definition poolconstants.h:38
zyppng::sat
This file contains private API, this might break at any time between releases.
Definition capabilities.h:22
zyppng
Definition MediaNetworkRequestExecutor.h:16
zyppng::make_filter_iterator
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
pool.h
precondition.h
Always-on precondition checking for NG code.
ZYPP_PRECONDITION
#define ZYPP_PRECONDITION(EXPR,...)
Always-on precondition check — fires in debug AND release builds.
Definition precondition.h:42
ranges.h
Namespace routing for C++20 ranges and C++23 ranges::to<T>() backport.
lookupattr.h
repository.h
zyppng::sat::detail::ByRepository
Functor filtering Solvable by Repository.
Definition repository.h:523