Imported Upstream version 17.16.0
[platform/upstream/libzypp.git] / zypp / sat / Solvable.h
1 /*---------------------------------------------------------------------\
2 |                          ____ _   __ __ ___                          |
3 |                         |__  / \ / / . \ . \                         |
4 |                           / / \ V /|  _/  _/                         |
5 |                          / /__ | | | | | |                           |
6 |                         /_____||_| |_| |_|                           |
7 |                                                                      |
8 \---------------------------------------------------------------------*/
9 /** \file       zypp/sat/Solvable.h
10  *
11 */
12 #ifndef ZYPP_SAT_SOLVABLE_H
13 #define ZYPP_SAT_SOLVABLE_H
14
15 #include <iosfwd>
16
17 #include "zypp/sat/detail/PoolMember.h"
18 #include "zypp/sat/SolvAttr.h"
19 #include "zypp/ResTraits.h"
20 #include "zypp/IdString.h"
21 #include "zypp/Edition.h"
22 #include "zypp/Arch.h"
23 #include "zypp/Dep.h"
24 #include "zypp/Capabilities.h"
25 #include "zypp/Capability.h"
26 #include "zypp/Locale.h"
27
28 ///////////////////////////////////////////////////////////////////
29 namespace zypp
30 {
31   class ByteCount;
32   class CheckSum;
33   class CpeId;
34   class Date;
35   class OnMediaLocation;
36   ///////////////////////////////////////////////////////////////////
37   namespace sat
38   {
39     ///////////////////////////////////////////////////////////////////
40     /// \class Solvable
41     /// \brief  A \ref Solvable object within the sat \ref Pool.
42     ///
43     /// \note Unfortunately libsolv combines the objects kind and
44     /// name in a single identifier \c "pattern:kde_multimedia",
45     /// \b except for packages and source packes. They are not prefixed
46     /// by any kind string. Instead the architecture is abused to store
47     /// \c "src" and \c "nosrc" values.
48     ///
49     /// \ref Solvable will hide this inconsistency by treating source
50     /// packages as an own kind of solvable and map their arch to
51     /// \ref Arch_noarch.
52     ///////////////////////////////////////////////////////////////////
53     class Solvable : protected detail::PoolMember
54     {
55     public:
56       typedef sat::detail::SolvableIdType IdType;
57
58     public:
59       /** Default ctor creates \ref noSolvable.*/
60       Solvable()
61       : _id( detail::noSolvableId )
62       {}
63
64       /** \ref PoolImpl ctor. */
65       explicit Solvable( IdType id_r )
66       : _id( id_r )
67       {}
68
69     public:
70       /** Represents no \ref Solvable. */
71       static const Solvable noSolvable;
72
73       /** Evaluate \ref Solvable in a boolean context (\c != \c noSolvable). */
74       explicit operator bool() const
75       { return get(); }
76
77     public:
78       /** The identifier.
79        * This is the solvables \ref name, \b except for packages and
80        * source packes, prefixed by it's \ref kind.
81        */
82       IdString ident()const;
83
84       /** The Solvables ResKind. */
85       ResKind kind()const;
86
87       /** Test whether a Solvable is of a certain \ref ResKind.
88        * The test is far cheaper than actually retrieving and
89        * comparing the \ref kind.
90        */
91       bool isKind( const ResKind & kind_r ) const;
92       /** \overload */
93       template<class TRes>
94       bool isKind() const
95       { return isKind( resKind<TRes>() ); }
96       /** \overload Extend the test to a range of \ref ResKind. */
97       template<class TIterator>
98       bool isKind( TIterator begin, TIterator end ) const
99       { for_( it, begin, end ) if ( isKind( *it ) ) return true; return false; }
100
101       /** The name (without any ResKind prefix). */
102       std::string name() const;
103
104       /** The edition (version-release). */
105       Edition edition() const;
106
107       /** The architecture. */
108       Arch arch() const;
109
110       /** The vendor. */
111       IdString vendor() const;
112
113       /** The \ref Repository this \ref Solvable belongs to. */
114       Repository repository() const;
115       /** The repositories \ref RepoInfo. */
116       RepoInfo repoInfo() const;
117
118       /** Return whether this \ref Solvable belongs to the system repo.
119        * \note This includes the otherwise hidden systemSolvable.
120        */
121       bool isSystem() const;
122
123       /** Whether this is known to be installed on behalf of a user request.
124        * \note Returns \c false for non-system (uninstalled) solvables.
125        */
126       bool onSystemByUser() const;
127
128       /** Whether this is known to be automatically installed (as dependency of a user request package).
129        * \note Returns \c false for non-system (uninstalled) solvables.
130        */
131       bool onSystemByAuto() const;
132
133       /** Whether an installed solvable with the same \ref ident is flagged as AutoInstalled. */
134       bool identIsAutoInstalled() const
135       { return identIsAutoInstalled( ident() ); }
136       /** \overload static version */
137       static bool identIsAutoInstalled( const IdString & ident_r );
138
139       /** Whether this solvable triggers the reboot-needed hint if installed/updated. */
140       bool isNeedreboot() const;
141
142       /** Whether different versions of this package can be installed at the same time.
143        * Per default \c false. \see also \ref ZConfig::multiversion.
144        */
145       bool multiversionInstall() const;
146
147       /** The items build time. */
148       Date buildtime() const;
149
150       /** The items install time (\c false if not installed). */
151       Date installtime() const;
152
153     public:
154       /** String representation <tt>"ident-edition.arch"</tt> or \c "noSolvable"
155        * \code
156        *   product:openSUSE-11.1.x86_64
157        *   autoyast2-2.16.19-0.1.src
158        *   noSolvable
159        * \endcode
160        */
161       std::string asString() const;
162
163       /** String representation <tt>"ident-edition.arch(repo)"</tt> or \c "noSolvable" */
164       std::string asUserString() const;
165
166       /** Test whether two Solvables have the same content.
167        * Basically the same name, edition, arch, vendor and buildtime.
168        */
169       bool identical( const Solvable & rhs ) const;
170
171       /** Test for same name-version-release.arch */
172       bool sameNVRA( const Solvable & rhs ) const
173       { return( get() == rhs.get() || ( ident() == rhs.ident() && edition() == rhs.edition() && arch() == rhs.arch() ) ); }
174
175     public:
176       /** \name Access to the \ref Solvable dependencies.
177        *
178        * \note Prerequires are a subset of requires.
179        */
180       //@{
181       Capabilities provides()    const;
182       Capabilities requires()    const;
183       Capabilities conflicts()   const;
184       Capabilities obsoletes()   const;
185       Capabilities recommends()  const;
186       Capabilities suggests()    const;
187       Capabilities enhances()    const;
188       Capabilities supplements() const;
189       Capabilities prerequires() const;
190
191       /** Return \ref Capabilities selected by \ref Dep constant. */
192       Capabilities dep( Dep which_r ) const
193       {
194         switch( which_r.inSwitch() )
195         {
196           case Dep::PROVIDES_e:    return provides();    break;
197           case Dep::REQUIRES_e:    return requires();    break;
198           case Dep::CONFLICTS_e:   return conflicts();   break;
199           case Dep::OBSOLETES_e:   return obsoletes();   break;
200           case Dep::RECOMMENDS_e:  return recommends();  break;
201           case Dep::SUGGESTS_e:    return suggests();    break;
202           case Dep::ENHANCES_e:    return enhances();    break;
203           case Dep::SUPPLEMENTS_e: return supplements(); break;
204           case Dep::PREREQUIRES_e: return prerequires(); break;
205         }
206         return Capabilities();
207       }
208       /** \overload operator[] */
209       Capabilities operator[]( Dep which_r ) const
210       { return dep( which_r ); }
211
212
213       /** Return the namespaced provides <tt>'namespace([value])[ op edition]'</tt> of this Solvable. */
214       CapabilitySet providesNamespace( const std::string & namespace_r ) const;
215
216       /** Return <tt>'value[ op edition]'</tt> for namespaced provides <tt>'namespace(value)[ op edition]'</tt>.
217        * Similar to \ref providesNamespace, but the namespace is stripped from the
218        * dependencies. This is convenient if the namespace denotes packages that
219        * should be looked up. E.g. the \c weakremover namespace used in a products
220        * release package denotes the packages that were dropped from the distribution.
221        * \see \ref Product::droplist
222        */
223       CapabilitySet valuesOfNamespace( const std::string & namespace_r ) const;
224       //@}
225
226       std::pair<bool, CapabilitySet> matchesSolvable ( const SolvAttr &attr, const sat::Solvable &solv ) const;
227
228     public:
229       /** \name Locale support. */
230       //@{
231       /** Whether this \c Solvable claims to support locales. */
232       bool supportsLocales() const;
233       /** Whether this \c Solvable supports a specific \ref Locale. */
234       bool supportsLocale( const Locale & locale_r ) const;
235       /** Whether this \c Solvable supports at least one of the specified locales. */
236       bool supportsLocale( const LocaleSet & locales_r ) const;
237       /** Whether this \c Solvable supports at least one requested locale.
238        * \see \ref Pool::setRequestedLocales
239        */
240       bool supportsRequestedLocales() const;
241       /** Return the supported locales. */
242       LocaleSet getSupportedLocales() const;
243       /** \overload Legacy return via arg \a locales_r */
244       void getSupportedLocales( LocaleSet & locales_r ) const
245       { locales_r = getSupportedLocales(); }
246       //@}
247
248     public:
249       /** The solvables CpeId if available. */
250       CpeId cpeId() const;
251
252       /** Media number the solvable is located on (\c 0 if no media access required). */
253       unsigned mediaNr() const;
254
255       /** Installed (unpacked) size.
256        * This is just a total number. Many objects provide even more detailed
257        * disk usage data. You can use \ref DiskUsageCounter to find out
258        * how objects data are distributed across partitions/directories.
259        * \code
260        *   // Load directory set into ducounter
261        *   DiskUsageCounter ducounter( { "/", "/usr", "/var" } );
262        *
263        *   // see how noch space the packages use
264        *   for ( const PoolItem & pi : pool )
265        *   {
266        *     cout << pi << ducounter.disk_usage( pi ) << endl;
267        *     // I__s_(7)GeoIP-1.4.8-3.1.2.x86_64(@System) {
268        *     // dir:[/] [ bs: 0 B ts: 0 B us: 0 B (+-: 1.0 KiB)]
269        *     // dir:[/usr] [ bs: 0 B ts: 0 B us: 0 B (+-: 133.0 KiB)]
270        *     // dir:[/var] [ bs: 0 B ts: 0 B us: 0 B (+-: 1.1 MiB)]
271        *     // }
272        *   }
273        * \endcode
274        * \see \ref DiskUsageCounter
275        */
276       ByteCount installSize() const;
277
278       /** Download size. */
279       ByteCount downloadSize() const;
280
281       /** The distribution string. */
282       std::string distribution() const;
283
284       /** Short (singleline) text describing the solvable (opt. translated). */
285       std::string summary( const Locale & lang_r = Locale() ) const;
286
287       /** Long (multiline) text describing the solvable (opt. translated). */
288       std::string description( const Locale & lang_r = Locale() ) const;
289
290       /** UI hint text when selecting the solvable for install (opt. translated). */
291       std::string insnotify( const Locale & lang_r = Locale() ) const;
292       /** UI hint text when selecting the solvable for uninstall (opt. translated).*/
293       std::string delnotify( const Locale & lang_r = Locale() ) const;
294
295       /** License or agreement to accept before installing the solvable (opt. translated). */
296       std::string licenseToConfirm( const Locale & lang_r = Locale() ) const;
297       /** \c True except for well known exceptions (i.e show license but no need to accept it). */
298       bool needToAcceptLicense() const;
299
300     public:
301       /** Helper that splits an identifier into kind and name or vice versa.
302        * \note In case \c name_r is preceded by a well known kind spec, the
303        * \c kind_r argument is ignored, and kind is derived from name.
304        * \see \ref ident
305        */
306       class SplitIdent
307       {
308       public:
309         SplitIdent() {}
310         SplitIdent( IdString ident_r );
311         SplitIdent( const char * ident_r );
312         SplitIdent( const std::string & ident_r );
313         SplitIdent( ResKind kind_r, IdString name_r );
314         SplitIdent( ResKind kind_r, const C_Str & name_r );
315
316         IdString ident() const { return _ident; }
317         ResKind  kind()  const { return _kind; }
318         IdString name()  const { return _name; }
319
320       private:
321         IdString  _ident;
322         ResKind   _kind;
323         IdString  _name;
324       };
325
326     public:
327       /** \name Attribute lookup.
328        * \see \ref LookupAttr and  \ref ArrayAttr providing a general, more
329        * query like interface for attribute retrieval.
330        */
331       //@{
332       /**
333        * returns the string attribute value for \ref attr
334        * or an empty string if it does not exists.
335        */
336       std::string lookupStrAttribute( const SolvAttr & attr ) const;
337       /** \overload Trying to look up a translated string attribute.
338        *
339        * Returns the translation for \c lang_r.
340        *
341        * Passing an empty \ref Locale will return the string for the
342        * current default locale (\see \ref ZConfig::TextLocale),
343        * \b considering all fallback locales.
344        *
345        * Returns an empty string if no translation is available.
346        */
347       std::string lookupStrAttribute( const SolvAttr & attr, const Locale & lang_r ) const;
348
349       /**
350        * returns the numeric attribute value for \ref attr
351        * or 0 if it does not exists.
352        */
353       unsigned long long lookupNumAttribute( const SolvAttr & attr ) const;
354       /** \overload returning custom notfound_r value */
355       unsigned long long lookupNumAttribute( const SolvAttr & attr, unsigned long long notfound_r ) const;
356
357       /**
358        * returns the boolean attribute value for \ref attr
359        * or \c false if it does not exists.
360        */
361       bool lookupBoolAttribute( const SolvAttr & attr ) const;
362
363       /**
364        * returns the id attribute value for \ref attr
365        * or \ref detail::noId if it does not exists.
366        */
367       detail::IdType lookupIdAttribute( const SolvAttr & attr ) const;
368
369       /**
370        * returns the CheckSum attribute value for \ref attr
371        * or an empty CheckSum if ir does not exist.
372        */
373       CheckSum lookupCheckSumAttribute( const SolvAttr & attr ) const;
374
375       /**
376        * returns OnMediaLocation data: This is everything we need to
377        * download e.g. an rpm (path, checksum, downloadsize, etc.).
378        */
379       OnMediaLocation lookupLocation() const;
380       //@}
381
382     public:
383       /** Return next Solvable in \ref Pool (or \ref noSolvable). */
384       Solvable nextInPool() const;
385       /** Return next Solvable in \ref Repo (or \ref noSolvable). */
386       Solvable nextInRepo() const;
387       /** Expert backdoor. */
388       detail::CSolvable * get() const;
389       /** Expert backdoor. */
390       IdType id() const { return _id; }
391
392     private:
393       IdType _id;
394     };
395     ///////////////////////////////////////////////////////////////////
396
397     /** \relates Solvable Stream output */
398     std::ostream & operator<<( std::ostream & str, const Solvable & obj );
399
400     /** \relates Solvable More verbose stream output including dependencies */
401     std::ostream & dumpOn( std::ostream & str, const Solvable & obj );
402
403     /** \relates Solvable XML output */
404     std::ostream & dumpAsXmlOn( std::ostream & str, const Solvable & obj );
405
406     /** \relates Solvable */
407     inline bool operator==( const Solvable & lhs, const Solvable & rhs )
408     { return lhs.get() == rhs.get(); }
409
410     /** \relates Solvable */
411     inline bool operator!=( const Solvable & lhs, const Solvable & rhs )
412     { return lhs.get() != rhs.get(); }
413
414     /** \relates Solvable */
415     inline bool operator<( const Solvable & lhs, const Solvable & rhs )
416     { return lhs.get() < rhs.get(); }
417
418     /** \relates Solvable Test whether a \ref Solvable is of a certain Kind. */
419     template<class TRes>
420     inline bool isKind( const Solvable & solvable_r )
421     { return solvable_r.isKind( ResTraits<TRes>::kind ); }
422
423     /** \relates Solvable Test for same content. */
424     inline bool identical( const Solvable & lhs, const Solvable & rhs )
425     { return lhs.identical( rhs ); }
426
427     /** \relates Solvable Test for same name version release and arch. */
428     inline bool sameNVRA( const Solvable & lhs, const Solvable & rhs )
429     { return lhs.sameNVRA( rhs ); }
430
431
432     /** \relates Solvable Compare according to \a kind and \a name. */
433     inline int compareByN( const Solvable & lhs, const Solvable & rhs )
434     {
435       int res = 0;
436       if ( lhs != rhs )
437       {
438         if ( (res = lhs.kind().compare( rhs.kind() )) == 0 )
439           res = lhs.name().compare( rhs.name() );
440       }
441       return res;
442     }
443
444     /** \relates Solvable Compare according to \a kind, \a name and \a edition. */
445     inline int compareByNVR( const Solvable & lhs, const Solvable & rhs )
446     {
447       int res = compareByN( lhs, rhs );
448       if ( res == 0 )
449         res = lhs.edition().compare( rhs.edition() );
450       return res;
451     }
452
453     /** \relates Solvable Compare according to \a kind, \a name, \a edition and \a arch. */
454     inline int compareByNVRA( const Solvable & lhs, const Solvable & rhs )
455     {
456       int res = compareByNVR( lhs, rhs );
457       if ( res == 0 )
458         res = lhs.arch().compare( rhs.arch() );
459       return res;
460     }
461
462     ///////////////////////////////////////////////////////////////////
463     namespace detail
464     {
465       ///////////////////////////////////////////////////////////////////
466       //
467       //        CLASS NAME : SolvableIterator
468       //
469       /** */
470       class SolvableIterator : public boost::iterator_adaptor<
471           SolvableIterator                   // Derived
472           , CSolvable*                       // Base
473           , const Solvable                   // Value
474           , boost::forward_traversal_tag     // CategoryOrTraversal
475           , const Solvable                   // Reference
476           >
477       {
478         public:
479           SolvableIterator()
480           : SolvableIterator::iterator_adaptor_( 0 )
481           {}
482
483           explicit SolvableIterator( const Solvable & val_r )
484           : SolvableIterator::iterator_adaptor_( 0 )
485           { assignVal( val_r ); }
486
487           explicit SolvableIterator( SolvableIdType id_r )
488           : SolvableIterator::iterator_adaptor_( 0 )
489           { assignVal( Solvable( id_r ) ); }
490
491         private:
492           friend class boost::iterator_core_access;
493
494           Solvable dereference() const
495           { return _val; }
496
497           void increment()
498           { assignVal( _val.nextInPool() ); }
499
500         private:
501           void assignVal( const Solvable & val_r )
502           { _val = val_r; base_reference() = _val.get(); }
503
504           Solvable _val;
505       };
506     } // namespace detail
507     ///////////////////////////////////////////////////////////////////
508   } // namespace sat
509   ///////////////////////////////////////////////////////////////////
510
511   class PoolItem;
512   ///////////////////////////////////////////////////////////////////
513   namespace sat
514   {
515     /** To Solvable transform functor.
516      * \relates Solvable
517      * \relates sat::SolvIterMixin
518      */
519     struct asSolvable
520     {
521       typedef Solvable result_type;
522
523       Solvable operator()( const Solvable & solv_r ) const
524       { return solv_r; }
525
526       Solvable operator()( const PoolItem & pi_r ) const;
527
528       Solvable operator()( const ResObject_constPtr & res_r ) const;
529     };
530   } // namespace sat
531   ///////////////////////////////////////////////////////////////////
532 } // namespace zypp
533 ///////////////////////////////////////////////////////////////////
534
535 ZYPP_DEFINE_ID_HASHABLE( ::zypp::sat::Solvable );
536
537 #endif // ZYPP_SAT_SOLVABLE_H