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