libzypp  17.30.2
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 MISMATCH: 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 MISMATCH: 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 MISMATCH: RPM_INTxx_TYPE " << tag_r << " got type " << headerget.type() << endl;
339  }
340  }
341  }
342  return 0;
343 }
344 
346 {
347  if ( !empty() )
348  {
349  HeaderEntryGetter headerget( _h, tag_r );
350 
351  if ( headerget.val() )
352  {
353  switch ( headerget.type() )
354  {
355  case RPM_NULL_TYPE:
356  return {};
357  case RPM_BIN_TYPE:
358  return ByteArray( reinterpret_cast<char *>( headerget.val() ), headerget.cnt() );
359  default:
360  INT << "RPM_TAG MISSMATCH: RPM_BIN_TYPE " << tag_r << " got type " << headerget.type() << endl;
361  }
362  }
363  }
364  return {};
365 }
366 
368 //
369 //
370 // METHOD NAME : BinHeader::string_val
371 // METHOD TYPE : std::string
372 //
373 // DESCRIPTION :
374 //
375 std::string BinHeader::string_val( tag tag_r ) const
376 {
377  if ( !empty() )
378  {
379  HeaderEntryGetter headerget( _h, tag_r );
380 
381  if ( headerget.val() )
382  {
383  switch ( headerget.type() )
384  {
385  case RPM_NULL_TYPE:
386  return "";
387  case RPM_STRING_TYPE:
388  return (char*)headerget.val();
389 
390  default:
391  INT << "RPM_TAG MISMATCH: RPM_STRING_TYPE " << tag_r << " got type " << headerget.type() << endl;
392  }
393  }
394  }
395  return "";
396 }
397 
398 std::string BinHeader::format(const char *fmt) const
399 {
400  zypp::AutoDispose<char *> form(headerFormat(_h, fmt, NULL), free);
401  if ( !form )
402  return std::string();
403 
404  return std::string(form);
405 }
406 
407 Header BinHeader::get() const
408 {
409  return _h;
410 }
411 
413 //
414 //
415 // METHOD NAME : BinHeader::stringList_val
416 // METHOD TYPE : std::list<std::string>
417 //
418 // DESCRIPTION :
419 //
420 std::list<std::string> BinHeader::stringList_val( tag tag_r ) const
421 {
422  std::list<std::string> ret;
423 
424  if ( !empty() )
425  {
426  stringList lines;
427  unsigned count = string_list( tag_r, lines );
428  for ( unsigned i = 0; i < count; ++i )
429  {
430  ret.push_back( lines[i] );
431  }
432  }
433  return ret;
434 }
435 
437 //
438 //
439 // METHOD NAME : BinHeader::dumpOn
440 // METHOD TYPE : ostream &
441 //
442 // DESCRIPTION :
443 //
444 std::ostream & BinHeader::dumpOn( std::ostream & str ) const
445 {
447  return str << '{' << (void*)_h << '}';
448 }
449 
450 } // namespace rpm
451 } // namespace target
452 } // namespace zypp
std::list< PublicKeyData > _data
Definition: KeyRing.cc:191
Reference counted access to a Tp object calling a custom Dispose function when the last AutoDispose h...
Definition: AutoDispose.h:94
unsigned set(void *val_r, unsigned cnt_r, rpmTagType type_r)
Definition: BinHeader.cc:94
unsigned set(char **val_r, unsigned cnt_r)
Definition: BinHeader.cc:134
std::string string_val(tag tag_r) const
Definition: BinHeader.cc:375
unsigned string_list(tag tag_r, stringList &lst_r) const
Definition: BinHeader.cc:279
virtual std::ostream & dumpOn(std::ostream &str) const
Overload to realize std::ostream & operator<<.
Definition: BinHeader.cc:444
std::string format(const char *fmt) const
Definition: BinHeader.cc:398
ByteArray blob_val(tag tag_r) const
Definition: BinHeader.cc:345
intrusive_ptr< BinHeader > Ptr
Definition: BinHeader.h:48
int int_val(tag tag_r) const
Definition: BinHeader.cc:310
std::list< std::string > stringList_val(tag tag_r) const
Definition: BinHeader.cc:420
bool has_tag(tag tag_r) const
Definition: BinHeader.cc:227
unsigned int_list(tag tag_r, intList &lst_r) const
Definition: BinHeader.cc:240
String related utilities and Regular expression matching.
boost::noncopyable NonCopyable
Ensure derived classes cannot be copied.
Definition: NonCopyable.h:26
std::string form(const char *format,...) __attribute__((format(printf
Printf style construction of std::string.
Definition: String.cc:36
Easy-to use interface to the ZYPP dependency resolver.
Definition: CodePitfalls.doc:2
std::ostream & dumpOn(std::ostream &str, const Capability &obj)
Definition: Capability.cc:567
Helper for header data retieval.
Definition: BinHeader.cc:49
HeaderEntryGetter(const Header &h_r, rpmTag &tag_r)
Definition: BinHeader.cc:67
#define INT
Definition: Logger.h:100