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