libzypp: zypp/parser/RepoindexFileReader.cc Source File

libzypp 17.36.6

RepoindexFileReader.cc
Go to the documentation of this file.
1/*---------------------------------------------------------------------\
2| ____ _ __ __ ___ |
3| |__ / \ / / . \ . \ |
4| / / \ V /| _/ _/ |
5| / /__ | | | | | | |
6| /_____||_| |_| |_| |
7| |
8\---------------------------------------------------------------------*/
12#include <iostream>
13#include <unordered_map>
14
15#include <zypp/base/String.h>
16#include <zypp/base/Logger.h>
17#include <zypp/base/Gettext.h>
18#include <utility>
19#include <zypp-core/base/InputStream>
20#include <zypp-core/base/DefaultIntegral>
21
22#include <zypp/Pathname.h>
23
24#include <zypp/parser/xml/Reader.h>
25#include <zypp-core/parser/ParseException>
26
27#include <zypp/RepoInfo.h>
28
29#include <zypp/parser/RepoindexFileReader.h>
30
31
32#undef ZYPP_BASE_LOGGER_LOGGROUP
33#define ZYPP_BASE_LOGGER_LOGGROUP "parser"
34
35using std::endl;
36
37namespace zypp
38{
39 namespace parser
40 {
41 using xml::Reader;
42 using xml::XmlString;
43
45 namespace
46 {
47 class VarReplacer : private base::NonCopyable
48 {
49 public:
51 void setVar( const std::string & key_r, const std::string & val_r )
52 {
53 //MIL << "*** Inject " << key_r << " = " << val_r;
54 _vars[key_r] = replace( val_r );
55 //MIL << " (" << _vars[key_r] << ")" << endl;
56 }
57
58 std::string replace( const std::string & val_r ) const
59 {
60 std::string::size_type vbeg = val_r.find( "%{", 0 );
61 if ( vbeg == std::string::npos )
62 return val_r;
63
64 str::Str ret;
65 std::string::size_type cbeg = 0;
66 for( ; vbeg != std::string::npos; vbeg = val_r.find( "%{", vbeg ) )
67 {
68 std::string::size_type nbeg = vbeg+2;
69 std::string::size_type nend = val_r.find( '}', nbeg );
70 if ( nend == std::string::npos )
71 {
72 WAR << "Incomplete variable in '" << val_r << "'" << endl;
73 break;
74 }
75 const auto & iter = _vars.find( val_r.substr( nbeg, nend-nbeg ) );
76 if ( iter != _vars.end() )
77 {
78 if ( cbeg < vbeg )
79 ret << val_r.substr( cbeg, vbeg-cbeg );
80 ret << iter->second;
81 cbeg = nend+1;
82 }
83 else
84 WAR << "Undefined variable %{" << val_r.substr( nbeg, nend-nbeg ) << "} in '" << val_r << "'" << endl;
85 vbeg = nend+1;
86 }
87 if ( cbeg < val_r.size() )
88 ret << val_r.substr( cbeg );
89
90 return ret;
91 }
92 private:
93 std::unordered_map<std::string,std::string> _vars;
94 };
95 } // namespace
97
99 //
100 // CLASS NAME : RepoindexFileReader::Impl
101 //
102 class RepoindexFileReader::Impl : private base::NonCopyable
103 {
104 public:
110 Impl(const InputStream &is, ProcessResource &&callback);
111
115 bool consumeNode( Reader & reader_r );
116
117 DefaultIntegral<Date::Duration,0> _ttl;
118
119 private:
120 bool getAttrValue( const std::string & key_r, Reader & reader_r, std::string & value_r )
121 {
122 const XmlString & s( reader_r->getAttribute( key_r ) );
123 if ( s.get() )
124 {
125 value_r = _replacer.replace( s.asString() );
126 return !value_r.empty();
127 }
128 value_r.clear();
129 return false;
130 }
131
132 private:
134 ProcessResource _callback;
135 VarReplacer _replacer;
136 };
137
138
139 RepoindexFileReader::Impl::Impl(const InputStream &is,
140 ProcessResource &&callback)
141 : _callback(std::move(callback))
142 {
143 Reader reader( is );
144 MIL << "Reading " << is.path() << endl;
145 reader.foreachNode( bind( &RepoindexFileReader::Impl::consumeNode, this, _1 ) );
146 }
147
148 // --------------------------------------------------------------------------
149
150 /*
151 * xpath and multiplicity of processed nodes are included in the code
152 * for convenience:
153 *
154 * // xpath: <xpath> (?|*|+)
155 *
156 * if multiplicity is ommited, then the node has multiplicity 'one'.
157 */
158
159 // --------------------------------------------------------------------------
160
161 bool RepoindexFileReader::Impl::consumeNode( Reader & reader_r )
162 {
163 if ( reader_r->nodeType() == XML_READER_TYPE_ELEMENT )
164 {
165 // xpath: /repoindex
166 if ( reader_r->name() == "repoindex" )
167 {
168 while ( reader_r.nextNodeAttribute() )
169 {
170 const std::string & name( reader_r->localName().asString() );
171 const std::string & value( reader_r->value().asString() );
172 _replacer.setVar( name, value );
173 // xpath: /repoindex@ttl
174 if ( name == "ttl" )
175 _ttl = str::strtonum<Date::Duration>(value);
176 }
177 return true;
178 }
179
180 // xpath: /repoindex/data (+)
181 if ( reader_r->name() == "repo" )
182 {
183 RepoInfo info;
184 // Set some defaults that are not contained in the repo information
185 info.setAutorefresh( true );
186 info.setEnabled(false);
187
188 std::string attrValue;
189
190 // required alias
191 // mandatory, so we can allow it in var replacement without reset
192 if ( getAttrValue( "alias", reader_r, attrValue ) )
193 {
194 info.setAlias( attrValue );
195 _replacer.setVar( "alias", attrValue );
196 }
197 else
198 throw ParseException(str::form(_("Required attribute '%s' is missing."), "alias"));
199
200 // required url
201 // SLES HACK: or path, but beware of the hardcoded '/repo' prefix!
202 {
203 std::string urlstr;
204 std::string pathstr;
205 getAttrValue( "url", reader_r, urlstr );
206 getAttrValue( "path", reader_r, pathstr );
207 if ( urlstr.empty() )
208 {
209 if ( pathstr.empty() )
210 throw ParseException(str::form(_("One or both of '%s' or '%s' attributes is required."), "url", "path"));
211 else
212 info.setPath( Pathname("/repo") / pathstr );
213 }
214 else
215 {
216 if ( pathstr.empty() )
217 info.setBaseUrl( Url(urlstr) );
218 else
219 {
220 Url url( urlstr );
221 url.setPathName( Pathname(url.getPathName()) / "repo" / pathstr );
222 info.setBaseUrl( url );
223 }
224 }
225 }
226
227 // optional name
228 if ( getAttrValue( "name", reader_r, attrValue ) )
229 info.setName( attrValue );
230
231 // optional targetDistro
232 if ( getAttrValue( "distro_target", reader_r, attrValue ) )
233 info.setTargetDistribution( attrValue );
234
235 // optional priority
236 if ( getAttrValue( "priority", reader_r, attrValue ) )
237 info.setPriority( str::strtonum<unsigned>( attrValue ) );
238
239
240 // optional enabled
241 if ( getAttrValue( "enabled", reader_r, attrValue ) )
242 info.setEnabled( str::strToBool( attrValue, info.enabled() ) );
243
244 // optional autorefresh
245 if ( getAttrValue( "autorefresh", reader_r, attrValue ) )
246 info.setAutorefresh( str::strToBool( attrValue, info.autorefresh() ) );
247
248 DBG << info << endl;
249
250 // ignore the rest
251 _callback(info);
252 return true;
253 }
254 }
255
256 return true;
257 }
258
259
261 //
262 // CLASS NAME : RepoindexFileReader
263 //
265
266 RepoindexFileReader::RepoindexFileReader( Pathname repoindex_file, ProcessResource callback )
267 : _pimpl(new Impl( InputStream(std::move(repoindex_file)), std::move(callback) ))
268 {}
269
270 RepoindexFileReader::RepoindexFileReader(const InputStream &is, ProcessResource callback )
271 : _pimpl(new Impl( is, std::move(callback) ))
272 {}
273
274 RepoindexFileReader::~RepoindexFileReader()
275 {}
276
277 Date::Duration RepoindexFileReader::ttl() const { return _pimpl->_ttl; }
278
279 } // ns parser
280} // ns zypp
281
282// vim: set ts=2 sts=2 sw=2 et ai:
Reader.h
RepoInfo.h
RepoindexFileReader.h
Interface of repoindex.xml file reader.
zypp::Date::Duration
time_t Duration
Definition Date.h:39
zypp::DefaultIntegral
Integral type with defined initial value when default constructed.
Definition defaultintegral.h:42
zypp::InputStream
Helper to create and pass std::istream.
Definition inputstream.h:57
zypp::InputStream::path
const Pathname & path() const
Path to the input file or empty if no file.
Definition inputstream.h:111
zypp::RepoInfo
What is known about a repository.
Definition RepoInfo.h:72
zypp::RepoInfo::setBaseUrl
void setBaseUrl(Url url)
Clears current base URL list and adds url.
Definition RepoInfo.cc:572
zypp::RepoInfo::setTargetDistribution
void setTargetDistribution(const std::string &targetDistribution)
Sets the distribution for which is this repository meant.
Definition RepoInfo.cc:606
zypp::RepoInfo::setPath
void setPath(const Pathname &path)
set the product path.
Definition RepoInfo.cc:581
zypp::RepoInfo::setPriority
void setPriority(unsigned newval_r)
Set repository priority for solver.
Definition RepoInfo.cc:421
zypp::Url
Url manipulation class.
Definition Url.h:93
zypp::filesystem::Pathname
Pathname.
Definition Pathname.h:47
zypp::parser::ParseException
Definition parseexception.h:34
zypp::parser::RepoindexFileReader::Impl
Definition RepoindexFileReader.cc:103
zypp::parser::RepoindexFileReader::Impl::_ttl
DefaultIntegral< Date::Duration, 0 > _ttl
Definition RepoindexFileReader.cc:117
zypp::parser::RepoindexFileReader::Impl::consumeNode
bool consumeNode(Reader &reader_r)
Callback provided to the XML parser.
Definition RepoindexFileReader.cc:161
zypp::parser::RepoindexFileReader::Impl::_replacer
VarReplacer _replacer
Definition RepoindexFileReader.cc:135
zypp::parser::RepoindexFileReader::Impl::Impl
Impl(const InputStream &is, ProcessResource &&callback)
CTOR.
Definition RepoindexFileReader.cc:139
zypp::parser::RepoindexFileReader::Impl::_callback
ProcessResource _callback
Function for processing collected data.
Definition RepoindexFileReader.cc:134
zypp::parser::RepoindexFileReader::Impl::getAttrValue
bool getAttrValue(const std::string &key_r, Reader &reader_r, std::string &value_r)
Definition RepoindexFileReader.cc:120
zypp::parser::RepoindexFileReader::ProcessResource
function< bool(const RepoInfo &)> ProcessResource
Callback definition.
Definition RepoindexFileReader.h:52
zypp::parser::RepoindexFileReader::_pimpl
RW_pointer< Impl, rw_pointer::Scoped< Impl > > _pimpl
Definition RepoindexFileReader.h:86
zypp::parser::RepoindexFileReader::RepoindexFileReader
RepoindexFileReader(Pathname repoindexFile, ProcessResource callback)
CTOR.
Definition RepoindexFileReader.cc:266
zypp::parser::RepoindexFileReader::ttl
Date::Duration ttl() const
Metadata TTL (repoindex.xml:xpath:/repoindex@ttl or 0).
Definition RepoindexFileReader.cc:277
zypp::parser::RepoindexFileReader::~RepoindexFileReader
~RepoindexFileReader()
DTOR.
Definition RepoindexFileReader.cc:274
zypp::repo::RepoInfoBase::setAutorefresh
void setAutorefresh(bool autorefresh)
enable or disable autorefresh
Definition RepoInfoBase.cc:91
zypp::repo::RepoInfoBase::setAlias
void setAlias(const std::string &alias)
set the repository alias
Definition RepoInfoBase.cc:94
zypp::repo::RepoInfoBase::setName
void setName(const std::string &name)
set the repository name
Definition RepoInfoBase.cc:97
zypp::repo::RepoInfoBase::autorefresh
bool autorefresh() const
If true, the repostory must be refreshed before creating resolvables from it.
Definition RepoInfoBase.cc:108
zypp::repo::RepoInfoBase::enabled
bool enabled() const
If enabled is false, then this repository must be ignored as if does not exists, except when checking...
Definition RepoInfoBase.cc:104
zypp::repo::RepoInfoBase::setEnabled
void setEnabled(bool enabled)
enable or disable the repository
Definition RepoInfoBase.cc:88
zypp::xml::Node::localName
XmlString localName() const
The local name of the node.
Definition Node.h:114
zypp::xml::Node::nodeType
NodeType nodeType() const
Get the node type of the current node.
Definition Node.h:126
zypp::xml::Node::value
XmlString value() const
Provides the text value of the node if present.
Definition Node.h:143
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::nextNodeAttribute
bool nextNodeAttribute()
Definition Reader.cc:162
zypp::xml::Reader::foreachNode
bool foreachNode(const ProcessNode &fnc_r)
Definition Reader.h:144
zypp::xml::XmlString
xmlChar * wrapper.
Definition XmlString.h:41
zypp::xml::XmlString::asString
std::string asString() const
Explicit conversion to std::string.
Definition XmlString.h:77
zypp::xml::XmlString::get
const xmlChar * get() const
Access the xmlChar *.
Definition XmlString.h:61
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::str::form
std::string form(const char *format,...) __attribute__((format(printf
Printf style construction of std::string.
Definition String.cc:37
zypp::str::strToBool
bool strToBool(const C_Str &str, bool default_r)
Parse str into a bool depending on the default value.
Definition String.h:429
zypp::str::strtonum
TInt strtonum(const C_Str &str)
Parsing numbers from string.
zypp::url
Url details namespace.
Definition UrlBase.cc:58
zypp
Easy-to use interface to the ZYPP dependency resolver.
Definition Application.cc:20
_
#define _(MSG)
Definition Gettext.h:39
DBG
#define DBG
Definition Logger.h:99
MIL
#define MIL
Definition Logger.h:100
WAR
#define WAR
Definition Logger.h:101
Pathname.h
Gettext.h
Interface to gettext.
Logger.h
String.h