libzypp 17.36.1
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;
42
47
50
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 }
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 }
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() ); }
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
142
143 bool collect( const RepoInfo &repo );
144
146 std::string targetDistro;
147 };
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
223
224 bool operator()( const ServiceInfo & service_r ) const
225 {
226 _services.insert( service_r );
227 return true;
228 }
229
230 private:
232 };
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:
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
313 const RepoManagerOptions &options() const;
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
337 expected<RepoStatus> metadataStatus( const RepoInfo & info ) const;
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 )
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
379 expected<RefreshCheckStatus> checkIfToRefreshMetadata( const RepoInfo & info, const zypp::Url & url, RawMetadataRefreshPolicy policy );
380
398 expected<void> refreshMetadata( const RepoInfo & info, RawMetadataRefreshPolicy policy, ProgressObserverRef myProgress = nullptr );
399
400 std::vector<std::pair<RepoInfo, expected<void> > > refreshMetadata(std::vector<RepoInfo> infos, RawMetadataRefreshPolicy policy, ProgressObserverRef myProgress = nullptr );
401
402 expected<zypp::repo::RepoType> probe( const zypp::Url & url, const zypp::Pathname & path = zypp::Pathname() ) const;
403
404 expected<void> buildCache( const RepoInfo & info, CacheBuildPolicy policy, ProgressObserverRef myProgress = nullptr );
405
409 expected<RepoInfo> addRepository( const RepoInfo & info, ProgressObserverRef myProgress = nullptr );
410
411 expected<void> addRepositories( const zypp::Url & url, ProgressObserverRef myProgress = nullptr );
412
413 public:
414 bool serviceEmpty() const { return _services.empty(); }
415 ServiceSizeType serviceSize() const { return _services.size(); }
416 ServiceConstIterator serviceBegin() const { return _services.begin(); }
417 ServiceConstIterator serviceEnd() const { return _services.end(); }
418
419 bool hasService( const std::string & alias ) const
420 { return foundAliasIn( alias, _services ); }
421
422 ServiceInfo getService( const std::string & alias ) const
423 {
425 return it == _services.end() ? ServiceInfo::noService : *it;
426 }
427
428 public:
429
431
432 expected<void> addService( const ServiceInfo & service );
433 expected<void> addService( const std::string & alias, const zypp::Url & url )
434 { return addService( ServiceInfo( alias, url ) ); }
435
436 expected<void> removeService( const std::string & alias );
438 { return removeService( service.alias() ); }
439
440 expected<void> refreshService( const std::string & alias, const RefreshServiceOptions & options_r );
443
445
446 expected<void> modifyService( const std::string & oldAlias, const ServiceInfo & newService );
447
448 static expected<void> touchIndexFile( const RepoInfo & info, const RepoManagerOptions &options );
449
450 expected<void> setCacheStatus( const RepoInfo & info, const RepoStatus & status )
451 {
452 using namespace zyppng::operators;
453 return solv_path_for_repoinfo( _options, info )
454 | and_then( [&]( zypp::Pathname base ){
455 try {
457 status.saveToCookieFile( base / "cookie" );
458 } catch ( const zypp::Exception &e ) {
459 return expected<void>::error( std::make_exception_ptr(e) );
460 }
462 });
463 }
464
470
471 template<typename OutputIterator>
472 void getRepositoriesInService( const std::string & alias, OutputIterator out ) const
473 {
474 MatchServiceAlias filter( alias );
475 std::copy( boost::make_filter_iterator( filter, repos().begin(), repos().end() ),
476 boost::make_filter_iterator( filter, repos().end(), repos().end() ),
477 out);
478 }
479
480 zypp::Pathname generateNonExistingName( const zypp::Pathname & dir, const std::string & basefilename ) const;
481
482 std::string generateFilename( const RepoInfo & info ) const
483 { return filenameFromAlias( info.alias(), "repo" ); }
484
485 std::string generateFilename( const ServiceInfo & info ) const
486 { return filenameFromAlias( info.alias(), "service" ); }
487
488
489 protected:
490 expected<void> saveService( ServiceInfo & service ) const;
491 expected<void> touchIndexFile( const RepoInfo & info );
492
493 protected:
496
497 public:
498 const RepoSet & repos() const { return _reposX; }
499 RepoSet & reposManip() { if ( ! _reposDirty ) _reposDirty = true; return _reposX; }
500
501 public:
504
505 protected:
512 };
513}
514
515#endif //ZYPP_NG_REPOMANAGER_INCLUDED
Reference counted access to a Tp object calling a custom Dispose function when the last AutoDispose h...
Definition AutoDispose.h:95
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:596
static const RepoInfo noRepo
Represents no Repository (one with an empty alias).
Definition RepoInfo.h:85
Pathname path() const
Repository path.
Definition RepoInfo.cc:638
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:641
Pathname packagesPath() const
Path where this repo packages are cached.
Definition RepoInfo.cc:599
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
std::string escaped_alias() const
Same as alias(), just escaped in a way to be a valid file name.
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...
RepoSet::size_type RepoSizeType
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
ServiceInfo typedefs.
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)
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)
bool serviceEmpty() const
static expected< void > touchIndexFile(const RepoInfo &info, const RepoManagerOptions &options)
expected< RepoInfo > getRepositoryInfo(const std::string &alias)
expected< void > refreshService(const ServiceInfo &service, const RefreshServiceOptions &options_r)
expected< RepoInfo > modifyRepository(const std::string &alias, const RepoInfo &newinfo_r, ProgressObserverRef myProgress=nullptr)
void getRepositoriesInService(const std::string &alias, OutputIterator out) const
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()
ZyppContextRefType ContextRefType
expected< RepoInfo > addProbedRepository(RepoInfo info, zypp::repo::RepoType probedType)
expected< void > setCacheStatus(const RepoInfo &info, const RepoStatus &status)
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.
const RepoSet & repos() const
ServiceSet::size_type ServiceSizeType
expected< void > cleanCacheDirGarbage(ProgressObserverRef myProgress=nullptr)
expected< RefreshCheckStatus > checkIfToRefreshMetadata(const RepoInfo &info, const zypp::Url &url, RawMetadataRefreshPolicy policy)
expected< bool > isCached(const RepoInfo &info) const
expected< RepoInfo > addRepository(const RepoInfo &info, ProgressObserverRef myProgress=nullptr)
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)
RepoSet::const_iterator RepoConstIterator
expected< zypp::Pathname > metadataPath(const RepoInfo &info) const
RepoSizeType repoSize() const
PluginRepoverification pluginRepoverification() const
expected< void > addRepositories(const zypp::Url &url, ProgressObserverRef myProgress=nullptr)
PluginRepoverification _pluginRepoverification
ServiceSet::const_iterator ServiceConstIterator
ContextRefType zyppContext() const
RepoManagerOptions _options
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)
std::set< RepoInfo > RepoSet
RepoInfo typedefs.
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 ZyppContextRefType::element_type ContextType
RepoConstIterator repoEnd() const
zypp::RepoManagerFlags::RefreshServiceOptions RefreshServiceOptions
expected< void > refreshService(const std::string &alias, const RefreshServiceOptions &options_r)
const RepoManagerOptions & options() const
zypp::RepoManagerFlags::CacheBuildPolicy CacheBuildPolicy
ContextRefType _zyppContext
RepoSet & reposManip()
Functor collecting ServiceInfos into a ServiceSet.
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
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)
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
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:39
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.
Iterator findAlias(const std::string &alias_r, Iterator begin_r, Iterator end_r)
Find alias_r in repo/service container.
Definition repomanager.h:99
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: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
Simple callback to collect the results.
RepoCollector(std::string targetDistro_)
std::string targetDistro
Functor thats filter RepoInfo by service which it belongs to.
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: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