]> git.saurik.com Git - apt.git/blame - apt-pkg/pkgcachegen.h
Tags should use StoreString, not WriteStringInMap.
[apt.git] / apt-pkg / pkgcachegen.h
CommitLineData
578bfd0a
AL
1// -*- mode: cpp; mode: fold -*-
2// Description /*{{{*/
578bfd0a
AL
3/* ######################################################################
4
5 Package Cache Generator - Generator for the cache structure.
6
7 This builds the cache structure from the abstract package list parser.
6fc33863
AL
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
578bfd0a
AL
15
16 ##################################################################### */
17 /*}}}*/
578bfd0a
AL
18#ifndef PKGLIB_PKGCACHEGEN_H
19#define PKGLIB_PKGCACHEGEN_H
20
a52f938b 21#include <apt-pkg/md5.h>
453b82a3
DK
22#include <apt-pkg/mmap.h>
23#include <apt-pkg/pkgcache.h>
24#include <apt-pkg/cacheiterators.h>
5a8e963b 25#include <apt-pkg/macros.h>
b35d2f5f 26
7635093c 27#include <vector>
453b82a3 28#include <string>
2837a718 29#if __cplusplus >= 201103L
869700d8 30#include <unordered_set>
2837a718 31#endif
eff0c22e
JAK
32#ifdef APT_PKG_EXPOSE_STRING_VIEW
33#include <apt-pkg/string_view.h>
34#endif
a9fe5928 35
453b82a3 36class FileFd;
b35d2f5f
AL
37class pkgSourceList;
38class OpProgress;
b2e465d6 39class pkgIndexFile;
c9443c01 40class pkgCacheListParser;
578bfd0a 41
dce45dbe 42class APT_HIDDEN pkgCacheGenerator /*{{{*/
578bfd0a 43{
49521f87
JAK
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
4ad8619b
DK
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);
7e58ab0c 50
2837a718
JAK
51 // Dirty hack for public users that do not use C++11 yet
52#if __cplusplus >= 201103L
869700d8
JAK
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)
a0e3c0f5 72 Hash = 33 * Hash + *I;
869700d8
JAK
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;
35d8bb14 81 std::unordered_set<string_pointer, hash> strTags;
2837a718 82#endif
78a5476f 83
c9443c01
DK
84 friend class pkgCacheListParser;
85 typedef pkgCacheListParser ListParser;
86
578bfd0a 87 public:
a9fe5928
DK
88
89 template<typename Iter> class Dynamic {
a9fe5928 90 public:
7635093c 91 static std::vector<Iter*> toReMap;
e8afd168 92 explicit Dynamic(Iter &I) {
7635093c 93 toReMap.push_back(&I);
a9fe5928
DK
94 }
95
96 ~Dynamic() {
7635093c 97 toReMap.pop_back();
a9fe5928 98 }
96f66f82
JAK
99
100#if __cplusplus >= 201103L
101 Dynamic(const Dynamic&) = delete;
102 void operator=(const Dynamic&) = delete;
103#endif
a9fe5928
DK
104 };
105
578bfd0a 106 protected:
a9fe5928 107
578bfd0a
AL
108 DynamicMMap &Map;
109 pkgCache Cache;
ddc1d8d0 110 OpProgress *Progress;
b07aeb1a
DK
111
112 std::string RlsFileName;
113 pkgCache::ReleaseFile *CurrentRlsFile;
8f3ba4e8 114 std::string PkgFileName;
578bfd0a 115 pkgCache::PackageFile *CurrentFile;
45415543 116
eff0c22e
JAK
117#ifdef APT_PKG_EXPOSE_STRING_VIEW
118 bool NewGroup(pkgCache::GrpIterator &Grp, APT::StringView Name);
119 bool NewPackage(pkgCache::PkgIterator &Pkg, APT::StringView Name, APT::StringView Arch);
8efd5947
DK
120 map_pointer_t NewVersion(pkgCache::VerIterator &Ver, APT::StringView const &VerStr,
121 map_pointer_t const ParentPkg, unsigned short const Hash,
122 map_pointer_t const Next);
49521f87 123 map_pointer_t NewDescription(pkgCache::DescIterator &Desc,const std::string &Lang, APT::StringView md5sum,map_stringitem_t const idxmd5str);
eff0c22e 124#endif
f55a958f 125 bool NewFileVer(pkgCache::VerIterator &Ver,ListParser &List);
a52f938b 126 bool NewFileDesc(pkgCache::DescIterator &Desc,ListParser &List);
9c44383f 127 bool NewDepends(pkgCache::PkgIterator &Pkg, pkgCache::VerIterator &Ver,
8c7af4d4
DK
128 map_pointer_t const Version, uint8_t const Op,
129 uint8_t const Type, map_pointer_t* &OldDepLast);
ecc138f8
DK
130 bool NewProvides(pkgCache::VerIterator &Ver, pkgCache::PkgIterator &Pkg,
131 map_stringitem_t const ProvidesVersion, uint8_t const Flags);
a473295d 132 bool NewTag(pkgCache::VerIterator &Ver,const char *NameStart,unsigned int NameSize);
578bfd0a 133
b2e465d6
AL
134 public:
135
35d8bb14 136 enum StringType { MIXED, PKGNAME, VERSIONNUMBER, SECTION, TAG };
30b45652 137 map_stringitem_t StoreString(StringType const type, const char * S, unsigned int const Size);
dd592790
JAK
138
139#ifdef APT_PKG_EXPOSE_STRING_VIEW
140 inline map_stringitem_t StoreString(enum StringType const type, APT::StringView S) {return StoreString(type, S.data(),S.length());};
141#endif
813c8eea 142
ddc1d8d0 143 void DropProgress() {Progress = 0;};
e185d8b3 144 bool SelectFile(const std::string &File,pkgIndexFile const &Index, std::string const &Architecture, std::string const &Component, unsigned long Flags = 0);
b07aeb1a 145 bool SelectReleaseFile(const std::string &File, const std::string &Site, unsigned long Flags = 0);
ddc1d8d0 146 bool MergeList(ListParser &List,pkgCache::VerIterator *Ver = 0);
ad00ae81 147 inline pkgCache &GetCache() {return Cache;};
1d3eea5c 148 inline pkgCache::PkgFileIterator GetCurFile()
b0b4efb9 149 {return pkgCache::PkgFileIterator(Cache,CurrentFile);};
1d3eea5c 150 inline pkgCache::RlsFileIterator GetCurRlsFile()
b07aeb1a 151 {return pkgCache::RlsFileIterator(Cache,CurrentRlsFile);};
45415543 152
dce45dbe 153 APT_PUBLIC static bool MakeStatusCache(pkgSourceList &List,OpProgress *Progress,
2e5f4e45 154 MMap **OutMap = 0,bool AllowMem = false);
f1616039
JAK
155 APT_HIDDEN static bool MakeStatusCache(pkgSourceList &List,OpProgress *Progress,
156 MMap **OutMap,pkgCache **OutCache, bool AllowMem = false);
dce45dbe 157 APT_PUBLIC static bool MakeOnlyStatusCache(OpProgress *Progress,DynamicMMap **OutMap);
25396fb0 158
19819ac5 159 void ReMap(void const * const oldMap, void const * const newMap, size_t oldSize);
a133f79c 160 bool Start();
a9fe5928 161
b2e465d6 162 pkgCacheGenerator(DynamicMMap *Map,OpProgress *Progress);
c8a4ce6c 163 virtual ~pkgCacheGenerator();
99a2ea5a
DK
164
165 private:
6c55f07a 166 void * const d;
ce62f1de
DK
167 APT_HIDDEN bool MergeListGroup(ListParser &List, std::string const &GrpName);
168 APT_HIDDEN bool MergeListPackage(ListParser &List, pkgCache::PkgIterator &Pkg);
8efd5947 169#ifdef APT_PKG_EXPOSE_STRING_VIEW
ce62f1de 170 APT_HIDDEN bool MergeListVersion(ListParser &List, pkgCache::PkgIterator &Pkg,
8efd5947
DK
171 APT::StringView const &Version, pkgCache::VerIterator* &OutVer);
172#endif
5a8e963b 173
ce62f1de 174 APT_HIDDEN bool AddImplicitDepends(pkgCache::GrpIterator &G, pkgCache::PkgIterator &P,
5a8e963b 175 pkgCache::VerIterator &V);
ce62f1de 176 APT_HIDDEN bool AddImplicitDepends(pkgCache::VerIterator &V, pkgCache::PkgIterator &D);
ffe3c68e 177
49521f87 178#ifdef APT_PKG_EXPOSE_STRING_VIEW
ffe3c68e 179 APT_HIDDEN bool AddNewDescription(ListParser &List, pkgCache::VerIterator &Ver,
49521f87
JAK
180 std::string const &lang, APT::StringView CurMd5, map_stringitem_t &md5idx);
181#endif
578bfd0a 182};
92fcbfc1
DK
183 /*}}}*/
184// This is the abstract package list parser class. /*{{{*/
c9443c01 185class APT_HIDDEN pkgCacheListParser
f55ece0e
AL
186{
187 pkgCacheGenerator *Owner;
b2e465d6 188 friend class pkgCacheGenerator;
c9443c01 189
f9eec0e7
AL
190 // Some cache items
191 pkgCache::VerIterator OldDepVer;
4ad8619b 192 map_pointer_t *OldDepLast;
45415543 193
6c55f07a 194 void * const d;
c8a4ce6c 195
f55ece0e 196 protected:
813c8eea 197
78a5476f 198 inline map_stringitem_t StoreString(pkgCacheGenerator::StringType const type, const char *S,unsigned int Size) {return Owner->StoreString(type, S, Size);};
dd592790
JAK
199#ifdef APT_PKG_EXPOSE_STRING_VIEW
200 inline map_stringitem_t StoreString(pkgCacheGenerator::StringType const type, APT::StringView S) {return Owner->StoreString(type, S);};
49521f87 201 inline map_stringitem_t WriteString(APT::StringView S) {return Owner->WriteStringInMap(S.data(), S.size());};
dd592790 202#endif
78a5476f 203
4ad8619b 204 inline map_stringitem_t WriteString(const char *S,unsigned int Size) {return Owner->WriteStringInMap(S,Size);};
eff0c22e
JAK
205#ifdef APT_PKG_EXPOSE_STRING_VIEW
206 bool NewDepends(pkgCache::VerIterator &Ver,APT::StringView Package, APT::StringView Arch,
207 APT::StringView Version,uint8_t const Op,
8c7af4d4 208 uint8_t const Type);
eff0c22e
JAK
209 bool NewProvides(pkgCache::VerIterator &Ver,APT::StringView PkgName,
210 APT::StringView PkgArch, APT::StringView Version,
8c7af4d4 211 uint8_t const Flags);
eff0c22e
JAK
212 bool NewProvidesAllArch(pkgCache::VerIterator &Ver, APT::StringView Package,
213 APT::StringView Version, uint8_t const Flags);
a473295d 214 bool NewTag(pkgCache::VerIterator &Ver,const char *NameStart,unsigned int NameSize);
eff0c22e 215#endif
f55ece0e
AL
216 public:
217
218 // These all operate against the current section
8f3ba4e8 219 virtual std::string Package() = 0;
857e9c13 220 virtual bool ArchitectureAll() = 0;
8efd5947
DK
221#ifdef APT_PKG_EXPOSE_STRING_VIEW
222 virtual APT::StringView Architecture() = 0;
223 virtual APT::StringView Version() = 0;
224#endif
32b9a14c 225 virtual bool NewVersion(pkgCache::VerIterator &Ver) = 0;
ffe3c68e 226 virtual std::vector<std::string> AvailableDescriptionLanguages() = 0;
49521f87
JAK
227#ifdef APT_PKG_EXPOSE_STRING_VIEW
228 virtual APT::StringView Description_md5() = 0;
229#endif
204fbdcc 230 virtual unsigned short VersionHash() = 0;
68134bda
DK
231 /** compare currently parsed version with given version
232 *
233 * \param Hash of the currently parsed version
234 * \param Ver to compare with
235 */
c8a4ce6c 236 virtual bool SameVersion(unsigned short const Hash, pkgCache::VerIterator const &Ver);
32b9a14c
DK
237 virtual bool UsePackage(pkgCache::PkgIterator &Pkg,
238 pkgCache::VerIterator &Ver) = 0;
4ad8619b
DK
239 virtual map_filesize_t Offset() = 0;
240 virtual map_filesize_t Size() = 0;
f55ece0e
AL
241
242 virtual bool Step() = 0;
243
65512241
DK
244 virtual bool CollectFileProvides(pkgCache &/*Cache*/,
245 pkgCache::VerIterator &/*Ver*/) {return true;};
45415543 246
c9443c01
DK
247 pkgCacheListParser();
248 virtual ~pkgCacheListParser();
f55ece0e 249};
92fcbfc1 250 /*}}}*/
2e5f4e45 251
5dd00edb 252APT_DEPRECATED_MSG("Use pkgCacheGenerator::MakeStatusCache instead") bool pkgMakeStatusCache(pkgSourceList &List,OpProgress &Progress,
b2e465d6 253 MMap **OutMap = 0,bool AllowMem = false);
5dd00edb 254APT_DEPRECATED_MSG("Use pkgCacheGenerator::MakeOnlyStatusCache instead") bool pkgMakeOnlyStatusCache(OpProgress &Progress,DynamicMMap **OutMap);
b2e465d6 255
578bfd0a 256#endif