pkgcachegen.h 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. // -*- mode: cpp; mode: fold -*-
  2. // Description /*{{{*/
  3. /* ######################################################################
  4. Package Cache Generator - Generator for the cache structure.
  5. This builds the cache structure from the abstract package list parser.
  6. Each archive source has it's own list parser that is instantiated by
  7. the caller to provide data for the generator.
  8. Parts of the cache are created by this generator class while other
  9. parts are created by the list parser. The list parser is responsible
  10. for creating version, depends and provides structures, and some of
  11. their contents
  12. ##################################################################### */
  13. /*}}}*/
  14. #ifndef PKGLIB_PKGCACHEGEN_H
  15. #define PKGLIB_PKGCACHEGEN_H
  16. #include <apt-pkg/md5.h>
  17. #include <apt-pkg/mmap.h>
  18. #include <apt-pkg/pkgcache.h>
  19. #include <apt-pkg/cacheiterators.h>
  20. #include <apt-pkg/macros.h>
  21. #include <vector>
  22. #include <string>
  23. #if __cplusplus >= 201103L
  24. #include <unordered_set>
  25. #endif
  26. #ifdef APT_PKG_EXPOSE_STRING_VIEW
  27. #include <apt-pkg/string_view.h>
  28. #endif
  29. class FileFd;
  30. class pkgSourceList;
  31. class OpProgress;
  32. class pkgIndexFile;
  33. class pkgCacheListParser;
  34. class APT_HIDDEN pkgCacheGenerator /*{{{*/
  35. {
  36. #ifdef APT_PKG_EXPOSE_STRING_VIEW
  37. APT_HIDDEN map_stringitem_t WriteStringInMap(APT::StringView String) { return WriteStringInMap(String.data(), String.size()); };
  38. #endif
  39. APT_HIDDEN map_stringitem_t WriteStringInMap(const char *String);
  40. APT_HIDDEN map_stringitem_t WriteStringInMap(const char *String, const unsigned long &Len);
  41. APT_HIDDEN map_pointer_t AllocateInMap(const unsigned long &size);
  42. // Dirty hack for public users that do not use C++11 yet
  43. #if __cplusplus >= 201103L
  44. struct string_pointer {
  45. const char *data_;
  46. size_t size;
  47. pkgCacheGenerator *generator;
  48. map_stringitem_t item;
  49. const char *data() const {
  50. return data_ != nullptr ? data_ : static_cast<char*>(generator->Map.Data()) + item;
  51. }
  52. bool operator ==(string_pointer const &other) const {
  53. return size == other.size && memcmp(data(), other.data(), size) == 0;
  54. }
  55. };
  56. struct hash {
  57. uint32_t operator()(string_pointer const &that) const {
  58. uint32_t Hash = 5381;
  59. const char * const end = that.data() + that.size;
  60. for (const char *I = that.data(); I != end; ++I)
  61. Hash = 33 * Hash + *I;
  62. return Hash;
  63. }
  64. };
  65. std::unordered_set<string_pointer, hash> strMixed;
  66. std::unordered_set<string_pointer, hash> strPkgNames;
  67. std::unordered_set<string_pointer, hash> strVersions;
  68. std::unordered_set<string_pointer, hash> strSections;
  69. std::unordered_set<string_pointer, hash> strTags;
  70. #endif
  71. friend class pkgCacheListParser;
  72. typedef pkgCacheListParser ListParser;
  73. public:
  74. template<typename Iter> class Dynamic {
  75. public:
  76. static std::vector<Iter*> toReMap;
  77. explicit Dynamic(Iter &I) {
  78. toReMap.push_back(&I);
  79. }
  80. ~Dynamic() {
  81. toReMap.pop_back();
  82. }
  83. #if __cplusplus >= 201103L
  84. Dynamic(const Dynamic&) = delete;
  85. void operator=(const Dynamic&) = delete;
  86. #endif
  87. };
  88. protected:
  89. DynamicMMap &Map;
  90. pkgCache Cache;
  91. OpProgress *Progress;
  92. std::string RlsFileName;
  93. pkgCache::ReleaseFile *CurrentRlsFile;
  94. std::string PkgFileName;
  95. pkgCache::PackageFile *CurrentFile;
  96. #ifdef APT_PKG_EXPOSE_STRING_VIEW
  97. bool NewGroup(pkgCache::GrpIterator &Grp, APT::StringView Name);
  98. bool NewPackage(pkgCache::PkgIterator &Pkg, APT::StringView Name, APT::StringView Arch);
  99. map_pointer_t NewVersion(pkgCache::VerIterator &Ver, APT::StringView const &VerStr,
  100. map_pointer_t const ParentPkg, unsigned short const Hash,
  101. map_pointer_t const Next);
  102. map_pointer_t NewDescription(pkgCache::DescIterator &Desc,const std::string &Lang, APT::StringView md5sum,map_stringitem_t const idxmd5str);
  103. #endif
  104. bool NewFileVer(pkgCache::VerIterator &Ver,ListParser &List);
  105. bool NewFileDesc(pkgCache::DescIterator &Desc,ListParser &List);
  106. bool NewDepends(pkgCache::PkgIterator &Pkg, pkgCache::VerIterator &Ver,
  107. map_pointer_t const Version, uint8_t const Op,
  108. uint8_t const Type, map_pointer_t* &OldDepLast);
  109. bool NewProvides(pkgCache::VerIterator &Ver, pkgCache::PkgIterator &Pkg,
  110. map_stringitem_t const ProvidesVersion, uint8_t const Flags);
  111. bool NewTag(pkgCache::VerIterator &Ver,const char *NameStart,unsigned int NameSize);
  112. public:
  113. enum StringType { MIXED, PKGNAME, VERSIONNUMBER, SECTION, TAG };
  114. map_stringitem_t StoreString(StringType const type, const char * S, unsigned int const Size);
  115. #ifdef APT_PKG_EXPOSE_STRING_VIEW
  116. inline map_stringitem_t StoreString(enum StringType const type, APT::StringView S) {return StoreString(type, S.data(),S.length());};
  117. #endif
  118. void DropProgress() {Progress = 0;};
  119. bool SelectFile(const std::string &File,pkgIndexFile const &Index, std::string const &Architecture, std::string const &Component, unsigned long Flags = 0);
  120. bool SelectReleaseFile(const std::string &File, const std::string &Site, unsigned long Flags = 0);
  121. bool MergeList(ListParser &List,pkgCache::VerIterator *Ver = 0);
  122. inline pkgCache &GetCache() {return Cache;};
  123. inline pkgCache::PkgFileIterator GetCurFile()
  124. {return pkgCache::PkgFileIterator(Cache,CurrentFile);};
  125. inline pkgCache::RlsFileIterator GetCurRlsFile()
  126. {return pkgCache::RlsFileIterator(Cache,CurrentRlsFile);};
  127. APT_PUBLIC static bool MakeStatusCache(pkgSourceList &List,OpProgress *Progress,
  128. MMap **OutMap = 0,bool AllowMem = false);
  129. APT_HIDDEN static bool MakeStatusCache(pkgSourceList &List,OpProgress *Progress,
  130. MMap **OutMap,pkgCache **OutCache, bool AllowMem = false);
  131. APT_PUBLIC static bool MakeOnlyStatusCache(OpProgress *Progress,DynamicMMap **OutMap);
  132. void ReMap(void const * const oldMap, void const * const newMap, size_t oldSize);
  133. bool Start();
  134. pkgCacheGenerator(DynamicMMap *Map,OpProgress *Progress);
  135. virtual ~pkgCacheGenerator();
  136. private:
  137. void * const d;
  138. APT_HIDDEN bool MergeListGroup(ListParser &List, std::string const &GrpName);
  139. APT_HIDDEN bool MergeListPackage(ListParser &List, pkgCache::PkgIterator &Pkg);
  140. #ifdef APT_PKG_EXPOSE_STRING_VIEW
  141. APT_HIDDEN bool MergeListVersion(ListParser &List, pkgCache::PkgIterator &Pkg,
  142. APT::StringView const &Version, pkgCache::VerIterator* &OutVer);
  143. #endif
  144. APT_HIDDEN bool AddImplicitDepends(pkgCache::GrpIterator &G, pkgCache::PkgIterator &P,
  145. pkgCache::VerIterator &V);
  146. APT_HIDDEN bool AddImplicitDepends(pkgCache::VerIterator &V, pkgCache::PkgIterator &D);
  147. #ifdef APT_PKG_EXPOSE_STRING_VIEW
  148. APT_HIDDEN bool AddNewDescription(ListParser &List, pkgCache::VerIterator &Ver,
  149. std::string const &lang, APT::StringView CurMd5, map_stringitem_t &md5idx);
  150. #endif
  151. };
  152. /*}}}*/
  153. // This is the abstract package list parser class. /*{{{*/
  154. class APT_HIDDEN pkgCacheListParser
  155. {
  156. pkgCacheGenerator *Owner;
  157. friend class pkgCacheGenerator;
  158. // Some cache items
  159. pkgCache::VerIterator OldDepVer;
  160. map_pointer_t *OldDepLast;
  161. void * const d;
  162. protected:
  163. inline map_stringitem_t StoreString(pkgCacheGenerator::StringType const type, const char *S,unsigned int Size) {return Owner->StoreString(type, S, Size);};
  164. #ifdef APT_PKG_EXPOSE_STRING_VIEW
  165. inline map_stringitem_t StoreString(pkgCacheGenerator::StringType const type, APT::StringView S) {return Owner->StoreString(type, S);};
  166. inline map_stringitem_t WriteString(APT::StringView S) {return Owner->WriteStringInMap(S.data(), S.size());};
  167. #endif
  168. inline map_stringitem_t WriteString(const char *S,unsigned int Size) {return Owner->WriteStringInMap(S,Size);};
  169. #ifdef APT_PKG_EXPOSE_STRING_VIEW
  170. bool NewDepends(pkgCache::VerIterator &Ver,APT::StringView Package, APT::StringView Arch,
  171. APT::StringView Version,uint8_t const Op,
  172. uint8_t const Type);
  173. bool NewProvides(pkgCache::VerIterator &Ver,APT::StringView PkgName,
  174. APT::StringView PkgArch, APT::StringView Version,
  175. uint8_t const Flags);
  176. bool NewProvidesAllArch(pkgCache::VerIterator &Ver, APT::StringView Package,
  177. APT::StringView Version, uint8_t const Flags);
  178. bool NewTag(pkgCache::VerIterator &Ver,const char *NameStart,unsigned int NameSize);
  179. #endif
  180. public:
  181. // These all operate against the current section
  182. virtual std::string Package() = 0;
  183. virtual bool ArchitectureAll() = 0;
  184. #ifdef APT_PKG_EXPOSE_STRING_VIEW
  185. virtual APT::StringView Architecture() = 0;
  186. virtual APT::StringView Version() = 0;
  187. #endif
  188. virtual bool NewVersion(pkgCache::VerIterator &Ver) = 0;
  189. virtual std::vector<std::string> AvailableDescriptionLanguages() = 0;
  190. #ifdef APT_PKG_EXPOSE_STRING_VIEW
  191. virtual APT::StringView Description_md5() = 0;
  192. #endif
  193. virtual unsigned short VersionHash() = 0;
  194. /** compare currently parsed version with given version
  195. *
  196. * \param Hash of the currently parsed version
  197. * \param Ver to compare with
  198. */
  199. virtual bool SameVersion(unsigned short const Hash, pkgCache::VerIterator const &Ver);
  200. virtual bool UsePackage(pkgCache::PkgIterator &Pkg,
  201. pkgCache::VerIterator &Ver) = 0;
  202. virtual map_filesize_t Offset() = 0;
  203. virtual map_filesize_t Size() = 0;
  204. virtual bool Step() = 0;
  205. virtual bool CollectFileProvides(pkgCache &/*Cache*/,
  206. pkgCache::VerIterator &/*Ver*/) {return true;};
  207. pkgCacheListParser();
  208. virtual ~pkgCacheListParser();
  209. };
  210. /*}}}*/
  211. APT_DEPRECATED_MSG("Use pkgCacheGenerator::MakeStatusCache instead") bool pkgMakeStatusCache(pkgSourceList &List,OpProgress &Progress,
  212. MMap **OutMap = 0,bool AllowMem = false);
  213. APT_DEPRECATED_MSG("Use pkgCacheGenerator::MakeOnlyStatusCache instead") bool pkgMakeOnlyStatusCache(OpProgress &Progress,DynamicMMap **OutMap);
  214. #endif