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