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