- Create the cache directly from the schema (installed) file.
[platform/upstream/libzypp.git] / zypp / Source.h
1 /*---------------------------------------------------------------------\
2 |                          ____ _   __ __ ___                          |
3 |                         |__  / \ / / . \ . \                         |
4 |                           / / \ V /|  _/  _/                         |
5 |                          / /__ | | | | | |                           |
6 |                         /_____||_| |_| |_|                           |
7 |                                                                      |
8 \---------------------------------------------------------------------*/
9 /** \file zypp/Source.h
10  *
11 */
12 #ifndef ZYPP_SOURCE_H
13 #define ZYPP_SOURCE_H
14
15 #include <iosfwd>
16 #include <string>
17
18 #include "zypp/base/PtrTypes.h"
19 #include "zypp/base/SafeBool.h"
20
21 #include "zypp/Package.h"
22 #include "zypp/Pathname.h"
23 #include "zypp/Url.h"
24 #include "zypp/Resolvable.h"
25
26 #include "zypp/media/MediaManager.h"
27
28 ///////////////////////////////////////////////////////////////////
29 namespace zypp
30 { /////////////////////////////////////////////////////////////////
31   namespace source
32   {
33     class SourceImpl;
34     DEFINE_PTR_TYPE(SourceImpl);
35
36     ///////////////////////////////////////////////////////////////////
37     //
38     //    CLASS NAME : SkipRequestedException
39     //
40     /**
41      * A specialized exception to inform the caller that user
42      * specifically asked the file/directory providing to be skipped.
43     */
44     class SkipRequestedException : public Exception {
45     public:
46       SkipRequestedException ( const std::string & msg_r ) : Exception( msg_r ) {}
47     };
48
49     ///////////////////////////////////////////////////////////////////
50     //
51     //    CLASS NAME : SourceUserRejectedException
52     //
53     /**
54      * A specialized exception to inform the caller that user
55      * specifically rejected the source
56      */
57     class SourceUserRejectedException : public Exception
58     {
59       public:
60         SourceUserRejectedException ( const std::string & msg_r ) : Exception( msg_r ) {}
61     };
62
63     /**
64      * A specialized exception to inform the caller that the metadata is invalid
65      */
66     class SourceMetadataException : public Exception
67     {
68       public:
69         SourceMetadataException ( const std::string & msg_r ) : Exception( msg_r ) {}
70     };
71     
72     /**
73      * A specialized exception to inform the caller that there is an IO error
74      */
75     class SourceIOException : public Exception
76     {
77       public:
78         SourceIOException ( const std::string & msg_r ) : Exception( msg_r ) {}
79     };
80     
81     /**
82      * A specialized exception to inform the caller that the source URL does not exist
83      */
84     class SourceNotFoundException : public Exception
85     {
86       public:
87         SourceNotFoundException ( const std::string & msg_r ) : Exception( msg_r ) {}
88     };
89     
90     /**
91      * A specialized exception to inform the caller that the source is not known
92      */
93     class SourceUnknownTypeException : public Exception
94     {
95       public:
96         SourceUnknownTypeException ( const std::string & msg_r ) : Exception( msg_r ) {}
97     };
98   }
99   class ResStore;
100
101   ///////////////////////////////////////////////////////////////////
102   //
103   //    CLASS NAME : Source
104   //
105   /**
106    * \note Source is a reference to the implementation. No COW
107    * is performed.
108   */
109   class Source_Ref : protected base::SafeBool<Source_Ref> /* private, but gcc refuses */
110   {
111     friend std::ostream & operator<<( std::ostream & str, const Source_Ref & obj );
112     friend bool operator==( const Source_Ref & lhs, const Source_Ref & rhs );
113     friend bool operator<( const Source_Ref & lhs, const Source_Ref & rhs );
114
115   public:
116     typedef source::SourceImpl     Impl;
117     typedef source::SourceImpl_Ptr Impl_Ptr;
118
119   public:
120
121     /** Default ctor: noSource.
122      * Real Sources are to be created via SourceFactory.
123     */
124     Source_Ref();
125
126     /** A dummy Source (Id \c 0) providing nothing, doing nothing.
127      * \todo provide a _constRef
128     */
129     static const Source_Ref noSource;
130
131     /** Validate Source_Ref in a boolean context.
132      * \c FALSE iff == noSource.
133     */
134     using base::SafeBool<Source_Ref>::operator bool_type;
135
136   public:
137     typedef unsigned long NumericId;
138
139     /** Runtime unique numeric Source Id. */
140     NumericId numericId() const;
141
142   public:
143
144   /**
145    * an aproxmate checksum that should change 
146    * when the source changes
147    * can be used to determine if 
148    * the source needs to be read again or not.
149    * (read as parse its metadata, not about downloading)
150    */
151     std::string checksum() const;
152     
153     /**
154      * aproximate age of the source, can be used to determine if 
155      * the source needs to be read again or not.
156      * (read as parse its metadata, not about downloading)
157      */
158     Date timestamp() const;
159     
160     /**
161      * wether this source provides or supports resolvables
162      * of certain kind.
163      */
164     bool hasResolvablesOfKind( const zypp::Resolvable::Kind &kind ) const;
165     
166     /**
167      * set of resolvable types the source can offer at this moment
168      */
169     std::set<zypp::Resolvable::Kind> resolvableKinds() const;
170     
171     /** Whether the ResStore is initialized.
172      * If we know that noone has seen the resolvables yet, we can skip
173      * them too, eg. when deleting a source. (#174840)
174      */      
175     bool resStoreInitialized() const;
176
177     /** All resolvables provided by this source. */
178     const ResStore & resolvables() const;
179
180     /** All resolvables of a given kind provided by this source. */
181     const ResStore resolvables(zypp::Resolvable::Kind kind) const;
182
183     const Pathname providePackage( Package::constPtr package );
184     
185     /** Provide a file to local filesystem */
186     const Pathname provideFile(const Pathname & file_r, const unsigned media_nr = 1);
187     const Pathname provideDirTree(const Pathname & dir_r, const unsigned media_nr = 1);
188                               
189     const void releaseFile(const Pathname & file_r, const unsigned media_nr = 1);
190     const void releaseDir(const Pathname & dir_r, const unsigned media_nr = 1, bool recursive = false);
191
192     bool enabled() const;
193
194     void enable();
195
196     void disable();
197
198     bool autorefresh() const;
199     void setAutorefresh( bool enable_r );
200     void refresh();
201
202     void storeMetadata(const Pathname & cache_dir_r);
203
204     /**
205      * User chosen identificaton, must be unique
206      */
207     std::string alias (void) const;
208     /**
209      * User chosen identificaton, must be unique
210      */
211     void setAlias (const std::string & alias_r);
212
213     /**
214      * Source type, like YaST or YUM
215      */
216     std::string type (void) const;
217
218     unsigned numberOfMedia(void) const;
219
220     //! from media.1/media
221     std::string vendor (void) const;
222     //! from media.1/media
223     std::string unique_id (void) const;
224
225     //! @name generic information get/set
226     //@{
227     //! runtime-unique, not persistent, a "handle" for Pkg::, string?!
228     std::string id (void) const;
229     void setId (const std::string id_r);
230     unsigned priority (void) const;
231     void setPriority (unsigned p);
232     unsigned priorityUnsubscribed (void) const;
233     void setPriorityUnsubscribed (unsigned p);
234     bool subscribed (void) const;
235     void setSubscribed (bool s);
236     const Pathname & cacheDir (void) const;
237     const std::list<Pathname> publicKeys();
238     //@}
239
240     //! @name for YaST
241     //@{
242     Url url (void) const;
243     /**
244      * required for the parse-metadata helper of libzypp-zmd-backend
245      * which gets local files to parse but the source is really remote.
246      */
247     void setUrl( const Url & url );
248     bool remote() const;
249     const Pathname & path (void) const;
250     bool baseSource() const;
251     //@}
252
253   public:
254     /**
255      * Change the media of the source (in case original media is not available)
256      * The media must be ready-to-use (in the same form as when passing to SourceImpl constructor)
257      */
258     void changeMedia(const media::MediaId & media_r, const Pathname & path_r);
259
260     /**
261      * Redirect the given media to the given URL instead of the standard one.
262      */
263     void redirect(unsigned media_nr, const Url & new_url);
264
265     /**
266      * Release all medias attached by the source
267      */
268     void release();
269
270     /**
271      * Reattach the source if it is not mounted, but downloaded,
272      * to different directory
273      *
274      * \throws Exception
275      */
276     void reattach(const Pathname &attach_point);
277
278     /**
279      * Provide a media verifier suitable for the given media number
280      */
281     media::MediaVerifierRef verifier(unsigned media_nr);
282
283   private:
284     friend base::SafeBool<Source_Ref>::operator bool_type() const;
285     /** \ref SafeBool test. */
286     bool boolTest() const
287     { return _pimpl != noSource._pimpl; }
288
289   private:
290     /** Factory */
291     friend class SourceFactory;
292     friend class source::SourceImpl;
293
294   private:
295     /** Factory ctor */
296     explicit
297     Source_Ref( const Impl_Ptr & impl_r );
298
299   private:
300     /** Pointer to implementation */
301     Impl_Ptr _pimpl;
302   };
303   ///////////////////////////////////////////////////////////////////
304
305   /** \relates Source Stream output. */
306   std::ostream & operator<<( std::ostream & str, const Source_Ref & obj );
307
308   /** \relates Source_Ref Equal if same implementation class. */
309   inline bool operator==( const Source_Ref & lhs, const Source_Ref & rhs )
310   { return lhs._pimpl == rhs._pimpl; }
311
312   /** \relates Source_Ref */
313   inline bool operator!=( const Source_Ref & lhs, const Source_Ref & rhs )
314   { return ! (lhs == rhs); }
315
316   /** \relates Source_Ref Order in std::conainer based on _pimpl. */
317   inline bool operator<( const Source_Ref & lhs, const Source_Ref & rhs )
318   { return lhs._pimpl < rhs._pimpl; }
319
320   /////////////////////////////////////////////////////////////////
321 } // namespace zypp
322 ///////////////////////////////////////////////////////////////////
323 #endif // ZYPP_SOURCE_H