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;
89 * \short creates and provides information about known sources.
94 friend std::ostream & operator<<( std::ostream & str, const RepoManager & obj );
100 /** service typedefs */
101 typedef std::set<ServiceInfo> ServiceSet;
102 typedef ServiceSet::const_iterator ServiceConstIterator;
103 typedef ServiceSet::size_type ServiceSizeType;
105 /** RepoInfo typedefs */
106 typedef std::set<RepoInfo> RepoSet;
107 typedef RepoSet::const_iterator RepoConstIterator;
108 typedef RepoSet::size_type RepoSizeType;
111 RepoManager( const RepoManagerOptions &options = RepoManagerOptions() );
115 enum RawMetadataRefreshPolicy
119 RefreshIfNeededIgnoreDelay
122 enum CacheBuildPolicy
128 enum RepoRemovePolicy
134 * \short List known repositories.
136 * The known repositories are read from
137 * \ref RepoManagerOptions::knownReposPath passed on the Ctor.
138 * Which defaults to ZYpp global settings.
139 * \deprecated use iterator instead which read only one time directory
140 * \return found list<RepoInfo>
142 ZYPP_DEPRECATED std::list<RepoInfo> knownRepositories() const;
144 bool repoEmpty() const;
145 RepoSizeType repoSize() const;
146 RepoConstIterator repoBegin() const;
147 RepoConstIterator repoEnd() const;
151 * \short Status of local metadata
153 RepoStatus metadataStatus( const RepoInfo &info ) const;
156 * Possibly return state of checkIfRefreshMEtadata function
158 enum RefreshCheckStatus {
159 REFRESH_NEEDED, /**< refresh is needed */
160 REPO_UP_TO_DATE, /**< repository not changed */
161 REPO_CHECK_DELAYED /**< refresh is delayed due to settings */
165 * Checks whether to refresh metadata for specified repository and url.
167 * The need for refresh is evaluated according to the following conditions,
170 * <li>the refresh policy (refresh may be forced)
171 * <li>the repo.refresh.delay ZConfig value compared to the difference between
172 * cached index file timestamp and actual time
173 * <li>the timestamp of cached repo index file compared to the remote
174 * index file timestamp.
177 * This method checks the status against the specified url only. If more
178 * baseurls are defined for in the RepoInfo, each one must be check
179 * individually. Example:
184 * // try urls one by one
185 * for ( RepoInfo::urls_const_iterator it = info.baseUrlsBegin();
186 * it != info.baseUrlsEnd(); ++it )
190 * // check whether to refresh metadata
191 * // if the check fails for this url, it throws, so another url will be checked
192 * if (checkIfToRefreshMetadata(info, *it, policy)!=RepoInfo::REFRESH_NEEDED)
195 * // do the actual refresh
197 * catch (const Exception & e)
200 * ERR << *it << " doesn't look good. Trying another url." << endl;
204 * handle("No more URLs.");
211 * \return state of repository
212 * \see RefreshCheckStatus
213 * \throws RepoUnknownTypeException
214 * \throws repo::RepoNoAliasException if can't figure an alias
215 * \throws Exception on unknown error
218 RefreshCheckStatus checkIfToRefreshMetadata( const RepoInfo &info,
220 RawMetadataRefreshPolicy policy = RefreshIfNeeded);
223 * \short Path where the metadata is downloaded and kept
225 * Given a repoinfo, tells where \ref RepoManager will download
226 * and keep the raw metadata.
228 * \param info Repository information
230 * \throws repo::RepoNoAliasException if can't figure an alias
232 Pathname metadataPath( const RepoInfo &info ) const;
236 * \short Path where the rpm packages are downloaded and kept
238 * Given a repoinfo, tells where \ref RepoProvidePackage will download
239 * and keep the .rpm files.
241 * \param info Repository information
243 * \throws repo::RepoNoAliasException if can't figure an alias
245 Pathname packagesPath( const RepoInfo &info ) const;
249 * \short Refresh local raw cache
251 * Will try to download the metadata
253 * In case of falure the metadata remains
256 * \throws repo::RepoNoUrlException if no urls are available.
257 * \throws repo::RepoNoAliasException if can't figure an alias
258 * \throws repo::RepoUnknownTypeException if the metadata is unknown
259 * \throws repo::RepoException if the repository is invalid
260 * (no valid metadata found at any of baseurls)
262 void refreshMetadata( const RepoInfo &info,
263 RawMetadataRefreshPolicy policy = RefreshIfNeeded,
264 const ProgressData::ReceiverFnc & progressrcv = ProgressData::ReceiverFnc() );
267 * \short Clean local metadata
269 * Empty local metadata.
271 * \throws repo::RepoNoAliasException if can't figure an alias
272 * \throws Exception on unknown error.
274 void cleanMetadata( const RepoInfo &info,
275 const ProgressData::ReceiverFnc & progressrcv = ProgressData::ReceiverFnc() );
278 * \short Clean local package cache
280 * Empty local directory with downloaded packages
282 * \throws repo::RepoNoAliasException if can't figure an alias
283 * \throws Exception on unknown error.
285 void cleanPackages( const RepoInfo &info,
286 const ProgressData::ReceiverFnc & progressrcv = ProgressData::ReceiverFnc() );
289 * \short Status of metadata cache
291 RepoStatus cacheStatus( const RepoInfo &info ) const;
294 * \short Refresh local cache
296 * Will try to build the cache from local metadata.
298 * If the cache exists it will be overwriten.
300 * \note the local metadata must be valid.
302 * \throws repo::RepoNoAliasException if can't figure
303 * an alias to look in cache
304 * \throws repo::RepoMetadataException if the metadata
305 * is not enough to build a cache (empty, incorrect, or
307 * \throws repo::RepoUnknownTypeException
308 * \throws parser::ParseException if parser encounters an error.
309 * \throws Exception on unknown error.
311 void buildCache( const RepoInfo &info,
312 CacheBuildPolicy policy = BuildIfNeeded,
313 const ProgressData::ReceiverFnc & progressrcv = ProgressData::ReceiverFnc() );
316 * \short clean local cache
318 * Clean the cached version of the metadata
320 * \note the local metadata must be valid.
322 * \throws repo::RepoNoAliasException if can't figure an alias to look in cache
323 * \throws cache::CacheRecordNotFoundException if the cache could not be
324 * cleaned because of repository record not found.
325 * \throws Exception on unknown error.
327 void cleanCache( const RepoInfo &info,
328 const ProgressData::ReceiverFnc & progressrcv = ProgressData::ReceiverFnc() );
331 * \short Whether a repository exists in cache
333 * \param RepoInfo to be checked.
335 bool isCached( const RepoInfo &info ) const;
339 * \short Load resolvables into the pool
341 * Creating from cache requires that the repository is
342 * refreshed (metadata downloaded) and cached
344 * \throws repo::RepoNoAliasException if can't figure an alias to look in cache
345 * \throw RepoNotCachedException When the source is not cached.
347 void loadFromCache( const RepoInfo &info,
348 const ProgressData::ReceiverFnc & progressrcv = ProgressData::ReceiverFnc() );
350 * \short Probe repo metadata type.
352 * \todo FIXME Should this be private?
354 repo::RepoType probe( const Url &url ) const;
358 * \short Adds a repository to the list of known repositories.
362 * \throws repo::RepoAlreadyExistsException If the repo clash some
363 * unique attribute like alias
364 * \throws RepoUnknownType
365 * If RepoManagerOptions::probe is true
366 * and repository type can't be determined.
367 * \throws RepoException
368 * If RepoManagerOptions::probe is true and access to the url fails.
369 * \throws Exception On other errors.
371 void addRepository( const RepoInfo &info,
372 const ProgressData::ReceiverFnc & progressrcv = ProgressData::ReceiverFnc() );
375 * \short Adds repositores from a repo file to the list of known repositories.
376 * \param url Url of the repo file
378 * \throws repo::RepoAlreadyExistsException If the repo clash some
379 * unique attribute like alias
380 * \throws MediaException If the access to the url fails
381 * \throws ParseException If the file parsing fails
382 * \throws RepoUnknownType If repository type can't be determined
383 * \throws RepoException ON other repository related errors
384 * \throws Exception On other errors.
386 void addRepositories( const Url &url,
387 const ProgressData::ReceiverFnc & progressrcv = ProgressData::ReceiverFnc() );
389 * \short Remove the best matching repository from known repos list
391 * \throws RepoNotFoundException If no repo match
393 void removeRepository( const RepoInfo & info,
394 const ProgressData::ReceiverFnc & progressrcv = ProgressData::ReceiverFnc() );
397 * \short Modify repository attributes
399 * \throws RepoAlreadyExistsException if the alias specified in newinfo
400 * is already used by another repository
401 * \throws RepoNotFoundException If no repo match
402 * \throws ParseException If the file parsing fails
403 * \throws Exception On other errors.
405 void modifyRepository( const std::string &alias,
406 const RepoInfo & newinfo,
407 const ProgressData::ReceiverFnc & progressrcv = ProgressData::ReceiverFnc() );
410 * \short Find a matching repository info
412 * \note if multiple repositories incorrectly share the
413 * same alias, the first one found will be returned.
415 * \param alias Repository alias
416 * \param progressrcv Progress reporting function
417 * \return RepoInfo of the found repository
418 * \throws RepoNotFoundException If no repo match the alias
419 * \throws ParseException If the file parsing fails
420 * \throws Exception On other errors.
422 RepoInfo getRepositoryInfo( const std::string &alias,
423 const ProgressData::ReceiverFnc & progressrcv = ProgressData::ReceiverFnc() );
426 * \short Find repository info by URL.
428 * \param url URL to find.
429 * \param urlview url::ViewOption to influence URL matching.
430 * \param progressrcv Progress receiver function.
431 * \return RepoInfo of the found repository.
433 * \note if multpile repositories incorrectly share the
434 * same URL, the first one found will be returned.
436 * \note the string representation of the URLs are compared.
437 * The \a urlview can be used to influence which
438 parts of the URL are to be compared.
440 * \throws RepoNotFoundException If no repo match
441 * \throws ParseException If the file parsing fails
442 * \throws Exception On other errors.
444 RepoInfo getRepositoryInfo( const Url & url,
445 const url::ViewOption & urlview = url::ViewOption::DEFAULTS,
446 const ProgressData::ReceiverFnc & progressrcv = ProgressData::ReceiverFnc() );
449 * Adds new service by it's alias and url
451 * \param alias unique identifier of the service
452 * \param url url to service
454 * \throws FIXME RepoAlreadyExistException and as reponame is service name
456 void addService( const std::string & alias, const Url& url );
461 * \param service service info
463 * \throws FIXME RepoAlreadyExistException and as reponame is service name
465 void addService( const ServiceInfo & service );
468 * Removes service specified by its name
470 * \param alias unique indientifier of the service to remove
472 * \throws RepoException if service is not found or file with ServiceInfo cannot be deleted
473 * \throws Exception if file contain more services and rewrite file failed
475 void removeService( const std::string & alias );
477 void removeService( const ServiceInfo & service );
480 * Gets true if no service is in RepoManager (so no one in specified location)
482 * \return true if any ServiceInfo is in RepoManager
484 bool serviceEmpty() const;
487 * Gets count of service in RepoManager (in specified location)
489 * \return count of service
491 ServiceSizeType serviceSize() const;
494 * Iterator to first service in internal storage.
495 * \note Iterator is immutable, so you cannot change pointed ServiceInfo
496 * \return Iterator to first service
498 ServiceConstIterator serviceBegin() const;
501 * Iterator to place behind last service in internal storage.
502 * \return iterator to end
504 ServiceConstIterator serviceEnd() const;
507 * Finds ServiceInfo by alias or return noService
509 * \param alias unique identifier of service
510 * \return information about service
512 ServiceInfo getService( const std::string & alias ) const;
515 * Refreshes all enabled services.
517 * \see refreshService(ServiceInfo)
519 void refreshServices();
522 * Refresh specific service.
524 * \param name service structure
525 * \throws MediaException If there's a problem downloading the repo index file.
527 void refreshService( const ServiceInfo & service );
530 * Modifies service file (rewrites it with new values) and underlying
531 * repositories if needed.
533 * Modifications of a service can lead to rewrite of all .repo files of
534 * contained repositories. Particularily, disabling a service (changing
535 * ServiceInfo::enabled() from true to false) will disable all contained
536 * repositories. Renaming of a service will modify the "service" key
537 * of all contained repositories.
539 * \param oldAlias Old alias of the service
540 * \param service ServiceInfo object containing new data
542 * \throws RepoException if sservice with oldAlias is not known
543 * \throws Exception if have problems with files
545 void modifyService(const std::string & oldAlias, const ServiceInfo & service);
549 * Functor thats filter RepoInfo by service which belongs to.
551 struct MatchServiceAlias
556 MatchServiceAlias( const std::string & alias_ ) : alias(alias_) {}
557 bool match( const RepoInfo & info ) { return info.service() == alias; }
563 * fill to output iterator repositories in service name. This output iterator can perform
564 * any action on with Repo or service Container, because it is sets and it isn't dynamic recreate.
566 * \note Don't use this function with RepoManager::removeRepository(), it will lead to segfaults
567 * due to invalidated internal iterators. FIXME can this be solved (using STL) so that this
568 * warning would not be needed?
570 * \param alias service alias
571 * \param out output iterator which get all the repositories belonging to
574 * example how set priority for each RepoInfo in this service:
577 * class ChangePriority
582 * ChangePriority(int prio) : priority(prio) {}
583 * // missing rewrite priority back via RepoManager::modifyRepo
584 * void doIt( RepoInfo info ) { info.setPriority(priority); }
587 * //somewhere in code
588 * ChangePriority changer(10);
589 * getRepositoriesInService(name,
590 * boost::make_function_output_iterator(
591 * bind(&ChangePriority::doIt, &changer, _1)));
594 template<typename OutputIterator>
595 void getRepositoriesInService( const std::string & alias,
596 OutputIterator out ) const
598 MatchServiceAlias filter(alias);
601 boost::make_filter_iterator(
602 bind(&MatchServiceAlias::match, filter, _1), repoBegin(), repoEnd()),
603 boost::make_filter_iterator(
604 bind(&MatchServiceAlias::match, filter, _1), repoEnd(), repoEnd()),
609 RepoStatus rawMetadataStatus( const RepoInfo &info );
610 void setCacheStatus( const RepoInfo &info, const RepoStatus &status );
613 * Update timestamp of repository index file for the specified repository \a info.
614 * Used in \ref checkIfToRefreshMetadata() for repo.refresh.delay feature.
616 void touchIndexFile(const RepoInfo & info);
621 /** Pointer to implementation */
622 RWCOW_pointer<Impl> _pimpl;
624 ///////////////////////////////////////////////////////////////////
626 /** \relates RepoManager Stream output */
627 std::ostream & operator<<( std::ostream & str, const RepoManager & obj );
629 /////////////////////////////////////////////////////////////////
631 ///////////////////////////////////////////////////////////////////
632 #endif // ZYPP2_REPOMANAGER_H