]> git.saurik.com Git - apt.git/blame - apt-pkg/cachefile.cc
use forward declaration in headers if possible instead of includes
[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/* */
3f8621c5 33pkgCacheFile::pkgCacheFile() : 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());
181}
182 /*}}}*/
b2e465d6
AL
183// CacheFile::Close - close the cache files /*{{{*/
184// ---------------------------------------------------------------------
185/* */
186void pkgCacheFile::Close()
187{
188 delete DCache;
189 delete Policy;
190 delete Cache;
3f8621c5 191 delete SrcList;
b2e465d6
AL
192 delete Map;
193 _system->UnLock(true);
194
3f8621c5
DK
195 Map = NULL;
196 DCache = NULL;
197 Policy = NULL;
198 Cache = NULL;
199 SrcList = NULL;
b2e465d6
AL
200}
201 /*}}}*/