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