libzypp 17.35.16
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
19#include <zypp/RepoStatus.h>
20
24
26#include <zypp-core/base/DefaultIntegral>
30#include <zypp-core/zyppng/base/Base>
33
34namespace zyppng {
35
38 using RepoInfoList = zypp::RepoInfoList;
41
46
48 using SyncRepoManagerRef = RepoManagerRef<SyncContextRef>;
49
51 using AsyncRepoManagerRef = RepoManagerRef<ContextRef>;
52
54 inline bool isTmpRepo( const RepoInfo & info_r )
55 { return( info_r.filepath().empty() && info_r.usesAutoMetadataPaths() ); }
56
58 {
59 if ( info.alias().empty() )
61 // bnc #473834. Maybe we can match the alias against a regex to define
62 // and check for valid aliases
63 if ( info.alias()[0] == '.')
65 info, _("Repository alias cannot start with dot."))) );
66
68 }
69
71 if (info.alias().empty())
73 // bnc #473834. Maybe we can match the alias against a regex to define
74 // and check for valid aliases
75 if (info.alias()[0] == '.')
77 info, _("Service alias cannot start with dot."))));
78
80 }
81
83 template <class Iterator>
84 inline bool foundAliasIn( const std::string & alias_r, Iterator begin_r, Iterator end_r )
85 {
86 for_( it, begin_r, end_r )
87 if ( it->alias() == alias_r )
88 return true;
89 return false;
90 }
91
92 template <class Container>
93 inline bool foundAliasIn( const std::string & alias_r, const Container & cont_r )
94 { return foundAliasIn( alias_r, cont_r.begin(), cont_r.end() ); }
95
97 template <class Iterator>
98 inline Iterator findAlias( const std::string & alias_r, Iterator begin_r, Iterator end_r )
99 {
100 for_( it, begin_r, end_r )
101 if ( it->alias() == alias_r )
102 return it;
103 return end_r;
104 }
105
106 template <class Container>
107 inline typename Container::iterator findAlias( const std::string & alias_r, Container & cont_r )
108 { return findAlias( alias_r, cont_r.begin(), cont_r.end() ); }
109
110 template <class Container>
111 inline typename Container::const_iterator findAlias( const std::string & alias_r, const Container & cont_r )
112 { return findAlias( alias_r, cont_r.begin(), cont_r.end() ); }
113
114
116 std::string filenameFromAlias( const std::string & alias_r, const std::string & stem_r );
117
134 {
137
138 RepoCollector(std::string targetDistro_)
139 : targetDistro(std::move(targetDistro_))
140 {}
141
142 bool collect( const RepoInfo &repo );
143
145 std::string targetDistro;
146 };
147
148
155
157
158 expected<void> assert_urls( const RepoInfo & info );
159
160 inline expected<void> assert_url( const ServiceInfo & info )
161 {
162 if ( ! info.url().isValid() )
165 }
166
172 {
173 using namespace zyppng::operators;
174 return assert_alias(info) | and_then( [&](){ return make_expected_success( isTmpRepo( info ) ? info.metadataPath() : opt.repoRawCachePath / info.escaped_alias()); });
175 }
176
186 {
187 using namespace zyppng::operators;
188 return rawcache_path_for_repoinfo( opt, info ) | and_then( [&]( zypp::Pathname p ) { return make_expected_success( p / info.path() ); } );
189 }
190
195 {
196 using namespace zyppng::operators;
197 return assert_alias(info) |
198 and_then([&](){ return make_expected_success(isTmpRepo( info ) ? info.packagesPath() : opt.repoPackagesCachePath / info.escaped_alias()); });
199 }
200
205 {
206 using namespace zyppng::operators;
207 return assert_alias(info) |
208 and_then([&](){ return make_expected_success(isTmpRepo( info ) ? info.metadataPath().dirname() / "%SLV%" : opt.repoSolvCachePath / info.escaped_alias()); });
209 }
210
212
215 {
216 public:
217 using ServiceSet = std::set<ServiceInfo>;
218
220 : _services( services_r )
221 {}
222
223 bool operator()( const ServiceInfo & service_r ) const
224 {
225 _services.insert( service_r );
226 return true;
227 }
228
229 private:
231 };
232
233
235 bool autoPruneInDir( const zypp::Pathname & path_r );
236
245 template <typename ZyppContextRefType>
246 class RepoManager : public Base, private MaybeAsyncMixin<std::is_same_v<ZyppContextRefType, ContextRef>>
247 {
249 ZYPP_ENABLE_MAYBE_ASYNC_MIXIN( (std::is_same_v<ZyppContextRefType, ContextRef>) );
250
251 public:
252
253 using ContextRefType = ZyppContextRefType;
254 using ContextType = typename ZyppContextRefType::element_type;
255
258
259
261
265
266
268
269 template < typename ...Args >
271 using namespace zyppng::operators;
272 auto mgr = std::make_shared< RepoManager<ZyppContextRefType> >( private_constr_t{}, std::forward<Args>(args)... );
273 return mgr->initialize() | and_then( [mgr](){ return make_expected_success(mgr); } );
274 }
275
276 public:
277
282 {
283 public:
284 MatchServiceAlias( std::string alias_ ) : alias(std::move(alias_)) {}
285 bool operator()( const RepoInfo & info ) const
286 { return info.service() == alias; }
287 private:
288 std::string alias;
289 };
290
292 using ServiceSet = std::set<ServiceInfo>;
293 using ServiceConstIterator = ServiceSet::const_iterator;
294 using ServiceSizeType = ServiceSet::size_type;
295
297 using RepoSet = std::set<RepoInfo>;
298 using RepoConstIterator = RepoSet::const_iterator;
299 using RepoSizeType = RepoSet::size_type;
300
301
302 virtual ~RepoManager();
303
304 public:
305
307
309 return _zyppContext;
310 }
311
313
314 bool repoEmpty() const { return repos().empty(); }
315 RepoSizeType repoSize() const { return repos().size(); }
316 RepoConstIterator repoBegin() const { return repos().begin(); }
317 RepoConstIterator repoEnd() const { return repos().end(); }
318
319 bool hasRepo( const std::string & alias ) const
320 { return foundAliasIn( alias, repos() ); }
321
322 RepoInfo getRepo( const std::string & alias ) const
323 {
324 RepoConstIterator it( findAlias( alias, repos() ) );
325 return it == repos().end() ? RepoInfo::noRepo : *it;
326 }
327
328 public:
331
334
337
338 expected<void> cleanMetadata( const RepoInfo & info, ProgressObserverRef myProgress = nullptr );
339
340 expected<void> cleanPackages( const RepoInfo & info, ProgressObserverRef myProgress = nullptr , bool isAutoClean = false );
341
343
344 expected<void> cleanCacheDirGarbage( ProgressObserverRef myProgress = nullptr );
345
346 expected<void> cleanCache( const RepoInfo & info, ProgressObserverRef myProgress = nullptr );
347
348 expected<bool> isCached( const RepoInfo & info ) const
349 {
350 using namespace zyppng::operators;
351 return solv_path_for_repoinfo( _options, info )
352 | and_then( [&]( zypp::Pathname solvPath) { return make_expected_success( zypp::PathInfo(solvPath / "solv").isExist()); } );
353 }
354
356 { return cacheStatus( info, _options ); }
357
359 {
360 using namespace zyppng::operators;
361 return solv_path_for_repoinfo( options, info )
362 | and_then( [&]( zypp::Pathname solvPath) {
363 return make_expected_success ( RepoStatus::fromCookieFile(solvPath / "cookie") );
364 });
365 }
366
367 expected<void> loadFromCache( const RepoInfo & info, ProgressObserverRef myProgress = nullptr );
368
370
371 expected<void> removeRepository( const RepoInfo & info, ProgressObserverRef myProgress = nullptr );
372
373 expected<RepoInfo> modifyRepository( const std::string & alias, const RepoInfo & newinfo_r, ProgressObserverRef myProgress = nullptr );
374
375 expected<RepoInfo> getRepositoryInfo( const std::string & alias );
377
379
397 expected<void> refreshMetadata( const RepoInfo & info, RawMetadataRefreshPolicy policy, ProgressObserverRef myProgress = nullptr );
398
399 std::vector<std::pair<RepoInfo, expected<void> > > refreshMetadata(std::vector<RepoInfo> infos, RawMetadataRefreshPolicy policy, ProgressObserverRef myProgress = nullptr );
400
402
403 expected<void> buildCache( const RepoInfo & info, CacheBuildPolicy policy, ProgressObserverRef myProgress = nullptr );
404
408 expected<RepoInfo> addRepository( const RepoInfo & info, ProgressObserverRef myProgress = nullptr );
409
410 expected<void> addRepositories( const zypp::Url & url, ProgressObserverRef myProgress = nullptr );
411
412 public:
413 bool serviceEmpty() const { return _services.empty(); }
414 ServiceSizeType serviceSize() const { return _services.size(); }
415 ServiceConstIterator serviceBegin() const { return _services.begin(); }
416 ServiceConstIterator serviceEnd() const { return _services.end(); }
417
418 bool hasService( const std::string & alias ) const
419 { return foundAliasIn( alias, _services ); }
420
421 ServiceInfo getService( const std::string & alias ) const
422 {
424 return it == _services.end() ? ServiceInfo::noService : *it;
425 }
426
427 public:
428
430
432 expected<void> addService( const std::string & alias, const zypp::Url & url )
433 { return addService( ServiceInfo( alias, url ) ); }
434
435 expected<void> removeService( const std::string & alias );
437 { return removeService( service.alias() ); }
438
439 expected<void> refreshService( const std::string & alias, const RefreshServiceOptions & options_r );
441 { return refreshService( service.alias(), options_r ); }
442
444
445 expected<void> modifyService( const std::string & oldAlias, const ServiceInfo & newService );
446
448
449 expected<void> setCacheStatus( const RepoInfo & info, const RepoStatus & status )
450 {
451 using namespace zyppng::operators;
452 return solv_path_for_repoinfo( _options, info )
453 | and_then( [&]( zypp::Pathname base ){
454 try {
456 status.saveToCookieFile( base / "cookie" );
457 } catch ( const zypp::Exception &e ) {
458 return expected<void>::error( std::make_exception_ptr(e) );
459 }
461 });
462 }
463
469
470 template<typename OutputIterator>
471 void getRepositoriesInService( const std::string & alias, OutputIterator out ) const
472 {
473 MatchServiceAlias filter( alias );
474 std::copy( boost::make_filter_iterator( filter, repos().begin(), repos().end() ),
475 boost::make_filter_iterator( filter, repos().end(), repos().end() ),
476 out);
477 }
478
479 zypp::Pathname generateNonExistingName( const zypp::Pathname & dir, const std::string & basefilename ) const;
480
481 std::string generateFilename( const RepoInfo & info ) const
482 { return filenameFromAlias( info.alias(), "repo" ); }
483
484 std::string generateFilename( const ServiceInfo & info ) const
485 { return filenameFromAlias( info.alias(), "service" ); }
486
487
488 protected:
491
492 protected:
495
496 public:
497 const RepoSet & repos() const { return _reposX; }
498 RepoSet & reposManip() { if ( ! _reposDirty ) _reposDirty = true; return _reposX; }
499
500 protected:
507 };
508}
509
510#endif //ZYPP_NG_REPOMANAGER_INCLUDED
Integral type with defined initial value when default constructed.
Base class for Exception.
Definition Exception.h:147
What is known about a repository.
Definition RepoInfo.h:72
Pathname metadataPath() const
Path where this repo metadata was read from.
Definition RepoInfo.cc:593
bool usesAutoMetadataPaths() const
Whether metadataPath uses AUTO% setup.
Definition RepoInfo.cc:599
static const RepoInfo noRepo
Represents no Repository (one with an empty alias).
Definition RepoInfo.h:85
Pathname path() const
Repository path.
Definition RepoInfo.cc:635
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:638
Pathname packagesPath() const
Path where this repo packages are cached.
Definition RepoInfo.cc:596
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:222
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...
Definition ZConfig.h:34
expected< RepoStatus > cacheStatus(const RepoInfo &info) const
void getRepositoriesInService(const std::string &alias, OutputIterator out) const
expected< void > refreshServices(const RefreshServiceOptions &options_r)
expected< void > addService(const std::string &alias, const zypp::Url &url)
static expected< RepoStatus > cacheStatus(const RepoInfo &info, const RepoManagerOptions &options)
RepoInfo getRepo(const std::string &alias) const
bool serviceEmpty() 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_ENABLE_MAYBE_ASYNC_MIXIN((std::is_same_v< ZyppContextRefType, ContextRef >))
ServiceConstIterator serviceBegin() const
RepoConstIterator repoBegin() const
std::string generateFilename(const ServiceInfo &info) const
expected< zypp::Pathname > metadataPath(const RepoInfo &info) const
const RepoSet & repos() const
expected< void > addService(const ServiceInfo &service)
expected< void > refreshGeoIp(const RepoInfo::url_set &urls)
expected< void > removeService(const std::string &alias)
expected< void > cleanPackages(const RepoInfo &info, ProgressObserverRef myProgress=nullptr, bool isAutoClean=false)
ServiceSizeType serviceSize() const
std::string generateFilename(const RepoInfo &info) 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< RepoInfo > getRepositoryInfo(const zypp::Url &url, const zypp::url::ViewOption &urlview)
RepoSet::const_iterator RepoConstIterator
expected< RepoInfo > modifyRepository(const std::string &alias, const RepoInfo &newinfo_r, ProgressObserverRef myProgress=nullptr)
ContextRefType zyppContext() const
static zypp::repo::RepoType probeCache(const zypp::Pathname &path_r)
Probe Metadata in a local cache directory.
expected< void > init_knownServices()
expected< void > refreshService(const ServiceInfo &service, const RefreshServiceOptions &options_r)
expected< RepoInfo > addProbedRepository(RepoInfo info, zypp::repo::RepoType probedType)
expected< zypp::repo::RepoType > probe(const zypp::Url &url, const zypp::Pathname &path=zypp::Pathname()) const
Probe the metadata type of a repository located at url.
RepoSizeType repoSize() const
expected< void > cleanCacheDirGarbage(ProgressObserverRef myProgress=nullptr)
expected< RefreshCheckStatus > checkIfToRefreshMetadata(const RepoInfo &info, const zypp::Url &url, RawMetadataRefreshPolicy policy)
bool hasRepo(const std::string &alias) const
std::set< ServiceInfo > ServiceSet
expected< RepoInfo > addRepository(const RepoInfo &info, ProgressObserverRef myProgress=nullptr)
RepoConstIterator repoEnd() const
expected< zypp::repo::ServiceType > probeService(const zypp::Url &url) const
bool hasService(const std::string &alias) 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< zypp::Pathname > packagesPath(const RepoInfo &info) const
zypp::RepoManagerFlags::RefreshServiceOptions RefreshServiceOptions
zypp::DefaultIntegral< bool, false > _reposDirty
ServiceInfo getService(const std::string &alias) const
zypp::RepoManagerFlags::RawMetadataRefreshPolicy RawMetadataRefreshPolicy
ZYPP_DECL_PRIVATE_CONSTR_ARGS(RepoManager, ZyppContextRefType zyppCtx, RepoManagerOptions opt)
expected< void > addRepositories(const zypp::Url &url, ProgressObserverRef myProgress=nullptr)
static expected< std::shared_ptr< RepoManager< ZyppContextRefType > > > create(Args &&...args)
expected< void > setCacheStatus(const RepoInfo &info, const RepoStatus &status)
zypp_private::repo::PluginRepoverification _pluginRepoverification
typename ZyppContextRefType::element_type ContextType
ServiceSet::size_type ServiceSizeType
expected< void > init_knownRepositories()
expected< void > cleanCache(const RepoInfo &info, ProgressObserverRef myProgress=nullptr)
expected< void > loadFromCache(const RepoInfo &info, ProgressObserverRef myProgress=nullptr)
expected< bool > isCached(const RepoInfo &info) const
expected< void > cleanMetadata(const RepoInfo &info, ProgressObserverRef myProgress=nullptr)
static expected< RepoStatus > metadataStatus(const RepoInfo &info, const RepoManagerOptions &options)
zypp::RepoManagerFlags::RefreshServiceBit RefreshServiceBit
expected< void > removeRepository(const RepoInfo &info, ProgressObserverRef myProgress=nullptr)
zypp::RepoManagerFlags::RefreshCheckStatus RefreshCheckStatus
expected< void > saveService(ServiceInfo &service) const
std::vector< std::pair< RepoInfo, expected< void > > > refreshMetadata(std::vector< RepoInfo > infos, RawMetadataRefreshPolicy policy, ProgressObserverRef myProgress=nullptr)
expected< void > refreshService(const std::string &alias, const RefreshServiceOptions &options_r)
const RepoManagerOptions & options() const
RepoSet & reposManip()
ServiceConstIterator serviceEnd() const
expected< void > removeService(const ServiceInfo &service)
ServiceSet::const_iterator ServiceConstIterator
bool repoEmpty() const
zypp::RepoManagerFlags::CacheBuildPolicy CacheBuildPolicy
ServiceCollector(ServiceSet &services_r)
bool operator()(const ServiceInfo &service_r) const
std::set< ServiceInfo > ServiceSet
static expected success(ConsParams &&...params)
Definition expected.h:115
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:54
expected< void > assert_url(const ServiceInfo &info)
expected< void > assert_urls(const RepoInfo &info)
RepoManager< SyncContextRef > SyncRepoManager
Definition repomanager.h:47
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:50
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:57
ResultType and_then(const expected< T, E > &exp, Function &&f)
Definition expected.h:423
zypp::ServiceInfo ServiceInfo
Definition repomanager.h:39
RepoManagerRef< SyncContextRef > SyncRepoManagerRef
Definition repomanager.h:48
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:51
Iterator findAlias(const std::string &alias_r, Iterator begin_r, Iterator end_r)
Find alias_r in repo/service container.
Definition repomanager.h:98
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:84
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:38
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:28
#define ZYPP_EXCPT_PTR(EXCPT)
Drops a logline and returns Exception as a std::exception_ptr.
Definition Exception.h:428
#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