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