]> git.saurik.com Git - apt.git/blame - apt-pkg/pkgcachegen.cc
Checkpoint
[apt.git] / apt-pkg / pkgcachegen.cc
CommitLineData
578bfd0a
AL
1// -*- mode: cpp; mode: fold -*-
2// Description /*{{{*/
0149949b 3// $Id: pkgcachegen.cc,v 1.3 1998/07/04 22:32:13 jgg Exp $
578bfd0a
AL
4/* ######################################################################
5
6 Package Cache Generator - Generator for the cache structure.
7
8 This builds the cache structure from the abstract package list parser.
9
10 ##################################################################### */
11 /*}}}*/
12// Include Files /*{{{*/
13#include <pkglib/pkgcachegen.h>
14#include <pkglib/error.h>
15#include <pkglib/version.h>
16
17#include <sys/stat.h>
18#include <unistd.h>
19 /*}}}*/
20
21// CacheGenerator::pkgCacheGenerator - Constructor /*{{{*/
22// ---------------------------------------------------------------------
23/* We set the diry flag and make sure that is written to the disk */
24pkgCacheGenerator::pkgCacheGenerator(DynamicMMap &Map) : Map(Map), Cache(Map)
25{
26 if (_error->PendingError() == true)
27 return;
28
29 if (Map.Size() == 0)
30 {
31 Map.RawAllocate(sizeof(pkgCache::Header));
32 *Cache.HeaderP = pkgCache::Header();
33 }
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]));
37}
38 /*}}}*/
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 */
43pkgCacheGenerator::~pkgCacheGenerator()
44{
45 if (_error->PendingError() == true)
46 return;
47 if (Map.Sync() == false)
48 return;
49
50 Cache.HeaderP->Dirty = false;
51 Map.Sync(0,sizeof(pkgCache::Header));
52}
53 /*}}}*/
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. */
58bool pkgCacheGenerator::MergeList(ListParser &List)
59{
60 List.Owner = this;
0149949b
AL
61
62 while (List.Step() == true)
578bfd0a
AL
63 {
64 // Get a pointer to the package structure
65 string Package = List.Package();
66 pkgCache::PkgIterator Pkg = Cache.FindPkg(Package);
f55a958f 67 if (Pkg.end() == true)
578bfd0a
AL
68 {
69 if (NewPackage(Pkg,Package) == false)
70 return false;
71
72 if (List.NewPackage(Pkg) == false)
73 return false;
74 }
578bfd0a
AL
75
76 /* Get a pointer to the version structure. We know the list is sorted
77 so we use that fact in the search. Insertion of new versions is
78 done with correct sorting */
79 string Version = List.Version();
f55a958f
AL
80 if (Version.empty() == true)
81 {
82 if (List.UsePackage(Pkg,pkgCache::VerIterator(Cache)) == false)
83 return false;
84 continue;
85 }
86
578bfd0a
AL
87 pkgCache::VerIterator Ver = Pkg.VersionList();
88 unsigned long *Last = &Pkg->VersionList;
89 int Res;
f55a958f 90 for (; Ver.end() == false; Last = &Ver->NextVer, Ver++)
578bfd0a
AL
91 {
92 Res = pkgVersionCompare(Version.begin(),Version.end(),Ver.VerStr(),
93 Ver.VerStr() + strlen(Ver.VerStr()));
94 if (Res >= 0)
95 break;
96 }
97
98 /* We already have a version for this item, record that we
99 saw it */
100 if (Res == 0)
101 {
f55a958f
AL
102 if (List.UsePackage(Pkg,Ver) == false)
103 return false;
104
578bfd0a
AL
105 if (NewFileVer(Ver,List) == false)
106 return false;
107
108 continue;
109 }
110
111 // Add a new version
f55a958f
AL
112 *Last = NewVersion(Ver,Version,*Last);
113 Ver->ParentPkg = Pkg.Index();
578bfd0a
AL
114 if (List.NewVersion(Ver) == false)
115 return false;
0149949b 116
f55a958f
AL
117 if (List.UsePackage(Pkg,Ver) == false)
118 return false;
119
578bfd0a
AL
120 if (NewFileVer(Ver,List) == false)
121 return false;
122 }
0149949b 123
578bfd0a
AL
124 return true;
125}
126 /*}}}*/
127// CacheGenerator::NewPackage - Add a new package /*{{{*/
128// ---------------------------------------------------------------------
129/* This creates a new package structure and adds it to the hash table */
f55a958f 130bool pkgCacheGenerator::NewPackage(pkgCache::PkgIterator &Pkg,string Name)
578bfd0a
AL
131{
132 // Get a structure
133 unsigned long Package = Map.Allocate(sizeof(pkgCache::Package));
134 if (Package == 0)
135 return false;
136
f55a958f 137 Pkg = pkgCache::PkgIterator(Cache,Cache.PkgP + Package);
578bfd0a
AL
138
139 // Insert it into the hash table
f55a958f 140 unsigned long Hash = Cache.Hash(Name);
578bfd0a
AL
141 Pkg->NextPackage = Cache.HeaderP->HashTable[Hash];
142 Cache.HeaderP->HashTable[Hash] = Package;
143
144 // Set the name and the ID
145 Pkg->Name = Map.WriteString(Name);
146 if (Pkg->Name == 0)
147 return false;
148 Pkg->ID = Cache.HeaderP->PackageCount++;
149
150 return true;
151}
152 /*}}}*/
153// CacheGenerator::NewFileVer - Create a new File<->Version association /*{{{*/
154// ---------------------------------------------------------------------
155/* */
f55a958f 156bool pkgCacheGenerator::NewFileVer(pkgCache::VerIterator &Ver,
578bfd0a
AL
157 ListParser &List)
158{
f55a958f 159 return true;
578bfd0a
AL
160}
161 /*}}}*/
162// CacheGenerator::NewVersion - Create a new Version /*{{{*/
163// ---------------------------------------------------------------------
f55a958f 164/* This puts a version structure in the linked list */
578bfd0a 165unsigned long pkgCacheGenerator::NewVersion(pkgCache::VerIterator &Ver,
f55a958f 166 string VerStr,
578bfd0a
AL
167 unsigned long Next)
168{
f55a958f
AL
169 // Get a structure
170 unsigned long Version = Map.Allocate(sizeof(pkgCache::Version));
171 if (Version == 0)
0149949b 172 return 0;
f55a958f
AL
173
174 // Fill it in
175 Ver = pkgCache::VerIterator(Cache,Cache.VerP + Version);
176 Ver->File = CurrentFile - Cache.PkgFileP;
177 Ver->NextVer = Next;
178 Ver->ID = Cache.HeaderP->VersionCount++;
179 Ver->VerStr = Map.WriteString(VerStr);
180 if (Ver->VerStr == 0)
0149949b 181 return 0;
f55a958f 182
0149949b 183 return Version;
578bfd0a
AL
184}
185 /*}}}*/
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
189 added versions. */
190bool pkgCacheGenerator::SelectFile(string File,unsigned long Flags)
191{
192 struct stat Buf;
193 if (stat(File.c_str(),&Buf) == -1)
194 return _error->Errno("stat","Couldn't stat ",File.c_str());
195
196 // Get some space for the structure
197 CurrentFile = Cache.PkgFileP + Map.Allocate(sizeof(*CurrentFile));
198 if (CurrentFile == Cache.PkgFileP)
199 return false;
200
201 // Fill it in
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;
207 PkgFileName = File;
208
209 if (CurrentFile->FileName == 0)
210 return false;
211}
212 /*}}}*/
f55a958f
AL
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 */
217unsigned long pkgCacheGenerator::WriteUniqString(const char *S,
218 unsigned int Size)
219{
220 // Search for an insertion point
221 pkgCache::StringItem *I = Cache.StringItemP + Cache.HeaderP->StringList;
222 int Res = 1;
223 unsigned long *Last = &Cache.HeaderP->StringList;
224 for (; I != Cache.StringItemP; Last = &I->NextItem,
225 I = Cache.StringItemP + I->NextItem)
226 {
227 Res = strncmp(Cache.StrP + I->String,S,Size);
228 if (Res == 0 && *(Cache.StrP + I->String + Size) != 0)
229 Res = 1;
230 if (Res >= 0)
231 break;
232 }
233
234 // Match
235 if (Res == 0)
0149949b 236 return I->String;
f55a958f
AL
237
238 // Get a structure
239 unsigned long Item = Map.Allocate(sizeof(pkgCache::StringItem));
240 if (Item == 0)
0149949b
AL
241 return 0;
242
f55a958f
AL
243 // Fill in the structure
244 pkgCache::StringItem *ItemP = Cache.StringItemP + Item;
245 ItemP->NextItem = I - Cache.StringItemP;
246 *Last = Item;
247 ItemP->String = Map.WriteString(S,Size);
248 if (ItemP->String == 0)
0149949b 249 return 0;
f55a958f 250
0149949b 251 return ItemP->String;
f55a958f
AL
252}
253 /*}}}*/