typo
[platform/upstream/libzypp.git] / zypp / Repository.h
1 /*---------------------------------------------------------------------\
2 |                          ____ _   __ __ ___                          |
3 |                         |__  / \ / / . \ . \                         |
4 |                           / / \ V /|  _/  _/                         |
5 |                          / /__ | | | | | |                           |
6 |                         /_____||_| |_| |_|                           |
7 |                                                                      |
8 \---------------------------------------------------------------------*/
9 /** \file       zypp/Repository.h
10  *
11 */
12 #ifndef ZYPP_SAT_REPOSITORY_H
13 #define ZYPP_SAT_REPOSITORY_H
14
15 #include <iosfwd>
16 #include "zypp/base/SafeBool.h"
17 #include "zypp/Pathname.h"
18 #include "zypp/sat/detail/PoolMember.h"
19 #include "zypp/sat/LookupAttr.h"     // LookupAttrTools.h included at EOF
20 #include "zypp/sat/Solvable.h"
21 #include "zypp/RepoInfo.h"
22 #include "zypp/Date.h"
23
24 ///////////////////////////////////////////////////////////////////
25 namespace zypp
26 { /////////////////////////////////////////////////////////////////
27
28     namespace detail
29     {
30       struct ByRepository;
31     }
32
33     ///////////////////////////////////////////////////////////////////
34     //
35     //  CLASS NAME : Repository
36     //
37     /** */
38     class Repository : protected sat::detail::PoolMember,
39                        private base::SafeBool<Repository>
40     {
41     public:
42         typedef filter_iterator<detail::ByRepository, sat::detail::SolvableIterator> SolvableIterator;
43         typedef sat::detail::size_type size_type;
44         typedef sat::detail::RepoIdType IdType;
45
46         typedef sat::ArrayAttr<std::string,std::string> Keywords;
47
48     public:
49         /** Default ctor creates \ref noRepository.*/
50         Repository()
51         : _id( sat::detail::noRepoId ) {}
52
53         /** \ref PoolImpl ctor. */
54         explicit Repository( IdType id_r )
55         : _id( id_r ) {}
56
57     public:
58         /** Represents no \ref Repository. */
59         static const Repository noRepository;
60
61 #ifndef SWIG // Swig treats it as syntax error
62         /** Evaluate \ref Repository in a boolean context (\c != \c noRepository). */
63         using base::SafeBool<Repository>::operator bool_type;
64 #endif
65         /** Reserved system repository alias \c @System. */
66         static const std::string & systemRepoAlias();
67
68         /** Return whether this is the system repository. */
69         bool isSystemRepo() const;
70
71     public:
72          /**
73           * Short unique string to identify a repo.
74           * ie: openSUSE-10.3
75           *
76           * If you are looking for a label to display
77           * see \ref name().
78           * ie: "openSUSE 10.3 Main repository"
79           *
80           */
81         std::string alias() const;
82
83         /** Label to display for this repo. */
84         std::string name() const;
85
86         /**
87          * Timestamp when this repository was generated
88          *
89          * Usually this value is calculated as the newer
90          * timestamp from the timestamp of all the resources
91          * that conform the repository's metadata.
92          *
93          * For example in a rpm-md repository, it would be
94          * the resource specified in the xml file whith
95          * the newest timestamp attribute (which is the
96          * timestamp of the file in the server ).
97          *
98          * The timestamp is 0 if the repository does not
99          * specify when it was generated.
100          *
101          */
102         Date generatedTimestamp() const;
103
104         /**
105          * Suggested expiration timestamp.
106          *
107          * Repositories can define an amount of time
108          * they expire, with the generated timestamp as
109          * the base point of time.
110          *
111          * Note that is the responsability of the repository
112          * to freshen the generated timestamp to tell the
113          * client that the repo is alive and updating the
114          * metadata.
115          *
116          * The timestamp is 0 if the repository does not specify
117          * an expiration date.
118          *
119          */
120         Date suggestedExpirationTimestamp() const;
121
122         /**
123          * repository keywords (tags)
124          */
125         Keywords keywords() const;
126
127         /**
128          * The suggested expiration date of this repository
129          * already passed
130          *
131          * rpm-md repositories can provide this tag using the
132          * expire extension tag:
133          * \see http://en.opensuse.org/Standards/Rpm_Metadata#SUSE_repository_info_.28suseinfo.xml.29.2C_extensions_to_repomd.xml
134          */
135         bool maybeOutdated() const;
136
137         /**
138          * if the repository claims to update something then
139          * it is an update repository
140          *
141          * This is implemented by looking at the repository updates
142          * tag.
143          * \see http://en.opensuse.org/Standards/Rpm_Metadata#SUSE_repository_info_.28suseinfo.xml.29.2C_extensions_to_repomd.xml
144          */
145         bool isUpdateRepo() const;
146
147         /**
148          * wether the repository claims to update something \ref prod
149          * with key \ref cpeid
150          *
151          * \see zypp::Product::cpeId()
152          *
153          * See http://cpe.mitre.org/ for more information on the
154          * Common Platform Enumearation.
155          */
156         bool providesUpdatesFor( const std::string &cpeid ) const;
157
158         /** Whether \ref Repository contains solvables. */
159         bool solvablesEmpty() const;
160
161         /** Number of solvables in \ref Repository. */
162         size_type solvablesSize() const;
163
164         /** Iterator to the first \ref Solvable. */
165         SolvableIterator solvablesBegin() const;
166
167         /** Iterator behind the last \ref Solvable. */
168         SolvableIterator solvablesEnd() const;
169
170     public:
171
172       /** Query class for Repository */
173       class ProductInfoIterator;
174
175       /**
176        * Get an iterator to the beginning of the repository
177        * compatible distros.
178        * \note This is only a hint. There is no guarantee that
179        * the repository is built for that product.
180        * \see Repository::ProductInfoIterator
181        */
182       ProductInfoIterator compatibleWithProductBegin() const;
183
184       /**
185        * Get an iterator to the end of the repository
186        * compatible distros.
187        * \see Repository::ProductInfoIterator
188        */
189       ProductInfoIterator compatibleWithProductEnd() const;
190
191       /**
192        * Get an iterator to the beginning of the repository
193        * compatible distros.
194        * \see Repository::ProductInfoIterator
195        */
196       ProductInfoIterator updatesProductBegin() const;
197
198       /**
199        * Get an iterator to the end of the repository
200        * compatible distros.
201        * \see Repository::ProductInfoIterator
202        */
203       ProductInfoIterator updatesProductEnd() const;
204
205     public:
206         /** Return any associated \ref RepoInfo. */
207         RepoInfo info() const;
208
209         /** Set \ref RepoInfo for this repository.
210          * \throws Exception if this is \ref noRepository
211          * \throws Exception if the \ref RepoInfo::alias
212          *         does not match the \ref Repository::name.
213          */
214         void setInfo( const RepoInfo & info_r );
215
216         /** Remove any \ref RepoInfo set for this repository. */
217         void clearInfo();
218
219     public:
220         /** Remove this \ref Repository from it's \ref Pool. */
221         void eraseFromPool();
222
223         /** Functor calling \ref eraseFromPool. */
224         struct EraseFromPool;
225
226    public:
227         /** Return next Repository in \ref Pool (or \ref noRepository). */
228         Repository nextInPool() const;
229
230    public:
231         /** \name Repository content manipulating methods.
232          * \todo maybe a separate Repository/Solvable content manip interface
233          * provided by the pool.
234          */
235         //@{
236         /** Load \ref Solvables from a solv-file.
237          * In case of an exception the repository remains in the \ref Pool.
238          * \throws Exception if this is \ref noRepository
239          * \throws Exception if loading the solv-file fails.
240          * \see \ref Pool::addRepoSolv and \ref Repository::EraseFromPool
241          */
242         void addSolv( const Pathname & file_r );
243
244          /** Load \ref Solvables from a helix-file.
245          * Supports loading of gzip compressed files (.gz). In case of an exception
246          * the repository remains in the \ref Pool.
247          * \throws Exception if this is \ref noRepository
248          * \throws Exception if loading the helix-file fails.
249          * \see \ref Pool::addRepoHelix and \ref Repository::EraseFromPool
250          */
251         void addHelix( const Pathname & file_r );
252
253        /** Add \c count_r new empty \ref Solvable to this \ref Repository. */
254         sat::Solvable::IdType addSolvables( unsigned count_r );
255         /** \overload Add only one new \ref Solvable. */
256         sat::Solvable::IdType addSolvable()
257             { return addSolvables( 1 ); }
258         //@}
259
260     public:
261         /** Expert backdoor. */
262         ::_Repo * get() const;
263         /** Expert backdoor. */
264         IdType id() const { return _id; }
265         /** satsolver internal priorities.
266          * Unlike the \ref RepoInfo priority which tries to be YUM conform
267          * (H[1-99]L), this one is the solvers internal priority representation.
268          * It is type \c int and as one might expect it, the higher the value
269          * the higher the priority. Subpriority is currently used to express
270          * media preferences (\see \ref MediaPriority).
271          */
272         //@{
273         int satInternalPriority() const;
274         int satInternalSubPriority() const;
275         //@}
276     private:
277 #ifndef SWIG // Swig treats it as syntax error
278         friend base::SafeBool<Repository>::operator bool_type() const;
279 #endif
280         bool boolTest() const { return get(); }
281     private:
282         IdType _id;
283     };
284     ///////////////////////////////////////////////////////////////////
285
286     /** \relates Repository Stream output */
287     std::ostream & operator<<( std::ostream & str, const Repository & obj );
288
289     /** \relates Repository */
290     inline bool operator==( const Repository & lhs, const Repository & rhs )
291     { return lhs.get() == rhs.get(); }
292
293     /** \relates Repository */
294     inline bool operator!=( const Repository & lhs, const Repository & rhs )
295     { return lhs.get() != rhs.get(); }
296
297     /** \relates Repository */
298     inline bool operator<( const Repository & lhs, const Repository & rhs )
299     { return lhs.get() < rhs.get(); }
300
301     ///////////////////////////////////////////////////////////////////
302     /**
303      * Query class for Repository related products
304      *
305      * The iterator does not provide a dereference
306      * operator so you can do * on it, but you can
307      * access the attributes of each related product
308      * directly from the iterator.
309      *
310      * \code
311      * for_( it, repo.compatibleWithProductBegin(), repo.compatibleWithProductEnd() )
312      * {
313      *   cout << it.cpeid() << endl;
314      * }
315      * \endcode
316      *
317      */
318     class Repository::ProductInfoIterator : public boost::iterator_adaptor<
319         Repository::ProductInfoIterator    // Derived
320         , sat::LookupAttr::iterator        // Base
321         , int                              // Value
322         , boost::forward_traversal_tag     // CategoryOrTraversal
323         , int                              // Reference
324     >
325     {
326       public:
327         ProductInfoIterator()
328         {}
329
330         /**
331          * Product label
332          */
333         std::string label() const;
334
335         /**
336          * The Common Platform Enumeration name
337          * for this product.
338          *
339          * See http://cpe.mitre.org
340          */
341         std::string cpeId() const;
342
343       private:
344         friend class Repository;
345         /** Hide ctor as just a limited set of attributes is valid. */
346         explicit ProductInfoIterator( sat::SolvAttr attr_r, Repository repo_r );
347
348       private:
349         friend class boost::iterator_core_access;
350         int dereference() const { return 0; }
351     };
352     ///////////////////////////////////////////////////////////////////
353
354     ///////////////////////////////////////////////////////////////////
355     //
356     //  CLASS NAME : Repository::EraseFromPool
357     //
358     /** Functor removing \ref Repository from it's \ref Pool.
359      *
360      * E.g. used as dispose function in. \ref AutoDispose
361      * to provide a convenient and exception safe temporary
362      * \ref Repository.
363      * \code
364      *  sat::Pool satpool;
365      *  MIL << "1 " << satpool << endl;
366      *  {
367      *    AutoDispose<Repository> tmprepo( (Repository::EraseFromPool()) );
368      *    *tmprepo = satpool.reposInsert( "A" );
369      *    tmprepo->addSolv( "sl10.1-beta7-packages.solv" );
370      *    DBG << "2 " << satpool << endl;
371      *    // Calling 'tmprepo.resetDispose();' here
372      *    // would keep the Repo.
373      *  }
374      *  MIL << "3 " << satpool << endl;
375      * \endcode
376      * \code
377      * 1 sat::pool(){0repos|2slov}
378      * 2 sat::pool(){1repos|2612slov}
379      * 3 sat::pool(){0repos|2slov}
380      * \endcode
381      * Leaving the block without calling <tt>tmprepo.resetDispose();</tt>
382      * before, will automatically remove the \ref Repo from it's \ref Pool.
383      */
384     struct Repository::EraseFromPool
385     {
386         void operator()( Repository repository_r ) const
387             { repository_r.eraseFromPool(); }
388     };
389     ///////////////////////////////////////////////////////////////////
390
391     ///////////////////////////////////////////////////////////////////
392     namespace detail
393     { /////////////////////////////////////////////////////////////////
394       ///////////////////////////////////////////////////////////////////
395       //
396       //        CLASS NAME : RepositoryIterator
397       //
398       /** */
399       class RepositoryIterator : public boost::iterator_adaptor<
400             RepositoryIterator                            // Derived
401                            , ::_Repo **                   // Base
402                            , Repository                   // Value
403                            , boost::forward_traversal_tag // CategoryOrTraversal
404                            , Repository                   // Reference
405                              >
406       {
407         public:
408           RepositoryIterator()
409           : RepositoryIterator::iterator_adaptor_( 0 )
410           {}
411
412           explicit RepositoryIterator( ::_Repo ** p )
413           : RepositoryIterator::iterator_adaptor_( p )
414           {}
415
416         private:
417           friend class boost::iterator_core_access;
418
419           Repository dereference() const
420           { return Repository( *base() ); }
421       };
422       ///////////////////////////////////////////////////////////////////
423       ///////////////////////////////////////////////////////////////////
424       //
425       //        CLASS NAME : ByRepository
426       //
427       /** Functor filtering \ref Solvable by \ref Repository.*/
428       struct ByRepository
429       {
430         public:
431           ByRepository( const Repository & repository_r ) : _repository( repository_r ) {}
432           ByRepository( sat::detail::RepoIdType id_r ) : _repository( id_r ) {}
433           ByRepository() {}
434
435           bool operator()( const sat::Solvable & slv_r ) const
436           { return slv_r.repository() == _repository; }
437
438         private:
439           Repository _repository;
440       };
441       ///////////////////////////////////////////////////////////////////
442       /////////////////////////////////////////////////////////////////
443     } // namespace detail
444     ///////////////////////////////////////////////////////////////////
445   /////////////////////////////////////////////////////////////////
446 } // namespace zypp
447 ///////////////////////////////////////////////////////////////////
448
449 // Late include as sat::ArrayAttr requires Repository.h
450 #include "zypp/sat/LookupAttrTools.h"
451
452 #endif // ZYPP_SAT_REPOSITORY_H