]> git.saurik.com Git - apt.git/blame - apt-pkg/cachefile.cc
let DPKG_COLORS default to our APT::Color setting
[apt.git] / apt-pkg / cachefile.cc
CommitLineData
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 /*{{{*/
ea542140
DK
15#include <config.h>
16
2d11135a
AL
17#include <apt-pkg/cachefile.h>
18#include <apt-pkg/error.h>
19#include <apt-pkg/sourcelist.h>
20#include <apt-pkg/pkgcachegen.h>
21#include <apt-pkg/configuration.h>
b2e465d6
AL
22#include <apt-pkg/policy.h>
23#include <apt-pkg/pkgsystem.h>
614adaa0 24#include <apt-pkg/fileutl.h>
472ff00e 25#include <apt-pkg/progress.h>
453b82a3
DK
26#include <apt-pkg/depcache.h>
27#include <apt-pkg/mmap.h>
28#include <apt-pkg/pkgcache.h>
a249b3e6 29#include <apt-pkg/indexfile.h>
453b82a3
DK
30
31#include <string.h>
32#include <unistd.h>
33#include <string>
34#include <vector>
f40fdaa4 35#include <memory>
ea542140 36
b2e465d6 37#include <apti18n.h>
2d11135a 38 /*}}}*/
2d11135a 39// CacheFile::CacheFile - Constructor /*{{{*/
9112f777
DK
40pkgCacheFile::pkgCacheFile() : d(NULL), ExternOwner(false), Map(NULL), Cache(NULL),
41 DCache(NULL), SrcList(NULL), Policy(NULL)
42{
43}
44pkgCacheFile::pkgCacheFile(pkgDepCache * const Owner) : d(NULL), ExternOwner(true),
45 Map(&Owner->GetCache().GetMap()), Cache(&Owner->GetCache()),
46 DCache(Owner), SrcList(NULL), Policy(NULL)
2d11135a
AL
47{
48}
49 /*}}}*/
b2e465d6 50// CacheFile::~CacheFile - Destructor /*{{{*/
2d11135a
AL
51// ---------------------------------------------------------------------
52/* */
53pkgCacheFile::~pkgCacheFile()
54{
9112f777
DK
55 if (ExternOwner == false)
56 {
57 delete DCache;
58 delete Cache;
59 delete Map;
60 }
b2e465d6 61 delete Policy;
3f8621c5 62 delete SrcList;
9112f777
DK
63 if (ExternOwner == false)
64 _system->UnLock(true);
3f8621c5 65}
2d11135a 66 /*}}}*/
0077d829 67// CacheFile::BuildCaches - Open and build the cache files /*{{{*/
95278287
DK
68class APT_HIDDEN ScopedErrorMerge {
69public:
70 ScopedErrorMerge() { _error->PushToStack(); }
71 ~ScopedErrorMerge() { _error->MergeWithStack(); }
72};
f40fdaa4 73
2e5f4e45 74bool pkgCacheFile::BuildCaches(OpProgress *Progress, bool WithLock)
2d11135a 75{
f40fdaa4
JAK
76 std::unique_ptr<pkgCache> Cache;
77 std::unique_ptr<MMap> Map;
78
79 if (this->Cache != NULL)
3f8621c5
DK
80 return true;
81
95278287 82 ScopedErrorMerge sem;
3f8621c5
DK
83 if (_config->FindB("pkgCacheFile::Generate", true) == false)
84 {
3d8232bf 85 FileFd file(_config->FindFile("Dir::Cache::pkgcache"), FileFd::ReadOnly);
95278287
DK
86 if (file.IsOpen() == false || file.Failed())
87 return false;
f40fdaa4 88 Map.reset(new MMap(file, MMap::Public|MMap::ReadOnly));
6789e01e
DK
89 if (unlikely(Map->validData() == false))
90 return false;
f40fdaa4
JAK
91 Cache.reset(new pkgCache(Map.get()));
92 if (_error->PendingError() == true)
93 return false;
94
95 this->Cache = Cache.release();
96 this->Map = Map.release();
97 return true;
3f8621c5
DK
98 }
99
2d11135a 100 if (WithLock == true)
b2e465d6
AL
101 if (_system->Lock() == false)
102 return false;
c4171975 103
2d11135a
AL
104 if (_error->PendingError() == true)
105 return false;
3f8621c5 106
f40fdaa4
JAK
107 if (BuildSourceList(Progress) == false)
108 return false;
b2e465d6
AL
109
110 // Read the caches
f40fdaa4
JAK
111 MMap *TmpMap = nullptr;
112 pkgCache *TmpCache = nullptr;
113 bool Res = pkgCacheGenerator::MakeStatusCache(*SrcList,Progress,&TmpMap, &TmpCache, true);
114 Map.reset(TmpMap);
115 Cache.reset(TmpCache);
2e5f4e45
DK
116 if (Progress != NULL)
117 Progress->Done();
b2e465d6
AL
118 if (Res == false)
119 return _error->Error(_("The package lists or status file could not be parsed or opened."));
120
121 /* This sux, remove it someday */
95278287 122 if (_error->PendingError() == true)
a7c835af 123 _error->Warning(_("You may want to run apt-get update to correct these problems"));
b2e465d6 124
f1616039 125 if (Cache == nullptr)
f40fdaa4 126 Cache.reset(new pkgCache(Map.get()));
b2e465d6
AL
127 if (_error->PendingError() == true)
128 return false;
f40fdaa4
JAK
129 this->Map = Map.release();
130 this->Cache = Cache.release();
131
0077d829
AL
132 return true;
133}
134 /*}}}*/
3f8621c5
DK
135// CacheFile::BuildSourceList - Open and build all relevant sources.list/*{{{*/
136// ---------------------------------------------------------------------
137/* */
65512241 138bool pkgCacheFile::BuildSourceList(OpProgress * /*Progress*/)
3f8621c5 139{
f40fdaa4
JAK
140 std::unique_ptr<pkgSourceList> SrcList;
141 if (this->SrcList != NULL)
3f8621c5
DK
142 return true;
143
f40fdaa4 144 SrcList.reset(new pkgSourceList());
3f8621c5
DK
145 if (SrcList->ReadMainList() == false)
146 return _error->Error(_("The list of sources could not be read."));
f40fdaa4 147 this->SrcList = SrcList.release();
3f8621c5
DK
148 return true;
149}
150 /*}}}*/
2e5f4e45 151// CacheFile::BuildPolicy - Open and build all relevant preferences /*{{{*/
0077d829
AL
152// ---------------------------------------------------------------------
153/* */
65512241 154bool pkgCacheFile::BuildPolicy(OpProgress * /*Progress*/)
0077d829 155{
f40fdaa4
JAK
156 std::unique_ptr<pkgPolicy> Policy;
157 if (this->Policy != NULL)
3f8621c5
DK
158 return true;
159
f40fdaa4 160 Policy.reset(new pkgPolicy(Cache));
b2e465d6
AL
161 if (_error->PendingError() == true)
162 return false;
6009e60d 163
e68ca100 164 if (ReadPinFile(*Policy) == false || ReadPinDir(*Policy) == false)
b2e465d6 165 return false;
2e5f4e45 166
f40fdaa4 167 this->Policy = Policy.release();
2e5f4e45
DK
168 return true;
169}
170 /*}}}*/
171// CacheFile::BuildDepCache - Open and build the dependency cache /*{{{*/
172// ---------------------------------------------------------------------
173/* */
174bool pkgCacheFile::BuildDepCache(OpProgress *Progress)
175{
f40fdaa4
JAK
176 std::unique_ptr<pkgDepCache> DCache;
177 if (this->DCache != NULL)
3f8621c5
DK
178 return true;
179
592d06b6
MV
180 if (BuildPolicy(Progress) == false)
181 return false;
182
f40fdaa4 183 DCache.reset(new pkgDepCache(Cache,Policy));
b2e465d6
AL
184 if (_error->PendingError() == true)
185 return false;
f40fdaa4
JAK
186 if (DCache->Init(Progress) == false)
187 return false;
2e5f4e45 188
f40fdaa4
JAK
189 this->DCache = DCache.release();
190 return true;
2e5f4e45
DK
191}
192 /*}}}*/
193// CacheFile::Open - Open the cache files, creating if necessary /*{{{*/
194// ---------------------------------------------------------------------
195/* */
196bool pkgCacheFile::Open(OpProgress *Progress, bool WithLock)
197{
198 if (BuildCaches(Progress,WithLock) == false)
199 return false;
200
201 if (BuildPolicy(Progress) == false)
202 return false;
203
204 if (BuildDepCache(Progress) == false)
205 return false;
206
207 if (Progress != NULL)
208 Progress->Done();
2d11135a
AL
209 if (_error->PendingError() == true)
210 return false;
2d11135a
AL
211
212 return true;
213}
214 /*}}}*/
a249b3e6
DK
215bool pkgCacheFile::AddIndexFile(pkgIndexFile * const File) /*{{{*/
216{
217 if (SrcList == NULL)
218 if (BuildSourceList() == false)
219 return false;
220 SrcList->AddVolatileFile(File);
221
222 if (Cache == nullptr || File->HasPackages() == false || File->Exists() == false)
223 return true;
224
225 if (File->FindInCache(*Cache).end() == false)
226 return _error->Warning("Duplicate sources.list entry %s",
227 File->Describe().c_str());
228
229 if (ExternOwner == false)
230 {
231 delete DCache;
232 delete Cache;
233 }
234 delete Policy;
235 DCache = NULL;
236 Policy = NULL;
237 Cache = NULL;
238
239 if (ExternOwner == false)
240 {
241 // a dynamic mmap means that we have build at least parts of the cache
242 // in memory – which we might or might not have written to disk.
243 // Throwing away would therefore be a very costly operation we want to avoid
244 DynamicMMap * dynmmap = dynamic_cast<DynamicMMap*>(Map);
245 if (dynmmap != nullptr)
246 {
247 {
248 pkgCacheGenerator Gen(dynmmap, nullptr);
a133f79c 249 if (Gen.Start() == false || File->Merge(Gen, nullptr) == false)
a249b3e6
DK
250 return false;
251 }
252 Cache = new pkgCache(Map);
f40fdaa4
JAK
253 if (_error->PendingError() == true) {
254 delete Cache;
255 Cache = nullptr;
256 return false;
257 }
258 return true;
a249b3e6
DK
259 }
260 else
261 {
262 delete Map;
263 Map = NULL;
264 }
265 }
266 else
267 {
268 ExternOwner = false;
269 Map = NULL;
270 }
b6f1b480 271 _system->UnLock(true);
a249b3e6
DK
272 return true;
273}
274 /*}}}*/
8de79b68
DK
275// CacheFile::RemoveCaches - remove all cache files from disk /*{{{*/
276// ---------------------------------------------------------------------
277/* */
278void pkgCacheFile::RemoveCaches()
279{
280 std::string const pkgcache = _config->FindFile("Dir::cache::pkgcache");
281 std::string const srcpkgcache = _config->FindFile("Dir::cache::srcpkgcache");
282
283 if (pkgcache.empty() == false && RealFileExists(pkgcache) == true)
ce1f3a2c 284 RemoveFile("RemoveCaches", pkgcache);
8de79b68 285 if (srcpkgcache.empty() == false && RealFileExists(srcpkgcache) == true)
ce1f3a2c 286 RemoveFile("RemoveCaches", srcpkgcache);
fbb2c7e0
DK
287 if (pkgcache.empty() == false)
288 {
289 std::string cachedir = flNotFile(pkgcache);
290 std::string cachefile = flNotDir(pkgcache);
c0d58f42 291 if (cachedir.empty() != true && cachefile.empty() != true && DirectoryExists(cachedir) == true)
fbb2c7e0
DK
292 {
293 cachefile.append(".");
294 std::vector<std::string> caches = GetListOfFilesInDir(cachedir, false);
295 for (std::vector<std::string>::const_iterator file = caches.begin(); file != caches.end(); ++file)
296 {
297 std::string nuke = flNotDir(*file);
298 if (strncmp(cachefile.c_str(), nuke.c_str(), cachefile.length()) != 0)
299 continue;
ce1f3a2c 300 RemoveFile("RemoveCaches", *file);
fbb2c7e0
DK
301 }
302 }
303 }
304
305 if (srcpkgcache.empty() == true)
306 return;
307
308 std::string cachedir = flNotFile(srcpkgcache);
309 std::string cachefile = flNotDir(srcpkgcache);
c0d58f42 310 if (cachedir.empty() == true || cachefile.empty() == true || DirectoryExists(cachedir) == false)
fbb2c7e0
DK
311 return;
312 cachefile.append(".");
313 std::vector<std::string> caches = GetListOfFilesInDir(cachedir, false);
314 for (std::vector<std::string>::const_iterator file = caches.begin(); file != caches.end(); ++file)
315 {
316 std::string nuke = flNotDir(*file);
317 if (strncmp(cachefile.c_str(), nuke.c_str(), cachefile.length()) != 0)
318 continue;
ce1f3a2c 319 RemoveFile("RemoveCaches", *file);
fbb2c7e0 320 }
8de79b68
DK
321}
322 /*}}}*/
b2e465d6
AL
323// CacheFile::Close - close the cache files /*{{{*/
324// ---------------------------------------------------------------------
325/* */
326void pkgCacheFile::Close()
327{
9112f777
DK
328 if (ExternOwner == false)
329 {
330 delete DCache;
331 delete Cache;
332 delete Map;
333 }
334 else
335 ExternOwner = false;
b2e465d6 336 delete Policy;
3f8621c5 337 delete SrcList;
b2e465d6
AL
338 _system->UnLock(true);
339
3f8621c5
DK
340 Map = NULL;
341 DCache = NULL;
342 Policy = NULL;
343 Cache = NULL;
344 SrcList = NULL;
b2e465d6
AL
345}
346 /*}}}*/