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