You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

313 lines
9.7 KiB

  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 <cstring>
  25. #include <apt-pkg/mmap.h>
  26. class pkgFLCache
  27. {
  28. public:
  29. struct Header;
  30. struct Node;
  31. struct Directory;
  32. struct Package;
  33. struct Diversion;
  34. struct ConfFile;
  35. class NodeIterator;
  36. class DirIterator;
  37. class PkgIterator;
  38. class DiverIterator;
  39. protected:
  40. string CacheFile;
  41. DynamicMMap &Map;
  42. map_ptrloc LastTreeLookup;
  43. unsigned long LastLookupSize;
  44. // Helpers for the addition algorithms
  45. map_ptrloc TreeLookup(map_ptrloc *Base,const char *Text,const char *TextEnd,
  46. unsigned long Size,unsigned int *Count = 0,
  47. bool Insert = false);
  48. public:
  49. // Pointers to the arrays of items
  50. Header *HeaderP;
  51. Node *NodeP;
  52. Directory *DirP;
  53. Package *PkgP;
  54. Diversion *DiverP;
  55. ConfFile *ConfP;
  56. char *StrP;
  57. unsigned char *AnyP;
  58. // Quick accessors
  59. Node *FileHash;
  60. // Accessors
  61. Header &Head() {return *HeaderP;};
  62. void PrintTree(map_ptrloc Base,unsigned long Size);
  63. // Add/Find things
  64. PkgIterator GetPkg(const char *Name,const char *End,bool Insert);
  65. inline PkgIterator GetPkg(const char *Name,bool Insert);
  66. NodeIterator GetNode(const char *Name,
  67. const char *NameEnd,
  68. map_ptrloc Loc,
  69. bool Insert,bool Divert);
  70. Node *HashNode(NodeIterator const &N);
  71. void DropNode(map_ptrloc Node);
  72. inline DiverIterator DiverBegin();
  73. // Diversion control
  74. void BeginDiverLoad();
  75. void FinishDiverLoad();
  76. bool AddDiversion(PkgIterator const &Owner,const char *From,
  77. const char *To);
  78. bool AddConfFile(const char *Name,const char *NameEnd,
  79. PkgIterator const &Owner,const unsigned char *Sum);
  80. pkgFLCache(DynamicMMap &Map);
  81. // ~pkgFLCache();
  82. };
  83. struct pkgFLCache::Header
  84. {
  85. // Signature information
  86. unsigned long Signature;
  87. short MajorVersion;
  88. short MinorVersion;
  89. bool Dirty;
  90. // Size of structure values
  91. unsigned HeaderSz;
  92. unsigned NodeSz;
  93. unsigned DirSz;
  94. unsigned PackageSz;
  95. unsigned DiversionSz;
  96. unsigned ConfFileSz;
  97. // Structure Counts;
  98. unsigned int NodeCount;
  99. unsigned int DirCount;
  100. unsigned int PackageCount;
  101. unsigned int DiversionCount;
  102. unsigned int ConfFileCount;
  103. unsigned int HashSize;
  104. unsigned long UniqNodes;
  105. // Offsets
  106. map_ptrloc FileHash;
  107. map_ptrloc DirTree;
  108. map_ptrloc Packages;
  109. map_ptrloc Diversions;
  110. /* Allocation pools, there should be one of these for each structure
  111. excluding the header */
  112. DynamicMMap::Pool Pools[5];
  113. bool CheckSizes(Header &Against) const;
  114. Header();
  115. };
  116. /* The bit field is used to advoid incurring an extra 4 bytes x 40000,
  117. Pointer is the most infrequently used member of the structure */
  118. struct pkgFLCache::Node
  119. {
  120. map_ptrloc Dir; // Dir
  121. map_ptrloc File; // String
  122. unsigned Pointer:24; // Package/Diversion/ConfFile
  123. unsigned Flags:8; // Package
  124. map_ptrloc Next; // Node
  125. map_ptrloc NextPkg; // Node
  126. enum Flags {Diversion = (1<<0),ConfFile = (1<<1),
  127. NewConfFile = (1<<2),NewFile = (1<<3),
  128. Unpacked = (1<<4),Replaced = (1<<5)};
  129. };
  130. struct pkgFLCache::Directory
  131. {
  132. map_ptrloc Left; // Directory
  133. map_ptrloc Right; // Directory
  134. map_ptrloc Name; // String
  135. };
  136. struct pkgFLCache::Package
  137. {
  138. map_ptrloc Left; // Package
  139. map_ptrloc Right; // Package
  140. map_ptrloc Name; // String
  141. map_ptrloc Files; // Node
  142. };
  143. struct pkgFLCache::Diversion
  144. {
  145. map_ptrloc OwnerPkg; // Package
  146. map_ptrloc DivertFrom; // Node
  147. map_ptrloc DivertTo; // String
  148. map_ptrloc Next; // Diversion
  149. unsigned long Flags;
  150. enum Flags {Touched = (1<<0)};
  151. };
  152. struct pkgFLCache::ConfFile
  153. {
  154. map_ptrloc OwnerPkg; // Package
  155. unsigned char MD5[16];
  156. };
  157. class pkgFLCache::PkgIterator
  158. {
  159. Package *Pkg;
  160. pkgFLCache *Owner;
  161. public:
  162. inline bool end() const {return Owner == 0 || Pkg == Owner->PkgP?true:false;}
  163. // Accessors
  164. inline Package *operator ->() {return Pkg;};
  165. inline Package const *operator ->() const {return Pkg;};
  166. inline Package const &operator *() const {return *Pkg;};
  167. inline operator Package *() {return Pkg == Owner->PkgP?0:Pkg;};
  168. inline operator Package const *() const {return Pkg == Owner->PkgP?0:Pkg;};
  169. inline unsigned long Offset() const {return Pkg - Owner->PkgP;};
  170. inline const char *Name() const {return Pkg->Name == 0?0:Owner->StrP + Pkg->Name;};
  171. inline pkgFLCache::NodeIterator Files() const;
  172. PkgIterator() : Pkg(0), Owner(0) {};
  173. PkgIterator(pkgFLCache &Owner,Package *Trg) : Pkg(Trg), Owner(&Owner) {};
  174. };
  175. class pkgFLCache::DirIterator
  176. {
  177. Directory *Dir;
  178. pkgFLCache *Owner;
  179. public:
  180. // Accessors
  181. inline Directory *operator ->() {return Dir;};
  182. inline Directory const *operator ->() const {return Dir;};
  183. inline Directory const &operator *() const {return *Dir;};
  184. inline operator Directory *() {return Dir == Owner->DirP?0:Dir;};
  185. inline operator Directory const *() const {return Dir == Owner->DirP?0:Dir;};
  186. inline const char *Name() const {return Dir->Name == 0?0:Owner->StrP + Dir->Name;};
  187. DirIterator() : Dir(0), Owner(0) {};
  188. DirIterator(pkgFLCache &Owner,Directory *Trg) : Dir(Trg), Owner(&Owner) {};
  189. };
  190. class pkgFLCache::DiverIterator
  191. {
  192. Diversion *Diver;
  193. pkgFLCache *Owner;
  194. public:
  195. // Iteration
  196. void operator ++(int) {if (Diver != Owner->DiverP) Diver = Owner->DiverP + Diver->Next;};
  197. inline void operator ++() {operator ++(0);};
  198. inline bool end() const {return Owner == 0 || Diver == Owner->DiverP;};
  199. // Accessors
  200. inline Diversion *operator ->() {return Diver;};
  201. inline Diversion const *operator ->() const {return Diver;};
  202. inline Diversion const &operator *() const {return *Diver;};
  203. inline operator Diversion *() {return Diver == Owner->DiverP?0:Diver;};
  204. inline operator Diversion const *() const {return Diver == Owner->DiverP?0:Diver;};
  205. inline PkgIterator OwnerPkg() const {return PkgIterator(*Owner,Owner->PkgP + Diver->OwnerPkg);};
  206. inline NodeIterator DivertFrom() const;
  207. inline NodeIterator DivertTo() const;
  208. DiverIterator() : Diver(0), Owner(0) {};
  209. DiverIterator(pkgFLCache &Owner,Diversion *Trg) : Diver(Trg), Owner(&Owner) {};
  210. };
  211. class pkgFLCache::NodeIterator
  212. {
  213. Node *Nde;
  214. enum {NdePkg, NdeHash} Type;
  215. pkgFLCache *Owner;
  216. public:
  217. // Iteration
  218. void operator ++(int) {if (Nde != Owner->NodeP) Nde = Owner->NodeP +
  219. (Type == NdePkg?Nde->NextPkg:Nde->Next);};
  220. inline void operator ++() {operator ++(0);};
  221. inline bool end() const {return Owner == 0 || Nde == Owner->NodeP;};
  222. // Accessors
  223. inline Node *operator ->() {return Nde;};
  224. inline Node const *operator ->() const {return Nde;};
  225. inline Node const &operator *() const {return *Nde;};
  226. inline operator Node *() {return Nde == Owner->NodeP?0:Nde;};
  227. inline operator Node const *() const {return Nde == Owner->NodeP?0:Nde;};
  228. inline unsigned long Offset() const {return Nde - Owner->NodeP;};
  229. inline DirIterator Dir() const {return DirIterator(*Owner,Owner->DirP + Nde->Dir);};
  230. inline DiverIterator Diversion() const {return DiverIterator(*Owner,Owner->DiverP + Nde->Pointer);};
  231. inline const char *File() const {return Nde->File == 0?0:Owner->StrP + Nde->File;};
  232. inline const char *DirN() const {return Owner->StrP + Owner->DirP[Nde->Dir].Name;};
  233. Package *RealPackage() const;
  234. NodeIterator() : Nde(0), Type(NdeHash), Owner(0) {};
  235. NodeIterator(pkgFLCache &Owner) : Nde(Owner.NodeP), Type(NdeHash), Owner(&Owner) {};
  236. NodeIterator(pkgFLCache &Owner,Node *Trg) : Nde(Trg), Type(NdeHash), Owner(&Owner) {};
  237. NodeIterator(pkgFLCache &Owner,Node *Trg,Package *) : Nde(Trg), Type(NdePkg), Owner(&Owner) {};
  238. };
  239. /* Inlines with forward references that cannot be included directly in their
  240. respsective classes */
  241. inline pkgFLCache::NodeIterator pkgFLCache::DiverIterator::DivertFrom() const
  242. {return NodeIterator(*Owner,Owner->NodeP + Diver->DivertFrom);};
  243. inline pkgFLCache::NodeIterator pkgFLCache::DiverIterator::DivertTo() const
  244. {return NodeIterator(*Owner,Owner->NodeP + Diver->DivertTo);};
  245. inline pkgFLCache::NodeIterator pkgFLCache::PkgIterator::Files() const
  246. {return NodeIterator(*Owner,Owner->NodeP + Pkg->Files,Pkg);};
  247. inline pkgFLCache::DiverIterator pkgFLCache::DiverBegin()
  248. {return DiverIterator(*this,DiverP + HeaderP->Diversions);};
  249. inline pkgFLCache::PkgIterator pkgFLCache::GetPkg(const char *Name,bool Insert)
  250. {return GetPkg(Name,Name+strlen(Name),Insert);};
  251. #endif