5efda63d68dbfb398dff83171475e1547ff7028f
[platform/upstream/libzypp.git] / zypp / PoolItem.cc
1 /*---------------------------------------------------------------------\
2 |                          ____ _   __ __ ___                          |
3 |                         |__  / \ / / . \ . \                         |
4 |                           / / \ V /|  _/  _/                         |
5 |                          / /__ | | | | | |                           |
6 |                         /_____||_| |_| |_|                           |
7 |                                                                      |
8 \---------------------------------------------------------------------*/
9 /** \file       zypp/PoolItem.cc
10  *
11 */
12 #include <iostream>
13 #include "zypp/base/Logger.h"
14 #include "zypp/base/DefaultIntegral.h"
15
16 #include "zypp/PoolItem.h"
17 #include "zypp/ResPool.h"
18 #include "zypp/Package.h"
19 #include "zypp/VendorAttr.h"
20
21 using std::endl;
22
23 ///////////////////////////////////////////////////////////////////
24 namespace zypp
25 { /////////////////////////////////////////////////////////////////
26
27   ///////////////////////////////////////////////////////////////////
28   //
29   //    CLASS NAME : PoolItem::Impl
30   //
31   /** PoolItem implementation.
32    * \c _buddy handling:
33    * \li \c ==0 no buddy
34    * \li \c >0 this uses \c _buddy status
35    * \li \c <0 this status used by \c -_buddy
36    */
37   struct PoolItem::Impl
38   {
39     public:
40       Impl() {}
41
42       Impl( ResObject::constPtr res_r,
43             const ResStatus & status_r )
44       : _status( status_r )
45       , _resolvable( res_r )
46       {}
47
48       ResStatus & status() const
49       { return _buddy > 0 ? PoolItem(buddy()).status() : _status; }
50
51       sat::Solvable buddy() const
52       {
53         if ( !_buddy )
54           return sat::Solvable::noSolvable;
55         if ( _buddy < 0 )
56           return sat::Solvable( -_buddy );
57         return sat::Solvable( _buddy );
58       }
59
60       void setBuddy( const sat::Solvable & solv_r );
61
62       ResObject::constPtr resolvable() const
63       { return _resolvable; }
64
65       ResStatus & statusReset() const
66       {
67         _status.setLock( false, zypp::ResStatus::USER );
68         _status.resetTransact( zypp::ResStatus::USER );
69         return _status;
70       }
71
72     public:
73       bool isUndetermined() const
74       {
75           return status().isUndetermined();
76       }
77
78       bool isRelevant() const
79       {
80           return !status().isNonRelevant();
81       }
82
83       bool isSatisfied() const
84       {
85           return status().isSatisfied();
86       }
87
88       bool isBroken() const
89       {
90           return status().isBroken();
91       }
92
93       bool isNeeded() const
94       {
95         return status().isToBeInstalled() || ( isBroken() && ! status().isLocked() );
96       }
97
98       bool isUnwanted() const
99       {
100         return isBroken() && status().isLocked();
101       }
102
103       std::string patchStatusAsString() const
104       {
105         if ( isUndetermined() ) return "undetermined";
106         if ( isRelevant() )     return "relevant";
107         if ( isSatisfied() )    return "satisfied";
108         if ( isBroken() )       return "broken";
109         if ( isNeeded() )       return "needed";
110         if ( isUnwanted() )     return "unwanted";
111         return "none";
112       }
113
114     private:
115       mutable ResStatus     _status;
116       ResObject::constPtr   _resolvable;
117       DefaultIntegral<sat::detail::IdType,sat::detail::noId> _buddy;
118
119     /** \name Poor man's save/restore state.
120        * \todo There may be better save/restore state strategies.
121      */
122     //@{
123     public:
124       void saveState() const
125       { _savedStatus = status(); }
126       void restoreState() const
127       { status() = _savedStatus; }
128       bool sameState() const
129       {
130         if ( status() == _savedStatus )
131           return true;
132         // some bits changed...
133         if ( status().getTransactValue() != _savedStatus.getTransactValue()
134              && ( ! status().isBySolver() // ignore solver state changes
135                   // removing a user lock also goes to bySolver
136                   || _savedStatus.getTransactValue() == ResStatus::LOCKED ) )
137           return false;
138         if ( status().isLicenceConfirmed() != _savedStatus.isLicenceConfirmed() )
139           return false;
140         return true;
141       }
142     private:
143       mutable ResStatus _savedStatus;
144     //@}
145
146     public:
147       /** Offer default Impl. */
148       static shared_ptr<Impl> nullimpl()
149       {
150         static shared_ptr<Impl> _nullimpl( new Impl );
151         return _nullimpl;
152       }
153   };
154   ///////////////////////////////////////////////////////////////////
155
156   /** \relates PoolItem::Impl Stream output */
157   inline std::ostream & operator<<( std::ostream & str, const PoolItem::Impl & obj )
158   {
159     str << obj.status();
160     if (obj.resolvable())
161         str << *obj.resolvable();
162     else
163         str << "(NULL)";
164     return str;
165   }
166
167   inline void PoolItem::Impl::setBuddy( const sat::Solvable & solv_r )
168   {
169     PoolItem myBuddy( solv_r );
170     if ( myBuddy )
171     {
172       if ( myBuddy._pimpl->_buddy )
173       {
174         ERR <<  *this << " would be buddy2 in " << myBuddy << endl;
175         return;
176       }
177       myBuddy._pimpl->_buddy = -resolvable()->satSolvable().id();
178       _buddy = myBuddy.satSolvable().id();
179       DBG << *this << " has buddy " << myBuddy << endl;
180     }
181   }
182
183   ///////////////////////////////////////////////////////////////////
184   //    class PoolItem
185   ///////////////////////////////////////////////////////////////////
186
187   PoolItem::PoolItem()
188   : _pimpl( Impl::nullimpl() )
189   {}
190
191   PoolItem::PoolItem( const sat::Solvable & solvable_r )
192   : _pimpl( ResPool::instance().find( solvable_r )._pimpl )
193   {}
194
195   PoolItem::PoolItem( const ResObject::constPtr & resolvable_r )
196   : _pimpl( ResPool::instance().find( resolvable_r )._pimpl )
197   {}
198
199   PoolItem::PoolItem( Impl * implptr_r )
200   : _pimpl( implptr_r )
201   {}
202
203   PoolItem PoolItem::makePoolItem( const sat::Solvable & solvable_r )
204   {
205     return PoolItem( new Impl( makeResObject( solvable_r ), solvable_r.isSystem() ) );
206   }
207
208   PoolItem::~PoolItem()
209   {}
210
211   ResPool PoolItem::pool() const
212   { return ResPool::instance(); }
213
214
215   ResStatus & PoolItem::status() const                  { return _pimpl->status(); }
216   ResStatus & PoolItem::statusReset() const             { return _pimpl->statusReset(); }
217   sat::Solvable PoolItem::buddy() const                 { return _pimpl->buddy(); }
218   void PoolItem::setBuddy( const sat::Solvable & solv_r )       { _pimpl->setBuddy( solv_r ); }
219   bool PoolItem::isUndetermined() const                 { return _pimpl->isUndetermined(); }
220   bool PoolItem::isRelevant() const                     { return _pimpl->isRelevant(); }
221   bool PoolItem::isSatisfied() const                    { return _pimpl->isSatisfied(); }
222   bool PoolItem::isBroken() const                       { return _pimpl->isBroken(); }
223   bool PoolItem::isNeeded() const                       { return _pimpl->isNeeded(); }
224   bool PoolItem::isUnwanted() const                     { return _pimpl->isUnwanted(); }
225   std::string PoolItem::patchStatusAsString() const     { return _pimpl->patchStatusAsString(); }
226
227   void PoolItem::saveState() const                      { _pimpl->saveState(); }
228   void PoolItem::restoreState() const                   { _pimpl->restoreState(); }
229   bool PoolItem::sameState() const                      { return _pimpl->sameState(); }
230   ResObject::constPtr PoolItem::resolvable() const      { return _pimpl->resolvable(); }
231
232
233   std::ostream & operator<<( std::ostream & str, const PoolItem & obj )
234   { return str << *obj._pimpl; }
235
236 } // namespace zypp
237 ///////////////////////////////////////////////////////////////////