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