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