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