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

libzypp 17.25.10

RepomdFileReader.cc
Go to the documentation of this file.
1 /*---------------------------------------------------------------------\
2 | ____ _ __ __ ___ |
3 | |__ / \ / / . \ . \ |
4 | / / \ V /| _/ _/ |
5 | / /__ | | | | | | |
6 | /_____||_| |_| |_| |
7 | |
8 \---------------------------------------------------------------------*/
12 #include <iostream>
13
14 #include <zypp/base/String.h>
15 #include <zypp/base/Logger.h>
16
17 #include <zypp/Pathname.h>
18 #include <zypp/Date.h>
19 #include <zypp/CheckSum.h>
20 #include <zypp/parser/xml/Reader.h>
21
22 #include <zypp/parser/yum/RepomdFileReader.h>
23
24 #undef ZYPP_BASE_LOGGER_LOGGROUP
25 #define ZYPP_BASE_LOGGER_LOGGROUP "parser::yum"
26
27 using std::endl;
28 using namespace zypp::xml;
29
30 namespace zypp
31 {
32 namespace parser
33 {
34 namespace yum
35 {
36
37
39 //
40 // CLASS NAME : RepomdFileReader::Impl
41 //
42 class RepomdFileReader::Impl : private base::NonCopyable
43 {
44 public:
46 Impl(const Pathname &repomd_file, const ProcessResource & callback )
47 : _callback( callback )
48 {
49 Reader reader( repomd_file );
50 MIL << "Reading " << repomd_file << endl;
51 reader.foreachNode( bind( &RepomdFileReader::Impl::consumeNode, this, _1 ) );
52 }
53
57 bool consumeNode( Reader & reader_r );
58
59 private:
61 CheckSum getChecksum( Reader & reader_r )
62 { return CheckSum( reader_r->getAttribute("type").asString(), reader_r.nodeText().asString() ); }
63
65 ByteCount getSize( Reader & reader_r )
66 { return ByteCount( str::strtonum<ByteCount::SizeType>( reader_r.nodeText().asString() ) ); }
67
68
69 private:
71 ProcessResource _callback;
72
74 std::string _typeStr;
75
77 OnMediaLocation _location;
78 };
80
81 /*
82 * xpath and multiplicity of processed nodes are included in the code
83 * for convenience:
84 *
85 * // xpath: <xpath> (?|*|+)
86 *
87 * if multiplicity is ommited, then the node has multiplicity 'one'.
88 */
89
90 // --------------------------------------------------------------------------
91
92 bool RepomdFileReader::Impl::consumeNode( Reader & reader_r )
93 {
94 if ( reader_r->nodeType() == XML_READER_TYPE_ELEMENT )
95 {
96 // xpath: /repomd
97 if ( reader_r->name() == "repomd" )
98 {
99 return true;
100 }
101
102 // xpath: /repomd/data (+)
103 if ( reader_r->name() == "data" )
104 {
105 _typeStr = reader_r->getAttribute("type").asString();
106 return true;
107 }
108
109 // xpath: /repomd/location
110 if ( reader_r->name() == "location" )
111 {
112 _location.setLocation( reader_r->getAttribute("href").asString(), 1 );
113 // ignoring attribute xml:base
114 return true;
115 }
116
117 // xpath: /repomd/checksum
118 if ( reader_r->name() == "checksum" )
119 {
120 _location.setChecksum( getChecksum( reader_r ) );
121 return true;
122 }
123
124 // xpath: /repomd/header-checksum
125 if ( reader_r->name() == "header-checksum" )
126 {
127 _location.setHeaderChecksum( getChecksum( reader_r ) );
128 return true;
129 }
130
131 // xpath: /repomd/timestamp
132 if ( reader_r->name() == "timestamp" )
133 {
134 // ignore it
135 return true;
136 }
137
138 // xpath: /repomd/size
139 if ( reader_r->name() == "size" )
140 {
141 _location.setDownloadSize( getSize( reader_r ) );
142 return true;
143 }
144
145 // xpath: /repomd/header-size
146 if ( reader_r->name() == "header-size" )
147 {
148 _location.setHeaderSize( getSize( reader_r ) );
149 return true;
150 }
151
153 }
154
155 else if ( reader_r->nodeType() == XML_READER_TYPE_END_ELEMENT )
156 {
157 // xpath: /repomd/data
158 if ( reader_r->name() == "data" )
159 {
160 if (_callback) {
161 _callback( std::move(_location), _typeStr );
162 _location = OnMediaLocation();
163 _typeStr.clear();
164 }
165 return true;
166 }
167 }
168
169 return true;
170 }
171
172
174 //
175 // CLASS NAME : RepomdFileReader
176 //
178
179 RepomdFileReader::RepomdFileReader( const Pathname & repomd_file, const ProcessResource & callback )
180 : _pimpl( new Impl(repomd_file, callback) )
181 {}
182
183 RepomdFileReader::~RepomdFileReader()
184 {}
185
186
187 } // ns yum
188 } // ns parser
189 } // ns zypp
190
191 // vim: set ts=2 sts=2 sw=2 et ai:
CheckSum.h
Date.h
Logger.h
MIL
#define MIL
Definition: Logger.h:91
Pathname.h
Reader.h
RepomdFileReader.h
Interface of repomd.xml file reader.
String.h
zypp::ByteCount
Store and operate with byte count.
Definition: ByteCount.h:31
zypp::CheckSum
Definition: CheckSum.h:34
zypp::OnMediaLocation
Describes a resource file located on a medium.
Definition: OnMediaLocation.h:39
zypp::OnMediaLocation::setDownloadSize
OnMediaLocation & setDownloadSize(ByteCount val_r)
Set the downloadSize.
Definition: OnMediaLocation.cc:115
zypp::OnMediaLocation::setChecksum
OnMediaLocation & setChecksum(CheckSum val_r)
Set the checksum.
Definition: OnMediaLocation.cc:121
zypp::OnMediaLocation::setHeaderSize
OnMediaLocation & setHeaderSize(ByteCount val_r)
Set the headerSize.
Definition: OnMediaLocation.cc:139
zypp::OnMediaLocation::setLocation
OnMediaLocation & setLocation(Pathname filename_r, unsigned medianr_r=1)
Set filename_r and medianr_r (defaults to 1).
Definition: OnMediaLocation.cc:91
zypp::OnMediaLocation::setHeaderChecksum
OnMediaLocation & setHeaderChecksum(CheckSum val_r)
Set the headerChecksum.
Definition: OnMediaLocation.cc:145
zypp::filesystem::Pathname
Pathname.
Definition: Pathname.h:45
zypp::parser::yum::RepomdFileReader::Impl
Definition: RepomdFileReader.cc:43
zypp::parser::yum::RepomdFileReader::Impl::getChecksum
CheckSum getChecksum(Reader &reader_r)
Retrieve a checksum node.
Definition: RepomdFileReader.cc:61
zypp::parser::yum::RepomdFileReader::Impl::consumeNode
bool consumeNode(Reader &reader_r)
Callback provided to the XML parser.
Definition: RepomdFileReader.cc:92
zypp::parser::yum::RepomdFileReader::Impl::Impl
Impl(const Pathname &repomd_file, const ProcessResource &callback)
Ctro taking a ProcessResource callback.
Definition: RepomdFileReader.cc:46
zypp::parser::yum::RepomdFileReader::Impl::getSize
ByteCount getSize(Reader &reader_r)
Retrieve a size node.
Definition: RepomdFileReader.cc:65
zypp::parser::yum::RepomdFileReader::Impl::_typeStr
std::string _typeStr
The resource type string.
Definition: RepomdFileReader.cc:74
zypp::parser::yum::RepomdFileReader::Impl::_location
OnMediaLocation _location
Location of metadata file.
Definition: RepomdFileReader.cc:77
zypp::parser::yum::RepomdFileReader::Impl::_callback
ProcessResource _callback
Function for processing collected data.
Definition: RepomdFileReader.cc:71
zypp::parser::yum::RepomdFileReader::ProcessResource
function< bool(OnMediaLocation &&, const std::string &)> ProcessResource
Callback taking OnMediaLocation and the resource type string.
Definition: RepomdFileReader.h:40
zypp::parser::yum::RepomdFileReader::_pimpl
RW_pointer< Impl, rw_pointer::Scoped< Impl > > _pimpl
Definition: RepomdFileReader.h:56
zypp::parser::yum::RepomdFileReader::~RepomdFileReader
~RepomdFileReader()
DTOR.
Definition: RepomdFileReader.cc:183
zypp::parser::yum::RepomdFileReader::RepomdFileReader
RepomdFileReader(const Pathname &repomd_file, const ProcessResource &callback)
CTOR.
Definition: RepomdFileReader.cc:179
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:140
zypp::xml::Reader::foreachNode
bool foreachNode(ProcessNode fnc_r)
Definition: Reader.h:144
zypp::xml::XmlString::asString
std::string asString() const
Explicit conversion to std::string.
Definition: XmlString.h:77
zypp::base::NonCopyable
boost::noncopyable NonCopyable
Ensure derived classes cannot be copied.
Definition: NonCopyable.h:26
zypp::xml
Definition: libxmlfwd.cc:22
zypp
Easy-to use interface to the ZYPP dependency resolver.
Definition: CodePitfalls.doc:2