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