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