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