From 65513b3810cf6289376a27dc56594e26a3856fb7 Mon Sep 17 00:00:00 2001 From: Jiri Srain Date: Wed, 4 Jan 2006 18:06:31 +0000 Subject: [PATCH] backup commit --- zypp/target/rpm/Makefile.am | 5 +- zypp/target/rpm/RpmCallbacks.h | 32 ++++- zypp/target/rpm/RpmDb.cc | 271 +++++++++++++++++++++------------------- zypp/target/rpm/RpmDb.h | 39 +++--- zypp/target/rpm/RpmException.cc | 77 ++++++++++++ zypp/target/rpm/RpmException.h | 198 +++++++++++++++++++++++++++++ zypp/target/rpm/RpmHeader.cc | 6 +- zypp/target/rpm/librpm.h | 33 ++--- zypp/target/rpm/librpmDb.cc | 78 +++++------- zypp/target/rpm/librpmDb.h | 53 ++------ 10 files changed, 531 insertions(+), 261 deletions(-) create mode 100644 zypp/target/rpm/RpmException.cc create mode 100644 zypp/target/rpm/RpmException.h diff --git a/zypp/target/rpm/Makefile.am b/zypp/target/rpm/Makefile.am index a1ac641..ec5bfda 100644 --- a/zypp/target/rpm/Makefile.am +++ b/zypp/target/rpm/Makefile.am @@ -7,6 +7,7 @@ SUBDIRS = include_HEADERS = \ librpm.h \ + RpmException.h \ BinHeader.h \ RpmHeader.h \ librpmDb.h \ @@ -20,14 +21,16 @@ noinst_LTLIBRARIES = lib@PACKAGE@_target_rpm.la ## ################################################## lib@PACKAGE@_target_rpm_la_SOURCES = \ + RpmException.cc \ BinHeader.cc \ RpmHeader.cc \ librpmDb.cc \ + librpmDb.cv3.cc \ RpmCallbacks.cc \ RpmPackageImpl.cc \ RpmDb.cc -lib@PACKAGE@_target_rpm_la_LIBADD = +lib@PACKAGE@_target_rpm_la_LIBADD = -lrpm ## ################################################## diff --git a/zypp/target/rpm/RpmCallbacks.h b/zypp/target/rpm/RpmCallbacks.h index eae3082..000b04b 100644 --- a/zypp/target/rpm/RpmCallbacks.h +++ b/zypp/target/rpm/RpmCallbacks.h @@ -27,9 +27,9 @@ namespace zypp { /////////////////////////////////////////////////////////////////// // Reporting progress of package removing /////////////////////////////////////////////////////////////////// - class RemovePkgReport : public HACK::Callback { + class RpmRemoveReport : public HACK::Callback { public: - virtual ~RemovePkgReport() + virtual ~RpmRemoveReport() {} /** Start the operation */ virtual void start( const std::string & name ) @@ -48,7 +48,7 @@ namespace zypp { { } }; - extern RemovePkgReport removePkgReport; + extern RpmRemoveReport rpmRemoveReport; /////////////////////////////////////////////////////////////////// // Reporting progress of package installation @@ -128,6 +128,32 @@ namespace zypp { extern RebuildDbReport rebuildDbReport; + /////////////////////////////////////////////////////////////////// + // Reporting progress of database rebuild + /////////////////////////////////////////////////////////////////// + class ConvertDbReport : public HACK::Callback { + public: + virtual ~ConvertDbReport() + {} + /** Start the operation */ + virtual void start() + { } + /** + * Inform about progress + * Return true on abort + */ + virtual bool progress( unsigned percent ) + { return false; } + /** Finish operation in case of success */ + virtual void end() + { } + /** Finish operatino in case of fail, report fail exception */ + virtual void end( Exception & excpt_r ) + { } + }; + + extern ConvertDbReport convertDbReport; + } // namespace rpm } // namespace target } // namespace zypp diff --git a/zypp/target/rpm/RpmDb.cc b/zypp/target/rpm/RpmDb.cc index 038c89e..bdfe4d5 100644 --- a/zypp/target/rpm/RpmDb.cc +++ b/zypp/target/rpm/RpmDb.cc @@ -1,23 +1,14 @@ /*---------------------------------------------------------------------\ +| ____ _ __ __ ___ | +| |__ / \ / / . \ . \ | +| / / \ V /| _/ _/ | +| / /__ | | | | | | | +| /_____||_| |_| |_| | | | -| __ __ ____ _____ ____ | -| \ \ / /_ _/ ___|_ _|___ \ | -| \ V / _` \___ \ | | __) | | -| | | (_| |___) || | / __/ | -| |_|\__,_|____/ |_| |_____| | -| | -| Package Management | -| (C) 2002 SuSE AG | -\----------------------------------------------------------------------/ - - File: RpmDb.cc - Purpose: Interface to installed RPM system - Author: Stefan Schubert - Maintainer: Ludwig Nussel - - Copied and adapted from agent-targetpkg - -/-*/ +\---------------------------------------------------------------------*/ +/** \file zypp/target/rpm/RpmDb.h + * +*/ #include "librpm.h" #include @@ -43,6 +34,7 @@ #include "zypp/target/rpm/librpmDb.h" #include "zypp/target/rpm/RpmPackageImpl.h" +#include "zypp/target/rpm/RpmException.h" #include "zypp/CapSet.h" #ifndef _ @@ -247,7 +239,7 @@ class RpmDb::Packages { // /////////////////////////////////////////////////////////////////// -#define FAILIFNOTINITIALIZED if( ! initialized() ) { ZYPP_THROW(Exception("Error::E_RpmDB_not_open")); } +#define FAILIFNOTINITIALIZED if( ! initialized() ) { ZYPP_THROW(RpmDbNotOpenException()); } /////////////////////////////////////////////////////////////////// @@ -336,7 +328,7 @@ void RpmDb::initDatabase( Pathname root_r, Pathname dbPath_r ) if ( ! (root_r.absolute() && dbPath_r.absolute()) ) { ERR << "Illegal root or dbPath: " << stringPath( root_r, dbPath_r ) << endl; - ZYPP_THROW(Exception("Error::E_invalid_argument")); + ZYPP_THROW(RpmInvalidRootException(root_r, dbPath_r)); } MIL << "Calling initDatabase: " << stringPath( root_r, dbPath_r ) << endl; @@ -348,9 +340,7 @@ void RpmDb::initDatabase( Pathname root_r, Pathname dbPath_r ) if ( root_r == _root && dbPath_r == _dbPath ) { return; } else { - ERR << "Can't switch to " << stringPath( root_r, dbPath_r ) - << " while accessing " << stringPath( _root, _dbPath ) << endl; - ZYPP_THROW(Exception("Error::E_RpmDB_already_open")); + ZYPP_THROW(RpmDbAlreadyOpenException(_root, _dbPath, root_r, dbPath_r)); } } @@ -362,8 +352,9 @@ void RpmDb::initDatabase( Pathname root_r, Pathname dbPath_r ) try { internal_initDatabase( root_r, dbPath_r, info ); } - catch (...) + catch (const RpmException & excpt_r) { + ZYPP_CAUGHT(excpt_r); librpmDb::blockAccess(); ERR << "Cleanup on error: state " << info << endl; @@ -372,44 +363,43 @@ void RpmDb::initDatabase( Pathname root_r, Pathname dbPath_r ) // any backup created on conversion. removeV4( root_r + dbPath_r, dbsi_has( info, DbSI_MADE_V3TOV4 ) ); } - #warning FIXME rethrow - + ZYPP_RETHROW(excpt_r); } - if ( dbsi_has( info, DbSI_HAVE_V3 ) ) { - if ( root_r == "/" || dbsi_has( info, DbSI_MODIFIED_V4 ) ) { - // Move obsolete rpm3 database beside. - MIL << "Cleanup: state " << info << endl; - removeV3( root_r + dbPath_r, dbsi_has( info, DbSI_MADE_V3TOV4 ) ); - dbsi_clr( info, DbSI_HAVE_V3 ); - } else { + if ( dbsi_has( info, DbSI_HAVE_V3 ) ) { + if ( root_r == "/" || dbsi_has( info, DbSI_MODIFIED_V4 ) ) { + // Move obsolete rpm3 database beside. + MIL << "Cleanup: state " << info << endl; + removeV3( root_r + dbPath_r, dbsi_has( info, DbSI_MADE_V3TOV4 ) ); + dbsi_clr( info, DbSI_HAVE_V3 ); + } else { // Performing an update: Keep the original rpm3 database // and wait if the rpm4 database gets modified by installing // or removing packages. Cleanup in modifyDatabase or closeDatabase. MIL << "Update mode: Cleanup delayed until closeOldDatabase." << endl; - } } + } #warning CHECK: notify root about conversion backup. - _root = root_r; - _dbPath = dbPath_r; - _dbStateInfo = info; + _root = root_r; + _dbPath = dbPath_r; + _dbStateInfo = info; -#warning FIXME uncomment code below +#warning Add rebuild database once have the info about context #if 0 - if ( ! ( err || Y2PM::runningFromSystem() ) ) { - if ( dbsi_has( info, DbSI_HAVE_V4 ) - && ! dbsi_has( info, DbSI_MADE_V4 ) ) { - err = rebuildDatabase(); - } + if ( ! ( Y2PM::runningFromSystem() ) ) { + if ( dbsi_has( info, DbSI_HAVE_V4 ) + && ! dbsi_has( info, DbSI_MADE_V4 ) ) { + err = rebuildDatabase(); } + } #endif - // Close the database in case any write acces (create/convert) - // happened during init. This should drop any lock acquired - // by librpm. On demand it will be reopened readonly and should - // not hold any lock. - librpmDb::dbRelease( true ); - MIL << "InitDatabase: " << *this << endl; + // Close the database in case any write acces (create/convert) + // happened during init. This should drop any lock acquired + // by librpm. On demand it will be reopened readonly and should + // not hold any lock. + librpmDb::dbRelease( true ); + MIL << "InitDatabase: " << *this << endl; } /////////////////////////////////////////////////////////////////// @@ -429,11 +419,12 @@ void RpmDb::internal_initDatabase( const Pathname & root_r, const Pathname & dbP librpmDb::DbDirInfo dbInfo( root_r, dbPath_r ); if ( dbInfo.illegalArgs() ) { - ZYPP_THROW(Exception("Error::E_invalid_argument")); // should not happen (checked in initDatabase) + // should not happen (checked in initDatabase) + ZYPP_THROW(RpmInvalidRootException(root_r, dbPath_r)); } if ( ! dbInfo.usableArgs() ) { ERR << "Bad database directory: " << dbInfo.dbDir() << endl; - ZYPP_THROW(Exception("Error::E_invalid_argument")); + ZYPP_THROW(RpmInvalidRootException(root_r, dbPath_r)); } if ( dbInfo.hasDbV4() ) { @@ -485,8 +476,8 @@ void RpmDb::internal_initDatabase( const Pathname & root_r, const Pathname & dbP MIL << "Found rpm3 database " << dbInfo.dbV3() << endl; if ( dbEmpty ) { - extern void convertV3toV4( const Pathname & v3db_r, const librpmDb::constPtr & v4db_r ); - + extern void convertV3toV4( const Pathname & v3db_r, const constlibrpmDbPtr & v4db_r ); +#warning FIXME exception handling for following function convertV3toV4( dbInfo.dbV3().path(), dbptr ); // create a backup copy @@ -715,6 +706,20 @@ void RpmDb::closeDatabase() // void RpmDb::rebuildDatabase() { + RebuildDbReport report; + try { + doRebuildDatabase(report); + } + catch (RpmException & excpt_r) + { + report.end(excpt_r); + ZYPP_RETHROW(excpt_r); + } + report.end(); +} + +void RpmDb::doRebuildDatabase(RebuildDbReport & report) +{ FAILIFNOTINITIALIZED; MIL << "RpmDb::rebuildDatabase" << *this << endl; @@ -723,10 +728,6 @@ void RpmDb::rebuildDatabase() PathInfo dbMaster( root() + dbPath() + "Packages" ); PathInfo dbMasterBackup( dbMaster.path().extend( ".y2backup" ) ); - // report - RebuildDbReport report; - report.start(); - // run rpm RpmArgVec opts; opts.push_back("--rebuilddb"); @@ -763,14 +764,9 @@ void RpmDb::rebuildDatabase() int rpm_status = systemStatus(); if ( rpm_status != 0 ) { - ERR << "rpm failed, message was:" << endl << errmsg; // has trailing NL -#warning FIXME error report -// err.setDetails(errmsg); -// report.end(err); - ZYPP_THROW(Exception("Error::E_RpmDB_subprocess_failed")); + ZYPP_THROW(RpmSubprocessException(string("rpm failed with message: ") + errmsg)); } else { report.progress( 100 ); // 100% - report.end(); } } @@ -807,8 +803,7 @@ void RpmDb::importPubkey( const Pathname & pubkey_r ) int rpm_status = systemStatus(); if ( rpm_status != 0 ) { - ERR << "Failed to import public key from file " << pubkey_r << ": rpm returned " << rpm_status << endl; - ZYPP_THROW(Exception("Error::E_RpmDB_subprocess_failed")); + ZYPP_THROW(RpmSubprocessException(string("Failed to import public key from file ") + pubkey_r.asString() + string(": rpm returned ") + str::numstring(rpm_status))); } else { MIL << "Imported public key from file " << pubkey_r << endl; } @@ -825,11 +820,10 @@ void RpmDb::importPubkey( const Pathname & keyring_r, const string & keyname_r ) FAILIFNOTINITIALIZED; // create tempfile - char tmpname[] = "/tmp/y2.pubkey.XXXXXX"; + char tmpname[] = "/tmp/zypp.pubkey.XXXXXX"; int tmpfd = mkstemp( tmpname ); if ( tmpfd == -1 ) { - ERR << "Unable to create a unique temporary file for pubkey" << endl; - ZYPP_THROW(Exception("Error::E_RpmDB_subprocess_failed")); + ZYPP_THROW(RpmSubprocessException("Unable to create a unique temporary file for pubkey")); } // export keyname from keyring @@ -849,43 +843,43 @@ void RpmDb::importPubkey( const Pathname & keyring_r, const string & keyname_r ) // launch gpg ExternalProgram prg( argv, ExternalProgram::Discard_Stderr, false, -1, true ); - string err; int res = 0; // read key - for ( string line( prg.receiveLine() ); line.length(); line = prg.receiveLine() ) { - ssize_t written = write( tmpfd, line.c_str(), line.length() ); - if ( written == -1 || unsigned(written) != line.length() ) { - ERR << "Error writing pubkey to " << tmpname << endl; - err = "Error::E_RpmDB_subprocess_failed"; - break; + + try { + for ( string line( prg.receiveLine() ); line.length(); line = prg.receiveLine() ) { + ssize_t written = write( tmpfd, line.c_str(), line.length() ); + if ( written == -1 || unsigned(written) != line.length() ) { + ZYPP_THROW(RpmSubprocessException(string("Error writing pubkey to ") + tmpname)); + } + res += written; // empty file indicates key not found } - res += written; // empty file indicates key not found + } + catch (RpmException & excpt_r) + { + close( tmpfd ); + filesystem::unlink( tmpname ); + ZYPP_RETHROW(excpt_r); } close( tmpfd ); if ( ! res ) { - WAR << "gpg: no key '" << keyname_r << "' found in '" << keyring_r << "'" << endl; - err = "Error::E_RpmDB_subprocess_failed"; + ZYPP_THROW(RpmSubprocessException(string("gpg: no key '") + keyname_r + string("' found in '") + keyring_r.asString() + string("'"))); } // check gpg returncode res = prg.close(); if ( res ) { - ERR << "gpg: export '" << keyname_r << "' from '" << keyring_r << "' returned " << res << endl; - err = "Error::E_RpmDB_subprocess_failed"; + // remove tempfile + filesystem::unlink( tmpname ); + ZYPP_THROW(RpmSubprocessException(string("gpg: export '") + keyname_r + string("' from '") + keyring_r.asString() + "' returned " + str::numstring(res))); } - if ( err == "" ) { - MIL << "Exported '" << keyname_r << "' from '" << keyring_r << "' to " << tmpname << endl; + MIL << "Exported '" << keyname_r << "' from '" << keyring_r << "' to " << tmpname << endl; #warning FIXME handle exception from line below - importPubkey( tmpname ); - } - - // remove tempfile + importPubkey( tmpname ); filesystem::unlink( tmpname ); - - ZYPP_THROW(Exception(err)); } /////////////////////////////////////////////////////////////////// @@ -927,14 +921,27 @@ bool RpmDb::packagesValid() const // const std::list & RpmDb::getPackages() { + ScanDbReport report; + try { + const std::list & ret = doGetPackages(report); + report.end(); + return ret; + } + catch (RpmException & excpt_r) + { + report.end(excpt_r); + ZYPP_RETHROW(excpt_r); + } +} + + +const std::list & RpmDb::doGetPackages(ScanDbReport & report) +{ if ( packagesValid() ) { return _packages._list; } - // report // FIXME Timecount _t( "RpmDb::getPackages" ); - ScanDbReport report; - report.start(); #warning how to detect corrupt db while reading. @@ -945,27 +952,21 @@ const std::list & RpmDb::getPackages() // multiple entries for the same string. If so we consider the last // one installed to be the one we're interesed in. /////////////////////////////////////////////////////////////////// + unsigned expect = 0; librpmDb::db_const_iterator iter; // findAll { // quick check - unsigned expect = 0; for ( ; *iter; ++iter ) { ++expect; } -#warning report error -#if 0 if ( iter.dbError() ) { ERR << "No database access: " << iter.dbError() << endl; - report.end( iter.dbError() ); - return _packages._list; + ZYPP_THROW(*(iter.dbError())); } -#endif -#warning progress update -// report->progress( pd.init( expect ) ); } + unsigned current = 0; -#warning progress update - for ( iter.findAll(); *iter; ++iter/*, report.progress( pd.incr() )*/ ) { + for ( iter.findAll(); *iter; ++iter, ++current, report.progress( (100*current)/expect)) { string name = iter->tag_name(); if ( name == string( "gpg-pubkey" ) ) { @@ -1008,7 +1009,7 @@ const std::list & RpmDb::getPackages() for ( iter.findByFile( *it ); *iter; ++iter ) { Package::Ptr pptr = _packages.lookup( iter->tag_name() ); if ( !pptr ) { - WAR << "rpmdb.findByFile returned unpknown package " << *iter << endl; + WAR << "rpmdb.findByFile returned unknown package " << *iter << endl; continue; } Dependencies _deps = pptr->deps(); @@ -1024,7 +1025,6 @@ const std::list & RpmDb::getPackages() /////////////////////////////////////////////////////////////////// _packages.buildList(); DBG << "Found installed packages: " << _packages._list.size() << endl; - report.end(); return _packages._list; } @@ -1153,8 +1153,8 @@ void RpmDb::getData( const string & name_r, librpmDb::db_const_iterator it; it.findPackage( name_r ); result_r = *it; -#warning FIXME error handling -// return it.dbError(); + if (it.dbError()) + ZYPP_THROW(*(it.dbError())); } /////////////////////////////////////////////////////////////////// @@ -1171,8 +1171,8 @@ void RpmDb::getData( const std::string & name_r, const Edition & ed_r, librpmDb::db_const_iterator it; it.findPackage( name_r, ed_r ); result_r = *it; -#warning FIXME error handling -// return it.dbError(); + if (it.dbError()) + ZYPP_THROW(*(it.dbError())); } /*--------------------------------------------------------------*/ @@ -1379,8 +1379,7 @@ RpmDb::run_rpm (const RpmArgVec& opts, exit_code = -1; if ( ! initialized() ) { - ERR << "Attempt to run rpm: " << "Error::E_RpmDB_not_open" << endl; - return; + ZYPP_THROW(RpmDbNotOpenException()); } RpmArgVec args; @@ -1563,15 +1562,25 @@ void RpmDb::processConfigFiles(const string& line, const string& name, const cha // void RpmDb::installPackage( const Pathname & filename, unsigned flags ) { + RpmInstallReport report; + try { + doInstallPackage(filename, flags, report); + } + catch (RpmException & excpt_r) + { + report.end(excpt_r); + ZYPP_RETHROW(excpt_r); + } + report.end(); + +} +void RpmDb::doInstallPackage( const Pathname & filename, unsigned flags, RpmInstallReport & report ) +{ FAILIFNOTINITIALIZED; Logfile progresslog; MIL << "RpmDb::installPackage(" << filename << "," << flags << ")" << endl; - // report - RpmInstallReport report; - report.start( filename ); - // backup if ( _packagebackups ) { // FIXME report->progress( pd.init( -2, 100 ) ); // allow 1% for backup creation. @@ -1654,16 +1663,11 @@ void RpmDb::installPackage( const Pathname & filename, unsigned flags ) _("rpm created %s as %s.\nHere are the first 25 lines of difference:\n")); } - string err; - if ( rpm_status != 0 ) { // %s = filename of rpm package progresslog(/*timestamp*/true) << str::form(_("%s install failed"), Pathname::basename(filename).c_str()) << endl; progresslog() << _("rpm output:") << endl << rpmmsg << endl; - ERR << "rpm failed, message was: " << rpmmsg << endl; - err = "Error::E_RpmDB_subprocess_failed"; -// TODO err.setDetails( rpmmsg ); -#warning TODO throw exception + ZYPP_THROW(RpmSubprocessException(string("RPM failed: ") + rpmmsg)); } else { // %s = filename of rpm package progresslog(/*timestamp*/true) << str::form(_("%s installed ok"), Pathname::basename(filename).c_str()) << endl; @@ -1671,8 +1675,6 @@ void RpmDb::installPackage( const Pathname & filename, unsigned flags ) progresslog() << _("Additional rpm output:") << endl << rpmmsg << endl; } } - - report.end(); } /////////////////////////////////////////////////////////////////// @@ -1694,15 +1696,26 @@ void RpmDb::removePackage( Package::constPtr package, unsigned flags ) // void RpmDb::removePackage( const string & name_r, unsigned flags ) { + RpmRemoveReport report; + try { + doRemovePackage(name_r, flags, report); + } + catch (RpmException & excpt_r) + { + report.end(excpt_r); + ZYPP_RETHROW(excpt_r); + } + report.end(); +} + + +void RpmDb::doRemovePackage( const string & name_r, unsigned flags, RpmRemoveReport & report ) +{ FAILIFNOTINITIALIZED; Logfile progresslog; MIL << "RpmDb::removePackage(" << name_r << "," << flags << ")" << endl; - // report - RemovePkgReport report; - report.start( name_r ); - // backup if ( _packagebackups ) { // FIXME solve this status report somehow @@ -1751,23 +1764,17 @@ void RpmDb::removePackage( const string & name_r, unsigned flags ) report.progress( 50 ); int rpm_status = systemStatus(); - string err; - if ( rpm_status != 0 ) { // %s = name of rpm package progresslog(/*timestamp*/true) << str::form(_("%s remove failed"), name_r.c_str()) << endl; progresslog() << _("rpm output:") << endl << rpmmsg << endl; - ERR << "rpm failed, message was: " << rpmmsg << endl; -// FIXME err = string("Error::E_RpmDB_subprocess_failed") + endl + rpmmsg; -#warning Add report->stop(Exception) here + ZYPP_THROW(RpmSubprocessException(string("RPM failed: ") + rpmmsg)); } else { progresslog(/*timestamp*/true) << str::form(_("%s remove ok"), name_r.c_str()) << endl; if( ! rpmmsg.empty() ) { progresslog() << _("Additional rpm output:") << endl << rpmmsg << endl; } } - - report.end(); } string diff --git a/zypp/target/rpm/RpmDb.h b/zypp/target/rpm/RpmDb.h index 7d65393..0bb1e0f 100644 --- a/zypp/target/rpm/RpmDb.h +++ b/zypp/target/rpm/RpmDb.h @@ -12,8 +12,8 @@ // -*- C++ -*- -#ifndef RpmDb_h -#define RpmDb_h +#ifndef ZYPP_TARGET_RPM_RPMDB_H +#define ZYPP_TARGET_RPM_RPMDB_H #include #include @@ -26,6 +26,7 @@ #include "zypp/Package.h" #include "zypp/target/rpm/RpmHeader.h" +#include "zypp/target/rpm/RpmCallbacks.h" namespace zypp { namespace target { @@ -94,7 +95,7 @@ namespace zypp { /** * Internal helper for @ref initDatabase. * - * \throws Exception + * \throws RpmException * **/ void internal_initDatabase( const Pathname & root_r, const Pathname & dbPath_r, @@ -165,7 +166,7 @@ namespace zypp { * (no packages were installed or deleted). Otherwise the new database * is kept, and the old one is removed. * - * \throws Exception + * \throws RpmException * **/ void initDatabase( Pathname root_r = Pathname(), @@ -176,7 +177,7 @@ namespace zypp { * state. On update: Decides what to do with any converted database * (see @ref initDatabase). * - * \throws Exception + * \throws RpmException * **/ void closeDatabase(); @@ -184,7 +185,7 @@ namespace zypp { /** * Rebuild the rpm database (rpm --rebuilddb). * - * \throws Exception + * \throws RpmException * **/ void rebuildDatabase(); @@ -192,7 +193,7 @@ namespace zypp { /** * Import ascii armored public key in file pubkey_r. * - * \throws Exception + * \throws RpmException * **/ void importPubkey( const Pathname & pubkey_r ); @@ -200,7 +201,7 @@ namespace zypp { /** * Import ascii armored public key keyname_r exported by keyring_r. * - * \throws Exception + * \throws RpmException * **/ void importPubkey( const Pathname & keyring_r, const std::string & keyname_r ); @@ -283,7 +284,7 @@ namespace zypp { * if packge is not installed (PMError is not set), or RPM database * could not be read (PMError is set). * - * \throws Exception + * \throws RpmException * * FIXME this and following comment * @@ -297,7 +298,7 @@ namespace zypp { * if packge is not installed (PMError is not set), or RPM database * could not be read (PMError is set). * - * \throws Exception + * \throws RpmException * **/ void getData( const std::string & name_r, const Edition & ed_r, @@ -320,7 +321,10 @@ namespace zypp { * @param n_opts The number of arguments * @param options Array of the arguments, @ref n_opts elements * @param stderr_disp How to handle stderr, merged with stdout by default - */ + * + * \throws RpmException + * + **/ void run_rpm( const RpmArgVec& options, ExternalProgram::Stderr_Disposition stderr_disp = ExternalProgram::Stderr_To_Stdout); @@ -432,7 +436,7 @@ namespace zypp { * * @return success * - * \throws Exception + * \throws RpmException * * */ void installPackage (const Pathname& filename, unsigned flags = 0 ); @@ -444,7 +448,7 @@ namespace zypp { * * @return success * - * \throws Exception + * \throws RpmException * * */ void removePackage(const std::string & name_r, unsigned flags = 0); @@ -532,6 +536,13 @@ namespace zypp { * Set logfile for progress log. Empty filename to disable logging. **/ static bool setInstallationLogfile( const Pathname & filename ); + + protected: + void doRemovePackage( const std::string & name_r, unsigned flags, RpmRemoveReport & report ); + void doInstallPackage( const Pathname & filename, unsigned flags, RpmInstallReport & report ); + const std::list & doGetPackages(ScanDbReport & report); + void doRebuildDatabase(RebuildDbReport & report); + }; @@ -539,4 +550,4 @@ namespace zypp { } // namespace target } // namespace zypp -#endif // RpmDb_h +#endif // ZYPP_TARGET_RPM_RPMDB_H diff --git a/zypp/target/rpm/RpmException.cc b/zypp/target/rpm/RpmException.cc new file mode 100644 index 0000000..341a4d6 --- /dev/null +++ b/zypp/target/rpm/RpmException.cc @@ -0,0 +1,77 @@ +/*---------------------------------------------------------------------\ +| ____ _ __ __ ___ | +| |__ / \ / / . \ . \ | +| / / \ V /| _/ _/ | +| / /__ | | | | | | | +| /_____||_| |_| |_| | +| | +\---------------------------------------------------------------------*/ +/** \file zypp/target/rpm/RpmException.cc + * +*/ + +#include +#include + +#include "zypp/target/rpm/RpmException.h" + +using namespace std; + +/////////////////////////////////////////////////////////////////// +namespace zypp +{ ///////////////////////////////////////////////////////////////// + namespace target { + ///////////////////////////////////////////////////////////////// + namespace rpm { + ///////////////////////////////////////////////////////////////// + + std::ostream & RpmInvalidRootException::dumpOn( std::ostream & str ) const + { + return str << "Illegal root " << _root + << " or dbPath " << _dbpath << endl; + } + + std::ostream & RpmAccessBlockedException::dumpOn( std::ostream & str ) const + { + return str << "Access is blocked: Root: " << _root + << " dbPath: " << _dbpath << endl; + } + + std::ostream & RpmSubprocessException::dumpOn( std::ostream & str ) const + { + return str << "Subprocess failed. Error: " << _errmsg << endl; + } + + std::ostream & RpmInitException::dumpOn( std::ostream & str) const + { + return str << "Failed to initialize database: Root: " << _root + << " dbPath: " << _dbpath << endl; + } + + std::ostream & RpmDbOpenException::dumpOn( std::ostream & str) const + { + return str << "Failed to open database: Root: " << _root + << " dbPath: " << _dbpath << endl; + } + + std::ostream & RpmDbAlreadyOpenException::dumpOn( std::ostream & str) const + { + return str << "Can't switch to " << _new_root << " " << _new_dbpath + << " while accessing " << _old_root << " " << _old_dbpath << endl; + } + + std::ostream & RpmDbNotOpenException::dumpOn( std::ostream & str) const + { + return str << "RPM database not open" << endl; + } + + std::ostream & MediaCurlInitException::dumpOn( std::ostream & str) const + { + return str << "Curl init failed for: " << _url << endl; + } + + ///////////////////////////////////////////////////////////////// + } // namespace rpm + } // namespace target +} // namespace zypp +/////////////////////////////////////////////////////////////////// diff --git a/zypp/target/rpm/RpmException.h b/zypp/target/rpm/RpmException.h new file mode 100644 index 0000000..c5ef0cd --- /dev/null +++ b/zypp/target/rpm/RpmException.h @@ -0,0 +1,198 @@ +/*---------------------------------------------------------------------\ +| ____ _ __ __ ___ | +| |__ / \ / / . \ . \ | +| / / \ V /| _/ _/ | +| / /__ | | | | | | | +| /_____||_| |_| |_| | +| | +\---------------------------------------------------------------------*/ +/** \file zypp/target/rpm/RpmException.h + * +*/ +#ifndef ZYPP_TARGET_RPM_RPMEXCEPTION_H +#define ZYPP_TARGET_RPM_RPMEXCEPTION_H + +#include + +#include + +#include "zypp/base/Exception.h" +#include "zypp/Pathname.h" +#include "zypp/Url.h" + +/////////////////////////////////////////////////////////////////// +namespace zypp +{ ///////////////////////////////////////////////////////////////// + namespace target { + /////////////////////////////////////////////////////////////// + namespace rpm { + /////////////////////////////////////////////////////////////// + // + // CLASS NAME : RpmException + /** Just inherits Exception to separate media exceptions + * + **/ + class RpmException : public Exception + { + public: + /** Ctor taking message. + * Use \ref ZYPP_THROW to throw exceptions. + */ + RpmException() + : Exception( "Rpm Exception" ) + {} + /** Ctor taking message. + * Use \ref ZYPP_THROW to throw exceptions. + */ + RpmException( const std::string & msg_r ) + : Exception( msg_r ) + {} + /** Dtor. */ + virtual ~RpmException() throw() {}; + }; + + class GlobalRpmInitException : public RpmException + { + public: + /** Ctor taking message. + * Use \ref ZYPP_THROW to throw exceptions. + */ + GlobalRpmInitException() + : RpmException("Global RPM initialization failed") + {} + /** Dtor. */ + virtual ~GlobalRpmInitException() throw() {}; + private: + }; + + class RpmInvalidRootException : public RpmException + { + public: + /** Ctor taking message. + * Use \ref ZYPP_THROW to throw exceptions. + */ + RpmInvalidRootException( const Pathname & root_r, + const Pathname & dbpath_r ) + : RpmException() + , _root(root_r.asString()) + , _dbpath(dbpath_r.asString()) + {} + /** Dtor. */ + virtual ~RpmInvalidRootException() throw() {}; + std::string root() const { return _root; } + std::string dbpath() const { return _dbpath; } + protected: + virtual std::ostream & dumpOn( std::ostream & str ) const; + private: + std::string _root; + std::string _dbpath; + }; + + class RpmAccessBlockedException : public RpmException + { + public: + RpmAccessBlockedException( const Pathname & root_r, + const Pathname & dbpath_r ) + : RpmException() + , _root(root_r.asString()) + , _dbpath(dbpath_r.asString()) + {} + virtual ~RpmAccessBlockedException() throw() {}; + std::string root() const { return _root; } + std::string dbpath() const { return _dbpath; } + protected: + virtual std::ostream & dumpOn( std::ostream & str ) const; + private: + std::string _root; + std::string _dbpath; + }; + + class RpmSubprocessException : public RpmException + { + public: + RpmSubprocessException(const std::string & errmsg_r) + : RpmException() + , _errmsg(errmsg_r) + {} + virtual ~RpmSubprocessException() throw() {}; + protected: + virtual std::ostream & dumpOn( std::ostream & str ) const; + private: + std::string _errmsg; + }; + + class RpmInitException : public RpmException + { + public: + RpmInitException(const Pathname & root_r, + const Pathname & dbpath_r) + : RpmException() + , _root(root_r.asString()) + , _dbpath(dbpath_r.asString()) + {} + virtual ~RpmInitException() throw() {}; + protected: + virtual std::ostream & dumpOn( std::ostream & str ) const; + private: + std::string _root; + std::string _dbpath; + }; + + class RpmDbOpenException : public RpmException + { + public: + RpmDbOpenException(const Pathname & root_r, + const Pathname & dbpath_r) + : RpmException() + , _root(root_r.asString()) + , _dbpath(dbpath_r.asString()) + {} + virtual ~RpmDbOpenException() throw() {}; + protected: + virtual std::ostream & dumpOn( std::ostream & str ) const; + private: + std::string _root; + std::string _dbpath; + }; + + class RpmDbAlreadyOpenException : public RpmException + { + public: + RpmDbAlreadyOpenException(const Pathname & old_root_r, + const Pathname & old_dbpath_r, + const Pathname & new_root_r, + const Pathname & new_dbpath_r) + : RpmException() + , _old_root(old_root_r.asString()) + , _old_dbpath(old_dbpath_r.asString()) + , _new_root(new_root_r.asString()) + , _new_dbpath(new_dbpath_r.asString()) + {} + virtual ~RpmDbAlreadyOpenException() throw() {}; + protected: + virtual std::ostream & dumpOn( std::ostream & str ) const; + private: + std::string _old_root; + std::string _old_dbpath; + std::string _new_root; + std::string _new_dbpath; + }; + + class RpmDbNotOpenException : public RpmException + { + public: + RpmDbNotOpenException() + : RpmException() + {} + virtual ~RpmDbNotOpenException() throw() {}; + protected: + virtual std::ostream & dumpOn( std::ostream & str ) const; + private: + }; + + ///////////////////////////////////////////////////////////////// + } // namespace rpm + } // namespace target +} // namespace zypp +/////////////////////////////////////////////////////////////////// +#endif // ZYPP_TARGET_RPM_RPMEXCEPTION_H diff --git a/zypp/target/rpm/RpmHeader.cc b/zypp/target/rpm/RpmHeader.cc index 7748f56..f8a8b0a 100644 --- a/zypp/target/rpm/RpmHeader.cc +++ b/zypp/target/rpm/RpmHeader.cc @@ -168,9 +168,9 @@ namespace zypp { // Edition RpmHeader::tag_edition() const { - return Edition( string_val ( RPMTAG_EPOCH ), - string_val( RPMTAG_VERSION ), - string_val( RPMTAG_RELEASE ) ); + return Edition( string_val( RPMTAG_VERSION ), + string_val( RPMTAG_RELEASE ), + string_val ( RPMTAG_EPOCH )); } /////////////////////////////////////////////////////////////////// diff --git a/zypp/target/rpm/librpm.h b/zypp/target/rpm/librpm.h index d330901..5ddd79d 100644 --- a/zypp/target/rpm/librpm.h +++ b/zypp/target/rpm/librpm.h @@ -1,25 +1,16 @@ /*---------------------------------------------------------------------\ +| ____ _ __ __ ___ | +| |__ / \ / / . \ . \ | +| / / \ V /| _/ _/ | +| / /__ | | | | | | | +| /_____||_| |_| |_| | | | -| __ __ ____ _____ ____ | -| \ \ / /_ _/ ___|_ _|___ \ | -| \ V / _` \___ \ | | __) | | -| | | (_| |___) || | / __/ | -| |_|\__,_|____/ |_| |_____| | -| | -| core system | -| (C) SuSE GmbH | -\----------------------------------------------------------------------/ - - File: librpm.h - - Author: Michael Andres - Maintainer: Michael Andres - - Purpose: - -/-*/ -#ifndef librpm_h -#define librpm_h +\---------------------------------------------------------------------*/ +/** \file zypp/target/rpm/librpm.h + * +*/ +#ifndef ZYPP_TARGET_RPM_LIBRPM_H +#define ZYPP_TARGET_RPM_LIBRPM_H extern "C" { #include @@ -29,4 +20,4 @@ extern "C" { #include } -#endif // librpm_h +#endif // ZYPP_TARGET_RPM_LIBRPM_H diff --git a/zypp/target/rpm/librpmDb.cc b/zypp/target/rpm/librpmDb.cc index 18598e9..56d8b8c 100644 --- a/zypp/target/rpm/librpmDb.cc +++ b/zypp/target/rpm/librpmDb.cc @@ -6,7 +6,7 @@ | /_____||_| |_| |_| | | | \---------------------------------------------------------------------*/ -/** \file zypp/target/rpm/BinHeader.h +/** \file zypp/target/rpm/librpmDb.cc * */ #include "librpm.h" @@ -16,7 +16,7 @@ #include "zypp/base/Logger.h" #include "zypp/target/rpm/librpmDb.h" #include "zypp/target/rpm/RpmHeader.h" -#include "zypp/base/Exception.h" +#include "zypp/target/rpm/RpmException.h" using namespace std; @@ -37,8 +37,7 @@ class librpmDb::D { const Pathname _root; // root directory for all operations const Pathname _dbPath; // directory (below root) that contains the rpmdb rpmdb _db; // database handle -#warning fix string representation of the error - shared_ptr _error; // database error + shared_ptr _error; // database error friend ostream & operator<<( ostream & str, const D & obj ) { str << "{" << obj._error << "(" << obj._root << ")" << obj._dbPath << "}"; @@ -62,21 +61,22 @@ class librpmDb::D { // init database int res = ::rpmdbInit( root, perms ); if ( res ) { - _error = shared_ptr(new Exception("Error::E_RpmDB_init_failed")); ERR << "rpmdbInit error(" << res << "): " << *this << endl; - return; + _error = shared_ptr(new RpmInitException(_root, _dbPath)); + ZYPP_THROW(*_error); } } // open database int res = ::rpmdbOpen( root, &_db, (readonly_r ? O_RDONLY : O_RDWR ), perms ); if ( res || !_db ) { - _error = shared_ptr(new Exception("Error::E_RpmDB_open_failed")); if ( _db ) { ::rpmdbClose( _db ); _db = 0; } ERR << "rpmdbOpen error(" << res << "): " << *this << endl; + _error = shared_ptr(new RpmDbOpenException(_root, _dbPath)); + ZYPP_THROW(*_error); return; } @@ -177,25 +177,25 @@ librpmDb * librpmDb::newLibrpmDb( Pathname root_r, Pathname dbPath_r, bool reado { // check arguments if ( ! (root_r.absolute() && dbPath_r.absolute()) ) { - ERR << "Illegal root or dbPath: " << stringPath( root_r, dbPath_r ) << endl; - ZYPP_THROW(Exception("Error::E_invalid_argument")); + ZYPP_THROW(RpmInvalidRootException(root_r, dbPath_r)); } // initialize librpm if ( ! globalInit() ) { - ZYPP_THROW(Exception("Error::E_RpmDB_global_init_failed")); + ZYPP_THROW(GlobalRpmInitException()); } // open rpmdb - librpmDb * ret = new librpmDb( root_r, dbPath_r, readonly_r ); - shared_ptr err_r = ret->_d._error; - if ( err_r ) { + librpmDb * ret = 0; + try { + ret = new librpmDb( root_r, dbPath_r, readonly_r ); + } + catch (const RpmException & excpt_r) + { + ZYPP_CAUGHT(excpt_r); delete ret; ret = 0; -#warning FIXME uncomment -#if 0 - ZYPP_THROW(ret->_d._error); -#endif + ZYPP_RETHROW(excpt_r); } return ret; } @@ -210,8 +210,7 @@ void librpmDb::dbAccess( const Pathname & root_r, const Pathname & dbPath_r ) { // check arguments if ( ! (root_r.absolute() && dbPath_r.absolute()) ) { - ERR << "Illegal root or dbPath: " << stringPath( root_r, dbPath_r ) << endl; - ZYPP_THROW(Exception("Error::E_invalid_argument")); + ZYPP_THROW(RpmInvalidRootException(root_r, dbPath_r)); } if ( _defaultDb ) { @@ -219,9 +218,7 @@ void librpmDb::dbAccess( const Pathname & root_r, const Pathname & dbPath_r ) if ( _defaultRoot == root_r && _defaultDbPath == dbPath_r ) return; else { - ERR << "Can't switch to " << stringPath( root_r, dbPath_r ) - << " while accessing " << stringPath( _defaultRoot, _defaultDbPath ) << endl; - ZYPP_THROW(Exception("Error::E_RpmDB_already_open")); + ZYPP_THROW(RpmDbAlreadyOpenException(_defaultRoot, _defaultDbPath, root_r, dbPath_r)); } } @@ -242,8 +239,7 @@ void librpmDb::dbAccess( const Pathname & root_r, const Pathname & dbPath_r ) void librpmDb::dbAccess() { if ( _dbBlocked ) { - WAR << "Access is blocked: " << stringPath( _defaultRoot, _defaultDbPath ) << endl; - ZYPP_THROW(Exception("Error::E_RpmDB_access_blocked")); + ZYPP_THROW(RpmAccessBlockedException(_defaultRoot, _defaultDbPath)); } if ( !_defaultDb ) { @@ -263,8 +259,9 @@ void librpmDb::dbAccess( librpmDb::constPtr & ptr_r ) try { dbAccess(); } - catch (Exception & excpt_r) + catch (const RpmException & excpt_r) { + ZYPP_CAUGHT(excpt_r); ptr_r = 0; ZYPP_RETHROW(excpt_r); } @@ -296,7 +293,8 @@ unsigned librpmDb::dbRelease( bool force_r ) DBG << "dbRelease: release" << (force_r && outstanding ? "(forced)" : "") << ", outstanding " << outstanding << endl; - _defaultDb->_d._error = shared_ptr(new Exception("Error::E_RpmDB_access_blocked")); // tag handle invalid + _defaultDb->_d._error = shared_ptr(new RpmAccessBlockedException(_defaultDb->_d._root, _defaultDb->_d._dbPath)); + // tag handle invalid _defaultDb = 0; break; } @@ -410,15 +408,13 @@ const Pathname & librpmDb::dbPath() const return _d._dbPath; } -#warning uncomment thsi function -#if 0 /////////////////////////////////////////////////////////////////// // // // METHOD NAME : librpmDb::error // METHOD TYPE : PMError // -PMError librpmDb::error() const +shared_ptr librpmDb::error() const { return _d._error; } @@ -433,7 +429,6 @@ bool librpmDb::empty() const { return( valid() && ! *db_const_iterator( this ) ); } -#endif /////////////////////////////////////////////////////////////////// // @@ -535,8 +530,7 @@ class librpmDb::db_const_iterator::D { public: librpmDb::constPtr _dbptr; -#warning use better type - string _dberr; + shared_ptr _dberr; RpmHeader::constPtr _hptr; rpmdbMatchIterator _mi; @@ -546,11 +540,13 @@ class librpmDb::db_const_iterator::D { , _mi( 0 ) { if ( !_dbptr ) { - // try to get librpmDb's default db -#warning FIXME uncomment -#if 0 - _dberr = librpmDb::dbAccess( _dbptr ); -#endif + try { + librpmDb::dbAccess( _dbptr ); + } + catch (const RpmException & excpt_r) + { + ZYPP_CAUGHT(excpt_r); + } if ( !_dbptr ) { WAR << "No database access: " << _dberr << endl; } @@ -586,14 +582,11 @@ class librpmDb::db_const_iterator::D { _mi = ::rpmdbFreeIterator( _mi ); _hptr = 0; } -#warning uncomment -#if 0 if ( _dbptr && _dbptr->error() ) { _dberr = _dbptr->error(); WAR << "Lost database access: " << _dberr << endl; _dbptr = 0; } -#endif return false; } @@ -711,22 +704,19 @@ const RpmHeader::constPtr & librpmDb::db_const_iterator::operator*() const return _d._hptr; } -#warning FIXME this function -#if 0 /////////////////////////////////////////////////////////////////// // // // METHOD NAME : librpmDb::db_const_iterator::dbError // METHOD TYPE : PMError // -PMError librpmDb::db_const_iterator::dbError() const +shared_ptr librpmDb::db_const_iterator::dbError() const { if ( _d._dbptr ) return _d._dbptr->error(); return _d._dberr; } -#endif /****************************************************************** ** diff --git a/zypp/target/rpm/librpmDb.h b/zypp/target/rpm/librpmDb.h index 6076760..7f2d81b 100644 --- a/zypp/target/rpm/librpmDb.h +++ b/zypp/target/rpm/librpmDb.h @@ -6,29 +6,9 @@ | /_____||_| |_| |_| | | | \---------------------------------------------------------------------*/ -/** \file zypp/target/rpm/ +/** \file zypp/target/rpm/librpmDb.h * */ -/*---------------------------------------------------------------------\ -| | -| __ __ ____ _____ ____ | -| \ \ / /_ _/ ___|_ _|___ \ | -| \ V / _` \___ \ | | __) | | -| | | (_| |___) || | / __/ | -| |_|\__,_|____/ |_| |_____| | -| | -| core system | -| (C) SuSE GmbH | -\----------------------------------------------------------------------/ - - File: librpmDb.h - - Author: Michael Andres - Maintainer: Michael Andres - - Purpose: Manage access to librpm database. - -/-*/ #ifndef librpmDb_h #define librpmDb_h @@ -40,6 +20,7 @@ #include "zypp/PathInfo.h" #include "zypp/Package.h" #include "zypp/target/rpm/RpmHeader.h" +#include "zypp/target/rpm/RpmException.h" namespace zypp { namespace target { @@ -63,7 +44,7 @@ namespace zypp { * librpmDb::constPtr. Currently we don't want to provide non const * handles, as the database is opened READONLY. * - * \throws Exception + * \throws RpmException * **/ static void dbAccess( librpmDb::Ptr & ptr_r ); @@ -103,7 +84,7 @@ namespace zypp { * For internal use. Pointer returned should immediately be * wrapped into librpmDb::Ptr. * - * \throws Exception + * \throws RpmException * **/ static librpmDb * newLibrpmDb( Pathname root_r, Pathname dbPath_r, bool readonly_r ); @@ -117,7 +98,7 @@ namespace zypp { * created, you'll have to explicitly call @ref dbRelease to close the * database. * - * \throws Exception + * \throws RpmException * **/ static void dbAccess(); @@ -167,7 +148,7 @@ namespace zypp { * It's not allowed to switch to another location while a database * is accessed. Use @ref dbRelease to force releasing the database first. * - * \throws Exception + * \throws RpmException * **/ static void dbAccess( const Pathname & root_r, const Pathname & dbPath_r ); @@ -178,7 +159,7 @@ namespace zypp { * it should not be used longer than necessary. Be prepared that the * handle might become invalid (see @ref dbRelease) later. * - * \throws Exception + * \throws RpmException * **/ static void dbAccess( librpmDb::constPtr & ptr_r ); @@ -283,20 +264,17 @@ namespace zypp { * @return This handles directory that contains the rpmdb. **/ const Pathname & dbPath() const; -#warning uncomment -#if 0 /** * Return any database error. Usg. if the database was * blocked by calling @ref dbRelease(true) or @ref blockAccess. **/ - PMError error() const; + shared_ptr error() const; /** * @return Whether **/ bool valid() const { return( ! error() ); } -#endif /** * @return True if handle is valid and database is empty. @@ -480,8 +458,6 @@ namespace zypp { **/ ~db_const_iterator(); -#warning FIXME this function -#if 0 /** * Return any database error. * @@ -489,8 +465,7 @@ namespace zypp { * dbError will immediately report this, but an already running * iteration will proceed to its end. Then the database is dropped. **/ - PMError dbError() const; -#endif + shared_ptr dbError() const; /** * Advance to next RpmHeader::constPtr. @@ -576,15 +551,7 @@ namespace zypp { * Commonly used by PMRpmPackageDataProvider. **/ bool findPackage( const std::string & name_r, const Edition & ed_r ); -#warning define if eneded -#if 0 - /** - * Abbr. for findPackage( which_r.name, which_r.edition ); - **/ - bool findPackage( const PkgNameEd & which_r ) { - return findPackage( which_r.name, which_r.edition ); - } -#endif + /** * Abbr. for findPackage( which_r->name(), which_r->edition() ); **/ -- 2.7.4