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
77 #include <apt-pkg/mmap.h>
78 #include <apt-pkg/macros.h>
84 #ifndef APT_8_CLEANER_HEADERS
88 // storing file sizes of indexes, which are way below 4 GB for now
89 typedef uint32_t map_filesize_t
;
90 // each package/group/dependency gets an id
91 typedef uint32_t map_id_t
;
92 // some files get an id, too, but in far less absolute numbers
93 typedef uint16_t map_fileid_t
;
94 // relative pointer from cache start
95 typedef uint32_t map_pointer_t
;
96 // same as the previous, but documented to be to a string item
97 typedef map_pointer_t map_stringitem_t
;
99 class pkgVersioningSystem
;
100 class pkgCache
/*{{{*/
103 // Cache element predeclarations
116 template<typename Str
, typename Itr
> class Iterator
;
123 class PkgFileIterator
;
124 class VerFileIterator
;
125 class DescFileIterator
;
129 // These are all the constants used in the cache structures
131 // WARNING - if you change these lists you must also edit
132 // the stringification in pkgcache.cc and also consider whether
133 // the cache file will become incompatible.
136 enum DepType
{Depends
=1,PreDepends
=2,Suggests
=3,Recommends
=4,
137 Conflicts
=5,Replaces
=6,Obsoletes
=7,DpkgBreaks
=8,Enhances
=9};
138 /** \brief available compare operators
140 The lower 4 bits are used to indicate what operator is being specified and
141 the upper 4 bits are flags. OR indicates that the next package is
142 or'd with the current package. */
143 enum DepCompareOp
{Or
=0x10,NoOp
=0,LessEq
=0x1,GreaterEq
=0x2,Less
=0x3,
144 Greater
=0x4,Equals
=0x5,NotEquals
=0x6};
149 /** \brief priority of a package version
151 Zero is used for unparsable or absent Priority fields. */
152 enum VerPriority
{Required
=1,Important
=2,Standard
=3,Optional
=4,Extra
=5};
153 enum PkgSelectedState
{Unknown
=0,Install
=1,Hold
=2,DeInstall
=3,Purge
=4};
154 enum PkgInstState
{Ok
=0,ReInstReq
=1,HoldInst
=2,HoldReInstReq
=3};
155 enum PkgCurrentState
{NotInstalled
=0,UnPacked
=1,HalfConfigured
=2,
156 HalfInstalled
=4,ConfigFiles
=5,Installed
=6,
157 TriggersAwaited
=7,TriggersPending
=8};
162 enum PkgFlags
{Auto
=(1<<0),Essential
=(1<<3),Important
=(1<<4)};
163 enum PkgFFlags
{NotSource
=(1<<0),NotAutomatic
=(1<<1),ButAutomaticUpgrades
=(1<<2)};
168 // Memory mapped cache file
169 std::string CacheFile
;
172 map_id_t
sHash(const std::string
&S
) const APT_PURE
;
173 map_id_t
sHash(const char *S
) const APT_PURE
;
177 // Pointers to the arrays of items
183 PackageFile
*PkgFileP
;
190 virtual bool ReMap(bool const &Errorchecks
= true);
191 inline bool Sync() {return Map
.Sync();}
192 inline MMap
&GetMap() {return Map
;}
193 inline void *DataEnd() {return ((unsigned char *)Map
.Data()) + Map
.Size();}
195 // String hashing function (512 range)
196 inline map_id_t
Hash(const std::string
&S
) const {return sHash(S
);}
197 inline map_id_t
Hash(const char *S
) const {return sHash(S
);}
199 // Useful transformation things
200 const char *Priority(unsigned char Priority
);
203 GrpIterator
FindGrp(const std::string
&Name
);
204 PkgIterator
FindPkg(const std::string
&Name
);
205 PkgIterator
FindPkg(const std::string
&Name
, const std::string
&Arch
);
207 Header
&Head() {return *HeaderP
;}
208 inline GrpIterator
GrpBegin();
209 inline GrpIterator
GrpEnd();
210 inline PkgIterator
PkgBegin();
211 inline PkgIterator
PkgEnd();
212 inline PkgFileIterator
FileBegin();
213 inline PkgFileIterator
FileEnd();
215 inline bool MultiArchCache() const { return MultiArchEnabled
; }
216 inline char const * NativeArch();
218 // Make me a function
219 pkgVersioningSystem
*VS
;
222 static const char *CompTypeDeb(unsigned char Comp
) APT_CONST
;
223 static const char *CompType(unsigned char Comp
) APT_CONST
;
224 static const char *DepType(unsigned char Dep
);
226 pkgCache(MMap
*Map
,bool DoMap
= true);
227 virtual ~pkgCache() {}
230 bool MultiArchEnabled
;
231 APT_HIDDEN PkgIterator
SingleArchFindPkg(const std::string
&Name
);
234 // Header structure /*{{{*/
235 struct pkgCache::Header
237 /** \brief Signature information
239 This must contain the hex value 0x98FE76DC which is designed to
240 verify that the system loading the image has the same byte order
241 and byte size as the system saving the image */
242 unsigned long Signature
;
243 /** These contain the version of the cache file */
246 /** \brief indicates if the cache should be erased
248 Dirty is true if the cache file was opened for reading, the client
249 expects to have written things to it and have not fully synced it.
250 The file should be erased and rebuilt if it is true. */
253 /** \brief Size of structure values
255 All *Sz variables contains the sizeof() that particular structure.
256 It is used as an extra consistency check on the structure of the file.
258 If any of the size values do not exactly match what the client expects
259 then the client should refuse the load the file. */
260 unsigned short HeaderSz
;
261 unsigned short GroupSz
;
262 unsigned short PackageSz
;
263 unsigned short PackageFileSz
;
264 unsigned short VersionSz
;
265 unsigned short DescriptionSz
;
266 unsigned short DependencySz
;
267 unsigned short ProvidesSz
;
268 unsigned short VerFileSz
;
269 unsigned short DescFileSz
;
271 /** \brief Structure counts
273 These indicate the number of each structure contained in the cache.
274 PackageCount is especially useful for generating user state structures.
275 See Package::Id for more info. */
277 map_id_t PackageCount
;
278 map_id_t VersionCount
;
279 map_id_t DescriptionCount
;
280 map_id_t DependsCount
;
281 map_fileid_t PackageFileCount
;
282 map_fileid_t VerFileCount
;
283 map_fileid_t DescFileCount
;
284 map_id_t ProvidesCount
;
286 /** \brief index of the first PackageFile structure
288 The PackageFile structures are singly linked lists that represent
289 all package files that have been merged into the cache. */
290 map_pointer_t FileList
;
291 /** \brief String representing the version system used */
292 map_pointer_t VerSysName
;
293 /** \brief native architecture the cache was built against */
294 map_pointer_t Architecture
;
295 /** \brief all architectures the cache was built against */
296 map_pointer_t Architectures
;
297 /** \brief The maximum size of a raw entry from the original Package file */
298 map_filesize_t MaxVerFileSize
;
299 /** \brief The maximum size of a raw entry from the original Translation file */
300 map_filesize_t MaxDescFileSize
;
302 /** \brief The Pool structures manage the allocation pools that the generator uses
304 Start indicates the first byte of the pool, Count is the number of objects
305 remaining in the pool and ItemSize is the structure size (alignment factor)
306 of the pool. An ItemSize of 0 indicates the pool is empty. There should be
307 the same number of pools as there are structure types. The generator
308 stores this information so future additions can make use of any unused pool
310 DynamicMMap::Pool Pools
[9];
312 /** \brief hash tables providing rapid group/package name lookup
314 Each group/package name is inserted into a hash table using pkgCache::Hash(const &string)
315 By iterating over each entry in the hash table it is possible to iterate over
316 the entire list of packages. Hash Collisions are handled with a singly linked
317 list of packages based at the hash item. The linked list contains only
318 packages that match the hashing function.
319 In the PkgHashTable is it possible that multiple packages have the same name -
320 these packages are stored as a sequence in the list.
321 The size of both tables is the same. */
322 unsigned int HashTableSize
;
323 map_pointer_t
* PkgHashTable() const { return (map_pointer_t
*) (this + 1); }
324 map_pointer_t
* GrpHashTable() const { return PkgHashTable() + HashTableSize
; }
326 /** \brief Size of the complete cache file */
327 unsigned long long CacheFileSize
;
329 bool CheckSizes(Header
&Against
) const APT_PURE
;
333 // Group structure /*{{{*/
334 /** \brief groups architecture depending packages together
336 On or more packages with the same name form a group, so we have
337 a simple way to access a package built for different architectures
338 Group exists in a singly linked list of group records starting at
339 the hash index of the name in the pkgCache::Header::GrpHashTable */
340 struct pkgCache::Group
342 /** \brief Name of the group */
343 map_stringitem_t Name
;
346 /** \brief Link to the first package which belongs to the group */
347 map_pointer_t FirstPackage
; // Package
348 /** \brief Link to the last package which belongs to the group */
349 map_pointer_t LastPackage
; // Package
350 /** \brief Link to the next Group */
351 map_pointer_t Next
; // Group
352 /** \brief unique sequel ID */
357 // Package structure /*{{{*/
358 /** \brief contains information for a single unique package
360 There can be any number of versions of a given package.
361 Package exists in a singly linked list of package records starting at
362 the hash index of the name in the pkgCache::Header::PkgHashTable
364 A package can be created for every architecture so package names are
365 not unique, but it is guaranteed that packages with the same name
366 are sequencel ordered in the list. Packages with the same name can be
367 accessed with the Group.
369 struct pkgCache::Package
371 /** \brief Name of the package
372 * Note that the access method Name() will remain. It is just this data member
373 * deprecated as this information is already stored and available via the
374 * associated Group – so it is wasting precious binary cache space */
375 APT_DEPRECATED map_stringitem_t Name
;
376 /** \brief Architecture of the package */
377 map_stringitem_t Arch
;
378 /** \brief Base of a singly linked list of versions
380 Each structure represents a unique version of the package.
381 The version structures contain links into PackageFile and the
382 original text file as well as detailed information about the size
383 and dependencies of the specific package. In this way multiple
384 versions of a package can be cleanly handled by the system.
385 Furthermore, this linked list is guaranteed to be sorted
386 from Highest version to lowest version with no duplicate entries. */
387 map_pointer_t VersionList
; // Version
388 /** \brief index to the installed version */
389 map_pointer_t CurrentVer
; // Version
390 /** \brief index of the group this package belongs to */
391 map_pointer_t Group
; // Group the Package belongs to
394 /** \brief Link to the next package in the same bucket */
395 map_pointer_t Next
; // Package
396 /** \brief List of all dependencies on this package */
397 map_pointer_t RevDepends
; // Dependency
398 /** \brief List of all "packages" this package provide */
399 map_pointer_t ProvidesList
; // Provides
401 // Install/Remove/Purge etc
402 /** \brief state that the user wishes the package to be in */
403 unsigned char SelectedState
; // What
404 /** \brief installation state of the package
406 This should be "ok" but in case the installation failed
407 it will be different.
409 unsigned char InstState
; // Flags
410 /** \brief indicates if the package is installed */
411 unsigned char CurrentState
; // State
413 /** \brief unique sequel ID
415 ID is a unique value from 0 to Header->PackageCount assigned by the generator.
416 This allows clients to create an array of size PackageCount and use it to store
417 state information for the package map. For instance the status file emitter uses
418 this to track which packages have been emitted already. */
420 /** \brief some useful indicators of the package's state */
424 // Package File structure /*{{{*/
425 /** \brief stores information about the files used to generate the cache
427 Package files are referenced by Version structures to be able to know
428 after the generation still from which Packages file includes this Version
429 as we need this information later on e.g. for pinning. */
430 struct pkgCache::PackageFile
432 /** \brief physical disk file that this PackageFile represents */
433 map_stringitem_t FileName
;
434 /** \brief the release information
436 Please see the files document for a description of what the
437 release information means. */
438 map_stringitem_t Archive
;
439 map_stringitem_t Codename
;
440 map_stringitem_t Component
;
441 map_stringitem_t Version
;
442 map_stringitem_t Origin
;
443 map_stringitem_t Label
;
444 map_stringitem_t Architecture
;
445 /** \brief The site the index file was fetched from */
446 map_stringitem_t Site
;
447 /** \brief indicates what sort of index file this is
449 @TODO enumerate at least the possible indexes */
450 map_stringitem_t IndexType
;
451 /** \brief Size of the file
453 Used together with the modification time as a
454 simple check to ensure that the Packages
455 file has not been altered since Cache generation. */
457 /** \brief Modification time for the file */
460 /** @TODO document PackageFile::Flags */
464 /** \brief Link to the next PackageFile in the Cache */
465 map_pointer_t NextFile
; // PackageFile
466 /** \brief unique sequel ID */
470 // VerFile structure /*{{{*/
471 /** \brief associates a version with a PackageFile
473 This allows a full description of all Versions in all files
474 (and hence all sources) under consideration. */
475 struct pkgCache::VerFile
477 /** \brief index of the package file that this version was found in */
478 map_pointer_t File
; // PackageFile
479 /** \brief next step in the linked list */
480 map_pointer_t NextFile
; // PkgVerFile
481 /** \brief position in the package file */
482 map_filesize_t Offset
; // File offset
483 /** @TODO document pkgCache::VerFile::Size */
487 // DescFile structure /*{{{*/
488 /** \brief associates a description with a Translation file */
489 struct pkgCache::DescFile
491 /** \brief index of the file that this description was found in */
492 map_pointer_t File
; // PackageFile
493 /** \brief next step in the linked list */
494 map_pointer_t NextFile
; // PkgVerFile
495 /** \brief position in the file */
496 map_filesize_t Offset
; // File offset
497 /** @TODO document pkgCache::DescFile::Size */
501 // Version structure /*{{{*/
502 /** \brief information for a single version of a package
504 The version list is always sorted from highest version to lowest
505 version by the generator. Equal version numbers are either merged
506 or handled as separate versions based on the Hash value. */
507 struct pkgCache::Version
509 /** \brief complete version string */
510 map_stringitem_t VerStr
;
511 /** \brief section this version is filled in */
512 map_stringitem_t Section
;
513 /** \brief source package name this version comes from
514 Always contains the name, even if it is the same as the binary name */
515 map_stringitem_t SourcePkgName
;
516 /** \brief source version this version comes from
517 Always contains the version string, even if it is the same as the binary version */
518 map_stringitem_t SourceVerStr
;
520 /** \brief Multi-Arch capabilities of a package version */
521 enum VerMultiArch
{ None
= 0, /*!< is the default and doesn't trigger special behaviour */
522 All
= (1<<0), /*!< will cause that Ver.Arch() will report "all" */
523 Foreign
= (1<<1), /*!< can satisfy dependencies in another architecture */
524 Same
= (1<<2), /*!< can be co-installed with itself from other architectures */
525 Allowed
= (1<<3), /*!< other packages are allowed to depend on thispkg:any */
526 AllForeign
= All
| Foreign
,
527 AllAllowed
= All
| Allowed
};
528 /** \brief stores the MultiArch capabilities of this version
530 Flags used are defined in pkgCache::Version::VerMultiArch
532 unsigned char MultiArch
;
534 /** \brief references all the PackageFile's that this version came from
536 FileList can be used to determine what distribution(s) the Version
537 applies to. If FileList is 0 then this is a blank version.
538 The structure should also have a 0 in all other fields excluding
539 pkgCache::Version::VerStr and Possibly pkgCache::Version::NextVer. */
540 map_pointer_t FileList
; // VerFile
541 /** \brief next (lower or equal) version in the linked list */
542 map_pointer_t NextVer
; // Version
543 /** \brief next description in the linked list */
544 map_pointer_t DescriptionList
; // Description
545 /** \brief base of the dependency list */
546 map_pointer_t DependsList
; // Dependency
547 /** \brief links to the owning package
549 This allows reverse dependencies to determine the package */
550 map_pointer_t ParentPkg
; // Package
551 /** \brief list of pkgCache::Provides */
552 map_pointer_t ProvidesList
; // Provides
554 /** \brief archive size for this version
556 For Debian this is the size of the .deb file. */
557 uint64_t Size
; // These are the .deb size
558 /** \brief uncompressed size for this version */
559 uint64_t InstalledSize
;
560 /** \brief characteristic value representing this version
562 No two packages in existence should have the same VerStr
563 and Hash with different contents. */
565 /** \brief unique sequel ID */
567 /** \brief parsed priority value */
568 unsigned char Priority
;
571 // Description structure /*{{{*/
572 /** \brief datamember of a linked list of available description for a version */
573 struct pkgCache::Description
575 /** \brief Language code of this description (translation)
577 If the value has a 0 length then this is read using the Package
578 file else the Translation-CODE file is used. */
579 map_stringitem_t language_code
;
580 /** \brief MD5sum of the original description
582 Used to map Translations of a description to a version
583 and to check that the Translation is up-to-date. */
584 map_stringitem_t md5sum
;
586 /** @TODO document pkgCache::Description::FileList */
587 map_pointer_t FileList
; // DescFile
588 /** \brief next translation for this description */
589 map_pointer_t NextDesc
; // Description
590 /** \brief the text is a description of this package */
591 map_pointer_t ParentPkg
; // Package
593 /** \brief unique sequel ID */
597 // Dependency structure /*{{{*/
598 /** \brief information for a single dependency record
600 The records are split up like this to ease processing by the client.
601 The base of the linked list is pkgCache::Version::DependsList.
602 All forms of dependencies are recorded here including Depends,
603 Recommends, Suggests, Enhances, Conflicts, Replaces and Breaks. */
604 struct pkgCache::Dependency
606 /** \brief string of the version the dependency is applied against */
607 map_stringitem_t Version
;
608 /** \brief index of the package this depends applies to
610 The generator will - if the package does not already exist -
611 create a blank (no version records) package. */
612 map_pointer_t Package
; // Package
613 /** \brief next dependency of this version */
614 map_pointer_t NextDepends
; // Dependency
615 /** \brief next reverse dependency of this package */
616 map_pointer_t NextRevDepends
; // Dependency
617 /** \brief version of the package which has the reverse depends */
618 map_pointer_t ParentVer
; // Version
620 /** \brief unique sequel ID */
622 /** \brief Dependency type - Depends, Recommends, Conflicts, etc */
624 /** \brief comparison operator specified on the depends line
626 If the high bit is set then it is a logical OR with the previous record. */
627 unsigned char CompareOp
;
630 // Provides structure /*{{{*/
631 /** \brief handles virtual packages
633 When a Provides: line is encountered a new provides record is added
634 associating the package with a virtual package name.
635 The provides structures are linked off the package structures.
636 This simplifies the analysis of dependencies and other aspects A provides
637 refers to a specific version of a specific package, not all versions need to
638 provide that provides.*/
639 struct pkgCache::Provides
641 /** \brief index of the package providing this */
642 map_pointer_t ParentPkg
; // Package
643 /** \brief index of the version this provide line applies to */
644 map_pointer_t Version
; // Version
645 /** \brief version in the provides line (if any)
647 This version allows dependencies to depend on specific versions of a
648 Provides, as well as allowing Provides to override existing packages.
649 This is experimental. Note that Debian doesn't allow versioned provides */
650 map_stringitem_t ProvideVersion
;
651 /** \brief next provides (based of package) */
652 map_pointer_t NextProvides
; // Provides
653 /** \brief next provides (based of version) */
654 map_pointer_t NextPkgProv
; // Provides
658 inline char const * pkgCache::NativeArch()
659 { return StrP
+ HeaderP
->Architecture
; }
661 #include <apt-pkg/cacheiterators.h>
663 inline pkgCache::GrpIterator
pkgCache::GrpBegin()
664 {return GrpIterator(*this);}
665 inline pkgCache::GrpIterator
pkgCache::GrpEnd()
666 {return GrpIterator(*this,GrpP
);}
667 inline pkgCache::PkgIterator
pkgCache::PkgBegin()
668 {return PkgIterator(*this);}
669 inline pkgCache::PkgIterator
pkgCache::PkgEnd()
670 {return PkgIterator(*this,PkgP
);}
671 inline pkgCache::PkgFileIterator
pkgCache::FileBegin()
672 {return PkgFileIterator(*this,PkgFileP
+ HeaderP
->FileList
);}
673 inline pkgCache::PkgFileIterator
pkgCache::FileEnd()
674 {return PkgFileIterator(*this,PkgFileP
);}
676 // Oh I wish for Real Name Space Support
677 class pkgCache::Namespace
/*{{{*/
680 typedef pkgCache::GrpIterator GrpIterator
;
681 typedef pkgCache::PkgIterator PkgIterator
;
682 typedef pkgCache::VerIterator VerIterator
;
683 typedef pkgCache::DescIterator DescIterator
;
684 typedef pkgCache::DepIterator DepIterator
;
685 typedef pkgCache::PrvIterator PrvIterator
;
686 typedef pkgCache::PkgFileIterator PkgFileIterator
;
687 typedef pkgCache::VerFileIterator VerFileIterator
;
688 typedef pkgCache::Version Version
;
689 typedef pkgCache::Description Description
;
690 typedef pkgCache::Package Package
;
691 typedef pkgCache::Header Header
;
692 typedef pkgCache::Dep Dep
;
693 typedef pkgCache::Flag Flag
;