libzypp: zypp/parser/yum/RepomdFileReader.cc Source File

libzypp 17.36.6

RepomdFileReader.cc
Go to the documentation of this file.
1/*---------------------------------------------------------------------\
2| ____ _ __ __ ___ |
3| |__ / \ / / . \ . \ |
4| / / \ V /| _/ _/ |
5| / /__ | | | | | | |
6| /_____||_| |_| |_| |
7| |
8\---------------------------------------------------------------------*/
12#include <iostream>
13#include <utility>
14
15#include <zypp/base/String.h>
16#include <zypp/base/Logger.h>
17#include <zypp/base/Regex.h>
18
19#include <zypp/Pathname.h>
20#include <zypp/Date.h>
21#include <zypp/Url.h>
22#include <zypp/CheckSum.h>
23#include <zypp/parser/xml/Reader.h>
24
25#include <zypp/parser/yum/RepomdFileReader.h>
26
27#undef ZYPP_BASE_LOGGER_LOGGROUP
28#define ZYPP_BASE_LOGGER_LOGGROUP "parser::yum"
29
30using std::endl;
31using namespace zypp::xml;
32
33namespace zypp
34{
35 namespace parser
36 {
37 namespace yum
38 {
39
40
42 //
43 // CLASS NAME : RepomdFileReader::Impl
44 //
45 class RepomdFileReader::Impl : private base::NonCopyable
46 {
47 public:
49 Impl(const Pathname &repomd_file, ProcessResource &&callback )
50 : _callback( std::move(callback) )
51 {
52 Reader reader( repomd_file );
53 MIL << "Reading " << repomd_file << endl;
54 reader.foreachNode( bind( &RepomdFileReader::Impl::consumeNode, this, _1 ) );
55 }
56
60 bool consumeNode( Reader & reader_r );
61
62
64 const std::set<std::string> & keywords() const
65 { return _keywords; }
66
67 private:
69 CheckSum getChecksum( Reader & reader_r )
70 { return CheckSum( reader_r->getAttribute("type").asString(), reader_r.nodeText().asString() ); }
71
73 ByteCount getSize( Reader & reader_r )
74 { return ByteCount( str::strtonum<ByteCount::SizeType>( reader_r.nodeText().asString() ) ); }
75
76
77 private:
79 ProcessResource _callback;
80
82 std::string _typeStr;
83
85 OnMediaLocation _location;
86
87 std::set<std::string> _keywords;
88 };
89
90
91 /*
92 * xpath and multiplicity of processed nodes are included in the code
93 * for convenience:
94 *
95 * // xpath: <xpath> (?|*|+)
96 *
97 * if multiplicity is ommited, then the node has multiplicity 'one'.
98 */
99
100 // --------------------------------------------------------------------------
101
102 bool RepomdFileReader::Impl::consumeNode( Reader & reader_r )
103 {
104 if ( reader_r->nodeType() == XML_READER_TYPE_ELEMENT )
105 {
106 // xpath: /repomd
107 if ( reader_r->name() == "repomd" )
108 {
109 return true;
110 }
111
112 // xpath: /repomd/data (+)
113 if ( reader_r->name() == "data" )
114 {
115 _typeStr = reader_r->getAttribute("type").asString();
116 return true;
117 }
118
119 // xpath: /repomd/location
120 if ( reader_r->name() == "location" )
121 {
122 _location.setLocation( reader_r->getAttribute("href").asString(), 1 );
123 // ignoring attribute xml:base
124 return true;
125 }
126
127 // xpath: /repomd/checksum
128 if ( reader_r->name() == "checksum" )
129 {
130 _location.setChecksum( getChecksum( reader_r ) );
131 return true;
132 }
133
134 // xpath: /repomd/header-checksum
135 if ( reader_r->name() == "header-checksum" )
136 {
137 _location.setHeaderChecksum( getChecksum( reader_r ) );
138 return true;
139 }
140
141 // xpath: /repomd/timestamp
142 if ( reader_r->name() == "timestamp" )
143 {
144 // ignore it
145 return true;
146 }
147
148 // xpath: /repomd/size
149 if ( reader_r->name() == "size" )
150 {
151 _location.setDownloadSize( getSize( reader_r ) );
152 return true;
153 }
154
155 // xpath: /repomd/header-size
156 if ( reader_r->name() == "header-size" )
157 {
158 _location.setHeaderSize( getSize( reader_r ) );
159 return true;
160 }
161
162 // xpath: /tags/content
163 if ( reader_r->name() == "content" )
164 {
165 const auto & tag = reader_r.nodeText();
166 if ( tag.c_str() && *tag.c_str() )
167 _keywords.insert( tag.asString() ); // remember keyword
168 return true;
169 }
170 }
171
172 else if ( reader_r->nodeType() == XML_READER_TYPE_END_ELEMENT )
173 {
174 // xpath: /repomd/data
175 if ( reader_r->name() == "data" )
176 {
177 if (_callback) {
178 _callback( std::move(_location), _typeStr );
179 _location = OnMediaLocation();
180 _typeStr.clear();
181 }
182 return true;
183 }
184 }
185
186 return true;
187 }
188
189
191 //
192 // CLASS NAME : RepomdFileReader
193 //
195
196 RepomdFileReader::RepomdFileReader( const Pathname & repomd_file, ProcessResource callback )
197 : _pimpl( new Impl(repomd_file, std::move(callback)) )
198 {}
199
200 RepomdFileReader::RepomdFileReader( const Pathname & repomd_file )
201 : _pimpl( new Impl(repomd_file, ProcessResource()) )
202 {}
203
204 RepomdFileReader::~RepomdFileReader()
205 {}
206
207 const std::set<std::string> & RepomdFileReader::keywords() const
208 { return _pimpl->keywords(); }
209
210 std::vector<std::pair<std::string,std::string>> RepomdFileReader::keyhints() const
211 {
212 std::vector<std::pair<std::string,std::string>> ret;
213 for ( const std::string & tag : keywords() ) {
214 // Get keyhints on the fly:
215 // gpg-pubkey-39db7c82-5847eb1f.asc?fpr=22C07BA534178CD02EFE22AAB88B2FD43DBDC284
216 // Fingerprint is explicitly mentioned or id/fpr can be derived from the filename
217 if ( tag.compare( 0,10,"gpg-pubkey" ) != 0 )
218 continue;
219
220 static const str::regex rx( "^(gpg-pubkey([^?]*))(\\?fpr=([[:xdigit:]]{8,}))?$" );
221 str::smatch what;
222 if ( str::regex_match( tag.c_str(), what, rx ) ) {
223 std::string keyfile { what[1] };
224 std::string keyident;
225 if ( what.size(4) != std::string::npos ) { // with fpr=
226 keyident = what[4];
227 }
228 else {
229 static const str::regex rx( /*gpg-pubkey*/"^-([[:xdigit:]]{8,})" );
230 if ( str::regex_match( what[2], what, rx ) ) {
231 keyident = what[1];
232 }
233 else {
234 DBG << "Tag " << tag << " does not contain a keyident. ignore it." << endl;
235 continue;
236 }
237 }
238 ret.push_back( std::make_pair( std::move(keyfile), std::move(keyident) ) );
239 }
240 }
241 return ret;
242 }
243
244 } // ns yum
245 } // ns parser
246} // ns zypp
247
248// vim: set ts=2 sts=2 sw=2 et ai:
Reader.h
RepomdFileReader.h
Interface of repomd.xml file reader.
zypp::ByteCount
Store and operate with byte count.
Definition ByteCount.h:32
zypp::CheckSum
Definition CheckSum.h:34
zypp::OnMediaLocation
Describes a resource file located on a medium.
Definition onmedialocation.h:37
zypp::filesystem::Pathname
Pathname.
Definition Pathname.h:47
zypp::parser::yum::RepomdFileReader::Impl
Definition RepomdFileReader.cc:46
zypp::parser::yum::RepomdFileReader::Impl::getChecksum
CheckSum getChecksum(Reader &reader_r)
Retrieve a checksum node.
Definition RepomdFileReader.cc:69
zypp::parser::yum::RepomdFileReader::Impl::consumeNode
bool consumeNode(Reader &reader_r)
Callback provided to the XML parser.
Definition RepomdFileReader.cc:102
zypp::parser::yum::RepomdFileReader::Impl::keywords
const std::set< std::string > & keywords() const
repo keywords parsed on the fly
Definition RepomdFileReader.cc:64
zypp::parser::yum::RepomdFileReader::Impl::getSize
ByteCount getSize(Reader &reader_r)
Retrieve a size node.
Definition RepomdFileReader.cc:73
zypp::parser::yum::RepomdFileReader::Impl::_keywords
std::set< std::string > _keywords
repo keywords parsed on the fly
Definition RepomdFileReader.cc:87
zypp::parser::yum::RepomdFileReader::Impl::_typeStr
std::string _typeStr
The resource type string.
Definition RepomdFileReader.cc:82
zypp::parser::yum::RepomdFileReader::Impl::_location
OnMediaLocation _location
Location of metadata file.
Definition RepomdFileReader.cc:85
zypp::parser::yum::RepomdFileReader::Impl::_callback
ProcessResource _callback
Function for processing collected data.
Definition RepomdFileReader.cc:79
zypp::parser::yum::RepomdFileReader::Impl::Impl
Impl(const Pathname &repomd_file, ProcessResource &&callback)
Ctro taking a ProcessResource callback.
Definition RepomdFileReader.cc:49
zypp::parser::yum::RepomdFileReader::_pimpl
RW_pointer< Impl, rw_pointer::Scoped< Impl > > _pimpl
Definition RepomdFileReader.h:68
zypp::parser::yum::RepomdFileReader::ProcessResource
function< bool(OnMediaLocation &&, const std::string &)> ProcessResource
Callback taking OnMediaLocation and the resource type string.
Definition RepomdFileReader.h:42
zypp::parser::yum::RepomdFileReader::RepomdFileReader
RepomdFileReader(const Pathname &repomd_file, ProcessResource callback)
CTOR.
Definition RepomdFileReader.cc:196
zypp::parser::yum::RepomdFileReader::~RepomdFileReader
~RepomdFileReader()
DTOR.
Definition RepomdFileReader.cc:204
zypp::parser::yum::RepomdFileReader::keywords
const std::set< std::string > & keywords() const
repo keywords parsed on the fly
Definition RepomdFileReader.cc:207
zypp::parser::yum::RepomdFileReader::keyhints
std::vector< std::pair< std::string, std::string > > keyhints() const
gpg key hits shipped in keywords (bsc#1184326)
Definition RepomdFileReader.cc:210
zypp::str::regex
Regular expression.
Definition Regex.h:95
zypp::str::smatch
Regular expression match result.
Definition Regex.h:168
zypp::str::smatch::size
unsigned size() const
Definition Regex.cc:106
zypp::xml::Node::nodeType
NodeType nodeType() const
Get the node type of the current node.
Definition Node.h:126
zypp::xml::Node::getAttribute
XmlString getAttribute(const char *name_r) const
Provides a copy of the attribute value with the specified qualified name.
Definition Node.h:71
zypp::xml::Node::name
XmlString name() const
The qualified name of the node, equal to Prefix :LocalName.
Definition Node.h:118
zypp::xml::Reader
xmlTextReader based interface to iterate xml streams.
Definition Reader.h:96
zypp::xml::Reader::nodeText
XmlString nodeText()
If the current node is not empty, advances the reader to the next node, and returns the value.
Definition Reader.cc:122
zypp::xml::Reader::foreachNode
bool foreachNode(const ProcessNode &fnc_r)
Definition Reader.h:144
zypp::xml::XmlString::asString
std::string asString() const
Explicit conversion to std::string.
Definition XmlString.h:77
std
Definition Arch.h:364
zypp::base::NonCopyable
boost::noncopyable NonCopyable
Ensure derived classes cannot be copied.
Definition NonCopyable.h:26
zypp::callback
Callbacks light.
Definition Callback.h:146
zypp::parser::yum
Definition RepomdFileReader.cc:38
zypp::parser
Definition HistoryLogReader.cc:30
zypp::str::regex_match
bool regex_match(const std::string &s, smatch &matches, const regex &regex)
\relates regex \ingroup ZYPP_STR_REGEX \relates regex \ingroup ZYPP_STR_REGEX
Definition Regex.h:70
zypp::str::strtonum
TInt strtonum(const C_Str &str)
Parsing numbers from string.
zypp::xml
Definition libxmlfwd.cc:22
zypp
Easy-to use interface to the ZYPP dependency resolver.
Definition Application.cc:20
DBG
#define DBG
Definition Logger.h:99
MIL
#define MIL
Definition Logger.h:100
CheckSum.h
Date.h
Pathname.h
Url.h
Logger.h
Regex.h
String.h