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 Service; //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<Service> 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 If repository type can't be determined
365 * \throws RepoException If the access to the url fails (while probing).
366 * \throws Exception On other errors.
368 void addRepository( const RepoInfo &info,
369 const ProgressData::ReceiverFnc & progressrcv = ProgressData::ReceiverFnc() );
372 * \short Adds repositores from a repo file to the list of known repositories.
373 * \param url Url of the repo file
375 * \throws repo::RepoAlreadyExistsException If the repo clash some
376 * unique attribute like alias
378 * \throws RepoAlreadyExistsException
379 * \throws MediaException If the access to the url fails
380 * \throws ParseException If the file parsing fails
381 * \throws RepoUnknownType If repository type can't be determined
382 * \throws RepoException ON other repository related errors
383 * \throws Exception On other errors.
385 void addRepositories( const Url &url,
386 const ProgressData::ReceiverFnc & progressrcv = ProgressData::ReceiverFnc() );
388 * \short Remove the best matching repository from known repos list
390 * \throws RepoNotFoundException If no repo match
392 void removeRepository( const RepoInfo & info,
393 const ProgressData::ReceiverFnc & progressrcv = ProgressData::ReceiverFnc() );
396 * \short Modify repository attributes
398 * \throws RepoAlreadyExistsException if the alias specified in newinfo
399 * is already used by another repository
400 * \throws RepoNotFoundException If no repo match
401 * \throws ParseException If the file parsing fails
402 * \throws Exception On other errors.
404 void modifyRepository( const std::string &alias,
405 const RepoInfo & newinfo,
406 const ProgressData::ReceiverFnc & progressrcv = ProgressData::ReceiverFnc() );
409 * \short Find a matching repository info
411 * \note if multiple repositories incorrectly share the
412 * same alias, the first one found will be returned.
414 * \param alias Repository alias
415 * \param progressrcv Progress reporting function
416 * \return RepoInfo of the found repository
417 * \throws RepoNotFoundException If no repo match the alias
418 * \throws ParseException If the file parsing fails
419 * \throws Exception On other errors.
421 RepoInfo getRepositoryInfo( const std::string &alias,
422 const ProgressData::ReceiverFnc & progressrcv = ProgressData::ReceiverFnc() );
425 * \short Find repository info by URL.
427 * \param url URL to find.
428 * \param urlview url::ViewOption to influence URL matching.
429 * \param progressrcv Progress receiver function.
430 * \return RepoInfo of the found repository.
432 * \note if multpile repositories incorrectly share the
433 * same URL, the first one found will be returned.
435 * \note the string representation of the URLs are compared.
436 * The \a urlview can be used to influence which
437 parts of the URL are to be compared.
439 * \throws RepoNotFoundException If no repo match
440 * \throws ParseException If the file parsing fails
441 * \throws Exception On other errors.
443 RepoInfo getRepositoryInfo( const Url & url,
444 const url::ViewOption & urlview = url::ViewOption::DEFAULTS,
445 const ProgressData::ReceiverFnc & progressrcv = ProgressData::ReceiverFnc() );
448 * Adds new service by it's name and url
450 * \param name unique name of service
451 * \param url url to service
453 * \throws FIXME RepoAlreadyExistException and as reponame is service name
455 void addService( const std::string& name, const Url& url );
460 * \param name service info
462 * \throws FIXME RepoAlreadyExistException and as reponame is service name
464 void addService( const Service& name );
467 * Removes service specified by its name
469 * \param name name of service to remove
471 * \throws RepoException if service is not found or file with Service cannot be deleted
472 * \throws Exception if file contain more services and rewrite file failed
474 void removeService( const std::string& name );
477 * Gets true if no service is in RepoManager (so no one in specified location)
479 * \return true if any Service is in RepoManager
481 bool serviceEmpty() const;
484 * Gets count of service in RepoManager (in specified location)
486 * \return count of service
488 ServiceSizeType serviceSize() const;
491 * Iterator to first service in internal storage.
492 * \note Iterator is immutable, so you cannot change pointed Service
493 * \return Iterator to first service
495 ServiceConstIterator serviceBegin() const;
498 * Iterator to place behind last service in internal storage.
499 * \return iterator to end
501 ServiceConstIterator serviceEnd() const;
504 * Finds Service by name or return noService
506 * \param name unique name of service
507 * \return information about Service
509 Service getService( const std::string& name ) const;
512 * Refreshs all services
513 * \see refreshService
515 void refreshServices();
518 * Refresh specific service.
519 * \throws Exception if cannot download file
520 * \param name service structure
522 void refreshService( const Service& name );
525 * modify service, rewrite Service to filesystem.
526 * If change Service name, then can escalate to rewrite all repositories which it contain.
528 * \param oldName oldName of service
529 * \param service Structure containing new datas
531 * \throws RepoException if sservice with oldName is not known
532 * \throws Exception if have problems with files
534 void modifyService(const std::string& oldName, const Service& service);
537 * Functor thats filter RepoInfo by service which belongs to.
539 struct MatchServiceName {
543 MatchServiceName( const std::string& name_ ) : name(name_) {}
544 bool match( const RepoInfo& info ) { return info.service()==name; }
548 * fill to output iterator repositories in service name. This output iterator can perform
549 * any action on with Repo or service Container, because it is sets and it isn't dynamic recreate.
550 * \param name service name
551 * \param out output iterator which get all repositories in Service
553 * example how set priority for each RepoInfo in this service:
556 * class ChangePriority{
560 * ChangePriority(int prio) : priority(prio) {}
561 * void doIt( RepoInfo info ) { info.setPriority(priority); } //missing rewrite priority back via RepoManager::modifyRepo
564 * //somewhere in code
565 * ChangePriority changer(10);
566 * getRepositoriesInService(name,getRepositoriesInService( name, boost::make_function_output_iterator(bind(&ChangePriority::doIt, &changer, _1))));
570 template<typename OutputIterator>
571 void getRepositoriesInService( const std::string& name, OutputIterator out ) const
573 MatchServiceName filter(name);
575 std::copy(boost::make_filter_iterator(bind(&MatchServiceName::match,
576 filter, _1),repoBegin(),repoEnd()), boost::make_filter_iterator(
577 bind(&MatchServiceName::match, filter, _1),repoEnd(),repoEnd()),
582 RepoStatus rawMetadataStatus( const RepoInfo &info );
583 void setCacheStatus( const RepoInfo &info, const RepoStatus &status );
586 * Update timestamp of repository index file for the specified repository \a info.
587 * Used in \ref checkIfToRefreshMetadata() for repo.refresh.delay feature.
589 void touchIndexFile(const RepoInfo & info);
594 /** Pointer to implementation */
595 RWCOW_pointer<Impl> _pimpl;
597 ///////////////////////////////////////////////////////////////////
599 /** \relates RepoManager Stream output */
600 std::ostream & operator<<( std::ostream & str, const RepoManager & obj );
602 /////////////////////////////////////////////////////////////////
604 ///////////////////////////////////////////////////////////////////
605 #endif // ZYPP2_REPOMANAGER_H