Imported Upstream version 14.45.0
[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( 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     private:
104       mutable ResStatus     _status;
105       ResObject::constPtr   _resolvable;
106       DefaultIntegral<sat::detail::IdType,sat::detail::noId> _buddy;
107
108     /** \name Poor man's save/restore state.
109        * \todo There may be better save/restore state strategies.
110      */
111     //@{
112     public:
113       void saveState() const
114       { _savedStatus = status(); }
115       void restoreState() const
116       { status() = _savedStatus; }
117       bool sameState() const
118       {
119         if ( status() == _savedStatus )
120           return true;
121         // some bits changed...
122         if ( status().getTransactValue() != _savedStatus.getTransactValue()
123              && ( ! status().isBySolver() // ignore solver state changes
124                   // removing a user lock also goes to bySolver
125                   || _savedStatus.getTransactValue() == ResStatus::LOCKED ) )
126           return false;
127         if ( status().isLicenceConfirmed() != _savedStatus.isLicenceConfirmed() )
128           return false;
129         return true;
130       }
131     private:
132       mutable ResStatus _savedStatus;
133     //@}
134
135     public:
136       /** Offer default Impl. */
137       static shared_ptr<Impl> nullimpl()
138       {
139         static shared_ptr<Impl> _nullimpl( new Impl );
140         return _nullimpl;
141       }
142   };
143   ///////////////////////////////////////////////////////////////////
144
145   /** \relates PoolItem::Impl Stream output */
146   inline std::ostream & operator<<( std::ostream & str, const PoolItem::Impl & obj )
147   {
148     str << obj.status();
149     if (obj.resolvable())
150         str << *obj.resolvable();
151     else
152         str << "(NULL)";
153     return str;
154   }
155
156   inline void PoolItem::Impl::setBuddy( sat::Solvable solv_r )
157   {
158     PoolItem myBuddy( solv_r );
159     if ( myBuddy )
160     {
161       if ( myBuddy._pimpl->_buddy )
162       {
163         ERR <<  *this << " would be buddy2 in " << myBuddy << endl;
164         return;
165       }
166       myBuddy._pimpl->_buddy = -resolvable()->satSolvable().id();
167       _buddy = myBuddy.satSolvable().id();
168       DBG << *this << " has buddy " << myBuddy << endl;
169     }
170   }
171
172   ///////////////////////////////////////////////////////////////////
173   //
174   //    CLASS NAME : PoolItem
175   //
176   ///////////////////////////////////////////////////////////////////
177
178   ///////////////////////////////////////////////////////////////////
179   //
180   //    METHOD NAME : PoolItem::PoolItem
181   //    METHOD TYPE : Ctor
182   //
183   PoolItem::PoolItem()
184   : _pimpl( Impl::nullimpl() )
185   {}
186
187   ///////////////////////////////////////////////////////////////////
188   //
189   //    METHOD NAME : PoolItem::PoolItem
190   //    METHOD TYPE : Ctor
191   //
192   PoolItem::PoolItem( const sat::Solvable & solvable_r )
193   : _pimpl( ResPool::instance().find( solvable_r )._pimpl )
194   {}
195
196   ///////////////////////////////////////////////////////////////////
197   //
198   //    METHOD NAME : PoolItem::PoolItem
199   //    METHOD TYPE : Ctor
200   //
201   PoolItem::PoolItem( const ResObject::constPtr & resolvable_r )
202   : _pimpl( ResPool::instance().find( resolvable_r )._pimpl )
203   {}
204
205   ///////////////////////////////////////////////////////////////////
206   //
207   //    METHOD NAME : PoolItem::PoolItem
208   //    METHOD TYPE : Ctor
209   //
210   PoolItem::PoolItem( Impl * implptr_r )
211   : _pimpl( implptr_r )
212   {}
213
214   ///////////////////////////////////////////////////////////////////
215   //
216   //    METHOD NAME : PoolItem::makePoolItem
217   //    METHOD TYPE : PoolItem
218   //
219   PoolItem PoolItem::makePoolItem( const sat::Solvable & solvable_r )
220   {
221     return PoolItem( new Impl( makeResObject( solvable_r ), solvable_r.isSystem() ) );
222   }
223
224   ///////////////////////////////////////////////////////////////////
225   //
226   //    METHOD NAME : PoolItem::~PoolItem
227   //    METHOD TYPE : Dtor
228   //
229   PoolItem::~PoolItem()
230   {}
231
232   ///////////////////////////////////////////////////////////////////
233   //
234   //    METHOD NAME : PoolItem::pool
235   //    METHOD TYPE : ResPool
236   //
237   ResPool PoolItem::pool() const
238   { return ResPool::instance(); }
239
240   ///////////////////////////////////////////////////////////////////
241   //
242   //    Forward to Impl:
243   //
244   ///////////////////////////////////////////////////////////////////
245
246   ResStatus & PoolItem::status() const
247   { return _pimpl->status(); }
248
249   ResStatus & PoolItem::statusReset() const
250   { return _pimpl->statusReset(); }
251
252   sat::Solvable PoolItem::buddy() const
253   { return _pimpl->buddy(); }
254
255   void PoolItem::setBuddy( sat::Solvable solv_r )
256   { _pimpl->setBuddy( solv_r ); }
257
258   bool PoolItem::isUndetermined() const
259   { return _pimpl->isUndetermined(); }
260
261   bool PoolItem::isRelevant() const
262   { return _pimpl->isRelevant(); }
263
264   bool PoolItem::isSatisfied() const
265   { return _pimpl->isSatisfied(); }
266
267   bool PoolItem::isBroken() const
268   { return _pimpl->isBroken(); }
269
270   bool PoolItem::isNeeded() const
271   { return _pimpl->isNeeded(); }
272
273   bool PoolItem::isUnwanted() const
274   { return _pimpl->isUnwanted(); }
275
276   void PoolItem::saveState() const
277   { _pimpl->saveState(); }
278
279   void PoolItem::restoreState() const
280   { _pimpl->restoreState(); }
281
282   bool PoolItem::sameState() const
283   { return _pimpl->sameState(); }
284
285   ResObject::constPtr PoolItem::resolvable() const
286   { return _pimpl->resolvable(); }
287
288   /******************************************************************
289    **
290    **   FUNCTION NAME : operator<<
291    **   FUNCTION TYPE : std::ostream &
292   */
293   std::ostream & operator<<( std::ostream & str, const PoolItem & obj )
294   {
295     return str << *obj._pimpl;
296   }
297
298   /////////////////////////////////////////////////////////////////
299 } // namespace zypp
300 ///////////////////////////////////////////////////////////////////