libzypp 17.37.17
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
25
27#include <zypp-core/base/DefaultIntegral>
31#include <zypp-core/zyppng/base/Base>
34
35namespace zyppng {
36
39 using RepoInfoList = zypp::RepoInfoList;
47
49 using SyncRepoManagerRef = RepoManagerRef<SyncContextRef>;
50
52 using AsyncRepoManagerRef = RepoManagerRef<ContextRef>;
53
55 inline bool isTmpRepo( const RepoInfo & info_r )
56 { return( info_r.filepath().empty() && info_r.usesAutoMetadataPaths() ); }
57
59 {
60 if ( info.alias().empty() )
62 // bnc #473834. Maybe we can match the alias against a regex to define
63 // and check for valid aliases
64 if ( info.alias()[0] == '.')
66 info, _("Repository alias cannot start with dot."))) );
67
69 }
70
72 if (info.alias().empty())
74 // bnc #473834. Maybe we can match the alias against a regex to define
75 // and check for valid aliases
76 if (info.alias()[0] == '.')
78 info, _("Service alias cannot start with dot."))));
79
81 }
82
84 template <class Iterator>
85 inline bool foundAliasIn( const std::string & alias_r, Iterator begin_r, Iterator end_r )
86 {
87 for_( it, begin_r, end_r )
88 if ( it->alias() == alias_r )
89 return true;
90 return false;
91 }
92
93 template <class Container>
94 inline bool foundAliasIn( const std::string & alias_r, const Container & cont_r )
95 { return foundAliasIn( alias_r, cont_r.begin(), cont_r.end() ); }
96
98 template <class Iterator>
99 inline Iterator findAlias( const std::string & alias_r, Iterator begin_r, Iterator end_r )
100 {
101 for_( it, begin_r, end_r )
102 if ( it->alias() == alias_r )
103 return it;
104 return end_r;
105 }
106
107 template <class Container>
108 inline typename Container::iterator findAlias( const std::string & alias_r, Container & cont_r )
109 { return findAlias( alias_r, cont_r.begin(), cont_r.end() ); }
110
111 template <class Container>
112 inline typename Container::const_iterator findAlias( const std::string & alias_r, const Container & cont_r )
113 { return findAlias( alias_r, cont_r.begin(), cont_r.end() ); }
114
115
117 std::string filenameFromAlias( const std::string & alias_r, const std::string & stem_r );
118
135 {
138
139 RepoCollector(std::string targetDistro_)
140 : targetDistro(std::move(targetDistro_))
141 {}
142
143 bool collect( const RepoInfo &repo );
144
146 std::string targetDistro;
147 };
148
149
156
158
159 expected<void> assert_urls( const RepoInfo & info );
160
161 inline expected<void> assert_url( const ServiceInfo & info )
162 {
163 if ( ! info.url().isValid() )
166 }
167
173 {
174 using namespace zyppng::operators;
175 return assert_alias(info) | and_then( [&](){ return make_expected_success( isTmpRepo( info ) ? info.metadataPath() : opt.repoRawCachePath / info.escaped_alias()); });
176 }
177
187 {
188 using namespace zyppng::operators;
189 return rawcache_path_for_repoinfo( opt, info ) | and_then( [&]( zypp::Pathname p ) { return make_expected_success( p / info.path() ); } );
190 }
191
196 {
197 using namespace zyppng::operators;
198 return assert_alias(info) |
199 and_then([&](){ return make_expected_success(isTmpRepo( info ) ? info.packagesPath() : opt.repoPackagesCachePath / info.escaped_alias()); });
200 }
201
206 {
207 using namespace zyppng::operators;
208 return assert_alias(info) |
209 and_then([&](){ return make_expected_success(isTmpRepo( info ) ? info.metadataPath().dirname() / "%SLV%" : opt.repoSolvCachePath / info.escaped_alias()); });
210 }
211
213
216 {
217 public:
218 using ServiceSet = std::set<ServiceInfo>;
219
221 : _services( services_r )
222 {}
223
224 bool operator()( const ServiceInfo & service_r ) const
225 {
226 _services.insert( service_r );
227 return true;
228 }
229
230 private:
232 };
233
234
236 bool autoPruneInDir( const zypp::Pathname & path_r );
237
246 template <typename ZyppContextRefType>
247 class RepoManager : public Base, private MaybeAsyncMixin<std::is_same_v<ZyppContextRefType, ContextRef>>
248 {
250 ZYPP_ENABLE_MAYBE_ASYNC_MIXIN( (std::is_same_v<ZyppContextRefType, ContextRef>) );
251
252 public:
253
254 using ContextRefType = ZyppContextRefType;
255 using ContextType = typename ZyppContextRefType::element_type;
256
259
260
262
266
267
269
270 template < typename ...Args >
272 using namespace zyppng::operators;
273 auto mgr = std::make_shared< RepoManager<ZyppContextRefType> >( private_constr_t{}, std::forward<Args>(args)... );
274 return mgr->initialize() | and_then( [mgr](){ return make_expected_success(mgr); } );
275 }
276
277 public:
278
283 {
284 public:
285 MatchServiceAlias( std::string alias_ ) : alias(std::move(alias_)) {}
286 bool operator()( const RepoInfo & info ) const
287 { return info.service() == alias; }
288 private:
289 std::string alias;
290 };
291
293 using ServiceSet = std::set<ServiceInfo>;
294 using ServiceConstIterator = ServiceSet::const_iterator;
295 using ServiceSizeType = ServiceSet::size_type;
296
298 using RepoSet = std::set<RepoInfo>;
299 using RepoConstIterator = RepoSet::const_iterator;
300 using RepoSizeType = RepoSet::size_type;
301
302
303 virtual ~RepoManager();
304
305 public:
306
308
310 return _zyppContext;
311 }
312
314
315 bool repoEmpty() const { return repos().empty(); }
316 RepoSizeType repoSize() const { return repos().size(); }
317 RepoConstIterator repoBegin() const { return repos().begin(); }
318 RepoConstIterator repoEnd() const { return repos().end(); }
319
320 bool hasRepo( const std::string & alias ) const
321 { return foundAliasIn( alias, repos() ); }
322
323 RepoInfo getRepo( const std::string & alias ) const
324 {
325 RepoConstIterator it( findAlias( alias, repos() ) );
326 return it == repos().end() ? RepoInfo::noRepo : *it;
327 }
328
329 public:
332
335
338
339 expected<void> cleanMetadata( const RepoInfo & info, ProgressObserverRef myProgress = nullptr );
340
341 expected<void> cleanPackages( const RepoInfo & info, ProgressObserverRef myProgress = nullptr , bool isAutoClean = false );
342
344
345 expected<void> cleanCacheDirGarbage( ProgressObserverRef myProgress = nullptr );
346
347 expected<void> cleanCache( const RepoInfo & info, ProgressObserverRef myProgress = nullptr );
348
349 expected<bool> isCached( const RepoInfo & info ) const
350 {
351 using namespace zyppng::operators;
352 return solv_path_for_repoinfo( _options, info )
353 | and_then( [&]( zypp::Pathname solvPath) { return make_expected_success( zypp::PathInfo(solvPath / "solv").isExist()); } );
354 }
355
357 { return cacheStatus( info, _options ); }
358
360 {
361 using namespace zyppng::operators;
362 return solv_path_for_repoinfo( options, info )
363 | and_then( [&]( zypp::Pathname solvPath) {
364 return make_expected_success ( RepoStatus::fromCookieFile(solvPath / "cookie") );
365 });
366 }
367
368 expected<void> loadFromCache( const RepoInfo & info, ProgressObserverRef myProgress = nullptr );
369
371
372 expected<void> removeRepository( const RepoInfo & info, ProgressObserverRef myProgress = nullptr );
373
374 expected<RepoInfo> modifyRepository( const std::string & alias, const RepoInfo & newinfo_r, ProgressObserverRef myProgress = nullptr );
375
376 expected<RepoInfo> getRepositoryInfo( const std::string & alias );
378
380
384
402 expected<void> refreshMetadata( const RepoInfo & info, RawMetadataRefreshPolicy policy, ProgressObserverRef myProgress = nullptr );
403
404 std::vector<std::pair<RepoInfo, expected<void> > > refreshMetadata(std::vector<RepoInfo> infos, RawMetadataRefreshPolicy policy, ProgressObserverRef myProgress = nullptr );
405
407
408 expected<void> buildCache( const RepoInfo & info, CacheBuildPolicy policy, ProgressObserverRef myProgress = nullptr );
409
413 expected<RepoInfo> addRepository( const RepoInfo & info, const ProgressObserverRef myProgress = nullptr, const zypp::TriBool & forcedProbe = zypp::indeterminate );
414
415 expected<void> addRepositories( const zypp::Url & url, ProgressObserverRef myProgress = nullptr );
416
417 public:
418 bool serviceEmpty() const { return _services.empty(); }
419 ServiceSizeType serviceSize() const { return _services.size(); }
420 ServiceConstIterator serviceBegin() const { return _services.begin(); }
421 ServiceConstIterator serviceEnd() const { return _services.end(); }
422
423 bool hasService( const std::string & alias ) const
424 { return foundAliasIn( alias, _services ); }
425
426 ServiceInfo getService( const std::string & alias ) const
427 {
429 return it == _services.end() ? ServiceInfo::noService : *it;
430 }
431
432 public:
433
435
437 expected<void> addService( const std::string & alias, const zypp::Url & url )
438 { return addService( ServiceInfo( alias, url ) ); }
439
440 expected<void> removeService( const std::string & alias );
442 { return removeService( service.alias() ); }
443
444 expected<void> refreshService( const std::string & alias, const RefreshServiceOptions & options_r );
446 { return refreshService( service.alias(), options_r ); }
447
449
450 expected<void> modifyService( const std::string & oldAlias, const ServiceInfo & newService );
451
453
454 expected<void> setCacheStatus( const RepoInfo & info, const RepoStatus & status )
455 {
456 using namespace zyppng::operators;
457 return solv_path_for_repoinfo( _options, info )
458 | and_then( [&]( zypp::Pathname base ){
459 try {
461 status.saveToCookieFile( base / "cookie" );
462 } catch ( const zypp::Exception &e ) {
463 return expected<void>::error( std::make_exception_ptr(e) );
464 }
466 });
467 }
468
474
475 template<typename OutputIterator>
476 void getRepositoriesInService( const std::string & alias, OutputIterator out ) const
477 {
478 MatchServiceAlias filter( alias );
479 std::copy( boost::make_filter_iterator( filter, repos().begin(), repos().end() ),
480 boost::make_filter_iterator( filter, repos().end(), repos().end() ),
481 out);
482 }
483
484 zypp::Pathname generateNonExistingName( const zypp::Pathname & dir, const std::string & basefilename ) const;
485
486 std::string generateFilename( const RepoInfo & info ) const
487 { return filenameFromAlias( info.alias(), "repo" ); }
488
489 std::string generateFilename( const ServiceInfo & info ) const
490 { return filenameFromAlias( info.alias(), "service" ); }
491
492
493 protected:
496
497 protected:
500
501 public:
502 const RepoSet & repos() const { return _reposX; }
503 RepoSet & reposManip() { if ( ! _reposDirty ) _reposDirty = true; return _reposX; }
504
505 public:
508
509 protected:
516 };
517}
518
519#endif //ZYPP_NG_REPOMANAGER_INCLUDED
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
Pathname metadataPath() const
Path where this repo metadata was read from.
Definition RepoInfo.cc:741
bool usesAutoMetadataPaths() const
Whether metadataPath uses AUTO% setup.
Definition RepoInfo.cc:747
static const RepoInfo noRepo
Represents no Repository (one with an empty alias).
Definition RepoInfo.h:85
Pathname path() const
Repository path.
Definition RepoInfo.cc:783
std::string service() const
Gets name of the service to which this repository belongs or empty string if it has been added manual...
Definition RepoInfo.cc:786
Pathname packagesPath() const
Path where this repo packages are cached.
Definition RepoInfo.cc:744
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
Url url() const
The service url.
Url manipulation class.
Definition Url.h:93
bool isValid() const
Verifies the Url.
Definition Url.cc:507
Wrapper class for stat/lstat.
Definition PathInfo.h:226
Pathname dirname() const
Return all but the last component od this path.
Definition Pathname.h:126
bool empty() const
Test for an empty path.
Definition Pathname.h:116
std::string escaped_alias() const
Same as alias(), just escaped in a way to be a valid file name.
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...
zypp::RepoManagerFlags::RefreshCheckStatus RefreshCheckStatus
ServiceInfo getService(const std::string &alias) const
expected< void > refreshServices(const RefreshServiceOptions &options_r)
static expected< std::shared_ptr< RepoManager< ZyppContextRefType > > > create(Args &&...args)
expected< RepoStatus > cacheStatus(const RepoInfo &info) const
std::set< ServiceInfo > ServiceSet
RepoInfo getRepo(const std::string &alias) const
expected< void > refreshMetadata(const RepoInfo &info, RawMetadataRefreshPolicy policy, ProgressObserverRef myProgress=nullptr)
Refresh local raw cache.
expected< void > modifyService(const std::string &oldAlias, const ServiceInfo &newService)
zypp::RepoManagerFlags::RefreshServiceBit RefreshServiceBit
static expected< RepoStatus > cacheStatus(const RepoInfo &info, const RepoManagerOptions &options)
expected< zypp::Pathname > packagesPath(const RepoInfo &info) const
ServiceConstIterator serviceBegin() const
expected< void > addService(const ServiceInfo &service)
expected< void > refreshGeoIp(const RepoInfo::url_set &urls)
expected< void > removeService(const std::string &alias)
ZYPP_ENABLE_MAYBE_ASYNC_MIXIN((std::is_same_v< ZyppContextRefType, ContextRef >))
expected< void > cleanPackages(const RepoInfo &info, ProgressObserverRef myProgress=nullptr, bool isAutoClean=false)
expected< void > removeService(const ServiceInfo &service)
expected< RefreshCheckStatus > checkIfToRefreshMetadata(const RepoInfo &info, const zypp::Url &url, RawMetadataRefreshPolicy policy)
bool serviceEmpty() const
static expected< void > touchIndexFile(const RepoInfo &info, const RepoManagerOptions &options)
expected< RepoInfo > getRepositoryInfo(const std::string &alias)
expected< RepoStatus > metadataStatus(const RepoInfo &info) const
expected< void > refreshService(const ServiceInfo &service, const RefreshServiceOptions &options_r)
expected< RepoInfo > getRepositoryInfo(const zypp::Url &url, const zypp::url::ViewOption &urlview)
expected< RepoInfo > modifyRepository(const std::string &alias, const RepoInfo &newinfo_r, ProgressObserverRef myProgress=nullptr)
void getRepositoriesInService(const std::string &alias, OutputIterator out) const
zypp::RepoManagerFlags::RawMetadataRefreshPolicy RawMetadataRefreshPolicy
ServiceConstIterator serviceEnd() const
static zypp::repo::RepoType probeCache(const zypp::Pathname &path_r)
Probe Metadata in a local cache directory.
expected< void > addService(const std::string &alias, const zypp::Url &url)
expected< void > init_knownServices()
expected< RepoInfo > addProbedRepository(RepoInfo info, zypp::repo::RepoType probedType)
expected< void > setCacheStatus(const RepoInfo &info, const RepoStatus &status)
const RepoSet & repos() const
ServiceSet::size_type ServiceSizeType
expected< void > cleanCacheDirGarbage(ProgressObserverRef myProgress=nullptr)
expected< bool > isCached(const RepoInfo &info) const
expected< zypp::repo::ServiceType > probeService(const zypp::Url &url) const
ServiceSizeType serviceSize() const
std::string generateFilename(const ServiceInfo &info) const
expected< void > initialize()
zypp::Pathname generateNonExistingName(const zypp::Pathname &dir, const std::string &basefilename) const
Generate a non existing filename in a directory, using a base name.
expected< void > buildCache(const RepoInfo &info, CacheBuildPolicy policy, ProgressObserverRef myProgress=nullptr)
expected< void > touchIndexFile(const RepoInfo &info)
expected< RefreshCheckStatus > checkIfToRefreshMetadata(const RepoInfo &info, const zypp::MirroredOrigin &origin, RawMetadataRefreshPolicy policy)
RepoSet::const_iterator RepoConstIterator
expected< zypp::Pathname > metadataPath(const RepoInfo &info) const
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.
RepoSizeType repoSize() const
PluginRepoverification pluginRepoverification() const
expected< void > addRepositories(const zypp::Url &url, ProgressObserverRef myProgress=nullptr)
PluginRepoverification _pluginRepoverification
expected< RepoInfo > addRepository(const RepoInfo &info, const ProgressObserverRef myProgress=nullptr, const zypp::TriBool &forcedProbe=zypp::indeterminate)
ServiceSet::const_iterator ServiceConstIterator
ContextRefType zyppContext() const
expected< void > init_knownRepositories()
expected< void > cleanCache(const RepoInfo &info, ProgressObserverRef myProgress=nullptr)
zypp::DefaultIntegral< bool, false > _reposDirty
expected< void > loadFromCache(const RepoInfo &info, ProgressObserverRef myProgress=nullptr)
expected< void > cleanMetadata(const RepoInfo &info, ProgressObserverRef myProgress=nullptr)
static expected< RepoStatus > metadataStatus(const RepoInfo &info, const RepoManagerOptions &options)
expected< void > removeRepository(const RepoInfo &info, ProgressObserverRef myProgress=nullptr)
bool hasService(const std::string &alias) const
RepoConstIterator repoBegin() const
expected< void > saveService(ServiceInfo &service) const
bool repoEmpty() const
bool hasRepo(const std::string &alias) const
std::string generateFilename(const RepoInfo &info) const
ZYPP_DECL_PRIVATE_CONSTR_ARGS(RepoManager, ZyppContextRefType zyppCtx, RepoManagerOptions opt)
typename SyncContextRef::element_type ContextType
RepoConstIterator repoEnd() const
std::vector< std::pair< RepoInfo, expected< void > > > refreshMetadata(std::vector< RepoInfo > infos, RawMetadataRefreshPolicy policy, ProgressObserverRef myProgress=nullptr)
zypp::RepoManagerFlags::RefreshServiceOptions RefreshServiceOptions
expected< void > refreshService(const std::string &alias, const RefreshServiceOptions &options_r)
const RepoManagerOptions & options() const
zypp::RepoManagerFlags::CacheBuildPolicy CacheBuildPolicy
RepoSet & reposManip()
ServiceCollector(ServiceSet &services_r)
bool operator()(const ServiceInfo &service_r) const
std::set< ServiceInfo > ServiceSet
static expected success(ConsParams &&...params)
Definition expected.h:115
boost::logic::tribool TriBool
3-state boolean logic (true, false and indeterminate).
Definition String.h:31
Definition Arch.h:364
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:324
Url details namespace.
Definition UrlBase.cc:58
detail::collect_helper collect()
Definition expected.h:650
bool isTmpRepo(const RepoInfo &info_r)
Whether repo is not under RM control and provides its own methadata paths.
Definition repomanager.h:55
expected< void > assert_url(const ServiceInfo &info)
expected< void > assert_urls(const RepoInfo &info)
RepoManager< SyncContextRef > SyncRepoManager
Definition repomanager.h:48
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:397
RepoManager< ContextRef > AsyncRepoManager
Definition repomanager.h:51
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:58
ResultType and_then(const expected< T, E > &exp, Function &&f)
Definition expected.h:423
zypp::ServiceInfo ServiceInfo
Definition repomanager.h:40
RepoManagerRef< SyncContextRef > SyncRepoManagerRef
Definition repomanager.h:49
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.
RepoManagerRef< ContextRef > AsyncRepoManagerRef
Definition repomanager.h:52
Iterator findAlias(const std::string &alias_r, Iterator begin_r, Iterator end_r)
Find alias_r in repo/service container.
Definition repomanager.h:99
zypp_private::repo::PluginRepoverification PluginRepoverification
Definition repomanager.h:42
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:85
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:39
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
bool operator()(const RepoInfo &info) const
#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
#define ZYPP_FWD_DECL_TEMPL_TYPE_WITH_REFS_ARG1(T, TArg1)
Definition zyppglobal.h:130
#define ZYPP_FWD_DECL_TYPE_WITH_REFS(T)
Definition zyppglobal.h:126