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