- implement repo.add.probe option
[platform/upstream/libzypp.git] / zypp / RepoManager.h
1 /*---------------------------------------------------------------------\
2 |                          ____ _   __ __ ___                          |
3 |                         |__  / \ / / . \ . \                         |
4 |                           / / \ V /|  _/  _/                         |
5 |                          / /__ | | | | | |                           |
6 |                         /_____||_| |_| |_|                           |
7 |                                                                      |
8 \---------------------------------------------------------------------*/
9 /** \file       zypp/RepoManager.h
10  *
11 */
12 #ifndef ZYPP_REPOMANAGER_H
13 #define ZYPP_REPOMANAGER_H
14
15 #include <iosfwd>
16 #include <list>
17
18 #include "zypp/base/PtrTypes.h"
19 //#include "zypp/base/ReferenceCounted.h"
20 //#include "zypp/base/NonCopyable.h"
21 #include "zypp/Pathname.h"
22 #include "zypp/ZConfig.h"
23 #include "zypp/Repository.h"
24 #include "zypp/RepoInfo.h"
25 #include "zypp/repo/RepoException.h"
26 #include "zypp/repo/RepoType.h"
27 #include "zypp/RepoStatus.h"
28 #include "zypp/ProgressData.h"
29
30 ///////////////////////////////////////////////////////////////////
31 namespace zypp
32 { /////////////////////////////////////////////////////////////////
33
34    /**
35     * Parses \a repo_file and returns a list of \ref RepoInfo objects
36     * corresponding to repositories found within the file.
37     * 
38     * \param repo_file Valid URL of the repo file.
39     * \return found list<RepoInfo>
40     *
41     * \throws MediaException If the access to the url fails
42     * \throws ParseException If the file parsing fails
43     * \throws Exception On other errors.
44     */
45    std::list<RepoInfo> readRepoFile(const Url & repo_file);
46
47   /**
48    * Repo manager settings.
49    * Settings default to ZYpp global settings.
50    */
51   struct RepoManagerOptions
52   {
53     RepoManagerOptions();
54     
55     Pathname repoCachePath;
56     Pathname repoRawCachePath;
57     Pathname knownReposPath;
58   };
59
60   /**
61    * \short creates and provides information about known sources.
62    *
63    */
64   class RepoManager
65   {
66     friend std::ostream & operator<<( std::ostream & str, const RepoManager & obj );
67
68   public:
69     /** Implementation  */
70     class Impl;
71
72   public:
73    RepoManager( const RepoManagerOptions &options = RepoManagerOptions() );
74    /** Dtor */
75     ~RepoManager();
76     
77     enum RawMetadataRefreshPolicy
78     {
79       RefreshIfNeeded,
80       RefreshForced
81     };
82     
83     enum CacheBuildPolicy
84     {
85       BuildIfNeeded,
86       BuildForced
87     };
88     
89     enum RepoRemovePolicy
90     {
91       
92     };
93
94    /**
95     * \short List known repositories.
96     *
97     * The known repositories are read from
98     * \ref RepoManagerOptions::knownReposPath passed on the Ctor.
99     * Which defaults to ZYpp global settings.
100     * \return found list<RepoInfo>
101     */
102    std::list<RepoInfo> knownRepositories() const;
103
104    /**
105     * \short Status of local metadata
106     */
107     RepoStatus metadataStatus( const RepoInfo &info ) const;
108
109    /**
110     * \short Refresh local raw cache
111     *
112     * Will try to download the metadata
113     *
114     * In case of falure the metadata remains
115     * as it was before.
116     *
117     * \throws repo::RepoNoUrlException if no urls are available.
118     * \throws repo::RepoNoAliasException if can't figure an alias
119     * \throws repo::RepoUnknownTypeException if the metadata is unknown
120     * \throws repo::RepoException if the repository is invalid
121     *         (no valid metadata found at any of baseurls) 
122     * \throws Exception on unknown error.
123     */
124    void refreshMetadata( const RepoInfo &info,
125                          RawMetadataRefreshPolicy policy = RefreshIfNeeded,
126                          const ProgressData::ReceiverFnc & progressrcv = ProgressData::ReceiverFnc() );
127    
128    /**
129     * \short Clean local metadata
130     *
131     * Empty local metadata.
132     *
133     * \throws repo::RepoNoAliasException if can't figure an alias
134     * \throws Exception on unknown error.
135     */
136    void cleanMetadata( const RepoInfo &info,
137                        const ProgressData::ReceiverFnc & progressrcv = ProgressData::ReceiverFnc() );
138
139    /**
140     * \short Status of metadata cache
141     */
142     RepoStatus cacheStatus( const RepoInfo &info ) const;
143    
144    /**
145     * \short Refresh local cache
146     *
147     * Will try to build the cache from
148     * local metadata.
149     *
150     * If the cache exists it will be overwriten.
151     *
152     * \note the local metadata must be valid.
153     *
154     * \throws repo::RepoNoAliasException if can't figure 
155     * an alias to look in cache
156     *
157     * \throws repo::RepoMetadataException if the metadata
158     * is not enough to build a cache (empty, incorrect, or
159     * refresh needed)
160     * \throws Exception on unknown error.
161     */
162    void buildCache( const RepoInfo &info,
163                     CacheBuildPolicy policy = BuildIfNeeded,
164                     const ProgressData::ReceiverFnc & progressrcv = ProgressData::ReceiverFnc() );
165    
166    /**
167     * \short clean local cache
168     *
169     * Clean the cached version of the metadata
170     *
171     * \note the local metadata must be valid.
172     *
173     * \throws repo::RepoNoAliasException if can't figure an alias to look in cache
174     * \throws Exception on unknown error.
175     */
176    void cleanCache( const RepoInfo &info,
177                     const ProgressData::ReceiverFnc & progressrcv = ProgressData::ReceiverFnc() );
178    
179    /**
180     * \short Whether a repository exists in cache
181     *
182     * \param RepoInfo to be checked.
183     */
184     bool isCached( const RepoInfo &info ) const;
185    
186    /**
187     * \short Create a repository object from the cache data
188     *
189     * Creating from cache requires that the repository is
190     * refreshed (metadata downloaded) and cached
191     *
192     * \throws repo::RepoNoAliasException if can't figure an alias to look in cache
193     * \throw RepoNotCachedException When the source is not cached.
194     */
195    Repository createFromCache( const RepoInfo &info,
196                                const ProgressData::ReceiverFnc & progressrcv = ProgressData::ReceiverFnc() );
197
198    /**
199     * \short Create a repository object from raw metadata
200     *
201     * Creating from cache requires that the repository is
202     * refreshed (metadata downloaded)
203     *
204     * \throw Exception If there are errors parsing the
205     * raw metadata
206     */
207    Repository createFromMetadata( const RepoInfo &info,
208                                   const ProgressData::ReceiverFnc & progressrcv = ProgressData::ReceiverFnc() );
209
210    /**
211     * \short Probe repo metadata type.
212     *
213     * \todo FIXME Should this be private?
214     */
215    repo::RepoType probe( const Url &url ) const;
216    
217    
218    /**
219     * \short Adds a repository to the list of known repositories.
220     *
221     * 
222     *
223     * \throws repo::RepoAlreadyExistsException If the repo clash some 
224     * unique attribute like alias
225     * \throws MediaException If the access to the url fails
226     * \throws ParseException If the file parsing fails
227     * \throws RepoUnknownType If repository type can't be determined
228     * \throws RepoException ON other repository related errors
229     * \throws Exception On other errors.
230     */
231    void addRepository( const RepoInfo &info,
232                        const ProgressData::ReceiverFnc & progressrcv = ProgressData::ReceiverFnc() );
233    
234    /**
235     * \short Adds repositores from a repo file to the list of known repositories.
236     * \param url Url of the repo file
237     * 
238     * \throws repo::RepoAlreadyExistsException If the repo clash some 
239     * unique attribute like alias
240     *
241     * \throws RepoAlreadyExistsException
242     * \throws MediaException If the access to the url fails
243     * \throws ParseException If the file parsing fails
244     * \throws RepoUnknownType If repository type can't be determined
245     * \throws RepoException ON other repository related errors
246     * \throws Exception On other errors.
247     */
248     void addRepositories( const Url &url,
249                          const ProgressData::ReceiverFnc & progressrcv = ProgressData::ReceiverFnc() );
250     /**
251      * \short Remove the best matching repository from known repos list
252      *
253      * \throws RepoNotFoundException If no repo match
254      */
255     void removeRepository( const RepoInfo & info,
256                            const ProgressData::ReceiverFnc & progressrcv = ProgressData::ReceiverFnc() );
257     
258     /**
259      * \short Modify repository attributes
260      *
261      * \throws RepoNotFoundException If no repo match
262      * \throws ParseException If the file parsing fails
263      * \throws Exception On other errors.
264      */
265     void modifyRepository( const std::string &alias,
266                            const RepoInfo & newinfo,
267                            const ProgressData::ReceiverFnc & progressrcv = ProgressData::ReceiverFnc() );
268
269     /**
270      * \short Find a matching repository info
271      *
272      * \note if multiple repositories incorrectly share the
273      * same alias, the first one found will be returned.
274      *
275      * \param alias Repository alias
276      * \param progressrcv Progress reporting function
277      * \return RepoInfo of the found repository
278      * \throws RepoNotFoundException If no repo match the alias
279      * \throws ParseException If the file parsing fails
280      * \throws Exception On other errors.
281      */
282     RepoInfo getRepositoryInfo( const std::string &alias,
283                                 const ProgressData::ReceiverFnc & progressrcv = ProgressData::ReceiverFnc() );
284     
285     /**
286      * \short Find repository info by URL.
287      *
288      * \param url URL to find.
289      * \param urlview url::ViewOption to influence URL matching.
290      * \param progressrcv Progress receiver function.
291      * \return RepoInfo of the found repository.
292      *
293      * \note if multpile repositories incorrectly share the
294      * same URL, the first one found will be returned.
295      *
296      * \note the string representation of the URLs are compared.
297      *       The \a urlview can be used to influence which
298              parts of the URL are to be compared.
299      *
300      * \throws RepoNotFoundException If no repo match
301      * \throws ParseException If the file parsing fails
302      * \throws Exception On other errors.
303      */
304     RepoInfo getRepositoryInfo( const Url & url,
305                                 const url::ViewOption & urlview = url::ViewOption::DEFAULTS,
306                                 const ProgressData::ReceiverFnc & progressrcv = ProgressData::ReceiverFnc() );
307
308   protected:
309     RepoStatus rawMetadataStatus( const RepoInfo &info );
310     RepoStatus cacheStatus( const RepoInfo &info );
311   public:
312
313   private:
314     /** Pointer to implementation */
315     RWCOW_pointer<Impl> _pimpl;
316   };
317   ///////////////////////////////////////////////////////////////////
318
319   /** \relates RepoManager Stream output */
320   std::ostream & operator<<( std::ostream & str, const RepoManager & obj );
321
322   /////////////////////////////////////////////////////////////////
323 } // namespace zypp
324 ///////////////////////////////////////////////////////////////////
325 #endif // ZYPP2_REPOMANAGER_H