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 bool tryLoadPoolSnapshot();
373
374 public:
375
377
378 expected<void> removeRepository( const RepoInfo & info, ProgressObserverRef myProgress = nullptr );
379
380 expected<RepoInfo> modifyRepository( const std::string & alias, const RepoInfo & newinfo_r, ProgressObserverRef myProgress = nullptr );
381
382 expected<RepoInfo> getRepositoryInfo( const std::string & alias );
384
385 expected<RefreshCheckStatus> checkIfToRefreshMetadata( const RepoInfo & info, const zypp::MirroredOrigin &origin, RawMetadataRefreshPolicy policy );
386
390
408 expected<void> refreshMetadata( const RepoInfo & info, RawMetadataRefreshPolicy policy, ProgressObserverRef myProgress = nullptr );
409
410 std::vector<std::pair<RepoInfo, expected<void> > > refreshMetadata(std::vector<RepoInfo> infos, RawMetadataRefreshPolicy policy, ProgressObserverRef myProgress = nullptr );
411
413
414 expected<void> buildCache( const RepoInfo & info, CacheBuildPolicy policy, ProgressObserverRef myProgress = nullptr );
415
419 expected<RepoInfo> addRepository( const RepoInfo & info, const ProgressObserverRef myProgress = nullptr, const zypp::TriBool & forcedProbe = zypp::indeterminate );
420
421 expected<void> addRepositories( const zypp::Url & url, ProgressObserverRef myProgress = nullptr );
422
423 public:
424 bool serviceEmpty() const { return _services.empty(); }
425 ServiceSizeType serviceSize() const { return _services.size(); }
426 ServiceConstIterator serviceBegin() const { return _services.begin(); }
427 ServiceConstIterator serviceEnd() const { return _services.end(); }
428
429 bool hasService( const std::string & alias ) const
430 { return foundAliasIn( alias, _services ); }
431
432 ServiceInfo getService( const std::string & alias ) const
433 {
435 return it == _services.end() ? ServiceInfo::noService : *it;
436 }
437
438 public:
439
441
442 expected<void> addService( const ServiceInfo & service );
443 expected<void> addService( const std::string & alias, const zypp::Url & url )
444 { return addService( ServiceInfo( alias, url ) ); }
445
446 expected<void> removeService( const std::string & alias );
448 { return removeService( service.alias() ); }
449
450 expected<void> refreshService( const std::string & alias, const RefreshServiceOptions & options_r );
452 { return refreshService( service.alias(), options_r ); }
453
455
456 expected<void> modifyService( const std::string & oldAlias, const ServiceInfo & newService );
457
459
461 {
462 using namespace zyppng::operators;
464 | and_then( [&]( zypp::Pathname base ){
465 try {
467 status.saveToCookieFile( base / "cookie" );
468 } catch ( const zypp::Exception &e ) {
469 return expected<void>::error( std::make_exception_ptr(e) );
470 }
472 });
473 }
474
480
481 template<typename OutputIterator>
482 void getRepositoriesInService( const std::string & alias, OutputIterator out ) const
483 {
484 MatchServiceAlias filter( alias );
485 std::copy( boost::make_filter_iterator( filter, repos().begin(), repos().end() ),
486 boost::make_filter_iterator( filter, repos().end(), repos().end() ),
487 out);
488 }
489
490 zypp::Pathname generateNonExistingName( const zypp::Pathname & dir, const std::string & basefilename ) const;
491
492 std::string generateFilename( const RepoInfo & info ) const
493 { return filenameFromAlias( info.alias(), "repo" ); }
494
495 std::string generateFilename( const ServiceInfo & info ) const
496 { return filenameFromAlias( info.alias(), "service" ); }
497
498
499 protected:
500 expected<void> saveService( ServiceInfo & service ) const;
502
503 protected:
506
507 public:
508 const RepoSet & repos() const { return _reposX; }
509 RepoSet & reposManip() { if ( ! _reposDirty ) _reposDirty = true; return _reposX; }
510
511 public:
514
515 protected:
524 };
525}
526
527#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
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)
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
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
Generate a non existing filename in a directory, using a base name.
ContextRefType _zyppContext
expected< void > removeService(const ServiceInfo &service)
PluginRepoverification pluginRepoverification() const
static expected< void > touchIndexFile(const RepoInfo &info, const RepoManagerOptions &options)
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
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
nullptr CURL handle void p CURLversion nullptr struct curl_slist list CURLM CURLM_INTERNAL_ERROR CURLM CURL CURLM_INTERNAL_ERROR CURLM curl_socket_t int int CURLM_INTERNAL_ERROR CURLINFO info
Definition curl_dl.cc:117
nullptr CURL handle void * p
Definition curl_dl.cc:99
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
detail::collect_helper collect()
Definition expected.h:735
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
zypp::ServiceInfo ServiceInfo
Definition repomanager.h:41
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
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
#define ZYPP_FWD_DECL_TYPE_WITH_REFS(T)
Definition zyppglobal.h:119