apt  2.2.4
About: Apt (Advanced Package Tool) is a management system for software packages (Debian/Ubuntu). Release series 2.2.
  Fossies Dox: apt-2.2.4.tar.gz  ("unofficial" and yet experimental doxygen-generated source code documentation)  

tagfile.h
Go to the documentation of this file.
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 <stdint.h>
25 #include <stdio.h>
26 
27 #include <list>
28 #include <string>
29 #include <vector>
30 #include <apt-pkg/string_view.h>
31 
32 
33 class FileFd;
35 class pkgTagFilePrivate;
36 
37 /** \class pkgTagSection parses a single deb822 stanza and provides various Find methods
38  * to extract the included values. It can also be used to modify and write a
39  * valid deb822 stanza optionally (re)ordering the fields inside the stanza.
40  *
41  * Beware: This class does \b NOT support (#-)comments in in- or output!
42  * If the input contains comments they have to be stripped first like pkgTagFile
43  * does with SUPPORT_COMMENTS flag set. */
45 {
46  const char *Section;
47  unsigned int AlphaIndexes[128];
48  unsigned int BetaIndexes[128];
49 
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 Find(sv).to_string(); }
71  std::string FindRawS(APT::StringView sv) const { return FindRaw(sv).to_string(); };
72 
73  // Functions for lookup with a perfect hash function
74  enum class Key;
75  APT_HIDDEN bool Find(Key key,const char *&Start, const char *&End) const;
76  APT_HIDDEN bool Find(Key key,unsigned int &Pos) const;
77  APT_HIDDEN signed int FindI(Key key,signed long Default = 0) const;
78  APT_HIDDEN bool FindB(Key key, bool Default = false) const;
79  APT_HIDDEN unsigned long long FindULL(Key key, unsigned long long const &Default = 0) const;
80  APT_HIDDEN bool FindFlag(Key key,uint8_t &Flags, uint8_t const Flag) const;
81  APT_HIDDEN bool FindFlag(Key key,unsigned long &Flags, unsigned long Flag) const;
82  APT_HIDDEN bool Exists(Key key) const;
83  APT_HIDDEN APT::StringView Find(Key key) const;
84  APT_HIDDEN APT::StringView FindRaw(Key key) const;
85  bool Find(APT::StringView Tag,const char *&Start, const char *&End) const;
86  bool Find(APT::StringView Tag,unsigned int &Pos) const;
88  APT::StringView FindRaw(APT::StringView Tag) const;
89  signed int FindI(APT::StringView Tag,signed long Default = 0) const;
90  bool FindB(APT::StringView, bool Default = false) const;
91  unsigned long long FindULL(APT::StringView Tag, unsigned long long const &Default = 0) const;
92 
93  bool FindFlag(APT::StringView Tag,uint8_t &Flags,
94  uint8_t const Flag) const;
95  bool FindFlag(APT::StringView Tag,unsigned long &Flags,
96  unsigned long Flag) const;
97  bool Exists(APT::StringView Tag) const;
98 
99  bool static FindFlag(uint8_t &Flags, uint8_t const Flag,
100  const char* const Start, const char* const Stop);
101  bool static FindFlag(unsigned long &Flags, unsigned long Flag,
102  const char* Start, const char* Stop);
103 
104  /** \brief searches the boundaries of the current section
105  *
106  * While parameter Start marks the beginning of the section, this method
107  * will search for the first double newline in the data stream which marks
108  * the end of the section. It also does a first pass over the content of
109  * the section parsing it as encountered for processing later on by Find
110  *
111  * @param Start is the beginning of the section
112  * @param MaxLength is the size of valid data in the stream pointed to by Start
113  * @param Restart if enabled internal state will be cleared, otherwise it is
114  * assumed that now more data is available in the stream and the parsing will
115  * start were it encountered insufficient data the last time.
116  *
117  * @return \b true if section end was found, \b false otherwise.
118  * Beware that internal state will be inconsistent if \b false is returned!
119  */
120  APT_MUSTCHECK bool Scan(const char *Start, unsigned long MaxLength, bool const Restart = true);
121 
122  inline unsigned long size() const {return Stop - Section;};
123  void Trim();
124  virtual void TrimRecord(bool BeforeRecord, const char* &End);
125 
126  /** \brief amount of Tags in the current section
127  *
128  * Note: if a Tag is mentioned repeatedly it will be counted multiple
129  * times, but only the last occurrence is available via Find methods.
130  */
131  unsigned int Count() const;
132 
133  void Get(const char *&Start,const char *&Stop,unsigned int I) const;
134 
135  inline void GetSection(const char *&Start,const char *&Stop) const
136  {
137  Start = Section;
138  Stop = this->Stop;
139  };
140 
141  pkgTagSection();
142  virtual ~~pkgTagSection();
143 
144  struct Tag
145  {
146  enum ActionType { REMOVE, RENAME, REWRITE } Action;
147  std::string Name;
148  std::string Data;
149 
150  static Tag Remove(std::string const &Name);
151  static Tag Rename(std::string const &OldName, std::string const &NewName);
152  static Tag Rewrite(std::string const &Name, std::string const &Data);
153  private:
154  Tag(ActionType const Action, std::string const &Name, std::string const &Data) :
155  Action(Action), Name(Name), Data(Data) {}
156  };
157 
158  /** Write this section (with optional rewrites) to a file
159  *
160  * @param File to write the section to
161  * @param Order in which tags should appear in the file
162  * @param Rewrite is a set of tags to be renamed, rewritten and/or removed
163  * @return \b true if successful, otherwise \b false
164  */
165  bool Write(FileFd &File, char const * const * const Order = NULL, std::vector<Tag> const &Rewrite = std::vector<Tag>()) const;
166 };
167 
168 
169 /** \class pkgTagFile reads and prepares a deb822 formatted file for parsing
170  * via #pkgTagSection. The default mode tries to be as fast as possible and
171  * assumes perfectly valid (machine generated) files like Packages. Support
172  * for comments e.g. needs to be enabled explicitly. */
174 {
176 
177  APT_HIDDEN bool Fill();
178  APT_HIDDEN bool Resize();
179  APT_HIDDEN bool Resize(unsigned long long const newSize);
180 
181 public:
182 
183  bool Step(pkgTagSection &Section);
184  unsigned long Offset();
185  bool Jump(pkgTagSection &Tag,unsigned long long Offset);
186 
187  enum Flags
188  {
189  STRICT = 0,
190  SUPPORT_COMMENTS = 1 << 0,
191  };
192 
193  void Init(FileFd * const F, pkgTagFile::Flags const Flags, unsigned long long Size = 32*1024);
194  void Init(FileFd * const F,unsigned long long const Size = 32*1024);
195 
196  pkgTagFile(FileFd * const F, pkgTagFile::Flags const Flags, unsigned long long Size = 32*1024);
197  pkgTagFile(FileFd * const F,unsigned long long Size = 32*1024);
198  virtual ~~pkgTagFile();
199 };
200 
201 APT_PUBLIC extern const char **TFRewritePackageOrder;
202 APT_PUBLIC extern const char **TFRewriteSourceOrder;
203 
204 #endif
static bool std::string const metaIndex const *const pkgAcqMetaClearSig *const pkgAcquire::Item *const I
static bool operator!=(LineBuffer const &buf, APT::StringView const exp) noexcept
Definition: gpgv.cc:108
static bool operator==(LineBuffer const &buf, APT::StringView const exp) noexcept
Definition: gpgv.cc:104
Simple subset of std::string_view from C++17.
Definition: string_view.h:27
Definition: fileutl.h:39
pkgTagFilePrivate *const d
Definition: tagfile.h:175
void GetSection(const char *&Start, const char *&Stop) const
Definition: tagfile.h:135
std::string FindS(APT::StringView sv) const
Definition: tagfile.h:70
APT_HIDDEN bool Exists(Key key) const
const char * Section
Definition: tagfile.h:46
const char * Stop
Definition: tagfile.h:62
std::string FindRawS(APT::StringView sv) const
Definition: tagfile.h:71
pkgTagSectionPrivate *const d
Definition: tagfile.h:50
APT_HIDDEN APT::StringView FindInternal(unsigned int Pos) const
unsigned long size() const
Definition: tagfile.h:122
bool Rename(std::string From, std::string To)
Definition: fileutl.cc:3187
#define APT_MUSTCHECK
Definition: macros.h:68
#define APT_PUBLIC
Definition: macros.h:77
#define APT_HIDDEN
Definition: macros.h:78
std::string Data
Definition: tagfile.h:148
Tag(ActionType const Action, std::string const &Name, std::string const &Data)
Definition: tagfile.h:154
std::string Name
Definition: tagfile.h:147
APT_PUBLIC const char ** TFRewriteSourceOrder
APT_PUBLIC const char ** TFRewritePackageOrder