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