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