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