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 * \return found list<RepoInfo>
152 std::list<RepoInfo> knownRepositories() const;
154 bool repoEmpty() const;
155 RepoSizeType repoSize() const;
156 RepoConstIterator repoBegin() const;
157 RepoConstIterator repoEnd() const;
161 * \short Status of local metadata
163 RepoStatus metadataStatus( const RepoInfo &info ) const;
166 * Possibly return state of checkIfRefreshMEtadata function
168 enum RefreshCheckStatus {
169 REFRESH_NEEDED, /**< refresh is needed */
170 REPO_UP_TO_DATE, /**< repository not changed */
171 REPO_CHECK_DELAYED /**< refresh is delayed due to settings */
175 * Checks whether to refresh metadata for specified repository and url.
177 * The need for refresh is evaluated according to the following conditions,
180 * <li>the refresh policy (refresh may be forced)
181 * <li>the repo.refresh.delay ZConfig value compared to the difference between
182 * cached index file timestamp and actual time
183 * <li>the timestamp of cached repo index file compared to the remote
184 * index file timestamp.
187 * This method checks the status against the specified url only. If more
188 * baseurls are defined for in the RepoInfo, each one must be check
189 * individually. Example:
194 * // try urls one by one
195 * for ( RepoInfo::urls_const_iterator it = info.baseUrlsBegin();
196 * it != info.baseUrlsEnd(); ++it )
200 * // check whether to refresh metadata
201 * // if the check fails for this url, it throws, so another url will be checked
202 * if (checkIfToRefreshMetadata(info, *it, policy)!=RepoInfo::REFRESH_NEEDED)
205 * // do the actual refresh
207 * catch (const Exception & e)
210 * ERR << *it << " doesn't look good. Trying another url." << endl;
214 * handle("No more URLs.");
221 * \return state of repository
222 * \see RefreshCheckStatus
223 * \throws RepoUnknownTypeException
224 * \throws repo::RepoNoAliasException if can't figure an alias
225 * \throws Exception on unknown error
228 RefreshCheckStatus checkIfToRefreshMetadata( const RepoInfo &info,
230 RawMetadataRefreshPolicy policy = RefreshIfNeeded);
233 * \short Path where the metadata is downloaded and kept
235 * Given a repoinfo, tells where \ref RepoManager will download
236 * and keep the raw metadata.
238 * \param info Repository information
240 * \throws repo::RepoNoAliasException if can't figure an alias
242 Pathname metadataPath( const RepoInfo &info ) const;
246 * \short Path where the rpm packages are downloaded and kept
248 * Given a repoinfo, tells where \ref RepoProvidePackage will download
249 * and keep the .rpm files.
251 * \param info Repository information
253 * \throws repo::RepoNoAliasException if can't figure an alias
255 Pathname packagesPath( const RepoInfo &info ) const;
259 * \short Refresh local raw cache
261 * Will try to download the metadata
263 * In case of falure the metadata remains
266 * \throws repo::RepoNoUrlException if no urls are available.
267 * \throws repo::RepoNoAliasException if can't figure an alias
268 * \throws repo::RepoUnknownTypeException if the metadata is unknown
269 * \throws repo::RepoException if the repository is invalid
270 * (no valid metadata found at any of baseurls)
272 void refreshMetadata( const RepoInfo &info,
273 RawMetadataRefreshPolicy policy = RefreshIfNeeded,
274 const ProgressData::ReceiverFnc & progressrcv = ProgressData::ReceiverFnc() );
277 * \short Clean local metadata
279 * Empty local metadata.
281 * \throws repo::RepoNoAliasException if can't figure an alias
282 * \throws Exception on unknown error.
284 void cleanMetadata( const RepoInfo &info,
285 const ProgressData::ReceiverFnc & progressrcv = ProgressData::ReceiverFnc() );
288 * \short Clean local package cache
290 * Empty local directory with downloaded packages
292 * \throws repo::RepoNoAliasException if can't figure an alias
293 * \throws Exception on unknown error.
295 void cleanPackages( const RepoInfo &info,
296 const ProgressData::ReceiverFnc & progressrcv = ProgressData::ReceiverFnc() );
299 * \short Status of metadata cache
301 RepoStatus cacheStatus( const RepoInfo &info ) const;
304 * \short Refresh local cache
306 * Will try to build the cache from local metadata.
308 * If the cache exists it will be overwriten.
310 * \note the local metadata must be valid.
312 * \throws repo::RepoNoAliasException if can't figure
313 * an alias to look in cache
314 * \throws repo::RepoMetadataException if the metadata
315 * is not enough to build a cache (empty, incorrect, or
317 * \throws repo::RepoUnknownTypeException
318 * \throws parser::ParseException if parser encounters an error.
319 * \throws Exception on unknown error.
321 void buildCache( const RepoInfo &info,
322 CacheBuildPolicy policy = BuildIfNeeded,
323 const ProgressData::ReceiverFnc & progressrcv = ProgressData::ReceiverFnc() );
326 * \short clean local cache
328 * Clean the cached version of the metadata
330 * \note the local metadata must be valid.
332 * \throws repo::RepoNoAliasException if can't figure an alias to look in cache
333 * \throws cache::CacheRecordNotFoundException if the cache could not be
334 * cleaned because of repository record not found.
335 * \throws Exception on unknown error.
337 void cleanCache( const RepoInfo &info,
338 const ProgressData::ReceiverFnc & progressrcv = ProgressData::ReceiverFnc() );
341 * \short Whether a repository exists in cache
343 * \param RepoInfo to be checked.
345 bool isCached( const RepoInfo &info ) const;
349 * \short Load resolvables into the pool
351 * Creating from cache requires that the repository is
352 * refreshed (metadata downloaded) and cached
354 * \throws repo::RepoNoAliasException if can't figure an alias to look in cache
355 * \throw RepoNotCachedException When the source is not cached.
357 void loadFromCache( const RepoInfo &info,
358 const ProgressData::ReceiverFnc & progressrcv = ProgressData::ReceiverFnc() );
360 * \short Probe repo metadata type.
362 * \todo FIXME Should this be private?
364 repo::RepoType probe( const Url &url ) const;
368 * \short Adds a repository to the list of known repositories.
372 * \throws repo::RepoAlreadyExistsException If the repo clash some
373 * unique attribute like alias
374 * \throws RepoUnknownType
375 * If RepoManagerOptions::probe is true
376 * and repository type can't be determined.
377 * \throws RepoException
378 * If RepoManagerOptions::probe is true and access to the url fails.
379 * \throws Exception On other errors.
381 void addRepository( const RepoInfo &info,
382 const ProgressData::ReceiverFnc & progressrcv = ProgressData::ReceiverFnc() );
385 * \short Adds repositores from a repo file to the list of known repositories.
386 * \param url Url of the repo file
388 * \throws repo::RepoAlreadyExistsException If the repo clash some
389 * unique attribute like alias
390 * \throws MediaException If the access to the url fails
391 * \throws ParseException If the file parsing fails
392 * \throws RepoUnknownType If repository type can't be determined
393 * \throws RepoException ON other repository related errors
394 * \throws Exception On other errors.
396 void addRepositories( const Url &url,
397 const ProgressData::ReceiverFnc & progressrcv = ProgressData::ReceiverFnc() );
399 * \short Remove the best matching repository from known repos list
401 * \throws RepoNotFoundException If no repo match
403 void removeRepository( const RepoInfo & info,
404 const ProgressData::ReceiverFnc & progressrcv = ProgressData::ReceiverFnc() );
407 * \short Modify repository attributes
409 * \throws RepoAlreadyExistsException if the alias specified in newinfo
410 * is already used by another repository
411 * \throws RepoNotFoundException If no repo match
412 * \throws ParseException If the file parsing fails
413 * \throws Exception On other errors.
415 void modifyRepository( const std::string &alias,
416 const RepoInfo & newinfo,
417 const ProgressData::ReceiverFnc & progressrcv = ProgressData::ReceiverFnc() );
420 * \short Find a matching repository info
422 * \note if multiple repositories incorrectly share the
423 * same alias, the first one found will be returned.
425 * \param alias Repository alias
426 * \param progressrcv Progress reporting function
427 * \return RepoInfo of the found repository
428 * \throws RepoNotFoundException If no repo match the alias
429 * \throws ParseException If the file parsing fails
430 * \throws Exception On other errors.
432 RepoInfo getRepositoryInfo( const std::string &alias,
433 const ProgressData::ReceiverFnc & progressrcv = ProgressData::ReceiverFnc() );
436 * \short Find repository info by URL.
438 * \param url URL to find.
439 * \param urlview url::ViewOption to influence URL matching.
440 * \param progressrcv Progress receiver function.
441 * \return RepoInfo of the found repository.
443 * \note if multpile repositories incorrectly share the
444 * same URL, the first one found will be returned.
446 * \note the string representation of the URLs are compared.
447 * The \a urlview can be used to influence which
448 parts of the URL are to be compared.
450 * \throws RepoNotFoundException If no repo match
451 * \throws ParseException If the file parsing fails
452 * \throws Exception On other errors.
454 RepoInfo getRepositoryInfo( const Url & url,
455 const url::ViewOption & urlview = url::ViewOption::DEFAULTS,
456 const ProgressData::ReceiverFnc & progressrcv = ProgressData::ReceiverFnc() );
459 * Adds new service by it's alias and url
461 * \param alias unique identifier of the service
462 * \param url url to service
464 * \throws FIXME RepoAlreadyExistException and as reponame is service name
466 void addService( const std::string & alias, const Url& url );
471 * \param service service info
473 * \throws FIXME RepoAlreadyExistException and as reponame is service name
475 void addService( const ServiceInfo & service );
478 * Removes service specified by its name
480 * \param alias unique indientifier of the service to remove
482 * \throws RepoException if service is not found or file with ServiceInfo cannot be deleted
483 * \throws Exception if file contain more services and rewrite file failed
485 void removeService( const std::string & alias );
487 void removeService( const ServiceInfo & service );
490 * Gets true if no service is in RepoManager (so no one in specified location)
492 * \return true if any ServiceInfo is in RepoManager
494 bool serviceEmpty() const;
497 * Gets count of service in RepoManager (in specified location)
499 * \return count of service
501 ServiceSizeType serviceSize() const;
504 * Iterator to first service in internal storage.
505 * \note Iterator is immutable, so you cannot change pointed ServiceInfo
506 * \return Iterator to first service
508 ServiceConstIterator serviceBegin() const;
511 * Iterator to place behind last service in internal storage.
512 * \return iterator to end
514 ServiceConstIterator serviceEnd() const;
517 * Finds ServiceInfo by alias or return noService
519 * \param alias unique identifier of service
520 * \return information about service
522 ServiceInfo getService( const std::string & alias ) const;
525 * Refreshes all enabled services.
527 * \see refreshService(ServiceInfo)
529 void refreshServices();
532 * Refresh specific service.
534 * \param name service structure
535 * \throws MediaException If there's a problem downloading the repo index file.
537 void refreshService( const ServiceInfo & service );
540 * Modifies service file (rewrites it with new values) and underlying
541 * repositories if needed.
543 * Modifications of a service can lead to rewrite of all .repo files of
544 * contained repositories. Particularily, disabling a service (changing
545 * ServiceInfo::enabled() from true to false) will disable all contained
546 * repositories. Renaming of a service will modify the "service" key
547 * of all contained repositories.
549 * \param oldAlias Old alias of the service
550 * \param service ServiceInfo object containing new data
552 * \throws RepoException if sservice with oldAlias is not known
553 * \throws Exception if have problems with files
555 void modifyService(const std::string & oldAlias, const ServiceInfo & service);
559 * Functor thats filter RepoInfo by service which belongs to.
561 struct MatchServiceAlias
566 MatchServiceAlias( const std::string & alias_ ) : alias(alias_) {}
567 bool match( const RepoInfo & info ) { return info.service() == alias; }
573 * fill to output iterator repositories in service name. This output iterator can perform
574 * any action on with Repo or service Container, because it is sets and it isn't dynamic recreate.
576 * \note Don't use this function with RepoManager::removeRepository(), it will lead to segfaults
577 * due to invalidated internal iterators. FIXME can this be solved (using STL) so that this
578 * warning would not be needed?
580 * \param alias service alias
581 * \param out output iterator which get all the repositories belonging to
584 * example how set priority for each RepoInfo in this service:
587 * class ChangePriority
592 * ChangePriority(int prio) : priority(prio) {}
593 * // missing rewrite priority back via RepoManager::modifyRepo
594 * void doIt( RepoInfo info ) { info.setPriority(priority); }
597 * //somewhere in code
598 * ChangePriority changer(10);
599 * getRepositoriesInService(name,
600 * boost::make_function_output_iterator(
601 * bind(&ChangePriority::doIt, &changer, _1)));
604 template<typename OutputIterator>
605 void getRepositoriesInService( const std::string & alias,
606 OutputIterator out ) const
608 MatchServiceAlias filter(alias);
611 boost::make_filter_iterator(
612 bind(&MatchServiceAlias::match, filter, _1), repoBegin(), repoEnd()),
613 boost::make_filter_iterator(
614 bind(&MatchServiceAlias::match, filter, _1), repoEnd(), repoEnd()),
619 RepoStatus rawMetadataStatus( const RepoInfo &info );
620 void setCacheStatus( const RepoInfo &info, const RepoStatus &status );
623 * Update timestamp of repository index file for the specified repository \a info.
624 * Used in \ref checkIfToRefreshMetadata() for repo.refresh.delay feature.
626 void touchIndexFile(const RepoInfo & info);
631 /** Pointer to implementation */
632 RWCOW_pointer<Impl> _pimpl;
634 ///////////////////////////////////////////////////////////////////
636 /** \relates RepoManager Stream output */
637 std::ostream & operator<<( std::ostream & str, const RepoManager & obj );
639 /////////////////////////////////////////////////////////////////
641 ///////////////////////////////////////////////////////////////////
642 #endif // ZYPP2_REPOMANAGER_H