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