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