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