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