]>
git.saurik.com Git - apt.git/blob - apt-pkg/pkgcachegen.cc
1 // -*- mode: cpp; mode: fold -*-
3 // $Id: pkgcachegen.cc,v 1.9 1998/07/12 23:58:34 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 "apt-pkg/pkgcachegen.h"
17 #include <apt-pkg/pkgcachegen.h>
18 #include <apt-pkg/error.h>
19 #include <apt-pkg/version.h>
26 // CacheGenerator::pkgCacheGenerator - Constructor /*{{{*/
27 // ---------------------------------------------------------------------
28 /* We set the diry flag and make sure that is written to the disk */
29 pkgCacheGenerator::pkgCacheGenerator(DynamicMMap
&Map
) : Map(Map
), Cache(Map
)
31 if (_error
->PendingError() == true)
36 Map
.RawAllocate(sizeof(pkgCache::Header
));
37 *Cache
.HeaderP
= pkgCache::Header();
39 Cache
.HeaderP
->Dirty
= true;
40 Map
.Sync(0,sizeof(pkgCache::Header
));
41 Map
.UsePools(*Cache
.HeaderP
->Pools
,sizeof(Cache
.HeaderP
->Pools
)/sizeof(Cache
.HeaderP
->Pools
[0]));
44 // CacheGenerator::~pkgCacheGenerator - Destructor /*{{{*/
45 // ---------------------------------------------------------------------
46 /* We sync the data then unset the dirty flag in two steps so as to
47 advoid a problem during a crash */
48 pkgCacheGenerator::~pkgCacheGenerator()
50 if (_error
->PendingError() == true)
52 if (Map
.Sync() == false)
55 Cache
.HeaderP
->Dirty
= false;
56 Map
.Sync(0,sizeof(pkgCache::Header
));
59 // CacheGenerator::MergeList - Merge the package list /*{{{*/
60 // ---------------------------------------------------------------------
61 /* This provides the generation of the entries in the cache. Each loop
62 goes through a single package record from the underlying parse engine. */
63 bool pkgCacheGenerator::MergeList(ListParser
&List
)
67 while (List
.Step() == true)
69 // Get a pointer to the package structure
70 string PackageName
= List
.Package();
71 pkgCache::PkgIterator Pkg
;
72 Cache
.FindPkg(PackageName
);
73 if (Pkg
.end() == true)
75 if (NewPackage(Pkg
,PackageName
) == false)
79 /* Get a pointer to the version structure. We know the list is sorted
80 so we use that fact in the search. Insertion of new versions is
81 done with correct sorting */
82 string Version
= List
.Version();
83 if (Version
.empty() == true)
85 if (List
.UsePackage(Pkg
,pkgCache::VerIterator(Cache
)) == false)
90 pkgCache::VerIterator Ver
= Pkg
.VersionList();
91 unsigned long *Last
= &Pkg
->VersionList
;
93 for (; Ver
.end() == false; Last
= &Ver
->NextVer
, Ver
++)
95 Res
= pkgVersionCompare(Version
.begin(),Version
.end(),Ver
.VerStr(),
96 Ver
.VerStr() + strlen(Ver
.VerStr()));
101 /* We already have a version for this item, record that we
105 if (List
.UsePackage(Pkg
,Ver
) == false)
108 if (NewFileVer(Ver
,List
) == false)
115 *Last
= NewVersion(Ver
,Version
,*Last
);
116 Ver
->ParentPkg
= Pkg
.Index();
117 if (List
.NewVersion(Ver
) == false)
120 if (List
.UsePackage(Pkg
,Ver
) == false)
123 if (NewFileVer(Ver
,List
) == false)
130 // CacheGenerator::NewPackage - Add a new package /*{{{*/
131 // ---------------------------------------------------------------------
132 /* This creates a new package structure and adds it to the hash table */
133 bool pkgCacheGenerator::NewPackage(pkgCache::PkgIterator
&Pkg
,string Name
)
136 unsigned long Package
= Map
.Allocate(sizeof(pkgCache::Package
));
140 Pkg
= pkgCache::PkgIterator(Cache
,Cache
.PkgP
+ Package
);
142 // Insert it into the hash table
143 unsigned long Hash
= Cache
.Hash(Name
);
144 Pkg
->NextPackage
= Cache
.HeaderP
->HashTable
[Hash
];
145 Cache
.HeaderP
->HashTable
[Hash
] = Package
;
147 // Set the name and the ID
148 Pkg
->Name
= Map
.WriteString(Name
);
151 Pkg
->ID
= Cache
.HeaderP
->PackageCount
++;
156 // CacheGenerator::NewFileVer - Create a new File<->Version association /*{{{*/
157 // ---------------------------------------------------------------------
159 bool pkgCacheGenerator::NewFileVer(pkgCache::VerIterator
&Ver
,
163 unsigned long VerFile
= Map
.Allocate(sizeof(pkgCache::VerFile
));
167 pkgCache::VerFileIterator
VF(Cache
,Cache
.VerFileP
+ VerFile
);
168 VF
->File
= CurrentFile
- Cache
.PkgFileP
;
169 VF
->NextFile
= Ver
->FileList
;
170 Ver
->FileList
= VF
.Index();
171 VF
->Offset
= List
.Offset();
172 VF
->Size
= List
.Size();
177 // CacheGenerator::NewVersion - Create a new Version /*{{{*/
178 // ---------------------------------------------------------------------
179 /* This puts a version structure in the linked list */
180 unsigned long pkgCacheGenerator::NewVersion(pkgCache::VerIterator
&Ver
,
185 unsigned long Version
= Map
.Allocate(sizeof(pkgCache::Version
));
190 Ver
= pkgCache::VerIterator(Cache
,Cache
.VerP
+ Version
);
192 Ver
->ID
= Cache
.HeaderP
->VersionCount
++;
193 Ver
->VerStr
= Map
.WriteString(VerStr
);
194 if (Ver
->VerStr
== 0)
200 // ListParser::NewDepends - Create a dependency element /*{{{*/
201 // ---------------------------------------------------------------------
202 /* This creates a dependency element in the tree. It is linked to the
203 version and to the package that it is pointing to. */
204 bool pkgCacheGenerator::ListParser::NewDepends(pkgCache::VerIterator Ver
,
210 pkgCache
&Cache
= Owner
->Cache
;
213 unsigned long Dependency
= Owner
->Map
.Allocate(sizeof(pkgCache::Dependency
));
218 pkgCache::DepIterator
Dep(Cache
,Cache
.DepP
+ Dependency
);
219 Dep
->ParentVer
= Ver
.Index();
222 Dep
->ID
= Cache
.HeaderP
->DependsCount
++;
224 // Locate the target package
225 pkgCache::PkgIterator Pkg
= Cache
.FindPkg(PackageName
);
226 if (Pkg
.end() == true)
227 if (Owner
->NewPackage(Pkg
,PackageName
) == false)
230 // Probe the reverse dependency list for a version string that matches
231 if (Version
.empty() == false)
233 for (pkgCache::DepIterator I
= Pkg
.RevDependsList(); I
.end() == false; I
++)
234 if (I
->Version
!= 0 && I
.TargetVer() == Version
)
235 Dep
->Version
= I
->Version
;
236 if (Dep
->Version
== 0)
237 if ((Dep
->Version
= WriteString(Version
)) == 0)
241 // Link it to the package
242 Dep
->Package
= Pkg
.Index();
243 Dep
->NextRevDepends
= Pkg
->RevDepends
;
244 Pkg
->RevDepends
= Dep
.Index();
246 // Link it to the version (at the end of the list)
247 unsigned long *Last
= &Ver
->DependsList
;
248 for (pkgCache::DepIterator D
= Ver
.DependsList(); D
.end() == false; D
++)
249 Last
= &D
->NextDepends
;
250 Dep
->NextDepends
= *Last
;
256 // ListParser::NewProvides - Create a Provides element /*{{{*/
257 // ---------------------------------------------------------------------
259 bool pkgCacheGenerator::ListParser::NewProvides(pkgCache::VerIterator Ver
,
263 pkgCache
&Cache
= Owner
->Cache
;
266 unsigned long Provides
= Owner
->Map
.Allocate(sizeof(pkgCache::Provides
));
271 pkgCache::PrvIterator
Prv(Cache
,Cache
.ProvideP
+ Provides
,Cache
.PkgP
);
272 Prv
->Version
= Ver
.Index();
273 Prv
->NextPkgProv
= Ver
->ProvidesList
;
274 Ver
->ProvidesList
= Prv
.Index();
275 if (Version
.empty() == false && (Prv
->Version
= WriteString(Version
)) == 0)
278 // Locate the target package
279 pkgCache::PkgIterator Pkg
= Cache
.FindPkg(PackageName
);
280 if (Pkg
.end() == true)
281 if (Owner
->NewPackage(Pkg
,PackageName
) == false)
284 // Link it to the package
285 Prv
->ParentPkg
= Pkg
.Index();
286 Prv
->NextProvides
= Pkg
->ProvidesList
;
287 Pkg
->ProvidesList
= Prv
.Index();
292 // CacheGenerator::SelectFile - Select the current file being parsed /*{{{*/
293 // ---------------------------------------------------------------------
294 /* This is used to select which file is to be associated with all newly
296 bool pkgCacheGenerator::SelectFile(string File
,unsigned long Flags
)
299 if (stat(File
.c_str(),&Buf
) == -1)
300 return _error
->Errno("stat","Couldn't stat ",File
.c_str());
302 // Get some space for the structure
303 CurrentFile
= Cache
.PkgFileP
+ Map
.Allocate(sizeof(*CurrentFile
));
304 if (CurrentFile
== Cache
.PkgFileP
)
308 CurrentFile
->FileName
= Map
.WriteString(File
);
309 CurrentFile
->Size
= Buf
.st_size
;
310 CurrentFile
->mtime
= Buf
.st_mtime
;
311 CurrentFile
->NextFile
= Cache
.HeaderP
->FileList
;
312 CurrentFile
->Flags
= Flags
;
315 if (CurrentFile
->FileName
== 0)
319 // CacheGenerator::WriteUniqueString - Insert a unique string /*{{{*/
320 // ---------------------------------------------------------------------
321 /* This is used to create handles to strings. Given the same text it
322 always returns the same number */
323 unsigned long pkgCacheGenerator::WriteUniqString(const char *S
,
326 // Search for an insertion point
327 pkgCache::StringItem
*I
= Cache
.StringItemP
+ Cache
.HeaderP
->StringList
;
329 unsigned long *Last
= &Cache
.HeaderP
->StringList
;
330 for (; I
!= Cache
.StringItemP
; Last
= &I
->NextItem
,
331 I
= Cache
.StringItemP
+ I
->NextItem
)
333 Res
= stringcmp(S
,S
+Size
,Cache
.StrP
+ I
->String
);
343 unsigned long Item
= Map
.Allocate(sizeof(pkgCache::StringItem
));
347 // Fill in the structure
348 pkgCache::StringItem
*ItemP
= Cache
.StringItemP
+ Item
;
349 ItemP
->NextItem
= I
- Cache
.StringItemP
;
351 ItemP
->String
= Map
.WriteString(S
,Size
);
352 if (ItemP
->String
== 0)
355 return ItemP
->String
;