enable APT in unpack/configure ordering to handle loops as well
[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
27 #include <apti18n.h>
28 /*}}}*/
29 // CacheFile::CacheFile - Constructor /*{{{*/
30 // ---------------------------------------------------------------------
31 /* */
32 pkgCacheFile::pkgCacheFile() : Map(NULL), Cache(NULL), DCache(NULL),
33 SrcList(NULL), Policy(NULL)
34 {
35 }
36 /*}}}*/
37 // CacheFile::~CacheFile - Destructor /*{{{*/
38 // ---------------------------------------------------------------------
39 /* */
40 pkgCacheFile::~pkgCacheFile()
41 {
42 delete DCache;
43 delete Policy;
44 delete SrcList;
45 delete Cache;
46 delete Map;
47 _system->UnLock(true);
48 }
49 /*}}}*/
50 // CacheFile::BuildCaches - Open and build the cache files /*{{{*/
51 // ---------------------------------------------------------------------
52 /* */
53 bool pkgCacheFile::BuildCaches(OpProgress *Progress, bool WithLock)
54 {
55 if (Cache != NULL)
56 return true;
57
58 if (_config->FindB("pkgCacheFile::Generate", true) == false)
59 {
60 Map = new MMap(*new FileFd(_config->FindFile("Dir::Cache::pkgcache"),
61 FileFd::ReadOnly),MMap::Public|MMap::ReadOnly);
62 Cache = new pkgCache(Map);
63 if (_error->PendingError() == true)
64 return false;
65 return true;
66 }
67
68 const bool ErrorWasEmpty = _error->empty();
69 if (WithLock == true)
70 if (_system->Lock() == false)
71 return false;
72
73 if (_config->FindB("Debug::NoLocking",false) == true)
74 WithLock = false;
75
76 if (_error->PendingError() == true)
77 return false;
78
79 BuildSourceList(Progress);
80
81 // Read the caches
82 bool Res = pkgCacheGenerator::MakeStatusCache(*SrcList,Progress,&Map,!WithLock);
83 if (Progress != NULL)
84 Progress->Done();
85 if (Res == false)
86 return _error->Error(_("The package lists or status file could not be parsed or opened."));
87
88 /* This sux, remove it someday */
89 if (ErrorWasEmpty == true && _error->empty() == false)
90 _error->Warning(_("You may want to run apt-get update to correct these problems"));
91
92 Cache = new pkgCache(Map);
93 if (_error->PendingError() == true)
94 return false;
95 return true;
96 }
97 /*}}}*/
98 // CacheFile::BuildSourceList - Open and build all relevant sources.list/*{{{*/
99 // ---------------------------------------------------------------------
100 /* */
101 bool pkgCacheFile::BuildSourceList(OpProgress *Progress)
102 {
103 if (SrcList != NULL)
104 return true;
105
106 SrcList = new pkgSourceList();
107 if (SrcList->ReadMainList() == false)
108 return _error->Error(_("The list of sources could not be read."));
109 return true;
110 }
111 /*}}}*/
112 // CacheFile::BuildPolicy - Open and build all relevant preferences /*{{{*/
113 // ---------------------------------------------------------------------
114 /* */
115 bool pkgCacheFile::BuildPolicy(OpProgress *Progress)
116 {
117 if (Policy != NULL)
118 return true;
119
120 Policy = new pkgPolicy(Cache);
121 if (_error->PendingError() == true)
122 return false;
123
124 if (ReadPinFile(*Policy) == false || ReadPinDir(*Policy) == false)
125 return false;
126
127 return true;
128 }
129 /*}}}*/
130 // CacheFile::BuildDepCache - Open and build the dependency cache /*{{{*/
131 // ---------------------------------------------------------------------
132 /* */
133 bool pkgCacheFile::BuildDepCache(OpProgress *Progress)
134 {
135 if (DCache != NULL)
136 return true;
137
138 DCache = new pkgDepCache(Cache,Policy);
139 if (_error->PendingError() == true)
140 return false;
141
142 DCache->Init(Progress);
143 return true;
144 }
145 /*}}}*/
146 // CacheFile::Open - Open the cache files, creating if necessary /*{{{*/
147 // ---------------------------------------------------------------------
148 /* */
149 bool pkgCacheFile::Open(OpProgress *Progress, bool WithLock)
150 {
151 if (BuildCaches(Progress,WithLock) == false)
152 return false;
153
154 if (BuildPolicy(Progress) == false)
155 return false;
156
157 if (BuildDepCache(Progress) == false)
158 return false;
159
160 if (Progress != NULL)
161 Progress->Done();
162 if (_error->PendingError() == true)
163 return false;
164
165 return true;
166 }
167 /*}}}*/
168 // CacheFile::RemoveCaches - remove all cache files from disk /*{{{*/
169 // ---------------------------------------------------------------------
170 /* */
171 void pkgCacheFile::RemoveCaches()
172 {
173 std::string const pkgcache = _config->FindFile("Dir::cache::pkgcache");
174 std::string const srcpkgcache = _config->FindFile("Dir::cache::srcpkgcache");
175
176 if (pkgcache.empty() == false && RealFileExists(pkgcache) == true)
177 unlink(pkgcache.c_str());
178 if (srcpkgcache.empty() == false && RealFileExists(srcpkgcache) == true)
179 unlink(srcpkgcache.c_str());
180 }
181 /*}}}*/
182 // CacheFile::Close - close the cache files /*{{{*/
183 // ---------------------------------------------------------------------
184 /* */
185 void pkgCacheFile::Close()
186 {
187 delete DCache;
188 delete Policy;
189 delete Cache;
190 delete SrcList;
191 delete Map;
192 _system->UnLock(true);
193
194 Map = NULL;
195 DCache = NULL;
196 Policy = NULL;
197 Cache = NULL;
198 SrcList = NULL;
199 }
200 /*}}}*/