Merge branch 'fixes0.6'
[fapman] / apt-src / pkgcache.h
1 /*
2 Apt is copyright 1997, 1998, 1999 Jason Gunthorpe and others.
3 Apt is currently developed by APT Development Team <deity@lists.debian.org>.
4
5 License: GPLv2+
6
7         This program is free software; you can redistribute it and/or modify
8         it under the terms of the GNU General Public License as published by
9         the Free Software Foundation; either version 2 of the License, or
10         (at your option) any later version.
11
12         This program is distributed in the hope that it will be useful,
13         but WITHOUT ANY WARRANTY; without even the implied warranty of
14         MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15         GNU General Public License for more details.
16
17         You should have received a copy of the GNU General Public License
18         along with this program; if not, write to the Free Software
19         Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20
21 See /usr/share/common-licenses/GPL-2, or
22 <http://www.gnu.org/copyleft/gpl.txt> for the terms of the latest version
23 of the GNU General Public License.
24 */
25
26 // -*- mode: cpp; mode: fold -*-
27 // Description                                                          /*{{{*/
28 // $Id: pkgcache.h,v 1.25 2001/07/01 22:28:24 jgg Exp $
29 /* ######################################################################
30    
31    Cache - Structure definitions for the cache file
32    
33    Please see doc/apt-pkg/cache.sgml for a more detailed description of 
34    this format. Also be sure to keep that file up-to-date!!
35    
36    Clients should always use the CacheIterators classes for access to the
37    cache. They provide a simple STL-like method for traversing the links
38    of the datastructure.
39    
40    See pkgcachegen.h for information about generating cache structures.
41    
42    ##################################################################### */
43                                                                         /*}}}*/
44 #ifndef PKGLIB_PKGCACHE_H
45 #define PKGLIB_PKGCACHE_H
46
47
48 #include <string>
49 #include <time.h>
50 #include "mmap.h"
51
52 using std::string;
53     
54 class pkgVersioningSystem;
55 class pkgCache
56 {
57    public:
58    // Cache element predeclarations
59    struct Header;
60    struct Package;
61    struct PackageFile;
62    struct Version;
63    struct Description;
64    struct Provides;
65    struct Dependency;
66    struct StringItem;
67    struct VerFile;
68    struct DescFile;
69    
70    // Iterators
71    class PkgIterator;
72    class VerIterator;
73    class DescIterator;
74    class DepIterator;
75    class PrvIterator;
76    class PkgFileIterator;
77    class VerFileIterator;
78    class DescFileIterator;
79    friend class PkgIterator;
80    friend class VerIterator;
81    friend class DescInterator;
82    friend class DepIterator;
83    friend class PrvIterator;
84    friend class PkgFileIterator;
85    friend class VerFileIterator;
86    friend class DescFileIterator;
87    
88    class Namespace;
89    
90    // These are all the constants used in the cache structures
91
92    // WARNING - if you change these lists you must also edit
93    // the stringification in pkgcache.cc and also consider whether
94    // the cache file will become incompatible.
95    struct Dep
96    {
97       enum DepType {Depends=1,PreDepends=2,Suggests=3,Recommends=4,
98          Conflicts=5,Replaces=6,Obsoletes=7,DpkgBreaks=8};
99       enum DepCompareOp {Or=0x10,NoOp=0,LessEq=0x1,GreaterEq=0x2,Less=0x3,
100          Greater=0x4,Equals=0x5,NotEquals=0x6};
101    };
102    
103    struct State
104    {
105       enum VerPriority {Important=1,Required=2,Standard=3,Optional=4,Extra=5};
106       enum PkgSelectedState {Unknown=0,Install=1,Hold=2,DeInstall=3,Purge=4};
107       enum PkgInstState {Ok=0,ReInstReq=1,HoldInst=2,HoldReInstReq=3};
108       enum PkgCurrentState {NotInstalled=0,UnPacked=1,HalfConfigured=2,
109            HalfInstalled=4,ConfigFiles=5,Installed=6,
110            TriggersAwaited=7,TriggersPending=8};
111    };
112    
113    struct Flag
114    {
115       enum PkgFlags {Auto=(1<<0),Essential=(1<<3),Important=(1<<4)};
116       enum PkgFFlags {NotSource=(1<<0),NotAutomatic=(1<<1)};
117    };
118    
119    protected:
120    
121    // Memory mapped cache file
122    string CacheFile;
123    MMap &Map;
124
125    unsigned long sHash(const string &S) const;
126    unsigned long sHash(const char *S) const;
127    
128    public:
129    
130    // Pointers to the arrays of items
131    Header *HeaderP;
132    Package *PkgP;
133    VerFile *VerFileP;
134    DescFile *DescFileP;
135    PackageFile *PkgFileP;
136    Version *VerP;
137    Description *DescP;
138    Provides *ProvideP;
139    Dependency *DepP;
140    StringItem *StringItemP;
141    char *StrP;
142
143    virtual bool ReMap();
144    inline bool Sync() {return Map.Sync();};
145    inline MMap &GetMap() {return Map;};
146    inline void *DataEnd() {return ((unsigned char *)Map.Data()) + Map.Size();};
147       
148    // String hashing function (512 range)
149    inline unsigned long Hash(const string &S) const {return sHash(S);};
150    inline unsigned long Hash(const char *S) const {return sHash(S);};
151
152    // Usefull transformation things
153    const char *Priority(unsigned char Priority);
154    
155    // Accessors
156    PkgIterator FindPkg(const string &Name);
157    Header &Head() {return *HeaderP;};
158    inline PkgIterator PkgBegin();
159    inline PkgIterator PkgEnd();
160    inline PkgFileIterator FileBegin();
161    inline PkgFileIterator FileEnd();
162
163    // Make me a function
164    pkgVersioningSystem *VS;
165    
166    // Converters
167    static const char *CompTypeDeb(unsigned char Comp);
168    static const char *CompType(unsigned char Comp);
169    static const char *DepType(unsigned char Dep);
170    
171    pkgCache(MMap *Map,bool DoMap = true);
172    virtual ~pkgCache() {};
173 };
174
175 // Header structure
176 struct pkgCache::Header
177 {
178    // Signature information
179    unsigned long Signature;
180    short MajorVersion;
181    short MinorVersion;
182    bool Dirty;
183    
184    // Size of structure values
185    unsigned short HeaderSz;
186    unsigned short PackageSz;
187    unsigned short PackageFileSz;
188    unsigned short VersionSz;
189    unsigned short DescriptionSz;
190    unsigned short DependencySz;
191    unsigned short ProvidesSz;
192    unsigned short VerFileSz;
193    unsigned short DescFileSz;
194    
195    // Structure counts
196    unsigned long PackageCount;
197    unsigned long VersionCount;
198    unsigned long DescriptionCount;
199    unsigned long DependsCount;
200    unsigned long PackageFileCount;
201    unsigned long VerFileCount;
202    unsigned long DescFileCount;
203    unsigned long ProvidesCount;
204    
205    // Offsets
206    map_ptrloc FileList;              // struct PackageFile
207    map_ptrloc StringList;            // struct StringItem
208    map_ptrloc VerSysName;            // StringTable
209    map_ptrloc Architecture;          // StringTable
210    unsigned long MaxVerFileSize;
211    unsigned long MaxDescFileSize;
212
213    /* Allocation pools, there should be one of these for each structure
214       excluding the header */
215    DynamicMMap::Pool Pools[8];
216    
217    // Rapid package name lookup
218    map_ptrloc HashTable[2*1048];
219
220    bool CheckSizes(Header &Against) const;
221    Header();
222 };
223
224 struct pkgCache::Package
225 {
226    // Pointers
227    map_ptrloc Name;              // Stringtable
228    map_ptrloc VersionList;       // Version
229    map_ptrloc CurrentVer;        // Version
230    map_ptrloc Section;           // StringTable (StringItem)
231       
232    // Linked list 
233    map_ptrloc NextPackage;       // Package
234    map_ptrloc RevDepends;        // Dependency
235    map_ptrloc ProvidesList;      // Provides
236
237    // Install/Remove/Purge etc
238    unsigned char SelectedState;     // What
239    unsigned char InstState;         // Flags
240    unsigned char CurrentState;      // State
241    
242    unsigned short ID;
243    unsigned long Flags;
244 };
245
246 struct pkgCache::PackageFile
247 {
248    // Names
249    map_ptrloc FileName;        // Stringtable
250    map_ptrloc Archive;         // Stringtable
251    map_ptrloc Component;       // Stringtable
252    map_ptrloc Version;         // Stringtable
253    map_ptrloc Origin;          // Stringtable
254    map_ptrloc Label;           // Stringtable
255    map_ptrloc Architecture;    // Stringtable
256    map_ptrloc Site;            // Stringtable
257    map_ptrloc IndexType;       // Stringtable
258    unsigned long Size;            
259    unsigned long Flags;
260    
261    // Linked list
262    map_ptrloc NextFile;        // PackageFile
263    unsigned short ID;
264    time_t mtime;                  // Modification time for the file
265 };
266
267 struct pkgCache::VerFile
268 {
269    map_ptrloc File;           // PackageFile
270    map_ptrloc NextFile;       // PkgVerFile
271    map_ptrloc Offset;         // File offset
272    unsigned long Size;
273 };
274
275 struct pkgCache::DescFile
276 {
277    map_ptrloc File;           // PackageFile
278    map_ptrloc NextFile;       // PkgVerFile
279    map_ptrloc Offset;         // File offset
280    unsigned long Size;
281 };
282
283 struct pkgCache::Version
284 {
285    map_ptrloc VerStr;            // Stringtable
286    map_ptrloc Section;           // StringTable (StringItem)
287    map_ptrloc Arch;              // StringTable
288       
289    // Lists
290    map_ptrloc FileList;          // VerFile
291    map_ptrloc NextVer;           // Version
292    map_ptrloc DescriptionList;   // Description
293    map_ptrloc DependsList;       // Dependency
294    map_ptrloc ParentPkg;         // Package
295    map_ptrloc ProvidesList;      // Provides
296    
297    map_ptrloc Size;              // These are the .deb size
298    map_ptrloc InstalledSize;
299    unsigned short Hash;
300    unsigned short ID;
301    unsigned char Priority;
302 };
303
304 struct pkgCache::Description
305 {
306    // Language Code store the description translation language code. If
307    // the value has a 0 lenght then this is readed using the Package
308    // file else the Translation-CODE are used.
309    map_ptrloc language_code;     // StringTable
310    map_ptrloc md5sum;            // StringTable
311
312    // Linked list 
313    map_ptrloc FileList;          // DescFile
314    map_ptrloc NextDesc;          // Description
315    map_ptrloc ParentPkg;         // Package
316
317    unsigned short ID;
318 };
319
320 struct pkgCache::Dependency
321 {
322    map_ptrloc Version;         // Stringtable
323    map_ptrloc Package;         // Package
324    map_ptrloc NextDepends;     // Dependency
325    map_ptrloc NextRevDepends;  // Dependency
326    map_ptrloc ParentVer;       // Version
327    
328    // Specific types of depends
329    map_ptrloc ID;   
330    unsigned char Type;
331    unsigned char CompareOp;
332 };
333
334 struct pkgCache::Provides
335 {
336    map_ptrloc ParentPkg;        // Pacakge
337    map_ptrloc Version;          // Version
338    map_ptrloc ProvideVersion;   // Stringtable
339    map_ptrloc NextProvides;     // Provides
340    map_ptrloc NextPkgProv;      // Provides
341 };
342
343 struct pkgCache::StringItem
344 {
345    map_ptrloc String;        // Stringtable
346    map_ptrloc NextItem;      // StringItem
347 };
348
349 #include "cacheiterators.h"
350
351 inline pkgCache::PkgIterator pkgCache::PkgBegin() 
352        {return PkgIterator(*this);};
353 inline pkgCache::PkgIterator pkgCache::PkgEnd() 
354        {return PkgIterator(*this,PkgP);};
355 inline pkgCache::PkgFileIterator pkgCache::FileBegin()
356        {return PkgFileIterator(*this,PkgFileP + HeaderP->FileList);};
357 inline pkgCache::PkgFileIterator pkgCache::FileEnd()
358        {return PkgFileIterator(*this,PkgFileP);};
359
360 // Oh I wish for Real Name Space Support
361 class pkgCache::Namespace
362 {   
363    public:
364
365    typedef pkgCache::PkgIterator PkgIterator;
366    typedef pkgCache::VerIterator VerIterator;
367    typedef pkgCache::DescIterator DescIterator;
368    typedef pkgCache::DepIterator DepIterator;
369    typedef pkgCache::PrvIterator PrvIterator;
370    typedef pkgCache::PkgFileIterator PkgFileIterator;
371    typedef pkgCache::VerFileIterator VerFileIterator;   
372    typedef pkgCache::Version Version;
373    typedef pkgCache::Description Description;
374    typedef pkgCache::Package Package;
375    typedef pkgCache::Header Header;
376    typedef pkgCache::Dep Dep;
377    typedef pkgCache::Flag Flag;
378 };
379
380 #endif