libzypp 17.38.3
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
40 using RepoInfoList = zypp::RepoInfoList;
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
153 expected<void> assert_urls( const RepoInfo & info );
154
155 inline expected<void> assert_url( const ServiceInfo & info )
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
328 expected<RepoStatus> metadataStatus( const RepoInfo & info ) const;
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
340 expected<bool> isCached( const RepoInfo & info ) const
341 {
342 using namespace zyppng::operators;
343 return solv_path_for_repoinfo( _options, info )
344 | and_then( [&]( zypp::Pathname solvPath) { return make_expected_success( zypp::PathInfo(solvPath / "solv").isExist()); } );
345 }
346
348 { return cacheStatus( info, _options ); }
349
351 {
352 using namespace zyppng::operators;
353 return solv_path_for_repoinfo( options, info )
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
362
363 expected<void> removeRepository( const RepoInfo & info, ProgressObserverRef myProgress = nullptr );
364
365 expected<RepoInfo> modifyRepository( const std::string & alias, const RepoInfo & newinfo_r, ProgressObserverRef myProgress = nullptr );
366
367 expected<RepoInfo> getRepositoryInfo( const std::string & alias );
369
370 expected<RefreshCheckStatus> checkIfToRefreshMetadata( const RepoInfo & info, const zypp::MirroredOrigin &origin, RawMetadataRefreshPolicy policy );
371
375
393 expected<void> refreshMetadata( const RepoInfo & info, RawMetadataRefreshPolicy policy, ProgressObserverRef myProgress = nullptr );
394
395 std::vector<std::pair<RepoInfo, expected<void> > > refreshMetadata(std::vector<RepoInfo> infos, RawMetadataRefreshPolicy policy, ProgressObserverRef myProgress = nullptr );
396
398
399 expected<void> buildCache( const RepoInfo & info, CacheBuildPolicy policy, ProgressObserverRef myProgress = nullptr );
400
404 expected<RepoInfo> addRepository( const RepoInfo & info, const ProgressObserverRef myProgress = nullptr, const zypp::TriBool & forcedProbe = zypp::indeterminate );
405
406 expected<void> addRepositories( const zypp::Url & url, ProgressObserverRef myProgress = nullptr );
407
408 public:
409 bool serviceEmpty() const { return _services.empty(); }
410 ServiceSizeType serviceSize() const { return _services.size(); }
411 ServiceConstIterator serviceBegin() const { return _services.begin(); }
412 ServiceConstIterator serviceEnd() const { return _services.end(); }
413
414 bool hasService( const std::string & alias ) const
415 { return foundAliasIn( alias, _services ); }
416
417 ServiceInfo getService( const std::string & alias ) const
418 {
420 return it == _services.end() ? ServiceInfo::noService : *it;
421 }
422
423 public:
424
426
427 expected<void> addService( const ServiceInfo & service );
428 expected<void> addService( const std::string & alias, const zypp::Url & url )
429 { return addService( ServiceInfo( alias, url ) ); }
430
431 expected<void> removeService( const std::string & alias );
433 { return removeService( service.alias() ); }
434
435 expected<void> refreshService( const std::string & alias, const RefreshServiceOptions & options_r );
437 { return refreshService( service.alias(), options_r ); }
438
440
441 expected<void> modifyService( const std::string & oldAlias, const ServiceInfo & newService );
442
444
445 expected<void> setCacheStatus( const RepoInfo & info, const RepoStatus & status )
446 {
447 using namespace zyppng::operators;
448 return solv_path_for_repoinfo( _options, info )
449 | and_then( [&]( zypp::Pathname base ){
450 try {
452 status.saveToCookieFile( base / "cookie" );
453 } catch ( const zypp::Exception &e ) {
454 return expected<void>::error( std::make_exception_ptr(e) );
455 }
457 });
458 }
459
465
466 template<typename OutputIterator>
467 void getRepositoriesInService( const std::string & alias, OutputIterator out ) const
468 {
469 MatchServiceAlias filter( alias );
470 std::copy( boost::make_filter_iterator( filter, repos().begin(), repos().end() ),
471 boost::make_filter_iterator( filter, repos().end(), repos().end() ),
472 out);
473 }
474
475 zypp::Pathname generateNonExistingName( const zypp::Pathname & dir, const std::string & basefilename ) const;
476
477 std::string generateFilename( const RepoInfo & info ) const
478 { return filenameFromAlias( info.alias(), "repo" ); }
479
480 std::string generateFilename( const ServiceInfo & info ) const
481 { return filenameFromAlias( info.alias(), "service" ); }
482
483
484 protected:
485 expected<void> saveService( ServiceInfo & service ) const;
487
488 protected:
491
492 public:
493 const RepoSet & repos() const { return _reposX; }
494 RepoSet & reposManip() { if ( ! _reposDirty ) _reposDirty = true; return _reposX; }
495
496 public:
499
500 protected:
507 };
508}
509
510#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
Pathname metadataPath() const
Path where this repo metadata was read from.
Definition RepoInfo.cc:777
bool usesAutoMetadataPaths() const
Whether metadataPath uses AUTO% setup.
Definition RepoInfo.cc:786
static const RepoInfo noRepo
Represents no Repository (one with an empty alias).
Definition RepoInfo.h:85
Pathname path() const
Repository path.
Definition RepoInfo.cc:822
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:825
Pathname packagesPath() const
packagesPath Checks if the effective user is allowed to write into the system package cache.
Definition RepoInfo.cc:783
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:516
Wrapper class for stat/lstat.
Definition PathInfo.h:226
Pathname dirname() const
Return all but the last component od this path.
Definition Pathname.h:133
bool empty() const
Test for an empty path.
Definition Pathname.h:117
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...
expected< void > refreshService(const ServiceInfo &service, const RefreshServiceOptions &options_r)
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
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
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
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
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
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
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:126