1 /*---------------------------------------------------------------------\
3 | |__ / \ / / . \ . \ |
8 \---------------------------------------------------------------------*/
9 /** \file zypp/RepoManager.h
12 #ifndef ZYPP_REPOMANAGER_H
13 #define ZYPP_REPOMANAGER_H
18 #include <boost/iterator.hpp>
20 #include "zypp/base/PtrTypes.h"
21 #include "zypp/Pathname.h"
22 #include "zypp/ZConfig.h"
23 #include "zypp/RepoInfo.h"
24 #include "zypp/repo/RepoException.h"
25 #include "zypp/repo/RepoType.h"
26 #include "zypp/RepoStatus.h"
27 #include "zypp/ProgressData.h"
29 ///////////////////////////////////////////////////////////////////
31 { /////////////////////////////////////////////////////////////////
32 class ServiceInfo; //predef
35 * Parses \a repo_file and returns a list of \ref RepoInfo objects
36 * corresponding to repositories found within the file.
38 * \param repo_file Valid URL of the repo file.
39 * \return found list<RepoInfo>
41 * \throws MediaException If the access to the url fails
42 * \throws ParseException If the file parsing fails
43 * \throws Exception On other errors.
45 std::list<RepoInfo> readRepoFile(const Url & repo_file);
48 * Repo manager settings.
49 * Settings default to ZYpp global settings.
51 struct RepoManagerOptions
53 /** Default ctor following \ref ZConfig global settings.
54 * If an optional \c root_r directory is given, all paths will
55 * be prefixed accordingly.
57 * root_r\repoCachePath
60 * \repoPackagesCachePath
64 RepoManagerOptions( const Pathname & root_r = Pathname() );
66 /** Test setup adjusting all paths to be located below one \c root_r directory.
68 * root_r\ - repoCachePath
69 * \raw - repoRawCachePath
70 * \solv - repoSolvCachePath
71 * \packages - repoPackagesCachePath
72 * \repos.d - knownReposPath
75 static RepoManagerOptions makeTestSetup( const Pathname & root_r );
77 Pathname repoCachePath;
78 Pathname repoRawCachePath;
79 Pathname repoSolvCachePath;
80 Pathname repoPackagesCachePath;
81 Pathname knownReposPath;
82 Pathname knownServicesPath;
85 * Target distro ID to be used when refreshing repo index services.
86 * Repositories not maching this ID will be skipped/removed.
88 * The value is initialized upon construction to
89 * \ref Target::targetDistribution() if the Target is already initialized,
90 * otherwise the value is initially empty.
92 * If empty, no repositories contained in the index will be skipped.
94 std::string servicesTargetDistro;
100 * \short creates and provides information about known sources.
105 friend std::ostream & operator<<( std::ostream & str, const RepoManager & obj );
108 /** Implementation */
111 /** service typedefs */
112 typedef std::set<ServiceInfo> ServiceSet;
113 typedef ServiceSet::const_iterator ServiceConstIterator;
114 typedef ServiceSet::size_type ServiceSizeType;
116 /** RepoInfo typedefs */
117 typedef std::set<RepoInfo> RepoSet;
118 typedef RepoSet::const_iterator RepoConstIterator;
119 typedef RepoSet::size_type RepoSizeType;
122 RepoManager( const RepoManagerOptions &options = RepoManagerOptions() );
126 enum RawMetadataRefreshPolicy
130 RefreshIfNeededIgnoreDelay
133 enum CacheBuildPolicy
139 enum RepoRemovePolicy
145 * \short List known repositories.
147 * The known repositories are read from
148 * \ref RepoManagerOptions::knownReposPath passed on the Ctor.
149 * Which defaults to ZYpp global settings.
150 * \deprecated use iterator instead which read only one time directory
151 * \return found list<RepoInfo>
153 ZYPP_DEPRECATED std::list<RepoInfo> knownRepositories() const;
155 bool repoEmpty() const;
156 RepoSizeType repoSize() const;
157 RepoConstIterator repoBegin() const;
158 RepoConstIterator repoEnd() const;
162 * \short Status of local metadata
164 RepoStatus metadataStatus( const RepoInfo &info ) const;
167 * Possibly return state of checkIfRefreshMEtadata function
169 enum RefreshCheckStatus {
170 REFRESH_NEEDED, /**< refresh is needed */
171 REPO_UP_TO_DATE, /**< repository not changed */
172 REPO_CHECK_DELAYED /**< refresh is delayed due to settings */
176 * Checks whether to refresh metadata for specified repository and url.
178 * The need for refresh is evaluated according to the following conditions,
181 * <li>the refresh policy (refresh may be forced)
182 * <li>the repo.refresh.delay ZConfig value compared to the difference between
183 * cached index file timestamp and actual time
184 * <li>the timestamp of cached repo index file compared to the remote
185 * index file timestamp.
188 * This method checks the status against the specified url only. If more
189 * baseurls are defined for in the RepoInfo, each one must be check
190 * individually. Example:
195 * // try urls one by one
196 * for ( RepoInfo::urls_const_iterator it = info.baseUrlsBegin();
197 * it != info.baseUrlsEnd(); ++it )
201 * // check whether to refresh metadata
202 * // if the check fails for this url, it throws, so another url will be checked
203 * if (checkIfToRefreshMetadata(info, *it, policy)!=RepoInfo::REFRESH_NEEDED)
206 * // do the actual refresh
208 * catch (const Exception & e)
211 * ERR << *it << " doesn't look good. Trying another url." << endl;
215 * handle("No more URLs.");
222 * \return state of repository
223 * \see RefreshCheckStatus
224 * \throws RepoUnknownTypeException
225 * \throws repo::RepoNoAliasException if can't figure an alias
226 * \throws Exception on unknown error
229 RefreshCheckStatus checkIfToRefreshMetadata( const RepoInfo &info,
231 RawMetadataRefreshPolicy policy = RefreshIfNeeded);
234 * \short Path where the metadata is downloaded and kept
236 * Given a repoinfo, tells where \ref RepoManager will download
237 * and keep the raw metadata.
239 * \param info Repository information
241 * \throws repo::RepoNoAliasException if can't figure an alias
243 Pathname metadataPath( const RepoInfo &info ) const;
247 * \short Path where the rpm packages are downloaded and kept
249 * Given a repoinfo, tells where \ref RepoProvidePackage will download
250 * and keep the .rpm files.
252 * \param info Repository information
254 * \throws repo::RepoNoAliasException if can't figure an alias
256 Pathname packagesPath( const RepoInfo &info ) const;
260 * \short Refresh local raw cache
262 * Will try to download the metadata
264 * In case of falure the metadata remains
267 * \throws repo::RepoNoUrlException if no urls are available.
268 * \throws repo::RepoNoAliasException if can't figure an alias
269 * \throws repo::RepoUnknownTypeException if the metadata is unknown
270 * \throws repo::RepoException if the repository is invalid
271 * (no valid metadata found at any of baseurls)
273 void refreshMetadata( const RepoInfo &info,
274 RawMetadataRefreshPolicy policy = RefreshIfNeeded,
275 const ProgressData::ReceiverFnc & progressrcv = ProgressData::ReceiverFnc() );
278 * \short Clean local metadata
280 * Empty local metadata.
282 * \throws repo::RepoNoAliasException if can't figure an alias
283 * \throws Exception on unknown error.
285 void cleanMetadata( const RepoInfo &info,
286 const ProgressData::ReceiverFnc & progressrcv = ProgressData::ReceiverFnc() );
289 * \short Clean local package cache
291 * Empty local directory with downloaded packages
293 * \throws repo::RepoNoAliasException if can't figure an alias
294 * \throws Exception on unknown error.
296 void cleanPackages( const RepoInfo &info,
297 const ProgressData::ReceiverFnc & progressrcv = ProgressData::ReceiverFnc() );
300 * \short Status of metadata cache
302 RepoStatus cacheStatus( const RepoInfo &info ) const;
305 * \short Refresh local cache
307 * Will try to build the cache from local metadata.
309 * If the cache exists it will be overwriten.
311 * \note the local metadata must be valid.
313 * \throws repo::RepoNoAliasException if can't figure
314 * an alias to look in cache
315 * \throws repo::RepoMetadataException if the metadata
316 * is not enough to build a cache (empty, incorrect, or
318 * \throws repo::RepoUnknownTypeException
319 * \throws parser::ParseException if parser encounters an error.
320 * \throws Exception on unknown error.
322 void buildCache( const RepoInfo &info,
323 CacheBuildPolicy policy = BuildIfNeeded,
324 const ProgressData::ReceiverFnc & progressrcv = ProgressData::ReceiverFnc() );
327 * \short clean local cache
329 * Clean the cached version of the metadata
331 * \note the local metadata must be valid.
333 * \throws repo::RepoNoAliasException if can't figure an alias to look in cache
334 * \throws cache::CacheRecordNotFoundException if the cache could not be
335 * cleaned because of repository record not found.
336 * \throws Exception on unknown error.
338 void cleanCache( const RepoInfo &info,
339 const ProgressData::ReceiverFnc & progressrcv = ProgressData::ReceiverFnc() );
342 * \short Whether a repository exists in cache
344 * \param RepoInfo to be checked.
346 bool isCached( const RepoInfo &info ) const;
350 * \short Load resolvables into the pool
352 * Creating from cache requires that the repository is
353 * refreshed (metadata downloaded) and cached
355 * \throws repo::RepoNoAliasException if can't figure an alias to look in cache
356 * \throw RepoNotCachedException When the source is not cached.
358 void loadFromCache( const RepoInfo &info,
359 const ProgressData::ReceiverFnc & progressrcv = ProgressData::ReceiverFnc() );
361 * \short Probe repo metadata type.
363 * \todo FIXME Should this be private?
365 repo::RepoType probe( const Url &url ) const;
369 * \short Adds a repository to the list of known repositories.
373 * \throws repo::RepoAlreadyExistsException If the repo clash some
374 * unique attribute like alias
375 * \throws RepoUnknownType
376 * If RepoManagerOptions::probe is true
377 * and repository type can't be determined.
378 * \throws RepoException
379 * If RepoManagerOptions::probe is true and access to the url fails.
380 * \throws Exception On other errors.
382 void addRepository( const RepoInfo &info,
383 const ProgressData::ReceiverFnc & progressrcv = ProgressData::ReceiverFnc() );
386 * \short Adds repositores from a repo file to the list of known repositories.
387 * \param url Url of the repo file
389 * \throws repo::RepoAlreadyExistsException If the repo clash some
390 * unique attribute like alias
391 * \throws MediaException If the access to the url fails
392 * \throws ParseException If the file parsing fails
393 * \throws RepoUnknownType If repository type can't be determined
394 * \throws RepoException ON other repository related errors
395 * \throws Exception On other errors.
397 void addRepositories( const Url &url,
398 const ProgressData::ReceiverFnc & progressrcv = ProgressData::ReceiverFnc() );
400 * \short Remove the best matching repository from known repos list
402 * \throws RepoNotFoundException If no repo match
404 void removeRepository( const RepoInfo & info,
405 const ProgressData::ReceiverFnc & progressrcv = ProgressData::ReceiverFnc() );
408 * \short Modify repository attributes
410 * \throws RepoAlreadyExistsException if the alias specified in newinfo
411 * is already used by another repository
412 * \throws RepoNotFoundException If no repo match
413 * \throws ParseException If the file parsing fails
414 * \throws Exception On other errors.
416 void modifyRepository( const std::string &alias,
417 const RepoInfo & newinfo,
418 const ProgressData::ReceiverFnc & progressrcv = ProgressData::ReceiverFnc() );
421 * \short Find a matching repository info
423 * \note if multiple repositories incorrectly share the
424 * same alias, the first one found will be returned.
426 * \param alias Repository alias
427 * \param progressrcv Progress reporting function
428 * \return RepoInfo of the found repository
429 * \throws RepoNotFoundException If no repo match the alias
430 * \throws ParseException If the file parsing fails
431 * \throws Exception On other errors.
433 RepoInfo getRepositoryInfo( const std::string &alias,
434 const ProgressData::ReceiverFnc & progressrcv = ProgressData::ReceiverFnc() );
437 * \short Find repository info by URL.
439 * \param url URL to find.
440 * \param urlview url::ViewOption to influence URL matching.
441 * \param progressrcv Progress receiver function.
442 * \return RepoInfo of the found repository.
444 * \note if multpile repositories incorrectly share the
445 * same URL, the first one found will be returned.
447 * \note the string representation of the URLs are compared.
448 * The \a urlview can be used to influence which
449 parts of the URL are to be compared.
451 * \throws RepoNotFoundException If no repo match
452 * \throws ParseException If the file parsing fails
453 * \throws Exception On other errors.
455 RepoInfo getRepositoryInfo( const Url & url,
456 const url::ViewOption & urlview = url::ViewOption::DEFAULTS,
457 const ProgressData::ReceiverFnc & progressrcv = ProgressData::ReceiverFnc() );
460 * Adds new service by it's alias and url
462 * \param alias unique identifier of the service
463 * \param url url to service
465 * \throws FIXME RepoAlreadyExistException and as reponame is service name
467 void addService( const std::string & alias, const Url& url );
472 * \param service service info
474 * \throws FIXME RepoAlreadyExistException and as reponame is service name
476 void addService( const ServiceInfo & service );
479 * Removes service specified by its name
481 * \param alias unique indientifier of the service to remove
483 * \throws RepoException if service is not found or file with ServiceInfo cannot be deleted
484 * \throws Exception if file contain more services and rewrite file failed
486 void removeService( const std::string & alias );
488 void removeService( const ServiceInfo & service );
491 * Gets true if no service is in RepoManager (so no one in specified location)
493 * \return true if any ServiceInfo is in RepoManager
495 bool serviceEmpty() const;
498 * Gets count of service in RepoManager (in specified location)
500 * \return count of service
502 ServiceSizeType serviceSize() const;
505 * Iterator to first service in internal storage.
506 * \note Iterator is immutable, so you cannot change pointed ServiceInfo
507 * \return Iterator to first service
509 ServiceConstIterator serviceBegin() const;
512 * Iterator to place behind last service in internal storage.
513 * \return iterator to end
515 ServiceConstIterator serviceEnd() const;
518 * Finds ServiceInfo by alias or return noService
520 * \param alias unique identifier of service
521 * \return information about service
523 ServiceInfo getService( const std::string & alias ) const;
526 * Refreshes all enabled services.
528 * \see refreshService(ServiceInfo)
530 void refreshServices();
533 * Refresh specific service.
535 * \param name service structure
536 * \throws MediaException If there's a problem downloading the repo index file.
538 void refreshService( const ServiceInfo & service );
541 * Modifies service file (rewrites it with new values) and underlying
542 * repositories if needed.
544 * Modifications of a service can lead to rewrite of all .repo files of
545 * contained repositories. Particularily, disabling a service (changing
546 * ServiceInfo::enabled() from true to false) will disable all contained
547 * repositories. Renaming of a service will modify the "service" key
548 * of all contained repositories.
550 * \param oldAlias Old alias of the service
551 * \param service ServiceInfo object containing new data
553 * \throws RepoException if sservice with oldAlias is not known
554 * \throws Exception if have problems with files
556 void modifyService(const std::string & oldAlias, const ServiceInfo & service);
560 * Functor thats filter RepoInfo by service which belongs to.
562 struct MatchServiceAlias
567 MatchServiceAlias( const std::string & alias_ ) : alias(alias_) {}
568 bool match( const RepoInfo & info ) { return info.service() == alias; }
574 * fill to output iterator repositories in service name. This output iterator can perform
575 * any action on with Repo or service Container, because it is sets and it isn't dynamic recreate.
577 * \note Don't use this function with RepoManager::removeRepository(), it will lead to segfaults
578 * due to invalidated internal iterators. FIXME can this be solved (using STL) so that this
579 * warning would not be needed?
581 * \param alias service alias
582 * \param out output iterator which get all the repositories belonging to
585 * example how set priority for each RepoInfo in this service:
588 * class ChangePriority
593 * ChangePriority(int prio) : priority(prio) {}
594 * // missing rewrite priority back via RepoManager::modifyRepo
595 * void doIt( RepoInfo info ) { info.setPriority(priority); }
598 * //somewhere in code
599 * ChangePriority changer(10);
600 * getRepositoriesInService(name,
601 * boost::make_function_output_iterator(
602 * bind(&ChangePriority::doIt, &changer, _1)));
605 template<typename OutputIterator>
606 void getRepositoriesInService( const std::string & alias,
607 OutputIterator out ) const
609 MatchServiceAlias filter(alias);
612 boost::make_filter_iterator(
613 bind(&MatchServiceAlias::match, filter, _1), repoBegin(), repoEnd()),
614 boost::make_filter_iterator(
615 bind(&MatchServiceAlias::match, filter, _1), repoEnd(), repoEnd()),
620 RepoStatus rawMetadataStatus( const RepoInfo &info );
621 void setCacheStatus( const RepoInfo &info, const RepoStatus &status );
624 * Update timestamp of repository index file for the specified repository \a info.
625 * Used in \ref checkIfToRefreshMetadata() for repo.refresh.delay feature.
627 void touchIndexFile(const RepoInfo & info);
632 /** Pointer to implementation */
633 RWCOW_pointer<Impl> _pimpl;
635 ///////////////////////////////////////////////////////////////////
637 /** \relates RepoManager Stream output */
638 std::ostream & operator<<( std::ostream & str, const RepoManager & obj );
640 /////////////////////////////////////////////////////////////////
642 ///////////////////////////////////////////////////////////////////
643 #endif // ZYPP2_REPOMANAGER_H