]> git.saurik.com Git - apt.git/blob - apt-pkg/pkgcachegen.cc
Working cache generator
[apt.git] / apt-pkg / pkgcachegen.cc
1 // -*- mode: cpp; mode: fold -*-
2 // Description /*{{{*/
3 // $Id: pkgcachegen.cc,v 1.10 1998/07/16 06:08:38 jgg Exp $
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 /*{{{*/
13 #ifdef __GNUG__
14 #pragma implementation "apt-pkg/pkgcachegen.h"
15 #endif
16
17 #include <apt-pkg/pkgcachegen.h>
18 #include <apt-pkg/error.h>
19 #include <apt-pkg/version.h>
20 #include <strutl.h>
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 */
29 pkgCacheGenerator::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 */
48 pkgCacheGenerator::~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. */
63 bool pkgCacheGenerator::MergeList(ListParser &List)
64 {
65 List.Owner = this;
66
67 while (List.Step() == true)
68 {
69 // Get a pointer to the package structure
70 string PackageName = List.Package();
71 pkgCache::PkgIterator Pkg;
72 if (NewPackage(Pkg,PackageName) == false)
73 return false;
74
75 /* Get a pointer to the version structure. We know the list is sorted
76 so we use that fact in the search. Insertion of new versions is
77 done with correct sorting */
78 string Version = List.Version();
79 if (Version.empty() == true)
80 {
81 if (List.UsePackage(Pkg,pkgCache::VerIterator(Cache)) == false)
82 return false;
83 continue;
84 }
85
86 pkgCache::VerIterator Ver = Pkg.VersionList();
87 unsigned long *Last = &Pkg->VersionList;
88 int Res = 1;
89 for (; Ver.end() == false; Last = &Ver->NextVer, Ver++)
90 {
91 Res = pkgVersionCompare(Version.begin(),Version.end(),Ver.VerStr(),
92 Ver.VerStr() + strlen(Ver.VerStr()));
93 if (Res >= 0)
94 break;
95 }
96
97 /* We already have a version for this item, record that we
98 saw it */
99 if (Res == 0)
100 {
101 if (List.UsePackage(Pkg,Ver) == false)
102 return false;
103
104 if (NewFileVer(Ver,List) == false)
105 return false;
106
107 continue;
108 }
109
110 // Add a new version
111 *Last = NewVersion(Ver,Version,*Last);
112 Ver->ParentPkg = Pkg.Index();
113 if (List.NewVersion(Ver) == false)
114 return false;
115
116 if (List.UsePackage(Pkg,Ver) == false)
117 return false;
118
119 if (NewFileVer(Ver,List) == false)
120 return false;
121 }
122
123 return true;
124 }
125 /*}}}*/
126 // CacheGenerator::NewPackage - Add a new package /*{{{*/
127 // ---------------------------------------------------------------------
128 /* This creates a new package structure and adds it to the hash table */
129 bool pkgCacheGenerator::NewPackage(pkgCache::PkgIterator &Pkg,string Name)
130 {
131 Pkg = Cache.FindPkg(Name);
132 if (Pkg.end() == false)
133 return true;
134
135 // Get a structure
136 unsigned long Package = Map.Allocate(sizeof(pkgCache::Package));
137 if (Package == 0)
138 return false;
139
140 Pkg = pkgCache::PkgIterator(Cache,Cache.PkgP + Package);
141
142 // Insert it into the hash table
143 unsigned long Hash = Cache.Hash(Name);
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 /* */
159 bool pkgCacheGenerator::NewFileVer(pkgCache::VerIterator &Ver,
160 ListParser &List)
161 {
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
174 return true;
175 }
176 /*}}}*/
177 // CacheGenerator::NewVersion - Create a new Version /*{{{*/
178 // ---------------------------------------------------------------------
179 /* This puts a version structure in the linked list */
180 unsigned long pkgCacheGenerator::NewVersion(pkgCache::VerIterator &Ver,
181 string VerStr,
182 unsigned long Next)
183 {
184 // Get a structure
185 unsigned long Version = Map.Allocate(sizeof(pkgCache::Version));
186 if (Version == 0)
187 return 0;
188
189 // Fill it in
190 Ver = pkgCache::VerIterator(Cache,Cache.VerP + Version);
191 Ver->NextVer = Next;
192 Ver->ID = Cache.HeaderP->VersionCount++;
193 Ver->VerStr = Map.WriteString(VerStr);
194 if (Ver->VerStr == 0)
195 return 0;
196
197 return Version;
198 }
199 /*}}}*/
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. */
204 bool pkgCacheGenerator::ListParser::NewDepends(pkgCache::VerIterator Ver,
205 string PackageName,
206 string Version,
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
225 pkgCache::PkgIterator Pkg;
226 if (Owner->NewPackage(Pkg,PackageName) == false)
227 return false;
228
229 // Probe the reverse dependency list for a version string that matches
230 if (Version.empty() == false)
231 {
232 for (pkgCache::DepIterator I = Pkg.RevDependsList(); I.end() == false; I++)
233 if (I->Version != 0 && I.TargetVer() == Version)
234 Dep->Version = I->Version;
235 if (Dep->Version == 0)
236 if ((Dep->Version = WriteString(Version)) == 0)
237 return false;
238 }
239
240 // Link it to the package
241 Dep->Package = Pkg.Index();
242 Dep->NextRevDepends = Pkg->RevDepends;
243 Pkg->RevDepends = Dep.Index();
244
245 // Link it to the version (at the end of the list)
246 unsigned long *Last = &Ver->DependsList;
247 for (pkgCache::DepIterator D = Ver.DependsList(); D.end() == false; D++)
248 Last = &D->NextDepends;
249 Dep->NextDepends = *Last;
250 *Last = Dep.Index();
251
252 return true;
253 }
254 /*}}}*/
255 // ListParser::NewProvides - Create a Provides element /*{{{*/
256 // ---------------------------------------------------------------------
257 /* */
258 bool pkgCacheGenerator::ListParser::NewProvides(pkgCache::VerIterator Ver,
259 string PackageName,
260 string Version)
261 {
262 pkgCache &Cache = Owner->Cache;
263
264 // We do not add self referencing provides
265 if (Ver.ParentPkg().Name() == PackageName)
266 return true;
267
268 // Get a structure
269 unsigned long Provides = Owner->Map.Allocate(sizeof(pkgCache::Provides));
270 if (Provides == 0)
271 return false;
272
273 // Fill it in
274 pkgCache::PrvIterator Prv(Cache,Cache.ProvideP + Provides,Cache.PkgP);
275 Prv->Version = Ver.Index();
276 Prv->NextPkgProv = Ver->ProvidesList;
277 Ver->ProvidesList = Prv.Index();
278 if (Version.empty() == false && (Prv->Version = WriteString(Version)) == 0)
279 return false;
280
281 // Locate the target package
282 pkgCache::PkgIterator Pkg;
283 if (Owner->NewPackage(Pkg,PackageName) == false)
284 return false;
285
286 // Link it to the package
287 Prv->ParentPkg = Pkg.Index();
288 Prv->NextProvides = Pkg->ProvidesList;
289 Pkg->ProvidesList = Prv.Index();
290
291 return true;
292 }
293 /*}}}*/
294 // CacheGenerator::SelectFile - Select the current file being parsed /*{{{*/
295 // ---------------------------------------------------------------------
296 /* This is used to select which file is to be associated with all newly
297 added versions. */
298 bool pkgCacheGenerator::SelectFile(string File,unsigned long Flags)
299 {
300 struct stat Buf;
301 if (stat(File.c_str(),&Buf) == -1)
302 return _error->Errno("stat","Couldn't stat ",File.c_str());
303
304 // Get some space for the structure
305 CurrentFile = Cache.PkgFileP + Map.Allocate(sizeof(*CurrentFile));
306 if (CurrentFile == Cache.PkgFileP)
307 return false;
308
309 // Fill it in
310 CurrentFile->FileName = Map.WriteString(File);
311 CurrentFile->Size = Buf.st_size;
312 CurrentFile->mtime = Buf.st_mtime;
313 CurrentFile->NextFile = Cache.HeaderP->FileList;
314 CurrentFile->Flags = Flags;
315 PkgFileName = File;
316
317 if (CurrentFile->FileName == 0)
318 return false;
319 return true;
320 }
321 /*}}}*/
322 // CacheGenerator::WriteUniqueString - Insert a unique string /*{{{*/
323 // ---------------------------------------------------------------------
324 /* This is used to create handles to strings. Given the same text it
325 always returns the same number */
326 unsigned long pkgCacheGenerator::WriteUniqString(const char *S,
327 unsigned int Size)
328 {
329 // Search for an insertion point
330 pkgCache::StringItem *I = Cache.StringItemP + Cache.HeaderP->StringList;
331 int Res = 1;
332 unsigned long *Last = &Cache.HeaderP->StringList;
333 for (; I != Cache.StringItemP; Last = &I->NextItem,
334 I = Cache.StringItemP + I->NextItem)
335 {
336 Res = stringcmp(S,S+Size,Cache.StrP + I->String);
337 if (Res >= 0)
338 break;
339 }
340
341 // Match
342 if (Res == 0)
343 return I->String;
344
345 // Get a structure
346 unsigned long Item = Map.Allocate(sizeof(pkgCache::StringItem));
347 if (Item == 0)
348 return 0;
349
350 // Fill in the structure
351 pkgCache::StringItem *ItemP = Cache.StringItemP + Item;
352 ItemP->NextItem = I - Cache.StringItemP;
353 *Last = Item;
354 ItemP->String = Map.WriteString(S,Size);
355 if (ItemP->String == 0)
356 return 0;
357
358 return ItemP->String;
359 }
360 /*}}}*/