- Create the cache directly from the schema (installed) file.
[platform/upstream/libzypp.git] / zypp / SourceManager.h
1 /*---------------------------------------------------------------------\
2 |                          ____ _   __ __ ___                          |
3 |                         |__  / \ / / . \ . \                         |
4 |                           / / \ V /|  _/  _/                         |
5 |                          / /__ | | | | | |                           |
6 |                         /_____||_| |_| |_|                           |
7 |                                                                      |
8 \---------------------------------------------------------------------*/
9 /** \file       zypp/SourceManager.h
10  *
11 */
12 #ifndef ZYPP_SOURCEMANAGER_H
13 #define ZYPP_SOURCEMANAGER_H
14
15 #include <iosfwd>
16 #include <list>
17 #include <map>
18
19 #include "zypp/base/ReferenceCounted.h"
20 #include "zypp/base/NonCopyable.h"
21 //#include "zypp/base/Iterator.h"
22
23 #include "zypp/base/Gettext.h" // move with FailedSourcesRestoreException & SourcesAlreadyRestoredException
24
25 #include "zypp/source/SourceInfo.h"
26
27 #include "zypp/Source.h"
28 #include "zypp/Url.h"
29 #include "zypp/Pathname.h"
30
31 ///////////////////////////////////////////////////////////////////
32 namespace zypp
33 { /////////////////////////////////////////////////////////////////
34
35   DEFINE_PTR_TYPE(SourceManager)
36
37   /** \todo move to separate header file.*/
38   class FailedSourcesRestoreException : public Exception
39   {
40     public:
41       FailedSourcesRestoreException()
42       : Exception(N_("Unable to restore all sources."))
43       , _summary()
44       , _translatedSummary()
45       , _aliases()
46       {}
47       virtual ~FailedSourcesRestoreException() throw() {};
48
49       void append( std::string source, std::string alias, const Exception& problem );
50       bool empty() const;
51       
52       std::set<std::string> aliases() const;
53       
54     protected:
55       virtual std::ostream & dumpOn( std::ostream & str ) const;
56       virtual std::ostream & dumpOnTranslated( std::ostream & str ) const;
57     private:
58       std::string _summary;
59       std::string _translatedSummary;
60       std::set<std::string> _aliases;
61   };
62
63   /** \todo move to separate header file.*/
64   class SourcesAlreadyRestoredException : public Exception
65   {
66     public:
67       SourcesAlreadyRestoredException()
68       : Exception(N_("At least one source already registered, stored sources cannot be restored."))
69       {}
70       virtual ~SourcesAlreadyRestoredException() throw() {};
71   };
72
73   ///////////////////////////////////////////////////////////////////
74   //
75   //    CLASS NAME : SourceManager
76   //
77   /** Provide the known Sources.
78    * \todo make it a resl singleton
79    * \todo throwing findSource is not acceptable, return either
80    * a Source or noSource.
81    * \todo Make restore either void or nonthrowing, but two ways of
82    * error reporting is bad.
83   */
84   class SourceManager : public base::ReferenceCounted, private base::NonCopyable
85   {
86     friend std::ostream & operator<<( std::ostream & str, const SourceManager & obj );
87
88   public:
89     /** Singleton access */
90     static SourceManager_Ptr sourceManager();
91
92   public:
93     /** Dtor */
94     ~SourceManager();
95
96   public:
97     /** Runtime unique numeric Source Id. */
98     typedef Source_Ref::NumericId SourceId;
99
100   private:
101     /** exposition only */
102     typedef std::map<SourceId, Source_Ref> SourceMap;
103
104     /** \name Iterate over all (SourceId,Source_Ref) pairs. */
105     //@{
106     typedef SourceMap::const_iterator const_iterator;
107
108     const_iterator begin() const;
109
110     const_iterator end() const;
111     //@}
112
113   public:
114     /** \name Iterate over all known SourceIds. */
115     //@{
116     typedef MapKVIteratorTraits<SourceMap>::Key_const_iterator SourceId_const_iterator;
117
118     SourceId_const_iterator SourceId_begin() const;
119
120     SourceId_const_iterator SourceId_end() const;
121     //@}
122
123   public:
124     /** \name Iterate over all known Sources. */
125     //@{
126     typedef MapKVIteratorTraits<SourceMap>::Value_const_iterator Source_const_iterator;
127
128     Source_const_iterator Source_begin() const;
129
130     Source_const_iterator Source_end() const;
131     //@}
132
133   public:
134
135     /**
136      * Reset the manager - discard the sources database,
137      * do not store the changes to the persistent store.
138      *
139      * \throws Exception
140      */
141     void reset() ;
142
143     /**
144      * List the known aliases and urls ( no need to restore first )
145      *
146      * \throws Exception
147      */
148     source::SourceInfoList knownSourceInfos(const Pathname &root_r);
149     
150     /**
151      * Store the current state to the given path
152      *
153      * \param root_r root path for storing the source definitions
154      * \param metadata_cache if true, this will also store/update
155      * metadata caches for the sources.
156      *
157      * \throws Exception
158      */
159     void store(Pathname root_r, bool metadata_cache );
160
161     /**
162      * Restore the sources state to the given path. If the sources
163      * database is not empty, it throws an exception
164      *
165      * \param use_caches  if true, source creation will try to use source cache
166      * and it's behavior on autorefresh. If false, it will not use
167      * the cache at all.
168      * \param alias_filter  if non-empty, restore only a source matching
169      * this alias.
170      * \param url_filter  if non-empty, restore only a source matching
171      * this url.
172      *
173      * The alias_filter take precedence over the url_filter.
174      *
175      * \return true on success
176      *
177      * \throws Exception
178      */
179     bool restore(Pathname root_r, bool use_caches = true, const std::string &alias_filter = "", const std::string &url_filter = "" );
180
181     /**
182      * Find a source with a specified ID
183      *
184      * \throws Exception
185      */
186     Source_Ref findSource(SourceId id);
187
188     /**
189      * Find a source with a specified alias
190      *
191      * \throws Exception
192      */
193     Source_Ref findSource(const std::string & alias_r);
194
195     /**
196      * Find a source with a specified URL.
197      * URLs are unique in zenworks but NOT in zypp.
198      * A bug in SL 10.1 causes alias mismatches so we have to resort to URLs.
199      * #177543
200      *
201      * \throws Exception
202      */
203     Source_Ref findSourceByUrl(const Url & url_r);
204
205     /**
206      * Return the list of the currently enabled sources
207      *
208      */
209     std::list<SourceId> enabledSources() const;
210
211     /**
212      * Return ids of all sources
213      */
214     std::list<SourceId> allSources() const;
215
216     /** Add a new source.
217      * An attempt to add Source_Ref::noSource does nothing but
218      * returning Source_Ref::noSource.numericId(). Thus it
219      * results in adding no Source.
220      */
221     SourceId addSource(Source_Ref source_r);
222
223     /** Rename an existing source by Alias. */
224     void renameSource( SourceId id, const std::string & new_alias_r );
225     
226     /** Remove an existing source by ID. */
227     void removeSource(SourceId id);
228
229     /** Remove an existing source by Alias. */
230     void removeSource(const std::string & alias_r);
231
232     /** Remove an existing source by Url. */
233     void removeSourceByUrl(const Url & url_r);
234
235     /**
236      * Release all medias held by all sources
237      *
238      * \throws Exception
239      */
240     void releaseAllSources();
241
242     /**
243      * Reattach all sources which are not mounted, but downloaded,
244      * to different directory
245      *
246      * \throws Exception
247      */
248     void reattachSources(const Pathname & attach_point);
249
250     /**
251      * Disable all registered sources
252      */
253     void disableAllSources();
254
255     /**
256      * Helper function to disable all sources in the persistent
257      * store on the given location. Does not manipulate with
258      * the current status of the source manager.
259      *
260      * \throws Exception ...
261      */
262     static void disableSourcesAt( const Pathname & root );
263
264   private:
265     /** Singleton ctor */
266     SourceManager();
267   };
268   ///////////////////////////////////////////////////////////////////
269
270   /** \relates SourceManager Stream output */
271   std::ostream & operator<<( std::ostream & str, const SourceManager & obj );
272
273   /////////////////////////////////////////////////////////////////
274 } // namespace zypp
275 ///////////////////////////////////////////////////////////////////
276 #endif // ZYPP_SOURCEMANAGER_H