Imported Upstream version 16.3.2
[platform/upstream/libzypp.git] / zypp / HistoryLog.cc
index 99d670f..310a4e9 100644 (file)
@@ -16,6 +16,7 @@
 #include "zypp/ZConfig.h"
 #include "zypp/base/String.h"
 #include "zypp/base/Logger.h"
+#include "zypp/base/IOStream.h"
 
 #include "zypp/PathInfo.h"
 #include "zypp/Date.h"
 using std::endl;
 using std::string;
 
-namespace
+namespace zypp
 {
-  inline string timestamp()
-  { return zypp::Date::now().form( HISTORY_LOG_DATE_FORMAT ); }
-
-  inline string userAtHostname()
+  namespace
   {
-    static char buf[256];
-    string result;
-    char * tmp = ::cuserid(buf);
-    if (tmp)
+    inline string timestamp()
+    { return zypp::Date::now().form( HISTORY_LOG_DATE_FORMAT ); }
+
+    inline string userAtHostname()
     {
-      result = string(tmp);
-      if (!::gethostname(buf, 255))
-        result += "@" + string(buf);
+      static char buf[256];
+      string result;
+      char * tmp = ::cuserid(buf);
+      if (tmp)
+      {
+       result = string(tmp);
+       if (!::gethostname(buf, 255))
+         result += "@" + string(buf);
+      }
+      return result;
     }
-    return result;
-  }
 
-  static std::string pidAndAppname()
-  {
-    static std::string _val;
-    if ( _val.empty() )
+    static std::string pidAndAppname()
     {
-      pid_t mypid = getpid();
-      zypp::Pathname p( "/proc/"+zypp::str::numstring(mypid)+"/exe" );
-      zypp::Pathname myname( zypp::filesystem::readlink( p ) );
+      static std::string _val;
+      if ( _val.empty() )
+      {
+       pid_t mypid = getpid();
+       zypp::Pathname p( "/proc/"+zypp::str::numstring(mypid)+"/exe" );
+       zypp::Pathname myname( zypp::filesystem::readlink( p ) );
 
-      _val += zypp::str::numstring(mypid);
-      _val += ":";
-      _val += myname.basename();
+       _val += zypp::str::numstring(mypid);
+       _val += ":";
+       _val += myname.basename();
+      }
+      return _val;
     }
-    return _val;
-  }
-}
 
-namespace zypp
-{
+    static std::string cmdline()
+    {
+      static std::string _val;
+      if ( _val.empty() )
+      {
+       pid_t mypid = getpid();
+       {
+         std::ifstream cmdlineStr( Pathname("/proc/"+zypp::str::numstring(mypid)+"/cmdline").c_str() );
+         char ch;
+         const char * sep = "'";
+         while ( cmdlineStr && cmdlineStr.get( ch ) )
+         {
+           if ( sep )
+           {
+             _val += sep;
+             sep = nullptr;
+           }
+           switch ( ch )
+           {
+             case '\0':        _val += '\''; sep = " '"; break;
+             case '\n':        _val += ' '; break;
+             case '\\':        _val += '\\'; _val += '\\'; break;
+             case '|':         _val += '\\'; _val += '|'; break;
+             default:          _val += ch; break;
+           }
+         }
+       }
+      }
+      return _val;
+    }
+  } // namespace
+
   namespace
   {
     const char         _sep = '|';
@@ -109,7 +141,7 @@ namespace zypp
       if ( !_refcnt )
         closeLog();
     }
-  }
+  } // namespace
 
   ///////////////////////////////////////////////////////////////////
   //
@@ -153,14 +185,14 @@ namespace zypp
 
   /////////////////////////////////////////////////////////////////////////
 
-  void HistoryLog::comment( const string & comment, bool timestamp )
+  void HistoryLog::comment( const string & comment, bool timestamp_r )
   {
     if (comment.empty())
       return;
 
     _log << "# ";
-    if ( timestamp )
-      _log << ::timestamp() << " ";
+    if ( timestamp_r )
+      _log << timestamp() << " ";
 
     const char * s = comment.c_str();
     const char * c = s;
@@ -185,6 +217,18 @@ namespace zypp
 
   /////////////////////////////////////////////////////////////////////////
 
+  void HistoryLog::stampCommand()
+  {
+    _log
+      << timestamp()                                                   // 1 timestamp
+      << _sep << HistoryActionID::STAMP_COMMAND.asString(true)         // 2 action
+      << _sep << userAtHostname()                                      // 3 requested by
+      << _sep << cmdline()                                             // 4 command
+      << _sep << str::escape(ZConfig::instance().userData(), _sep)     // 6 userdata
+      << endl;
+
+  }
+
   void HistoryLog::install( const PoolItem & pi )
   {
     const Package::constPtr p = asKind<Package>(pi.resolvable());
@@ -200,7 +244,7 @@ namespace zypp
 
     // ApplLow is what the solver selected on behalf of the user.
     if (pi.status().isByUser() || pi.status().isByApplLow() )
-      _log << _sep << userAtHostname();                                        // 6 reqested by
+      _log << _sep << userAtHostname();                                        // 6 requested by
     else if (pi.status().isByApplHigh())
       _log << _sep << pidAndAppname();
     else
@@ -229,7 +273,7 @@ namespace zypp
 
     // ApplLow is what the solver selected on behalf of the user.
     if ( pi.status().isByUser() || pi.status().isByApplLow() )
-      _log << _sep << userAtHostname();                                        // 6 reqested by
+      _log << _sep << userAtHostname();                                        // 6 requested by
     else if (pi.status().isByApplHigh())
       _log << _sep << pidAndAppname();
     else
@@ -248,7 +292,7 @@ namespace zypp
       << timestamp()                                                   // 1 timestamp
       << _sep << HistoryActionID::REPO_ADD.asString(true)              // 2 action
       << _sep << str::escape(repo.alias(), _sep)                       // 3 alias
-      << _sep << str::escape(repo.url().asString(), _sep)              // 4 primary URL
+      << _sep << *repo.baseUrlsBegin()                                 // 4 primary URL
       << _sep << str::escape(ZConfig::instance().userData(), _sep)     // 5 userdata
       << endl;
   }
@@ -278,13 +322,13 @@ namespace zypp
         << _sep << str::escape(ZConfig::instance().userData(), _sep)   // 5 userdata
         << endl;
     }
-    if ( oldrepo.url() != newrepo.url() )
+    if (*oldrepo.baseUrlsBegin() != *newrepo.baseUrlsBegin())
     {
       _log
         << timestamp()                                                 // 1 timestamp
         << _sep << HistoryActionID::REPO_CHANGE_URL.asString(true)     // 2 action
-        << _sep << str::escape(oldrepo.url().asString(), _sep)         // 3 old url
-        << _sep << str::escape(newrepo.url().asString(), _sep)         // 4 new url
+        << _sep << str::escape(oldrepo.alias(), _sep)                  // 3 old url
+        << _sep << *newrepo.baseUrlsBegin()                            // 4 new url
         << _sep << str::escape(ZConfig::instance().userData(), _sep)   // 5 userdata
         << endl;
     }