apt: apt-pkg/pkgcachegen.h Source File

apt 2.3.15 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
36class FileFd;
37class pkgSourceList;
38class OpProgress;
39class pkgIndexFile;
40class 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 friend class pkgCacheListParser;
80 typedef pkgCacheListParser ListParser;
81
82 public:
83
84 template<typename Iter> class Dynamic {
85 public:
86 static std::vector<Iter*> toReMap;
87 explicit Dynamic(Iter &I) {
88 toReMap.push_back(&I);
89 }
90
91 ~Dynamic() {
92 toReMap.pop_back();
93 }
94
95#if __cplusplus >= 201103L
96 Dynamic(const Dynamic&) = delete;
97 void operator=(const Dynamic&) = delete;
98#endif
99 };
100
101 protected:
102
103 DynamicMMap &Map;
104 pkgCache Cache;
105 OpProgress *Progress;
106
107 std::string RlsFileName;
108 pkgCache::ReleaseFile *CurrentRlsFile;
109 std::string PkgFileName;
110 pkgCache::PackageFile *CurrentFile;
111
112 bool NewGroup(pkgCache::GrpIterator &Grp, APT::StringView Name);
113 bool NewPackage(pkgCache::PkgIterator &Pkg, APT::StringView Name, APT::StringView Arch);
114 map_pointer<pkgCache::Version> NewVersion(pkgCache::VerIterator &Ver, APT::StringView const &VerStr,
115 map_pointer<pkgCache::Package> const ParentPkg, uint32_t Hash,
116 map_pointer<pkgCache::Version> const Next);
117 map_pointer<pkgCache::Description> NewDescription(pkgCache::DescIterator &Desc,const std::string &Lang, APT::StringView md5sum,map_stringitem_t const idxmd5str);
118 bool NewFileVer(pkgCache::VerIterator &Ver,ListParser &List);
119 bool NewFileDesc(pkgCache::DescIterator &Desc,ListParser &List);
120 bool NewDepends(pkgCache::PkgIterator &Pkg, pkgCache::VerIterator &Ver,
121 map_stringitem_t const Version, uint8_t const Op,
122 uint8_t const Type, map_pointer<pkgCache::Dependency>* &OldDepLast);
123 bool NewProvides(pkgCache::VerIterator &Ver, pkgCache::PkgIterator &Pkg,
124 map_stringitem_t const ProvidesVersion, uint8_t const Flags);
125
126 public:
127
128 enum StringType { MIXED, VERSIONNUMBER, SECTION };
129 map_stringitem_t StoreString(StringType const type, const char * S, unsigned int const Size);
130
131 inline map_stringitem_t StoreString(enum StringType const type, APT::StringView S) {return StoreString(type, S.data(),S.length());};
132
133 void DropProgress() {Progress = 0;};
134 bool SelectFile(const std::string &File,pkgIndexFile const &Index, std::string const &Architecture, std::string const &Component, unsigned long Flags = 0);
135 bool SelectReleaseFile(const std::string &File, const std::string &Site, unsigned long Flags = 0);
136 bool MergeList(ListParser &List,pkgCache::VerIterator *Ver = 0);
137 inline pkgCache &GetCache() {return Cache;};
138 inline pkgCache::PkgFileIterator GetCurFile()
139 {return pkgCache::PkgFileIterator(Cache,CurrentFile);};
140 inline pkgCache::RlsFileIterator GetCurRlsFile()
141 {return pkgCache::RlsFileIterator(Cache,CurrentRlsFile);};
142
143 APT_PUBLIC static bool MakeStatusCache(pkgSourceList &List,OpProgress *Progress,
144 MMap **OutMap = 0,bool AllowMem = false);
145 APT_HIDDEN static bool MakeStatusCache(pkgSourceList &List,OpProgress *Progress,
146 MMap **OutMap,pkgCache **OutCache, bool AllowMem = false);
147 APT_PUBLIC static bool MakeOnlyStatusCache(OpProgress *Progress,DynamicMMap **OutMap);
148
149 void ReMap(void const * const oldMap, void * const newMap, size_t oldSize);
150 bool Start();
151
152 pkgCacheGenerator(DynamicMMap *Map,OpProgress *Progress);
153 virtual ~pkgCacheGenerator();
154
155 private:
156 void * const d;
157 APT_HIDDEN bool MergeListGroup(ListParser &List, std::string const &GrpName);
158 APT_HIDDEN bool MergeListPackage(ListParser &List, pkgCache::PkgIterator &Pkg);
159 APT_HIDDEN bool MergeListVersion(ListParser &List, pkgCache::PkgIterator &Pkg,
160 APT::StringView const &Version, pkgCache::VerIterator* &OutVer);
161
162 APT_HIDDEN bool AddImplicitDepends(pkgCache::GrpIterator &G, pkgCache::PkgIterator &P,
163 pkgCache::VerIterator &V);
164 APT_HIDDEN bool AddImplicitDepends(pkgCache::VerIterator &V, pkgCache::PkgIterator &D);
165
166 APT_HIDDEN bool AddNewDescription(ListParser &List, pkgCache::VerIterator &Ver,
167 std::string const &lang, APT::StringView CurMd5, map_stringitem_t &md5idx);
168};
169 /*}}}*/
170// This is the abstract package list parser class. /*{{{*/
171 class APT_HIDDEN pkgCacheListParser
172{
173 pkgCacheGenerator *Owner;
174 friend class pkgCacheGenerator;
175
176 // Some cache items
177 pkgCache::VerIterator OldDepVer;
178 map_pointer<pkgCache::Dependency> *OldDepLast;
179
180 void * const d;
181
182 protected:
183 inline bool NewGroup(pkgCache::GrpIterator &Grp, APT::StringView Name) { return Owner->NewGroup(Grp, Name); }
184 inline map_stringitem_t StoreString(pkgCacheGenerator::StringType const type, const char *S,unsigned int Size) {return Owner->StoreString(type, S, Size);};
185 inline map_stringitem_t StoreString(pkgCacheGenerator::StringType const type, APT::StringView S) {return Owner->StoreString(type, S);};
186 inline map_stringitem_t WriteString(APT::StringView S) {return Owner->WriteStringInMap(S.data(), S.size());};
187
188 inline map_stringitem_t WriteString(const char *S,unsigned int Size) {return Owner->WriteStringInMap(S,Size);};
189 bool NewDepends(pkgCache::VerIterator &Ver,APT::StringView Package, APT::StringView Arch,
190 APT::StringView Version,uint8_t const Op,
191 uint8_t const Type);
192 bool NewProvides(pkgCache::VerIterator &Ver,APT::StringView PkgName,
193 APT::StringView PkgArch, APT::StringView Version,
194 uint8_t const Flags);
195 bool NewProvidesAllArch(pkgCache::VerIterator &Ver, APT::StringView Package,
196 APT::StringView Version, uint8_t const Flags);
197 public:
198
199 // These all operate against the current section
200 virtual std::string Package() = 0;
201 virtual bool ArchitectureAll() = 0;
202 virtual APT::StringView Architecture() = 0;
203 virtual APT::StringView Version() = 0;
204 virtual bool NewVersion(pkgCache::VerIterator &Ver) = 0;
205 virtual std::vector<std::string> AvailableDescriptionLanguages() = 0;
206 virtual APT::StringView Description_md5() = 0;
207 virtual uint32_t VersionHash() = 0;
213 virtual bool SameVersion(uint32_t Hash, pkgCache::VerIterator const &Ver);
214 virtual bool UsePackage(pkgCache::PkgIterator &Pkg,
215 pkgCache::VerIterator &Ver) = 0;
216 virtual map_filesize_t Offset() = 0;
217 virtual map_filesize_t Size() = 0;
218
219 virtual bool Step() = 0;
220
221 virtual bool CollectFileProvides(pkgCache &/*Cache*/,
222 pkgCache::VerIterator &/*Ver*/) {return true;};
223
224 pkgCacheListParser();
225 virtual ~pkgCacheListParser();
226};
227 /*}}}*/
228
229#endif
APT::StringView
Simple subset of std::string_view from C++17.
Definition: string_view.h:27
DynamicMMap
Definition: mmap.h:78
FileFd
Definition: fileutl.h:39
MMap
Definition: mmap.h:41
OpProgress
Definition: progress.h:30
map_pointer< char >
pkgCacheGenerator::Dynamic
Definition: pkgcachegen.h:84
pkgCacheGenerator
Definition: pkgcachegen.h:43
pkgCacheListParser
Definition: pkgcachegen.h:172
pkgCache
Definition: cacheiterators.h:48
pkgIndexFile
Definition: indexfile.h:102
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