pkgcachegen.h 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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_map>
  25. #endif
  26. class FileFd;
  27. class pkgSourceList;
  28. class OpProgress;
  29. class pkgIndexFile;
  30. class pkgCacheListParser;
  31. class APT_HIDDEN pkgCacheGenerator /*{{{*/
  32. {
  33. APT_HIDDEN map_stringitem_t WriteStringInMap(std::string const &String) { return WriteStringInMap(String.c_str()); };
  34. APT_HIDDEN map_stringitem_t WriteStringInMap(const char *String);
  35. APT_HIDDEN map_stringitem_t WriteStringInMap(const char *String, const unsigned long &Len);
  36. APT_HIDDEN map_pointer_t AllocateInMap(const unsigned long &size);
  37. // Dirty hack for public users that do not use C++11 yet
  38. #if __cplusplus >= 201103L
  39. std::unordered_map<std::string,map_stringitem_t> strMixed;
  40. std::unordered_map<std::string,map_stringitem_t> strSections;
  41. std::unordered_map<std::string,map_stringitem_t> strPkgNames;
  42. std::unordered_map<std::string,map_stringitem_t> strVersions;
  43. #endif
  44. friend class pkgCacheListParser;
  45. typedef pkgCacheListParser ListParser;
  46. public:
  47. template<typename Iter> class Dynamic {
  48. public:
  49. static std::vector<Iter*> toReMap;
  50. explicit Dynamic(Iter &I) {
  51. toReMap.push_back(&I);
  52. }
  53. ~Dynamic() {
  54. toReMap.pop_back();
  55. }
  56. };
  57. protected:
  58. DynamicMMap &Map;
  59. pkgCache Cache;
  60. OpProgress *Progress;
  61. std::string RlsFileName;
  62. pkgCache::ReleaseFile *CurrentRlsFile;
  63. std::string PkgFileName;
  64. pkgCache::PackageFile *CurrentFile;
  65. bool NewGroup(pkgCache::GrpIterator &Grp,const std::string &Name);
  66. bool NewPackage(pkgCache::PkgIterator &Pkg,const std::string &Name, const std::string &Arch);
  67. bool NewFileVer(pkgCache::VerIterator &Ver,ListParser &List);
  68. bool NewFileDesc(pkgCache::DescIterator &Desc,ListParser &List);
  69. bool NewDepends(pkgCache::PkgIterator &Pkg, pkgCache::VerIterator &Ver,
  70. map_pointer_t const Version, uint8_t const Op,
  71. uint8_t const Type, map_pointer_t* &OldDepLast);
  72. map_pointer_t NewVersion(pkgCache::VerIterator &Ver,const std::string &VerStr,map_pointer_t const Next) APT_DEPRECATED
  73. { return NewVersion(Ver, VerStr, 0, 0, Next); }
  74. map_pointer_t NewVersion(pkgCache::VerIterator &Ver,const std::string &VerStr,
  75. map_pointer_t const ParentPkg, unsigned short const Hash,
  76. map_pointer_t const Next);
  77. map_pointer_t NewDescription(pkgCache::DescIterator &Desc,const std::string &Lang,const MD5SumValue &md5sum,map_stringitem_t const idxmd5str);
  78. bool NewProvides(pkgCache::VerIterator &Ver, pkgCache::PkgIterator &Pkg,
  79. map_stringitem_t const ProvidesVersion, uint8_t const Flags);
  80. public:
  81. enum StringType { MIXED, PKGNAME, VERSIONNUMBER, SECTION };
  82. map_stringitem_t StoreString(StringType const type, const char * S, unsigned int const Size);
  83. inline map_stringitem_t StoreString(enum StringType const type, const std::string &S) {return StoreString(type, S.c_str(),S.length());};
  84. void DropProgress() {Progress = 0;};
  85. bool SelectFile(const std::string &File,pkgIndexFile const &Index, std::string const &Architecture, std::string const &Component, unsigned long Flags = 0);
  86. bool SelectReleaseFile(const std::string &File, const std::string &Site, unsigned long Flags = 0);
  87. bool MergeList(ListParser &List,pkgCache::VerIterator *Ver = 0);
  88. inline pkgCache &GetCache() {return Cache;};
  89. inline pkgCache::PkgFileIterator GetCurFile()
  90. {return pkgCache::PkgFileIterator(Cache,CurrentFile);};
  91. inline pkgCache::RlsFileIterator GetCurRlsFile()
  92. {return pkgCache::RlsFileIterator(Cache,CurrentRlsFile);};
  93. APT_PUBLIC static bool MakeStatusCache(pkgSourceList &List,OpProgress *Progress,
  94. MMap **OutMap = 0,bool AllowMem = false);
  95. APT_PUBLIC static bool MakeOnlyStatusCache(OpProgress *Progress,DynamicMMap **OutMap);
  96. void ReMap(void const * const oldMap, void const * const newMap);
  97. pkgCacheGenerator(DynamicMMap *Map,OpProgress *Progress);
  98. virtual ~pkgCacheGenerator();
  99. private:
  100. void * const d;
  101. APT_HIDDEN bool MergeListGroup(ListParser &List, std::string const &GrpName);
  102. APT_HIDDEN bool MergeListPackage(ListParser &List, pkgCache::PkgIterator &Pkg);
  103. APT_HIDDEN bool MergeListVersion(ListParser &List, pkgCache::PkgIterator &Pkg,
  104. std::string const &Version, pkgCache::VerIterator* &OutVer);
  105. APT_HIDDEN bool AddImplicitDepends(pkgCache::GrpIterator &G, pkgCache::PkgIterator &P,
  106. pkgCache::VerIterator &V);
  107. APT_HIDDEN bool AddImplicitDepends(pkgCache::VerIterator &V, pkgCache::PkgIterator &D);
  108. APT_HIDDEN bool AddNewDescription(ListParser &List, pkgCache::VerIterator &Ver,
  109. std::string const &lang, MD5SumValue const &CurMd5, map_stringitem_t &md5idx);
  110. };
  111. /*}}}*/
  112. // This is the abstract package list parser class. /*{{{*/
  113. class APT_HIDDEN pkgCacheListParser
  114. {
  115. pkgCacheGenerator *Owner;
  116. friend class pkgCacheGenerator;
  117. // Some cache items
  118. pkgCache::VerIterator OldDepVer;
  119. map_pointer_t *OldDepLast;
  120. void * const d;
  121. protected:
  122. inline map_stringitem_t StoreString(pkgCacheGenerator::StringType const type, std::string const &S) {return Owner->StoreString(type, S);};
  123. inline map_stringitem_t StoreString(pkgCacheGenerator::StringType const type, const char *S,unsigned int Size) {return Owner->StoreString(type, S, Size);};
  124. inline map_stringitem_t WriteString(const std::string &S) {return Owner->WriteStringInMap(S);};
  125. inline map_stringitem_t WriteString(const char *S,unsigned int Size) {return Owner->WriteStringInMap(S,Size);};
  126. bool NewDepends(pkgCache::VerIterator &Ver,const std::string &Package, const std::string &Arch,
  127. const std::string &Version,uint8_t const Op,
  128. uint8_t const Type);
  129. bool NewProvides(pkgCache::VerIterator &Ver,const std::string &PkgName,
  130. const std::string &PkgArch, const std::string &Version,
  131. uint8_t const Flags);
  132. bool NewProvidesAllArch(pkgCache::VerIterator &Ver, std::string const &Package,
  133. std::string const &Version, uint8_t const Flags);
  134. public:
  135. // These all operate against the current section
  136. virtual std::string Package() = 0;
  137. virtual std::string Architecture() = 0;
  138. virtual bool ArchitectureAll() = 0;
  139. virtual std::string Version() = 0;
  140. virtual bool NewVersion(pkgCache::VerIterator &Ver) = 0;
  141. virtual std::string Description(std::string const &lang) = 0;
  142. virtual std::vector<std::string> AvailableDescriptionLanguages() = 0;
  143. virtual MD5SumValue Description_md5() = 0;
  144. virtual unsigned short VersionHash() = 0;
  145. /** compare currently parsed version with given version
  146. *
  147. * \param Hash of the currently parsed version
  148. * \param Ver to compare with
  149. */
  150. virtual bool SameVersion(unsigned short const Hash, pkgCache::VerIterator const &Ver);
  151. virtual bool UsePackage(pkgCache::PkgIterator &Pkg,
  152. pkgCache::VerIterator &Ver) = 0;
  153. virtual map_filesize_t Offset() = 0;
  154. virtual map_filesize_t Size() = 0;
  155. virtual bool Step() = 0;
  156. virtual bool CollectFileProvides(pkgCache &/*Cache*/,
  157. pkgCache::VerIterator &/*Ver*/) {return true;};
  158. pkgCacheListParser();
  159. virtual ~pkgCacheListParser();
  160. };
  161. /*}}}*/
  162. APT_DEPRECATED_MSG("Use pkgCacheGenerator::MakeStatusCache instead") bool pkgMakeStatusCache(pkgSourceList &List,OpProgress &Progress,
  163. MMap **OutMap = 0,bool AllowMem = false);
  164. APT_DEPRECATED_MSG("Use pkgCacheGenerator::MakeOnlyStatusCache instead") bool pkgMakeOnlyStatusCache(OpProgress &Progress,DynamicMMap **OutMap);
  165. #endif