1 // -*- mode: cpp; mode: fold -*-
3 /* ######################################################################
5 Package Cache Generator - Generator for the cache structure.
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.
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
16 ##################################################################### */
18 #ifndef PKGLIB_PKGCACHEGEN_H
19 #define PKGLIB_PKGCACHEGEN_H
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>
29 #if __cplusplus >= 201103L
30 #include <unordered_set>
32 #ifdef APT_PKG_EXPOSE_STRING_VIEW
33 #include <apt-pkg/string_view.h>
40 class pkgCacheListParser
;
42 class APT_HIDDEN pkgCacheGenerator
/*{{{*/
44 #ifdef APT_PKG_EXPOSE_STRING_VIEW
45 APT_HIDDEN map_stringitem_t
WriteStringInMap(APT::StringView String
) { return WriteStringInMap(String
.data(), String
.size()); };
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
);
51 // Dirty hack for public users that do not use C++11 yet
52 #if __cplusplus >= 201103L
53 struct string_pointer
{
56 pkgCacheGenerator
*generator
;
57 map_stringitem_t item
;
59 const char *data() const {
60 return data_
!= nullptr ? data_
: static_cast<char*>(generator
->Map
.Data()) + item
;
63 bool operator ==(string_pointer
const &other
) const {
64 return size
== other
.size
&& memcmp(data(), other
.data(), size
) == 0;
68 uint32_t operator()(string_pointer
const &that
) const {
70 const char * const end
= that
.data() + that
.size
;
71 for (const char *I
= that
.data(); I
!= end
; ++I
)
72 Hash
= 33 * Hash
+ *I
;
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 std::unordered_set
<string_pointer
, hash
> strTags
;
84 friend class pkgCacheListParser
;
85 typedef pkgCacheListParser ListParser
;
89 template<typename Iter
> class Dynamic
{
91 static std::vector
<Iter
*> toReMap
;
92 explicit Dynamic(Iter
&I
) {
93 toReMap
.push_back(&I
);
100 #if __cplusplus >= 201103L
101 Dynamic(const Dynamic
&) = delete;
102 void operator=(const Dynamic
&) = delete;
110 OpProgress
*Progress
;
112 std::string RlsFileName
;
113 pkgCache::ReleaseFile
*CurrentRlsFile
;
114 std::string PkgFileName
;
115 pkgCache::PackageFile
*CurrentFile
;
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
);
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
);
123 map_pointer_t
NewDescription(pkgCache::DescIterator
&Desc
,const std::string
&Lang
, APT::StringView md5sum
,map_stringitem_t
const idxmd5str
);
125 bool NewFileVer(pkgCache::VerIterator
&Ver
,ListParser
&List
);
126 bool NewFileDesc(pkgCache::DescIterator
&Desc
,ListParser
&List
);
127 bool NewDepends(pkgCache::PkgIterator
&Pkg
, pkgCache::VerIterator
&Ver
,
128 map_pointer_t
const Version
, uint8_t const Op
,
129 uint8_t const Type
, map_pointer_t
* &OldDepLast
);
130 bool NewProvides(pkgCache::VerIterator
&Ver
, pkgCache::PkgIterator
&Pkg
,
131 map_stringitem_t
const ProvidesVersion
, uint8_t const Flags
);
132 bool NewTag(pkgCache::VerIterator
&Ver
,const char *NameStart
,unsigned int NameSize
);
136 enum StringType
{ MIXED
, PKGNAME
, VERSIONNUMBER
, SECTION
, TAG
};
137 map_stringitem_t
StoreString(StringType
const type
, const char * S
, unsigned int const Size
);
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());};
143 void DropProgress() {Progress
= 0;};
144 bool SelectFile(const std::string
&File
,pkgIndexFile
const &Index
, std::string
const &Architecture
, std::string
const &Component
, unsigned long Flags
= 0);
145 bool SelectReleaseFile(const std::string
&File
, const std::string
&Site
, unsigned long Flags
= 0);
146 bool MergeList(ListParser
&List
,pkgCache::VerIterator
*Ver
= 0);
147 inline pkgCache
&GetCache() {return Cache
;};
148 inline pkgCache::PkgFileIterator
GetCurFile()
149 {return pkgCache::PkgFileIterator(Cache
,CurrentFile
);};
150 inline pkgCache::RlsFileIterator
GetCurRlsFile()
151 {return pkgCache::RlsFileIterator(Cache
,CurrentRlsFile
);};
153 APT_PUBLIC
static bool MakeStatusCache(pkgSourceList
&List
,OpProgress
*Progress
,
154 MMap
**OutMap
= 0,bool AllowMem
= false);
155 APT_HIDDEN
static bool MakeStatusCache(pkgSourceList
&List
,OpProgress
*Progress
,
156 MMap
**OutMap
,pkgCache
**OutCache
, bool AllowMem
= false);
157 APT_PUBLIC
static bool MakeOnlyStatusCache(OpProgress
*Progress
,DynamicMMap
**OutMap
);
159 void ReMap(void const * const oldMap
, void const * const newMap
, size_t oldSize
);
162 pkgCacheGenerator(DynamicMMap
*Map
,OpProgress
*Progress
);
163 virtual ~pkgCacheGenerator();
167 APT_HIDDEN
bool MergeListGroup(ListParser
&List
, std::string
const &GrpName
);
168 APT_HIDDEN
bool MergeListPackage(ListParser
&List
, pkgCache::PkgIterator
&Pkg
);
169 #ifdef APT_PKG_EXPOSE_STRING_VIEW
170 APT_HIDDEN
bool MergeListVersion(ListParser
&List
, pkgCache::PkgIterator
&Pkg
,
171 APT::StringView
const &Version
, pkgCache::VerIterator
* &OutVer
);
174 APT_HIDDEN
bool AddImplicitDepends(pkgCache::GrpIterator
&G
, pkgCache::PkgIterator
&P
,
175 pkgCache::VerIterator
&V
);
176 APT_HIDDEN
bool AddImplicitDepends(pkgCache::VerIterator
&V
, pkgCache::PkgIterator
&D
);
178 #ifdef APT_PKG_EXPOSE_STRING_VIEW
179 APT_HIDDEN
bool AddNewDescription(ListParser
&List
, pkgCache::VerIterator
&Ver
,
180 std::string
const &lang
, APT::StringView CurMd5
, map_stringitem_t
&md5idx
);
184 // This is the abstract package list parser class. /*{{{*/
185 class APT_HIDDEN pkgCacheListParser
187 pkgCacheGenerator
*Owner
;
188 friend class pkgCacheGenerator
;
191 pkgCache::VerIterator OldDepVer
;
192 map_pointer_t
*OldDepLast
;
198 inline map_stringitem_t
StoreString(pkgCacheGenerator::StringType
const type
, const char *S
,unsigned int Size
) {return Owner
->StoreString(type
, S
, Size
);};
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
);};
201 inline map_stringitem_t
WriteString(APT::StringView S
) {return Owner
->WriteStringInMap(S
.data(), S
.size());};
204 inline map_stringitem_t
WriteString(const char *S
,unsigned int Size
) {return Owner
->WriteStringInMap(S
,Size
);};
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
,
209 bool NewProvides(pkgCache::VerIterator
&Ver
,APT::StringView PkgName
,
210 APT::StringView PkgArch
, APT::StringView Version
,
211 uint8_t const Flags
);
212 bool NewProvidesAllArch(pkgCache::VerIterator
&Ver
, APT::StringView Package
,
213 APT::StringView Version
, uint8_t const Flags
);
214 bool NewTag(pkgCache::VerIterator
&Ver
,const char *NameStart
,unsigned int NameSize
);
218 // These all operate against the current section
219 virtual std::string
Package() = 0;
220 virtual bool ArchitectureAll() = 0;
221 #ifdef APT_PKG_EXPOSE_STRING_VIEW
222 virtual APT::StringView
Architecture() = 0;
223 virtual APT::StringView
Version() = 0;
225 virtual bool NewVersion(pkgCache::VerIterator
&Ver
) = 0;
226 virtual std::vector
<std::string
> AvailableDescriptionLanguages() = 0;
227 #ifdef APT_PKG_EXPOSE_STRING_VIEW
228 virtual APT::StringView
Description_md5() = 0;
230 virtual unsigned short VersionHash() = 0;
231 /** compare currently parsed version with given version
233 * \param Hash of the currently parsed version
234 * \param Ver to compare with
236 virtual bool SameVersion(unsigned short const Hash
, pkgCache::VerIterator
const &Ver
);
237 virtual bool UsePackage(pkgCache::PkgIterator
&Pkg
,
238 pkgCache::VerIterator
&Ver
) = 0;
239 virtual map_filesize_t
Offset() = 0;
240 virtual map_filesize_t
Size() = 0;
242 virtual bool Step() = 0;
244 virtual bool CollectFileProvides(pkgCache
&/*Cache*/,
245 pkgCache::VerIterator
&/*Ver*/) {return true;};
247 pkgCacheListParser();
248 virtual ~pkgCacheListParser();
252 APT_DEPRECATED_MSG("Use pkgCacheGenerator::MakeStatusCache instead") bool pkgMakeStatusCache(pkgSourceList
&List
,OpProgress
&Progress
,
253 MMap
**OutMap
= 0,bool AllowMem
= false);
254 APT_DEPRECATED_MSG("Use pkgCacheGenerator::MakeOnlyStatusCache instead") bool pkgMakeOnlyStatusCache(OpProgress
&Progress
,DynamicMMap
**OutMap
);