apt: apt-pkg/pkgcachegen.h Source File

apt 3.2.0 commandline package manager

pkgcachegen.h
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/macros.h>
22#include <apt-pkg/mmap.h>
23#include <apt-pkg/pkgcache.h>
24
25#include <string>
26#include <string_view>
27#include <vector>
28#if __cplusplus >= 201103L
29#include <unordered_set>
30#endif
31
32#ifdef APT_COMPILING_APT
33#include <xxhash.h>
34#endif
35
36class FileFd;
37class pkgSourceList;
38class IndexTarget;
39class OpProgress;
40class pkgIndexFile;
41class pkgCacheListParser;
42
43 class APT_HIDDEN pkgCacheGenerator /*{{{*/
44{
45 APT_HIDDEN map_stringitem_t WriteStringInMap(std::string_view String) { return WriteStringInMap(String.data(), String.size()); };
46 APT_HIDDEN map_stringitem_t WriteStringInMap(const char *String);
47 APT_HIDDEN map_stringitem_t WriteStringInMap(const char *String, const unsigned long &Len);
48 APT_HIDDEN uint32_t AllocateInMap(const unsigned long &size);
49 template<typename T> map_pointer<T> AllocateInMap() {
50 return map_pointer<T>{AllocateInMap(sizeof(T))};
51 }
52
53 // Dirty hack for public users that do not use C++11 yet
54#if __cplusplus >= 201103L && defined(APT_COMPILING_APT)
55 struct string_pointer {
56 const char *data_;
57 size_t size;
58 pkgCacheGenerator *generator;
59 map_stringitem_t item;
60
61 const char *data() const {
62 return data_ != nullptr ? data_ : static_cast<char*>(generator->Map.Data()) + item;
63 }
64
65 bool operator ==(string_pointer const &other) const {
66 return size == other.size && memcmp(data(), other.data(), size) == 0;
67 }
68 };
69 struct hash {
70 uint32_t operator()(string_pointer const &that) const {
71 return XXH3_64bits(that.data(), that.size) & 0xFFFFFFFF;
72 }
73 };
74
75 std::unordered_set<string_pointer, hash> strMixed;
76 std::unordered_set<string_pointer, hash> strVersions;
77 std::unordered_set<string_pointer, hash> strSections;
78#endif
79
80 struct VersionExtra
81 {
82 char SHA256[64];
83 };
84 std::vector<VersionExtra> VersionExtra;
85
86 friend class pkgCacheListParser;
87 typedef pkgCacheListParser ListParser;
88
89 public:
90
91 template<typename Iter> class Dynamic {
92 public:
93 static std::vector<Iter*> toReMap;
94 explicit Dynamic(Iter &I) {
95 toReMap.push_back(&I);
96 }
97
98 ~Dynamic() {
99 toReMap.pop_back();
100 }
101
102#if __cplusplus >= 201103L
103 Dynamic(const Dynamic&) = delete;
104 void operator=(const Dynamic&) = delete;
105#endif
106 };
107
108 protected:
109
110 DynamicMMap &Map;
111 pkgCache Cache;
112 OpProgress *Progress;
113
114 std::string RlsFileName;
115 pkgCache::ReleaseFile *CurrentRlsFile;
116 std::string PkgFileName;
117 pkgCache::PackageFile *CurrentFile;
118 std::unordered_set<std::string> include;
119 std::unordered_set<std::string> exclude;
120
121 bool NewGroup(pkgCache::GrpIterator &Grp, std::string_view Name);
122 bool NewPackage(pkgCache::PkgIterator &Pkg, std::string_view Name, std::string_view Arch);
123 map_pointer<pkgCache::Version> NewVersion(pkgCache::VerIterator &Ver, std::string_view VerStr,
124 map_pointer<pkgCache::Package> const ParentPkg, uint32_t Hash,
125 map_pointer<pkgCache::Version> const Next);
126 map_pointer<pkgCache::Description> NewDescription(pkgCache::DescIterator &Desc,const std::string &Lang, std::string_view md5sum,map_stringitem_t const idxmd5str);
127 bool NewFileVer(pkgCache::VerIterator &Ver,ListParser &List);
128 bool NewFileDesc(pkgCache::DescIterator &Desc,ListParser &List);
129 bool NewDepends(pkgCache::PkgIterator &Pkg, pkgCache::VerIterator &Ver,
130 map_stringitem_t const Version, uint8_t const Op,
131 uint8_t const Type, map_pointer<pkgCache::Dependency>* &OldDepLast);
132 bool NewProvides(pkgCache::VerIterator &Ver, pkgCache::PkgIterator &Pkg,
133 map_stringitem_t const ProvidesVersion, uint8_t const Flags);
134
135 public:
136
137 enum StringType { MIXED, VERSIONNUMBER, SECTION };
138 map_stringitem_t StoreString(StringType const type, const char * S, unsigned int const Size);
139
140 inline map_stringitem_t StoreString(enum StringType const type, std::string_view S) {return StoreString(type, S.data(),S.length());};
141
142 void DropProgress() {Progress = 0;};
143 bool SelectFile(const std::string &File, pkgIndexFile const &Index, std::string const &Architecture, std::string const &Component, const IndexTarget *target, 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 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 APT_HIDDEN bool MergeListVersion(ListParser &List, pkgCache::PkgIterator &Pkg,
169 std::string_view Version, pkgCache::VerIterator* &OutVer);
170
171 APT_HIDDEN bool AddImplicitDepends(pkgCache::GrpIterator &G, pkgCache::PkgIterator &P,
172 pkgCache::VerIterator &V);
173 APT_HIDDEN bool AddImplicitDepends(pkgCache::VerIterator &V, pkgCache::PkgIterator &D);
174
175 APT_HIDDEN bool AddNewDescription(ListParser &List, pkgCache::VerIterator &Ver,
176 std::string const &lang, std::string_view CurMd5, map_stringitem_t &md5idx);
177};
178 /*}}}*/
179// This is the abstract package list parser class. /*{{{*/
180 class APT_HIDDEN pkgCacheListParser
181{
182 pkgCacheGenerator *Owner;
183 friend class pkgCacheGenerator;
184
185 // Some cache items
186 pkgCache::VerIterator OldDepVer;
187 map_pointer<pkgCache::Dependency> *OldDepLast;
188
189 void * const d;
190
191 protected:
192 inline bool NewGroup(pkgCache::GrpIterator &Grp, std::string_view Name) { return Owner->NewGroup(Grp, Name); }
193 inline map_stringitem_t StoreString(pkgCacheGenerator::StringType const type, const char *S,unsigned int Size) {return Owner->StoreString(type, S, Size);};
194 inline map_stringitem_t StoreString(pkgCacheGenerator::StringType const type, std::string_view S) {return Owner->StoreString(type, S);};
195 inline map_stringitem_t WriteString(std::string_view S) {return Owner->WriteStringInMap(S.data(), S.size());};
196
197 inline map_stringitem_t WriteString(const char *S,unsigned int Size) {return Owner->WriteStringInMap(S,Size);};
198 bool NewDepends(pkgCache::VerIterator &Ver,std::string_view Package, std::string_view Arch,
199 std::string_view Version,uint8_t const Op,
200 uint8_t const Type);
201 bool NewProvides(pkgCache::VerIterator &Ver,std::string_view PkgName,
202 std::string_view PkgArch, std::string_view Version,
203 uint8_t const Flags);
204 bool NewProvidesAllArch(pkgCache::VerIterator &Ver, std::string_view Package,
205 std::string_view Version, uint8_t const Flags);
206 public:
207
208 // These all operate against the current section
209 virtual std::string Package() = 0;
210 virtual bool ArchitectureAll() = 0;
211 virtual std::string_view Architecture() = 0;
212 virtual std::string_view Version() = 0;
213 virtual bool NewVersion(pkgCache::VerIterator &Ver) = 0;
214 virtual std::vector<std::string> AvailableDescriptionLanguages() = 0;
215 virtual std::string_view Description_md5() = 0;
216 virtual uint32_t VersionHash() = 0;
222 virtual bool SameVersion(uint32_t Hash, pkgCache::VerIterator const &Ver);
223 virtual bool UsePackage(pkgCache::PkgIterator &Pkg,
224 pkgCache::VerIterator &Ver) = 0;
225 virtual map_filesize_t Offset() = 0;
226 virtual map_filesize_t Size() = 0;
227
228 virtual bool Step() = 0;
229
230 virtual bool CollectFileProvides(pkgCache &/*Cache*/,
231 pkgCache::VerIterator &/*Ver*/) {return true;};
232
233 pkgCacheListParser();
234 virtual ~pkgCacheListParser();
235};
236 /*}}}*/
237
238#endif
DynamicMMap
Definition mmap.h:82
FileFd
Definition fileutl.h:43
IndexTarget
Information about an index file.
Definition indexfile.h:39
OpProgress
Definition progress.h:30
map_pointer
Definition pkgcache.h:98
pkgCacheListParser
Definition pkgcachegen.h:181
pkgCacheListParser::SameVersion
virtual bool SameVersion(uint32_t Hash, pkgCache::VerIterator const &Ver)
Definition pkgcachegen.cc:1326
pkgCache::DescIterator
Definition cacheiterators.h:311
pkgCache::GrpIterator
Definition cacheiterators.h:106
pkgCache::PkgIterator
Definition cacheiterators.h:147
pkgCache::VerIterator
Definition cacheiterators.h:218
pkgCache
Definition pkgcache.h:126
pkgIndexFile
Definition indexfile.h:103
pkgSourceList
Definition sourcelist.h:43
pkgcache.h
pkgCache - Structure definitions for the cache file
pkgCache::PackageFile
stores information about the files used to generate the cache
Definition pkgcache.h:562
pkgCache::ReleaseFile
stores information about the release files used to generate the cache
Definition pkgcache.h:518