merged from debian-sid
[ntk/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/acquire-item.h>
25 #include <apt-pkg/fileutl.h>
26 #include <apt-pkg/progress.h>
27
28 #include <apti18n.h>
29 /*}}}*/
30 // CacheFile::CacheFile - Constructor /*{{{*/
31 // ---------------------------------------------------------------------
32 /* */
33 pkgCacheFile::pkgCacheFile() : Map(NULL), Cache(NULL), DCache(NULL),
34 SrcList(NULL), Policy(NULL)
35 {
36 }
37 /*}}}*/
38 // CacheFile::~CacheFile - Destructor /*{{{*/
39 // ---------------------------------------------------------------------
40 /* */
41 pkgCacheFile::~pkgCacheFile()
42 {
43 delete DCache;
44 delete Policy;
45 delete SrcList;
46 delete Cache;
47 delete Map;
48 _system->UnLock(true);
49 }
50 /*}}}*/
51 // CacheFile::BuildCaches - Open and build the cache files /*{{{*/
52 // ---------------------------------------------------------------------
53 /* */
54 bool pkgCacheFile::BuildCaches(OpProgress *Progress, bool WithLock)
55 {
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
69 const bool ErrorWasEmpty = _error->empty();
70 if (WithLock == true)
71 if (_system->Lock() == false)
72 return false;
73
74 if (_config->FindB("Debug::NoLocking",false) == true)
75 WithLock = false;
76
77 if (_error->PendingError() == true)
78 return false;
79
80 BuildSourceList(Progress);
81
82 // Read the caches
83 bool Res = pkgCacheGenerator::MakeStatusCache(*SrcList,Progress,&Map,!WithLock);
84 if (Progress != NULL)
85 Progress->Done();
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 */
90 if (ErrorWasEmpty == true && _error->empty() == false)
91 _error->Warning(_("You may want to run apt-get update to correct these problems"));
92
93 Cache = new pkgCache(Map);
94 if (_error->PendingError() == true)
95 return false;
96 return true;
97 }
98 /*}}}*/
99 // CacheFile::BuildSourceList - Open and build all relevant sources.list/*{{{*/
100 // ---------------------------------------------------------------------
101 /* */
102 bool 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 /*}}}*/
113 // CacheFile::BuildPolicy - Open and build all relevant preferences /*{{{*/
114 // ---------------------------------------------------------------------
115 /* */
116 bool pkgCacheFile::BuildPolicy(OpProgress *Progress)
117 {
118 if (Policy != NULL)
119 return true;
120
121 Policy = new pkgPolicy(Cache);
122 if (_error->PendingError() == true)
123 return false;
124
125 if (ReadPinFile(*Policy) == false || ReadPinDir(*Policy) == false)
126 return false;
127
128 return true;
129 }
130 /*}}}*/
131 // CacheFile::BuildDepCache - Open and build the dependency cache /*{{{*/
132 // ---------------------------------------------------------------------
133 /* */
134 bool pkgCacheFile::BuildDepCache(OpProgress *Progress)
135 {
136 if (DCache != NULL)
137 return true;
138
139 DCache = new pkgDepCache(Cache,Policy);
140 if (_error->PendingError() == true)
141 return false;
142
143 DCache->Init(Progress);
144 return true;
145 }
146 /*}}}*/
147 // CacheFile::Open - Open the cache files, creating if necessary /*{{{*/
148 // ---------------------------------------------------------------------
149 /* */
150 bool 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();
163 if (_error->PendingError() == true)
164 return false;
165
166 return true;
167 }
168 /*}}}*/
169 // CacheFile::RemoveCaches - remove all cache files from disk /*{{{*/
170 // ---------------------------------------------------------------------
171 /* */
172 void 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 /*}}}*/
183 // CacheFile::Close - close the cache files /*{{{*/
184 // ---------------------------------------------------------------------
185 /* */
186 void pkgCacheFile::Close()
187 {
188 delete DCache;
189 delete Policy;
190 delete Cache;
191 delete SrcList;
192 delete Map;
193 _system->UnLock(true);
194
195 Map = NULL;
196 DCache = NULL;
197 Policy = NULL;
198 Cache = NULL;
199 SrcList = NULL;
200 }
201 /*}}}*/