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 ) )
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() );
PathInfo _dbV4
rpmV4 database (_dbDir/Packages)
int assert_dir(const Pathname &path, unsigned mode)
Like 'mkdir -p'.
librpmDb::constPtr _dbptr
PathInfo _dbV3ToV4
rpmV3 database backup created on conversion to rpmV4 (_dbDir/packages.rpm3)
#define ZYPP_THROW(EXCPT)
Drops a logline and throws the Exception.
void operator++()
Advance to next RpmHeader::constPtr.
bool findByProvides(const std::string &tag_r)
Reset to iterate all packages that provide a certain tag.
shared_ptr< RpmException > error() const
Return any database error.
bool advance()
Advance to the first/next header in iterator.
Pathname _dbPath
Directory that contains the rpmdb.
shared_ptr< RpmException > _error
bool set(int off_r)
Create an itertator that contains the database entry located at off_r, and advance to the 1st header...
Collect info about what kind of rpmdb seems to be present by looking at paths and filenames...
static void dbAccess()
Access the database at the current default location.
bool findByRequiredBy(const std::string &tag_r)
Reset to iterate all packages that require a certain tag.
virtual std::ostream & dumpOn(std::ostream &str) const
Dump debug info.
const Pathname & root() const
Root directory for all operations.
Edition represents [epoch:]version[-release]
bool illegalArgs() const
Whether constructor arguments were illegal.
db_const_iterator(const db_const_iterator &)
DbDirInfo(const Pathname &root_r, const Pathname &dbPath_r)
For Constructor arguments see accessPath.
string stringPath(const Pathname &root_r, const Pathname &sub_r)
Subclass to retrieve database content.
virtual ~librpmDb()
Destructor.
const Pathname & root() const
#define ZYPP_RETHROW(EXCPT)
Drops a logline and rethrows, updating the CodeLocation.
void * dont_call_it() const
Dont call it ;) It's for development and testing only.
shared_ptr< RpmException > dbError() const
Return any database error.
bool findByName(const std::string &name_r)
Reset to iterate all packages with a certain name.
PathInfo _dbDir
database directory (unset on illegal constructor arguments)
static unsigned dbRelease(bool force_r=false)
If there are no outstanding references to the database (e.g.
const RpmHeader::constPtr & operator*() const
Returns the current RpmHeader::constPtr or NULL, if no more entries available.
std::ostream & dumpOn(std::ostream &str, const Capability &obj)
bool findByFile(const std::string &file_r)
Reset to iterate all packages that own a certain file.
Just inherits Exception to separate media exceptions.
ReferenceCounted & operator=(const ReferenceCounted &)
Assignment.
Manage access to librpm database.
bool findPackage(const std::string &name_r)
Find package by name.
virtual void unref_to(unsigned refCount_r) const
Trigger from Rep, after refCount was decreased.
shared_ptr< RpmException > _dberr
#define ZYPP_CAUGHT(EXCPT)
Drops a logline telling the Exception was caught (in order to handle it).
friend ostream & operator<<(ostream &str, const D &obj)
bool findAll()
Reset to iterate all packages.
D(librpmDb::constPtr dbptr_r)
librpmDb internal database handle
~db_const_iterator()
Destructor.
bool findByConflicts(const std::string &tag_r)
Reset to iterate all packages that conflict with a certain tag.
D(const Pathname &root_r, const Pathname &dbPath_r, bool readonly_r)
unsigned dbHdrNum() const
Returns the current headers index in database, 0 if no header.
bool destroy()
Destroy iterator.
bool init(int rpmtag, const void *keyp=NULL, size_t keylen=0)
Access a dbindex file and advance to the 1st header.
intrusive_ptr< const librpmDb > constPtr
std::string asString(const std::string &t)
Global asString() that works with std::string too.
bool create(int rpmtag, const void *keyp=NULL, size_t keylen=0)
Let iterator access a dbindex file.
Pathname _root
Root directory for all operations.
RpmHeader::constPtr _hptr
PathInfo _dbV3
rpmV3 database (_dbDir/packages.rpm)
const Pathname & dbPath() const
Directory that contains the rpmdb.
void restat()
Restat all paths.
const Pathname & dbPath() const
TraitsType::constPtrType constPtr
friend std::ostream & operator<<(std::ostream &str, const ReferenceCounted &obj)
Stream output via dumpOn.