]>
Commit | Line | Data |
---|---|---|
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 (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 | /*}}}*/ | |
244 | // CacheFile::RemoveCaches - remove all cache files from disk /*{{{*/ | |
245 | // --------------------------------------------------------------------- | |
246 | /* */ | |
247 | void 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) | |
253 | RemoveFile("RemoveCaches", pkgcache); | |
254 | if (srcpkgcache.empty() == false && RealFileExists(srcpkgcache) == true) | |
255 | RemoveFile("RemoveCaches", srcpkgcache); | |
256 | if (pkgcache.empty() == false) | |
257 | { | |
258 | std::string cachedir = flNotFile(pkgcache); | |
259 | std::string cachefile = flNotDir(pkgcache); | |
260 | if (cachedir.empty() != true && cachefile.empty() != true && DirectoryExists(cachedir) == true) | |
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; | |
269 | RemoveFile("RemoveCaches", *file); | |
270 | } | |
271 | } | |
272 | } | |
273 | ||
274 | if (srcpkgcache.empty() == true) | |
275 | return; | |
276 | ||
277 | std::string cachedir = flNotFile(srcpkgcache); | |
278 | std::string cachefile = flNotDir(srcpkgcache); | |
279 | if (cachedir.empty() == true || cachefile.empty() == true || DirectoryExists(cachedir) == false) | |
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; | |
288 | RemoveFile("RemoveCaches", *file); | |
289 | } | |
290 | } | |
291 | /*}}}*/ | |
292 | // CacheFile::Close - close the cache files /*{{{*/ | |
293 | // --------------------------------------------------------------------- | |
294 | /* */ | |
295 | void pkgCacheFile::Close() | |
296 | { | |
297 | if (ExternOwner == false) | |
298 | { | |
299 | delete DCache; | |
300 | delete Cache; | |
301 | delete Map; | |
302 | } | |
303 | else | |
304 | ExternOwner = false; | |
305 | delete Policy; | |
306 | delete SrcList; | |
307 | _system->UnLock(true); | |
308 | ||
309 | Map = NULL; | |
310 | DCache = NULL; | |
311 | Policy = NULL; | |
312 | Cache = NULL; | |
313 | SrcList = NULL; | |
314 | } | |
315 | /*}}}*/ |