1 /*---------------------------------------------------------------------\
3 | |__ / \ / / . \ . \ |
8 \---------------------------------------------------------------------*/
9 /** \file zypp/target/rpm/librpmDb.cc
16 #include <zypp/base/Logger.h>
17 #include <zypp/PathInfo.h>
18 #include <zypp/target/rpm/librpmDb.h>
19 #include <zypp/target/rpm/RpmHeader.h>
20 #include <zypp/target/rpm/RpmException.h>
22 #undef ZYPP_BASE_LOGGER_LOGGROUP
23 #define ZYPP_BASE_LOGGER_LOGGROUP "librpmDb"
33 ///////////////////////////////////////////////////////////////////
35 // CLASS NAME : librpmDb::D
37 * @short librpmDb internal database handle
41 D & operator=( const D & ); // NO ASSIGNMENT!
42 D ( const D & ); // NO COPY!
45 const Pathname _root; // root directory for all operations
46 const Pathname _dbPath; // directory (below root) that contains the rpmdb
47 rpmts _ts; // transaction handle, includes database
48 shared_ptr<RpmException> _error; // database error
50 friend std::ostream & operator<<( std::ostream & str, const D & obj )
52 str << "{" << obj._error << "(" << obj._root << ")" << obj._dbPath << "}";
56 D( const Pathname & root_r, const Pathname & dbPath_r, bool readonly_r )
63 ::addMacro( NULL, "_dbpath", NULL, _dbPath.asString().c_str(), RMIL_CMDLINE );
65 _ts = ::rpmtsCreate();
66 ::rpmtsSetRootDir( _ts, _root.c_str() );
68 // open database (creates a missing one on the fly)
69 int res = ::rpmtsOpenDB( _ts, (readonly_r ? O_RDONLY : O_RDWR ));
72 ERR << "rpmdbOpen error(" << res << "): " << *this << endl;
73 _error = shared_ptr<RpmDbOpenException>(new RpmDbOpenException(_root, _dbPath));
79 DBG << "DBACCESS " << *this << endl;
91 ///////////////////////////////////////////////////////////////////
93 ///////////////////////////////////////////////////////////////////
95 // CLASS NAME : librpmDb (ststic interface)
97 ///////////////////////////////////////////////////////////////////
99 Pathname librpmDb::_defaultRoot { "/" };
100 Pathname librpmDb::_defaultDbPath; // set in dbAccess depending on suggestedDbPath below /root
101 Pathname librpmDb::_rpmDefaultDbPath; // set by globalInit
102 librpmDb::constPtr librpmDb::_defaultDb;
103 bool librpmDb::_dbBlocked = true;
105 ///////////////////////////////////////////////////////////////////
108 // METHOD NAME : librpmDb::globalInit
109 // METHOD TYPE : bool
111 bool librpmDb::globalInit()
113 static bool initialized = false;
118 int rc = ::rpmReadConfigFiles( NULL, NULL );
121 ERR << "rpmReadConfigFiles returned " << rc << endl;
125 initialized = true; // Necessary to be able to use exand().
126 _rpmDefaultDbPath = expand( "%{_dbpath}" );
128 MIL << "librpm init done: (_target:" << expand( "%{_target}" ) << ") (_dbpath:" << _rpmDefaultDbPath << ")" << endl;
132 ///////////////////////////////////////////////////////////////////
135 // METHOD NAME : librpmDb::expand
136 // METHOD TYPE : std::string
138 std::string librpmDb::expand( const std::string & macro_r )
140 if ( ! globalInit() )
141 return macro_r; // unexpanded
143 char * val = ::rpmExpand( macro_r.c_str(), NULL );
147 std::string ret( val );
152 ///////////////////////////////////////////////////////////////////
155 // METHOD NAME : librpmDb::newLibrpmDb
156 // METHOD TYPE : librpmDb *
158 librpmDb * librpmDb::newLibrpmDb()
161 if ( ! globalInit() )
163 ZYPP_THROW(GlobalRpmInitException());
170 ret = new librpmDb( _defaultRoot, _defaultDbPath, /*readonly*/true );
172 catch (const RpmException & excpt_r)
174 ZYPP_CAUGHT(excpt_r);
177 ZYPP_RETHROW(excpt_r);
183 Pathname librpmDb::suggestedDbPath( const Pathname & root_r )
185 if ( ! root_r.absolute() )
186 ZYPP_THROW(RpmInvalidRootException( root_r, "" ));
188 // initialize librpm (for _rpmDefaultDbPath)
189 if ( ! globalInit() )
190 ZYPP_THROW(GlobalRpmInitException());
192 if ( PathInfo( root_r ).isDir() ) {
193 // If a known dbpath exsists, we continue to use it
194 for ( auto p : { "/var/lib/rpm", "/usr/lib/sysimage/rpm" } ) {
195 if ( PathInfo( root_r/p, PathInfo::LSTAT/*!no symlink*/ ).isDir() ) {
196 MIL << "Suggest existing database at " << stringPath( root_r, p ) << endl;
202 MIL << "Suggest rpm _dbpath " << stringPath( root_r, _rpmDefaultDbPath ) << endl;
203 return _rpmDefaultDbPath;
206 ///////////////////////////////////////////////////////////////////
209 // METHOD NAME : librpmDb::dbAccess
210 // METHOD TYPE : PMError
212 void librpmDb::dbAccess( const Pathname & root_r )
216 // already accessing a database: switching is not allowed.
217 if ( _defaultRoot == root_r )
220 ZYPP_THROW(RpmDbAlreadyOpenException(_defaultRoot, _defaultDbPath, root_r, _defaultDbPath));
223 // got no database: we could switch to a new one (even if blocked!)
224 _defaultDbPath = suggestedDbPath( root_r ); // also asserts root_r is absolute
225 _defaultRoot = root_r;
227 MIL << "Set new database location: " << stringPath( _defaultRoot, _defaultDbPath ) << endl;
231 ///////////////////////////////////////////////////////////////////
234 // METHOD NAME : librpmDb::dbAccess
235 // METHOD TYPE : PMError
237 void librpmDb::dbAccess()
241 ZYPP_THROW(RpmAccessBlockedException(_defaultRoot, _defaultDbPath));
247 _defaultDb = newLibrpmDb();
251 ///////////////////////////////////////////////////////////////////
254 // METHOD NAME : librpmDb::dbAccess
255 // METHOD TYPE : PMError
257 void librpmDb::dbAccess( librpmDb::constPtr & ptr_r )
264 ///////////////////////////////////////////////////////////////////
267 // METHOD NAME : librpmDb::dbRelease
268 // METHOD TYPE : unsigned
270 unsigned librpmDb::dbRelease( bool force_r )
277 unsigned outstanding = _defaultDb->refCount() - 1; // refCount can't be 0
279 switch ( outstanding )
284 DBG << "dbRelease: keep access, outstanding " << outstanding << endl;
287 // else fall through:
289 DBG << "dbRelease: release" << (force_r && outstanding ? "(forced)" : "")
290 << ", outstanding " << outstanding << endl;
292 _defaultDb->_d._error = shared_ptr<RpmAccessBlockedException>(new RpmAccessBlockedException(_defaultDb->_d._root, _defaultDb->_d._dbPath));
293 // tag handle invalid
301 ///////////////////////////////////////////////////////////////////
304 // METHOD NAME : librpmDb::blockAccess
305 // METHOD TYPE : unsigned
307 unsigned librpmDb::blockAccess()
309 MIL << "Block access" << endl;
311 return dbRelease( /*force*/true );
314 ///////////////////////////////////////////////////////////////////
317 // METHOD NAME : librpmDb::unblockAccess
318 // METHOD TYPE : void
320 void librpmDb::unblockAccess()
322 MIL << "Unblock access" << endl;
326 ///////////////////////////////////////////////////////////////////
329 // METHOD NAME : librpmDb::dumpState
330 // METHOD TYPE : ostream &
332 std::ostream & librpmDb::dumpState( std::ostream & str )
336 return str << "[librpmDb " << (_dbBlocked?"BLOCKED":"CLOSED") << " " << stringPath( _defaultRoot, _defaultDbPath ) << "]";
338 return str << "[" << _defaultDb << "]";
341 ///////////////////////////////////////////////////////////////////
343 // CLASS NAME : librpmDb (internal database handle interface (nonstatic))
345 ///////////////////////////////////////////////////////////////////
347 ///////////////////////////////////////////////////////////////////
350 // METHOD NAME : librpmDb::librpmDb
351 // METHOD TYPE : Constructor
355 librpmDb::librpmDb( const Pathname & root_r, const Pathname & dbPath_r, bool readonly_r )
356 : _d( * new D( root_r, dbPath_r, readonly_r ) )
359 ///////////////////////////////////////////////////////////////////
362 // METHOD NAME : librpmDb::~librpmDb
363 // METHOD TYPE : Destructor
367 librpmDb::~librpmDb()
372 ///////////////////////////////////////////////////////////////////
375 // METHOD NAME : librpmDb::unref_to
376 // METHOD TYPE : void
378 void librpmDb::unref_to( unsigned refCount_r ) const
380 if ( refCount_r == 1 )
386 ///////////////////////////////////////////////////////////////////
389 // METHOD NAME : librpmDb::root
390 // METHOD TYPE : const Pathname &
392 const Pathname & librpmDb::root() const
397 ///////////////////////////////////////////////////////////////////
400 // METHOD NAME : librpmDb::dbPath
401 // METHOD TYPE : const Pathname &
403 const Pathname & librpmDb::dbPath() const
408 ///////////////////////////////////////////////////////////////////
411 // METHOD NAME : librpmDb::error
412 // METHOD TYPE : PMError
414 shared_ptr<RpmException> librpmDb::error() const
419 ///////////////////////////////////////////////////////////////////
422 // METHOD NAME : librpmDb::empty
423 // METHOD TYPE : bool
425 bool librpmDb::empty() const
427 return( valid() && ! *db_const_iterator( this ) );
430 ///////////////////////////////////////////////////////////////////
433 // METHOD NAME : librpmDb::size
434 // METHOD TYPE : unsigned
436 unsigned librpmDb::size() const
441 db_const_iterator it( this );
442 for ( db_const_iterator it( this ); *it; ++it )
448 ///////////////////////////////////////////////////////////////////
451 // METHOD NAME : librpmDb::dont_call_it
452 // METHOD TYPE : void *
454 void * librpmDb::dont_call_it() const
456 return rpmtsGetRdb(_d._ts);
459 ///////////////////////////////////////////////////////////////////
462 // METHOD NAME : librpmDb::dumpOn
463 // METHOD TYPE : ostream &
467 std::ostream & librpmDb::dumpOn( std::ostream & str ) const
469 ReferenceCounted::dumpOn( str ) << _d;
473 ///////////////////////////////////////////////////////////////////
475 // CLASS NAME : librpmDb::db_const_iterator::D
479 class librpmDb::db_const_iterator::D
481 D & operator=( const D & ); // NO ASSIGNMENT!
482 D ( const D & ); // NO COPY!
485 librpmDb::constPtr _dbptr;
486 shared_ptr<RpmException> _dberr;
488 RpmHeader::constPtr _hptr;
489 rpmdbMatchIterator _mi;
491 D( librpmDb::constPtr dbptr_r )
499 librpmDb::dbAccess( _dbptr );
501 catch (const RpmException & excpt_r)
503 ZYPP_CAUGHT(excpt_r);
507 WAR << "No database access: " << _dberr << endl;
512 destroy(); // Checks whether _dbptr still valid
520 ::rpmdbFreeIterator( _mi );
525 * Let iterator access a dbindex file. Call @ref advance to access the
526 * 1st element (if present).
528 bool create( int rpmtag, const void * keyp = NULL, size_t keylen = 0 )
533 _mi = ::rpmtsInitIterator( _dbptr->_d._ts, rpmTag(rpmtag), keyp, keylen );
538 * Destroy iterator. Invalidates _dbptr, if database was blocked meanwile.
539 * Always returns false.
545 _mi = ::rpmdbFreeIterator( _mi );
548 if ( _dbptr && _dbptr->error() )
550 _dberr = _dbptr->error();
551 WAR << "Lost database access: " << _dberr << endl;
558 * Advance to the first/next header in iterator. Destroys iterator if
559 * no more headers available.
565 Header h = ::rpmdbNextIterator( _mi );
571 _hptr = new RpmHeader( h );
576 * Access a dbindex file and advance to the 1st header.
578 bool init( int rpmtag, const void * keyp = NULL, size_t keylen = 0 )
580 if ( ! create( rpmtag, keyp, keylen ) )
586 * Create an itertator that contains the database entry located at
587 * off_r, and advance to the 1st header.
589 bool set( int off_r )
591 if ( ! create( RPMDBI_PACKAGES ) )
593 #ifdef RPMFILEITERMAX // since rpm.4.12
594 ::rpmdbAppendIterator( _mi, (const unsigned *)&off_r, 1 );
596 ::rpmdbAppendIterator( _mi, &off_r, 1 );
603 return( _mi ? ::rpmdbGetIteratorOffset( _mi ) : 0 );
610 int ret = ::rpmdbGetIteratorCount( _mi );
611 return( ret ? ret : -1 ); // -1: sequential access
615 ///////////////////////////////////////////////////////////////////
617 ///////////////////////////////////////////////////////////////////
619 // CLASS NAME : librpmDb::Ptr::db_const_iterator
621 ///////////////////////////////////////////////////////////////////
623 ///////////////////////////////////////////////////////////////////
626 // METHOD NAME : librpmDb::db_const_iterator::db_iterator
627 // METHOD TYPE : Constructor
629 librpmDb::db_const_iterator::db_const_iterator( librpmDb::constPtr dbptr_r )
630 : _d( * new D( dbptr_r ) )
635 ///////////////////////////////////////////////////////////////////
638 // METHOD NAME : librpmDb::db_const_iterator::~db_const_iterator
639 // METHOD TYPE : Destructor
641 librpmDb::db_const_iterator::~db_const_iterator()
646 ///////////////////////////////////////////////////////////////////
649 // METHOD NAME : librpmDb::db_const_iterator::operator++
650 // METHOD TYPE : void
652 void librpmDb::db_const_iterator::operator++()
657 ///////////////////////////////////////////////////////////////////
660 // METHOD NAME : librpmDb::db_const_iterator::dbHdrNum
661 // METHOD TYPE : unsigned
663 unsigned librpmDb::db_const_iterator::dbHdrNum() const
668 ///////////////////////////////////////////////////////////////////
671 // METHOD NAME : librpmDb::db_const_iterator::operator*
672 // METHOD TYPE : const RpmHeader::constPtr &
674 const RpmHeader::constPtr & librpmDb::db_const_iterator::operator*() const
679 ///////////////////////////////////////////////////////////////////
682 // METHOD NAME : librpmDb::db_const_iterator::dbError
683 // METHOD TYPE : PMError
685 shared_ptr<RpmException> librpmDb::db_const_iterator::dbError() const
688 return _d._dbptr->error();
693 /******************************************************************
696 ** FUNCTION NAME : operator<<
697 ** FUNCTION TYPE : ostream &
699 std::ostream & operator<<( std::ostream & str, const librpmDb::db_const_iterator & obj )
701 str << "db_const_iterator(" << obj._d._dbptr
702 << " Size:" << obj._d.size()
703 << " HdrNum:" << obj._d.offset()
708 ///////////////////////////////////////////////////////////////////
711 // METHOD NAME : librpmDb::db_const_iterator::findAll
712 // METHOD TYPE : bool
714 bool librpmDb::db_const_iterator::findAll()
716 return _d.init( RPMDBI_PACKAGES );
719 ///////////////////////////////////////////////////////////////////
722 // METHOD NAME : librpmDb::db_const_iterator::findByFile
723 // METHOD TYPE : bool
725 bool librpmDb::db_const_iterator::findByFile( const std::string & file_r )
727 return _d.init( RPMTAG_BASENAMES, file_r.c_str() );
730 ///////////////////////////////////////////////////////////////////
733 // METHOD NAME : librpmDb::db_const_iterator::findByProvides
734 // METHOD TYPE : bool
736 bool librpmDb::db_const_iterator::findByProvides( const std::string & tag_r )
738 return _d.init( RPMTAG_PROVIDENAME, tag_r.c_str() );
741 ///////////////////////////////////////////////////////////////////
744 // METHOD NAME : librpmDb::db_const_iterator::findByRequiredBy
745 // METHOD TYPE : bool
747 bool librpmDb::db_const_iterator::findByRequiredBy( const std::string & tag_r )
749 return _d.init( RPMTAG_REQUIRENAME, tag_r.c_str() );
752 ///////////////////////////////////////////////////////////////////
755 // METHOD NAME : librpmDb::db_const_iterator::findByConflicts
756 // METHOD TYPE : bool
758 bool librpmDb::db_const_iterator::findByConflicts( const std::string & tag_r )
760 return _d.init( RPMTAG_CONFLICTNAME, tag_r.c_str() );
763 ///////////////////////////////////////////////////////////////////
766 // METHOD NAME : librpmDb::findByName
767 // METHOD TYPE : bool
769 bool librpmDb::db_const_iterator::findByName( const std::string & name_r )
771 return _d.init( RPMTAG_NAME, name_r.c_str() );
774 ///////////////////////////////////////////////////////////////////
777 // METHOD NAME : librpmDb::db_const_iterator::findPackage
778 // METHOD TYPE : bool
780 bool librpmDb::db_const_iterator::findPackage( const std::string & name_r )
782 if ( ! _d.init( RPMTAG_NAME, name_r.c_str() ) )
785 if ( _d.size() == 1 )
788 // check installtime on multiple entries
791 for ( ; operator*(); operator++() )
793 if ( operator*()->tag_installtime() > itime )
796 itime = operator*()->tag_installtime();
800 return _d.set( match );
803 ///////////////////////////////////////////////////////////////////
806 // METHOD NAME : librpmDb::db_const_iterator::findPackage
807 // METHOD TYPE : bool
809 bool librpmDb::db_const_iterator::findPackage( const std::string & name_r, const Edition & ed_r )
811 if ( ! _d.init( RPMTAG_NAME, name_r.c_str() ) )
814 for ( ; operator*(); operator++() )
816 if ( ed_r == operator*()->tag_edition() )
818 int match = _d.offset();
819 return _d.set( match );
826 ///////////////////////////////////////////////////////////////////
829 // METHOD NAME : librpmDb::db_const_iterator::findPackage
830 // METHOD TYPE : bool
832 bool librpmDb::db_const_iterator::findPackage( const Package::constPtr & which_r )
837 return findPackage( which_r->name(), which_r->edition() );
841 } // namespace target