]>
git.saurik.com Git - apt.git/blob - apt-pkg/pkgcachegen.cc
acc05f133f25e37133f985b665160d9d22aec9a6
1 // -*- mode: cpp; mode: fold -*-
3 // $Id: pkgcachegen.cc,v 1.6 1998/07/05 05:53:52 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 PackageName
= List
.Package();
66 pkgCache::PkgIterator Pkg
;
67 Cache
.FindPkg(PackageName
);
68 if (Pkg
.end() == true)
70 if (NewPackage(Pkg
,PackageName
) == false)
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)
80 if (List
.UsePackage(Pkg
,pkgCache::VerIterator(Cache
)) == false)
85 pkgCache::VerIterator Ver
= Pkg
.VersionList();
86 unsigned long *Last
= &Pkg
->VersionList
;
88 for (; Ver
.end() == false; Last
= &Ver
->NextVer
, Ver
++)
90 Res
= pkgVersionCompare(Version
.begin(),Version
.end(),Ver
.VerStr(),
91 Ver
.VerStr() + strlen(Ver
.VerStr()));
96 /* We already have a version for this item, record that we
100 if (List
.UsePackage(Pkg
,Ver
) == false)
103 if (NewFileVer(Ver
,List
) == false)
110 *Last
= NewVersion(Ver
,Version
,*Last
);
111 Ver
->ParentPkg
= Pkg
.Index();
112 if (List
.NewVersion(Ver
) == false)
115 if (List
.UsePackage(Pkg
,Ver
) == false)
118 if (NewFileVer(Ver
,List
) == false)
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
)
131 unsigned long Package
= Map
.Allocate(sizeof(pkgCache::Package
));
135 Pkg
= pkgCache::PkgIterator(Cache
,Cache
.PkgP
+ Package
);
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
;
142 // Set the name and the ID
143 Pkg
->Name
= Map
.WriteString(Name
);
146 Pkg
->ID
= Cache
.HeaderP
->PackageCount
++;
151 // CacheGenerator::NewFileVer - Create a new File<->Version association /*{{{*/
152 // ---------------------------------------------------------------------
154 bool pkgCacheGenerator::NewFileVer(pkgCache::VerIterator
&Ver
,
158 unsigned long VerFile
= Map
.Allocate(sizeof(pkgCache::VerFile
));
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();
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
,
180 unsigned long Version
= Map
.Allocate(sizeof(pkgCache::Version
));
185 Ver
= pkgCache::VerIterator(Cache
,Cache
.VerP
+ Version
);
187 Ver
->ID
= Cache
.HeaderP
->VersionCount
++;
188 Ver
->VerStr
= Map
.WriteString(VerStr
);
189 if (Ver
->VerStr
== 0)
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
,
205 pkgCache
&Cache
= Owner
->Cache
;
208 unsigned long Dependency
= Owner
->Map
.Allocate(sizeof(pkgCache::Dependency
));
213 pkgCache::DepIterator
Dep(Cache
,Cache
.DepP
+ Dependency
);
214 Dep
->ParentVer
= Ver
.Index();
217 Dep
->ID
= Cache
.HeaderP
->DependsCount
++;
219 // Locate the target package
220 pkgCache::PkgIterator Pkg
= Cache
.FindPkg(PackageName
);
221 if (Pkg
.end() == true)
222 if (Owner
->NewPackage(Pkg
,PackageName
) == false)
225 // Probe the reverse dependency list for a version string that matches
226 if (Version
.empty() == false)
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)
236 // Link it to the package
237 Dep
->Package
= Pkg
.Index();
238 Dep
->NextRevDepends
= Pkg
->RevDepends
;
239 Pkg
->RevDepends
= Dep
.Index();
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
;
251 // ListParser::NewProvides - Create a Provides element /*{{{*/
252 // ---------------------------------------------------------------------
254 bool pkgCacheGenerator::ListParser::NewProvides(pkgCache::VerIterator Ver
,
258 pkgCache
&Cache
= Owner
->Cache
;
261 unsigned long Provides
= Owner
->Map
.Allocate(sizeof(pkgCache::Provides
));
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)
273 // Locate the target package
274 pkgCache::PkgIterator Pkg
= Cache
.FindPkg(PackageName
);
275 if (Pkg
.end() == true)
276 if (Owner
->NewPackage(Pkg
,PackageName
) == false)
279 // Link it to the package
280 Prv
->ParentPkg
= Pkg
.Index();
281 Prv
->NextProvides
= Pkg
->ProvidesList
;
282 Pkg
->ProvidesList
= Prv
.Index();
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
291 bool pkgCacheGenerator::SelectFile(string File
,unsigned long Flags
)
294 if (stat(File
.c_str(),&Buf
) == -1)
295 return _error
->Errno("stat","Couldn't stat ",File
.c_str());
297 // Get some space for the structure
298 CurrentFile
= Cache
.PkgFileP
+ Map
.Allocate(sizeof(*CurrentFile
));
299 if (CurrentFile
== Cache
.PkgFileP
)
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
;
310 if (CurrentFile
->FileName
== 0)
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
,
321 // Search for an insertion point
322 pkgCache::StringItem
*I
= Cache
.StringItemP
+ Cache
.HeaderP
->StringList
;
324 unsigned long *Last
= &Cache
.HeaderP
->StringList
;
325 for (; I
!= Cache
.StringItemP
; Last
= &I
->NextItem
,
326 I
= Cache
.StringItemP
+ I
->NextItem
)
328 Res
= strncmp(Cache
.StrP
+ I
->String
,S
,Size
);
329 if (Res
== 0 && *(Cache
.StrP
+ I
->String
+ Size
) != 0)
340 unsigned long Item
= Map
.Allocate(sizeof(pkgCache::StringItem
));
344 // Fill in the structure
345 pkgCache::StringItem
*ItemP
= Cache
.StringItemP
+ Item
;
346 ItemP
->NextItem
= I
- Cache
.StringItemP
;
348 ItemP
->String
= Map
.WriteString(S
,Size
);
349 if (ItemP
->String
== 0)
352 return ItemP
->String
;