]>
git.saurik.com Git - apt.git/blob - apt-pkg/pkgcachegen.cc
634179bdd4f20bd9ac0c56052fd395516bceca4c
1 // -*- mode: cpp; mode: fold -*-
3 // $Id: pkgcachegen.cc,v 1.4 1998/07/05 05:33:56 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 /*{{{*/
13 #include <pkglib/pkgcachegen.h>
14 #include <pkglib/error.h>
15 #include <pkglib/version.h>
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
)
26 if (_error
->PendingError() == true)
31 Map
.RawAllocate(sizeof(pkgCache::Header
));
32 *Cache
.HeaderP
= pkgCache::Header();
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]));
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()
45 if (_error
->PendingError() == true)
47 if (Map
.Sync() == false)
50 Cache
.HeaderP
->Dirty
= false;
51 Map
.Sync(0,sizeof(pkgCache::Header
));
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
)
62 while (List
.Step() == true)
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)
69 if (NewPackage(Pkg
,Package
) == false)
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)
79 if (List
.UsePackage(Pkg
,pkgCache::VerIterator(Cache
)) == false)
84 pkgCache::VerIterator Ver
= Pkg
.VersionList();
85 unsigned long *Last
= &Pkg
->VersionList
;
87 for (; Ver
.end() == false; Last
= &Ver
->NextVer
, Ver
++)
89 Res
= pkgVersionCompare(Version
.begin(),Version
.end(),Ver
.VerStr(),
90 Ver
.VerStr() + strlen(Ver
.VerStr()));
95 /* We already have a version for this item, record that we
99 if (List
.UsePackage(Pkg
,Ver
) == false)
102 if (NewFileVer(Ver
,List
) == false)
109 *Last
= NewVersion(Ver
,Version
,*Last
);
110 Ver
->ParentPkg
= Pkg
.Index();
111 if (List
.NewVersion(Ver
) == false)
114 if (List
.UsePackage(Pkg
,Ver
) == false)
117 if (NewFileVer(Ver
,List
) == false)
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
)
130 unsigned long Package
= Map
.Allocate(sizeof(pkgCache::Package
));
134 Pkg
= pkgCache::PkgIterator(Cache
,Cache
.PkgP
+ Package
);
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
;
141 // Set the name and the ID
142 Pkg
->Name
= Map
.WriteString(Name
);
145 Pkg
->ID
= Cache
.HeaderP
->PackageCount
++;
150 // CacheGenerator::NewFileVer - Create a new File<->Version association /*{{{*/
151 // ---------------------------------------------------------------------
153 bool pkgCacheGenerator::NewFileVer(pkgCache::VerIterator
&Ver
,
157 unsigned long VerFile
= Map
.Allocate(sizeof(pkgCache::VerFile
));
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();
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
,
179 unsigned long Version
= Map
.Allocate(sizeof(pkgCache::Version
));
184 Ver
= pkgCache::VerIterator(Cache
,Cache
.VerP
+ Version
);
186 Ver
->ID
= Cache
.HeaderP
->VersionCount
++;
187 Ver
->VerStr
= Map
.WriteString(VerStr
);
188 if (Ver
->VerStr
== 0)
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
,
203 pkgCache
&Cache
= Owner
->Cache
;
206 unsigned long Dependency
= Owner
->Map
.Allocate(sizeof(pkgCache::Dependency
));
211 pkgCache::DepIterator
Dep(Cache
,Cache
.DepP
+ Dependency
);
212 Dep
->ParentVer
= Ver
.Index();
215 Dep
->ID
= Cache
.HeaderP
->DependsCount
++;
217 // Locate the target package
218 pkgCache::PkgIterator Pkg
= Cache
.FindPkg(Package
);
219 if (Pkg
.end() == true)
220 if (Owner
->NewPackage(Pkg
,Package
) == false)
223 // Probe the reverse dependency list for a version string that matches
224 if (Version
.empty() == false)
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)
234 // Link it to the package
235 Dep
->Package
= Pkg
.Index();
236 Dep
->NextRevDepends
= Pkg
->RevDepends
;
237 Pkg
->RevDepends
= Dep
.Index();
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
;
249 // ListParser::NewProvides - Create a Provides element /*{{{*/
250 // ---------------------------------------------------------------------
252 bool pkgCacheGenerator::ListParser::NewProvides(pkgCache::VerIterator Ver
,
253 string Package
,string Version
)
255 pkgCache
&Cache
= Owner
->Cache
;
258 unsigned long Provides
= Owner
->Map
.Allocate(sizeof(pkgCache::Provides
));
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)
270 // Locate the target package
271 pkgCache::PkgIterator Pkg
= Cache
.FindPkg(Package
);
272 if (Pkg
.end() == true)
273 if (Owner
->NewPackage(Pkg
,Package
) == false)
276 // Link it to the package
277 Prv
->ParentPkg
= Pkg
.Index();
278 Prv
->NextProvides
= Pkg
->ProvidesList
;
279 Pkg
->ProvidesList
= Prv
.Index();
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
288 bool pkgCacheGenerator::SelectFile(string File
,unsigned long Flags
)
291 if (stat(File
.c_str(),&Buf
) == -1)
292 return _error
->Errno("stat","Couldn't stat ",File
.c_str());
294 // Get some space for the structure
295 CurrentFile
= Cache
.PkgFileP
+ Map
.Allocate(sizeof(*CurrentFile
));
296 if (CurrentFile
== Cache
.PkgFileP
)
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
;
307 if (CurrentFile
->FileName
== 0)
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
,
318 // Search for an insertion point
319 pkgCache::StringItem
*I
= Cache
.StringItemP
+ Cache
.HeaderP
->StringList
;
321 unsigned long *Last
= &Cache
.HeaderP
->StringList
;
322 for (; I
!= Cache
.StringItemP
; Last
= &I
->NextItem
,
323 I
= Cache
.StringItemP
+ I
->NextItem
)
325 Res
= strncmp(Cache
.StrP
+ I
->String
,S
,Size
);
326 if (Res
== 0 && *(Cache
.StrP
+ I
->String
+ Size
) != 0)
337 unsigned long Item
= Map
.Allocate(sizeof(pkgCache::StringItem
));
341 // Fill in the structure
342 pkgCache::StringItem
*ItemP
= Cache
.StringItemP
+ Item
;
343 ItemP
->NextItem
= I
- Cache
.StringItemP
;
345 ItemP
->String
= Map
.WriteString(S
,Size
);
346 if (ItemP
->String
== 0)
349 return ItemP
->String
;