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