Imported Upstream version 17.20.0
[platform/upstream/libzypp.git] / zypp / target / rpm / librpmDb.cc
index edf6ff5..daf6cef 100644 (file)
@@ -65,28 +65,7 @@ public:
     _ts = ::rpmtsCreate();
     ::rpmtsSetRootDir( _ts, _root.c_str() );
 
-    // check whether to create a new db
-    PathInfo master( _root + _dbPath + "Packages" );
-    if ( ! master.isFile() )
-    {
-      // init database
-      if ( filesystem::assert_dir(_root + _dbPath) != 0 )
-      {
-        ERR << "Could not create dbpath " << (_root + _dbPath).asString() << endl;
-        _error = shared_ptr<RpmInitException>(new RpmInitException(_root, _dbPath));
-        ZYPP_THROW(*_error);
-      }
-      int res = ::rpmtsInitDB( _ts, 0644 );
-      if ( res )
-      {
-        ERR << "rpmdbInit error(" << res << "): " << *this << endl;
-        _error = shared_ptr<RpmInitException>(new RpmInitException(_root, _dbPath));
-       rpmtsFree(_ts);
-        ZYPP_THROW(*_error);
-      }
-    }
-
-    // open database
+    // open database (creates a missing one on the fly)
     int res = ::rpmtsOpenDB( _ts, (readonly_r ? O_RDONLY : O_RDWR ));
     if ( res )
     {
@@ -118,7 +97,12 @@ public:
 ///////////////////////////////////////////////////////////////////
 
 Pathname         librpmDb::_defaultRoot  ( "/" );
-Pathname         librpmDb::_defaultDbPath( "/var/lib/rpm" );
+// NOTE: Former variable, but now locked to "/var/lib/rpm".
+// A custom dbPath is not actually needed and would only work
+// reliably if libsolv also supports it.
+// The protected librpmDb ctor would allow to build a db_const_iterator
+// to access (ro) a database at a custom location.
+const Pathname   librpmDb::_defaultDbPath( "/var/lib/rpm" );
 librpmDb::constPtr librpmDb::_defaultDb;
 bool             librpmDb::_dbBlocked    ( true );
 
@@ -179,14 +163,8 @@ std::string librpmDb::expand( const std::string & macro_r )
 //     METHOD NAME : librpmDb::newLibrpmDb
 //     METHOD TYPE : librpmDb *
 //
-librpmDb * librpmDb::newLibrpmDb( Pathname root_r, Pathname dbPath_r, bool readonly_r )
+librpmDb * librpmDb::newLibrpmDb()
 {
-  // check arguments
-  if ( ! (root_r.absolute() && dbPath_r.absolute()) )
-  {
-    ZYPP_THROW(RpmInvalidRootException(root_r, dbPath_r));
-  }
-
   // initialize librpm
   if ( ! globalInit() )
   {
@@ -197,7 +175,7 @@ librpmDb * librpmDb::newLibrpmDb( Pathname root_r, Pathname dbPath_r, bool reado
   librpmDb * ret = 0;
   try
   {
-    ret = new librpmDb( root_r, dbPath_r, readonly_r );
+    ret = new librpmDb( _defaultRoot, _defaultDbPath, /*readonly*/true );
   }
   catch (const RpmException & excpt_r)
   {
@@ -215,28 +193,30 @@ librpmDb * librpmDb::newLibrpmDb( Pathname root_r, Pathname dbPath_r, bool reado
 //     METHOD NAME : librpmDb::dbAccess
 //     METHOD TYPE : PMError
 //
-void librpmDb::dbAccess( const Pathname & root_r, const Pathname & dbPath_r )
+void librpmDb::dbAccess( const Pathname & root_r )
 {
-  // check arguments
-  if ( ! (root_r.absolute() && dbPath_r.absolute()) )
-  {
-    ZYPP_THROW(RpmInvalidRootException(root_r, dbPath_r));
-  }
-
   if ( _defaultDb )
   {
     // already accessing a database: switching is not allowed.
-    if ( _defaultRoot == root_r && _defaultDbPath == dbPath_r )
+    if ( _defaultRoot == root_r )
       return;
     else
-    {
-      ZYPP_THROW(RpmDbAlreadyOpenException(_defaultRoot, _defaultDbPath, root_r, dbPath_r));
-    }
+      ZYPP_THROW(RpmDbAlreadyOpenException(_defaultRoot, _defaultDbPath, root_r, _defaultDbPath));
   }
 
   // got no database: we could switch to a new one (even if blocked!)
+
+  if ( root_r.empty() || ! root_r.absolute() )
+    ZYPP_THROW(RpmInvalidRootException(root_r, _defaultDbPath));
+
+  PathInfo pi { root_r / _defaultDbPath };
+  if ( pi.isExist() && ! pi.isDir() ) {
+    RpmInvalidRootException excpt { root_r, _defaultDbPath };
+    excpt.addHistory( str::Str() << pi );
+    ZYPP_THROW(excpt);
+  }
+
   _defaultRoot = root_r;
-  _defaultDbPath = dbPath_r;
   MIL << "Set new database location: " << stringPath( _defaultRoot, _defaultDbPath ) << endl;
 
   return dbAccess();
@@ -258,7 +238,7 @@ void librpmDb::dbAccess()
   if ( !_defaultDb )
   {
     // get access
-    _defaultDb = newLibrpmDb( _defaultRoot, _defaultDbPath, /*readonly*/true );
+    _defaultDb = newLibrpmDb();
   }
 }
 
@@ -270,16 +250,8 @@ void librpmDb::dbAccess()
 //
 void librpmDb::dbAccess( librpmDb::constPtr & ptr_r )
 {
-  try
-  {
-    dbAccess();
-  }
-  catch (const RpmException & excpt_r)
-  {
-    ZYPP_CAUGHT(excpt_r);
-    ptr_r = 0;
-    ZYPP_RETHROW(excpt_r);
-  }
+  ptr_r = nullptr;
+  dbAccess();
   ptr_r = _defaultDb;
 }
 
@@ -494,75 +466,6 @@ ostream & librpmDb::dumpOn( ostream & str ) const
 
 ///////////////////////////////////////////////////////////////////
 //
-//     CLASS NAME : librpmDb::DbDirInfo
-//
-///////////////////////////////////////////////////////////////////
-
-///////////////////////////////////////////////////////////////////
-//
-//
-//     METHOD NAME : librpmDb::DbDirInfo::DbDirInfo
-//     METHOD TYPE : Constructor
-//
-librpmDb::DbDirInfo::DbDirInfo( const Pathname & root_r, const Pathname & dbPath_r )
-    : _root( root_r )
-    , _dbPath( dbPath_r )
-{
-  // check and adjust arguments
-  if ( ! (root_r.absolute() && dbPath_r.absolute()) )
-  {
-    ERR << "Relative path for root(" << _root << ") or dbPath(" << _dbPath << ")" << endl;
-  }
-  else
-  {
-    _dbDir   ( _root + _dbPath );
-    _dbV4    ( _dbDir.path() + "Packages" );
-    _dbV3    ( _dbDir.path() + "packages.rpm" );
-    _dbV3ToV4( _dbDir.path() + "packages.rpm3" );
-    DBG << *this << endl;
-  }
-}
-
-///////////////////////////////////////////////////////////////////
-//
-//
-//     METHOD NAME : librpmDb::DbDirInfo::update
-//     METHOD TYPE : void
-//
-void librpmDb::DbDirInfo::restat()
-{
-  _dbDir();
-  _dbV4();
-  _dbV3();
-  _dbV3ToV4();
-  DBG << *this << endl;
-}
-
-/******************************************************************
-**
-**
-**     FUNCTION NAME : operator<<
-**     FUNCTION TYPE : std::ostream &
-*/
-std::ostream & operator<<( std::ostream & str, const librpmDb::DbDirInfo & obj )
-{
-  if ( obj.illegalArgs() )
-  {
-    str << "ILLEGAL: '(" << obj.root() << ")" << obj.dbPath() << "'";
-  }
-  else
-  {
-    str << "'(" << obj.root() << ")" << obj.dbPath() << "':" << endl;
-    str << "  Dir:    " << obj._dbDir << endl;
-    str << "  V4:     " << obj._dbV4 << endl;
-    str << "  V3:     " << obj._dbV3 << endl;
-    str << "  V3ToV4: " << obj._dbV3ToV4;
-  }
-  return str;
-}
-
-///////////////////////////////////////////////////////////////////
-//
 //     CLASS NAME : librpmDb::db_const_iterator::D
 /**
  *