]> git.saurik.com Git - apt.git/blame - apt-pkg/cachefile.cc
This is realloc, not reallocf: be more careful :/.
[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)
baec76f5 93 return _error->ReturnError();
f40fdaa4
JAK
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 104 if (_error->PendingError() == true)
baec76f5 105 return _error->ReturnError();
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
f1616039 121 if (Cache == nullptr)
f40fdaa4 122 Cache.reset(new pkgCache(Map.get()));
f40fdaa4
JAK
123 this->Map = Map.release();
124 this->Cache = Cache.release();
125
0077d829
AL
126 return true;
127}
128 /*}}}*/
3f8621c5
DK
129// CacheFile::BuildSourceList - Open and build all relevant sources.list/*{{{*/
130// ---------------------------------------------------------------------
131/* */
65512241 132bool pkgCacheFile::BuildSourceList(OpProgress * /*Progress*/)
3f8621c5 133{
f40fdaa4
JAK
134 std::unique_ptr<pkgSourceList> SrcList;
135 if (this->SrcList != NULL)
3f8621c5
DK
136 return true;
137
f40fdaa4 138 SrcList.reset(new pkgSourceList());
3f8621c5
DK
139 if (SrcList->ReadMainList() == false)
140 return _error->Error(_("The list of sources could not be read."));
f40fdaa4 141 this->SrcList = SrcList.release();
3f8621c5
DK
142 return true;
143}
144 /*}}}*/
2e5f4e45 145// CacheFile::BuildPolicy - Open and build all relevant preferences /*{{{*/
0077d829
AL
146// ---------------------------------------------------------------------
147/* */
65512241 148bool pkgCacheFile::BuildPolicy(OpProgress * /*Progress*/)
0077d829 149{
f40fdaa4
JAK
150 std::unique_ptr<pkgPolicy> Policy;
151 if (this->Policy != NULL)
3f8621c5
DK
152 return true;
153
f40fdaa4 154 Policy.reset(new pkgPolicy(Cache));
b2e465d6 155 if (_error->PendingError() == true)
baec76f5 156 return _error->ReturnError();
6009e60d 157
e68ca100 158 if (ReadPinFile(*Policy) == false || ReadPinDir(*Policy) == false)
b2e465d6 159 return false;
2e5f4e45 160
f40fdaa4 161 this->Policy = Policy.release();
2e5f4e45
DK
162 return true;
163}
164 /*}}}*/
165// CacheFile::BuildDepCache - Open and build the dependency cache /*{{{*/
166// ---------------------------------------------------------------------
167/* */
168bool pkgCacheFile::BuildDepCache(OpProgress *Progress)
169{
fdf9eef4
DK
170 if (BuildCaches(Progress, false) == false)
171 return false;
172
f40fdaa4
JAK
173 std::unique_ptr<pkgDepCache> DCache;
174 if (this->DCache != NULL)
3f8621c5
DK
175 return true;
176
592d06b6
MV
177 if (BuildPolicy(Progress) == false)
178 return false;
179
f40fdaa4 180 DCache.reset(new pkgDepCache(Cache,Policy));
b2e465d6 181 if (_error->PendingError() == true)
baec76f5 182 return _error->ReturnError();
f40fdaa4
JAK
183 if (DCache->Init(Progress) == false)
184 return false;
2e5f4e45 185
f40fdaa4
JAK
186 this->DCache = DCache.release();
187 return true;
2e5f4e45
DK
188}
189 /*}}}*/
190// CacheFile::Open - Open the cache files, creating if necessary /*{{{*/
191// ---------------------------------------------------------------------
192/* */
193bool pkgCacheFile::Open(OpProgress *Progress, bool WithLock)
194{
195 if (BuildCaches(Progress,WithLock) == false)
196 return false;
197
198 if (BuildPolicy(Progress) == false)
199 return false;
200
201 if (BuildDepCache(Progress) == false)
202 return false;
203
204 if (Progress != NULL)
205 Progress->Done();
2d11135a
AL
206
207 return true;
208}
209 /*}}}*/
a249b3e6
DK
210bool pkgCacheFile::AddIndexFile(pkgIndexFile * const File) /*{{{*/
211{
212 if (SrcList == NULL)
213 if (BuildSourceList() == false)
214 return false;
215 SrcList->AddVolatileFile(File);
216
217 if (Cache == nullptr || File->HasPackages() == false || File->Exists() == false)
218 return true;
219
220 if (File->FindInCache(*Cache).end() == false)
221 return _error->Warning("Duplicate sources.list entry %s",
222 File->Describe().c_str());
223
224 if (ExternOwner == false)
225 {
226 delete DCache;
227 delete Cache;
228 }
229 delete Policy;
230 DCache = NULL;
231 Policy = NULL;
232 Cache = NULL;
233
234 if (ExternOwner == false)
235 {
236 // a dynamic mmap means that we have build at least parts of the cache
237 // in memory – which we might or might not have written to disk.
238 // Throwing away would therefore be a very costly operation we want to avoid
239 DynamicMMap * dynmmap = dynamic_cast<DynamicMMap*>(Map);
240 if (dynmmap != nullptr)
241 {
242 {
243 pkgCacheGenerator Gen(dynmmap, nullptr);
a133f79c 244 if (Gen.Start() == false || File->Merge(Gen, nullptr) == false)
a249b3e6
DK
245 return false;
246 }
247 Cache = new pkgCache(Map);
f40fdaa4
JAK
248 if (_error->PendingError() == true) {
249 delete Cache;
250 Cache = nullptr;
baec76f5 251 return _error->ReturnError();
f40fdaa4
JAK
252 }
253 return true;
a249b3e6
DK
254 }
255 else
256 {
257 delete Map;
258 Map = NULL;
259 }
260 }
261 else
262 {
263 ExternOwner = false;
264 Map = NULL;
265 }
b6f1b480 266 _system->UnLock(true);
a249b3e6
DK
267 return true;
268}
269 /*}}}*/
8de79b68
DK
270// CacheFile::RemoveCaches - remove all cache files from disk /*{{{*/
271// ---------------------------------------------------------------------
272/* */
273void pkgCacheFile::RemoveCaches()
274{
275 std::string const pkgcache = _config->FindFile("Dir::cache::pkgcache");
276 std::string const srcpkgcache = _config->FindFile("Dir::cache::srcpkgcache");
277
278 if (pkgcache.empty() == false && RealFileExists(pkgcache) == true)
ce1f3a2c 279 RemoveFile("RemoveCaches", pkgcache);
8de79b68 280 if (srcpkgcache.empty() == false && RealFileExists(srcpkgcache) == true)
ce1f3a2c 281 RemoveFile("RemoveCaches", srcpkgcache);
fbb2c7e0
DK
282 if (pkgcache.empty() == false)
283 {
284 std::string cachedir = flNotFile(pkgcache);
285 std::string cachefile = flNotDir(pkgcache);
c0d58f42 286 if (cachedir.empty() != true && cachefile.empty() != true && DirectoryExists(cachedir) == true)
fbb2c7e0
DK
287 {
288 cachefile.append(".");
289 std::vector<std::string> caches = GetListOfFilesInDir(cachedir, false);
290 for (std::vector<std::string>::const_iterator file = caches.begin(); file != caches.end(); ++file)
291 {
292 std::string nuke = flNotDir(*file);
293 if (strncmp(cachefile.c_str(), nuke.c_str(), cachefile.length()) != 0)
294 continue;
ce1f3a2c 295 RemoveFile("RemoveCaches", *file);
fbb2c7e0
DK
296 }
297 }
298 }
299
300 if (srcpkgcache.empty() == true)
301 return;
302
303 std::string cachedir = flNotFile(srcpkgcache);
304 std::string cachefile = flNotDir(srcpkgcache);
c0d58f42 305 if (cachedir.empty() == true || cachefile.empty() == true || DirectoryExists(cachedir) == false)
fbb2c7e0
DK
306 return;
307 cachefile.append(".");
308 std::vector<std::string> caches = GetListOfFilesInDir(cachedir, false);
309 for (std::vector<std::string>::const_iterator file = caches.begin(); file != caches.end(); ++file)
310 {
311 std::string nuke = flNotDir(*file);
312 if (strncmp(cachefile.c_str(), nuke.c_str(), cachefile.length()) != 0)
313 continue;
ce1f3a2c 314 RemoveFile("RemoveCaches", *file);
fbb2c7e0 315 }
8de79b68
DK
316}
317 /*}}}*/
b2e465d6
AL
318// CacheFile::Close - close the cache files /*{{{*/
319// ---------------------------------------------------------------------
320/* */
321void pkgCacheFile::Close()
322{
9112f777
DK
323 if (ExternOwner == false)
324 {
325 delete DCache;
326 delete Cache;
327 delete Map;
328 }
329 else
330 ExternOwner = false;
b2e465d6 331 delete Policy;
3f8621c5 332 delete SrcList;
b2e465d6
AL
333 _system->UnLock(true);
334
3f8621c5
DK
335 Map = NULL;
336 DCache = NULL;
337 Policy = NULL;
338 Cache = NULL;
339 SrcList = NULL;
b2e465d6
AL
340}
341 /*}}}*/