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