Imported Upstream version 15.0.0
[platform/upstream/libzypp.git] / zypp / target / TargetImpl.cc
index 92ff04e..edd1055 100644 (file)
 #include "zypp/base/IOStream.h"
 #include "zypp/base/Functional.h"
 #include "zypp/base/UserRequestException.h"
+#include "zypp/base/Json.h"
 
 #include "zypp/ZConfig.h"
+#include "zypp/ZYppFactory.h"
 
 #include "zypp/PoolItem.h"
 #include "zypp/ResObjects.h"
 #include "zypp/target/TargetCallbackReceiver.h"
 #include "zypp/target/rpm/librpmDb.h"
 #include "zypp/target/CommitPackageCache.h"
+#include "zypp/target/RpmPostTransCollector.h"
 
 #include "zypp/parser/ProductFileReader.h"
 
-#include "zypp/pool/GetResolvablesToInsDel.h"
-#include "zypp/solver/detail/Helper.h"
+#include "zypp/solver/detail/Testcase.h"
 
-#include "zypp/repo/DeltaCandidates.h"
-#include "zypp/repo/PackageProvider.h"
 #include "zypp/repo/SrcPackageProvider.h"
 
 #include "zypp/sat/Pool.h"
+#include "zypp/sat/Transaction.h"
+
+#include "zypp/PluginScript.h"
 
 using namespace std;
 
@@ -61,8 +64,359 @@ using namespace std;
 namespace zypp
 { /////////////////////////////////////////////////////////////////
   ///////////////////////////////////////////////////////////////////
+  namespace json
+  {
+    // Lazy via template specialisation / should switch to overloading
+
+    template<>
+    inline std::string toJSON( const ZYppCommitResult::TransactionStepList & steps_r )
+    {
+      using sat::Transaction;
+      json::Array ret;
+
+      for ( const Transaction::Step & step : steps_r )
+       // ignore implicit deletes due to obsoletes and non-package actions
+       if ( step.stepType() != Transaction::TRANSACTION_IGNORE )
+         ret.add( step );
+
+      return ret.asJSON();
+    }
+
+    /** See \ref commitbegin on page \ref plugin-commit for the specs. */
+    template<>
+    inline std::string toJSON( const sat::Transaction::Step & step_r )
+    {
+      static const std::string strType( "type" );
+      static const std::string strStage( "stage" );
+      static const std::string strSolvable( "solvable" );
+
+      static const std::string strTypeDel( "-" );
+      static const std::string strTypeIns( "+" );
+      static const std::string strTypeMul( "M" );
+
+      static const std::string strStageDone( "ok" );
+      static const std::string strStageFailed( "err" );
+
+      static const std::string strSolvableN( "n" );
+      static const std::string strSolvableE( "e" );
+      static const std::string strSolvableV( "v" );
+      static const std::string strSolvableR( "r" );
+      static const std::string strSolvableA( "a" );
+
+      using sat::Transaction;
+      json::Object ret;
+
+      switch ( step_r.stepType() )
+      {
+       case Transaction::TRANSACTION_IGNORE:   /*empty*/ break;
+       case Transaction::TRANSACTION_ERASE:    ret.add( strType, strTypeDel ); break;
+       case Transaction::TRANSACTION_INSTALL:  ret.add( strType, strTypeIns ); break;
+       case Transaction::TRANSACTION_MULTIINSTALL: ret.add( strType, strTypeMul ); break;
+      }
+
+      switch ( step_r.stepStage() )
+      {
+       case Transaction::STEP_TODO:            /*empty*/ break;
+       case Transaction::STEP_DONE:            ret.add( strStage, strStageDone ); break;
+       case Transaction::STEP_ERROR:           ret.add( strStage, strStageFailed ); break;
+      }
+
+      {
+       IdString ident;
+       Edition ed;
+       Arch arch;
+       if ( sat::Solvable solv = step_r.satSolvable() )
+       {
+         ident = solv.ident();
+         ed    = solv.edition();
+         arch  = solv.arch();
+       }
+       else
+       {
+         // deleted package; post mortem data stored in Transaction::Step
+         ident = step_r.ident();
+         ed    = step_r.edition();
+         arch  = step_r.arch();
+       }
+
+       json::Object s {
+         { strSolvableN, ident.asString() },
+         { strSolvableV, ed.version() },
+         { strSolvableR, ed.release() },
+         { strSolvableA, arch.asString() }
+       };
+       if ( Edition::epoch_t epoch = ed.epoch() )
+         s.add( strSolvableE, epoch );
+
+       ret.add( strSolvable, s );
+      }
+
+      return ret.asJSON();
+    }
+  } // namespace json
+  ///////////////////////////////////////////////////////////////////
+
+  ///////////////////////////////////////////////////////////////////
   namespace target
-  { /////////////////////////////////////////////////////////////////
+  {
+    ///////////////////////////////////////////////////////////////////
+    namespace
+    {
+      SolvIdentFile::Data getUserInstalledFromHistory( const Pathname & historyFile_r )
+      {
+       SolvIdentFile::Data onSystemByUserList;
+       // go and parse it: 'who' must constain an '@', then it was installed by user request.
+       // 2009-09-29 07:25:19|install|lirc-remotes|0.8.5-3.2|x86_64|root@opensuse|InstallationImage|a204211eb0...
+       std::ifstream infile( historyFile_r.c_str() );
+       for( iostr::EachLine in( infile ); in; in.next() )
+       {
+         const char * ch( (*in).c_str() );
+         // start with year
+         if ( *ch < '1' || '9' < *ch )
+           continue;
+         const char * sep1 = ::strchr( ch, '|' );      // | after date
+         if ( !sep1 )
+           continue;
+         ++sep1;
+         // if logs an install or delete
+         bool installs = true;
+         if ( ::strncmp( sep1, "install|", 8 ) )
+         {
+           if ( ::strncmp( sep1, "remove |", 8 ) )
+             continue; // no install and no remove
+             else
+               installs = false; // remove
+         }
+         sep1 += 8;                                    // | after what
+         // get the package name
+         const char * sep2 = ::strchr( sep1, '|' );    // | after name
+         if ( !sep2 || sep1 == sep2 )
+           continue;
+         (*in)[sep2-ch] = '\0';
+         IdString pkg( sep1 );
+         // we're done, if a delete
+         if ( !installs )
+         {
+           onSystemByUserList.erase( pkg );
+           continue;
+         }
+         // now guess whether user installed or not (3rd next field contains 'user@host')
+         if ( (sep1 = ::strchr( sep2+1, '|' ))         // | after version
+           && (sep1 = ::strchr( sep1+1, '|' ))         // | after arch
+           && (sep2 = ::strchr( sep1+1, '|' )) )       // | after who
+         {
+           (*in)[sep2-ch] = '\0';
+           if ( ::strchr( sep1+1, '@' ) )
+           {
+             // by user
+             onSystemByUserList.insert( pkg );
+             continue;
+           }
+         }
+       }
+       MIL << "onSystemByUserList found: " << onSystemByUserList.size() << endl;
+       return onSystemByUserList;
+      }
+    } // namespace
+    ///////////////////////////////////////////////////////////////////
+
+    /** Helper for commit plugin execution.
+     * \ingroup g_RAII
+     */
+    class CommitPlugins : private base::NonCopyable
+    {
+      public:
+       /** Default ctor: Empty plugin list */
+       CommitPlugins()
+       {}
+
+       /** Dtor: Send PLUGINEND message and close plugins. */
+       ~CommitPlugins()
+       {
+         if ( ! _scripts.empty() )
+           send( PluginFrame( "PLUGINEND" ) );
+         // ~PluginScript will disconnect all remaining plugins!
+       }
+
+       /** Whether no plugins are waiting */
+       bool empty() const
+       { return _scripts.empty(); }
+
+
+       /** Send \ref PluginFrame to all open plugins.
+        * Failed plugins are removed from the execution list.
+        */
+       void send( const PluginFrame & frame_r )
+       {
+         DBG << "+++++++++++++++ send " << frame_r << endl;
+         for ( auto it = _scripts.begin(); it != _scripts.end(); )
+         {
+           doSend( *it, frame_r );
+           if ( it->isOpen() )
+             ++it;
+           else
+             it = _scripts.erase( it );
+         }
+         DBG << "--------------- send " << frame_r << endl;
+       }
+
+       /** Find and launch plugins sending PLUGINSTART message.
+        *
+        * If \a path_r is a directory all executable files whithin are
+        * expected to be plugins. Otherwise \a path_r must point to an
+        * executable plugin.
+        */
+       void load( const Pathname & path_r )
+       {
+         PathInfo pi( path_r );
+         DBG << "+++++++++++++++ load " << pi << endl;
+         if ( pi.isDir() )
+         {
+           std::list<Pathname> entries;
+           if ( filesystem::readdir( entries, pi.path(), false ) != 0 )
+           {
+             WAR << "Plugin dir is not readable: " << pi << endl;
+             return;
+           }
+           for_( it, entries.begin(), entries.end() )
+           {
+             PathInfo pii( *it );
+             if ( pii.isFile() && pii.userMayRX() )
+               doLoad( pii );
+           }
+         }
+         else if ( pi.isFile() )
+         {
+           if ( pi.userMayRX() )
+             doLoad( pi );
+           else
+             WAR << "Plugin file is not executable: " << pi << endl;
+         }
+         else
+         {
+           WAR << "Plugin path is neither dir nor file: " << pi << endl;
+         }
+         DBG << "--------------- load " << pi << endl;
+       }
+
+      private:
+       /** Send \ref PluginFrame and expect valid answer (ACK|_ENOMETHOD).
+        * Upon invalid answer or error, close the plugin. and remove it from the
+        * execution list.
+        * \returns the received \ref PluginFrame (empty Frame upon Exception)
+        */
+       PluginFrame doSend( PluginScript & script_r, const PluginFrame & frame_r )
+       {
+         PluginFrame ret;
+
+         try {
+           script_r.send( frame_r );
+           ret = script_r.receive();
+         }
+         catch( const zypp::Exception & e )
+         { ZYPP_CAUGHT(e); }
+
+         if ( ! ( ret.isAckCommand() || ret.isEnomethodCommand() ) )
+         {
+           WAR << "Bad plugin response from " << script_r << endl;
+           WAR << dump(ret) << endl;
+           script_r.close();
+         }
+
+         return ret;
+       }
+
+       /** Launch a plugin sending PLUGINSTART message. */
+       void doLoad( const PathInfo & pi_r )
+       {
+         MIL << "Load plugin: " << pi_r << endl;
+         try {
+           PluginScript plugin( pi_r.path() );
+           plugin.open();
+
+           PluginFrame frame( "PLUGINBEGIN" );
+           if ( ZConfig::instance().hasUserData() )
+             frame.setHeader( "userdata", ZConfig::instance().userData() );
+
+           doSend( plugin, frame );    // closes on error
+           if ( plugin.isOpen() )
+             _scripts.push_back( plugin );
+         }
+         catch( const zypp::Exception & e )
+         {
+            WAR << "Failed to load plugin " << pi_r << endl;
+         }
+       }
+
+      private:
+       std::list<PluginScript> _scripts;
+    };
+
+    void testCommitPlugins( const Pathname & path_r ) // for testing only
+    {
+      USR << "+++++" << endl;
+      {
+       CommitPlugins pl;
+       pl.load( path_r );
+       USR << "=====" << endl;
+      }
+      USR << "-----" << endl;
+    }
+
+    ///////////////////////////////////////////////////////////////////
+    namespace
+    {
+      inline PluginFrame transactionPluginFrame( const std::string & command_r, ZYppCommitResult::TransactionStepList & steps_r )
+      {
+       return PluginFrame( command_r, json::Object {
+         { "TransactionStepList", steps_r }
+       }.asJSON() );
+      }
+    } // namespace
+    ///////////////////////////////////////////////////////////////////
+
+    /** \internal Manage writing a new testcase when doing an upgrade. */
+    void writeUpgradeTestcase()
+    {
+      unsigned toKeep( ZConfig::instance().solver_upgradeTestcasesToKeep() );
+      MIL << "Testcases to keep: " << toKeep << endl;
+      if ( !toKeep )
+        return;
+      Target_Ptr target( getZYpp()->getTarget() );
+      if ( ! target )
+      {
+        WAR << "No Target no Testcase!" << endl;
+        return;
+      }
+
+      std::string stem( "updateTestcase" );
+      Pathname dir( target->assertRootPrefix("/var/log/") );
+      Pathname next( dir / Date::now().form( stem+"-%Y-%m-%d-%H-%M-%S" ) );
+
+      {
+        std::list<std::string> content;
+        filesystem::readdir( content, dir, /*dots*/false );
+        std::set<std::string> cases;
+        for_( c, content.begin(), content.end() )
+        {
+          if ( str::startsWith( *c, stem ) )
+            cases.insert( *c );
+        }
+        if ( cases.size() >= toKeep )
+        {
+          unsigned toDel = cases.size() - toKeep + 1; // +1 for the new one
+          for_( c, cases.begin(), cases.end() )
+          {
+            filesystem::recursive_rmdir( dir/(*c) );
+            if ( ! --toDel )
+              break;
+          }
+        }
+      }
+
+      MIL << "Write new testcase " << next << endl;
+      getZYpp()->resolver()->createSolverTestcase( next.asString(), false/*no solving*/ );
+    }
 
     ///////////////////////////////////////////////////////////////////
     namespace
@@ -82,7 +436,7 @@ namespace zypp
                                                                  const Pathname & script_r,
                                                                  callback::SendReport<PatchScriptReport> & report_r )
       {
-        MIL << "Execute script " << PathInfo(script_r) << endl;
+        MIL << "Execute script " << PathInfo(Pathname::assertprefix( root_r,script_r)) << endl;
 
         HistoryLog historylog;
         historylog.comment(script_r.asString() + _(" executed"), /*timestamp*/true);
@@ -153,8 +507,10 @@ namespace zypp
         return false; // abort.
       }
 
-      /** Look for patch scripts named 'name-version-release-*' and
+      /** Look for update scripts named 'name-version-release-*' and
        *  execute them. Return \c false if \c ABORT was requested.
+       *
+       * \see http://en.opensuse.org/Software_Management/Code11/Scripts_and_Messages
        */
       bool RunUpdateScripts( const Pathname & root_r,
                              const Pathname & scriptsPath_r,
@@ -164,7 +520,7 @@ namespace zypp
         if ( checkPackages_r.empty() )
           return true; // no installed packages to check
 
-        MIL << "Looking for new patch scripts in (" <<  root_r << ")" << scriptsPath_r << endl;
+        MIL << "Looking for new update scripts in (" <<  root_r << ")" << scriptsPath_r << endl;
         Pathname scriptsDir( Pathname::assertprefix( root_r, scriptsPath_r ) );
         if ( ! PathInfo( scriptsDir ).isDir() )
           return true; // no script dir
@@ -176,33 +532,72 @@ namespace zypp
 
         // Now collect and execute all matching scripts.
         // On ABORT: at least log all outstanding scripts.
+        // - "name-version-release"
+        // - "name-version-release-*"
         bool abort = false;
+       std::map<std::string, Pathname> unify; // scripts <md5,path>
         for_( it, checkPackages_r.begin(), checkPackages_r.end() )
         {
-          std::string prefix( str::form( "%s-%s-", it->name().c_str(), it->edition().c_str() ) );
+          std::string prefix( str::form( "%s-%s", it->name().c_str(), it->edition().c_str() ) );
           for_( sit, scripts.begin(), scripts.end() )
           {
             if ( ! str::hasPrefix( *sit, prefix ) )
               continue;
 
+            if ( (*sit)[prefix.size()] != '\0' && (*sit)[prefix.size()] != '-' )
+              continue; // if not exact match it had to continue with '-'
+
             PathInfo script( scriptsDir / *sit );
-            if ( ! script.isFile() )
-              continue;
+            Pathname localPath( scriptsPath_r/(*sit) );        // without root prefix
+            std::string unifytag;                      // must not stay empty
+
+           if ( script.isFile() )
+           {
+             // Assert it's set executable, unify by md5sum.
+             filesystem::addmod( script.path(), 0500 );
+             unifytag = filesystem::md5sum( script.path() );
+           }
+           else if ( ! script.isExist() )
+           {
+             // Might be a dangling symlink, might be ok if we are in
+             // instsys (absolute symlink within the system below /mnt).
+             // readlink will tell....
+             unifytag = filesystem::readlink( script.path() ).asString();
+           }
+
+           if ( unifytag.empty() )
+             continue;
+
+           // Unify scripts
+           if ( unify[unifytag].empty() )
+           {
+             unify[unifytag] = localPath;
+           }
+           else
+           {
+             // translators: We may find the same script content in files with different names.
+             // Only the first occurence is executed, subsequent ones are skipped. It's a one-line
+             // message for a log file. Preferably start translation with "%s"
+             std::string msg( str::form(_("%s already executed as %s)"), localPath.asString().c_str(), unify[unifytag].c_str() ) );
+              MIL << "Skip update script: " << msg << endl;
+              HistoryLog().comment( msg, /*timestamp*/true );
+             continue;
+           }
 
             if ( abort || aborting_r )
             {
-              WAR << "Aborting: Skip patch script " << *sit << endl;
+              WAR << "Aborting: Skip update script " << *sit << endl;
               HistoryLog().comment(
-                  script.path().asString() + _(" execution skipped while aborting"),
+                  localPath.asString() + _(" execution skipped while aborting"),
                   /*timestamp*/true);
             }
             else
             {
-              MIL << "Found patch script " << *sit << endl;
+              MIL << "Found update script " << *sit << endl;
               callback::SendReport<PatchScriptReport> report;
               report->start( make<Package>( *it ), script.path() );
 
-              if ( ! executeScript( root_r, script.path(), report ) )
+              if ( ! executeScript( root_r, localPath, report ) ) // script path without root prefix!
                 abort = true; // requested abort.
             }
           }
@@ -210,64 +605,204 @@ namespace zypp
         return !abort;
       }
 
-      /////////////////////////////////////////////////////////////////
-    } // namespace
-    ///////////////////////////////////////////////////////////////////
+      ///////////////////////////////////////////////////////////////////
+      //
+      ///////////////////////////////////////////////////////////////////
 
-    /** Helper for PackageProvider queries during commit. */
-    struct QueryInstalledEditionHelper
-    {
-      bool operator()( const std::string & name_r,
-                       const Edition &     ed_r,
-                       const Arch &        arch_r ) const
+      inline void copyTo( std::ostream & out_r, const Pathname & file_r )
       {
-        rpm::librpmDb::db_const_iterator it;
-        for ( it.findByName( name_r ); *it; ++it )
+        std::ifstream infile( file_r.c_str() );
+        for( iostr::EachLine in( infile ); in; in.next() )
+        {
+          out_r << *in << endl;
+        }
+      }
+
+      inline std::string notificationCmdSubst( const std::string & cmd_r, const UpdateNotificationFile & notification_r )
+      {
+        std::string ret( cmd_r );
+#define SUBST_IF(PAT,VAL) if ( ret.find( PAT ) != std::string::npos ) ret = str::gsub( ret, PAT, VAL )
+        SUBST_IF( "%p", notification_r.solvable().asString() );
+        SUBST_IF( "%P", notification_r.file().asString() );
+#undef SUBST_IF
+        return ret;
+      }
+
+      void sendNotification( const Pathname & root_r,
+                             const UpdateNotifications & notifications_r )
+      {
+        if ( notifications_r.empty() )
+          return;
+
+        std::string cmdspec( ZConfig::instance().updateMessagesNotify() );
+        MIL << "Notification command is '" << cmdspec << "'" << endl;
+        if ( cmdspec.empty() )
+          return;
+
+        std::string::size_type pos( cmdspec.find( '|' ) );
+        if ( pos == std::string::npos )
+        {
+          ERR << "Can't send Notification: Missing 'format |' in command spec." << endl;
+          HistoryLog().comment( str::Str() << _("Error sending update message notification."), /*timestamp*/true );
+          return;
+        }
+
+        std::string formatStr( str::toLower( str::trim( cmdspec.substr( 0, pos ) ) ) );
+        std::string commandStr( str::trim( cmdspec.substr( pos + 1 ) ) );
+
+        enum Format { UNKNOWN, NONE, SINGLE, DIGEST, BULK };
+        Format format = UNKNOWN;
+        if ( formatStr == "none" )
+          format = NONE;
+        else if ( formatStr == "single" )
+          format = SINGLE;
+        else if ( formatStr == "digest" )
+          format = DIGEST;
+        else if ( formatStr == "bulk" )
+          format = BULK;
+        else
+        {
+          ERR << "Can't send Notification: Unknown format '" << formatStr << " |' in command spec." << endl;
+          HistoryLog().comment( str::Str() << _("Error sending update message notification."), /*timestamp*/true );
+         return;
+        }
+
+        // Take care: commands are ececuted chroot(root_r). The message file
+        // pathnames in notifications_r are local to root_r. For physical access
+        // to the file they need to be prefixed.
+
+        if ( format == NONE || format == SINGLE )
+        {
+          for_( it, notifications_r.begin(), notifications_r.end() )
           {
-            if ( arch_r == it->tag_arch()
-                 && ( ed_r == Edition::noedition || ed_r == it->tag_edition() ) )
+            std::vector<std::string> command;
+            if ( format == SINGLE )
+              command.push_back( "<"+Pathname::assertprefix( root_r, it->file() ).asString() );
+            str::splitEscaped( notificationCmdSubst( commandStr, *it ), std::back_inserter( command ) );
+
+            ExternalProgram prog( command, ExternalProgram::Stderr_To_Stdout, false, -1, true, root_r );
+            if ( true ) // Wait for feedback
+            {
+              for( std::string line = prog.receiveLine(); ! line.empty(); line = prog.receiveLine() )
               {
-                return true;
+                DBG << line;
               }
+              int ret = prog.close();
+              if ( ret != 0 )
+              {
+                ERR << "Notification command returned with error (" << ret << ")." << endl;
+                HistoryLog().comment( str::Str() << _("Error sending update message notification."), /*timestamp*/true );
+                return;
+              }
+            }
+          }
+        }
+        else if ( format == DIGEST || format == BULK )
+        {
+          filesystem::TmpFile tmpfile;
+          ofstream out( tmpfile.path().c_str() );
+          for_( it, notifications_r.begin(), notifications_r.end() )
+          {
+            if ( format == DIGEST )
+            {
+              out << it->file() << endl;
+            }
+            else if ( format == BULK )
+            {
+              copyTo( out << '\f', Pathname::assertprefix( root_r, it->file() ) );
+            }
+          }
+
+          std::vector<std::string> command;
+          command.push_back( "<"+tmpfile.path().asString() ); // redirect input
+          str::splitEscaped( notificationCmdSubst( commandStr, *notifications_r.begin() ), std::back_inserter( command ) );
+
+          ExternalProgram prog( command, ExternalProgram::Stderr_To_Stdout, false, -1, true, root_r );
+          if ( true ) // Wait for feedback otherwise the TmpFile goes out of scope.
+          {
+            for( std::string line = prog.receiveLine(); ! line.empty(); line = prog.receiveLine() )
+            {
+              DBG << line;
+            }
+            int ret = prog.close();
+            if ( ret != 0 )
+            {
+              ERR << "Notification command returned with error (" << ret << ")." << endl;
+              HistoryLog().comment( str::Str() << _("Error sending update message notification."), /*timestamp*/true );
+              return;
+            }
           }
-        return false;
+        }
+        else
+        {
+          INT << "Can't send Notification: Missing handler for 'format |' in command spec." << endl;
+          HistoryLog().comment( str::Str() << _("Error sending update message notification."), /*timestamp*/true );
+          return;
+        }
       }
-    };
 
-    /**
-     * \short Let the Source provide the package.
-     * \p pool_r \ref ResPool used to get candidates
-     * \p pi item to be commited
-    */
-    struct RepoProvidePackage
-    {
-      ResPool _pool;
-      repo::RepoMediaAccess &_access;
 
-      RepoProvidePackage( repo::RepoMediaAccess &access, ResPool pool_r )
-        : _pool(pool_r), _access(access)
+      /** Look for update messages named 'name-version-release-*' and
+       *  send notification according to \ref ZConfig::updateMessagesNotify.
+       *
+       * \see http://en.opensuse.org/Software_Management/Code11/Scripts_and_Messages
+       */
+      void RunUpdateMessages( const Pathname & root_r,
+                              const Pathname & messagesPath_r,
+                              const std::vector<sat::Solvable> & checkPackages_r,
+                              ZYppCommitResult & result_r )
       {
+        if ( checkPackages_r.empty() )
+          return; // no installed packages to check
 
-      }
+        MIL << "Looking for new update messages in (" <<  root_r << ")" << messagesPath_r << endl;
+        Pathname messagesDir( Pathname::assertprefix( root_r, messagesPath_r ) );
+        if ( ! PathInfo( messagesDir ).isDir() )
+          return; // no messages dir
 
-      ManagedFile operator()( const PoolItem & pi )
-      {
-        // Redirect PackageProvider queries for installed editions
-        // (in case of patch/delta rpm processing) to rpmDb.
-        repo::PackageProviderPolicy packageProviderPolicy;
-        packageProviderPolicy.queryInstalledCB( QueryInstalledEditionHelper() );
+        std::list<std::string> messages;
+        filesystem::readdir( messages, messagesDir, /*dots*/false );
+        if ( messages.empty() )
+          return; // no messages in message dir
+
+        // Now collect all matching messages in result and send them
+        // - "name-version-release"
+        // - "name-version-release-*"
+        HistoryLog historylog;
+        for_( it, checkPackages_r.begin(), checkPackages_r.end() )
+        {
+          std::string prefix( str::form( "%s-%s", it->name().c_str(), it->edition().c_str() ) );
+          for_( sit, messages.begin(), messages.end() )
+          {
+            if ( ! str::hasPrefix( *sit, prefix ) )
+              continue;
 
-        Package::constPtr p = asKind<Package>(pi.resolvable());
+            if ( (*sit)[prefix.size()] != '\0' && (*sit)[prefix.size()] != '-' )
+              continue; // if not exact match it had to continue with '-'
 
+            PathInfo message( messagesDir / *sit );
+            if ( ! message.isFile() || message.size() == 0 )
+              continue;
 
-        // Build a repository list for repos
-        // contributing to the pool
-        std::list<Repository> repos( _pool.knownRepositoriesBegin(), _pool.knownRepositoriesEnd() );
-        repo::DeltaCandidates deltas(repos, p->name());
-        repo::PackageProvider pkgProvider( _access, p, deltas, packageProviderPolicy );
-        return pkgProvider.providePackage();
+            MIL << "Found update message " << *sit << endl;
+            Pathname localPath( messagesPath_r/(*sit) ); // without root prefix
+            result_r.rUpdateMessages().push_back( UpdateNotificationFile( *it, localPath ) );
+            historylog.comment( str::Str() << _("New update message") << " " << localPath, /*timestamp*/true );
+          }
+        }
+        sendNotification( root_r, result_r.updateMessages() );
       }
-    };
+
+      /////////////////////////////////////////////////////////////////
+    } // namespace
+    ///////////////////////////////////////////////////////////////////
+
+    void XRunUpdateMessages( const Pathname & root_r,
+                             const Pathname & messagesPath_r,
+                             const std::vector<sat::Solvable> & checkPackages_r,
+                             ZYppCommitResult & result_r )
+    { RunUpdateMessages( root_r, messagesPath_r, checkPackages_r, result_r ); }
+
     ///////////////////////////////////////////////////////////////////
 
     IMPL_PTR_TYPE(TargetImpl);
@@ -290,7 +825,7 @@ namespace zypp
     TargetImpl::TargetImpl( const Pathname & root_r, bool doRebuild_r )
     : _root( root_r )
     , _requestedLocalesFile( home() / "RequestedLocales" )
-    , _softLocksFile( home() / "SoftLocks" )
+    , _autoInstalledFile( home() / "AutoInstalled" )
     , _hardLocksFile( Pathname::assertprefix( _root, ZConfig::instance().locksFile() ) )
     {
       _rpm.initDatabase( root_r, Pathname(), doRebuild_r );
@@ -299,36 +834,16 @@ namespace zypp
 
       createAnonymousId();
 
-
       MIL << "Initialized target on " << _root << endl;
     }
 
     /**
      * generates a random id using uuidgen
      */
-    static string generateRandomId()
+    static std::string generateRandomId()
     {
-      string id;
-      const char* argv[] =
-      {
-         "/usr/bin/uuidgen",
-         NULL
-      };
-
-      ExternalProgram prog( argv,
-                            ExternalProgram::Normal_Stderr,
-                            false, -1, true);
-      std::string line;
-      for(line = prog.receiveLine();
-          ! line.empty();
-          line = prog.receiveLine() )
-      {
-          MIL << line << endl;
-          id = line;
-          break;
-      }
-      prog.close();
-      return id;
+      std::ifstream uuidprovider( "/proc/sys/kernel/random/uuid" );
+      return iostr::getline( uuidprovider );
     }
 
     /**
@@ -438,25 +953,33 @@ namespace zypp
       MIL << "Targets closed" << endl;
     }
 
+    ///////////////////////////////////////////////////////////////////
+    //
+    // solv file handling
+    //
+    ///////////////////////////////////////////////////////////////////
+
+    Pathname TargetImpl::defaultSolvfilesPath() const
+    {
+      return Pathname::assertprefix( _root, ZConfig::instance().repoSolvfilesPath() / sat::Pool::instance().systemRepoAlias() );
+    }
+
     void TargetImpl::clearCache()
     {
-      Pathname base = Pathname::assertprefix( _root,
-                                              ZConfig::instance().repoSolvfilesPath() / sat::Pool::instance().systemRepoAlias() );
+      Pathname base = solvfilesPath();
       filesystem::recursive_rmdir( base );
     }
 
-    void TargetImpl::buildCache()
+    bool TargetImpl::buildCache()
     {
-      Pathname base = Pathname::assertprefix( _root,
-                                              ZConfig::instance().repoSolvfilesPath() / sat::Pool::instance().systemRepoAlias() );
+      Pathname base = solvfilesPath();
       Pathname rpmsolv       = base/"solv";
       Pathname rpmsolvcookie = base/"cookie";
 
       bool build_rpm_solv = true;
       // lets see if the rpm solv cache exists
 
-      RepoStatus rpmstatus( RepoStatus( _root/"/var/lib/rpm/Name" )
-                            && (_root/"/etc/products.d") );
+      RepoStatus rpmstatus( RepoStatus(_root/"var/lib/rpm/Name") && RepoStatus(_root/"etc/products.d") );
 
       bool solvexisted = PathInfo(rpmsolv).isExist();
       if ( solvexisted )
@@ -468,43 +991,75 @@ namespace zypp
         {
           RepoStatus status = RepoStatus::fromCookieFile(rpmsolvcookie);
           // now compare it with the rpm database
-          if ( status.checksum() == rpmstatus.checksum() )
-            build_rpm_solv = false;
-          MIL << "Read cookie: " << rpmsolvcookie << " says: "
-              << (build_rpm_solv ? "outdated" : "uptodate") << endl;
+          if ( status == rpmstatus )
+           build_rpm_solv = false;
+         MIL << "Read cookie: " << rpmsolvcookie << " says: "
+         << (build_rpm_solv ? "outdated" : "uptodate") << endl;
         }
       }
 
       if ( build_rpm_solv )
       {
-        // Take care we unlink the solvfile on exception
-        ManagedFile guard( base, filesystem::recursive_rmdir );
-
-        // if it does not exist yet, we better create it
+        // if the solvfile dir does not exist yet, we better create it
         filesystem::assert_dir( base );
 
+        Pathname oldSolvFile( solvexisted ? rpmsolv : Pathname() ); // to speedup rpmdb2solv
+
         filesystem::TmpFile tmpsolv( filesystem::TmpFile::makeSibling( rpmsolv ) );
-        if (!tmpsolv)
+        if ( !tmpsolv )
         {
+          // Can't create temporary solv file, usually due to insufficient permission
+          // (user query while @System solv needs refresh). If so, try switching
+          // to a location within zypps temp. space (will be cleaned at application end).
+
+          bool switchingToTmpSolvfile = false;
           Exception ex("Failed to cache rpm database.");
-          ex.remember(str::form(
-              "Cannot create temporary file under %s.", base.c_str()));
-          ZYPP_THROW(ex);
+          ex.remember(str::form("Cannot create temporary file under %s.", base.c_str()));
+
+          if ( ! solvfilesPathIsTemp() )
+          {
+            base = getZYpp()->tmpPath() / sat::Pool::instance().systemRepoAlias();
+            rpmsolv       = base/"solv";
+            rpmsolvcookie = base/"cookie";
+
+            filesystem::assert_dir( base );
+            tmpsolv = filesystem::TmpFile::makeSibling( rpmsolv );
+
+            if ( tmpsolv )
+            {
+              WAR << "Using a temporary solv file at " << base << endl;
+              switchingToTmpSolvfile = true;
+              _tmpSolvfilesPath = base;
+            }
+            else
+            {
+              ex.remember(str::form("Cannot create temporary file under %s.", base.c_str()));
+            }
+          }
+
+          if ( ! switchingToTmpSolvfile )
+          {
+            ZYPP_THROW(ex);
+          }
         }
 
+        // Take care we unlink the solvfile on exception
+        ManagedFile guard( base, filesystem::recursive_rmdir );
+
         std::ostringstream cmd;
         cmd << "rpmdb2solv";
         if ( ! _root.empty() )
           cmd << " -r '" << _root << "'";
-
+       cmd << " -X";   // autogenerate pattern/product/... from -package
+       cmd << " -A";   // autogenerate application pseudo packages
         cmd << " -p '" << Pathname::assertprefix( _root, "/etc/products.d" ) << "'";
 
-        if ( solvexisted )
-          cmd << " '" << rpmsolv << "'";
+        if ( ! oldSolvFile.empty() )
+          cmd << " '" << oldSolvFile << "'";
 
         cmd << "  > '" << tmpsolv.path() << "'";
 
-        MIL << "Executing: " << cmd << endl;
+        MIL << "Executing: " << cmd.str() << endl;
         ExternalProgram prog( cmd.str(), ExternalProgram::Stderr_To_Stdout );
 
         cmd << endl;
@@ -531,7 +1086,36 @@ namespace zypp
 
         // We keep it.
         guard.resetDispose();
+
+       // Finally send notification to plugins
+       // NOTE: quick hack looking for spacewalk plugin only
+       {
+         Pathname script( Pathname::assertprefix( _root, ZConfig::instance().pluginsPath()/"system/spacewalk" ) );
+         if ( PathInfo( script ).isX() )
+           try {
+             PluginScript spacewalk( script );
+             spacewalk.open();
+
+             PluginFrame notify( "PACKAGESETCHANGED" );
+             spacewalk.send( notify );
+
+             PluginFrame ret( spacewalk.receive() );
+             MIL << ret << endl;
+             if ( ret.command() == "ERROR" )
+               ret.writeTo( WAR ) << endl;
+           }
+           catch ( const Exception & excpt )
+           {
+             WAR << excpt.asUserHistory() << endl;
+           }
+       }
       }
+      return build_rpm_solv;
+    }
+
+    void TargetImpl::reload()
+    {
+        load( false );
     }
 
     void TargetImpl::unload()
@@ -541,23 +1125,32 @@ namespace zypp
         system.eraseFromPool();
     }
 
-
-    void TargetImpl::load()
+    void TargetImpl::load( bool force )
     {
-      buildCache();
+      bool newCache = buildCache();
+      MIL << "New cache built: " << (newCache?"true":"false") <<
+        ", force loading: " << (force?"true":"false") << endl;
 
       // now add the repos to the pool
       sat::Pool satpool( sat::Pool::instance() );
-      Pathname rpmsolv( Pathname::assertprefix( _root,
-                        ZConfig::instance().repoSolvfilesPath() / satpool.systemRepoAlias() / "solv" ) );
+      Pathname rpmsolv( solvfilesPath() / "solv" );
       MIL << "adding " << rpmsolv << " to pool(" << satpool.systemRepoAlias() << ")" << endl;
 
       // Providing an empty system repo, unload any old content
       Repository system( sat::Pool::instance().findSystemRepo() );
+
       if ( system && ! system.solvablesEmpty() )
       {
-        system.eraseFromPool(); // invalidates system
+        if ( newCache || force )
+        {
+          system.eraseFromPool(); // invalidates system
+        }
+        else
+        {
+          return;     // nothing to do
+        }
       }
+
       if ( ! system )
       {
         system = satpool.systemRepo();
@@ -565,6 +1158,7 @@ namespace zypp
 
       try
       {
+        MIL << "adding " << rpmsolv << " to system" << endl;
         system.addSolv( rpmsolv );
       }
       catch ( const Exception & exp )
@@ -576,6 +1170,7 @@ namespace zypp
 
         system.addSolv( rpmsolv );
       }
+      sat::Pool::instance().rootDir( _root );
 
       // (Re)Load the requested locales et al.
       // If the requested locales are empty, we leave the pool untouched
@@ -590,16 +1185,30 @@ namespace zypp
         }
       }
       {
-        SoftLocksFile::Data softLocks( _softLocksFile.data() );
-        if ( ! softLocks.empty() )
-        {
-          // Don't soft lock any installed item.
-          for_( it, system.solvablesBegin(), system.solvablesEnd() )
-          {
-            softLocks.erase( it->ident() );
-          }
-          ResPool::instance().setAutoSoftLocks( softLocks );
-        }
+       if ( ! PathInfo( _autoInstalledFile.file() ).isExist() )
+       {
+         // Initialize from history, if it does not exist
+         Pathname historyFile( Pathname::assertprefix( _root, ZConfig::instance().historyLogFile() ) );
+         if ( PathInfo( historyFile ).isExist() )
+         {
+           SolvIdentFile::Data onSystemByUser( getUserInstalledFromHistory( historyFile ) );
+           SolvIdentFile::Data onSystemByAuto;
+           for_( it, system.solvablesBegin(), system.solvablesEnd() )
+           {
+             IdString ident( (*it).ident() );
+             if ( onSystemByUser.find( ident ) == onSystemByUser.end() )
+               onSystemByAuto.insert( ident );
+           }
+           _autoInstalledFile.setData( onSystemByAuto );
+         }
+         // on the fly removed any obsolete SoftLocks file
+         filesystem::unlink( home() / "SoftLocks" );
+       }
+       // read from AutoInstalled file
+       sat::StringQueue q;
+       for ( const auto & idstr : _autoInstalledFile.data() )
+         q.push( idstr.id() );
+       satpool.setAutoInstalled( q );
       }
       if ( ZConfig::instance().apply_locks_file() )
       {
@@ -624,196 +1233,321 @@ namespace zypp
     ZYppCommitResult TargetImpl::commit( ResPool pool_r, const ZYppCommitPolicy & policy_rX )
     {
       // ----------------------------------------------------------------- //
+      ZYppCommitPolicy policy_r( policy_rX );
+
       // Fake outstanding YCP fix: Honour restriction to media 1
       // at installation, but install all remaining packages if post-boot.
-      ZYppCommitPolicy policy_r( policy_rX );
       if ( policy_r.restrictToMedia() > 1 )
         policy_r.allMedia();
+
+      if ( policy_r.downloadMode() == DownloadDefault ) {
+        if ( root() == "/" )
+          policy_r.downloadMode(DownloadInHeaps);
+        else
+          policy_r.downloadMode(DownloadAsNeeded);
+      }
+      // DownloadOnly implies dry-run.
+      else if ( policy_r.downloadMode() == DownloadOnly )
+        policy_r.dryRun( true );
       // ----------------------------------------------------------------- //
 
       MIL << "TargetImpl::commit(<pool>, " << policy_r << ")" << endl;
 
       ///////////////////////////////////////////////////////////////////
-      // Store non-package data:
+      // Compute transaction:
       ///////////////////////////////////////////////////////////////////
-      filesystem::assert_dir( home() );
-      // requested locales
-      _requestedLocalesFile.setLocales( pool_r.getRequestedLocales() );
-      // weak locks
+      ZYppCommitResult result( root() );
+      result.rTransaction() = pool_r.resolver().getTransaction();
+      result.rTransaction().order();
+      // steps: this is our todo-list
+      ZYppCommitResult::TransactionStepList & steps( result.rTransactionStepList() );
+      if ( policy_r.restrictToMedia() )
       {
-        SoftLocksFile::Data newdata;
-        pool_r.getActiveSoftLocks( newdata );
-        _softLocksFile.setData( newdata );
+       // Collect until the 1st package from an unwanted media occurs.
+        // Further collection could violate install order.
+       MIL << "Restrict to media number " << policy_r.restrictToMedia() << endl;
+       for_( it, result.transaction().begin(), result.transaction().end() )
+       {
+         if ( makeResObject( *it )->mediaNr() > 1 )
+           break;
+         steps.push_back( *it );
+       }
       }
-      // hard locks
-      if ( ZConfig::instance().apply_locks_file() )
+      else
       {
-        HardLocksFile::Data newdata;
-        pool_r.getHardLockQueries( newdata );
-        _hardLocksFile.setData( newdata );
+       result.rTransactionStepList().insert( steps.end(), result.transaction().begin(), result.transaction().end() );
       }
+      MIL << "Todo: " << result << endl;
 
       ///////////////////////////////////////////////////////////////////
-      // Process packages:
+      // Prepare execution of commit plugins:
       ///////////////////////////////////////////////////////////////////
-
-      DBG << "commit log file is set to: " << HistoryLog::fname() << endl;
-
-      ZYppCommitResult result;
-
-      TargetImpl::PoolItemList to_uninstall;
-      TargetImpl::PoolItemList to_install;
-      TargetImpl::PoolItemList to_srcinstall;
+      CommitPlugins commitPlugins;
+      if ( root() == "/" && ! policy_r.dryRun() )
       {
+       Pathname plugindir( Pathname::assertprefix( _root, ZConfig::instance().pluginsPath()/"commit" ) );
+       commitPlugins.load( plugindir );
+      }
+      if ( ! commitPlugins.empty() )
+       commitPlugins.send( transactionPluginFrame( "COMMITBEGIN", steps ) );
 
-        pool::GetResolvablesToInsDel
-        collect( pool_r, policy_r.restrictToMedia() ? pool::GetResolvablesToInsDel::ORDER_BY_MEDIANR
-                 : pool::GetResolvablesToInsDel::ORDER_BY_SOURCE );
-        MIL << "GetResolvablesToInsDel: " << endl << collect << endl;
-        to_uninstall.swap( collect._toDelete );
-        to_install.swap( collect._toInstall );
-        to_srcinstall.swap( collect._toSrcinstall );
+      ///////////////////////////////////////////////////////////////////
+      // Write out a testcase if we're in dist upgrade mode.
+      ///////////////////////////////////////////////////////////////////
+      if ( getZYpp()->resolver()->upgradeMode() )
+      {
+        if ( ! policy_r.dryRun() )
+        {
+          writeUpgradeTestcase();
+        }
+        else
+        {
+          DBG << "dryRun: Not writing upgrade testcase." << endl;
+        }
       }
 
-      if ( policy_r.restrictToMedia() )
+     ///////////////////////////////////////////////////////////////////
+      // Store non-package data:
+      ///////////////////////////////////////////////////////////////////
+      if ( ! policy_r.dryRun() )
+      {
+        filesystem::assert_dir( home() );
+        // requested locales
+        _requestedLocalesFile.setLocales( pool_r.getRequestedLocales() );
+       // autoinstalled
+        {
+         SolvIdentFile::Data newdata;
+         for ( sat::Queue::value_type id : result.rTransaction().autoInstalled() )
+           newdata.insert( IdString(id) );
+         _autoInstalledFile.setData( newdata );
+        }
+        // hard locks
+        if ( ZConfig::instance().apply_locks_file() )
+        {
+          HardLocksFile::Data newdata;
+          pool_r.getHardLockQueries( newdata );
+          _hardLocksFile.setData( newdata );
+        }
+      }
+      else
       {
-        MIL << "Restrict to media number " << policy_r.restrictToMedia() << endl;
+        DBG << "dryRun: Not stroring non-package data." << endl;
       }
 
       ///////////////////////////////////////////////////////////////////
       // First collect and display all messages
       // associated with patches to be installed.
       ///////////////////////////////////////////////////////////////////
-      for_( it, to_install.begin(), to_install.end() )
+      if ( ! policy_r.dryRun() )
       {
-        if ( ! isKind<Patch>(it->resolvable()) )
-          continue;
-        if ( ! it->status().isToBeInstalled() )
-          continue;
-
-        Patch::constPtr patch( asKind<Patch>(it->resolvable()) );
-        if ( ! patch->message().empty() )
+        for_( it, steps.begin(), steps.end() )
         {
-          MIL << "Show message for " << patch << endl;
-          callback::SendReport<target::PatchMessageReport> report;
-          if ( ! report->show( patch ) )
-          {
-            WAR << "commit aborted by the user" << endl;
-            ZYPP_THROW( TargetAbortedException( N_("Installation has been aborted as directed.") ) );
+         if ( ! it->satSolvable().isKind<Patch>() )
+           continue;
+
+         PoolItem pi( *it );
+          if ( ! pi.status().isToBeInstalled() )
+            continue;
+
+          Patch::constPtr patch( asKind<Patch>(pi.resolvable()) );
+         if ( ! patch ||patch->message().empty()  )
+           continue;
+
+         MIL << "Show message for " << patch << endl;
+         callback::SendReport<target::PatchMessageReport> report;
+         if ( ! report->show( patch ) )
+         {
+           WAR << "commit aborted by the user" << endl;
+           ZYPP_THROW( TargetAbortedException( N_("Installation has been aborted as directed.") ) );
           }
         }
       }
+      else
+      {
+        DBG << "dryRun: Not checking patch messages." << endl;
+      }
 
       ///////////////////////////////////////////////////////////////////
       // Remove/install packages.
       ///////////////////////////////////////////////////////////////////
-     commit ( to_uninstall, policy_r, pool_r );
-
-      if (policy_r.restrictToMedia() == 0)
-      {                        // commit all
-        result._remaining = commit( to_install, policy_r, pool_r );
-        result._srcremaining = commit( to_srcinstall, policy_r, pool_r );
-      }
-      else
+      DBG << "commit log file is set to: " << HistoryLog::fname() << endl;
+      if ( ! policy_r.dryRun() || policy_r.downloadMode() == DownloadOnly )
       {
-        TargetImpl::PoolItemList current_install;
-        TargetImpl::PoolItemList current_srcinstall;
+       // Prepare the package cache. Pass all items requiring download.
+        CommitPackageCache packageCache( root() );
+       packageCache.setCommitList( steps.begin(), steps.end() );
 
-        // Collect until the 1st package from an unwanted media occurs.
-        // Further collection could violate install order.
-        bool hitUnwantedMedia = false;
-        for (TargetImpl::PoolItemList::iterator it = to_install.begin(); it != to_install.end(); ++it)
+        bool miss = false;
+        if ( policy_r.downloadMode() != DownloadAsNeeded )
         {
-          ResObject::constPtr res( it->resolvable() );
-
-          if ( hitUnwantedMedia
-               || ( res->mediaNr() && res->mediaNr() != policy_r.restrictToMedia() ) )
-          {
-            hitUnwantedMedia = true;
-            result._remaining.push_back( *it );
-          }
-          else
+          // Preload the cache. Until now this means pre-loading all packages.
+          // Once DownloadInHeaps is fully implemented, this will change and
+          // we may actually have more than one heap.
+          for_( it, steps.begin(), steps.end() )
           {
-            current_install.push_back( *it );
+           switch ( it->stepType() )
+           {
+             case sat::Transaction::TRANSACTION_INSTALL:
+             case sat::Transaction::TRANSACTION_MULTIINSTALL:
+               // proceed: only install actionas may require download.
+               break;
+
+             default:
+               // next: no download for or non-packages and delete actions.
+               continue;
+               break;
+           }
+
+           PoolItem pi( *it );
+            if ( pi->isKind<Package>() || pi->isKind<SrcPackage>() )
+            {
+              ManagedFile localfile;
+              try
+              {
+               // TODO: unify packageCache.get for Package and SrcPackage
+               if ( pi->isKind<Package>() )
+               {
+                 localfile = packageCache.get( pi );
+               }
+               else if ( pi->isKind<SrcPackage>() )
+               {
+                 repo::RepoMediaAccess access;
+                 repo::SrcPackageProvider prov( access );
+                 localfile = prov.provideSrcPackage( pi->asKind<SrcPackage>() );
+               }
+               else
+               {
+                 INT << "Don't know howto cache: Neither Package nor SrcPackage: " << pi << endl;
+                 continue;
+               }
+                localfile.resetDispose(); // keep the package file in the cache
+              }
+              catch ( const AbortRequestException & exp )
+              {
+               it->stepStage( sat::Transaction::STEP_ERROR );
+                miss = true;
+                WAR << "commit cache preload aborted by the user" << endl;
+                ZYPP_THROW( TargetAbortedException( N_("Installation has been aborted as directed.") ) );
+                break;
+              }
+              catch ( const SkipRequestException & exp )
+              {
+                ZYPP_CAUGHT( exp );
+               it->stepStage( sat::Transaction::STEP_ERROR );
+                miss = true;
+                WAR << "Skipping cache preload package " << pi->asKind<Package>() << " in commit" << endl;
+                continue;
+              }
+              catch ( const Exception & exp )
+              {
+                // bnc #395704: missing catch causes abort.
+                // TODO see if packageCache fails to handle errors correctly.
+                ZYPP_CAUGHT( exp );
+               it->stepStage( sat::Transaction::STEP_ERROR );
+                miss = true;
+                INT << "Unexpected Error: Skipping cache preload package " << pi->asKind<Package>() << " in commit" << endl;
+                continue;
+              }
+            }
           }
+          packageCache.preloaded( true ); // try to avoid duplicate infoInCache CBs in commit
         }
 
-        TargetImpl::PoolItemList bad = commit( current_install, policy_r, pool_r );
-        result._remaining.insert(result._remaining.end(), bad.begin(), bad.end());
-
-        for (TargetImpl::PoolItemList::iterator it = to_srcinstall.begin(); it != to_srcinstall.end(); ++it)
+        if ( miss )
         {
-          Resolvable::constPtr res( it->resolvable() );
-          Package::constPtr pkg( asKind<Package>(res) );
-          if (pkg && policy_r.restrictToMedia() != pkg->mediaNr()) // check medianr for packages only
-          {
-            XXX << "Package " << *pkg << ", wrong media " << pkg->mediaNr() << endl;
-            result._srcremaining.push_back( *it );
-          }
-          else
-          {
-            current_srcinstall.push_back( *it );
-          }
+          ERR << "Some packages could not be provided. Aborting commit."<< endl;
         }
-        bad = commit( current_srcinstall, policy_r, pool_r );
-        result._srcremaining.insert(result._srcremaining.end(), bad.begin(), bad.end());
+        else
+       {
+         if ( ! policy_r.dryRun() )
+         {
+           // if cache is preloaded, check for file conflicts
+           commitFindFileConflicts( policy_r, result );
+           commit( policy_r, packageCache, result );
+         }
+         else
+         {
+           DBG << "dryRun/downloadOnly: Not installing/deleting anything." << endl;
+         }
+       }
+      }
+      else
+      {
+        DBG << "dryRun: Not downloading/installing/deleting anything." << endl;
       }
 
       ///////////////////////////////////////////////////////////////////
-      // Try to rebuild solv file while rpm database is still in cache.
+      // Send result to commit plugins:
       ///////////////////////////////////////////////////////////////////
-      buildCache();
+      if ( ! commitPlugins.empty() )
+       commitPlugins.send( transactionPluginFrame( "COMMITEND", steps ) );
+
+      ///////////////////////////////////////////////////////////////////
+      // Try to rebuild solv file while rpm database is still in cache
+      ///////////////////////////////////////////////////////////////////
+      if ( ! policy_r.dryRun() )
+      {
+        buildCache();
+      }
 
-      result._result = (to_install.size() - result._remaining.size());
       MIL << "TargetImpl::commit(<pool>, " << policy_r << ") returns: " << result << endl;
       return result;
     }
 
-
     ///////////////////////////////////////////////////////////////////
     //
     // COMMIT internal
     //
     ///////////////////////////////////////////////////////////////////
-    TargetImpl::PoolItemList
-    TargetImpl::commit( const TargetImpl::PoolItemList & items_r,
-                        const ZYppCommitPolicy & policy_r,
-                        const ResPool & pool_r )
+    void TargetImpl::commit( const ZYppCommitPolicy & policy_r,
+                            CommitPackageCache & packageCache_r,
+                            ZYppCommitResult & result_r )
     {
-      TargetImpl::PoolItemList remaining;
-      repo::RepoMediaAccess access;
-      MIL << "TargetImpl::commit(<list>" << policy_r << ")" << items_r.size() << endl;
+      // steps: this is our todo-list
+      ZYppCommitResult::TransactionStepList & steps( result_r.rTransactionStepList() );
+      MIL << "TargetImpl::commit(<list>" << policy_r << ")" << steps.size() << endl;
 
       bool abort = false;
+      RpmPostTransCollector postTransCollector( _root );
       std::vector<sat::Solvable> successfullyInstalledPackages;
+      TargetImpl::PoolItemList remaining;
 
-      // prepare the package cache.
-      RepoProvidePackage repoProvidePackage( access, pool_r );
-      CommitPackageCache packageCache( items_r.begin(), items_r.end(),
-                                       root() / "tmp", repoProvidePackage );
-
-      for ( TargetImpl::PoolItemList::const_iterator it = items_r.begin(); it != items_r.end(); it++ )
+      for_( step, steps.begin(), steps.end() )
       {
-        if ( (*it)->isKind<Package>() )
+       PoolItem citem( *step );
+       if ( step->stepType() == sat::Transaction::TRANSACTION_IGNORE )
+       {
+         if ( citem->isKind<Package>() )
+         {
+           // for packages this means being obsoleted (by rpm)
+           // thius no additional action is needed.
+           step->stepStage( sat::Transaction::STEP_DONE );
+           continue;
+         }
+       }
+
+        if ( citem->isKind<Package>() )
         {
-          Package::constPtr p = (*it)->asKind<Package>();
-          if (it->status().isToBeInstalled())
+          Package::constPtr p = citem->asKind<Package>();
+          if ( citem.status().isToBeInstalled() )
           {
             ManagedFile localfile;
             try
             {
-              localfile = packageCache.get( it );
+             localfile = packageCache_r.get( citem );
             }
             catch ( const AbortRequestException &e )
             {
               WAR << "commit aborted by the user" << endl;
               abort = true;
-              break;
+             step->stepStage( sat::Transaction::STEP_ERROR );
+             break;
             }
             catch ( const SkipRequestException &e )
             {
               ZYPP_CAUGHT( e );
               WAR << "Skipping package " << p << " in commit" << endl;
+             step->stepStage( sat::Transaction::STEP_ERROR );
               continue;
             }
             catch ( const Exception &e )
@@ -822,12 +1556,13 @@ namespace zypp
               // TODO see if packageCache fails to handle errors correctly.
               ZYPP_CAUGHT( e );
               INT << "Unexpected Error: Skipping package " << p << " in commit" << endl;
+             step->stepStage( sat::Transaction::STEP_ERROR );
               continue;
             }
 
 #warning Exception handling
             // create a installation progress report proxy
-            RpmInstallPackageReceiver progress( it->resolvable() );
+            RpmInstallPackageReceiver progress( citem.resolvable() );
             progress.connect(); // disconnected on destruction.
 
             bool success = false;
@@ -842,7 +1577,7 @@ namespace zypp
             flags |= rpm::RPMINST_NODEPS;
             flags |= rpm::RPMINST_FORCE;
             //
-            if (p->installOnly())          flags |= rpm::RPMINST_NOUPGRADE;
+            if (p->multiversionInstall())  flags |= rpm::RPMINST_NOUPGRADE;
             if (policy_r.dryRun())         flags |= rpm::RPMINST_TEST;
             if (policy_r.rpmExcludeDocs()) flags |= rpm::RPMINST_EXCLUDEDOCS;
             if (policy_r.rpmNoSignature()) flags |= rpm::RPMINST_NOSIGNATURE;
@@ -850,26 +1585,34 @@ namespace zypp
             try
             {
               progress.tryLevel( target::rpm::InstallResolvableReport::RPM_NODEPS_FORCE );
-              rpm().installPackage( localfile, flags );
-              HistoryLog().install(*it);
+             if ( postTransCollector.collectScriptFromPackage( localfile ) )
+               flags |= rpm::RPMINST_NOPOSTTRANS;
+             rpm().installPackage( localfile, flags );
+              HistoryLog().install(citem);
 
               if ( progress.aborted() )
               {
                 WAR << "commit aborted by the user" << endl;
+                localfile.resetDispose(); // keep the package file in the cache
                 abort = true;
+               step->stepStage( sat::Transaction::STEP_ERROR );
                 break;
               }
               else
               {
                 success = true;
+               step->stepStage( sat::Transaction::STEP_DONE );
               }
             }
             catch ( Exception & excpt_r )
             {
               ZYPP_CAUGHT(excpt_r);
+              localfile.resetDispose(); // keep the package file in the cache
+
               if ( policy_r.dryRun() )
               {
                 WAR << "dry run failed" << endl;
+               step->stepStage( sat::Transaction::STEP_ERROR );
                 break;
               }
               // else
@@ -882,20 +1625,20 @@ namespace zypp
               {
                 WAR << "Install failed" << endl;
               }
-              remaining.push_back( *it );
+              step->stepStage( sat::Transaction::STEP_ERROR );
               break; // stop
             }
 
             if ( success && !policy_r.dryRun() )
             {
-              it->status().resetTransact( ResStatus::USER );
-              // Remember to check this package for presence of patch scripts.
-              successfullyInstalledPackages.push_back( it->satSolvable() );
+              citem.status().resetTransact( ResStatus::USER );
+              successfullyInstalledPackages.push_back( citem.satSolvable() );
+             step->stepStage( sat::Transaction::STEP_DONE );
             }
           }
           else
           {
-            RpmRemovePackageReceiver progress( it->resolvable() );
+            RpmRemovePackageReceiver progress( citem.resolvable() );
             progress.connect(); // disconnected on destruction.
 
             bool success = false;
@@ -904,18 +1647,20 @@ namespace zypp
             if (policy_r.dryRun()) flags |= rpm::RPMINST_TEST;
             try
             {
-              rpm().removePackage( p, flags );
-              HistoryLog().remove(*it);
+             rpm().removePackage( p, flags );
+              HistoryLog().remove(citem);
 
               if ( progress.aborted() )
               {
                 WAR << "commit aborted by the user" << endl;
                 abort = true;
+               step->stepStage( sat::Transaction::STEP_ERROR );
                 break;
               }
               else
               {
                 success = true;
+               step->stepStage( sat::Transaction::STEP_DONE );
               }
             }
             catch (Exception & excpt_r)
@@ -925,14 +1670,17 @@ namespace zypp
               {
                 WAR << "commit aborted by the user" << endl;
                 abort = true;
+               step->stepStage( sat::Transaction::STEP_ERROR );
                 break;
               }
               // else
               WAR << "removal of " << p << " failed";
+             step->stepStage( sat::Transaction::STEP_ERROR );
             }
             if ( success && !policy_r.dryRun() )
             {
-              it->status().resetTransact( ResStatus::USER );
+              citem.status().resetTransact( ResStatus::USER );
+             step->stepStage( sat::Transaction::STEP_DONE );
             }
           }
         }
@@ -940,14 +1688,14 @@ namespace zypp
         {
           // Status is changed as the buddy package buddy
           // gets installed/deleted. Handle non-buddies only.
-          if ( ! it->buddy() )
+          if ( ! citem.buddy() )
           {
-            if ( (*it)->isKind<Product>() )
+            if ( citem->isKind<Product>() )
             {
-              Product::constPtr p = (*it)->asKind<Product>();
-              if ( it->status().isToBeInstalled() )
+              Product::constPtr p = citem->asKind<Product>();
+              if ( citem.status().isToBeInstalled() )
               {
-                ERR << "Can't install orphan product without release-package! " << (*it) << endl;
+                ERR << "Can't install orphan product without release-package! " << citem << endl;
               }
               else
               {
@@ -956,7 +1704,7 @@ namespace zypp
                 std::string referenceFilename( p->referenceFilename() );
                 if ( referenceFilename.empty() )
                 {
-                  ERR << "Can't remove orphan product without 'referenceFilename'! " << (*it) << endl;
+                  ERR << "Can't remove orphan product without 'referenceFilename'! " << citem << endl;
                 }
                 else
                 {
@@ -968,21 +1716,29 @@ namespace zypp
                 }
               }
             }
-            else if ( (*it)->isKind<SrcPackage>() && it->status().isToBeInstalled() )
+            else if ( citem->isKind<SrcPackage>() && citem.status().isToBeInstalled() )
             {
               // SrcPackage is install-only
-              SrcPackage::constPtr p = (*it)->asKind<SrcPackage>();
+              SrcPackage::constPtr p = citem->asKind<SrcPackage>();
               installSrcPackage( p );
             }
 
-            it->status().resetTransact( ResStatus::USER );
+            citem.status().resetTransact( ResStatus::USER );
+           step->stepStage( sat::Transaction::STEP_DONE );
           }
+
         }  // other resolvables
 
       } // for
 
-      // Check presence of patch scripts. If aborting, at least log
-      // omitted scripts.
+      // process all remembered posttrans scripts.
+      if ( !abort )
+       postTransCollector.executeScripts();
+      else
+       postTransCollector.discardScripts();
+
+      // Check presence of update scripts/messages. If aborting,
+      // at least log omitted scripts.
       if ( ! successfullyInstalledPackages.empty() )
       {
         if ( ! RunUpdateScripts( _root, ZConfig::instance().update_scriptsPath(),
@@ -991,16 +1747,21 @@ namespace zypp
           WAR << "Commit aborted by the user" << endl;
           abort = true;
         }
+        // send messages after scripts in case some script generates output,
+        // that should be kept in t %ghost message file.
+        RunUpdateMessages( _root, ZConfig::instance().update_messagesPath(),
+                          successfullyInstalledPackages,
+                          result_r );
       }
 
       if ( abort )
       {
         ZYPP_THROW( TargetAbortedException( N_("Installation has been aborted as directed.") ) );
       }
-
-     return remaining;
     }
 
+    ///////////////////////////////////////////////////////////////////
+
     rpm::RpmDb & TargetImpl::rpm()
     {
       return _rpm;
@@ -1018,20 +1779,56 @@ namespace zypp
     }
 
     ///////////////////////////////////////////////////////////////////
-
-    std::string TargetImpl::release() const
+    namespace
     {
-      std::ifstream suseRelease( (_root / "/etc/SuSE-release").c_str() );
-      for( iostr::EachLine in( suseRelease ); in; in.next() )
+      parser::ProductFileData baseproductdata( const Pathname & root_r )
       {
-        std::string line( str::trim( *in ) );
-        if ( ! line.empty() )
-          return line;
+       parser::ProductFileData ret;
+        PathInfo baseproduct( Pathname::assertprefix( root_r, "/etc/products.d/baseproduct" ) );
+
+        if ( baseproduct.isFile() )
+        {
+          try
+          {
+            ret = parser::ProductFileReader::scanFile( baseproduct.path() );
+          }
+          catch ( const Exception & excpt )
+          {
+            ZYPP_CAUGHT( excpt );
+          }
+        }
+       else if ( PathInfo( Pathname::assertprefix( root_r, "/etc/products.d" ) ).isDir() )
+       {
+         ERR << "baseproduct symlink is dangling or missing: " << baseproduct << endl;
+       }
+        return ret;
       }
 
-      return _("Unknown Distribution");
-    }
+      inline Pathname staticGuessRoot( const Pathname & root_r )
+      {
+        if ( root_r.empty() )
+        {
+          // empty root: use existing Target or assume "/"
+          Pathname ret ( ZConfig::instance().systemRoot() );
+          if ( ret.empty() )
+            return Pathname("/");
+          return ret;
+        }
+        return root_r;
+      }
 
+      inline std::string firstNonEmptyLineIn( const Pathname & file_r )
+      {
+        std::ifstream idfile( file_r.c_str() );
+        for( iostr::EachLine in( idfile ); in; in.next() )
+        {
+          std::string line( str::trim( *in ) );
+          if ( ! line.empty() )
+            return line;
+        }
+        return std::string();
+      }
+    } // namescpace
     ///////////////////////////////////////////////////////////////////
 
     Product::constPtr TargetImpl::baseProduct() const
@@ -1043,71 +1840,115 @@ namespace zypp
         if ( p->isTargetDistribution() )
           return p;
       }
-      return 0L;
+      return nullptr;
     }
 
-    namespace
+    LocaleSet TargetImpl::requestedLocales( const Pathname & root_r )
     {
-      parser::ProductFileData baseproductdata( const Pathname & root_r )
-      {
-        PathInfo baseproduct( Pathname::assertprefix( root_r, "/etc/products.d/baseproduct" ) );
-        if ( baseproduct.isFile() )
-        {
-          try
-          {
-            return parser::ProductFileReader::scanFile( baseproduct.path() );
-          }
-          catch ( const Exception & excpt )
-          {
-            ZYPP_CAUGHT( excpt );
-          }
-        }
-        return parser::ProductFileData();
-      }
-
+      const Pathname needroot( staticGuessRoot(root_r) );
+      const Target_constPtr target( getZYpp()->getTarget() );
+      if ( target && target->root() == needroot )
+       return target->requestedLocales();
+      return RequestedLocalesFile( home(needroot) / "RequestedLocales" ).locales();
     }
 
     std::string TargetImpl::targetDistribution() const
     { return baseproductdata( _root ).registerTarget(); }
+    // static version:
+    std::string TargetImpl::targetDistribution( const Pathname & root_r )
+    { return baseproductdata( staticGuessRoot(root_r) ).registerTarget(); }
 
     std::string TargetImpl::targetDistributionRelease() const
     { return baseproductdata( _root ).registerRelease(); }
+    // static version:
+    std::string TargetImpl::targetDistributionRelease( const Pathname & root_r )
+    { return baseproductdata( staticGuessRoot(root_r) ).registerRelease();}
+
+    std::string TargetImpl::targetDistributionFlavor() const
+    { return baseproductdata( _root ).registerFlavor(); }
+    // static version:
+    std::string TargetImpl::targetDistributionFlavor( const Pathname & root_r )
+    { return baseproductdata( staticGuessRoot(root_r) ).registerFlavor();}
+
+    Target::DistributionLabel TargetImpl::distributionLabel() const
+    {
+      Target::DistributionLabel ret;
+      parser::ProductFileData pdata( baseproductdata( _root ) );
+      ret.shortName = pdata.shortName();
+      ret.summary = pdata.summary();
+      return ret;
+    }
+    // static version:
+    Target::DistributionLabel TargetImpl::distributionLabel( const Pathname & root_r )
+    {
+      Target::DistributionLabel ret;
+      parser::ProductFileData pdata( baseproductdata( staticGuessRoot(root_r) ) );
+      ret.shortName = pdata.shortName();
+      ret.summary = pdata.summary();
+      return ret;
+    }
 
     std::string TargetImpl::distributionVersion() const
     {
       if ( _distributionVersion.empty() )
       {
-        _distributionVersion = baseproductdata( _root ).edition().version();
+        _distributionVersion = TargetImpl::distributionVersion(root());
         if ( !_distributionVersion.empty() )
           MIL << "Remember distributionVersion = '" << _distributionVersion << "'" << endl;
       }
       return _distributionVersion;
     }
-
-    std::string TargetImpl::distributionFlavor() const
+    // static version
+    std::string TargetImpl::distributionVersion( const Pathname & root_r )
     {
-        std::ifstream idfile( ( home() / "LastDistributionFlavor" ).c_str() );
-        for( iostr::EachLine in( idfile ); in; in.next() )
+      std::string distributionVersion = baseproductdata( staticGuessRoot(root_r) ).edition().version();
+      if ( distributionVersion.empty() )
+      {
+        // ...But the baseproduct method is not expected to work on RedHat derivatives.
+        // On RHEL, Fedora and others the "product version" is determined by the first package
+        // providing 'redhat-release'. This value is not hardcoded in YUM and can be configured
+        // with the $distroverpkg variable.
+        scoped_ptr<rpm::RpmDb> tmprpmdb;
+        if ( ZConfig::instance().systemRoot() == Pathname() )
         {
-            std::string line( str::trim( *in ) );
-            if ( ! line.empty() )
-                return line;
+          try
+          {
+              tmprpmdb.reset( new rpm::RpmDb );
+              tmprpmdb->initDatabase( /*default ctor uses / but no additional keyring exports */ );
+          }
+          catch( ... )
+          {
+            return "";
+          }
         }
-        return std::string();
+        rpm::librpmDb::db_const_iterator it;
+        if ( it.findByProvides( ZConfig::instance().distroverpkg() ) )
+          distributionVersion = it->tag_version();
+      }
+      return distributionVersion;
+    }
+
+
+    std::string TargetImpl::distributionFlavor() const
+    {
+      return firstNonEmptyLineIn( home() / "LastDistributionFlavor" );
+    }
+    // static version:
+    std::string TargetImpl::distributionFlavor( const Pathname & root_r )
+    {
+      return firstNonEmptyLineIn( staticGuessRoot(root_r) / "/var/lib/zypp/LastDistributionFlavor" );
     }
 
     ///////////////////////////////////////////////////////////////////
 
     std::string TargetImpl::anonymousUniqueId() const
     {
-        std::ifstream idfile( ( home() / "AnonymousUniqueId" ).c_str() );
-        for( iostr::EachLine in( idfile ); in; in.next() )
-        {
-            std::string line( str::trim( *in ) );
-            if ( ! line.empty() )
-                return line;
-        }
-        return std::string();
+      return firstNonEmptyLineIn( home() / "AnonymousUniqueId" );
+    }
+    // static version:
+    std::string TargetImpl::anonymousUniqueId( const Pathname & root_r )
+    {
+      return firstNonEmptyLineIn( staticGuessRoot(root_r) / "/var/lib/zypp/AnonymousUniqueId" );
     }
 
     ///////////////////////////////////////////////////////////////////
@@ -1115,14 +1956,19 @@ namespace zypp
     void TargetImpl::installSrcPackage( const SrcPackage_constPtr & srcPackage_r )
     {
       // provide on local disk
-      repo::RepoMediaAccess access_r;
-      repo::SrcPackageProvider prov( access_r );
-      ManagedFile localfile = prov.provideSrcPackage( srcPackage_r );
+      ManagedFile localfile = provideSrcPackage(srcPackage_r);
       // install it
       rpm().installPackage ( localfile );
     }
 
-    /////////////////////////////////////////////////////////////////
+    ManagedFile TargetImpl::provideSrcPackage( const SrcPackage_constPtr & srcPackage_r )
+    {
+      // provide on local disk
+      repo::RepoMediaAccess access_r;
+      repo::SrcPackageProvider prov( access_r );
+      return prov.provideSrcPackage( srcPackage_r );
+    }
+    ////////////////////////////////////////////////////////////////
   } // namespace target
   ///////////////////////////////////////////////////////////////////
   /////////////////////////////////////////////////////////////////