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