libzypp 17.37.10

SysContent.cc
Go to the documentation of this file.
1/*---------------------------------------------------------------------\
2| ____ _ __ __ ___ |
3| |__ / \ / / . \ . \ |
4| / / \ V /| _/ _/ |
5| / /__ | | | | | | |
6| /_____||_| |_| |_| |
7| |
8\---------------------------------------------------------------------*/
12#include <iostream>
13#include <zypp/base/Logger.h>
14
15#include <zypp/SysContent.h>
16#include <zypp/parser/xml/Reader.h>
17#include <zypp/parser/xml/ParseDef.h>
18#include <zypp/parser/xml/ParseDefConsume.h>
19
20 using std::endl;
21
23 namespace zypp
24{
26 namespace syscontent
27 {
28
30 namespace // Writer helpers
31 {
32
37 inline std::string attrIf( const std::string & tag_r,
38 const std::string & value_r )
39 {
40 std::string ret;
41 if ( ! value_r.empty() )
42 {
43 ret += " ";
44 ret += tag_r;
45 ret += "=\"";
46 ret += value_r;
47 ret += "\"";
48 }
49 return ret;
50 }
51
53 } // namespace
55
57 //
58 // CLASS NAME : Writer::Impl
59 //
61 class Writer::Impl
62 {
63 public:
64 std::ostream & writeXml( std::ostream & str ) const;
65
66 public:
67 std::string _name;
68 Edition _edition;
69 std::string _description;
70 StorageT _onsys;
71
72 public:
74 static shared_ptr<Impl> nullimpl()
75 {
76 static shared_ptr<Impl> _nullimpl( new Impl );
77 return _nullimpl;
78 }
79
80 private:
81 friend Impl * rwcowClone<Impl>( const Impl * rhs );
83 Impl * clone() const
84 { return new Impl( *this ); }
85 };
86
87
89 //
90 // METHOD NAME : Writer::Impl::writeXml
91 // METHOD TYPE : std::ostream &
92 //
93 std::ostream & Writer::Impl::writeXml( std::ostream & str ) const
94 {
95 // intro
96 str << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
97 str << "<syscontent>\n";
98 // ident data
99 str << " <ident>\n";
100 str << " <name>" << _name << "</name>\n";
101 str << " <version"
102 << attrIf( "epoch", str::numstring(_edition.epoch()) )
103 << attrIf( "ver", _edition.version() )
104 << attrIf( "rel", _edition.release() )
105 << "/>\n";
106 str << " <description>" << _description << "</description>\n";
107 str << " <created>" << Date::now().asSeconds() << "</created>\n";
108 str << " </ident>\n";
109 // ResObjects
110 str << " <onsys>\n";
111 for ( iterator it = _onsys.begin(); it != _onsys.end(); ++it )
112 {
113 str << " <entry"
114 << attrIf( "kind", (*it)->kind().asString() )
115 << attrIf( "name", (*it)->name() )
116 << attrIf( "epoch", str::numstring((*it)->edition().epoch()) )
117 << attrIf( "ver", (*it)->edition().version() )
118 << attrIf( "rel", (*it)->edition().release() )
119 << attrIf( "arch", (*it)->arch().asString() )
120 << "/>\n";
121 }
122 str << " </onsys>\n";
123 // extro
124 str << "</syscontent>" << endl;
125 return str;
126 };
127
129 //
130 // CLASS NAME : Writer
131 //
133
134 Writer::Writer()
135 : _pimpl( Impl::nullimpl() )
136 {}
137
138 const std::string & Writer::name() const
139 { return _pimpl->_name; }
140
141 Writer & Writer::name( const std::string & val_r )
142 { _pimpl->_name = val_r; return *this; }
143
144 const Edition & Writer::edition() const
145 { return _pimpl->_edition; }
146
147 Writer & Writer::edition( const Edition & val_r )
148 { _pimpl->_edition = val_r; return *this; }
149
150 const std::string & Writer::description() const
151 { return _pimpl->_description; }
152
153 Writer & Writer::description( const std::string & val_r )
154 { _pimpl->_description = val_r; return *this; }
155
156 void Writer::addInstalled( const PoolItem & obj_r )
157 {
158 if ( obj_r.status().isInstalled() )
159 {
160 _pimpl->_onsys.insert( obj_r.resolvable() );
161 }
162 }
163
164 void Writer::addIf( const PoolItem & obj_r )
165 {
166 if ( obj_r.status().isInstalled() != obj_r.status().transacts()
167 && ! ( obj_r.status().transacts() && obj_r.status().isBySolver() ) )
168 {
169 _pimpl->_onsys.insert( obj_r.resolvable() );
170 }
171 }
172
173 void Writer::add( const ResObject::constPtr & obj_r )
174 { _pimpl->_onsys.insert( obj_r ); }
175
176 bool Writer::empty() const
177 { return _pimpl->_onsys.empty(); }
178
179 Writer::size_type Writer::size() const
180 { return _pimpl->_onsys.size(); }
181
182 Writer::const_iterator Writer::begin() const
183 { return _pimpl->_onsys.begin(); }
184
185 Writer::const_iterator Writer::end() const
186 { return _pimpl->_onsys.end(); }
187
188 std::ostream & Writer::writeXml( std::ostream & str ) const
189 { return _pimpl->writeXml( str ); }
190
192 //
193 // CLASS NAME : Reader::Entry::Impl
194 //
195 class Reader::Entry::Impl
196 {
197 public:
198 std::string _kind;
199 std::string _name;
200 Edition _edition;
201 Arch _arch;
202 };
203
204
206 //
207 // CLASS NAME : Reader::Entry
208 //
210
211 Reader::Entry::Entry()
212 : _pimpl( new Impl )
213 {}
214
215 Reader::Entry::Entry( const shared_ptr<Impl> & pimpl_r )
216 : _pimpl( pimpl_r )
217 {}
218
219 const std::string & Reader::Entry::kind() const
220 { return _pimpl->_kind; }
221
222 const std::string & Reader::Entry::name() const
223 { return _pimpl->_name; }
224
225 const Edition & Reader::Entry::edition() const
226 { return _pimpl->_edition; }
227
228 const Arch & Reader::Entry::arch() const
229 { return _pimpl->_arch; }
230
232 //
233 // CLASS NAME : Reader::Impl
234 //
236 class Reader::Impl
237 {
238 public:
239 Impl()
240 {}
241
242 Impl( std::istream & input_r );
243
244 public:
245 std::string _name;
246 Edition _edition;
247 std::string _description;
248 Date _created;
249
250 std::list<Entry> _content;
251
252 public:
254 static shared_ptr<Impl> nullimpl()
255 {
256 static shared_ptr<Impl> _nullimpl( new Impl );
257 return _nullimpl;
258 }
259
260 private:
261 friend Impl * rwcowClone<Impl>( const Impl * rhs );
263 Impl * clone() const
264 { return new Impl( *this ); }
265 };
266
267
269 namespace // Reader helpers
270 {
271
272 using namespace xml;
273
275 struct SycontentNode : public ParseDef
276 {
277 SycontentNode( Mode mode_r )
278 : ParseDef( "syscontent", mode_r )
279 {
280 (*this)("ident", OPTIONAL)
281 ("onsys", OPTIONAL)
282 ;
283
284 (*this)["ident"]
285 ("name", OPTIONAL)
286 ("version", OPTIONAL)
287 ("description", OPTIONAL)
288 ("created", OPTIONAL)
289 ;
290
291 (*this)["onsys"]
292 ("entry", MULTIPLE_OPTIONAL)
293 ;
294 }
295 };
296
298 struct ConsumeEdition : public ParseDefConsume
299 {
300 ConsumeEdition( Edition & value_r )
301 : _value( & value_r )
302 {}
303
304 void start( const Node & node_r ) override
305 {
306 *_value = Edition( node_r.getAttribute("ver").asString(),
307 node_r.getAttribute("rel").asString(),
308 node_r.getAttribute("epoch").asString() );
309 }
310
311 Edition *_value;
312 };
313
315 struct ConsumeString : public ParseDefConsume
316 {
317 ConsumeString( std::string & value_r )
318 : _value( & value_r )
319 {}
320
321 void text( const Node & node_r ) override
322 {
323 *_value = node_r.value().asString();
324 }
325
326 std::string *_value;
327 };
328
330 struct ConsumeDate : public ParseDefConsume
331 {
332 ConsumeDate( Date & value_r )
333 : _value( & value_r )
334 {}
335
336 void text( const Node & node_r ) override
337 {
338 *_value = Date(node_r.value().asString());
339 }
340
341 Date *_value;
342 };
343
345 struct ConsumeEntries : public ParseDefConsume
346 {
347 ConsumeEntries( std::list<Reader::Entry> & value_r )
348 : _value( & value_r )
349 {}
350
351 void start( const Node & node_r ) override
352 {
353 shared_ptr<Reader::Entry::Impl> centry( new Reader::Entry::Impl );
354
355 centry->_kind = node_r.getAttribute("kind").asString();
356 centry->_name = node_r.getAttribute("name").asString();
357 centry->_edition = Edition( node_r.getAttribute("ver").asString(),
358 node_r.getAttribute("rel").asString(),
359 node_r.getAttribute("epoch").asString() );
360 centry->_arch = Arch( node_r.getAttribute("arch").asString() );
361
362 _value->push_back( Reader::Entry( centry ) );
363 }
364
365 std::list<Reader::Entry> *_value;
366 };
367
369 } // namespace
371
373 //
374 // METHOD NAME : Reader::Impl::Impl
375 // METHOD TYPE : Constructor
376 //
377 Reader::Impl::Impl( std::istream & input_r )
378 {
379 xml::Reader reader( input_r );
380 SycontentNode rootNode( xml::ParseDef::MANDTAORY );
381
382 rootNode["ident"]["name"].setConsumer
383 ( new ConsumeString( _name ) );
384
385 rootNode["ident"]["version"].setConsumer
386 ( new ConsumeEdition( _edition ) );
387
388 rootNode["ident"]["description"].setConsumer
389 ( new ConsumeString( _description ) );
390
391 rootNode["ident"]["created"].setConsumer
392 ( new ConsumeDate( _created ) );
393
394 rootNode["onsys"]["entry"].setConsumer
395 ( new ConsumeEntries( _content ) );
396
397 // parse
398 rootNode.take( reader );
399 }
400
402 //
403 // CLASS NAME : Reader
404 //
406
407 Reader::Reader()
408 : _pimpl( Impl::nullimpl() )
409 {}
410
411 Reader::Reader( std::istream & input_r )
412 : _pimpl( new Impl( input_r ) )
413 {}
414
415 const std::string & Reader::name() const
416 { return _pimpl->_name; }
417
418 const Edition & Reader::edition() const
419 { return _pimpl->_edition; }
420
421 const std::string & Reader::description() const
422 { return _pimpl->_description; }
423
424 const Date & Reader::ctime() const
425 { return _pimpl->_created; }
426
427 bool Reader::empty() const
428 { return _pimpl->_content.empty(); }
429
430 Reader::size_type Reader::size() const
431 { return _pimpl->_content.size(); }
432
433 Reader::const_iterator Reader::begin() const
434 { return _pimpl->_content.begin(); }
435
436 Reader::const_iterator Reader::end() const
437 { return _pimpl->_content.end(); }
438
439 /******************************************************************
440 **
441 ** FUNCTION NAME : operator<<
442 ** FUNCTION TYPE : inline std::ostream &
443 */
444 std::ostream & operator<<( std::ostream & str, const Reader & obj )
445 {
446 return str << "syscontent(" << obj.name() << "-" << obj.edition()
447 << ", " << obj.size() << " entries"
448 << ", created " << obj.ctime()
449 << ")";
450 }
451
453 } // namespace syscontent
454
456} // namespace zypp
ParseDefConsume.h
ParseDef.h
Reader.h
SysContent.h
Impl
shared_ptr
zypp::Arch
Architecture.
Definition Arch.h:37
zypp::Date
Store and operate on date (time_t).
Definition Date.h:33
zypp::Date::asSeconds
std::string asSeconds() const
Convert to string representation of calendar time in numeric form (like "1029255142").
Definition Date.h:126
zypp::Date::now
static Date now()
Return the current time.
Definition Date.h:78
zypp::Edition
Edition represents [epoch:]version[-release]
Definition Edition.h:61
zypp::PoolItem
Combining sat::Solvable and ResStatus.
Definition PoolItem.h:51
zypp::PoolItem::resolvable
ResObject::constPtr resolvable() const
Returns the ResObject::constPtr.
Definition PoolItem.cc:227
zypp::PoolItem::status
ResStatus & status() const
Returns the current status.
Definition PoolItem.cc:212
zypp::ResObject::constPtr
TraitsType::constPtrType constPtr
Definition ResObject.h:43
zypp::ResStatus::transacts
bool transacts() const
Definition ResStatus.h:282
zypp::ResStatus::isInstalled
bool isInstalled() const
Definition ResStatus.h:246
zypp::ResStatus::isBySolver
bool isBySolver() const
Definition ResStatus.h:296
xml::Reader::Entry::Impl
Definition SysContent.cc:196
xml::Reader::Entry::Impl::_edition
Edition _edition
Definition SysContent.cc:200
xml::Reader::Entry::Impl::_arch
Arch _arch
Definition SysContent.cc:201
xml::Reader::Entry::Impl::_kind
std::string _kind
Definition SysContent.cc:198
xml::Reader::Entry::Impl::_name
std::string _name
Definition SysContent.cc:199
zypp::syscontent::Reader::Entry::arch
const Arch & arch() const
Definition SysContent.cc:228
zypp::syscontent::Reader::Entry::name
const std::string & name() const
Definition SysContent.cc:222
zypp::syscontent::Reader::Entry::Entry
Entry()
Definition SysContent.cc:211
zypp::syscontent::Reader::Entry::_pimpl
RW_pointer< Impl > _pimpl
Definition SysContent.h:255
zypp::syscontent::Reader::Entry::edition
const Edition & edition() const
Definition SysContent.cc:225
zypp::syscontent::Reader::Entry::kind
const std::string & kind() const
Definition SysContent.cc:219
xml::Reader::Impl
Definition SysContent.cc:237
xml::Reader::Impl::nullimpl
static shared_ptr< Impl > nullimpl()
Offer default Impl.
Definition SysContent.cc:254
xml::Reader::Impl::rwcowClone
friend Impl * rwcowClone(const Impl *rhs)
xml::Reader::Impl::_edition
Edition _edition
Definition SysContent.cc:246
xml::Reader::Impl::_name
std::string _name
Definition SysContent.cc:245
xml::Reader::Impl::Impl
Impl()
Definition SysContent.cc:239
xml::Reader::Impl::_created
Date _created
Definition SysContent.cc:248
xml::Reader::Impl::_description
std::string _description
Definition SysContent.cc:247
xml::Reader::Impl::clone
Impl * clone() const
clone for RWCOW_pointer
Definition SysContent.cc:263
xml::Reader::Impl::_content
std::list< Entry > _content
Definition SysContent.cc:250
zypp::syscontent::Reader::ctime
const Date & ctime() const
Get creation date.
Definition SysContent.cc:424
zypp::syscontent::Reader::end
const_iterator end() const
Iterator to the end of collected data.
Definition SysContent.cc:436
zypp::syscontent::Reader::const_iterator
StorageT::const_iterator const_iterator
Definition SysContent.h:184
zypp::syscontent::Reader::edition
const Edition & edition() const
Get edition.
Definition SysContent.cc:418
zypp::syscontent::Reader::size_type
StorageT::size_type size_type
Definition SysContent.h:182
zypp::syscontent::Reader::Reader
Reader()
Default Ctor.
Definition SysContent.cc:407
zypp::syscontent::Reader::description
const std::string & description() const
Get description.
Definition SysContent.cc:421
zypp::syscontent::Reader::_pimpl
RWCOW_pointer< Impl > _pimpl
Definition SysContent.h:230
zypp::syscontent::Reader::empty
bool empty() const
Whether no data collected so far.
Definition SysContent.cc:427
zypp::syscontent::Reader::begin
const_iterator begin() const
Iterator to the begin of collected data.
Definition SysContent.cc:433
zypp::syscontent::Reader::name
const std::string & name() const
Get name.
Definition SysContent.cc:415
zypp::syscontent::Reader::size
size_type size() const
Number of items collected.
Definition SysContent.cc:430
zypp::syscontent::Writer::Impl
Definition SysContent.cc:62
zypp::syscontent::Writer::Impl::_description
std::string _description
Definition SysContent.cc:69
zypp::syscontent::Writer::Impl::rwcowClone
friend Impl * rwcowClone(const Impl *rhs)
zypp::syscontent::Writer::Impl::_edition
Edition _edition
Definition SysContent.cc:68
zypp::syscontent::Writer::Impl::_onsys
StorageT _onsys
Definition SysContent.cc:70
zypp::syscontent::Writer::Impl::clone
Impl * clone() const
clone for RWCOW_pointer
Definition SysContent.cc:83
zypp::syscontent::Writer::Impl::_name
std::string _name
Definition SysContent.cc:67
zypp::syscontent::Writer::Impl::nullimpl
static shared_ptr< Impl > nullimpl()
Offer default Impl.
Definition SysContent.cc:74
zypp::syscontent::Writer::Impl::writeXml
std::ostream & writeXml(std::ostream &str) const
Definition SysContent.cc:93
zypp::syscontent::Writer::size_type
StorageT::size_type size_type
Definition SysContent.h:69
zypp::syscontent::Writer::edition
const Edition & edition() const
Get edition.
Definition SysContent.cc:144
zypp::syscontent::Writer::StorageT
std::set< ResObject::constPtr > StorageT
Definition SysContent.h:66
zypp::syscontent::Writer::writeXml
std::ostream & writeXml(std::ostream &str) const
Write collected data as XML.
Definition SysContent.cc:188
zypp::syscontent::Writer::description
const std::string & description() const
Get description.
Definition SysContent.cc:150
zypp::syscontent::Writer::addIf
void addIf(const PoolItem &obj_r)
Collect PoolItem if it stays on the system.
Definition SysContent.cc:164
zypp::syscontent::Writer::const_iterator
StorageT::const_iterator const_iterator
Definition SysContent.h:71
zypp::syscontent::Writer::_pimpl
RWCOW_pointer< Impl > _pimpl
Definition SysContent.h:156
zypp::syscontent::Writer::Writer
Writer()
Default Ctor.
Definition SysContent.cc:134
zypp::syscontent::Writer::addInstalled
void addInstalled(const PoolItem &obj_r)
Collect currently installed PoolItem.
Definition SysContent.cc:156
zypp::syscontent::Writer::begin
const_iterator begin() const
Iterator to the begin of collected data.
Definition SysContent.cc:182
zypp::syscontent::Writer::empty
bool empty() const
Whether no data collected so far.
Definition SysContent.cc:176
zypp::syscontent::Writer::size
size_type size() const
Number of items collected.
Definition SysContent.cc:179
zypp::syscontent::Writer::add
void add(const ResObject::constPtr &obj_r)
Unconditionally add this ResObject (or PoolItem).
Definition SysContent.cc:173
zypp::syscontent::Writer::name
const std::string & name() const
Get name.
Definition SysContent.cc:138
zypp::syscontent::Writer::iterator
StorageT::iterator iterator
Definition SysContent.h:70
zypp::syscontent::Writer::end
const_iterator end() const
Iterator to the end of collected data.
Definition SysContent.cc:185
zypp::xml::ParseDef::MANDTAORY
@ MANDTAORY
Definition ParseDef.h:136
zypp::xml::Reader::Reader
Reader(const InputStream &stream_r, const Validate &validate_r=Validate::none())
Ctor.
Definition Reader.cc:88
str
String related utilities and Regular expression matching.
ztui::table::CStyle::Edition
@ Edition
Editions with v-r setparator highlighted.
Definition Table.h:160
zypp::str::numstring
std::string numstring(char n, int w=0)
Definition String.h:290
zypp::syscontent
Definition SysContent.cc:27
zypp::xml::operator<<
std::ostream & operator<<(std::ostream &str, const ReadState &obj)
Definition libxmlfwd.cc:29
zypp
Easy-to use interface to the ZYPP dependency resolver.
Definition Application.cc:20
Logger.h