]>
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 | ||
30 | #include <string.h> | |
31 | #include <unistd.h> | |
32 | #include <string> | |
33 | #include <vector> | |
34 | ||
35 | #include <apti18n.h> | |
36 | /*}}}*/ | |
37 | // CacheFile::CacheFile - Constructor /*{{{*/ | |
38 | pkgCacheFile::pkgCacheFile() : d(NULL), ExternOwner(false), Map(NULL), Cache(NULL), | |
39 | DCache(NULL), SrcList(NULL), Policy(NULL) | |
40 | { | |
41 | } | |
42 | pkgCacheFile::pkgCacheFile(pkgDepCache * const Owner) : d(NULL), ExternOwner(true), | |
43 | Map(&Owner->GetCache().GetMap()), Cache(&Owner->GetCache()), | |
44 | DCache(Owner), SrcList(NULL), Policy(NULL) | |
45 | { | |
46 | } | |
47 | /*}}}*/ | |
48 | // CacheFile::~CacheFile - Destructor /*{{{*/ | |
49 | // --------------------------------------------------------------------- | |
50 | /* */ | |
51 | pkgCacheFile::~pkgCacheFile() | |
52 | { | |
53 | if (ExternOwner == false) | |
54 | { | |
55 | delete DCache; | |
56 | delete Cache; | |
57 | delete Map; | |
58 | } | |
59 | delete Policy; | |
60 | delete SrcList; | |
61 | if (ExternOwner == false) | |
62 | _system->UnLock(true); | |
63 | } | |
64 | /*}}}*/ | |
65 | // CacheFile::BuildCaches - Open and build the cache files /*{{{*/ | |
66 | class APT_HIDDEN ScopedErrorMerge { | |
67 | public: | |
68 | ScopedErrorMerge() { _error->PushToStack(); } | |
69 | ~ScopedErrorMerge() { _error->MergeWithStack(); } | |
70 | }; | |
71 | bool pkgCacheFile::BuildCaches(OpProgress *Progress, bool WithLock) | |
72 | { | |
73 | if (Cache != NULL) | |
74 | return true; | |
75 | ||
76 | ScopedErrorMerge sem; | |
77 | if (_config->FindB("pkgCacheFile::Generate", true) == false) | |
78 | { | |
79 | FileFd file(_config->FindFile("Dir::Cache::pkgcache"), FileFd::ReadOnly); | |
80 | if (file.IsOpen() == false || file.Failed()) | |
81 | return false; | |
82 | Map = new MMap(file, MMap::Public|MMap::ReadOnly); | |
83 | if (unlikely(Map->validData() == false)) | |
84 | return false; | |
85 | Cache = new pkgCache(Map); | |
86 | return _error->PendingError() == false; | |
87 | } | |
88 | ||
89 | if (WithLock == true) | |
90 | if (_system->Lock() == false) | |
91 | return false; | |
92 | ||
93 | if (_error->PendingError() == true) | |
94 | return false; | |
95 | ||
96 | BuildSourceList(Progress); | |
97 | ||
98 | // Read the caches | |
99 | Cache = nullptr; | |
100 | bool Res = pkgCacheGenerator::MakeStatusCache(*SrcList,Progress,&Map, &Cache, true); | |
101 | if (Progress != NULL) | |
102 | Progress->Done(); | |
103 | if (Res == false) | |
104 | return _error->Error(_("The package lists or status file could not be parsed or opened.")); | |
105 | ||
106 | /* This sux, remove it someday */ | |
107 | if (_error->PendingError() == true) | |
108 | _error->Warning(_("You may want to run apt-get update to correct these problems")); | |
109 | ||
110 | if (Cache == nullptr) | |
111 | Cache = new pkgCache(Map); | |
112 | if (_error->PendingError() == true) | |
113 | return false; | |
114 | return true; | |
115 | } | |
116 | /*}}}*/ | |
117 | // CacheFile::BuildSourceList - Open and build all relevant sources.list/*{{{*/ | |
118 | // --------------------------------------------------------------------- | |
119 | /* */ | |
120 | bool pkgCacheFile::BuildSourceList(OpProgress * /*Progress*/) | |
121 | { | |
122 | if (SrcList != NULL) | |
123 | return true; | |
124 | ||
125 | SrcList = new pkgSourceList(); | |
126 | if (SrcList->ReadMainList() == false) | |
127 | return _error->Error(_("The list of sources could not be read.")); | |
128 | return true; | |
129 | } | |
130 | /*}}}*/ | |
131 | // CacheFile::BuildPolicy - Open and build all relevant preferences /*{{{*/ | |
132 | // --------------------------------------------------------------------- | |
133 | /* */ | |
134 | bool pkgCacheFile::BuildPolicy(OpProgress * /*Progress*/) | |
135 | { | |
136 | if (Policy != NULL) | |
137 | return true; | |
138 | ||
139 | Policy = new pkgPolicy(Cache); | |
140 | if (_error->PendingError() == true) | |
141 | return false; | |
142 | ||
143 | if (ReadPinFile(*Policy) == false || ReadPinDir(*Policy) == false) | |
144 | return false; | |
145 | ||
146 | return true; | |
147 | } | |
148 | /*}}}*/ | |
149 | // CacheFile::BuildDepCache - Open and build the dependency cache /*{{{*/ | |
150 | // --------------------------------------------------------------------- | |
151 | /* */ | |
152 | bool pkgCacheFile::BuildDepCache(OpProgress *Progress) | |
153 | { | |
154 | if (DCache != NULL) | |
155 | return true; | |
156 | ||
157 | if (BuildPolicy(Progress) == false) | |
158 | return false; | |
159 | ||
160 | DCache = new pkgDepCache(Cache,Policy); | |
161 | if (_error->PendingError() == true) | |
162 | return false; | |
163 | ||
164 | return DCache->Init(Progress); | |
165 | } | |
166 | /*}}}*/ | |
167 | // CacheFile::Open - Open the cache files, creating if necessary /*{{{*/ | |
168 | // --------------------------------------------------------------------- | |
169 | /* */ | |
170 | bool pkgCacheFile::Open(OpProgress *Progress, bool WithLock) | |
171 | { | |
172 | if (BuildCaches(Progress,WithLock) == false) | |
173 | return false; | |
174 | ||
175 | if (BuildPolicy(Progress) == false) | |
176 | return false; | |
177 | ||
178 | if (BuildDepCache(Progress) == false) | |
179 | return false; | |
180 | ||
181 | if (Progress != NULL) | |
182 | Progress->Done(); | |
183 | if (_error->PendingError() == true) | |
184 | return false; | |
185 | ||
186 | return true; | |
187 | } | |
188 | /*}}}*/ | |
189 | // CacheFile::RemoveCaches - remove all cache files from disk /*{{{*/ | |
190 | // --------------------------------------------------------------------- | |
191 | /* */ | |
192 | void pkgCacheFile::RemoveCaches() | |
193 | { | |
194 | std::string const pkgcache = _config->FindFile("Dir::cache::pkgcache"); | |
195 | std::string const srcpkgcache = _config->FindFile("Dir::cache::srcpkgcache"); | |
196 | ||
197 | if (pkgcache.empty() == false && RealFileExists(pkgcache) == true) | |
198 | RemoveFile("RemoveCaches", pkgcache); | |
199 | if (srcpkgcache.empty() == false && RealFileExists(srcpkgcache) == true) | |
200 | RemoveFile("RemoveCaches", srcpkgcache); | |
201 | if (pkgcache.empty() == false) | |
202 | { | |
203 | std::string cachedir = flNotFile(pkgcache); | |
204 | std::string cachefile = flNotDir(pkgcache); | |
205 | if (cachedir.empty() != true && cachefile.empty() != true && DirectoryExists(cachedir) == true) | |
206 | { | |
207 | cachefile.append("."); | |
208 | std::vector<std::string> caches = GetListOfFilesInDir(cachedir, false); | |
209 | for (std::vector<std::string>::const_iterator file = caches.begin(); file != caches.end(); ++file) | |
210 | { | |
211 | std::string nuke = flNotDir(*file); | |
212 | if (strncmp(cachefile.c_str(), nuke.c_str(), cachefile.length()) != 0) | |
213 | continue; | |
214 | RemoveFile("RemoveCaches", *file); | |
215 | } | |
216 | } | |
217 | } | |
218 | ||
219 | if (srcpkgcache.empty() == true) | |
220 | return; | |
221 | ||
222 | std::string cachedir = flNotFile(srcpkgcache); | |
223 | std::string cachefile = flNotDir(srcpkgcache); | |
224 | if (cachedir.empty() == true || cachefile.empty() == true || DirectoryExists(cachedir) == false) | |
225 | return; | |
226 | cachefile.append("."); | |
227 | std::vector<std::string> caches = GetListOfFilesInDir(cachedir, false); | |
228 | for (std::vector<std::string>::const_iterator file = caches.begin(); file != caches.end(); ++file) | |
229 | { | |
230 | std::string nuke = flNotDir(*file); | |
231 | if (strncmp(cachefile.c_str(), nuke.c_str(), cachefile.length()) != 0) | |
232 | continue; | |
233 | RemoveFile("RemoveCaches", *file); | |
234 | } | |
235 | } | |
236 | /*}}}*/ | |
237 | // CacheFile::Close - close the cache files /*{{{*/ | |
238 | // --------------------------------------------------------------------- | |
239 | /* */ | |
240 | void pkgCacheFile::Close() | |
241 | { | |
242 | if (ExternOwner == false) | |
243 | { | |
244 | delete DCache; | |
245 | delete Cache; | |
246 | delete Map; | |
247 | } | |
248 | else | |
249 | ExternOwner = false; | |
250 | delete Policy; | |
251 | delete SrcList; | |
252 | _system->UnLock(true); | |
253 | ||
254 | Map = NULL; | |
255 | DCache = NULL; | |
256 | Policy = NULL; | |
257 | Cache = NULL; | |
258 | SrcList = NULL; | |
259 | } | |
260 | /*}}}*/ |