Ensure we have a Policy in CacheFile.BuildDepCache()
[ntk/apt.git] / apt-private / private-cachefile.cc
1 // Include files /*{{{*/
2 #include<config.h>
3
4 #include <apt-pkg/algorithms.h>
5 #include <apt-pkg/upgrade.h>
6 #include <apt-pkg/error.h>
7 #include <apt-pkg/configuration.h>
8 #include <apt-pkg/depcache.h>
9 #include <apt-pkg/pkgcache.h>
10 #include <apt-pkg/cacheiterators.h>
11
12 #include <apt-private/private-output.h>
13 #include <apt-private/private-cachefile.h>
14
15 #include <string.h>
16 #include <ostream>
17 #include <cstdlib>
18
19 #include <apti18n.h>
20 /*}}}*/
21
22 using namespace std;
23
24 // CacheFile::NameComp - QSort compare by name /*{{{*/
25 // ---------------------------------------------------------------------
26 /* */
27 pkgCache *CacheFile::SortCache = 0;
28 int CacheFile::NameComp(const void *a,const void *b)
29 {
30 if (*(pkgCache::Package **)a == 0 || *(pkgCache::Package **)b == 0)
31 return *(pkgCache::Package **)a - *(pkgCache::Package **)b;
32
33 const pkgCache::Package &A = **(pkgCache::Package **)a;
34 const pkgCache::Package &B = **(pkgCache::Package **)b;
35
36 return strcmp(SortCache->StrP + A.Name,SortCache->StrP + B.Name);
37 }
38 /*}}}*/
39 // CacheFile::Sort - Sort by name /*{{{*/
40 // ---------------------------------------------------------------------
41 /* */
42 void CacheFile::Sort()
43 {
44 delete [] List;
45 List = new pkgCache::Package *[Cache->Head().PackageCount];
46 memset(List,0,sizeof(*List)*Cache->Head().PackageCount);
47 pkgCache::PkgIterator I = Cache->PkgBegin();
48 for (;I.end() != true; ++I)
49 List[I->ID] = I;
50
51 SortCache = *this;
52 qsort(List,Cache->Head().PackageCount,sizeof(*List),NameComp);
53 }
54 /*}}}*/
55 // CacheFile::CheckDeps - Open the cache file /*{{{*/
56 // ---------------------------------------------------------------------
57 /* This routine generates the caches and then opens the dependency cache
58 and verifies that the system is OK. */
59 bool CacheFile::CheckDeps(bool AllowBroken)
60 {
61 bool FixBroken = _config->FindB("APT::Get::Fix-Broken",false);
62
63 if (_error->PendingError() == true)
64 return false;
65
66 // Check that the system is OK
67 if (DCache->DelCount() != 0 || DCache->InstCount() != 0)
68 return _error->Error("Internal error, non-zero counts");
69
70 // Apply corrections for half-installed packages
71 if (pkgApplyStatus(*DCache) == false)
72 return false;
73
74 if (_config->FindB("APT::Get::Fix-Policy-Broken",false) == true)
75 {
76 FixBroken = true;
77 if ((DCache->PolicyBrokenCount() > 0))
78 {
79 // upgrade all policy-broken packages with ForceImportantDeps=True
80 for (pkgCache::PkgIterator I = Cache->PkgBegin(); !I.end(); ++I)
81 if ((*DCache)[I].NowPolicyBroken() == true)
82 DCache->MarkInstall(I,true,0, false, true);
83 }
84 }
85
86 // Nothing is broken
87 if (DCache->BrokenCount() == 0 || AllowBroken == true)
88 return true;
89
90 // Attempt to fix broken things
91 if (FixBroken == true)
92 {
93 c1out << _("Correcting dependencies...") << flush;
94 if (pkgFixBroken(*DCache) == false || DCache->BrokenCount() != 0)
95 {
96 c1out << _(" failed.") << endl;
97 ShowBroken(c1out,*this,true);
98
99 return _error->Error(_("Unable to correct dependencies"));
100 }
101 if (pkgMinimizeUpgrade(*DCache) == false)
102 return _error->Error(_("Unable to minimize the upgrade set"));
103
104 c1out << _(" Done") << endl;
105 }
106 else
107 {
108 c1out << _("You might want to run 'apt-get -f install' to correct these.") << endl;
109 ShowBroken(c1out,*this,true);
110
111 return _error->Error(_("Unmet dependencies. Try using -f."));
112 }
113
114 return true;
115 }
116 /*}}}*/