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