]>
git.saurik.com Git - apt.git/blob - apt-pkg/pkgcachegen.cc
1 // -*- mode: cpp; mode: fold -*-
3 // $Id: pkgcachegen.cc,v 1.2 1998/07/04 05:57:37 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
)
63 // Get a pointer to the package structure
64 string Package
= List
.Package();
65 pkgCache::PkgIterator Pkg
= Cache
.FindPkg(Package
);
66 if (Pkg
.end() == true)
68 if (NewPackage(Pkg
,Package
) == false)
71 if (List
.NewPackage(Pkg
) == false)
75 /* Get a pointer to the version structure. We know the list is sorted
76 so we use that fact in the search. Insertion of new versions is
77 done with correct sorting */
78 string Version
= List
.Version();
79 if (Version
.empty() == true)
81 if (List
.UsePackage(Pkg
,pkgCache::VerIterator(Cache
)) == false)
86 pkgCache::VerIterator Ver
= Pkg
.VersionList();
87 unsigned long *Last
= &Pkg
->VersionList
;
89 for (; Ver
.end() == false; Last
= &Ver
->NextVer
, Ver
++)
91 Res
= pkgVersionCompare(Version
.begin(),Version
.end(),Ver
.VerStr(),
92 Ver
.VerStr() + strlen(Ver
.VerStr()));
97 /* We already have a version for this item, record that we
101 if (List
.UsePackage(Pkg
,Ver
) == false)
104 if (NewFileVer(Ver
,List
) == false)
111 *Last
= NewVersion(Ver
,Version
,*Last
);
112 Ver
->ParentPkg
= Pkg
.Index();
113 if (List
.NewVersion(Ver
) == false)
116 if (List
.UsePackage(Pkg
,Ver
) == false)
119 if (NewFileVer(Ver
,List
) == false)
122 while (List
.Step() == true);
127 // CacheGenerator::NewPackage - Add a new package /*{{{*/
128 // ---------------------------------------------------------------------
129 /* This creates a new package structure and adds it to the hash table */
130 bool pkgCacheGenerator::NewPackage(pkgCache::PkgIterator
&Pkg
,string Name
)
133 unsigned long Package
= Map
.Allocate(sizeof(pkgCache::Package
));
137 Pkg
= pkgCache::PkgIterator(Cache
,Cache
.PkgP
+ Package
);
139 // Insert it into the hash table
140 unsigned long Hash
= Cache
.Hash(Name
);
141 Pkg
->NextPackage
= Cache
.HeaderP
->HashTable
[Hash
];
142 Cache
.HeaderP
->HashTable
[Hash
] = Package
;
144 // Set the name and the ID
145 Pkg
->Name
= Map
.WriteString(Name
);
148 Pkg
->ID
= Cache
.HeaderP
->PackageCount
++;
153 // CacheGenerator::NewFileVer - Create a new File<->Version association /*{{{*/
154 // ---------------------------------------------------------------------
156 bool pkgCacheGenerator::NewFileVer(pkgCache::VerIterator
&Ver
,
162 // CacheGenerator::NewVersion - Create a new Version /*{{{*/
163 // ---------------------------------------------------------------------
164 /* This puts a version structure in the linked list */
165 unsigned long pkgCacheGenerator::NewVersion(pkgCache::VerIterator
&Ver
,
170 unsigned long Version
= Map
.Allocate(sizeof(pkgCache::Version
));
175 Ver
= pkgCache::VerIterator(Cache
,Cache
.VerP
+ Version
);
176 Ver
->File
= CurrentFile
- Cache
.PkgFileP
;
178 Ver
->ID
= Cache
.HeaderP
->VersionCount
++;
179 Ver
->VerStr
= Map
.WriteString(VerStr
);
180 if (Ver
->VerStr
== 0)
186 // CacheGenerator::SelectFile - Select the current file being parsed /*{{{*/
187 // ---------------------------------------------------------------------
188 /* This is used to select which file is to be associated with all newly
190 bool pkgCacheGenerator::SelectFile(string File
,unsigned long Flags
)
193 if (stat(File
.c_str(),&Buf
) == -1)
194 return _error
->Errno("stat","Couldn't stat ",File
.c_str());
196 // Get some space for the structure
197 CurrentFile
= Cache
.PkgFileP
+ Map
.Allocate(sizeof(*CurrentFile
));
198 if (CurrentFile
== Cache
.PkgFileP
)
202 CurrentFile
->FileName
= Map
.WriteString(File
);
203 CurrentFile
->Size
= Buf
.st_size
;
204 CurrentFile
->mtime
= Buf
.st_mtime
;
205 CurrentFile
->NextFile
= Cache
.HeaderP
->FileList
;
206 CurrentFile
->Flags
= Flags
;
209 if (CurrentFile
->FileName
== 0)
213 // CacheGenerator::WriteUniqueString - Insert a unique string /*{{{*/
214 // ---------------------------------------------------------------------
215 /* This is used to create handles to strings. Given the same text it
216 always returns the same number */
217 unsigned long pkgCacheGenerator::WriteUniqString(const char *S
,
220 // Search for an insertion point
221 pkgCache::StringItem
*I
= Cache
.StringItemP
+ Cache
.HeaderP
->StringList
;
223 unsigned long *Last
= &Cache
.HeaderP
->StringList
;
224 for (; I
!= Cache
.StringItemP
; Last
= &I
->NextItem
,
225 I
= Cache
.StringItemP
+ I
->NextItem
)
227 Res
= strncmp(Cache
.StrP
+ I
->String
,S
,Size
);
228 if (Res
== 0 && *(Cache
.StrP
+ I
->String
+ Size
) != 0)
236 return I
- Cache
.StringItemP
;
239 unsigned long Item
= Map
.Allocate(sizeof(pkgCache::StringItem
));
243 // Fill in the structure
244 pkgCache::StringItem
*ItemP
= Cache
.StringItemP
+ Item
;
245 ItemP
->NextItem
= I
- Cache
.StringItemP
;
247 ItemP
->String
= Map
.WriteString(S
,Size
);
248 if (ItemP
->String
== 0)