filelist.h 9.7 KB

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