1 // -*- mode: cpp; mode: fold -*-
4 \brief pkgCache - Structure definitions for the cache file
6 The goal of the cache file is two fold:
7 Firstly to speed loading and processing of the package file array and
8 secondly to reduce memory consumption of the package file array.
10 The implementation is aimed at an environment with many primary package
11 files, for instance someone that has a Package file for their CD-ROM, a
12 Package file for the latest version of the distribution on the CD-ROM and a
13 package file for the development version. Always present is the information
14 contained in the status file which might be considered a separate package
17 Please understand, this is designed as a <b>Cache file</b> it is not meant to be
18 used on any system other than the one it was created for. It is not meant to
19 be authoritative either, i.e. if a system crash or software failure occurs it
20 must be perfectly acceptable for the cache file to be in an inconsistent
21 state. Furthermore at any time the cache file may be erased without losing
24 Also the structures and storage layout is optimized for use by the APT
25 and may not be suitable for all purposes. However it should be possible
26 to extend it with associate cache files that contain other information.
28 To keep memory use down the cache file only contains often used fields and
29 fields that are inexpensive to store, the Package file has a full list of
30 fields. Also the client may assume that all items are perfectly valid and
31 need not perform checks against their correctness. Removal of information
32 from the cache is possible, but blanks will be left in the file, and
33 unused strings will also be present. The recommended implementation is to
34 simply rebuild the cache each time any of the data files change. It is
35 possible to add a new package file to the cache without any negative side
38 <b>Note on Pointer access</b>
39 Clients should always use the CacheIterators classes for access to the
40 cache and the data in it. They also provide a simple STL-like method for
41 traversing the links of the datastructure.
43 Every item in every structure is stored as the index to that structure.
44 What this means is that once the files is mmaped every data access has to
45 go through a fix up stage to get a real memory pointer. This is done
46 by taking the index, multiplying it by the type size and then adding
47 it to the start address of the memory block. This sounds complex, but
48 in C it is a single array dereference. Because all items are aligned to
49 their size and indexes are stored as multiples of the size of the structure
50 the format is immediately portable to all possible architectures - BUT the
51 generated files are -NOT-.
53 This scheme allows code like this to be written:
55 void *Map = mmap(...);
56 Package *PkgList = (Package *)Map;
57 Header *Head = (Header *)Map;
58 char *Strings = (char *)Map;
59 cout << (Strings + PkgList[Head->HashTable[0]]->Name) << endl;
61 Notice the lack of casting or multiplication. The net result is to return
62 the name of the first package in the first hash bucket, without error
65 The generator uses allocation pools to group similarly sized structures in
66 large blocks to eliminate any alignment overhead. The generator also
67 assures that no structures overlap and all indexes are unique. Although
68 at first glance it may seem like there is the potential for two structures
69 to exist at the same point the generator never allows this to happen.
70 (See the discussion of free space pools)
72 See \ref pkgcachegen.h for more information about generating cache structures. */
74 #ifndef PKGLIB_PKGCACHE_H
75 #define PKGLIB_PKGCACHE_H
80 #include <apt-pkg/mmap.h>
84 class pkgVersioningSystem
;
85 class pkgCache
/*{{{*/
88 // Cache element predeclarations
102 template<typename Str
, typename Itr
> class Iterator
;
109 class PkgFileIterator
;
110 class VerFileIterator
;
111 class DescFileIterator
;
115 // These are all the constants used in the cache structures
117 // WARNING - if you change these lists you must also edit
118 // the stringification in pkgcache.cc and also consider whether
119 // the cache file will become incompatible.
122 enum DepType
{Depends
=1,PreDepends
=2,Suggests
=3,Recommends
=4,
123 Conflicts
=5,Replaces
=6,Obsoletes
=7,DpkgBreaks
=8,Enhances
=9};
124 /** \brief available compare operators
126 The lower 4 bits are used to indicate what operator is being specified and
127 the upper 4 bits are flags. OR indicates that the next package is
128 or'd with the current package. */
129 enum DepCompareOp
{Or
=0x10,NoOp
=0,LessEq
=0x1,GreaterEq
=0x2,Less
=0x3,
130 Greater
=0x4,Equals
=0x5,NotEquals
=0x6};
135 /** \brief priority of a package version
137 Zero is used for unparsable or absent Priority fields. */
138 enum VerPriority
{Important
=1,Required
=2,Standard
=3,Optional
=4,Extra
=5};
139 enum PkgSelectedState
{Unknown
=0,Install
=1,Hold
=2,DeInstall
=3,Purge
=4};
140 enum PkgInstState
{Ok
=0,ReInstReq
=1,HoldInst
=2,HoldReInstReq
=3};
141 enum PkgCurrentState
{NotInstalled
=0,UnPacked
=1,HalfConfigured
=2,
142 HalfInstalled
=4,ConfigFiles
=5,Installed
=6,
143 TriggersAwaited
=7,TriggersPending
=8};
148 enum PkgFlags
{Auto
=(1<<0),Essential
=(1<<3),Important
=(1<<4)};
149 enum PkgFFlags
{NotSource
=(1<<0),NotAutomatic
=(1<<1)};
154 // Memory mapped cache file
158 unsigned long sHash(const string
&S
) const;
159 unsigned long sHash(const char *S
) const;
163 // Pointers to the arrays of items
169 PackageFile
*PkgFileP
;
174 StringItem
*StringItemP
;
177 virtual bool ReMap();
178 inline bool Sync() {return Map
.Sync();};
179 inline MMap
&GetMap() {return Map
;};
180 inline void *DataEnd() {return ((unsigned char *)Map
.Data()) + Map
.Size();};
182 // String hashing function (512 range)
183 inline unsigned long Hash(const string
&S
) const {return sHash(S
);};
184 inline unsigned long Hash(const char *S
) const {return sHash(S
);};
186 // Useful transformation things
187 const char *Priority(unsigned char Priority
);
190 GrpIterator
FindGrp(const string
&Name
);
191 PkgIterator
FindPkg(const string
&Name
);
192 PkgIterator
FindPkg(const string
&Name
, const string
&Arch
);
194 Header
&Head() {return *HeaderP
;};
195 inline GrpIterator
GrpBegin();
196 inline GrpIterator
GrpEnd();
197 inline PkgIterator
PkgBegin();
198 inline PkgIterator
PkgEnd();
199 inline PkgFileIterator
FileBegin();
200 inline PkgFileIterator
FileEnd();
202 inline bool MultiArchCache() const { return MultiArchEnabled
; };
204 // Make me a function
205 pkgVersioningSystem
*VS
;
208 static const char *CompTypeDeb(unsigned char Comp
);
209 static const char *CompType(unsigned char Comp
);
210 static const char *DepType(unsigned char Dep
);
212 pkgCache(MMap
*Map
,bool DoMap
= true);
213 virtual ~pkgCache() {};
216 bool MultiArchEnabled
;
217 PkgIterator
SingleArchFindPkg(const string
&Name
);
220 // Header structure /*{{{*/
221 struct pkgCache::Header
223 /** \brief Signature information
225 This must contain the hex value 0x98FE76DC which is designed to
226 verify that the system loading the image has the same byte order
227 and byte size as the system saving the image */
228 unsigned long Signature
;
229 /** These contain the version of the cache file */
232 /** \brief indicates if the cache should be erased
234 Dirty is true if the cache file was opened for reading, the client
235 expects to have written things to it and have not fully synced it.
236 The file should be erased and rebuilt if it is true. */
239 /** \brief Size of structure values
241 All *Sz variables contains the sizeof() that particular structure.
242 It is used as an extra consistency check on the structure of the file.
244 If any of the size values do not exactly match what the client expects
245 then the client should refuse the load the file. */
246 unsigned short HeaderSz
;
247 unsigned short PackageSz
;
248 unsigned short PackageFileSz
;
249 unsigned short VersionSz
;
250 unsigned short DescriptionSz
;
251 unsigned short DependencySz
;
252 unsigned short ProvidesSz
;
253 unsigned short VerFileSz
;
254 unsigned short DescFileSz
;
256 /** \brief Structure counts
258 These indicate the number of each structure contained in the cache.
259 PackageCount is especially useful for generating user state structures.
260 See Package::Id for more info. */
261 unsigned long GroupCount
;
262 unsigned long PackageCount
;
263 unsigned long VersionCount
;
264 unsigned long DescriptionCount
;
265 unsigned long DependsCount
;
266 unsigned long PackageFileCount
;
267 unsigned long VerFileCount
;
268 unsigned long DescFileCount
;
269 unsigned long ProvidesCount
;
271 /** \brief index of the first PackageFile structure
273 The PackageFile structures are singly linked lists that represent
274 all package files that have been merged into the cache. */
276 /** \brief index of the first StringItem structure
278 The cache contains a list of all the unique strings (StringItems).
279 The parser reads this list into memory so it can match strings
281 map_ptrloc StringList
;
282 /** \brief String representing the version system used */
283 map_ptrloc VerSysName
;
284 /** \brief Architecture(s) the cache was built against */
285 map_ptrloc Architecture
;
286 /** \brief The maximum size of a raw entry from the original Package file */
287 unsigned long MaxVerFileSize
;
288 /** \brief The maximum size of a raw entry from the original Translation file */
289 unsigned long MaxDescFileSize
;
291 /** \brief The Pool structures manage the allocation pools that the generator uses
293 Start indicates the first byte of the pool, Count is the number of objects
294 remaining in the pool and ItemSize is the structure size (alignment factor)
295 of the pool. An ItemSize of 0 indicates the pool is empty. There should be
296 the same number of pools as there are structure types. The generator
297 stores this information so future additions can make use of any unused pool
299 DynamicMMap::Pool Pools
[9];
301 /** \brief hash tables providing rapid group/package name lookup
303 Each group/package name is inserted into the hash table using pkgCache::Hash(const &string)
304 By iterating over each entry in the hash table it is possible to iterate over
305 the entire list of packages. Hash Collisions are handled with a singly linked
306 list of packages based at the hash item. The linked list contains only
307 packages that match the hashing function.
308 In the PkgHashTable is it possible that multiple packages have the same name -
309 these packages are stored as a sequence in the list.
311 Beware: The Hashmethod assumes that the hash table sizes are equal */
312 map_ptrloc PkgHashTable
[2*1048];
313 map_ptrloc GrpHashTable
[2*1048];
315 bool CheckSizes(Header
&Against
) const;
319 // Group structure /*{{{*/
320 /** \brief groups architecture depending packages together
322 On or more packages with the same name form a group, so we have
323 a simple way to access a package built for different architectures
324 Group exists in a singly linked list of group records starting at
325 the hash index of the name in the pkgCache::Header::GrpHashTable */
326 struct pkgCache::Group
328 /** \brief Name of the group */
329 map_ptrloc Name
; // StringItem
332 /** Link to the first package which belongs to the group */
333 map_ptrloc FirstPackage
; // Package
334 /** Link to the last package which belongs to the group */
335 map_ptrloc LastPackage
; // Package
336 /** Link to the next Group */
337 map_ptrloc Next
; // Group
340 // Package structure /*{{{*/
341 /** \brief contains information for a single unique package
343 There can be any number of versions of a given package.
344 Package exists in a singly linked list of package records starting at
345 the hash index of the name in the pkgCache::Header::PkgHashTable
347 A package can be created for every architecture so package names are
348 not unique, but it is garanteed that packages with the same name
349 are sequencel ordered in the list. Packages with the same name can be
350 accessed with the Group.
352 struct pkgCache::Package
354 /** \brief Name of the package */
355 map_ptrloc Name
; // StringItem
356 /** \brief Architecture of the package */
357 map_ptrloc Arch
; // StringItem
358 /** \brief Base of a singly linked list of versions
360 Each structure represents a unique version of the package.
361 The version structures contain links into PackageFile and the
362 original text file as well as detailed information about the size
363 and dependencies of the specific package. In this way multiple
364 versions of a package can be cleanly handled by the system.
365 Furthermore, this linked list is guaranteed to be sorted
366 from Highest version to lowest version with no duplicate entries. */
367 map_ptrloc VersionList
; // Version
368 /** \brief index to the installed version */
369 map_ptrloc CurrentVer
; // Version
370 /** \brief indicates the deduced section
372 Should be the index to the string "Unknown" or to the section
373 of the last parsed item. */
374 map_ptrloc Section
; // StringItem
375 /** \brief index of the group this package belongs to */
376 map_ptrloc Group
; // Group the Package belongs to
379 /** \brief Link to the next package in the same bucket */
380 map_ptrloc NextPackage
; // Package
381 /** \brief List of all dependencies on this package */
382 map_ptrloc RevDepends
; // Dependency
383 /** \brief List of all "packages" this package provide */
384 map_ptrloc ProvidesList
; // Provides
386 // Install/Remove/Purge etc
387 /** \brief state that the user wishes the package to be in */
388 unsigned char SelectedState
; // What
389 /** \brief installation state of the package
391 This should be "ok" but in case the installation failed
392 it will be different.
394 unsigned char InstState
; // Flags
395 /** \brief indicates if the package is installed */
396 unsigned char CurrentState
; // State
398 /** \brief unique sequel ID
400 ID is a unique value from 0 to Header->PackageCount assigned by the generator.
401 This allows clients to create an array of size PackageCount and use it to store
402 state information for the package map. For instance the status file emitter uses
403 this to track which packages have been emitted already. */
405 /** \brief some useful indicators of the package's state */
409 // Package File structure /*{{{*/
410 /** \brief stores information about the files used to generate the cache
412 Package files are referenced by Version structures to be able to know
413 after the generation still from which Packages file includes this Version
414 as we need this information later on e.g. for pinning. */
415 struct pkgCache::PackageFile
417 /** \brief physical disk file that this PackageFile represents */
418 map_ptrloc FileName
; // StringItem
419 /** \brief the release information
421 Please see the files document for a description of what the
422 release information means. */
423 map_ptrloc Archive
; // StringItem
424 map_ptrloc Codename
; // StringItem
425 map_ptrloc Component
; // StringItem
426 map_ptrloc Version
; // StringItem
427 map_ptrloc Origin
; // StringItem
428 map_ptrloc Label
; // StringItem
429 map_ptrloc Architecture
; // StringItem
430 /** \brief The site the index file was fetched from */
431 map_ptrloc Site
; // StringItem
432 /** \brief indicates what sort of index file this is
434 @TODO enumerate at least the possible indexes */
435 map_ptrloc IndexType
; // StringItem
436 /** \brief Size of the file
438 Used together with the modification time as a
439 simple check to ensure that the Packages
440 file has not been altered since Cache generation. */
442 /** \brief Modification time for the file */
445 /* @TODO document PackageFile::Flags */
449 /** \brief Link to the next PackageFile in the Cache */
450 map_ptrloc NextFile
; // PackageFile
451 /** \brief unique sequel ID */
455 // VerFile structure /*{{{*/
456 /** \brief associates a version with a PackageFile
458 This allows a full description of all Versions in all files
459 (and hence all sources) under consideration. */
460 struct pkgCache::VerFile
462 /** \brief index of the package file that this version was found in */
463 map_ptrloc File
; // PackageFile
464 /** \brief next step in the linked list */
465 map_ptrloc NextFile
; // PkgVerFile
466 /** \brief position in the package file */
467 map_ptrloc Offset
; // File offset
468 /* @TODO document pkgCache::VerFile::Size */
472 // DescFile structure /*{{{*/
473 /** \brief associates a description with a Translation file */
474 struct pkgCache::DescFile
476 /** \brief index of the file that this description was found in */
477 map_ptrloc File
; // PackageFile
478 /** \brief next step in the linked list */
479 map_ptrloc NextFile
; // PkgVerFile
480 /** \brief position in the file */
481 map_ptrloc Offset
; // File offset
482 /* @TODO document pkgCache::DescFile::Size */
486 // Version structure /*{{{*/
487 /** \brief information for a single version of a package
489 The version list is always sorted from highest version to lowest
490 version by the generator. Equal version numbers are either merged
491 or handled as separate versions based on the Hash value. */
492 struct pkgCache::Version
494 /** \brief complete version string */
495 map_ptrloc VerStr
; // StringItem
496 /** \brief section this version is filled in */
497 map_ptrloc Section
; // StringItem
498 /** \brief stores the MultiArch capabilities of this version
500 None is the default and doesn't trigger special behaviour,
501 Foreign means that this version can fulfill dependencies even
502 if it is built for another architecture as the requester.
503 Same indicates that builds for different architectures can
504 be co-installed on the system and All is the marker for a
505 version with the Architecture: all. */
506 enum {None
, All
, Foreign
, Same
, Allowed
} MultiArch
;
508 /** \brief references all the PackageFile's that this version came from
510 FileList can be used to determine what distribution(s) the Version
511 applies to. If FileList is 0 then this is a blank version.
512 The structure should also have a 0 in all other fields excluding
513 pkgCache::Version::VerStr and Possibly pkgCache::Version::NextVer. */
514 map_ptrloc FileList
; // VerFile
515 /** \brief next (lower or equal) version in the linked list */
516 map_ptrloc NextVer
; // Version
517 /** \brief next description in the linked list */
518 map_ptrloc DescriptionList
; // Description
519 /** \brief base of the dependency list */
520 map_ptrloc DependsList
; // Dependency
521 /** \brief links to the owning package
523 This allows reverse dependencies to determine the package */
524 map_ptrloc ParentPkg
; // Package
525 /** \brief list of pkgCache::Provides */
526 map_ptrloc ProvidesList
; // Provides
528 /** \brief archive size for this version
530 For Debian this is the size of the .deb file. */
531 map_ptrloc Size
; // These are the .deb size
532 /** \brief uncompressed size for this version */
533 map_ptrloc InstalledSize
;
534 /** \brief characteristic value representing this version
536 No two packages in existence should have the same VerStr
537 and Hash with different contents. */
539 /** \brief unique sequel ID */
541 /** \brief parsed priority value */
542 unsigned char Priority
;
545 // Description structure /*{{{*/
546 /** \brief datamember of a linked list of available description for a version */
547 struct pkgCache::Description
549 /** \brief Language code of this description (translation)
551 If the value has a 0 length then this is read using the Package
552 file else the Translation-CODE file is used. */
553 map_ptrloc language_code
; // StringItem
554 /** \brief MD5sum of the original description
556 Used to map Translations of a description to a version
557 and to check that the Translation is up-to-date. */
558 map_ptrloc md5sum
; // StringItem
560 /* @TODO document pkgCache::Description::FileList */
561 map_ptrloc FileList
; // DescFile
562 /** \brief next translation for this description */
563 map_ptrloc NextDesc
; // Description
564 /** \brief the text is a description of this package */
565 map_ptrloc ParentPkg
; // Package
567 /** \brief unique sequel ID */
571 // Dependency structure /*{{{*/
572 /** \brief information for a single dependency record
574 The records are split up like this to ease processing by the client.
575 The base of the linked list is pkgCache::Version::DependsList.
576 All forms of dependencies are recorded here including Depends,
577 Recommends, Suggests, Enhances, Conflicts, Replaces and Breaks. */
578 struct pkgCache::Dependency
580 /** \brief string of the version the dependency is applied against */
581 map_ptrloc Version
; // StringItem
582 /** \brief index of the package this depends applies to
584 The generator will - if the package does not already exist -
585 create a blank (no version records) package. */
586 map_ptrloc Package
; // Package
587 /** \brief next dependency of this version */
588 map_ptrloc NextDepends
; // Dependency
589 /** \brief next reverse dependency of this package */
590 map_ptrloc NextRevDepends
; // Dependency
591 /** \brief version of the package which has the reverse depends */
592 map_ptrloc ParentVer
; // Version
594 /** \brief unique sequel ID */
596 /** \brief Dependency type - Depends, Recommends, Conflicts, etc */
598 /** \brief comparison operator specified on the depends line
600 If the high bit is set then it is a logical OR with the previous record. */
601 unsigned char CompareOp
;
604 // Provides structure /*{{{*/
605 /** \brief handles virtual packages
607 When a Provides: line is encountered a new provides record is added
608 associating the package with a virtual package name.
609 The provides structures are linked off the package structures.
610 This simplifies the analysis of dependencies and other aspects A provides
611 refers to a specific version of a specific package, not all versions need to
612 provide that provides.*/
613 struct pkgCache::Provides
615 /** \brief index of the package providing this */
616 map_ptrloc ParentPkg
; // Package
617 /** \brief index of the version this provide line applies to */
618 map_ptrloc Version
; // Version
619 /** \brief version in the provides line (if any)
621 This version allows dependencies to depend on specific versions of a
622 Provides, as well as allowing Provides to override existing packages.
623 This is experimental. Note that Debian doesn't allow versioned provides */
624 map_ptrloc ProvideVersion
; // StringItem
625 /** \brief next provides (based of package) */
626 map_ptrloc NextProvides
; // Provides
627 /** \brief next provides (based of version) */
628 map_ptrloc NextPkgProv
; // Provides
631 // StringItem structure /*{{{*/
632 /** \brief used for generating single instances of strings
634 Some things like Section Name are are useful to have as unique tags.
635 It is part of a linked list based at pkgCache::Header::StringList
637 All strings are simply inlined any place in the file that is natural
638 for the writer. The client should make no assumptions about the positioning
639 of strings. All StringItems should be null-terminated. */
640 struct pkgCache::StringItem
642 /** \brief string this refers to */
643 map_ptrloc String
; // StringItem
644 /** \brief Next link in the chain */
645 map_ptrloc NextItem
; // StringItem
648 #include <apt-pkg/cacheiterators.h>
650 inline pkgCache::GrpIterator
pkgCache::GrpBegin()
651 {return GrpIterator(*this);};
652 inline pkgCache::GrpIterator
pkgCache::GrpEnd()
653 {return GrpIterator(*this,GrpP
);};
654 inline pkgCache::PkgIterator
pkgCache::PkgBegin()
655 {return PkgIterator(*this);};
656 inline pkgCache::PkgIterator
pkgCache::PkgEnd()
657 {return PkgIterator(*this,PkgP
);};
658 inline pkgCache::PkgFileIterator
pkgCache::FileBegin()
659 {return PkgFileIterator(*this,PkgFileP
+ HeaderP
->FileList
);};
660 inline pkgCache::PkgFileIterator
pkgCache::FileEnd()
661 {return PkgFileIterator(*this,PkgFileP
);};
663 // Oh I wish for Real Name Space Support
664 class pkgCache::Namespace
/*{{{*/
667 typedef pkgCache::GrpIterator GrpIterator
;
668 typedef pkgCache::PkgIterator PkgIterator
;
669 typedef pkgCache::VerIterator VerIterator
;
670 typedef pkgCache::DescIterator DescIterator
;
671 typedef pkgCache::DepIterator DepIterator
;
672 typedef pkgCache::PrvIterator PrvIterator
;
673 typedef pkgCache::PkgFileIterator PkgFileIterator
;
674 typedef pkgCache::VerFileIterator VerFileIterator
;
675 typedef pkgCache::Version Version
;
676 typedef pkgCache::Description Description
;
677 typedef pkgCache::Package Package
;
678 typedef pkgCache::Header Header
;
679 typedef pkgCache::Dep Dep
;
680 typedef pkgCache::Flag Flag
;