libzypp 17.37.10

MirroredOrigin.cc
Go to the documentation of this file.
1/*---------------------------------------------------------------------\
2| ____ _ __ __ ___ |
3| |__ / \ / / . \ . \ |
4| / / \ V /| _/ _/ |
5| / /__ | | | | | | |
6| /_____||_| |_| |_| |
7| |
8\---------------------------------------------------------------------*/
9#include "MirroredOrigin.h"
10
11#include <zypp-core/base/LogTools.h>
12#undef ZYPP_BASE_LOGGER_LOGGROUP
13#define ZYPP_BASE_LOGGER_LOGGROUP "zypp::MirroredOrigin"
14
15 namespace zypp {
16
17 struct OriginEndpoint::Private {
18
19 Private( Url &&u, OriginEndpoint::SettingsMap &&m )
20 : _url(std::move(u))
21 , _settings(std::move(m))
22 {}
23 ~Private() = default;
24
25 Private *clone () const {
26 return new Private(*this);
27 }
28
29 zypp::Url _url;
30 std::unordered_map<std::string, std::any> _settings;
31 //OriginEndpoint::SettingsMap _settings;
32 };
33
34 OriginEndpoint::OriginEndpoint()
35 : _pimpl( new Private(Url(), {} ) )
36 {}
37
38 OriginEndpoint::OriginEndpoint( Url url, SettingsMap settings )
39 : _pimpl( new Private(std::move(url), std::move(settings) ) )
40 {}
41
42 OriginEndpoint::OriginEndpoint( Url url )
43 : OriginEndpoint( std::move(url), SettingsMap() )
44 { }
45
46 Url &OriginEndpoint::url()
47 {
48 return _pimpl->_url;
49 }
50
51 const Url &OriginEndpoint::url() const
52 {
53 return _pimpl->_url;
54 }
55
56 void OriginEndpoint::setUrl(const Url &newUrl)
57 {
58 _pimpl->_url = newUrl;
59 }
60
61 bool OriginEndpoint::hasConfig(const std::string &key) const
62 {
63 return (_pimpl->_settings.count (key) > 0);
64 }
65
66 std::string OriginEndpoint::scheme() const
67 {
68 return _pimpl->_url.getScheme();
69 }
70
71 bool OriginEndpoint::schemeIsDownloading() const
72 {
73 return _pimpl->_url.schemeIsDownloading ();
74 }
75
76 bool OriginEndpoint::isValid() const
77 {
78 return _pimpl->_url.isValid();
79 }
80
81 void OriginEndpoint::setConfig(const std::string &key, std::any value)
82 {
83 _pimpl->_settings.insert_or_assign ( key, std::move(value) );
84 }
85
86 const std::any &OriginEndpoint::getConfig(const std::string &key) const
87 {
88 return _pimpl->_settings.at(key);
89 }
90
91 std::any &OriginEndpoint::getConfig(const std::string &key)
92 {
93 return _pimpl->_settings.at(key);
94 }
95
96 void OriginEndpoint::eraseConfigValue( const std::string &key )
97 {
98 auto it = _pimpl->_settings.find (key);
99 if ( it == _pimpl->_settings.end() )
100 return;
101 _pimpl->_settings.erase(it);
102 }
103
104 const OriginEndpoint::SettingsMap &OriginEndpoint::config() const
105 {
106 return _pimpl->_settings;
107 }
108
109
110 OriginEndpoint::SettingsMap &OriginEndpoint::config()
111 {
112 return _pimpl->_settings;
113 }
114
115
116 std::ostream & operator<<( std::ostream & str, const OriginEndpoint & url )
117 {
118 return str << url.url().asString();
119 }
120
121 bool operator<( const OriginEndpoint &lhs, const OriginEndpoint &rhs )
122 {
123 return (lhs.url().asCompleteString() < rhs.url().asCompleteString());
124 }
125
126 bool operator==( const OriginEndpoint &lhs, const OriginEndpoint &rhs )
127 {
128 return (lhs.url().asCompleteString() == rhs.url().asCompleteString());
129 }
130
131 bool operator!=( const OriginEndpoint &lhs, const OriginEndpoint &rhs )
132 {
133 return (lhs.url().asCompleteString() != rhs.url().asCompleteString());
134 }
135
136 struct MirroredOrigin::Private {
137 Private( OriginEndpoint &&authority = {}, std::vector<OriginEndpoint> &&mirrors = {} )
138 : _authority( std::move(authority) )
139 , _mirrors( std::move(mirrors) )
140 {}
141 ~Private() = default;
142
143 Private *clone () const {
144 return new Private(*this);
145 }
146
147 OriginEndpoint _authority;
148 std::vector<OriginEndpoint> _mirrors;
149 };
150
151 MirroredOrigin::MirroredOrigin()
152 : _pimpl( new Private() )
153 {}
154
155 MirroredOrigin::MirroredOrigin(OriginEndpoint authority, std::vector<OriginEndpoint> mirrors )
156 : _pimpl( new Private( std::move(authority) ) )
157 {
158 for( auto &m : mirrors ) { addMirror ( std::move(m) ); }
159 }
160
161 void MirroredOrigin::setAuthority(OriginEndpoint newAuthority)
162 {
163 const auto &newScheme = newAuthority.scheme();
164 bool newAuthIsDl = newAuthority.url().schemeIsDownloading();
165
166 _pimpl->_authority = std::move(newAuthority);
167
168 if ( !_pimpl->_authority.isValid() || !_pimpl->_mirrors.size () )
169 return;
170
171 // house keeeping, we want only compatible mirrors
172 for ( auto i = _pimpl->_mirrors.begin (); i != _pimpl->_mirrors.end(); ) {
173 if ( ( newAuthIsDl && !i->schemeIsDownloading() ) // drop mirror if its not downloading but authority is
174 && ( i->scheme () != newScheme ) // otherwise drop if scheme is not identical
175 ) {
176 MIL << "Dropping mirror " << *i << " scheme is not compatible to new authority URL ( " << i->scheme() << " vs " << newScheme << ")" << std::endl;
177 i = _pimpl->_mirrors.erase(i);
178 } else {
179 i++;
180 }
181 }
182 }
183
184 const OriginEndpoint &MirroredOrigin::authority() const
185 {
186 return _pimpl->_authority;
187 }
188
189 const std::vector<OriginEndpoint> &MirroredOrigin::mirrors() const
190 {
191 return _pimpl->_mirrors;
192 }
193
194 bool MirroredOrigin::isValid() const
195 {
196 return _pimpl->_authority.isValid();
197 }
198
199 bool MirroredOrigin::addMirror(OriginEndpoint newMirror)
200 {
201 if ( _pimpl->_authority.isValid()
202 && ( _pimpl->_authority.schemeIsDownloading() && !newMirror.schemeIsDownloading () )
203 && ( _pimpl->_authority.scheme () != newMirror.scheme () )
204
205 ) {
206 MIL << "Ignoring mirror " << newMirror << " scheme is not compatible to new authority URL ( " << newMirror.scheme() << " vs " << _pimpl->_authority.scheme() << ")" << std::endl;
207 return false;
208 }
209 _pimpl->_mirrors.push_back( std::move(newMirror) );
210 return true;
211 }
212
213 void MirroredOrigin::setMirrors(std::vector<OriginEndpoint> mirrors)
214 {
215 clearMirrors();
216 for ( auto &m : mirrors )
217 addMirror( std::move(m) );
218 }
219
220 void MirroredOrigin::clearMirrors()
221 {
222 _pimpl->_mirrors.clear();
223 }
224
225 std::string MirroredOrigin::scheme() const
226 {
227 return _pimpl->_authority.url().getScheme();
228 }
229
230 bool MirroredOrigin::schemeIsDownloading() const
231 {
232 return _pimpl->_authority.schemeIsDownloading();
233 }
234
235 uint MirroredOrigin::endpointCount() const
236 {
237 // authority is always accessible, even if its a invalid URL
238 return _pimpl->_mirrors.size() + 1;
239 }
240
241 const OriginEndpoint &MirroredOrigin::at(uint index) const
242 {
243 if ( index >= endpointCount() ) {
244 throw std::out_of_range( "OriginEndpoint index out of range." );
245 }
246 if ( index == 0 ) {
247 return _pimpl->_authority;
248 }
249
250 return _pimpl->_mirrors.at( index - 1 );
251 }
252
253 OriginEndpoint &MirroredOrigin::at(uint index)
254 {
255 if ( index >= endpointCount() ) {
256 throw std::out_of_range( "OriginEndpoint index out of range." );
257 }
258 if ( index == 0 ) {
259 return _pimpl->_authority;
260 }
261
262 return _pimpl->_mirrors.at( index - 1 );
263 }
264
265 struct MirroredOriginSet::Private
266 {
267 Private() {}
268 ~Private() = default;
269
270 Private *clone () const {
271 return new Private(*this);
272 }
273
274 std::optional<std::size_t> _dlIndex; //< set if there is a downloading MirroredOrigin
275 std::vector<MirroredOrigin> _mirrors;
276 };
277
278
279 MirroredOriginSet::MirroredOriginSet()
280 : _pimpl( new Private() )
281 {}
282
283 MirroredOriginSet::MirroredOriginSet( std::vector<OriginEndpoint> eps )
284 : MirroredOriginSet()
285 {
286 if ( eps.size() )
287 addEndpoints( std::move(eps) );
288 }
289
290 MirroredOriginSet::MirroredOriginSet(std::list<Url> urls)
291 : MirroredOriginSet()
292 {
293 for( auto &url: urls )
294 addEndpoint( std::move(url) );
295 }
296
297 MirroredOriginSet::MirroredOriginSet(std::vector<Url> urls)
298 : MirroredOriginSet()
299 {
300 for( auto &url: urls )
301 addEndpoint( std::move(url) );
302 }
303
304 const MirroredOrigin &MirroredOriginSet::at(size_type idx) const
305 {
306 return _pimpl->_mirrors.at(idx);
307 }
308
309 MirroredOrigin &MirroredOriginSet::at(size_type idx)
310 {
311 return _pimpl->_mirrors.at(idx);
312 }
313
314 std::ostream & operator<<( std::ostream & str, const MirroredOrigin & origin )
315 {
316 return dumpRange( str << "MirroredOrigin { authority: \"" << origin.authority() << "\", ",
317 origin.mirrors().begin(), origin.mirrors().end(), "mirrors: [", "\"", "\",\"", "\"", "]" )
318 << " }";
319 }
320
321 MirroredOriginSet::iterator MirroredOriginSet::findByUrl( const Url &url )
322 {
323 for ( auto i = begin(); i!=end(); i++ ) {
324 auto epI = std::find_if( i->begin (), i->end(), [&](const OriginEndpoint &ep){ return ep.url () == url; } );
325 if ( epI != i->end() )
326 return i;
327 }
328 return end();
329 }
330
331 MirroredOriginSet::const_iterator MirroredOriginSet::findByUrl( const Url &url ) const
332 {
333 for ( auto i = begin(); i!=end(); i++ ) {
334 auto epI = std::find_if( i->begin (), i->end(), [&](const OriginEndpoint &ep){ return ep.url () == url; } );
335 if ( epI != i->end() )
336 return i;
337 }
338 return end();
339 }
340
341 void MirroredOriginSet::addEndpoint( OriginEndpoint endpoint )
342 {
343 if ( !endpoint.url().schemeIsDownloading () ) {
344 _pimpl->_mirrors.push_back ( MirroredOrigin(std::move(endpoint), {} ) );
345 return;
346 }
347
348 if ( _pimpl->_dlIndex ) {
349 _pimpl->_mirrors.at(*_pimpl->_dlIndex).addMirror( std::move(endpoint) );
350 return;
351 }
352
353 // start a new origin
354 _pimpl->_mirrors.push_back ( MirroredOrigin(std::move(endpoint), {} ) );
355 _pimpl->_dlIndex = _pimpl->_mirrors.size() - 1;
356 }
357
358
359 void MirroredOriginSet::addEndpoints( std::vector<OriginEndpoint> endpoints )
360 {
361 for ( auto &ep : endpoints )
362 addEndpoint ( std::move(ep) );
363 }
364
365 bool MirroredOriginSet::empty() const
366 {
367 return _pimpl->_mirrors.empty ();
368 }
369
370 void MirroredOriginSet::clear()
371 {
372 _pimpl->_mirrors.clear();
373 _pimpl->_dlIndex.reset();
374 }
375
376 MirroredOriginSet::iterator MirroredOriginSet::begin()
377 {
378 return _pimpl->_mirrors.begin ();
379 }
380
381
382 MirroredOriginSet::iterator MirroredOriginSet::end()
383 {
384 return _pimpl->_mirrors.end ();
385 }
386
387
388 MirroredOriginSet::const_iterator MirroredOriginSet::begin() const
389 {
390 return _pimpl->_mirrors.begin ();
391 }
392
393
394 MirroredOriginSet::const_iterator MirroredOriginSet::end() const
395 {
396 return _pimpl->_mirrors.end ();
397 }
398
399 MirroredOriginSet::size_type MirroredOriginSet::size() const
400 {
401 return _pimpl->_mirrors.size ();
402 }
403
404 bool MirroredOriginSet::hasFallbackUrls() const
405 {
406 return ( size() == 1 && at( 0 ).endpointCount() > 1 ) || size() > 1;
407 }
408
409 std::ostream & operator<<( std::ostream & str, const MirroredOriginSet & origin )
410 {
411 return dumpRange( str, origin.begin(), origin.end(), "MirroredOriginSet {", " ", ", ", " ", "}" );
412 }
413
414}
MirroredOrigin.h
zypp::MirroredOriginSet
A smart container that manages a collection of MirroredOrigin objects, automatically grouping endpoin...
Definition MirroredOrigin.h:325
zypp::MirroredOriginSet::hasFallbackUrls
bool hasFallbackUrls() const
Whether this set contains more than one Url in total (authorities or mirrors).
Definition MirroredOrigin.cc:404
zypp::MirroredOriginSet::findByUrl
const_iterator findByUrl(const zypp::Url &url) const
Finds the MirroredOrigin that contains a specific URL.
Definition MirroredOrigin.cc:331
zypp::MirroredOriginSet::clear
void clear()
Definition MirroredOrigin.cc:370
zypp::MirroredOriginSet::at
const MirroredOrigin & at(size_type idx) const
Accesses the MirroredOrigin at a specific index.
Definition MirroredOrigin.cc:304
zypp::MirroredOriginSet::size
size_type size() const
Returns the number of MirroredOrigin objects in the set.
Definition MirroredOrigin.cc:399
zypp::MirroredOriginSet::end
iterator end()
Returns an iterator to the element following the last MirroredOrigin.
Definition MirroredOrigin.cc:382
zypp::MirroredOriginSet::MirroredOriginSet
MirroredOriginSet()
Definition MirroredOrigin.cc:279
zypp::MirroredOriginSet::const_iterator
std::vector< MirroredOrigin >::const_iterator const_iterator
Definition MirroredOrigin.h:329
zypp::MirroredOriginSet::_pimpl
RWCOW_pointer< Private > _pimpl
Definition MirroredOrigin.h:436
zypp::MirroredOriginSet::begin
iterator begin()
Returns an iterator to the first MirroredOrigin in insertion order.
Definition MirroredOrigin.cc:376
zypp::MirroredOriginSet::iterator
std::vector< MirroredOrigin >::iterator iterator
Definition MirroredOrigin.h:328
zypp::MirroredOriginSet::size_type
size_t size_type
Definition MirroredOrigin.h:330
zypp::MirroredOriginSet::empty
bool empty() const
Definition MirroredOrigin.cc:365
zypp::MirroredOriginSet::addEndpoints
void addEndpoints(InputIterator first, InputIterator last)
A convenience method to add multiple endpoints from a range.
Definition MirroredOrigin.h:387
zypp::MirroredOriginSet::addEndpoint
void addEndpoint(OriginEndpoint endpoint)
Adds a single endpoint, routing it to the correct MirroredOrigin.
Definition MirroredOrigin.cc:341
zypp::MirroredOrigin
Manages a data source characterized by an authoritative URL and a list of mirror URLs.
Definition MirroredOrigin.h:170
zypp::MirroredOrigin::schemeIsDownloading
bool schemeIsDownloading() const
Definition MirroredOrigin.cc:230
zypp::MirroredOrigin::mirrors
const std::vector< OriginEndpoint > & mirrors() const
Definition MirroredOrigin.cc:189
zypp::MirroredOrigin::at
const OriginEndpoint & at(uint index) const
Definition MirroredOrigin.cc:241
zypp::MirroredOrigin::setAuthority
void setAuthority(OriginEndpoint newAuthority)
Definition MirroredOrigin.cc:161
zypp::MirroredOrigin::_pimpl
RWCOW_pointer< Private > _pimpl
Definition MirroredOrigin.h:298
zypp::MirroredOrigin::setMirrors
void setMirrors(std::vector< OriginEndpoint > mirrors)
Definition MirroredOrigin.cc:213
zypp::MirroredOrigin::MirroredOrigin
MirroredOrigin()
Definition MirroredOrigin.cc:151
zypp::MirroredOrigin::isValid
bool isValid() const
Definition MirroredOrigin.cc:194
zypp::MirroredOrigin::clearMirrors
void clearMirrors()
Definition MirroredOrigin.cc:220
zypp::MirroredOrigin::addMirror
bool addMirror(OriginEndpoint newMirror)
Definition MirroredOrigin.cc:199
zypp::MirroredOrigin::scheme
std::string scheme() const
Definition MirroredOrigin.cc:225
zypp::MirroredOrigin::authority
const OriginEndpoint & authority() const
Definition MirroredOrigin.cc:184
zypp::MirroredOrigin::endpointCount
uint endpointCount() const
Definition MirroredOrigin.cc:235
zypp::OriginEndpoint
Represents a single, configurable network endpoint, combining a URL with specific access settings.
Definition MirroredOrigin.h:41
zypp::OriginEndpoint::schemeIsDownloading
bool schemeIsDownloading() const
Definition MirroredOrigin.cc:71
zypp::OriginEndpoint::OriginEndpoint
OriginEndpoint()
Definition MirroredOrigin.cc:34
zypp::OriginEndpoint::_pimpl
RWCOW_pointer< Private > _pimpl
Definition MirroredOrigin.h:119
zypp::OriginEndpoint::scheme
std::string scheme() const
Definition MirroredOrigin.cc:66
zypp::OriginEndpoint::url
const zypp::Url & url() const
Definition MirroredOrigin.cc:51
zypp::OriginEndpoint::isValid
bool isValid() const
Definition MirroredOrigin.cc:76
zypp::OriginEndpoint::setUrl
void setUrl(const zypp::Url &newUrl)
Definition MirroredOrigin.cc:56
zypp::OriginEndpoint::hasConfig
bool hasConfig(const std::string &key) const
Definition MirroredOrigin.cc:61
zypp::Url
Url manipulation class.
Definition Url.h:93
zypp::Url::asCompleteString
std::string asCompleteString() const
Returns a complete string representation of the Url object.
Definition Url.cc:523
zypp::Url::schemeIsDownloading
static bool schemeIsDownloading(const std::string &scheme_r)
http https ftp sftp tftp
Definition Url.cc:493
std
Definition Arch.h:364
str
String related utilities and Regular expression matching.
zypp::url
Url details namespace.
Definition UrlBase.cc:58
zypp
Easy-to use interface to the ZYPP dependency resolver.
Definition Application.cc:20
zypp::operator<
bool operator<(const StrMatcher &lhs, const StrMatcher &rhs)
Definition StrMatcher.cc:340
zypp::dumpRange
std::ostream & dumpRange(std::ostream &str, TIterator begin, TIterator end, const std::string &intro="{", const std::string &pfx="\n ", const std::string &sep="\n ", const std::string &sfx="\n", const std::string &extro="}")
Print range defined by iterators (multiline style).
Definition LogTools.h:120
zypp::operator==
bool operator==(const SetRelation::Enum &lhs, const SetCompare &rhs)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition SetRelationMixin.h:84
zypp::operator<<
std::ostream & operator<<(std::ostream &str, const SerialNumber &obj)
Definition SerialNumber.cc:52
zypp::operator!=
bool operator!=(const SetRelation::Enum &lhs, const SetCompare &rhs)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition SetRelationMixin.h:109
zypp::MirroredOriginSet::Private
Definition MirroredOrigin.cc:266
zypp::MirroredOriginSet::Private::_mirrors
std::vector< MirroredOrigin > _mirrors
Definition MirroredOrigin.cc:275
zypp::MirroredOriginSet::Private::~Private
~Private()=default
zypp::MirroredOriginSet::Private::clone
Private * clone() const
Definition MirroredOrigin.cc:270
zypp::MirroredOriginSet::Private::Private
Private()
Definition MirroredOrigin.cc:267
zypp::MirroredOriginSet::Private::_dlIndex
std::optional< std::size_t > _dlIndex
Definition MirroredOrigin.cc:274
zypp::MirroredOrigin::Private
Definition MirroredOrigin.cc:136
zypp::MirroredOrigin::Private::~Private
~Private()=default
zypp::MirroredOrigin::Private::Private
Private(OriginEndpoint &&authority={}, std::vector< OriginEndpoint > &&mirrors={})
Definition MirroredOrigin.cc:137
zypp::MirroredOrigin::Private::_authority
OriginEndpoint _authority
Definition MirroredOrigin.cc:147
zypp::MirroredOrigin::Private::clone
Private * clone() const
Definition MirroredOrigin.cc:143
zypp::MirroredOrigin::Private::_mirrors
std::vector< OriginEndpoint > _mirrors
Definition MirroredOrigin.cc:148
zypp::OriginEndpoint::Private
Definition MirroredOrigin.cc:17
zypp::OriginEndpoint::Private::_settings
std::unordered_map< std::string, std::any > _settings
Definition MirroredOrigin.cc:30
zypp::OriginEndpoint::Private::~Private
~Private()=default
zypp::OriginEndpoint::Private::_url
zypp::Url _url
Definition MirroredOrigin.cc:29
zypp::OriginEndpoint::Private::clone
Private * clone() const
Definition MirroredOrigin.cc:25
zypp::OriginEndpoint::Private::Private
Private(Url &&u, OriginEndpoint::SettingsMap &&m)
Definition MirroredOrigin.cc:19
LogTools.h
MIL
#define MIL
Definition Logger.h:100