filelist.h 9.6 KB

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