Config class and source list
[ntk/apt.git] / apt-pkg / pkgcachegen.cc
CommitLineData
578bfd0a
AL
1// -*- mode: cpp; mode: fold -*-
2// Description /*{{{*/
9c14e3d6 3// $Id: pkgcachegen.cc,v 1.8 1998/07/09 05:12:27 jgg Exp $
578bfd0a
AL
4/* ######################################################################
5
6 Package Cache Generator - Generator for the cache structure.
7
8 This builds the cache structure from the abstract package list parser.
9
10 ##################################################################### */
11 /*}}}*/
12// Include Files /*{{{*/
6c139d6e
AL
13#ifdef __GNUG__
14#pragma implementation "pkglib/pkgcachegen.h"
15#endif
16
578bfd0a
AL
17#include <pkglib/pkgcachegen.h>
18#include <pkglib/error.h>
19#include <pkglib/version.h>
9c14e3d6 20#include <strutl.h>
578bfd0a
AL
21
22#include <sys/stat.h>
23#include <unistd.h>
24 /*}}}*/
25
26// CacheGenerator::pkgCacheGenerator - Constructor /*{{{*/
27// ---------------------------------------------------------------------
28/* We set the diry flag and make sure that is written to the disk */
29pkgCacheGenerator::pkgCacheGenerator(DynamicMMap &Map) : Map(Map), Cache(Map)
30{
31 if (_error->PendingError() == true)
32 return;
33
34 if (Map.Size() == 0)
35 {
36 Map.RawAllocate(sizeof(pkgCache::Header));
37 *Cache.HeaderP = pkgCache::Header();
38 }
39 Cache.HeaderP->Dirty = true;
40 Map.Sync(0,sizeof(pkgCache::Header));
41 Map.UsePools(*Cache.HeaderP->Pools,sizeof(Cache.HeaderP->Pools)/sizeof(Cache.HeaderP->Pools[0]));
42}
43 /*}}}*/
44// CacheGenerator::~pkgCacheGenerator - Destructor /*{{{*/
45// ---------------------------------------------------------------------
46/* We sync the data then unset the dirty flag in two steps so as to
47 advoid a problem during a crash */
48pkgCacheGenerator::~pkgCacheGenerator()
49{
50 if (_error->PendingError() == true)
51 return;
52 if (Map.Sync() == false)
53 return;
54
55 Cache.HeaderP->Dirty = false;
56 Map.Sync(0,sizeof(pkgCache::Header));
57}
58 /*}}}*/
59// CacheGenerator::MergeList - Merge the package list /*{{{*/
60// ---------------------------------------------------------------------
61/* This provides the generation of the entries in the cache. Each loop
62 goes through a single package record from the underlying parse engine. */
63bool pkgCacheGenerator::MergeList(ListParser &List)
64{
65 List.Owner = this;
0149949b
AL
66
67 while (List.Step() == true)
578bfd0a
AL
68 {
69 // Get a pointer to the package structure
9ddf7030
AL
70 string PackageName = List.Package();
71 pkgCache::PkgIterator Pkg;
72 Cache.FindPkg(PackageName);
f55a958f 73 if (Pkg.end() == true)
578bfd0a 74 {
9ddf7030 75 if (NewPackage(Pkg,PackageName) == false)
578bfd0a 76 return false;
578bfd0a 77 }
578bfd0a
AL
78
79 /* Get a pointer to the version structure. We know the list is sorted
80 so we use that fact in the search. Insertion of new versions is
81 done with correct sorting */
82 string Version = List.Version();
f55a958f
AL
83 if (Version.empty() == true)
84 {
85 if (List.UsePackage(Pkg,pkgCache::VerIterator(Cache)) == false)
86 return false;
87 continue;
88 }
89
578bfd0a
AL
90 pkgCache::VerIterator Ver = Pkg.VersionList();
91 unsigned long *Last = &Pkg->VersionList;
2246928b 92 int Res = 1;
f55a958f 93 for (; Ver.end() == false; Last = &Ver->NextVer, Ver++)
578bfd0a
AL
94 {
95 Res = pkgVersionCompare(Version.begin(),Version.end(),Ver.VerStr(),
96 Ver.VerStr() + strlen(Ver.VerStr()));
97 if (Res >= 0)
98 break;
99 }
100
101 /* We already have a version for this item, record that we
102 saw it */
103 if (Res == 0)
104 {
f55a958f
AL
105 if (List.UsePackage(Pkg,Ver) == false)
106 return false;
107
578bfd0a
AL
108 if (NewFileVer(Ver,List) == false)
109 return false;
110
111 continue;
112 }
113
114 // Add a new version
f55a958f
AL
115 *Last = NewVersion(Ver,Version,*Last);
116 Ver->ParentPkg = Pkg.Index();
578bfd0a
AL
117 if (List.NewVersion(Ver) == false)
118 return false;
0149949b 119
f55a958f
AL
120 if (List.UsePackage(Pkg,Ver) == false)
121 return false;
122
578bfd0a
AL
123 if (NewFileVer(Ver,List) == false)
124 return false;
125 }
0149949b 126
578bfd0a
AL
127 return true;
128}
129 /*}}}*/
130// CacheGenerator::NewPackage - Add a new package /*{{{*/
131// ---------------------------------------------------------------------
132/* This creates a new package structure and adds it to the hash table */
f55a958f 133bool pkgCacheGenerator::NewPackage(pkgCache::PkgIterator &Pkg,string Name)
578bfd0a
AL
134{
135 // Get a structure
136 unsigned long Package = Map.Allocate(sizeof(pkgCache::Package));
137 if (Package == 0)
138 return false;
139
f55a958f 140 Pkg = pkgCache::PkgIterator(Cache,Cache.PkgP + Package);
578bfd0a
AL
141
142 // Insert it into the hash table
f55a958f 143 unsigned long Hash = Cache.Hash(Name);
578bfd0a
AL
144 Pkg->NextPackage = Cache.HeaderP->HashTable[Hash];
145 Cache.HeaderP->HashTable[Hash] = Package;
146
147 // Set the name and the ID
148 Pkg->Name = Map.WriteString(Name);
149 if (Pkg->Name == 0)
150 return false;
151 Pkg->ID = Cache.HeaderP->PackageCount++;
152
153 return true;
154}
155 /*}}}*/
156// CacheGenerator::NewFileVer - Create a new File<->Version association /*{{{*/
157// ---------------------------------------------------------------------
158/* */
f55a958f 159bool pkgCacheGenerator::NewFileVer(pkgCache::VerIterator &Ver,
578bfd0a
AL
160 ListParser &List)
161{
dcb79bae
AL
162 // Get a structure
163 unsigned long VerFile = Map.Allocate(sizeof(pkgCache::VerFile));
164 if (VerFile == 0)
165 return 0;
166
167 pkgCache::VerFileIterator VF(Cache,Cache.VerFileP + VerFile);
168 VF->File = CurrentFile - Cache.PkgFileP;
169 VF->NextFile = Ver->FileList;
170 Ver->FileList = VF.Index();
171 VF->Offset = List.Offset();
172 VF->Size = List.Size();
173
f55a958f 174 return true;
578bfd0a
AL
175}
176 /*}}}*/
177// CacheGenerator::NewVersion - Create a new Version /*{{{*/
178// ---------------------------------------------------------------------
f55a958f 179/* This puts a version structure in the linked list */
578bfd0a 180unsigned long pkgCacheGenerator::NewVersion(pkgCache::VerIterator &Ver,
f55a958f 181 string VerStr,
578bfd0a
AL
182 unsigned long Next)
183{
f55a958f
AL
184 // Get a structure
185 unsigned long Version = Map.Allocate(sizeof(pkgCache::Version));
186 if (Version == 0)
0149949b 187 return 0;
f55a958f
AL
188
189 // Fill it in
190 Ver = pkgCache::VerIterator(Cache,Cache.VerP + Version);
f55a958f
AL
191 Ver->NextVer = Next;
192 Ver->ID = Cache.HeaderP->VersionCount++;
193 Ver->VerStr = Map.WriteString(VerStr);
194 if (Ver->VerStr == 0)
0149949b 195 return 0;
f55a958f 196
0149949b 197 return Version;
578bfd0a
AL
198}
199 /*}}}*/
dcb79bae
AL
200// ListParser::NewDepends - Create a dependency element /*{{{*/
201// ---------------------------------------------------------------------
202/* This creates a dependency element in the tree. It is linked to the
203 version and to the package that it is pointing to. */
204bool pkgCacheGenerator::ListParser::NewDepends(pkgCache::VerIterator Ver,
9ddf7030
AL
205 string PackageName,
206 string Version,
dcb79bae
AL
207 unsigned int Op,
208 unsigned int Type)
209{
210 pkgCache &Cache = Owner->Cache;
211
212 // Get a structure
213 unsigned long Dependency = Owner->Map.Allocate(sizeof(pkgCache::Dependency));
214 if (Dependency == 0)
215 return false;
216
217 // Fill it in
218 pkgCache::DepIterator Dep(Cache,Cache.DepP + Dependency);
219 Dep->ParentVer = Ver.Index();
220 Dep->Type = Type;
221 Dep->CompareOp = Op;
222 Dep->ID = Cache.HeaderP->DependsCount++;
223
224 // Locate the target package
9ddf7030 225 pkgCache::PkgIterator Pkg = Cache.FindPkg(PackageName);
dcb79bae 226 if (Pkg.end() == true)
9ddf7030 227 if (Owner->NewPackage(Pkg,PackageName) == false)
dcb79bae
AL
228 return false;
229
230 // Probe the reverse dependency list for a version string that matches
231 if (Version.empty() == false)
232 {
233 for (pkgCache::DepIterator I = Pkg.RevDependsList(); I.end() == false; I++)
234 if (I->Version != 0 && I.TargetVer() == Version)
235 Dep->Version = I->Version;
236 if (Dep->Version == 0)
237 if ((Dep->Version = WriteString(Version)) == 0)
238 return false;
239 }
240
241 // Link it to the package
242 Dep->Package = Pkg.Index();
243 Dep->NextRevDepends = Pkg->RevDepends;
244 Pkg->RevDepends = Dep.Index();
245
246 // Link it to the version (at the end of the list)
247 unsigned long *Last = &Ver->DependsList;
248 for (pkgCache::DepIterator D = Ver.DependsList(); D.end() == false; D++)
249 Last = &D->NextDepends;
250 Dep->NextDepends = *Last;
251 *Last = Dep.Index();
252
253 return true;
254}
255 /*}}}*/
256// ListParser::NewProvides - Create a Provides element /*{{{*/
257// ---------------------------------------------------------------------
258/* */
259bool pkgCacheGenerator::ListParser::NewProvides(pkgCache::VerIterator Ver,
9ddf7030
AL
260 string PackageName,
261 string Version)
dcb79bae
AL
262{
263 pkgCache &Cache = Owner->Cache;
264
265 // Get a structure
266 unsigned long Provides = Owner->Map.Allocate(sizeof(pkgCache::Provides));
267 if (Provides == 0)
268 return false;
269
270 // Fill it in
271 pkgCache::PrvIterator Prv(Cache,Cache.ProvideP + Provides,Cache.PkgP);
272 Prv->Version = Ver.Index();
273 Prv->NextPkgProv = Ver->ProvidesList;
274 Ver->ProvidesList = Prv.Index();
275 if (Version.empty() == false && (Prv->Version = WriteString(Version)) == 0)
276 return false;
277
278 // Locate the target package
9ddf7030 279 pkgCache::PkgIterator Pkg = Cache.FindPkg(PackageName);
dcb79bae 280 if (Pkg.end() == true)
9ddf7030 281 if (Owner->NewPackage(Pkg,PackageName) == false)
dcb79bae
AL
282 return false;
283
284 // Link it to the package
285 Prv->ParentPkg = Pkg.Index();
286 Prv->NextProvides = Pkg->ProvidesList;
287 Pkg->ProvidesList = Prv.Index();
288
289 return true;
290}
291 /*}}}*/
578bfd0a
AL
292// CacheGenerator::SelectFile - Select the current file being parsed /*{{{*/
293// ---------------------------------------------------------------------
294/* This is used to select which file is to be associated with all newly
295 added versions. */
296bool pkgCacheGenerator::SelectFile(string File,unsigned long Flags)
297{
298 struct stat Buf;
299 if (stat(File.c_str(),&Buf) == -1)
300 return _error->Errno("stat","Couldn't stat ",File.c_str());
301
302 // Get some space for the structure
303 CurrentFile = Cache.PkgFileP + Map.Allocate(sizeof(*CurrentFile));
304 if (CurrentFile == Cache.PkgFileP)
305 return false;
306
307 // Fill it in
308 CurrentFile->FileName = Map.WriteString(File);
309 CurrentFile->Size = Buf.st_size;
310 CurrentFile->mtime = Buf.st_mtime;
311 CurrentFile->NextFile = Cache.HeaderP->FileList;
312 CurrentFile->Flags = Flags;
313 PkgFileName = File;
314
315 if (CurrentFile->FileName == 0)
316 return false;
317}
318 /*}}}*/
f55a958f
AL
319// CacheGenerator::WriteUniqueString - Insert a unique string /*{{{*/
320// ---------------------------------------------------------------------
321/* This is used to create handles to strings. Given the same text it
322 always returns the same number */
323unsigned long pkgCacheGenerator::WriteUniqString(const char *S,
324 unsigned int Size)
325{
326 // Search for an insertion point
327 pkgCache::StringItem *I = Cache.StringItemP + Cache.HeaderP->StringList;
328 int Res = 1;
329 unsigned long *Last = &Cache.HeaderP->StringList;
330 for (; I != Cache.StringItemP; Last = &I->NextItem,
331 I = Cache.StringItemP + I->NextItem)
332 {
9c14e3d6 333 Res = stringcmp(S,S+Size,Cache.StrP + I->String);
f55a958f
AL
334 if (Res >= 0)
335 break;
336 }
337
338 // Match
339 if (Res == 0)
0149949b 340 return I->String;
f55a958f
AL
341
342 // Get a structure
343 unsigned long Item = Map.Allocate(sizeof(pkgCache::StringItem));
344 if (Item == 0)
0149949b
AL
345 return 0;
346
f55a958f
AL
347 // Fill in the structure
348 pkgCache::StringItem *ItemP = Cache.StringItemP + Item;
349 ItemP->NextItem = I - Cache.StringItemP;
350 *Last = Item;
351 ItemP->String = Map.WriteString(S,Size);
352 if (ItemP->String == 0)
0149949b 353 return 0;
f55a958f 354
0149949b 355 return ItemP->String;
f55a958f
AL
356}
357 /*}}}*/