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