libzypp: zypp-core/MirroredOrigin.cc Source File

libzypp 17.38.5

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
15namespace 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() = default;
138 ~Private() = default;
139
140 Private *clone () const {
141 return new Private(*this);
142 }
143
144 OriginEndpoint _authority;
145 std::vector<OriginEndpoint> _origins;
146 };
147
148 MirroredOrigin::MirroredOrigin()
149 : _pimpl( new Private() )
150 {}
151
152 MirroredOrigin::MirroredOrigin(OriginEndpoint authority, std::vector<OriginEndpoint> mirrors )
153 : _pimpl( new Private() )
154 {
155 if ( authority.isValid() )
156 _pimpl->_authority = std::move(authority);
157 else
158 WAR << "Ignoring invalid authority in constructor: " << authority << std::endl;
159
160 for( auto &m : mirrors ) { addMirror ( std::move(m) ); }
161 }
162
163 void MirroredOrigin::setAuthority(OriginEndpoint newAuthority)
164 {
165 if ( !newAuthority.isValid() )
166 {
167 WAR << "Ignoring invalid authority: " << newAuthority << std::endl;
168 return;
169 }
170
171 const auto &newScheme = newAuthority.scheme();
172 bool newAuthIsDl = newAuthority.url().schemeIsDownloading();
173
174 _pimpl->_authority = std::move(newAuthority);
175
176 if ( !_pimpl->_authority.isValid() || !_pimpl->_origins.size () )
177 return;
178
179 // house keeeping, we want only compatible mirrors
180 for ( auto i = _pimpl->_origins.begin (); i != _pimpl->_origins.end(); ) {
181 if ( ( newAuthIsDl && !i->schemeIsDownloading() ) // drop mirror if its not downloading but authority is
182 && ( i->scheme () != newScheme ) // otherwise drop if scheme is not identical
183 ) {
184 MIL << "Dropping mirror " << *i << " scheme is not compatible to new authority URL ( " << i->scheme() << " vs " << newScheme << ")" << std::endl;
185 i = _pimpl->_origins.erase(i);
186 } else {
187 i++;
188 }
189 }
190 }
191
192 const OriginEndpoint &MirroredOrigin::authority() const
193 {
194 return _pimpl->_authority;
195 }
196
197 const std::vector<OriginEndpoint> &MirroredOrigin::mirrors() const
198 {
199 return _pimpl->_origins;
200 }
201
202 bool MirroredOrigin::isValid() const
203 {
204 return _pimpl->_authority.isValid();
205 }
206
207 bool MirroredOrigin::addMirror(OriginEndpoint newMirror)
208 {
209 if ( !newMirror.isValid() )
210 {
211 WAR << "Ignoring invalid mirror: " << newMirror << std::endl;
212 return false;
213 }
214
215 if ( _pimpl->_authority.isValid()
216 && ( _pimpl->_authority.schemeIsDownloading() && !newMirror.schemeIsDownloading () )
217 && ( _pimpl->_authority.scheme () != newMirror.scheme () )
218
219 ) {
220 MIL << "Ignoring mirror " << newMirror << " scheme is not compatible to new authority URL ( " << newMirror.scheme() << " vs " << _pimpl->_authority.scheme() << ")" << std::endl;
221 return false;
222 }
223 _pimpl->_origins.push_back( std::move(newMirror) );
224 return true;
225 }
226
227 void MirroredOrigin::setMirrors(std::vector<OriginEndpoint> mirrors)
228 {
229 clearMirrors();
230 for ( auto &m : mirrors )
231 addMirror( std::move(m) );
232 }
233
234 void MirroredOrigin::clearMirrors()
235 {
236 _pimpl->_origins.clear();
237 }
238
239 std::string MirroredOrigin::scheme() const
240 {
241 return _pimpl->_authority.url().getScheme();
242 }
243
244 bool MirroredOrigin::schemeIsDownloading() const
245 {
246 return _pimpl->_authority.schemeIsDownloading();
247 }
248
249 uint MirroredOrigin::endpointCount() const
250 {
251 // authority is always accessible, even if its a invalid URL
252 return _pimpl->_origins.size() + 1;
253 }
254
255 const OriginEndpoint &MirroredOrigin::at(uint index) const
256 {
257 if ( index >= endpointCount() ) {
258 throw std::out_of_range( "OriginEndpoint index out of range." );
259 }
260 if ( index == 0 ) {
261 return _pimpl->_authority;
262 }
263
264 return _pimpl->_origins.at( index - 1 );
265 }
266
267 OriginEndpoint &MirroredOrigin::at(uint index)
268 {
269 if ( index >= endpointCount() ) {
270 throw std::out_of_range( "OriginEndpoint index out of range." );
271 }
272 if ( index == 0 ) {
273 return _pimpl->_authority;
274 }
275
276 return _pimpl->_origins.at( index - 1 );
277 }
278
279 struct MirroredOriginSet::Private
280 {
281 Private() {}
282 ~Private() = default;
283
284 Private *clone () const {
285 return new Private(*this);
286 }
287
288 std::optional<std::size_t> _dlIndex; //< set if there is a downloading MirroredOrigin
289 std::vector<MirroredOrigin> _origins;
290 };
291
292
293 MirroredOriginSet::MirroredOriginSet()
294 : _pimpl( new Private() )
295 {}
296
297 MirroredOriginSet::MirroredOriginSet( std::vector<OriginEndpoint> eps )
298 : MirroredOriginSet()
299 {
300 if ( eps.size() )
301 addEndpoints( std::move(eps) );
302 }
303
304 MirroredOriginSet::MirroredOriginSet(std::list<Url> urls)
305 : MirroredOriginSet()
306 {
307 for( auto &url: urls )
308 addEndpoint( std::move(url) );
309 }
310
311 MirroredOriginSet::MirroredOriginSet(std::vector<Url> urls)
312 : MirroredOriginSet()
313 {
314 for( auto &url: urls )
315 addEndpoint( std::move(url) );
316 }
317
318 const MirroredOrigin &MirroredOriginSet::at(size_type idx) const
319 {
320 return _pimpl->_origins.at(idx);
321 }
322
323 MirroredOrigin &MirroredOriginSet::at(size_type idx)
324 {
325 return _pimpl->_origins.at(idx);
326 }
327
328 std::ostream & operator<<( std::ostream & str, const MirroredOrigin & origin )
329 {
330 return dumpRange( str << "MirroredOrigin { authority: \"" << origin.authority() << "\", ",
331 origin.mirrors().begin(), origin.mirrors().end(), "mirrors: [", "\"", "\",\"", "\"", "]" )
332 << " }";
333 }
334
335 MirroredOriginSet::iterator MirroredOriginSet::findByUrl( const Url &url )
336 {
337 for ( auto i = begin(); i!=end(); i++ ) {
338 auto epI = std::find_if( i->begin (), i->end(), [&](const OriginEndpoint &ep){ return ep.url () == url; } );
339 if ( epI != i->end() )
340 return i;
341 }
342 return end();
343 }
344
345 MirroredOriginSet::const_iterator MirroredOriginSet::findByUrl( const Url &url ) const
346 {
347 for ( auto i = begin(); i!=end(); i++ ) {
348 auto epI = std::find_if( i->begin (), i->end(), [&](const OriginEndpoint &ep){ return ep.url () == url; } );
349 if ( epI != i->end() )
350 return i;
351 }
352 return end();
353 }
354
355 void MirroredOriginSet::addEndpoint( OriginEndpoint endpoint )
356 {
357 if ( !endpoint.url().schemeIsDownloading () ) {
358 _pimpl->_origins.push_back ( MirroredOrigin(std::move(endpoint), {} ) );
359 return;
360 }
361
362 if ( _pimpl->_dlIndex ) {
363 _pimpl->_origins.at(*_pimpl->_dlIndex).addMirror( std::move(endpoint) );
364 return;
365 }
366
367 // start a new origin
368 _pimpl->_origins.push_back ( MirroredOrigin(std::move(endpoint), {} ) );
369 _pimpl->_dlIndex = _pimpl->_origins.size() - 1;
370 }
371
372
373 void MirroredOriginSet::addEndpoints( std::vector<OriginEndpoint> endpoints )
374 {
375 for ( auto &ep : endpoints )
376 addEndpoint ( std::move(ep) );
377 }
378
379 bool MirroredOriginSet::empty() const
380 {
381 return _pimpl->_origins.empty ();
382 }
383
384 void MirroredOriginSet::clear()
385 {
386 _pimpl->_origins.clear();
387 _pimpl->_dlIndex.reset();
388 }
389
390 MirroredOriginSet::iterator MirroredOriginSet::begin()
391 {
392 return _pimpl->_origins.begin ();
393 }
394
395
396 MirroredOriginSet::iterator MirroredOriginSet::end()
397 {
398 return _pimpl->_origins.end ();
399 }
400
401
402 MirroredOriginSet::const_iterator MirroredOriginSet::begin() const
403 {
404 return _pimpl->_origins.begin ();
405 }
406
407
408 MirroredOriginSet::const_iterator MirroredOriginSet::end() const
409 {
410 return _pimpl->_origins.end ();
411 }
412
413 MirroredOriginSet::size_type MirroredOriginSet::size() const
414 {
415 return _pimpl->_origins.size ();
416 }
417
418 bool MirroredOriginSet::hasFallbackUrls() const
419 {
420 return ( size() == 1 && at( 0 ).endpointCount() > 1 ) || size() > 1;
421 }
422
423 std::ostream & operator<<( std::ostream & str, const MirroredOriginSet & origin )
424 {
425 return dumpRange( str, origin.begin(), origin.end(), "MirroredOriginSet {", " ", ", ", " ", "}" );
426 }
427
428}
LogTools.h
MIL
#define MIL
Definition Logger.h:100
WAR
#define WAR
Definition Logger.h:101
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:418
zypp::MirroredOriginSet::findByUrl
const_iterator findByUrl(const zypp::Url &url) const
Finds the MirroredOrigin that contains a specific URL.
Definition MirroredOrigin.cc:345
zypp::MirroredOriginSet::clear
void clear()
Definition MirroredOrigin.cc:384
zypp::MirroredOriginSet::at
const MirroredOrigin & at(size_type idx) const
Accesses the MirroredOrigin at a specific index.
Definition MirroredOrigin.cc:318
zypp::MirroredOriginSet::size
size_type size() const
Returns the number of MirroredOrigin objects in the set.
Definition MirroredOrigin.cc:413
zypp::MirroredOriginSet::end
iterator end()
Returns an iterator to the element following the last MirroredOrigin.
Definition MirroredOrigin.cc:396
zypp::MirroredOriginSet::MirroredOriginSet
MirroredOriginSet()
Definition MirroredOrigin.cc:293
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:390
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:379
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:355
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:244
zypp::MirroredOrigin::mirrors
const std::vector< OriginEndpoint > & mirrors() const
Definition MirroredOrigin.cc:197
zypp::MirroredOrigin::at
const OriginEndpoint & at(uint index) const
Definition MirroredOrigin.cc:255
zypp::MirroredOrigin::setAuthority
void setAuthority(OriginEndpoint newAuthority)
Definition MirroredOrigin.cc:163
zypp::MirroredOrigin::_pimpl
RWCOW_pointer< Private > _pimpl
Definition MirroredOrigin.h:298
zypp::MirroredOrigin::setMirrors
void setMirrors(std::vector< OriginEndpoint > mirrors)
Definition MirroredOrigin.cc:227
zypp::MirroredOrigin::MirroredOrigin
MirroredOrigin()
Definition MirroredOrigin.cc:148
zypp::MirroredOrigin::isValid
bool isValid() const
Definition MirroredOrigin.cc:202
zypp::MirroredOrigin::clearMirrors
void clearMirrors()
Definition MirroredOrigin.cc:234
zypp::MirroredOrigin::addMirror
bool addMirror(OriginEndpoint newMirror)
Definition MirroredOrigin.cc:207
zypp::MirroredOrigin::scheme
std::string scheme() const
Definition MirroredOrigin.cc:239
zypp::MirroredOrigin::authority
const OriginEndpoint & authority() const
Definition MirroredOrigin.cc:192
zypp::MirroredOrigin::endpointCount
uint endpointCount() const
Definition MirroredOrigin.cc:249
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:532
zypp::Url::schemeIsDownloading
static bool schemeIsDownloading(const std::string &scheme_r)
http https ftp sftp tftp
Definition Url.cc:493
std
Definition ansi.h:855
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 CodePitfalls.doc:2
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:404
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:280
zypp::MirroredOriginSet::Private::_origins
std::vector< MirroredOrigin > _origins
Definition MirroredOrigin.cc:289
zypp::MirroredOriginSet::Private::~Private
~Private()=default
zypp::MirroredOriginSet::Private::clone
Private * clone() const
Definition MirroredOrigin.cc:284
zypp::MirroredOriginSet::Private::Private
Private()
Definition MirroredOrigin.cc:281
zypp::MirroredOriginSet::Private::_dlIndex
std::optional< std::size_t > _dlIndex
Definition MirroredOrigin.cc:288
zypp::MirroredOrigin::Private
Definition MirroredOrigin.cc:136
zypp::MirroredOrigin::Private::~Private
~Private()=default
zypp::MirroredOrigin::Private::_authority
OriginEndpoint _authority
Definition MirroredOrigin.cc:144
zypp::MirroredOrigin::Private::clone
Private * clone() const
Definition MirroredOrigin.cc:140
zypp::MirroredOrigin::Private::_origins
std::vector< OriginEndpoint > _origins
Definition MirroredOrigin.cc:145
zypp::MirroredOrigin::Private::Private
Private()=default
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