]> git.saurik.com Git - apt.git/blame_incremental - apt-pkg/pkgcachegen.h
Bug #807012 also involves package dependencies :/.
[apt.git] / apt-pkg / pkgcachegen.h
... / ...
CommitLineData
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
36class FileFd;
37class pkgSourceList;
38class OpProgress;
39class pkgIndexFile;
40class pkgCacheListParser;
41
42class APT_HIDDEN pkgCacheGenerator /*{{{*/
43{
44#ifdef APT_PKG_EXPOSE_STRING_VIEW
45 APT_HIDDEN map_stringitem_t WriteStringInMap(APT::StringView String) { return WriteStringInMap(String.data(), String.size()); };
46#endif
47 APT_HIDDEN map_stringitem_t WriteStringInMap(const char *String);
48 APT_HIDDEN map_stringitem_t WriteStringInMap(const char *String, const unsigned long &Len);
49 APT_HIDDEN map_pointer_t AllocateInMap(const unsigned long &size);
50
51 // Dirty hack for public users that do not use C++11 yet
52#if __cplusplus >= 201103L
53 struct string_pointer {
54 const char *data_;
55 size_t size;
56 pkgCacheGenerator *generator;
57 map_stringitem_t item;
58
59 const char *data() const {
60 return data_ != nullptr ? data_ : static_cast<char*>(generator->Map.Data()) + item;
61 }
62
63 bool operator ==(string_pointer const &other) const {
64 return size == other.size && memcmp(data(), other.data(), size) == 0;
65 }
66 };
67 struct hash {
68 uint32_t operator()(string_pointer const &that) const {
69 uint32_t Hash = 5381;
70 const char * const end = that.data() + that.size;
71 for (const char *I = that.data(); I != end; ++I)
72 Hash = 33 * Hash + *I;
73 return Hash;
74 }
75 };
76
77 std::unordered_set<string_pointer, hash> strMixed;
78 std::unordered_set<string_pointer, hash> strPkgNames;
79 std::unordered_set<string_pointer, hash> strVersions;
80 std::unordered_set<string_pointer, hash> strSections;
81#endif
82
83 friend class pkgCacheListParser;
84 typedef pkgCacheListParser ListParser;
85
86 public:
87
88 template<typename Iter> class Dynamic {
89 public:
90 static std::vector<Iter*> toReMap;
91 explicit Dynamic(Iter &I) {
92 toReMap.push_back(&I);
93 }
94
95 ~Dynamic() {
96 toReMap.pop_back();
97 }
98
99#if __cplusplus >= 201103L
100 Dynamic(const Dynamic&) = delete;
101 void operator=(const Dynamic&) = delete;
102#endif
103 };
104
105 protected:
106
107 DynamicMMap &Map;
108 pkgCache Cache;
109 OpProgress *Progress;
110
111 std::string RlsFileName;
112 pkgCache::ReleaseFile *CurrentRlsFile;
113 std::string PkgFileName;
114 pkgCache::PackageFile *CurrentFile;
115
116#ifdef APT_PKG_EXPOSE_STRING_VIEW
117 bool NewGroup(pkgCache::GrpIterator &Grp, APT::StringView Name);
118 bool NewPackage(pkgCache::PkgIterator &Pkg, APT::StringView Name, APT::StringView Arch);
119 map_pointer_t NewVersion(pkgCache::VerIterator &Ver, APT::StringView const &VerStr,
120 map_pointer_t const ParentPkg, unsigned short const Hash,
121 map_pointer_t const Next);
122 map_pointer_t NewDescription(pkgCache::DescIterator &Desc,const std::string &Lang, APT::StringView md5sum,map_stringitem_t const idxmd5str);
123#endif
124 bool NewFileVer(pkgCache::VerIterator &Ver,ListParser &List);
125 bool NewFileDesc(pkgCache::DescIterator &Desc,ListParser &List);
126 bool NewDepends(pkgCache::PkgIterator &Pkg, pkgCache::VerIterator &Ver,
127 map_pointer_t const Version, uint8_t const Op,
128 uint8_t const Type, map_pointer_t* &OldDepLast);
129 bool NewProvides(pkgCache::VerIterator &Ver, pkgCache::PkgIterator &Pkg,
130 map_stringitem_t const ProvidesVersion, uint8_t const Flags);
131 bool NewTag(pkgCache::VerIterator &Ver,const char *NameStart,unsigned int NameSize);
132
133 public:
134
135 enum StringType { MIXED, PKGNAME, VERSIONNUMBER, SECTION };
136 map_stringitem_t StoreString(StringType const type, const char * S, unsigned int const Size);
137
138#ifdef APT_PKG_EXPOSE_STRING_VIEW
139 inline map_stringitem_t StoreString(enum StringType const type, APT::StringView S) {return StoreString(type, S.data(),S.length());};
140#endif
141
142 void DropProgress() {Progress = 0;};
143 bool SelectFile(const std::string &File,pkgIndexFile const &Index, std::string const &Architecture, std::string const &Component, unsigned long Flags = 0);
144 bool SelectReleaseFile(const std::string &File, const std::string &Site, unsigned long Flags = 0);
145 bool MergeList(ListParser &List,pkgCache::VerIterator *Ver = 0);
146 inline pkgCache &GetCache() {return Cache;};
147 inline pkgCache::PkgFileIterator GetCurFile()
148 {return pkgCache::PkgFileIterator(Cache,CurrentFile);};
149 inline pkgCache::RlsFileIterator GetCurRlsFile()
150 {return pkgCache::RlsFileIterator(Cache,CurrentRlsFile);};
151
152 APT_PUBLIC static bool MakeStatusCache(pkgSourceList &List,OpProgress *Progress,
153 MMap **OutMap = 0,bool AllowMem = false);
154 APT_HIDDEN static bool MakeStatusCache(pkgSourceList &List,OpProgress *Progress,
155 MMap **OutMap,pkgCache **OutCache, bool AllowMem = false);
156 APT_PUBLIC static bool MakeOnlyStatusCache(OpProgress *Progress,DynamicMMap **OutMap);
157
158 void ReMap(void const * const oldMap, void const * const newMap, size_t oldSize);
159 bool Start();
160
161 pkgCacheGenerator(DynamicMMap *Map,OpProgress *Progress);
162 virtual ~pkgCacheGenerator();
163
164 private:
165 void * const d;
166 APT_HIDDEN bool MergeListGroup(ListParser &List, std::string const &GrpName);
167 APT_HIDDEN bool MergeListPackage(ListParser &List, pkgCache::PkgIterator &Pkg);
168#ifdef APT_PKG_EXPOSE_STRING_VIEW
169 APT_HIDDEN bool MergeListVersion(ListParser &List, pkgCache::PkgIterator &Pkg,
170 APT::StringView const &Version, pkgCache::VerIterator* &OutVer);
171#endif
172
173 APT_HIDDEN bool AddImplicitDepends(pkgCache::GrpIterator &G, pkgCache::PkgIterator &P,
174 pkgCache::VerIterator &V);
175 APT_HIDDEN bool AddImplicitDepends(pkgCache::VerIterator &V, pkgCache::PkgIterator &D);
176
177#ifdef APT_PKG_EXPOSE_STRING_VIEW
178 APT_HIDDEN bool AddNewDescription(ListParser &List, pkgCache::VerIterator &Ver,
179 std::string const &lang, APT::StringView CurMd5, map_stringitem_t &md5idx);
180#endif
181};
182 /*}}}*/
183// This is the abstract package list parser class. /*{{{*/
184class APT_HIDDEN pkgCacheListParser
185{
186 pkgCacheGenerator *Owner;
187 friend class pkgCacheGenerator;
188
189 // Some cache items
190 pkgCache::VerIterator OldDepVer;
191 map_pointer_t *OldDepLast;
192
193 void * const d;
194
195 protected:
196
197 inline map_stringitem_t StoreString(pkgCacheGenerator::StringType const type, const char *S,unsigned int Size) {return Owner->StoreString(type, S, Size);};
198#ifdef APT_PKG_EXPOSE_STRING_VIEW
199 inline map_stringitem_t StoreString(pkgCacheGenerator::StringType const type, APT::StringView S) {return Owner->StoreString(type, S);};
200 inline map_stringitem_t WriteString(APT::StringView S) {return Owner->WriteStringInMap(S.data(), S.size());};
201#endif
202
203 inline map_stringitem_t WriteString(const char *S,unsigned int Size) {return Owner->WriteStringInMap(S,Size);};
204#ifdef APT_PKG_EXPOSE_STRING_VIEW
205 bool NewDepends(pkgCache::VerIterator &Ver,APT::StringView Package, APT::StringView Arch,
206 APT::StringView Version,uint8_t const Op,
207 uint8_t const Type);
208 bool NewProvides(pkgCache::VerIterator &Ver,APT::StringView PkgName,
209 APT::StringView PkgArch, APT::StringView Version,
210 uint8_t const Flags);
211 bool NewProvidesAllArch(pkgCache::VerIterator &Ver, APT::StringView Package,
212 APT::StringView Version, uint8_t const Flags);
213 bool NewTag(pkgCache::VerIterator &Ver,const char *NameStart,unsigned int NameSize);
214#endif
215 public:
216
217 // These all operate against the current section
218 virtual std::string Package() = 0;
219 virtual bool ArchitectureAll() = 0;
220#ifdef APT_PKG_EXPOSE_STRING_VIEW
221 virtual APT::StringView Architecture() = 0;
222 virtual APT::StringView Version() = 0;
223#endif
224 virtual bool NewVersion(pkgCache::VerIterator &Ver) = 0;
225 virtual std::vector<std::string> AvailableDescriptionLanguages() = 0;
226#ifdef APT_PKG_EXPOSE_STRING_VIEW
227 virtual APT::StringView Description_md5() = 0;
228#endif
229 virtual unsigned short VersionHash() = 0;
230 /** compare currently parsed version with given version
231 *
232 * \param Hash of the currently parsed version
233 * \param Ver to compare with
234 */
235 virtual bool SameVersion(unsigned short const Hash, pkgCache::VerIterator const &Ver);
236 virtual bool UsePackage(pkgCache::PkgIterator &Pkg,
237 pkgCache::VerIterator &Ver) = 0;
238 virtual map_filesize_t Offset() = 0;
239 virtual map_filesize_t Size() = 0;
240
241 virtual bool Step() = 0;
242
243 virtual bool CollectFileProvides(pkgCache &/*Cache*/,
244 pkgCache::VerIterator &/*Ver*/) {return true;};
245
246 pkgCacheListParser();
247 virtual ~pkgCacheListParser();
248};
249 /*}}}*/
250
251APT_DEPRECATED_MSG("Use pkgCacheGenerator::MakeStatusCache instead") bool pkgMakeStatusCache(pkgSourceList &List,OpProgress &Progress,
252 MMap **OutMap = 0,bool AllowMem = false);
253APT_DEPRECATED_MSG("Use pkgCacheGenerator::MakeOnlyStatusCache instead") bool pkgMakeOnlyStatusCache(OpProgress &Progress,DynamicMMap **OutMap);
254
255#endif