]> git.saurik.com Git - apt.git/blame - apt-pkg/packagemanager.cc
Fixed a problem where the loop flag would be removed prematurely.
[apt.git] / apt-pkg / packagemanager.cc
CommitLineData
6c139d6e
AL
1// -*- mode: cpp; mode: fold -*-
2// Description /*{{{*/
b9c0654c 3// $Id: packagemanager.cc,v 1.30 2003/04/27 03:04:15 doogie 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 /*{{{*/
094a497d
AL
16#include <apt-pkg/packagemanager.h>
17#include <apt-pkg/orderlist.h>
18#include <apt-pkg/depcache.h>
19#include <apt-pkg/error.h>
20#include <apt-pkg/version.h>
03e39e59 21#include <apt-pkg/acquire-item.h>
30e1eab5
AL
22#include <apt-pkg/algorithms.h>
23#include <apt-pkg/configuration.h>
b2e465d6
AL
24#include <apt-pkg/sptr.h>
25
26#include <apti18n.h>
5819a761 27#include <iostream>
1d6386f3 28#include <fcntl.h>
92fcbfc1 29 /*}}}*/
5819a761
AL
30using namespace std;
31
590f1923
CB
32bool pkgPackageManager::SigINTStop = false;
33
6c139d6e
AL
34// PM::PackageManager - Constructor /*{{{*/
35// ---------------------------------------------------------------------
36/* */
b2e465d6 37pkgPackageManager::pkgPackageManager(pkgDepCache *pCache) : Cache(*pCache)
6c139d6e
AL
38{
39 FileNames = new string[Cache.Head().PackageCount];
40 List = 0;
30e1eab5 41 Debug = _config->FindB("Debug::pkgPackageManager",false);
6c139d6e
AL
42}
43 /*}}}*/
44// PM::PackageManager - Destructor /*{{{*/
45// ---------------------------------------------------------------------
46/* */
47pkgPackageManager::~pkgPackageManager()
48{
49 delete List;
50 delete [] FileNames;
51}
52 /*}}}*/
03e39e59
AL
53// PM::GetArchives - Queue the archives for download /*{{{*/
54// ---------------------------------------------------------------------
55/* */
56bool pkgPackageManager::GetArchives(pkgAcquire *Owner,pkgSourceList *Sources,
57 pkgRecords *Recs)
58{
7a1b1f8b
AL
59 if (CreateOrderList() == false)
60 return false;
61
5e312de7
DK
62 bool const ordering =
63 _config->FindB("PackageManager::UnpackAll",true) ?
64 List->OrderUnpack() : List->OrderCritical();
65 if (ordering == false)
7a1b1f8b
AL
66 return _error->Error("Internal ordering error");
67
68 for (pkgOrderList::iterator I = List->begin(); I != List->end(); I++)
69 {
70 PkgIterator Pkg(Cache,*I);
281daf46
AL
71 FileNames[Pkg->ID] = string();
72
7a1b1f8b
AL
73 // Skip packages to erase
74 if (Cache[Pkg].Delete() == true)
03e39e59 75 continue;
d38b7b3d
AL
76
77 // Skip Packages that need configure only.
9dbb421f
AL
78 if (Pkg.State() == pkgCache::PkgIterator::NeedsConfigure &&
79 Cache[Pkg].Keep() == true)
d38b7b3d 80 continue;
281daf46
AL
81
82 // Skip already processed packages
83 if (List->IsNow(Pkg) == false)
84 continue;
803ea2a8 85
7a1b1f8b
AL
86 new pkgAcqArchive(Owner,Sources,Recs,Cache[Pkg].InstVerIter(Cache),
87 FileNames[Pkg->ID]);
03e39e59 88 }
7a1b1f8b 89
03e39e59
AL
90 return true;
91}
92 /*}}}*/
6c139d6e
AL
93// PM::FixMissing - Keep all missing packages /*{{{*/
94// ---------------------------------------------------------------------
95/* This is called to correct the installation when packages could not
96 be downloaded. */
97bool pkgPackageManager::FixMissing()
bdae53f1 98{
e6756cde 99 pkgDepCache::ActionGroup group(Cache);
b2e465d6 100 pkgProblemResolver Resolve(&Cache);
2fd65468 101 List->SetFileList(FileNames);
e6756cde 102
9dbb421f 103 bool Bad = false;
6c139d6e
AL
104 for (PkgIterator I = Cache.PkgBegin(); I.end() == false; I++)
105 {
2fd65468 106 if (List->IsMissing(I) == false)
9dbb421f 107 continue;
2fd65468 108
9dbb421f
AL
109 // Okay, this file is missing and we need it. Mark it for keep
110 Bad = true;
74a05226 111 Cache.MarkKeep(I, false, false);
6c139d6e 112 }
bdae53f1
AL
113
114 // We have to empty the list otherwise it will not have the new changes
115 delete List;
116 List = 0;
6c139d6e 117
9dbb421f
AL
118 if (Bad == false)
119 return true;
120
6c139d6e 121 // Now downgrade everything that is broken
30e1eab5 122 return Resolve.ResolveByKeep() == true && Cache.BrokenCount() == 0;
6c139d6e
AL
123}
124 /*}}}*/
3a6d37fd
MV
125// PM::ImmediateAdd - Add the immediate flag recursivly /*{{{*/
126// ---------------------------------------------------------------------
127/* This adds the immediate flag to the pkg and recursively to the
128 dependendies
129 */
d183f850 130void pkgPackageManager::ImmediateAdd(PkgIterator I, bool UseInstallVer, unsigned const int &Depth)
3a6d37fd
MV
131{
132 DepIterator D;
133
134 if(UseInstallVer)
135 {
136 if(Cache[I].InstallVer == 0)
137 return;
138 D = Cache[I].InstVerIter(Cache).DependsList();
139 } else {
140 if (I->CurrentVer == 0)
141 return;
142 D = I.CurrentVer().DependsList();
143 }
144
145 for ( /* nothing */ ; D.end() == false; D++)
146 if (D->Type == pkgCache::Dep::Depends || D->Type == pkgCache::Dep::PreDepends)
147 {
148 if(!List->IsFlag(D.TargetPkg(), pkgOrderList::Immediate))
149 {
150 if(Debug)
d183f850 151 clog << OutputInDepth(Depth) << "ImmediateAdd(): Adding Immediate flag to " << D.TargetPkg() << " cause of " << D.DepType() << " " << I.Name() << endl;
3a6d37fd 152 List->Flag(D.TargetPkg(),pkgOrderList::Immediate);
d183f850 153 ImmediateAdd(D.TargetPkg(), UseInstallVer, Depth + 1);
3a6d37fd
MV
154 }
155 }
156 return;
157}
158 /*}}}*/
7a1b1f8b
AL
159// PM::CreateOrderList - Create the ordering class /*{{{*/
160// ---------------------------------------------------------------------
161/* This populates the ordering list with all the packages that are
162 going to change. */
163bool pkgPackageManager::CreateOrderList()
164{
281daf46
AL
165 if (List != 0)
166 return true;
167
7a1b1f8b 168 delete List;
b2e465d6 169 List = new pkgOrderList(&Cache);
7a1b1f8b 170
b684d8c7 171 NoImmConfigure = !_config->FindB("APT::Immediate-Configure",true);
e2a5ff0c 172 ImmConfigureAll = _config->FindB("APT::Immediate-Configure-All",false);
a6c8798a
CB
173
174 if (Debug && ImmConfigureAll)
175 clog << "CreateOrderList(): Adding Immediate flag for all packages because of APT::Immediate-Configure-All" << endl;
079cc404 176
7a1b1f8b
AL
177 // Generate the list of affected packages and sort it
178 for (PkgIterator I = Cache.PkgBegin(); I.end() == false; I++)
179 {
e7b470ee
AL
180 // Ignore no-version packages
181 if (I->VersionList == 0)
182 continue;
183
138d4b3d 184 // Mark the package and its dependends for immediate configuration
a6c8798a 185 if ((((I->Flags & pkgCache::Flag::Essential) == pkgCache::Flag::Essential ||
138d4b3d 186 (I->Flags & pkgCache::Flag::Important) == pkgCache::Flag::Important) &&
a6c8798a 187 NoImmConfigure == false) || ImmConfigureAll)
7a1b1f8b 188 {
a6c8798a 189 if(Debug && !ImmConfigureAll)
3a6d37fd 190 clog << "CreateOrderList(): Adding Immediate flag for " << I.Name() << endl;
7a1b1f8b 191 List->Flag(I,pkgOrderList::Immediate);
a6c8798a
CB
192
193 if (!ImmConfigureAll) {
a6c8798a
CB
194 // Look for other install packages to make immediate configurea
195 ImmediateAdd(I, true);
e2a5ff0c 196
a6c8798a
CB
197 // And again with the current version.
198 ImmediateAdd(I, false);
199 }
7a1b1f8b
AL
200 }
201
202 // Not interesting
203 if ((Cache[I].Keep() == true ||
204 Cache[I].InstVerIter(Cache) == I.CurrentVer()) &&
d556d1a1 205 I.State() == pkgCache::PkgIterator::NeedsNothing &&
d0c59649 206 (Cache[I].iFlags & pkgDepCache::ReInstall) != pkgDepCache::ReInstall &&
d556d1a1
AL
207 (I.Purge() != false || Cache[I].Mode != pkgDepCache::ModeDelete ||
208 (Cache[I].iFlags & pkgDepCache::Purge) != pkgDepCache::Purge))
7a1b1f8b
AL
209 continue;
210
211 // Append it to the list
138d4b3d 212 List->push_back(I);
7a1b1f8b
AL
213 }
214
215 return true;
216}
217 /*}}}*/
6c139d6e
AL
218// PM::DepAlwaysTrue - Returns true if this dep is irrelevent /*{{{*/
219// ---------------------------------------------------------------------
220/* The restriction on provides is to eliminate the case when provides
221 are transitioning between valid states [ie exim to smail] */
222bool pkgPackageManager::DepAlwaysTrue(DepIterator D)
223{
224 if (D.TargetPkg()->ProvidesList != 0)
225 return false;
226
227 if ((Cache[D] & pkgDepCache::DepInstall) != 0 &&
228 (Cache[D] & pkgDepCache::DepNow) != 0)
229 return true;
230 return false;
231}
232 /*}}}*/
233// PM::CheckRConflicts - Look for reverse conflicts /*{{{*/
234// ---------------------------------------------------------------------
235/* This looks over the reverses for a conflicts line that needs early
236 removal. */
237bool pkgPackageManager::CheckRConflicts(PkgIterator Pkg,DepIterator D,
238 const char *Ver)
239{
240 for (;D.end() == false; D++)
241 {
b2e465d6
AL
242 if (D->Type != pkgCache::Dep::Conflicts &&
243 D->Type != pkgCache::Dep::Obsoletes)
6c139d6e 244 continue;
5af32db6
AL
245
246 // The package hasnt been changed
247 if (List->IsNow(Pkg) == false)
248 continue;
6c139d6e 249
5af32db6
AL
250 // Ignore self conflicts, ignore conflicts from irrelevent versions
251 if (D.ParentPkg() == Pkg || D.ParentVer() != D.ParentPkg().CurrentVer())
6c139d6e
AL
252 continue;
253
b2e465d6 254 if (Cache.VS().CheckDep(Ver,D->CompareOp,D.TargetVer()) == false)
6c139d6e 255 continue;
b2e465d6 256
6c139d6e 257 if (EarlyRemove(D.ParentPkg()) == false)
5af32db6
AL
258 return _error->Error("Reverse conflicts early remove for package '%s' failed",
259 Pkg.Name());
260 }
6c139d6e
AL
261 return true;
262}
263 /*}}}*/
264// PM::ConfigureAll - Run the all out configuration /*{{{*/
265// ---------------------------------------------------------------------
266/* This configures every package. It is assumed they are all unpacked and
590f1923
CB
267 that the final configuration is valid. This is also used to catch packages
268 that have not been configured when using ImmConfigureAll */
6c139d6e
AL
269bool pkgPackageManager::ConfigureAll()
270{
b2e465d6 271 pkgOrderList OList(&Cache);
6c139d6e
AL
272
273 // Populate the order list
274 for (pkgOrderList::iterator I = List->begin(); I != List->end(); I++)
275 if (List->IsFlag(pkgCache::PkgIterator(Cache,*I),
276 pkgOrderList::UnPacked) == true)
277 OList.push_back(*I);
278
279 if (OList.OrderConfigure() == false)
280 return false;
5e312de7
DK
281
282 std::string const conf = _config->Find("PackageManager::Configure","all");
283 bool const ConfigurePkgs = (conf == "all");
284
6c139d6e
AL
285 // Perform the configuring
286 for (pkgOrderList::iterator I = OList.begin(); I != OList.end(); I++)
287 {
288 PkgIterator Pkg(Cache,*I);
17182c0c
CB
289
290 /* Check if the package has been configured, this can happen if SmartConfigure
291 calls its self */
292 if (List->IsFlag(Pkg,pkgOrderList::Configured)) continue;
803ea2a8 293
d41d0e01 294 if (ConfigurePkgs == true && SmartConfigure(Pkg, 0) == false) {
c7c7d3e8
CB
295 if (ImmConfigureAll)
296 _error->Error(_("Could not perform immediate configuration on '%s'. "
297 "Please see man 5 apt.conf under APT::Immediate-Configure for details. (%d)"),Pkg.Name(),1);
298 else
299 _error->Error("Internal error, packages left unconfigured. %s",Pkg.Name());
6c139d6e 300 return false;
634985f8 301 }
6c139d6e
AL
302
303 List->Flag(Pkg,pkgOrderList::Configured,pkgOrderList::States);
304 }
305
306 return true;
307}
308 /*}}}*/
309// PM::SmartConfigure - Perform immediate configuration of the pkg /*{{{*/
310// ---------------------------------------------------------------------
c7c7d3e8
CB
311/* This function tries to put the system in a state where Pkg can be configured.
312 This involves checking each of Pkg's dependanies and unpacking and
313 configuring packages where needed.
314
315 Note on failure: This method can fail, without causing any problems.
316 This can happen when using Immediate-Configure-All, SmartUnPack may call
317 SmartConfigure, it may fail because of a complex dependancy situation, but
318 a error will only be reported if ConfigureAll fails. This is why some of the
319 messages this function reports on failure (return false;) as just warnings
320 only shown when debuging*/
d41d0e01 321bool pkgPackageManager::SmartConfigure(PkgIterator Pkg, int const Depth)
6c139d6e 322{
d41d0e01
CB
323 // If this is true, only check and correct and dependancies without the Loop flag
324 bool PkgLoop = List->IsFlag(Pkg,pkgOrderList::Loop);
325
987d8d03
CB
326 if (Debug) {
327 VerIterator InstallVer = VerIterator(Cache,Cache[Pkg].InstallVer);
d41d0e01
CB
328 clog << OutputInDepth(Depth) << "SmartConfigure " << Pkg.Name() << " (" << InstallVer.VerStr() << ")";
329 if (PkgLoop)
330 clog << " (Only Correct Dependancies)";
331 clog << endl;
987d8d03 332 }
a99d02a8 333
590f1923 334 VerIterator const instVer = Cache[Pkg].InstVerIter(Cache);
b2e465d6 335
590f1923 336 /* Because of the ordered list, most dependancies should be unpacked,
c7c7d3e8
CB
337 however if there is a loop (A depends on B, B depends on A) this will not
338 be the case, so check for dependancies before configuring. */
6c139d6e 339 bool Bad = false;
590f1923
CB
340 for (DepIterator D = instVer.DependsList();
341 D.end() == false; )
6c139d6e 342 {
590f1923
CB
343 // Compute a single dependency element (glob or)
344 pkgCache::DepIterator Start;
345 pkgCache::DepIterator End;
346 D.GlobOr(Start,End);
6c139d6e 347
590f1923
CB
348 if (End->Type == pkgCache::Dep::Depends)
349 Bad = true;
6c139d6e 350
590f1923
CB
351 // Check for dependanices that have not been unpacked, probably due to loops.
352 while (End->Type == pkgCache::Dep::Depends) {
353 PkgIterator DepPkg;
354 VerIterator InstallVer;
355 SPtrArray<Version *> VList = Start.AllTargets();
356
c7c7d3e8 357 // Check through each version of each package that could satisfy this dependancy
590f1923 358 for (Version **I = VList; *I != 0; I++) {
6c139d6e 359 VerIterator Ver(Cache,*I);
590f1923 360 DepPkg = Ver.ParentPkg();
590f1923 361 InstallVer = VerIterator(Cache,Cache[DepPkg].InstallVer);
c7c7d3e8
CB
362
363 // Check if the current version of the package is avalible and will satisfy this dependancy
590f1923 364 if (DepPkg.CurrentVer() == Ver && List->IsNow(DepPkg) == true &&
b9f66879 365 !List->IsFlag(DepPkg,pkgOrderList::Removed) && DepPkg.State() == PkgIterator::NeedsNothing)
6c139d6e
AL
366 {
367 Bad = false;
c7c7d3e8 368 break;
6c139d6e 369 }
a6c8798a 370
c7c7d3e8 371 // Check if the version that is going to be installed will satisfy the dependancy
590f1923
CB
372 if (Cache[DepPkg].InstallVer == *I) {
373 if (List->IsFlag(DepPkg,pkgOrderList::UnPacked)) {
b57257d2 374 if (List->IsFlag(DepPkg,pkgOrderList::Loop) && PkgLoop) {
a99d02a8
CB
375 // This dependancy has already been dealt with by another SmartConfigure on Pkg
376 Bad = false;
377 break;
b57257d2
CB
378 } else if (List->IsFlag(Pkg,pkgOrderList::Loop)) {
379 /* Check for a loop to prevent one forming
380 If A depends on B and B depends on A, SmartConfigure will
381 just hop between them if this is not checked. Dont remove the
382 loop flag after finishing however as loop is already set.
383 This means that there is another SmartConfigure call for this
384 package and it will remove the loop flag */
385 Bad = !SmartConfigure(DepPkg, Depth + 1);
386 } else {
387 /* Check for a loop to prevent one forming
388 If A depends on B and B depends on A, SmartConfigure will
389 just hop between them if this is not checked */
390 List->Flag(Pkg,pkgOrderList::Loop);
391 Bad = !SmartConfigure(DepPkg, Depth + 1);
392 List->RmFlag(Pkg,pkgOrderList::Loop);
a99d02a8 393 }
a99d02a8 394 // If SmartConfigure was succesfull, Bad is false, so break
a99d02a8 395 if (!Bad) break;
590f1923
CB
396 } else if (List->IsFlag(DepPkg,pkgOrderList::Configured)) {
397 Bad = false;
c7c7d3e8 398 break;
590f1923 399 }
590f1923 400 }
6c139d6e 401 }
590f1923 402
c7c7d3e8 403 /* If the dependany is still not satisfied, try, if possible, unpacking a package to satisfy it */
590f1923
CB
404 if (InstallVer != 0 && Bad) {
405 Bad = false;
165ff1df 406 if (List->IsNow(DepPkg) && !List->IsFlag(DepPkg,pkgOrderList::Loop)) {
cbea0578 407 List->Flag(Pkg,pkgOrderList::Loop);
590f1923 408 if (Debug)
d41d0e01
CB
409 cout << OutputInDepth(Depth) << "Unpacking " << DepPkg.Name() << " to avoid loop" << endl;
410 SmartUnPack(DepPkg, true, Depth + 1);
e844b947 411 List->RmFlag(Pkg,pkgOrderList::Loop);
590f1923
CB
412 }
413 }
414
415 if (Start==End) {
416 if (Bad && Debug) {
417 if (!List->IsFlag(DepPkg,pkgOrderList::Loop)) {
418 _error->Warning("Could not satisfy dependancies for %s",Pkg.Name());
419 }
420 }
421 break;
590f1923
CB
422 } else {
423 Start++;
424 }
6c139d6e 425 }
590f1923 426 }
c7c7d3e8
CB
427
428 if (Bad) {
429 if (Debug)
430 _error->Warning(_("Could not configure '%s'. "),Pkg.Name());
431 return false;
432 }
a99d02a8
CB
433
434 if (PkgLoop) return true;
590f1923
CB
435
436 static std::string const conf = _config->Find("PackageManager::Configure","all");
437 static bool const ConfigurePkgs = (conf == "all" || conf == "smart");
438
17182c0c
CB
439 if (List->IsFlag(Pkg,pkgOrderList::Configured))
440 return _error->Error("Internal configure error on '%s'. ",Pkg.Name(),1);
441
590f1923
CB
442 if (ConfigurePkgs == true && Configure(Pkg) == false)
443 return false;
6c139d6e 444
590f1923
CB
445 List->Flag(Pkg,pkgOrderList::Configured,pkgOrderList::States);
446
447 if (Cache[Pkg].InstVerIter(Cache)->MultiArch == pkgCache::Version::Same)
448 for (PkgIterator P = Pkg.Group().PackageList();
449 P.end() == false; P = Pkg.Group().NextPkg(P))
6c139d6e 450 {
590f1923
CB
451 if (Pkg == P || List->IsFlag(P,pkgOrderList::Configured) == true ||
452 Cache[P].InstallVer == 0 || (P.CurrentVer() == Cache[P].InstallVer &&
453 (Cache[Pkg].iFlags & pkgDepCache::ReInstall) != pkgDepCache::ReInstall))
454 continue;
d41d0e01 455 SmartConfigure(P, (Depth +1));
6c139d6e 456 }
590f1923
CB
457
458 // Sanity Check
c7c7d3e8
CB
459 if (List->IsFlag(Pkg,pkgOrderList::Configured) == false)
460 return _error->Error(_("Could not configure '%s'. "),Pkg.Name());
590f1923 461
6c139d6e
AL
462 return true;
463}
464 /*}}}*/
465// PM::EarlyRemove - Perform removal of packages before their time /*{{{*/
466// ---------------------------------------------------------------------
467/* This is called to deal with conflicts arising from unpacking */
468bool pkgPackageManager::EarlyRemove(PkgIterator Pkg)
469{
470 if (List->IsNow(Pkg) == false)
471 return true;
472
473 // Already removed it
474 if (List->IsFlag(Pkg,pkgOrderList::Removed) == true)
475 return true;
476
477 // Woops, it will not be re-installed!
478 if (List->IsFlag(Pkg,pkgOrderList::InList) == false)
479 return false;
9d4c8f67
AL
480
481 // Essential packages get special treatment
5af32db6 482 bool IsEssential = false;
9d4c8f67 483 if ((Pkg->Flags & pkgCache::Flag::Essential) != 0)
5af32db6
AL
484 IsEssential = true;
485
486 /* Check for packages that are the dependents of essential packages and
487 promote them too */
488 if (Pkg->CurrentVer != 0)
489 {
490 for (DepIterator D = Pkg.RevDependsList(); D.end() == false &&
491 IsEssential == false; D++)
492 if (D->Type == pkgCache::Dep::Depends || D->Type == pkgCache::Dep::PreDepends)
493 if ((D.ParentPkg()->Flags & pkgCache::Flag::Essential) != 0)
494 IsEssential = true;
495 }
496
497 if (IsEssential == true)
9d4c8f67
AL
498 {
499 if (_config->FindB("APT::Force-LoopBreak",false) == false)
b2e465d6
AL
500 return _error->Error(_("This installation run will require temporarily "
501 "removing the essential package %s due to a "
502 "Conflicts/Pre-Depends loop. This is often bad, "
503 "but if you really want to do it, activate the "
504 "APT::Force-LoopBreak option."),Pkg.Name());
9d4c8f67 505 }
6c139d6e
AL
506
507 bool Res = SmartRemove(Pkg);
508 if (Cache[Pkg].Delete() == false)
509 List->Flag(Pkg,pkgOrderList::Removed,pkgOrderList::States);
510
511 return Res;
512}
513 /*}}}*/
514// PM::SmartRemove - Removal Helper /*{{{*/
515// ---------------------------------------------------------------------
516/* */
517bool pkgPackageManager::SmartRemove(PkgIterator Pkg)
518{
519 if (List->IsNow(Pkg) == false)
520 return true;
521
522 List->Flag(Pkg,pkgOrderList::Configured,pkgOrderList::States);
803ea2a8 523
28166356 524 return Remove(Pkg,(Cache[Pkg].iFlags & pkgDepCache::Purge) == pkgDepCache::Purge);
803ea2a8 525 return true;
6c139d6e
AL
526}
527 /*}}}*/
528// PM::SmartUnPack - Install helper /*{{{*/
529// ---------------------------------------------------------------------
590f1923
CB
530/* This puts the system in a state where it can Unpack Pkg, if Pkg is allready
531 unpacked, or when it has been unpacked, if Immediate==true it configures it. */
6c139d6e 532bool pkgPackageManager::SmartUnPack(PkgIterator Pkg)
d77b985a 533{
d41d0e01 534 return SmartUnPack(Pkg, true, 0);
d77b985a 535}
d41d0e01 536bool pkgPackageManager::SmartUnPack(PkgIterator Pkg, bool const Immediate, int const Depth)
6c139d6e 537{
d41d0e01
CB
538 bool PkgLoop = List->IsFlag(Pkg,pkgOrderList::Loop);
539
987d8d03 540 if (Debug) {
d41d0e01 541 clog << OutputInDepth(Depth) << "SmartUnPack " << Pkg.Name();
987d8d03
CB
542 VerIterator InstallVer = VerIterator(Cache,Cache[Pkg].InstallVer);
543 if (Pkg.CurrentVer() == 0)
d41d0e01 544 cout << " (install version " << InstallVer.VerStr() << ")";
987d8d03 545 else
d41d0e01
CB
546 cout << " (replace version " << Pkg.CurrentVer().VerStr() << " with " << InstallVer.VerStr() << ")";
547 if (PkgLoop)
548 cout << " (Only Perform PreUnpack Checks)";
549 cout << endl;
987d8d03 550 }
cfcdf7fe 551
6c139d6e
AL
552 // Check if it is already unpacked
553 if (Pkg.State() == pkgCache::PkgIterator::NeedsConfigure &&
554 Cache[Pkg].Keep() == true)
555 {
d41d0e01 556 cout << OutputInDepth(Depth) << "SmartUnPack called on Package " << Pkg.Name() << " but its unpacked" << endl;
940f2160 557 return false;
6c139d6e 558 }
940f2160 559
d77b985a
DK
560 VerIterator const instVer = Cache[Pkg].InstVerIter(Cache);
561
c7c7d3e8
CB
562 /* PreUnpack Checks: This loop checks and attempts to rectify and problems that would prevent the package being unpacked.
563 It addresses: PreDepends, Conflicts, Obsoletes and Breaks (DpkgBreaks). Any resolutions that do not require it should
564 avoid configuration (calling SmartUnpack with Immediate=true), this is because when unpacking some packages with
565 complex dependancy structures, trying to configure some packages while breaking the loops can complicate things .
566 This will be either dealt with if the package is configured as a dependency of Pkg (if and when Pkg is configured),
567 or by the ConfigureAll call at the end of the for loop in OrderInstall. */
d77b985a 568 for (DepIterator D = instVer.DependsList();
421c8d10 569 D.end() == false; )
6c139d6e 570 {
421c8d10
AL
571 // Compute a single dependency element (glob or)
572 pkgCache::DepIterator Start;
573 pkgCache::DepIterator End;
574 D.GlobOr(Start,End);
575
576 while (End->Type == pkgCache::Dep::PreDepends)
6c139d6e 577 {
9fc57a59 578 if (Debug)
d41d0e01 579 clog << OutputInDepth(Depth) << "PreDepends order for " << Pkg.Name() << std::endl;
f4945db3 580
6c139d6e 581 // Look for possible ok targets.
b2e465d6 582 SPtrArray<Version *> VList = Start.AllTargets();
6c139d6e
AL
583 bool Bad = true;
584 for (Version **I = VList; *I != 0 && Bad == true; I++)
585 {
586 VerIterator Ver(Cache,*I);
587 PkgIterator Pkg = Ver.ParentPkg();
588
589 // See if the current version is ok
590 if (Pkg.CurrentVer() == Ver && List->IsNow(Pkg) == true &&
591 Pkg.State() == PkgIterator::NeedsNothing)
592 {
593 Bad = false;
9fc57a59 594 if (Debug)
d41d0e01 595 clog << OutputInDepth(Depth) << "Found ok package " << Pkg.Name() << endl;
6c139d6e
AL
596 continue;
597 }
598 }
599
600 // Look for something that could be configured.
601 for (Version **I = VList; *I != 0 && Bad == true; I++)
602 {
603 VerIterator Ver(Cache,*I);
604 PkgIterator Pkg = Ver.ParentPkg();
605
606 // Not the install version
607 if (Cache[Pkg].InstallVer != *I ||
608 (Cache[Pkg].Keep() == true && Pkg.State() == PkgIterator::NeedsNothing))
609 continue;
17182c0c
CB
610
611 if (List->IsFlag(Pkg,pkgOrderList::Configured)) {
612 Bad = false;
613 continue;
614 }
6c139d6e 615
9fc57a59 616 if (Debug)
d41d0e01
CB
617 clog << OutputInDepth(Depth) << "Trying to SmartConfigure " << Pkg.Name() << endl;
618 Bad = !SmartConfigure(Pkg, Depth + 1);
6c139d6e 619 }
1006601e 620
421c8d10 621 /* If this or element did not match then continue on to the
1006601e 622 next or element until a matching element is found */
421c8d10 623 if (Bad == true)
1006601e
AL
624 {
625 // This triggers if someone make a pre-depends/depend loop.
421c8d10 626 if (Start == End)
1006601e
AL
627 return _error->Error("Couldn't configure pre-depend %s for %s, "
628 "probably a dependency cycle.",
629 End.TargetPkg().Name(),Pkg.Name());
421c8d10
AL
630 Start++;
631 }
a6c8798a 632 else
421c8d10 633 break;
6c139d6e
AL
634 }
635
b2e465d6
AL
636 if (End->Type == pkgCache::Dep::Conflicts ||
637 End->Type == pkgCache::Dep::Obsoletes)
6c139d6e
AL
638 {
639 /* Look for conflicts. Two packages that are both in the install
640 state cannot conflict so we don't check.. */
b2e465d6 641 SPtrArray<Version *> VList = End.AllTargets();
6c139d6e
AL
642 for (Version **I = VList; *I != 0; I++)
643 {
644 VerIterator Ver(Cache,*I);
9fc57a59
CB
645 PkgIterator ConflictPkg = Ver.ParentPkg();
646 VerIterator InstallVer(Cache,Cache[ConflictPkg].InstallVer);
647
e2a5ff0c 648 // See if the current version is conflicting
b9f66879 649 if (ConflictPkg.CurrentVer() == Ver && List->IsNow(ConflictPkg))
cbea0578 650 {
d41d0e01 651 cout << OutputInDepth(Depth) << Pkg.Name() << " conflicts with " << ConflictPkg.Name() << endl;
c7c7d3e8
CB
652 /* If a loop is not present or has not yet been detected, attempt to unpack packages
653 to resolve this conflict. If there is a loop present, remove packages to resolve this conflict */
e2a5ff0c
CB
654 if (!List->IsFlag(ConflictPkg,pkgOrderList::Loop)) {
655 if (Cache[ConflictPkg].Keep() == 0 && Cache[ConflictPkg].InstallVer != 0) {
b9f66879 656 if (Debug)
d41d0e01 657 cout << OutputInDepth(Depth) << OutputInDepth(Depth) << "Unpacking " << ConflictPkg.Name() << " to prevent conflict" << endl;
cbea0578 658 List->Flag(Pkg,pkgOrderList::Loop);
d41d0e01 659 SmartUnPack(ConflictPkg,false, Depth + 1);
cbea0578
CB
660 // Remove loop to allow it to be used later if needed
661 List->RmFlag(Pkg,pkgOrderList::Loop);
e2a5ff0c
CB
662 } else {
663 if (EarlyRemove(ConflictPkg) == false)
664 return _error->Error("Internal Error, Could not early remove %s",ConflictPkg.Name());
665 }
666 } else {
667 if (!List->IsFlag(ConflictPkg,pkgOrderList::Removed)) {
b9f66879 668 if (Debug)
d41d0e01 669 cout << OutputInDepth(Depth) << "Because of conficts knot, removing " << ConflictPkg.Name() << " to conflict violation" << endl;
e2a5ff0c
CB
670 if (EarlyRemove(ConflictPkg) == false)
671 return _error->Error("Internal Error, Could not early remove %s",ConflictPkg.Name());
672 }
673 }
6c139d6e
AL
674 }
675 }
6c139d6e 676 }
cfcdf7fe
CB
677
678 // Check for breaks
679 if (End->Type == pkgCache::Dep::DpkgBreaks) {
680 SPtrArray<Version *> VList = End.AllTargets();
681 for (Version **I = VList; *I != 0; I++)
682 {
683 VerIterator Ver(Cache,*I);
8b1f5756 684 PkgIterator BrokenPkg = Ver.ParentPkg();
e2a5ff0c
CB
685 VerIterator InstallVer(Cache,Cache[BrokenPkg].InstallVer);
686
55c04aa4 687 // Check if it needs to be unpacked
8b1f5756 688 if (List->IsFlag(BrokenPkg,pkgOrderList::InList) && Cache[BrokenPkg].Delete() == false &&
940f2160 689 List->IsNow(BrokenPkg)) {
b57257d2 690 if (List->IsFlag(BrokenPkg,pkgOrderList::Loop) && PkgLoop) {
940f2160
CB
691 // This dependancy has already been dealt with by another SmartUnPack on Pkg
692 break;
b57257d2
CB
693 } else if (List->IsFlag(Pkg,pkgOrderList::Loop)) {
694 /* Found a break, so unpack the package, but dont remove loop as already set.
695 This means that there is another SmartUnPack call for this
696 package and it will remove the loop flag. */
697 if (Debug)
698 cout << OutputInDepth(Depth) << " Unpacking " << BrokenPkg.Name() << " to avoid break" << endl;
699
700 SmartUnPack(BrokenPkg, false, Depth + 1);
701 } else {
702 List->Flag(Pkg,pkgOrderList::Loop);
703 // Found a break, so unpack the package
704 if (Debug)
705 cout << OutputInDepth(Depth) << " Unpacking " << BrokenPkg.Name() << " to avoid break" << endl;
c7c7d3e8 706
b57257d2
CB
707 SmartUnPack(BrokenPkg, false, Depth + 1);
708 List->RmFlag(Pkg,pkgOrderList::Loop);
709 }
cfcdf7fe 710 }
b57257d2 711
9fc57a59 712 // Check if a package needs to be removed
e2a5ff0c 713 if (Cache[BrokenPkg].Delete() == true && !List->IsFlag(BrokenPkg,pkgOrderList::Configured)) {
9fc57a59 714 if (Debug)
d41d0e01 715 cout << OutputInDepth(Depth) << " Removing " << BrokenPkg.Name() << " to avoid break" << endl;
9fc57a59
CB
716 SmartRemove(BrokenPkg);
717 }
cfcdf7fe
CB
718 }
719 }
9fc57a59
CB
720 }
721
c7c7d3e8
CB
722 // Check for reverse conflicts.
723 if (CheckRConflicts(Pkg,Pkg.RevDependsList(),
9fc57a59 724 instVer.VerStr()) == false)
c7c7d3e8 725 return false;
9fc57a59 726
c7c7d3e8 727 for (PrvIterator P = instVer.ProvidesList();
9fc57a59
CB
728 P.end() == false; P++)
729 CheckRConflicts(Pkg,P.ParentPkg().RevDependsList(),P.ProvideVersion());
730
940f2160
CB
731 if (PkgLoop) return true;
732
c7c7d3e8 733 List->Flag(Pkg,pkgOrderList::UnPacked,pkgOrderList::States);
9fc57a59 734
c7c7d3e8
CB
735 if (instVer->MultiArch == pkgCache::Version::Same)
736 for (PkgIterator P = Pkg.Group().PackageList();
737 P.end() == false; P = Pkg.Group().NextPkg(P))
738 {
739 if (Pkg == P || List->IsFlag(P,pkgOrderList::UnPacked) == true ||
9fc57a59
CB
740 Cache[P].InstallVer == 0 || (P.CurrentVer() == Cache[P].InstallVer &&
741 (Cache[Pkg].iFlags & pkgDepCache::ReInstall) != pkgDepCache::ReInstall))
c7c7d3e8 742 continue;
d41d0e01 743 SmartUnPack(P, false, Depth + 1);
c7c7d3e8 744 }
9fc57a59
CB
745
746 if(Install(Pkg,FileNames[Pkg->ID]) == false)
747 return false;
748
d41d0e01 749 if (Immediate == true) {
590f1923 750 // Perform immedate configuration of the package.
d41d0e01 751 if (SmartConfigure(Pkg, Depth + 1) == false)
590f1923
CB
752 _error->Warning(_("Could not perform immediate configuration on '%s'. "
753 "Please see man 5 apt.conf under APT::Immediate-Configure for details. (%d)"),Pkg.Name(),2);
754 }
6c139d6e
AL
755
756 return true;
757}
758 /*}}}*/
759// PM::OrderInstall - Installation ordering routine /*{{{*/
760// ---------------------------------------------------------------------
761/* */
281daf46 762pkgPackageManager::OrderResult pkgPackageManager::OrderInstall()
6c139d6e 763{
7a1b1f8b 764 if (CreateOrderList() == false)
281daf46
AL
765 return Failed;
766
767 Reset();
6c139d6e 768
30e1eab5 769 if (Debug == true)
5e312de7 770 clog << "Beginning to order" << endl;
6c139d6e 771
5e312de7
DK
772 bool const ordering =
773 _config->FindB("PackageManager::UnpackAll",true) ?
774 List->OrderUnpack(FileNames) : List->OrderCritical();
775 if (ordering == false)
281daf46
AL
776 {
777 _error->Error("Internal ordering error");
778 return Failed;
779 }
780
30e1eab5
AL
781 if (Debug == true)
782 clog << "Done ordering" << endl;
783
281daf46 784 bool DoneSomething = false;
6c139d6e
AL
785 for (pkgOrderList::iterator I = List->begin(); I != List->end(); I++)
786 {
787 PkgIterator Pkg(Cache,*I);
e2a5ff0c 788
281daf46
AL
789 if (List->IsNow(Pkg) == false)
790 {
b684d8c7 791 if (!List->IsFlag(Pkg,pkgOrderList::Configured) && !NoImmConfigure) {
d41d0e01 792 if (SmartConfigure(Pkg, 0) == false && Debug)
634985f8
CB
793 _error->Warning("Internal Error, Could not configure %s",Pkg.Name());
794 // FIXME: The above warning message might need changing
b684d8c7 795 } else {
634985f8
CB
796 if (Debug == true)
797 clog << "Skipping already done " << Pkg.Name() << endl;
798 }
281daf46 799 continue;
634985f8 800
281daf46
AL
801 }
802
2fd65468 803 if (List->IsMissing(Pkg) == true)
281daf46
AL
804 {
805 if (Debug == true)
a3eaf954 806 clog << "Sequence completed at " << Pkg.Name() << endl;
281daf46
AL
807 if (DoneSomething == false)
808 {
809 _error->Error("Internal Error, ordering was unable to handle the media swap");
810 return Failed;
811 }
812 return Incomplete;
813 }
6c139d6e
AL
814
815 // Sanity check
d0c59649
AL
816 if (Cache[Pkg].Keep() == true &&
817 Pkg.State() == pkgCache::PkgIterator::NeedsNothing &&
818 (Cache[Pkg].iFlags & pkgDepCache::ReInstall) != pkgDepCache::ReInstall)
281daf46 819 {
71a174ee 820 _error->Error("Internal Error, trying to manipulate a kept package (%s)",Pkg.Name());
281daf46
AL
821 return Failed;
822 }
6c139d6e
AL
823
824 // Perform a delete or an install
825 if (Cache[Pkg].Delete() == true)
826 {
827 if (SmartRemove(Pkg) == false)
281daf46 828 return Failed;
6c139d6e
AL
829 }
830 else
d41d0e01 831 if (SmartUnPack(Pkg,List->IsFlag(Pkg,pkgOrderList::Immediate),0) == false)
281daf46
AL
832 return Failed;
833 DoneSomething = true;
590f1923
CB
834
835 if (ImmConfigureAll) {
836 /* ConfigureAll here to pick up and packages left unconfigured becuase they were unpacked in the
837 "PreUnpack Checks" section */
c7c7d3e8
CB
838 if (!ConfigureAll())
839 return Failed;
590f1923 840 }
6c139d6e 841 }
5e312de7 842
6c139d6e
AL
843 // Final run through the configure phase
844 if (ConfigureAll() == false)
281daf46 845 return Failed;
6c139d6e
AL
846
847 // Sanity check
848 for (pkgOrderList::iterator I = List->begin(); I != List->end(); I++)
281daf46 849 {
6c139d6e 850 if (List->IsFlag(*I,pkgOrderList::Configured) == false)
281daf46
AL
851 {
852 _error->Error("Internal error, packages left unconfigured. %s",
853 PkgIterator(Cache,*I).Name());
854 return Failed;
855 }
9fc57a59 856 }
281daf46
AL
857
858 return Completed;
6c139d6e
AL
859}
860 /*}}}*/
1d6386f3
MV
861// PM::DoInstallPostFork - Does install part that happens after the fork /*{{{*/
862// ---------------------------------------------------------------------
863pkgPackageManager::OrderResult
864pkgPackageManager::DoInstallPostFork(int statusFd)
865{
866 if(statusFd > 0)
867 // FIXME: use SetCloseExec here once it taught about throwing
868 // exceptions instead of doing _exit(100) on failure
869 fcntl(statusFd,F_SETFD,FD_CLOEXEC);
870 bool goResult = Go(statusFd);
871 if(goResult == false)
872 return Failed;
873
1d6386f3
MV
874 return Res;
875};
876
2a7e07c7
MV
877// PM::DoInstall - Does the installation /*{{{*/
878// ---------------------------------------------------------------------
879/* This uses the filenames in FileNames and the information in the
880 DepCache to perform the installation of packages.*/
881pkgPackageManager::OrderResult pkgPackageManager::DoInstall(int statusFd)
882{
883 if(DoInstallPreFork() == Failed)
884 return Failed;
885
7230ad48 886 return DoInstallPostFork(statusFd);
2a7e07c7 887}
eef71338 888 /*}}}*/