libzypp 17.38.15
repomanager.h
Go to the documentation of this file.
1/*---------------------------------------------------------------------\
2| ____ _ __ __ ___ |
3| |__ / \ / / . \ . \ |
4| / / \ V /| _/ _/ |
5| / /__ | | | | | | |
6| /_____||_| |_| |_| |
7| |
8\---------------------------------------------------------------------*/
12#ifndef ZYPP_NG_REPOMANAGER_INCLUDED
13#define ZYPP_NG_REPOMANAGER_INCLUDED
14
15#include <utility>
16#include <optional>
17
20#include <zypp/RepoStatus.h>
21
24
25#include <zypp/ng/context.h>
26
28#include <zypp-core/base/DefaultIntegral>
32#include <zypp-core/ng/base/Base>
35
36namespace zyppng {
37
44 ZYPP_FWD_DECL_TYPE_WITH_REFS( SyncContext );
47
49 inline bool isTmpRepo( const RepoInfo & info_r )
50 { return( info_r.filepath().empty() && info_r.usesAutoMetadataPaths() ); }
51
53 {
54 if ( info.alias().empty() )
56 // bnc #473834. Maybe we can match the alias against a regex to define
57 // and check for valid aliases
58 if ( info.alias()[0] == '.')
60 info, _("Repository alias cannot start with dot."))) );
61
63 }
64
66 if (info.alias().empty())
68 // bnc #473834. Maybe we can match the alias against a regex to define
69 // and check for valid aliases
70 if (info.alias()[0] == '.')
72 info, _("Service alias cannot start with dot."))));
73
75 }
76
78 template <class Iterator>
79 inline bool foundAliasIn( const std::string & alias_r, Iterator begin_r, Iterator end_r )
80 {
81 for_( it, begin_r, end_r )
82 if ( it->alias() == alias_r )
83 return true;
84 return false;
85 }
86
87 template <class Container>
88 inline bool foundAliasIn( const std::string & alias_r, const Container & cont_r )
89 { return foundAliasIn( alias_r, cont_r.begin(), cont_r.end() ); }
90
92 template <class Iterator>
93 inline Iterator findAlias( const std::string & alias_r, Iterator begin_r, Iterator end_r )
94 {
95 for_( it, begin_r, end_r )
96 if ( it->alias() == alias_r )
97 return it;
98 return end_r;
99 }
100
101 template <class Container>
102 inline typename Container::iterator findAlias( const std::string & alias_r, Container & cont_r )
103 { return findAlias( alias_r, cont_r.begin(), cont_r.end() ); }
104
105 template <class Container>
106 inline typename Container::const_iterator findAlias( const std::string & alias_r, const Container & cont_r )
107 { return findAlias( alias_r, cont_r.begin(), cont_r.end() ); }
108
109
111 std::string filenameFromAlias( const std::string & alias_r, const std::string & stem_r );
112
129 {
132
133 RepoCollector(std::string targetDistro_)
134 : targetDistro(std::move(targetDistro_))
135 {}
136
137 bool collect( const RepoInfo &repo );
138
140 std::string targetDistro;
141 };
142
143
150
152
154
156 {
157 if ( ! info.url().isValid() )
160 }
161
167 {
168 using namespace zyppng::operators;
169 return assert_alias(info) | and_then( [&](){ return make_expected_success( isTmpRepo( info ) ? info.metadataPath() : opt.repoRawCachePath / info.escaped_alias()); });
170 }
171
181 {
182 using namespace zyppng::operators;
183 return rawcache_path_for_repoinfo( opt, info ) | and_then( [&]( zypp::Pathname p ) { return make_expected_success( p / info.path() ); } );
184 }
185
190 {
191 using namespace zyppng::operators;
192 return assert_alias(info) |
193 and_then([&](){ return make_expected_success(isTmpRepo( info ) ? info.packagesPath() : opt.repoPackagesCachePath / info.escaped_alias()); });
194 }
195
200 {
201 using namespace zyppng::operators;
202 return assert_alias(info) |
203 and_then([&](){ return make_expected_success(isTmpRepo( info ) ? info.metadataPath().dirname() / "%SLV%" : opt.repoSolvCachePath / info.escaped_alias()); });
204 }
205
207
210 {
211 public:
212 using ServiceSet = std::set<ServiceInfo>;
213
215 : _services( services_r )
216 {}
217
218 bool operator()( const ServiceInfo & service_r ) const
219 {
220 _services.insert( service_r );
221 return true;
222 }
223
224 private:
226 };
227
228
230 bool autoPruneInDir( const zypp::Pathname & path_r );
231
240 class RepoManager : public Base
241 {
243 public:
244
245 using ContextRefType = ContextRef;
247
250
251
253
257
258
260
261 template < typename ...Args >
262 inline static expected<std::shared_ptr<RepoManager>> create ( Args && ...args ) {
263 using namespace zyppng::operators;
264 auto mgr = std::make_shared< RepoManager >( private_constr_t{}, std::forward<Args>(args)... );
265 return mgr->initialize() | and_then( [mgr](){ return make_expected_success(mgr); } );
266 }
267
268 public:
269
274 {
275 public:
276 MatchServiceAlias( std::string alias_ ) : alias(std::move(alias_)) {}
277 bool operator()( const RepoInfo & info ) const
278 { return info.service() == alias; }
279 private:
280 std::string alias;
281 };
282
284 using ServiceSet = std::set<ServiceInfo>;
285 using ServiceConstIterator = ServiceSet::const_iterator;
286 using ServiceSizeType = ServiceSet::size_type;
287
289 using RepoSet = std::set<RepoInfo>;
290 using RepoConstIterator = RepoSet::const_iterator;
291 using RepoSizeType = RepoSet::size_type;
292
293
294 virtual ~RepoManager();
295
296 public:
297
299
301 return _zyppContext;
302 }
303
304 const RepoManagerOptions &options() const;
305
306 bool repoEmpty() const { return repos().empty(); }
307 RepoSizeType repoSize() const { return repos().size(); }
308 RepoConstIterator repoBegin() const { return repos().begin(); }
309 RepoConstIterator repoEnd() const { return repos().end(); }
310
311 bool hasRepo( const std::string & alias ) const
312 { return foundAliasIn( alias, repos() ); }
313
314 RepoInfo getRepo( const std::string & alias ) const
315 {
316 RepoConstIterator it( findAlias( alias, repos() ) );
317 return it == repos().end() ? RepoInfo::noRepo : *it;
318 }
319
320 public:
323
326
329
330 expected<void> cleanMetadata( const RepoInfo & info, ProgressObserverRef myProgress = nullptr );
331
332 expected<void> cleanPackages( const RepoInfo & info, ProgressObserverRef myProgress = nullptr , bool isAutoClean = false );
333
334 static zypp::repo::RepoType probeCache( const zypp::Pathname & path_r );
335
336 expected<void> cleanCacheDirGarbage( ProgressObserverRef myProgress = nullptr );
337
338 expected<void> cleanCache( const RepoInfo & info, ProgressObserverRef myProgress = nullptr );
339
341 {
342 using namespace zyppng::operators;
344 | and_then( [&]( zypp::Pathname solvPath) { return make_expected_success( zypp::PathInfo(solvPath / "solv").isExist()); } );
345 }
346
349
351 {
352 using namespace zyppng::operators;
354 | and_then( [&]( zypp::Pathname solvPath) {
355 return make_expected_success ( RepoStatus::fromCookieFile(solvPath / "cookie") );
356 });
357 }
358
359 expected<void> loadFromCache( const RepoInfo & info, ProgressObserverRef myProgress = nullptr );
360
361 private:
364 void reservePoolIds();
365
368 std::string poolSnapshotCookie() const;
369
372
374 void cleanPoolSnapshot() const;
375
378 bool tryLoadPoolSnapshot();
379
380 public:
381
383
384 expected<void> removeRepository( const RepoInfo & info, ProgressObserverRef myProgress = nullptr );
385
386 expected<RepoInfo> modifyRepository( const std::string & alias, const RepoInfo & newinfo_r, ProgressObserverRef myProgress = nullptr );
387
388 expected<RepoInfo> getRepositoryInfo( const std::string & alias );
390
391 expected<RefreshCheckStatus> checkIfToRefreshMetadata( const RepoInfo & info, const zypp::MirroredOrigin &origin, RawMetadataRefreshPolicy policy );
392
396
414 expected<void> refreshMetadata( const RepoInfo & info, RawMetadataRefreshPolicy policy, ProgressObserverRef myProgress = nullptr );
415
416 std::vector<std::pair<RepoInfo, expected<void> > > refreshMetadata(std::vector<RepoInfo> infos, RawMetadataRefreshPolicy policy, ProgressObserverRef myProgress = nullptr );
417
419
420 expected<void> buildCache( const RepoInfo & info, CacheBuildPolicy policy, ProgressObserverRef myProgress = nullptr );
421
425 expected<RepoInfo> addRepository( const RepoInfo & info, const ProgressObserverRef myProgress = nullptr, const zypp::TriBool & forcedProbe = zypp::indeterminate );
426
427 expected<void> addRepositories( const zypp::Url & url, ProgressObserverRef myProgress = nullptr );
428
429 public:
430 bool serviceEmpty() const { return _services.empty(); }
431 ServiceSizeType serviceSize() const { return _services.size(); }
432 ServiceConstIterator serviceBegin() const { return _services.begin(); }
433 ServiceConstIterator serviceEnd() const { return _services.end(); }
434
435 bool hasService( const std::string & alias ) const
436 { return foundAliasIn( alias, _services ); }
437
438 ServiceInfo getService( const std::string & alias ) const
439 {
441 return it == _services.end() ? ServiceInfo::noService : *it;
442 }
443
444 public:
445
447
448 expected<void> addService( const ServiceInfo & service );
449 expected<void> addService( const std::string & alias, const zypp::Url & url )
450 { return addService( ServiceInfo( alias, url ) ); }
451
452 expected<void> removeService( const std::string & alias );
454 { return removeService( service.alias() ); }
455
456 expected<void> refreshService( const std::string & alias, const RefreshServiceOptions & options_r );
458 { return refreshService( service.alias(), options_r ); }
459
461
462 expected<void> modifyService( const std::string & oldAlias, const ServiceInfo & newService );
463
465
467 {
468 using namespace zyppng::operators;
470 | and_then( [&]( zypp::Pathname base ){
471 try {
473 status.saveToCookieFile( base / "cookie" );
474 } catch ( const zypp::Exception &e ) {
475 return expected<void>::error( std::make_exception_ptr(e) );
476 }
478 });
479 }
480
486
487 template<typename OutputIterator>
488 void getRepositoriesInService( const std::string & alias, OutputIterator out ) const
489 {
490 MatchServiceAlias filter( alias );
491 std::copy( boost::make_filter_iterator( filter, repos().begin(), repos().end() ),
492 boost::make_filter_iterator( filter, repos().end(), repos().end() ),
493 out);
494 }
495
496 zypp::Pathname generateNonExistingName( const zypp::Pathname & dir, const std::string & basefilename ) const;
497
498 std::string generateFilename( const RepoInfo & info ) const
499 { return filenameFromAlias( info.alias(), "repo" ); }
500
501 std::string generateFilename( const ServiceInfo & info ) const
502 { return filenameFromAlias( info.alias(), "service" ); }
503
504
505 protected:
506 expected<void> saveService( ServiceInfo & service ) const;
508
509 protected:
512
513 public:
514 const RepoSet & repos() const { return _reposX; }
515 RepoSet & reposManip() { if ( ! _reposDirty ) _reposDirty = true; return _reposX; }
516
517 public:
520
521 protected:
530 };
531}
532
533#endif //ZYPP_NG_REPOMANAGER_INCLUDED
#define for_(IT, BEG, END)
Convenient for-loops using iterator.
Definition Easy.h:27
#define ZYPP_EXCPT_PTR(EXCPT)
Drops a logline and returns Exception as a std::exception_ptr.
Definition Exception.h:463
#define _(MSG)
Definition Gettext.h:39
Integral type with defined initial value when default constructed.
Base class for Exception.
Definition Exception.h:153
Manages a data source characterized by an authoritative URL and a list of mirror URLs.
What is known about a repository.
Definition RepoInfo.h:72
bool usesAutoMetadataPaths() const
Whether metadataPath uses AUTO% setup.
Definition RepoInfo.cc:828
static const RepoInfo noRepo
Represents no Repository (one with an empty alias).
Definition RepoInfo.h:85
std::list< Url > url_set
Definition RepoInfo.h:108
Track changing files or directories.
Definition RepoStatus.h:41
static RepoStatus fromCookieFile(const Pathname &path)
Reads the status from a cookie file.
void saveToCookieFile(const Pathname &path_r) const
Save the status information to a cookie file.
Service data.
Definition ServiceInfo.h:37
ServiceInfo()
Default ctor creates noService.
Url manipulation class.
Definition Url.h:93
Wrapper class for stat/lstat.
Definition PathInfo.h:226
bool empty() const
Test for an empty path.
Definition Pathname.h:117
Pathname filepath() const
File where this repo was read from.
std::string alias() const
unique identifier for this source.
Thrown when the repo alias is found to be invalid.
thrown when it was impossible to determine an alias for this repo.
Thrown when the repo alias is found to be invalid.
Service without alias was used in an operation.
Service has no or invalid url defined.
Repository metadata verification beyond GPG.
The RepoManager class Provides knowledge and methods to maintain repo settings and metadata for a giv...
expected< void > refreshService(const ServiceInfo &service, const RefreshServiceOptions &options_r)
std::string poolSnapshotCookie() const
Cookie describing the enabled repos and their solv caches, empty if the pool snapshot cannot be used.
static expected< RepoStatus > cacheStatus(const RepoInfo &info, const RepoManagerOptions &options)
RepoSet::size_type RepoSizeType
RepoInfo getRepo(const std::string &alias) const
expected< void > cleanPackages(const RepoInfo &info, ProgressObserverRef myProgress=nullptr, bool isAutoClean=false)
static expected< void > touchIndexFile(const RepoInfo &info, const RepoManagerOptions &options)
ContextRef ContextRefType
expected< RefreshCheckStatus > checkIfToRefreshMetadata(const RepoInfo &info, const zypp::MirroredOrigin &origin, RawMetadataRefreshPolicy policy)
expected< RepoStatus > cacheStatus(const RepoInfo &info) const
expected< void > removeRepository(const RepoInfo &info, ProgressObserverRef myProgress=nullptr)
RepoSet::const_iterator RepoConstIterator
expected< bool > isCached(const RepoInfo &info) const
expected< void > refreshGeoIp(const RepoInfo::url_set &urls)
ZYPP_DECL_PRIVATE_CONSTR_ARGS(RepoManager, ContextRef zyppCtx, RepoManagerOptions opt)
expected< void > buildCache(const RepoInfo &info, CacheBuildPolicy policy, ProgressObserverRef myProgress=nullptr)
expected< RepoInfo > addProbedRepository(RepoInfo info, zypp::repo::RepoType probedType)
std::string generateFilename(const RepoInfo &info) const
zypp::Pathname poolSnapshotPath() const
Where the pool snapshot lives.
expected< void > init_knownServices()
ServiceInfo getService(const std::string &alias) const
ContextRefType zyppContext() const
bool repoEmpty() const
zypp::RepoManagerFlags::RefreshServiceOptions RefreshServiceOptions
expected< RepoInfo > modifyRepository(const std::string &alias, const RepoInfo &newinfo_r, ProgressObserverRef myProgress=nullptr)
zypp::DefaultIntegral< bool, false > _reposDirty
expected< void > cleanCacheDirGarbage(ProgressObserverRef myProgress=nullptr)
static expected< RepoStatus > metadataStatus(const RepoInfo &info, const RepoManagerOptions &options)
static expected< std::shared_ptr< RepoManager > > create(Args &&...args)
expected< void > setCacheStatus(const RepoInfo &info, const RepoStatus &status)
bool hasRepo(const std::string &alias) const
expected< void > saveService(ServiceInfo &service) const
expected< void > refreshMetadata(const RepoInfo &info, RawMetadataRefreshPolicy policy, ProgressObserverRef myProgress=nullptr)
Refresh local raw cache.
expected< RefreshCheckStatus > checkIfToRefreshMetadata(const RepoInfo &info, const zypp::Url &url, RawMetadataRefreshPolicy policy)
expected< RepoInfo > getRepositoryInfo(const std::string &alias)
expected< void > removeService(const std::string &alias)
expected< void > addService(const ServiceInfo &service)
zypp::RepoManagerFlags::RawMetadataRefreshPolicy RawMetadataRefreshPolicy
ServiceSet::const_iterator ServiceConstIterator
zypp::RepoManagerFlags::RefreshServiceBit RefreshServiceBit
Flags for tuning RefreshService.
expected< void > refreshService(const std::string &alias, const RefreshServiceOptions &options_r)
expected< void > loadFromCache(const RepoInfo &info, ProgressObserverRef myProgress=nullptr)
static zypp::repo::RepoType probeCache(const zypp::Pathname &path_r)
Probe Metadata in a local cache directory.
const RepoSet & repos() const
ServiceConstIterator serviceBegin() const
zypp::Pathname generateNonExistingName(const zypp::Pathname &dir, const std::string &basefilename) const
ContextRefType _zyppContext
expected< void > removeService(const ServiceInfo &service)
expected< void > touchIndexFile(const RepoInfo &info)
PluginRepoverification pluginRepoverification() const
PluginRepoverification _pluginRepoverification
void getRepositoriesInService(const std::string &alias, OutputIterator out) const
RepoConstIterator repoEnd() const
expected< void > cleanCache(const RepoInfo &info, ProgressObserverRef myProgress=nullptr)
RepoConstIterator repoBegin() const
std::set< RepoInfo > RepoSet
RepoInfo typedefs.
expected< void > refreshServices(const RefreshServiceOptions &options_r)
expected< void > initialize()
std::set< ServiceInfo > ServiceSet
ServiceInfo typedefs.
bool serviceEmpty() const
expected< RepoInfo > addRepository(const RepoInfo &info, const ProgressObserverRef myProgress=nullptr, const zypp::TriBool &forcedProbe=zypp::indeterminate)
RepoSet & reposManip()
RepoManagerOptions _options
expected< void > init_knownRepositories()
expected< void > addService(const std::string &alias, const zypp::Url &url)
zypp::RepoManagerFlags::RefreshCheckStatus RefreshCheckStatus
expected< void > addRepositories(const zypp::Url &url, ProgressObserverRef myProgress=nullptr)
expected< zypp::Pathname > packagesPath(const RepoInfo &info) const
ServiceSizeType serviceSize() const
int _poolSnapshotState
0 unknown, 1 mapped, -1 unusable
expected< void > cleanMetadata(const RepoInfo &info, ProgressObserverRef myProgress=nullptr)
RepoSizeType repoSize() const
expected< void > modifyService(const std::string &oldAlias, const ServiceInfo &newService)
expected< zypp::Pathname > metadataPath(const RepoInfo &info) const
bool tryLoadPoolSnapshot()
Try to map the pool snapshot on the first loadFromCache, and arm writing a fresh one after a normal l...
void reservePoolIds()
Peek at the solv files of all enabled repos and size the pool's id hashes once for their sum,...
ServiceConstIterator serviceEnd() const
bool hasService(const std::string &alias) const
const RepoManagerOptions & options() const
void cleanPoolSnapshot() const
Remove the pool snapshot and any leftover of an interrupted write.
zypp::RepoManagerFlags::CacheBuildPolicy CacheBuildPolicy
expected< zypp::repo::RepoType > probe(const zypp::MirroredOrigin &origin, const zypp::Pathname &path=zypp::Pathname()) const
Probe the metadata type of a repository located at url.
expected< zypp::repo::ServiceType > probeService(const zypp::Url &url) const
ServiceSet::size_type ServiceSizeType
zypp::DefaultIntegral< bool, false > _poolIdsReserved
std::string generateFilename(const ServiceInfo &info) const
ServiceCollector(ServiceSet &services_r)
bool operator()(const ServiceInfo &service_r) const
std::set< ServiceInfo > ServiceSet
static expected success(ConsParams &&...params)
Definition expected.h:178
CURL handle void * p
Definition curl_dl.cc:142
CURL handle void p CURLversion age struct curl_slist list CURLM multi CURLM CURL handle CURLM curl_socket_t int int running_handles CURLINFO info
Definition curl_dl.cc:160
boost::logic::tribool TriBool
3-state boolean logic (true, false and indeterminate).
Definition String.h:31
Definition ansi.h:855
RefreshCheckStatus
Possibly return state of RepoManager::checkIfToRefreshMetadata function.
RefreshServiceFlags RefreshServiceOptions
Options tuning RefreshService.
RefreshServiceBit
Flags for tuning RefreshService.
boost::noncopyable NonCopyable
Ensure derived classes cannot be copied.
Definition NonCopyable.h:26
int assert_dir(const Pathname &path, unsigned mode)
Like 'mkdir -p'.
Definition PathInfo.cc:338
Url details namespace.
Definition UrlBase.cc:58
std::list< RepoInfo > RepoInfoList
relates: RepoInfo
Definition RepoInfo.h:664
ZYPP_FWD_DECL_TYPE_WITH_REFS(EventDispatcher)
bool isTmpRepo(const RepoInfo &info_r)
Whether repo is not under RM control and provides its own methadata paths.
Definition repomanager.h:49
expected< void > assert_url(const ServiceInfo &info)
expected< void > assert_urls(const RepoInfo &info)
std::string filenameFromAlias(const std::string &alias_r, const std::string &stem_r)
Generate a related filename from a repo/service infos alias.
static expected< std::decay_t< Type >, Err > make_expected_success(Type &&t)
Definition expected.h:470
expected< zypp::Pathname > rawcache_path_for_repoinfo(const RepoManagerOptions &opt, const RepoInfo &info)
Calculates the raw cache path for a repository, this is usually /var/cache/zypp/alias.
expected< void > assert_alias(const RepoInfo &info)
Definition repomanager.h:52
ResultType and_then(const expected< T, E > &exp, Function &&f)
Definition expected.h:520
expected< zypp::Pathname > solv_path_for_repoinfo(const RepoManagerOptions &opt, const RepoInfo &info)
Calculates the solv cache path for a repository.
expected< std::list< RepoInfo > > repositories_in_file(const zypp::Pathname &file)
Reads RepoInfo's from a repo file.
Iterator findAlias(const std::string &alias_r, Iterator begin_r, Iterator end_r)
Find alias_r in repo/service container.
Definition repomanager.h:93
std::enable_if_t<!std::is_same_v< void, T >, expected< Container< T >, E > > collect(Container< expected< T, E >, CArgs... > &&in)
Definition expected.h:586
zypp_private::repo::PluginRepoverification PluginRepoverification
Definition repomanager.h:43
bool foundAliasIn(const std::string &alias_r, Iterator begin_r, Iterator end_r)
Check if alias_r is present in repo/service container.
Definition repomanager.h:79
expected< zypp::Pathname > packagescache_path_for_repoinfo(const RepoManagerOptions &opt, const RepoInfo &info)
Calculates the packages cache path for a repository.
expected< zypp::Pathname > rawproductdata_path_for_repoinfo(const RepoManagerOptions &opt, const RepoInfo &info)
Calculates the raw product metadata path for a repository, this is inside the raw cache dir,...
zypp::RepoInfoList RepoInfoList
Definition repomanager.h:40
bool autoPruneInDir(const zypp::Pathname &path_r)
bsc#1204956: Tweak to prevent auto pruning package caches.
Repo manager settings.
Repository type enumeration.
Definition RepoType.h:29
Url::asString() view options.
Definition UrlBase.h:41
RepoCollector(std::string targetDistro_)
std::string targetDistro
Functor thats filter RepoInfo by service which it belongs to.
bool operator()(const RepoInfo &info) const