]> git.saurik.com Git - apt.git/blob - apt-pkg/pkgcachegen.cc
New make changes
[apt.git] / apt-pkg / pkgcachegen.cc
1 // -*- mode: cpp; mode: fold -*-
2 // Description /*{{{*/
3 // $Id: pkgcachegen.cc,v 1.9 1998/07/12 23:58:34 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 Cache.FindPkg(PackageName);
73 if (Pkg.end() == true)
74 {
75 if (NewPackage(Pkg,PackageName) == false)
76 return false;
77 }
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();
83 if (Version.empty() == true)
84 {
85 if (List.UsePackage(Pkg,pkgCache::VerIterator(Cache)) == false)
86 return false;
87 continue;
88 }
89
90 pkgCache::VerIterator Ver = Pkg.VersionList();
91 unsigned long *Last = &Pkg->VersionList;
92 int Res = 1;
93 for (; Ver.end() == false; Last = &Ver->NextVer, Ver++)
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 {
105 if (List.UsePackage(Pkg,Ver) == false)
106 return false;
107
108 if (NewFileVer(Ver,List) == false)
109 return false;
110
111 continue;
112 }
113
114 // Add a new version
115 *Last = NewVersion(Ver,Version,*Last);
116 Ver->ParentPkg = Pkg.Index();
117 if (List.NewVersion(Ver) == false)
118 return false;
119
120 if (List.UsePackage(Pkg,Ver) == false)
121 return false;
122
123 if (NewFileVer(Ver,List) == false)
124 return false;
125 }
126
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 */
133 bool pkgCacheGenerator::NewPackage(pkgCache::PkgIterator &Pkg,string Name)
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 = Cache.FindPkg(PackageName);
226 if (Pkg.end() == true)
227 if (Owner->NewPackage(Pkg,PackageName) == false)
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 /* */
259 bool pkgCacheGenerator::ListParser::NewProvides(pkgCache::VerIterator Ver,
260 string PackageName,
261 string Version)
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
279 pkgCache::PkgIterator Pkg = Cache.FindPkg(PackageName);
280 if (Pkg.end() == true)
281 if (Owner->NewPackage(Pkg,PackageName) == false)
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 /*}}}*/
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. */
296 bool 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 /*}}}*/
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 */
323 unsigned 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 {
333 Res = stringcmp(S,S+Size,Cache.StrP + I->String);
334 if (Res >= 0)
335 break;
336 }
337
338 // Match
339 if (Res == 0)
340 return I->String;
341
342 // Get a structure
343 unsigned long Item = Map.Allocate(sizeof(pkgCache::StringItem));
344 if (Item == 0)
345 return 0;
346
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)
353 return 0;
354
355 return ItemP->String;
356 }
357 /*}}}*/