libzypp  17.22.1
ProductFileReader.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 #include "zypp/base/Exception.h"
15 #include "zypp/base/Functional.h"
16 
17 #include "zypp/PathInfo.h"
18 
22 #include "zypp/parser/xml/Reader.h"
23 
24 using std::endl;
25 
27 namespace zypp
28 {
29  namespace parser
31  {
32 
34  //
35  // class ProductFileData::Upgrade
36  //
38 
40  {
41  std::string _name;
42  std::string _summary;
43  std::string _repository;
44  std::string _product;
46  std::string _status;
47  };
48 
50  : _pimpl( allocated_r ? allocated_r : new Impl )
51  {}
52 
53  std::string ProductFileData::Upgrade::name() const { return _pimpl->_name; }
54  std::string ProductFileData::Upgrade::summary() const { return _pimpl->_summary; }
55  std::string ProductFileData::Upgrade::repository() const { return _pimpl->_repository; }
56  std::string ProductFileData::Upgrade::product() const { return _pimpl->_product; }
57  bool ProductFileData::Upgrade::notify() const { return _pimpl->_notify; }
58  std::string ProductFileData::Upgrade::status() const { return _pimpl->_status; }
59 
61  //
62  // class ProductFileData
63  //
65 
67  {
72 
73  std::string _shortName;
74  std::string _summary;
75 
76  std::string _productline;
77  std::string _registerTarget;
78  std::string _registerRelease;
79  std::string _registerFlavor;
80 
81  std::string _updaterepokey;
82 
84  };
85 
87  : _pimpl( allocated_r ? allocated_r : new Impl )
88  {}
89 
93  Arch ProductFileData::arch() const { return _pimpl->_arch; }
94 
95  std::string ProductFileData::shortName() const { return _pimpl->_shortName; }
96  std::string ProductFileData::summary() const { return _pimpl->_summary; }
97 
98  std::string ProductFileData::productline() const { return _pimpl->_productline; }
99  std::string ProductFileData::registerTarget() const { return _pimpl->_registerTarget; }
101  std::string ProductFileData::registerFlavor() const { return _pimpl->_registerFlavor; }
102 
103  std::string ProductFileData::updaterepokey() const { return _pimpl->_updaterepokey; }
104 
106 
107  std::ostream & operator<<( std::ostream & str, const ProductFileData & obj )
108  {
109  str << str::form( "|product|%s|%s|%s|%s|",
110  obj.name().c_str(),
111  obj.edition().c_str(),
112  obj.arch().c_str(),
113  obj.vendor().c_str() );
114  if ( ! obj.upgrades().empty() )
115  {
116  for_( it, obj.upgrades().begin(), obj.upgrades().end() )
117  str << endl << " " << *it;
118  }
119  return str;
120  }
121 
122  std::ostream & operator<<( std::ostream & str, const ProductFileData::Upgrade & obj )
123  {
124  str << str::form( "|upgrade|%s|%s|%s|%s|%s|",
125  obj.name().c_str(),
126  obj.repository().c_str(),
127  obj.product().c_str(),
128  obj.status().c_str(),
129  (obj.notify() ? "notify" : "noNotify") );
130  return str;
131  }
133  //
134  // class ProductFileReader
135  //
137 
138  struct ProductNode : public xml::ParseDef
139  {
141  : ParseDef( "product", MANDTAORY )
142  , _pdata( pdata_r )
143  {
144  (*this)
145  ("vendor", OPTIONAL, xml::parseDefAssign( _pdata._vendor ) )
146  ("name", MANDTAORY, xml::parseDefAssign( _pdata._name ) )
147  ("version", MANDTAORY, xml::parseDefAssign( _version ) )
148  ("release", MANDTAORY, xml::parseDefAssign( _release ) )
149  ("arch", MANDTAORY, xml::parseDefAssign( _pdata._arch ) )
150  ("shortsummary", OPTIONAL, xml::parseDefAssign( _pdata._shortName ) )
151  ("summary", MULTIPLE_OPTIONAL, xml::parseDefAssign( _ttext )( "lang", _tlocale )
152  >>bind( &ProductNode::doneLocalizedDefault, this, _1, boost::ref(_pdata._summary) ))
153  ("productline", OPTIONAL, xml::parseDefAssign( _pdata._productline ) )
154  ("register", OPTIONAL)
155  ("updaterepokey", OPTIONAL, xml::parseDefAssign( _pdata._updaterepokey ) )
156  ("upgrades", OPTIONAL)
157  ;
158 
159  (*this)["register"]
160  ("target", OPTIONAL, xml::parseDefAssign( _pdata._registerTarget ) )
161  ("release", OPTIONAL, xml::parseDefAssign( _pdata._registerRelease ) )
162  ("flavor", OPTIONAL, xml::parseDefAssign( _pdata._registerFlavor ) )
163  ;
164 
165  (*this)["upgrades"]
166  ("upgrade", MULTIPLE_OPTIONAL, xml::parseDefAssign()
167  >> bind( &ProductNode::doneUpgrade, this, _1 ))
168  ;
169 
170  (*this)["upgrades"]["upgrade"]
171  ("name", OPTIONAL, xml::parseDefAssign( _upgrade._name ) )
172  ("summary", OPTIONAL, xml::parseDefAssign( _upgrade._summary ) )
173  ("repository", OPTIONAL, xml::parseDefAssign( _upgrade._repository ) )
174  ("product", OPTIONAL, xml::parseDefAssign( _upgrade._product ) )
175  ("notify", OPTIONAL, xml::parseDefAssign( _upgrade._notify ) )
176  ("status", OPTIONAL, xml::parseDefAssign( _upgrade._status ) )
177  ;
178 
179  // </product> callback to build edition.
180  setConsumer( xml::parseDefAssign() >> bind( &ProductNode::done, this, _1 ) );
181  // xml::ParseDef::_debug = true;
182  }
183 
185  void doneUpgrade( const xml::Node & _node )
186  {
188  _pdata._upgrades.push_back( cdata );
190  }
192  void doneLocalizedDefault( const xml::Node & _node, std::string & store_r )
193  {
194  // take 1st or default
195  if ( store_r.empty() || _tlocale.empty() )
196  store_r = _ttext;
197  }
198 
200  void done( const xml::Node & _node )
201  {
203  }
204 
205  private:
207 
208  std::string _version;
209  std::string _release;
210 
211  std::string _ttext;
212  std::string _tlocale;
213 
215  };
216 
217  bool ProductFileReader::parse( const InputStream & input_r ) const
218  {
219  MIL << "+++" << input_r << endl;
220  bool ret = true;
221 
222  ProductFileData::Impl * pdataImpl = 0;
223  ProductFileData pdata( (pdataImpl = new ProductFileData::Impl) );
224 
225  try
226  {
227  xml::Reader reader( input_r );
228  ProductNode rootNode( *pdataImpl );
229  rootNode.take( reader );
230 
231  }
232  catch ( const Exception & err )
233  {
234  // parse error
235  ERR << err << endl;
236  ERR << "---" << ret << " - " << input_r << endl;
237  return ret;
238  }
239 
240  if ( _consumer )
241  {
242  ret = _consumer( pdata );
243  }
244 
245  MIL << "---" << ret << " - " << input_r << endl;
246  return ret;
247  }
248 
250 
251  bool ProductFileReader::scanDir( const Consumer & consumer_r, const Pathname & dir_r )
252  {
253  std::list<Pathname> retlist;
254  int res = filesystem::readdir( retlist, dir_r, /*dots*/false );
255  if ( res != 0 )
256  {
257  WAR << "scanDir " << dir_r << " failed (" << res << ")" << endl;
258  return true;
259  }
260 
261  ProductFileReader reader( consumer_r );
262  for_( it, retlist.begin(), retlist.end() )
263  {
264  if ( PathInfo( *it, PathInfo::LSTAT ).isFile() && ! reader.parse( *it ) )
265  {
266  return false; // consumer_r request to stop parsing.
267  }
268  }
269  return true;
270  }
271 
273  {
274  if ( ! PathInfo( file_r ).isFile() )
275  {
276  WAR << "scanFile " << PathInfo( file_r ) << " is not a file." << endl;
277  return ProductFileData();
278  }
279 
280  ProductFileData ret;
281  ProductFileReader reader( functor::getFirst( ret ), file_r );
282  return ret;
283  }
284 
286  } // namespace parser
289 } // namespace zypp
zypp::parser::ProductFileData::productline
std::string productline() const
Definition: ProductFileReader.cc:98
zypp::parser::ProductFileData::Impl::_edition
Edition _edition
Definition: ProductFileReader.cc:70
zypp::filesystem::readdir
int readdir(std::list< std::string > &retlist_r, const Pathname &path_r, bool dots_r)
Return content of directory via retlist.
Definition: PathInfo.cc:584
zypp::parser::ProductFileReader::Consumer
function< bool(const ProductFileData &)> Consumer
Callback being invoked for each ProductFileData parsed.
Definition: ProductFileReader.h:126
zypp::parser::ProductFileData::Upgrade
Definition: ProductFileReader.h:72
zypp::parser::ProductNode::done
void done(const xml::Node &_node)
finaly
Definition: ProductFileReader.cc:200
zypp::parser::ProductFileReader::scanFile
static ProductFileData scanFile(const Pathname &file_r)
Parse one file (or symlink) and return the ProductFileData parsed.
Definition: ProductFileReader.cc:272
zypp::parser::ProductNode::_release
std::string _release
Definition: ProductFileReader.cc:209
zypp::parser::ProductFileData::Upgrade::Impl::_repository
std::string _repository
Definition: ProductFileReader.cc:43
PathInfo.h
zypp::parser::ProductFileData::arch
Arch arch() const
Definition: ProductFileReader.cc:93
zypp::parser::ProductFileData::upgrades
const Upgrades & upgrades() const
Definition: ProductFileReader.cc:105
zypp::parser::ProductFileData::Upgrade::status
std::string status() const
Definition: ProductFileReader.cc:58
zypp::parser::ProductNode::_tlocale
std::string _tlocale
Definition: ProductFileReader.cc:212
zypp::Exception
Base class for Exception.
Definition: Exception.h:145
zypp::parser::ProductNode::ProductNode
ProductNode(ProductFileData::Impl &pdata_r)
Definition: ProductFileReader.cc:140
zypp::parser::ProductFileData::Impl::_registerFlavor
std::string _registerFlavor
Definition: ProductFileReader.cc:79
zypp::IdString::c_str
const char * c_str() const
Conversion to const char *
Definition: IdString.cc:50
zypp::xml::ParseDef::ParseDef
ParseDef(const std::string &name_r, Mode mode_r)
Definition: ParseDef.cc:368
zypp::parser::ProductNode::_pdata
ProductFileData::Impl & _pdata
Definition: ProductFileReader.cc:206
zypp::parser::ProductFileData::updaterepokey
std::string updaterepokey() const
Definition: ProductFileReader.cc:103
zypp::parser::ProductFileData::_pimpl
RWCOW_pointer< Impl > _pimpl
Definition: ProductFileReader.h:96
zypp::parser::ProductFileData::Upgrade::Upgrade
Upgrade(Impl *allocated_r=0)
Ctor takes ownership of allocated_r.
Definition: ProductFileReader.cc:49
MIL
#define MIL
Definition: Logger.h:79
zypp::Edition
Edition represents [epoch:]version[-release]
Definition: Edition.h:60
zypp::parser::ProductFileData::Impl::_updaterepokey
std::string _updaterepokey
Definition: ProductFileReader.cc:81
zypp::parser::ProductFileData::Impl::_productline
std::string _productline
Definition: ProductFileReader.cc:76
zypp::parser::ProductFileReader::parse
bool parse(const InputStream &input_r=InputStream()) const
Parse the input stream and call _consumer for each parsed section.
Definition: ProductFileReader.cc:217
zypp::parser::ProductFileData::Upgrade::Impl::_summary
std::string _summary
Definition: ProductFileReader.cc:42
zypp::filesystem::PathInfo::LSTAT
Definition: PathInfo.h:226
zypp::parser::ProductFileReader
Parser for /etc/products.d enries (just relevant entires).
Definition: ProductFileReader.h:120
zypp::Arch
Architecture.
Definition: Arch.h:36
zypp::xml::ParseDef::MANDTAORY
Definition: ParseDef.h:136
zypp::parser::ProductFileData::Upgrade::notify
bool notify() const
Definition: ProductFileReader.cc:57
ParseDef.h
zypp::parser::ProductFileData::Upgrades
std::vector< Upgrade > Upgrades
Definition: ProductFileReader.h:92
zypp::parser::ProductFileData::Upgrade::Impl::_product
std::string _product
Definition: ProductFileReader.cc:44
zypp::parser::ProductFileData::Impl::_upgrades
Upgrades _upgrades
Definition: ProductFileReader.cc:83
zypp::IdString
Access to the sat-pools string space.
Definition: IdString.h:41
zypp::parser::ProductFileData::Impl
Definition: ProductFileReader.cc:66
zypp::parser::ProductNode::_version
std::string _version
Definition: ProductFileReader.cc:208
Exception.h
zypp::xml::ParseDef::take
void take(Reader &reader_r)
Parse the node.
Definition: ParseDef.cc:438
zypp::filesystem::PathInfo
Wrapper class for ::stat/::lstat.
Definition: PathInfo.h:220
zypp::parser::ProductFileData::Upgrade::product
std::string product() const
Definition: ProductFileReader.cc:56
zypp::parser::ProductNode::doneUpgrade
void doneUpgrade(const xml::Node &_node)
collect _upgrade
Definition: ProductFileReader.cc:185
zypp::parser::ProductFileData::Impl::_registerTarget
std::string _registerTarget
Definition: ProductFileReader.cc:77
Logger.h
WAR
#define WAR
Definition: Logger.h:80
zypp::filesystem::PathInfo::isFile
bool isFile() const
Definition: PathInfo.h:289
Functional.h
zypp::xml::ParseDef
Define a xml node structure to parse.
Definition: ParseDef.h:128
zypp::parser::ProductFileData::vendor
IdString vendor() const
Definition: ProductFileReader.cc:90
zypp::parser::ProductFileData::registerFlavor
std::string registerFlavor() const
Definition: ProductFileReader.cc:101
zypp::parser::ProductFileReader::_consumer
Consumer _consumer
Definition: ProductFileReader.h:168
zypp::parser::ProductFileData::Impl::_name
IdString _name
Definition: ProductFileReader.cc:69
zypp::parser::ProductFileData::Upgrade::Impl::_notify
DefaultIntegral< bool, false > _notify
Definition: ProductFileReader.cc:45
zypp::str::form
std::string form(const char *format,...) __attribute__((format(printf
Printf style construction of std::string.
Definition: String.cc:35
zypp::parser::ProductFileData::Impl::_registerRelease
std::string _registerRelease
Definition: ProductFileReader.cc:78
zypp::InputStream
Helper to create and pass std::istream.
Definition: InputStream.h:56
zypp
Easy-to use interface to the ZYPP dependency resolver.
Definition: CodePitfalls.doc:1
zypp::parser::ProductFileData::Upgrade::Impl::_status
std::string _status
Definition: ProductFileReader.cc:46
zypp::Arch::c_str
const char * c_str() const
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition: Arch.h:59
zypp::parser::ProductFileData::Impl::_shortName
std::string _shortName
Definition: ProductFileReader.cc:73
zypp::functor::getFirst
GetFirst< Tp > getFirst(Tp &result_r)
Convenience function for creating GetFirst.
Definition: Functional.h:382
zypp::parser::ProductFileData::summary
std::string summary() const
Definition: ProductFileReader.cc:96
zypp::parser::ProductNode::_upgrade
ProductFileData::Upgrade::Impl _upgrade
Definition: ProductFileReader.cc:214
ParseDefConsume.h
ProductFileReader.h
zypp::parser::ProductFileData::Impl::_arch
Arch _arch
Definition: ProductFileReader.cc:71
zypp::parser::ProductNode
Definition: ProductFileReader.cc:138
zypp::parser::ProductFileData::Impl::_summary
std::string _summary
Definition: ProductFileReader.cc:74
zypp::parser::ProductFileData::registerRelease
std::string registerRelease() const
Definition: ProductFileReader.cc:100
zypp::parser::ProductFileData::edition
Edition edition() const
Definition: ProductFileReader.cc:92
zypp::DefaultIntegral< bool, false >
zypp::parser::ProductFileData
Data returned by ProductFileReader.
Definition: ProductFileReader.h:40
zypp::xml::ParseDef::OPTIONAL
Definition: ParseDef.h:135
zypp::parser::operator<<
std::ostream & operator<<(std::ostream &str, const IniDict &obj)
Definition: IniDict.cc:142
zypp::parser::ProductFileData::registerTarget
std::string registerTarget() const
Definition: ProductFileReader.cc:99
for_
#define for_(IT, BEG, END)
Convenient for-loops using iterator.
Definition: Easy.h:28
zypp::parser::ProductFileData::name
IdString name() const
Definition: ProductFileReader.cc:91
zypp::parser::ProductFileData::Upgrade::summary
std::string summary() const
Definition: ProductFileReader.cc:54
zypp::parser::ProductFileData::Upgrade::name
std::string name() const
Definition: ProductFileReader.cc:53
zypp::filesystem::Pathname
Pathname.
Definition: Pathname.h:44
xml::Reader
ERR
#define ERR
Definition: Logger.h:81
zypp::xml::ParseDef::MULTIPLE_OPTIONAL
Definition: ParseDef.h:137
zypp::xml::Node
xmlTextReader based interface to Reader's current node.
Definition: Node.h:35
zypp::parser::ProductNode::doneLocalizedDefault
void doneLocalizedDefault(const xml::Node &_node, std::string &store_r)
collect localized data
Definition: ProductFileReader.cc:192
zypp::parser::ProductNode::_ttext
std::string _ttext
Definition: ProductFileReader.cc:211
zypp::xml::ParseDef::setConsumer
void setConsumer(const shared_ptr< ParseDefConsume > &target_r)
Set data consumer.
Definition: ParseDef.cc:422
str
String related utilities and Regular expression matching.
zypp::IdStringType::c_str
const char * c_str() const
Definition: IdStringType.h:105
Reader.h
zypp::parser::ProductFileData::Upgrade::Impl
Definition: ProductFileReader.cc:39
zypp::parser::ProductFileData::shortName
std::string shortName() const
Definition: ProductFileReader.cc:95
zypp::parser::ProductFileData::Upgrade::Impl::_name
std::string _name
Definition: ProductFileReader.cc:41
zypp::parser::ProductFileReader::scanDir
static bool scanDir(const Consumer &consumer_r, const Pathname &dir_r)
Parse all files (no symlinks) in dir_r and call consumer_r for each ProductFileData parsed.
Definition: ProductFileReader.cc:251
zypp::parser::ProductFileData::Impl::_vendor
IdString _vendor
Definition: ProductFileReader.cc:68
zypp::parser::ProductFileData::ProductFileData
ProductFileData(Impl *allocated_r=0)
Ctor takes ownership of allocated_r.
Definition: ProductFileReader.cc:86
zypp::parser::ProductFileData::Upgrade::repository
std::string repository() const
Definition: ProductFileReader.cc:55