]>
Commit | Line | Data |
---|---|---|
2d11135a AL |
1 | // -*- mode: cpp; mode: fold -*- |
2 | // Description /*{{{*/ | |
0077d829 | 3 | // $Id: cachefile.cc,v 1.8 2002/04/27 04:28:04 jgg Exp $ |
2d11135a AL |
4 | /* ###################################################################### |
5 | ||
6 | CacheFile - Simple wrapper class for opening, generating and whatnot | |
7 | ||
8 | This class implements a simple 2 line mechanism to open various sorts | |
9 | of caches. It can operate as root, as not root, show progress and so on, | |
10 | it transparently handles everything necessary. | |
11 | ||
12 | ##################################################################### */ | |
13 | /*}}}*/ | |
14 | // Include Files /*{{{*/ | |
2d11135a AL |
15 | #include <apt-pkg/cachefile.h> |
16 | #include <apt-pkg/error.h> | |
17 | #include <apt-pkg/sourcelist.h> | |
18 | #include <apt-pkg/pkgcachegen.h> | |
19 | #include <apt-pkg/configuration.h> | |
b2e465d6 AL |
20 | #include <apt-pkg/policy.h> |
21 | #include <apt-pkg/pkgsystem.h> | |
89b70b5a | 22 | #include <apt-pkg/acquire-item.h> |
614adaa0 | 23 | #include <apt-pkg/fileutl.h> |
b2e465d6 AL |
24 | |
25 | #include <apti18n.h> | |
2d11135a | 26 | /*}}}*/ |
2d11135a AL |
27 | // CacheFile::CacheFile - Constructor /*{{{*/ |
28 | // --------------------------------------------------------------------- | |
29 | /* */ | |
3f8621c5 DK |
30 | pkgCacheFile::pkgCacheFile() : Map(NULL), Cache(NULL), DCache(NULL), |
31 | Policy(NULL), SrcList(NULL) | |
2d11135a AL |
32 | { |
33 | } | |
34 | /*}}}*/ | |
b2e465d6 | 35 | // CacheFile::~CacheFile - Destructor /*{{{*/ |
2d11135a AL |
36 | // --------------------------------------------------------------------- |
37 | /* */ | |
38 | pkgCacheFile::~pkgCacheFile() | |
39 | { | |
b2e465d6 AL |
40 | delete DCache; |
41 | delete Policy; | |
3f8621c5 | 42 | delete SrcList; |
2d11135a AL |
43 | delete Cache; |
44 | delete Map; | |
b2e465d6 | 45 | _system->UnLock(true); |
3f8621c5 | 46 | } |
2d11135a | 47 | /*}}}*/ |
0077d829 | 48 | // CacheFile::BuildCaches - Open and build the cache files /*{{{*/ |
2d11135a AL |
49 | // --------------------------------------------------------------------- |
50 | /* */ | |
2e5f4e45 | 51 | bool pkgCacheFile::BuildCaches(OpProgress *Progress, bool WithLock) |
2d11135a | 52 | { |
3f8621c5 DK |
53 | if (Cache != NULL) |
54 | return true; | |
55 | ||
56 | if (_config->FindB("pkgCacheFile::Generate", true) == false) | |
57 | { | |
58 | Map = new MMap(*new FileFd(_config->FindFile("Dir::Cache::pkgcache"), | |
59 | FileFd::ReadOnly),MMap::Public|MMap::ReadOnly); | |
60 | Cache = new pkgCache(Map); | |
61 | if (_error->PendingError() == true) | |
62 | return false; | |
63 | return true; | |
64 | } | |
65 | ||
6009e60d | 66 | const bool ErrorWasEmpty = _error->empty(); |
2d11135a | 67 | if (WithLock == true) |
b2e465d6 AL |
68 | if (_system->Lock() == false) |
69 | return false; | |
2d11135a | 70 | |
c37b9502 AL |
71 | if (_config->FindB("Debug::NoLocking",false) == true) |
72 | WithLock = false; | |
73 | ||
2d11135a AL |
74 | if (_error->PendingError() == true) |
75 | return false; | |
3f8621c5 DK |
76 | |
77 | BuildSourceList(Progress); | |
b2e465d6 AL |
78 | |
79 | // Read the caches | |
3f8621c5 | 80 | bool Res = pkgCacheGenerator::MakeStatusCache(*SrcList,Progress,&Map,!WithLock); |
2e5f4e45 DK |
81 | if (Progress != NULL) |
82 | Progress->Done(); | |
b2e465d6 AL |
83 | if (Res == false) |
84 | return _error->Error(_("The package lists or status file could not be parsed or opened.")); | |
85 | ||
86 | /* This sux, remove it someday */ | |
6009e60d | 87 | if (ErrorWasEmpty == true && _error->empty() == false) |
a7c835af | 88 | _error->Warning(_("You may want to run apt-get update to correct these problems")); |
b2e465d6 AL |
89 | |
90 | Cache = new pkgCache(Map); | |
91 | if (_error->PendingError() == true) | |
92 | return false; | |
0077d829 AL |
93 | return true; |
94 | } | |
95 | /*}}}*/ | |
3f8621c5 DK |
96 | // CacheFile::BuildSourceList - Open and build all relevant sources.list/*{{{*/ |
97 | // --------------------------------------------------------------------- | |
98 | /* */ | |
99 | bool pkgCacheFile::BuildSourceList(OpProgress *Progress) | |
100 | { | |
101 | if (SrcList != NULL) | |
102 | return true; | |
103 | ||
104 | SrcList = new pkgSourceList(); | |
105 | if (SrcList->ReadMainList() == false) | |
106 | return _error->Error(_("The list of sources could not be read.")); | |
107 | return true; | |
108 | } | |
109 | /*}}}*/ | |
2e5f4e45 | 110 | // CacheFile::BuildPolicy - Open and build all relevant preferences /*{{{*/ |
0077d829 AL |
111 | // --------------------------------------------------------------------- |
112 | /* */ | |
2e5f4e45 | 113 | bool pkgCacheFile::BuildPolicy(OpProgress *Progress) |
0077d829 | 114 | { |
3f8621c5 DK |
115 | if (Policy != NULL) |
116 | return true; | |
117 | ||
b2e465d6 AL |
118 | Policy = new pkgPolicy(Cache); |
119 | if (_error->PendingError() == true) | |
120 | return false; | |
6009e60d | 121 | |
e68ca100 | 122 | if (ReadPinFile(*Policy) == false || ReadPinDir(*Policy) == false) |
b2e465d6 | 123 | return false; |
2e5f4e45 DK |
124 | |
125 | return true; | |
126 | } | |
127 | /*}}}*/ | |
128 | // CacheFile::BuildDepCache - Open and build the dependency cache /*{{{*/ | |
129 | // --------------------------------------------------------------------- | |
130 | /* */ | |
131 | bool pkgCacheFile::BuildDepCache(OpProgress *Progress) | |
132 | { | |
3f8621c5 DK |
133 | if (DCache != NULL) |
134 | return true; | |
135 | ||
b2e465d6 AL |
136 | DCache = new pkgDepCache(Cache,Policy); |
137 | if (_error->PendingError() == true) | |
138 | return false; | |
2e5f4e45 DK |
139 | |
140 | DCache->Init(Progress); | |
141 | return true; | |
142 | } | |
143 | /*}}}*/ | |
144 | // CacheFile::Open - Open the cache files, creating if necessary /*{{{*/ | |
145 | // --------------------------------------------------------------------- | |
146 | /* */ | |
147 | bool pkgCacheFile::Open(OpProgress *Progress, bool WithLock) | |
148 | { | |
149 | if (BuildCaches(Progress,WithLock) == false) | |
150 | return false; | |
151 | ||
152 | if (BuildPolicy(Progress) == false) | |
153 | return false; | |
154 | ||
155 | if (BuildDepCache(Progress) == false) | |
156 | return false; | |
157 | ||
158 | if (Progress != NULL) | |
159 | Progress->Done(); | |
2d11135a AL |
160 | if (_error->PendingError() == true) |
161 | return false; | |
2d11135a AL |
162 | |
163 | return true; | |
164 | } | |
165 | /*}}}*/ | |
b2e465d6 AL |
166 | // CacheFile::Close - close the cache files /*{{{*/ |
167 | // --------------------------------------------------------------------- | |
168 | /* */ | |
169 | void pkgCacheFile::Close() | |
170 | { | |
171 | delete DCache; | |
172 | delete Policy; | |
173 | delete Cache; | |
3f8621c5 | 174 | delete SrcList; |
b2e465d6 AL |
175 | delete Map; |
176 | _system->UnLock(true); | |
177 | ||
3f8621c5 DK |
178 | Map = NULL; |
179 | DCache = NULL; | |
180 | Policy = NULL; | |
181 | Cache = NULL; | |
182 | SrcList = NULL; | |
b2e465d6 AL |
183 | } |
184 | /*}}}*/ |