libzypp 17.37.10

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