f2217d987d6e572322a388a501fe0a203ed0a2ef
[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_IGNORE_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 sat::Transaction SATResolver::getTransaction()
166 {
167   if ( !_solv )
168     return sat::Transaction();
169
170   ::Transaction * sattrans = ::solver_create_transaction( _solv );
171   sat::Transaction ret ( *sattrans );
172   ::transaction_free( sattrans );
173   return ret;
174 }
175
176 ResPool
177 SATResolver::pool (void) const
178 {
179     return _pool;
180 }
181
182 void
183 SATResolver::resetItemTransaction (PoolItem item)
184 {
185     bool found = false;
186     for (PoolItemList::const_iterator iter = _items_to_remove.begin();
187          iter != _items_to_remove.end(); ++iter) {
188         if (*iter == item) {
189             _items_to_remove.remove(*iter);
190             found = true;
191             break;
192         }
193     }
194     if (!found) {
195         for (PoolItemList::const_iterator iter = _items_to_install.begin();
196              iter != _items_to_install.end(); ++iter) {
197             if (*iter == item) {
198                 _items_to_install.remove(*iter);
199                 found = true;
200                 break;
201             }
202         }
203     }
204     if (!found) {
205         for (PoolItemList::const_iterator iter = _items_to_keep.begin();
206              iter != _items_to_keep.end(); ++iter) {
207             if (*iter == item) {
208                 _items_to_keep.remove(*iter);
209                 found = true;
210                 break;
211             }
212         }
213     }
214     if (!found) {
215         for (PoolItemList::const_iterator iter = _items_to_lock.begin();
216              iter != _items_to_lock.end(); ++iter) {
217             if (*iter == item) {
218                 _items_to_lock.remove(*iter);
219                 found = true;
220                 break;
221             }
222         }
223     }
224 }
225
226
227 void
228 SATResolver::addPoolItemToInstall (PoolItem item)
229 {
230     resetItemTransaction (item);
231     _items_to_install.push_back (item);
232     _items_to_install.unique ();
233 }
234
235
236 void
237 SATResolver::addPoolItemsToInstallFromList (PoolItemList & rl)
238 {
239     for (PoolItemList::const_iterator iter = rl.begin(); iter != rl.end(); iter++) {
240         addPoolItemToInstall (*iter);
241     }
242 }
243
244
245 void
246 SATResolver::addPoolItemToRemove (PoolItem item)
247 {
248     resetItemTransaction (item);
249     _items_to_remove.push_back (item);
250     _items_to_remove.unique ();
251 }
252
253
254 void
255 SATResolver::addPoolItemsToRemoveFromList (PoolItemList & rl)
256 {
257     for (PoolItemList::const_iterator iter = rl.begin(); iter != rl.end(); iter++) {
258         addPoolItemToRemove (*iter);
259     }
260 }
261
262 void
263 SATResolver::addPoolItemToLock (PoolItem item)
264 {
265     resetItemTransaction (item);
266     _items_to_lock.push_back (item);
267     _items_to_lock.unique ();
268 }
269
270 void
271 SATResolver::addPoolItemToKeep (PoolItem item)
272 {
273     resetItemTransaction (item);
274     _items_to_keep.push_back (item);
275     _items_to_keep.unique ();
276 }
277
278 //---------------------------------------------------------------------------
279
280 // copy marked item from solution back to pool
281 // if data != NULL, set as APPL_LOW (from establishPool())
282
283 static void
284 SATSolutionToPool (PoolItem item, const ResStatus & status, const ResStatus::TransactByValue causer)
285 {
286     // resetting
287     item.status().resetTransact (causer);
288     item.status().resetWeak ();
289
290     bool r;
291
292     // installation/deletion
293     if (status.isToBeInstalled()) {
294         r = item.status().setToBeInstalled (causer);
295         _XDEBUG("SATSolutionToPool install returns " << item << ", " << r);
296     }
297     else if (status.isToBeUninstalledDueToUpgrade()) {
298         r = item.status().setToBeUninstalledDueToUpgrade (causer);
299         _XDEBUG("SATSolutionToPool upgrade returns " << item << ", " <<  r);
300     }
301     else if (status.isToBeUninstalled()) {
302         r = item.status().setToBeUninstalled (causer);
303         _XDEBUG("SATSolutionToPool remove returns " << item << ", " <<  r);
304     }
305
306     return;
307 }
308
309 //----------------------------------------------------------------------------
310 //----------------------------------------------------------------------------
311 // resolvePool
312 //----------------------------------------------------------------------------
313 //----------------------------------------------------------------------------
314
315 //----------------------------------------------------------------------------
316 // Helper functions for the ZYPP-Pool
317 //----------------------------------------------------------------------------
318
319
320 //------------------------------------------------------------------------------------------------------------
321 //  This function loops over the pool and grabs all items
322 //  It clears all previous bySolver() states also
323 //
324 //  Every toBeInstalled is passed to zypp::solver:detail::Resolver.addPoolItemToInstall()
325 //  Every toBeUninstalled is passed to zypp::solver:detail::Resolver.addPoolItemToRemove()
326 //
327 //  Solver results must be written back to the pool.
328 //------------------------------------------------------------------------------------------------------------
329
330
331 struct SATCollectTransact : public resfilter::PoolItemFilterFunctor
332 {
333     SATResolver & resolver;
334
335     SATCollectTransact (SATResolver & r)
336         : resolver (r)
337     { }
338
339     bool operator()( PoolItem item )            // only transacts() items go here
340     {
341         ResStatus status = item.status();
342         bool by_solver = (status.isBySolver() || status.isByApplLow());
343
344         if (by_solver) {
345             item.status().resetTransact( ResStatus::APPL_LOW );// clear any solver/establish transactions
346             return true;                                // back out here, dont re-queue former solver result
347         }
348
349         if ( item.satSolvable().isKind<SrcPackage>() && ! resolver.solveSrcPackages() )
350         {
351           // Later we may continue on a per source package base.
352           return true; // dont process this source package.
353         }
354
355         if (status.isToBeInstalled()) {
356             resolver.addPoolItemToInstall(item);        // -> install!
357         }
358         else if (status.isToBeUninstalled()) {
359             resolver.addPoolItemToRemove(item);         // -> remove !
360         }
361         else if (status.isLocked()
362                  && !by_solver) {
363             resolver.addPoolItemToLock (item);
364         }
365         else if (status.isKept()
366                  && !by_solver) {
367             resolver.addPoolItemToKeep (item);
368         }
369
370         return true;
371     }
372 };
373
374
375 //----------------------------------------------------------------------------
376 //----------------------------------------------------------------------------
377 // solving.....
378 //----------------------------------------------------------------------------
379 //----------------------------------------------------------------------------
380
381
382 class CheckIfUpdate : public resfilter::PoolItemFilterFunctor
383 {
384   public:
385     bool is_updated;
386     bool multiversion;
387     sat::Solvable _installed;
388
389     CheckIfUpdate( sat::Solvable installed_r )
390         : is_updated( false )
391         , multiversion( installed_r.multiversionInstall() )
392         , _installed( installed_r )
393     {}
394
395     // check this item will be updated
396
397     bool operator()( PoolItem item )
398     {
399         if ( item.status().isToBeInstalled() )
400         {
401           if ( ! multiversion || sameNVRA( _installed, item ) )
402           {
403             is_updated = true;
404             return false;
405           }
406         }
407         return true;
408     }
409 };
410
411
412 class CollectPseudoInstalled : public resfilter::PoolItemFilterFunctor
413 {
414   public:
415     Queue *solvableQueue;
416
417     CollectPseudoInstalled( Queue *queue )
418         :solvableQueue (queue)
419     {}
420
421     // collecting PseudoInstalled items
422     bool operator()( PoolItem item )
423     {
424       if ( traits::isPseudoInstalled( item.satSolvable().kind() ) )
425         queue_push( solvableQueue, item.satSolvable().id() );
426       return true;
427     }
428 };
429
430 bool
431 SATResolver::solving(const CapabilitySet & requires_caps,
432                      const CapabilitySet & conflict_caps)
433 {
434     _solv = solver_create( _SATPool );
435     _solv->vendorCheckCb = &vendorCheck;
436     if (_fixsystem) {
437         queue_push( &(_jobQueue), SOLVER_VERIFY|SOLVER_SOLVABLE_ALL);
438         queue_push( &(_jobQueue), 0 );
439     }
440     if (_updatesystem) {
441         queue_push( &(_jobQueue), SOLVER_UPDATE|SOLVER_SOLVABLE_ALL);
442         queue_push( &(_jobQueue), 0 );
443     }
444     if (_distupgrade) {
445         queue_push( &(_jobQueue), SOLVER_DISTUPGRADE|SOLVER_SOLVABLE_ALL);
446         queue_push( &(_jobQueue), 0 );
447     }
448     if (_distupgrade_removeunsupported) {
449         queue_push( &(_jobQueue), SOLVER_DROP_ORPHANED|SOLVER_SOLVABLE_ALL);
450         queue_push( &(_jobQueue), 0 );
451     }
452     solver_set_flag(_solv, SOLVER_FLAG_IGNORE_ALREADY_RECOMMENDED, _ignorealreadyrecommended);
453     solver_set_flag(_solv, SOLVER_FLAG_ALLOW_DOWNGRADE, _allowdowngrade);
454     solver_set_flag(_solv, SOLVER_FLAG_ALLOW_UNINSTALL, _allowuninstall);
455     solver_set_flag(_solv, SOLVER_FLAG_ALLOW_ARCHCHANGE, _allowarchchange);
456     solver_set_flag(_solv, SOLVER_FLAG_ALLOW_VENDORCHANGE, _allowvendorchange);
457     solver_set_flag(_solv, SOLVER_FLAG_SPLITPROVIDES, _dosplitprovides);
458     solver_set_flag(_solv, SOLVER_FLAG_NO_UPDATEPROVIDE, _noupdateprovide);
459     solver_set_flag(_solv, SOLVER_FLAG_IGNORE_RECOMMENDED, _onlyRequires);
460
461     sat::Pool::instance().prepareForSolving();
462
463     // Solve !
464     MIL << "Starting solving...." << endl;
465     MIL << *this;
466     solver_solve( _solv, &(_jobQueue) );
467     MIL << "....Solver end" << endl;
468
469     // copying solution back to zypp pool
470     //-----------------------------------------
471     _result_items_to_install.clear();
472     _result_items_to_remove.clear();
473
474     /*  solvables to be installed */
475     Queue decisionq;
476     queue_init(&decisionq);
477     solver_get_decisionqueue(_solv, &decisionq);
478     for ( int i = 0; i < decisionq.count; ++i )
479     {
480       sat::Solvable slv( decisionq.elements[i] );
481       if ( !slv || slv.isSystem() )
482         continue;
483
484       PoolItem poolItem( slv );
485       SATSolutionToPool (poolItem, ResStatus::toBeInstalled, ResStatus::SOLVER);
486       _result_items_to_install.push_back (poolItem);
487     }
488     queue_free(&decisionq);
489
490     /* solvables to be erased */
491     Repository systemRepo( sat::Pool::instance().findSystemRepo() ); // don't create if it does not exist
492     if ( systemRepo && ! systemRepo.solvablesEmpty() )
493     {
494       bool mustCheckObsoletes = false;
495       for_( it, systemRepo.solvablesBegin(), systemRepo.solvablesEnd() )
496       {
497         if (solver_get_decisionlevel(_solv, it->id()) > 0)
498           continue;
499
500         // Check if this is an update
501         CheckIfUpdate info( *it );
502         PoolItem poolItem( *it );
503         invokeOnEach( _pool.byIdentBegin( poolItem ),
504                       _pool.byIdentEnd( poolItem ),
505                       resfilter::ByUninstalled(),                       // ByUninstalled
506                       functor::functorRef<bool,PoolItem> (info) );
507
508         if (info.is_updated) {
509           SATSolutionToPool( poolItem, ResStatus::toBeUninstalledDueToUpgrade, ResStatus::SOLVER );
510         } else {
511           SATSolutionToPool( poolItem, ResStatus::toBeUninstalled, ResStatus::SOLVER );
512           if ( ! mustCheckObsoletes )
513             mustCheckObsoletes = true; // lazy check for UninstalledDueToObsolete
514         }
515         _result_items_to_remove.push_back (poolItem);
516       }
517       if ( mustCheckObsoletes )
518       {
519         sat::WhatObsoletes obsoleted( _result_items_to_install.begin(), _result_items_to_install.end() );
520         for_( it, obsoleted.poolItemBegin(), obsoleted.poolItemEnd() )
521         {
522           ResStatus & status( it->status() );
523           // WhatObsoletes contains installed items only!
524           if ( status.transacts() && ! status.isToBeUninstalledDueToUpgrade() )
525             status.setToBeUninstalledDueToObsolete();
526         }
527       }
528     }
529
530     /*  solvables which are recommended */
531     for ( int i = 0; i < _solv->recommendations.count; ++i )
532     {
533       PoolItem poolItem( getPoolItem( _solv->recommendations.elements[i] ) );
534       poolItem.status().setRecommended( true );
535     }
536
537     /*  solvables which are suggested */
538     for ( int i = 0; i < _solv->suggestions.count; ++i )
539     {
540       PoolItem poolItem( getPoolItem( _solv->suggestions.elements[i] ) );
541       poolItem.status().setSuggested( true );
542     }
543
544     _problem_items.clear();
545     /*  solvables which are orphaned */
546     for ( int i = 0; i < _solv->orphaned.count; ++i )
547     {
548       PoolItem poolItem( getPoolItem( _solv->orphaned.elements[i] ) );
549       poolItem.status().setOrphaned( true );
550       _problem_items.push_back( poolItem );
551     }
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 | 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_IGNORE_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, true);
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 //----------------------------------------------------------------------------
958
959
960 PoolItem SATResolver::mapItem (const PoolItem &item)
961 {
962     sat::Solvable buddy = item.buddy();
963     if (buddy != sat::Solvable())
964     {
965         return _pool.find (buddy);
966     }
967     else
968     {
969         return item;
970     }
971 }
972
973 sat::Solvable SATResolver::mapSolvable (const Id &id)
974 {
975     PoolItem item = _pool.find (sat::Solvable(id));
976     return mapItem(item).satSolvable();
977 }
978
979 string SATResolver::SATprobleminfoString(Id problem, string &detail, Id &ignoreId)
980 {
981   string ret;
982   Pool *pool = _solv->pool;
983   Id probr;
984   Id dep, source, target;
985   sat::Solvable s, s2;
986
987   ignoreId = 0;
988   probr = solver_findproblemrule(_solv, problem);
989   switch (solver_ruleinfo(_solv, probr, &source, &target, &dep))
990   {
991       case SOLVER_RULE_DISTUPGRADE:
992           s = mapSolvable (source);
993           ret = str::form (_("%s does not belong to a distupgrade repository"), s.asString().c_str());
994           break;
995       case SOLVER_RULE_INFARCH:
996           s = mapSolvable (source);
997           ret = str::form (_("%s has inferior architecture"), s.asString().c_str());
998           break;
999       case SOLVER_RULE_UPDATE:
1000           s = mapSolvable (source);
1001           ret = str::form (_("problem with installed package %s"), s.asString().c_str());
1002           break;
1003       case SOLVER_RULE_JOB:
1004           ret = _("conflicting requests");
1005           break;
1006       case SOLVER_RULE_RPM:
1007           ret = _("some dependency problem");
1008           break;
1009       case SOLVER_RULE_JOB_NOTHING_PROVIDES_DEP:
1010           ret = str::form (_("nothing provides requested %s"), pool_dep2str(pool, dep));
1011           detail += _("Have you enabled all requested repositories?");
1012           break;
1013       case SOLVER_RULE_RPM_NOT_INSTALLABLE:
1014           s = mapSolvable (source);
1015           ret = str::form (_("%s is not installable"), s.asString().c_str());
1016           break;
1017       case SOLVER_RULE_RPM_NOTHING_PROVIDES_DEP:
1018           ignoreId = source; // for setting weak dependencies
1019           s = mapSolvable (source);
1020           ret = str::form (_("nothing provides %s needed by %s"), pool_dep2str(pool, dep), s.asString().c_str());
1021           break;
1022       case SOLVER_RULE_RPM_SAME_NAME:
1023           s = mapSolvable (source);
1024           s2 = mapSolvable (target);
1025           ret = str::form (_("cannot install both %s and %s"), s.asString().c_str(), s2.asString().c_str());
1026           break;
1027       case SOLVER_RULE_RPM_PACKAGE_CONFLICT:
1028           s = mapSolvable (source);
1029           s2 = mapSolvable (target);
1030           ret = str::form (_("%s conflicts with %s provided by %s"), s.asString().c_str(), pool_dep2str(pool, dep), s2.asString().c_str());
1031           break;
1032       case SOLVER_RULE_RPM_PACKAGE_OBSOLETES:
1033           s = mapSolvable (source);
1034           s2 = mapSolvable (target);
1035           ret = str::form (_("%s obsoletes %s provided by %s"), s.asString().c_str(), pool_dep2str(pool, dep), s2.asString().c_str());
1036           break;
1037       case SOLVER_RULE_RPM_INSTALLEDPKG_OBSOLETES:
1038           s = mapSolvable (source);
1039           s2 = mapSolvable (target);
1040           ret = str::form (_("installed %s obsoletes %s provided by %s"), s.asString().c_str(), pool_dep2str(pool, dep), s2.asString().c_str());
1041           break;
1042       case SOLVER_RULE_RPM_SELF_CONFLICT:
1043           s = mapSolvable (source);
1044           ret = str::form (_("solvable %s conflicts with %s provided by itself"), s.asString().c_str(), pool_dep2str(pool, dep));
1045           break;
1046       case SOLVER_RULE_RPM_PACKAGE_REQUIRES:
1047           ignoreId = source; // for setting weak dependencies
1048           s = mapSolvable (source);
1049           Capability cap(dep);
1050           sat::WhatProvides possibleProviders(cap);
1051
1052           // check, if a provider will be deleted
1053           typedef list<PoolItem> ProviderList;
1054           ProviderList providerlistInstalled, providerlistUninstalled;
1055           for_( iter1, possibleProviders.begin(), possibleProviders.end() ) {
1056               PoolItem provider1 = ResPool::instance().find( *iter1 );
1057               // find pair of an installed/uninstalled item with the same NVR
1058               bool found = false;
1059               for_( iter2, possibleProviders.begin(), possibleProviders.end() ) {
1060                   PoolItem provider2 = ResPool::instance().find( *iter2 );
1061                   if (compareByNVR (provider1.resolvable(),provider2.resolvable()) == 0
1062                       && ( (provider1.status().isInstalled() && provider2.status().isUninstalled())
1063                           || (provider2.status().isInstalled() && provider1.status().isUninstalled()) ))  {
1064                       found = true;
1065                       break;
1066                   }
1067               }
1068               if (!found) {
1069                   if (provider1.status().isInstalled())
1070                       providerlistInstalled.push_back(provider1);
1071                   else
1072                       providerlistUninstalled.push_back(provider1);
1073               }
1074           }
1075
1076           ret = str::form (_("%s requires %s, but this requirement cannot be provided"), s.asString().c_str(), pool_dep2str(pool, dep));
1077           if (providerlistInstalled.size() > 0) {
1078               detail += _("deleted providers: ");
1079               for (ProviderList::const_iterator iter = providerlistInstalled.begin(); iter != providerlistInstalled.end(); iter++) {
1080                   if (iter == providerlistInstalled.begin())
1081                       detail += itemToString( *iter );
1082                   else
1083                       detail += "\n                   " + itemToString( mapItem(*iter) );
1084               }
1085           }
1086           if (providerlistUninstalled.size() > 0) {
1087               if (detail.size() > 0)
1088                   detail += _("\nuninstallable providers: ");
1089               else
1090                   detail = _("uninstallable providers: ");
1091               for (ProviderList::const_iterator iter = providerlistUninstalled.begin(); iter != providerlistUninstalled.end(); iter++) {
1092                   if (iter == providerlistUninstalled.begin())
1093                       detail += itemToString( *iter );
1094                   else
1095                       detail += "\n                   " + itemToString( mapItem(*iter) );
1096               }
1097           }
1098           break;
1099   }
1100
1101   return ret;
1102 }
1103
1104 ResolverProblemList
1105 SATResolver::problems ()
1106 {
1107     ResolverProblemList resolverProblems;
1108     if (_solv && solver_problem_count(_solv)) {
1109         Pool *pool = _solv->pool;
1110         int pcnt;
1111         Id p, rp, what;
1112         Id problem, solution, element;
1113         sat::Solvable s, sd;
1114
1115         CapabilitySet system_requires = SystemCheck::instance().requiredSystemCap();
1116         CapabilitySet system_conflicts = SystemCheck::instance().conflictSystemCap();
1117
1118         MIL << "Encountered problems! Here are the solutions:\n" << endl;
1119         pcnt = 1;
1120         problem = 0;
1121         while ((problem = solver_next_problem(_solv, problem)) != 0) {
1122             MIL << "Problem " <<  pcnt++ << ":" << endl;
1123             MIL << "====================================" << endl;
1124             string detail;
1125             Id ignoreId;
1126             string whatString = SATprobleminfoString (problem,detail,ignoreId);
1127             MIL << whatString << endl;
1128             MIL << "------------------------------------" << endl;
1129             ResolverProblem_Ptr resolverProblem = new ResolverProblem (whatString, detail);
1130
1131             solution = 0;
1132             while ((solution = solver_next_solution(_solv, problem, solution)) != 0) {
1133                 element = 0;
1134                 ProblemSolutionCombi *problemSolution = new ProblemSolutionCombi(resolverProblem);
1135                 while ((element = solver_next_solutionelement(_solv, problem, solution, element, &p, &rp)) != 0) {
1136                     if (p == SOLVER_SOLUTION_JOB) {
1137                         /* job, rp is index into job queue */
1138                         what = _jobQueue.elements[rp];
1139                         switch (_jobQueue.elements[rp-1]&(SOLVER_SELECTMASK|SOLVER_JOBMASK))
1140                         {
1141                             case SOLVER_INSTALL | SOLVER_SOLVABLE: {
1142                                 s = mapSolvable (what);
1143                                 PoolItem poolItem = _pool.find (s);
1144                                 if (poolItem) {
1145                                     if (pool->installed && s.get()->repo == pool->installed) {
1146                                         problemSolution->addSingleAction (poolItem, REMOVE);
1147                                         string description = str::form (_("do not keep %s installed"),  s.asString().c_str() );
1148                                         MIL << description << endl;
1149                                         problemSolution->addDescription (description);
1150                                     } else {
1151                                         problemSolution->addSingleAction (poolItem, KEEP);
1152                                         string description = str::form (_("do not install %s"), s.asString().c_str());
1153                                         MIL << description << endl;
1154                                         problemSolution->addDescription (description);
1155                                     }
1156                                 } else {
1157                                     ERR << "SOLVER_INSTALL_SOLVABLE: No item found for " << s.asString() << endl;
1158                                 }
1159                             }
1160                                 break;
1161                             case SOLVER_ERASE | SOLVER_SOLVABLE: {
1162                                 s = mapSolvable (what);
1163                                 PoolItem poolItem = _pool.find (s);
1164                                 if (poolItem) {
1165                                     if (pool->installed && s.get()->repo == pool->installed) {
1166                                         problemSolution->addSingleAction (poolItem, KEEP);
1167                                         string description = str::form (_("keep %s"), s.asString().c_str());
1168                                         MIL << description << endl;
1169                                         problemSolution->addDescription (description);
1170                                     } else {
1171                                         problemSolution->addSingleAction (poolItem, UNLOCK);
1172                                         string description = str::form (_("do not forbid installation of %s"), itemToString( poolItem ).c_str());
1173                                         MIL << description << endl;
1174                                         problemSolution->addDescription (description);
1175                                     }
1176                                 } else {
1177                                     ERR << "SOLVER_ERASE_SOLVABLE: No item found for " << s.asString() << endl;
1178                                 }
1179                             }
1180                                 break;
1181                             case SOLVER_INSTALL | SOLVER_SOLVABLE_NAME:
1182                                 {
1183                                 IdString ident( what );
1184                                 SolverQueueItemInstall_Ptr install =
1185                                     new SolverQueueItemInstall(_pool, ident.asString(), false );
1186                                 problemSolution->addSingleAction (install, REMOVE_SOLVE_QUEUE_ITEM);
1187
1188                                 string description = str::form (_("do not install %s"), ident.c_str() );
1189                                 MIL << description << endl;
1190                                 problemSolution->addDescription (description);
1191                                 }
1192                                 break;
1193                             case SOLVER_ERASE | SOLVER_SOLVABLE_NAME:
1194                                 {
1195                                 // As we do not know, if this request has come from resolvePool or
1196                                 // resolveQueue we will have to take care for both cases.
1197                                 IdString ident( what );
1198                                 FindPackage info (problemSolution, KEEP);
1199                                 invokeOnEach( _pool.byIdentBegin( ident ),
1200                                               _pool.byIdentEnd( ident ),
1201                                               functor::chain (resfilter::ByInstalled (),                        // ByInstalled
1202                                                               resfilter::ByTransact ()),                        // will be deinstalled
1203                                               functor::functorRef<bool,PoolItem> (info) );
1204
1205                                 SolverQueueItemDelete_Ptr del =
1206                                     new SolverQueueItemDelete(_pool, ident.asString(), false );
1207                                 problemSolution->addSingleAction (del, REMOVE_SOLVE_QUEUE_ITEM);
1208
1209                                 string description = str::form (_("keep %s"), ident.c_str());
1210                                 MIL << description << endl;
1211                                 problemSolution->addDescription (description);
1212                                 }
1213                                 break;
1214                             case SOLVER_INSTALL | SOLVER_SOLVABLE_PROVIDES:
1215                                 {
1216                                 problemSolution->addSingleAction (Capability(what), REMOVE_EXTRA_REQUIRE);
1217                                 string description = "";
1218
1219                                 // Checking if this problem solution would break your system
1220                                 if (system_requires.find(Capability(what)) != system_requires.end()) {
1221                                     // Show a better warning
1222                                     resolverProblem->setDetails( resolverProblem->description() + "\n" + resolverProblem->details() );
1223                                     resolverProblem->setDescription(_("This request will break your system!"));
1224                                     description = _("ignore the warning of a broken system");
1225                                     description += string(" (requires:")+pool_dep2str(pool, what)+")";
1226                                     MIL << description << endl;
1227                                     problemSolution->addFrontDescription (description);
1228                                 } else {
1229                                     description = str::form (_("do not ask to install a solvable providing %s"), pool_dep2str(pool, what));
1230                                     MIL << description << endl;
1231                                     problemSolution->addDescription (description);
1232                                 }
1233                                 }
1234                                 break;
1235                             case SOLVER_ERASE | SOLVER_SOLVABLE_PROVIDES:
1236                                 {
1237                                 problemSolution->addSingleAction (Capability(what), REMOVE_EXTRA_CONFLICT);
1238                                 string description = "";
1239
1240                                 // Checking if this problem solution would break your system
1241                                 if (system_conflicts.find(Capability(what)) != system_conflicts.end()) {
1242                                     // Show a better warning
1243                                     resolverProblem->setDetails( resolverProblem->description() + "\n" + resolverProblem->details() );
1244                                     resolverProblem->setDescription(_("This request will break your system!"));
1245                                     description = _("ignore the warning of a broken system");
1246                                     description += string(" (conflicts:")+pool_dep2str(pool, what)+")";
1247                                     MIL << description << endl;
1248                                     problemSolution->addFrontDescription (description);
1249
1250                                 } else {
1251                                     description = str::form (_("do not ask to delete all solvables providing %s"), pool_dep2str(pool, what));
1252                                     MIL << description << endl;
1253                                     problemSolution->addDescription (description);
1254                                 }
1255                                 }
1256                                 break;
1257                             case SOLVER_UPDATE | SOLVER_SOLVABLE:
1258                                 {
1259                                 s = mapSolvable (what);
1260                                 PoolItem poolItem = _pool.find (s);
1261                                 if (poolItem) {
1262                                     if (pool->installed && s.get()->repo == pool->installed) {
1263                                         problemSolution->addSingleAction (poolItem, KEEP);
1264                                         string description = str::form (_("do not install most recent version of %s"), s.asString().c_str());
1265                                         MIL << description << endl;
1266                                         problemSolution->addDescription (description);
1267                                     } else {
1268                                         ERR << "SOLVER_INSTALL_SOLVABLE_UPDATE " << poolItem << " is not selected for installation" << endl;
1269                                     }
1270                                 } else {
1271                                     ERR << "SOLVER_INSTALL_SOLVABLE_UPDATE: No item found for " << s.asString() << endl;
1272                                 }
1273                                 }
1274                                 break;
1275                             default:
1276                                 MIL << "- do something different" << endl;
1277                                 ERR << "No valid solution available" << endl;
1278                                 break;
1279                         }
1280                     } else if (p == SOLVER_SOLUTION_INFARCH) {
1281                         s = mapSolvable (rp);
1282                         PoolItem poolItem = _pool.find (s);
1283                         if (pool->installed && s.get()->repo == pool->installed) {
1284                             problemSolution->addSingleAction (poolItem, LOCK);
1285                             string description = str::form (_("keep %s despite the inferior architecture"), s.asString().c_str());
1286                             MIL << description << endl;
1287                             problemSolution->addDescription (description);
1288                         } else {
1289                             problemSolution->addSingleAction (poolItem, INSTALL);
1290                             string description = str::form (_("install %s despite the inferior architecture"), s.asString().c_str());
1291                             MIL << description << endl;
1292                             problemSolution->addDescription (description);
1293                         }
1294                     } else if (p == SOLVER_SOLUTION_DISTUPGRADE) {
1295                         s = mapSolvable (rp);
1296                         PoolItem poolItem = _pool.find (s);
1297                         if (pool->installed && s.get()->repo == pool->installed) {
1298                             problemSolution->addSingleAction (poolItem, LOCK);
1299                             string description = str::form (_("keep obsolete %s"), s.asString().c_str());
1300                             MIL << description << endl;
1301                             problemSolution->addDescription (description);
1302                         } else {
1303                             problemSolution->addSingleAction (poolItem, INSTALL);
1304                             string description = str::form (_("install %s from excluded repository"), s.asString().c_str());
1305                             MIL << description << endl;
1306                             problemSolution->addDescription (description);
1307                         }
1308                     } else {
1309                         /* policy, replace p with rp */
1310                         s = mapSolvable (p);
1311                         PoolItem itemFrom = _pool.find (s);
1312                         if (rp)
1313                         {
1314                             int gotone = 0;
1315
1316                             sd = mapSolvable (rp);
1317                             PoolItem itemTo = _pool.find (sd);
1318                             if (itemFrom && itemTo) {
1319                                 problemSolution->addSingleAction (itemTo, INSTALL);
1320                                 int illegal = policy_is_illegal(_solv, s.get(), sd.get(), 0);
1321
1322                                 if ((illegal & POLICY_ILLEGAL_DOWNGRADE) != 0)
1323                                 {
1324                                     string description = str::form (_("downgrade of %s to %s"), s.asString().c_str(), sd.asString().c_str());
1325                                     MIL << description << endl;
1326                                     problemSolution->addDescription (description);
1327                                     gotone = 1;
1328                                 }
1329                                 if ((illegal & POLICY_ILLEGAL_ARCHCHANGE) != 0)
1330                                 {
1331                                     string description = str::form (_("architecture change of %s to %s"), s.asString().c_str(), sd.asString().c_str());
1332                                     MIL << description << endl;
1333                                     problemSolution->addDescription (description);
1334                                     gotone = 1;
1335                                 }
1336                                 if ((illegal & POLICY_ILLEGAL_VENDORCHANGE) != 0)
1337                                 {
1338                                     IdString s_vendor( s.vendor() );
1339                                     IdString sd_vendor( sd.vendor() );
1340                                     string description = str::form (_("install %s (with vendor change)\n  %s  -->  %s") ,
1341                                                                     sd.asString().c_str(),
1342                                                                     ( s_vendor ? s_vendor.c_str() : " (no vendor) " ),
1343                                                                     ( sd_vendor ? sd_vendor.c_str() : " (no vendor) " ) );
1344                                     MIL << description << endl;
1345                                     problemSolution->addDescription (description);
1346                                     gotone = 1;
1347                                 }
1348                                 if (!gotone) {
1349                                     string description = str::form (_("replacement of %s with %s"), s.asString().c_str(), sd.asString().c_str());
1350                                     MIL << description << endl;
1351                                     problemSolution->addDescription (description);
1352                                 }
1353                             } else {
1354                                 ERR << s.asString() << " or "  << sd.asString() << " not found" << endl;
1355                             }
1356                         }
1357                         else
1358                         {
1359                             if (itemFrom) {
1360                                 string description = str::form (_("deinstallation of %s"), s.asString().c_str());
1361                                 MIL << description << endl;
1362                                 problemSolution->addDescription (description);
1363                                 problemSolution->addSingleAction (itemFrom, REMOVE);
1364                             }
1365                         }
1366                     }
1367                 }
1368                 resolverProblem->addSolution (problemSolution,
1369                                               problemSolution->actionCount() > 1 ? true : false); // Solutions with more than 1 action will be shown first.
1370                 MIL << "------------------------------------" << endl;
1371             }
1372
1373             if (ignoreId > 0) {
1374                 // There is a possibility to ignore this error by setting weak dependencies
1375                 PoolItem item = _pool.find (sat::Solvable(ignoreId));
1376                 ProblemSolutionIgnore *problemSolution = new ProblemSolutionIgnore(resolverProblem, item);
1377                 resolverProblem->addSolution (problemSolution,
1378                                               false); // Solutions will be shown at the end
1379                 MIL << "ignore some dependencies of " << item << endl;
1380                 MIL << "------------------------------------" << endl;
1381             }
1382
1383             // save problem
1384             resolverProblems.push_back (resolverProblem);
1385         }
1386     }
1387     return resolverProblems;
1388 }
1389
1390 void
1391 SATResolver::applySolutions (const ProblemSolutionList & solutions)
1392 {
1393     for (ProblemSolutionList::const_iterator iter = solutions.begin();
1394          iter != solutions.end(); ++iter) {
1395         ProblemSolution_Ptr solution = *iter;
1396         Resolver dummyResolver(_pool);
1397         if (!solution->apply (dummyResolver))
1398             break;
1399     }
1400 }
1401
1402 void SATResolver::setLocks()
1403 {
1404     for (PoolItemList::const_iterator iter = _items_to_lock.begin(); iter != _items_to_lock.end(); iter++) {
1405         sat::detail::SolvableIdType ident( (*iter)->satSolvable().id() );
1406         if (iter->status().isInstalled()) {
1407             MIL << "Lock installed item " << *iter << endl;
1408             queue_push( &(_jobQueue), SOLVER_INSTALL | SOLVER_SOLVABLE );
1409             queue_push( &(_jobQueue), ident );
1410         } else {
1411             MIL << "Lock NOT installed item " << *iter << endl;
1412             queue_push( &(_jobQueue), SOLVER_ERASE | SOLVER_SOLVABLE | MAYBE_CLEANDEPS );
1413             queue_push( &(_jobQueue), ident );
1414         }
1415     }
1416
1417     for (PoolItemList::const_iterator iter = _items_to_keep.begin(); iter != _items_to_keep.end(); iter++) {
1418         sat::detail::SolvableIdType ident( (*iter)->satSolvable().id() );
1419         if (iter->status().isInstalled()) {
1420             MIL << "Keep installed item " << *iter << endl;
1421             queue_push( &(_jobQueue), SOLVER_INSTALL | SOLVER_SOLVABLE | SOLVER_WEAK);
1422             queue_push( &(_jobQueue), ident );
1423         } else {
1424             MIL << "Keep NOT installed item " << *iter << ident << endl;
1425             queue_push( &(_jobQueue), SOLVER_ERASE | SOLVER_SOLVABLE | SOLVER_WEAK | MAYBE_CLEANDEPS );
1426             queue_push( &(_jobQueue), ident );
1427         }
1428     }
1429 }
1430
1431 void SATResolver::setSystemRequirements()
1432 {
1433     CapabilitySet system_requires = SystemCheck::instance().requiredSystemCap();
1434     CapabilitySet system_conflicts = SystemCheck::instance().conflictSystemCap();
1435
1436     for (CapabilitySet::const_iterator iter = system_requires.begin(); iter != system_requires.end(); iter++) {
1437         queue_push( &(_jobQueue), SOLVER_INSTALL | SOLVER_SOLVABLE_PROVIDES );
1438         queue_push( &(_jobQueue), iter->id() );
1439         MIL << "SYSTEM Requires " << *iter << endl;
1440     }
1441
1442     for (CapabilitySet::const_iterator iter = system_conflicts.begin(); iter != system_conflicts.end(); iter++) {
1443         queue_push( &(_jobQueue), SOLVER_ERASE | SOLVER_SOLVABLE_PROVIDES | MAYBE_CLEANDEPS );
1444         queue_push( &(_jobQueue), iter->id() );
1445         MIL << "SYSTEM Conflicts " << *iter << endl;
1446     }
1447
1448     // Lock the architecture of the running systems rpm
1449     // package on distupgrade.
1450     if ( _distupgrade && ZConfig::instance().systemRoot() == "/" )
1451     {
1452       ResPool pool( ResPool::instance() );
1453       IdString rpm( "rpm" );
1454       for_( it, pool.byIdentBegin(rpm), pool.byIdentEnd(rpm) )
1455       {
1456         if ( (*it)->isSystem() )
1457         {
1458           Capability archrule( (*it)->arch(), rpm.c_str(), Capability::PARSED );
1459           queue_push( &(_jobQueue), SOLVER_INSTALL | SOLVABLE_NAME | SOLVER_ESSENTIAL );
1460           queue_push( &(_jobQueue), archrule.id() );
1461
1462         }
1463       }
1464     }
1465 }
1466
1467
1468 ///////////////////////////////////////////////////////////////////
1469 };// namespace detail
1470     /////////////////////////////////////////////////////////////////////
1471     /////////////////////////////////////////////////////////////////////
1472   };// namespace solver
1473   ///////////////////////////////////////////////////////////////////////
1474   ///////////////////////////////////////////////////////////////////////
1475 };// namespace zypp
1476 /////////////////////////////////////////////////////////////////////////
1477