]> git.saurik.com Git - apt.git/blame - apt-inst/filelist.h
releasing package apt version 1.1~exp1
[apt.git] / apt-inst / filelist.h
CommitLineData
b2e465d6
AL
1// -*- mode: cpp; mode: fold -*-
2// Description /*{{{*/
3// $Id: filelist.h,v 1.2 2001/02/20 07:03:16 jgg Exp $
4/* ######################################################################
5
6 File Listing - Manages a Cache of File -> Package names.
7
8 This is identical to the Package cache, except that the generator
9 (which is much simpler) is integrated directly into the main class,
10 and it has been designed to handle live updates.
11
12 The storage content of the class is maintained in a memory map and is
13 written directly to the file system. Performance is traded against
14 space to give something that performs well and remains small.
15 The average per file usage is 32 bytes which yeilds about a meg every
16 36k files. Directory paths are collected into a binary tree and stored
17 only once, this offsets the cost of the hash nodes enough to keep
18 memory usage slightly less than the sum of the filenames.
19
20 The file names are stored into a fixed size chained hash table that is
21 linked to the package name and to the directory component.
22
23 Each file node has a set of associated flags that indicate the current
24 state of the file.
25
26 ##################################################################### */
27 /*}}}*/
28#ifndef PKGLIB_FILELIST_H
29#define PKGLIB_FILELIST_H
30
472ff00e 31#include <apt-pkg/mmap.h>
13500573 32
4f333a8b 33#include <cstring>
472ff00e 34#include <string>
b2e465d6
AL
35
36class pkgFLCache
37{
38 public:
39 struct Header;
40 struct Node;
41 struct Directory;
42 struct Package;
43 struct Diversion;
44 struct ConfFile;
d3e8fbb3 45
b2e465d6
AL
46 class NodeIterator;
47 class DirIterator;
48 class PkgIterator;
49 class DiverIterator;
d3e8fbb3 50
b2e465d6 51 protected:
8f3ba4e8 52 std::string CacheFile;
b2e465d6
AL
53 DynamicMMap &Map;
54 map_ptrloc LastTreeLookup;
55 unsigned long LastLookupSize;
d3e8fbb3 56
b2e465d6
AL
57 // Helpers for the addition algorithms
58 map_ptrloc TreeLookup(map_ptrloc *Base,const char *Text,const char *TextEnd,
59 unsigned long Size,unsigned int *Count = 0,
60 bool Insert = false);
d3e8fbb3 61
b2e465d6 62 public:
d3e8fbb3 63
b2e465d6
AL
64 // Pointers to the arrays of items
65 Header *HeaderP;
66 Node *NodeP;
67 Directory *DirP;
68 Package *PkgP;
69 Diversion *DiverP;
70 ConfFile *ConfP;
71 char *StrP;
72 unsigned char *AnyP;
d3e8fbb3 73
b2e465d6
AL
74 // Quick accessors
75 Node *FileHash;
d3e8fbb3 76
b2e465d6
AL
77 // Accessors
78 Header &Head() {return *HeaderP;};
79 void PrintTree(map_ptrloc Base,unsigned long Size);
80
81 // Add/Find things
82 PkgIterator GetPkg(const char *Name,const char *End,bool Insert);
83 inline PkgIterator GetPkg(const char *Name,bool Insert);
84 NodeIterator GetNode(const char *Name,
85 const char *NameEnd,
86 map_ptrloc Loc,
87 bool Insert,bool Divert);
88 Node *HashNode(NodeIterator const &N);
89 void DropNode(map_ptrloc Node);
90
91 inline DiverIterator DiverBegin();
d3e8fbb3 92
b2e465d6
AL
93 // Diversion control
94 void BeginDiverLoad();
95 void FinishDiverLoad();
96 bool AddDiversion(PkgIterator const &Owner,const char *From,
97 const char *To);
98 bool AddConfFile(const char *Name,const char *NameEnd,
99 PkgIterator const &Owner,const unsigned char *Sum);
d3e8fbb3 100
b2e465d6
AL
101 pkgFLCache(DynamicMMap &Map);
102// ~pkgFLCache();
103};
104
105struct pkgFLCache::Header
106{
107 // Signature information
108 unsigned long Signature;
109 short MajorVersion;
110 short MinorVersion;
111 bool Dirty;
d3e8fbb3 112
b2e465d6
AL
113 // Size of structure values
114 unsigned HeaderSz;
115 unsigned NodeSz;
116 unsigned DirSz;
117 unsigned PackageSz;
118 unsigned DiversionSz;
119 unsigned ConfFileSz;
d3e8fbb3 120
b2e465d6
AL
121 // Structure Counts;
122 unsigned int NodeCount;
123 unsigned int DirCount;
124 unsigned int PackageCount;
125 unsigned int DiversionCount;
126 unsigned int ConfFileCount;
127 unsigned int HashSize;
128 unsigned long UniqNodes;
d3e8fbb3 129
b2e465d6
AL
130 // Offsets
131 map_ptrloc FileHash;
132 map_ptrloc DirTree;
133 map_ptrloc Packages;
134 map_ptrloc Diversions;
d3e8fbb3 135
b2e465d6
AL
136 /* Allocation pools, there should be one of these for each structure
137 excluding the header */
138 DynamicMMap::Pool Pools[5];
139
140 bool CheckSizes(Header &Against) const;
141 Header();
142};
143
144/* The bit field is used to advoid incurring an extra 4 bytes x 40000,
145 Pointer is the most infrequently used member of the structure */
146struct pkgFLCache::Node
147{
148 map_ptrloc Dir; // Dir
149 map_ptrloc File; // String
150 unsigned Pointer:24; // Package/Diversion/ConfFile
151 unsigned Flags:8; // Package
152 map_ptrloc Next; // Node
153 map_ptrloc NextPkg; // Node
154
155 enum Flags {Diversion = (1<<0),ConfFile = (1<<1),
156 NewConfFile = (1<<2),NewFile = (1<<3),
157 Unpacked = (1<<4),Replaced = (1<<5)};
158};
159
160struct pkgFLCache::Directory
161{
162 map_ptrloc Left; // Directory
163 map_ptrloc Right; // Directory
164 map_ptrloc Name; // String
165};
166
167struct pkgFLCache::Package
168{
169 map_ptrloc Left; // Package
170 map_ptrloc Right; // Package
171 map_ptrloc Name; // String
172 map_ptrloc Files; // Node
173};
174
175struct pkgFLCache::Diversion
176{
177 map_ptrloc OwnerPkg; // Package
178 map_ptrloc DivertFrom; // Node
179 map_ptrloc DivertTo; // String
d3e8fbb3 180
b2e465d6
AL
181 map_ptrloc Next; // Diversion
182 unsigned long Flags;
183
184 enum Flags {Touched = (1<<0)};
185};
186
187struct pkgFLCache::ConfFile
188{
189 map_ptrloc OwnerPkg; // Package
190 unsigned char MD5[16];
191};
192
193class pkgFLCache::PkgIterator
194{
195 Package *Pkg;
196 pkgFLCache *Owner;
d3e8fbb3 197
b2e465d6 198 public:
d3e8fbb3 199
b2e465d6 200 inline bool end() const {return Owner == 0 || Pkg == Owner->PkgP?true:false;}
d3e8fbb3 201
b2e465d6 202 // Accessors
d3e8fbb3
DK
203 inline Package *operator ->() {return Pkg;}
204 inline Package const *operator ->() const {return Pkg;}
205 inline Package const &operator *() const {return *Pkg;}
206 inline operator Package *() {return Pkg == Owner->PkgP?0:Pkg;}
207 inline operator Package const *() const {return Pkg == Owner->PkgP?0:Pkg;}
208
209 inline unsigned long Offset() const {return Pkg - Owner->PkgP;}
210 inline const char *Name() const {return Pkg->Name == 0?0:Owner->StrP + Pkg->Name;}
b2e465d6
AL
211 inline pkgFLCache::NodeIterator Files() const;
212
d3e8fbb3
DK
213 PkgIterator() : Pkg(0), Owner(0) {}
214 PkgIterator(pkgFLCache &Owner,Package *Trg) : Pkg(Trg), Owner(&Owner) {}
b2e465d6
AL
215};
216
217class pkgFLCache::DirIterator
218{
219 Directory *Dir;
220 pkgFLCache *Owner;
d3e8fbb3 221
b2e465d6 222 public:
d3e8fbb3 223
b2e465d6 224 // Accessors
d3e8fbb3
DK
225 inline Directory *operator ->() {return Dir;}
226 inline Directory const *operator ->() const {return Dir;}
227 inline Directory const &operator *() const {return *Dir;}
228 inline operator Directory *() {return Dir == Owner->DirP?0:Dir;}
229 inline operator Directory const *() const {return Dir == Owner->DirP?0:Dir;}
b2e465d6 230
d3e8fbb3 231 inline const char *Name() const {return Dir->Name == 0?0:Owner->StrP + Dir->Name;}
b2e465d6 232
d3e8fbb3
DK
233 DirIterator() : Dir(0), Owner(0) {}
234 DirIterator(pkgFLCache &Owner,Directory *Trg) : Dir(Trg), Owner(&Owner) {}
b2e465d6
AL
235};
236
237class pkgFLCache::DiverIterator
238{
239 Diversion *Diver;
240 pkgFLCache *Owner;
d3e8fbb3 241
b2e465d6
AL
242 public:
243
244 // Iteration
d3e8fbb3
DK
245 void operator ++(int) {if (Diver != Owner->DiverP) Diver = Owner->DiverP + Diver->Next;}
246 inline void operator ++() {operator ++(0);}
247 inline bool end() const {return Owner == 0 || Diver == Owner->DiverP;}
b2e465d6
AL
248
249 // Accessors
d3e8fbb3
DK
250 inline Diversion *operator ->() {return Diver;}
251 inline Diversion const *operator ->() const {return Diver;}
252 inline Diversion const &operator *() const {return *Diver;}
253 inline operator Diversion *() {return Diver == Owner->DiverP?0:Diver;}
254 inline operator Diversion const *() const {return Diver == Owner->DiverP?0:Diver;}
b2e465d6 255
d3e8fbb3 256 inline PkgIterator OwnerPkg() const {return PkgIterator(*Owner,Owner->PkgP + Diver->OwnerPkg);}
b2e465d6
AL
257 inline NodeIterator DivertFrom() const;
258 inline NodeIterator DivertTo() const;
259
260 DiverIterator() : Diver(0), Owner(0) {};
d3e8fbb3 261 DiverIterator(pkgFLCache &Owner,Diversion *Trg) : Diver(Trg), Owner(&Owner) {}
b2e465d6
AL
262};
263
264class pkgFLCache::NodeIterator
265{
266 Node *Nde;
d3e8fbb3 267 enum {NdePkg, NdeHash} Type;
b2e465d6 268 pkgFLCache *Owner;
d3e8fbb3 269
b2e465d6 270 public:
d3e8fbb3 271
b2e465d6 272 // Iteration
d3e8fbb3
DK
273 void operator ++(int) {if (Nde != Owner->NodeP) Nde = Owner->NodeP +
274 (Type == NdePkg?Nde->NextPkg:Nde->Next);}
275 inline void operator ++() {operator ++(0);}
276 inline bool end() const {return Owner == 0 || Nde == Owner->NodeP;}
b2e465d6
AL
277
278 // Accessors
d3e8fbb3
DK
279 inline Node *operator ->() {return Nde;}
280 inline Node const *operator ->() const {return Nde;}
281 inline Node const &operator *() const {return *Nde;}
282 inline operator Node *() {return Nde == Owner->NodeP?0:Nde;}
283 inline operator Node const *() const {return Nde == Owner->NodeP?0:Nde;}
284 inline unsigned long Offset() const {return Nde - Owner->NodeP;}
285 inline DirIterator Dir() const {return DirIterator(*Owner,Owner->DirP + Nde->Dir);}
286 inline DiverIterator Diversion() const {return DiverIterator(*Owner,Owner->DiverP + Nde->Pointer);}
287 inline const char *File() const {return Nde->File == 0?0:Owner->StrP + Nde->File;}
288 inline const char *DirN() const {return Owner->StrP + Owner->DirP[Nde->Dir].Name;}
b2e465d6 289 Package *RealPackage() const;
d3e8fbb3 290
b2e465d6 291 NodeIterator() : Nde(0), Type(NdeHash), Owner(0) {};
d3e8fbb3
DK
292 NodeIterator(pkgFLCache &Owner) : Nde(Owner.NodeP), Type(NdeHash), Owner(&Owner) {}
293 NodeIterator(pkgFLCache &Owner,Node *Trg) : Nde(Trg), Type(NdeHash), Owner(&Owner) {}
294 NodeIterator(pkgFLCache &Owner,Node *Trg,Package *) : Nde(Trg), Type(NdePkg), Owner(&Owner) {}
b2e465d6
AL
295};
296
297/* Inlines with forward references that cannot be included directly in their
298 respsective classes */
d3e8fbb3
DK
299inline pkgFLCache::NodeIterator pkgFLCache::DiverIterator::DivertFrom() const
300 {return NodeIterator(*Owner,Owner->NodeP + Diver->DivertFrom);}
b2e465d6 301inline pkgFLCache::NodeIterator pkgFLCache::DiverIterator::DivertTo() const
d3e8fbb3 302 {return NodeIterator(*Owner,Owner->NodeP + Diver->DivertTo);}
b2e465d6
AL
303
304inline pkgFLCache::NodeIterator pkgFLCache::PkgIterator::Files() const
d3e8fbb3 305 {return NodeIterator(*Owner,Owner->NodeP + Pkg->Files,Pkg);}
b2e465d6
AL
306
307inline pkgFLCache::DiverIterator pkgFLCache::DiverBegin()
d3e8fbb3 308 {return DiverIterator(*this,DiverP + HeaderP->Diversions);}
b2e465d6 309
d3e8fbb3
DK
310inline pkgFLCache::PkgIterator pkgFLCache::GetPkg(const char *Name,bool Insert)
311 {return GetPkg(Name,Name+strlen(Name),Insert);}
b2e465d6
AL
312
313#endif