9fcbe1d411828f835b7e41c93a0eb2ea6558728a
[platform/upstream/libzypp.git] / zypp / RepoInfo.h
1 /*---------------------------------------------------------------------\
2 |                          ____ _   __ __ ___                          |
3 |                         |__  / \ / / . \ . \                         |
4 |                           / / \ V /|  _/  _/                         |
5 |                          / /__ | | | | | |                           |
6 |                         /_____||_| |_| |_|                           |
7 |                                                                      |
8 \---------------------------------------------------------------------*/
9 /** \file       zypp/RepoInfo.h
10  *
11 */
12 #ifndef ZYPP2_REPOSITORYINFO_H
13 #define ZYPP2_REPOSITORYINFO_H
14
15 #include <list>
16 #include <set>
17
18 #include "zypp/base/Iterator.h"
19 #include "zypp/APIConfig.h"
20
21 #include "zypp/Url.h"
22 #include "zypp/Locale.h"
23 #include "zypp/TriBool.h"
24 #include "zypp/base/EnumClass.h"
25 #include "zypp/repo/RepoType.h"
26 #include "zypp/repo/RepoVariables.h"
27
28 #include "zypp/repo/RepoInfoBase.h"
29
30 ///////////////////////////////////////////////////////////////////
31 namespace zypp
32 { /////////////////////////////////////////////////////////////////
33
34   ///////////////////////////////////////////////////////////////////
35   //
36   //    CLASS NAME : RepoInfo
37   //
38   /**
39    * \short What is known about a repository
40    *
41    * The class RepoInfo represents everything that
42    * is known about a software repository.
43    *
44    * It can be used to store information about known
45    * sources.
46    *
47    * This class tries to be compatible with the
48    * concept of a .repo file used by YUM and
49    * also available in the openSUSE build service.
50    * See <tt>man yum.conf</tt>.
51    *
52    * Example file
53    *
54    * \code
55    * [ruby]
56    * name=Ruby repository (openSUSE_10.2)
57    * type=rpm-md
58    * baseurl=http://software.opensuse.org/download/ruby/openSUSE_10.2/
59    *         http://some.opensuse.mirror/ruby/openSUSE_10.2/
60    * gpgcheck=1
61    * gpgkey=http://software.opensuse.org/openSUSE-Build-Service.asc
62    * enabled=1
63    * priority=10
64    * \endcode
65    *
66    * \note A RepoInfo is a hint about how
67    * to create a Repository.
68    *
69    * \note Name, baseUrls and mirrorUrl are subject to repo variable replacement
70    * (\see \ref RepoVariablesStringReplacer).
71    */
72   class RepoInfo : public repo::RepoInfoBase
73   {
74     friend std::ostream & operator<<( std::ostream & str, const RepoInfo & obj );
75
76     public:
77       RepoInfo();
78       virtual ~RepoInfo();
79
80       /** Represents no Repository (one with an empty alias). */
81       static const RepoInfo noRepo;
82
83     public:
84       /**
85        * The default priority (\c 99).
86        */
87       static unsigned defaultPriority();
88       /**
89        * Repository priority for solver.
90        * Some number between \c 1 (highest priority) and \c 99 (\ref defaultPriority).
91        */
92       unsigned priority() const;
93       /**
94        * Set repository priority for solver.
95        * A \c newval_r of \c 0 sets the default priority.
96        * \see \ref priority.
97        */
98       void setPriority( unsigned newval_r );
99
100       typedef std::list<Url>          url_set;
101       typedef url_set::size_type      urls_size_type;
102       typedef transform_iterator<repo::RepoVariablesUrlReplacer, url_set::const_iterator> urls_const_iterator;
103       /**
104        * whether repository urls are available
105        */
106       bool baseUrlsEmpty() const;
107       /**
108        * Whether there are manualy configured repository urls.
109        * If \c false, a mirrorlist might be used.
110        */
111       bool baseUrlSet() const;
112       /**
113        * number of repository urls
114        */
115       urls_size_type baseUrlsSize() const;
116       /**
117        * iterator that points at begin of repository urls
118        */
119       urls_const_iterator baseUrlsBegin() const;
120       /**
121        * iterator that points at end of repository urls
122        */
123       urls_const_iterator baseUrlsEnd() const;
124
125       /**
126        * Pars pro toto: The first repository url
127        */
128       Url url() const
129       { return( baseUrlsEmpty() ? Url() : *baseUrlsBegin()); }
130       /**
131        * Pars pro toto: The first repository raw url (no variables replaced)
132        */
133       Url rawUrl() const;
134
135       /**
136        * The complete set of repository urls
137        *
138        * These are either the configured baseurls, or if empty, the downloaded
139        * mirror list (\see \ref mirrorListUrl)
140        */
141       url_set baseUrls() const;
142       /**
143        * The complete set of raw repository urls (no variables replaced)
144        */
145       url_set rawBaseUrls() const;
146
147       /**
148        * Add a base url. \see baseUrls
149        * \param url The base url for the repository.
150        *
151        * To recreate the base URLs list, use \ref setBaseUrl(const Url &) followed
152        * by addBaseUrl().
153        */
154       void addBaseUrl( const Url &url );
155       /**
156        * Clears current base URL list and adds \a url.
157        */
158       void setBaseUrl( const Url &url );
159       /**
160        * Clears current base URL list and adds an \ref url_set.
161        */
162       void setBaseUrls( url_set urls );
163
164       /**
165        * \short Repository path
166        *
167        * Pathname relative to the base Url where the product/repository
168        * is located
169        *
170        * For media containing more than one product, or repositories not
171        * located at the root of the media it is important to know the path
172        * to the product directory relative to the media root. So a media
173        * verifier can be set for that media. You may also read it as
174        * <tt>baseUrl = url to mount</tt> and <tt>path = path on the
175        * mounted media</tt>.
176        *
177        * It is not mandatory, and the default is \c /.
178        *
179        * \note As a repository can have multiple Urls, the path is unique and
180        * the same for all Urls, so it is assumed all the Urls have the
181        * same media layout.
182        *
183        */
184       Pathname path() const;
185       /**
186        * set the product path. \see path()
187        * \param path the path to the product
188        */
189       void setPath( const Pathname &path );
190
191       /**
192        * Url of a file which contains a list of repository urls
193        */
194       Url mirrorListUrl() const;
195       /**
196        * The raw mirrorListUrl (no variables replaced).
197        */
198       Url rawMirrorListUrl() const;
199       /**
200        * Set mirror list url. \see mirrorListUrl
201        * \param url The base url for the list
202        */
203       void setMirrorListUrl( const Url &url );
204
205       /**
206        * Type of repository,
207        *
208        */
209       repo::RepoType type() const;
210       /**
211        * This allows to adjust the \ref  RepoType lazy, from \c NONE to
212        * some probed value, even for const objects.
213        *
214        * This is a NOOP if the current type is not \c NONE.
215        */
216       void setProbedType( const repo::RepoType &t ) const;
217       /**
218        * set the repository type \see type
219        * \param t
220        */
221       void setType( const repo::RepoType &t );
222
223       /**
224        * \short Path where this repo metadata was read from
225        *
226        * \note could be an empty pathname for repo
227        * infos created in memory.
228        */
229       Pathname metadataPath() const;
230       /**
231        * \short set the path where the local metadata is stored
232        *
233        * The path to the metadata of this repository
234        * was defined, or empty if nowhere.
235        *
236        * \param path directory path
237        */
238       void setMetadataPath( const Pathname &path );
239
240       /**
241        * \short Path where this repo packages are cached
242        */
243       Pathname packagesPath() const;
244       /**
245        * \short set the path where the local packages are stored
246        *
247        * \param path directory path
248        */
249       void setPackagesPath( const Pathname &path );
250
251
252       /** \name Repository gpgchecks
253        * How signature checking should be performed for this repo.
254        *
255        * The values are computed based in the settings of \c gpgcheck, \c repo_gpgcheck
256        * end \c pkg_gpgcheck in \c zypp.conf. Explicitly setting these values in the
257        * repositories \a .repo file will overwrite the defaults from \c zypp.conf for this
258        * repo.
259        *
260        * If \c gpgcheck is \c on (the default) we will check the signature of repo metadata
261        * (packages are secured via checksum inside the metadata). Using unsigned repos
262        * needs to be confirmed.
263        * Packages from signed repos are accepted if their checksum matches the checksum
264        * stated in the repo metadata.
265        * Packages from unsigned repos need a valid gpg signature, using unsigned packages
266        * needs to be confirmed.
267        *
268        * The above default behavior can be tuned by explicitly setting \c repo_gpgcheck
269        * and/or \c pkg_gpgcheck:
270        *
271        *   \c repo_gpgcheck = \c on same as the default.
272        *
273        *   \c repo_gpgcheck = \c off will silently accept unsigned repos. It will NOT turn of
274        *   signature checking on the whole, nevertheless it's not a secure setting.
275        *
276        *   \c pkg_gpgcheck = \c on will enforce the package signature checking and the need
277        *   to confirm unsigned packages for all repos (signed and unsigned).
278        *
279        *   \c pkg_gpgcheck = \c off will silently accept unsigned packages. It will NOT turn of
280        *   signature checking on the whole, nevertheless it's not a secure setting.
281        *
282        * If \c gpgCheck is \c off (not recommneded), no checks are performed. You can still
283        * enable them individually by setting \c repo_gpgcheck and/or \c pkg_gpgcheck to \c on.
284        *
285        * \code
286        *  R: check repo signature is mandatory, confirm unsigned repos
287        *  r: check repo signature, unsigned repos are ok but enforce p
288        *   : do not check repo signatures
289        *
290        *  P: check package signature always, confirm unsigned packages
291        *  p: like P for unsigned repos, accepted by checksum for signed repos
292        *  b: like p but accept unsigned packages
293        *   : do not check package signatures
294        *                    pkg_
295        * gpgcheck 1|     *       0       1
296        * ------------------------------------
297        * repo_   *1|     R/p     R/b     R/P
298        *          0|     r/p     r/b     r/P
299        *
300        *                    pkg_
301        * gpgcheck 0|     *       0       1
302        * ------------------------------------
303        * repo_   *0|                       P
304        *          1|     R       R       R/P
305        * \endcode
306        */
307       //@{
308       /** Whether default signature checking should be performed. */
309       bool gpgCheck() const;
310       /** Set the value for \ref gpgCheck (or \c indeterminate to use the default). */
311       void setGpgCheck( TriBool value_r );
312       /** \overload \deprecated legacy and for squid */
313       void setGpgCheck( bool value_r );
314
315       /** Whether the signature of repo metadata should be checked for this repo. */
316       bool repoGpgCheck() const;
317       /** Mandatory check (\ref repoGpgCheck is \c on) must ask to confirm using unsigned repos. */
318       bool repoGpgCheckIsMandatory() const;
319       /** Set the value for \ref repoGpgCheck (or \c indeterminate to use the default). */
320       void setRepoGpgCheck( TriBool value_r );
321
322       /** Whether the signature of rpm packages should be checked for this repo. */
323       bool pkgGpgCheck() const;
324       /** Mandatory check (\ref pkgGpgCheck is not \c off) must ask to confirm using unsigned packages. */
325       bool pkgGpgCheckIsMandatory() const;
326       /** Set the value for \ref pkgGpgCheck (or \c indeterminate to use the default). */
327       void setPkgGpgCheck( TriBool value_r );
328
329       /** Whether the repo metadata are signed and successfully validated or \c indeterminate if unsigned.
330        * The value is usually set by \ref repo::Downloader when retrieving the metadata.
331        */
332       TriBool validRepoSignature() const;
333       /** Set the value for \ref validRepoSignature (or \c indeterminate if unsigned). */
334       void setValidRepoSignature( TriBool value_r );
335
336       /** Some predefined settings (use like 'enum class \ref GpgCheck') */
337       struct GpgCheckDef {
338         enum Enum {
339           indeterminate,                //< not specified
340           On,                   //< 1** --gpgcheck
341           Strict,                       //< 111 --gpgcheck-strict
342           AllowUnsigned,                //< 100 --gpgcheck-allow-unsigned
343           AllowUnsignedRepo,    //< 10* --gpgcheck-allow-unsigned-repo
344           AllowUnsignedPackage, //< 1*0 --gpgcheck-allow-unsigned-package
345           Default,              //< *** --default-gpgcheck
346           Off,                  //< 0** --no-gpgcheck
347         };
348       };
349       typedef base::EnumClass<GpgCheckDef> GpgCheck;    ///< 'enum class GpgCheck'
350
351       /** Adjust *GpgCheck settings according to \a mode_r.
352        * \c GpgCheck::indeterminate will leave the settings as they are.
353        * \return whether setting were changed
354        */
355       bool setGpgCheck( GpgCheck mode_r );
356       //@}
357
358
359       /** Whether gpgkey URLs are defined */
360       bool gpgKeyUrlsEmpty() const;
361       /** Number of gpgkey URLs defined */
362       urls_size_type gpgKeyUrlsSize() const;
363
364       /** The list of gpgkey URLs defined for this repo */
365       url_set gpgKeyUrls() const;
366       /** The list of raw gpgkey URLs defined for this repo (no variables replaced) */
367       url_set rawGpgKeyUrls() const;
368       /** Set a list of gpgkey URLs defined for this repo */
369       void setGpgKeyUrls( url_set urls );
370
371       /** (leagcy API) The 1st gpgkey URL defined for this repo */
372       Url gpgKeyUrl() const;
373       /** (leagcy API) The 1st raw gpgkey URL defined for this repo (no variables replaced) */
374       Url rawGpgKeyUrl() const;
375       /** (leagcy API) Set the gpgkey URL defined for this repo */
376       void setGpgKeyUrl( const Url &gpgkey );
377
378
379       /**
380        * \short Whether packages downloaded from this repository will be kept in local cache
381        */
382       bool keepPackages() const;
383       /**
384        * \short Set if packaqes downloaded from this repository will be kept in local cache
385        *
386        * If the setting is true, all downloaded packages from this repository will be
387        * copied to the local raw cache.
388        *
389        * \param keep true (keep the downloaded packages) or false (delete them after installation)
390        *
391        */
392       void setKeepPackages( bool keep );
393
394       /**
395        * Gets name of the service to which this repository belongs or empty string
396        * if it has been added manually.
397        */
398       std::string service() const;
399       /**
400        * sets service which added this repository
401        */
402       void setService( const std::string& name );
403
404       /**
405        * Distribution for which is this repository meant.
406        */
407       std::string targetDistribution() const;
408       /**
409        * Sets the distribution for which is this repository meant. This is
410        * an in-memory value only, does not get written to the .repo file upon
411        * saving.
412        */
413       void setTargetDistribution(const std::string & targetDistribution);
414
415       /** Add content keywords */
416       void addContent( const std::string & keyword_r );
417       /** \overload add keywords from container */
418       template <class _Iterator>
419       void addContentFrom( _Iterator begin_r, _Iterator end_r )
420       { for_( it, begin_r, end_r ) addContent( *it ); }
421       /** \overload  */
422       template <class _Container>
423       void addContentFrom( const _Container & container_r )
424       { addContentFrom( container_r.begin(), container_r.end() ); }
425
426       /** Check for content keywords.
427        * Checking for an empty string returns whether content kewords are
428        * known at all. They may be missing due to missing metadata in disabled
429        * repos.
430        */
431       bool hasContent( const std::string & keyword_r = std::string() ) const;
432       /** \overload check for \b all keywords being present */
433       template <class _Iterator>
434       bool hasContentAll( _Iterator begin_r, _Iterator end_r ) const
435       { for_( it, begin_r, end_r ) if ( ! hasContent( *it ) ) return false; return true; }
436       /** \overload  */
437       template <class _Container>
438       bool hasContentAll( const _Container & container_r ) const
439       { return hasContentAll( container_r.begin(), container_r.end() ); }
440       /** \overload check for \b any keyword being present */
441       template <class _Iterator>
442       bool hasContentAny( _Iterator begin_r, _Iterator end_r ) const
443       { for_( it, begin_r, end_r ) if ( hasContent( *it ) ) return true; return false; }
444       /** \overload  */
445       template <class _Container>
446       bool hasContentAny( const _Container & container_r ) const
447       { return hasContentAny( container_r.begin(), container_r.end() ); }
448
449     public:
450       /** \name Repository license
451       */
452       //@{
453       /** Whether there is a license associated with the repo. */
454       bool hasLicense() const;
455
456       /** Whether the repo license has to be accepted, e.g. there is no
457        * no acceptance needed for openSUSE.
458        */
459       bool needToAcceptLicense() const;
460
461       /** Return the best license for the current (or a specified) locale. */
462       std::string getLicense( const Locale & lang_r = Locale() ) const;
463       std::string getLicense( const Locale & lang_r = Locale() ); // LEGACY API
464
465       /** Return the locales the license is available for.
466        * \ref Locale::noCode is included in case of \c license.txt which does
467        * not specify a specific locale.
468        */
469       LocaleSet getLicenseLocales() const;
470       //@}
471
472     public:
473       /**
474        * Write a human-readable representation of this RepoInfo object
475        * into the \a str stream. Useful for logging.
476        */
477       virtual std::ostream & dumpOn( std::ostream & str ) const;
478
479       /**
480        * Write this RepoInfo object into \a str in a <tr>.repo</tt> file format.
481        * Raw values, no variable replacement.
482        */
483       virtual std::ostream & dumpAsIniOn( std::ostream & str ) const;
484
485       /**
486        * Write an XML representation of this RepoInfo object.
487        * Repo variables replaced.
488        *
489        * \param str
490        * \param content this argument is ignored (used in other classed derived
491        *                from RepoInfoBase.
492        */
493       virtual std::ostream & dumpAsXmlOn( std::ostream & str, const std::string & content = "" ) const;
494
495       class Impl;
496     private:
497       friend class RepoManager;
498       /** Raw values for RepoManager */
499       void getRawGpgChecks( TriBool & g_r, TriBool & r_r, TriBool & p_r ) const;
500
501       /** Pointer to implementation */
502       RWCOW_pointer<Impl> _pimpl;
503   };
504   ///////////////////////////////////////////////////////////////////
505
506   /** \relates RepoInfo */
507   typedef shared_ptr<RepoInfo> RepoInfo_Ptr;
508   /** \relates RepoInfo */
509   typedef shared_ptr<const RepoInfo> RepoInfo_constPtr;
510   /** \relates RepoInfo */
511   typedef std::list<RepoInfo> RepoInfoList;
512
513   /** \relates RepoInfo Stream output */
514   std::ostream & operator<<( std::ostream & str, const RepoInfo & obj );
515
516   /** \relates RepoInfo::GpgCheck Stream output */
517   std::ostream & operator<<( std::ostream & str, const RepoInfo::GpgCheck & obj );
518
519   /////////////////////////////////////////////////////////////////
520 } // namespace zypp
521 ///////////////////////////////////////////////////////////////////
522 #endif // ZYPP2_REPOSITORYINFO_H