ResolverContext.cc

Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 4 -*- */
00002 /* ResolverContext.cc
00003  *
00004  * Copyright (C) 2000-2002 Ximian, Inc.
00005  * Copyright (C) 2005 SUSE Linux Products GmbH
00006  *
00007  * This program is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU General Public License,
00009  * version 2, as published by the Free Software Foundation.
00010  *
00011  * This program is distributed in the hope that it will be useful, but
00012  * WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014  * General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU General Public License
00017  * along with this program; if not, write to the Free Software
00018  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00019  * 02111-1307, USA.
00020  */
00021 
00022 
00023 #include <values.h>
00024 
00025 #include "zypp/CapSet.h"
00026 #include "zypp/base/Logger.h"
00027 #include "zypp/base/String.h"
00028 #include "zypp/base/Gettext.h"
00029 #include "zypp/base/String.h"
00030 
00031 #include "zypp/base/Algorithm.h"
00032 #include "zypp/ResPool.h"
00033 #include "zypp/ResFilters.h"
00034 #include "zypp/CapFilters.h"
00035 #include "zypp/Package.h"
00036 #include "zypp/Resolvable.h"
00037 
00038 #include "zypp/solver/detail/Types.h"
00039 #include "zypp/solver/detail/Helper.h"
00040 #include "zypp/solver/detail/ResolverContext.h"
00041 #include "zypp/solver/detail/ResolverInfoMisc.h"
00042 #include "zypp/solver/detail/ResolverInfoConflictsWith.h"
00043 
00045 namespace zypp
00046 { 
00047 
00048   namespace solver
00049   { 
00050 
00051     namespace detail
00052     { 
00053 
00054 using namespace std;
00055 
00056 IMPL_PTR_TYPE(ResolverContext);
00057 
00058 //---------------------------------------------------------------------------
00059         
00060 class compare_items {
00061 public:
00062     int operator() (PoolItem_Ref p1,
00063                     PoolItem_Ref p2) const
00064         { return compareByN(p1.resolvable(),p2.resolvable()) < 0 ; }
00065 };
00066         
00067 
00068 //---------------------------------------------------------------------------
00069 
00070 ostream&
00071 operator<<( ostream& os, const ResolverContext & context)
00072 {
00073     if (context._parent != NULL) {
00074         os << "Parent @" << context._parent << endl;
00075         os << *(context._parent);
00076     }
00077     os << "ResolverContext with " << context._context.size() << " entries" << endl;
00078     for (ResolverContext::Context::const_iterator iter = context._context.begin(); iter != context._context.end(); ++iter) {
00079         os << iter->first << " : " << iter->second << endl;
00080     }
00081     return os;
00082 }
00083 
00084 //---------------------------------------------------------------------------
00085 
00086 ResolverContext::ResolverContext (const ResPool & pool, const Arch & arch, ResolverContext_Ptr parent)
00087     : _parent (parent)
00088     , _pool (pool)
00089     , _download_size (0)
00090     , _install_size (0)
00091     , _total_priority (0)
00092     , _min_priority (0)
00093     , _max_priority (0)
00094     , _other_penalties (0)
00095     , _verifying (false)
00096     , _establishing (false)
00097     , _invalid (false)
00098     , _askUser(false)
00099     , _architecture(arch)
00100     , _forceResolve(false)
00101     , _upgradeMode(false)
00102       
00103 {
00104 _XDEBUG( "ResolverContext[" << this << "]::ResolverContext(" << parent << ")" );
00105     if (parent != NULL) {
00106         _pool                = parent->_pool;
00107         _download_size       = parent->_download_size;
00108         _install_size        = parent->_install_size;
00109         _total_priority      = parent->_total_priority;
00110         _max_priority        = parent->_max_priority;
00111         _min_priority        = parent->_min_priority;
00112         _other_penalties     = parent->_other_penalties;
00113         _verifying           = parent->_verifying;
00114         _establishing        = parent->_establishing;
00115         _ignoreConflicts     = parent->_ignoreConflicts;
00116         _ignoreRequires      = parent->_ignoreRequires;
00117         _ignoreObsoletes     = parent->_ignoreObsoletes;
00118         _ignoreInstalledItem = parent->_ignoreInstalledItem;
00119         _ignoreArchitectureItem = parent->_ignoreArchitectureItem;      
00120         _forceResolve        = parent->_forceResolve;
00121         _upgradeMode         = parent->_upgradeMode;
00122     } else {
00123         _min_priority = MAXINT;
00124     }
00125 }
00126 
00127 
00128 ResolverContext::~ResolverContext()
00129 {
00130 }
00131 
00132 //---------------------------------------------------------------------------
00133 // status retrieve
00134 
00135 ResStatus
00136 ResolverContext::getStatus (PoolItem_Ref item)
00137 {
00138 //_XDEBUG( "[" << this << "]getStatus(" << item << ")" );
00139 
00140     if (item == _last_checked_item) return _last_checked_status;
00141 
00142     _last_checked_item = item;
00143 
00144     Context::const_iterator it;
00145     ResolverContext_constPtr context = this;
00146 
00147     while (context) {                                   // go through the _parent chain
00148 
00149         it = context->_context.find(item);              // part of local context ?
00150         if (it != context->_context.end()) {
00151 //_XDEBUG( "[" << context << "]:" << it->second );
00152             _last_checked_status = it->second;
00153             return it->second;                          // Y: return
00154         }
00155         context = context->_parent;                     // N: go up the chain
00156     }
00157 
00158     ResStatus status( item.status() );                  // make a copy of the status
00159     status.resetTransact( ResStatus::USER );            //   without transaction
00160 #if 0
00161     if (item.status().isInstalled())
00162         status = ResStatus::installed;                  // return _is_ state, not _to be_ state
00163     else
00164         status = ResStatus::uninstalled;                // return _is_ state, not _to be_ state
00165 #endif
00166     _last_checked_status = status;
00167 //_XDEBUG( "[NULL]:" << status );    
00168 
00169     return _last_checked_status;                                // Not part of context, return Pool status
00170 }
00171 
00172 
00173 //---------------------------------------------------------------------------
00174 // status change
00175 
00176 void
00177 ResolverContext::setStatus (PoolItem_Ref item, const ResStatus & status)
00178 {
00179     if (_invalid) return;
00180 
00181     _XDEBUG( "[" << this << "]setStatus(" << item << ", " << status << ")" );
00182     ResStatus old_status = getStatus (item);
00183 
00184     if (old_status != status) {         // new status ?
00185         _XDEBUG( "MARK" );
00186         _context[item] = status;                // set it !
00187     }
00188 
00189     _last_checked_item = item;
00190     _last_checked_status = status;
00191 
00192     return;
00193 }
00194 
00195 
00196 // change state to TO_BE_INSTALLED (does not upgrade)
00197 
00198 bool
00199 ResolverContext::install (PoolItem_Ref item, bool is_soft, int other_penalty)
00200 {
00201     ResStatus status, new_status;
00202     std::string msg;
00203 
00204     status = getStatus(item);
00205     _XDEBUG( "ResolverContext[" << this << "]::install(<" << status  << "> " << item << ")" );
00206 
00207     if (status.isToBeUninstalled()
00208         && !status.isToBeUninstalledDueToUnlink()) {
00209         ResolverInfo_Ptr misc_info = new ResolverInfoMisc (RESOLVER_INFO_TYPE_INSTALL_TO_BE_UNINSTALLED, item, RESOLVER_INFO_PRIORITY_VERBOSE);
00210         addError (misc_info);
00211         return false;
00212     }
00213 
00214     if (status.isImpossible()) {
00215         ResolverInfo_Ptr misc_info = new ResolverInfoMisc (RESOLVER_INFO_TYPE_UNINSTALLABLE, item, RESOLVER_INFO_PRIORITY_VERBOSE);
00216         // it is only an error, if the user wants to install explicity.
00217         if (is_soft) {
00218             addInfo (misc_info);
00219         } else {
00220             addError (misc_info);
00221         }
00222         return false;
00223     }
00224 
00225     if (status.isUnneeded()) {
00226         ResolverInfo_Ptr misc_info = new ResolverInfoMisc (RESOLVER_INFO_TYPE_INSTALL_UNNEEDED, item, RESOLVER_INFO_PRIORITY_VERBOSE);
00227         addInfo (misc_info);
00228         return false;
00229     }
00230 
00231     if (status.isToBeInstalled()) {
00232         return true;
00233     }
00234 
00235     if (isParallelInstall( item )) {
00236         ResolverInfoMisc_Ptr misc_info = new ResolverInfoMisc( RESOLVER_INFO_TYPE_INSTALL_PARALLEL, item, RESOLVER_INFO_PRIORITY_VERBOSE );
00237         misc_info->setOtherPoolItem( getParallelInstall( item ) );
00238         addError( misc_info );
00239         return false;
00240     }
00241 
00242     if (is_soft)
00243         setStatus (item, ResStatus::toBeInstalledSoft);
00244     else if (status.isToBeUninstalledDueToUnlink())
00245         setStatus (item, ResStatus(true));
00246     else
00247         setStatus (item, ResStatus::toBeInstalled);
00248 
00249     if (status.wasUninstalled()) {
00250         Resolvable::constPtr res = item.resolvable();
00251         Package::constPtr pkg = asKind<Package>(res);                   // try to access it as a package
00252         if (pkg) {                                                      // if its !=NULL, get size information
00253 
00254             _download_size += pkg->archivesize();
00255             _install_size += pkg->size();
00256 
00257         }
00258 
00259         int priority;
00260 #if 0
00261         if (item->local())
00262             priority = 0;
00263         else {
00264 #endif
00265             priority = getSourcePriority (item->source());
00266 //      }
00267 
00268         if (priority < _min_priority) _min_priority = priority;
00269         if (priority > _max_priority) _max_priority = priority;
00270 
00271         _other_penalties += other_penalty;
00272 
00273     }
00274 
00275     return true;
00276 }
00277 
00278 
00279 // change state to TO_BE_INSTALLED (does upgrade)
00280 
00281 bool
00282 ResolverContext::upgrade (PoolItem_Ref item, PoolItem_Ref old_item, bool is_soft, int other_penalty)
00283 {
00284     ResStatus status;
00285 
00286     _XDEBUG( "ResolverContext[" << this << "]::upgrade(" << item << " upgrades " << old_item << ")" );
00287 
00288     status = getStatus(item);
00289 
00290     if (status.isToBeUninstalled()
00291         || status.isImpossible())
00292         return false;
00293     
00294     if (status.isToBeInstalled())
00295         return true;
00296 
00297     if (isParallelInstall( item )) {
00298         ResolverInfoMisc_Ptr misc_info = new ResolverInfoMisc( RESOLVER_INFO_TYPE_INSTALL_PARALLEL, item, RESOLVER_INFO_PRIORITY_VERBOSE );
00299         misc_info->setOtherPoolItem( getParallelInstall( item ) );
00300         addError( misc_info );
00301         return false;
00302     }
00303 
00304     ResStatus::TransactByValue by = ResStatus::SOLVER;
00305     if (item.status().isToBeInstalled()
00306         && item.status().getTransactByValue() > ResStatus::SOLVER) {
00307         // if the item has already has been set for installation by 
00308         // user, or other applications with higher priority
00309         // We will use this priority
00310         by = item.status().getTransactByValue();
00311     }
00312         
00313     if (is_soft) {
00314         ResStatus newStatus = ResStatus::toBeInstalledSoft; // This can only be done by the solver
00315         setStatus (item, newStatus);
00316     }
00317     else {
00318         ResStatus newStatus;
00319         newStatus.setToBeInstalled (by);
00320         setStatus (item, newStatus);
00321     }
00322 
00323     Resolvable::constPtr res = old_item.resolvable();
00324     Package::constPtr pkg = asKind<Package>(res);                       // try to access it as a package
00325     if (pkg) {                                                  // if its !=NULL, get size information
00326 
00327         _install_size -= pkg->size();
00328     }
00329 
00330     if (status == ResStatus::uninstalled) {
00331         res = item.resolvable();
00332         pkg = asKind<Package>(res);                                     // try to access it as a package
00333         if (pkg) {                                                      // if its !=NULL, get size information
00334 
00335             _download_size += pkg->archivesize();
00336             _install_size += pkg->size();
00337 
00338         }
00339 
00340         int priority;
00341 #if 0
00342         if (item->local())
00343             priority = 0;
00344         else {
00345 #endif
00346             priority = getSourcePriority (item->source());
00347 //      }
00348 
00349         if (priority < _min_priority) _min_priority = priority;
00350         if (priority > _max_priority) _max_priority = priority;
00351 
00352         _other_penalties += other_penalty;
00353     }
00354     return true;
00355 }
00356 
00357 
00358 // change state to 'TO_BE_UNINSTALLED{, DUE_TO_OBSOLETE, DUE_TO_UNLINK}'
00359 
00360 bool
00361 ResolverContext::uninstall (PoolItem_Ref item, bool part_of_upgrade, bool due_to_obsolete, bool due_to_unlink)
00362 {
00363     ResStatus status, new_status;
00364     std::string msg;
00365 
00366     status = getStatus(item);
00367     
00368     _XDEBUG( "ResolverContext[" << this << "]::uninstall("
00369                     << item << " " << (part_of_upgrade ? "part_of_upgrade" : "") << " "
00370                     << (due_to_obsolete ? "due_to_obsolete": "") << " "
00371              << (due_to_unlink ? "due_to_unlink" : "") << ")" << "context-status:" << status);
00372 
00373     assert (! (due_to_obsolete && due_to_unlink));
00374 
00375     if ( ( (forceResolve() // This is the behaviour of ZMD
00376             || upgradeMode())
00377           && (status.isToBeInstalled()             // \ The resolvable will be installed
00378               || item.status().isToBeInstalled())) // / explicit.
00379          
00380          || ( (!forceResolve() // This is the bahaviour of YaST
00381                && !upgradeMode())
00382               && ((status.staysInstalled() || status.isToBeInstalled())                   //   \ We will have the resolvable
00383                   && (item.status().staysInstalled() || item.status().isToBeInstalled())  //   / available.
00384                   || status.isToBeInstalled())                                            //   is to be installed e.g. due solver requirement
00385               && !part_of_upgrade
00386               && !due_to_obsolete
00387               && !due_to_unlink)) {
00388         // We have a resolvable which should be kept on the system or is set to be installed explicit.
00389         // So we are not allowed deleting it. The reason WHY this resolvable has to be deleted here
00390         // is not show. We go back to the ResolverInfo to evaluate the reason. This reason (marked as an info)
00391         // will be stored at the end of ResolverInfo and will be marked as an error which is shown 
00392         // to the UI (solution included)
00393         // Canditates of searched ResolverInfo are RESOLVER_INFO_TYPE_CONFLICT_CANT_INSTALL
00394         //                                         RESOLVER_INFO_TYPE_NO_PROVIDER
00395         //                                         RESOLVER_INFO_TYPE_NO_OTHER_PROVIDER
00396         //                                         RESOLVER_INFO_TYPE_CANT_SATISFY
00397         //                                         RESOLVER_INFO_TYPE_CONFLICTS_WITH
00398         //
00399         // Testcases are:
00400         // conflict2-test.xml  conflict-test.xml              remove-still-needed2-test.xml  require-test.xml
00401         // conflict3-test.xml  remove-still-needed1-test.xml  remove-still-needed3-test.xml  unfulfilled-2-test.xml
00402         //
00403         bool found = false;
00404 
00405         ResolverInfoList addList;
00406         for (ResolverInfoList::const_iterator iter = _log.begin(); iter != _log.end(); iter++) {
00407             ResolverInfo_Ptr info = *iter;
00408 
00409             if (info->type() == RESOLVER_INFO_TYPE_CONFLICT_CANT_INSTALL
00410                 || info->type() == RESOLVER_INFO_TYPE_CONFLICTS_WITH) {
00411 
00412                 // There is a conflict like "a conflicts with b"
00413                 // Searching if there is already an error like "b conflicts with a"
00414                 PoolItem_Ref other_item = PoolItem_Ref();
00415                 if (info->type() == RESOLVER_INFO_TYPE_CONFLICT_CANT_INSTALL) {
00416                     ResolverInfoMisc_constPtr misc_info = dynamic_pointer_cast<const ResolverInfoMisc>(info);
00417                     other_item = misc_info->other();
00418                 } else {
00419                     ResolverInfoConflictsWith_constPtr conflicts_with = dynamic_pointer_cast<const ResolverInfoConflictsWith>(info);
00420                     if (conflicts_with->items().size() == 1) {
00421                         // It is only useful if there is ONE other item
00422                         other_item = *(conflicts_with->items().begin());
00423                     }
00424                 }
00425 
00426                 bool other_found = false;
00427 
00428                 if (other_item != PoolItem_Ref()) {
00429                     // searching for other solutions which have the same problem/solution
00430                     for (ResolverInfoList::const_iterator iter_other = addList.begin();
00431                          iter_other != addList.end(); iter_other++) {
00432                         
00433                         if ((*iter_other)->type() == RESOLVER_INFO_TYPE_CONFLICT_CANT_INSTALL) {
00434                                 ResolverInfoMisc_constPtr misc_info = dynamic_pointer_cast<const ResolverInfoMisc>(*iter_other);
00435                                 if (   (other_item == misc_info->other()
00436                                         && item == misc_info->affected())
00437                                     || (other_item == misc_info->affected()
00438                                         && item == misc_info->other())) 
00439                                     other_found = true;
00440                         }
00441                         else if ((*iter_other)->type() == RESOLVER_INFO_TYPE_CONFLICTS_WITH) {
00442                             ResolverInfoConflictsWith_constPtr conflicts_with = dynamic_pointer_cast<const ResolverInfoConflictsWith>(*iter_other);
00443                             if (conflicts_with->items().size() == 1) {
00444                                 // It is only useful if there is ONE other item
00445                                 if (   (other_item == *(conflicts_with->items().begin())
00446                                         && item == conflicts_with->affected())
00447                                     || (other_item == conflicts_with->affected()
00448                                         && item == *(conflicts_with->items().begin())))
00449                                     other_found = true;                                 
00450                             }
00451                         }
00452                     }
00453                 }
00454 
00455                 if ( !other_found
00456                      && (info->affected() == item
00457                          || other_item == item)) {
00458                     // put the info on the end as error
00459                     found = true;
00460                     addList.push_back (info);
00461                 }
00462             } else if ( (info->type() == RESOLVER_INFO_TYPE_NO_PROVIDER
00463                          || info->type() == RESOLVER_INFO_TYPE_NO_OTHER_PROVIDER
00464                          || info->type() == RESOLVER_INFO_TYPE_CANT_SATISFY)
00465                         && info->affected() == item)
00466             {
00467                 // put the info on the end as error
00468                 found = true;
00469                 // dont duplicate known errors (#167309)
00470                 if (!info->error())
00471                     addList.push_back (info);
00472             } else if (info->type() == RESOLVER_INFO_TYPE_CONFLICTS_WITH
00473                        && info->affected() == item) {
00474                 // put the info on the end as error
00475                 found = true;
00476                 // dont duplicate known errors (#167309)
00477                 if (!info->error())
00478                     addList.push_back (info);           
00479             }
00480         }
00481         if (!found) {
00482             // generating a default problem
00483             ResolverInfo_Ptr misc_info = new ResolverInfoMisc (RESOLVER_INFO_TYPE_REJECT_INSTALL, item, RESOLVER_INFO_PRIORITY_VERBOSE);
00484             addError (misc_info, true); // true = asking the user
00485         } else {
00486             // Put the info at the end of the list, flagged as error
00487             for (ResolverInfoList::const_iterator iter = addList.begin(); iter != addList.end(); iter++) {
00488                 ResolverInfo_Ptr info = *iter;
00489                 addError (info, true);  // true = asking the user
00490             }
00491         }
00492         
00493 //      return false;
00494     }
00495 
00496     if (status.isToBeUninstalled()
00497         && !status.isToBeUninstalledDueToUnlink())
00498     {
00499         return true;
00500     }
00501 
00502     if (status.wasUninstalled()
00503         || status.isImpossible()
00504         || status.isToBeUninstalledDueToUnlink())
00505     {
00506         ResolverInfo_Ptr misc_info = new ResolverInfoMisc (RESOLVER_INFO_TYPE_UNINSTALLABLE, item, RESOLVER_INFO_PRIORITY_VERBOSE);
00507         addInfo (misc_info);
00508     }
00509 
00510     if (due_to_obsolete) {
00511         setStatus (item, ResStatus::toBeUninstalledDueToObsolete);
00512     }
00513     else if (due_to_unlink) {
00514         setStatus (item, ResStatus::toBeUninstalledDueToUnlink);
00515     }
00516     else if (status.wasUninstalled()) {
00517         setStatus (item, ResStatus::impossible);
00518     }
00519     else if (part_of_upgrade) {
00520         setStatus (item, ResStatus::toBeUninstalledDueToUpgrade);
00521     }
00522     else {
00523         setStatus (item, ResStatus::toBeUninstalled);
00524     }
00525 
00526     if (status.wasInstalled()) {
00527         Resolvable::constPtr res = item.resolvable();
00528         Package::constPtr pkg = asKind<Package>(res);                   // try to access it as a package
00529         if (pkg) {                                                      // if its !=NULL, get size information
00530             _install_size -= pkg->size();
00531         }
00532     }
00533 
00534     return true;
00535 }
00536 
00537 
00538 // change state to UNNEEDED
00539 
00540 bool
00541 ResolverContext::unneeded (PoolItem_Ref item, int other_penalty)
00542 {
00543     ResStatus status;
00544 
00545     _XDEBUG( "ResolverContext[" << this << "]::unneeded(" << item << ")" );
00546 
00547     status = getStatus(item);
00548 
00549     if (status.wasInstalled()) {
00550         setStatus (item, ResStatus::satisfied);
00551     }
00552     else if (status.wasUninstalled()) {
00553         setStatus (item, ResStatus::unneeded);
00554     }
00555 
00556     return true;
00557 }
00558 
00559 
00560 // change state to SATISFIED
00561 
00562 bool
00563 ResolverContext::satisfy (PoolItem_Ref item, int other_penalty)
00564 {
00565     ResStatus status;
00566 
00567     status = getStatus(item);
00568 
00569     _XDEBUG( "ResolverContext[" << this << "]::satisfy(" << item << ":" << status << ")" );
00570 
00571     if (status.wasInstalled()) {
00572         setStatus (item, ResStatus::complete);
00573     }
00574     else if (status.wasUninstalled()) {
00575         setStatus (item, ResStatus::satisfied);
00576     }
00577 
00578     return true;
00579 }
00580 
00581 
00582 // change state to INCOMPLETE
00583 
00584 bool
00585 ResolverContext::incomplete (PoolItem_Ref item, int other_penalty)
00586 {
00587     ResStatus status = getStatus (item);
00588 
00589     _XDEBUG( "ResolverContext[" << this << "]::incomplete(" << item << "):" << status );
00590 
00591     if (_establishing) {
00592         if (status.wasInstalled()) {
00593             setStatus (item, ResStatus::incomplete);
00594         }
00595         else {
00596             setStatus (item, ResStatus::needed);
00597         }
00598 
00599         return true;
00600     }
00601 
00602     // if something goes 'incomplete' outside of the establishing call, its always an error
00603 
00604     ResolverInfo_Ptr misc_info = new ResolverInfoMisc (RESOLVER_INFO_TYPE_INCOMPLETES, item, RESOLVER_INFO_PRIORITY_VERBOSE);
00605     addError (misc_info);
00606     return false;
00607 }
00608 
00609 //---------------------------------------------------------------------------
00610 
00611 // is it installed (after transaction) ?
00612 // if yes, install/requires requests are considered done
00613 
00614 bool
00615 ResolverContext::isPresent (PoolItem_Ref item)
00616 {
00617     ResStatus status = getStatus(item);
00618 
00619     bool res = (status.staysInstalled()
00620                 || (status.isToBeInstalled() && !status.isNeeded())
00621                 || status.isUnneeded()
00622                 || status.isSatisfied());
00623 
00624 _XDEBUG("ResolverContext::itemIsPresent(<" << status << ">" << item << ") " << (res?"Y":"N"));
00625 
00626     return res;
00627 }
00628 
00629 
00630 // is it uninstalled (after transaction) ?
00631 // if yes, uninstall requests are considered done
00632 
00633 bool
00634 ResolverContext::isAbsent (PoolItem_Ref item)
00635 {
00636     ResStatus status = getStatus(item);
00637 
00638     // DONT add incomplete here, uninstall requests for incompletes must be handled
00639 
00640     bool res = (status.staysUninstalled()
00641                 || status.isToBeUninstalled()
00642                 || status.isImpossible());
00643 
00644 _XDEBUG("ResolverContext::itemIsAbsent(<" << status << ">" << item << ") " << (res?"Y":"N"));
00645 
00646     return res;
00647 }
00648 
00649 
00650 //---------------------------------------------------------------------------
00651 // marked
00652 
00653 void
00654 ResolverContext::foreachMarked (MarkedPoolItemFn fn, void *data) const
00655 {
00656     ResolverContext_constPtr context = this;
00657     while (context) {
00658         for (Context::const_iterator iter = context->_context.begin(); iter != context->_context.end(); ++iter) {
00659             fn (iter->first, iter->second, data);
00660         }
00661         context = context->_parent;
00662     }
00663 }
00664 
00665 
00666 //---------------------------------------------------------------------------
00667 // collect
00668 
00669 typedef struct {
00670     PoolItemList *rl;
00671     int status;                         // <0: uninstalls, ==0: all, >0: installs
00672 } MarkedResolvableInfo;
00673 
00674 
00675 static void
00676 marked_item_collector (PoolItem_Ref item, const ResStatus & status, void *data)
00677 {
00678     MarkedResolvableInfo *info = (MarkedResolvableInfo *)data;
00679     if (info->status == 0
00680        || (info->status > 0 && status.isToBeInstalled())
00681        || (info->status < 0 && status.isToBeUninstalled()))
00682     {
00683         info->rl->push_back (item);
00684     }
00685 }
00686 
00687 
00688 PoolItemList
00689 ResolverContext::getMarked (int which) // <0: uninstalls, ==0: all, >0: installs
00690 {
00691     if ( _last_getMarked_which == which
00692          && _last_getMarked.size() > 0 )
00693         return _last_getMarked; // use the last run
00694 
00695     MarkedResolvableInfo info = { &_last_getMarked, which };
00696 
00697     foreachMarked (marked_item_collector, &info);
00698 
00699     _last_getMarked.sort(compare_items());
00700     _last_getMarked_which = which;
00701 
00702     return _last_getMarked;
00703 }
00704 
00705 //---------------------------------------------------------------------------
00706 // install
00707 
00708 typedef struct {
00709     ResPool pool;
00710     MarkedPoolItemFn fn;
00711     PoolItemList *rl;
00712     int count;
00713 } InstallInfo;
00714 
00715 static void
00716 install_item_cb (PoolItem_Ref item, const ResStatus & status, void *data)
00717 {
00718     InstallInfo *info = (InstallInfo *)data;
00719 
00720     if (status.isToBeInstalled()
00721         && !item.status().isInstalled()
00722         && !Helper::findInstalledItem( info->pool, item))
00723     {
00724         if (info->fn) info->fn (item, status, info->rl);
00725         ++info->count;
00726     }
00727 }
00728 
00729 
00730 int
00731 ResolverContext::foreachInstall (MarkedPoolItemFn fn, void *data) const
00732 {
00733     PoolItemList *rl = (PoolItemList *)data;
00734     InstallInfo info = { _pool, fn, rl, 0 };
00735 
00736     foreachMarked (install_item_cb, (void *)&info);
00737 
00738     return info.count;
00739 }
00740 
00741 
00742 static void
00743 context_item_collector (PoolItem_Ref item, const ResStatus & status, void *data)
00744 {
00745     PoolItemList *rl = (PoolItemList *)data;
00746     if (status.isToBeInstalled()
00747         || status.isToBeUninstalled())
00748     {
00749         rl->push_front (item);
00750     }
00751 }
00752 
00753 
00754 PoolItemList
00755 ResolverContext::getInstalls (void) const
00756 {
00757     PoolItemList rl;
00758 
00759     foreachInstall (context_item_collector, (void *)&rl);
00760 
00761     return rl;
00762 }
00763 
00764 
00765 //---------------------------------------------------------------------------
00766 // satisfy
00767 
00768 typedef struct {
00769     ResPool pool;
00770     MarkedPoolItemFn fn;
00771     PoolItemList *rl;
00772     int count;
00773 } SatisfyInfo;
00774 
00775 static void
00776 satisfy_item_cb (PoolItem_Ref item, const ResStatus & status, void *data)
00777 {
00778     SatisfyInfo *info = (SatisfyInfo *)data;
00779     if (status.isSatisfied()
00780        && ! status.staysInstalled ()
00781        && !Helper::findInstalledItem (info->pool, item))
00782     {
00783        if (info->fn) info->fn (item, status, info->rl);
00784        ++info->count;
00785     }
00786 }
00787 
00788 
00789 int
00790 ResolverContext::foreachSatisfy (MarkedPoolItemFn fn, void *data) const
00791 {
00792     PoolItemList *rl = (PoolItemList *)data;
00793     SatisfyInfo info = { _pool, fn, rl, 0 };
00794 
00795     foreachMarked (satisfy_item_cb, (void *)&info);
00796 
00797     return info.count;
00798 }
00799 
00800 
00801 static void
00802 context_item_collector_satisfy (PoolItem_Ref item, const ResStatus & status, void *data)
00803 {
00804     PoolItemList *rl = (PoolItemList *)data;
00805     if (status.isSatisfied ())
00806     {
00807        rl->push_front (item);
00808     }
00809 }
00810 
00811 
00812 PoolItemList
00813 ResolverContext::getSatisfies (void) const
00814 {
00815     PoolItemList rl;
00816 
00817     foreachSatisfy (context_item_collector_satisfy, (void *)&rl);
00818 
00819     return rl;
00820 }
00821 
00822 
00823 //---------------------------------------------------------------------------
00824 // incomplete
00825 
00826 typedef struct {
00827     ResPool pool;
00828     MarkedPoolItemFn fn;
00829     PoolItemList *rl;
00830     int count;
00831 } IncompleteInfo;
00832 
00833 static void
00834 incomplete_item_cb (PoolItem_Ref item, const ResStatus & status, void *data)
00835 {
00836     IncompleteInfo *info = (IncompleteInfo *)data;
00837 
00838     if (status.isIncomplete ()) {
00839        if (info->fn) info->fn (item, status, info->rl);
00840        ++info->count;
00841     }
00842 }
00843 
00844 
00845 int
00846 ResolverContext::foreachIncomplete (MarkedPoolItemFn fn, void *data) const
00847 {
00848     PoolItemList *rl = (PoolItemList *)data;
00849     IncompleteInfo info = { _pool, fn, rl, 0 };
00850 
00851     foreachMarked (incomplete_item_cb, (void *)&info);
00852 
00853     return info.count;
00854 }
00855 
00856 
00857 static void
00858 context_item_collector_incomplete (PoolItem_Ref item, const ResStatus & status, void *data)
00859 {
00860     PoolItemList *rl = (PoolItemList *)data;
00861     if (status.isIncomplete ())
00862     {
00863        rl->push_front (item);
00864     }
00865 }
00866 
00867 
00868 PoolItemList
00869 ResolverContext::getIncompletes (void) const
00870 {
00871     PoolItemList rl;
00872 
00873     foreachIncomplete (context_item_collector_incomplete, (void *)&rl);
00874 
00875     return rl;
00876 }
00877 
00878 
00879 //---------------------------------------------------------------------------
00880 // upgrade
00881 
00882 typedef struct {
00883     ResPool pool;
00884     MarkedPoolItemPairFn fn;
00885     void *data;
00886     ResolverContext_Ptr context;
00887     int count;
00888 } UpgradeInfo;
00889 
00890 static void
00891 upgrade_item_cb (PoolItem_Ref item, const ResStatus & status, void *data)
00892 {
00893     UpgradeInfo *info = (UpgradeInfo *)data;
00894 
00895     PoolItem_Ref to_be_upgraded;
00896 
00897     if (status.isToBeInstalled()
00898         && ! item.status().isInstalled ())
00899     {
00900         to_be_upgraded = Helper::findInstalledItem(info->pool, item);
00901         if (to_be_upgraded) {
00902             if (info->fn) {
00903                 info->fn (item, status, to_be_upgraded, info->context->getStatus(to_be_upgraded), info->data);
00904             }
00905             ++info->count;
00906         }
00907     }
00908 }
00909 
00910 
00911 int
00912 ResolverContext::foreachUpgrade (MarkedPoolItemPairFn fn, void *data)
00913 {
00914     UpgradeInfo info = { _pool, fn, data, this, 0 };
00915 
00916     foreachMarked (upgrade_item_cb, (void *)&info);
00917 
00918     return info.count;
00919 }
00920 
00921 
00922 static void
00923 pair_item_collector (PoolItem_Ref item, const ResStatus & status, PoolItem_Ref old_item, const ResStatus & old_status, void *data)
00924 {
00925     PoolItemList *rl = (PoolItemList *)data;
00926     rl->push_back (item);
00927 }
00928 
00929 
00930 PoolItemList
00931 ResolverContext::getUpgrades (void)
00932 {
00933     PoolItemList rl;
00934 
00935     foreachUpgrade (pair_item_collector, (void *)&rl);
00936 
00937     return rl;
00938 }
00939 
00940 
00941 //---------------------------------------------------------------------------
00942 // uninstall
00943 
00944 typedef std::map<std::string,PoolItem_Ref> UpgradeTable;
00945 
00946 typedef struct {
00947     MarkedPoolItemFn fn;
00948     PoolItemList *rl;
00949     UpgradeTable upgrade_hash;
00950     int count;
00951 } UninstallInfo;
00952 
00953 static void
00954 uninstall_item_cb (PoolItem_Ref item, const ResStatus & status, void *data)
00955 {
00956     UninstallInfo *info = (UninstallInfo *)data;
00957 
00958     UpgradeTable::const_iterator pos = info->upgrade_hash.find(item->name());
00959 
00960     if (status.isToBeUninstalled ()
00961         && pos == info->upgrade_hash.end())             // dont count upgrades
00962     {
00963         if (info->fn)
00964             info->fn (item, status, info->rl);
00965         ++info->count;
00966     }
00967 }
00968 
00969 
00970 static void
00971 build_upgrade_hash_cb (PoolItem_Ref item_add, const ResStatus & add_status, PoolItem_Ref item_del, const ResStatus & del_status, void *data)
00972 {
00973     UpgradeTable *upgrade_hash = (UpgradeTable *)data;
00974     (*upgrade_hash)[item_del->name()] = item_del;
00975 }
00976 
00977 
00978 int
00979 ResolverContext::foreachUninstall (MarkedPoolItemFn fn, void *data)
00980 {
00981     UninstallInfo info;         // inits upgrade_hash
00982 
00983     info.fn = fn;
00984     info.rl = (PoolItemList *)data;
00985     info.count = 0;
00986 
00987     foreachUpgrade (build_upgrade_hash_cb, (void *)&(info.upgrade_hash));
00988     foreachMarked (uninstall_item_cb, (void *)&info);
00989 
00990     return info.count;
00991 }
00992 
00993 
00994 PoolItemList
00995 ResolverContext::getUninstalls (void)
00996 {
00997     PoolItemList rl;
00998 
00999     foreachUninstall (context_item_collector, (void *)&rl);
01000 
01001     return rl;
01002 }
01003 
01004 
01005 //---------------------------------------------------------------------------
01006 // impossible
01007 
01008 typedef struct {
01009     ResPool pool;
01010     MarkedPoolItemFn fn;
01011     int count;
01012     void *data;
01013 } ImpossibleInfo;
01014 
01015 static void
01016 impossible_item_cb (PoolItem_Ref item, const ResStatus & status, void *data)
01017 {
01018     ImpossibleInfo *info = (ImpossibleInfo *)data;
01019 
01020     if (status.isImpossible ()) {
01021        if (info->fn) info->fn (item, status, info->data);
01022        ++info->count;
01023     }
01024 }
01025 
01026 
01027 int
01028 ResolverContext::foreachImpossible (MarkedPoolItemFn fn, void *data)
01029 {
01030     ImpossibleInfo info = { _pool, fn, 0, data };
01031 
01032     foreachMarked (impossible_item_cb, (void *)&info);
01033 
01034     return info.count;
01035 }
01036 
01037 
01038 //---------------------------------------------------------------------------
01039 
01040 static void
01041 install_count_cb (PoolItem_Ref item, const ResStatus & status, void *data)
01042 {
01043     int *count = (int *)data;
01044     if (!item.status().isInstalled ()) {
01045         ++*count;
01046     }
01047 }
01048 
01049 int
01050 ResolverContext::installCount (void) const
01051 {
01052     int count = 0;
01053 
01054     foreachInstall (install_count_cb, (void *)&count);
01055 
01056     return count;
01057 }
01058 
01059 
01060 static void
01061 uninstall_count_cb (PoolItem_Ref item, const ResStatus & status, void *data)
01062 {
01063     int *count = (int *)data;
01064     if (item.status().isInstalled ()) {
01065         ++*count;
01066     }
01067 }
01068 
01069 
01070 int
01071 ResolverContext::uninstallCount (void)
01072 {
01073     int count = 0;
01074 
01075     foreachUninstall (uninstall_count_cb, (void *)&count);
01076 
01077     return count;
01078 }
01079 
01080 
01081 int
01082 ResolverContext::upgradeCount (void)
01083 {
01084     return foreachUpgrade ((MarkedPoolItemPairFn)NULL, (void *)NULL);
01085 }
01086 
01087 
01088 static void
01089 satisfy_count_cb (PoolItem_Ref item, const ResStatus & status, void *data)
01090 {
01091     int *count = (int *)data;
01092     if (!item.status().isInstalled ()) {
01093         ++*count;
01094     }
01095 }
01096 
01097 int
01098 ResolverContext::satisfyCount (void) const
01099 {
01100     int count = 0;
01101 
01102     foreachSatisfy (satisfy_count_cb, (void *)&count);
01103 
01104     return count;
01105 }
01106 
01107 
01108 int
01109 ResolverContext::incompleteCount (void) const
01110 {
01111     return foreachIncomplete ((MarkedPoolItemFn)NULL, (void *)NULL);
01112 }
01113 
01114 
01115 
01116 //---------------------------------------------------------------------------
01117 // info
01118 
01119 void
01120 ResolverContext::addInfo (ResolverInfo_Ptr info, bool askUser)
01121 {
01122     _XDEBUG( "ResolverContext[" << this << "]::addInfo(" << *info << ")" );
01123     _log.push_back (info);
01124 
01125     // _propagated_importance = false;
01126 
01127     if (info->error ()
01128         && !askUser) { // Go forward in order to evaluate more problems
01129 
01130         if (! _invalid) {
01131             ResolverInfo_Ptr info = new ResolverInfoMisc (RESOLVER_INFO_TYPE_INVALID_SOLUTION, PoolItem_Ref(), RESOLVER_INFO_PRIORITY_VERBOSE);
01132             info->flagAsError ();
01133             _log.push_back (info);
01134         }
01135 
01136         _invalid = true;
01137     }
01138     if (askUser)
01139         _askUser = true;
01140 }
01141 
01142 
01143 void
01144 ResolverContext::addError (ResolverInfo_Ptr info, bool askUser)
01145 {
01146     bool is_error = true;
01147 
01148     if (info->type() == RESOLVER_INFO_TYPE_UNINSTALL_LOCKED) {
01149         for (PoolItemList::const_iterator iter = _ignoreInstalledItem.begin(); iter != _ignoreInstalledItem.end(); iter++) {
01150             if (info->affected() == *iter) {
01151                 DBG << "ignore keep installed: " << info->affected() << endl;
01152                 is_error = false;
01153                 break;
01154             }
01155         }
01156     }
01157 
01158     if (is_error)
01159         info->flagAsError ();
01160 
01161     WAR << "******** Error: " << *info << endl;
01162     addInfo (info, askUser);
01163 }
01164 
01165 
01166 //---------------------------------------------------------------------------
01167 // foreach info
01168 
01169 //  We call a item mentioned by an error info an "error-item".
01170 //  We call a item mentioned by an important info an "important-item".
01171 //
01172 //  The rules:
01173 //  (1) An info item that mentions an error-item is important.
01174 //  (2) An info item is about an important-item is important.
01175 
01176 static void
01177 mark_important_info (const ResolverInfoList & il)
01178 {
01179     // set of all items mentioned in the ResolverInfoList
01180     PoolItemSet error_set;
01181 
01182     bool did_something;
01183     int pass_num = 1;
01184 
01185     /* First of all, store all error-items in a set. */
01186 
01187     for (ResolverInfoList::const_iterator info_iter = il.begin(); info_iter != il.end(); ++info_iter) {
01188         ResolverInfo_Ptr info = (*info_iter);
01189         if (info != NULL                                                // list items might be NULL
01190             && info->error ())                                          // only look at error infos
01191         {
01192             PoolItem_Ref item = info->affected();               // get item from ResolverInfoList
01193             if (item) {
01194                 error_set.insert (item);
01195             }
01196 
01197             // the info might be a container, check it by doing a dynamic cast
01198 
01199             PoolItemList containerItems;
01200             ResolverInfoContainer_constPtr c = dynamic_pointer_cast<const ResolverInfoContainer>(*info_iter);
01201             if (c != NULL) containerItems = c->items();
01202 
01203             // containerItems is non-empty if info is really a ResolverInfoContainer
01204 
01205             for (PoolItemList::iterator res_iter = containerItems.begin(); res_iter != containerItems.end(); res_iter++) {
01206                 PoolItem_Ref item = (*res_iter);
01207                 if (item) {
01208                     error_set.insert (item);
01209                 }
01210             }
01211         }
01212     }
01213 
01214     // now collect all important ones
01215 
01216     PoolItemSet important_set;
01217 
01218     do {
01219         ++pass_num;
01220         assert (pass_num < 10000);
01221 
01222         did_something = false;
01223 
01224         for (ResolverInfoList::const_iterator info_iter = il.begin(); info_iter != il.end(); ++info_iter) {
01225 
01226             ResolverInfo_Ptr info = (*info_iter);
01227 
01228             if (info != NULL                                            // list items might be set to NULL
01229                 && !info->important ())                                 // only look at ones we didn't consider yet
01230             {
01231                 bool should_be_important = false;
01232 
01233                 for (PoolItemSet::const_iterator res_iter = error_set.begin(); res_iter != error_set.end() && ! should_be_important; ++res_iter) {
01234                     ResolverInfoContainer_constPtr c = dynamic_pointer_cast<const ResolverInfoContainer>(*info_iter);
01235                     if (c != NULL                                       // check if it really is a container
01236                         && c->mentions (*res_iter))
01237                     {
01238                         should_be_important = true;
01239                     }
01240                 }
01241 
01242                 for (PoolItemSet::const_iterator res_iter = important_set.begin(); res_iter != important_set.end() && ! should_be_important; ++res_iter) {
01243                     if (info->isAbout (*res_iter)) {
01244                         should_be_important = true;
01245                         break;
01246                     }
01247                 }
01248 
01249                 if (should_be_important) {
01250                     did_something = true;
01251                     info->flagAsImportant ();
01252                     PoolItemList items;
01253                     ResolverInfoContainer_constPtr c = dynamic_pointer_cast<const ResolverInfoContainer>(*info_iter);           // check if it really is a container
01254                     if (c != NULL) items = c->items();
01255                     for (PoolItemList::iterator res_iter = items.begin(); res_iter != items.end(); res_iter++) {
01256                         important_set.insert (*res_iter);
01257                     }
01258                 }
01259             }
01260         }
01261 
01262     } while (did_something);
01263 
01264 }
01265 
01266 void
01267 ResolverContext::foreachInfo (PoolItem_Ref item, int priority, ResolverInfoFn fn, void *data) const
01268 {
01269     ResolverInfoList info_list;
01270 
01271     ResolverContext_constPtr context = this;
01272      // Assemble a list of copies of all of the info objects
01273     while (context != NULL) {
01274 
01275         for (ResolverInfoList::const_iterator iter = context->_log.begin(); iter != context->_log.end(); ++iter) {
01276 
01277             ResolverInfo_Ptr info = *iter;
01278 
01279             if ((item == PoolItem_Ref()
01280                  || info->affected() == item)
01281                 && info->priority() >= priority)
01282             {
01283                 info_list.push_back( info );
01284             }
01285         }
01286         context = context->_parent;
01287     }
01288 #if 1
01289     // Merge info objects
01290     for (ResolverInfoList::iterator iter = info_list.begin(); iter != info_list.end(); ++iter) {
01291 
01292         ResolverInfo_Ptr info1 = (*iter);
01293         ResolverInfoList::iterator subiter = iter;
01294 
01295         if (info1 != NULL) {
01296             for (subiter++; subiter != info_list.end();) {
01297                 ResolverInfo_Ptr info2 = *subiter;
01298                 ResolverInfoList::iterator next = subiter; ++next;
01299                 if (info2 && info1->merge (info2)) {
01300                     info_list.erase( subiter );
01301                 }
01302                 subiter = next;
01303             }
01304         }
01305     }
01306 #endif
01307     mark_important_info( info_list );
01308 
01309     // Walk across the list of info objects and invoke our callback
01310 
01311     for (ResolverInfoList::iterator iter = info_list.begin(); iter != info_list.end(); ++iter) {
01312         if (*iter != NULL) {
01313             fn( *iter, data );
01314         }
01315     }
01316 }
01317 
01318 
01319 
01320 static void
01321 get_info_foreach_cb (ResolverInfo_Ptr info, void *data)
01322 {
01323     ResolverInfoList *il = (ResolverInfoList *)data;
01324 
01325     if (info->important ()) {
01326         il->push_back (info);
01327     }
01328 }
01329 
01330 
01331 
01332 ResolverInfoList
01333 ResolverContext::getInfo (void) const
01334 {
01335     ResolverInfoList il;
01336     foreachInfo (PoolItem_Ref(), -1, get_info_foreach_cb, (void *)&il);
01337     return il;
01338 }
01339 
01340 
01341 //---------------------------------------------------------------------------
01342 // spew
01343 
01344 static void
01345 spew_item_cb (PoolItem_Ref item, const ResStatus & status, void *unused)
01346 {
01347     MIL << "  " << item << " (" << status << ")" << endl;
01348 }
01349 
01350 
01351 void
01352 spew_item_pair_cb (PoolItem_Ref item1, const ResStatus & status1, PoolItem_Ref item2, const ResStatus & status2, void *unused)
01353 {
01354     MIL << "  " << item2 << " (" << status2 << ") => (" << item1 << " (" << status2 << ")" << endl;
01355 }
01356 
01357 
01358 void
01359 ResolverContext::spew (void)
01360 {
01361     MIL << "TO INSTALL:" << endl;
01362     foreachInstall (spew_item_cb, NULL);
01363     MIL << endl;
01364 
01365     MIL << "TO REMOVE:" << endl;
01366     foreachUninstall (spew_item_cb, NULL);
01367     MIL << endl;
01368 
01369     MIL << "TO UPGRADE:" << endl;
01370     foreachUpgrade (spew_item_pair_cb, NULL);
01371     MIL << endl;
01372 }
01373 
01374 
01375 static void
01376 spew_info_cb (ResolverInfo_Ptr info, void *unused)
01377 {
01378     if (info == NULL) return;
01379 
01380     if (info->error ()) MIL << "[ERROR] " << *info << endl;
01381     else if (info->important()) MIL << "[>>>>>] " << *info << endl;
01382     else MIL << *info << endl;
01383 }
01384 
01385 
01386 void
01387 ResolverContext::spewInfo (void) const
01388 {
01389     _XDEBUG( "ResolverContext[" << this << "]::spewInfo" );
01390     foreachInfo (PoolItem_Ref(), -1, spew_info_cb, NULL);
01391 }
01392 
01393 //---------------------------------------------------------------------------
01394 // requirements
01395 
01396 struct RequirementMet
01397 {
01398     ResolverContext_Ptr context;
01399     const Capability capability;
01400     bool flag;
01401 
01402     RequirementMet (ResolverContext_Ptr ctx, const Capability & c)
01403         : context (ctx)
01404         , capability (c)
01405         , flag (false)
01406     { }
01407 
01408 
01409     bool operator()( const CapAndItem & cai )
01410     {
01411         Capability match( cai.cap );
01412         PoolItem provider( cai.item );
01413         // capability is set for item set children. If it is set, query the
01414         //   exact version only.
01415         if ((capability == Capability::noCap
01416              || capability == match)
01417             && context->isPresent (provider))
01418         {
01419             flag = true;
01420         }
01421 
01422 //      ERR << "RequirementMet(" <<  provider << ", " << match << ") [capability " <<
01423 //        capability << "] -> " <<  (flag ? "true" : "false") << endl;
01424 
01425         return ! flag;
01426     }
01427 };
01428 
01429 
01430 bool
01431 ResolverContext::requirementIsMet (const Capability & capability, bool is_child)
01432 {
01433     RequirementMet info (this, is_child ? capability : Capability::noCap);
01434 
01435     //    world()->foreachProviding (capability, requirement_met_cb, (void *)&info);
01436 
01437     Dep dep( Dep::PROVIDES );
01438 
01439     // world->foreachProvidingResItem (capability, require_process_cb, &info);
01440 
01441     invokeOnEach( pool().byCapabilityIndexBegin( capability.index(), dep ),
01442                   pool().byCapabilityIndexEnd( capability.index(), dep ),
01443                   resfilter::ByCapMatch( capability ),
01444                   functor::functorRef<bool,CapAndItem>(info) );
01445 _XDEBUG( "ResolverContext::requirementIsMet(" << capability << ") " << (info.flag?"Y":"N") );
01446     return info.flag;
01447 }
01448 
01449 
01450 //---------------------------------------------------------------------------
01451 
01452 struct RequirementPossible
01453 {
01454     ResolverContext_Ptr context;
01455     bool flag;
01456 
01457     RequirementPossible( ResolverContext_Ptr ctx )
01458         : context (ctx)
01459         , flag (false)
01460     { }
01461 
01462     bool operator()( const CapAndItem & cai )
01463     {
01464         PoolItem provider( cai.item );
01465         ResStatus status = context->getStatus( provider );
01466         if (! (status.isToBeUninstalled () || status.isImpossible())
01467             || status.isToBeUninstalledDueToUnlink())
01468         {
01469             flag = true;
01470         }
01471 
01472         // Checking, if it has already been selected for removing by the user
01473         // Bug 155368; Testcase data.deptestomatic/yast-tests/bug155368-test.xml
01474         if (flag
01475             && !context->forceResolve()) {
01476             PoolItem installedItem = Helper::findInstalledByNameAndKind (context->pool(), provider->name(), provider->kind() );
01477             if (installedItem) {
01478                 ResStatus statusInstalled = context->getStatus (installedItem);
01479                 if (installedItem.status().isToBeUninstalled()
01480                     && installedItem.status().isByUser()){
01481                     DBG << provider << " would satify the requirement but it has been selected for removing by the user." << endl;
01482                     flag = false;
01483                 }
01484             }
01485         }
01486         
01487         return ! flag;
01488     }
01489 };
01490 
01491 
01492 bool
01493 ResolverContext::requirementIsPossible (const Capability & capability)
01494 {
01495     RequirementPossible info( this );
01496 
01497     // world()->foreachProviding (dep, requirement_possible_cb, (void *)&info);
01498 
01499     Dep dep( Dep::PROVIDES );
01500 
01501     invokeOnEach( pool().byCapabilityIndexBegin( capability.index(), dep ),
01502                   pool().byCapabilityIndexEnd( capability.index(), dep ),
01503                   resfilter::ByCapMatch( capability ),
01504                   functor::functorRef<bool,CapAndItem>(info) );
01505     _XDEBUG("requirementIsPossible( " << capability << ") = " << (info.flag ? "Y" : "N"));
01506     return info.flag;
01507 }
01508 
01509 
01510 bool
01511 ResolverContext::itemIsPossible (PoolItem_Ref item)
01512 {
01513     CapSet requires = item->dep (Dep::REQUIRES);
01514     for (CapSet::iterator iter = requires.begin(); iter !=  requires.end(); iter++) {
01515         if (! requirementIsPossible (*iter)) {
01516             return false;
01517         }
01518     }
01519 
01520     return true;
01521 }
01522 
01523 //---------------------------------------------------------------------------
01524 
01525 typedef struct {
01526     PoolItem_Ref other;
01527     bool flag;
01528     PoolItem_Ref foundItem;
01529 } DupNameCheckInfo;
01530 
01531 static void
01532 dup_name_check_cb (PoolItem_Ref item, const ResStatus & status, void *data)
01533 {
01534     DupNameCheckInfo *info = (DupNameCheckInfo *)data;
01535     if (! info->flag
01536         && status.isToBeInstalled ()
01537         && info->other->kind() == item->kind()
01538         && info->other->name() == item->name()
01539 #if 0
01540         && item->edition().compare(info->other->edition()) == 0
01541         && item->arch() == info->other->arch()
01542 #endif
01543         && item != info->other) // if it's exactly the same package, ignore it silently.
01544     {
01545         Package::constPtr p1 = asKind<Package>(item.resolvable());
01546         Package::constPtr p2 = asKind<Package>(info->other.resolvable());
01547         if (p1 && p2 && p1->installOnly() && p2->installOnly())         // both are parallel installable
01548             return;
01549 
01550         info->flag = true;
01551         info->foundItem = item;
01552     }
01553 }
01554 
01555 
01556 bool
01557 ResolverContext::isParallelInstall (PoolItem_Ref item) const
01558 {
01559     if (item->kind() == ResTraits<Atom>::kind) {
01560         return false;                                   // Atoms are paralell installable (#170098)
01561     }
01562 
01563     for (PoolItemList::const_iterator iter = _ignoreInstalledItem.begin();
01564          iter != _ignoreInstalledItem.end(); iter++) {
01565         if (item == *iter) {
01566             DBG << "ignore parallel install: " << item << endl;
01567             return false;
01568         }
01569     }
01570     
01571     DupNameCheckInfo info;
01572 
01573     info.other = item;
01574     info.flag = false;
01575     foreachMarked (dup_name_check_cb, (void *)&info);
01576     if (info.flag) {
01577         DBG << "isParallelInstall!!(" << item << ", " << info.foundItem << ")" << endl;
01578     }
01579     return info.flag;
01580 }
01581 
01582 
01583 PoolItem_Ref
01584 ResolverContext::getParallelInstall (PoolItem_Ref item) const
01585 {
01586     DupNameCheckInfo info;
01587 
01588     info.other = item;
01589     info.flag = false;
01590     foreachMarked( dup_name_check_cb, (void *)&info );
01591     return info.foundItem;
01592 }
01593 
01594 
01595 int
01596 ResolverContext::getSourcePriority (Source_Ref source) const
01597 {
01598     if (source.subscribed())
01599         return source.priority();
01600     return source.priorityUnsubscribed();
01601 }
01602 
01603 //---------------------------------------------------------------------------
01604 
01605 static int
01606 num_cmp (double a, double b)
01607 {
01608     return (b < a) - (a < b);
01609 }
01610 
01611 static int
01612 rev_num_cmp (double a, double b)
01613 {
01614     return (a < b) - (b < a);
01615 }
01616 
01617 static double
01618 churn_factor (ResolverContext_Ptr a)
01619 {
01620     return a->upgradeCount() + (2.0 * a->installCount ()) + (4.0 * a->uninstallCount ());
01621 }
01622 
01623 void
01624 ResolverContext::collectCompareInfo (int & cmpVersion,    // Version compare of ACCUMULATED items
01625                                      int & cmpSource,    // compare of Sources
01626                                      ResolverContext_Ptr compareContext)
01627 {
01628     Source_Ref userSource;         // Source Id of items which have been selected by the user for installation
01629                                    // It is empty, if there are different sources
01630     bool differentUserSources = false;
01631     Source_Ref userSourceCompare;  // Source Id of items which have been selected by the user for installation
01632                                    // It is empty, if there are different sources
01633     bool differentUserCompareSources = false;    
01634     SourceCounter thisMap;         // Map of to be installed sources with an item counter
01635     SourceCounter compareMap;      // Map of to be installed sources with an item counter
01636     
01637     PoolItemList installList = getMarked(1);
01638     PoolItemList compareList = compareContext->getMarked(1);; // List of comparing items which has to be installed
01639     PoolItemList::const_iterator itCompare = compareList.begin();
01640     _XDEBUG ("Starting comparing two solutions--------");
01641     for ( PoolItemList::const_iterator thisIt = installList.begin();
01642           thisIt != installList.end(); thisIt++ )
01643     {
01644         // evaluate, if the user selected packages (items) has the same source
01645         ResStatus status = getStatus (*thisIt);
01646         if (status.isByUser()
01647             || thisIt->status().isByUser())
01648         {
01649             if (userSource == Source_Ref::noSource
01650                 && !differentUserSources)
01651             {
01652                 userSource = thisIt->resolvable()->source();
01653             }
01654             else if (userSource != thisIt->resolvable()->source())
01655             {
01656                 differentUserSources = true; // there are other items of other sources which have been set by the user
01657             }
01658         }
01659 
01660         // collecting relationship between channels and installed items
01661         if (thisMap.find (thisIt->resolvable()->source()) == thisMap.end()) {
01662             thisMap[thisIt->resolvable()->source()] = 1;
01663         }
01664         else {
01665             thisMap[thisIt->resolvable()->source()] += 1;
01666         }
01667         _XDEBUG ("Count of left " << thisIt->resolvable()->source() << ": " << thisMap[thisIt->resolvable()->source()] << " : " << *(thisIt->resolvable()));    
01668 
01669         // Comparing versions
01670         while (itCompare != compareList.end() )
01671         {
01672             int cmp = compareByN ( thisIt->resolvable(), itCompare->resolvable());
01673             if ( cmp == 0) {
01674                 // comparing the version of both items and "adding" the result
01675                 // to accumulated compare result.
01676                 // Testcase: freshen-tests/exercise-7f-test
01677                 // Testcase: freshen-tests/exercise-7-test
01678                 cmpVersion += thisIt->resolvable()->edition().compare( itCompare->resolvable()->edition());
01679                 _XDEBUG ("Version: " << *(thisIt->resolvable()) << "[" << thisIt->resolvable()->source() << "]" << endl
01680                          << " <--> " << endl 
01681                          << "Version: " << *(itCompare->resolvable()) << "[" << itCompare->resolvable()->source() << "]"
01682                          << " --> cmpVersion : " << cmpVersion);                
01683 
01684                 // evaluate if the user selected packages (items) has the same source
01685                 ResObject::constPtr sourceItem = itCompare->resolvable();
01686                 ResStatus compStatus = compareContext->getStatus(*itCompare);
01687                 if (compStatus.isByUser()
01688                     || itCompare->status().isByUser())
01689                 {
01690                     if (userSourceCompare == Source_Ref::noSource
01691                         && !differentUserCompareSources)
01692                         userSourceCompare = sourceItem->source();
01693                     else if (userSourceCompare != sourceItem->source())
01694                         differentUserCompareSources = true; // there are other items of other sources which have been set by the user
01695                 }
01696                 // collecting relationship between channels and installed items
01697                 if (compareMap.find (sourceItem->source()) == compareMap.end()) 
01698                     compareMap[sourceItem->source()] = 1;
01699                 else
01700                     compareMap[sourceItem->source()] += 1;
01701                 _XDEBUG ("Count of right " << sourceItem->source() << ": " << compareMap[sourceItem->source()] << " : " << *(itCompare->resolvable()));
01702                 itCompare++;
01703             } else if (cmp > 0 )
01704                 itCompare++;
01705             else break;
01706         }
01707     }
01708 
01709     // comparing the rest of the other install list
01710     while (itCompare != compareList.end() )
01711     {
01712         // evaluate if the user selected packages (items) has the same source
01713         ResObject::constPtr sourceItem = itCompare->resolvable();
01714         ResStatus compStatus = compareContext->getStatus(*itCompare);
01715         if (compStatus.isByUser()
01716             || itCompare->status().isByUser()) 
01717         {
01718             if (userSourceCompare == Source_Ref::noSource
01719                 && !differentUserCompareSources)
01720                     userSourceCompare = sourceItem->source();           
01721             else if (userSourceCompare != sourceItem->source())
01722                 differentUserCompareSources = true; // there are other items of other sources which have been set by the user           
01723         }
01724 
01725         // collecting relationship between channels and installed items
01726         if (compareMap.find (sourceItem->source()) == compareMap.end()) 
01727             compareMap[sourceItem->source()] = 1;
01728         else
01729             compareMap[sourceItem->source()] += 1;
01730         _XDEBUG ("Count of right" << sourceItem->source() << ": " << compareMap[sourceItem->source()] << " : "
01731                  << *(itCompare->resolvable()));        
01732         itCompare++;    
01733     }
01734 
01735     // evaluate cmpSource
01736     cmpSource = 0;
01737     int cmpCompare = 0;
01738 
01739     if (!differentUserSources)
01740     {
01741         // user selected items which has to be installed has only one channel;
01742         // cmpSource = number of items of that channel
01743         cmpSource = thisMap[userSource];
01744     }
01745         
01746     if (!differentUserCompareSources) {
01747         // user selected items which has to be installed has only one channel;  
01748         // cmpCompare = number of items of that channel
01749         cmpCompare = compareMap[userSourceCompare];
01750     }
01751     _XDEBUG ("cmpSource = " << cmpSource << " ; cmpCompare = " << cmpCompare << " ; sizeof compareMap:" <<  compareMap.size());
01752     if (compareMap.size() == 1
01753         && thisMap.size() == 1
01754         && userSource == userSourceCompare) {
01755         // We have only one source from which all items will be instaled.
01756         // So we will regards the complete amount of installed/updated packages
01757         // Testcase basic-exercises/exercise-14-test
01758         cmpSource = 0;
01759     } else {
01760         // The solutions has different channels with user selected items.
01761         // Take the solution with the greater account of items in this channel
01762         // Testcase basic-exercises/exercise-solution-order-test
01763         cmpSource = cmpSource - cmpCompare;         
01764     }
01765 
01766     if (cmpSource == 0)
01767     {
01768         // less amount of channels are better
01769         cmpSource = compareMap.size() - thisMap.size();
01770     }
01771     _XDEBUG ("End comparing two solutions-------- Version compare: " << cmpVersion << " Source compare: "<< cmpSource);    
01772 }
01773 
01774 int
01775 ResolverContext::partialCompare (ResolverContext_Ptr context)
01776 {
01777     int cmp = 0;
01778     if (this != context) {
01779 
01780         // collecting all data for comparing both resultion results
01781         int  cmpVersion = 0; // Version compare of ACCUMULATED items
01782         int  cmpSource = 0;  // compare of Sources
01783 
01784         collectCompareInfo (cmpVersion, cmpSource, context);
01785 
01786         // comparing versions
01787         cmp = cmpVersion;
01788         DBG << "Comparing versions returned :" << cmp << endl;
01789         if (cmp == 0) { 
01790             // High numbers are good... we don't want solutions containing low-priority channels.
01791             // Source priority which has been set externally
01792             cmp = num_cmp (_min_priority, context->_min_priority);
01793             DBG << "Comparing priority returned :" << cmp << endl;
01794             if (cmp == 0) {
01795 
01796                 // Comparing sources regarding the items which has to be installed
01797                 cmp = cmpSource;
01798                 DBG << "Comparing sources returned :" << cmp << endl;
01799                 if (cmp == 0) {         
01800                 
01801                     // High numbers are bad.  Less churn is better.
01802                     cmp = rev_num_cmp (churn_factor (this), churn_factor (context));
01803                     DBG << "Comparing churn_factor returned :" << cmp << endl;
01804                     if (cmp == 0) {
01805 
01806                         // High numbers are bad.  Bigger #s means more penalties.
01807                         cmp = rev_num_cmp (_other_penalties, context->_other_penalties);
01808                         DBG << "Comparing other penalties returned :" << cmp << endl;                   
01809                     }
01810                 }
01811             }
01812         }
01813     }
01814 
01815     return cmp;
01816 }
01817 
01818 int
01819 ResolverContext::compare (ResolverContext_Ptr context)
01820 {
01821     int cmp;
01822     
01823     if (this == context)
01824         return 0;
01825     
01826     cmp = partialCompare (context);
01827     if (cmp)
01828         return cmp;
01829     
01830     /* High numbers are bad.  Smaller downloads are best. */
01831     cmp = rev_num_cmp (_download_size, context->_download_size);
01832     if (cmp)
01833         return cmp;
01834     
01835     /* High numbers are bad.  Less disk space consumed is good. */
01836     cmp = rev_num_cmp (_install_size, context->_install_size);
01837     if (cmp)
01838         return cmp;
01839     
01840     return 0;
01841 }
01842 
01844     };// namespace detail
01847   };// namespace solver
01850 };// namespace zypp
01852 

Generated on Thu May 4 16:03:25 2006 for zypp by  doxygen 1.4.6