38 D & operator=(
const D & );
53 D(
const Pathname & root_r,
const Pathname & dbPath_r,
bool readonly_r )
60 ::addMacro( NULL,
"_dbpath", NULL, _dbPath.asString().c_str(), RMIL_CMDLINE );
62 _ts = ::rpmtsCreate();
63 ::rpmtsSetRootDir( _ts, _root.c_str() );
66 PathInfo master( _root + _dbPath +
"Packages" );
67 if ( ! master.isFile() )
72 ERR <<
"Could not create dbpath " << (_root + _dbPath).
asString() << endl;
76 int res = ::rpmtsInitDB( _ts, 0644 );
79 ERR <<
"rpmdbInit error(" << res <<
"): " << *
this << endl;
87 int res = ::rpmtsOpenDB( _ts, (readonly_r ? O_RDONLY : O_RDWR ));
90 ERR <<
"rpmdbOpen error(" << res <<
"): " << *
this << endl;
97 DBG <<
"DBACCESS " << *
this << endl;
117 Pathname librpmDb::_defaultRoot (
"/" );
118 Pathname librpmDb::_defaultDbPath(
"/var/lib/rpm" );
120 bool librpmDb::_dbBlocked (
true );
128 bool librpmDb::globalInit()
130 static bool initialized =
false;
135 int rc = ::rpmReadConfigFiles( NULL, NULL );
138 ERR <<
"rpmReadConfigFiles returned " << rc << endl;
144 #define OUTVAL(n) << " (" #n ":" << expand( "%{" #n "}" ) << ")"
145 MIL <<
"librpm init done:"
159 std::string librpmDb::expand(
const std::string & macro_r )
161 if ( ! globalInit() )
164 char * val = ::rpmExpand( macro_r.c_str(), NULL );
179 librpmDb * librpmDb::newLibrpmDb( Pathname root_r, Pathname dbPath_r,
bool readonly_r )
182 if ( ! (root_r.absolute() && dbPath_r.absolute()) )
188 if ( ! globalInit() )
197 ret =
new librpmDb( root_r, dbPath_r, readonly_r );
215 void librpmDb::dbAccess(
const Pathname & root_r,
const Pathname & dbPath_r )
218 if ( ! (root_r.absolute() && dbPath_r.absolute()) )
226 if ( _defaultRoot == root_r && _defaultDbPath == dbPath_r )
235 _defaultRoot = root_r;
236 _defaultDbPath = dbPath_r;
237 MIL <<
"Set new database location: " <<
stringPath( _defaultRoot, _defaultDbPath ) << endl;
248 void librpmDb::dbAccess()
258 _defaultDb = newLibrpmDb( _defaultRoot, _defaultDbPath,
true );
289 unsigned librpmDb::dbRelease(
bool force_r )
296 unsigned outstanding = _defaultDb->refCount() - 1;
298 switch ( outstanding )
303 DBG <<
"dbRelease: keep access, outstanding " << outstanding << endl;
308 DBG <<
"dbRelease: release" << (force_r && outstanding ?
"(forced)" :
"")
309 <<
", outstanding " << outstanding << endl;
311 _defaultDb->_d._error = shared_ptr<RpmAccessBlockedException>(
new RpmAccessBlockedException(_defaultDb->_d._root, _defaultDb->_d._dbPath));
326 unsigned librpmDb::blockAccess()
328 MIL <<
"Block access" << endl;
330 return dbRelease(
true );
339 void librpmDb::unblockAccess()
341 MIL <<
"Unblock access" << endl;
351 ostream & librpmDb::dumpState( ostream & str )
355 return str <<
"[librpmDb " << (_dbBlocked?
"BLOCKED":
"CLOSED") <<
" " <<
stringPath( _defaultRoot, _defaultDbPath ) <<
"]";
357 return str <<
"[" << _defaultDb <<
"]";
374 librpmDb::librpmDb(
const Pathname & root_r,
const Pathname & dbPath_r,
bool readonly_r )
375 : _d( * new
D( root_r, dbPath_r, readonly_r ) )
399 if ( refCount_r == 1 )
475 return rpmtsGetRdb(
_d.
_ts);
506 , _dbPath( dbPath_r )
509 if ( ! (root_r.absolute() && dbPath_r.absolute()) )
511 ERR <<
"Relative path for root(" <<
_root <<
") or dbPath(" <<
_dbPath <<
")" << endl;
519 DBG << *
this << endl;
535 DBG << *
this << endl;
548 str <<
"ILLEGAL: '(" << obj.
root() <<
")" << obj.
dbPath() <<
"'";
552 str <<
"'(" << obj.
root() <<
")" << obj.
dbPath() <<
"':" << endl;
553 str <<
" Dir: " << obj.
_dbDir << endl;
554 str <<
" V4: " << obj.
_dbV4 << endl;
555 str <<
" V3: " << obj.
_dbV3 << endl;
595 WAR <<
"No database access: " << _dberr << endl;
608 ::rpmdbFreeIterator( _mi );
616 bool create(
int rpmtag,
const void * keyp = NULL,
size_t keylen = 0 )
621 _mi = ::rpmtsInitIterator( _dbptr->_d._ts, rpmTag(rpmtag), keyp, keylen );
633 _mi = ::rpmdbFreeIterator( _mi );
636 if ( _dbptr && _dbptr->error() )
638 _dberr = _dbptr->error();
639 WAR <<
"Lost database access: " << _dberr << endl;
653 Header h = ::rpmdbNextIterator( _mi );
666 bool init(
int rpmtag,
const void * keyp = NULL,
size_t keylen = 0 )
668 if ( ! create( rpmtag, keyp, keylen ) )
677 bool set(
int off_r )
679 if ( ! create( RPMDBI_PACKAGES ) )
681 #warning TESTCASE: rpmdbAppendIterator and (non)sequential access?
682 ::rpmdbAppendIterator( _mi, &off_r, 1 );
688 return( _mi ? ::rpmdbGetIteratorOffset( _mi ) : 0 );
695 int ret = ::rpmdbGetIteratorCount( _mi );
696 #warning TESTCASE: rpmdbGetIteratorCount returns 0 on sequential access?
697 return( ret ? ret : -1 );
716 :
_d( * new
D( dbptr_r ) )
774 return _d._dbptr->error();
787 str <<
"db_const_iterator(" << obj.
_d._dbptr
788 <<
" Size:" << obj.
_d.size()
789 <<
" HdrNum:" << obj.
_d.offset()
802 return _d.init( RPMDBI_PACKAGES );
813 return _d.init( RPMTAG_BASENAMES, file_r.c_str() );
824 return _d.init( RPMTAG_PROVIDENAME, tag_r.c_str() );
835 return _d.init( RPMTAG_REQUIRENAME, tag_r.c_str() );
846 return _d.init( RPMTAG_CONFLICTNAME, tag_r.c_str() );
857 return _d.init( RPMTAG_NAME, name_r.c_str() );
868 if ( !
_d.init( RPMTAG_NAME, name_r.c_str() ) )
871 if (
_d.size() == 1 )
877 for ( ; operator*(); operator++() )
879 if (
operator*()->tag_installtime() > itime )
882 itime = operator*()->tag_installtime();
886 return _d.set( match );
897 if ( !
_d.init( RPMTAG_NAME, name_r.c_str() ) )
900 for ( ; operator*(); operator++() )
902 if ( ed_r ==
operator*()->tag_edition() )
904 int match =
_d.offset();
905 return _d.set( match );
923 return findPackage( which_r->name(), which_r->edition() );