]> git.saurik.com Git - apt.git/blob - apt-pkg/pkgcachegen.cc
Class File name change to FileFd
[apt.git] / apt-pkg / pkgcachegen.cc
1 // -*- mode: cpp; mode: fold -*-
2 // Description /*{{{*/
3 // $Id: pkgcachegen.cc,v 1.11 1998/07/19 04:22:02 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 if (Cache.HeaderP->MaxVerFileSize < VF->Size)
174 Cache.HeaderP->MaxVerFileSize = VF->Size;
175 return true;
176 }
177 /*}}}*/
178 // CacheGenerator::NewVersion - Create a new Version /*{{{*/
179 // ---------------------------------------------------------------------
180 /* This puts a version structure in the linked list */
181 unsigned long pkgCacheGenerator::NewVersion(pkgCache::VerIterator &Ver,
182 string VerStr,
183 unsigned long Next)
184 {
185 // Get a structure
186 unsigned long Version = Map.Allocate(sizeof(pkgCache::Version));
187 if (Version == 0)
188 return 0;
189
190 // Fill it in
191 Ver = pkgCache::VerIterator(Cache,Cache.VerP + Version);
192 Ver->NextVer = Next;
193 Ver->ID = Cache.HeaderP->VersionCount++;
194 Ver->VerStr = Map.WriteString(VerStr);
195 if (Ver->VerStr == 0)
196 return 0;
197
198 return Version;
199 }
200 /*}}}*/
201 // ListParser::NewDepends - Create a dependency element /*{{{*/
202 // ---------------------------------------------------------------------
203 /* This creates a dependency element in the tree. It is linked to the
204 version and to the package that it is pointing to. */
205 bool pkgCacheGenerator::ListParser::NewDepends(pkgCache::VerIterator Ver,
206 string PackageName,
207 string Version,
208 unsigned int Op,
209 unsigned int Type)
210 {
211 pkgCache &Cache = Owner->Cache;
212
213 // Get a structure
214 unsigned long Dependency = Owner->Map.Allocate(sizeof(pkgCache::Dependency));
215 if (Dependency == 0)
216 return false;
217
218 // Fill it in
219 pkgCache::DepIterator Dep(Cache,Cache.DepP + Dependency);
220 Dep->ParentVer = Ver.Index();
221 Dep->Type = Type;
222 Dep->CompareOp = Op;
223 Dep->ID = Cache.HeaderP->DependsCount++;
224
225 // Locate the target package
226 pkgCache::PkgIterator Pkg;
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 // We do not add self referencing provides
266 if (Ver.ParentPkg().Name() == PackageName)
267 return true;
268
269 // Get a structure
270 unsigned long Provides = Owner->Map.Allocate(sizeof(pkgCache::Provides));
271 if (Provides == 0)
272 return false;
273
274 // Fill it in
275 pkgCache::PrvIterator Prv(Cache,Cache.ProvideP + Provides,Cache.PkgP);
276 Prv->Version = Ver.Index();
277 Prv->NextPkgProv = Ver->ProvidesList;
278 Ver->ProvidesList = Prv.Index();
279 if (Version.empty() == false && (Prv->Version = WriteString(Version)) == 0)
280 return false;
281
282 // Locate the target package
283 pkgCache::PkgIterator Pkg;
284 if (Owner->NewPackage(Pkg,PackageName) == false)
285 return false;
286
287 // Link it to the package
288 Prv->ParentPkg = Pkg.Index();
289 Prv->NextProvides = Pkg->ProvidesList;
290 Pkg->ProvidesList = Prv.Index();
291
292 return true;
293 }
294 /*}}}*/
295 // CacheGenerator::SelectFile - Select the current file being parsed /*{{{*/
296 // ---------------------------------------------------------------------
297 /* This is used to select which file is to be associated with all newly
298 added versions. */
299 bool pkgCacheGenerator::SelectFile(string File,unsigned long Flags)
300 {
301 struct stat Buf;
302 if (stat(File.c_str(),&Buf) == -1)
303 return _error->Errno("stat","Couldn't stat ",File.c_str());
304
305 // Get some space for the structure
306 CurrentFile = Cache.PkgFileP + Map.Allocate(sizeof(*CurrentFile));
307 if (CurrentFile == Cache.PkgFileP)
308 return false;
309
310 // Fill it in
311 CurrentFile->FileName = Map.WriteString(File);
312 CurrentFile->Size = Buf.st_size;
313 CurrentFile->mtime = Buf.st_mtime;
314 CurrentFile->NextFile = Cache.HeaderP->FileList;
315 CurrentFile->Flags = Flags;
316 PkgFileName = File;
317 Cache.HeaderP->FileList = CurrentFile - Cache.PkgFileP;
318
319 if (CurrentFile->FileName == 0)
320 return false;
321 return true;
322 }
323 /*}}}*/
324 // CacheGenerator::WriteUniqueString - Insert a unique string /*{{{*/
325 // ---------------------------------------------------------------------
326 /* This is used to create handles to strings. Given the same text it
327 always returns the same number */
328 unsigned long pkgCacheGenerator::WriteUniqString(const char *S,
329 unsigned int Size)
330 {
331 // Search for an insertion point
332 pkgCache::StringItem *I = Cache.StringItemP + Cache.HeaderP->StringList;
333 int Res = 1;
334 unsigned long *Last = &Cache.HeaderP->StringList;
335 for (; I != Cache.StringItemP; Last = &I->NextItem,
336 I = Cache.StringItemP + I->NextItem)
337 {
338 Res = stringcmp(S,S+Size,Cache.StrP + I->String);
339 if (Res >= 0)
340 break;
341 }
342
343 // Match
344 if (Res == 0)
345 return I->String;
346
347 // Get a structure
348 unsigned long Item = Map.Allocate(sizeof(pkgCache::StringItem));
349 if (Item == 0)
350 return 0;
351
352 // Fill in the structure
353 pkgCache::StringItem *ItemP = Cache.StringItemP + Item;
354 ItemP->NextItem = I - Cache.StringItemP;
355 *Last = Item;
356 ItemP->String = Map.WriteString(S,Size);
357 if (ItemP->String == 0)
358 return 0;
359
360 return ItemP->String;
361 }
362 /*}}}*/