]> git.saurik.com Git - apt.git/blob - apt-pkg/cachefile.cc
get group again after potential remap in Source: parse
[apt.git] / apt-pkg / cachefile.cc
1 // -*- mode: cpp; mode: fold -*-
2 // Description /*{{{*/
3 // $Id: cachefile.cc,v 1.8 2002/04/27 04:28:04 jgg Exp $
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 /*{{{*/
15 #include <config.h>
16
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>
22 #include <apt-pkg/policy.h>
23 #include <apt-pkg/pkgsystem.h>
24 #include <apt-pkg/fileutl.h>
25 #include <apt-pkg/progress.h>
26 #include <apt-pkg/depcache.h>
27 #include <apt-pkg/mmap.h>
28 #include <apt-pkg/pkgcache.h>
29 #include <apt-pkg/indexfile.h>
30
31 #include <string.h>
32 #include <unistd.h>
33 #include <string>
34 #include <vector>
35
36 #include <apti18n.h>
37 /*}}}*/
38 // CacheFile::CacheFile - Constructor /*{{{*/
39 pkgCacheFile::pkgCacheFile() : d(NULL), ExternOwner(false), Map(NULL), Cache(NULL),
40 DCache(NULL), SrcList(NULL), Policy(NULL)
41 {
42 }
43 pkgCacheFile::pkgCacheFile(pkgDepCache * const Owner) : d(NULL), ExternOwner(true),
44 Map(&Owner->GetCache().GetMap()), Cache(&Owner->GetCache()),
45 DCache(Owner), SrcList(NULL), Policy(NULL)
46 {
47 }
48 /*}}}*/
49 // CacheFile::~CacheFile - Destructor /*{{{*/
50 // ---------------------------------------------------------------------
51 /* */
52 pkgCacheFile::~pkgCacheFile()
53 {
54 if (ExternOwner == false)
55 {
56 delete DCache;
57 delete Cache;
58 delete Map;
59 }
60 delete Policy;
61 delete SrcList;
62 if (ExternOwner == false)
63 _system->UnLock(true);
64 }
65 /*}}}*/
66 // CacheFile::BuildCaches - Open and build the cache files /*{{{*/
67 class APT_HIDDEN ScopedErrorMerge {
68 public:
69 ScopedErrorMerge() { _error->PushToStack(); }
70 ~ScopedErrorMerge() { _error->MergeWithStack(); }
71 };
72 bool pkgCacheFile::BuildCaches(OpProgress *Progress, bool WithLock)
73 {
74 if (Cache != NULL)
75 return true;
76
77 ScopedErrorMerge sem;
78 if (_config->FindB("pkgCacheFile::Generate", true) == false)
79 {
80 FileFd file(_config->FindFile("Dir::Cache::pkgcache"), FileFd::ReadOnly);
81 if (file.IsOpen() == false || file.Failed())
82 return false;
83 Map = new MMap(file, MMap::Public|MMap::ReadOnly);
84 if (unlikely(Map->validData() == false))
85 return false;
86 Cache = new pkgCache(Map);
87 return _error->PendingError() == false;
88 }
89
90 if (WithLock == true)
91 if (_system->Lock() == false)
92 return false;
93
94 if (_error->PendingError() == true)
95 return false;
96
97 BuildSourceList(Progress);
98
99 // Read the caches
100 Cache = nullptr;
101 bool Res = pkgCacheGenerator::MakeStatusCache(*SrcList,Progress,&Map, &Cache, true);
102 if (Progress != NULL)
103 Progress->Done();
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 */
108 if (_error->PendingError() == true)
109 _error->Warning(_("You may want to run apt-get update to correct these problems"));
110
111 if (Cache == nullptr)
112 Cache = new pkgCache(Map);
113 if (_error->PendingError() == true)
114 return false;
115 return true;
116 }
117 /*}}}*/
118 // CacheFile::BuildSourceList - Open and build all relevant sources.list/*{{{*/
119 // ---------------------------------------------------------------------
120 /* */
121 bool pkgCacheFile::BuildSourceList(OpProgress * /*Progress*/)
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 /*}}}*/
132 // CacheFile::BuildPolicy - Open and build all relevant preferences /*{{{*/
133 // ---------------------------------------------------------------------
134 /* */
135 bool pkgCacheFile::BuildPolicy(OpProgress * /*Progress*/)
136 {
137 if (Policy != NULL)
138 return true;
139
140 Policy = new pkgPolicy(Cache);
141 if (_error->PendingError() == true)
142 return false;
143
144 if (ReadPinFile(*Policy) == false || ReadPinDir(*Policy) == false)
145 return false;
146
147 return true;
148 }
149 /*}}}*/
150 // CacheFile::BuildDepCache - Open and build the dependency cache /*{{{*/
151 // ---------------------------------------------------------------------
152 /* */
153 bool pkgCacheFile::BuildDepCache(OpProgress *Progress)
154 {
155 if (DCache != NULL)
156 return true;
157
158 if (BuildPolicy(Progress) == false)
159 return false;
160
161 DCache = new pkgDepCache(Cache,Policy);
162 if (_error->PendingError() == true)
163 return false;
164
165 return DCache->Init(Progress);
166 }
167 /*}}}*/
168 // CacheFile::Open - Open the cache files, creating if necessary /*{{{*/
169 // ---------------------------------------------------------------------
170 /* */
171 bool 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();
184 if (_error->PendingError() == true)
185 return false;
186
187 return true;
188 }
189 /*}}}*/
190 bool 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 (Gen.Start() == false || 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 _system->UnLock(true);
242 return true;
243 }
244 /*}}}*/
245 // CacheFile::RemoveCaches - remove all cache files from disk /*{{{*/
246 // ---------------------------------------------------------------------
247 /* */
248 void pkgCacheFile::RemoveCaches()
249 {
250 std::string const pkgcache = _config->FindFile("Dir::cache::pkgcache");
251 std::string const srcpkgcache = _config->FindFile("Dir::cache::srcpkgcache");
252
253 if (pkgcache.empty() == false && RealFileExists(pkgcache) == true)
254 RemoveFile("RemoveCaches", pkgcache);
255 if (srcpkgcache.empty() == false && RealFileExists(srcpkgcache) == true)
256 RemoveFile("RemoveCaches", srcpkgcache);
257 if (pkgcache.empty() == false)
258 {
259 std::string cachedir = flNotFile(pkgcache);
260 std::string cachefile = flNotDir(pkgcache);
261 if (cachedir.empty() != true && cachefile.empty() != true && DirectoryExists(cachedir) == true)
262 {
263 cachefile.append(".");
264 std::vector<std::string> caches = GetListOfFilesInDir(cachedir, false);
265 for (std::vector<std::string>::const_iterator file = caches.begin(); file != caches.end(); ++file)
266 {
267 std::string nuke = flNotDir(*file);
268 if (strncmp(cachefile.c_str(), nuke.c_str(), cachefile.length()) != 0)
269 continue;
270 RemoveFile("RemoveCaches", *file);
271 }
272 }
273 }
274
275 if (srcpkgcache.empty() == true)
276 return;
277
278 std::string cachedir = flNotFile(srcpkgcache);
279 std::string cachefile = flNotDir(srcpkgcache);
280 if (cachedir.empty() == true || cachefile.empty() == true || DirectoryExists(cachedir) == false)
281 return;
282 cachefile.append(".");
283 std::vector<std::string> caches = GetListOfFilesInDir(cachedir, false);
284 for (std::vector<std::string>::const_iterator file = caches.begin(); file != caches.end(); ++file)
285 {
286 std::string nuke = flNotDir(*file);
287 if (strncmp(cachefile.c_str(), nuke.c_str(), cachefile.length()) != 0)
288 continue;
289 RemoveFile("RemoveCaches", *file);
290 }
291 }
292 /*}}}*/
293 // CacheFile::Close - close the cache files /*{{{*/
294 // ---------------------------------------------------------------------
295 /* */
296 void pkgCacheFile::Close()
297 {
298 if (ExternOwner == false)
299 {
300 delete DCache;
301 delete Cache;
302 delete Map;
303 }
304 else
305 ExternOwner = false;
306 delete Policy;
307 delete SrcList;
308 _system->UnLock(true);
309
310 Map = NULL;
311 DCache = NULL;
312 Policy = NULL;
313 Cache = NULL;
314 SrcList = NULL;
315 }
316 /*}}}*/