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

libzypp 17.38.6

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/ng/sat/pool.h>
25#include <zypp/ng/sat/repository.h>
26#include <zypp/ng/sat/lookupattr.h>
27
28using std::endl;
29
31namespace zyppng
32{
33 namespace sat
34 {
35 namespace detail {
36 template<> Pool & poolFromType( Repository & r )
37 {
38 detail::CRepo * repo = r.get();
39 ZYPP_PRECONDITION( repo && repo->pool && repo->pool->appdata );
40 return *static_cast<Pool *>( repo->pool->appdata );
41 }
42 template<> const Pool & poolFromType( const Repository & r )
43 {
44 const detail::CRepo * repo = r.get();
45 ZYPP_PRECONDITION( repo && repo->pool && repo->pool->appdata );
46 return *static_cast<const Pool *>( repo->pool->appdata );
47 }
48 }
49
50 const Repository Repository::noRepository;
51
52 const std::string & Repository::systemRepoAlias()
53 { return Pool::systemRepoAlias(); }
54
56
57 detail::CRepo * Repository::get() const
58 { return _id; }
59
60#define NO_REPOSITORY_RETURN( VAL ) \
61 detail::CRepo * _repo( get() ); \
62 if ( ! _repo ) return VAL
63
64#define NO_REPOSITORY_THROW( VAL ) \
65 detail::CRepo * _repo( get() ); \
66 if ( ! _repo ) ZYPP_THROW( VAL )
67
68 bool Repository::isSystemRepo() const
69 {
70 NO_REPOSITORY_RETURN( false );
71 return pool().isSystemRepo( _repo );
72 }
73
74 std::string Repository::alias() const
75 {
76 NO_REPOSITORY_RETURN( std::string() );
77 if ( ! _repo->name )
78 return std::string();
79 return _repo->name;
80 }
81
82 int Repository::satInternalPriority() const
83 {
84 NO_REPOSITORY_RETURN( INT_MIN );
85 return _repo->priority;
86 }
87
88 int Repository::satInternalSubPriority() const
89 {
90 NO_REPOSITORY_RETURN( INT_MIN );
91 return _repo->subpriority;
92 }
93
94 Repository::ContentRevision Repository::contentRevision() const
95 {
96 NO_REPOSITORY_RETURN( ContentRevision() );
97 sat::LookupRepoAttr q( sat::SolvAttr::repositoryRevision, *this );
98 return q.empty() ? std::string() : q.begin().asString();
99 }
100
101 Repository::ContentIdentifier Repository::contentIdentifier() const
102 {
103 NO_REPOSITORY_RETURN( ContentIdentifier() );
104 sat::LookupRepoAttr q( sat::SolvAttr::repositoryRepoid, *this );
105 return q.empty() ? std::string() : q.begin().asString();
106 }
107
108 bool Repository::hasContentIdentifier( const ContentIdentifier & id_r ) const
109 {
110 NO_REPOSITORY_RETURN( false );
111
112 sat::LookupRepoAttr q( sat::SolvAttr::repositoryRepoid, *this );
113 for_( it, q.begin(), q.end() )
114 if ( it.asString() == id_r )
115 return true;
116
117 return false;
118 }
119
120 zypp::Date Repository::generatedTimestamp() const
121 {
122 NO_REPOSITORY_RETURN( zypp::Date() );
123 sat::LookupRepoAttr q( sat::SolvAttr::repositoryTimestamp, *this );
124 return( q.empty() ? 0 : q.begin().asUnsigned() );
125
126 }
127
128 zypp::Date Repository::suggestedExpirationTimestamp() const
129 {
130 NO_REPOSITORY_RETURN( zypp::Date() );
131 zypp::Date generated = generatedTimestamp();
132 if ( ! generated )
133 return 0; // do not calculate over a missing generated timestamp
134
135 sat::LookupRepoAttr q( sat::SolvAttr::repositoryExpire, *this );
136 if ( q.empty() )
137 return 0;
138
139 return generated + zypp::Date(q.begin().asUnsigned());
140
141 }
142
143 bool Repository::maybeOutdated() const
144 {
145 NO_REPOSITORY_RETURN( false );
146 // system repo is not mirrored
147 if ( isSystemRepo() )
148 return false;
149
150 zypp::Date suggested = suggestedExpirationTimestamp();
151
152 // if no data, don't suggest
153 if ( ! suggested )
154 return false;
155
156 return suggested < zypp::Date::now();
157 }
158
159 bool Repository::solvablesEmpty() const
160 {
161 NO_REPOSITORY_RETURN( true );
162 return !_repo->nsolvables;
163 }
164
165 Repository::size_type Repository::solvablesSize() const
166 {
167 NO_REPOSITORY_RETURN( 0 );
168 return _repo->nsolvables;
169 }
170
171 Repository::SolvableIterator Repository::solvablesBegin() const
172 {
173 NO_REPOSITORY_RETURN( zypp::make_filter_iterator( detail::ByRepository( *this ),
174 sat::detail::SolvableIterator(),
175 sat::detail::SolvableIterator() ) );
176 return zypp::make_filter_iterator( detail::ByRepository( *this ),
177 sat::detail::SolvableIterator(_repo->start),
178 sat::detail::SolvableIterator(_repo->end) );
179 }
180
181 Repository::SolvableIterator Repository::solvablesEnd() const
182 {
183 NO_REPOSITORY_RETURN( zypp::make_filter_iterator( detail::ByRepository( *this ),
184 sat::detail::SolvableIterator(),
185 sat::detail::SolvableIterator() ) );
186 return zypp::make_filter_iterator(detail::ByRepository( *this ),
187 sat::detail::SolvableIterator(_repo->end),
188 sat::detail::SolvableIterator(_repo->end) );
189 }
190
191 void Repository::eraseFromPool()
192 {
193 NO_REPOSITORY_RETURN();
194 //MIL << *this << " removed from pool" << endl;
195 pool()._deleteRepo( _repo );
196 _id = detail::noRepoId;
197 }
198
199 Repository Repository::nextInPool() const
200 {
201 NO_REPOSITORY_RETURN( noRepository );
202 auto iterable = pool().repos();
203 for( auto it = iterable.begin(); it != iterable.end(); ++it )
204 {
205 if ( *it == *this )
206 {
207 if ( ++it != iterable.end() )
208 return *it;
209 break;
210 }
211 }
212 return noRepository;
213 }
214
215 void Repository::addSolv( const zypp::Pathname & file_r )
216 {
217 NO_REPOSITORY_THROW( zypp::Exception( "Can't add solvables to norepo." ) );
218
219 zypp::AutoDispose<FILE*> file( ::fopen( file_r.c_str(), "re" ), ::fclose );
220 if ( file == NULL )
221 {
222 file.resetDispose();
223 ZYPP_THROW( zypp::Exception( "Can't open solv-file: "+file_r.asString() ) );
224 }
225
226 if ( pool()._addSolv( _repo, file ) != 0 )
227 {
228 ZYPP_THROW( zypp::Exception( "Error reading solv-file: "+file_r.asString() ) );
229 }
230
231 //MIL << *this << " after adding " << file_r << endl;
232 }
233
234 void Repository::addHelix( const zypp::Pathname & file_r )
235 {
236 NO_REPOSITORY_THROW( zypp::Exception( "Can't add solvables to norepo." ) );
237
238 std::string command( file_r.extension() == ".gz" ? "zcat " : "cat " );
239 command += "'";
240 command += file_r.asString();
241 command += "'";
242
243 zypp::AutoDispose<FILE*> file( ::popen( command.c_str(), "re" ), ::pclose );
244 if ( file == NULL )
245 {
246 file.resetDispose();
247 ZYPP_THROW( zypp::Exception( "Can't open helix-file: "+file_r.asString() ) );
248 }
249
250 if ( pool()._addHelix( _repo, file ) != 0 )
251 {
252 ZYPP_THROW( zypp::Exception( "Error reading helix-file: "+file_r.asString() ) );
253 }
254
255 //MIL << *this << " after adding " << file_r << endl;
256 }
257
258 void Repository::addTesttags( const zypp::Pathname & file_r )
259 {
260 NO_REPOSITORY_THROW( zypp::Exception( "Can't add solvables to norepo." ) );
261
262 std::string command( file_r.extension() == ".gz" ? "zcat " : "cat " );
263 command += "'";
264 command += file_r.asString();
265 command += "'";
266
267 zypp::AutoDispose<FILE*> file( ::popen( command.c_str(), "re" ), ::pclose );
268 if ( file == NULL )
269 {
270 file.resetDispose();
271 ZYPP_THROW( zypp::Exception( "Can't open testtags-file: "+file_r.asString() ) );
272 }
273
274 if ( pool()._addTesttags( _repo, file ) != 0 )
275 {
276 ZYPP_THROW( zypp::Exception( "Error reading testtags-file: "+file_r.asString() ) );
277 }
278
279 //MIL << *this << " after adding " << file_r << endl;
280 }
281
282 sat::Solvable::IdType Repository::addSolvables( unsigned count_r )
283 {
284 NO_REPOSITORY_THROW( zypp::Exception( "Can't add solvables to norepo.") );
285 return pool()._addSolvables( _repo, count_r );
286 }
287
288 namespace detail
289 {
290 void RepositoryIterator::increment()
291 {
292 if ( base() )
293 {
294 sat::detail::CPool * satpool = (*base())->pool;
295 do {
296 ++base_reference();
297 } while ( base() < satpool->repos+satpool->nrepos && !*base() );
298 }
299 }
300 }
301
302 } // namespace sat
303} // namespace zyppng
AutoDispose.h
for_
#define for_(IT, BEG, END)
Convenient for-loops using iterator.
Definition Easy.h:27
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::LookupAttr::iterator::asUnsigned
unsigned asUnsigned() const
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition lookupattr.cc:514
zyppng::sat::LookupAttr::iterator::asString
std::string asString() const
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition lookupattr.cc:566
zyppng::sat::LookupAttr::end
iterator end() const
Iterator behind the end of query results.
Definition lookupattr.cc:243
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:257
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:50
zyppng::sat::Repository::Repository
Repository()
Default ctor creates noRepository.
Definition repository.h:64
zyppng::sat::Repository::isSystemRepo
bool isSystemRepo() const
Return whether this is the system repository.
Definition repository.cc:68
zyppng::sat::Repository::noRepository
static const Repository noRepository
Represents no Repository.
Definition repository.h:78
zyppng::sat::Repository::_id
IdType _id
Definition repository.h:347
zyppng::sat::Repository::generatedTimestamp
zypp::Date generatedTimestamp() const
Timestamp when this repository was generated.
Definition repository.cc:120
zyppng::sat::Repository::SolvableIterator
boost::filter_iterator< detail::ByRepository, sat::detail::SolvableIterator > SolvableIterator
Definition repository.h:52
zyppng::sat::Repository::ContentRevision
std::string ContentRevision
Definition repository.h:59
zyppng::sat::Repository::get
detail::CRepo * get() const
Expert backdoor.
Definition repository.cc:57
zyppng::sat::Repository::ContentIdentifier
std::string ContentIdentifier
Definition repository.h:60
zyppng::sat::Repository::size_type
unsigned int size_type
Definition repository.h:54
zyppng::sat::Repository::suggestedExpirationTimestamp
zypp::Date suggestedExpirationTimestamp() const
Suggested expiration timestamp.
Definition repository.cc:128
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:67
zyppng::sat::detail::RepositoryIterator::increment
void increment()
Definition repository.cc:290
zyppng::sat::detail::SolvableIterator
Iterate over valid Solvables in the pool.
Definition solvable.h:417
zypp::base
Definition Random.cc:7
zypp::pool
Definition ByIdent.h:24
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
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
lookupattr.h
repository.h
zyppng::sat::detail::ByRepository
Functor filtering Solvable by Repository.
Definition repository.h:507