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 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
);
49 // Dirty hack for public users that do not use C++11 yet
50 #if __cplusplus >= 201103L
51 struct string_pointer
{
54 pkgCacheGenerator
*generator
;
55 map_stringitem_t item
;
57 const char *data() const {
58 return data_
!= nullptr ? data_
: static_cast<char*>(generator
->Map
.Data()) + item
;
61 bool operator ==(string_pointer
const &other
) const {
62 return size
== other
.size
&& memcmp(data(), other
.data(), size
) == 0;
66 uint32_t operator()(string_pointer
const &that
) const {
68 const char * const end
= that
.data() + that
.size
;
69 for (const char *I
= that
.data(); I
!= end
; ++I
)
70 Hash
= 33 * Hash
+ *I
;
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
;
81 friend class pkgCacheListParser
;
82 typedef pkgCacheListParser ListParser
;
86 template<typename Iter
> class Dynamic
{
88 static std::vector
<Iter
*> toReMap
;
89 explicit Dynamic(Iter
&I
) {
90 toReMap
.push_back(&I
);
97 #if __cplusplus >= 201103L
98 Dynamic(const Dynamic
&) = delete;
99 void operator=(const Dynamic
&) = delete;
107 OpProgress
*Progress
;
109 std::string RlsFileName
;
110 pkgCache::ReleaseFile
*CurrentRlsFile
;
111 std::string PkgFileName
;
112 pkgCache::PackageFile
*CurrentFile
;
114 #ifdef APT_PKG_EXPOSE_STRING_VIEW
115 bool NewGroup(pkgCache::GrpIterator
&Grp
, APT::StringView Name
);
116 bool NewPackage(pkgCache::PkgIterator
&Pkg
, APT::StringView Name
, APT::StringView Arch
);
117 map_pointer_t
NewVersion(pkgCache::VerIterator
&Ver
, APT::StringView
const &VerStr
,
118 map_pointer_t
const ParentPkg
, unsigned short const Hash
,
119 map_pointer_t
const Next
);
121 bool NewFileVer(pkgCache::VerIterator
&Ver
,ListParser
&List
);
122 bool NewFileDesc(pkgCache::DescIterator
&Desc
,ListParser
&List
);
123 bool NewDepends(pkgCache::PkgIterator
&Pkg
, pkgCache::VerIterator
&Ver
,
124 map_pointer_t
const Version
, uint8_t const Op
,
125 uint8_t const Type
, map_pointer_t
* &OldDepLast
);
126 map_pointer_t
NewDescription(pkgCache::DescIterator
&Desc
,const std::string
&Lang
,const MD5SumValue
&md5sum
,map_stringitem_t
const idxmd5str
);
127 bool NewProvides(pkgCache::VerIterator
&Ver
, pkgCache::PkgIterator
&Pkg
,
128 map_stringitem_t
const ProvidesVersion
, uint8_t const Flags
);
132 enum StringType
{ MIXED
, PKGNAME
, VERSIONNUMBER
, SECTION
};
133 map_stringitem_t
StoreString(StringType
const type
, const char * S
, unsigned int const Size
);
135 #ifdef APT_PKG_EXPOSE_STRING_VIEW
136 inline map_stringitem_t
StoreString(enum StringType
const type
, APT::StringView S
) {return StoreString(type
, S
.data(),S
.length());};
139 void DropProgress() {Progress
= 0;};
140 bool SelectFile(const std::string
&File
,pkgIndexFile
const &Index
, std::string
const &Architecture
, std::string
const &Component
, unsigned long Flags
= 0);
141 bool SelectReleaseFile(const std::string
&File
, const std::string
&Site
, unsigned long Flags
= 0);
142 bool MergeList(ListParser
&List
,pkgCache::VerIterator
*Ver
= 0);
143 inline pkgCache
&GetCache() {return Cache
;};
144 inline pkgCache::PkgFileIterator
GetCurFile()
145 {return pkgCache::PkgFileIterator(Cache
,CurrentFile
);};
146 inline pkgCache::RlsFileIterator
GetCurRlsFile()
147 {return pkgCache::RlsFileIterator(Cache
,CurrentRlsFile
);};
149 APT_PUBLIC
static bool MakeStatusCache(pkgSourceList
&List
,OpProgress
*Progress
,
150 MMap
**OutMap
= 0,bool AllowMem
= false);
151 APT_HIDDEN
static bool MakeStatusCache(pkgSourceList
&List
,OpProgress
*Progress
,
152 MMap
**OutMap
,pkgCache
**OutCache
, bool AllowMem
= false);
153 APT_PUBLIC
static bool MakeOnlyStatusCache(OpProgress
*Progress
,DynamicMMap
**OutMap
);
155 void ReMap(void const * const oldMap
, void const * const newMap
, size_t oldSize
);
158 pkgCacheGenerator(DynamicMMap
*Map
,OpProgress
*Progress
);
159 virtual ~pkgCacheGenerator();
163 APT_HIDDEN
bool MergeListGroup(ListParser
&List
, std::string
const &GrpName
);
164 APT_HIDDEN
bool MergeListPackage(ListParser
&List
, pkgCache::PkgIterator
&Pkg
);
165 #ifdef APT_PKG_EXPOSE_STRING_VIEW
166 APT_HIDDEN
bool MergeListVersion(ListParser
&List
, pkgCache::PkgIterator
&Pkg
,
167 APT::StringView
const &Version
, pkgCache::VerIterator
* &OutVer
);
170 APT_HIDDEN
bool AddImplicitDepends(pkgCache::GrpIterator
&G
, pkgCache::PkgIterator
&P
,
171 pkgCache::VerIterator
&V
);
172 APT_HIDDEN
bool AddImplicitDepends(pkgCache::VerIterator
&V
, pkgCache::PkgIterator
&D
);
174 APT_HIDDEN
bool AddNewDescription(ListParser
&List
, pkgCache::VerIterator
&Ver
,
175 std::string
const &lang
, MD5SumValue
const &CurMd5
, map_stringitem_t
&md5idx
);
178 // This is the abstract package list parser class. /*{{{*/
179 class APT_HIDDEN pkgCacheListParser
181 pkgCacheGenerator
*Owner
;
182 friend class pkgCacheGenerator
;
185 pkgCache::VerIterator OldDepVer
;
186 map_pointer_t
*OldDepLast
;
192 inline map_stringitem_t
StoreString(pkgCacheGenerator::StringType
const type
, const char *S
,unsigned int Size
) {return Owner
->StoreString(type
, S
, Size
);};
193 #ifdef APT_PKG_EXPOSE_STRING_VIEW
194 inline map_stringitem_t
StoreString(pkgCacheGenerator::StringType
const type
, APT::StringView S
) {return Owner
->StoreString(type
, S
);};
197 inline map_stringitem_t
WriteString(const std::string
&S
) {return Owner
->WriteStringInMap(S
);};
198 inline map_stringitem_t
WriteString(const char *S
,unsigned int Size
) {return Owner
->WriteStringInMap(S
,Size
);};
199 #ifdef APT_PKG_EXPOSE_STRING_VIEW
200 bool NewDepends(pkgCache::VerIterator
&Ver
,APT::StringView Package
, APT::StringView Arch
,
201 APT::StringView Version
,uint8_t const Op
,
203 bool NewProvides(pkgCache::VerIterator
&Ver
,APT::StringView PkgName
,
204 APT::StringView PkgArch
, APT::StringView Version
,
205 uint8_t const Flags
);
206 bool NewProvidesAllArch(pkgCache::VerIterator
&Ver
, APT::StringView Package
,
207 APT::StringView Version
, uint8_t const Flags
);
211 // These all operate against the current section
212 virtual std::string
Package() = 0;
213 virtual bool ArchitectureAll() = 0;
214 #ifdef APT_PKG_EXPOSE_STRING_VIEW
215 virtual APT::StringView
Architecture() = 0;
216 virtual APT::StringView
Version() = 0;
218 virtual bool NewVersion(pkgCache::VerIterator
&Ver
) = 0;
219 virtual std::vector
<std::string
> AvailableDescriptionLanguages() = 0;
220 virtual MD5SumValue
Description_md5() = 0;
221 virtual unsigned short VersionHash() = 0;
222 /** compare currently parsed version with given version
224 * \param Hash of the currently parsed version
225 * \param Ver to compare with
227 virtual bool SameVersion(unsigned short const Hash
, pkgCache::VerIterator
const &Ver
);
228 virtual bool UsePackage(pkgCache::PkgIterator
&Pkg
,
229 pkgCache::VerIterator
&Ver
) = 0;
230 virtual map_filesize_t
Offset() = 0;
231 virtual map_filesize_t
Size() = 0;
233 virtual bool Step() = 0;
235 virtual bool CollectFileProvides(pkgCache
&/*Cache*/,
236 pkgCache::VerIterator
&/*Ver*/) {return true;};
238 pkgCacheListParser();
239 virtual ~pkgCacheListParser();
243 APT_DEPRECATED_MSG("Use pkgCacheGenerator::MakeStatusCache instead") bool pkgMakeStatusCache(pkgSourceList
&List
,OpProgress
&Progress
,
244 MMap
**OutMap
= 0,bool AllowMem
= false);
245 APT_DEPRECATED_MSG("Use pkgCacheGenerator::MakeOnlyStatusCache instead") bool pkgMakeOnlyStatusCache(OpProgress
&Progress
,DynamicMMap
**OutMap
);