Imported Upstream version 14.45.0
[platform/upstream/libzypp.git] / zypp / target / RpmPostTransCollector.cc
index 6aa35ca..3014af9 100644 (file)
 
 #include "zypp/TmpPath.h"
 #include "zypp/PathInfo.h"
+#include "zypp/HistoryLog.h"
+#include "zypp/ZYppCallbacks.h"
 #include "zypp/ExternalProgram.h"
 #include "zypp/target/rpm/RpmHeader.h"
-
+#include "zypp/ZConfig.h"
 
 using std::endl;
 #undef ZYPP_BASE_LOGGER_LOGGROUP
@@ -61,7 +63,7 @@ namespace zypp
          script.autoCleanup( false );  // no autodelete; within a tmpdir
          {
            std::ofstream out( script.path().c_str() );
-           out << "# " << pkg->tag_posttransprog() << endl
+           out << "#! " << pkg->tag_posttransprog() << endl
                << pkg->tag_posttrans() << endl;
          }
          _scripts.push_back( script.path().basename() );
@@ -77,22 +79,44 @@ namespace zypp
          if ( _scripts.empty() )
            return;
 
-         Pathname noRootScriptDir( filesystem::TmpDir::defaultLocation() / tmpDir().basename() );
+         HistoryLog historylog;
+
+         Pathname noRootScriptDir( ZConfig::instance().update_scriptsPath() / tmpDir().basename() );
 
          for ( auto && script : _scripts )
          {
            MIL << "EXECUTE posttrans: " << script << endl;
             ExternalProgram prog( (noRootScriptDir/script).asString(), ExternalProgram::Stderr_To_Stdout, false, -1, true, _root );
+
+           str::Str collect;
            for( std::string line = prog.receiveLine(); ! line.empty(); line = prog.receiveLine() )
            {
              DBG << line;
+             collect << "    " << line;
            }
            int ret = prog.close();
-           if ( ret != 0 )
+           const std::string & scriptmsg( collect );
+
+           if ( ret != 0 || ! scriptmsg.empty() )
            {
-             ERR << "FAILED posttrans: (" << ret << ") " << script << endl;
-             // HistoryLog()
-             // continue on error?
+             const std::string & pkgident( script.substr( 0, script.size()-6 ) );      // strip tmp file suffix
+
+             if ( ! scriptmsg.empty() )
+             {
+               str::Str msg;
+               msg << "Output of " << pkgident << " %posttrans script:\n" << scriptmsg;
+               historylog.comment( msg, true /*timestamp*/);
+               JobReport::info( msg );
+             }
+
+             if ( ret != 0 )
+             {
+               str::Str msg;
+               msg << pkgident << " %posttrans script failed (returned " << ret << ")";
+               WAR << msg << endl;
+               historylog.comment( msg, true /*timestamp*/);
+               JobReport::warning( msg );
+             }
            }
          }
          _scripts.clear();
@@ -104,8 +128,20 @@ namespace zypp
          if ( _scripts.empty() )
            return;
 
+         HistoryLog historylog;
+
+         str::Str msg;
+         msg << "%posttrans scripts skipped while aborting:\n";
          for ( auto && script : _scripts )
-         { WAR << "UNEXECUTED posttrans: " << script << endl; }
+         {
+           const std::string & pkgident( script.substr( 0, script.size()-6 ) );        // strip tmp file suffix
+           WAR << "UNEXECUTED posttrans: " << script << endl;
+           msg << "    " << pkgident << "\n";
+         }
+
+         historylog.comment( msg, true /*timestamp*/);
+         JobReport::warning( msg );
+
          _scripts.clear();
        }
 
@@ -113,7 +149,7 @@ namespace zypp
        /** Lazy create tmpdir on demand. */
        Pathname tmpDir()
        {
-         if ( !_ptrTmpdir ) _ptrTmpdir.reset( new filesystem::TmpDir( _root / filesystem::TmpDir::defaultLocation(), "posttrans" ) );
+         if ( !_ptrTmpdir ) _ptrTmpdir.reset( new filesystem::TmpDir( _root / ZConfig::instance().update_scriptsPath(), "posttrans" ) );
          DBG << _ptrTmpdir->path() << endl;
          return _ptrTmpdir->path();
        }