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