f38d7e0ecc565934c7980aaa1ac7625572343300
[platform/upstream/libzypp.git] / zypp / sat / 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 #include <sstream>
22 #include "zypp/solver/detail/Helper.h"
23 #include "zypp/base/String.h"
24 #include "zypp/Capability.h"
25 #include "zypp/ResStatus.h"
26 #include "zypp/VendorAttr.h"
27 #include "zypp/base/Logger.h"
28 #include "zypp/base/String.h"
29 #include "zypp/base/Gettext.h"
30 #include "zypp/base/Algorithm.h"
31 #include "zypp/ResPool.h"
32 #include "zypp/ResFilters.h"
33 #include "zypp/ZConfig.h"
34 #include "zypp/sat/SATResolver.h"
35 #include "zypp/sat/Pool.h"
36 #include "zypp/sat/WhatProvides.h"
37 #include "zypp/solver/detail/ProblemSolutionCombi.h"
38
39 extern "C" {
40 #include "satsolver/repo_solv.h"
41 #include "satsolver/poolarch.h"
42 #include "satsolver/evr.h"
43 #include "satsolver/poolvendor.h"
44 #include "satsolver/policy.h"
45 }
46
47 /////////////////////////////////////////////////////////////////////////
48 namespace zypp
49 { ///////////////////////////////////////////////////////////////////////
50   ///////////////////////////////////////////////////////////////////////
51   namespace solver
52   { /////////////////////////////////////////////////////////////////////
53     /////////////////////////////////////////////////////////////////////
54     namespace detail
55     { ///////////////////////////////////////////////////////////////////
56
57 using namespace std;
58
59 IMPL_PTR_TYPE(SATResolver);
60
61 static PoolItemSet triggeredSolution;   // only the latest state of an item is interesting
62                                         // for the pool. Documents already inserted items.
63
64 //---------------------------------------------------------------------------
65 // Callbacks for SAT policies
66 //---------------------------------------------------------------------------
67
68 int vendorCheck (Pool *pool, Solvable *solvable1, Solvable *solvable2) {
69 //    DBG << "vendorCheck: " << id2str(pool, solvable1->vendor) << " <--> " << id2str(pool, solvable1->vendor) << endl;
70     return VendorAttr::instance().equivalent(id2str(pool, solvable1->vendor), id2str(pool, solvable2->vendor)) ? 0:1;
71 }
72
73
74 string
75 itemToString (PoolItem item, bool shortVersion)
76 {
77     ostringstream os;
78     if (!item) return "";
79
80     if (item->kind() != ResTraits<zypp::Package>::kind)
81         os << item->kind() << ':';
82     os  << item->name();
83     if (!shortVersion) {
84         os << '-' << item->edition();
85         if (item->arch() != "") {
86             os << '.' << item->arch();
87         }
88         Repository s = item->repository();
89         if (s) {
90             string alias = s.info().alias();
91             if (!alias.empty()
92                 && alias != "@system")
93             {
94                 os << '[' << s.info().alias() << ']';
95             }
96         }
97     }
98     return os.str();
99 }
100         
101
102 //---------------------------------------------------------------------------
103
104 std::ostream &
105 SATResolver::dumpOn( std::ostream & os ) const
106 {
107     os << "<resolver>";     
108     os << "  fixsystem = " << _fixsystem << endl;
109     os << "  allowdowngrade = " << _allowdowngrade << endl;
110     os << "  allowarchchange = " << _allowarchchange << endl;
111     os << "  allowvendorchange = " <<  _allowvendorchange << endl;
112     os << "  allowuninstall = " << _allowuninstall << endl;
113     os << "  updatesystem = " << _updatesystem << endl;
114     os << "  allowvirtualconflicts = " <<  _allowvirtualconflicts << endl;
115     os << "  noupdateprovide = " << _noupdateprovide << endl;
116     os << "  dosplitprovides = " << _dosplitprovides << endl;
117     os << "  onlyRequires = " << _onlyRequires << endl;
118     return os << "<resolver/>" << endl;
119 }
120
121 //---------------------------------------------------------------------------
122
123 SATResolver::SATResolver (const ResPool & pool, Pool *SATPool)
124     : _pool (pool)
125     , _SATPool (SATPool)
126     , _solv(NULL)
127     , _fixsystem(false)
128     , _allowdowngrade(false)
129     , _allowarchchange(false)
130     , _allowvendorchange(false)
131     , _allowuninstall(false)
132     , _updatesystem(false)
133     , _allowvirtualconflicts(false)
134     , _noupdateprovide(false)
135     , _dosplitprovides(false)
136     , _onlyRequires(ZConfig::instance().solver_onlyRequires())
137
138 {
139 }
140
141
142 SATResolver::~SATResolver()
143 {
144 }
145
146 //---------------------------------------------------------------------------
147
148
149 ResPool
150 SATResolver::pool (void) const
151 {
152     return _pool;
153 }
154
155
156 void
157 SATResolver::addPoolItemToInstall (PoolItem item)
158 {
159     bool found = false;
160     for (PoolItemList::const_iterator iter = _items_to_remove.begin();
161          iter != _items_to_remove.end(); iter++) {
162         if (*iter == item) {
163             _items_to_remove.remove(*iter);
164             found = true;
165             break;
166         }
167     }
168     if (!found) {
169         _items_to_install.push_back (item);
170         _items_to_install.unique ();
171     }
172 }
173
174
175 void
176 SATResolver::addPoolItemsToInstallFromList (PoolItemList & rl)
177 {
178     for (PoolItemList::const_iterator iter = rl.begin(); iter != rl.end(); iter++) {
179         addPoolItemToInstall (*iter);
180     }
181 }
182
183
184 void
185 SATResolver::addPoolItemToRemove (PoolItem item)
186 {
187     bool found = false;
188     for (PoolItemList::const_iterator iter = _items_to_install.begin();
189          iter != _items_to_install.end(); iter++) {
190         if (*iter == item) {
191             _items_to_install.remove(*iter);
192             found = true;
193             break;
194         }
195     }
196     if (!found) {
197         _items_to_remove.push_back (item);
198         _items_to_remove.unique ();
199     }
200 }
201
202
203 void
204 SATResolver::addPoolItemsToRemoveFromList (PoolItemList & rl)
205 {
206     for (PoolItemList::const_iterator iter = rl.begin(); iter != rl.end(); iter++) {
207         addPoolItemToRemove (*iter);
208     }
209 }
210
211 void
212 SATResolver::addPoolItemToLock (PoolItem item)
213 {
214     _items_to_lock.push_back (item);
215     _items_to_lock.unique ();
216 }
217
218
219 //---------------------------------------------------------------------------
220
221 // copy marked item from solution back to pool
222 // if data != NULL, set as APPL_LOW (from establishPool())
223
224 static void
225 SATSolutionToPool (PoolItem item, const ResStatus & status, const ResStatus::TransactByValue causer)
226 {
227 #if 0
228     if (triggeredSolution.find(item) != triggeredSolution.end()) {
229         _XDEBUG("SATSolutionToPool(" << item << ") is already in the pool --> skip");
230         return;
231     }
232 #endif
233
234     triggeredSolution.insert(item);
235
236     // resetting transaction only
237     item.status().resetTransact (causer);
238
239     bool r;
240
241     if (status.isToBeInstalled()) {
242         r = item.status().setToBeInstalled (causer);
243         _XDEBUG("SATSolutionToPool(" << item << ", " << status << ") install !" << r);
244     }
245     else if (status.isToBeUninstalledDueToUpgrade()) {
246         r = item.status().setToBeUninstalledDueToUpgrade (causer);
247         _XDEBUG("SATSolutionToPool(" << item << ", " << status << ") upgrade !" << r);
248     }
249     else if (status.isToBeUninstalled()) {
250         r = item.status().setToBeUninstalled (causer);
251         _XDEBUG("SATSolutionToPool(" << item << ", " << status << ") remove !" << r);
252     }
253     else if (status.isRecommended()) {
254         item.status().setRecommended(true);
255         _XDEBUG("SATSolutionToPool(" << item << ", " << status << ") recommended !" << r);
256     }
257     else if (status.isSuggested()) {
258         item.status().setSuggested(true);
259         _XDEBUG("SATSolutionToPool(" << item << ", " << status << ") suggested !" << r);        
260     } else {
261         _XDEBUG("SATSolutionToPool(" << item << ", " << status << ") unchanged !");
262     }
263     return;
264 }
265
266
267 //----------------------------------------------------------------------------
268 // helper functions for distupgrade 
269 //----------------------------------------------------------------------------
270
271 bool SATResolver::doesObsoleteItem (PoolItem candidate, PoolItem installed) {
272   Solvable *sCandidate = _SATPool->solvables + candidate.satSolvable().id();
273   ::_Repo *installedRepo = sat::Pool::instance().systemRepo().get();
274   
275   Id p, *pp, obsolete, *obsoleteIt;
276   
277   if ((!installedRepo || sCandidate->repo != installedRepo) && sCandidate->obsoletes) {
278       obsoleteIt = sCandidate->repo->idarraydata + sCandidate->obsoletes;
279       while ((obsolete = *obsoleteIt++) != 0)
280       {
281           for (pp = pool_whatprovides(_SATPool, obsolete) ; (p = *pp++) != 0; ) {
282               if (p > 0 &&  installed.satSolvable().id() == (sat::detail::SolvableIdType)p) {
283                   MIL << candidate << " obsoletes " << installed << endl;
284                   return true;
285               }
286           }
287       }
288   }
289   return false;
290 }
291
292 //----------------------------------------------------------------------------
293 //----------------------------------------------------------------------------
294 // resolvePool
295 //----------------------------------------------------------------------------
296 //----------------------------------------------------------------------------
297
298 //----------------------------------------------------------------------------
299 // Helper functions for the ZYPP-Pool
300 //----------------------------------------------------------------------------
301
302
303 //------------------------------------------------------------------------------------------------------------
304 //  This function loops over the pool and grabs all items
305 //  It clears all previous bySolver() states also
306 //
307 //  Every toBeInstalled is passed to zypp::solver:detail::Resolver.addPoolItemToInstall()
308 //  Every toBeUninstalled is passed to zypp::solver:detail::Resolver.addPoolItemToRemove()
309 //
310 //  Solver results must be written back to the pool.
311 //------------------------------------------------------------------------------------------------------------
312
313
314 struct SATCollectTransact : public resfilter::PoolItemFilterFunctor
315 {
316     SATResolver & resolver;
317
318     SATCollectTransact (SATResolver & r)
319         : resolver (r)
320     { }
321
322     bool operator()( PoolItem item )            // only transacts() items go here
323     {
324         ResStatus status = item.status();
325         bool by_solver = (status.isBySolver() || status.isByApplLow());
326
327         if (by_solver) {
328             _XDEBUG("Resetting " << item );
329             item.status().resetTransact( ResStatus::APPL_LOW );// clear any solver/establish transactions
330             return true;                                // back out here, dont re-queue former solver result
331         }
332
333         if (status.isToBeInstalled()) {
334             resolver.addPoolItemToInstall(item);        // -> install!
335         }
336         else if (status.isToBeUninstalled()) {
337             resolver.addPoolItemToRemove(item);         // -> remove !
338         }
339         else if (status.isLocked()
340                  || (status.isKept()
341                      && !by_solver)) {
342             resolver.addPoolItemToLock (item);
343         }
344
345         return true;
346     }
347 };
348
349
350 //----------------------------------------------------------------------------
351 //----------------------------------------------------------------------------
352 // solving.....
353 //----------------------------------------------------------------------------
354 //----------------------------------------------------------------------------
355
356
357 class CheckIfUpdate : public resfilter::PoolItemFilterFunctor
358 {
359   public:
360     bool is_updated;
361
362     CheckIfUpdate()
363         : is_updated( false )
364     {}
365
366     // check this item will be installed
367
368     bool operator()( PoolItem item )
369     {
370         if (item.status().isToBeInstalled())
371         {
372             is_updated = true;
373             return false;
374         }
375         return true;
376     }
377 };
378
379
380 bool
381 SATResolver::resolvePool(const CapabilitySet & requires_caps,
382                          const CapabilitySet & conflict_caps)
383 {
384     SATCollectTransact info (*this);
385     
386     MIL << "SATResolver::resolvePool()" << endl;
387
388     if (_solv) {
389         // remove old stuff
390         solver_free(_solv);
391         _solv = NULL;
392         queue_free( &(_jobQueue) );
393     }
394
395     queue_init( &_jobQueue );
396     _items_to_install.clear();
397     _items_to_remove.clear();
398     _items_to_lock.clear();
399
400     invokeOnEach ( _pool.begin(), _pool.end(),
401                    functor::functorRef<bool,PoolItem>(info) );
402
403     for (PoolItemList::const_iterator iter = _items_to_install.begin(); iter != _items_to_install.end(); iter++) {
404         PoolItem r = *iter;
405
406         Id id = (*iter)->satSolvable().id();
407         if (id == ID_NULL) {
408             ERR << "Install: " << *iter << " not found" << endl;
409         }
410         MIL << "Install " << *iter << " with the SAT-Pool ID: " << id << endl;
411         queue_push( &(_jobQueue), SOLVER_INSTALL_SOLVABLE );
412         queue_push( &(_jobQueue), id );
413     }
414
415     for (PoolItemList::const_iterator iter = _items_to_remove.begin(); iter != _items_to_remove.end(); iter++) {
416         sat::detail::IdType ident( (*iter)->satSolvable().ident().id() );
417         MIL << "Delete " << *iter << " with the string ID: " << ident << endl;
418         queue_push( &(_jobQueue), SOLVER_ERASE_SOLVABLE_NAME );
419         queue_push( &(_jobQueue), ident);
420     }
421
422     for (CapabilitySet::const_iterator iter = requires_caps.begin(); iter != requires_caps.end(); iter++) {
423         queue_push( &(_jobQueue), SOLVER_INSTALL_SOLVABLE_PROVIDES );
424         queue_push( &(_jobQueue), iter->id() );
425         MIL << "Requires " << *iter << endl;
426     }
427
428     for (CapabilitySet::const_iterator iter = conflict_caps.begin(); iter != conflict_caps.end(); iter++) {
429         queue_push( &(_jobQueue), SOLVER_ERASE_SOLVABLE_PROVIDES);
430         queue_push( &(_jobQueue), iter->id() );
431         MIL << "Conflicts " << *iter << endl;
432     }
433
434     for (PoolItemList::const_iterator iter = _items_to_lock.begin(); iter != _items_to_lock.end(); iter++) {
435         sat::detail::SolvableIdType ident( (*iter)->satSolvable().id() );
436         if (iter->status().isInstalled()) {
437             MIL << "Lock installed item " << *iter << " with the string ID: " << ident << endl;
438             queue_push( &(_jobQueue), SOLVER_INSTALL_SOLVABLE );
439             queue_push( &(_jobQueue), ident );
440         } else {
441             MIL << "Lock NOT installed item " << *iter << " with the string ID: " << ident << endl;
442             queue_push( &(_jobQueue), SOLVER_ERASE_SOLVABLE );
443             queue_push( &(_jobQueue), ident );
444         }
445     }
446
447     _solv = solver_create( _SATPool, sat::Pool::instance().systemRepo().get() );
448     _solv->vendorCheckCb = &vendorCheck;
449     _solv->fixsystem = _fixsystem;
450     _solv->updatesystem = _updatesystem;
451     _solv->allowdowngrade = _allowdowngrade;
452     _solv->allowuninstall = _allowuninstall;
453     _solv->allowarchchange = _allowarchchange;
454     _solv->dosplitprovides = _dosplitprovides;
455     _solv->noupdateprovide = _noupdateprovide;
456     _solv->dontinstallrecommended = _onlyRequires;
457     
458     sat::Pool::instance().prepare();
459
460     // Solve !
461     MIL << "Starting solving...." << endl;
462     MIL << *this;
463     solver_solve( _solv, &(_jobQueue) );
464     MIL << "....Solver end" << endl;
465
466     // copying solution back to zypp pool
467     //-----------------------------------------
468
469     if (_solv->problems.count > 0 )
470     {
471         ERR << "Solverrun finished with an ERROR" << endl;
472         return false;
473     }
474
475     /*  solvables to be installed */
476     for (int i = 0; i < _solv->decisionq.count; i++)
477     {
478       Id p;
479       p = _solv->decisionq.elements[i];
480       if (p < 0 || !sat::Solvable(p))
481         continue;
482       if (sat::Solvable(p).repository().get() == _solv->installed)
483         continue;
484
485       PoolItem poolItem = _pool.find (sat::Solvable(p));
486       if (poolItem) {
487           SATSolutionToPool (poolItem, ResStatus::toBeInstalled, ResStatus::SOLVER);
488       } else {
489           ERR << "id " << p << " not found in ZYPP pool." << endl;
490       }
491     }
492
493     /* solvables to be erased */
494     for (int i = _solv->installed->start; i < _solv->installed->start + _solv->installed->nsolvables; i++)
495     {
496       if (_solv->decisionmap[i] > 0)
497         continue;
498
499       PoolItem poolItem = _pool.find (sat::Solvable(i));
500       if (poolItem) {
501           // Check if this is an update
502           CheckIfUpdate info;
503           invokeOnEach( _pool.byIdentBegin( poolItem ),
504                         _pool.byIdentEnd( poolItem ),
505                         resfilter::ByUninstalled(),                     // ByUninstalled
506                         functor::functorRef<bool,PoolItem> (info) );
507
508           if (info.is_updated) {
509               SATSolutionToPool (poolItem, ResStatus::toBeUninstalledDueToUpgrade , ResStatus::SOLVER);
510           } else {
511               SATSolutionToPool (poolItem, ResStatus::toBeUninstalled, ResStatus::SOLVER);
512           }
513       } else {
514           ERR << "id " << i << " not found in ZYPP pool." << endl;
515       }
516     }
517
518     /*  solvables which are recommended */
519     for (int i = 0; i < _solv->recommendations.count; i++)
520     {
521       Id p;
522       p = _solv->recommendations.elements[i];
523       if (p < 0 || !sat::Solvable(p))
524         continue;
525
526       PoolItem poolItem = _pool.find (sat::Solvable(p));
527       if (poolItem) {
528           ResStatus status = poolItem.status();
529           status.setRecommended (true);
530           SATSolutionToPool (poolItem, status, ResStatus::SOLVER);
531       } else {
532           ERR << "id " << p << " not found in ZYPP pool." << endl;
533       }
534     }
535
536     /*  solvables which are suggested */
537     for (int i = 0; i < _solv->suggestions.count; i++)
538     {
539       Id p;
540       p = _solv->suggestions.elements[i];
541       if (p < 0 || !sat::Solvable(p))
542         continue;
543
544       PoolItem poolItem = _pool.find (sat::Solvable(p));
545       if (poolItem) {
546           ResStatus status = poolItem.status();
547           status.setSuggested (true);
548           SATSolutionToPool (poolItem, status, ResStatus::SOLVER);
549       } else {
550           ERR << "id " << p << " not found in ZYPP pool." << endl;
551       }
552     }
553
554     // cleanup
555     solver_free(_solv);
556     _solv = NULL;
557     queue_free( &(_jobQueue) );    
558
559     MIL << "SATResolver::resolvePool() done" << endl;
560     return true;
561 }
562
563
564 bool SATResolver::doUpdate()
565 {
566     MIL << "SATResolver::doUpdate()" << endl;
567
568     if (_solv) {
569         // remove old stuff
570         solver_free(_solv);
571         _solv = NULL;
572         queue_free( &(_jobQueue) );
573     }
574
575     queue_init( &_jobQueue );
576
577     _solv = solver_create( _SATPool, sat::Pool::instance().systemRepo().get() );
578     _solv->vendorCheckCb = &vendorCheck;
579
580     _solv->updatesystem = true;
581     _solv->dontinstallrecommended = true; // #FIXME dontinstallrecommended maybe set to false if it works correctly
582     
583     sat::Pool::instance().prepare();
584
585     // Solve !
586     MIL << "Starting solving for update...." << endl;
587     MIL << *this;    
588     solver_solve( _solv, &(_jobQueue) );
589     MIL << "....Solver end" << endl;
590
591     // copying solution back to zypp pool
592     //-----------------------------------------
593
594     /*  solvables to be installed */
595     for (int i = 0; i < _solv->decisionq.count; i++)
596     {
597       Id p;
598       p = _solv->decisionq.elements[i];
599       if (p < 0 || !sat::Solvable(p))
600         continue;
601       if (sat::Solvable(p).repository().get() == _solv->installed)
602         continue;
603
604       PoolItem poolItem = _pool.find (sat::Solvable(p));
605       if (poolItem) {
606           SATSolutionToPool (poolItem, ResStatus::toBeInstalled, ResStatus::SOLVER);
607       } else {
608           ERR << "id " << p << " not found in ZYPP pool." << endl;
609       }
610     }
611
612     /* solvables to be erased */
613     for (int i = _solv->installed->start; i < _solv->installed->start + _solv->installed->nsolvables; i++)
614     {
615       if (_solv->decisionmap[i] > 0)
616         continue;
617
618       PoolItem poolItem = _pool.find (sat::Solvable(i));
619       if (poolItem) {
620           // Check if this is an update
621           CheckIfUpdate info;
622           invokeOnEach( _pool.byIdentBegin( poolItem ),
623                         _pool.byIdentEnd( poolItem ),
624                         resfilter::ByUninstalled(),                     // ByUninstalled
625                         functor::functorRef<bool,PoolItem> (info) );
626
627           if (info.is_updated) {
628               SATSolutionToPool (poolItem, ResStatus::toBeUninstalledDueToUpgrade , ResStatus::SOLVER);
629           } else {
630               SATSolutionToPool (poolItem, ResStatus::toBeUninstalled, ResStatus::SOLVER);
631           }
632       } else {
633           ERR << "id " << i << " not found in ZYPP pool." << endl;
634       }
635     }
636
637     // cleanup
638     solver_free(_solv);
639     _solv = NULL;
640     queue_free( &(_jobQueue) );    
641
642     MIL << "SATResolver::doUpdate() done" << endl;
643     return true;
644 }
645
646
647
648 //----------------------------------------------------------------------------
649 //----------------------------------------------------------------------------
650 // error handling
651 //----------------------------------------------------------------------------
652 //----------------------------------------------------------------------------
653
654 //----------------------------------------------------------------------------
655 // helper function
656 //----------------------------------------------------------------------------
657
658 struct FindPackage : public resfilter::ResObjectFilterFunctor
659 {
660     ProblemSolutionCombi *problemSolution;
661     TransactionKind action;
662     FindPackage (ProblemSolutionCombi *p, const TransactionKind act)
663        : problemSolution (p)
664          , action (act)
665     {
666     }
667
668     bool operator()( PoolItem p)
669     {
670         problemSolution->addSingleAction (p, action);
671         return true;
672     }
673 };
674
675
676 string SATResolver::SATprobleminfoString(Id problem, string &detail)
677 {
678   string ret;
679   Pool *pool = _solv->pool;
680   Id probr;
681   Id dep, source, target;
682   Solvable *s, *s2;
683
684   probr = solver_findproblemrule(_solv, problem);
685   switch (solver_problemruleinfo(_solv, &(_jobQueue), probr, &dep, &source, &target))
686   {
687       case SOLVER_PROBLEM_UPDATE_RULE:
688           s = pool_id2solvable(pool, source);
689           ret = str::form (_("problem with installed package %s"), solvable2str(pool, s));
690           break;
691       case SOLVER_PROBLEM_JOB_RULE:
692           ret = str::form (_("conflicting requests"));
693           break;
694       case SOLVER_PROBLEM_JOB_NOTHING_PROVIDES_DEP:
695           ret = str::form (_("nothing provides requested %s"), dep2str(pool, dep));
696           break;
697       case SOLVER_PROBLEM_NOT_INSTALLABLE:
698           s = pool_id2solvable(pool, source);
699           ret = str::form (_("%s is not installable"), solvable2str(pool, s));
700           break;
701       case SOLVER_PROBLEM_NOTHING_PROVIDES_DEP:
702           s = pool_id2solvable(pool, source);
703           ret = str::form (_("nothing provides %s needed by %s"), dep2str(pool, dep), solvable2str(pool, s));
704           break;
705       case SOLVER_PROBLEM_SAME_NAME:
706           s = pool_id2solvable(pool, source);
707           s2 = pool_id2solvable(pool, target);
708           ret = str::form (_("cannot install both %s and %s"), solvable2str(pool, s), solvable2str(pool, s2));
709           break;
710       case SOLVER_PROBLEM_PACKAGE_CONFLICT:
711           s = pool_id2solvable(pool, source);
712           s2 = pool_id2solvable(pool, target);
713           ret = str::form (_("%s conflicts with %s provided by %s"), solvable2str(pool, s), dep2str(pool, dep), solvable2str(pool, s2));
714           break;
715       case SOLVER_PROBLEM_PACKAGE_OBSOLETES:
716           s = pool_id2solvable(pool, source);
717           s2 = pool_id2solvable(pool, target);
718           ret = str::form (_("%s obsoletes %s provided by %s"), solvable2str(pool, s), dep2str(pool, dep), solvable2str(pool, s2));
719           break;
720       case SOLVER_PROBLEM_DEP_PROVIDERS_NOT_INSTALLABLE:
721           s = pool_id2solvable(pool, source);
722           Capability cap(dep);
723           sat::WhatProvides possibleProviders(cap);
724
725           // check, if a provider will be deleted
726           typedef list<PoolItem> ProviderList;
727           ProviderList providerlistInstalled, providerlistUninstalled;
728           for_( iter1, possibleProviders.begin(), possibleProviders.end() ) {
729               PoolItem provider1 = ResPool::instance().find( *iter1 );
730               // find pair of an installed/uninstalled item with the same NVR
731               bool found = false;
732               for_( iter2, possibleProviders.begin(), possibleProviders.end() ) {
733                   PoolItem provider2 = ResPool::instance().find( *iter2 );                
734                   if (compareByNVR (provider1.resolvable(),provider2.resolvable()) == 0
735                       && ( (provider1.status().isInstalled() && provider2.status().isUninstalled())
736                           || (provider2.status().isInstalled() && provider1.status().isUninstalled()) ))  {
737                       found = true;
738                       break;
739                   }
740               }
741               if (!found) {
742                   if (provider1.status().isInstalled())
743                       providerlistInstalled.push_back(provider1);
744                   else
745                       providerlistUninstalled.push_back(provider1);
746               }
747           }
748
749           ret = str::form (_("%s requires %s, but this requirement cannot be provided"), solvable2str(pool, s), dep2str(pool, dep));
750           if (providerlistInstalled.size() > 0) {
751               detail += _("deleted providers: ");
752               for (ProviderList::const_iterator iter = providerlistInstalled.begin(); iter != providerlistInstalled.end(); iter++) {
753                   if (iter == providerlistInstalled.begin())
754                       detail += itemToString (*iter, false);
755                   else
756                       detail += "\n                   " + itemToString (*iter, false);
757               }
758           }
759           if (providerlistUninstalled.size() > 0) {
760               if (detail.size() > 0)
761                   detail += _("\nuninstallable providers: ");
762               else
763                   detail = _("uninstallable providers: ");                
764               for (ProviderList::const_iterator iter = providerlistUninstalled.begin(); iter != providerlistUninstalled.end(); iter++) {
765                   if (iter == providerlistUninstalled.begin())
766                       detail += itemToString (*iter, false);                  
767                   else
768                       detail += "\n                   " + itemToString (*iter, false);                
769               }
770           }       
771           break;
772   }
773
774   return ret;
775 }
776
777 ResolverProblemList
778 SATResolver::problems ()
779 {
780     ResolverProblemList resolverProblems;
781     if (_solv && _solv->problems.count) {
782         Pool *pool = _solv->pool;
783         int pcnt;
784         Id p, rp, what;
785         Id problem, solution, element;
786         Solvable *s, *sd;
787
788         MIL << "Encountered problems! Here are the solutions:\n" << endl;
789         pcnt = 1;
790         problem = 0;
791         while ((problem = solver_next_problem(_solv, problem)) != 0) {
792             MIL << "Problem " <<  pcnt++ << ":" << endl;
793             MIL << "====================================" << endl;
794             string detail;
795             string whatString = SATprobleminfoString (problem,detail);
796             MIL << whatString << endl;
797             MIL << "------------------------------------" << endl;
798             ResolverProblem_Ptr resolverProblem = new ResolverProblem (whatString, detail);
799             solution = 0;
800             while ((solution = solver_next_solution(_solv, problem, solution)) != 0) {
801                 element = 0;
802                 ProblemSolutionCombi *problemSolution = new ProblemSolutionCombi(resolverProblem);
803                 while ((element = solver_next_solutionelement(_solv, problem, solution, element, &p, &rp)) != 0) {
804                     if (p == 0) {
805                         /* job, rp is index into job queue */
806                         what = _jobQueue.elements[rp];
807                         switch (_jobQueue.elements[rp-1])
808                         {
809                             case SOLVER_INSTALL_SOLVABLE: {
810                                 s = pool->solvables + what;
811                                 PoolItem poolItem = _pool.find (sat::Solvable(what));
812                                 if (poolItem) {
813                                     if (_solv->installed && s->repo == _solv->installed) {
814                                         problemSolution->addSingleAction (poolItem, REMOVE);
815                                         string description = str::form (_("do not keep %s installed"),  solvable2str(pool, s) );
816                                         MIL << description << endl;
817                                         problemSolution->addDescription (description);
818                                     } else {
819                                         problemSolution->addSingleAction (poolItem, REMOVE);
820                                         string description = str::form (_("do not install %s"), solvable2str(pool, s));
821                                         MIL << description << endl;
822                                         problemSolution->addDescription (description);
823                                     }
824                                 } else {
825                                     ERR << "SOLVER_INSTALL_SOLVABLE: No item found for " << id2str(pool, s->name) << "-"
826                                         <<  id2str(pool, s->evr) << "." <<  id2str(pool, s->arch) << endl;
827                                 }
828                             }
829                                 break;
830                             case SOLVER_ERASE_SOLVABLE: {
831                                 s = pool->solvables + what;
832                                 PoolItem poolItem = _pool.find (sat::Solvable(what));
833                                 if (poolItem) {
834                                     if (_solv->installed && s->repo == _solv->installed) {
835                                         problemSolution->addSingleAction (poolItem, KEEP);
836                                         string description = str::form (_("keep %s"), solvable2str(pool, s));
837                                         MIL << description << endl;
838                                         problemSolution->addDescription (description);
839                                     } else {
840                                         problemSolution->addSingleAction (poolItem, INSTALL);
841                                         string description = str::form (_("do not forbid installation of %s"), solvable2str(pool, s));
842                                         MIL << description << endl;
843                                         problemSolution->addDescription (description);
844                                     }
845                                 } else {
846                                     ERR << "SOLVER_ERASE_SOLVABLE: No item found for " << id2str(pool, s->name) << "-" <<  id2str(pool, s->evr) << "." <<
847                                         id2str(pool, s->arch) << endl;
848                                 }
849                             }
850                                 break;
851                             case SOLVER_INSTALL_SOLVABLE_NAME:
852                                 {
853                                 FindPackage info (problemSolution, KEEP);
854                                 IdString ident( what );
855                                 invokeOnEach( _pool.byIdentBegin( ident ),
856                                               _pool.byIdentEnd( ident ),
857                                               resfilter::ByUninstalled (),
858                                               functor::functorRef<bool,PoolItem> (info) );
859                                 string description = str::form (_("do not install %s"), ident.c_str() );
860                                 MIL << description << endl;
861                                 problemSolution->addDescription (description);
862                                 }
863                                 break;
864                             case SOLVER_ERASE_SOLVABLE_NAME:
865                                 {
866                                 FindPackage info (problemSolution, KEEP);
867                                 IdString ident( what );
868                                 invokeOnEach( _pool.byIdentBegin( ident ),
869                                               _pool.byIdentEnd( ident ),
870                                               functor::chain (resfilter::ByInstalled (),                        // ByInstalled
871                                                               resfilter::ByTransact ()),                        // will be deinstalled
872                                               functor::functorRef<bool,PoolItem> (info) );
873                                 string description = str::form (_("keep %s"), ident.c_str());
874                                 MIL << description << endl;
875                                 problemSolution->addDescription (description);
876                                 }
877                                 break;
878                             case SOLVER_INSTALL_SOLVABLE_PROVIDES:
879                                 {
880                                 Id p, *pp;
881                                 FOR_PROVIDES(p, pp, what);
882                                 {
883                                     PoolItem poolItem = _pool.find (sat::Solvable(p));
884                                     if (poolItem.status().isToBeInstalled()
885                                         || poolItem.status().staysUninstalled())
886                                         problemSolution->addSingleAction (poolItem, KEEP);
887                                 }
888                                 string description = str::form (_("do not ask to install a solvable providing %s"), dep2str(pool, what));
889                                 MIL << description << endl;
890                                 problemSolution->addDescription (description);
891                                 }
892                                 break;
893                             case SOLVER_ERASE_SOLVABLE_PROVIDES:
894                                 {
895                                 Id p, *pp;
896                                 FOR_PROVIDES(p, pp, what);
897                                 {
898                                     PoolItem poolItem = _pool.find (sat::Solvable(p));
899                                     if (poolItem.status().isToBeUninstalled()
900                                         || poolItem.status().staysInstalled())
901                                         problemSolution->addSingleAction (poolItem, KEEP);
902                                 }
903                                 string description = str::form (_("do not ask to delete all solvables providing %s"), dep2str(pool, what));
904                                 MIL << description << endl;
905                                 problemSolution->addDescription (description);
906                                 }
907                                 break;
908                             case SOLVER_INSTALL_SOLVABLE_UPDATE:
909                                 {
910                                 PoolItem poolItem = _pool.find (sat::Solvable(what));
911                                 s = pool->solvables + what;
912                                 if (poolItem) {
913                                     if (_solv->installed && s->repo == _solv->installed) {
914                                         problemSolution->addSingleAction (poolItem, KEEP);
915                                         string description = str::form (_("do not install most recent version of %s"), solvable2str(pool, s));
916                                         MIL << description << endl;
917                                         problemSolution->addDescription (description);
918                                     } else {
919                                         ERR << "SOLVER_INSTALL_SOLVABLE_UPDATE " << poolItem << " is not selected for installation" << endl;
920                                     }
921                                 } else {
922                                     ERR << "SOLVER_INSTALL_SOLVABLE_UPDATE: No item found for " << id2str(pool, s->name) << "-" <<  id2str(pool, s->evr) << "." <<
923                                         id2str(pool, s->arch) << endl;
924                                 }
925                                 }
926                                 break;
927                             default:
928                                 MIL << "- do something different" << endl;
929                                 ERR << "No valid solution available" << endl;
930                                 break;
931                         }
932                     } else {
933                         /* policy, replace p with rp */
934                         s = pool->solvables + p;
935                         sd = rp ? pool->solvables + rp : 0;
936
937                         PoolItem itemFrom = _pool.find (sat::Solvable(p));
938                         if (rp)
939                         {
940                             int gotone = 0;
941
942                             PoolItem itemTo = _pool.find (sat::Solvable(rp));
943                             if (itemFrom && itemTo) {
944                                 problemSolution->addSingleAction (itemTo, INSTALL);
945
946                                 if (evrcmp(pool, s->evr, sd->evr, EVRCMP_COMPARE ) > 0)
947                                 {
948                                     string description = str::form (_("downgrade of %s to %s"), solvable2str(pool, s), solvable2str(pool, sd));
949                                     MIL << description << endl;
950                                     problemSolution->addDescription (description);
951                                     gotone = 1;
952                                 }
953                                 if (!_solv->allowarchchange && s->name == sd->name && s->arch != sd->arch && policy_illegal_archchange(_solv, s, sd))
954                                 {
955                                     string description = str::form (_("architecture change of %s to %s"), solvable2str(pool, s), solvable2str(pool, sd));
956                                     MIL << description << endl;
957                                     problemSolution->addDescription (description);
958                                     gotone = 1;
959                                 }
960                                 if (!_solv->allowvendorchange && s->name == sd->name && s->vendor != sd->vendor && policy_illegal_vendorchange(_solv, s, sd))
961                                 {
962                                     string description = str::form (_("install %s (with vendor change)\n  %s\n-->\n  %s") ,
963                                                                     solvable2str(pool, sd) , id2str(pool, s->vendor),
964                                                                     string(sd->vendor ?  id2str(pool, sd->vendor) : " (no vendor) ").c_str() );
965                                     MIL << description << endl;
966                                     problemSolution->addDescription (description);
967                                     gotone = 1;
968                                 }
969                                 if (!gotone) {
970                                     string description = str::form (_("replacement of %s with %s"), solvable2str(pool, s), solvable2str(pool, sd));
971                                     MIL << description << endl;
972                                     problemSolution->addDescription (description);
973                                 }
974                             } else {
975                                 ERR << id2str(pool, s->name) << "-" <<  id2str(pool, s->evr) << "." <<  id2str(pool, s->arch)
976                                     << " or "  << id2str(pool, sd->name) << "-" <<  id2str(pool, sd->evr) << "." <<  id2str(pool, sd->arch) << " not found" << endl;
977                             }
978                         }
979                         else
980                         {
981                             if (itemFrom) {
982                                 string description = str::form (_("deinstallation of %s"), solvable2str(pool, s));
983                                 MIL << description << endl;
984                                 problemSolution->addDescription (description);
985                                 problemSolution->addSingleAction (itemFrom, REMOVE);
986                             }
987                         }
988                     }
989                 }
990                 resolverProblem->addSolution (problemSolution,
991                                               problemSolution->actionCount() > 1 ? true : false); // Solutions with more than 1 action will be shown first.
992                 MIL << "------------------------------------" << endl;
993             }
994             // save problem
995             resolverProblems.push_back (resolverProblem);
996         }
997     }
998     return resolverProblems;
999 }
1000
1001 void
1002 SATResolver::applySolutions (const ProblemSolutionList & solutions)
1003 {
1004     for (ProblemSolutionList::const_iterator iter = solutions.begin();
1005          iter != solutions.end(); ++iter) {
1006         ProblemSolution_Ptr solution = *iter;
1007         Resolver dummyResolver(_pool);
1008         if (!solution->apply (dummyResolver))
1009             break;
1010     }
1011 }
1012
1013
1014
1015 ///////////////////////////////////////////////////////////////////
1016 };// namespace detail
1017     /////////////////////////////////////////////////////////////////////
1018     /////////////////////////////////////////////////////////////////////
1019   };// namespace solver
1020   ///////////////////////////////////////////////////////////////////////
1021   ///////////////////////////////////////////////////////////////////////
1022 };// namespace zypp
1023 /////////////////////////////////////////////////////////////////////////
1024