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