1 /*---------------------------------------------------------------------\
3 | |__ / \ / / . \ . \ |
8 \---------------------------------------------------------------------*/
9 /** \file zypp/RepoManager.h
12 #ifndef ZYPP_REPOMANAGER_H
13 #define ZYPP_REPOMANAGER_H
18 #include "zypp/base/PtrTypes.h"
19 #include "zypp/base/Iterator.h"
20 #include "zypp/base/Flags.h"
22 #include "zypp/Pathname.h"
23 #include "zypp/ZConfig.h"
24 #include "zypp/RepoInfo.h"
25 #include "zypp/repo/RepoException.h"
26 #include "zypp/repo/RepoType.h"
27 #include "zypp/repo/ServiceType.h"
28 #include "zypp/ServiceInfo.h"
29 #include "zypp/RepoStatus.h"
30 #include "zypp/ProgressData.h"
32 ///////////////////////////////////////////////////////////////////
34 { /////////////////////////////////////////////////////////////////
37 * Parses \a repo_file and returns a list of \ref RepoInfo objects
38 * corresponding to repositories found within the file.
40 * \param repo_file Valid URL of the repo file.
41 * \return found list<RepoInfo>
43 * \throws MediaException If the access to the url fails
44 * \throws ParseException If the file parsing fails
45 * \throws Exception On other errors.
47 std::list<RepoInfo> readRepoFile(const Url & repo_file);
50 * Repo manager settings.
51 * Settings default to ZYpp global settings.
53 struct RepoManagerOptions
55 /** Default ctor following \ref ZConfig global settings.
56 * If an optional \c root_r directory is given, all paths will
57 * be prefixed accordingly.
59 * root_r\repoCachePath
62 * \repoPackagesCachePath
66 RepoManagerOptions( const Pathname & root_r = Pathname() );
68 /** Test setup adjusting all paths to be located below one \c root_r directory.
70 * root_r\ - repoCachePath
71 * \raw - repoRawCachePath
72 * \solv - repoSolvCachePath
73 * \packages - repoPackagesCachePath
74 * \repos.d - knownReposPath
77 static RepoManagerOptions makeTestSetup( const Pathname & root_r );
79 Pathname repoCachePath;
80 Pathname repoRawCachePath;
81 Pathname repoSolvCachePath;
82 Pathname repoPackagesCachePath;
83 Pathname knownReposPath;
84 Pathname knownServicesPath;
88 * Target distro ID to be used when refreshing repo index services.
89 * Repositories not maching this ID will be skipped/removed.
91 * If empty, \ref Target::targetDistribution() will be used instead.
93 std::string servicesTargetDistro;
95 /** remembers root_r value for later use */
102 * \short creates and provides information about known sources.
107 friend std::ostream & operator<<( std::ostream & str, const RepoManager & obj );
110 /** Implementation */
113 /** ServiceInfo typedefs */
114 typedef std::set<ServiceInfo> ServiceSet;
115 typedef ServiceSet::const_iterator ServiceConstIterator;
116 typedef ServiceSet::size_type ServiceSizeType;
118 /** RepoInfo typedefs */
119 typedef std::set<RepoInfo> RepoSet;
120 typedef RepoSet::const_iterator RepoConstIterator;
121 typedef RepoSet::size_type RepoSizeType;
124 RepoManager( const RepoManagerOptions &options = RepoManagerOptions() );
128 enum RawMetadataRefreshPolicy
132 RefreshIfNeededIgnoreDelay
135 enum CacheBuildPolicy
141 /** Flags for tuning RefreshService */
142 enum RefreshServiceBit
144 RefreshService_restoreStatus = (1<<0), ///< Force restoring repo enabled/disabled status
145 RefreshService_forceRefresh = (1<<1), ///< Force refresh even if TTL is not reached
147 ZYPP_DECLARE_FLAGS(RefreshServiceFlags,RefreshServiceBit);
149 /** Options tuning RefreshService */
150 typedef RefreshServiceFlags RefreshServiceOptions;
153 /** \name Known repositories.
155 * The known repositories are read from
156 * \ref RepoManagerOptions::knownReposPath passed on the Ctor.
157 * Which defaults to ZYpp global settings.
160 bool repoEmpty() const;
161 RepoSizeType repoSize() const;
162 RepoConstIterator repoBegin() const;
163 RepoConstIterator repoEnd() const;
165 /** List of known repositories. */
166 std::list<RepoInfo> knownRepositories() const
167 { return std::list<RepoInfo>(repoBegin(),repoEnd()); }
169 /** Find RepoInfo by alias or return \ref RepoInfo::noRepo. */
170 RepoInfo getRepo( const std::string & alias ) const;
171 /** \overload Take alias from RepoInfo. */
172 RepoInfo getRepo( const RepoInfo & info_r ) const
173 { return getRepo( info_r.alias() ); }
175 /** Return whether there is a known repository for \c alias. */
176 bool hasRepo( const std::string & alias ) const;
177 /** \overload Take alias from RepoInfo. */
178 bool hasRepo( const RepoInfo & info_r ) const
179 { return hasRepo( info_r.alias() ); }
181 /** Some stupid string but suitable as alias for your url if nothing better is available.
182 * Something like \c "http-download.opensuse.org-83df67e5"
184 static std::string makeStupidAlias( const Url & url_r = Url() );
188 * \short Status of local metadata
190 RepoStatus metadataStatus( const RepoInfo &info ) const;
193 * Possibly return state of checkIfRefreshMEtadata function
195 enum RefreshCheckStatus {
196 REFRESH_NEEDED, /**< refresh is needed */
197 REPO_UP_TO_DATE, /**< repository not changed */
198 REPO_CHECK_DELAYED /**< refresh is delayed due to settings */
202 * Checks whether to refresh metadata for specified repository and url.
204 * The need for refresh is evaluated according to the following conditions,
207 * <li>the refresh policy (refresh may be forced)
208 * <li>the repo.refresh.delay ZConfig value compared to the difference between
209 * cached index file timestamp and actual time
210 * <li>the timestamp of cached repo index file compared to the remote
211 * index file timestamp.
214 * This method checks the status against the specified url only. If more
215 * baseurls are defined for in the RepoInfo, each one must be check
216 * individually. Example:
221 * // try urls one by one
222 * for ( RepoInfo::urls_const_iterator it = info.baseUrlsBegin();
223 * it != info.baseUrlsEnd(); ++it )
227 * // check whether to refresh metadata
228 * // if the check fails for this url, it throws, so another url will be checked
229 * if (checkIfToRefreshMetadata(info, *it, policy)!=RepoInfo::REFRESH_NEEDED)
232 * // do the actual refresh
234 * catch (const Exception & e)
237 * ERR << *it << " doesn't look good. Trying another url." << endl;
241 * handle("No more URLs.");
248 * \return state of repository
249 * \see RefreshCheckStatus
250 * \throws RepoUnknownTypeException
251 * \throws repo::RepoNoAliasException if can't figure an alias
252 * \throws Exception on unknown error
255 RefreshCheckStatus checkIfToRefreshMetadata( const RepoInfo &info,
257 RawMetadataRefreshPolicy policy = RefreshIfNeeded);
260 * \short Path where the metadata is downloaded and kept
262 * Given a repoinfo, tells where \ref RepoManager will download
263 * and keep the raw metadata.
265 * \param info Repository information
267 * \throws repo::RepoNoAliasException if can't figure an alias
269 Pathname metadataPath( const RepoInfo &info ) const;
273 * \short Path where the rpm packages are downloaded and kept
275 * Given a repoinfo, tells where \ref RepoProvidePackage will download
276 * and keep the .rpm files.
278 * \param info Repository information
280 * \throws repo::RepoNoAliasException if can't figure an alias
282 Pathname packagesPath( const RepoInfo &info ) const;
286 * \short Refresh local raw cache
288 * Will try to download the metadata
290 * In case of falure the metadata remains
293 * \throws repo::RepoNoUrlException if no urls are available.
294 * \throws repo::RepoNoAliasException if can't figure an alias
295 * \throws repo::RepoUnknownTypeException if the metadata is unknown
296 * \throws repo::RepoException if the repository is invalid
297 * (no valid metadata found at any of baseurls)
299 void refreshMetadata( const RepoInfo &info,
300 RawMetadataRefreshPolicy policy = RefreshIfNeeded,
301 const ProgressData::ReceiverFnc & progressrcv = ProgressData::ReceiverFnc() );
304 * \short Clean local metadata
306 * Empty local metadata.
308 * \throws repo::RepoNoAliasException if can't figure an alias
309 * \throws Exception on unknown error.
311 void cleanMetadata( const RepoInfo &info,
312 const ProgressData::ReceiverFnc & progressrcv = ProgressData::ReceiverFnc() );
315 * \short Clean local package cache
317 * Empty local directory with downloaded packages
319 * \throws repo::RepoNoAliasException if can't figure an alias
320 * \throws Exception on unknown error.
322 void cleanPackages( const RepoInfo &info,
323 const ProgressData::ReceiverFnc & progressrcv = ProgressData::ReceiverFnc() );
326 * \short Status of metadata cache
328 RepoStatus cacheStatus( const RepoInfo &info ) const;
331 * \short Refresh local cache
333 * Will try to build the cache from local metadata.
335 * If the cache exists it will be overwriten.
337 * \note the local metadata must be valid.
339 * \throws repo::RepoNoAliasException if can't figure
340 * an alias to look in cache
341 * \throws repo::RepoMetadataException if the metadata
342 * is not enough to build a cache (empty, incorrect, or
344 * \throws repo::RepoUnknownTypeException
345 * \throws parser::ParseException if parser encounters an error.
346 * \throws Exception on unknown error.
348 void buildCache( const RepoInfo &info,
349 CacheBuildPolicy policy = BuildIfNeeded,
350 const ProgressData::ReceiverFnc & progressrcv = ProgressData::ReceiverFnc() );
353 * \short clean local cache
355 * Clean the cached version of the metadata
357 * \note the local metadata must be valid.
359 * \throws repo::RepoNoAliasException if can't figure an alias to look in cache
360 * \throws cache::CacheRecordNotFoundException if the cache could not be
361 * cleaned because of repository record not found.
362 * \throws Exception on unknown error.
364 void cleanCache( const RepoInfo &info,
365 const ProgressData::ReceiverFnc & progressrcv = ProgressData::ReceiverFnc() );
368 * \short Whether a repository exists in cache
370 * \param RepoInfo to be checked.
372 bool isCached( const RepoInfo &info ) const;
376 * \short Load resolvables into the pool
378 * Creating from cache requires that the repository is
379 * refreshed (metadata downloaded) and cached
381 * \throws repo::RepoNoAliasException if can't figure an alias to look in cache
382 * \throw RepoNotCachedException When the source is not cached.
384 void loadFromCache( const RepoInfo &info,
385 const ProgressData::ReceiverFnc & progressrcv = ProgressData::ReceiverFnc() );
388 * Remove any subdirectories of cache directories which no longer belong
389 * to any of known repositories.
391 * These can be temporary directories left by interrupted refresh,
392 * or dirs left after changing .repo files outside of libzypp.
394 void cleanCacheDirGarbage( const ProgressData::ReceiverFnc & progressrcv = ProgressData::ReceiverFnc() );
397 * \short Probe repo metadata type.
399 * The location to probe consists of the base \a url (you may think of it as
400 * a mountpoint) and the \a path to the repository on the mounted media
403 repo::RepoType probe( const Url & url, const Pathname & path ) const;
405 * \overload Using the default path \c "/".
407 repo::RepoType probe( const Url & url ) const;
411 * \short Adds a repository to the list of known repositories.
415 * \throws repo::RepoAlreadyExistsException If the repo clash some
416 * unique attribute like alias
417 * \throws RepoUnknownType
418 * If RepoManagerOptions::probe is true
419 * and repository type can't be determined.
420 * \throws RepoException
421 * If RepoManagerOptions::probe is true and access to the url fails.
422 * \throws Exception On other errors.
424 void addRepository( const RepoInfo &info,
425 const ProgressData::ReceiverFnc & progressrcv = ProgressData::ReceiverFnc() );
428 * \short Adds repositores from a repo file to the list of known repositories.
429 * \param url Url of the repo file
431 * \throws repo::RepoAlreadyExistsException If the repo clash some
432 * unique attribute like alias
433 * \throws MediaException If the access to the url fails
434 * \throws ParseException If the file parsing fails
435 * \throws RepoUnknownType If repository type can't be determined
436 * \throws RepoException ON other repository related errors
437 * \throws Exception On other errors.
439 void addRepositories( const Url &url,
440 const ProgressData::ReceiverFnc & progressrcv = ProgressData::ReceiverFnc() );
442 * \short Remove the best matching repository from known repos list
444 * \throws RepoNotFoundException If no repo match
446 void removeRepository( const RepoInfo & info,
447 const ProgressData::ReceiverFnc & progressrcv = ProgressData::ReceiverFnc() );
450 * \short Modify repository attributes
452 * \throws RepoAlreadyExistsException if the alias specified in newinfo
453 * is already used by another repository
454 * \throws RepoNotFoundException If no repo match
455 * \throws ParseException If the file parsing fails
456 * \throws Exception On other errors.
458 void modifyRepository( const std::string &alias,
459 const RepoInfo & newinfo,
460 const ProgressData::ReceiverFnc & progressrcv = ProgressData::ReceiverFnc() );
461 /** \overload Take alias from RepoInfo. */
462 void modifyRepository( const RepoInfo & newinfo,
463 const ProgressData::ReceiverFnc & progressrcv = ProgressData::ReceiverFnc() )
464 { modifyRepository( newinfo.alias(), newinfo, progressrcv ); }
467 * \short Find a matching repository info
469 * \note if multiple repositories incorrectly share the
470 * same alias, the first one found will be returned.
472 * \param alias Repository alias
473 * \param progressrcv Progress reporting function
474 * \return RepoInfo of the found repository
475 * \throws RepoNotFoundException If no repo match the alias
476 * \throws ParseException If the file parsing fails
477 * \throws Exception On other errors.
479 RepoInfo getRepositoryInfo( const std::string &alias,
480 const ProgressData::ReceiverFnc & progressrcv = ProgressData::ReceiverFnc() );
483 * \short Find repository info by URL.
485 * \param url URL to find.
486 * \param urlview url::ViewOption to influence URL matching.
487 * \param progressrcv Progress receiver function.
488 * \return RepoInfo of the found repository.
490 * \note if multpile repositories incorrectly share the
491 * same URL, the first one found will be returned.
493 * \note the string representation of the URLs are compared.
494 * The \a urlview can be used to influence which
495 parts of the URL are to be compared.
497 * \throws RepoNotFoundException If no repo match
498 * \throws ParseException If the file parsing fails
499 * \throws Exception On other errors.
501 RepoInfo getRepositoryInfo( const Url & url,
502 const url::ViewOption & urlview = url::ViewOption::DEFAULTS,
503 const ProgressData::ReceiverFnc & progressrcv = ProgressData::ReceiverFnc() );
506 /** \name Known services.
508 * The known services are read from
509 * \ref RepoManagerOptions::knownServicesPath passed on the Ctor.
510 * Which defaults to ZYpp global settings.
514 * Gets true if no service is in RepoManager (so no one in specified location)
516 * \return true if any ServiceInfo is in RepoManager
518 bool serviceEmpty() const;
521 * Gets count of service in RepoManager (in specified location)
523 * \return count of service
525 ServiceSizeType serviceSize() const;
528 * Iterator to first service in internal storage.
529 * \note Iterator is immutable, so you cannot change pointed ServiceInfo
530 * \return Iterator to first service
532 ServiceConstIterator serviceBegin() const;
535 * Iterator to place behind last service in internal storage.
536 * \return iterator to end
538 ServiceConstIterator serviceEnd() const;
540 /** List of known services. */
541 std::list<ServiceInfo> knownServices() const
542 { return std::list<ServiceInfo>(serviceBegin(),serviceEnd()); }
545 * \short Finds ServiceInfo by alias or return \ref ServiceInfo::noService
547 * \param alias unique identifier of service
548 * \return information about service
550 ServiceInfo getService( const std::string & alias ) const;
552 /** Return whether there is a known service for \c alias. */
553 bool hasService( const std::string & alias ) const;
557 * \short Probe the type or the service.
559 repo::ServiceType probeService( const Url &url ) const;
562 * Adds new service by it's alias and url
564 * \param alias unique identifier of the service
565 * \param url url to service
567 * \throws FIXME RepoAlreadyExistException and as reponame is service name
569 void addService( const std::string & alias, const Url& url );
574 * \param service service info
576 * \throws FIXME RepoAlreadyExistException and as reponame is service name
578 void addService( const ServiceInfo & service );
581 * Removes service specified by its name
583 * \param alias unique indientifier of the service to remove
585 * \throws RepoException if service is not found or file with ServiceInfo cannot be deleted
586 * \throws Exception if file contain more services and rewrite file failed
588 void removeService( const std::string & alias );
589 /** \overload Take alias from ServiceInfo */
590 void removeService( const ServiceInfo & service );
594 * Refreshes all enabled services.
596 * \see refreshService(ServiceInfo)
598 void refreshServices( const RefreshServiceOptions & options_r = RefreshServiceOptions() );
601 * Refresh specific service.
603 * \param alias unique indientifier of the service to refresh
605 * \throws RepoException if service is not found.
606 * \throws MediaException If there's a problem downloading the repo index file.
608 void refreshService( const std::string & alias, const RefreshServiceOptions & options_r = RefreshServiceOptions() );
609 /** \overload Take alias from ServiceInfo */
610 void refreshService( const ServiceInfo & service, const RefreshServiceOptions & options_r = RefreshServiceOptions() );
613 * Modifies service file (rewrites it with new values) and underlying
614 * repositories if needed.
616 * Modifications of a service can lead to rewrite of all .repo files of
617 * contained repositories. Particularily, disabling a service (changing
618 * ServiceInfo::enabled() from true to false) will disable all contained
619 * repositories. Renaming of a service will modify the "service" key
620 * of all contained repositories.
622 * \param oldAlias Old alias of the service
623 * \param service ServiceInfo object containing new data
625 * \throws RepoException if sservice with oldAlias is not known
626 * \throws Exception if have problems with files
628 void modifyService( const std::string & oldAlias, const ServiceInfo & service );
629 /** \overload Take alias from ServiceInfo. */
630 void modifyService( const ServiceInfo & service )
631 { modifyService( service.alias(), service ); }
635 * Functor thats filter RepoInfo by service which it belongs to.
637 struct MatchServiceAlias
640 MatchServiceAlias( const std::string & alias_ ) : alias(alias_) {}
641 bool operator()( const RepoInfo & info ) const
642 { return info.service() == alias; }
650 * fill to output iterator repositories in service name. This output iterator can perform
651 * any action on with Repo or service Container, because it is sets and it isn't dynamic recreate.
653 * \note Don't use this function with RepoManager::removeRepository(), it will lead to segfaults
654 * due to invalidated internal iterators. FIXME can this be solved (using STL) so that this
655 * warning would not be needed?
657 * \param alias service alias
658 * \param out output iterator which get all the repositories belonging to
661 * example how set priority for each RepoInfo in this service:
664 * class ChangePriority
669 * ChangePriority(int prio) : priority(prio) {}
670 * // missing rewrite priority back via RepoManager::modifyRepo
671 * void doIt( RepoInfo info ) { info.setPriority(priority); }
674 * //somewhere in code
675 * ChangePriority changer(10);
676 * getRepositoriesInService(name,
677 * boost::make_function_output_iterator(
678 * bind(&ChangePriority::doIt, &changer, _1)));
681 template<typename OutputIterator>
682 void getRepositoriesInService( const std::string & alias,
683 OutputIterator out ) const
685 MatchServiceAlias filter(alias);
687 std::copy( boost::make_filter_iterator( filter, repoBegin(), repoEnd() ),
688 boost::make_filter_iterator( filter, repoEnd(), repoEnd() ),
693 /** Pointer to implementation */
694 RWCOW_pointer<Impl> _pimpl;
696 ZYPP_DECLARE_OPERATORS_FOR_FLAGS(RepoManager::RefreshServiceFlags);
697 ///////////////////////////////////////////////////////////////////
699 /** \relates RepoManager Stream output */
700 std::ostream & operator<<( std::ostream & str, const RepoManager & obj );
702 /////////////////////////////////////////////////////////////////
704 ///////////////////////////////////////////////////////////////////
705 #endif // ZYPP2_REPOMANAGER_H