]>
git.saurik.com Git - apt.git/blob - apt-pkg/pkgcachegen.cc
aac3f77d9596299cc074db051daa2e898d28c927
1 // -*- mode: cpp; mode: fold -*-
3 // $Id: pkgcachegen.cc,v 1.7 1998/07/07 04:17:04 jgg Exp $
4 /* ######################################################################
6 Package Cache Generator - Generator for the cache structure.
8 This builds the cache structure from the abstract package list parser.
10 ##################################################################### */
12 // Include Files /*{{{*/
14 #pragma implementation "pkglib/pkgcachegen.h"
17 #include <pkglib/pkgcachegen.h>
18 #include <pkglib/error.h>
19 #include <pkglib/version.h>
25 // CacheGenerator::pkgCacheGenerator - Constructor /*{{{*/
26 // ---------------------------------------------------------------------
27 /* We set the diry flag and make sure that is written to the disk */
28 pkgCacheGenerator::pkgCacheGenerator(DynamicMMap
&Map
) : Map(Map
), Cache(Map
)
30 if (_error
->PendingError() == true)
35 Map
.RawAllocate(sizeof(pkgCache::Header
));
36 *Cache
.HeaderP
= pkgCache::Header();
38 Cache
.HeaderP
->Dirty
= true;
39 Map
.Sync(0,sizeof(pkgCache::Header
));
40 Map
.UsePools(*Cache
.HeaderP
->Pools
,sizeof(Cache
.HeaderP
->Pools
)/sizeof(Cache
.HeaderP
->Pools
[0]));
43 // CacheGenerator::~pkgCacheGenerator - Destructor /*{{{*/
44 // ---------------------------------------------------------------------
45 /* We sync the data then unset the dirty flag in two steps so as to
46 advoid a problem during a crash */
47 pkgCacheGenerator::~pkgCacheGenerator()
49 if (_error
->PendingError() == true)
51 if (Map
.Sync() == false)
54 Cache
.HeaderP
->Dirty
= false;
55 Map
.Sync(0,sizeof(pkgCache::Header
));
58 // CacheGenerator::MergeList - Merge the package list /*{{{*/
59 // ---------------------------------------------------------------------
60 /* This provides the generation of the entries in the cache. Each loop
61 goes through a single package record from the underlying parse engine. */
62 bool pkgCacheGenerator::MergeList(ListParser
&List
)
66 while (List
.Step() == true)
68 // Get a pointer to the package structure
69 string PackageName
= List
.Package();
70 pkgCache::PkgIterator Pkg
;
71 Cache
.FindPkg(PackageName
);
72 if (Pkg
.end() == true)
74 if (NewPackage(Pkg
,PackageName
) == false)
78 /* Get a pointer to the version structure. We know the list is sorted
79 so we use that fact in the search. Insertion of new versions is
80 done with correct sorting */
81 string Version
= List
.Version();
82 if (Version
.empty() == true)
84 if (List
.UsePackage(Pkg
,pkgCache::VerIterator(Cache
)) == false)
89 pkgCache::VerIterator Ver
= Pkg
.VersionList();
90 unsigned long *Last
= &Pkg
->VersionList
;
92 for (; Ver
.end() == false; Last
= &Ver
->NextVer
, Ver
++)
94 Res
= pkgVersionCompare(Version
.begin(),Version
.end(),Ver
.VerStr(),
95 Ver
.VerStr() + strlen(Ver
.VerStr()));
100 /* We already have a version for this item, record that we
104 if (List
.UsePackage(Pkg
,Ver
) == false)
107 if (NewFileVer(Ver
,List
) == false)
114 *Last
= NewVersion(Ver
,Version
,*Last
);
115 Ver
->ParentPkg
= Pkg
.Index();
116 if (List
.NewVersion(Ver
) == false)
119 if (List
.UsePackage(Pkg
,Ver
) == false)
122 if (NewFileVer(Ver
,List
) == false)
129 // CacheGenerator::NewPackage - Add a new package /*{{{*/
130 // ---------------------------------------------------------------------
131 /* This creates a new package structure and adds it to the hash table */
132 bool pkgCacheGenerator::NewPackage(pkgCache::PkgIterator
&Pkg
,string Name
)
135 unsigned long Package
= Map
.Allocate(sizeof(pkgCache::Package
));
139 Pkg
= pkgCache::PkgIterator(Cache
,Cache
.PkgP
+ Package
);
141 // Insert it into the hash table
142 unsigned long Hash
= Cache
.Hash(Name
);
143 Pkg
->NextPackage
= Cache
.HeaderP
->HashTable
[Hash
];
144 Cache
.HeaderP
->HashTable
[Hash
] = Package
;
146 // Set the name and the ID
147 Pkg
->Name
= Map
.WriteString(Name
);
150 Pkg
->ID
= Cache
.HeaderP
->PackageCount
++;
155 // CacheGenerator::NewFileVer - Create a new File<->Version association /*{{{*/
156 // ---------------------------------------------------------------------
158 bool pkgCacheGenerator::NewFileVer(pkgCache::VerIterator
&Ver
,
162 unsigned long VerFile
= Map
.Allocate(sizeof(pkgCache::VerFile
));
166 pkgCache::VerFileIterator
VF(Cache
,Cache
.VerFileP
+ VerFile
);
167 VF
->File
= CurrentFile
- Cache
.PkgFileP
;
168 VF
->NextFile
= Ver
->FileList
;
169 Ver
->FileList
= VF
.Index();
170 VF
->Offset
= List
.Offset();
171 VF
->Size
= List
.Size();
176 // CacheGenerator::NewVersion - Create a new Version /*{{{*/
177 // ---------------------------------------------------------------------
178 /* This puts a version structure in the linked list */
179 unsigned long pkgCacheGenerator::NewVersion(pkgCache::VerIterator
&Ver
,
184 unsigned long Version
= Map
.Allocate(sizeof(pkgCache::Version
));
189 Ver
= pkgCache::VerIterator(Cache
,Cache
.VerP
+ Version
);
191 Ver
->ID
= Cache
.HeaderP
->VersionCount
++;
192 Ver
->VerStr
= Map
.WriteString(VerStr
);
193 if (Ver
->VerStr
== 0)
199 // ListParser::NewDepends - Create a dependency element /*{{{*/
200 // ---------------------------------------------------------------------
201 /* This creates a dependency element in the tree. It is linked to the
202 version and to the package that it is pointing to. */
203 bool pkgCacheGenerator::ListParser::NewDepends(pkgCache::VerIterator Ver
,
209 pkgCache
&Cache
= Owner
->Cache
;
212 unsigned long Dependency
= Owner
->Map
.Allocate(sizeof(pkgCache::Dependency
));
217 pkgCache::DepIterator
Dep(Cache
,Cache
.DepP
+ Dependency
);
218 Dep
->ParentVer
= Ver
.Index();
221 Dep
->ID
= Cache
.HeaderP
->DependsCount
++;
223 // Locate the target package
224 pkgCache::PkgIterator Pkg
= Cache
.FindPkg(PackageName
);
225 if (Pkg
.end() == true)
226 if (Owner
->NewPackage(Pkg
,PackageName
) == false)
229 // Probe the reverse dependency list for a version string that matches
230 if (Version
.empty() == false)
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)
240 // Link it to the package
241 Dep
->Package
= Pkg
.Index();
242 Dep
->NextRevDepends
= Pkg
->RevDepends
;
243 Pkg
->RevDepends
= Dep
.Index();
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
;
255 // ListParser::NewProvides - Create a Provides element /*{{{*/
256 // ---------------------------------------------------------------------
258 bool pkgCacheGenerator::ListParser::NewProvides(pkgCache::VerIterator Ver
,
262 pkgCache
&Cache
= Owner
->Cache
;
265 unsigned long Provides
= Owner
->Map
.Allocate(sizeof(pkgCache::Provides
));
270 pkgCache::PrvIterator
Prv(Cache
,Cache
.ProvideP
+ Provides
,Cache
.PkgP
);
271 Prv
->Version
= Ver
.Index();
272 Prv
->NextPkgProv
= Ver
->ProvidesList
;
273 Ver
->ProvidesList
= Prv
.Index();
274 if (Version
.empty() == false && (Prv
->Version
= WriteString(Version
)) == 0)
277 // Locate the target package
278 pkgCache::PkgIterator Pkg
= Cache
.FindPkg(PackageName
);
279 if (Pkg
.end() == true)
280 if (Owner
->NewPackage(Pkg
,PackageName
) == false)
283 // Link it to the package
284 Prv
->ParentPkg
= Pkg
.Index();
285 Prv
->NextProvides
= Pkg
->ProvidesList
;
286 Pkg
->ProvidesList
= Prv
.Index();
291 // CacheGenerator::SelectFile - Select the current file being parsed /*{{{*/
292 // ---------------------------------------------------------------------
293 /* This is used to select which file is to be associated with all newly
295 bool pkgCacheGenerator::SelectFile(string File
,unsigned long Flags
)
298 if (stat(File
.c_str(),&Buf
) == -1)
299 return _error
->Errno("stat","Couldn't stat ",File
.c_str());
301 // Get some space for the structure
302 CurrentFile
= Cache
.PkgFileP
+ Map
.Allocate(sizeof(*CurrentFile
));
303 if (CurrentFile
== Cache
.PkgFileP
)
307 CurrentFile
->FileName
= Map
.WriteString(File
);
308 CurrentFile
->Size
= Buf
.st_size
;
309 CurrentFile
->mtime
= Buf
.st_mtime
;
310 CurrentFile
->NextFile
= Cache
.HeaderP
->FileList
;
311 CurrentFile
->Flags
= Flags
;
314 if (CurrentFile
->FileName
== 0)
318 // CacheGenerator::WriteUniqueString - Insert a unique string /*{{{*/
319 // ---------------------------------------------------------------------
320 /* This is used to create handles to strings. Given the same text it
321 always returns the same number */
322 unsigned long pkgCacheGenerator::WriteUniqString(const char *S
,
325 // Search for an insertion point
326 pkgCache::StringItem
*I
= Cache
.StringItemP
+ Cache
.HeaderP
->StringList
;
328 unsigned long *Last
= &Cache
.HeaderP
->StringList
;
329 for (; I
!= Cache
.StringItemP
; Last
= &I
->NextItem
,
330 I
= Cache
.StringItemP
+ I
->NextItem
)
332 Res
= strncmp(Cache
.StrP
+ I
->String
,S
,Size
);
333 if (Res
== 0 && *(Cache
.StrP
+ I
->String
+ Size
) != 0)
344 unsigned long Item
= Map
.Allocate(sizeof(pkgCache::StringItem
));
348 // Fill in the structure
349 pkgCache::StringItem
*ItemP
= Cache
.StringItemP
+ Item
;
350 ItemP
->NextItem
= I
- Cache
.StringItemP
;
352 ItemP
->String
= Map
.WriteString(S
,Size
);
353 if (ItemP
->String
== 0)
356 return ItemP
->String
;