fix in "ignore conflicts" if the conflict has been caused by an obsolete
[platform/upstream/libzypp.git] / zypp / solver / detail / SolutionAction.cc
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 4 -*- */
2 /* SolutionAction.cc
3  *
4  * Easy-to use interface to the ZYPP dependency resolver
5  *
6  * Copyright (C) 2000-2002 Ximian, Inc.
7  * Copyright (C) 2005 SUSE Linux Products GmbH
8  *
9  * This program is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU General Public License,
11  * version 2, as published by the Free Software Foundation.
12  *
13  * This program is distributed in the hope that it will be useful, but
14  * WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
21  * 02111-1307, USA.
22  */
23
24 #include "zypp/solver/detail/Resolver.h"
25 #include "zypp/solver/detail/SolutionAction.h"
26 #include "zypp/CapSet.h"
27 #include "zypp/base/Logger.h"
28 #include "zypp/Dependencies.h"
29
30
31 /////////////////////////////////////////////////////////////////////////
32 namespace zypp
33 { ///////////////////////////////////////////////////////////////////////
34   ///////////////////////////////////////////////////////////////////////
35   namespace solver
36   { /////////////////////////////////////////////////////////////////////
37     /////////////////////////////////////////////////////////////////////
38     namespace detail
39     { ///////////////////////////////////////////////////////////////////
40
41 using namespace std;
42
43 IMPL_PTR_TYPE(SolutionAction);
44 IMPL_PTR_TYPE(TransactionSolutionAction);
45 IMPL_PTR_TYPE(InjectSolutionAction);
46
47 //---------------------------------------------------------------------------
48
49 SolutionAction::SolutionAction()
50 {
51 }
52
53
54 SolutionAction::~SolutionAction()
55 {
56 }
57
58
59 //---------------------------------------------------------------------------
60
61 ostream &
62 TransactionSolutionAction::dumpOn( ostream& os) const
63 {
64     os << "TransactionSolutionAction: ";
65     switch (_action) {
66         case KEEP:      os << "Keep"; break;
67         case INSTALL:   os << "Install"; break;
68         case REMOVE:    os << "Remove"; break;
69         case UNLOCK:    os << "Unlock"; break;
70         case ALLBRANCHES:       os << "All branches"; break;                
71     }
72     os << " ";
73     os << _item;
74     os << endl;
75     return os;
76 }
77
78
79 ostream&
80 operator<<( ostream& os, const SolutionActionList & actionlist)
81 {
82     for (SolutionActionList::const_iterator iter = actionlist.begin(); iter != actionlist.end(); ++iter) {
83         os << *(*iter);
84         os << endl;
85     }
86     return os;
87 }
88
89
90 ostream&
91 operator<<( ostream& os, const CSolutionActionList & actionlist)
92 {
93     for (CSolutionActionList::const_iterator iter = actionlist.begin(); iter != actionlist.end(); ++iter) {
94         os << *(*iter);
95         os << endl;
96     }
97     return os;
98 }
99
100 //---------------------------------------------------------------------------
101
102 ostream &
103 InjectSolutionAction::dumpOn( ostream& os ) const
104 {
105     os << "InjectSolutionAction: ";
106     os << _capability;
107     os << ", ";
108     switch (_kind) {
109         case REQUIRES:  os << "Requires"; break;
110         case CONFLICTS: os << "Conflicts"; break;
111         case OBSOLETES: os << "Obsoletes"; break;
112         case INSTALLED: os << "Installed"; break;
113         case ARCHITECTURE: os << "Architecture"; break;     
114         default: os << "Wrong kind"; break;
115     }
116     os << " ";
117     os << _item;            
118     os << endl;
119     return os;
120 }
121
122 //---------------------------------------------------------------------------
123
124
125 ostream &
126 SolutionAction::dumpOn( std::ostream & os ) const
127 {
128     os << "SolutionAction<";
129     os << "not specified";
130     os << "> ";
131     return os;
132 }
133
134
135 bool 
136 TransactionSolutionAction::execute(Resolver & resolver) const
137 {
138     bool ret = true;
139     switch (action()) {
140         case KEEP:
141             resolver.addIgnoreInstalledItem( _item );
142             /*FALLTHRU*/
143         case INSTALL:
144             if (_item.status().isToBeUninstalled())
145                 ret = _item.status().setTransact (false, ResStatus::USER);
146             else
147                 _item.status().setToBeInstalled (ResStatus::USER);
148             break;
149         case REMOVE:
150             if (_item.status().isToBeInstalled()) {
151                 _item.status().setTransact (false,ResStatus::USER);
152                 _item.status().setLock (true,ResStatus::USER); // no other dependency can set it again          
153             } else if (_item.status().isInstalled())
154                 _item.status().setToBeUninstalled (ResStatus::USER);
155             else
156                 _item.status().setLock (true,ResStatus::USER); // no other dependency can set it again          
157             break;
158         case UNLOCK:
159             ret = _item.status().setLock (false, ResStatus::USER);
160             if (!ret) ERR << "Cannot unlock " << _item << endl;
161             break;
162         case ALLBRANCHES:
163             resolver.setTryAllPossibilities (true);
164             break;
165         default:
166             ERR << "Wrong TransactionKind" << endl;
167             ret = false;
168     }
169     return ret;
170 }
171
172 bool
173 InjectSolutionAction::execute(Resolver & resolver) const
174 {
175     Dependencies dependencies = _item.resolvable()->deps();
176     CapSet depList = dependencies[Dep::CONFLICTS];    
177     switch (_kind) {
178         case CONFLICTS:
179             // removing conflict in both resolvables
180             for (CapSet::const_iterator iter = depList.begin(); iter != depList.end(); iter++) {
181                 if (iter->matches (_capability) != CapMatch::yes )
182                 {
183                     resolver.addIgnoreConflict (_item, _capability);
184                 }
185             }
186             // Obsoletes are conflicts too
187             depList = dependencies[Dep::OBSOLETES];
188             for (CapSet::const_iterator iter = depList.begin(); iter != depList.end(); iter++) {
189                 if (iter->matches (_capability) != CapMatch::yes )
190                 {
191                     resolver.addIgnoreConflict (_otherItem, _capability);
192                 }
193             }
194             
195             dependencies = _otherItem.resolvable()->deps();
196             depList = dependencies[Dep::CONFLICTS];
197             for (CapSet::const_iterator iter = depList.begin(); iter != depList.end(); iter++) {
198                 if (iter->matches (_capability) != CapMatch::yes )
199                 {
200                     resolver.addIgnoreConflict (_otherItem, _capability);
201                 }
202             }
203             // Obsoletes are conflicts too          
204             depList = dependencies[Dep::OBSOLETES];
205             for (CapSet::const_iterator iter = depList.begin(); iter != depList.end(); iter++) {
206                 if (iter->matches (_capability) != CapMatch::yes )
207                 {
208                     resolver.addIgnoreConflict (_otherItem, _capability);
209                 }
210             }
211             
212             break;
213         case REQUIRES:
214             // removing the requires dependency from the item
215             resolver.addIgnoreRequires (_item, _capability);
216             break;
217         case ARCHITECTURE:
218             // This item is for ALL architectures available
219             resolver.addIgnoreArchitectureItem (_item);
220             break;          
221         case OBSOLETES:
222             // removing the obsoletes dependency from the item
223             resolver.addIgnoreObsoletes (_otherItem, _capability);
224             break;          
225         case INSTALLED:
226             // ignoring already installed items
227             resolver.addIgnoreInstalledItem (_item);
228             resolver.addIgnoreInstalledItem (_otherItem);
229             break;
230         default:
231             ERR << "No valid InjectSolutionAction kind found" << endl;
232             return false;
233     }
234
235     return true;
236 }
237
238       ///////////////////////////////////////////////////////////////////
239     };// namespace detail
240     /////////////////////////////////////////////////////////////////////
241     /////////////////////////////////////////////////////////////////////
242   };// namespace solver
243   ///////////////////////////////////////////////////////////////////////
244   ///////////////////////////////////////////////////////////////////////
245 };// namespace zypp
246 /////////////////////////////////////////////////////////////////////////