Package Manager
[ntk/apt.git] / apt-pkg / packagemanager.cc
CommitLineData
6c139d6e
AL
1// -*- mode: cpp; mode: fold -*-
2// Description /*{{{*/
b50b2c97 3// $Id: packagemanager.cc,v 1.2 1998/07/09 05:41:12 jgg Exp $
6c139d6e
AL
4/* ######################################################################
5
6 Package Manager - Abstacts the package manager
7
8 More work is needed in the area of transitioning provides, ie exim
9 replacing smail. This can cause interesing side effects.
10
11 Other cases involving conflicts+replaces should be tested.
12
13 ##################################################################### */
14 /*}}}*/
15// Include Files /*{{{*/
16#ifdef __GNUG__
17#pragma implementation "pkglib/packagemanager.h"
18#endif
19#include <pkglib/packagemanager.h>
20#include <pkglib/orderlist.h>
21#include <pkglib/depcache.h>
6c139d6e 22#include <pkglib/error.h>
b50b2c97 23#include <pkglib/version.h>
6c139d6e
AL
24 /*}}}*/
25
26// PM::PackageManager - Constructor /*{{{*/
27// ---------------------------------------------------------------------
28/* */
29pkgPackageManager::pkgPackageManager(pkgDepCache &Cache) : Cache(Cache)
30{
31 FileNames = new string[Cache.Head().PackageCount];
32 List = 0;
33}
34 /*}}}*/
35// PM::PackageManager - Destructor /*{{{*/
36// ---------------------------------------------------------------------
37/* */
38pkgPackageManager::~pkgPackageManager()
39{
40 delete List;
41 delete [] FileNames;
42}
43 /*}}}*/
6c139d6e
AL
44// PM::FixMissing - Keep all missing packages /*{{{*/
45// ---------------------------------------------------------------------
46/* This is called to correct the installation when packages could not
47 be downloaded. */
48bool pkgPackageManager::FixMissing()
49{
50 unsigned char *Touch = new unsigned char[Cache.Head().PackageCount];
51 for (PkgIterator I = Cache.PkgBegin(); I.end() == false; I++)
52 {
53 // Create the status list that ResolveConflicts needs
54 if ((Cache[I].DepState & pkgDepCache::DepNowMin) == pkgDepCache::DepNowMin)
55 Touch[I->ID] = (1 << 0) | (1 << 1);
56 else
57 Touch[I->ID] = 1 << 1;
58
59 if (Cache[I].Keep() == true)
60 continue;
61 if (FileNames[I->ID].empty() == false || Cache[I].Delete() == true)
62 continue;
63 Cache.MarkKeep(I);
64 }
65
66 // Now downgrade everything that is broken
67 Cache.ResolveConflicts(Touch);
68 delete [] Touch;
69
70 return Cache.BrokenCount() == 0;
71}
72 /*}}}*/
73
74// PM::DepAlwaysTrue - Returns true if this dep is irrelevent /*{{{*/
75// ---------------------------------------------------------------------
76/* The restriction on provides is to eliminate the case when provides
77 are transitioning between valid states [ie exim to smail] */
78bool pkgPackageManager::DepAlwaysTrue(DepIterator D)
79{
80 if (D.TargetPkg()->ProvidesList != 0)
81 return false;
82
83 if ((Cache[D] & pkgDepCache::DepInstall) != 0 &&
84 (Cache[D] & pkgDepCache::DepNow) != 0)
85 return true;
86 return false;
87}
88 /*}}}*/
89// PM::CheckRConflicts - Look for reverse conflicts /*{{{*/
90// ---------------------------------------------------------------------
91/* This looks over the reverses for a conflicts line that needs early
92 removal. */
93bool pkgPackageManager::CheckRConflicts(PkgIterator Pkg,DepIterator D,
94 const char *Ver)
95{
96 for (;D.end() == false; D++)
97 {
b50b2c97 98 if (D->Type != pkgCache::Dep::Conflicts)
6c139d6e
AL
99 continue;
100
101 if (D.ParentPkg() == Pkg)
102 continue;
103
104 if (pkgCheckDep(D.TargetVer(),Ver,D->CompareOp) == false)
105 continue;
106
107 if (List->IsNow(Pkg) == false)
108 continue;
109
110 if (EarlyRemove(D.ParentPkg()) == false)
111 return false;
112 }
113 return true;
114}
115 /*}}}*/
116// PM::ConfigureAll - Run the all out configuration /*{{{*/
117// ---------------------------------------------------------------------
118/* This configures every package. It is assumed they are all unpacked and
119 that the final configuration is valid. */
120bool pkgPackageManager::ConfigureAll()
121{
122 pkgOrderList OList(Cache);
123
124 // Populate the order list
125 for (pkgOrderList::iterator I = List->begin(); I != List->end(); I++)
126 if (List->IsFlag(pkgCache::PkgIterator(Cache,*I),
127 pkgOrderList::UnPacked) == true)
128 OList.push_back(*I);
129
130 if (OList.OrderConfigure() == false)
131 return false;
132
133 // Perform the configuring
134 for (pkgOrderList::iterator I = OList.begin(); I != OList.end(); I++)
135 {
136 PkgIterator Pkg(Cache,*I);
137
138 if (Configure(Pkg) == false)
139 return false;
140
141 List->Flag(Pkg,pkgOrderList::Configured,pkgOrderList::States);
142 }
143
144 return true;
145}
146 /*}}}*/
147// PM::SmartConfigure - Perform immediate configuration of the pkg /*{{{*/
148// ---------------------------------------------------------------------
149/* This routine scheduals the configuration of the given package and all
150 of it's dependents. */
151bool pkgPackageManager::SmartConfigure(PkgIterator Pkg)
152{
153 pkgOrderList OList(Cache);
154
155 if (DepAdd(OList,Pkg) == false)
156 return false;
157
158 if (OList.OrderConfigure() == false)
159 return false;
160
161 // Perform the configuring
162 for (pkgOrderList::iterator I = OList.begin(); I != OList.end(); I++)
163 {
164 PkgIterator Pkg(Cache,*I);
165
166 if (Configure(Pkg) == false)
167 return false;
168
169 List->Flag(Pkg,pkgOrderList::Configured,pkgOrderList::States);
170 }
171
172 // Sanity Check
173 if (List->IsFlag(Pkg,pkgOrderList::Configured) == false)
174 return _error->Error("Internal error, could not immediate configure %s",Pkg.Name());
175
176 return true;
177}
178 /*}}}*/
179// PM::DepAdd - Add all dependents to the oder list /*{{{*/
180// ---------------------------------------------------------------------
181/* This recursively adds all dependents to the order list */
182bool pkgPackageManager::DepAdd(pkgOrderList &OList,PkgIterator Pkg,int Depth)
183{
184 if (OList.IsFlag(Pkg,pkgOrderList::Added) == true)
185 return true;
186 if (List->IsFlag(Pkg,pkgOrderList::Configured) == true)
187 return true;
188 if (List->IsFlag(Pkg,pkgOrderList::UnPacked) == false)
189 return false;
190
191
192 // Put the package on the list
193 OList.push_back(Pkg);
194 OList.Flag(Pkg,pkgOrderList::Added);
195 Depth++;
196
197 // Check the dependencies to see if they are all satisfied.
198 bool Bad = false;
199 for (DepIterator D = Cache[Pkg].InstVerIter(Cache).DependsList(); D.end() == false;)
200 {
b50b2c97 201 if (D->Type != pkgCache::Dep::Depends && D->Type != pkgCache::Dep::PreDepends)
6c139d6e
AL
202 {
203 D++;
204 continue;
205 }
206
207 // Grok or groups
208 Bad = true;
209 for (bool LastOR = true; D.end() == false && LastOR == true; D++)
210 {
b50b2c97 211 LastOR = (D->CompareOp & pkgCache::Dep::Or) == pkgCache::Dep::Or;
6c139d6e
AL
212
213 if (Bad == false)
214 continue;
215
216 Version **VList = D.AllTargets();
217 for (Version **I = VList; *I != 0 && Bad == true; I++)
218 {
219 VerIterator Ver(Cache,*I);
220 PkgIterator Pkg = Ver.ParentPkg();
221
222 // See if the current version is ok
223 if (Pkg.CurrentVer() == Ver && List->IsNow(Pkg) == true &&
224 Pkg.State() == PkgIterator::NeedsNothing)
225 {
226 Bad = false;
227 continue;
228 }
229
230 // Not the install version
231 if (Cache[Pkg].InstallVer != *I ||
232 (Cache[Pkg].Keep() == true && Pkg.State() == PkgIterator::NeedsNothing))
233 continue;
234 if (List->IsFlag(Pkg,pkgOrderList::UnPacked) == true)
235 Bad = !DepAdd(OList,Pkg,Depth);
236 if (List->IsFlag(Pkg,pkgOrderList::Configured) == true)
237 Bad = false;
238 }
239 delete [] VList;
240 }
241
242 if (Bad == true)
243 {
244 OList.Flag(Pkg,0,pkgOrderList::Added);
245 OList.pop_back();
246 Depth--;
247 return false;
248 }
249 }
250
251 Depth--;
252 return true;
253}
254 /*}}}*/
255// PM::EarlyRemove - Perform removal of packages before their time /*{{{*/
256// ---------------------------------------------------------------------
257/* This is called to deal with conflicts arising from unpacking */
258bool pkgPackageManager::EarlyRemove(PkgIterator Pkg)
259{
260 if (List->IsNow(Pkg) == false)
261 return true;
262
263 // Already removed it
264 if (List->IsFlag(Pkg,pkgOrderList::Removed) == true)
265 return true;
266
267 // Woops, it will not be re-installed!
268 if (List->IsFlag(Pkg,pkgOrderList::InList) == false)
269 return false;
270
271 bool Res = SmartRemove(Pkg);
272 if (Cache[Pkg].Delete() == false)
273 List->Flag(Pkg,pkgOrderList::Removed,pkgOrderList::States);
274
275 return Res;
276}
277 /*}}}*/
278// PM::SmartRemove - Removal Helper /*{{{*/
279// ---------------------------------------------------------------------
280/* */
281bool pkgPackageManager::SmartRemove(PkgIterator Pkg)
282{
283 if (List->IsNow(Pkg) == false)
284 return true;
285
286 List->Flag(Pkg,pkgOrderList::Configured,pkgOrderList::States);
287 return Remove(Pkg);
288}
289 /*}}}*/
290// PM::SmartUnPack - Install helper /*{{{*/
291// ---------------------------------------------------------------------
292/* This performs the task of handling pre-depends. */
293bool pkgPackageManager::SmartUnPack(PkgIterator Pkg)
294{
295 // Check if it is already unpacked
296 if (Pkg.State() == pkgCache::PkgIterator::NeedsConfigure &&
297 Cache[Pkg].Keep() == true)
298 {
299 List->Flag(Pkg,pkgOrderList::UnPacked,pkgOrderList::States);
300 if (List->IsFlag(Pkg,pkgOrderList::Immediate) == true)
301 if (SmartConfigure(Pkg) == false)
302 return _error->Error("Internal Error, Could not perform immediate configuraton");
303 return true;
304 }
305
306 /* See if this packages install version has any predependencies
307 that are not met by 'now' packages. */
308 for (DepIterator D = Cache[Pkg].InstVerIter(Cache).DependsList();
309 D.end() == false; D++)
310 {
b50b2c97 311 if (D->Type == pkgCache::Dep::PreDepends)
6c139d6e
AL
312 {
313 // Look for possible ok targets.
314 Version **VList = D.AllTargets();
315 bool Bad = true;
316 for (Version **I = VList; *I != 0 && Bad == true; I++)
317 {
318 VerIterator Ver(Cache,*I);
319 PkgIterator Pkg = Ver.ParentPkg();
320
321 // See if the current version is ok
322 if (Pkg.CurrentVer() == Ver && List->IsNow(Pkg) == true &&
323 Pkg.State() == PkgIterator::NeedsNothing)
324 {
325 Bad = false;
326 continue;
327 }
328 }
329
330 // Look for something that could be configured.
331 for (Version **I = VList; *I != 0 && Bad == true; I++)
332 {
333 VerIterator Ver(Cache,*I);
334 PkgIterator Pkg = Ver.ParentPkg();
335
336 // Not the install version
337 if (Cache[Pkg].InstallVer != *I ||
338 (Cache[Pkg].Keep() == true && Pkg.State() == PkgIterator::NeedsNothing))
339 continue;
340
341 Bad = !SmartConfigure(Pkg);
342 }
343
344 delete [] VList;
345
346 if (Bad == true)
347 return _error->Error("Internal Error, Couldn't configure a pre-depend");
348
349 continue;
350 }
351
b50b2c97 352 if (D->Type == pkgCache::Dep::Conflicts)
6c139d6e
AL
353 {
354 /* Look for conflicts. Two packages that are both in the install
355 state cannot conflict so we don't check.. */
356 Version **VList = D.AllTargets();
357 for (Version **I = VList; *I != 0; I++)
358 {
359 VerIterator Ver(Cache,*I);
360 PkgIterator Pkg = Ver.ParentPkg();
361
362 // See if the current version is conflicting
363 if (Pkg.CurrentVer() == Ver && List->IsNow(Pkg) == true)
364 {
365 if (EarlyRemove(Pkg) == false)
366 return _error->Error("Internal Error, Could not early remove %s",Pkg.Name());
367 }
368 }
369 delete [] VList;
370 }
371 }
372
373 // Check for reverse conflicts.
374 CheckRConflicts(Pkg,Pkg.RevDependsList(),
375 Cache[Pkg].InstVerIter(Cache).VerStr());
376 for (PrvIterator P = Cache[Pkg].InstVerIter(Cache).ProvidesList();
377 P.end() == false; P++)
378 CheckRConflicts(Pkg,P.ParentPkg().RevDependsList(),P.ProvideVersion());
379
380 if (Install(Pkg,FileNames[Pkg->ID]) == false)
381 return false;
382
383 List->Flag(Pkg,pkgOrderList::UnPacked,pkgOrderList::States);
384
385 // Perform immedate configuration of the package.
386 if (List->IsFlag(Pkg,pkgOrderList::Immediate) == true)
387 if (SmartConfigure(Pkg) == false)
388 return _error->Error("Internal Error, Could not perform immediate configuraton");
389
390 return true;
391}
392 /*}}}*/
393// PM::OrderInstall - Installation ordering routine /*{{{*/
394// ---------------------------------------------------------------------
395/* */
396bool pkgPackageManager::OrderInstall()
397{
398 delete List;
399 List = new pkgOrderList(Cache);
400
401 // Generate the list of affected packages and sort it
402 for (PkgIterator I = Cache.PkgBegin(); I.end() == false; I++)
403 {
404 // Consider all depends
b50b2c97 405 if ((I->Flags & pkgCache::Flag::Essential) == pkgCache::Flag::Essential)
6c139d6e
AL
406 {
407 List->Flag(I,pkgOrderList::Immediate);
408 if (Cache[I].InstallVer != 0)
409 for (DepIterator D = Cache[I].InstVerIter(Cache).DependsList();
410 D.end() == false; D++)
b50b2c97 411 if (D->Type == pkgCache::Dep::Depends || D->Type == pkgCache::Dep::PreDepends)
6c139d6e
AL
412 List->Flag(D.TargetPkg(),pkgOrderList::Immediate);
413 if (I->CurrentVer != 0)
414 for (DepIterator D = I.CurrentVer().DependsList();
415 D.end() == false; D++)
b50b2c97 416 if (D->Type == pkgCache::Dep::Depends || D->Type == pkgCache::Dep::PreDepends)
6c139d6e
AL
417 List->Flag(D.TargetPkg(),pkgOrderList::Immediate);
418 }
419
420 // Not interesting
421 if ((Cache[I].Keep() == true ||
422 Cache[I].InstVerIter(Cache) == I.CurrentVer()) &&
423 I.State() == pkgCache::PkgIterator::NeedsNothing)
424 continue;
425
426 // Append it to the list
427 List->push_back(I);
428
b50b2c97 429 if ((I->Flags & pkgCache::Flag::ImmediateConf) == pkgCache::Flag::ImmediateConf)
6c139d6e
AL
430 List->Flag(I,pkgOrderList::Immediate);
431 }
432
433 if (List->OrderUnpack() == false)
434 return _error->Error("Internal ordering error");
435
436 for (pkgOrderList::iterator I = List->begin(); I != List->end(); I++)
437 {
438 PkgIterator Pkg(Cache,*I);
439
440 // Sanity check
441 if (Cache[Pkg].Keep() == true && Pkg.State() == pkgCache::PkgIterator::NeedsNothing)
442 return _error->Error("Internal Error, trying to manipulate a kept package");
443
444 // Perform a delete or an install
445 if (Cache[Pkg].Delete() == true)
446 {
447 if (SmartRemove(Pkg) == false)
448 return false;
449 }
450 else
451 if (SmartUnPack(Pkg) == false)
452 return false;
453 }
454
455 // Final run through the configure phase
456 if (ConfigureAll() == false)
457 return false;
458
459 // Sanity check
460 for (pkgOrderList::iterator I = List->begin(); I != List->end(); I++)
461 if (List->IsFlag(*I,pkgOrderList::Configured) == false)
462 return _error->Error("Internal error, packages left unconfigured. %s",
463 PkgIterator(Cache,*I).Name());
464
465 return true;
466}
467 /*}}}*/
468// PM::DoInstall - Does the installation /*{{{*/
469// ---------------------------------------------------------------------
470/* This uses the filenames in FileNames and the information in the
471 DepCache to perform the installation of packages.*/
472bool pkgPackageManager::DoInstall()
473{
474 return OrderInstall() && Go();
475}
476 /*}}}*/