apt 2.9.14 commandline package manager

tagfile.h
1// -*- mode: cpp; mode: fold -*-
2// Description /*{{{*/
3/* ######################################################################
4
5 Fast scanner for RFC-822 type header information
6
7 This parser handles Debian package files (and others). Their form is
8 RFC-822 type header fields in groups separated by a blank line.
9
10 The parser reads the file and provides methods to step linearly
11 over it or to jump to a pre-recorded start point and read that record.
12
13 A second class is used to perform pre-parsing of the record. It works
14 by indexing the start of each header field and providing lookup
15 functions for header fields.
16
17 ##################################################################### */
18 /*}}}*/
19#ifndef PKGLIB_TAGFILE_H
20#define PKGLIB_TAGFILE_H
21
22#include <apt-pkg/macros.h>
23
24#include <cstdint>
25#include <cstdio>
26
27#include <list>
28#include <string>
29#include <vector>
30#include <apt-pkg/string_view.h>
31
32
33 class FileFd;
34 class pkgTagSectionPrivate;
35 class pkgTagFilePrivate;
36
44 class APT_PUBLIC pkgTagSection
45{
46 const char *Section;
47 unsigned int AlphaIndexes[128];
48 unsigned int BetaIndexes[128];
49
50 pkgTagSectionPrivate * const d;
51
52 APT_HIDDEN bool FindInternal(unsigned int Pos,const char *&Start, const char *&End) const;
53 APT_HIDDEN APT::StringView FindInternal(unsigned int Pos) const;
54 APT_HIDDEN APT::StringView FindRawInternal(unsigned int Pos) const;
55 APT_HIDDEN signed int FindIInternal(unsigned int Pos,signed long Default = 0) const;
56 APT_HIDDEN bool FindBInternal(unsigned int Pos, bool Default = false) const;
57 APT_HIDDEN unsigned long long FindULLInternal(unsigned int Pos, unsigned long long const &Default = 0) const;
58 APT_HIDDEN bool FindFlagInternal(unsigned int Pos,uint8_t &Flags, uint8_t const Flag) const;
59 APT_HIDDEN bool FindFlagInternal(unsigned int Pos,unsigned long &Flags, unsigned long Flag) const;
60
61 protected:
62 const char *Stop;
63
64 public:
65
66 inline bool operator ==(const pkgTagSection &rhs) {return Section == rhs.Section;};
67 inline bool operator !=(const pkgTagSection &rhs) {return Section != rhs.Section;};
68
69 // TODO: Remove internally
70 std::string FindS(APT::StringView sv) const { return std::string{Find(sv)}; }
71 std::string FindRawS(APT::StringView sv) const { return std::string{FindRaw(sv)}; };
72
73 // Functions for lookup with a perfect hash function
74 enum class Key;
75#ifdef APT_COMPILING_APT
76 bool Find(Key key,const char *&Start, const char *&End) const;
77 bool Find(Key key,unsigned int &Pos) const;
78 signed int FindI(Key key,signed long Default = 0) const;
79 bool FindB(Key key, bool Default = false) const;
80 unsigned long long FindULL(Key key, unsigned long long const &Default = 0) const;
81 bool FindFlag(Key key,uint8_t &Flags, uint8_t const Flag) const;
82 bool FindFlag(Key key,unsigned long &Flags, unsigned long Flag) const;
83 bool Exists(Key key) const;
84 APT::StringView Find(Key key) const;
85 APT::StringView FindRaw(Key key) const;
86#endif
87
88 bool Find(APT::StringView Tag,const char *&Start, const char *&End) const;
89 bool Find(APT::StringView Tag,unsigned int &Pos) const;
90 APT::StringView Find(APT::StringView Tag) const;
91 APT::StringView FindRaw(APT::StringView Tag) const;
92 signed int FindI(APT::StringView Tag,signed long Default = 0) const;
93 bool FindB(APT::StringView, bool Default = false) const;
94 unsigned long long FindULL(APT::StringView Tag, unsigned long long const &Default = 0) const;
95
96 bool FindFlag(APT::StringView Tag,uint8_t &Flags,
97 uint8_t const Flag) const;
98 bool FindFlag(APT::StringView Tag,unsigned long &Flags,
99 unsigned long Flag) const;
100 bool Exists(APT::StringView Tag) const;
101
102 bool static FindFlag(uint8_t &Flags, uint8_t const Flag,
103 const char* const Start, const char* const Stop);
104 bool static FindFlag(unsigned long &Flags, unsigned long Flag,
105 const char* Start, const char* Stop);
106
123 APT_MUSTCHECK bool Scan(const char *Start, unsigned long MaxLength, bool const Restart = true);
124
125 inline unsigned long size() const {return Stop - Section;};
126 void Trim();
127 virtual void TrimRecord(bool BeforeRecord, const char* &End);
128
134 unsigned int Count() const;
135
136 void Get(const char *&Start,const char *&Stop,unsigned int I) const;
137
138 inline void GetSection(const char *&Start,const char *&Stop) const
139 {
140 Start = Section;
141 Stop = this->Stop;
142 };
143
144 pkgTagSection();
145 virtual ~pkgTagSection();
146
147 struct Tag
148 {
149 enum ActionType { REMOVE, RENAME, REWRITE } Action;
150 std::string Name;
151 std::string Data;
152
153#if APT_PKG_ABI > 600
154 static Tag Remove(std::string_view Name);
155 static Tag Rename(std::string_view OldName, std::string_view NewName);
156 static Tag Rewrite(std::string_view Name, std::string_view Data);
157#else
158 static Tag Remove(std::string const &Name);
159 static Tag Rename(std::string const &OldName, std::string const &NewName);
160 static Tag Rewrite(std::string const &Name, std::string const &Data);
161#endif
162 private:
163 Tag(ActionType const Action, std::string_view Name, std::string_view Data) :
164 Action(Action), Name(Name), Data(Data) {}
165 };
166
174 bool Write(FileFd &File, char const * const * const Order = NULL, std::vector<Tag> const &Rewrite = std::vector<Tag>()) const;
175#ifdef APT_COMPILING_APT
176 enum WriteFlags
177 {
178 WRITE_DEFAULT = 0,
179 WRITE_HUMAN = (1 << 0), /* write human readable output, may include highlighting */
180 };
181 bool Write(FileFd &File, WriteFlags flags, char const *const *const Order = NULL, std::vector<Tag> const &Rewrite = std::vector<Tag>()) const;
182#endif
183};
184
185
190 class APT_PUBLIC pkgTagFile
191{
192 pkgTagFilePrivate * const d;
193
194 APT_HIDDEN bool Fill();
195 APT_HIDDEN bool Resize();
196 APT_HIDDEN bool Resize(unsigned long long const newSize);
197
198 public:
199
200 bool Step(pkgTagSection &Section);
201 unsigned long Offset();
202 bool Jump(pkgTagSection &Tag,unsigned long long Offset);
203
204 enum Flags
205 {
206 STRICT = 0,
207 SUPPORT_COMMENTS = 1 << 0,
208 };
209
210 void Init(FileFd * const F, pkgTagFile::Flags const Flags, unsigned long long Size = APT_BUFFER_SIZE);
211 void Init(FileFd * const F,unsigned long long const Size = APT_BUFFER_SIZE);
212
213 pkgTagFile(FileFd * const F, pkgTagFile::Flags const Flags, unsigned long long Size = APT_BUFFER_SIZE);
214 pkgTagFile(FileFd * const F,unsigned long long Size = APT_BUFFER_SIZE);
215 virtual ~pkgTagFile();
216};
217
218APT_PUBLIC extern const char **TFRewritePackageOrder;
219APT_PUBLIC extern const char **TFRewriteSourceOrder;
220
221#endif
APT::StringView
Simple subset of std::string_view from C++17.
Definition string_view.h:35
FileFd
Definition fileutl.h:42
pkgTagFilePrivate
Definition tagfile.cc:39
pkgTagFile
Definition tagfile.h:191
pkgTagSectionPrivate
Definition tagfile.cc:86
pkgTagSection
Definition tagfile.h:45
pkgTagSection::Tag
Definition tagfile.h:148