libzypp  17.25.0
BinHeader.cc
Go to the documentation of this file.
1 /*---------------------------------------------------------------------\
2 | ____ _ __ __ ___ |
3 | |__ / \ / / . \ . \ |
4 | / / \ V /| _/ _/ |
5 | / /__ | | | | | | |
6 | /_____||_| |_| |_| |
7 | |
8 \---------------------------------------------------------------------*/
12 #include "librpm.h"
13 extern "C"
14 {
15 #ifdef _RPM_5
16 #undef RPM_NULL_TYPE
17 #define RPM_NULL_TYPE rpmTagType(0)
18 typedef rpmuint32_t rpm_count_t;
19 #endif
20 }
21 
22 #include <iostream>
23 
24 #include <zypp/base/Logger.h>
25 #include <zypp/base/NonCopyable.h>
26 #include <zypp/base/String.h>
27 #include <zypp/AutoDispose.h>
28 
30 
31 using std::endl;
32 
33 #undef Y2LOG
34 #define Y2LOG "BinHeader"
35 
36 namespace zypp
37 {
38 namespace target
39 {
40 namespace rpm
41 {
42 
49  {
50  public:
51  HeaderEntryGetter( const Header & h_r, rpmTag & tag_r );
53  rpmTagType type();
54  rpm_count_t cnt();
55  void * val();
56  private:
57 #ifndef _RPM_5
58  ::rpmtd _rpmtd;
59 #else
60  rpmTagType _type;
61  rpm_count_t _cnt;
62  void * _val;
63 #endif //_RPM_5
64  };
65 
66 #ifndef _RPM_5
67  inline HeaderEntryGetter::HeaderEntryGetter( const Header & h_r, rpmTag & tag_r )
68  : _rpmtd( ::rpmtdNew() )
69  { ::headerGet( h_r, tag_r, _rpmtd, HEADERGET_DEFAULT ); }
71  { ::rpmtdFreeData( _rpmtd ); ::rpmtdFree( _rpmtd ); }
72  inline rpmTagType HeaderEntryGetter::type() { return rpmtdType( _rpmtd ); }
73  inline rpm_count_t HeaderEntryGetter::cnt() { return _rpmtd->count; }
74  inline void * HeaderEntryGetter::val() { return _rpmtd->data; }
75 #else
76  inline HeaderEntryGetter::HeaderEntryGetter( const Header & h_r, rpmTag & tag_r )
77  : _type( RPM_NULL_TYPE )
78  , _cnt( 0 )
79  , _val( 0 )
80  { ::headerGetEntry( h_r, tag_r, hTYP_t(&_type), &_val, &_cnt ); }
82  { if ( _val && _type == RPM_STRING_ARRAY_TYPE ) free( _val ); }
83  inline rpmTagType HeaderEntryGetter::type() { return _type; }
84  inline rpm_count_t HeaderEntryGetter::cnt() { return _cnt; }
85  inline void * HeaderEntryGetter::val() { return _val; }
86 #endif //_RPM_5
87 
89 //
90 // CLASS NAME : BinHeader::intList
91 //
93 
94 unsigned BinHeader::intList::set( void * val_r, unsigned cnt_r, rpmTagType type_r )
95 {
96  _type = type_r; // remember the type!
97  if ( val_r )
98  switch ( _type )
99  {
100 #if RPM_CHAR_TYPE != RPM_INT8_TYPE
101  case RPM_CHAR_TYPE:
102  std::vector<long>( (char*)val_r, ((char*)val_r)+cnt_r ).swap( _data );
103  break;
104 #endif
105  case RPM_INT8_TYPE:
106  std::vector<long>( (int8_t*)val_r, ((int8_t*)val_r)+cnt_r ).swap( _data );
107  break;
108  case RPM_INT16_TYPE:
109  std::vector<long>( (int16_t*)val_r, ((int16_t*)val_r)+cnt_r ).swap( _data );
110  break;
111  case RPM_INT32_TYPE:
112  std::vector<long>( (int32_t*)val_r, ((int32_t*)val_r)+cnt_r ).swap( _data );
113  break;
114 #ifndef _RPM_5
115  case RPM_INT64_TYPE:
116  std::vector<long>( (int64_t*)val_r, ((int64_t*)val_r)+cnt_r ).swap( _data );
117  break;
118 #endif
119  default:
120  std::vector<long>( cnt_r, 0L ).swap( _data );
121  break;
122  }
123  else
124  _data.clear();
125  return _data.size();
126 }
127 
129 //
130 // CLASS NAME : BinHeader::stringList
131 //
133 
134 unsigned BinHeader::stringList::set( char ** val_r, unsigned cnt_r )
135 {
136  if ( val_r )
137  std::vector<std::string>( val_r, val_r+cnt_r ).swap( _data );
138  else
139  _data.clear();
140  return _data.size();
141 }
142 
144 //
145 // CLASS NAME : BinHeader
146 //
148 
150 //
151 //
152 // METHOD NAME : BinHeader::BinHeader
153 // METHOD TYPE : Constructor
154 //
155 BinHeader::BinHeader( Header h_r )
156  : _h( h_r )
157 {
158  if ( _h )
159  {
160  headerLink( _h );
161  }
162 }
163 
165 //
166 //
167 // METHOD NAME : BinHeader::BinHeader
168 // METHOD TYPE : Constructor
169 //
171 {
172  INT << "INJECT from " << rhs;
173  if ( ! (rhs && rhs->_h) )
174  {
175  _h = 0;
176  }
177  else
178  {
179  _h = rhs->_h; // ::headerLink already done in rhs
180  rhs->_h = 0;
181  }
182  INT << ": " << *this << " (" << rhs << ")" << endl;
183 }
184 
186 //
187 //
188 // METHOD NAME : BinHeader::~BinHeader
189 // METHOD TYPE : Destructor
190 //
192 {
193  if ( _h )
194  {
195  headerFree( _h );
196  }
197 }
198 
200 //
201 //
202 // METHOD NAME : BinHeader::assertHeader
203 // METHOD TYPE : void
204 //
206 {
207  if ( !_h )
208  {
209  _h = ::headerNew();
210  if ( !_h )
211  {
212  INT << "OOPS: NULL HEADER created!" << endl;
213  return false;
214  }
215  }
216  return true;
217 }
218 
220 //
221 //
222 // METHOD NAME : BinHeader::has_tag
223 // METHOD TYPE : bool
224 //
225 // DESCRIPTION :
226 //
227 bool BinHeader::has_tag( tag tag_r ) const
228 {
229  return( !empty() && ::headerIsEntry( _h, tag_r ) );
230 }
231 
233 //
234 //
235 // METHOD NAME : BinHeader::int_list
236 // METHOD TYPE : unsigned
237 //
238 // DESCRIPTION :
239 //
240 unsigned BinHeader::int_list( tag tag_r, intList & lst_r ) const
241 {
242  if ( !empty() )
243  {
244  HeaderEntryGetter headerget( _h, tag_r );
245 
246  if ( headerget.val() )
247  {
248  switch ( headerget.type() )
249  {
250  case RPM_NULL_TYPE:
251  return lst_r.set( 0, 0, headerget.type() );
252 #if RPM_CHAR_TYPE != RPM_INT8_TYPE
253  case RPM_CHAR_TYPE:
254 #endif
255  case RPM_INT8_TYPE:
256  case RPM_INT16_TYPE:
257  case RPM_INT32_TYPE:
258 #ifndef _RPM_5
259  case RPM_INT64_TYPE:
260 #endif
261  return lst_r.set( headerget.val(), headerget.cnt(), headerget.type() );
262 
263  default:
264  INT << "RPM_TAG MISSMATCH: RPM_INTxx_TYPE " << tag_r << " got type " << headerget.type() << endl;
265  }
266  }
267  }
268  return lst_r.set( 0, 0, RPM_NULL_TYPE );
269 }
270 
272 //
273 //
274 // METHOD NAME : BinHeader::string_list
275 // METHOD TYPE : unsigned
276 //
277 // DESCRIPTION :
278 //
279 unsigned BinHeader::string_list( tag tag_r, stringList & lst_r ) const
280 {
281  if ( !empty() )
282  {
283  HeaderEntryGetter headerget( _h, tag_r );
284 
285  if ( headerget.val() )
286  {
287  switch ( headerget.type() )
288  {
289  case RPM_NULL_TYPE:
290  return lst_r.set( 0, 0 );
291  case RPM_STRING_ARRAY_TYPE:
292  return lst_r.set( (char**)headerget.val(), headerget.cnt() );
293 
294  default:
295  INT << "RPM_TAG MISSMATCH: RPM_STRING_ARRAY_TYPE " << tag_r << " got type " << headerget.type() << endl;
296  }
297  }
298  }
299  return lst_r.set( 0, 0 );
300 }
301 
303 //
304 //
305 // METHOD NAME : BinHeader::int_val
306 // METHOD TYPE : int
307 //
308 // DESCRIPTION :
309 //
310 int BinHeader::int_val( tag tag_r ) const
311 {
312  if ( !empty() )
313  {
314  HeaderEntryGetter headerget( _h, tag_r );
315 
316  if ( headerget.val() )
317  {
318  switch ( headerget.type() )
319  {
320  case RPM_NULL_TYPE:
321  return 0;
322 #if RPM_CHAR_TYPE != RPM_INT8_TYPE
323  case RPM_CHAR_TYPE:
324  return *((char*)headerget.val());
325 #endif
326  case RPM_INT8_TYPE:
327  return *((int8_t*)headerget.val());
328  case RPM_INT16_TYPE:
329  return *((int16_t*)headerget.val());
330  case RPM_INT32_TYPE:
331  return *((int32_t*)headerget.val());
332 #ifndef _RPM_5
333  case RPM_INT64_TYPE:
334  return *((int64_t*)headerget.val());
335 #endif
336 
337  default:
338  INT << "RPM_TAG MISSMATCH: RPM_INTxx_TYPE " << tag_r << " got type " << headerget.type() << endl;
339  }
340  }
341  }
342  return 0;
343 }
344 
346 //
347 //
348 // METHOD NAME : BinHeader::string_val
349 // METHOD TYPE : std::string
350 //
351 // DESCRIPTION :
352 //
353 std::string BinHeader::string_val( tag tag_r ) const
354 {
355  if ( !empty() )
356  {
357  HeaderEntryGetter headerget( _h, tag_r );
358 
359  if ( headerget.val() )
360  {
361  switch ( headerget.type() )
362  {
363  case RPM_NULL_TYPE:
364  return "";
365  case RPM_STRING_TYPE:
366  return (char*)headerget.val();
367 
368  default:
369  INT << "RPM_TAG MISSMATCH: RPM_STRING_TYPE " << tag_r << " got type " << headerget.type() << endl;
370  }
371  }
372  }
373  return "";
374 }
375 
376 std::string BinHeader::format(const char *fmt) const
377 {
378  zypp::AutoDispose<char *> form(headerFormat(_h, fmt, NULL), free);
379  if ( !form )
380  return std::string();
381 
382  return std::string(form);
383 }
384 
386 //
387 //
388 // METHOD NAME : BinHeader::stringList_val
389 // METHOD TYPE : std::list<std::string>
390 //
391 // DESCRIPTION :
392 //
393 std::list<std::string> BinHeader::stringList_val( tag tag_r ) const
394 {
395  std::list<std::string> ret;
396 
397  if ( !empty() )
398  {
399  stringList lines;
400  unsigned count = string_list( tag_r, lines );
401  for ( unsigned i = 0; i < count; ++i )
402  {
403  ret.push_back( lines[i] );
404  }
405  }
406  return ret;
407 }
408 
410 //
411 //
412 // METHOD NAME : BinHeader::dumpOn
413 // METHOD TYPE : ostream &
414 //
415 // DESCRIPTION :
416 //
417 std::ostream & BinHeader::dumpOn( std::ostream & str ) const
418 {
420  return str << '{' << (void*)_h << '}';
421 }
422 
423 } // namespace rpm
424 } // namespace target
425 } // namespace zypp
zypp::target::rpm::HeaderEntryGetter::val
void * val()
Definition: BinHeader.cc:74
BinHeader.h
std::dumpOn
std::ostream & dumpOn(std::ostream &str, const zypp::shared_ptr< void > &obj)
Definition: PtrTypes.h:151
zypp::target::rpm::BinHeader::string_val
std::string string_val(tag tag_r) const
Definition: BinHeader.cc:353
zypp::target::rpm::HeaderEntryGetter::type
rpmTagType type()
Definition: BinHeader.cc:72
zypp::target::rpm::BinHeader::intList::set
unsigned set(void *val_r, unsigned cnt_r, rpmTagType type_r)
Definition: BinHeader.cc:94
zypp::target::rpm::HeaderEntryGetter::HeaderEntryGetter
HeaderEntryGetter(const Header &h_r, rpmTag &tag_r)
Definition: BinHeader.cc:67
zypp::target::rpm::BinHeader::int_list
unsigned int_list(tag tag_r, intList &lst_r) const
Definition: BinHeader.cc:240
zypp::target::rpm::HeaderEntryGetter::cnt
rpm_count_t cnt()
Definition: BinHeader.cc:73
zypp::target::rpm::BinHeader::empty
bool empty() const
Definition: BinHeader.h:77
INT
#define INT
Definition: Logger.h:83
zypp::target::rpm::BinHeader::has_tag
bool has_tag(tag tag_r) const
Definition: BinHeader.cc:227
zypp::target::rpm::BinHeader::assertHeader
bool assertHeader()
Definition: BinHeader.cc:205
zypp::target::rpm::BinHeader::int_val
int int_val(tag tag_r) const
Definition: BinHeader.cc:310
zypp::target::rpm::HeaderEntryGetter
Helper for header data retieval.
Definition: BinHeader.cc:48
zypp::target::rpm::BinHeader::stringList_val
std::list< std::string > stringList_val(tag tag_r) const
Definition: BinHeader.cc:393
zypp::target::rpm::BinHeader::~BinHeader
virtual ~BinHeader()
Definition: BinHeader.cc:191
zypp::target::rpm::BinHeader::stringList::set
unsigned set(char **val_r, unsigned cnt_r)
Definition: BinHeader.cc:134
zypp::target::rpm::HeaderEntryGetter::_rpmtd
::rpmtd _rpmtd
Definition: BinHeader.cc:58
zypp::base::NonCopyable
boost::noncopyable NonCopyable
Ensure derived classes cannot be copied.
Definition: NonCopyable.h:26
Logger.h
zypp::str::form
std::string form(const char *format,...) __attribute__((format(printf
Printf style construction of std::string.
Definition: String.cc:35
zypp
Easy-to use interface to the ZYPP dependency resolver.
Definition: CodePitfalls.doc:1
_data
std::list< PublicKeyData > _data
Definition: KeyRing.cc:145
zypp::target::rpm::BinHeader::intList
Definition: BinHeader.h:110
AutoDispose.h
zypp::target::rpm::BinHeader::intList::_data
std::vector< long > _data
Definition: BinHeader.h:131
zypp::target::rpm::BinHeader::_h
Header _h
Definition: BinHeader.h:55
zypp::target::rpm::BinHeader::format
std::string format(const char *fmt) const
Definition: BinHeader.cc:376
zypp::target::rpm::BinHeader::stringList
Definition: BinHeader.h:143
zypp::target::rpm::BinHeader::intList::_type
rpmTagType _type
Definition: BinHeader.h:132
zypp::target::rpm::HeaderEntryGetter::~HeaderEntryGetter
~HeaderEntryGetter()
Definition: BinHeader.cc:70
zypp::AutoDispose
Reference counted access to a Tp object calling a custom Dispose function when the last AutoDispose h...
Definition: AutoDispose.h:92
String.h
zypp::target::rpm::BinHeader::dumpOn
virtual std::ostream & dumpOn(std::ostream &str) const
Overload to realize std::ostream & operator<<.
Definition: BinHeader.cc:417
zypp::target::rpm::BinHeader::Ptr
intrusive_ptr< BinHeader > Ptr
Definition: BinHeader.h:47
librpm.h
str
String related utilities and Regular expression matching.
zypp::target::rpm::BinHeader::BinHeader
BinHeader(Header h_r=0)
Definition: BinHeader.cc:155
zypp::target::rpm::BinHeader::tag
rpmTag tag
Definition: BinHeader.h:51
zypp::target::rpm::BinHeader::string_list
unsigned string_list(tag tag_r, stringList &lst_r) const
Definition: BinHeader.cc:279
NonCopyable.h