apt 3.2.0 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 <memory>
29#include <string>
30#include <string_view>
31#include <vector>
32
33
34 class FileFd;
35 class pkgTagSectionPrivate;
36 class pkgTagFilePrivate;
37
45 class APT_PUBLIC pkgTagSection
46{
47 const char *Section;
48 unsigned int AlphaIndexes[128];
49 unsigned int BetaIndexes[128];
50
51 std::unique_ptr<pkgTagSectionPrivate> const d;
52
53 APT_HIDDEN bool FindInternal(unsigned int Pos,const char *&Start, const char *&End) const;
54 APT_HIDDEN std::string_view FindInternal(unsigned int Pos) const;
55 APT_HIDDEN std::string_view FindRawInternal(unsigned int Pos) const;
56 APT_HIDDEN signed int FindIInternal(unsigned int Pos,signed long Default = 0) const;
57 APT_HIDDEN bool FindBInternal(unsigned int Pos, bool Default = false) const;
58 APT_HIDDEN unsigned long long FindULLInternal(unsigned int Pos, unsigned long long const &Default = 0) const;
59 APT_HIDDEN bool FindFlagInternal(unsigned int Pos,uint8_t &Flags, uint8_t const Flag) const;
60 APT_HIDDEN bool FindFlagInternal(unsigned int Pos,unsigned long &Flags, unsigned long Flag) const;
61
62 protected:
63 const char *Stop;
64
65 public:
66
67 inline bool operator ==(const pkgTagSection &rhs) {return Section == rhs.Section;};
68 inline bool operator !=(const pkgTagSection &rhs) {return Section != rhs.Section;};
69
70 // TODO: Remove internally
71 std::string FindS(std::string_view sv) const { return std::string{Find(sv)}; }
72 std::string FindRawS(std::string_view sv) const { return std::string{FindRaw(sv)}; };
73
74 // Functions for lookup with a perfect hash function
75 enum class Key;
76#ifdef APT_COMPILING_APT
77 bool Find(Key key,const char *&Start, const char *&End) const;
78 bool Find(Key key,unsigned int &Pos) const;
79 signed int FindI(Key key,signed long Default = 0) const;
80 bool FindB(Key key, bool Default = false) const;
81 unsigned long long FindULL(Key key, unsigned long long const &Default = 0) const;
82 bool FindFlag(Key key,uint8_t &Flags, uint8_t const Flag) const;
83 bool FindFlag(Key key,unsigned long &Flags, unsigned long Flag) const;
84 bool Exists(Key key) const;
85 std::string_view Find(Key key) const;
86 std::string_view FindRaw(Key key) const;
87#endif
88
89 bool Find(std::string_view Tag,const char *&Start, const char *&End) const;
90 bool Find(std::string_view Tag,unsigned int &Pos) const;
91 std::string_view Find(std::string_view Tag) const;
92 std::string_view FindRaw(std::string_view Tag) const;
93 signed int FindI(std::string_view Tag,signed long Default = 0) const;
94 bool FindB(std::string_view, bool Default = false) const;
95 unsigned long long FindULL(std::string_view Tag, unsigned long long const &Default = 0) const;
96
97 bool FindFlag(std::string_view Tag,uint8_t &Flags,
98 uint8_t const Flag) const;
99 bool FindFlag(std::string_view Tag,unsigned long &Flags,
100 unsigned long Flag) const;
101 bool Exists(std::string_view Tag) const;
102
103 bool static FindFlag(uint8_t &Flags, uint8_t const Flag,
104 const char* const Start, const char* const Stop);
105 bool static FindFlag(unsigned long &Flags, unsigned long Flag,
106 const char* Start, const char* Stop);
107
124 [[nodiscard]] bool Scan(const char *Start, unsigned long MaxLength, bool const Restart = true);
125
126 inline unsigned long size() const {return Stop - Section;};
127 void Trim();
128 virtual void TrimRecord(bool BeforeRecord, const char* &End);
129
135 unsigned int Count() const;
136
137 void Get(const char *&Start,const char *&Stop,unsigned int I) const;
138
139 inline void GetSection(const char *&Start,const char *&Stop) const
140 {
141 Start = Section;
142 Stop = this->Stop;
143 };
144
145 pkgTagSection();
146 virtual ~pkgTagSection();
147
148 struct Tag
149 {
150 enum ActionType { REMOVE, RENAME, REWRITE } Action;
151 std::string Name;
152 std::string Data;
153
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 private:
158 Tag(ActionType const Action, std::string_view Name, std::string_view Data) :
159 Action(Action), Name(Name), Data(Data) {}
160 };
161
169 bool Write(FileFd &File, char const * const * const Order = NULL, std::vector<Tag> const &Rewrite = std::vector<Tag>()) const;
170#ifdef APT_COMPILING_APT
171 enum WriteFlags
172 {
173 WRITE_DEFAULT = 0,
174 WRITE_HUMAN = (1 << 0), /* write human readable output, may include highlighting */
175 };
176 bool Write(FileFd &File, WriteFlags flags, char const *const *const Order = NULL, std::vector<Tag> const &Rewrite = std::vector<Tag>()) const;
177#endif
178};
179
180
185 class APT_PUBLIC pkgTagFile
186{
187 std::unique_ptr<pkgTagFilePrivate> const d;
188
189 APT_HIDDEN bool Fill();
190 APT_HIDDEN bool Resize();
191 APT_HIDDEN bool Resize(unsigned long long const newSize);
192
193 public:
194
195 bool Step(pkgTagSection &Section);
196 unsigned long Offset();
197 bool Jump(pkgTagSection &Tag,unsigned long long Offset);
198
199 enum Flags
200 {
201 STRICT = 0,
202 SUPPORT_COMMENTS = 1 << 0,
203 };
204
205 void Init(FileFd * const F, pkgTagFile::Flags const Flags, unsigned long long Size = APT_BUFFER_SIZE);
206 void Init(FileFd * const F,unsigned long long const Size = APT_BUFFER_SIZE);
207
208 pkgTagFile(FileFd * const F, pkgTagFile::Flags const Flags, unsigned long long Size = APT_BUFFER_SIZE);
209 pkgTagFile(FileFd * const F,unsigned long long Size = APT_BUFFER_SIZE);
210 virtual ~pkgTagFile();
211};
212
213APT_PUBLIC extern const char **TFRewritePackageOrder;
214APT_PUBLIC extern const char **TFRewriteSourceOrder;
215
216#endif
FileFd
Definition fileutl.h:43
pkgTagFilePrivate
Definition tagfile.cc:38
pkgTagSectionPrivate
Definition tagfile.cc:88
pkgTagSection
Definition tagfile.h:46
pkgTagSection::Scan
bool Scan(const char *Start, unsigned long MaxLength, bool const Restart=true)
searches the boundaries of the current section
Definition tagfile.cc:490
pkgTagSection::Write
bool Write(FileFd &File, char const *const *const Order=NULL, std::vector< Tag > const &Rewrite=std::vector< Tag >()) const
Definition tagfile.cc:1014
pkgTagSection::Count
unsigned int Count() const
amount of Tags in the current section
Definition tagfile.cc:953
pkgTagSection::Tag
Definition tagfile.h:149