Imported Upstream version 15.18.0
[platform/upstream/libzypp.git] / zypp / solver / detail / SATResolver.cc
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 4 -*- */
2 /* SATResolver.cc
3  *
4  * Copyright (C) 2000-2002 Ximian, Inc.
5  * Copyright (C) 2005 SUSE Linux Products GmbH
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License,
9  * version 2, as published by the Free Software Foundation.
10  *
11  * This program is distributed in the hope that it will be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19  * 02111-1307, USA.
20  */
21 extern "C"
22 {
23 #include <solv/repo_solv.h>
24 #include <solv/poolarch.h>
25 #include <solv/evr.h>
26 #include <solv/poolvendor.h>
27 #include <solv/policy.h>
28 #include <solv/bitmap.h>
29 #include <solv/queue.h>
30 }
31
32 #define ZYPP_USE_RESOLVER_INTERNALS
33
34 #include "zypp/base/String.h"
35 #include "zypp/Product.h"
36 #include "zypp/Capability.h"
37 #include "zypp/ResStatus.h"
38 #include "zypp/VendorAttr.h"
39 #include "zypp/base/LogTools.h"
40 #include "zypp/base/String.h"
41 #include "zypp/base/Gettext.h"
42 #include "zypp/base/Algorithm.h"
43 #include "zypp/ResPool.h"
44 #include "zypp/ResFilters.h"
45 #include "zypp/ZConfig.h"
46 #include "zypp/sat/Pool.h"
47 #include "zypp/sat/WhatProvides.h"
48 #include "zypp/sat/WhatObsoletes.h"
49 #include "zypp/solver/detail/Resolver.h"
50 #include "zypp/solver/detail/SATResolver.h"
51 #include "zypp/solver/detail/ProblemSolutionCombi.h"
52 #include "zypp/solver/detail/ProblemSolutionIgnore.h"
53 #include "zypp/solver/detail/SolverQueueItemInstall.h"
54 #include "zypp/solver/detail/SolverQueueItemDelete.h"
55 #include "zypp/solver/detail/SystemCheck.h"
56 #include "zypp/solver/detail/SolutionAction.h"
57 #include "zypp/solver/detail/SolverQueueItem.h"
58 #include "zypp/sat/Transaction.h"
59 #include "zypp/sat/Queue.h"
60
61 #include "zypp/sat/detail/PoolImpl.h"
62
63 #define _XDEBUG(x) do { if (base::logger::isExcessive()) XXX << x << std::endl;} while (0)
64
65 /////////////////////////////////////////////////////////////////////////
66 namespace zypp
67 { ///////////////////////////////////////////////////////////////////////
68
69   /////////////////////////////////////////////////////////////////////////
70   namespace env
71   {
72     inline bool HACKENV( const char * var_r, bool default_r )
73     {
74       bool ret = default_r;
75       const char * val = ::getenv( var_r );
76       if ( val )
77       {
78         ret = str::strToBool( val, default_r );
79         if ( ret != default_r )
80           INT << "HACKENV " << var_r << " = " << ret << endl;
81       }
82       return ret;
83     }
84   } // namespace env
85   /////////////////////////////////////////////////////////////////////////
86
87   ///////////////////////////////////////////////////////////////////////
88   namespace solver
89   { /////////////////////////////////////////////////////////////////////
90     /////////////////////////////////////////////////////////////////////
91     namespace detail
92     { ///////////////////////////////////////////////////////////////////
93
94 using namespace std;
95
96 IMPL_PTR_TYPE(SATResolver);
97
98 #define MAYBE_CLEANDEPS (cleandepsOnRemove()?SOLVER_CLEANDEPS:0)
99
100 //---------------------------------------------------------------------------
101 // Callbacks for SAT policies
102 //---------------------------------------------------------------------------
103
104 int vendorCheck( Pool *pool, Solvable *solvable1, Solvable *solvable2 )
105 {
106   return VendorAttr::instance().equivalent( IdString(solvable1->vendor),
107                                             IdString(solvable2->vendor) ) ? 0 : 1;
108 }
109
110
111 inline std::string itemToString( const PoolItem & item )
112 {
113   if ( !item )
114     return std::string();
115
116   sat::Solvable slv( item.satSolvable() );
117   std::string ret( slv.asString() ); // n-v-r.a
118   if ( ! slv.isSystem() )
119   {
120     ret += "[";
121     ret += slv.repository().alias();
122     ret += "]";
123   }
124   return ret;
125 }
126
127 inline PoolItem getPoolItem( Id id_r )
128 {
129   PoolItem ret( (sat::Solvable( id_r )) );
130   if ( !ret && id_r )
131     INT << "id " << id_r << " not found in ZYPP pool." << endl;
132   return ret;
133 }
134
135 //---------------------------------------------------------------------------
136
137 std::ostream &
138 SATResolver::dumpOn( std::ostream & os ) const
139 {
140     os << "<resolver>" << endl;
141     if (_solv) {
142 #define OUTS(X) os << "  " << #X << "\t= " << solver_get_flag(_solv, SOLVER_FLAG_##X) << endl
143         OUTS( ALLOW_DOWNGRADE );
144         OUTS( ALLOW_ARCHCHANGE );
145         OUTS( ALLOW_VENDORCHANGE );
146         OUTS( ALLOW_UNINSTALL );
147         OUTS( NO_UPDATEPROVIDE );
148         OUTS( SPLITPROVIDES );
149         OUTS( IGNORE_RECOMMENDED );
150         OUTS( ADD_ALREADY_RECOMMENDED );
151         OUTS( NO_INFARCHCHECK );
152         OUTS( ALLOW_NAMECHANGE );
153         OUTS( KEEP_EXPLICIT_OBSOLETES );
154         OUTS( BEST_OBEY_POLICY );
155         OUTS( NO_AUTOTARGET );
156         OUTS( DUP_ALLOW_DOWNGRADE );
157         OUTS( DUP_ALLOW_ARCHCHANGE );
158         OUTS( DUP_ALLOW_VENDORCHANGE );
159         OUTS( DUP_ALLOW_NAMECHANGE );
160         OUTS( KEEP_ORPHANS );
161         OUTS( BREAK_ORPHANS );
162         OUTS( FOCUS_INSTALLED );
163         OUTS( YUM_OBSOLETES );
164 #undef OUTS
165         os << "  distupgrade    = "     << _distupgrade << endl;
166         os << "  distupgrade_removeunsupported  = " << _distupgrade_removeunsupported << endl;
167         os << "  solveSrcPackages       = "     << _solveSrcPackages << endl;
168         os << "  cleandepsOnRemove      = "     << _cleandepsOnRemove << endl;
169         os << "  fixsystem              = "     << _fixsystem << endl;
170     } else {
171         os << "<NULL>";
172     }
173     return os << "<resolver/>" << endl;
174 }
175
176 //---------------------------------------------------------------------------
177
178 SATResolver::SATResolver (const ResPool & pool, Pool *SATPool)
179     : _pool (pool)
180     , _SATPool (SATPool)
181     , _solv(NULL)
182     , _fixsystem(false)
183     , _allowdowngrade(false)
184     , _allowarchchange(false)
185     , _allowvendorchange(ZConfig::instance().solver_allowVendorChange())
186     , _allowuninstall(false)
187     , _updatesystem(false)
188     , _noupdateprovide(false)
189     , _dosplitprovides(true)
190     , _onlyRequires(ZConfig::instance().solver_onlyRequires())
191     , _ignorealreadyrecommended(true)
192     , _distupgrade(false)
193     , _distupgrade_removeunsupported(false)
194     , _dup_allowdowngrade       ( ZConfig::instance().solver_dupAllowDowngrade() )
195     , _dup_allownamechange      ( ZConfig::instance().solver_dupAllowNameChange() )
196     , _dup_allowarchchange      ( ZConfig::instance().solver_dupAllowArchChange() )
197     , _dup_allowvendorchange    ( ZConfig::instance().solver_dupAllowVendorChange() )
198     , _solveSrcPackages(false)
199     , _cleandepsOnRemove(ZConfig::instance().solver_cleandepsOnRemove())
200 {
201 }
202
203
204 SATResolver::~SATResolver()
205 {
206   solverEnd();
207 }
208
209 //---------------------------------------------------------------------------
210
211 ResPool
212 SATResolver::pool (void) const
213 {
214     return _pool;
215 }
216
217 void
218 SATResolver::resetItemTransaction (PoolItem item)
219 {
220     bool found = false;
221     for (PoolItemList::const_iterator iter = _items_to_remove.begin();
222          iter != _items_to_remove.end(); ++iter) {
223         if (*iter == item) {
224             _items_to_remove.remove(*iter);
225             found = true;
226             break;
227         }
228     }
229     if (!found) {
230         for (PoolItemList::const_iterator iter = _items_to_install.begin();
231              iter != _items_to_install.end(); ++iter) {
232             if (*iter == item) {
233                 _items_to_install.remove(*iter);
234                 found = true;
235                 break;
236             }
237         }
238     }
239     if (!found) {
240         for (PoolItemList::const_iterator iter = _items_to_keep.begin();
241              iter != _items_to_keep.end(); ++iter) {
242             if (*iter == item) {
243                 _items_to_keep.remove(*iter);
244                 found = true;
245                 break;
246             }
247         }
248     }
249     if (!found) {
250         for (PoolItemList::const_iterator iter = _items_to_lock.begin();
251              iter != _items_to_lock.end(); ++iter) {
252             if (*iter == item) {
253                 _items_to_lock.remove(*iter);
254                 found = true;
255                 break;
256             }
257         }
258     }
259 }
260
261
262 void
263 SATResolver::addPoolItemToInstall (PoolItem item)
264 {
265     resetItemTransaction (item);
266     _items_to_install.push_back (item);
267     _items_to_install.unique ();
268 }
269
270
271 void
272 SATResolver::addPoolItemsToInstallFromList (PoolItemList & rl)
273 {
274     for (PoolItemList::const_iterator iter = rl.begin(); iter != rl.end(); iter++) {
275         addPoolItemToInstall (*iter);
276     }
277 }
278
279
280 void
281 SATResolver::addPoolItemToRemove (PoolItem item)
282 {
283     resetItemTransaction (item);
284     _items_to_remove.push_back (item);
285     _items_to_remove.unique ();
286 }
287
288
289 void
290 SATResolver::addPoolItemsToRemoveFromList (PoolItemList & rl)
291 {
292     for (PoolItemList::const_iterator iter = rl.begin(); iter != rl.end(); iter++) {
293         addPoolItemToRemove (*iter);
294     }
295 }
296
297 void
298 SATResolver::addPoolItemToLock (PoolItem item)
299 {
300     resetItemTransaction (item);
301     _items_to_lock.push_back (item);
302     _items_to_lock.unique ();
303 }
304
305 void
306 SATResolver::addPoolItemToKeep (PoolItem item)
307 {
308     resetItemTransaction (item);
309     _items_to_keep.push_back (item);
310     _items_to_keep.unique ();
311 }
312
313 //---------------------------------------------------------------------------
314
315 // copy marked item from solution back to pool
316 // if data != NULL, set as APPL_LOW (from establishPool())
317
318 static void
319 SATSolutionToPool (PoolItem item, const ResStatus & status, const ResStatus::TransactByValue causer)
320 {
321     // resetting
322     item.status().resetTransact (causer);
323     item.status().resetWeak ();
324
325     bool r;
326
327     // installation/deletion
328     if (status.isToBeInstalled()) {
329         r = item.status().setToBeInstalled (causer);
330         _XDEBUG("SATSolutionToPool install returns " << item << ", " << r);
331     }
332     else if (status.isToBeUninstalledDueToUpgrade()) {
333         r = item.status().setToBeUninstalledDueToUpgrade (causer);
334         _XDEBUG("SATSolutionToPool upgrade returns " << item << ", " <<  r);
335     }
336     else if (status.isToBeUninstalled()) {
337         r = item.status().setToBeUninstalled (causer);
338         _XDEBUG("SATSolutionToPool remove returns " << item << ", " <<  r);
339     }
340
341     return;
342 }
343
344 //----------------------------------------------------------------------------
345 //----------------------------------------------------------------------------
346 // resolvePool
347 //----------------------------------------------------------------------------
348 //----------------------------------------------------------------------------
349
350 //----------------------------------------------------------------------------
351 // Helper functions for the ZYPP-Pool
352 //----------------------------------------------------------------------------
353
354
355 //------------------------------------------------------------------------------------------------------------
356 //  This function loops over the pool and grabs all items
357 //  It clears all previous bySolver() states also
358 //
359 //  Every toBeInstalled is passed to zypp::solver:detail::Resolver.addPoolItemToInstall()
360 //  Every toBeUninstalled is passed to zypp::solver:detail::Resolver.addPoolItemToRemove()
361 //
362 //  Solver results must be written back to the pool.
363 //------------------------------------------------------------------------------------------------------------
364
365
366 struct SATCollectTransact : public resfilter::PoolItemFilterFunctor
367 {
368     SATResolver & resolver;
369
370     SATCollectTransact (SATResolver & r)
371         : resolver (r)
372     { }
373
374     bool operator()( PoolItem item )            // only transacts() items go here
375     {
376         ResStatus status = item.status();
377         bool by_solver = (status.isBySolver() || status.isByApplLow());
378
379         if (by_solver) {
380             item.status().resetTransact( ResStatus::APPL_LOW );// clear any solver/establish transactions
381             return true;                                // back out here, dont re-queue former solver result
382         }
383
384         if ( item.satSolvable().isKind<SrcPackage>() && ! resolver.solveSrcPackages() )
385         {
386           // Later we may continue on a per source package base.
387           return true; // dont process this source package.
388         }
389
390         if (status.isToBeInstalled()) {
391             resolver.addPoolItemToInstall(item);        // -> install!
392         }
393         else if (status.isToBeUninstalled()) {
394             resolver.addPoolItemToRemove(item);         // -> remove !
395         }
396         else if (status.isLocked()
397                  && !by_solver) {
398             resolver.addPoolItemToLock (item);
399         }
400         else if (status.isKept()
401                  && !by_solver) {
402             resolver.addPoolItemToKeep (item);
403         }
404
405         return true;
406     }
407 };
408
409
410 //----------------------------------------------------------------------------
411 //----------------------------------------------------------------------------
412 // solving.....
413 //----------------------------------------------------------------------------
414 //----------------------------------------------------------------------------
415
416
417 class CheckIfUpdate : public resfilter::PoolItemFilterFunctor
418 {
419   public:
420     bool is_updated;
421     sat::Solvable _installed;
422
423     CheckIfUpdate( const sat::Solvable & installed_r )
424     : is_updated( false )
425     , _installed( installed_r )
426     {}
427
428     // check this item will be updated
429
430     bool operator()( const PoolItem & item )
431     {
432         if ( item.status().isToBeInstalled() )
433         {
434           if ( ! item.multiversionInstall() || sameNVRA( _installed, item ) )
435           {
436             is_updated = true;
437             return false;
438           }
439         }
440         return true;
441     }
442 };
443
444
445 class CollectPseudoInstalled : public resfilter::PoolItemFilterFunctor
446 {
447   public:
448     Queue *solvableQueue;
449
450     CollectPseudoInstalled( Queue *queue )
451         :solvableQueue (queue)
452     {}
453
454     // collecting PseudoInstalled items
455     bool operator()( PoolItem item )
456     {
457       if ( traits::isPseudoInstalled( item.satSolvable().kind() ) )
458         queue_push( solvableQueue, item.satSolvable().id() );
459       return true;
460     }
461 };
462
463 bool
464 SATResolver::solving(const CapabilitySet & requires_caps,
465                      const CapabilitySet & conflict_caps)
466 {
467     _solv = solver_create( _SATPool );
468     ::pool_set_custom_vendorcheck( _SATPool, &vendorCheck );
469     if (_fixsystem) {
470         queue_push( &(_jobQueue), SOLVER_VERIFY|SOLVER_SOLVABLE_ALL);
471         queue_push( &(_jobQueue), 0 );
472     }
473     if (_updatesystem) {
474         queue_push( &(_jobQueue), SOLVER_UPDATE|SOLVER_SOLVABLE_ALL);
475         queue_push( &(_jobQueue), 0 );
476     }
477     if (_distupgrade) {
478         queue_push( &(_jobQueue), SOLVER_DISTUPGRADE|SOLVER_SOLVABLE_ALL);
479         queue_push( &(_jobQueue), 0 );
480     }
481     if (_distupgrade_removeunsupported) {
482         queue_push( &(_jobQueue), SOLVER_DROP_ORPHANED|SOLVER_SOLVABLE_ALL);
483         queue_push( &(_jobQueue), 0 );
484     }
485     solver_set_flag(_solv, SOLVER_FLAG_ADD_ALREADY_RECOMMENDED, !_ignorealreadyrecommended);
486     solver_set_flag(_solv, SOLVER_FLAG_ALLOW_DOWNGRADE, _allowdowngrade);
487     solver_set_flag(_solv, SOLVER_FLAG_ALLOW_UNINSTALL, _allowuninstall);
488     solver_set_flag(_solv, SOLVER_FLAG_ALLOW_ARCHCHANGE, _allowarchchange);
489     solver_set_flag(_solv, SOLVER_FLAG_ALLOW_VENDORCHANGE, _allowvendorchange);
490     solver_set_flag(_solv, SOLVER_FLAG_SPLITPROVIDES, _dosplitprovides);
491     solver_set_flag(_solv, SOLVER_FLAG_NO_UPDATEPROVIDE, _noupdateprovide);
492     solver_set_flag(_solv, SOLVER_FLAG_IGNORE_RECOMMENDED, _onlyRequires);
493     solver_set_flag(_solv, SOLVER_FLAG_DUP_ALLOW_DOWNGRADE,     _dup_allowdowngrade );
494     solver_set_flag(_solv, SOLVER_FLAG_DUP_ALLOW_NAMECHANGE,    _dup_allownamechange );
495     solver_set_flag(_solv, SOLVER_FLAG_DUP_ALLOW_ARCHCHANGE,    _dup_allowarchchange );
496     solver_set_flag(_solv, SOLVER_FLAG_DUP_ALLOW_VENDORCHANGE,  _dup_allowvendorchange );
497 #if 1
498 #define HACKENV(X,D) solver_set_flag(_solv, X, env::HACKENV( #X, D ) );
499     HACKENV( SOLVER_FLAG_DUP_ALLOW_DOWNGRADE,   _dup_allowdowngrade );
500     HACKENV( SOLVER_FLAG_DUP_ALLOW_NAMECHANGE,  _dup_allownamechange );
501     HACKENV( SOLVER_FLAG_DUP_ALLOW_ARCHCHANGE,  _dup_allowarchchange );
502     HACKENV( SOLVER_FLAG_DUP_ALLOW_VENDORCHANGE,_dup_allowvendorchange );
503 #undef HACKENV
504 #endif
505     sat::Pool::instance().prepareForSolving();
506
507     // Solve !
508     MIL << "Starting solving...." << endl;
509     MIL << *this;
510     solver_solve( _solv, &(_jobQueue) );
511     MIL << "....Solver end" << endl;
512
513     // copying solution back to zypp pool
514     //-----------------------------------------
515     _result_items_to_install.clear();
516     _result_items_to_remove.clear();
517
518     /*  solvables to be installed */
519     Queue decisionq;
520     queue_init(&decisionq);
521     solver_get_decisionqueue(_solv, &decisionq);
522     for ( int i = 0; i < decisionq.count; ++i )
523     {
524       sat::Solvable slv( decisionq.elements[i] );
525       if ( !slv || slv.isSystem() )
526         continue;
527
528       PoolItem poolItem( slv );
529       SATSolutionToPool (poolItem, ResStatus::toBeInstalled, ResStatus::SOLVER);
530       _result_items_to_install.push_back (poolItem);
531     }
532     queue_free(&decisionq);
533
534     /* solvables to be erased */
535     Repository systemRepo( sat::Pool::instance().findSystemRepo() ); // don't create if it does not exist
536     if ( systemRepo && ! systemRepo.solvablesEmpty() )
537     {
538       bool mustCheckObsoletes = false;
539       for_( it, systemRepo.solvablesBegin(), systemRepo.solvablesEnd() )
540       {
541         if (solver_get_decisionlevel(_solv, it->id()) > 0)
542           continue;
543
544         // Check if this is an update
545         CheckIfUpdate info( *it );
546         PoolItem poolItem( *it );
547         invokeOnEach( _pool.byIdentBegin( poolItem ),
548                       _pool.byIdentEnd( poolItem ),
549                       resfilter::ByUninstalled(),                       // ByUninstalled
550                       functor::functorRef<bool,PoolItem> (info) );
551
552         if (info.is_updated) {
553           SATSolutionToPool( poolItem, ResStatus::toBeUninstalledDueToUpgrade, ResStatus::SOLVER );
554         } else {
555           SATSolutionToPool( poolItem, ResStatus::toBeUninstalled, ResStatus::SOLVER );
556           if ( ! mustCheckObsoletes )
557             mustCheckObsoletes = true; // lazy check for UninstalledDueToObsolete
558         }
559         _result_items_to_remove.push_back (poolItem);
560       }
561       if ( mustCheckObsoletes )
562       {
563         sat::WhatObsoletes obsoleted( _result_items_to_install.begin(), _result_items_to_install.end() );
564         for_( it, obsoleted.poolItemBegin(), obsoleted.poolItemEnd() )
565         {
566           ResStatus & status( it->status() );
567           // WhatObsoletes contains installed items only!
568           if ( status.transacts() && ! status.isToBeUninstalledDueToUpgrade() )
569             status.setToBeUninstalledDueToObsolete();
570         }
571       }
572     }
573
574     Queue recommendations;
575     Queue suggestions;
576     Queue orphaned;
577     Queue unneeded;
578     queue_init(&recommendations);
579     queue_init(&suggestions);
580     queue_init(&orphaned);
581     queue_init(&unneeded);
582     solver_get_recommendations(_solv, &recommendations, &suggestions, 0);
583     solver_get_orphaned(_solv, &orphaned);
584     solver_get_unneeded(_solv, &unneeded, 1);
585     /*  solvables which are recommended */
586     for ( int i = 0; i < recommendations.count; ++i )
587     {
588       PoolItem poolItem( getPoolItem( recommendations.elements[i] ) );
589       poolItem.status().setRecommended( true );
590     }
591
592     /*  solvables which are suggested */
593     for ( int i = 0; i < suggestions.count; ++i )
594     {
595       PoolItem poolItem( getPoolItem( suggestions.elements[i] ) );
596       poolItem.status().setSuggested( true );
597     }
598
599     _problem_items.clear();
600     /*  solvables which are orphaned */
601     for ( int i = 0; i < orphaned.count; ++i )
602     {
603       PoolItem poolItem( getPoolItem( orphaned.elements[i] ) );
604       poolItem.status().setOrphaned( true );
605       _problem_items.push_back( poolItem );
606     }
607
608     /*  solvables which are unneeded */
609     for ( int i = 0; i < unneeded.count; ++i )
610     {
611       PoolItem poolItem( getPoolItem( unneeded.elements[i] ) );
612       poolItem.status().setUnneeded( true );
613     }
614
615     queue_free(&recommendations);
616     queue_free(&suggestions);
617     queue_free(&orphaned);
618     queue_free(&unneeded);
619
620     /* Write validation state back to pool */
621     Queue flags, solvableQueue;
622
623     queue_init(&flags);
624     queue_init(&solvableQueue);
625
626     CollectPseudoInstalled collectPseudoInstalled(&solvableQueue);
627     invokeOnEach( _pool.begin(),
628                   _pool.end(),
629                   functor::functorRef<bool,PoolItem> (collectPseudoInstalled) );
630     solver_trivial_installable(_solv, &solvableQueue, &flags );
631     for (int i = 0; i < solvableQueue.count; i++) {
632         PoolItem item = _pool.find (sat::Solvable(solvableQueue.elements[i]));
633         item.status().setUndetermined();
634
635         if (flags.elements[i] == -1) {
636             item.status().setNonRelevant();
637             _XDEBUG("SATSolutionToPool(" << item << " ) nonRelevant !");
638         } else if (flags.elements[i] == 1) {
639             item.status().setSatisfied();
640             _XDEBUG("SATSolutionToPool(" << item << " ) satisfied !");
641         } else if (flags.elements[i] == 0) {
642             item.status().setBroken();
643             _XDEBUG("SATSolutionToPool(" << item << " ) broken !");
644         }
645     }
646     queue_free(&(solvableQueue));
647     queue_free(&flags);
648
649
650     // Solvables which were selected due requirements which have been made by the user will
651     // be selected by APPL_LOW. We can't use any higher level, because this setting must
652     // not serve as a request for the next solver run. APPL_LOW is reset before solving.
653     for (CapabilitySet::const_iterator iter = requires_caps.begin(); iter != requires_caps.end(); iter++) {
654         sat::WhatProvides rpmProviders(*iter);
655         for_( iter2, rpmProviders.begin(), rpmProviders.end() ) {
656             PoolItem poolItem(*iter2);
657             if (poolItem.status().isToBeInstalled()) {
658                 MIL << "User requirement " << *iter << " sets " << poolItem << endl;
659                 poolItem.status().setTransactByValue (ResStatus::APPL_LOW);
660             }
661         }
662     }
663     for (CapabilitySet::const_iterator iter = conflict_caps.begin(); iter != conflict_caps.end(); iter++) {
664         sat::WhatProvides rpmProviders(*iter);
665         for_( iter2, rpmProviders.begin(), rpmProviders.end() ) {
666             PoolItem poolItem(*iter2);
667             if (poolItem.status().isToBeUninstalled()) {
668                 MIL << "User conflict " << *iter << " sets " << poolItem << endl;
669                 poolItem.status().setTransactByValue (ResStatus::APPL_LOW);
670             }
671         }
672     }
673
674     if (solver_problem_count(_solv) > 0 )
675     {
676         ERR << "Solverrun finished with an ERROR" << endl;
677         return false;
678     }
679
680     return true;
681 }
682
683
684 void
685 SATResolver::solverInit(const PoolItemList & weakItems)
686 {
687     SATCollectTransact info (*this);
688
689     MIL << "SATResolver::solverInit()" << endl;
690
691     // remove old stuff
692     solverEnd();
693
694     queue_init( &_jobQueue );
695     _items_to_install.clear();
696     _items_to_remove.clear();
697     _items_to_lock.clear();
698     _items_to_keep.clear();
699
700     invokeOnEach ( _pool.begin(), _pool.end(),
701                    functor::functorRef<bool,PoolItem>(info) );
702
703     for (PoolItemList::const_iterator iter = weakItems.begin(); iter != weakItems.end(); iter++) {
704         Id id = (*iter)->satSolvable().id();
705         if (id == ID_NULL) {
706             ERR << "Weaken: " << *iter << " not found" << endl;
707         }
708         MIL << "Weaken dependencies of " << *iter << endl;
709         queue_push( &(_jobQueue), SOLVER_WEAKENDEPS | SOLVER_SOLVABLE );
710         queue_push( &(_jobQueue), id );
711     }
712
713     // Ad rules for changed requestedLocales
714     const auto & trackedLocaleIds( myPool().trackedLocaleIds() );
715     for ( const auto & locale : trackedLocaleIds.added() )
716     {
717       queue_push( &(_jobQueue), SOLVER_INSTALL | SOLVER_SOLVABLE_PROVIDES );
718       queue_push( &(_jobQueue), Capability( ResolverNamespace::language, IdString(locale) ).id() );
719     }
720
721     for ( const auto & locale : trackedLocaleIds.removed() )
722     {
723       queue_push( &(_jobQueue), SOLVER_ERASE | SOLVER_SOLVABLE_PROVIDES | SOLVER_CLEANDEPS );   // needs uncond. SOLVER_CLEANDEPS!
724       queue_push( &(_jobQueue), Capability( ResolverNamespace::language, IdString(locale) ).id() );
725     }
726
727     // Add rules for parallel installable resolvables with different versions
728     for ( const sat::Solvable & solv : myPool().multiversionList() )
729     {
730       queue_push( &(_jobQueue), SOLVER_NOOBSOLETES | SOLVER_SOLVABLE );
731       queue_push( &(_jobQueue), solv.id() );
732     }
733
734     ::pool_add_userinstalled_jobs(_SATPool, sat::Pool::instance().autoInstalled(), &(_jobQueue), GET_USERINSTALLED_NAMES|GET_USERINSTALLED_INVERTED);
735
736     if ( _distupgrade )
737     {
738       if ( ZConfig::instance().solverUpgradeRemoveDroppedPackages() )
739       {
740         MIL << "Checking droplists ..." << endl;
741         // Dropped packages: look for 'weakremover()' provides
742         // in dup candidates of installed products.
743         ResPoolProxy proxy( ResPool::instance().proxy() );
744         for_( it, proxy.byKindBegin<Product>(), proxy.byKindEnd<Product>() )
745         {
746           if ( (*it)->onSystem() ) // (to install) or (not to delete)
747           {
748             Product::constPtr prodCand( (*it)->candidateAsKind<Product>() );
749             if ( ! prodCand )
750               continue; // product no longer available
751
752             CapabilitySet droplist( prodCand->droplist() );
753             dumpRangeLine( MIL << "Droplist for " << (*it)->candidateObj() << ": " << droplist.size() << " ", droplist.begin(), droplist.end() ) << endl;
754             for_( cap, droplist.begin(), droplist.end() )
755             {
756               queue_push( &_jobQueue, SOLVER_DROP_ORPHANED | SOLVER_SOLVABLE_NAME );
757               queue_push( &_jobQueue, cap->id() );
758             }
759           }
760         }
761       }
762       else
763       {
764         MIL << "Droplist processing is disabled." << endl;
765       }
766     }
767 }
768
769 void
770 SATResolver::solverEnd()
771 {
772   // cleanup
773   if ( _solv )
774   {
775     solver_free(_solv);
776     _solv = NULL;
777     queue_free( &(_jobQueue) );
778   }
779 }
780
781
782 bool
783 SATResolver::resolvePool(const CapabilitySet & requires_caps,
784                          const CapabilitySet & conflict_caps,
785                          const PoolItemList & weakItems,
786                          const std::set<Repository> & upgradeRepos)
787 {
788     MIL << "SATResolver::resolvePool()" << endl;
789
790     // initialize
791     solverInit(weakItems);
792
793     for (PoolItemList::const_iterator iter = _items_to_install.begin(); iter != _items_to_install.end(); iter++) {
794         Id id = (*iter)->satSolvable().id();
795         if (id == ID_NULL) {
796             ERR << "Install: " << *iter << " not found" << endl;
797         } else {
798             MIL << "Install " << *iter << endl;
799             queue_push( &(_jobQueue), SOLVER_INSTALL | SOLVER_SOLVABLE );
800             queue_push( &(_jobQueue), id );
801         }
802     }
803
804     for (PoolItemList::const_iterator iter = _items_to_remove.begin(); iter != _items_to_remove.end(); iter++) {
805         Id id = (*iter)->satSolvable().id();
806         if (id == ID_NULL) {
807             ERR << "Delete: " << *iter << " not found" << endl;
808         } else {
809             MIL << "Delete " << *iter << endl;
810             queue_push( &(_jobQueue), SOLVER_ERASE | SOLVER_SOLVABLE | MAYBE_CLEANDEPS );
811             queue_push( &(_jobQueue), id);
812         }
813     }
814
815     for_( iter, upgradeRepos.begin(), upgradeRepos.end() )
816     {
817         queue_push( &(_jobQueue), SOLVER_DISTUPGRADE | SOLVER_SOLVABLE_REPO );
818         queue_push( &(_jobQueue), iter->get()->repoid );
819         MIL << "Upgrade repo " << *iter << endl;
820     }
821
822     for (CapabilitySet::const_iterator iter = requires_caps.begin(); iter != requires_caps.end(); iter++) {
823         queue_push( &(_jobQueue), SOLVER_INSTALL | SOLVER_SOLVABLE_PROVIDES );
824         queue_push( &(_jobQueue), iter->id() );
825         MIL << "Requires " << *iter << endl;
826     }
827
828     for (CapabilitySet::const_iterator iter = conflict_caps.begin(); iter != conflict_caps.end(); iter++) {
829         queue_push( &(_jobQueue), SOLVER_ERASE | SOLVER_SOLVABLE_PROVIDES | MAYBE_CLEANDEPS );
830         queue_push( &(_jobQueue), iter->id() );
831         MIL << "Conflicts " << *iter << endl;
832     }
833
834     // set requirements for a running system
835     setSystemRequirements();
836
837     // set locks for the solver
838     setLocks();
839
840     // solving
841     bool ret = solving(requires_caps, conflict_caps);
842
843     (ret?MIL:WAR) << "SATResolver::resolvePool() done. Ret:" << ret <<  endl;
844     return ret;
845 }
846
847
848 bool
849 SATResolver::resolveQueue(const SolverQueueItemList &requestQueue,
850                           const PoolItemList & weakItems)
851 {
852     MIL << "SATResolver::resolvQueue()" << endl;
853
854     // initialize
855     solverInit(weakItems);
856
857     // generate solver queue
858     for (SolverQueueItemList::const_iterator iter = requestQueue.begin(); iter != requestQueue.end(); iter++) {
859         (*iter)->addRule(_jobQueue);
860     }
861
862     // Add addition item status to the resolve-queue cause these can be set by problem resolutions
863     for (PoolItemList::const_iterator iter = _items_to_install.begin(); iter != _items_to_install.end(); iter++) {
864         Id id = (*iter)->satSolvable().id();
865         if (id == ID_NULL) {
866             ERR << "Install: " << *iter << " not found" << endl;
867         } else {
868             MIL << "Install " << *iter << endl;
869             queue_push( &(_jobQueue), SOLVER_INSTALL | SOLVER_SOLVABLE );
870             queue_push( &(_jobQueue), id );
871         }
872     }
873     for (PoolItemList::const_iterator iter = _items_to_remove.begin(); iter != _items_to_remove.end(); iter++) {
874         sat::detail::IdType ident( (*iter)->satSolvable().ident().id() );
875         MIL << "Delete " << *iter << ident << endl;
876         queue_push( &(_jobQueue), SOLVER_ERASE | SOLVER_SOLVABLE_NAME | MAYBE_CLEANDEPS );
877         queue_push( &(_jobQueue), ident);
878     }
879
880     // set requirements for a running system
881     setSystemRequirements();
882
883     // set locks for the solver
884     setLocks();
885
886     // solving
887     bool ret = solving();
888
889     MIL << "SATResolver::resolveQueue() done. Ret:" << ret <<  endl;
890     return ret;
891 }
892
893 /** \todo duplicate code to be joined with \ref solving. */
894 void SATResolver::doUpdate()
895 {
896     MIL << "SATResolver::doUpdate()" << endl;
897
898     // initialize
899     solverInit(PoolItemList());
900
901     // set requirements for a running system
902     setSystemRequirements();
903
904     // set locks for the solver
905     setLocks();
906
907     _solv = solver_create( _SATPool );
908     ::pool_set_custom_vendorcheck( _SATPool, &vendorCheck );
909     if (_fixsystem) {
910         queue_push( &(_jobQueue), SOLVER_VERIFY|SOLVER_SOLVABLE_ALL);
911         queue_push( &(_jobQueue), 0 );
912     }
913     if (1) {
914         queue_push( &(_jobQueue), SOLVER_UPDATE|SOLVER_SOLVABLE_ALL);
915         queue_push( &(_jobQueue), 0 );
916     }
917     if (_distupgrade) {
918         queue_push( &(_jobQueue), SOLVER_DISTUPGRADE|SOLVER_SOLVABLE_ALL);
919         queue_push( &(_jobQueue), 0 );
920     }
921     if (_distupgrade_removeunsupported) {
922         queue_push( &(_jobQueue), SOLVER_DROP_ORPHANED|SOLVER_SOLVABLE_ALL);
923         queue_push( &(_jobQueue), 0 );
924     }
925     solver_set_flag(_solv, SOLVER_FLAG_ADD_ALREADY_RECOMMENDED, !_ignorealreadyrecommended);
926     solver_set_flag(_solv, SOLVER_FLAG_ALLOW_DOWNGRADE, _allowdowngrade);
927     solver_set_flag(_solv, SOLVER_FLAG_ALLOW_UNINSTALL, _allowuninstall);
928     solver_set_flag(_solv, SOLVER_FLAG_ALLOW_ARCHCHANGE, _allowarchchange);
929     solver_set_flag(_solv, SOLVER_FLAG_ALLOW_VENDORCHANGE, _allowvendorchange);
930     solver_set_flag(_solv, SOLVER_FLAG_SPLITPROVIDES, _dosplitprovides);
931     solver_set_flag(_solv, SOLVER_FLAG_NO_UPDATEPROVIDE, _noupdateprovide);
932     solver_set_flag(_solv, SOLVER_FLAG_IGNORE_RECOMMENDED, _onlyRequires);
933
934     sat::Pool::instance().prepareForSolving();
935
936     // Solve !
937     MIL << "Starting solving for update...." << endl;
938     MIL << *this;
939     solver_solve( _solv, &(_jobQueue) );
940     MIL << "....Solver end" << endl;
941
942     // copying solution back to zypp pool
943     //-----------------------------------------
944
945     /*  solvables to be installed */
946     Queue decisionq;
947     queue_init(&decisionq);
948     solver_get_decisionqueue(_solv, &decisionq);
949     for (int i = 0; i < decisionq.count; i++)
950     {
951       Id p;
952       p = decisionq.elements[i];
953       if (p < 0 || !sat::Solvable(p))
954         continue;
955       if (sat::Solvable(p).repository().get() == _solv->pool->installed)
956         continue;
957
958       PoolItem poolItem = _pool.find (sat::Solvable(p));
959       if (poolItem) {
960           SATSolutionToPool (poolItem, ResStatus::toBeInstalled, ResStatus::SOLVER);
961       } else {
962           ERR << "id " << p << " not found in ZYPP pool." << endl;
963       }
964     }
965     queue_free(&decisionq);
966
967     /* solvables to be erased */
968     for (int i = _solv->pool->installed->start; i < _solv->pool->installed->start + _solv->pool->installed->nsolvables; i++)
969     {
970       if (solver_get_decisionlevel(_solv, i) > 0)
971           continue;
972
973       PoolItem poolItem( _pool.find( sat::Solvable(i) ) );
974       if (poolItem) {
975           // Check if this is an update
976           CheckIfUpdate info( (sat::Solvable(i)) );
977           invokeOnEach( _pool.byIdentBegin( poolItem ),
978                         _pool.byIdentEnd( poolItem ),
979                         resfilter::ByUninstalled(),                     // ByUninstalled
980                         functor::functorRef<bool,PoolItem> (info) );
981
982           if (info.is_updated) {
983               SATSolutionToPool (poolItem, ResStatus::toBeUninstalledDueToUpgrade , ResStatus::SOLVER);
984           } else {
985               SATSolutionToPool (poolItem, ResStatus::toBeUninstalled, ResStatus::SOLVER);
986           }
987       } else {
988           ERR << "id " << i << " not found in ZYPP pool." << endl;
989       }
990     }
991     MIL << "SATResolver::doUpdate() done" << endl;
992 }
993
994
995
996 //----------------------------------------------------------------------------
997 //----------------------------------------------------------------------------
998 // error handling
999 //----------------------------------------------------------------------------
1000 //----------------------------------------------------------------------------
1001
1002 //----------------------------------------------------------------------------
1003 // helper function
1004 //----------------------------------------------------------------------------
1005
1006 struct FindPackage : public resfilter::ResObjectFilterFunctor
1007 {
1008     ProblemSolutionCombi *problemSolution;
1009     TransactionKind action;
1010     FindPackage (ProblemSolutionCombi *p, const TransactionKind act)
1011        : problemSolution (p)
1012        , action (act)
1013         {
1014         }
1015
1016     bool operator()( PoolItem p)
1017    {
1018        problemSolution->addSingleAction (p, action);
1019        return true;
1020    }
1021 };
1022
1023
1024 //----------------------------------------------------------------------------
1025 // Checking if this solvable/item has a buddy which reflect the real
1026 // user visible description of an item
1027 // e.g. The release package has a buddy to the concerning product item.
1028 // This user want's the message "Product foo conflicts with product bar" and
1029 // NOT "package release-foo conflicts with package release-bar"
1030 // (ma: that's why we should map just packages to buddies, not vice versa)
1031 //----------------------------------------------------------------------------
1032 inline sat::Solvable mapBuddy( const PoolItem & item_r )
1033 {
1034   if ( item_r.satSolvable().isKind<Package>() )
1035   {
1036     sat::Solvable buddy = item_r.buddy();
1037     if ( buddy )
1038       return buddy;
1039   }
1040   return item_r.satSolvable();
1041 }
1042 inline sat::Solvable mapBuddy( sat::Solvable item_r )
1043 { return mapBuddy( PoolItem( item_r ) ); }
1044
1045 PoolItem SATResolver::mapItem ( const PoolItem & item )
1046 { return PoolItem( mapBuddy( item ) ); }
1047
1048 sat::Solvable SATResolver::mapSolvable ( const Id & id )
1049 { return mapBuddy( sat::Solvable(id) ); }
1050
1051 string SATResolver::SATprobleminfoString(Id problem, string &detail, Id &ignoreId)
1052 {
1053   string ret;
1054   Pool *pool = _solv->pool;
1055   Id probr;
1056   Id dep, source, target;
1057   sat::Solvable s, s2;
1058
1059   ignoreId = 0;
1060
1061   // FIXME: solver_findallproblemrules to get all rules for this problem
1062   // (the 'most relevabt' one returned by solver_findproblemrule is embedded
1063   probr = solver_findproblemrule(_solv, problem);
1064   switch (solver_ruleinfo(_solv, probr, &source, &target, &dep))
1065   {
1066       case SOLVER_RULE_DISTUPGRADE:
1067           s = mapSolvable (source);
1068           ret = str::form (_("%s does not belong to a distupgrade repository"), s.asString().c_str());
1069           break;
1070       case SOLVER_RULE_INFARCH:
1071           s = mapSolvable (source);
1072           ret = str::form (_("%s has inferior architecture"), s.asString().c_str());
1073           break;
1074       case SOLVER_RULE_UPDATE:
1075           s = mapSolvable (source);
1076           ret = str::form (_("problem with installed package %s"), s.asString().c_str());
1077           break;
1078       case SOLVER_RULE_JOB:
1079           ret = _("conflicting requests");
1080           break;
1081       case SOLVER_RULE_RPM:
1082           ret = _("some dependency problem");
1083           break;
1084       case SOLVER_RULE_JOB_NOTHING_PROVIDES_DEP:
1085           ret = str::form (_("nothing provides requested %s"), pool_dep2str(pool, dep));
1086           detail += _("Have you enabled all requested repositories?");
1087           break;
1088       case SOLVER_RULE_JOB_UNKNOWN_PACKAGE:
1089           ret = str::form (_("package %s does not exist"), pool_dep2str(pool, dep));
1090           detail += _("Have you enabled all requested repositories?");
1091           break;
1092       case SOLVER_RULE_JOB_UNSUPPORTED:
1093           ret = _("unsupported request");
1094           break;
1095       case SOLVER_RULE_JOB_PROVIDED_BY_SYSTEM:
1096           ret = str::form (_("%s is provided by the system and cannot be erased"), pool_dep2str(pool, dep));
1097           break;
1098       case SOLVER_RULE_RPM_NOT_INSTALLABLE:
1099           s = mapSolvable (source);
1100           ret = str::form (_("%s is not installable"), s.asString().c_str());
1101           break;
1102       case SOLVER_RULE_RPM_NOTHING_PROVIDES_DEP:
1103           ignoreId = source; // for setting weak dependencies
1104           s = mapSolvable (source);
1105           ret = str::form (_("nothing provides %s needed by %s"), pool_dep2str(pool, dep), s.asString().c_str());
1106           break;
1107       case SOLVER_RULE_RPM_SAME_NAME:
1108           s = mapSolvable (source);
1109           s2 = mapSolvable (target);
1110           ret = str::form (_("cannot install both %s and %s"), s.asString().c_str(), s2.asString().c_str());
1111           break;
1112       case SOLVER_RULE_RPM_PACKAGE_CONFLICT:
1113           s = mapSolvable (source);
1114           s2 = mapSolvable (target);
1115           ret = str::form (_("%s conflicts with %s provided by %s"), s.asString().c_str(), pool_dep2str(pool, dep), s2.asString().c_str());
1116           break;
1117       case SOLVER_RULE_RPM_PACKAGE_OBSOLETES:
1118           s = mapSolvable (source);
1119           s2 = mapSolvable (target);
1120           ret = str::form (_("%s obsoletes %s provided by %s"), s.asString().c_str(), pool_dep2str(pool, dep), s2.asString().c_str());
1121           break;
1122       case SOLVER_RULE_RPM_INSTALLEDPKG_OBSOLETES:
1123           s = mapSolvable (source);
1124           s2 = mapSolvable (target);
1125           ret = str::form (_("installed %s obsoletes %s provided by %s"), s.asString().c_str(), pool_dep2str(pool, dep), s2.asString().c_str());
1126           break;
1127       case SOLVER_RULE_RPM_SELF_CONFLICT:
1128           s = mapSolvable (source);
1129           ret = str::form (_("solvable %s conflicts with %s provided by itself"), s.asString().c_str(), pool_dep2str(pool, dep));
1130           break;
1131       case SOLVER_RULE_RPM_PACKAGE_REQUIRES:
1132           ignoreId = source; // for setting weak dependencies
1133           s = mapSolvable (source);
1134           Capability cap(dep);
1135           sat::WhatProvides possibleProviders(cap);
1136
1137           // check, if a provider will be deleted
1138           typedef list<PoolItem> ProviderList;
1139           ProviderList providerlistInstalled, providerlistUninstalled;
1140           for_( iter1, possibleProviders.begin(), possibleProviders.end() ) {
1141               PoolItem provider1 = ResPool::instance().find( *iter1 );
1142               // find pair of an installed/uninstalled item with the same NVR
1143               bool found = false;
1144               for_( iter2, possibleProviders.begin(), possibleProviders.end() ) {
1145                   PoolItem provider2 = ResPool::instance().find( *iter2 );
1146                   if (compareByNVR (provider1,provider2) == 0
1147                       && ( (provider1.status().isInstalled() && provider2.status().isUninstalled())
1148                           || (provider2.status().isInstalled() && provider1.status().isUninstalled()) ))  {
1149                       found = true;
1150                       break;
1151                   }
1152               }
1153               if (!found) {
1154                   if (provider1.status().isInstalled())
1155                       providerlistInstalled.push_back(provider1);
1156                   else
1157                       providerlistUninstalled.push_back(provider1);
1158               }
1159           }
1160
1161           ret = str::form (_("%s requires %s, but this requirement cannot be provided"), s.asString().c_str(), pool_dep2str(pool, dep));
1162           if (providerlistInstalled.size() > 0) {
1163               detail += _("deleted providers: ");
1164               for (ProviderList::const_iterator iter = providerlistInstalled.begin(); iter != providerlistInstalled.end(); iter++) {
1165                   if (iter == providerlistInstalled.begin())
1166                       detail += itemToString( *iter );
1167                   else
1168                       detail += "\n                   " + itemToString( mapItem(*iter) );
1169               }
1170           }
1171           if (providerlistUninstalled.size() > 0) {
1172               if (detail.size() > 0)
1173                   detail += _("\nuninstallable providers: ");
1174               else
1175                   detail = _("uninstallable providers: ");
1176               for (ProviderList::const_iterator iter = providerlistUninstalled.begin(); iter != providerlistUninstalled.end(); iter++) {
1177                   if (iter == providerlistUninstalled.begin())
1178                       detail += itemToString( *iter );
1179                   else
1180                       detail += "\n                   " + itemToString( mapItem(*iter) );
1181               }
1182           }
1183           break;
1184   }
1185
1186   return ret;
1187 }
1188
1189 ResolverProblemList
1190 SATResolver::problems ()
1191 {
1192     ResolverProblemList resolverProblems;
1193     if (_solv && solver_problem_count(_solv)) {
1194         Pool *pool = _solv->pool;
1195         int pcnt;
1196         Id p, rp, what;
1197         Id problem, solution, element;
1198         sat::Solvable s, sd;
1199
1200         CapabilitySet system_requires = SystemCheck::instance().requiredSystemCap();
1201         CapabilitySet system_conflicts = SystemCheck::instance().conflictSystemCap();
1202
1203         MIL << "Encountered problems! Here are the solutions:\n" << endl;
1204         pcnt = 1;
1205         problem = 0;
1206         while ((problem = solver_next_problem(_solv, problem)) != 0) {
1207             MIL << "Problem " <<  pcnt++ << ":" << endl;
1208             MIL << "====================================" << endl;
1209             string detail;
1210             Id ignoreId;
1211             string whatString = SATprobleminfoString (problem,detail,ignoreId);
1212             MIL << whatString << endl;
1213             MIL << "------------------------------------" << endl;
1214             ResolverProblem_Ptr resolverProblem = new ResolverProblem (whatString, detail);
1215
1216             solution = 0;
1217             while ((solution = solver_next_solution(_solv, problem, solution)) != 0) {
1218                 element = 0;
1219                 ProblemSolutionCombi *problemSolution = new ProblemSolutionCombi;
1220                 while ((element = solver_next_solutionelement(_solv, problem, solution, element, &p, &rp)) != 0) {
1221                     if (p == SOLVER_SOLUTION_JOB) {
1222                         /* job, rp is index into job queue */
1223                         what = _jobQueue.elements[rp];
1224                         switch (_jobQueue.elements[rp-1]&(SOLVER_SELECTMASK|SOLVER_JOBMASK))
1225                         {
1226                             case SOLVER_INSTALL | SOLVER_SOLVABLE: {
1227                                 s = mapSolvable (what);
1228                                 PoolItem poolItem = _pool.find (s);
1229                                 if (poolItem) {
1230                                     if (pool->installed && s.get()->repo == pool->installed) {
1231                                         problemSolution->addSingleAction (poolItem, REMOVE);
1232                                         string description = str::form (_("remove lock to allow removal of %s"),  s.asString().c_str() );
1233                                         MIL << description << endl;
1234                                         problemSolution->addDescription (description);
1235                                     } else {
1236                                         problemSolution->addSingleAction (poolItem, KEEP);
1237                                         string description = str::form (_("do not install %s"), s.asString().c_str());
1238                                         MIL << description << endl;
1239                                         problemSolution->addDescription (description);
1240                                     }
1241                                 } else {
1242                                     ERR << "SOLVER_INSTALL_SOLVABLE: No item found for " << s.asString() << endl;
1243                                 }
1244                             }
1245                                 break;
1246                             case SOLVER_ERASE | SOLVER_SOLVABLE: {
1247                                 s = mapSolvable (what);
1248                                 PoolItem poolItem = _pool.find (s);
1249                                 if (poolItem) {
1250                                     if (pool->installed && s.get()->repo == pool->installed) {
1251                                         problemSolution->addSingleAction (poolItem, KEEP);
1252                                         string description = str::form (_("keep %s"), s.asString().c_str());
1253                                         MIL << description << endl;
1254                                         problemSolution->addDescription (description);
1255                                     } else {
1256                                         problemSolution->addSingleAction (poolItem, UNLOCK);
1257                                         string description = str::form (_("remove lock to allow installation of %s"), itemToString( poolItem ).c_str());
1258                                         MIL << description << endl;
1259                                         problemSolution->addDescription (description);
1260                                     }
1261                                 } else {
1262                                     ERR << "SOLVER_ERASE_SOLVABLE: No item found for " << s.asString() << endl;
1263                                 }
1264                             }
1265                                 break;
1266                             case SOLVER_INSTALL | SOLVER_SOLVABLE_NAME:
1267                                 {
1268                                 IdString ident( what );
1269                                 SolverQueueItemInstall_Ptr install =
1270                                     new SolverQueueItemInstall(_pool, ident.asString(), false );
1271                                 problemSolution->addSingleAction (install, REMOVE_SOLVE_QUEUE_ITEM);
1272
1273                                 string description = str::form (_("do not install %s"), ident.c_str() );
1274                                 MIL << description << endl;
1275                                 problemSolution->addDescription (description);
1276                                 }
1277                                 break;
1278                             case SOLVER_ERASE | SOLVER_SOLVABLE_NAME:
1279                                 {
1280                                 // As we do not know, if this request has come from resolvePool or
1281                                 // resolveQueue we will have to take care for both cases.
1282                                 IdString ident( what );
1283                                 FindPackage info (problemSolution, KEEP);
1284                                 invokeOnEach( _pool.byIdentBegin( ident ),
1285                                               _pool.byIdentEnd( ident ),
1286                                               functor::chain (resfilter::ByInstalled (),                        // ByInstalled
1287                                                               resfilter::ByTransact ()),                        // will be deinstalled
1288                                               functor::functorRef<bool,PoolItem> (info) );
1289
1290                                 SolverQueueItemDelete_Ptr del =
1291                                     new SolverQueueItemDelete(_pool, ident.asString(), false );
1292                                 problemSolution->addSingleAction (del, REMOVE_SOLVE_QUEUE_ITEM);
1293
1294                                 string description = str::form (_("keep %s"), ident.c_str());
1295                                 MIL << description << endl;
1296                                 problemSolution->addDescription (description);
1297                                 }
1298                                 break;
1299                             case SOLVER_INSTALL | SOLVER_SOLVABLE_PROVIDES:
1300                                 {
1301                                 problemSolution->addSingleAction (Capability(what), REMOVE_EXTRA_REQUIRE);
1302                                 string description = "";
1303
1304                                 // Checking if this problem solution would break your system
1305                                 if (system_requires.find(Capability(what)) != system_requires.end()) {
1306                                     // Show a better warning
1307                                     resolverProblem->setDetails( resolverProblem->description() + "\n" + resolverProblem->details() );
1308                                     resolverProblem->setDescription(_("This request will break your system!"));
1309                                     description = _("ignore the warning of a broken system");
1310                                     description += string(" (requires:")+pool_dep2str(pool, what)+")";
1311                                     MIL << description << endl;
1312                                     problemSolution->addFrontDescription (description);
1313                                 } else {
1314                                     description = str::form (_("do not ask to install a solvable providing %s"), pool_dep2str(pool, what));
1315                                     MIL << description << endl;
1316                                     problemSolution->addDescription (description);
1317                                 }
1318                                 }
1319                                 break;
1320                             case SOLVER_ERASE | SOLVER_SOLVABLE_PROVIDES:
1321                                 {
1322                                 problemSolution->addSingleAction (Capability(what), REMOVE_EXTRA_CONFLICT);
1323                                 string description = "";
1324
1325                                 // Checking if this problem solution would break your system
1326                                 if (system_conflicts.find(Capability(what)) != system_conflicts.end()) {
1327                                     // Show a better warning
1328                                     resolverProblem->setDetails( resolverProblem->description() + "\n" + resolverProblem->details() );
1329                                     resolverProblem->setDescription(_("This request will break your system!"));
1330                                     description = _("ignore the warning of a broken system");
1331                                     description += string(" (conflicts:")+pool_dep2str(pool, what)+")";
1332                                     MIL << description << endl;
1333                                     problemSolution->addFrontDescription (description);
1334
1335                                 } else {
1336                                     description = str::form (_("do not ask to delete all solvables providing %s"), pool_dep2str(pool, what));
1337                                     MIL << description << endl;
1338                                     problemSolution->addDescription (description);
1339                                 }
1340                                 }
1341                                 break;
1342                             case SOLVER_UPDATE | SOLVER_SOLVABLE:
1343                                 {
1344                                 s = mapSolvable (what);
1345                                 PoolItem poolItem = _pool.find (s);
1346                                 if (poolItem) {
1347                                     if (pool->installed && s.get()->repo == pool->installed) {
1348                                         problemSolution->addSingleAction (poolItem, KEEP);
1349                                         string description = str::form (_("do not install most recent version of %s"), s.asString().c_str());
1350                                         MIL << description << endl;
1351                                         problemSolution->addDescription (description);
1352                                     } else {
1353                                         ERR << "SOLVER_INSTALL_SOLVABLE_UPDATE " << poolItem << " is not selected for installation" << endl;
1354                                     }
1355                                 } else {
1356                                     ERR << "SOLVER_INSTALL_SOLVABLE_UPDATE: No item found for " << s.asString() << endl;
1357                                 }
1358                                 }
1359                                 break;
1360                             default:
1361                                 MIL << "- do something different" << endl;
1362                                 ERR << "No valid solution available" << endl;
1363                                 break;
1364                         }
1365                     } else if (p == SOLVER_SOLUTION_INFARCH) {
1366                         s = mapSolvable (rp);
1367                         PoolItem poolItem = _pool.find (s);
1368                         if (pool->installed && s.get()->repo == pool->installed) {
1369                             problemSolution->addSingleAction (poolItem, LOCK);
1370                             string description = str::form (_("keep %s despite the inferior architecture"), s.asString().c_str());
1371                             MIL << description << endl;
1372                             problemSolution->addDescription (description);
1373                         } else {
1374                             problemSolution->addSingleAction (poolItem, INSTALL);
1375                             string description = str::form (_("install %s despite the inferior architecture"), s.asString().c_str());
1376                             MIL << description << endl;
1377                             problemSolution->addDescription (description);
1378                         }
1379                     } else if (p == SOLVER_SOLUTION_DISTUPGRADE) {
1380                         s = mapSolvable (rp);
1381                         PoolItem poolItem = _pool.find (s);
1382                         if (pool->installed && s.get()->repo == pool->installed) {
1383                             problemSolution->addSingleAction (poolItem, LOCK);
1384                             string description = str::form (_("keep obsolete %s"), s.asString().c_str());
1385                             MIL << description << endl;
1386                             problemSolution->addDescription (description);
1387                         } else {
1388                             problemSolution->addSingleAction (poolItem, INSTALL);
1389                             string description = str::form (_("install %s from excluded repository"), s.asString().c_str());
1390                             MIL << description << endl;
1391                             problemSolution->addDescription (description);
1392                         }
1393                     } else {
1394                         /* policy, replace p with rp */
1395                         s = mapSolvable (p);
1396                         PoolItem itemFrom = _pool.find (s);
1397                         if (rp)
1398                         {
1399                             int gotone = 0;
1400
1401                             sd = mapSolvable (rp);
1402                             PoolItem itemTo = _pool.find (sd);
1403                             if (itemFrom && itemTo) {
1404                                 problemSolution->addSingleAction (itemTo, INSTALL);
1405                                 int illegal = policy_is_illegal(_solv, s.get(), sd.get(), 0);
1406
1407                                 if ((illegal & POLICY_ILLEGAL_DOWNGRADE) != 0)
1408                                 {
1409                                     string description = str::form (_("downgrade of %s to %s"), s.asString().c_str(), sd.asString().c_str());
1410                                     MIL << description << endl;
1411                                     problemSolution->addDescription (description);
1412                                     gotone = 1;
1413                                 }
1414                                 if ((illegal & POLICY_ILLEGAL_ARCHCHANGE) != 0)
1415                                 {
1416                                     string description = str::form (_("architecture change of %s to %s"), s.asString().c_str(), sd.asString().c_str());
1417                                     MIL << description << endl;
1418                                     problemSolution->addDescription (description);
1419                                     gotone = 1;
1420                                 }
1421                                 if ((illegal & POLICY_ILLEGAL_VENDORCHANGE) != 0)
1422                                 {
1423                                     IdString s_vendor( s.vendor() );
1424                                     IdString sd_vendor( sd.vendor() );
1425                                     string description = str::form (_("install %s (with vendor change)\n  %s  -->  %s") ,
1426                                                                     sd.asString().c_str(),
1427                                                                     ( s_vendor ? s_vendor.c_str() : " (no vendor) " ),
1428                                                                     ( sd_vendor ? sd_vendor.c_str() : " (no vendor) " ) );
1429                                     MIL << description << endl;
1430                                     problemSolution->addDescription (description);
1431                                     gotone = 1;
1432                                 }
1433                                 if (!gotone) {
1434                                     string description = str::form (_("replacement of %s with %s"), s.asString().c_str(), sd.asString().c_str());
1435                                     MIL << description << endl;
1436                                     problemSolution->addDescription (description);
1437                                 }
1438                             } else {
1439                                 ERR << s.asString() << " or "  << sd.asString() << " not found" << endl;
1440                             }
1441                         }
1442                         else
1443                         {
1444                             if (itemFrom) {
1445                                 string description = str::form (_("deinstallation of %s"), s.asString().c_str());
1446                                 MIL << description << endl;
1447                                 problemSolution->addDescription (description);
1448                                 problemSolution->addSingleAction (itemFrom, REMOVE);
1449                             }
1450                         }
1451                     }
1452                 }
1453                 resolverProblem->addSolution (problemSolution,
1454                                               problemSolution->actionCount() > 1 ? true : false); // Solutions with more than 1 action will be shown first.
1455                 MIL << "------------------------------------" << endl;
1456             }
1457
1458             if (ignoreId > 0) {
1459                 // There is a possibility to ignore this error by setting weak dependencies
1460                 PoolItem item = _pool.find (sat::Solvable(ignoreId));
1461                 ProblemSolutionIgnore *problemSolution = new ProblemSolutionIgnore(item);
1462                 resolverProblem->addSolution (problemSolution,
1463                                               false); // Solutions will be shown at the end
1464                 MIL << "ignore some dependencies of " << item << endl;
1465                 MIL << "------------------------------------" << endl;
1466             }
1467
1468             // save problem
1469             resolverProblems.push_back (resolverProblem);
1470         }
1471     }
1472     return resolverProblems;
1473 }
1474
1475 void SATResolver::applySolutions( const ProblemSolutionList & solutions )
1476 { Resolver( _pool ).applySolutions( solutions ); }
1477
1478 void SATResolver::setLocks()
1479 {
1480     for (PoolItemList::const_iterator iter = _items_to_lock.begin(); iter != _items_to_lock.end(); ++iter) {
1481         sat::detail::SolvableIdType ident( (*iter)->satSolvable().id() );
1482         if (iter->status().isInstalled()) {
1483             MIL << "Lock installed item " << *iter << endl;
1484             queue_push( &(_jobQueue), SOLVER_INSTALL | SOLVER_SOLVABLE );
1485             queue_push( &(_jobQueue), ident );
1486         } else {
1487             MIL << "Lock NOT installed item " << *iter << endl;
1488             queue_push( &(_jobQueue), SOLVER_ERASE | SOLVER_SOLVABLE | MAYBE_CLEANDEPS );
1489             queue_push( &(_jobQueue), ident );
1490         }
1491     }
1492
1493     ///////////////////////////////////////////////////////////////////
1494     // Weak locks: Ignore if an item with this name is already installed.
1495     // If it's not installed try to keep it this way using a weak delete
1496     ///////////////////////////////////////////////////////////////////
1497     std::set<IdString> unifiedByName;
1498     for (PoolItemList::const_iterator iter = _items_to_keep.begin(); iter != _items_to_keep.end(); ++iter) {
1499       IdString ident( (*iter)->satSolvable().ident() );
1500       if ( unifiedByName.insert( ident ).second )
1501       {
1502         if ( ! ui::Selectable::get( *iter )->hasInstalledObj() )
1503         {
1504           MIL << "Keep NOT installed name " << ident << " (" << *iter << ")" << endl;
1505           queue_push( &(_jobQueue), SOLVER_ERASE | SOLVER_SOLVABLE_NAME | SOLVER_WEAK | MAYBE_CLEANDEPS );
1506           queue_push( &(_jobQueue), ident.id() );
1507         }
1508       }
1509     }
1510 }
1511
1512 void SATResolver::setSystemRequirements()
1513 {
1514     CapabilitySet system_requires = SystemCheck::instance().requiredSystemCap();
1515     CapabilitySet system_conflicts = SystemCheck::instance().conflictSystemCap();
1516
1517     for (CapabilitySet::const_iterator iter = system_requires.begin(); iter != system_requires.end(); ++iter) {
1518         queue_push( &(_jobQueue), SOLVER_INSTALL | SOLVER_SOLVABLE_PROVIDES );
1519         queue_push( &(_jobQueue), iter->id() );
1520         MIL << "SYSTEM Requires " << *iter << endl;
1521     }
1522
1523     for (CapabilitySet::const_iterator iter = system_conflicts.begin(); iter != system_conflicts.end(); ++iter) {
1524         queue_push( &(_jobQueue), SOLVER_ERASE | SOLVER_SOLVABLE_PROVIDES | MAYBE_CLEANDEPS );
1525         queue_push( &(_jobQueue), iter->id() );
1526         MIL << "SYSTEM Conflicts " << *iter << endl;
1527     }
1528
1529     // Lock the architecture of the running systems rpm
1530     // package on distupgrade.
1531     if ( _distupgrade && ZConfig::instance().systemRoot() == "/" )
1532     {
1533       ResPool pool( ResPool::instance() );
1534       IdString rpm( "rpm" );
1535       for_( it, pool.byIdentBegin(rpm), pool.byIdentEnd(rpm) )
1536       {
1537         if ( (*it)->isSystem() )
1538         {
1539           Capability archrule( (*it)->arch(), rpm.c_str(), Capability::PARSED );
1540           queue_push( &(_jobQueue), SOLVER_INSTALL | SOLVER_SOLVABLE_NAME | SOLVER_ESSENTIAL );
1541           queue_push( &(_jobQueue), archrule.id() );
1542
1543         }
1544       }
1545     }
1546 }
1547
1548 sat::StringQueue SATResolver::autoInstalled() const
1549 {
1550   sat::StringQueue ret;
1551   if ( _solv )
1552     ::solver_get_userinstalled( _solv, ret, GET_USERINSTALLED_NAMES|GET_USERINSTALLED_INVERTED );
1553   return ret;
1554 }
1555
1556 sat::StringQueue SATResolver::userInstalled() const
1557 {
1558   sat::StringQueue ret;
1559   if ( _solv )
1560     ::solver_get_userinstalled( _solv, ret, GET_USERINSTALLED_NAMES );
1561   return ret;
1562 }
1563
1564
1565 ///////////////////////////////////////////////////////////////////
1566 };// namespace detail
1567     /////////////////////////////////////////////////////////////////////
1568     /////////////////////////////////////////////////////////////////////
1569   };// namespace solver
1570   ///////////////////////////////////////////////////////////////////////
1571   ///////////////////////////////////////////////////////////////////////
1572 };// namespace zypp
1573 /////////////////////////////////////////////////////////////////////////
1574