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