apt 2.9.16 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 <vector>
27#if __cplusplus >= 201103L
28#include <unordered_set>
29#endif
30
31#ifdef APT_COMPILING_APT
32#include <xxhash.h>
33#endif
34
35 class FileFd;
36 class pkgSourceList;
37 class OpProgress;
38 class pkgIndexFile;
39 class pkgCacheListParser;
40
41 class APT_HIDDEN pkgCacheGenerator /*{{{*/
42{
43 APT_HIDDEN map_stringitem_t WriteStringInMap(std::string_view String) { return WriteStringInMap(String.data(), String.size()); };
44 APT_HIDDEN map_stringitem_t WriteStringInMap(const char *String);
45 APT_HIDDEN map_stringitem_t WriteStringInMap(const char *String, const unsigned long &Len);
46 APT_HIDDEN uint32_t AllocateInMap(const unsigned long &size);
47 template<typename T> map_pointer<T> AllocateInMap() {
48 return map_pointer<T>{AllocateInMap(sizeof(T))};
49 }
50
51 // Dirty hack for public users that do not use C++11 yet
52#if __cplusplus >= 201103L && defined(APT_COMPILING_APT)
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 return XXH3_64bits(that.data(), that.size) & 0xFFFFFFFF;
70 }
71 };
72
73 std::unordered_set<string_pointer, hash> strMixed;
74 std::unordered_set<string_pointer, hash> strVersions;
75 std::unordered_set<string_pointer, hash> strSections;
76#endif
77
78 struct VersionExtra
79 {
80 char SHA256[64];
81 };
82 std::vector<VersionExtra> VersionExtra{32 * 1024};
83
84 friend class pkgCacheListParser;
85 typedef pkgCacheListParser ListParser;
86
87 public:
88
89 template<typename Iter> class Dynamic {
90 public:
91 static std::vector<Iter*> toReMap;
92 explicit Dynamic(Iter &I) {
93 toReMap.push_back(&I);
94 }
95
96 ~Dynamic() {
97 toReMap.pop_back();
98 }
99
100#if __cplusplus >= 201103L
101 Dynamic(const Dynamic&) = delete;
102 void operator=(const Dynamic&) = delete;
103#endif
104 };
105
106 protected:
107
108 DynamicMMap &Map;
109 pkgCache Cache;
110 OpProgress *Progress;
111
112 std::string RlsFileName;
113 pkgCache::ReleaseFile *CurrentRlsFile;
114 std::string PkgFileName;
115 pkgCache::PackageFile *CurrentFile;
116
117 bool NewGroup(pkgCache::GrpIterator &Grp, std::string_view Name);
118 bool NewPackage(pkgCache::PkgIterator &Pkg, std::string_view Name, std::string_view Arch);
119 map_pointer<pkgCache::Version> NewVersion(pkgCache::VerIterator &Ver, std::string_view VerStr,
120 map_pointer<pkgCache::Package> const ParentPkg, uint32_t Hash,
121 map_pointer<pkgCache::Version> const Next);
122 map_pointer<pkgCache::Description> NewDescription(pkgCache::DescIterator &Desc,const std::string &Lang, std::string_view md5sum,map_stringitem_t const idxmd5str);
123 bool NewFileVer(pkgCache::VerIterator &Ver,ListParser &List);
124 bool NewFileDesc(pkgCache::DescIterator &Desc,ListParser &List);
125 bool NewDepends(pkgCache::PkgIterator &Pkg, pkgCache::VerIterator &Ver,
126 map_stringitem_t const Version, uint8_t const Op,
127 uint8_t const Type, map_pointer<pkgCache::Dependency>* &OldDepLast);
128 bool NewProvides(pkgCache::VerIterator &Ver, pkgCache::PkgIterator &Pkg,
129 map_stringitem_t const ProvidesVersion, uint8_t const Flags);
130
131 public:
132
133 enum StringType { MIXED, VERSIONNUMBER, SECTION };
134 map_stringitem_t StoreString(StringType const type, const char * S, unsigned int const Size);
135
136 inline map_stringitem_t StoreString(enum StringType const type, std::string_view S) {return StoreString(type, S.data(),S.length());};
137
138 void DropProgress() {Progress = 0;};
139 bool SelectFile(const std::string &File,pkgIndexFile const &Index, std::string const &Architecture, std::string const &Component, unsigned long Flags = 0);
140 bool SelectReleaseFile(const std::string &File, const std::string &Site, unsigned long Flags = 0);
141 bool MergeList(ListParser &List,pkgCache::VerIterator *Ver = 0);
142 inline pkgCache &GetCache() {return Cache;};
143 inline pkgCache::PkgFileIterator GetCurFile()
144 {return pkgCache::PkgFileIterator(Cache,CurrentFile);};
145 inline pkgCache::RlsFileIterator GetCurRlsFile()
146 {return pkgCache::RlsFileIterator(Cache,CurrentRlsFile);};
147
148 APT_PUBLIC static bool MakeStatusCache(pkgSourceList &List,OpProgress *Progress,
149 MMap **OutMap = 0,bool AllowMem = false);
150 APT_HIDDEN static bool MakeStatusCache(pkgSourceList &List,OpProgress *Progress,
151 MMap **OutMap,pkgCache **OutCache, bool AllowMem = false);
152 APT_PUBLIC static bool MakeOnlyStatusCache(OpProgress *Progress,DynamicMMap **OutMap);
153
154 void ReMap(void const * const oldMap, void * const newMap, size_t oldSize);
155 bool Start();
156
157 pkgCacheGenerator(DynamicMMap *Map,OpProgress *Progress);
158 virtual ~pkgCacheGenerator();
159
160 private:
161 void * const d;
162 APT_HIDDEN bool MergeListGroup(ListParser &List, std::string const &GrpName);
163 APT_HIDDEN bool MergeListPackage(ListParser &List, pkgCache::PkgIterator &Pkg);
164 APT_HIDDEN bool MergeListVersion(ListParser &List, pkgCache::PkgIterator &Pkg,
165 std::string_view Version, pkgCache::VerIterator* &OutVer);
166
167 APT_HIDDEN bool AddImplicitDepends(pkgCache::GrpIterator &G, pkgCache::PkgIterator &P,
168 pkgCache::VerIterator &V);
169 APT_HIDDEN bool AddImplicitDepends(pkgCache::VerIterator &V, pkgCache::PkgIterator &D);
170
171 APT_HIDDEN bool AddNewDescription(ListParser &List, pkgCache::VerIterator &Ver,
172 std::string const &lang, std::string_view CurMd5, map_stringitem_t &md5idx);
173};
174 /*}}}*/
175// This is the abstract package list parser class. /*{{{*/
176 class APT_HIDDEN pkgCacheListParser
177{
178 pkgCacheGenerator *Owner;
179 friend class pkgCacheGenerator;
180
181 // Some cache items
182 pkgCache::VerIterator OldDepVer;
183 map_pointer<pkgCache::Dependency> *OldDepLast;
184
185 void * const d;
186
187 protected:
188 inline bool NewGroup(pkgCache::GrpIterator &Grp, std::string_view Name) { return Owner->NewGroup(Grp, Name); }
189 inline map_stringitem_t StoreString(pkgCacheGenerator::StringType const type, const char *S,unsigned int Size) {return Owner->StoreString(type, S, Size);};
190 inline map_stringitem_t StoreString(pkgCacheGenerator::StringType const type, std::string_view S) {return Owner->StoreString(type, S);};
191 inline map_stringitem_t WriteString(std::string_view S) {return Owner->WriteStringInMap(S.data(), S.size());};
192
193 inline map_stringitem_t WriteString(const char *S,unsigned int Size) {return Owner->WriteStringInMap(S,Size);};
194 bool NewDepends(pkgCache::VerIterator &Ver,std::string_view Package, std::string_view Arch,
195 std::string_view Version,uint8_t const Op,
196 uint8_t const Type);
197 bool NewProvides(pkgCache::VerIterator &Ver,std::string_view PkgName,
198 std::string_view PkgArch, std::string_view Version,
199 uint8_t const Flags);
200 bool NewProvidesAllArch(pkgCache::VerIterator &Ver, std::string_view Package,
201 std::string_view Version, uint8_t const Flags);
202 public:
203
204 // These all operate against the current section
205 virtual std::string Package() = 0;
206 virtual bool ArchitectureAll() = 0;
207 virtual std::string_view Architecture() = 0;
208 virtual std::string_view Version() = 0;
209 virtual bool NewVersion(pkgCache::VerIterator &Ver) = 0;
210 virtual std::vector<std::string> AvailableDescriptionLanguages() = 0;
211 virtual std::string_view Description_md5() = 0;
212 virtual uint32_t VersionHash() = 0;
218 virtual bool SameVersion(uint32_t Hash, pkgCache::VerIterator const &Ver);
219 virtual bool UsePackage(pkgCache::PkgIterator &Pkg,
220 pkgCache::VerIterator &Ver) = 0;
221 virtual map_filesize_t Offset() = 0;
222 virtual map_filesize_t Size() = 0;
223
224 virtual bool Step() = 0;
225
226 virtual bool CollectFileProvides(pkgCache &/*Cache*/,
227 pkgCache::VerIterator &/*Ver*/) {return true;};
228
229 pkgCacheListParser();
230 virtual ~pkgCacheListParser();
231};
232 /*}}}*/
233
234#endif
DynamicMMap
Definition mmap.h:82
FileFd
Definition fileutl.h:42
MMap
Definition mmap.h:45
OpProgress
Definition progress.h:30
map_pointer< char >
pkgCacheGenerator::Dynamic
Definition pkgcachegen.h:89
pkgCacheGenerator
Definition pkgcachegen.h:42
pkgCacheListParser
Definition pkgcachegen.h:177
pkgCache::DescIterator
Definition cacheiterators.h:273
pkgCache::GrpIterator
Definition cacheiterators.h:106
pkgCache::PkgFileIterator
Definition cacheiterators.h:454
pkgCache::PkgIterator
Definition cacheiterators.h:147
pkgCache::RlsFileIterator
Definition cacheiterators.h:425
pkgCache::VerIterator
Definition cacheiterators.h:193
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:552
pkgCache::ReleaseFile
stores information about the release files used to generate the cache
Definition pkgcache.h:508