BinHeader.cc
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00012 #include "librpm.h"
00013 extern "C"
00014 {
00015 #ifdef _RPM_5
00016 #undef RPM_NULL_TYPE
00017 #define RPM_NULL_TYPE rpmTagType(0)
00018 #endif
00019
00020 #ifndef _RPM_4_4_COMPAT
00021 #ifdef _RPM_5
00022 typedef rpmuint32_t rpm_count_t;
00023 #else
00024 typedef int32_t rpm_count_t;
00025 #endif
00026 #endif
00027 }
00028
00029 #include <iostream>
00030
00031 #include "zypp/base/Logger.h"
00032
00033 #include "zypp/target/rpm/BinHeader.h"
00034
00035 using namespace std;
00036
00037 #undef Y2LOG
00038 #define Y2LOG "BinHeader"
00039
00040 namespace zypp
00041 {
00042 namespace target
00043 {
00044 namespace rpm
00045 {
00046
00048
00049
00050
00052
00053 BinHeader::intList::intList()
00054 : cnt( 0 ), val( 0 ), type( RPM_NULL_TYPE )
00055 {}
00056
00057 unsigned BinHeader::intList::set( void * val_r, unsigned cnt_r, rpmTagType type_r )
00058 {
00059 val = val_r;
00060 cnt = val ? cnt_r : 0;
00061 type = type_r;
00062 return cnt;
00063 }
00064
00065 int BinHeader::intList::operator[]( const unsigned idx_r ) const
00066 {
00067 if ( idx_r < cnt )
00068 {
00069 switch ( type )
00070 {
00071 #if RPM_CHAR_TYPE != RPM_INT8_TYPE
00072 case RPM_CHAR_TYPE:
00073 return ((char*)val)[idx_r];
00074 #endif
00075 case RPM_INT8_TYPE:
00076 return ((int8_t*)val)[idx_r];
00077 case RPM_INT16_TYPE:
00078 return ((int16_t*)val)[idx_r];
00079 case RPM_INT32_TYPE:
00080 return ((int32_t*)val)[idx_r];
00081 }
00082 }
00083 return 0;
00084 }
00085
00087
00088
00089
00091
00092 void BinHeader::stringList::clear()
00093 {
00094 if ( val )
00095 free( val );
00096 val = 0;
00097 cnt = 0;
00098 }
00099
00100 BinHeader::stringList::stringList()
00101 : cnt( 0 ), val( 0 )
00102 {}
00103
00104 unsigned BinHeader::stringList::set( char ** val_r, unsigned cnt_r )
00105 {
00106 clear();
00107 val = val_r;
00108 cnt = val ? cnt_r : 0;
00109 return cnt;
00110 }
00111
00112 std::string BinHeader::stringList::operator[]( const unsigned idx_r ) const
00113 {
00114 return( idx_r < cnt ? val[idx_r] : "" );
00115 }
00116
00118
00119
00120
00122
00124
00125
00126
00127
00128
00129 BinHeader::BinHeader( Header h_r )
00130 : _h( h_r )
00131 {
00132 if ( _h )
00133 {
00134 headerLink( _h );
00135 }
00136 }
00137
00139
00140
00141
00142
00143
00144 BinHeader::BinHeader( BinHeader::Ptr & rhs )
00145 {
00146 INT << "INJECT from " << rhs;
00147 if ( ! (rhs && rhs->_h) )
00148 {
00149 _h = 0;
00150 }
00151 else
00152 {
00153 _h = rhs->_h;
00154 rhs->_h = 0;
00155 }
00156 INT << ": " << *this << " (" << rhs << ")" << endl;
00157 }
00158
00160
00161
00162
00163
00164
00165 BinHeader::~BinHeader()
00166 {
00167 if ( _h )
00168 {
00169 headerFree( _h );
00170 }
00171 }
00172
00174
00175
00176
00177
00178
00179 bool BinHeader::assertHeader()
00180 {
00181 if ( !_h )
00182 {
00183 _h = ::headerNew();
00184 if ( !_h )
00185 {
00186 INT << "OOPS: NULL HEADER created!" << endl;
00187 return false;
00188 }
00189 }
00190 return true;
00191 }
00192
00194
00195
00196
00197
00198
00199
00200
00201 bool BinHeader::has_tag( tag tag_r ) const
00202 {
00203 return( !empty() && ::headerIsEntry( _h, tag_r ) );
00204 }
00205
00207
00208
00209
00210
00211
00212
00213
00214 unsigned BinHeader::int_list( tag tag_r, intList & lst_r ) const
00215 {
00216 if ( !empty() )
00217 {
00218 rpmTagType type = RPM_NULL_TYPE;
00219 rpm_count_t cnt = 0;
00220 void * val = 0;
00221 ::headerGetEntry( _h, tag_r, hTYP_t(&type), &val, &cnt );
00222
00223 if ( val )
00224 {
00225 switch ( type )
00226 {
00227 case RPM_NULL_TYPE:
00228 return lst_r.set( 0, 0, type );
00229 #if RPM_CHAR_TYPE != RPM_INT8_TYPE
00230 case RPM_CHAR_TYPE:
00231 #endif
00232 case RPM_INT8_TYPE:
00233 case RPM_INT16_TYPE:
00234 case RPM_INT32_TYPE:
00235 return lst_r.set( val, cnt, type );
00236
00237 case RPM_STRING_ARRAY_TYPE:
00238 free( val );
00239
00240 default:
00241 INT << "RPM_TAG MISSMATCH: RPM_INT32_TYPE " << tag_r << " got type " << type << endl;
00242 }
00243 }
00244 }
00245 return lst_r.set( 0, 0, RPM_NULL_TYPE );
00246 }
00247
00249
00250
00251
00252
00253
00254
00255
00256 unsigned BinHeader::string_list( tag tag_r, stringList & lst_r ) const
00257 {
00258 if ( !empty() )
00259 {
00260 rpmTagType type = RPM_NULL_TYPE;
00261 rpm_count_t cnt = 0;
00262 void * val = 0;
00263 ::headerGetEntry( _h, tag_r, hTYP_t(&type), &val, &cnt );
00264
00265 if ( val )
00266 {
00267 switch ( type )
00268 {
00269 case RPM_NULL_TYPE:
00270 return lst_r.set( 0, 0 );
00271 case RPM_STRING_ARRAY_TYPE:
00272 return lst_r.set( (char**)val, cnt );
00273
00274 default:
00275 INT << "RPM_TAG MISSMATCH: RPM_STRING_ARRAY_TYPE " << tag_r << " got type " << type << endl;
00276 }
00277 }
00278 }
00279 return lst_r.set( 0, 0 );
00280 }
00281
00283
00284
00285
00286
00287
00288
00289
00290 int BinHeader::int_val( tag tag_r ) const
00291 {
00292 if ( !empty() )
00293 {
00294 rpmTagType type = RPM_NULL_TYPE;
00295 rpm_count_t cnt = 0;
00296 void * val = 0;
00297 ::headerGetEntry( _h, tag_r, hTYP_t(&type), &val, &cnt );
00298
00299 if ( val )
00300 {
00301 switch ( type )
00302 {
00303 case RPM_NULL_TYPE:
00304 return 0;
00305 #if RPM_CHAR_TYPE != RPM_INT8_TYPE
00306 case RPM_CHAR_TYPE:
00307 return *((char*)val);
00308 #endif
00309 case RPM_INT8_TYPE:
00310 return *((int8_t*)val);
00311 case RPM_INT16_TYPE:
00312 return *((int16_t*)val);
00313 case RPM_INT32_TYPE:
00314 return *((int32_t*)val);
00315
00316 case RPM_STRING_ARRAY_TYPE:
00317 free( val );
00318
00319 default:
00320 INT << "RPM_TAG MISSMATCH: RPM_INT32_TYPE " << tag_r << " got type " << type << endl;
00321 }
00322 }
00323 }
00324 return 0;
00325 }
00326
00328
00329
00330
00331
00332
00333
00334
00335 std::string BinHeader::string_val( tag tag_r ) const
00336 {
00337 if ( !empty() )
00338 {
00339 rpmTagType type = RPM_NULL_TYPE;
00340 rpm_count_t cnt = 0;
00341 void * val = 0;
00342 ::headerGetEntry( _h, tag_r, hTYP_t(&type), &val, &cnt );
00343
00344 if ( val )
00345 {
00346 switch ( type )
00347 {
00348 case RPM_NULL_TYPE:
00349 return "";
00350 case RPM_STRING_TYPE:
00351 return (char*)val;
00352
00353 case RPM_STRING_ARRAY_TYPE:
00354 free( val );
00355
00356 default:
00357 INT << "RPM_TAG MISSMATCH: RPM_STRING_TYPE " << tag_r << " got type " << type << endl;
00358 }
00359 }
00360 }
00361 return "";
00362 }
00363
00365
00366
00367
00368
00369
00370
00371
00372 std::list<std::string> BinHeader::stringList_val( tag tag_r ) const
00373 {
00374 std::list<std::string> ret;
00375
00376 if ( !empty() )
00377 {
00378 stringList lines;
00379 unsigned count = string_list( tag_r, lines );
00380 for ( unsigned i = 0; i < count; ++i )
00381 {
00382 ret.push_back( lines[i] );
00383 }
00384 }
00385 return ret;
00386 }
00387
00389
00390
00391
00392
00393
00394
00395
00396 ostream & BinHeader::dumpOn( ostream & str ) const
00397 {
00398 ReferenceCounted::dumpOn( str );
00399 return str << '{' << (void*)_h << '}';
00400 }
00401
00402 }
00403 }
00404 }